serialio.c 7.9 KB

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