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/mutex.h>
33 #include <linux/seq_file.h>
34 #include <linux/serial_reg.h>
35 #include <linux/circ_buf.h>
36 #include <linux/gfp.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
40 #include <linux/mmc/core.h>
41 #include <linux/mmc/card.h>
42 #include <linux/mmc/sdio_func.h>
43 #include <linux/mmc/sdio_ids.h>
46 #define UART_NR 8 /* Number of UARTs this driver can handle */
49 #define UART_XMIT_SIZE PAGE_SIZE
50 #define WAKEUP_CHARS 256
52 #define circ_empty(circ) ((circ)->head == (circ)->tail)
53 #define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
55 #define circ_chars_pending(circ) \
56 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
58 #define circ_chars_free(circ) \
59 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
75 struct sdio_uart_port {
77 struct tty_struct *tty;
80 struct mutex open_lock;
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 read_status_mask;
91 unsigned int ignore_status_mask;
97 static struct sdio_uart_port *sdio_uart_table[UART_NR];
98 static DEFINE_SPINLOCK(sdio_uart_table_lock);
100 static int sdio_uart_add_port(struct sdio_uart_port *port)
102 int index, ret = -EBUSY;
104 kref_init(&port->kref);
105 mutex_init(&port->open_lock);
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;
155 BUG_ON(sdio_uart_table[port->index] != port);
157 spin_lock(&sdio_uart_table_lock);
158 sdio_uart_table[port->index] = NULL;
159 spin_unlock(&sdio_uart_table_lock);
162 * We're killing a port that potentially still is in use by
163 * the tty layer. Be careful to prevent any further access
164 * to the SDIO function and arrange for the tty layer to
165 * give up on that port ASAP.
166 * Beware: the lock ordering is critical.
168 mutex_lock(&port->open_lock);
169 mutex_lock(&port->func_lock);
171 sdio_claim_host(func);
173 mutex_unlock(&port->func_lock);
175 tty_hangup(port->tty);
176 mutex_unlock(&port->open_lock);
177 sdio_release_irq(func);
178 sdio_disable_func(func);
179 sdio_release_host(func);
181 sdio_uart_port_put(port);
184 static int sdio_uart_claim_func(struct sdio_uart_port *port)
186 mutex_lock(&port->func_lock);
187 if (unlikely(!port->func)) {
188 mutex_unlock(&port->func_lock);
191 if (likely(port->in_sdio_uart_irq != current))
192 sdio_claim_host(port->func);
193 mutex_unlock(&port->func_lock);
197 static inline void sdio_uart_release_func(struct sdio_uart_port *port)
199 if (likely(port->in_sdio_uart_irq != current))
200 sdio_release_host(port->func);
203 static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
206 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
210 static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
212 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
215 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
217 unsigned char status;
220 status = sdio_in(port, UART_MSR);
223 if (status & UART_MSR_DCD)
225 if (status & UART_MSR_RI)
227 if (status & UART_MSR_DSR)
229 if (status & UART_MSR_CTS)
234 static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
237 unsigned char mcr = 0;
239 if (mctrl & TIOCM_RTS)
241 if (mctrl & TIOCM_DTR)
243 if (mctrl & TIOCM_OUT1)
244 mcr |= UART_MCR_OUT1;
245 if (mctrl & TIOCM_OUT2)
246 mcr |= UART_MCR_OUT2;
247 if (mctrl & TIOCM_LOOP)
248 mcr |= UART_MCR_LOOP;
250 sdio_out(port, UART_MCR, mcr);
253 static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
254 unsigned int set, unsigned int clear)
259 port->mctrl = (old & ~clear) | set;
260 if (old != port->mctrl)
261 sdio_uart_write_mctrl(port, port->mctrl);
264 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
265 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
267 static void sdio_uart_change_speed(struct sdio_uart_port *port,
268 struct ktermios *termios,
269 struct ktermios *old)
271 unsigned char cval, fcr = 0;
272 unsigned int baud, quot;
274 switch (termios->c_cflag & CSIZE) {
276 cval = UART_LCR_WLEN5;
279 cval = UART_LCR_WLEN6;
282 cval = UART_LCR_WLEN7;
286 cval = UART_LCR_WLEN8;
290 if (termios->c_cflag & CSTOPB)
291 cval |= UART_LCR_STOP;
292 if (termios->c_cflag & PARENB)
293 cval |= UART_LCR_PARITY;
294 if (!(termios->c_cflag & PARODD))
295 cval |= UART_LCR_EPAR;
298 baud = tty_termios_baud_rate(termios);
300 baud = 9600; /* Special case: B0 rate. */
301 if (baud <= port->uartclk)
304 * Oops, the quotient was zero. Try again with the old
305 * baud rate if possible, otherwise default to 9600.
307 termios->c_cflag &= ~CBAUD;
309 termios->c_cflag |= old->c_cflag & CBAUD;
312 termios->c_cflag |= B9600;
314 quot = (2 * port->uartclk + baud) / (2 * baud);
317 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
319 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
321 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
322 if (termios->c_iflag & INPCK)
323 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
324 if (termios->c_iflag & (BRKINT | PARMRK))
325 port->read_status_mask |= UART_LSR_BI;
328 * Characters to ignore
330 port->ignore_status_mask = 0;
331 if (termios->c_iflag & IGNPAR)
332 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
333 if (termios->c_iflag & IGNBRK) {
334 port->ignore_status_mask |= UART_LSR_BI;
336 * If we're ignoring parity and break indicators,
337 * ignore overruns too (for real raw support).
339 if (termios->c_iflag & IGNPAR)
340 port->ignore_status_mask |= UART_LSR_OE;
344 * ignore all characters if CREAD is not set
346 if ((termios->c_cflag & CREAD) == 0)
347 port->ignore_status_mask |= UART_LSR_DR;
350 * CTS flow control flag and modem status interrupts
352 port->ier &= ~UART_IER_MSI;
353 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
354 port->ier |= UART_IER_MSI;
358 sdio_out(port, UART_IER, port->ier);
359 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
360 sdio_out(port, UART_DLL, quot & 0xff);
361 sdio_out(port, UART_DLM, quot >> 8);
362 sdio_out(port, UART_LCR, cval);
363 sdio_out(port, UART_FCR, fcr);
365 sdio_uart_write_mctrl(port, port->mctrl);
368 static void sdio_uart_start_tx(struct sdio_uart_port *port)
370 if (!(port->ier & UART_IER_THRI)) {
371 port->ier |= UART_IER_THRI;
372 sdio_out(port, UART_IER, port->ier);
376 static void sdio_uart_stop_tx(struct sdio_uart_port *port)
378 if (port->ier & UART_IER_THRI) {
379 port->ier &= ~UART_IER_THRI;
380 sdio_out(port, UART_IER, port->ier);
384 static void sdio_uart_stop_rx(struct sdio_uart_port *port)
386 port->ier &= ~UART_IER_RLSI;
387 port->read_status_mask &= ~UART_LSR_DR;
388 sdio_out(port, UART_IER, port->ier);
391 static void sdio_uart_receive_chars(struct sdio_uart_port *port,
392 unsigned int *status)
394 struct tty_struct *tty = port->tty;
395 unsigned int ch, flag;
399 ch = sdio_in(port, UART_RX);
403 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
404 UART_LSR_FE | UART_LSR_OE))) {
406 * For statistics only
408 if (*status & UART_LSR_BI) {
409 *status &= ~(UART_LSR_FE | UART_LSR_PE);
411 } else if (*status & UART_LSR_PE)
412 port->icount.parity++;
413 else if (*status & UART_LSR_FE)
414 port->icount.frame++;
415 if (*status & UART_LSR_OE)
416 port->icount.overrun++;
419 * Mask off conditions which should be ignored.
421 *status &= port->read_status_mask;
422 if (*status & UART_LSR_BI)
424 else if (*status & UART_LSR_PE)
426 else if (*status & UART_LSR_FE)
430 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
431 tty_insert_flip_char(tty, ch, flag);
434 * Overrun is special. Since it's reported immediately,
435 * it doesn't affect the current character.
437 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
438 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
440 *status = sdio_in(port, UART_LSR);
441 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
442 tty_flip_buffer_push(tty);
445 static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
447 struct circ_buf *xmit = &port->xmit;
451 sdio_out(port, UART_TX, port->x_char);
456 if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
457 sdio_uart_stop_tx(port);
463 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
464 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
466 if (circ_empty(xmit))
468 } while (--count > 0);
470 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
471 tty_wakeup(port->tty);
473 if (circ_empty(xmit))
474 sdio_uart_stop_tx(port);
477 static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
481 status = sdio_in(port, UART_MSR);
483 if ((status & UART_MSR_ANY_DELTA) == 0)
486 if (status & UART_MSR_TERI)
488 if (status & UART_MSR_DDSR)
490 if (status & UART_MSR_DDCD)
492 if (status & UART_MSR_DCTS) {
494 if (port->tty->termios->c_cflag & CRTSCTS) {
495 int cts = (status & UART_MSR_CTS);
496 if (port->tty->hw_stopped) {
498 port->tty->hw_stopped = 0;
499 sdio_uart_start_tx(port);
500 tty_wakeup(port->tty);
504 port->tty->hw_stopped = 1;
505 sdio_uart_stop_tx(port);
513 * This handles the interrupt from one port.
515 static void sdio_uart_irq(struct sdio_func *func)
517 struct sdio_uart_port *port = sdio_get_drvdata(func);
518 unsigned int iir, lsr;
521 * In a few places sdio_uart_irq() is called directly instead of
522 * waiting for the actual interrupt to be raised and the SDIO IRQ
523 * thread scheduled in order to reduce latency. However, some
524 * interaction with the tty core may end up calling us back
525 * (serial echo, flow control, etc.) through those same places
526 * causing undesirable effects. Let's stop the recursion here.
528 if (unlikely(port->in_sdio_uart_irq == current))
531 iir = sdio_in(port, UART_IIR);
532 if (iir & UART_IIR_NO_INT)
535 port->in_sdio_uart_irq = current;
536 lsr = sdio_in(port, UART_LSR);
537 if (lsr & UART_LSR_DR)
538 sdio_uart_receive_chars(port, &lsr);
539 sdio_uart_check_modem_status(port);
540 if (lsr & UART_LSR_THRE)
541 sdio_uart_transmit_chars(port);
542 port->in_sdio_uart_irq = NULL;
545 static int sdio_uart_startup(struct sdio_uart_port *port)
551 * Set the TTY IO error marker - we will only clear this
552 * once we have successfully opened the port.
554 set_bit(TTY_IO_ERROR, &port->tty->flags);
556 /* Initialise and allocate the transmit buffer. */
557 page = __get_free_page(GFP_KERNEL);
560 port->xmit.buf = (unsigned char *)page;
561 circ_clear(&port->xmit);
563 ret = sdio_uart_claim_func(port);
566 ret = sdio_enable_func(port->func);
569 ret = sdio_claim_irq(port->func, sdio_uart_irq);
574 * Clear the FIFO buffers and disable them.
575 * (they will be reenabled in sdio_change_speed())
577 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
578 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
579 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
580 sdio_out(port, UART_FCR, 0);
583 * Clear the interrupt registers.
585 (void) sdio_in(port, UART_LSR);
586 (void) sdio_in(port, UART_RX);
587 (void) sdio_in(port, UART_IIR);
588 (void) sdio_in(port, UART_MSR);
591 * Now, initialize the UART
593 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
595 port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
596 port->mctrl = TIOCM_OUT2;
598 sdio_uart_change_speed(port, port->tty->termios, NULL);
600 if (port->tty->termios->c_cflag & CBAUD)
601 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
603 if (port->tty->termios->c_cflag & CRTSCTS)
604 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
605 port->tty->hw_stopped = 1;
607 clear_bit(TTY_IO_ERROR, &port->tty->flags);
609 /* Kick the IRQ handler once while we're still holding the host lock */
610 sdio_uart_irq(port->func);
612 sdio_uart_release_func(port);
616 sdio_disable_func(port->func);
618 sdio_uart_release_func(port);
620 free_page((unsigned long)port->xmit.buf);
624 static void sdio_uart_shutdown(struct sdio_uart_port *port)
628 ret = sdio_uart_claim_func(port);
632 sdio_uart_stop_rx(port);
634 /* TODO: wait here for TX FIFO to drain */
636 /* Turn off DTR and RTS early. */
637 if (port->tty->termios->c_cflag & HUPCL)
638 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
640 /* Disable interrupts from this port */
641 sdio_release_irq(port->func);
643 sdio_out(port, UART_IER, 0);
645 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
647 /* Disable break condition and FIFOs. */
648 port->lcr &= ~UART_LCR_SBC;
649 sdio_out(port, UART_LCR, port->lcr);
650 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
651 UART_FCR_CLEAR_RCVR |
652 UART_FCR_CLEAR_XMIT);
653 sdio_out(port, UART_FCR, 0);
655 sdio_disable_func(port->func);
657 sdio_uart_release_func(port);
660 /* Free the transmit buffer page. */
661 free_page((unsigned long)port->xmit.buf);
664 static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
666 struct sdio_uart_port *port;
669 port = sdio_uart_port_get(tty->index);
673 mutex_lock(&port->open_lock);
676 * Make sure not to mess up with a dead port
677 * which has not been closed yet.
679 if (tty->driver_data && tty->driver_data != port) {
680 mutex_unlock(&port->open_lock);
681 sdio_uart_port_put(port);
686 tty->driver_data = port;
688 ret = sdio_uart_startup(port);
690 tty->driver_data = NULL;
692 mutex_unlock(&port->open_lock);
693 sdio_uart_port_put(port);
698 mutex_unlock(&port->open_lock);
702 static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
704 struct sdio_uart_port *port = tty->driver_data;
709 mutex_lock(&port->open_lock);
710 BUG_ON(!port->opened);
713 * This is messy. The tty layer calls us even when open()
714 * returned an error. Ignore this close request if tty->count
715 * is larger than port->count.
717 if (tty->count > port->opened) {
718 mutex_unlock(&port->open_lock);
722 if (--port->opened == 0) {
724 sdio_uart_shutdown(port);
725 tty_ldisc_flush(tty);
727 tty->driver_data = NULL;
730 mutex_unlock(&port->open_lock);
731 sdio_uart_port_put(port);
734 static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
737 struct sdio_uart_port *port = tty->driver_data;
738 struct circ_buf *circ = &port->xmit;
744 spin_lock(&port->write_lock);
746 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
751 memcpy(circ->buf + circ->head, buf, c);
752 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
757 spin_unlock(&port->write_lock);
759 if ( !(port->ier & UART_IER_THRI)) {
760 int err = sdio_uart_claim_func(port);
762 sdio_uart_start_tx(port);
763 sdio_uart_irq(port->func);
764 sdio_uart_release_func(port);
772 static int sdio_uart_write_room(struct tty_struct *tty)
774 struct sdio_uart_port *port = tty->driver_data;
775 return port ? circ_chars_free(&port->xmit) : 0;
778 static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
780 struct sdio_uart_port *port = tty->driver_data;
781 return port ? circ_chars_pending(&port->xmit) : 0;
784 static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
786 struct sdio_uart_port *port = tty->driver_data;
789 if (ch && !(port->ier & UART_IER_THRI)) {
790 if (sdio_uart_claim_func(port) != 0)
792 sdio_uart_start_tx(port);
793 sdio_uart_irq(port->func);
794 sdio_uart_release_func(port);
798 static void sdio_uart_throttle(struct tty_struct *tty)
800 struct sdio_uart_port *port = tty->driver_data;
802 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
805 if (sdio_uart_claim_func(port) != 0)
809 port->x_char = STOP_CHAR(tty);
810 sdio_uart_start_tx(port);
813 if (tty->termios->c_cflag & CRTSCTS)
814 sdio_uart_clear_mctrl(port, TIOCM_RTS);
816 sdio_uart_irq(port->func);
817 sdio_uart_release_func(port);
820 static void sdio_uart_unthrottle(struct tty_struct *tty)
822 struct sdio_uart_port *port = tty->driver_data;
824 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
827 if (sdio_uart_claim_func(port) != 0)
834 port->x_char = START_CHAR(tty);
835 sdio_uart_start_tx(port);
839 if (tty->termios->c_cflag & CRTSCTS)
840 sdio_uart_set_mctrl(port, TIOCM_RTS);
842 sdio_uart_irq(port->func);
843 sdio_uart_release_func(port);
846 static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
848 struct sdio_uart_port *port = tty->driver_data;
849 unsigned int cflag = tty->termios->c_cflag;
851 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
853 if ((cflag ^ old_termios->c_cflag) == 0 &&
854 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
857 if (sdio_uart_claim_func(port) != 0)
860 sdio_uart_change_speed(port, tty->termios, old_termios);
862 /* Handle transition to B0 status */
863 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
864 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
866 /* Handle transition away from B0 status */
867 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
868 unsigned int mask = TIOCM_DTR;
869 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
871 sdio_uart_set_mctrl(port, mask);
874 /* Handle turning off CRTSCTS */
875 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
877 sdio_uart_start_tx(port);
880 /* Handle turning on CRTSCTS */
881 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
882 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
884 sdio_uart_stop_tx(port);
888 sdio_uart_release_func(port);
891 static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
893 struct sdio_uart_port *port = tty->driver_data;
896 result = sdio_uart_claim_func(port);
900 if (break_state == -1)
901 port->lcr |= UART_LCR_SBC;
903 port->lcr &= ~UART_LCR_SBC;
904 sdio_out(port, UART_LCR, port->lcr);
906 sdio_uart_release_func(port);
910 static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
912 struct sdio_uart_port *port = tty->driver_data;
915 result = sdio_uart_claim_func(port);
917 result = port->mctrl | sdio_uart_get_mctrl(port);
918 sdio_uart_release_func(port);
924 static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
925 unsigned int set, unsigned int clear)
927 struct sdio_uart_port *port = tty->driver_data;
930 result = sdio_uart_claim_func(port);
932 sdio_uart_update_mctrl(port, set, clear);
933 sdio_uart_release_func(port);
939 static int sdio_uart_proc_show(struct seq_file *m, void *v)
943 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
945 for (i = 0; i < UART_NR; i++) {
946 struct sdio_uart_port *port = sdio_uart_port_get(i);
948 seq_printf(m, "%d: uart:SDIO", i);
949 if(capable(CAP_SYS_ADMIN)) {
950 seq_printf(m, " tx:%d rx:%d",
951 port->icount.tx, port->icount.rx);
952 if (port->icount.frame)
953 seq_printf(m, " fe:%d",
955 if (port->icount.parity)
956 seq_printf(m, " pe:%d",
957 port->icount.parity);
958 if (port->icount.brk)
959 seq_printf(m, " brk:%d",
961 if (port->icount.overrun)
962 seq_printf(m, " oe:%d",
963 port->icount.overrun);
964 if (port->icount.cts)
965 seq_printf(m, " cts:%d",
967 if (port->icount.dsr)
968 seq_printf(m, " dsr:%d",
970 if (port->icount.rng)
971 seq_printf(m, " rng:%d",
973 if (port->icount.dcd)
974 seq_printf(m, " dcd:%d",
977 sdio_uart_port_put(port);
984 static int sdio_uart_proc_open(struct inode *inode, struct file *file)
986 return single_open(file, sdio_uart_proc_show, NULL);
989 static const struct file_operations sdio_uart_proc_fops = {
990 .owner = THIS_MODULE,
991 .open = sdio_uart_proc_open,
994 .release = single_release,
997 static const struct tty_operations sdio_uart_ops = {
998 .open = sdio_uart_open,
999 .close = sdio_uart_close,
1000 .write = sdio_uart_write,
1001 .write_room = sdio_uart_write_room,
1002 .chars_in_buffer = sdio_uart_chars_in_buffer,
1003 .send_xchar = sdio_uart_send_xchar,
1004 .throttle = sdio_uart_throttle,
1005 .unthrottle = sdio_uart_unthrottle,
1006 .set_termios = sdio_uart_set_termios,
1007 .break_ctl = sdio_uart_break_ctl,
1008 .tiocmget = sdio_uart_tiocmget,
1009 .tiocmset = sdio_uart_tiocmset,
1010 .proc_fops = &sdio_uart_proc_fops,
1013 static struct tty_driver *sdio_uart_tty_driver;
1015 static int sdio_uart_probe(struct sdio_func *func,
1016 const struct sdio_device_id *id)
1018 struct sdio_uart_port *port;
1021 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1025 if (func->class == SDIO_CLASS_UART) {
1026 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1027 sdio_func_id(func));
1030 } else if (func->class == SDIO_CLASS_GPS) {
1032 * We need tuple 0x91. It contains SUBTPL_SIOREG
1033 * and SUBTPL_RCVCAPS.
1035 struct sdio_func_tuple *tpl;
1036 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1037 if (tpl->code != 0x91)
1041 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1046 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1047 sdio_func_id(func));
1051 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1052 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1053 port->regs_offset = (tpl->data[4] << 0) |
1054 (tpl->data[5] << 8) |
1055 (tpl->data[6] << 16);
1056 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1057 sdio_func_id(func), port->regs_offset);
1058 port->uartclk = tpl->data[7] * 115200;
1059 if (port->uartclk == 0)
1060 port->uartclk = 115200;
1061 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1062 sdio_func_id(func), port->uartclk,
1063 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1070 sdio_set_drvdata(func, port);
1072 ret = sdio_uart_add_port(port);
1077 dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
1079 sdio_uart_port_remove(port);
1087 static void sdio_uart_remove(struct sdio_func *func)
1089 struct sdio_uart_port *port = sdio_get_drvdata(func);
1091 tty_unregister_device(sdio_uart_tty_driver, port->index);
1092 sdio_uart_port_remove(port);
1095 static const struct sdio_device_id sdio_uart_ids[] = {
1096 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1097 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1098 { /* end: all zeroes */ },
1101 MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1103 static struct sdio_driver sdio_uart_driver = {
1104 .probe = sdio_uart_probe,
1105 .remove = sdio_uart_remove,
1106 .name = "sdio_uart",
1107 .id_table = sdio_uart_ids,
1110 static int __init sdio_uart_init(void)
1113 struct tty_driver *tty_drv;
1115 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1119 tty_drv->owner = THIS_MODULE;
1120 tty_drv->driver_name = "sdio_uart";
1121 tty_drv->name = "ttySDIO";
1122 tty_drv->major = 0; /* dynamically allocated */
1123 tty_drv->minor_start = 0;
1124 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1125 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1126 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1127 tty_drv->init_termios = tty_std_termios;
1128 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1129 tty_drv->init_termios.c_ispeed = 4800;
1130 tty_drv->init_termios.c_ospeed = 4800;
1131 tty_set_operations(tty_drv, &sdio_uart_ops);
1133 ret = tty_register_driver(tty_drv);
1137 ret = sdio_register_driver(&sdio_uart_driver);
1144 tty_unregister_driver(tty_drv);
1146 put_tty_driver(tty_drv);
1150 static void __exit sdio_uart_exit(void)
1152 sdio_unregister_driver(&sdio_uart_driver);
1153 tty_unregister_driver(sdio_uart_tty_driver);
1154 put_tty_driver(sdio_uart_tty_driver);
1157 module_init(sdio_uart_init);
1158 module_exit(sdio_uart_exit);
1160 MODULE_AUTHOR("Nicolas Pitre");
1161 MODULE_LICENSE("GPL");