serialio.c 7.5 KB

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