Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/interrupt.h>
0003 #include <linux/ioport.h>
0004 
0005 #include "spk_types.h"
0006 #include "speakup.h"
0007 #include "spk_priv.h"
0008 #include "serialio.h"
0009 
0010 #include <linux/serial_core.h>
0011 /* WARNING:  Do not change this to <linux/serial.h> without testing that
0012  * SERIAL_PORT_DFNS does get defined to the appropriate value.
0013  */
0014 #include <asm/serial.h>
0015 
0016 #ifndef SERIAL_PORT_DFNS
0017 #define SERIAL_PORT_DFNS
0018 #endif
0019 
0020 static void start_serial_interrupt(int irq);
0021 
0022 static const struct old_serial_port rs_table[] = {
0023     SERIAL_PORT_DFNS
0024 };
0025 
0026 static const struct old_serial_port *serstate;
0027 static int timeouts;
0028 
0029 static int spk_serial_out(struct spk_synth *in_synth, const char ch);
0030 static void spk_serial_send_xchar(struct spk_synth *in_synth, char ch);
0031 static void spk_serial_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear);
0032 static unsigned char spk_serial_in(struct spk_synth *in_synth);
0033 static unsigned char spk_serial_in_nowait(struct spk_synth *in_synth);
0034 static void spk_serial_flush_buffer(struct spk_synth *in_synth);
0035 static int spk_serial_wait_for_xmitr(struct spk_synth *in_synth);
0036 
0037 struct spk_io_ops spk_serial_io_ops = {
0038     .synth_out = spk_serial_out,
0039     .send_xchar = spk_serial_send_xchar,
0040     .tiocmset = spk_serial_tiocmset,
0041     .synth_in = spk_serial_in,
0042     .synth_in_nowait = spk_serial_in_nowait,
0043     .flush_buffer = spk_serial_flush_buffer,
0044     .wait_for_xmitr = spk_serial_wait_for_xmitr,
0045 };
0046 EXPORT_SYMBOL_GPL(spk_serial_io_ops);
0047 
0048 const struct old_serial_port *spk_serial_init(int index)
0049 {
0050     int baud = 9600, quot = 0;
0051     unsigned int cval = 0;
0052     int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8;
0053     const struct old_serial_port *ser;
0054     int err;
0055 
0056     if (index >= ARRAY_SIZE(rs_table)) {
0057         pr_info("no port info for ttyS%d\n", index);
0058         return NULL;
0059     }
0060     ser = rs_table + index;
0061 
0062     /*  Divisor, byte size and parity */
0063     quot = ser->baud_base / baud;
0064     cval = cflag & (CSIZE | CSTOPB);
0065 #if defined(__powerpc__) || defined(__alpha__)
0066     cval >>= 8;
0067 #else /* !__powerpc__ && !__alpha__ */
0068     cval >>= 4;
0069 #endif /* !__powerpc__ && !__alpha__ */
0070     if (cflag & PARENB)
0071         cval |= UART_LCR_PARITY;
0072     if (!(cflag & PARODD))
0073         cval |= UART_LCR_EPAR;
0074     if (synth_request_region(ser->port, 8)) {
0075         /* try to take it back. */
0076         pr_info("Ports not available, trying to steal them\n");
0077         __release_region(&ioport_resource, ser->port, 8);
0078         err = synth_request_region(ser->port, 8);
0079         if (err) {
0080             pr_warn("Unable to allocate port at %x, errno %i",
0081                 ser->port, err);
0082             return NULL;
0083         }
0084     }
0085 
0086     /*  Disable UART interrupts, set DTR and RTS high
0087      *  and set speed.
0088      */
0089     outb(cval | UART_LCR_DLAB, ser->port + UART_LCR);   /* set DLAB */
0090     outb(quot & 0xff, ser->port + UART_DLL);    /* LS of divisor */
0091     outb(quot >> 8, ser->port + UART_DLM);      /* MS of divisor */
0092     outb(cval, ser->port + UART_LCR);       /* reset DLAB */
0093 
0094     /* Turn off Interrupts */
0095     outb(0, ser->port + UART_IER);
0096     outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
0097 
0098     /* If we read 0xff from the LSR, there is no UART here. */
0099     if (inb(ser->port + UART_LSR) == 0xff) {
0100         synth_release_region(ser->port, 8);
0101         serstate = NULL;
0102         return NULL;
0103     }
0104 
0105     mdelay(1);
0106     speakup_info.port_tts = ser->port;
0107     serstate = ser;
0108 
0109     start_serial_interrupt(ser->irq);
0110 
0111     return ser;
0112 }
0113 
0114 static irqreturn_t synth_readbuf_handler(int irq, void *dev_id)
0115 {
0116     unsigned long flags;
0117     int c;
0118 
0119     spin_lock_irqsave(&speakup_info.spinlock, flags);
0120     while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) {
0121         c = inb_p(speakup_info.port_tts + UART_RX);
0122         synth->read_buff_add((u_char)c);
0123     }
0124     spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0125     return IRQ_HANDLED;
0126 }
0127 
0128 static void start_serial_interrupt(int irq)
0129 {
0130     int rv;
0131 
0132     if (!synth->read_buff_add)
0133         return;
0134 
0135     rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
0136              "serial", (void *)synth_readbuf_handler);
0137 
0138     if (rv)
0139         pr_err("Unable to request Speakup serial I R Q\n");
0140     /* Set MCR */
0141     outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2,
0142          speakup_info.port_tts + UART_MCR);
0143     /* Turn on Interrupts */
0144     outb(UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI,
0145          speakup_info.port_tts + UART_IER);
0146     inb(speakup_info.port_tts + UART_LSR);
0147     inb(speakup_info.port_tts + UART_RX);
0148     inb(speakup_info.port_tts + UART_IIR);
0149     inb(speakup_info.port_tts + UART_MSR);
0150     outb(1, speakup_info.port_tts + UART_FCR);  /* Turn FIFO On */
0151 }
0152 
0153 static void spk_serial_send_xchar(struct spk_synth *synth, char ch)
0154 {
0155     int timeout = SPK_XMITR_TIMEOUT;
0156 
0157     while (spk_serial_tx_busy()) {
0158         if (!--timeout)
0159             break;
0160         udelay(1);
0161     }
0162     outb(ch, speakup_info.port_tts);
0163 }
0164 
0165 static void spk_serial_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear)
0166 {
0167     int old = inb(speakup_info.port_tts + UART_MCR);
0168 
0169     outb((old & ~clear) | set, speakup_info.port_tts + UART_MCR);
0170 }
0171 
0172 int spk_serial_synth_probe(struct spk_synth *synth)
0173 {
0174     const struct old_serial_port *ser;
0175     int failed = 0;
0176 
0177     if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) {
0178         ser = spk_serial_init(synth->ser);
0179         if (!ser) {
0180             failed = -1;
0181         } else {
0182             outb_p(0, ser->port);
0183             mdelay(1);
0184             outb_p('\r', ser->port);
0185         }
0186     } else {
0187         failed = -1;
0188         pr_warn("ttyS%i is an invalid port\n", synth->ser);
0189     }
0190     if (failed) {
0191         pr_info("%s: not found\n", synth->long_name);
0192         return -ENODEV;
0193     }
0194     pr_info("%s: ttyS%i, Driver Version %s\n",
0195         synth->long_name, synth->ser, synth->version);
0196     synth->alive = 1;
0197     return 0;
0198 }
0199 EXPORT_SYMBOL_GPL(spk_serial_synth_probe);
0200 
0201 void spk_stop_serial_interrupt(void)
0202 {
0203     if (speakup_info.port_tts == 0)
0204         return;
0205 
0206     if (!synth->read_buff_add)
0207         return;
0208 
0209     /* Turn off interrupts */
0210     outb(0, speakup_info.port_tts + UART_IER);
0211     /* Free IRQ */
0212     free_irq(serstate->irq, (void *)synth_readbuf_handler);
0213 }
0214 EXPORT_SYMBOL_GPL(spk_stop_serial_interrupt);
0215 
0216 static int spk_serial_wait_for_xmitr(struct spk_synth *in_synth)
0217 {
0218     int tmout = SPK_XMITR_TIMEOUT;
0219 
0220     if ((in_synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) {
0221         pr_warn("%s: too many timeouts, deactivating speakup\n",
0222             in_synth->long_name);
0223         in_synth->alive = 0;
0224         /* No synth any more, so nobody will restart TTYs, and we thus
0225          * need to do it ourselves.  Now that there is no synth we can
0226          * let application flood anyway
0227          */
0228         speakup_start_ttys();
0229         timeouts = 0;
0230         return 0;
0231     }
0232     while (spk_serial_tx_busy()) {
0233         if (--tmout == 0) {
0234             pr_warn("%s: timed out (tx busy)\n",
0235                 in_synth->long_name);
0236             timeouts++;
0237             return 0;
0238         }
0239         udelay(1);
0240     }
0241     tmout = SPK_CTS_TIMEOUT;
0242     while (!((inb_p(speakup_info.port_tts + UART_MSR)) & UART_MSR_CTS)) {
0243         /* CTS */
0244         if (--tmout == 0) {
0245             timeouts++;
0246             return 0;
0247         }
0248         udelay(1);
0249     }
0250     timeouts = 0;
0251     return 1;
0252 }
0253 
0254 static unsigned char spk_serial_in(struct spk_synth *in_synth)
0255 {
0256     int tmout = SPK_SERIAL_TIMEOUT;
0257 
0258     while (!(inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR)) {
0259         if (--tmout == 0) {
0260             pr_warn("time out while waiting for input.\n");
0261             return 0xff;
0262         }
0263         udelay(1);
0264     }
0265     return inb_p(speakup_info.port_tts + UART_RX);
0266 }
0267 
0268 static unsigned char spk_serial_in_nowait(struct spk_synth *in_synth)
0269 {
0270     unsigned char lsr;
0271 
0272     lsr = inb_p(speakup_info.port_tts + UART_LSR);
0273     if (!(lsr & UART_LSR_DR))
0274         return 0;
0275     return inb_p(speakup_info.port_tts + UART_RX);
0276 }
0277 
0278 static void spk_serial_flush_buffer(struct spk_synth *in_synth)
0279 {
0280     /* TODO: flush the UART 16550 buffer */
0281 }
0282 
0283 static int spk_serial_out(struct spk_synth *in_synth, const char ch)
0284 {
0285     if (in_synth->alive && spk_serial_wait_for_xmitr(in_synth)) {
0286         outb_p(ch, speakup_info.port_tts);
0287         return 1;
0288     }
0289     return 0;
0290 }
0291 
0292 const char *spk_serial_synth_immediate(struct spk_synth *synth,
0293                        const char *buff)
0294 {
0295     u_char ch;
0296 
0297     while ((ch = *buff)) {
0298         if (ch == '\n')
0299             ch = synth->procspeech;
0300         if (spk_serial_wait_for_xmitr(synth))
0301             outb(ch, speakup_info.port_tts);
0302         else
0303             return buff;
0304         buff++;
0305     }
0306     return NULL;
0307 }
0308 EXPORT_SYMBOL_GPL(spk_serial_synth_immediate);
0309 
0310 void spk_serial_release(struct spk_synth *synth)
0311 {
0312     spk_stop_serial_interrupt();
0313     if (speakup_info.port_tts == 0)
0314         return;
0315     synth_release_region(speakup_info.port_tts, 8);
0316     speakup_info.port_tts = 0;
0317 }
0318 EXPORT_SYMBOL_GPL(spk_serial_release);