William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 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 | |
Samuel Thibault | 327b882 | 2016-01-15 00:47:41 +0100 | [diff] [blame] | 9 | #include <linux/serial_core.h> |
| 10 | /* WARNING: Do not change this to <linux/serial.h> without testing that |
Janani Ravichandran | e6ceec82 | 2016-02-13 22:05:01 -0500 | [diff] [blame] | 11 | * SERIAL_PORT_DFNS does get defined to the appropriate value. |
| 12 | */ |
Samuel Thibault | 327b882 | 2016-01-15 00:47:41 +0100 | [diff] [blame] | 13 | #include <asm/serial.h> |
| 14 | |
Chen Gang | 5e6dc54 | 2013-10-31 15:27:37 +0800 | [diff] [blame] | 15 | #ifndef SERIAL_PORT_DFNS |
| 16 | #define SERIAL_PORT_DFNS |
| 17 | #endif |
| 18 | |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 19 | static void start_serial_interrupt(int irq); |
| 20 | |
Jiri Slaby | 3ee0017 | 2012-03-05 14:52:11 +0100 | [diff] [blame] | 21 | static const struct old_serial_port rs_table[] = { |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 22 | SERIAL_PORT_DFNS |
| 23 | }; |
Arushi Singhal | defaa9a | 2017-03-14 10:46:42 +0530 | [diff] [blame] | 24 | |
Jiri Slaby | 3ee0017 | 2012-03-05 14:52:11 +0100 | [diff] [blame] | 25 | static const struct old_serial_port *serstate; |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 26 | static int timeouts; |
| 27 | |
Okash Khawaja | 1e44159 | 2017-03-14 13:41:53 +0000 | [diff] [blame] | 28 | static int spk_serial_out(struct spk_synth *in_synth, const char ch); |
Okash Khawaja | be223d5 | 2017-04-22 09:00:28 +0100 | [diff] [blame] | 29 | static void spk_serial_send_xchar(char ch); |
| 30 | static void spk_serial_tiocmset(unsigned int set, unsigned int clear); |
| 31 | |
Okash Khawaja | 1e44159 | 2017-03-14 13:41:53 +0000 | [diff] [blame] | 32 | struct spk_io_ops spk_serial_io_ops = { |
| 33 | .synth_out = spk_serial_out, |
Okash Khawaja | be223d5 | 2017-04-22 09:00:28 +0100 | [diff] [blame] | 34 | .send_xchar = spk_serial_send_xchar, |
| 35 | .tiocmset = spk_serial_tiocmset, |
Okash Khawaja | 1e44159 | 2017-03-14 13:41:53 +0000 | [diff] [blame] | 36 | }; |
| 37 | EXPORT_SYMBOL_GPL(spk_serial_io_ops); |
| 38 | |
Jiri Slaby | 3ee0017 | 2012-03-05 14:52:11 +0100 | [diff] [blame] | 39 | const struct old_serial_port *spk_serial_init(int index) |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 40 | { |
| 41 | int baud = 9600, quot = 0; |
| 42 | unsigned int cval = 0; |
| 43 | int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8; |
Samuel Thibault | 327b882 | 2016-01-15 00:47:41 +0100 | [diff] [blame] | 44 | const struct old_serial_port *ser; |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 45 | int err; |
| 46 | |
Samuel Thibault | 327b882 | 2016-01-15 00:47:41 +0100 | [diff] [blame] | 47 | if (index >= ARRAY_SIZE(rs_table)) { |
| 48 | pr_info("no port info for ttyS%d\n", index); |
| 49 | return NULL; |
| 50 | } |
| 51 | ser = rs_table + index; |
| 52 | |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 53 | /* Divisor, bytesize and parity */ |
| 54 | quot = ser->baud_base / baud; |
| 55 | cval = cflag & (CSIZE | CSTOPB); |
| 56 | #if defined(__powerpc__) || defined(__alpha__) |
| 57 | cval >>= 8; |
| 58 | #else /* !__powerpc__ && !__alpha__ */ |
| 59 | cval >>= 4; |
| 60 | #endif /* !__powerpc__ && !__alpha__ */ |
| 61 | if (cflag & PARENB) |
| 62 | cval |= UART_LCR_PARITY; |
| 63 | if (!(cflag & PARODD)) |
| 64 | cval |= UART_LCR_EPAR; |
| 65 | if (synth_request_region(ser->port, 8)) { |
| 66 | /* try to take it back. */ |
Keerthimai Janarthanan | 3a046c1 | 2014-03-18 14:10:13 +0530 | [diff] [blame] | 67 | pr_info("Ports not available, trying to steal them\n"); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 68 | __release_region(&ioport_resource, ser->port, 8); |
| 69 | err = synth_request_region(ser->port, 8); |
| 70 | if (err) { |
Jiri Slaby | 3ee0017 | 2012-03-05 14:52:11 +0100 | [diff] [blame] | 71 | pr_warn("Unable to allocate port at %x, errno %i", |
William Hubbs | baf9ac9 | 2010-10-15 22:13:36 -0500 | [diff] [blame] | 72 | ser->port, err); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /* Disable UART interrupts, set DTR and RTS high |
Aleksei Fedotov | 13d825e | 2015-08-14 22:34:37 +0300 | [diff] [blame] | 78 | * and set speed. |
| 79 | */ |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 80 | outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */ |
| 81 | outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */ |
| 82 | outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */ |
| 83 | outb(cval, ser->port + UART_LCR); /* reset DLAB */ |
| 84 | |
| 85 | /* Turn off Interrupts */ |
| 86 | outb(0, ser->port + UART_IER); |
| 87 | outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR); |
| 88 | |
| 89 | /* If we read 0xff from the LSR, there is no UART here. */ |
| 90 | if (inb(ser->port + UART_LSR) == 0xff) { |
| 91 | synth_release_region(ser->port, 8); |
| 92 | serstate = NULL; |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | mdelay(1); |
| 97 | speakup_info.port_tts = ser->port; |
| 98 | serstate = ser; |
| 99 | |
| 100 | start_serial_interrupt(ser->irq); |
| 101 | |
| 102 | return ser; |
| 103 | } |
| 104 | |
| 105 | static irqreturn_t synth_readbuf_handler(int irq, void *dev_id) |
| 106 | { |
| 107 | unsigned long flags; |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 108 | int c; |
Domagoj Trsan | 8e69a81 | 2014-09-09 20:04:34 +0200 | [diff] [blame] | 109 | |
William Hubbs | 9fb31b1 | 2013-05-13 00:02:58 -0500 | [diff] [blame] | 110 | spin_lock_irqsave(&speakup_info.spinlock, flags); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 111 | while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) { |
Varsha Rao | e3bab5e | 2017-02-22 23:16:40 +0530 | [diff] [blame] | 112 | c = inb_p(speakup_info.port_tts + UART_RX); |
Shiva Kerdel | d290eff | 2016-11-06 15:09:18 +0100 | [diff] [blame] | 113 | synth->read_buff_add((u_char)c); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 114 | } |
William Hubbs | 9fb31b1 | 2013-05-13 00:02:58 -0500 | [diff] [blame] | 115 | spin_unlock_irqrestore(&speakup_info.spinlock, flags); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 116 | return IRQ_HANDLED; |
| 117 | } |
| 118 | |
| 119 | static void start_serial_interrupt(int irq) |
| 120 | { |
| 121 | int rv; |
| 122 | |
Shraddha Barke | 114885e | 2015-09-11 11:32:27 +0530 | [diff] [blame] | 123 | if (!synth->read_buff_add) |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 124 | return; |
| 125 | |
| 126 | rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED, |
Shiva Kerdel | d290eff | 2016-11-06 15:09:18 +0100 | [diff] [blame] | 127 | "serial", (void *)synth_readbuf_handler); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 128 | |
| 129 | if (rv) |
Keerthimai Janarthanan | 3a046c1 | 2014-03-18 14:10:13 +0530 | [diff] [blame] | 130 | pr_err("Unable to request Speakup serial I R Q\n"); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 131 | /* Set MCR */ |
| 132 | outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2, |
Arushi Singhal | 65bf4ea1 | 2017-03-21 17:12:34 +0530 | [diff] [blame] | 133 | speakup_info.port_tts + UART_MCR); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 134 | /* Turn on Interrupts */ |
| 135 | outb(UART_IER_MSI|UART_IER_RLSI|UART_IER_RDI, |
| 136 | speakup_info.port_tts + UART_IER); |
Varsha Rao | e3bab5e | 2017-02-22 23:16:40 +0530 | [diff] [blame] | 137 | inb(speakup_info.port_tts + UART_LSR); |
| 138 | inb(speakup_info.port_tts + UART_RX); |
| 139 | inb(speakup_info.port_tts + UART_IIR); |
| 140 | inb(speakup_info.port_tts + UART_MSR); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 141 | outb(1, speakup_info.port_tts + UART_FCR); /* Turn FIFO On */ |
| 142 | } |
| 143 | |
Okash Khawaja | be223d5 | 2017-04-22 09:00:28 +0100 | [diff] [blame] | 144 | static void spk_serial_send_xchar(char ch) |
| 145 | { |
| 146 | int timeout = SPK_XMITR_TIMEOUT; |
| 147 | |
| 148 | while (spk_serial_tx_busy()) { |
| 149 | if (!--timeout) |
| 150 | break; |
| 151 | udelay(1); |
| 152 | } |
| 153 | outb(ch, speakup_info.port_tts); |
| 154 | } |
| 155 | |
| 156 | static void spk_serial_tiocmset(unsigned int set, unsigned int clear) |
| 157 | { |
| 158 | int old = inb(speakup_info.port_tts + UART_MCR); |
| 159 | outb((old & ~clear) | set, speakup_info.port_tts + UART_MCR); |
| 160 | } |
| 161 | |
Okash Khawaja | 98c1fda | 2017-03-16 08:10:17 +0000 | [diff] [blame] | 162 | int spk_serial_synth_probe(struct spk_synth *synth) |
| 163 | { |
| 164 | const struct old_serial_port *ser; |
| 165 | int failed = 0; |
| 166 | |
| 167 | if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) { |
| 168 | ser = spk_serial_init(synth->ser); |
| 169 | if (!ser) { |
| 170 | failed = -1; |
| 171 | } else { |
| 172 | outb_p(0, ser->port); |
| 173 | mdelay(1); |
| 174 | outb_p('\r', ser->port); |
| 175 | } |
| 176 | } else { |
| 177 | failed = -1; |
| 178 | pr_warn("ttyS%i is an invalid port\n", synth->ser); |
| 179 | } |
| 180 | if (failed) { |
| 181 | pr_info("%s: not found\n", synth->long_name); |
| 182 | return -ENODEV; |
| 183 | } |
| 184 | pr_info("%s: ttyS%i, Driver Version %s\n", |
| 185 | synth->long_name, synth->ser, synth->version); |
| 186 | synth->alive = 1; |
| 187 | return 0; |
| 188 | } |
| 189 | EXPORT_SYMBOL_GPL(spk_serial_synth_probe); |
| 190 | |
Samuel Thibault | ca2beaf | 2013-01-02 02:37:40 +0100 | [diff] [blame] | 191 | void spk_stop_serial_interrupt(void) |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 192 | { |
| 193 | if (speakup_info.port_tts == 0) |
| 194 | return; |
| 195 | |
Shraddha Barke | 114885e | 2015-09-11 11:32:27 +0530 | [diff] [blame] | 196 | if (!synth->read_buff_add) |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 197 | return; |
| 198 | |
| 199 | /* Turn off interrupts */ |
Varsha Rao | e3bab5e | 2017-02-22 23:16:40 +0530 | [diff] [blame] | 200 | outb(0, speakup_info.port_tts + UART_IER); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 201 | /* Free IRQ */ |
Shiva Kerdel | d290eff | 2016-11-06 15:09:18 +0100 | [diff] [blame] | 202 | free_irq(serstate->irq, (void *)synth_readbuf_handler); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 203 | } |
Okash Khawaja | a50ef31 | 2017-03-14 13:41:54 +0000 | [diff] [blame] | 204 | EXPORT_SYMBOL_GPL(spk_stop_serial_interrupt); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 205 | |
Okash Khawaja | 9176d15 | 2017-03-14 13:41:52 +0000 | [diff] [blame] | 206 | int spk_wait_for_xmitr(struct spk_synth *in_synth) |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 207 | { |
| 208 | int tmout = SPK_XMITR_TIMEOUT; |
Domagoj Trsan | 8e69a81 | 2014-09-09 20:04:34 +0200 | [diff] [blame] | 209 | |
Okash Khawaja | 9176d15 | 2017-03-14 13:41:52 +0000 | [diff] [blame] | 210 | if ((in_synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) { |
William Hubbs | baf9ac9 | 2010-10-15 22:13:36 -0500 | [diff] [blame] | 211 | pr_warn("%s: too many timeouts, deactivating speakup\n", |
Okash Khawaja | 9176d15 | 2017-03-14 13:41:52 +0000 | [diff] [blame] | 212 | in_synth->long_name); |
| 213 | in_synth->alive = 0; |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 214 | /* No synth any more, so nobody will restart TTYs, and we thus |
| 215 | * need to do it ourselves. Now that there is no synth we can |
Aleksei Fedotov | 13d825e | 2015-08-14 22:34:37 +0300 | [diff] [blame] | 216 | * let application flood anyway |
| 217 | */ |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 218 | speakup_start_ttys(); |
| 219 | timeouts = 0; |
| 220 | return 0; |
| 221 | } |
| 222 | while (spk_serial_tx_busy()) { |
| 223 | if (--tmout == 0) { |
Okash Khawaja | 9176d15 | 2017-03-14 13:41:52 +0000 | [diff] [blame] | 224 | pr_warn("%s: timed out (tx busy)\n", in_synth->long_name); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 225 | timeouts++; |
| 226 | return 0; |
| 227 | } |
| 228 | udelay(1); |
| 229 | } |
| 230 | tmout = SPK_CTS_TIMEOUT; |
| 231 | while (!((inb_p(speakup_info.port_tts + UART_MSR)) & UART_MSR_CTS)) { |
| 232 | /* CTS */ |
| 233 | if (--tmout == 0) { |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 234 | timeouts++; |
| 235 | return 0; |
| 236 | } |
| 237 | udelay(1); |
| 238 | } |
| 239 | timeouts = 0; |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | unsigned char spk_serial_in(void) |
| 244 | { |
| 245 | int tmout = SPK_SERIAL_TIMEOUT; |
| 246 | |
| 247 | while (!(inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR)) { |
| 248 | if (--tmout == 0) { |
| 249 | pr_warn("time out while waiting for input.\n"); |
| 250 | return 0xff; |
| 251 | } |
| 252 | udelay(1); |
| 253 | } |
| 254 | return inb_p(speakup_info.port_tts + UART_RX); |
| 255 | } |
| 256 | EXPORT_SYMBOL_GPL(spk_serial_in); |
| 257 | |
| 258 | unsigned char spk_serial_in_nowait(void) |
| 259 | { |
| 260 | unsigned char lsr; |
| 261 | |
| 262 | lsr = inb_p(speakup_info.port_tts + UART_LSR); |
| 263 | if (!(lsr & UART_LSR_DR)) |
| 264 | return 0; |
| 265 | return inb_p(speakup_info.port_tts + UART_RX); |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(spk_serial_in_nowait); |
| 268 | |
Gustavo A. R. Silva | a15505e | 2017-03-27 01:37:29 -0500 | [diff] [blame] | 269 | static int spk_serial_out(struct spk_synth *in_synth, const char ch) |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 270 | { |
Okash Khawaja | 9176d15 | 2017-03-14 13:41:52 +0000 | [diff] [blame] | 271 | if (in_synth->alive && spk_wait_for_xmitr(in_synth)) { |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 272 | outb_p(ch, speakup_info.port_tts); |
| 273 | return 1; |
| 274 | } |
| 275 | return 0; |
| 276 | } |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 277 | |
Okash Khawaja | 98c1fda | 2017-03-16 08:10:17 +0000 | [diff] [blame] | 278 | const char *spk_serial_synth_immediate(struct spk_synth *synth, const char *buff) |
| 279 | { |
| 280 | u_char ch; |
| 281 | |
| 282 | while ((ch = *buff)) { |
| 283 | if (ch == '\n') |
| 284 | ch = synth->procspeech; |
| 285 | if (spk_wait_for_xmitr(synth)) |
| 286 | outb(ch, speakup_info.port_tts); |
| 287 | else |
| 288 | return buff; |
| 289 | buff++; |
| 290 | } |
| 291 | return NULL; |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(spk_serial_synth_immediate); |
| 294 | |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 295 | void spk_serial_release(void) |
| 296 | { |
Okash Khawaja | a50ef31 | 2017-03-14 13:41:54 +0000 | [diff] [blame] | 297 | spk_stop_serial_interrupt(); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 298 | if (speakup_info.port_tts == 0) |
| 299 | return; |
| 300 | synth_release_region(speakup_info.port_tts, 8); |
| 301 | speakup_info.port_tts = 0; |
| 302 | } |
| 303 | EXPORT_SYMBOL_GPL(spk_serial_release); |