serialio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <linux/interrupt.h>
  2. #include <linux/ioport.h>
  3. #include "spk_types.h"
  4. #include "speakup.h"
  5. #include "spk_priv.h"
  6. #include "serialio.h"
  7. #include <linux/serial_core.h>
  8. /* WARNING: Do not change this to <linux/serial.h> without testing that
  9. * SERIAL_PORT_DFNS does get defined to the appropriate value. */
  10. #include <asm/serial.h>
  11. #ifndef SERIAL_PORT_DFNS
  12. #define SERIAL_PORT_DFNS
  13. #endif
  14. static void start_serial_interrupt(int irq);
  15. static const struct old_serial_port rs_table[] = {
  16. SERIAL_PORT_DFNS
  17. };
  18. static const struct old_serial_port *serstate;
  19. static int timeouts;
  20. const struct old_serial_port *spk_serial_init(int index)
  21. {
  22. int baud = 9600, quot = 0;
  23. unsigned int cval = 0;
  24. int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8;
  25. const struct old_serial_port *ser;
  26. int err;
  27. if (index >= ARRAY_SIZE(rs_table)) {
  28. pr_info("no port info for ttyS%d\n", index);
  29. return NULL;
  30. }
  31. ser = rs_table + index;
  32. /* Divisor, bytesize and parity */
  33. quot = ser->baud_base / baud;
  34. cval = cflag & (CSIZE | CSTOPB);
  35. #if defined(__powerpc__) || defined(__alpha__)
  36. cval >>= 8;
  37. #else /* !__powerpc__ && !__alpha__ */
  38. cval >>= 4;
  39. #endif /* !__powerpc__ && !__alpha__ */
  40. if (cflag & PARENB)
  41. cval |= UART_LCR_PARITY;
  42. if (!(cflag & PARODD))
  43. cval |= UART_LCR_EPAR;
  44. if (synth_request_region(ser->port, 8)) {
  45. /* try to take it back. */
  46. pr_info("Ports not available, trying to steal them\n");
  47. __release_region(&ioport_resource, ser->port, 8);
  48. err = synth_request_region(ser->port, 8);
  49. if (err) {
  50. pr_warn("Unable to allocate port at %x, errno %i",
  51. ser->port, err);
  52. return NULL;
  53. }
  54. }
  55. /* Disable UART interrupts, set DTR and RTS high
  56. * and set speed.
  57. */
  58. outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
  59. outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
  60. outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
  61. outb(cval, ser->port + UART_LCR); /* reset DLAB */
  62. /* Turn off Interrupts */
  63. outb(0, ser->port + UART_IER);
  64. outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
  65. /* If we read 0xff from the LSR, there is no UART here. */
  66. if (inb(ser->port + UART_LSR) == 0xff) {
  67. synth_release_region(ser->port, 8);
  68. serstate = NULL;
  69. return NULL;
  70. }
  71. mdelay(1);
  72. speakup_info.port_tts = ser->port;
  73. serstate = ser;
  74. start_serial_interrupt(ser->irq);
  75. return ser;
  76. }
  77. static irqreturn_t synth_readbuf_handler(int irq, void *dev_id)
  78. {
  79. unsigned long flags;
  80. /*printk(KERN_ERR "in irq\n"); */
  81. /*pr_warn("in IRQ\n"); */
  82. int c;
  83. spin_lock_irqsave(&speakup_info.spinlock, flags);
  84. while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) {
  85. c = inb_p(speakup_info.port_tts+UART_RX);
  86. synth->read_buff_add((u_char) c);
  87. /*printk(KERN_ERR "c = %d\n", c); */
  88. /*pr_warn("C = %d\n", c); */
  89. }
  90. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  91. return IRQ_HANDLED;
  92. }
  93. static void start_serial_interrupt(int irq)
  94. {
  95. int rv;
  96. if (!synth->read_buff_add)
  97. return;
  98. rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
  99. "serial", (void *) synth_readbuf_handler);
  100. if (rv)
  101. pr_err("Unable to request Speakup serial I R Q\n");
  102. /* Set MCR */
  103. outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2,
  104. speakup_info.port_tts + UART_MCR);
  105. /* Turn on Interrupts */
  106. outb(UART_IER_MSI|UART_IER_RLSI|UART_IER_RDI,
  107. speakup_info.port_tts + UART_IER);
  108. inb(speakup_info.port_tts+UART_LSR);
  109. inb(speakup_info.port_tts+UART_RX);
  110. inb(speakup_info.port_tts+UART_IIR);
  111. inb(speakup_info.port_tts+UART_MSR);
  112. outb(1, speakup_info.port_tts + UART_FCR); /* Turn FIFO On */
  113. }
  114. void spk_stop_serial_interrupt(void)
  115. {
  116. if (speakup_info.port_tts == 0)
  117. return;
  118. if (!synth->read_buff_add)
  119. return;
  120. /* Turn off interrupts */
  121. outb(0, speakup_info.port_tts+UART_IER);
  122. /* Free IRQ */
  123. free_irq(serstate->irq, (void *) synth_readbuf_handler);
  124. }
  125. int spk_wait_for_xmitr(void)
  126. {
  127. int tmout = SPK_XMITR_TIMEOUT;
  128. if ((synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) {
  129. pr_warn("%s: too many timeouts, deactivating speakup\n",
  130. synth->long_name);
  131. synth->alive = 0;
  132. /* No synth any more, so nobody will restart TTYs, and we thus
  133. * need to do it ourselves. Now that there is no synth we can
  134. * let application flood anyway
  135. */
  136. speakup_start_ttys();
  137. timeouts = 0;
  138. return 0;
  139. }
  140. while (spk_serial_tx_busy()) {
  141. if (--tmout == 0) {
  142. pr_warn("%s: timed out (tx busy)\n", synth->long_name);
  143. timeouts++;
  144. return 0;
  145. }
  146. udelay(1);
  147. }
  148. tmout = SPK_CTS_TIMEOUT;
  149. while (!((inb_p(speakup_info.port_tts + UART_MSR)) & UART_MSR_CTS)) {
  150. /* CTS */
  151. if (--tmout == 0) {
  152. /* pr_warn("%s: timed out (cts)\n",
  153. * synth->long_name);
  154. */
  155. timeouts++;
  156. return 0;
  157. }
  158. udelay(1);
  159. }
  160. timeouts = 0;
  161. return 1;
  162. }
  163. unsigned char spk_serial_in(void)
  164. {
  165. int tmout = SPK_SERIAL_TIMEOUT;
  166. while (!(inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR)) {
  167. if (--tmout == 0) {
  168. pr_warn("time out while waiting for input.\n");
  169. return 0xff;
  170. }
  171. udelay(1);
  172. }
  173. return inb_p(speakup_info.port_tts + UART_RX);
  174. }
  175. EXPORT_SYMBOL_GPL(spk_serial_in);
  176. unsigned char spk_serial_in_nowait(void)
  177. {
  178. unsigned char lsr;
  179. lsr = inb_p(speakup_info.port_tts + UART_LSR);
  180. if (!(lsr & UART_LSR_DR))
  181. return 0;
  182. return inb_p(speakup_info.port_tts + UART_RX);
  183. }
  184. EXPORT_SYMBOL_GPL(spk_serial_in_nowait);
  185. int spk_serial_out(const char ch)
  186. {
  187. if (synth->alive && spk_wait_for_xmitr()) {
  188. outb_p(ch, speakup_info.port_tts);
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(spk_serial_out);
  194. void spk_serial_release(void)
  195. {
  196. if (speakup_info.port_tts == 0)
  197. return;
  198. synth_release_region(speakup_info.port_tts, 8);
  199. speakup_info.port_tts = 0;
  200. }
  201. EXPORT_SYMBOL_GPL(spk_serial_release);