Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * SDIO UART/GPS driver
0004  *
0005  * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
0006  * by Russell King.
0007  *
0008  * Author:  Nicolas Pitre
0009  * Created: June 15, 2007
0010  * Copyright:   MontaVista Software, Inc.
0011  */
0012 
0013 /*
0014  * Note: Although this driver assumes a 16550A-like UART implementation,
0015  * it is not possible to leverage the common 8250/16550 driver, nor the
0016  * core UART infrastructure, as they assumes direct access to the hardware
0017  * registers, often under a spinlock.  This is not possible in the SDIO
0018  * context as SDIO access functions must be able to sleep.
0019  *
0020  * Because we need to lock the SDIO host to ensure an exclusive access to
0021  * the card, we simply rely on that lock to also prevent and serialize
0022  * concurrent access to the same port.
0023  */
0024 
0025 #include <linux/module.h>
0026 #include <linux/init.h>
0027 #include <linux/kernel.h>
0028 #include <linux/sched.h>
0029 #include <linux/mutex.h>
0030 #include <linux/seq_file.h>
0031 #include <linux/serial.h>
0032 #include <linux/serial_reg.h>
0033 #include <linux/circ_buf.h>
0034 #include <linux/tty.h>
0035 #include <linux/tty_flip.h>
0036 #include <linux/kfifo.h>
0037 #include <linux/slab.h>
0038 
0039 #include <linux/mmc/core.h>
0040 #include <linux/mmc/card.h>
0041 #include <linux/mmc/sdio_func.h>
0042 #include <linux/mmc/sdio_ids.h>
0043 
0044 
0045 #define UART_NR     8   /* Number of UARTs this driver can handle */
0046 
0047 
0048 #define FIFO_SIZE   PAGE_SIZE
0049 #define WAKEUP_CHARS    256
0050 
0051 struct uart_icount {
0052     __u32   cts;
0053     __u32   dsr;
0054     __u32   rng;
0055     __u32   dcd;
0056     __u32   rx;
0057     __u32   tx;
0058     __u32   frame;
0059     __u32   overrun;
0060     __u32   parity;
0061     __u32   brk;
0062 };
0063 
0064 struct sdio_uart_port {
0065     struct tty_port     port;
0066     unsigned int        index;
0067     struct sdio_func    *func;
0068     struct mutex        func_lock;
0069     struct task_struct  *in_sdio_uart_irq;
0070     unsigned int        regs_offset;
0071     struct kfifo        xmit_fifo;
0072     spinlock_t      write_lock;
0073     struct uart_icount  icount;
0074     unsigned int        uartclk;
0075     unsigned int        mctrl;
0076     unsigned int        rx_mctrl;
0077     unsigned int        read_status_mask;
0078     unsigned int        ignore_status_mask;
0079     unsigned char       x_char;
0080     unsigned char           ier;
0081     unsigned char           lcr;
0082 };
0083 
0084 static struct sdio_uart_port *sdio_uart_table[UART_NR];
0085 static DEFINE_SPINLOCK(sdio_uart_table_lock);
0086 
0087 static int sdio_uart_add_port(struct sdio_uart_port *port)
0088 {
0089     int index, ret = -EBUSY;
0090 
0091     mutex_init(&port->func_lock);
0092     spin_lock_init(&port->write_lock);
0093     if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
0094         return -ENOMEM;
0095 
0096     spin_lock(&sdio_uart_table_lock);
0097     for (index = 0; index < UART_NR; index++) {
0098         if (!sdio_uart_table[index]) {
0099             port->index = index;
0100             sdio_uart_table[index] = port;
0101             ret = 0;
0102             break;
0103         }
0104     }
0105     spin_unlock(&sdio_uart_table_lock);
0106 
0107     return ret;
0108 }
0109 
0110 static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
0111 {
0112     struct sdio_uart_port *port;
0113 
0114     if (index >= UART_NR)
0115         return NULL;
0116 
0117     spin_lock(&sdio_uart_table_lock);
0118     port = sdio_uart_table[index];
0119     if (port)
0120         tty_port_get(&port->port);
0121     spin_unlock(&sdio_uart_table_lock);
0122 
0123     return port;
0124 }
0125 
0126 static void sdio_uart_port_put(struct sdio_uart_port *port)
0127 {
0128     tty_port_put(&port->port);
0129 }
0130 
0131 static void sdio_uart_port_remove(struct sdio_uart_port *port)
0132 {
0133     struct sdio_func *func;
0134 
0135     spin_lock(&sdio_uart_table_lock);
0136     sdio_uart_table[port->index] = NULL;
0137     spin_unlock(&sdio_uart_table_lock);
0138 
0139     /*
0140      * We're killing a port that potentially still is in use by
0141      * the tty layer. Be careful to prevent any further access
0142      * to the SDIO function and arrange for the tty layer to
0143      * give up on that port ASAP.
0144      * Beware: the lock ordering is critical.
0145      */
0146     mutex_lock(&port->port.mutex);
0147     mutex_lock(&port->func_lock);
0148     func = port->func;
0149     sdio_claim_host(func);
0150     port->func = NULL;
0151     mutex_unlock(&port->func_lock);
0152     /* tty_hangup is async so is this safe as is ?? */
0153     tty_port_tty_hangup(&port->port, false);
0154     mutex_unlock(&port->port.mutex);
0155     sdio_release_irq(func);
0156     sdio_disable_func(func);
0157     sdio_release_host(func);
0158 
0159     sdio_uart_port_put(port);
0160 }
0161 
0162 static int sdio_uart_claim_func(struct sdio_uart_port *port)
0163 {
0164     mutex_lock(&port->func_lock);
0165     if (unlikely(!port->func)) {
0166         mutex_unlock(&port->func_lock);
0167         return -ENODEV;
0168     }
0169     if (likely(port->in_sdio_uart_irq != current))
0170         sdio_claim_host(port->func);
0171     mutex_unlock(&port->func_lock);
0172     return 0;
0173 }
0174 
0175 static inline void sdio_uart_release_func(struct sdio_uart_port *port)
0176 {
0177     if (likely(port->in_sdio_uart_irq != current))
0178         sdio_release_host(port->func);
0179 }
0180 
0181 static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
0182 {
0183     unsigned char c;
0184     c = sdio_readb(port->func, port->regs_offset + offset, NULL);
0185     return c;
0186 }
0187 
0188 static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
0189 {
0190     sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
0191 }
0192 
0193 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
0194 {
0195     unsigned char status;
0196     unsigned int ret;
0197 
0198     /* FIXME: What stops this losing the delta bits and breaking
0199        sdio_uart_check_modem_status ? */
0200     status = sdio_in(port, UART_MSR);
0201 
0202     ret = 0;
0203     if (status & UART_MSR_DCD)
0204         ret |= TIOCM_CAR;
0205     if (status & UART_MSR_RI)
0206         ret |= TIOCM_RNG;
0207     if (status & UART_MSR_DSR)
0208         ret |= TIOCM_DSR;
0209     if (status & UART_MSR_CTS)
0210         ret |= TIOCM_CTS;
0211     return ret;
0212 }
0213 
0214 static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
0215                   unsigned int mctrl)
0216 {
0217     unsigned char mcr = 0;
0218 
0219     if (mctrl & TIOCM_RTS)
0220         mcr |= UART_MCR_RTS;
0221     if (mctrl & TIOCM_DTR)
0222         mcr |= UART_MCR_DTR;
0223     if (mctrl & TIOCM_OUT1)
0224         mcr |= UART_MCR_OUT1;
0225     if (mctrl & TIOCM_OUT2)
0226         mcr |= UART_MCR_OUT2;
0227     if (mctrl & TIOCM_LOOP)
0228         mcr |= UART_MCR_LOOP;
0229 
0230     sdio_out(port, UART_MCR, mcr);
0231 }
0232 
0233 static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
0234                       unsigned int set, unsigned int clear)
0235 {
0236     unsigned int old;
0237 
0238     old = port->mctrl;
0239     port->mctrl = (old & ~clear) | set;
0240     if (old != port->mctrl)
0241         sdio_uart_write_mctrl(port, port->mctrl);
0242 }
0243 
0244 #define sdio_uart_set_mctrl(port, x)    sdio_uart_update_mctrl(port, x, 0)
0245 #define sdio_uart_clear_mctrl(port, x)  sdio_uart_update_mctrl(port, 0, x)
0246 
0247 static void sdio_uart_change_speed(struct sdio_uart_port *port,
0248                    struct ktermios *termios,
0249                    struct ktermios *old)
0250 {
0251     unsigned char cval, fcr = 0;
0252     unsigned int baud, quot;
0253 
0254     cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
0255 
0256     if (termios->c_cflag & CSTOPB)
0257         cval |= UART_LCR_STOP;
0258     if (termios->c_cflag & PARENB)
0259         cval |= UART_LCR_PARITY;
0260     if (!(termios->c_cflag & PARODD))
0261         cval |= UART_LCR_EPAR;
0262 
0263     for (;;) {
0264         baud = tty_termios_baud_rate(termios);
0265         if (baud == 0)
0266             baud = 9600;  /* Special case: B0 rate. */
0267         if (baud <= port->uartclk)
0268             break;
0269         /*
0270          * Oops, the quotient was zero.  Try again with the old
0271          * baud rate if possible, otherwise default to 9600.
0272          */
0273         termios->c_cflag &= ~CBAUD;
0274         if (old) {
0275             termios->c_cflag |= old->c_cflag & CBAUD;
0276             old = NULL;
0277         } else
0278             termios->c_cflag |= B9600;
0279     }
0280     quot = (2 * port->uartclk + baud) / (2 * baud);
0281 
0282     if (baud < 2400)
0283         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
0284     else
0285         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
0286 
0287     port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
0288     if (termios->c_iflag & INPCK)
0289         port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
0290     if (termios->c_iflag & (BRKINT | PARMRK))
0291         port->read_status_mask |= UART_LSR_BI;
0292 
0293     /*
0294      * Characters to ignore
0295      */
0296     port->ignore_status_mask = 0;
0297     if (termios->c_iflag & IGNPAR)
0298         port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
0299     if (termios->c_iflag & IGNBRK) {
0300         port->ignore_status_mask |= UART_LSR_BI;
0301         /*
0302          * If we're ignoring parity and break indicators,
0303          * ignore overruns too (for real raw support).
0304          */
0305         if (termios->c_iflag & IGNPAR)
0306             port->ignore_status_mask |= UART_LSR_OE;
0307     }
0308 
0309     /*
0310      * ignore all characters if CREAD is not set
0311      */
0312     if ((termios->c_cflag & CREAD) == 0)
0313         port->ignore_status_mask |= UART_LSR_DR;
0314 
0315     /*
0316      * CTS flow control flag and modem status interrupts
0317      */
0318     port->ier &= ~UART_IER_MSI;
0319     if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
0320         port->ier |= UART_IER_MSI;
0321 
0322     port->lcr = cval;
0323 
0324     sdio_out(port, UART_IER, port->ier);
0325     sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
0326     sdio_out(port, UART_DLL, quot & 0xff);
0327     sdio_out(port, UART_DLM, quot >> 8);
0328     sdio_out(port, UART_LCR, cval);
0329     sdio_out(port, UART_FCR, fcr);
0330 
0331     sdio_uart_write_mctrl(port, port->mctrl);
0332 }
0333 
0334 static void sdio_uart_start_tx(struct sdio_uart_port *port)
0335 {
0336     if (!(port->ier & UART_IER_THRI)) {
0337         port->ier |= UART_IER_THRI;
0338         sdio_out(port, UART_IER, port->ier);
0339     }
0340 }
0341 
0342 static void sdio_uart_stop_tx(struct sdio_uart_port *port)
0343 {
0344     if (port->ier & UART_IER_THRI) {
0345         port->ier &= ~UART_IER_THRI;
0346         sdio_out(port, UART_IER, port->ier);
0347     }
0348 }
0349 
0350 static void sdio_uart_stop_rx(struct sdio_uart_port *port)
0351 {
0352     port->ier &= ~UART_IER_RLSI;
0353     port->read_status_mask &= ~UART_LSR_DR;
0354     sdio_out(port, UART_IER, port->ier);
0355 }
0356 
0357 static void sdio_uart_receive_chars(struct sdio_uart_port *port,
0358                     unsigned int *status)
0359 {
0360     unsigned int ch, flag;
0361     int max_count = 256;
0362 
0363     do {
0364         ch = sdio_in(port, UART_RX);
0365         flag = TTY_NORMAL;
0366         port->icount.rx++;
0367 
0368         if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
0369                     UART_LSR_FE | UART_LSR_OE))) {
0370             /*
0371              * For statistics only
0372              */
0373             if (*status & UART_LSR_BI) {
0374                 *status &= ~(UART_LSR_FE | UART_LSR_PE);
0375                 port->icount.brk++;
0376             } else if (*status & UART_LSR_PE)
0377                 port->icount.parity++;
0378             else if (*status & UART_LSR_FE)
0379                 port->icount.frame++;
0380             if (*status & UART_LSR_OE)
0381                 port->icount.overrun++;
0382 
0383             /*
0384              * Mask off conditions which should be ignored.
0385              */
0386             *status &= port->read_status_mask;
0387             if (*status & UART_LSR_BI)
0388                 flag = TTY_BREAK;
0389             else if (*status & UART_LSR_PE)
0390                 flag = TTY_PARITY;
0391             else if (*status & UART_LSR_FE)
0392                 flag = TTY_FRAME;
0393         }
0394 
0395         if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
0396             tty_insert_flip_char(&port->port, ch, flag);
0397 
0398         /*
0399          * Overrun is special.  Since it's reported immediately,
0400          * it doesn't affect the current character.
0401          */
0402         if (*status & ~port->ignore_status_mask & UART_LSR_OE)
0403             tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
0404 
0405         *status = sdio_in(port, UART_LSR);
0406     } while ((*status & UART_LSR_DR) && (max_count-- > 0));
0407 
0408     tty_flip_buffer_push(&port->port);
0409 }
0410 
0411 static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
0412 {
0413     struct kfifo *xmit = &port->xmit_fifo;
0414     int count;
0415     struct tty_struct *tty;
0416     u8 iobuf[16];
0417     int len;
0418 
0419     if (port->x_char) {
0420         sdio_out(port, UART_TX, port->x_char);
0421         port->icount.tx++;
0422         port->x_char = 0;
0423         return;
0424     }
0425 
0426     tty = tty_port_tty_get(&port->port);
0427 
0428     if (tty == NULL || !kfifo_len(xmit) ||
0429                 tty->flow.stopped || tty->hw_stopped) {
0430         sdio_uart_stop_tx(port);
0431         tty_kref_put(tty);
0432         return;
0433     }
0434 
0435     len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
0436     for (count = 0; count < len; count++) {
0437         sdio_out(port, UART_TX, iobuf[count]);
0438         port->icount.tx++;
0439     }
0440 
0441     len = kfifo_len(xmit);
0442     if (len < WAKEUP_CHARS) {
0443         tty_wakeup(tty);
0444         if (len == 0)
0445             sdio_uart_stop_tx(port);
0446     }
0447     tty_kref_put(tty);
0448 }
0449 
0450 static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
0451 {
0452     int status;
0453     struct tty_struct *tty;
0454 
0455     status = sdio_in(port, UART_MSR);
0456 
0457     if ((status & UART_MSR_ANY_DELTA) == 0)
0458         return;
0459 
0460     if (status & UART_MSR_TERI)
0461         port->icount.rng++;
0462     if (status & UART_MSR_DDSR)
0463         port->icount.dsr++;
0464     if (status & UART_MSR_DDCD) {
0465         port->icount.dcd++;
0466         /* DCD raise - wake for open */
0467         if (status & UART_MSR_DCD)
0468             wake_up_interruptible(&port->port.open_wait);
0469         else {
0470             /* DCD drop - hang up if tty attached */
0471             tty_port_tty_hangup(&port->port, false);
0472         }
0473     }
0474     if (status & UART_MSR_DCTS) {
0475         port->icount.cts++;
0476         tty = tty_port_tty_get(&port->port);
0477         if (tty && C_CRTSCTS(tty)) {
0478             int cts = (status & UART_MSR_CTS);
0479             if (tty->hw_stopped) {
0480                 if (cts) {
0481                     tty->hw_stopped = 0;
0482                     sdio_uart_start_tx(port);
0483                     tty_wakeup(tty);
0484                 }
0485             } else {
0486                 if (!cts) {
0487                     tty->hw_stopped = 1;
0488                     sdio_uart_stop_tx(port);
0489                 }
0490             }
0491         }
0492         tty_kref_put(tty);
0493     }
0494 }
0495 
0496 /*
0497  * This handles the interrupt from one port.
0498  */
0499 static void sdio_uart_irq(struct sdio_func *func)
0500 {
0501     struct sdio_uart_port *port = sdio_get_drvdata(func);
0502     unsigned int iir, lsr;
0503 
0504     /*
0505      * In a few places sdio_uart_irq() is called directly instead of
0506      * waiting for the actual interrupt to be raised and the SDIO IRQ
0507      * thread scheduled in order to reduce latency.  However, some
0508      * interaction with the tty core may end up calling us back
0509      * (serial echo, flow control, etc.) through those same places
0510      * causing undesirable effects.  Let's stop the recursion here.
0511      */
0512     if (unlikely(port->in_sdio_uart_irq == current))
0513         return;
0514 
0515     iir = sdio_in(port, UART_IIR);
0516     if (iir & UART_IIR_NO_INT)
0517         return;
0518 
0519     port->in_sdio_uart_irq = current;
0520     lsr = sdio_in(port, UART_LSR);
0521     if (lsr & UART_LSR_DR)
0522         sdio_uart_receive_chars(port, &lsr);
0523     sdio_uart_check_modem_status(port);
0524     if (lsr & UART_LSR_THRE)
0525         sdio_uart_transmit_chars(port);
0526     port->in_sdio_uart_irq = NULL;
0527 }
0528 
0529 static int uart_carrier_raised(struct tty_port *tport)
0530 {
0531     struct sdio_uart_port *port =
0532             container_of(tport, struct sdio_uart_port, port);
0533     unsigned int ret = sdio_uart_claim_func(port);
0534     if (ret)    /* Missing hardware shouldn't block for carrier */
0535         return 1;
0536     ret = sdio_uart_get_mctrl(port);
0537     sdio_uart_release_func(port);
0538     if (ret & TIOCM_CAR)
0539         return 1;
0540     return 0;
0541 }
0542 
0543 /**
0544  *  uart_dtr_rts        -    port helper to set uart signals
0545  *  @tport: tty port to be updated
0546  *  @onoff: set to turn on DTR/RTS
0547  *
0548  *  Called by the tty port helpers when the modem signals need to be
0549  *  adjusted during an open, close and hangup.
0550  */
0551 
0552 static void uart_dtr_rts(struct tty_port *tport, int onoff)
0553 {
0554     struct sdio_uart_port *port =
0555             container_of(tport, struct sdio_uart_port, port);
0556     int ret = sdio_uart_claim_func(port);
0557     if (ret)
0558         return;
0559     if (onoff == 0)
0560         sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
0561     else
0562         sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
0563     sdio_uart_release_func(port);
0564 }
0565 
0566 /**
0567  *  sdio_uart_activate  -   start up hardware
0568  *  @tport: tty port to activate
0569  *  @tty: tty bound to this port
0570  *
0571  *  Activate a tty port. The port locking guarantees us this will be
0572  *  run exactly once per set of opens, and if successful will see the
0573  *  shutdown method run exactly once to match. Start up and shutdown are
0574  *  protected from each other by the internal locking and will not run
0575  *  at the same time even during a hangup event.
0576  *
0577  *  If we successfully start up the port we take an extra kref as we
0578  *  will keep it around until shutdown when the kref is dropped.
0579  */
0580 
0581 static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
0582 {
0583     struct sdio_uart_port *port =
0584             container_of(tport, struct sdio_uart_port, port);
0585     int ret;
0586 
0587     /*
0588      * Set the TTY IO error marker - we will only clear this
0589      * once we have successfully opened the port.
0590      */
0591     set_bit(TTY_IO_ERROR, &tty->flags);
0592 
0593     kfifo_reset(&port->xmit_fifo);
0594 
0595     ret = sdio_uart_claim_func(port);
0596     if (ret)
0597         return ret;
0598     ret = sdio_enable_func(port->func);
0599     if (ret)
0600         goto err1;
0601     ret = sdio_claim_irq(port->func, sdio_uart_irq);
0602     if (ret)
0603         goto err2;
0604 
0605     /*
0606      * Clear the FIFO buffers and disable them.
0607      * (they will be reenabled in sdio_change_speed())
0608      */
0609     sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
0610     sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
0611                UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
0612     sdio_out(port, UART_FCR, 0);
0613 
0614     /*
0615      * Clear the interrupt registers.
0616      */
0617     (void) sdio_in(port, UART_LSR);
0618     (void) sdio_in(port, UART_RX);
0619     (void) sdio_in(port, UART_IIR);
0620     (void) sdio_in(port, UART_MSR);
0621 
0622     /*
0623      * Now, initialize the UART
0624      */
0625     sdio_out(port, UART_LCR, UART_LCR_WLEN8);
0626 
0627     port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
0628     port->mctrl = TIOCM_OUT2;
0629 
0630     sdio_uart_change_speed(port, &tty->termios, NULL);
0631 
0632     if (C_BAUD(tty))
0633         sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
0634 
0635     if (C_CRTSCTS(tty))
0636         if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
0637             tty->hw_stopped = 1;
0638 
0639     clear_bit(TTY_IO_ERROR, &tty->flags);
0640 
0641     /* Kick the IRQ handler once while we're still holding the host lock */
0642     sdio_uart_irq(port->func);
0643 
0644     sdio_uart_release_func(port);
0645     return 0;
0646 
0647 err2:
0648     sdio_disable_func(port->func);
0649 err1:
0650     sdio_uart_release_func(port);
0651     return ret;
0652 }
0653 
0654 /**
0655  *  sdio_uart_shutdown  -   stop hardware
0656  *  @tport: tty port to shut down
0657  *
0658  *  Deactivate a tty port. The port locking guarantees us this will be
0659  *  run only if a successful matching activate already ran. The two are
0660  *  protected from each other by the internal locking and will not run
0661  *  at the same time even during a hangup event.
0662  */
0663 
0664 static void sdio_uart_shutdown(struct tty_port *tport)
0665 {
0666     struct sdio_uart_port *port =
0667             container_of(tport, struct sdio_uart_port, port);
0668     int ret;
0669 
0670     ret = sdio_uart_claim_func(port);
0671     if (ret)
0672         return;
0673 
0674     sdio_uart_stop_rx(port);
0675 
0676     /* Disable interrupts from this port */
0677     sdio_release_irq(port->func);
0678     port->ier = 0;
0679     sdio_out(port, UART_IER, 0);
0680 
0681     sdio_uart_clear_mctrl(port, TIOCM_OUT2);
0682 
0683     /* Disable break condition and FIFOs. */
0684     port->lcr &= ~UART_LCR_SBC;
0685     sdio_out(port, UART_LCR, port->lcr);
0686     sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
0687                  UART_FCR_CLEAR_RCVR |
0688                  UART_FCR_CLEAR_XMIT);
0689     sdio_out(port, UART_FCR, 0);
0690 
0691     sdio_disable_func(port->func);
0692 
0693     sdio_uart_release_func(port);
0694 }
0695 
0696 static void sdio_uart_port_destroy(struct tty_port *tport)
0697 {
0698     struct sdio_uart_port *port =
0699         container_of(tport, struct sdio_uart_port, port);
0700     kfifo_free(&port->xmit_fifo);
0701     kfree(port);
0702 }
0703 
0704 /**
0705  *  sdio_uart_install   -   install method
0706  *  @driver: the driver in use (sdio_uart in our case)
0707  *  @tty: the tty being bound
0708  *
0709  *  Look up and bind the tty and the driver together. Initialize
0710  *  any needed private data (in our case the termios)
0711  */
0712 
0713 static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
0714 {
0715     int idx = tty->index;
0716     struct sdio_uart_port *port = sdio_uart_port_get(idx);
0717     int ret = tty_standard_install(driver, tty);
0718 
0719     if (ret == 0)
0720         /* This is the ref sdio_uart_port get provided */
0721         tty->driver_data = port;
0722     else
0723         sdio_uart_port_put(port);
0724     return ret;
0725 }
0726 
0727 /**
0728  *  sdio_uart_cleanup   -   called on the last tty kref drop
0729  *  @tty: the tty being destroyed
0730  *
0731  *  Called asynchronously when the last reference to the tty is dropped.
0732  *  We cannot destroy the tty->driver_data port kref until this point
0733  */
0734 
0735 static void sdio_uart_cleanup(struct tty_struct *tty)
0736 {
0737     struct sdio_uart_port *port = tty->driver_data;
0738     tty->driver_data = NULL;    /* Bug trap */
0739     sdio_uart_port_put(port);
0740 }
0741 
0742 /*
0743  *  Open/close/hangup is now entirely boilerplate
0744  */
0745 
0746 static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
0747 {
0748     struct sdio_uart_port *port = tty->driver_data;
0749     return tty_port_open(&port->port, tty, filp);
0750 }
0751 
0752 static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
0753 {
0754     struct sdio_uart_port *port = tty->driver_data;
0755     tty_port_close(&port->port, tty, filp);
0756 }
0757 
0758 static void sdio_uart_hangup(struct tty_struct *tty)
0759 {
0760     struct sdio_uart_port *port = tty->driver_data;
0761     tty_port_hangup(&port->port);
0762 }
0763 
0764 static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
0765                int count)
0766 {
0767     struct sdio_uart_port *port = tty->driver_data;
0768     int ret;
0769 
0770     if (!port->func)
0771         return -ENODEV;
0772 
0773     ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
0774     if (!(port->ier & UART_IER_THRI)) {
0775         int err = sdio_uart_claim_func(port);
0776         if (!err) {
0777             sdio_uart_start_tx(port);
0778             sdio_uart_irq(port->func);
0779             sdio_uart_release_func(port);
0780         } else
0781             ret = err;
0782     }
0783 
0784     return ret;
0785 }
0786 
0787 static unsigned int sdio_uart_write_room(struct tty_struct *tty)
0788 {
0789     struct sdio_uart_port *port = tty->driver_data;
0790     return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
0791 }
0792 
0793 static unsigned int sdio_uart_chars_in_buffer(struct tty_struct *tty)
0794 {
0795     struct sdio_uart_port *port = tty->driver_data;
0796     return kfifo_len(&port->xmit_fifo);
0797 }
0798 
0799 static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
0800 {
0801     struct sdio_uart_port *port = tty->driver_data;
0802 
0803     port->x_char = ch;
0804     if (ch && !(port->ier & UART_IER_THRI)) {
0805         if (sdio_uart_claim_func(port) != 0)
0806             return;
0807         sdio_uart_start_tx(port);
0808         sdio_uart_irq(port->func);
0809         sdio_uart_release_func(port);
0810     }
0811 }
0812 
0813 static void sdio_uart_throttle(struct tty_struct *tty)
0814 {
0815     struct sdio_uart_port *port = tty->driver_data;
0816 
0817     if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
0818         return;
0819 
0820     if (sdio_uart_claim_func(port) != 0)
0821         return;
0822 
0823     if (I_IXOFF(tty)) {
0824         port->x_char = STOP_CHAR(tty);
0825         sdio_uart_start_tx(port);
0826     }
0827 
0828     if (C_CRTSCTS(tty))
0829         sdio_uart_clear_mctrl(port, TIOCM_RTS);
0830 
0831     sdio_uart_irq(port->func);
0832     sdio_uart_release_func(port);
0833 }
0834 
0835 static void sdio_uart_unthrottle(struct tty_struct *tty)
0836 {
0837     struct sdio_uart_port *port = tty->driver_data;
0838 
0839     if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
0840         return;
0841 
0842     if (sdio_uart_claim_func(port) != 0)
0843         return;
0844 
0845     if (I_IXOFF(tty)) {
0846         if (port->x_char) {
0847             port->x_char = 0;
0848         } else {
0849             port->x_char = START_CHAR(tty);
0850             sdio_uart_start_tx(port);
0851         }
0852     }
0853 
0854     if (C_CRTSCTS(tty))
0855         sdio_uart_set_mctrl(port, TIOCM_RTS);
0856 
0857     sdio_uart_irq(port->func);
0858     sdio_uart_release_func(port);
0859 }
0860 
0861 static void sdio_uart_set_termios(struct tty_struct *tty,
0862                         struct ktermios *old_termios)
0863 {
0864     struct sdio_uart_port *port = tty->driver_data;
0865     unsigned int cflag = tty->termios.c_cflag;
0866 
0867     if (sdio_uart_claim_func(port) != 0)
0868         return;
0869 
0870     sdio_uart_change_speed(port, &tty->termios, old_termios);
0871 
0872     /* Handle transition to B0 status */
0873     if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
0874         sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
0875 
0876     /* Handle transition away from B0 status */
0877     if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
0878         unsigned int mask = TIOCM_DTR;
0879         if (!(cflag & CRTSCTS) || !tty_throttled(tty))
0880             mask |= TIOCM_RTS;
0881         sdio_uart_set_mctrl(port, mask);
0882     }
0883 
0884     /* Handle turning off CRTSCTS */
0885     if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
0886         tty->hw_stopped = 0;
0887         sdio_uart_start_tx(port);
0888     }
0889 
0890     /* Handle turning on CRTSCTS */
0891     if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
0892         if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
0893             tty->hw_stopped = 1;
0894             sdio_uart_stop_tx(port);
0895         }
0896     }
0897 
0898     sdio_uart_release_func(port);
0899 }
0900 
0901 static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
0902 {
0903     struct sdio_uart_port *port = tty->driver_data;
0904     int result;
0905 
0906     result = sdio_uart_claim_func(port);
0907     if (result != 0)
0908         return result;
0909 
0910     if (break_state == -1)
0911         port->lcr |= UART_LCR_SBC;
0912     else
0913         port->lcr &= ~UART_LCR_SBC;
0914     sdio_out(port, UART_LCR, port->lcr);
0915 
0916     sdio_uart_release_func(port);
0917     return 0;
0918 }
0919 
0920 static int sdio_uart_tiocmget(struct tty_struct *tty)
0921 {
0922     struct sdio_uart_port *port = tty->driver_data;
0923     int result;
0924 
0925     result = sdio_uart_claim_func(port);
0926     if (!result) {
0927         result = port->mctrl | sdio_uart_get_mctrl(port);
0928         sdio_uart_release_func(port);
0929     }
0930 
0931     return result;
0932 }
0933 
0934 static int sdio_uart_tiocmset(struct tty_struct *tty,
0935                   unsigned int set, unsigned int clear)
0936 {
0937     struct sdio_uart_port *port = tty->driver_data;
0938     int result;
0939 
0940     result = sdio_uart_claim_func(port);
0941     if (!result) {
0942         sdio_uart_update_mctrl(port, set, clear);
0943         sdio_uart_release_func(port);
0944     }
0945 
0946     return result;
0947 }
0948 
0949 static int sdio_uart_proc_show(struct seq_file *m, void *v)
0950 {
0951     int i;
0952 
0953     seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
0954                "", "", "");
0955     for (i = 0; i < UART_NR; i++) {
0956         struct sdio_uart_port *port = sdio_uart_port_get(i);
0957         if (port) {
0958             seq_printf(m, "%d: uart:SDIO", i);
0959             if (capable(CAP_SYS_ADMIN)) {
0960                 seq_printf(m, " tx:%d rx:%d",
0961                           port->icount.tx, port->icount.rx);
0962                 if (port->icount.frame)
0963                     seq_printf(m, " fe:%d",
0964                               port->icount.frame);
0965                 if (port->icount.parity)
0966                     seq_printf(m, " pe:%d",
0967                               port->icount.parity);
0968                 if (port->icount.brk)
0969                     seq_printf(m, " brk:%d",
0970                               port->icount.brk);
0971                 if (port->icount.overrun)
0972                     seq_printf(m, " oe:%d",
0973                               port->icount.overrun);
0974                 if (port->icount.cts)
0975                     seq_printf(m, " cts:%d",
0976                               port->icount.cts);
0977                 if (port->icount.dsr)
0978                     seq_printf(m, " dsr:%d",
0979                               port->icount.dsr);
0980                 if (port->icount.rng)
0981                     seq_printf(m, " rng:%d",
0982                               port->icount.rng);
0983                 if (port->icount.dcd)
0984                     seq_printf(m, " dcd:%d",
0985                               port->icount.dcd);
0986             }
0987             sdio_uart_port_put(port);
0988             seq_putc(m, '\n');
0989         }
0990     }
0991     return 0;
0992 }
0993 
0994 static const struct tty_port_operations sdio_uart_port_ops = {
0995     .dtr_rts = uart_dtr_rts,
0996     .carrier_raised = uart_carrier_raised,
0997     .shutdown = sdio_uart_shutdown,
0998     .activate = sdio_uart_activate,
0999     .destruct = sdio_uart_port_destroy,
1000 };
1001 
1002 static const struct tty_operations sdio_uart_ops = {
1003     .open           = sdio_uart_open,
1004     .close          = sdio_uart_close,
1005     .write          = sdio_uart_write,
1006     .write_room     = sdio_uart_write_room,
1007     .chars_in_buffer    = sdio_uart_chars_in_buffer,
1008     .send_xchar     = sdio_uart_send_xchar,
1009     .throttle       = sdio_uart_throttle,
1010     .unthrottle     = sdio_uart_unthrottle,
1011     .set_termios        = sdio_uart_set_termios,
1012     .hangup         = sdio_uart_hangup,
1013     .break_ctl      = sdio_uart_break_ctl,
1014     .tiocmget       = sdio_uart_tiocmget,
1015     .tiocmset       = sdio_uart_tiocmset,
1016     .install        = sdio_uart_install,
1017     .cleanup        = sdio_uart_cleanup,
1018     .proc_show      = sdio_uart_proc_show,
1019 };
1020 
1021 static struct tty_driver *sdio_uart_tty_driver;
1022 
1023 static int sdio_uart_probe(struct sdio_func *func,
1024                const struct sdio_device_id *id)
1025 {
1026     struct sdio_uart_port *port;
1027     int ret;
1028 
1029     port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1030     if (!port)
1031         return -ENOMEM;
1032 
1033     if (func->class == SDIO_CLASS_UART) {
1034         pr_warn("%s: need info on UART class basic setup\n",
1035             sdio_func_id(func));
1036         kfree(port);
1037         return -ENOSYS;
1038     } else if (func->class == SDIO_CLASS_GPS) {
1039         /*
1040          * We need tuple 0x91.  It contains SUBTPL_SIOREG
1041          * and SUBTPL_RCVCAPS.
1042          */
1043         struct sdio_func_tuple *tpl;
1044         for (tpl = func->tuples; tpl; tpl = tpl->next) {
1045             if (tpl->code != 0x91)
1046                 continue;
1047             if (tpl->size < 10)
1048                 continue;
1049             if (tpl->data[1] == 0)  /* SUBTPL_SIOREG */
1050                 break;
1051         }
1052         if (!tpl) {
1053             pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1054                 sdio_func_id(func));
1055             kfree(port);
1056             return -EINVAL;
1057         }
1058         pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1059                sdio_func_id(func), tpl->data[2], tpl->data[3]);
1060         port->regs_offset = (tpl->data[4] << 0) |
1061                     (tpl->data[5] << 8) |
1062                     (tpl->data[6] << 16);
1063         pr_debug("%s: regs offset = 0x%x\n",
1064                sdio_func_id(func), port->regs_offset);
1065         port->uartclk = tpl->data[7] * 115200;
1066         if (port->uartclk == 0)
1067             port->uartclk = 115200;
1068         pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
1069                sdio_func_id(func), port->uartclk,
1070                tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1071     } else {
1072         kfree(port);
1073         return -EINVAL;
1074     }
1075 
1076     port->func = func;
1077     sdio_set_drvdata(func, port);
1078     tty_port_init(&port->port);
1079     port->port.ops = &sdio_uart_port_ops;
1080 
1081     ret = sdio_uart_add_port(port);
1082     if (ret) {
1083         kfree(port);
1084     } else {
1085         struct device *dev;
1086         dev = tty_port_register_device(&port->port,
1087                 sdio_uart_tty_driver, port->index, &func->dev);
1088         if (IS_ERR(dev)) {
1089             sdio_uart_port_remove(port);
1090             ret = PTR_ERR(dev);
1091         }
1092     }
1093 
1094     return ret;
1095 }
1096 
1097 static void sdio_uart_remove(struct sdio_func *func)
1098 {
1099     struct sdio_uart_port *port = sdio_get_drvdata(func);
1100 
1101     tty_unregister_device(sdio_uart_tty_driver, port->index);
1102     sdio_uart_port_remove(port);
1103 }
1104 
1105 static const struct sdio_device_id sdio_uart_ids[] = {
1106     { SDIO_DEVICE_CLASS(SDIO_CLASS_UART)        },
1107     { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS)     },
1108     { /* end: all zeroes */             },
1109 };
1110 
1111 MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1112 
1113 static struct sdio_driver sdio_uart_driver = {
1114     .probe      = sdio_uart_probe,
1115     .remove     = sdio_uart_remove,
1116     .name       = "sdio_uart",
1117     .id_table   = sdio_uart_ids,
1118 };
1119 
1120 static int __init sdio_uart_init(void)
1121 {
1122     int ret;
1123     struct tty_driver *tty_drv;
1124 
1125     sdio_uart_tty_driver = tty_drv = tty_alloc_driver(UART_NR,
1126             TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV);
1127     if (IS_ERR(tty_drv))
1128         return PTR_ERR(tty_drv);
1129 
1130     tty_drv->driver_name = "sdio_uart";
1131     tty_drv->name =   "ttySDIO";
1132     tty_drv->major = 0;  /* dynamically allocated */
1133     tty_drv->minor_start = 0;
1134     tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1135     tty_drv->subtype = SERIAL_TYPE_NORMAL;
1136     tty_drv->init_termios = tty_std_termios;
1137     tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1138     tty_drv->init_termios.c_ispeed = 4800;
1139     tty_drv->init_termios.c_ospeed = 4800;
1140     tty_set_operations(tty_drv, &sdio_uart_ops);
1141 
1142     ret = tty_register_driver(tty_drv);
1143     if (ret)
1144         goto err1;
1145 
1146     ret = sdio_register_driver(&sdio_uart_driver);
1147     if (ret)
1148         goto err2;
1149 
1150     return 0;
1151 
1152 err2:
1153     tty_unregister_driver(tty_drv);
1154 err1:
1155     tty_driver_kref_put(tty_drv);
1156     return ret;
1157 }
1158 
1159 static void __exit sdio_uart_exit(void)
1160 {
1161     sdio_unregister_driver(&sdio_uart_driver);
1162     tty_unregister_driver(sdio_uart_tty_driver);
1163     tty_driver_kref_put(sdio_uart_tty_driver);
1164 }
1165 
1166 module_init(sdio_uart_init);
1167 module_exit(sdio_uart_exit);
1168 
1169 MODULE_AUTHOR("Nicolas Pitre");
1170 MODULE_LICENSE("GPL");