bzip2/lzma: quiet Kconfig warning for INITRAMFS_COMPRESSION_NONE
[linux-2.6/linux-2.6-openrd.git] / drivers / mmc / card / sdio_uart.c
blob78ad48718ab028e61b1d77d83e29de8efbe9930c
1 /*
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
5 * by Russell King.
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))
61 struct uart_icount {
62 __u32 cts;
63 __u32 dsr;
64 __u32 rng;
65 __u32 dcd;
66 __u32 rx;
67 __u32 tx;
68 __u32 frame;
69 __u32 overrun;
70 __u32 parity;
71 __u32 brk;
74 struct sdio_uart_port {
75 struct kref kref;
76 struct tty_struct *tty;
77 unsigned int index;
78 unsigned int opened;
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;
84 struct circ_buf xmit;
85 spinlock_t write_lock;
86 struct uart_icount icount;
87 unsigned int uartclk;
88 unsigned int mctrl;
89 unsigned int read_status_mask;
90 unsigned int ignore_status_mask;
91 unsigned char x_char;
92 unsigned char ier;
93 unsigned char lcr;
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]) {
111 port->index = index;
112 sdio_uart_table[index] = port;
113 ret = 0;
114 break;
117 spin_unlock(&sdio_uart_table_lock);
119 return ret;
122 static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
124 struct sdio_uart_port *port;
126 if (index >= UART_NR)
127 return NULL;
129 spin_lock(&sdio_uart_table_lock);
130 port = sdio_uart_table[index];
131 if (port)
132 kref_get(&port->kref);
133 spin_unlock(&sdio_uart_table_lock);
135 return port;
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);
142 kfree(port);
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);
169 func = port->func;
170 sdio_claim_host(func);
171 port->func = NULL;
172 mutex_unlock(&port->func_lock);
173 if (port->opened)
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);
188 return -ENODEV;
190 if (likely(port->in_sdio_uart_irq != current))
191 sdio_claim_host(port->func);
192 mutex_unlock(&port->func_lock);
193 return 0;
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)
204 unsigned char c;
205 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
206 return c;
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;
217 unsigned int ret;
219 status = sdio_in(port, UART_MSR);
221 ret = 0;
222 if (status & UART_MSR_DCD)
223 ret |= TIOCM_CAR;
224 if (status & UART_MSR_RI)
225 ret |= TIOCM_RNG;
226 if (status & UART_MSR_DSR)
227 ret |= TIOCM_DSR;
228 if (status & UART_MSR_CTS)
229 ret |= TIOCM_CTS;
230 return ret;
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)
238 mcr |= UART_MCR_RTS;
239 if (mctrl & TIOCM_DTR)
240 mcr |= UART_MCR_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)
254 unsigned int old;
256 old = port->mctrl;
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) {
273 case CS5:
274 cval = UART_LCR_WLEN5;
275 break;
276 case CS6:
277 cval = UART_LCR_WLEN6;
278 break;
279 case CS7:
280 cval = UART_LCR_WLEN7;
281 break;
282 default:
283 case CS8:
284 cval = UART_LCR_WLEN8;
285 break;
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;
295 for (;;) {
296 baud = tty_termios_baud_rate(termios);
297 if (baud == 0)
298 baud = 9600; /* Special case: B0 rate. */
299 if (baud <= port->uartclk)
300 break;
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;
306 if (old) {
307 termios->c_cflag |= old->c_cflag & CBAUD;
308 old = NULL;
309 } else
310 termios->c_cflag |= B9600;
312 quot = (2 * port->uartclk + baud) / (2 * baud);
314 if (baud < 2400)
315 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
316 else
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;
354 port->lcr = cval;
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, unsigned int *status)
391 struct tty_struct *tty = port->tty;
392 unsigned int ch, flag;
393 int max_count = 256;
395 do {
396 ch = sdio_in(port, UART_RX);
397 flag = TTY_NORMAL;
398 port->icount.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);
407 port->icount.brk++;
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) {
420 flag = TTY_BREAK;
421 } else if (*status & UART_LSR_PE)
422 flag = TTY_PARITY;
423 else if (*status & UART_LSR_FE)
424 flag = TTY_FRAME;
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;
445 int count;
447 if (port->x_char) {
448 sdio_out(port, UART_TX, port->x_char);
449 port->icount.tx++;
450 port->x_char = 0;
451 return;
453 if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
454 sdio_uart_stop_tx(port);
455 return;
458 count = 16;
459 do {
460 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
461 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
462 port->icount.tx++;
463 if (circ_empty(xmit))
464 break;
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)
476 int status;
478 status = sdio_in(port, UART_MSR);
480 if ((status & UART_MSR_ANY_DELTA) == 0)
481 return;
483 if (status & UART_MSR_TERI)
484 port->icount.rng++;
485 if (status & UART_MSR_DDSR)
486 port->icount.dsr++;
487 if (status & UART_MSR_DDCD)
488 port->icount.dcd++;
489 if (status & UART_MSR_DCTS) {
490 port->icount.cts++;
491 if (port->tty->termios->c_cflag & CRTSCTS) {
492 int cts = (status & UART_MSR_CTS);
493 if (port->tty->hw_stopped) {
494 if (cts) {
495 port->tty->hw_stopped = 0;
496 sdio_uart_start_tx(port);
497 tty_wakeup(port->tty);
499 } else {
500 if (!cts) {
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))
526 return;
528 iir = sdio_in(port, UART_IIR);
529 if (iir & UART_IIR_NO_INT)
530 return;
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)
544 unsigned long page;
545 int ret;
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);
555 if (!page)
556 return -ENOMEM;
557 port->xmit.buf = (unsigned char *)page;
558 circ_clear(&port->xmit);
560 ret = sdio_uart_claim_func(port);
561 if (ret)
562 goto err1;
563 ret = sdio_enable_func(port->func);
564 if (ret)
565 goto err2;
566 ret = sdio_claim_irq(port->func, sdio_uart_irq);
567 if (ret)
568 goto err3;
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);
610 return 0;
612 err3:
613 sdio_disable_func(port->func);
614 err2:
615 sdio_uart_release_func(port);
616 err1:
617 free_page((unsigned long)port->xmit.buf);
618 return ret;
621 static void sdio_uart_shutdown(struct sdio_uart_port *port)
623 int ret;
625 ret = sdio_uart_claim_func(port);
626 if (ret)
627 goto skip;
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);
639 port->ier = 0;
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);
656 skip:
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;
664 int ret;
666 port = sdio_uart_port_get(tty->index);
667 if (!port)
668 return -ENODEV;
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);
679 return -EBUSY;
682 if (!port->opened) {
683 tty->driver_data = port;
684 port->tty = tty;
685 ret = sdio_uart_startup(port);
686 if (ret) {
687 tty->driver_data = NULL;
688 port->tty = NULL;
689 mutex_unlock(&port->open_lock);
690 sdio_uart_port_put(port);
691 return ret;
694 port->opened++;
695 mutex_unlock(&port->open_lock);
696 return 0;
699 static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
701 struct sdio_uart_port *port = tty->driver_data;
703 if (!port)
704 return;
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);
716 return;
719 if (--port->opened == 0) {
720 tty->closing = 1;
721 sdio_uart_shutdown(port);
722 tty_ldisc_flush(tty);
723 port->tty = NULL;
724 tty->driver_data = NULL;
725 tty->closing = 0;
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,
732 int count)
734 struct sdio_uart_port *port = tty->driver_data;
735 struct circ_buf *circ = &port->xmit;
736 int c, ret = 0;
738 if (!port->func)
739 return -ENODEV;
741 spin_lock(&port->write_lock);
742 while (1) {
743 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
744 if (count < c)
745 c = count;
746 if (c <= 0)
747 break;
748 memcpy(circ->buf + circ->head, buf, c);
749 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
750 buf += c;
751 count -= c;
752 ret += c;
754 spin_unlock(&port->write_lock);
756 if ( !(port->ier & UART_IER_THRI)) {
757 int err = sdio_uart_claim_func(port);
758 if (!err) {
759 sdio_uart_start_tx(port);
760 sdio_uart_irq(port->func);
761 sdio_uart_release_func(port);
762 } else
763 ret = err;
766 return ret;
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;
785 port->x_char = ch;
786 if (ch && !(port->ier & UART_IER_THRI)) {
787 if (sdio_uart_claim_func(port) != 0)
788 return;
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))
800 return;
802 if (sdio_uart_claim_func(port) != 0)
803 return;
805 if (I_IXOFF(tty)) {
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))
822 return;
824 if (sdio_uart_claim_func(port) != 0)
825 return;
827 if (I_IXOFF(tty)) {
828 if (port->x_char) {
829 port->x_char = 0;
830 } else {
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)
852 return;
854 if (sdio_uart_claim_func(port) != 0)
855 return;
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))
867 mask |= TIOCM_RTS;
868 sdio_uart_set_mctrl(port, mask);
871 /* Handle turning off CRTSCTS */
872 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
873 tty->hw_stopped = 0;
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)) {
880 tty->hw_stopped = 1;
881 sdio_uart_stop_tx(port);
885 sdio_uart_release_func(port);
888 static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
890 struct sdio_uart_port *port = tty->driver_data;
891 int result;
893 result = sdio_uart_claim_func(port);
894 if (result != 0)
895 return result;
897 if (break_state == -1)
898 port->lcr |= UART_LCR_SBC;
899 else
900 port->lcr &= ~UART_LCR_SBC;
901 sdio_out(port, UART_LCR, port->lcr);
903 sdio_uart_release_func(port);
904 return 0;
907 static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
909 struct sdio_uart_port *port = tty->driver_data;
910 int result;
912 result = sdio_uart_claim_func(port);
913 if (!result) {
914 result = port->mctrl | sdio_uart_get_mctrl(port);
915 sdio_uart_release_func(port);
918 return result;
921 static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
922 unsigned int set, unsigned int clear)
924 struct sdio_uart_port *port = tty->driver_data;
925 int result;
927 result =sdio_uart_claim_func(port);
928 if(!result) {
929 sdio_uart_update_mctrl(port, set, clear);
930 sdio_uart_release_func(port);
933 return result;
936 static int sdio_uart_read_proc(char *page, char **start, off_t off,
937 int count, int *eof, void *data)
939 int i, len = 0;
940 off_t begin = 0;
942 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
943 "", "", "");
944 for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
945 struct sdio_uart_port *port = sdio_uart_port_get(i);
946 if (port) {
947 len += sprintf(page+len, "%d: uart:SDIO", i);
948 if(capable(CAP_SYS_ADMIN)) {
949 len += sprintf(page + len, " tx:%d rx:%d",
950 port->icount.tx, port->icount.rx);
951 if (port->icount.frame)
952 len += sprintf(page + len, " fe:%d",
953 port->icount.frame);
954 if (port->icount.parity)
955 len += sprintf(page + len, " pe:%d",
956 port->icount.parity);
957 if (port->icount.brk)
958 len += sprintf(page + len, " brk:%d",
959 port->icount.brk);
960 if (port->icount.overrun)
961 len += sprintf(page + len, " oe:%d",
962 port->icount.overrun);
963 if (port->icount.cts)
964 len += sprintf(page + len, " cts:%d",
965 port->icount.cts);
966 if (port->icount.dsr)
967 len += sprintf(page + len, " dsr:%d",
968 port->icount.dsr);
969 if (port->icount.rng)
970 len += sprintf(page + len, " rng:%d",
971 port->icount.rng);
972 if (port->icount.dcd)
973 len += sprintf(page + len, " dcd:%d",
974 port->icount.dcd);
976 strcat(page, "\n");
977 len++;
978 sdio_uart_port_put(port);
981 if (len + begin > off + count)
982 goto done;
983 if (len + begin < off) {
984 begin += len;
985 len = 0;
988 *eof = 1;
990 done:
991 if (off >= len + begin)
992 return 0;
993 *start = page + (off - begin);
994 return (count < begin + len - off) ? count : (begin + len - off);
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 .read_proc = sdio_uart_read_proc,
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;
1019 int ret;
1021 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1022 if (!port)
1023 return -ENOMEM;
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));
1028 kfree(port);
1029 return -ENOSYS;
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)
1038 continue;
1039 if (tpl->size < 10)
1040 continue;
1041 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1042 break;
1044 if (!tpl) {
1045 printk(KERN_WARNING
1046 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1047 sdio_func_id(func));
1048 kfree(port);
1049 return -EINVAL;
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));
1064 } else {
1065 kfree(port);
1066 return -EINVAL;
1069 port->func = func;
1070 sdio_set_drvdata(func, port);
1072 ret = sdio_uart_add_port(port);
1073 if (ret) {
1074 kfree(port);
1075 } else {
1076 struct device *dev;
1077 dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
1078 if (IS_ERR(dev)) {
1079 sdio_uart_port_remove(port);
1080 ret = PTR_ERR(dev);
1084 return ret;
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)
1112 int ret;
1113 struct tty_driver *tty_drv;
1115 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1116 if (!tty_drv)
1117 return -ENOMEM;
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);
1134 if (ret)
1135 goto err1;
1137 ret = sdio_register_driver(&sdio_uart_driver);
1138 if (ret)
1139 goto err2;
1141 return 0;
1143 err2:
1144 tty_unregister_driver(tty_drv);
1145 err1:
1146 put_tty_driver(tty_drv);
1147 return ret;
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");