blob: 53580bdc5baa1be549edeea82b577f878a16ad92 [file] [log] [blame]
Greg Kroah-Hartman64969222018-01-11 11:08:40 +01001// SPDX-License-Identifier: GPL-2.0
William Hubbsc6e3fd22010-10-07 13:20:02 -05002#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
Samuel Thibault327b8822016-01-15 00:47:41 +010010#include <linux/serial_core.h>
11/* WARNING: Do not change this to <linux/serial.h> without testing that
Janani Ravichandrane6ceec822016-02-13 22:05:01 -050012 * SERIAL_PORT_DFNS does get defined to the appropriate value.
13 */
Samuel Thibault327b8822016-01-15 00:47:41 +010014#include <asm/serial.h>
15
Chen Gang5e6dc542013-10-31 15:27:37 +080016#ifndef SERIAL_PORT_DFNS
17#define SERIAL_PORT_DFNS
18#endif
19
William Hubbsc6e3fd22010-10-07 13:20:02 -050020static void start_serial_interrupt(int irq);
21
Jiri Slaby3ee00172012-03-05 14:52:11 +010022static const struct old_serial_port rs_table[] = {
William Hubbsc6e3fd22010-10-07 13:20:02 -050023 SERIAL_PORT_DFNS
24};
Arushi Singhaldefaa9a2017-03-14 10:46:42 +053025
Jiri Slaby3ee00172012-03-05 14:52:11 +010026static const struct old_serial_port *serstate;
William Hubbsc6e3fd22010-10-07 13:20:02 -050027static int timeouts;
28
Okash Khawaja1e441592017-03-14 13:41:53 +000029static int spk_serial_out(struct spk_synth *in_synth, const char ch);
Samuel Thibault1941ab12021-01-26 23:21:44 +010030static 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);
Samuel Thibault2b86d9b2020-08-04 18:06:37 +020035static int spk_serial_wait_for_xmitr(struct spk_synth *in_synth);
Okash Khawajabe223d52017-04-22 09:00:28 +010036
Okash Khawaja1e441592017-03-14 13:41:53 +000037struct spk_io_ops spk_serial_io_ops = {
38 .synth_out = spk_serial_out,
Okash Khawajabe223d52017-04-22 09:00:28 +010039 .send_xchar = spk_serial_send_xchar,
40 .tiocmset = spk_serial_tiocmset,
Okash Khawajaca693dc2017-04-29 20:52:58 +010041 .synth_in = spk_serial_in,
42 .synth_in_nowait = spk_serial_in_nowait,
Okash Khawaja1c597362017-05-15 18:45:37 +010043 .flush_buffer = spk_serial_flush_buffer,
Samuel Thibault2b86d9b2020-08-04 18:06:37 +020044 .wait_for_xmitr = spk_serial_wait_for_xmitr,
Okash Khawaja1e441592017-03-14 13:41:53 +000045};
46EXPORT_SYMBOL_GPL(spk_serial_io_ops);
47
Jiri Slaby3ee00172012-03-05 14:52:11 +010048const struct old_serial_port *spk_serial_init(int index)
William Hubbsc6e3fd22010-10-07 13:20:02 -050049{
50 int baud = 9600, quot = 0;
51 unsigned int cval = 0;
52 int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8;
Samuel Thibault327b8822016-01-15 00:47:41 +010053 const struct old_serial_port *ser;
William Hubbsc6e3fd22010-10-07 13:20:02 -050054 int err;
55
Samuel Thibault327b8822016-01-15 00:47:41 +010056 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
William Hubbsc6e3fd22010-10-07 13:20:02 -050062 /* 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. */
Keerthimai Janarthanan3a046c12014-03-18 14:10:13 +053076 pr_info("Ports not available, trying to steal them\n");
William Hubbsc6e3fd22010-10-07 13:20:02 -050077 __release_region(&ioport_resource, ser->port, 8);
78 err = synth_request_region(ser->port, 8);
79 if (err) {
Jiri Slaby3ee00172012-03-05 14:52:11 +010080 pr_warn("Unable to allocate port at %x, errno %i",
William Hubbsbaf9ac92010-10-15 22:13:36 -050081 ser->port, err);
William Hubbsc6e3fd22010-10-07 13:20:02 -050082 return NULL;
83 }
84 }
85
86 /* Disable UART interrupts, set DTR and RTS high
Aleksei Fedotov13d825e2015-08-14 22:34:37 +030087 * and set speed.
88 */
William Hubbsc6e3fd22010-10-07 13:20:02 -050089 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;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500117 int c;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200118
William Hubbs9fb31b12013-05-13 00:02:58 -0500119 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500120 while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) {
Varsha Raoe3bab5e2017-02-22 23:16:40 +0530121 c = inb_p(speakup_info.port_tts + UART_RX);
Shiva Kerdeld290eff2016-11-06 15:09:18 +0100122 synth->read_buff_add((u_char)c);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500123 }
William Hubbs9fb31b12013-05-13 00:02:58 -0500124 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500125 return IRQ_HANDLED;
126}
127
128static void start_serial_interrupt(int irq)
129{
130 int rv;
131
Shraddha Barke114885e2015-09-11 11:32:27 +0530132 if (!synth->read_buff_add)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500133 return;
134
135 rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
Shiva Kerdeld290eff2016-11-06 15:09:18 +0100136 "serial", (void *)synth_readbuf_handler);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500137
138 if (rv)
Keerthimai Janarthanan3a046c12014-03-18 14:10:13 +0530139 pr_err("Unable to request Speakup serial I R Q\n");
William Hubbsc6e3fd22010-10-07 13:20:02 -0500140 /* Set MCR */
141 outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2,
Arushi Singhal65bf4ea12017-03-21 17:12:34 +0530142 speakup_info.port_tts + UART_MCR);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500143 /* Turn on Interrupts */
Bo YU79de2d02017-06-05 23:44:11 -0400144 outb(UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI,
Bo YUe5770b72017-06-05 23:44:40 -0400145 speakup_info.port_tts + UART_IER);
Varsha Raoe3bab5e2017-02-22 23:16:40 +0530146 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);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500150 outb(1, speakup_info.port_tts + UART_FCR); /* Turn FIFO On */
151}
152
Samuel Thibault1941ab12021-01-26 23:21:44 +0100153static void spk_serial_send_xchar(struct spk_synth *synth, char ch)
Okash Khawajabe223d52017-04-22 09:00:28 +0100154{
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
Samuel Thibault1941ab12021-01-26 23:21:44 +0100165static void spk_serial_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear)
Okash Khawajabe223d52017-04-22 09:00:28 +0100166{
167 int old = inb(speakup_info.port_tts + UART_MCR);
Bo YUf6089ae2017-06-05 23:43:55 -0400168
Okash Khawajabe223d52017-04-22 09:00:28 +0100169 outb((old & ~clear) | set, speakup_info.port_tts + UART_MCR);
170}
171
Okash Khawaja98c1fda2017-03-16 08:10:17 +0000172int 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
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100201void spk_stop_serial_interrupt(void)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500202{
203 if (speakup_info.port_tts == 0)
204 return;
205
Shraddha Barke114885e2015-09-11 11:32:27 +0530206 if (!synth->read_buff_add)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500207 return;
208
209 /* Turn off interrupts */
Varsha Raoe3bab5e2017-02-22 23:16:40 +0530210 outb(0, speakup_info.port_tts + UART_IER);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500211 /* Free IRQ */
Shiva Kerdeld290eff2016-11-06 15:09:18 +0100212 free_irq(serstate->irq, (void *)synth_readbuf_handler);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500213}
Okash Khawajaa50ef312017-03-14 13:41:54 +0000214EXPORT_SYMBOL_GPL(spk_stop_serial_interrupt);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500215
Samuel Thibault2b86d9b2020-08-04 18:06:37 +0200216static int spk_serial_wait_for_xmitr(struct spk_synth *in_synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500217{
218 int tmout = SPK_XMITR_TIMEOUT;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200219
Okash Khawaja9176d152017-03-14 13:41:52 +0000220 if ((in_synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) {
William Hubbsbaf9ac92010-10-15 22:13:36 -0500221 pr_warn("%s: too many timeouts, deactivating speakup\n",
Okash Khawaja9176d152017-03-14 13:41:52 +0000222 in_synth->long_name);
223 in_synth->alive = 0;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500224 /* 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
Aleksei Fedotov13d825e2015-08-14 22:34:37 +0300226 * let application flood anyway
227 */
William Hubbsc6e3fd22010-10-07 13:20:02 -0500228 speakup_start_ttys();
229 timeouts = 0;
230 return 0;
231 }
232 while (spk_serial_tx_busy()) {
233 if (--tmout == 0) {
Bo YU1f101452017-06-05 23:44:26 -0400234 pr_warn("%s: timed out (tx busy)\n",
235 in_synth->long_name);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500236 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) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500245 timeouts++;
246 return 0;
247 }
248 udelay(1);
249 }
250 timeouts = 0;
251 return 1;
252}
253
Samuel Thibault1941ab12021-01-26 23:21:44 +0100254static unsigned char spk_serial_in(struct spk_synth *in_synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500255{
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}
William Hubbsc6e3fd22010-10-07 13:20:02 -0500267
Samuel Thibault1941ab12021-01-26 23:21:44 +0100268static unsigned char spk_serial_in_nowait(struct spk_synth *in_synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500269{
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}
William Hubbsc6e3fd22010-10-07 13:20:02 -0500277
Samuel Thibault1941ab12021-01-26 23:21:44 +0100278static void spk_serial_flush_buffer(struct spk_synth *in_synth)
Okash Khawaja1c597362017-05-15 18:45:37 +0100279{
280 /* TODO: flush the UART 16550 buffer */
281}
282
Gustavo A. R. Silvaa15505e2017-03-27 01:37:29 -0500283static int spk_serial_out(struct spk_synth *in_synth, const char ch)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500284{
Samuel Thibault2b86d9b2020-08-04 18:06:37 +0200285 if (in_synth->alive && spk_serial_wait_for_xmitr(in_synth)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500286 outb_p(ch, speakup_info.port_tts);
287 return 1;
288 }
289 return 0;
290}
William Hubbsc6e3fd22010-10-07 13:20:02 -0500291
Bo YU1f101452017-06-05 23:44:26 -0400292const char *spk_serial_synth_immediate(struct spk_synth *synth,
293 const char *buff)
Okash Khawaja98c1fda2017-03-16 08:10:17 +0000294{
295 u_char ch;
296
297 while ((ch = *buff)) {
298 if (ch == '\n')
299 ch = synth->procspeech;
Samuel Thibault2b86d9b2020-08-04 18:06:37 +0200300 if (spk_serial_wait_for_xmitr(synth))
Okash Khawaja98c1fda2017-03-16 08:10:17 +0000301 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
Samuel Thibault1941ab12021-01-26 23:21:44 +0100310void spk_serial_release(struct spk_synth *synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500311{
Okash Khawajaa50ef312017-03-14 13:41:54 +0000312 spk_stop_serial_interrupt();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500313 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);