]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/accessibility/speakup/serialio.c
Merge tag 'for-v5.14' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux...
[mirror_ubuntu-jammy-kernel.git] / drivers / accessibility / speakup / serialio.c
CommitLineData
64969228 1// SPDX-License-Identifier: GPL-2.0
c6e3fd22
WH
2#include <linux/interrupt.h>
3#include <linux/ioport.h>
4
5#include "spk_types.h"
6#include "speakup.h"
7#include "spk_priv.h"
8#include "serialio.h"
9
327b882d
ST
10#include <linux/serial_core.h>
11/* WARNING: Do not change this to <linux/serial.h> without testing that
e6ceec82
JR
12 * SERIAL_PORT_DFNS does get defined to the appropriate value.
13 */
327b882d
ST
14#include <asm/serial.h>
15
5e6dc548
CG
16#ifndef SERIAL_PORT_DFNS
17#define SERIAL_PORT_DFNS
18#endif
19
c6e3fd22
WH
20static void start_serial_interrupt(int irq);
21
3ee0017e 22static const struct old_serial_port rs_table[] = {
c6e3fd22
WH
23 SERIAL_PORT_DFNS
24};
defaa9ad 25
3ee0017e 26static const struct old_serial_port *serstate;
c6e3fd22
WH
27static int timeouts;
28
1e441594 29static int spk_serial_out(struct spk_synth *in_synth, const char ch);
1941ab1d
ST
30static void spk_serial_send_xchar(struct spk_synth *in_synth, char ch);
31static void spk_serial_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear);
32static unsigned char spk_serial_in(struct spk_synth *in_synth);
33static unsigned char spk_serial_in_nowait(struct spk_synth *in_synth);
34static void spk_serial_flush_buffer(struct spk_synth *in_synth);
2b86d9b8 35static int spk_serial_wait_for_xmitr(struct spk_synth *in_synth);
be223d57 36
1e441594
OK
37struct spk_io_ops spk_serial_io_ops = {
38 .synth_out = spk_serial_out,
be223d57
OK
39 .send_xchar = spk_serial_send_xchar,
40 .tiocmset = spk_serial_tiocmset,
ca693dcd
OK
41 .synth_in = spk_serial_in,
42 .synth_in_nowait = spk_serial_in_nowait,
1c597367 43 .flush_buffer = spk_serial_flush_buffer,
2b86d9b8 44 .wait_for_xmitr = spk_serial_wait_for_xmitr,
1e441594
OK
45};
46EXPORT_SYMBOL_GPL(spk_serial_io_ops);
47
3ee0017e 48const struct old_serial_port *spk_serial_init(int index)
c6e3fd22
WH
49{
50 int baud = 9600, quot = 0;
51 unsigned int cval = 0;
52 int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8;
327b882d 53 const struct old_serial_port *ser;
c6e3fd22
WH
54 int err;
55
327b882d
ST
56 if (index >= ARRAY_SIZE(rs_table)) {
57 pr_info("no port info for ttyS%d\n", index);
58 return NULL;
59 }
60 ser = rs_table + index;
61
c6e3fd22
WH
62 /* Divisor, bytesize and parity */
63 quot = ser->baud_base / baud;
64 cval = cflag & (CSIZE | CSTOPB);
65#if defined(__powerpc__) || defined(__alpha__)
66 cval >>= 8;
67#else /* !__powerpc__ && !__alpha__ */
68 cval >>= 4;
69#endif /* !__powerpc__ && !__alpha__ */
70 if (cflag & PARENB)
71 cval |= UART_LCR_PARITY;
72 if (!(cflag & PARODD))
73 cval |= UART_LCR_EPAR;
74 if (synth_request_region(ser->port, 8)) {
75 /* try to take it back. */
3a046c19 76 pr_info("Ports not available, trying to steal them\n");
c6e3fd22
WH
77 __release_region(&ioport_resource, ser->port, 8);
78 err = synth_request_region(ser->port, 8);
79 if (err) {
3ee0017e 80 pr_warn("Unable to allocate port at %x, errno %i",
baf9ac9f 81 ser->port, err);
c6e3fd22
WH
82 return NULL;
83 }
84 }
85
86 /* Disable UART interrupts, set DTR and RTS high
13d825ed
AF
87 * and set speed.
88 */
c6e3fd22
WH
89 outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
90 outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
91 outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
92 outb(cval, ser->port + UART_LCR); /* reset DLAB */
93
94 /* Turn off Interrupts */
95 outb(0, ser->port + UART_IER);
96 outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
97
98 /* If we read 0xff from the LSR, there is no UART here. */
99 if (inb(ser->port + UART_LSR) == 0xff) {
100 synth_release_region(ser->port, 8);
101 serstate = NULL;
102 return NULL;
103 }
104
105 mdelay(1);
106 speakup_info.port_tts = ser->port;
107 serstate = ser;
108
109 start_serial_interrupt(ser->irq);
110
111 return ser;
112}
113
114static irqreturn_t synth_readbuf_handler(int irq, void *dev_id)
115{
116 unsigned long flags;
c6e3fd22 117 int c;
8e69a811 118
9fb31b1a 119 spin_lock_irqsave(&speakup_info.spinlock, flags);
c6e3fd22 120 while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) {
e3bab5eb 121 c = inb_p(speakup_info.port_tts + UART_RX);
d290effe 122 synth->read_buff_add((u_char)c);
c6e3fd22 123 }
9fb31b1a 124 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22
WH
125 return IRQ_HANDLED;
126}
127
128static void start_serial_interrupt(int irq)
129{
130 int rv;
131
114885e0 132 if (!synth->read_buff_add)
c6e3fd22
WH
133 return;
134
135 rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
d290effe 136 "serial", (void *)synth_readbuf_handler);
c6e3fd22
WH
137
138 if (rv)
3a046c19 139 pr_err("Unable to request Speakup serial I R Q\n");
c6e3fd22
WH
140 /* Set MCR */
141 outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2,
65bf4ea1 142 speakup_info.port_tts + UART_MCR);
c6e3fd22 143 /* Turn on Interrupts */
79de2d0e 144 outb(UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI,
e5770b7b 145 speakup_info.port_tts + UART_IER);
e3bab5eb
VR
146 inb(speakup_info.port_tts + UART_LSR);
147 inb(speakup_info.port_tts + UART_RX);
148 inb(speakup_info.port_tts + UART_IIR);
149 inb(speakup_info.port_tts + UART_MSR);
c6e3fd22
WH
150 outb(1, speakup_info.port_tts + UART_FCR); /* Turn FIFO On */
151}
152
1941ab1d 153static void spk_serial_send_xchar(struct spk_synth *synth, char ch)
be223d57
OK
154{
155 int timeout = SPK_XMITR_TIMEOUT;
156
157 while (spk_serial_tx_busy()) {
158 if (!--timeout)
159 break;
160 udelay(1);
161 }
162 outb(ch, speakup_info.port_tts);
163}
164
1941ab1d 165static void spk_serial_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear)
be223d57
OK
166{
167 int old = inb(speakup_info.port_tts + UART_MCR);
f6089ae4 168
be223d57
OK
169 outb((old & ~clear) | set, speakup_info.port_tts + UART_MCR);
170}
171
98c1fda7
OK
172int spk_serial_synth_probe(struct spk_synth *synth)
173{
174 const struct old_serial_port *ser;
175 int failed = 0;
176
177 if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) {
178 ser = spk_serial_init(synth->ser);
179 if (!ser) {
180 failed = -1;
181 } else {
182 outb_p(0, ser->port);
183 mdelay(1);
184 outb_p('\r', ser->port);
185 }
186 } else {
187 failed = -1;
188 pr_warn("ttyS%i is an invalid port\n", synth->ser);
189 }
190 if (failed) {
191 pr_info("%s: not found\n", synth->long_name);
192 return -ENODEV;
193 }
194 pr_info("%s: ttyS%i, Driver Version %s\n",
195 synth->long_name, synth->ser, synth->version);
196 synth->alive = 1;
197 return 0;
198}
199EXPORT_SYMBOL_GPL(spk_serial_synth_probe);
200
ca2beaf8 201void spk_stop_serial_interrupt(void)
c6e3fd22
WH
202{
203 if (speakup_info.port_tts == 0)
204 return;
205
114885e0 206 if (!synth->read_buff_add)
c6e3fd22
WH
207 return;
208
209 /* Turn off interrupts */
e3bab5eb 210 outb(0, speakup_info.port_tts + UART_IER);
c6e3fd22 211 /* Free IRQ */
d290effe 212 free_irq(serstate->irq, (void *)synth_readbuf_handler);
c6e3fd22 213}
a50ef316 214EXPORT_SYMBOL_GPL(spk_stop_serial_interrupt);
c6e3fd22 215
2b86d9b8 216static int spk_serial_wait_for_xmitr(struct spk_synth *in_synth)
c6e3fd22
WH
217{
218 int tmout = SPK_XMITR_TIMEOUT;
8e69a811 219
9176d156 220 if ((in_synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) {
baf9ac9f 221 pr_warn("%s: too many timeouts, deactivating speakup\n",
9176d156
OK
222 in_synth->long_name);
223 in_synth->alive = 0;
c6e3fd22
WH
224 /* No synth any more, so nobody will restart TTYs, and we thus
225 * need to do it ourselves. Now that there is no synth we can
13d825ed
AF
226 * let application flood anyway
227 */
c6e3fd22
WH
228 speakup_start_ttys();
229 timeouts = 0;
230 return 0;
231 }
232 while (spk_serial_tx_busy()) {
233 if (--tmout == 0) {
1f101456
BY
234 pr_warn("%s: timed out (tx busy)\n",
235 in_synth->long_name);
c6e3fd22
WH
236 timeouts++;
237 return 0;
238 }
239 udelay(1);
240 }
241 tmout = SPK_CTS_TIMEOUT;
242 while (!((inb_p(speakup_info.port_tts + UART_MSR)) & UART_MSR_CTS)) {
243 /* CTS */
244 if (--tmout == 0) {
c6e3fd22
WH
245 timeouts++;
246 return 0;
247 }
248 udelay(1);
249 }
250 timeouts = 0;
251 return 1;
252}
253
1941ab1d 254static unsigned char spk_serial_in(struct spk_synth *in_synth)
c6e3fd22
WH
255{
256 int tmout = SPK_SERIAL_TIMEOUT;
257
258 while (!(inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR)) {
259 if (--tmout == 0) {
260 pr_warn("time out while waiting for input.\n");
261 return 0xff;
262 }
263 udelay(1);
264 }
265 return inb_p(speakup_info.port_tts + UART_RX);
266}
c6e3fd22 267
1941ab1d 268static unsigned char spk_serial_in_nowait(struct spk_synth *in_synth)
c6e3fd22
WH
269{
270 unsigned char lsr;
271
272 lsr = inb_p(speakup_info.port_tts + UART_LSR);
273 if (!(lsr & UART_LSR_DR))
274 return 0;
275 return inb_p(speakup_info.port_tts + UART_RX);
276}
c6e3fd22 277
1941ab1d 278static void spk_serial_flush_buffer(struct spk_synth *in_synth)
1c597367
OK
279{
280 /* TODO: flush the UART 16550 buffer */
281}
282
a15505e6 283static int spk_serial_out(struct spk_synth *in_synth, const char ch)
c6e3fd22 284{
2b86d9b8 285 if (in_synth->alive && spk_serial_wait_for_xmitr(in_synth)) {
c6e3fd22
WH
286 outb_p(ch, speakup_info.port_tts);
287 return 1;
288 }
289 return 0;
290}
c6e3fd22 291
1f101456
BY
292const char *spk_serial_synth_immediate(struct spk_synth *synth,
293 const char *buff)
98c1fda7
OK
294{
295 u_char ch;
296
297 while ((ch = *buff)) {
298 if (ch == '\n')
299 ch = synth->procspeech;
2b86d9b8 300 if (spk_serial_wait_for_xmitr(synth))
98c1fda7
OK
301 outb(ch, speakup_info.port_tts);
302 else
303 return buff;
304 buff++;
305 }
306 return NULL;
307}
308EXPORT_SYMBOL_GPL(spk_serial_synth_immediate);
309
1941ab1d 310void spk_serial_release(struct spk_synth *synth)
c6e3fd22 311{
a50ef316 312 spk_stop_serial_interrupt();
c6e3fd22
WH
313 if (speakup_info.port_tts == 0)
314 return;
315 synth_release_region(speakup_info.port_tts, 8);
316 speakup_info.port_tts = 0;
317}
318EXPORT_SYMBOL_GPL(spk_serial_release);