2 * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
4 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
7 * Author: Nicolas Pitre
8 * Created: June 15, 2007
9 * Copyright: MontaVista Software, Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
18 * Note: Although this driver assumes a 16550A-like UART implementation,
19 * it is not possible to leverage the common 8250/16550 driver, nor the
20 * core UART infrastructure, as they assumes direct access to the hardware
21 * registers, often under a spinlock. This is not possible in the SDIO
22 * context as SDIO access functions must be able to sleep.
24 * Because we need to lock the SDIO host to ensure an exclusive access to
25 * the card, we simply rely on that lock to also prevent and serialize
26 * concurrent access to the same port.
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/mutex.h>
34 #include <linux/seq_file.h>
35 #include <linux/serial_reg.h>
36 #include <linux/circ_buf.h>
37 #include <linux/gfp.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
41 #include <linux/mmc/core.h>
42 #include <linux/mmc/card.h>
43 #include <linux/mmc/sdio_func.h>
44 #include <linux/mmc/sdio_ids.h>
47 #define UART_NR 8 /* Number of UARTs this driver can handle */
50 #define UART_XMIT_SIZE PAGE_SIZE
51 #define WAKEUP_CHARS 256
53 #define circ_empty(circ) ((circ)->head == (circ)->tail)
54 #define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
56 #define circ_chars_pending(circ) \
57 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
59 #define circ_chars_free(circ) \
60 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
76 struct sdio_uart_port {
79 struct tty_struct *tty;
81 struct sdio_func *func;
82 struct mutex func_lock;
83 struct task_struct *in_sdio_uart_irq;
84 unsigned int regs_offset;
86 spinlock_t write_lock;
87 struct uart_icount icount;
90 unsigned int rx_mctrl;
91 unsigned int read_status_mask;
92 unsigned int ignore_status_mask;
98 static struct sdio_uart_port *sdio_uart_table[UART_NR];
99 static DEFINE_SPINLOCK(sdio_uart_table_lock);
101 static int sdio_uart_add_port(struct sdio_uart_port *port)
103 int index, ret = -EBUSY;
105 kref_init(&port->kref);
106 mutex_init(&port->func_lock);
107 spin_lock_init(&port->write_lock);
109 spin_lock(&sdio_uart_table_lock);
110 for (index = 0; index < UART_NR; index++) {
111 if (!sdio_uart_table[index]) {
113 sdio_uart_table[index] = port;
118 spin_unlock(&sdio_uart_table_lock);
123 static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
125 struct sdio_uart_port *port;
127 if (index >= UART_NR)
130 spin_lock(&sdio_uart_table_lock);
131 port = sdio_uart_table[index];
133 kref_get(&port->kref);
134 spin_unlock(&sdio_uart_table_lock);
139 static void sdio_uart_port_destroy(struct kref *kref)
141 struct sdio_uart_port *port =
142 container_of(kref, struct sdio_uart_port, kref);
146 static void sdio_uart_port_put(struct sdio_uart_port *port)
148 kref_put(&port->kref, sdio_uart_port_destroy);
151 static void sdio_uart_port_remove(struct sdio_uart_port *port)
153 struct sdio_func *func;
154 struct tty_struct *tty;
156 BUG_ON(sdio_uart_table[port->index] != port);
158 spin_lock(&sdio_uart_table_lock);
159 sdio_uart_table[port->index] = NULL;
160 spin_unlock(&sdio_uart_table_lock);
163 * We're killing a port that potentially still is in use by
164 * the tty layer. Be careful to prevent any further access
165 * to the SDIO function and arrange for the tty layer to
166 * give up on that port ASAP.
167 * Beware: the lock ordering is critical.
169 mutex_lock(&port->port.mutex);
170 mutex_lock(&port->func_lock);
172 sdio_claim_host(func);
174 mutex_unlock(&port->func_lock);
175 tty = tty_port_tty_get(&port->port);
176 /* tty_hangup is async so is this safe as is ?? */
181 mutex_unlock(&port->port.mutex);
182 sdio_release_irq(func);
183 sdio_disable_func(func);
184 sdio_release_host(func);
186 sdio_uart_port_put(port);
189 static int sdio_uart_claim_func(struct sdio_uart_port *port)
191 mutex_lock(&port->func_lock);
192 if (unlikely(!port->func)) {
193 mutex_unlock(&port->func_lock);
196 if (likely(port->in_sdio_uart_irq != current))
197 sdio_claim_host(port->func);
198 mutex_unlock(&port->func_lock);
202 static inline void sdio_uart_release_func(struct sdio_uart_port *port)
204 if (likely(port->in_sdio_uart_irq != current))
205 sdio_release_host(port->func);
208 static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
211 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
215 static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
217 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
220 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
222 unsigned char status;
225 /* FIXME: What stops this losing the delta bits and breaking
226 sdio_uart_check_modem_status ? */
227 status = sdio_in(port, UART_MSR);
230 if (status & UART_MSR_DCD)
232 if (status & UART_MSR_RI)
234 if (status & UART_MSR_DSR)
236 if (status & UART_MSR_CTS)
241 static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
244 unsigned char mcr = 0;
246 if (mctrl & TIOCM_RTS)
248 if (mctrl & TIOCM_DTR)
250 if (mctrl & TIOCM_OUT1)
251 mcr |= UART_MCR_OUT1;
252 if (mctrl & TIOCM_OUT2)
253 mcr |= UART_MCR_OUT2;
254 if (mctrl & TIOCM_LOOP)
255 mcr |= UART_MCR_LOOP;
257 sdio_out(port, UART_MCR, mcr);
260 static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
261 unsigned int set, unsigned int clear)
266 port->mctrl = (old & ~clear) | set;
267 if (old != port->mctrl)
268 sdio_uart_write_mctrl(port, port->mctrl);
271 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
272 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
274 static void sdio_uart_change_speed(struct sdio_uart_port *port,
275 struct ktermios *termios,
276 struct ktermios *old)
278 unsigned char cval, fcr = 0;
279 unsigned int baud, quot;
281 switch (termios->c_cflag & CSIZE) {
283 cval = UART_LCR_WLEN5;
286 cval = UART_LCR_WLEN6;
289 cval = UART_LCR_WLEN7;
293 cval = UART_LCR_WLEN8;
297 if (termios->c_cflag & CSTOPB)
298 cval |= UART_LCR_STOP;
299 if (termios->c_cflag & PARENB)
300 cval |= UART_LCR_PARITY;
301 if (!(termios->c_cflag & PARODD))
302 cval |= UART_LCR_EPAR;
305 baud = tty_termios_baud_rate(termios);
307 baud = 9600; /* Special case: B0 rate. */
308 if (baud <= port->uartclk)
311 * Oops, the quotient was zero. Try again with the old
312 * baud rate if possible, otherwise default to 9600.
314 termios->c_cflag &= ~CBAUD;
316 termios->c_cflag |= old->c_cflag & CBAUD;
319 termios->c_cflag |= B9600;
321 quot = (2 * port->uartclk + baud) / (2 * baud);
324 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
326 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
328 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
329 if (termios->c_iflag & INPCK)
330 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
331 if (termios->c_iflag & (BRKINT | PARMRK))
332 port->read_status_mask |= UART_LSR_BI;
335 * Characters to ignore
337 port->ignore_status_mask = 0;
338 if (termios->c_iflag & IGNPAR)
339 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
340 if (termios->c_iflag & IGNBRK) {
341 port->ignore_status_mask |= UART_LSR_BI;
343 * If we're ignoring parity and break indicators,
344 * ignore overruns too (for real raw support).
346 if (termios->c_iflag & IGNPAR)
347 port->ignore_status_mask |= UART_LSR_OE;
351 * ignore all characters if CREAD is not set
353 if ((termios->c_cflag & CREAD) == 0)
354 port->ignore_status_mask |= UART_LSR_DR;
357 * CTS flow control flag and modem status interrupts
359 port->ier &= ~UART_IER_MSI;
360 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
361 port->ier |= UART_IER_MSI;
365 sdio_out(port, UART_IER, port->ier);
366 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
367 sdio_out(port, UART_DLL, quot & 0xff);
368 sdio_out(port, UART_DLM, quot >> 8);
369 sdio_out(port, UART_LCR, cval);
370 sdio_out(port, UART_FCR, fcr);
372 sdio_uart_write_mctrl(port, port->mctrl);
375 static void sdio_uart_start_tx(struct sdio_uart_port *port)
377 if (!(port->ier & UART_IER_THRI)) {
378 port->ier |= UART_IER_THRI;
379 sdio_out(port, UART_IER, port->ier);
383 static void sdio_uart_stop_tx(struct sdio_uart_port *port)
385 if (port->ier & UART_IER_THRI) {
386 port->ier &= ~UART_IER_THRI;
387 sdio_out(port, UART_IER, port->ier);
391 static void sdio_uart_stop_rx(struct sdio_uart_port *port)
393 port->ier &= ~UART_IER_RLSI;
394 port->read_status_mask &= ~UART_LSR_DR;
395 sdio_out(port, UART_IER, port->ier);
398 static void sdio_uart_receive_chars(struct sdio_uart_port *port,
399 unsigned int *status)
401 struct tty_struct *tty = tty_port_tty_get(&port->port);
402 unsigned int ch, flag;
406 ch = sdio_in(port, UART_RX);
410 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
411 UART_LSR_FE | UART_LSR_OE))) {
413 * For statistics only
415 if (*status & UART_LSR_BI) {
416 *status &= ~(UART_LSR_FE | UART_LSR_PE);
418 } else if (*status & UART_LSR_PE)
419 port->icount.parity++;
420 else if (*status & UART_LSR_FE)
421 port->icount.frame++;
422 if (*status & UART_LSR_OE)
423 port->icount.overrun++;
426 * Mask off conditions which should be ignored.
428 *status &= port->read_status_mask;
429 if (*status & UART_LSR_BI)
431 else if (*status & UART_LSR_PE)
433 else if (*status & UART_LSR_FE)
437 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
439 tty_insert_flip_char(tty, ch, flag);
442 * Overrun is special. Since it's reported immediately,
443 * it doesn't affect the current character.
445 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
447 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
449 *status = sdio_in(port, UART_LSR);
450 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
452 tty_flip_buffer_push(tty);
457 static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
459 struct circ_buf *xmit = &port->xmit;
461 struct tty_struct *tty;
464 sdio_out(port, UART_TX, port->x_char);
470 tty = tty_port_tty_get(&port->port);
472 if (tty == NULL || circ_empty(xmit) ||
473 tty->stopped || tty->hw_stopped) {
474 sdio_uart_stop_tx(port);
481 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
482 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
484 if (circ_empty(xmit))
486 } while (--count > 0);
488 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
491 if (circ_empty(xmit))
492 sdio_uart_stop_tx(port);
496 static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
499 struct tty_struct *tty;
501 status = sdio_in(port, UART_MSR);
503 if ((status & UART_MSR_ANY_DELTA) == 0)
506 if (status & UART_MSR_TERI)
508 if (status & UART_MSR_DDSR)
510 if (status & UART_MSR_DDCD) {
512 /* DCD raise - wake for open */
513 if (status & UART_MSR_DCD)
514 wake_up_interruptible(&port->port.open_wait);
516 /* DCD drop - hang up if tty attached */
517 tty = tty_port_tty_get(&port->port);
524 if (status & UART_MSR_DCTS) {
526 tty = tty_port_tty_get(&port->port);
527 if (tty && (tty->termios->c_cflag & CRTSCTS)) {
528 int cts = (status & UART_MSR_CTS);
529 if (tty->hw_stopped) {
532 sdio_uart_start_tx(port);
538 sdio_uart_stop_tx(port);
547 * This handles the interrupt from one port.
549 static void sdio_uart_irq(struct sdio_func *func)
551 struct sdio_uart_port *port = sdio_get_drvdata(func);
552 unsigned int iir, lsr;
555 * In a few places sdio_uart_irq() is called directly instead of
556 * waiting for the actual interrupt to be raised and the SDIO IRQ
557 * thread scheduled in order to reduce latency. However, some
558 * interaction with the tty core may end up calling us back
559 * (serial echo, flow control, etc.) through those same places
560 * causing undesirable effects. Let's stop the recursion here.
562 if (unlikely(port->in_sdio_uart_irq == current))
565 iir = sdio_in(port, UART_IIR);
566 if (iir & UART_IIR_NO_INT)
569 port->in_sdio_uart_irq = current;
570 lsr = sdio_in(port, UART_LSR);
571 if (lsr & UART_LSR_DR)
572 sdio_uart_receive_chars(port, &lsr);
573 sdio_uart_check_modem_status(port);
574 if (lsr & UART_LSR_THRE)
575 sdio_uart_transmit_chars(port);
576 port->in_sdio_uart_irq = NULL;
579 static int uart_carrier_raised(struct tty_port *tport)
581 struct sdio_uart_port *port =
582 container_of(tport, struct sdio_uart_port, port);
583 unsigned int ret = sdio_uart_claim_func(port);
584 if (ret) /* Missing hardware shoudn't block for carrier */
586 ret = sdio_uart_get_mctrl(port);
587 sdio_uart_release_func(port);
594 * uart_dtr_rts - port helper to set uart signals
595 * @tport: tty port to be updated
596 * @onoff: set to turn on DTR/RTS
598 * Called by the tty port helpers when the modem signals need to be
599 * adjusted during an open, close and hangup.
602 static void uart_dtr_rts(struct tty_port *tport, int onoff)
604 struct sdio_uart_port *port =
605 container_of(tport, struct sdio_uart_port, port);
606 int ret = sdio_uart_claim_func(port);
610 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
612 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
613 sdio_uart_release_func(port);
617 * sdio_uart_activate - start up hardware
618 * @tport: tty port to activate
619 * @tty: tty bound to this port
621 * Activate a tty port. The port locking guarantees us this will be
622 * run exactly once per set of opens, and if successful will see the
623 * shutdown method run exactly once to match. Start up and shutdown are
624 * protected from each other by the internal locking and will not run
625 * at the same time even during a hangup event.
627 * If we successfully start up the port we take an extra kref as we
628 * will keep it around until shutdown when the kref is dropped.
631 static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
633 struct sdio_uart_port *port =
634 container_of(tport, struct sdio_uart_port, port);
639 * Set the TTY IO error marker - we will only clear this
640 * once we have successfully opened the port.
642 set_bit(TTY_IO_ERROR, &tty->flags);
644 /* Initialise and allocate the transmit buffer. */
645 page = __get_free_page(GFP_KERNEL);
648 port->xmit.buf = (unsigned char *)page;
649 circ_clear(&port->xmit);
651 ret = sdio_uart_claim_func(port);
654 ret = sdio_enable_func(port->func);
657 ret = sdio_claim_irq(port->func, sdio_uart_irq);
662 * Clear the FIFO buffers and disable them.
663 * (they will be reenabled in sdio_change_speed())
665 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
666 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
667 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
668 sdio_out(port, UART_FCR, 0);
671 * Clear the interrupt registers.
673 (void) sdio_in(port, UART_LSR);
674 (void) sdio_in(port, UART_RX);
675 (void) sdio_in(port, UART_IIR);
676 (void) sdio_in(port, UART_MSR);
679 * Now, initialize the UART
681 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
683 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
684 port->mctrl = TIOCM_OUT2;
686 sdio_uart_change_speed(port, tty->termios, NULL);
688 if (tty->termios->c_cflag & CBAUD)
689 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
691 if (tty->termios->c_cflag & CRTSCTS)
692 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
695 clear_bit(TTY_IO_ERROR, &tty->flags);
697 /* Kick the IRQ handler once while we're still holding the host lock */
698 sdio_uart_irq(port->func);
700 sdio_uart_release_func(port);
704 sdio_disable_func(port->func);
706 sdio_uart_release_func(port);
708 free_page((unsigned long)port->xmit.buf);
713 * sdio_uart_shutdown - stop hardware
714 * @tport: tty port to shut down
716 * Deactivate a tty port. The port locking guarantees us this will be
717 * run only if a successful matching activate already ran. The two are
718 * protected from each other by the internal locking and will not run
719 * at the same time even during a hangup event.
722 static void sdio_uart_shutdown(struct tty_port *tport)
724 struct sdio_uart_port *port =
725 container_of(tport, struct sdio_uart_port, port);
728 ret = sdio_uart_claim_func(port);
732 sdio_uart_stop_rx(port);
734 /* Disable interrupts from this port */
735 sdio_release_irq(port->func);
737 sdio_out(port, UART_IER, 0);
739 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
741 /* Disable break condition and FIFOs. */
742 port->lcr &= ~UART_LCR_SBC;
743 sdio_out(port, UART_LCR, port->lcr);
744 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
745 UART_FCR_CLEAR_RCVR |
746 UART_FCR_CLEAR_XMIT);
747 sdio_out(port, UART_FCR, 0);
749 sdio_disable_func(port->func);
751 sdio_uart_release_func(port);
754 /* Free the transmit buffer page. */
755 free_page((unsigned long)port->xmit.buf);
759 * sdio_uart_install - install method
760 * @driver: the driver in use (sdio_uart in our case)
761 * @tty: the tty being bound
763 * Look up and bind the tty and the driver together. Initialize
764 * any needed private data (in our case the termios)
767 static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
769 int idx = tty->index;
770 struct sdio_uart_port *port = sdio_uart_port_get(idx);
771 int ret = tty_init_termios(tty);
774 tty_driver_kref_get(driver);
776 /* This is the ref sdio_uart_port get provided */
777 tty->driver_data = port;
778 driver->ttys[idx] = tty;
780 sdio_uart_port_put(port);
785 * sdio_uart_cleanup - called on the last tty kref drop
786 * @tty: the tty being destroyed
788 * Called asynchronously when the last reference to the tty is dropped.
789 * We cannot destroy the tty->driver_data port kref until this point
792 static void sdio_uart_cleanup(struct tty_struct *tty)
794 struct sdio_uart_port *port = tty->driver_data;
795 tty->driver_data = NULL; /* Bug trap */
796 sdio_uart_port_put(port);
800 * Open/close/hangup is now entirely boilerplate
803 static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
805 struct sdio_uart_port *port = tty->driver_data;
806 return tty_port_open(&port->port, tty, filp);
809 static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
811 struct sdio_uart_port *port = tty->driver_data;
812 tty_port_close(&port->port, tty, filp);
815 static void sdio_uart_hangup(struct tty_struct *tty)
817 struct sdio_uart_port *port = tty->driver_data;
818 tty_port_hangup(&port->port);
821 static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
824 struct sdio_uart_port *port = tty->driver_data;
825 struct circ_buf *circ = &port->xmit;
831 spin_lock(&port->write_lock);
833 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
838 memcpy(circ->buf + circ->head, buf, c);
839 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
844 spin_unlock(&port->write_lock);
846 if (!(port->ier & UART_IER_THRI)) {
847 int err = sdio_uart_claim_func(port);
849 sdio_uart_start_tx(port);
850 sdio_uart_irq(port->func);
851 sdio_uart_release_func(port);
859 static int sdio_uart_write_room(struct tty_struct *tty)
861 struct sdio_uart_port *port = tty->driver_data;
862 return port ? circ_chars_free(&port->xmit) : 0;
865 static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
867 struct sdio_uart_port *port = tty->driver_data;
868 return port ? circ_chars_pending(&port->xmit) : 0;
871 static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
873 struct sdio_uart_port *port = tty->driver_data;
876 if (ch && !(port->ier & UART_IER_THRI)) {
877 if (sdio_uart_claim_func(port) != 0)
879 sdio_uart_start_tx(port);
880 sdio_uart_irq(port->func);
881 sdio_uart_release_func(port);
885 static void sdio_uart_throttle(struct tty_struct *tty)
887 struct sdio_uart_port *port = tty->driver_data;
889 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
892 if (sdio_uart_claim_func(port) != 0)
896 port->x_char = STOP_CHAR(tty);
897 sdio_uart_start_tx(port);
900 if (tty->termios->c_cflag & CRTSCTS)
901 sdio_uart_clear_mctrl(port, TIOCM_RTS);
903 sdio_uart_irq(port->func);
904 sdio_uart_release_func(port);
907 static void sdio_uart_unthrottle(struct tty_struct *tty)
909 struct sdio_uart_port *port = tty->driver_data;
911 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
914 if (sdio_uart_claim_func(port) != 0)
921 port->x_char = START_CHAR(tty);
922 sdio_uart_start_tx(port);
926 if (tty->termios->c_cflag & CRTSCTS)
927 sdio_uart_set_mctrl(port, TIOCM_RTS);
929 sdio_uart_irq(port->func);
930 sdio_uart_release_func(port);
933 static void sdio_uart_set_termios(struct tty_struct *tty,
934 struct ktermios *old_termios)
936 struct sdio_uart_port *port = tty->driver_data;
937 unsigned int cflag = tty->termios->c_cflag;
939 if (sdio_uart_claim_func(port) != 0)
942 sdio_uart_change_speed(port, tty->termios, old_termios);
944 /* Handle transition to B0 status */
945 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
946 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
948 /* Handle transition away from B0 status */
949 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
950 unsigned int mask = TIOCM_DTR;
951 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
953 sdio_uart_set_mctrl(port, mask);
956 /* Handle turning off CRTSCTS */
957 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
959 sdio_uart_start_tx(port);
962 /* Handle turning on CRTSCTS */
963 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
964 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
966 sdio_uart_stop_tx(port);
970 sdio_uart_release_func(port);
973 static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
975 struct sdio_uart_port *port = tty->driver_data;
978 result = sdio_uart_claim_func(port);
982 if (break_state == -1)
983 port->lcr |= UART_LCR_SBC;
985 port->lcr &= ~UART_LCR_SBC;
986 sdio_out(port, UART_LCR, port->lcr);
988 sdio_uart_release_func(port);
992 static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
994 struct sdio_uart_port *port = tty->driver_data;
997 result = sdio_uart_claim_func(port);
999 result = port->mctrl | sdio_uart_get_mctrl(port);
1000 sdio_uart_release_func(port);
1006 static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
1007 unsigned int set, unsigned int clear)
1009 struct sdio_uart_port *port = tty->driver_data;
1012 result = sdio_uart_claim_func(port);
1014 sdio_uart_update_mctrl(port, set, clear);
1015 sdio_uart_release_func(port);
1021 static int sdio_uart_proc_show(struct seq_file *m, void *v)
1025 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1027 for (i = 0; i < UART_NR; i++) {
1028 struct sdio_uart_port *port = sdio_uart_port_get(i);
1030 seq_printf(m, "%d: uart:SDIO", i);
1031 if (capable(CAP_SYS_ADMIN)) {
1032 seq_printf(m, " tx:%d rx:%d",
1033 port->icount.tx, port->icount.rx);
1034 if (port->icount.frame)
1035 seq_printf(m, " fe:%d",
1036 port->icount.frame);
1037 if (port->icount.parity)
1038 seq_printf(m, " pe:%d",
1039 port->icount.parity);
1040 if (port->icount.brk)
1041 seq_printf(m, " brk:%d",
1043 if (port->icount.overrun)
1044 seq_printf(m, " oe:%d",
1045 port->icount.overrun);
1046 if (port->icount.cts)
1047 seq_printf(m, " cts:%d",
1049 if (port->icount.dsr)
1050 seq_printf(m, " dsr:%d",
1052 if (port->icount.rng)
1053 seq_printf(m, " rng:%d",
1055 if (port->icount.dcd)
1056 seq_printf(m, " dcd:%d",
1059 sdio_uart_port_put(port);
1066 static int sdio_uart_proc_open(struct inode *inode, struct file *file)
1068 return single_open(file, sdio_uart_proc_show, NULL);
1071 static const struct file_operations sdio_uart_proc_fops = {
1072 .owner = THIS_MODULE,
1073 .open = sdio_uart_proc_open,
1075 .llseek = seq_lseek,
1076 .release = single_release,
1079 static const struct tty_port_operations sdio_uart_port_ops = {
1080 .dtr_rts = uart_dtr_rts,
1081 .carrier_raised = uart_carrier_raised,
1082 .shutdown = sdio_uart_shutdown,
1083 .activate = sdio_uart_activate,
1086 static const struct tty_operations sdio_uart_ops = {
1087 .open = sdio_uart_open,
1088 .close = sdio_uart_close,
1089 .write = sdio_uart_write,
1090 .write_room = sdio_uart_write_room,
1091 .chars_in_buffer = sdio_uart_chars_in_buffer,
1092 .send_xchar = sdio_uart_send_xchar,
1093 .throttle = sdio_uart_throttle,
1094 .unthrottle = sdio_uart_unthrottle,
1095 .set_termios = sdio_uart_set_termios,
1096 .hangup = sdio_uart_hangup,
1097 .break_ctl = sdio_uart_break_ctl,
1098 .tiocmget = sdio_uart_tiocmget,
1099 .tiocmset = sdio_uart_tiocmset,
1100 .install = sdio_uart_install,
1101 .cleanup = sdio_uart_cleanup,
1102 .proc_fops = &sdio_uart_proc_fops,
1105 static struct tty_driver *sdio_uart_tty_driver;
1107 static int sdio_uart_probe(struct sdio_func *func,
1108 const struct sdio_device_id *id)
1110 struct sdio_uart_port *port;
1113 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1117 if (func->class == SDIO_CLASS_UART) {
1118 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1119 sdio_func_id(func));
1122 } else if (func->class == SDIO_CLASS_GPS) {
1124 * We need tuple 0x91. It contains SUBTPL_SIOREG
1125 * and SUBTPL_RCVCAPS.
1127 struct sdio_func_tuple *tpl;
1128 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1129 if (tpl->code != 0x91)
1133 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1138 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1139 sdio_func_id(func));
1143 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1144 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1145 port->regs_offset = (tpl->data[4] << 0) |
1146 (tpl->data[5] << 8) |
1147 (tpl->data[6] << 16);
1148 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1149 sdio_func_id(func), port->regs_offset);
1150 port->uartclk = tpl->data[7] * 115200;
1151 if (port->uartclk == 0)
1152 port->uartclk = 115200;
1153 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1154 sdio_func_id(func), port->uartclk,
1155 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1162 sdio_set_drvdata(func, port);
1163 tty_port_init(&port->port);
1164 port->port.ops = &sdio_uart_port_ops;
1166 ret = sdio_uart_add_port(port);
1171 dev = tty_register_device(sdio_uart_tty_driver,
1172 port->index, &func->dev);
1174 sdio_uart_port_remove(port);
1182 static void sdio_uart_remove(struct sdio_func *func)
1184 struct sdio_uart_port *port = sdio_get_drvdata(func);
1186 tty_unregister_device(sdio_uart_tty_driver, port->index);
1187 sdio_uart_port_remove(port);
1190 static const struct sdio_device_id sdio_uart_ids[] = {
1191 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1192 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1193 { /* end: all zeroes */ },
1196 MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1198 static struct sdio_driver sdio_uart_driver = {
1199 .probe = sdio_uart_probe,
1200 .remove = sdio_uart_remove,
1201 .name = "sdio_uart",
1202 .id_table = sdio_uart_ids,
1205 static int __init sdio_uart_init(void)
1208 struct tty_driver *tty_drv;
1210 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1214 tty_drv->owner = THIS_MODULE;
1215 tty_drv->driver_name = "sdio_uart";
1216 tty_drv->name = "ttySDIO";
1217 tty_drv->major = 0; /* dynamically allocated */
1218 tty_drv->minor_start = 0;
1219 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1220 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1221 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1222 tty_drv->init_termios = tty_std_termios;
1223 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1224 tty_drv->init_termios.c_ispeed = 4800;
1225 tty_drv->init_termios.c_ospeed = 4800;
1226 tty_set_operations(tty_drv, &sdio_uart_ops);
1228 ret = tty_register_driver(tty_drv);
1232 ret = sdio_register_driver(&sdio_uart_driver);
1239 tty_unregister_driver(tty_drv);
1241 put_tty_driver(tty_drv);
1245 static void __exit sdio_uart_exit(void)
1247 sdio_unregister_driver(&sdio_uart_driver);
1248 tty_unregister_driver(sdio_uart_tty_driver);
1249 put_tty_driver(sdio_uart_tty_driver);
1252 module_init(sdio_uart_init);
1253 module_exit(sdio_uart_exit);
1255 MODULE_AUTHOR("Nicolas Pitre");
1256 MODULE_LICENSE("GPL");