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/serial_reg.h>
34 #include <linux/circ_buf.h>
35 #include <linux/gfp.h>
36 #include <linux/tty.h>
37 #include <linux/tty_flip.h>
39 #include <linux/mmc/core.h>
40 #include <linux/mmc/card.h>
41 #include <linux/mmc/sdio_func.h>
42 #include <linux/mmc/sdio_ids.h>
45 #define UART_NR 8 /* Number of UARTs this driver can handle */
48 #define UART_XMIT_SIZE PAGE_SIZE
49 #define WAKEUP_CHARS 256
51 #define circ_empty(circ) ((circ)->head == (circ)->tail)
52 #define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
54 #define circ_chars_pending(circ) \
55 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
57 #define circ_chars_free(circ) \
58 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
74 struct sdio_uart_port {
76 struct tty_struct *tty;
79 struct mutex open_lock;
80 struct sdio_func *func;
81 struct mutex func_lock;
82 struct task_struct *in_sdio_uart_irq;
83 unsigned int regs_offset;
85 spinlock_t write_lock;
86 struct uart_icount icount;
89 unsigned int read_status_mask;
90 unsigned int ignore_status_mask;
96 static struct sdio_uart_port *sdio_uart_table[UART_NR];
97 static DEFINE_SPINLOCK(sdio_uart_table_lock);
99 static int sdio_uart_add_port(struct sdio_uart_port *port)
101 int index, ret = -EBUSY;
103 kref_init(&port->kref);
104 mutex_init(&port->open_lock);
105 mutex_init(&port->func_lock);
106 spin_lock_init(&port->write_lock);
108 spin_lock(&sdio_uart_table_lock);
109 for (index = 0; index < UART_NR; index++) {
110 if (!sdio_uart_table[index]) {
112 sdio_uart_table[index] = port;
117 spin_unlock(&sdio_uart_table_lock);
122 static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
124 struct sdio_uart_port *port;
126 if (index >= UART_NR)
129 spin_lock(&sdio_uart_table_lock);
130 port = sdio_uart_table[index];
132 kref_get(&port->kref);
133 spin_unlock(&sdio_uart_table_lock);
138 static void sdio_uart_port_destroy(struct kref *kref)
140 struct sdio_uart_port *port =
141 container_of(kref, struct sdio_uart_port, kref);
145 static void sdio_uart_port_put(struct sdio_uart_port *port)
147 kref_put(&port->kref, sdio_uart_port_destroy);
150 static void sdio_uart_port_remove(struct sdio_uart_port *port)
152 struct sdio_func *func;
154 BUG_ON(sdio_uart_table[port->index] != port);
156 spin_lock(&sdio_uart_table_lock);
157 sdio_uart_table[port->index] = NULL;
158 spin_unlock(&sdio_uart_table_lock);
161 * We're killing a port that potentially still is in use by
162 * the tty layer. Be careful to prevent any further access
163 * to the SDIO function and arrange for the tty layer to
164 * give up on that port ASAP.
165 * Beware: the lock ordering is critical.
167 mutex_lock(&port->open_lock);
168 mutex_lock(&port->func_lock);
170 sdio_claim_host(func);
172 mutex_unlock(&port->func_lock);
174 tty_hangup(port->tty);
175 mutex_unlock(&port->open_lock);
176 sdio_release_irq(func);
177 sdio_disable_func(func);
178 sdio_release_host(func);
180 sdio_uart_port_put(port);
183 static int sdio_uart_claim_func(struct sdio_uart_port *port)
185 mutex_lock(&port->func_lock);
186 if (unlikely(!port->func)) {
187 mutex_unlock(&port->func_lock);
190 if (likely(port->in_sdio_uart_irq != current))
191 sdio_claim_host(port->func);
192 mutex_unlock(&port->func_lock);
196 static inline void sdio_uart_release_func(struct sdio_uart_port *port)
198 if (likely(port->in_sdio_uart_irq != current))
199 sdio_release_host(port->func);
202 static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
205 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
209 static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
211 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
214 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
216 unsigned char status;
219 status = sdio_in(port, UART_MSR);
222 if (status & UART_MSR_DCD)
224 if (status & UART_MSR_RI)
226 if (status & UART_MSR_DSR)
228 if (status & UART_MSR_CTS)
233 static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
235 unsigned char mcr = 0;
237 if (mctrl & TIOCM_RTS)
239 if (mctrl & TIOCM_DTR)
241 if (mctrl & TIOCM_OUT1)
242 mcr |= UART_MCR_OUT1;
243 if (mctrl & TIOCM_OUT2)
244 mcr |= UART_MCR_OUT2;
245 if (mctrl & TIOCM_LOOP)
246 mcr |= UART_MCR_LOOP;
248 sdio_out(port, UART_MCR, mcr);
251 static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
252 unsigned int set, unsigned int clear)
257 port->mctrl = (old & ~clear) | set;
258 if (old != port->mctrl)
259 sdio_uart_write_mctrl(port, port->mctrl);
262 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
263 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
265 static void sdio_uart_change_speed(struct sdio_uart_port *port,
266 struct ktermios *termios,
267 struct ktermios *old)
269 unsigned char cval, fcr = 0;
270 unsigned int baud, quot;
272 switch (termios->c_cflag & CSIZE) {
274 cval = UART_LCR_WLEN5;
277 cval = UART_LCR_WLEN6;
280 cval = UART_LCR_WLEN7;
284 cval = UART_LCR_WLEN8;
288 if (termios->c_cflag & CSTOPB)
289 cval |= UART_LCR_STOP;
290 if (termios->c_cflag & PARENB)
291 cval |= UART_LCR_PARITY;
292 if (!(termios->c_cflag & PARODD))
293 cval |= UART_LCR_EPAR;
296 baud = tty_termios_baud_rate(termios);
298 baud = 9600; /* Special case: B0 rate. */
299 if (baud <= port->uartclk)
302 * Oops, the quotient was zero. Try again with the old
303 * baud rate if possible, otherwise default to 9600.
305 termios->c_cflag &= ~CBAUD;
307 termios->c_cflag |= old->c_cflag & CBAUD;
310 termios->c_cflag |= B9600;
312 quot = (2 * port->uartclk + baud) / (2 * baud);
315 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
317 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
319 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
320 if (termios->c_iflag & INPCK)
321 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
322 if (termios->c_iflag & (BRKINT | PARMRK))
323 port->read_status_mask |= UART_LSR_BI;
326 * Characters to ignore
328 port->ignore_status_mask = 0;
329 if (termios->c_iflag & IGNPAR)
330 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
331 if (termios->c_iflag & IGNBRK) {
332 port->ignore_status_mask |= UART_LSR_BI;
334 * If we're ignoring parity and break indicators,
335 * ignore overruns too (for real raw support).
337 if (termios->c_iflag & IGNPAR)
338 port->ignore_status_mask |= UART_LSR_OE;
342 * ignore all characters if CREAD is not set
344 if ((termios->c_cflag & CREAD) == 0)
345 port->ignore_status_mask |= UART_LSR_DR;
348 * CTS flow control flag and modem status interrupts
350 port->ier &= ~UART_IER_MSI;
351 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
352 port->ier |= UART_IER_MSI;
356 sdio_out(port, UART_IER, port->ier);
357 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
358 sdio_out(port, UART_DLL, quot & 0xff);
359 sdio_out(port, UART_DLM, quot >> 8);
360 sdio_out(port, UART_LCR, cval);
361 sdio_out(port, UART_FCR, fcr);
363 sdio_uart_write_mctrl(port, port->mctrl);
366 static void sdio_uart_start_tx(struct sdio_uart_port *port)
368 if (!(port->ier & UART_IER_THRI)) {
369 port->ier |= UART_IER_THRI;
370 sdio_out(port, UART_IER, port->ier);
374 static void sdio_uart_stop_tx(struct sdio_uart_port *port)
376 if (port->ier & UART_IER_THRI) {
377 port->ier &= ~UART_IER_THRI;
378 sdio_out(port, UART_IER, port->ier);
382 static void sdio_uart_stop_rx(struct sdio_uart_port *port)
384 port->ier &= ~UART_IER_RLSI;
385 port->read_status_mask &= ~UART_LSR_DR;
386 sdio_out(port, UART_IER, port->ier);
389 static void sdio_uart_receive_chars(struct sdio_uart_port *port, int *status)
391 struct tty_struct *tty = port->tty;
392 unsigned int ch, flag;
396 ch = sdio_in(port, UART_RX);
400 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
401 UART_LSR_FE | UART_LSR_OE))) {
403 * For statistics only
405 if (*status & UART_LSR_BI) {
406 *status &= ~(UART_LSR_FE | UART_LSR_PE);
408 } else if (*status & UART_LSR_PE)
409 port->icount.parity++;
410 else if (*status & UART_LSR_FE)
411 port->icount.frame++;
412 if (*status & UART_LSR_OE)
413 port->icount.overrun++;
416 * Mask off conditions which should be ignored.
418 *status &= port->read_status_mask;
419 if (*status & UART_LSR_BI) {
421 } else if (*status & UART_LSR_PE)
423 else if (*status & UART_LSR_FE)
427 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
428 tty_insert_flip_char(tty, ch, flag);
431 * Overrun is special. Since it's reported immediately,
432 * it doesn't affect the current character.
434 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
435 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
437 *status = sdio_in(port, UART_LSR);
438 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
439 tty_flip_buffer_push(tty);
442 static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
444 struct circ_buf *xmit = &port->xmit;
448 sdio_out(port, UART_TX, port->x_char);
453 if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
454 sdio_uart_stop_tx(port);
460 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
461 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
463 if (circ_empty(xmit))
465 } while (--count > 0);
467 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
468 tty_wakeup(port->tty);
470 if (circ_empty(xmit))
471 sdio_uart_stop_tx(port);
474 static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
478 status = sdio_in(port, UART_MSR);
480 if ((status & UART_MSR_ANY_DELTA) == 0)
483 if (status & UART_MSR_TERI)
485 if (status & UART_MSR_DDSR)
487 if (status & UART_MSR_DDCD)
489 if (status & UART_MSR_DCTS) {
491 if (port->tty->termios->c_cflag & CRTSCTS) {
492 int cts = (status & UART_MSR_CTS);
493 if (port->tty->hw_stopped) {
495 port->tty->hw_stopped = 0;
496 sdio_uart_start_tx(port);
497 tty_wakeup(port->tty);
501 port->tty->hw_stopped = 1;
502 sdio_uart_stop_tx(port);
510 * This handles the interrupt from one port.
512 static void sdio_uart_irq(struct sdio_func *func)
514 struct sdio_uart_port *port = sdio_get_drvdata(func);
515 unsigned int iir, lsr;
518 * In a few places sdio_uart_irq() is called directly instead of
519 * waiting for the actual interrupt to be raised and the SDIO IRQ
520 * thread scheduled in order to reduce latency. However, some
521 * interaction with the tty core may end up calling us back
522 * (serial echo, flow control, etc.) through those same places
523 * causing undesirable effects. Let's stop the recursion here.
525 if (unlikely(port->in_sdio_uart_irq == current))
528 iir = sdio_in(port, UART_IIR);
529 if (iir & UART_IIR_NO_INT)
532 port->in_sdio_uart_irq = current;
533 lsr = sdio_in(port, UART_LSR);
534 if (lsr & UART_LSR_DR)
535 sdio_uart_receive_chars(port, &lsr);
536 sdio_uart_check_modem_status(port);
537 if (lsr & UART_LSR_THRE)
538 sdio_uart_transmit_chars(port);
539 port->in_sdio_uart_irq = NULL;
542 static int sdio_uart_startup(struct sdio_uart_port *port)
548 * Set the TTY IO error marker - we will only clear this
549 * once we have successfully opened the port.
551 set_bit(TTY_IO_ERROR, &port->tty->flags);
553 /* Initialise and allocate the transmit buffer. */
554 page = __get_free_page(GFP_KERNEL);
557 port->xmit.buf = (unsigned char *)page;
558 circ_clear(&port->xmit);
560 ret = sdio_uart_claim_func(port);
563 ret = sdio_enable_func(port->func);
566 ret = sdio_claim_irq(port->func, sdio_uart_irq);
571 * Clear the FIFO buffers and disable them.
572 * (they will be reenabled in sdio_change_speed())
574 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
575 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
576 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
577 sdio_out(port, UART_FCR, 0);
580 * Clear the interrupt registers.
582 (void) sdio_in(port, UART_LSR);
583 (void) sdio_in(port, UART_RX);
584 (void) sdio_in(port, UART_IIR);
585 (void) sdio_in(port, UART_MSR);
588 * Now, initialize the UART
590 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
592 port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
593 port->mctrl = TIOCM_OUT2;
595 sdio_uart_change_speed(port, port->tty->termios, NULL);
597 if (port->tty->termios->c_cflag & CBAUD)
598 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
600 if (port->tty->termios->c_cflag & CRTSCTS)
601 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
602 port->tty->hw_stopped = 1;
604 clear_bit(TTY_IO_ERROR, &port->tty->flags);
606 /* Kick the IRQ handler once while we're still holding the host lock */
607 sdio_uart_irq(port->func);
609 sdio_uart_release_func(port);
613 sdio_disable_func(port->func);
615 sdio_uart_release_func(port);
617 free_page((unsigned long)port->xmit.buf);
621 static void sdio_uart_shutdown(struct sdio_uart_port *port)
625 ret = sdio_uart_claim_func(port);
629 sdio_uart_stop_rx(port);
631 /* TODO: wait here for TX FIFO to drain */
633 /* Turn off DTR and RTS early. */
634 if (port->tty->termios->c_cflag & HUPCL)
635 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
637 /* Disable interrupts from this port */
638 sdio_release_irq(port->func);
640 sdio_out(port, UART_IER, 0);
642 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
644 /* Disable break condition and FIFOs. */
645 port->lcr &= ~UART_LCR_SBC;
646 sdio_out(port, UART_LCR, port->lcr);
647 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
648 UART_FCR_CLEAR_RCVR |
649 UART_FCR_CLEAR_XMIT);
650 sdio_out(port, UART_FCR, 0);
652 sdio_disable_func(port->func);
654 sdio_uart_release_func(port);
657 /* Free the transmit buffer page. */
658 free_page((unsigned long)port->xmit.buf);
661 static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
663 struct sdio_uart_port *port;
666 port = sdio_uart_port_get(tty->index);
670 mutex_lock(&port->open_lock);
673 * Make sure not to mess up with a dead port
674 * which has not been closed yet.
676 if (tty->driver_data && tty->driver_data != port) {
677 mutex_unlock(&port->open_lock);
678 sdio_uart_port_put(port);
683 tty->driver_data = port;
685 ret = sdio_uart_startup(port);
687 tty->driver_data = NULL;
689 mutex_unlock(&port->open_lock);
690 sdio_uart_port_put(port);
695 mutex_unlock(&port->open_lock);
699 static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
701 struct sdio_uart_port *port = tty->driver_data;
706 mutex_lock(&port->open_lock);
707 BUG_ON(!port->opened);
710 * This is messy. The tty layer calls us even when open()
711 * returned an error. Ignore this close request if tty->count
712 * is larger than port->count.
714 if (tty->count > port->opened) {
715 mutex_unlock(&port->open_lock);
719 if (--port->opened == 0) {
721 sdio_uart_shutdown(port);
722 tty_ldisc_flush(tty);
724 tty->driver_data = NULL;
727 mutex_unlock(&port->open_lock);
728 sdio_uart_port_put(port);
731 static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
734 struct sdio_uart_port *port = tty->driver_data;
735 struct circ_buf *circ = &port->xmit;
741 spin_lock(&port->write_lock);
743 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
748 memcpy(circ->buf + circ->head, buf, c);
749 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
754 spin_unlock(&port->write_lock);
756 if ( !(port->ier & UART_IER_THRI)) {
757 int err = sdio_uart_claim_func(port);
759 sdio_uart_start_tx(port);
760 sdio_uart_irq(port->func);
761 sdio_uart_release_func(port);
769 static int sdio_uart_write_room(struct tty_struct *tty)
771 struct sdio_uart_port *port = tty->driver_data;
772 return port ? circ_chars_free(&port->xmit) : 0;
775 static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
777 struct sdio_uart_port *port = tty->driver_data;
778 return port ? circ_chars_pending(&port->xmit) : 0;
781 static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
783 struct sdio_uart_port *port = tty->driver_data;
786 if (ch && !(port->ier & UART_IER_THRI)) {
787 if (sdio_uart_claim_func(port) != 0)
789 sdio_uart_start_tx(port);
790 sdio_uart_irq(port->func);
791 sdio_uart_release_func(port);
795 static void sdio_uart_throttle(struct tty_struct *tty)
797 struct sdio_uart_port *port = tty->driver_data;
799 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
802 if (sdio_uart_claim_func(port) != 0)
806 port->x_char = STOP_CHAR(tty);
807 sdio_uart_start_tx(port);
810 if (tty->termios->c_cflag & CRTSCTS)
811 sdio_uart_clear_mctrl(port, TIOCM_RTS);
813 sdio_uart_irq(port->func);
814 sdio_uart_release_func(port);
817 static void sdio_uart_unthrottle(struct tty_struct *tty)
819 struct sdio_uart_port *port = tty->driver_data;
821 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
824 if (sdio_uart_claim_func(port) != 0)
831 port->x_char = START_CHAR(tty);
832 sdio_uart_start_tx(port);
836 if (tty->termios->c_cflag & CRTSCTS)
837 sdio_uart_set_mctrl(port, TIOCM_RTS);
839 sdio_uart_irq(port->func);
840 sdio_uart_release_func(port);
843 static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
845 struct sdio_uart_port *port = tty->driver_data;
846 unsigned int cflag = tty->termios->c_cflag;
848 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
850 if ((cflag ^ old_termios->c_cflag) == 0 &&
851 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
854 if (sdio_uart_claim_func(port) != 0)
857 sdio_uart_change_speed(port, tty->termios, old_termios);
859 /* Handle transition to B0 status */
860 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
861 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
863 /* Handle transition away from B0 status */
864 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
865 unsigned int mask = TIOCM_DTR;
866 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
868 sdio_uart_set_mctrl(port, mask);
871 /* Handle turning off CRTSCTS */
872 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
874 sdio_uart_start_tx(port);
877 /* Handle turning on CRTSCTS */
878 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
879 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
881 sdio_uart_stop_tx(port);
885 sdio_uart_release_func(port);
888 static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
890 struct sdio_uart_port *port = tty->driver_data;
892 if (sdio_uart_claim_func(port) != 0)
895 if (break_state == -1)
896 port->lcr |= UART_LCR_SBC;
898 port->lcr &= ~UART_LCR_SBC;
899 sdio_out(port, UART_LCR, port->lcr);
901 sdio_uart_release_func(port);
904 static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
906 struct sdio_uart_port *port = tty->driver_data;
909 result = sdio_uart_claim_func(port);
911 result = port->mctrl | sdio_uart_get_mctrl(port);
912 sdio_uart_release_func(port);
918 static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
919 unsigned int set, unsigned int clear)
921 struct sdio_uart_port *port = tty->driver_data;
924 result =sdio_uart_claim_func(port);
926 sdio_uart_update_mctrl(port, set, clear);
927 sdio_uart_release_func(port);
933 static int sdio_uart_read_proc(char *page, char **start, off_t off,
934 int count, int *eof, void *data)
939 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
941 for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
942 struct sdio_uart_port *port = sdio_uart_port_get(i);
944 len += sprintf(page+len, "%d: uart:SDIO", i);
945 if(capable(CAP_SYS_ADMIN)) {
946 len += sprintf(page + len, " tx:%d rx:%d",
947 port->icount.tx, port->icount.rx);
948 if (port->icount.frame)
949 len += sprintf(page + len, " fe:%d",
951 if (port->icount.parity)
952 len += sprintf(page + len, " pe:%d",
953 port->icount.parity);
954 if (port->icount.brk)
955 len += sprintf(page + len, " brk:%d",
957 if (port->icount.overrun)
958 len += sprintf(page + len, " oe:%d",
959 port->icount.overrun);
960 if (port->icount.cts)
961 len += sprintf(page + len, " cts:%d",
963 if (port->icount.dsr)
964 len += sprintf(page + len, " dsr:%d",
966 if (port->icount.rng)
967 len += sprintf(page + len, " rng:%d",
969 if (port->icount.dcd)
970 len += sprintf(page + len, " dcd:%d",
975 sdio_uart_port_put(port);
978 if (len + begin > off + count)
980 if (len + begin < off) {
988 if (off >= len + begin)
990 *start = page + (off - begin);
991 return (count < begin + len - off) ? count : (begin + len - off);
994 static const struct tty_operations sdio_uart_ops = {
995 .open = sdio_uart_open,
996 .close = sdio_uart_close,
997 .write = sdio_uart_write,
998 .write_room = sdio_uart_write_room,
999 .chars_in_buffer = sdio_uart_chars_in_buffer,
1000 .send_xchar = sdio_uart_send_xchar,
1001 .throttle = sdio_uart_throttle,
1002 .unthrottle = sdio_uart_unthrottle,
1003 .set_termios = sdio_uart_set_termios,
1004 .break_ctl = sdio_uart_break_ctl,
1005 .tiocmget = sdio_uart_tiocmget,
1006 .tiocmset = sdio_uart_tiocmset,
1007 .read_proc = sdio_uart_read_proc,
1010 static struct tty_driver *sdio_uart_tty_driver;
1012 static int sdio_uart_probe(struct sdio_func *func,
1013 const struct sdio_device_id *id)
1015 struct sdio_uart_port *port;
1018 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1022 if (func->class == SDIO_CLASS_UART) {
1023 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1024 sdio_func_id(func));
1027 } else if (func->class == SDIO_CLASS_GPS) {
1029 * We need tuple 0x91. It contains SUBTPL_SIOREG
1030 * and SUBTPL_RCVCAPS.
1032 struct sdio_func_tuple *tpl;
1033 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1034 if (tpl->code != 0x91)
1038 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1043 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1044 sdio_func_id(func));
1048 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1049 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1050 port->regs_offset = (tpl->data[4] << 0) |
1051 (tpl->data[5] << 8) |
1052 (tpl->data[6] << 16);
1053 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1054 sdio_func_id(func), port->regs_offset);
1055 port->uartclk = tpl->data[7] * 115200;
1056 if (port->uartclk == 0)
1057 port->uartclk = 115200;
1058 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1059 sdio_func_id(func), port->uartclk,
1060 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1067 sdio_set_drvdata(func, port);
1069 ret = sdio_uart_add_port(port);
1074 dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
1076 sdio_uart_port_remove(port);
1084 static void sdio_uart_remove(struct sdio_func *func)
1086 struct sdio_uart_port *port = sdio_get_drvdata(func);
1088 tty_unregister_device(sdio_uart_tty_driver, port->index);
1089 sdio_uart_port_remove(port);
1092 static const struct sdio_device_id sdio_uart_ids[] = {
1093 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1094 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1095 { /* end: all zeroes */ },
1098 MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1100 static struct sdio_driver sdio_uart_driver = {
1101 .probe = sdio_uart_probe,
1102 .remove = sdio_uart_remove,
1103 .name = "sdio_uart",
1104 .id_table = sdio_uart_ids,
1107 static int __init sdio_uart_init(void)
1110 struct tty_driver *tty_drv;
1112 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1116 tty_drv->owner = THIS_MODULE;
1117 tty_drv->driver_name = "sdio_uart";
1118 tty_drv->name = "ttySDIO";
1119 tty_drv->major = 0; /* dynamically allocated */
1120 tty_drv->minor_start = 0;
1121 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1122 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1123 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1124 tty_drv->init_termios = tty_std_termios;
1125 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1126 tty_drv->init_termios.c_ispeed = 4800;
1127 tty_drv->init_termios.c_ospeed = 4800;
1128 tty_set_operations(tty_drv, &sdio_uart_ops);
1130 ret = tty_register_driver(tty_drv);
1134 ret = sdio_register_driver(&sdio_uart_driver);
1141 tty_unregister_driver(tty_drv);
1143 put_tty_driver(tty_drv);
1147 static void __exit sdio_uart_exit(void)
1149 sdio_unregister_driver(&sdio_uart_driver);
1150 tty_unregister_driver(sdio_uart_tty_driver);
1151 put_tty_driver(sdio_uart_tty_driver);
1154 module_init(sdio_uart_init);
1155 module_exit(sdio_uart_exit);
1157 MODULE_AUTHOR("Nicolas Pitre");
1158 MODULE_LICENSE("GPL");