Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / speakup / serialio.c
blob7f3d87bf592739821ca05e72b9beb4c52c254605
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 static void start_serial_interrupt(int irq);
11 static struct serial_state rs_table[] = {
12 SERIAL_PORT_DFNS
14 static struct serial_state *serstate;
15 static int timeouts;
17 struct serial_state *spk_serial_init(int index)
19 int baud = 9600, quot = 0;
20 unsigned int cval = 0;
21 int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8;
22 struct serial_state *ser = NULL;
23 int err;
25 ser = rs_table + index;
26 /* Divisor, bytesize and parity */
27 quot = ser->baud_base / baud;
28 cval = cflag & (CSIZE | CSTOPB);
29 #if defined(__powerpc__) || defined(__alpha__)
30 cval >>= 8;
31 #else /* !__powerpc__ && !__alpha__ */
32 cval >>= 4;
33 #endif /* !__powerpc__ && !__alpha__ */
34 if (cflag & PARENB)
35 cval |= UART_LCR_PARITY;
36 if (!(cflag & PARODD))
37 cval |= UART_LCR_EPAR;
38 if (synth_request_region(ser->port, 8)) {
39 /* try to take it back. */
40 printk(KERN_INFO "Ports not available, trying to steal them\n");
41 __release_region(&ioport_resource, ser->port, 8);
42 err = synth_request_region(ser->port, 8);
43 if (err) {
44 pr_warn("Unable to allocate port at %lx, errno %i",
45 ser->port, err);
46 return NULL;
50 /* Disable UART interrupts, set DTR and RTS high
51 * and set speed. */
52 outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
53 outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
54 outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
55 outb(cval, ser->port + UART_LCR); /* reset DLAB */
57 /* Turn off Interrupts */
58 outb(0, ser->port + UART_IER);
59 outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
61 /* If we read 0xff from the LSR, there is no UART here. */
62 if (inb(ser->port + UART_LSR) == 0xff) {
63 synth_release_region(ser->port, 8);
64 serstate = NULL;
65 return NULL;
68 mdelay(1);
69 speakup_info.port_tts = ser->port;
70 serstate = ser;
72 start_serial_interrupt(ser->irq);
74 return ser;
77 static irqreturn_t synth_readbuf_handler(int irq, void *dev_id)
79 unsigned long flags;
80 /*printk(KERN_ERR "in irq\n"); */
81 /*pr_warn("in IRQ\n"); */
82 int c;
83 spk_lock(flags);
84 while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) {
86 c = inb_p(speakup_info.port_tts+UART_RX);
87 synth->read_buff_add((u_char) c);
88 /*printk(KERN_ERR "c = %d\n", c); */
89 /*pr_warn("C = %d\n", c); */
91 spk_unlock(flags);
92 return IRQ_HANDLED;
95 static void start_serial_interrupt(int irq)
97 int rv;
99 if (synth->read_buff_add == NULL)
100 return;
102 rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
103 "serial", (void *) synth_readbuf_handler);
105 if (rv)
106 printk(KERN_ERR "Unable to request Speakup serial I R Q\n");
107 /* Set MCR */
108 outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2,
109 speakup_info.port_tts + UART_MCR);
110 /* Turn on Interrupts */
111 outb(UART_IER_MSI|UART_IER_RLSI|UART_IER_RDI,
112 speakup_info.port_tts + UART_IER);
113 inb(speakup_info.port_tts+UART_LSR);
114 inb(speakup_info.port_tts+UART_RX);
115 inb(speakup_info.port_tts+UART_IIR);
116 inb(speakup_info.port_tts+UART_MSR);
117 outb(1, speakup_info.port_tts + UART_FCR); /* Turn FIFO On */
120 void stop_serial_interrupt(void)
122 if (speakup_info.port_tts == 0)
123 return;
125 if (synth->read_buff_add == NULL)
126 return;
128 /* Turn off interrupts */
129 outb(0, speakup_info.port_tts+UART_IER);
130 /* Free IRQ */
131 free_irq(serstate->irq, (void *) synth_readbuf_handler);
134 int wait_for_xmitr(void)
136 int tmout = SPK_XMITR_TIMEOUT;
137 if ((synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) {
138 pr_warn("%s: too many timeouts, deactivating speakup\n",
139 synth->long_name);
140 synth->alive = 0;
141 /* No synth any more, so nobody will restart TTYs, and we thus
142 * need to do it ourselves. Now that there is no synth we can
143 * let application flood anyway */
144 speakup_start_ttys();
145 timeouts = 0;
146 return 0;
148 while (spk_serial_tx_busy()) {
149 if (--tmout == 0) {
150 pr_warn("%s: timed out (tx busy)\n", synth->long_name);
151 timeouts++;
152 return 0;
154 udelay(1);
156 tmout = SPK_CTS_TIMEOUT;
157 while (!((inb_p(speakup_info.port_tts + UART_MSR)) & UART_MSR_CTS)) {
158 /* CTS */
159 if (--tmout == 0) {
160 /* pr_warn("%s: timed out (cts)\n",
161 * synth->long_name); */
162 timeouts++;
163 return 0;
165 udelay(1);
167 timeouts = 0;
168 return 1;
171 unsigned char spk_serial_in(void)
173 int tmout = SPK_SERIAL_TIMEOUT;
175 while (!(inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR)) {
176 if (--tmout == 0) {
177 pr_warn("time out while waiting for input.\n");
178 return 0xff;
180 udelay(1);
182 return inb_p(speakup_info.port_tts + UART_RX);
184 EXPORT_SYMBOL_GPL(spk_serial_in);
186 unsigned char spk_serial_in_nowait(void)
188 unsigned char lsr;
190 lsr = inb_p(speakup_info.port_tts + UART_LSR);
191 if (!(lsr & UART_LSR_DR))
192 return 0;
193 return inb_p(speakup_info.port_tts + UART_RX);
195 EXPORT_SYMBOL_GPL(spk_serial_in_nowait);
197 int spk_serial_out(const char ch)
199 if (synth->alive && wait_for_xmitr()) {
200 outb_p(ch, speakup_info.port_tts);
201 return 1;
203 return 0;
205 EXPORT_SYMBOL_GPL(spk_serial_out);
207 void spk_serial_release(void)
209 if (speakup_info.port_tts == 0)
210 return;
211 synth_release_region(speakup_info.port_tts, 8);
212 speakup_info.port_tts = 0;
214 EXPORT_SYMBOL_GPL(spk_serial_release);