1 #include <linux/interrupt.h>
2 #include <linux/ioport.h>
3 
4 #include "spk_types.h"
5 #include "speakup.h"
6 #include "spk_priv.h"
7 #include "serialio.h"
8 
9 static void start_serial_interrupt(int irq);
10 
11 static struct serial_state rs_table[] = {
12 	SERIAL_PORT_DFNS
13 };
14 static struct serial_state *serstate;
15 static int timeouts;
16 
spk_serial_init(int index)17 struct serial_state *spk_serial_init(int index)
18 {
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;
24 
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;
47 		}
48 	}
49 
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 */
56 
57 	/* Turn off Interrupts */
58 	outb(0, ser->port + UART_IER);
59 	outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
60 
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;
66 	}
67 
68 	mdelay(1);
69 	speakup_info.port_tts = ser->port;
70 	serstate = ser;
71 
72 	start_serial_interrupt(ser->irq);
73 
74 	return ser;
75 }
76 
synth_readbuf_handler(int irq,void * dev_id)77 static irqreturn_t synth_readbuf_handler(int irq, void *dev_id)
78 {
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) {
85 
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); */
90 	}
91 	spk_unlock(flags);
92 	return IRQ_HANDLED;
93 }
94 
start_serial_interrupt(int irq)95 static void start_serial_interrupt(int irq)
96 {
97 	int rv;
98 
99 	if (synth->read_buff_add == NULL)
100 		return;
101 
102 	rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
103 			 "serial", (void *) synth_readbuf_handler);
104 
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 */
118 }
119 
stop_serial_interrupt(void)120 void stop_serial_interrupt(void)
121 {
122 	if (speakup_info.port_tts == 0)
123 		return;
124 
125 	if (synth->read_buff_add == NULL)
126 		return;
127 
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);
132 }
133 
wait_for_xmitr(void)134 int wait_for_xmitr(void)
135 {
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;
147 	}
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;
153 		}
154 		udelay(1);
155 	}
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;
164 		}
165 		udelay(1);
166 	}
167 	timeouts = 0;
168 	return 1;
169 }
170 
spk_serial_in(void)171 unsigned char spk_serial_in(void)
172 {
173 	int tmout = SPK_SERIAL_TIMEOUT;
174 
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;
179 		}
180 		udelay(1);
181 	}
182 	return inb_p(speakup_info.port_tts + UART_RX);
183 }
184 EXPORT_SYMBOL_GPL(spk_serial_in);
185 
spk_serial_in_nowait(void)186 unsigned char spk_serial_in_nowait(void)
187 {
188 	unsigned char lsr;
189 
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);
194 }
195 EXPORT_SYMBOL_GPL(spk_serial_in_nowait);
196 
spk_serial_out(const char ch)197 int spk_serial_out(const char ch)
198 {
199 	if (synth->alive && wait_for_xmitr()) {
200 		outb_p(ch, speakup_info.port_tts);
201 		return 1;
202 	}
203 	return 0;
204 }
205 EXPORT_SYMBOL_GPL(spk_serial_out);
206 
spk_serial_release(void)207 void spk_serial_release(void)
208 {
209 	if (speakup_info.port_tts == 0)
210 		return;
211 	synth_release_region(speakup_info.port_tts, 8);
212 	speakup_info.port_tts = 0;
213 }
214 EXPORT_SYMBOL_GPL(spk_serial_release);
215 
216