mei: me: add cannon point device ids
[linux/fpc-iii.git] / drivers / staging / speakup / serialio.c
blob9cfc8142a31878b522018c72084a23284f02298b
1 #include <linux/interrupt.h>
2 #include <linux/ioport.h>
4 #include "spk_types.h"
5 #include "speakup.h"
6 #include "spk_priv.h"
7 #include "serialio.h"
9 #include <linux/serial_core.h>
10 /* WARNING: Do not change this to <linux/serial.h> without testing that
11 * SERIAL_PORT_DFNS does get defined to the appropriate value.
13 #include <asm/serial.h>
15 #ifndef SERIAL_PORT_DFNS
16 #define SERIAL_PORT_DFNS
17 #endif
19 static void start_serial_interrupt(int irq);
21 static const struct old_serial_port rs_table[] = {
22 SERIAL_PORT_DFNS
25 static const struct old_serial_port *serstate;
26 static int timeouts;
28 static int spk_serial_out(struct spk_synth *in_synth, const char ch);
29 static void spk_serial_send_xchar(char ch);
30 static void spk_serial_tiocmset(unsigned int set, unsigned int clear);
31 static unsigned char spk_serial_in(void);
32 static unsigned char spk_serial_in_nowait(void);
33 static void spk_serial_flush_buffer(void);
35 struct spk_io_ops spk_serial_io_ops = {
36 .synth_out = spk_serial_out,
37 .send_xchar = spk_serial_send_xchar,
38 .tiocmset = spk_serial_tiocmset,
39 .synth_in = spk_serial_in,
40 .synth_in_nowait = spk_serial_in_nowait,
41 .flush_buffer = spk_serial_flush_buffer,
43 EXPORT_SYMBOL_GPL(spk_serial_io_ops);
45 const struct old_serial_port *spk_serial_init(int index)
47 int baud = 9600, quot = 0;
48 unsigned int cval = 0;
49 int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8;
50 const struct old_serial_port *ser;
51 int err;
53 if (index >= ARRAY_SIZE(rs_table)) {
54 pr_info("no port info for ttyS%d\n", index);
55 return NULL;
57 ser = rs_table + index;
59 /* Divisor, bytesize and parity */
60 quot = ser->baud_base / baud;
61 cval = cflag & (CSIZE | CSTOPB);
62 #if defined(__powerpc__) || defined(__alpha__)
63 cval >>= 8;
64 #else /* !__powerpc__ && !__alpha__ */
65 cval >>= 4;
66 #endif /* !__powerpc__ && !__alpha__ */
67 if (cflag & PARENB)
68 cval |= UART_LCR_PARITY;
69 if (!(cflag & PARODD))
70 cval |= UART_LCR_EPAR;
71 if (synth_request_region(ser->port, 8)) {
72 /* try to take it back. */
73 pr_info("Ports not available, trying to steal them\n");
74 __release_region(&ioport_resource, ser->port, 8);
75 err = synth_request_region(ser->port, 8);
76 if (err) {
77 pr_warn("Unable to allocate port at %x, errno %i",
78 ser->port, err);
79 return NULL;
83 /* Disable UART interrupts, set DTR and RTS high
84 * and set speed.
86 outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
87 outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
88 outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
89 outb(cval, ser->port + UART_LCR); /* reset DLAB */
91 /* Turn off Interrupts */
92 outb(0, ser->port + UART_IER);
93 outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
95 /* If we read 0xff from the LSR, there is no UART here. */
96 if (inb(ser->port + UART_LSR) == 0xff) {
97 synth_release_region(ser->port, 8);
98 serstate = NULL;
99 return NULL;
102 mdelay(1);
103 speakup_info.port_tts = ser->port;
104 serstate = ser;
106 start_serial_interrupt(ser->irq);
108 return ser;
111 static irqreturn_t synth_readbuf_handler(int irq, void *dev_id)
113 unsigned long flags;
114 int c;
116 spin_lock_irqsave(&speakup_info.spinlock, flags);
117 while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) {
118 c = inb_p(speakup_info.port_tts + UART_RX);
119 synth->read_buff_add((u_char)c);
121 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
122 return IRQ_HANDLED;
125 static void start_serial_interrupt(int irq)
127 int rv;
129 if (!synth->read_buff_add)
130 return;
132 rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
133 "serial", (void *)synth_readbuf_handler);
135 if (rv)
136 pr_err("Unable to request Speakup serial I R Q\n");
137 /* Set MCR */
138 outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2,
139 speakup_info.port_tts + UART_MCR);
140 /* Turn on Interrupts */
141 outb(UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI,
142 speakup_info.port_tts + UART_IER);
143 inb(speakup_info.port_tts + UART_LSR);
144 inb(speakup_info.port_tts + UART_RX);
145 inb(speakup_info.port_tts + UART_IIR);
146 inb(speakup_info.port_tts + UART_MSR);
147 outb(1, speakup_info.port_tts + UART_FCR); /* Turn FIFO On */
150 static void spk_serial_send_xchar(char ch)
152 int timeout = SPK_XMITR_TIMEOUT;
154 while (spk_serial_tx_busy()) {
155 if (!--timeout)
156 break;
157 udelay(1);
159 outb(ch, speakup_info.port_tts);
162 static void spk_serial_tiocmset(unsigned int set, unsigned int clear)
164 int old = inb(speakup_info.port_tts + UART_MCR);
166 outb((old & ~clear) | set, speakup_info.port_tts + UART_MCR);
169 int spk_serial_synth_probe(struct spk_synth *synth)
171 const struct old_serial_port *ser;
172 int failed = 0;
174 if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) {
175 ser = spk_serial_init(synth->ser);
176 if (!ser) {
177 failed = -1;
178 } else {
179 outb_p(0, ser->port);
180 mdelay(1);
181 outb_p('\r', ser->port);
183 } else {
184 failed = -1;
185 pr_warn("ttyS%i is an invalid port\n", synth->ser);
187 if (failed) {
188 pr_info("%s: not found\n", synth->long_name);
189 return -ENODEV;
191 pr_info("%s: ttyS%i, Driver Version %s\n",
192 synth->long_name, synth->ser, synth->version);
193 synth->alive = 1;
194 return 0;
196 EXPORT_SYMBOL_GPL(spk_serial_synth_probe);
198 void spk_stop_serial_interrupt(void)
200 if (speakup_info.port_tts == 0)
201 return;
203 if (!synth->read_buff_add)
204 return;
206 /* Turn off interrupts */
207 outb(0, speakup_info.port_tts + UART_IER);
208 /* Free IRQ */
209 free_irq(serstate->irq, (void *)synth_readbuf_handler);
211 EXPORT_SYMBOL_GPL(spk_stop_serial_interrupt);
213 int spk_wait_for_xmitr(struct spk_synth *in_synth)
215 int tmout = SPK_XMITR_TIMEOUT;
217 if ((in_synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) {
218 pr_warn("%s: too many timeouts, deactivating speakup\n",
219 in_synth->long_name);
220 in_synth->alive = 0;
221 /* No synth any more, so nobody will restart TTYs, and we thus
222 * need to do it ourselves. Now that there is no synth we can
223 * let application flood anyway
225 speakup_start_ttys();
226 timeouts = 0;
227 return 0;
229 while (spk_serial_tx_busy()) {
230 if (--tmout == 0) {
231 pr_warn("%s: timed out (tx busy)\n",
232 in_synth->long_name);
233 timeouts++;
234 return 0;
236 udelay(1);
238 tmout = SPK_CTS_TIMEOUT;
239 while (!((inb_p(speakup_info.port_tts + UART_MSR)) & UART_MSR_CTS)) {
240 /* CTS */
241 if (--tmout == 0) {
242 timeouts++;
243 return 0;
245 udelay(1);
247 timeouts = 0;
248 return 1;
251 static unsigned char spk_serial_in(void)
253 int tmout = SPK_SERIAL_TIMEOUT;
255 while (!(inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR)) {
256 if (--tmout == 0) {
257 pr_warn("time out while waiting for input.\n");
258 return 0xff;
260 udelay(1);
262 return inb_p(speakup_info.port_tts + UART_RX);
265 static unsigned char spk_serial_in_nowait(void)
267 unsigned char lsr;
269 lsr = inb_p(speakup_info.port_tts + UART_LSR);
270 if (!(lsr & UART_LSR_DR))
271 return 0;
272 return inb_p(speakup_info.port_tts + UART_RX);
275 static void spk_serial_flush_buffer(void)
277 /* TODO: flush the UART 16550 buffer */
280 static int spk_serial_out(struct spk_synth *in_synth, const char ch)
282 if (in_synth->alive && spk_wait_for_xmitr(in_synth)) {
283 outb_p(ch, speakup_info.port_tts);
284 return 1;
286 return 0;
289 const char *spk_serial_synth_immediate(struct spk_synth *synth,
290 const char *buff)
292 u_char ch;
294 while ((ch = *buff)) {
295 if (ch == '\n')
296 ch = synth->procspeech;
297 if (spk_wait_for_xmitr(synth))
298 outb(ch, speakup_info.port_tts);
299 else
300 return buff;
301 buff++;
303 return NULL;
305 EXPORT_SYMBOL_GPL(spk_serial_synth_immediate);
307 void spk_serial_release(void)
309 spk_stop_serial_interrupt();
310 if (speakup_info.port_tts == 0)
311 return;
312 synth_release_region(speakup_info.port_tts, 8);
313 speakup_info.port_tts = 0;
315 EXPORT_SYMBOL_GPL(spk_serial_release);