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 unsigned int regs_offset
;
84 spinlock_t write_lock
;
85 struct uart_icount icount
;
88 unsigned int read_status_mask
;
89 unsigned int ignore_status_mask
;
95 static struct sdio_uart_port
*sdio_uart_table
[UART_NR
];
96 static DEFINE_SPINLOCK(sdio_uart_table_lock
);
98 static int sdio_uart_add_port(struct sdio_uart_port
*port
)
100 int index
, ret
= -EBUSY
;
102 kref_init(&port
->kref
);
103 mutex_init(&port
->open_lock
);
104 mutex_init(&port
->func_lock
);
105 spin_lock_init(&port
->write_lock
);
107 spin_lock(&sdio_uart_table_lock
);
108 for (index
= 0; index
< UART_NR
; index
++) {
109 if (!sdio_uart_table
[index
]) {
111 sdio_uart_table
[index
] = port
;
116 spin_unlock(&sdio_uart_table_lock
);
121 static struct sdio_uart_port
*sdio_uart_port_get(unsigned index
)
123 struct sdio_uart_port
*port
;
125 if (index
>= UART_NR
)
128 spin_lock(&sdio_uart_table_lock
);
129 port
= sdio_uart_table
[index
];
131 kref_get(&port
->kref
);
132 spin_unlock(&sdio_uart_table_lock
);
137 static void sdio_uart_port_destroy(struct kref
*kref
)
139 struct sdio_uart_port
*port
=
140 container_of(kref
, struct sdio_uart_port
, kref
);
144 static void sdio_uart_port_put(struct sdio_uart_port
*port
)
146 kref_put(&port
->kref
, sdio_uart_port_destroy
);
149 static void sdio_uart_port_remove(struct sdio_uart_port
*port
)
151 struct sdio_func
*func
;
153 BUG_ON(sdio_uart_table
[port
->index
] != port
);
155 spin_lock(&sdio_uart_table_lock
);
156 sdio_uart_table
[port
->index
] = NULL
;
157 spin_unlock(&sdio_uart_table_lock
);
160 * We're killing a port that potentially still is in use by
161 * the tty layer. Be careful to prevent any further access
162 * to the SDIO function and arrange for the tty layer to
163 * give up on that port ASAP.
164 * Beware: the lock ordering is critical.
166 mutex_lock(&port
->open_lock
);
167 mutex_lock(&port
->func_lock
);
169 sdio_claim_host(func
);
171 mutex_unlock(&port
->func_lock
);
173 tty_hangup(port
->tty
);
174 mutex_unlock(&port
->open_lock
);
175 sdio_release_irq(func
);
176 sdio_disable_func(func
);
177 sdio_release_host(func
);
179 sdio_uart_port_put(port
);
182 static int sdio_uart_claim_func(struct sdio_uart_port
*port
)
184 mutex_lock(&port
->func_lock
);
185 if (unlikely(!port
->func
)) {
186 mutex_unlock(&port
->func_lock
);
189 sdio_claim_host(port
->func
);
190 mutex_unlock(&port
->func_lock
);
194 static inline void sdio_uart_release_func(struct sdio_uart_port
*port
)
196 sdio_release_host(port
->func
);
199 static inline unsigned int sdio_in(struct sdio_uart_port
*port
, int offset
)
202 c
= sdio_readb(port
->func
, port
->regs_offset
+ offset
, NULL
);
206 static inline void sdio_out(struct sdio_uart_port
*port
, int offset
, int value
)
208 sdio_writeb(port
->func
, value
, port
->regs_offset
+ offset
, NULL
);
211 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port
*port
)
213 unsigned char status
;
216 status
= sdio_in(port
, UART_MSR
);
219 if (status
& UART_MSR_DCD
)
221 if (status
& UART_MSR_RI
)
223 if (status
& UART_MSR_DSR
)
225 if (status
& UART_MSR_CTS
)
230 static void sdio_uart_write_mctrl(struct sdio_uart_port
*port
, unsigned int mctrl
)
232 unsigned char mcr
= 0;
234 if (mctrl
& TIOCM_RTS
)
236 if (mctrl
& TIOCM_DTR
)
238 if (mctrl
& TIOCM_OUT1
)
239 mcr
|= UART_MCR_OUT1
;
240 if (mctrl
& TIOCM_OUT2
)
241 mcr
|= UART_MCR_OUT2
;
242 if (mctrl
& TIOCM_LOOP
)
243 mcr
|= UART_MCR_LOOP
;
245 sdio_out(port
, UART_MCR
, mcr
);
248 static inline void sdio_uart_update_mctrl(struct sdio_uart_port
*port
,
249 unsigned int set
, unsigned int clear
)
254 port
->mctrl
= (old
& ~clear
) | set
;
255 if (old
!= port
->mctrl
)
256 sdio_uart_write_mctrl(port
, port
->mctrl
);
259 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
260 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
262 static void sdio_uart_change_speed(struct sdio_uart_port
*port
,
263 struct ktermios
*termios
,
264 struct ktermios
*old
)
266 unsigned char cval
, fcr
= 0;
267 unsigned int baud
, quot
;
269 switch (termios
->c_cflag
& CSIZE
) {
271 cval
= UART_LCR_WLEN5
;
274 cval
= UART_LCR_WLEN6
;
277 cval
= UART_LCR_WLEN7
;
281 cval
= UART_LCR_WLEN8
;
285 if (termios
->c_cflag
& CSTOPB
)
286 cval
|= UART_LCR_STOP
;
287 if (termios
->c_cflag
& PARENB
)
288 cval
|= UART_LCR_PARITY
;
289 if (!(termios
->c_cflag
& PARODD
))
290 cval
|= UART_LCR_EPAR
;
293 baud
= tty_termios_baud_rate(termios
);
295 baud
= 9600; /* Special case: B0 rate. */
296 if (baud
<= port
->uartclk
)
299 * Oops, the quotient was zero. Try again with the old
300 * baud rate if possible, otherwise default to 9600.
302 termios
->c_cflag
&= ~CBAUD
;
304 termios
->c_cflag
|= old
->c_cflag
& CBAUD
;
307 termios
->c_cflag
|= B9600
;
309 quot
= (2 * port
->uartclk
+ baud
) / (2 * baud
);
312 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_1
;
314 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_R_TRIG_10
;
316 port
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
317 if (termios
->c_iflag
& INPCK
)
318 port
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
319 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
320 port
->read_status_mask
|= UART_LSR_BI
;
323 * Characters to ignore
325 port
->ignore_status_mask
= 0;
326 if (termios
->c_iflag
& IGNPAR
)
327 port
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
328 if (termios
->c_iflag
& IGNBRK
) {
329 port
->ignore_status_mask
|= UART_LSR_BI
;
331 * If we're ignoring parity and break indicators,
332 * ignore overruns too (for real raw support).
334 if (termios
->c_iflag
& IGNPAR
)
335 port
->ignore_status_mask
|= UART_LSR_OE
;
339 * ignore all characters if CREAD is not set
341 if ((termios
->c_cflag
& CREAD
) == 0)
342 port
->ignore_status_mask
|= UART_LSR_DR
;
345 * CTS flow control flag and modem status interrupts
347 port
->ier
&= ~UART_IER_MSI
;
348 if ((termios
->c_cflag
& CRTSCTS
) || !(termios
->c_cflag
& CLOCAL
))
349 port
->ier
|= UART_IER_MSI
;
353 sdio_out(port
, UART_IER
, port
->ier
);
354 sdio_out(port
, UART_LCR
, cval
| UART_LCR_DLAB
);
355 sdio_out(port
, UART_DLL
, quot
& 0xff);
356 sdio_out(port
, UART_DLM
, quot
>> 8);
357 sdio_out(port
, UART_LCR
, cval
);
358 sdio_out(port
, UART_FCR
, fcr
);
360 sdio_uart_write_mctrl(port
, port
->mctrl
);
363 static void sdio_uart_start_tx(struct sdio_uart_port
*port
)
365 if (!(port
->ier
& UART_IER_THRI
)) {
366 port
->ier
|= UART_IER_THRI
;
367 sdio_out(port
, UART_IER
, port
->ier
);
371 static void sdio_uart_stop_tx(struct sdio_uart_port
*port
)
373 if (port
->ier
& UART_IER_THRI
) {
374 port
->ier
&= ~UART_IER_THRI
;
375 sdio_out(port
, UART_IER
, port
->ier
);
379 static void sdio_uart_stop_rx(struct sdio_uart_port
*port
)
381 port
->ier
&= ~UART_IER_RLSI
;
382 port
->read_status_mask
&= ~UART_LSR_DR
;
383 sdio_out(port
, UART_IER
, port
->ier
);
386 static void sdio_uart_receive_chars(struct sdio_uart_port
*port
, int *status
)
388 struct tty_struct
*tty
= port
->tty
;
389 unsigned int ch
, flag
;
393 ch
= sdio_in(port
, UART_RX
);
397 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
398 UART_LSR_FE
| UART_LSR_OE
))) {
400 * For statistics only
402 if (*status
& UART_LSR_BI
) {
403 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
405 } else if (*status
& UART_LSR_PE
)
406 port
->icount
.parity
++;
407 else if (*status
& UART_LSR_FE
)
408 port
->icount
.frame
++;
409 if (*status
& UART_LSR_OE
)
410 port
->icount
.overrun
++;
413 * Mask off conditions which should be ignored.
415 *status
&= port
->read_status_mask
;
416 if (*status
& UART_LSR_BI
) {
418 } else if (*status
& UART_LSR_PE
)
420 else if (*status
& UART_LSR_FE
)
424 if ((*status
& port
->ignore_status_mask
& ~UART_LSR_OE
) == 0)
425 tty_insert_flip_char(tty
, ch
, flag
);
428 * Overrun is special. Since it's reported immediately,
429 * it doesn't affect the current character.
431 if (*status
& ~port
->ignore_status_mask
& UART_LSR_OE
)
432 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
434 *status
= sdio_in(port
, UART_LSR
);
435 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
436 tty_flip_buffer_push(tty
);
439 static void sdio_uart_transmit_chars(struct sdio_uart_port
*port
)
441 struct circ_buf
*xmit
= &port
->xmit
;
445 sdio_out(port
, UART_TX
, port
->x_char
);
450 if (circ_empty(xmit
) || port
->tty
->stopped
|| port
->tty
->hw_stopped
) {
451 sdio_uart_stop_tx(port
);
457 sdio_out(port
, UART_TX
, xmit
->buf
[xmit
->tail
]);
458 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
460 if (circ_empty(xmit
))
462 } while (--count
> 0);
464 if (circ_chars_pending(xmit
) < WAKEUP_CHARS
)
465 tty_wakeup(port
->tty
);
467 if (circ_empty(xmit
))
468 sdio_uart_stop_tx(port
);
471 static void sdio_uart_check_modem_status(struct sdio_uart_port
*port
)
475 status
= sdio_in(port
, UART_MSR
);
477 if ((status
& UART_MSR_ANY_DELTA
) == 0)
480 if (status
& UART_MSR_TERI
)
482 if (status
& UART_MSR_DDSR
)
484 if (status
& UART_MSR_DDCD
)
486 if (status
& UART_MSR_DCTS
) {
488 if (port
->tty
->termios
->c_cflag
& CRTSCTS
) {
489 int cts
= (status
& UART_MSR_CTS
);
490 if (port
->tty
->hw_stopped
) {
492 port
->tty
->hw_stopped
= 0;
493 sdio_uart_start_tx(port
);
494 tty_wakeup(port
->tty
);
498 port
->tty
->hw_stopped
= 1;
499 sdio_uart_stop_tx(port
);
507 * This handles the interrupt from one port.
509 static void sdio_uart_irq(struct sdio_func
*func
)
511 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
512 unsigned int iir
, lsr
;
514 iir
= sdio_in(port
, UART_IIR
);
515 if (iir
& UART_IIR_NO_INT
)
517 lsr
= sdio_in(port
, UART_LSR
);
518 if (lsr
& UART_LSR_DR
)
519 sdio_uart_receive_chars(port
, &lsr
);
520 sdio_uart_check_modem_status(port
);
521 if (lsr
& UART_LSR_THRE
)
522 sdio_uart_transmit_chars(port
);
525 static int sdio_uart_startup(struct sdio_uart_port
*port
)
531 * Set the TTY IO error marker - we will only clear this
532 * once we have successfully opened the port.
534 set_bit(TTY_IO_ERROR
, &port
->tty
->flags
);
536 /* Initialise and allocate the transmit buffer. */
537 page
= __get_free_page(GFP_KERNEL
);
540 port
->xmit
.buf
= (unsigned char *)page
;
541 circ_clear(&port
->xmit
);
543 ret
= sdio_uart_claim_func(port
);
546 ret
= sdio_enable_func(port
->func
);
549 ret
= sdio_claim_irq(port
->func
, sdio_uart_irq
);
554 * Clear the FIFO buffers and disable them.
555 * (they will be reenabled in sdio_change_speed())
557 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
558 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
559 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
560 sdio_out(port
, UART_FCR
, 0);
563 * Clear the interrupt registers.
565 (void) sdio_in(port
, UART_LSR
);
566 (void) sdio_in(port
, UART_RX
);
567 (void) sdio_in(port
, UART_IIR
);
568 (void) sdio_in(port
, UART_MSR
);
571 * Now, initialize the UART
573 sdio_out(port
, UART_LCR
, UART_LCR_WLEN8
);
575 port
->ier
= UART_IER_RLSI
| UART_IER_RDI
| UART_IER_RTOIE
| UART_IER_UUE
;
576 port
->mctrl
= TIOCM_OUT2
;
578 sdio_uart_change_speed(port
, port
->tty
->termios
, NULL
);
580 if (port
->tty
->termios
->c_cflag
& CBAUD
)
581 sdio_uart_set_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
583 if (port
->tty
->termios
->c_cflag
& CRTSCTS
)
584 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
))
585 port
->tty
->hw_stopped
= 1;
587 clear_bit(TTY_IO_ERROR
, &port
->tty
->flags
);
589 /* Kick the IRQ handler once while we're still holding the host lock */
590 sdio_uart_irq(port
->func
);
592 sdio_uart_release_func(port
);
596 sdio_disable_func(port
->func
);
598 sdio_uart_release_func(port
);
600 free_page((unsigned long)port
->xmit
.buf
);
604 static void sdio_uart_shutdown(struct sdio_uart_port
*port
)
608 ret
= sdio_uart_claim_func(port
);
612 sdio_uart_stop_rx(port
);
614 /* TODO: wait here for TX FIFO to drain */
616 /* Turn off DTR and RTS early. */
617 if (port
->tty
->termios
->c_cflag
& HUPCL
)
618 sdio_uart_clear_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
620 /* Disable interrupts from this port */
621 sdio_release_irq(port
->func
);
623 sdio_out(port
, UART_IER
, 0);
625 sdio_uart_clear_mctrl(port
, TIOCM_OUT2
);
627 /* Disable break condition and FIFOs. */
628 port
->lcr
&= ~UART_LCR_SBC
;
629 sdio_out(port
, UART_LCR
, port
->lcr
);
630 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
631 UART_FCR_CLEAR_RCVR
|
632 UART_FCR_CLEAR_XMIT
);
633 sdio_out(port
, UART_FCR
, 0);
635 sdio_disable_func(port
->func
);
637 sdio_uart_release_func(port
);
640 /* Free the transmit buffer page. */
641 free_page((unsigned long)port
->xmit
.buf
);
644 static int sdio_uart_open (struct tty_struct
*tty
, struct file
* filp
)
646 struct sdio_uart_port
*port
;
649 port
= sdio_uart_port_get(tty
->index
);
653 mutex_lock(&port
->open_lock
);
656 * Make sure not to mess up with a dead port
657 * which has not been closed yet.
659 if (tty
->driver_data
&& tty
->driver_data
!= port
) {
660 mutex_unlock(&port
->open_lock
);
661 sdio_uart_port_put(port
);
666 tty
->driver_data
= port
;
668 ret
= sdio_uart_startup(port
);
670 tty
->driver_data
= NULL
;
672 mutex_unlock(&port
->open_lock
);
673 sdio_uart_port_put(port
);
678 mutex_unlock(&port
->open_lock
);
682 static void sdio_uart_close(struct tty_struct
*tty
, struct file
* filp
)
684 struct sdio_uart_port
*port
= tty
->driver_data
;
689 mutex_lock(&port
->open_lock
);
690 BUG_ON(!port
->opened
);
693 * This is messy. The tty layer calls us even when open()
694 * returned an error. Ignore this close request if tty->count
695 * is larger than port->count.
697 if (tty
->count
> port
->opened
) {
698 mutex_unlock(&port
->open_lock
);
702 if (--port
->opened
== 0) {
704 sdio_uart_shutdown(port
);
705 tty_ldisc_flush(tty
);
707 tty
->driver_data
= NULL
;
710 mutex_unlock(&port
->open_lock
);
711 sdio_uart_port_put(port
);
714 static int sdio_uart_write(struct tty_struct
* tty
, const unsigned char *buf
,
717 struct sdio_uart_port
*port
= tty
->driver_data
;
718 struct circ_buf
*circ
= &port
->xmit
;
724 spin_lock(&port
->write_lock
);
726 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
731 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
732 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
737 spin_unlock(&port
->write_lock
);
739 if ( !(port
->ier
& UART_IER_THRI
)) {
740 int err
= sdio_uart_claim_func(port
);
742 sdio_uart_start_tx(port
);
743 sdio_uart_irq(port
->func
);
744 sdio_uart_release_func(port
);
752 static int sdio_uart_write_room(struct tty_struct
*tty
)
754 struct sdio_uart_port
*port
= tty
->driver_data
;
755 return port
? circ_chars_free(&port
->xmit
) : 0;
758 static int sdio_uart_chars_in_buffer(struct tty_struct
*tty
)
760 struct sdio_uart_port
*port
= tty
->driver_data
;
761 return port
? circ_chars_pending(&port
->xmit
) : 0;
764 static void sdio_uart_send_xchar(struct tty_struct
*tty
, char ch
)
766 struct sdio_uart_port
*port
= tty
->driver_data
;
769 if (ch
&& !(port
->ier
& UART_IER_THRI
)) {
770 if (sdio_uart_claim_func(port
) != 0)
772 sdio_uart_start_tx(port
);
773 sdio_uart_irq(port
->func
);
774 sdio_uart_release_func(port
);
778 static void sdio_uart_throttle(struct tty_struct
*tty
)
780 struct sdio_uart_port
*port
= tty
->driver_data
;
782 if (!I_IXOFF(tty
) && !(tty
->termios
->c_cflag
& CRTSCTS
))
785 if (sdio_uart_claim_func(port
) != 0)
789 port
->x_char
= STOP_CHAR(tty
);
790 sdio_uart_start_tx(port
);
793 if (tty
->termios
->c_cflag
& CRTSCTS
)
794 sdio_uart_clear_mctrl(port
, TIOCM_RTS
);
796 sdio_uart_irq(port
->func
);
797 sdio_uart_release_func(port
);
800 static void sdio_uart_unthrottle(struct tty_struct
*tty
)
802 struct sdio_uart_port
*port
= tty
->driver_data
;
804 if (!I_IXOFF(tty
) && !(tty
->termios
->c_cflag
& CRTSCTS
))
807 if (sdio_uart_claim_func(port
) != 0)
814 port
->x_char
= START_CHAR(tty
);
815 sdio_uart_start_tx(port
);
819 if (tty
->termios
->c_cflag
& CRTSCTS
)
820 sdio_uart_set_mctrl(port
, TIOCM_RTS
);
822 sdio_uart_irq(port
->func
);
823 sdio_uart_release_func(port
);
826 static void sdio_uart_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
828 struct sdio_uart_port
*port
= tty
->driver_data
;
829 unsigned int cflag
= tty
->termios
->c_cflag
;
831 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
833 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
834 RELEVANT_IFLAG(tty
->termios
->c_iflag
^ old_termios
->c_iflag
) == 0)
837 if (sdio_uart_claim_func(port
) != 0)
840 sdio_uart_change_speed(port
, tty
->termios
, old_termios
);
842 /* Handle transition to B0 status */
843 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
844 sdio_uart_clear_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
846 /* Handle transition away from B0 status */
847 if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
848 unsigned int mask
= TIOCM_DTR
;
849 if (!(cflag
& CRTSCTS
) || !test_bit(TTY_THROTTLED
, &tty
->flags
))
851 sdio_uart_set_mctrl(port
, mask
);
854 /* Handle turning off CRTSCTS */
855 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
857 sdio_uart_start_tx(port
);
860 /* Handle turning on CRTSCTS */
861 if (!(old_termios
->c_cflag
& CRTSCTS
) && (cflag
& CRTSCTS
)) {
862 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
)) {
864 sdio_uart_stop_tx(port
);
868 sdio_uart_release_func(port
);
871 static void sdio_uart_break_ctl(struct tty_struct
*tty
, int break_state
)
873 struct sdio_uart_port
*port
= tty
->driver_data
;
875 if (sdio_uart_claim_func(port
) != 0)
878 if (break_state
== -1)
879 port
->lcr
|= UART_LCR_SBC
;
881 port
->lcr
&= ~UART_LCR_SBC
;
882 sdio_out(port
, UART_LCR
, port
->lcr
);
884 sdio_uart_release_func(port
);
887 static int sdio_uart_tiocmget(struct tty_struct
*tty
, struct file
*file
)
889 struct sdio_uart_port
*port
= tty
->driver_data
;
892 result
= sdio_uart_claim_func(port
);
894 result
= port
->mctrl
| sdio_uart_get_mctrl(port
);
895 sdio_uart_release_func(port
);
901 static int sdio_uart_tiocmset(struct tty_struct
*tty
, struct file
*file
,
902 unsigned int set
, unsigned int clear
)
904 struct sdio_uart_port
*port
= tty
->driver_data
;
907 result
=sdio_uart_claim_func(port
);
909 sdio_uart_update_mctrl(port
, set
, clear
);
910 sdio_uart_release_func(port
);
916 static int sdio_uart_read_proc(char *page
, char **start
, off_t off
,
917 int count
, int *eof
, void *data
)
922 len
+= sprintf(page
, "serinfo:1.0 driver%s%s revision:%s\n",
924 for (i
= 0; i
< UART_NR
&& len
< PAGE_SIZE
- 96; i
++) {
925 struct sdio_uart_port
*port
= sdio_uart_port_get(i
);
927 len
+= sprintf(page
+len
, "%d: uart:SDIO", i
);
928 if(capable(CAP_SYS_ADMIN
)) {
929 len
+= sprintf(page
+ len
, " tx:%d rx:%d",
930 port
->icount
.tx
, port
->icount
.rx
);
931 if (port
->icount
.frame
)
932 len
+= sprintf(page
+ len
, " fe:%d",
934 if (port
->icount
.parity
)
935 len
+= sprintf(page
+ len
, " pe:%d",
936 port
->icount
.parity
);
937 if (port
->icount
.brk
)
938 len
+= sprintf(page
+ len
, " brk:%d",
940 if (port
->icount
.overrun
)
941 len
+= sprintf(page
+ len
, " oe:%d",
942 port
->icount
.overrun
);
943 if (port
->icount
.cts
)
944 len
+= sprintf(page
+ len
, " cts:%d",
946 if (port
->icount
.dsr
)
947 len
+= sprintf(page
+ len
, " dsr:%d",
949 if (port
->icount
.rng
)
950 len
+= sprintf(page
+ len
, " rng:%d",
952 if (port
->icount
.dcd
)
953 len
+= sprintf(page
+ len
, " dcd:%d",
958 sdio_uart_port_put(port
);
961 if (len
+ begin
> off
+ count
)
963 if (len
+ begin
< off
) {
971 if (off
>= len
+ begin
)
973 *start
= page
+ (off
- begin
);
974 return (count
< begin
+ len
- off
) ? count
: (begin
+ len
- off
);
977 static const struct tty_operations sdio_uart_ops
= {
978 .open
= sdio_uart_open
,
979 .close
= sdio_uart_close
,
980 .write
= sdio_uart_write
,
981 .write_room
= sdio_uart_write_room
,
982 .chars_in_buffer
= sdio_uart_chars_in_buffer
,
983 .send_xchar
= sdio_uart_send_xchar
,
984 .throttle
= sdio_uart_throttle
,
985 .unthrottle
= sdio_uart_unthrottle
,
986 .set_termios
= sdio_uart_set_termios
,
987 .break_ctl
= sdio_uart_break_ctl
,
988 .tiocmget
= sdio_uart_tiocmget
,
989 .tiocmset
= sdio_uart_tiocmset
,
990 .read_proc
= sdio_uart_read_proc
,
993 static struct tty_driver
*sdio_uart_tty_driver
;
995 static int sdio_uart_probe(struct sdio_func
*func
,
996 const struct sdio_device_id
*id
)
998 struct sdio_uart_port
*port
;
1001 port
= kzalloc(sizeof(struct sdio_uart_port
), GFP_KERNEL
);
1005 if (func
->class == SDIO_CLASS_UART
) {
1006 printk(KERN_WARNING
"%s: need info on UART class basic setup\n",
1007 sdio_func_id(func
));
1010 } else if (func
->class == SDIO_CLASS_GPS
) {
1012 * We need tuple 0x91. It contains SUBTPL_SIOREG
1013 * and SUBTPL_RCVCAPS.
1015 struct sdio_func_tuple
*tpl
;
1016 for (tpl
= func
->tuples
; tpl
; tpl
= tpl
->next
) {
1017 if (tpl
->code
!= 0x91)
1021 if (tpl
->data
[1] == 0) /* SUBTPL_SIOREG */
1026 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1027 sdio_func_id(func
));
1031 printk(KERN_DEBUG
"%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1032 sdio_func_id(func
), tpl
->data
[2], tpl
->data
[3]);
1033 port
->regs_offset
= (tpl
->data
[4] << 0) |
1034 (tpl
->data
[5] << 8) |
1035 (tpl
->data
[6] << 16);
1036 printk(KERN_DEBUG
"%s: regs offset = 0x%x\n",
1037 sdio_func_id(func
), port
->regs_offset
);
1038 port
->uartclk
= tpl
->data
[7] * 115200;
1039 if (port
->uartclk
== 0)
1040 port
->uartclk
= 115200;
1041 printk(KERN_DEBUG
"%s: clk %d baudcode %u 4800-div %u\n",
1042 sdio_func_id(func
), port
->uartclk
,
1043 tpl
->data
[7], tpl
->data
[8] | (tpl
->data
[9] << 8));
1050 sdio_set_drvdata(func
, port
);
1052 ret
= sdio_uart_add_port(port
);
1057 dev
= tty_register_device(sdio_uart_tty_driver
, port
->index
, &func
->dev
);
1059 sdio_uart_port_remove(port
);
1067 static void sdio_uart_remove(struct sdio_func
*func
)
1069 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
1071 tty_unregister_device(sdio_uart_tty_driver
, port
->index
);
1072 sdio_uart_port_remove(port
);
1075 static const struct sdio_device_id sdio_uart_ids
[] = {
1076 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART
) },
1077 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS
) },
1078 { /* end: all zeroes */ },
1081 MODULE_DEVICE_TABLE(sdio
, sdio_uart_ids
);
1083 static struct sdio_driver sdio_uart_driver
= {
1084 .probe
= sdio_uart_probe
,
1085 .remove
= sdio_uart_remove
,
1086 .name
= "sdio_uart",
1087 .id_table
= sdio_uart_ids
,
1090 static int __init
sdio_uart_init(void)
1093 struct tty_driver
*tty_drv
;
1095 sdio_uart_tty_driver
= tty_drv
= alloc_tty_driver(UART_NR
);
1099 tty_drv
->owner
= THIS_MODULE
;
1100 tty_drv
->driver_name
= "sdio_uart";
1101 tty_drv
->name
= "ttySDIO";
1102 tty_drv
->major
= 0; /* dynamically allocated */
1103 tty_drv
->minor_start
= 0;
1104 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1105 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1106 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1107 tty_drv
->init_termios
= tty_std_termios
;
1108 tty_drv
->init_termios
.c_cflag
= B4800
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1109 tty_set_operations(tty_drv
, &sdio_uart_ops
);
1111 ret
= tty_register_driver(tty_drv
);
1115 ret
= sdio_register_driver(&sdio_uart_driver
);
1122 tty_unregister_driver(tty_drv
);
1124 put_tty_driver(tty_drv
);
1128 static void __exit
sdio_uart_exit(void)
1130 sdio_unregister_driver(&sdio_uart_driver
);
1131 tty_unregister_driver(sdio_uart_tty_driver
);
1132 put_tty_driver(sdio_uart_tty_driver
);
1135 module_init(sdio_uart_init
);
1136 module_exit(sdio_uart_exit
);
1138 MODULE_AUTHOR("Nicolas Pitre");
1139 MODULE_LICENSE("GPL");