2 * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
4 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
7 * Author: Nicolas Pitre
8 * Created: June 15, 2007
9 * Copyright: MontaVista Software, Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
18 * Note: Although this driver assumes a 16550A-like UART implementation,
19 * it is not possible to leverage the common 8250/16550 driver, nor the
20 * core UART infrastructure, as they assumes direct access to the hardware
21 * registers, often under a spinlock. This is not possible in the SDIO
22 * context as SDIO access functions must be able to sleep.
24 * Because we need to lock the SDIO host to ensure an exclusive access to
25 * the card, we simply rely on that lock to also prevent and serialize
26 * concurrent access to the same port.
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/mutex.h>
34 #include <linux/seq_file.h>
35 #include <linux/serial_reg.h>
36 #include <linux/circ_buf.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39 #include <linux/kfifo.h>
40 #include <linux/slab.h>
42 #include <linux/mmc/core.h>
43 #include <linux/mmc/card.h>
44 #include <linux/mmc/sdio_func.h>
45 #include <linux/mmc/sdio_ids.h>
48 #define UART_NR 8 /* Number of UARTs this driver can handle */
51 #define FIFO_SIZE PAGE_SIZE
52 #define WAKEUP_CHARS 256
67 struct sdio_uart_port
{
70 struct tty_struct
*tty
;
72 struct sdio_func
*func
;
73 struct mutex func_lock
;
74 struct task_struct
*in_sdio_uart_irq
;
75 unsigned int regs_offset
;
76 struct kfifo xmit_fifo
;
77 spinlock_t write_lock
;
78 struct uart_icount icount
;
81 unsigned int rx_mctrl
;
82 unsigned int read_status_mask
;
83 unsigned int ignore_status_mask
;
89 static struct sdio_uart_port
*sdio_uart_table
[UART_NR
];
90 static DEFINE_SPINLOCK(sdio_uart_table_lock
);
92 static int sdio_uart_add_port(struct sdio_uart_port
*port
)
94 int index
, ret
= -EBUSY
;
96 kref_init(&port
->kref
);
97 mutex_init(&port
->func_lock
);
98 spin_lock_init(&port
->write_lock
);
99 if (kfifo_alloc(&port
->xmit_fifo
, FIFO_SIZE
, GFP_KERNEL
))
102 spin_lock(&sdio_uart_table_lock
);
103 for (index
= 0; index
< UART_NR
; index
++) {
104 if (!sdio_uart_table
[index
]) {
106 sdio_uart_table
[index
] = port
;
111 spin_unlock(&sdio_uart_table_lock
);
116 static struct sdio_uart_port
*sdio_uart_port_get(unsigned index
)
118 struct sdio_uart_port
*port
;
120 if (index
>= UART_NR
)
123 spin_lock(&sdio_uart_table_lock
);
124 port
= sdio_uart_table
[index
];
126 kref_get(&port
->kref
);
127 spin_unlock(&sdio_uart_table_lock
);
132 static void sdio_uart_port_destroy(struct kref
*kref
)
134 struct sdio_uart_port
*port
=
135 container_of(kref
, struct sdio_uart_port
, kref
);
136 kfifo_free(&port
->xmit_fifo
);
140 static void sdio_uart_port_put(struct sdio_uart_port
*port
)
142 kref_put(&port
->kref
, sdio_uart_port_destroy
);
145 static void sdio_uart_port_remove(struct sdio_uart_port
*port
)
147 struct sdio_func
*func
;
148 struct tty_struct
*tty
;
150 BUG_ON(sdio_uart_table
[port
->index
] != port
);
152 spin_lock(&sdio_uart_table_lock
);
153 sdio_uart_table
[port
->index
] = NULL
;
154 spin_unlock(&sdio_uart_table_lock
);
157 * We're killing a port that potentially still is in use by
158 * the tty layer. Be careful to prevent any further access
159 * to the SDIO function and arrange for the tty layer to
160 * give up on that port ASAP.
161 * Beware: the lock ordering is critical.
163 mutex_lock(&port
->port
.mutex
);
164 mutex_lock(&port
->func_lock
);
166 sdio_claim_host(func
);
168 mutex_unlock(&port
->func_lock
);
169 tty
= tty_port_tty_get(&port
->port
);
170 /* tty_hangup is async so is this safe as is ?? */
175 mutex_unlock(&port
->port
.mutex
);
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 /* FIXME: What stops this losing the delta bits and breaking
220 sdio_uart_check_modem_status ? */
221 status
= sdio_in(port
, UART_MSR
);
224 if (status
& UART_MSR_DCD
)
226 if (status
& UART_MSR_RI
)
228 if (status
& UART_MSR_DSR
)
230 if (status
& UART_MSR_CTS
)
235 static void sdio_uart_write_mctrl(struct sdio_uart_port
*port
,
238 unsigned char mcr
= 0;
240 if (mctrl
& TIOCM_RTS
)
242 if (mctrl
& TIOCM_DTR
)
244 if (mctrl
& TIOCM_OUT1
)
245 mcr
|= UART_MCR_OUT1
;
246 if (mctrl
& TIOCM_OUT2
)
247 mcr
|= UART_MCR_OUT2
;
248 if (mctrl
& TIOCM_LOOP
)
249 mcr
|= UART_MCR_LOOP
;
251 sdio_out(port
, UART_MCR
, mcr
);
254 static inline void sdio_uart_update_mctrl(struct sdio_uart_port
*port
,
255 unsigned int set
, unsigned int clear
)
260 port
->mctrl
= (old
& ~clear
) | set
;
261 if (old
!= port
->mctrl
)
262 sdio_uart_write_mctrl(port
, port
->mctrl
);
265 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
266 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
268 static void sdio_uart_change_speed(struct sdio_uart_port
*port
,
269 struct ktermios
*termios
,
270 struct ktermios
*old
)
272 unsigned char cval
, fcr
= 0;
273 unsigned int baud
, quot
;
275 switch (termios
->c_cflag
& CSIZE
) {
277 cval
= UART_LCR_WLEN5
;
280 cval
= UART_LCR_WLEN6
;
283 cval
= UART_LCR_WLEN7
;
287 cval
= UART_LCR_WLEN8
;
291 if (termios
->c_cflag
& CSTOPB
)
292 cval
|= UART_LCR_STOP
;
293 if (termios
->c_cflag
& PARENB
)
294 cval
|= UART_LCR_PARITY
;
295 if (!(termios
->c_cflag
& PARODD
))
296 cval
|= UART_LCR_EPAR
;
299 baud
= tty_termios_baud_rate(termios
);
301 baud
= 9600; /* Special case: B0 rate. */
302 if (baud
<= port
->uartclk
)
305 * Oops, the quotient was zero. Try again with the old
306 * baud rate if possible, otherwise default to 9600.
308 termios
->c_cflag
&= ~CBAUD
;
310 termios
->c_cflag
|= old
->c_cflag
& CBAUD
;
313 termios
->c_cflag
|= B9600
;
315 quot
= (2 * port
->uartclk
+ baud
) / (2 * baud
);
318 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_1
;
320 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_R_TRIG_10
;
322 port
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
323 if (termios
->c_iflag
& INPCK
)
324 port
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
325 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
326 port
->read_status_mask
|= UART_LSR_BI
;
329 * Characters to ignore
331 port
->ignore_status_mask
= 0;
332 if (termios
->c_iflag
& IGNPAR
)
333 port
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
334 if (termios
->c_iflag
& IGNBRK
) {
335 port
->ignore_status_mask
|= UART_LSR_BI
;
337 * If we're ignoring parity and break indicators,
338 * ignore overruns too (for real raw support).
340 if (termios
->c_iflag
& IGNPAR
)
341 port
->ignore_status_mask
|= UART_LSR_OE
;
345 * ignore all characters if CREAD is not set
347 if ((termios
->c_cflag
& CREAD
) == 0)
348 port
->ignore_status_mask
|= UART_LSR_DR
;
351 * CTS flow control flag and modem status interrupts
353 port
->ier
&= ~UART_IER_MSI
;
354 if ((termios
->c_cflag
& CRTSCTS
) || !(termios
->c_cflag
& CLOCAL
))
355 port
->ier
|= UART_IER_MSI
;
359 sdio_out(port
, UART_IER
, port
->ier
);
360 sdio_out(port
, UART_LCR
, cval
| UART_LCR_DLAB
);
361 sdio_out(port
, UART_DLL
, quot
& 0xff);
362 sdio_out(port
, UART_DLM
, quot
>> 8);
363 sdio_out(port
, UART_LCR
, cval
);
364 sdio_out(port
, UART_FCR
, fcr
);
366 sdio_uart_write_mctrl(port
, port
->mctrl
);
369 static void sdio_uart_start_tx(struct sdio_uart_port
*port
)
371 if (!(port
->ier
& UART_IER_THRI
)) {
372 port
->ier
|= UART_IER_THRI
;
373 sdio_out(port
, UART_IER
, port
->ier
);
377 static void sdio_uart_stop_tx(struct sdio_uart_port
*port
)
379 if (port
->ier
& UART_IER_THRI
) {
380 port
->ier
&= ~UART_IER_THRI
;
381 sdio_out(port
, UART_IER
, port
->ier
);
385 static void sdio_uart_stop_rx(struct sdio_uart_port
*port
)
387 port
->ier
&= ~UART_IER_RLSI
;
388 port
->read_status_mask
&= ~UART_LSR_DR
;
389 sdio_out(port
, UART_IER
, port
->ier
);
392 static void sdio_uart_receive_chars(struct sdio_uart_port
*port
,
393 unsigned int *status
)
395 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
396 unsigned int ch
, flag
;
400 ch
= sdio_in(port
, UART_RX
);
404 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
405 UART_LSR_FE
| UART_LSR_OE
))) {
407 * For statistics only
409 if (*status
& UART_LSR_BI
) {
410 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
412 } else if (*status
& UART_LSR_PE
)
413 port
->icount
.parity
++;
414 else if (*status
& UART_LSR_FE
)
415 port
->icount
.frame
++;
416 if (*status
& UART_LSR_OE
)
417 port
->icount
.overrun
++;
420 * Mask off conditions which should be ignored.
422 *status
&= port
->read_status_mask
;
423 if (*status
& UART_LSR_BI
)
425 else if (*status
& UART_LSR_PE
)
427 else if (*status
& UART_LSR_FE
)
431 if ((*status
& port
->ignore_status_mask
& ~UART_LSR_OE
) == 0)
433 tty_insert_flip_char(tty
, ch
, flag
);
436 * Overrun is special. Since it's reported immediately,
437 * it doesn't affect the current character.
439 if (*status
& ~port
->ignore_status_mask
& UART_LSR_OE
)
441 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
443 *status
= sdio_in(port
, UART_LSR
);
444 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
446 tty_flip_buffer_push(tty
);
451 static void sdio_uart_transmit_chars(struct sdio_uart_port
*port
)
453 struct kfifo
*xmit
= &port
->xmit_fifo
;
455 struct tty_struct
*tty
;
460 sdio_out(port
, UART_TX
, port
->x_char
);
466 tty
= tty_port_tty_get(&port
->port
);
468 if (tty
== NULL
|| !kfifo_len(xmit
) ||
469 tty
->stopped
|| tty
->hw_stopped
) {
470 sdio_uart_stop_tx(port
);
475 len
= kfifo_out_locked(xmit
, iobuf
, 16, &port
->write_lock
);
476 for (count
= 0; count
< len
; count
++) {
477 sdio_out(port
, UART_TX
, iobuf
[count
]);
481 len
= kfifo_len(xmit
);
482 if (len
< WAKEUP_CHARS
) {
485 sdio_uart_stop_tx(port
);
490 static void sdio_uart_check_modem_status(struct sdio_uart_port
*port
)
493 struct tty_struct
*tty
;
495 status
= sdio_in(port
, UART_MSR
);
497 if ((status
& UART_MSR_ANY_DELTA
) == 0)
500 if (status
& UART_MSR_TERI
)
502 if (status
& UART_MSR_DDSR
)
504 if (status
& UART_MSR_DDCD
) {
506 /* DCD raise - wake for open */
507 if (status
& UART_MSR_DCD
)
508 wake_up_interruptible(&port
->port
.open_wait
);
510 /* DCD drop - hang up if tty attached */
511 tty
= tty_port_tty_get(&port
->port
);
518 if (status
& UART_MSR_DCTS
) {
520 tty
= tty_port_tty_get(&port
->port
);
521 if (tty
&& (tty
->termios
->c_cflag
& CRTSCTS
)) {
522 int cts
= (status
& UART_MSR_CTS
);
523 if (tty
->hw_stopped
) {
526 sdio_uart_start_tx(port
);
532 sdio_uart_stop_tx(port
);
541 * This handles the interrupt from one port.
543 static void sdio_uart_irq(struct sdio_func
*func
)
545 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
546 unsigned int iir
, lsr
;
549 * In a few places sdio_uart_irq() is called directly instead of
550 * waiting for the actual interrupt to be raised and the SDIO IRQ
551 * thread scheduled in order to reduce latency. However, some
552 * interaction with the tty core may end up calling us back
553 * (serial echo, flow control, etc.) through those same places
554 * causing undesirable effects. Let's stop the recursion here.
556 if (unlikely(port
->in_sdio_uart_irq
== current
))
559 iir
= sdio_in(port
, UART_IIR
);
560 if (iir
& UART_IIR_NO_INT
)
563 port
->in_sdio_uart_irq
= current
;
564 lsr
= sdio_in(port
, UART_LSR
);
565 if (lsr
& UART_LSR_DR
)
566 sdio_uart_receive_chars(port
, &lsr
);
567 sdio_uart_check_modem_status(port
);
568 if (lsr
& UART_LSR_THRE
)
569 sdio_uart_transmit_chars(port
);
570 port
->in_sdio_uart_irq
= NULL
;
573 static int uart_carrier_raised(struct tty_port
*tport
)
575 struct sdio_uart_port
*port
=
576 container_of(tport
, struct sdio_uart_port
, port
);
577 unsigned int ret
= sdio_uart_claim_func(port
);
578 if (ret
) /* Missing hardware shouldn't block for carrier */
580 ret
= sdio_uart_get_mctrl(port
);
581 sdio_uart_release_func(port
);
588 * uart_dtr_rts - port helper to set uart signals
589 * @tport: tty port to be updated
590 * @onoff: set to turn on DTR/RTS
592 * Called by the tty port helpers when the modem signals need to be
593 * adjusted during an open, close and hangup.
596 static void uart_dtr_rts(struct tty_port
*tport
, int onoff
)
598 struct sdio_uart_port
*port
=
599 container_of(tport
, struct sdio_uart_port
, port
);
600 int ret
= sdio_uart_claim_func(port
);
604 sdio_uart_clear_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
606 sdio_uart_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
607 sdio_uart_release_func(port
);
611 * sdio_uart_activate - start up hardware
612 * @tport: tty port to activate
613 * @tty: tty bound to this port
615 * Activate a tty port. The port locking guarantees us this will be
616 * run exactly once per set of opens, and if successful will see the
617 * shutdown method run exactly once to match. Start up and shutdown are
618 * protected from each other by the internal locking and will not run
619 * at the same time even during a hangup event.
621 * If we successfully start up the port we take an extra kref as we
622 * will keep it around until shutdown when the kref is dropped.
625 static int sdio_uart_activate(struct tty_port
*tport
, struct tty_struct
*tty
)
627 struct sdio_uart_port
*port
=
628 container_of(tport
, struct sdio_uart_port
, port
);
632 * Set the TTY IO error marker - we will only clear this
633 * once we have successfully opened the port.
635 set_bit(TTY_IO_ERROR
, &tty
->flags
);
637 kfifo_reset(&port
->xmit_fifo
);
639 ret
= sdio_uart_claim_func(port
);
642 ret
= sdio_enable_func(port
->func
);
645 ret
= sdio_claim_irq(port
->func
, sdio_uart_irq
);
650 * Clear the FIFO buffers and disable them.
651 * (they will be reenabled in sdio_change_speed())
653 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
654 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
655 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
656 sdio_out(port
, UART_FCR
, 0);
659 * Clear the interrupt registers.
661 (void) sdio_in(port
, UART_LSR
);
662 (void) sdio_in(port
, UART_RX
);
663 (void) sdio_in(port
, UART_IIR
);
664 (void) sdio_in(port
, UART_MSR
);
667 * Now, initialize the UART
669 sdio_out(port
, UART_LCR
, UART_LCR_WLEN8
);
671 port
->ier
= UART_IER_RLSI
|UART_IER_RDI
|UART_IER_RTOIE
|UART_IER_UUE
;
672 port
->mctrl
= TIOCM_OUT2
;
674 sdio_uart_change_speed(port
, tty
->termios
, NULL
);
676 if (tty
->termios
->c_cflag
& CBAUD
)
677 sdio_uart_set_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
679 if (tty
->termios
->c_cflag
& CRTSCTS
)
680 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
))
683 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
685 /* Kick the IRQ handler once while we're still holding the host lock */
686 sdio_uart_irq(port
->func
);
688 sdio_uart_release_func(port
);
692 sdio_disable_func(port
->func
);
694 sdio_uart_release_func(port
);
699 * sdio_uart_shutdown - stop hardware
700 * @tport: tty port to shut down
702 * Deactivate a tty port. The port locking guarantees us this will be
703 * run only if a successful matching activate already ran. The two are
704 * protected from each other by the internal locking and will not run
705 * at the same time even during a hangup event.
708 static void sdio_uart_shutdown(struct tty_port
*tport
)
710 struct sdio_uart_port
*port
=
711 container_of(tport
, struct sdio_uart_port
, port
);
714 ret
= sdio_uart_claim_func(port
);
718 sdio_uart_stop_rx(port
);
720 /* Disable interrupts from this port */
721 sdio_release_irq(port
->func
);
723 sdio_out(port
, UART_IER
, 0);
725 sdio_uart_clear_mctrl(port
, TIOCM_OUT2
);
727 /* Disable break condition and FIFOs. */
728 port
->lcr
&= ~UART_LCR_SBC
;
729 sdio_out(port
, UART_LCR
, port
->lcr
);
730 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
731 UART_FCR_CLEAR_RCVR
|
732 UART_FCR_CLEAR_XMIT
);
733 sdio_out(port
, UART_FCR
, 0);
735 sdio_disable_func(port
->func
);
737 sdio_uart_release_func(port
);
741 * sdio_uart_install - install method
742 * @driver: the driver in use (sdio_uart in our case)
743 * @tty: the tty being bound
745 * Look up and bind the tty and the driver together. Initialize
746 * any needed private data (in our case the termios)
749 static int sdio_uart_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
751 int idx
= tty
->index
;
752 struct sdio_uart_port
*port
= sdio_uart_port_get(idx
);
753 int ret
= tty_init_termios(tty
);
756 tty_driver_kref_get(driver
);
758 /* This is the ref sdio_uart_port get provided */
759 tty
->driver_data
= port
;
760 driver
->ttys
[idx
] = tty
;
762 sdio_uart_port_put(port
);
767 * sdio_uart_cleanup - called on the last tty kref drop
768 * @tty: the tty being destroyed
770 * Called asynchronously when the last reference to the tty is dropped.
771 * We cannot destroy the tty->driver_data port kref until this point
774 static void sdio_uart_cleanup(struct tty_struct
*tty
)
776 struct sdio_uart_port
*port
= tty
->driver_data
;
777 tty
->driver_data
= NULL
; /* Bug trap */
778 sdio_uart_port_put(port
);
782 * Open/close/hangup is now entirely boilerplate
785 static int sdio_uart_open(struct tty_struct
*tty
, struct file
*filp
)
787 struct sdio_uart_port
*port
= tty
->driver_data
;
788 return tty_port_open(&port
->port
, tty
, filp
);
791 static void sdio_uart_close(struct tty_struct
*tty
, struct file
* filp
)
793 struct sdio_uart_port
*port
= tty
->driver_data
;
794 tty_port_close(&port
->port
, tty
, filp
);
797 static void sdio_uart_hangup(struct tty_struct
*tty
)
799 struct sdio_uart_port
*port
= tty
->driver_data
;
800 tty_port_hangup(&port
->port
);
803 static int sdio_uart_write(struct tty_struct
*tty
, const unsigned char *buf
,
806 struct sdio_uart_port
*port
= tty
->driver_data
;
812 ret
= kfifo_in_locked(&port
->xmit_fifo
, buf
, count
, &port
->write_lock
);
813 if (!(port
->ier
& UART_IER_THRI
)) {
814 int err
= sdio_uart_claim_func(port
);
816 sdio_uart_start_tx(port
);
817 sdio_uart_irq(port
->func
);
818 sdio_uart_release_func(port
);
826 static int sdio_uart_write_room(struct tty_struct
*tty
)
828 struct sdio_uart_port
*port
= tty
->driver_data
;
829 return FIFO_SIZE
- kfifo_len(&port
->xmit_fifo
);
832 static int sdio_uart_chars_in_buffer(struct tty_struct
*tty
)
834 struct sdio_uart_port
*port
= tty
->driver_data
;
835 return kfifo_len(&port
->xmit_fifo
);
838 static void sdio_uart_send_xchar(struct tty_struct
*tty
, char ch
)
840 struct sdio_uart_port
*port
= tty
->driver_data
;
843 if (ch
&& !(port
->ier
& UART_IER_THRI
)) {
844 if (sdio_uart_claim_func(port
) != 0)
846 sdio_uart_start_tx(port
);
847 sdio_uart_irq(port
->func
);
848 sdio_uart_release_func(port
);
852 static void sdio_uart_throttle(struct tty_struct
*tty
)
854 struct sdio_uart_port
*port
= tty
->driver_data
;
856 if (!I_IXOFF(tty
) && !(tty
->termios
->c_cflag
& CRTSCTS
))
859 if (sdio_uart_claim_func(port
) != 0)
863 port
->x_char
= STOP_CHAR(tty
);
864 sdio_uart_start_tx(port
);
867 if (tty
->termios
->c_cflag
& CRTSCTS
)
868 sdio_uart_clear_mctrl(port
, TIOCM_RTS
);
870 sdio_uart_irq(port
->func
);
871 sdio_uart_release_func(port
);
874 static void sdio_uart_unthrottle(struct tty_struct
*tty
)
876 struct sdio_uart_port
*port
= tty
->driver_data
;
878 if (!I_IXOFF(tty
) && !(tty
->termios
->c_cflag
& CRTSCTS
))
881 if (sdio_uart_claim_func(port
) != 0)
888 port
->x_char
= START_CHAR(tty
);
889 sdio_uart_start_tx(port
);
893 if (tty
->termios
->c_cflag
& CRTSCTS
)
894 sdio_uart_set_mctrl(port
, TIOCM_RTS
);
896 sdio_uart_irq(port
->func
);
897 sdio_uart_release_func(port
);
900 static void sdio_uart_set_termios(struct tty_struct
*tty
,
901 struct ktermios
*old_termios
)
903 struct sdio_uart_port
*port
= tty
->driver_data
;
904 unsigned int cflag
= tty
->termios
->c_cflag
;
906 if (sdio_uart_claim_func(port
) != 0)
909 sdio_uart_change_speed(port
, tty
->termios
, old_termios
);
911 /* Handle transition to B0 status */
912 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
913 sdio_uart_clear_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
915 /* Handle transition away from B0 status */
916 if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
917 unsigned int mask
= TIOCM_DTR
;
918 if (!(cflag
& CRTSCTS
) || !test_bit(TTY_THROTTLED
, &tty
->flags
))
920 sdio_uart_set_mctrl(port
, mask
);
923 /* Handle turning off CRTSCTS */
924 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
926 sdio_uart_start_tx(port
);
929 /* Handle turning on CRTSCTS */
930 if (!(old_termios
->c_cflag
& CRTSCTS
) && (cflag
& CRTSCTS
)) {
931 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
)) {
933 sdio_uart_stop_tx(port
);
937 sdio_uart_release_func(port
);
940 static int sdio_uart_break_ctl(struct tty_struct
*tty
, int break_state
)
942 struct sdio_uart_port
*port
= tty
->driver_data
;
945 result
= sdio_uart_claim_func(port
);
949 if (break_state
== -1)
950 port
->lcr
|= UART_LCR_SBC
;
952 port
->lcr
&= ~UART_LCR_SBC
;
953 sdio_out(port
, UART_LCR
, port
->lcr
);
955 sdio_uart_release_func(port
);
959 static int sdio_uart_tiocmget(struct tty_struct
*tty
, struct file
*file
)
961 struct sdio_uart_port
*port
= tty
->driver_data
;
964 result
= sdio_uart_claim_func(port
);
966 result
= port
->mctrl
| sdio_uart_get_mctrl(port
);
967 sdio_uart_release_func(port
);
973 static int sdio_uart_tiocmset(struct tty_struct
*tty
, struct file
*file
,
974 unsigned int set
, unsigned int clear
)
976 struct sdio_uart_port
*port
= tty
->driver_data
;
979 result
= sdio_uart_claim_func(port
);
981 sdio_uart_update_mctrl(port
, set
, clear
);
982 sdio_uart_release_func(port
);
988 static int sdio_uart_proc_show(struct seq_file
*m
, void *v
)
992 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
994 for (i
= 0; i
< UART_NR
; i
++) {
995 struct sdio_uart_port
*port
= sdio_uart_port_get(i
);
997 seq_printf(m
, "%d: uart:SDIO", i
);
998 if (capable(CAP_SYS_ADMIN
)) {
999 seq_printf(m
, " tx:%d rx:%d",
1000 port
->icount
.tx
, port
->icount
.rx
);
1001 if (port
->icount
.frame
)
1002 seq_printf(m
, " fe:%d",
1003 port
->icount
.frame
);
1004 if (port
->icount
.parity
)
1005 seq_printf(m
, " pe:%d",
1006 port
->icount
.parity
);
1007 if (port
->icount
.brk
)
1008 seq_printf(m
, " brk:%d",
1010 if (port
->icount
.overrun
)
1011 seq_printf(m
, " oe:%d",
1012 port
->icount
.overrun
);
1013 if (port
->icount
.cts
)
1014 seq_printf(m
, " cts:%d",
1016 if (port
->icount
.dsr
)
1017 seq_printf(m
, " dsr:%d",
1019 if (port
->icount
.rng
)
1020 seq_printf(m
, " rng:%d",
1022 if (port
->icount
.dcd
)
1023 seq_printf(m
, " dcd:%d",
1026 sdio_uart_port_put(port
);
1033 static int sdio_uart_proc_open(struct inode
*inode
, struct file
*file
)
1035 return single_open(file
, sdio_uart_proc_show
, NULL
);
1038 static const struct file_operations sdio_uart_proc_fops
= {
1039 .owner
= THIS_MODULE
,
1040 .open
= sdio_uart_proc_open
,
1042 .llseek
= seq_lseek
,
1043 .release
= single_release
,
1046 static const struct tty_port_operations sdio_uart_port_ops
= {
1047 .dtr_rts
= uart_dtr_rts
,
1048 .carrier_raised
= uart_carrier_raised
,
1049 .shutdown
= sdio_uart_shutdown
,
1050 .activate
= sdio_uart_activate
,
1053 static const struct tty_operations sdio_uart_ops
= {
1054 .open
= sdio_uart_open
,
1055 .close
= sdio_uart_close
,
1056 .write
= sdio_uart_write
,
1057 .write_room
= sdio_uart_write_room
,
1058 .chars_in_buffer
= sdio_uart_chars_in_buffer
,
1059 .send_xchar
= sdio_uart_send_xchar
,
1060 .throttle
= sdio_uart_throttle
,
1061 .unthrottle
= sdio_uart_unthrottle
,
1062 .set_termios
= sdio_uart_set_termios
,
1063 .hangup
= sdio_uart_hangup
,
1064 .break_ctl
= sdio_uart_break_ctl
,
1065 .tiocmget
= sdio_uart_tiocmget
,
1066 .tiocmset
= sdio_uart_tiocmset
,
1067 .install
= sdio_uart_install
,
1068 .cleanup
= sdio_uart_cleanup
,
1069 .proc_fops
= &sdio_uart_proc_fops
,
1072 static struct tty_driver
*sdio_uart_tty_driver
;
1074 static int sdio_uart_probe(struct sdio_func
*func
,
1075 const struct sdio_device_id
*id
)
1077 struct sdio_uart_port
*port
;
1080 port
= kzalloc(sizeof(struct sdio_uart_port
), GFP_KERNEL
);
1084 if (func
->class == SDIO_CLASS_UART
) {
1085 printk(KERN_WARNING
"%s: need info on UART class basic setup\n",
1086 sdio_func_id(func
));
1089 } else if (func
->class == SDIO_CLASS_GPS
) {
1091 * We need tuple 0x91. It contains SUBTPL_SIOREG
1092 * and SUBTPL_RCVCAPS.
1094 struct sdio_func_tuple
*tpl
;
1095 for (tpl
= func
->tuples
; tpl
; tpl
= tpl
->next
) {
1096 if (tpl
->code
!= 0x91)
1100 if (tpl
->data
[1] == 0) /* SUBTPL_SIOREG */
1105 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1106 sdio_func_id(func
));
1110 printk(KERN_DEBUG
"%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1111 sdio_func_id(func
), tpl
->data
[2], tpl
->data
[3]);
1112 port
->regs_offset
= (tpl
->data
[4] << 0) |
1113 (tpl
->data
[5] << 8) |
1114 (tpl
->data
[6] << 16);
1115 printk(KERN_DEBUG
"%s: regs offset = 0x%x\n",
1116 sdio_func_id(func
), port
->regs_offset
);
1117 port
->uartclk
= tpl
->data
[7] * 115200;
1118 if (port
->uartclk
== 0)
1119 port
->uartclk
= 115200;
1120 printk(KERN_DEBUG
"%s: clk %d baudcode %u 4800-div %u\n",
1121 sdio_func_id(func
), port
->uartclk
,
1122 tpl
->data
[7], tpl
->data
[8] | (tpl
->data
[9] << 8));
1129 sdio_set_drvdata(func
, port
);
1130 tty_port_init(&port
->port
);
1131 port
->port
.ops
= &sdio_uart_port_ops
;
1133 ret
= sdio_uart_add_port(port
);
1138 dev
= tty_register_device(sdio_uart_tty_driver
,
1139 port
->index
, &func
->dev
);
1141 sdio_uart_port_remove(port
);
1149 static void sdio_uart_remove(struct sdio_func
*func
)
1151 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
1153 tty_unregister_device(sdio_uart_tty_driver
, port
->index
);
1154 sdio_uart_port_remove(port
);
1157 static const struct sdio_device_id sdio_uart_ids
[] = {
1158 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART
) },
1159 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS
) },
1160 { /* end: all zeroes */ },
1163 MODULE_DEVICE_TABLE(sdio
, sdio_uart_ids
);
1165 static struct sdio_driver sdio_uart_driver
= {
1166 .probe
= sdio_uart_probe
,
1167 .remove
= sdio_uart_remove
,
1168 .name
= "sdio_uart",
1169 .id_table
= sdio_uart_ids
,
1172 static int __init
sdio_uart_init(void)
1175 struct tty_driver
*tty_drv
;
1177 sdio_uart_tty_driver
= tty_drv
= alloc_tty_driver(UART_NR
);
1181 tty_drv
->owner
= THIS_MODULE
;
1182 tty_drv
->driver_name
= "sdio_uart";
1183 tty_drv
->name
= "ttySDIO";
1184 tty_drv
->major
= 0; /* dynamically allocated */
1185 tty_drv
->minor_start
= 0;
1186 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1187 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1188 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1189 tty_drv
->init_termios
= tty_std_termios
;
1190 tty_drv
->init_termios
.c_cflag
= B4800
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1191 tty_drv
->init_termios
.c_ispeed
= 4800;
1192 tty_drv
->init_termios
.c_ospeed
= 4800;
1193 tty_set_operations(tty_drv
, &sdio_uart_ops
);
1195 ret
= tty_register_driver(tty_drv
);
1199 ret
= sdio_register_driver(&sdio_uart_driver
);
1206 tty_unregister_driver(tty_drv
);
1208 put_tty_driver(tty_drv
);
1212 static void __exit
sdio_uart_exit(void)
1214 sdio_unregister_driver(&sdio_uart_driver
);
1215 tty_unregister_driver(sdio_uart_tty_driver
);
1216 put_tty_driver(sdio_uart_tty_driver
);
1219 module_init(sdio_uart_init
);
1220 module_exit(sdio_uart_exit
);
1222 MODULE_AUTHOR("Nicolas Pitre");
1223 MODULE_LICENSE("GPL");