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 sdio_func
*func
;
71 struct mutex func_lock
;
72 struct task_struct
*in_sdio_uart_irq
;
73 unsigned int regs_offset
;
74 struct kfifo xmit_fifo
;
75 spinlock_t write_lock
;
76 struct uart_icount icount
;
79 unsigned int rx_mctrl
;
80 unsigned int read_status_mask
;
81 unsigned int ignore_status_mask
;
87 static struct sdio_uart_port
*sdio_uart_table
[UART_NR
];
88 static DEFINE_SPINLOCK(sdio_uart_table_lock
);
90 static int sdio_uart_add_port(struct sdio_uart_port
*port
)
92 int index
, ret
= -EBUSY
;
94 mutex_init(&port
->func_lock
);
95 spin_lock_init(&port
->write_lock
);
96 if (kfifo_alloc(&port
->xmit_fifo
, FIFO_SIZE
, GFP_KERNEL
))
99 spin_lock(&sdio_uart_table_lock
);
100 for (index
= 0; index
< UART_NR
; index
++) {
101 if (!sdio_uart_table
[index
]) {
103 sdio_uart_table
[index
] = port
;
108 spin_unlock(&sdio_uart_table_lock
);
113 static struct sdio_uart_port
*sdio_uart_port_get(unsigned index
)
115 struct sdio_uart_port
*port
;
117 if (index
>= UART_NR
)
120 spin_lock(&sdio_uart_table_lock
);
121 port
= sdio_uart_table
[index
];
123 tty_port_get(&port
->port
);
124 spin_unlock(&sdio_uart_table_lock
);
129 static void sdio_uart_port_put(struct sdio_uart_port
*port
)
131 tty_port_put(&port
->port
);
134 static void sdio_uart_port_remove(struct sdio_uart_port
*port
)
136 struct sdio_func
*func
;
137 struct tty_struct
*tty
;
139 BUG_ON(sdio_uart_table
[port
->index
] != port
);
141 spin_lock(&sdio_uart_table_lock
);
142 sdio_uart_table
[port
->index
] = NULL
;
143 spin_unlock(&sdio_uart_table_lock
);
146 * We're killing a port that potentially still is in use by
147 * the tty layer. Be careful to prevent any further access
148 * to the SDIO function and arrange for the tty layer to
149 * give up on that port ASAP.
150 * Beware: the lock ordering is critical.
152 mutex_lock(&port
->port
.mutex
);
153 mutex_lock(&port
->func_lock
);
155 sdio_claim_host(func
);
157 mutex_unlock(&port
->func_lock
);
158 tty
= tty_port_tty_get(&port
->port
);
159 /* tty_hangup is async so is this safe as is ?? */
164 mutex_unlock(&port
->port
.mutex
);
165 sdio_release_irq(func
);
166 sdio_disable_func(func
);
167 sdio_release_host(func
);
169 sdio_uart_port_put(port
);
172 static int sdio_uart_claim_func(struct sdio_uart_port
*port
)
174 mutex_lock(&port
->func_lock
);
175 if (unlikely(!port
->func
)) {
176 mutex_unlock(&port
->func_lock
);
179 if (likely(port
->in_sdio_uart_irq
!= current
))
180 sdio_claim_host(port
->func
);
181 mutex_unlock(&port
->func_lock
);
185 static inline void sdio_uart_release_func(struct sdio_uart_port
*port
)
187 if (likely(port
->in_sdio_uart_irq
!= current
))
188 sdio_release_host(port
->func
);
191 static inline unsigned int sdio_in(struct sdio_uart_port
*port
, int offset
)
194 c
= sdio_readb(port
->func
, port
->regs_offset
+ offset
, NULL
);
198 static inline void sdio_out(struct sdio_uart_port
*port
, int offset
, int value
)
200 sdio_writeb(port
->func
, value
, port
->regs_offset
+ offset
, NULL
);
203 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port
*port
)
205 unsigned char status
;
208 /* FIXME: What stops this losing the delta bits and breaking
209 sdio_uart_check_modem_status ? */
210 status
= sdio_in(port
, UART_MSR
);
213 if (status
& UART_MSR_DCD
)
215 if (status
& UART_MSR_RI
)
217 if (status
& UART_MSR_DSR
)
219 if (status
& UART_MSR_CTS
)
224 static void sdio_uart_write_mctrl(struct sdio_uart_port
*port
,
227 unsigned char mcr
= 0;
229 if (mctrl
& TIOCM_RTS
)
231 if (mctrl
& TIOCM_DTR
)
233 if (mctrl
& TIOCM_OUT1
)
234 mcr
|= UART_MCR_OUT1
;
235 if (mctrl
& TIOCM_OUT2
)
236 mcr
|= UART_MCR_OUT2
;
237 if (mctrl
& TIOCM_LOOP
)
238 mcr
|= UART_MCR_LOOP
;
240 sdio_out(port
, UART_MCR
, mcr
);
243 static inline void sdio_uart_update_mctrl(struct sdio_uart_port
*port
,
244 unsigned int set
, unsigned int clear
)
249 port
->mctrl
= (old
& ~clear
) | set
;
250 if (old
!= port
->mctrl
)
251 sdio_uart_write_mctrl(port
, port
->mctrl
);
254 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
255 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
257 static void sdio_uart_change_speed(struct sdio_uart_port
*port
,
258 struct ktermios
*termios
,
259 struct ktermios
*old
)
261 unsigned char cval
, fcr
= 0;
262 unsigned int baud
, quot
;
264 switch (termios
->c_cflag
& CSIZE
) {
266 cval
= UART_LCR_WLEN5
;
269 cval
= UART_LCR_WLEN6
;
272 cval
= UART_LCR_WLEN7
;
276 cval
= UART_LCR_WLEN8
;
280 if (termios
->c_cflag
& CSTOPB
)
281 cval
|= UART_LCR_STOP
;
282 if (termios
->c_cflag
& PARENB
)
283 cval
|= UART_LCR_PARITY
;
284 if (!(termios
->c_cflag
& PARODD
))
285 cval
|= UART_LCR_EPAR
;
288 baud
= tty_termios_baud_rate(termios
);
290 baud
= 9600; /* Special case: B0 rate. */
291 if (baud
<= port
->uartclk
)
294 * Oops, the quotient was zero. Try again with the old
295 * baud rate if possible, otherwise default to 9600.
297 termios
->c_cflag
&= ~CBAUD
;
299 termios
->c_cflag
|= old
->c_cflag
& CBAUD
;
302 termios
->c_cflag
|= B9600
;
304 quot
= (2 * port
->uartclk
+ baud
) / (2 * baud
);
307 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_1
;
309 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_R_TRIG_10
;
311 port
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
312 if (termios
->c_iflag
& INPCK
)
313 port
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
314 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
315 port
->read_status_mask
|= UART_LSR_BI
;
318 * Characters to ignore
320 port
->ignore_status_mask
= 0;
321 if (termios
->c_iflag
& IGNPAR
)
322 port
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
323 if (termios
->c_iflag
& IGNBRK
) {
324 port
->ignore_status_mask
|= UART_LSR_BI
;
326 * If we're ignoring parity and break indicators,
327 * ignore overruns too (for real raw support).
329 if (termios
->c_iflag
& IGNPAR
)
330 port
->ignore_status_mask
|= UART_LSR_OE
;
334 * ignore all characters if CREAD is not set
336 if ((termios
->c_cflag
& CREAD
) == 0)
337 port
->ignore_status_mask
|= UART_LSR_DR
;
340 * CTS flow control flag and modem status interrupts
342 port
->ier
&= ~UART_IER_MSI
;
343 if ((termios
->c_cflag
& CRTSCTS
) || !(termios
->c_cflag
& CLOCAL
))
344 port
->ier
|= UART_IER_MSI
;
348 sdio_out(port
, UART_IER
, port
->ier
);
349 sdio_out(port
, UART_LCR
, cval
| UART_LCR_DLAB
);
350 sdio_out(port
, UART_DLL
, quot
& 0xff);
351 sdio_out(port
, UART_DLM
, quot
>> 8);
352 sdio_out(port
, UART_LCR
, cval
);
353 sdio_out(port
, UART_FCR
, fcr
);
355 sdio_uart_write_mctrl(port
, port
->mctrl
);
358 static void sdio_uart_start_tx(struct sdio_uart_port
*port
)
360 if (!(port
->ier
& UART_IER_THRI
)) {
361 port
->ier
|= UART_IER_THRI
;
362 sdio_out(port
, UART_IER
, port
->ier
);
366 static void sdio_uart_stop_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_rx(struct sdio_uart_port
*port
)
376 port
->ier
&= ~UART_IER_RLSI
;
377 port
->read_status_mask
&= ~UART_LSR_DR
;
378 sdio_out(port
, UART_IER
, port
->ier
);
381 static void sdio_uart_receive_chars(struct sdio_uart_port
*port
,
382 unsigned int *status
)
384 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
385 unsigned int ch
, flag
;
389 ch
= sdio_in(port
, UART_RX
);
393 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
394 UART_LSR_FE
| UART_LSR_OE
))) {
396 * For statistics only
398 if (*status
& UART_LSR_BI
) {
399 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
401 } else if (*status
& UART_LSR_PE
)
402 port
->icount
.parity
++;
403 else if (*status
& UART_LSR_FE
)
404 port
->icount
.frame
++;
405 if (*status
& UART_LSR_OE
)
406 port
->icount
.overrun
++;
409 * Mask off conditions which should be ignored.
411 *status
&= port
->read_status_mask
;
412 if (*status
& UART_LSR_BI
)
414 else if (*status
& UART_LSR_PE
)
416 else if (*status
& UART_LSR_FE
)
420 if ((*status
& port
->ignore_status_mask
& ~UART_LSR_OE
) == 0)
422 tty_insert_flip_char(tty
, ch
, flag
);
425 * Overrun is special. Since it's reported immediately,
426 * it doesn't affect the current character.
428 if (*status
& ~port
->ignore_status_mask
& UART_LSR_OE
)
430 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
432 *status
= sdio_in(port
, UART_LSR
);
433 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
435 tty_flip_buffer_push(tty
);
440 static void sdio_uart_transmit_chars(struct sdio_uart_port
*port
)
442 struct kfifo
*xmit
= &port
->xmit_fifo
;
444 struct tty_struct
*tty
;
449 sdio_out(port
, UART_TX
, port
->x_char
);
455 tty
= tty_port_tty_get(&port
->port
);
457 if (tty
== NULL
|| !kfifo_len(xmit
) ||
458 tty
->stopped
|| tty
->hw_stopped
) {
459 sdio_uart_stop_tx(port
);
464 len
= kfifo_out_locked(xmit
, iobuf
, 16, &port
->write_lock
);
465 for (count
= 0; count
< len
; count
++) {
466 sdio_out(port
, UART_TX
, iobuf
[count
]);
470 len
= kfifo_len(xmit
);
471 if (len
< WAKEUP_CHARS
) {
474 sdio_uart_stop_tx(port
);
479 static void sdio_uart_check_modem_status(struct sdio_uart_port
*port
)
482 struct tty_struct
*tty
;
484 status
= sdio_in(port
, UART_MSR
);
486 if ((status
& UART_MSR_ANY_DELTA
) == 0)
489 if (status
& UART_MSR_TERI
)
491 if (status
& UART_MSR_DDSR
)
493 if (status
& UART_MSR_DDCD
) {
495 /* DCD raise - wake for open */
496 if (status
& UART_MSR_DCD
)
497 wake_up_interruptible(&port
->port
.open_wait
);
499 /* DCD drop - hang up if tty attached */
500 tty
= tty_port_tty_get(&port
->port
);
507 if (status
& UART_MSR_DCTS
) {
509 tty
= tty_port_tty_get(&port
->port
);
510 if (tty
&& (tty
->termios
.c_cflag
& CRTSCTS
)) {
511 int cts
= (status
& UART_MSR_CTS
);
512 if (tty
->hw_stopped
) {
515 sdio_uart_start_tx(port
);
521 sdio_uart_stop_tx(port
);
530 * This handles the interrupt from one port.
532 static void sdio_uart_irq(struct sdio_func
*func
)
534 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
535 unsigned int iir
, lsr
;
538 * In a few places sdio_uart_irq() is called directly instead of
539 * waiting for the actual interrupt to be raised and the SDIO IRQ
540 * thread scheduled in order to reduce latency. However, some
541 * interaction with the tty core may end up calling us back
542 * (serial echo, flow control, etc.) through those same places
543 * causing undesirable effects. Let's stop the recursion here.
545 if (unlikely(port
->in_sdio_uart_irq
== current
))
548 iir
= sdio_in(port
, UART_IIR
);
549 if (iir
& UART_IIR_NO_INT
)
552 port
->in_sdio_uart_irq
= current
;
553 lsr
= sdio_in(port
, UART_LSR
);
554 if (lsr
& UART_LSR_DR
)
555 sdio_uart_receive_chars(port
, &lsr
);
556 sdio_uart_check_modem_status(port
);
557 if (lsr
& UART_LSR_THRE
)
558 sdio_uart_transmit_chars(port
);
559 port
->in_sdio_uart_irq
= NULL
;
562 static int uart_carrier_raised(struct tty_port
*tport
)
564 struct sdio_uart_port
*port
=
565 container_of(tport
, struct sdio_uart_port
, port
);
566 unsigned int ret
= sdio_uart_claim_func(port
);
567 if (ret
) /* Missing hardware shouldn't block for carrier */
569 ret
= sdio_uart_get_mctrl(port
);
570 sdio_uart_release_func(port
);
577 * uart_dtr_rts - port helper to set uart signals
578 * @tport: tty port to be updated
579 * @onoff: set to turn on DTR/RTS
581 * Called by the tty port helpers when the modem signals need to be
582 * adjusted during an open, close and hangup.
585 static void uart_dtr_rts(struct tty_port
*tport
, int onoff
)
587 struct sdio_uart_port
*port
=
588 container_of(tport
, struct sdio_uart_port
, port
);
589 int ret
= sdio_uart_claim_func(port
);
593 sdio_uart_clear_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
595 sdio_uart_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
596 sdio_uart_release_func(port
);
600 * sdio_uart_activate - start up hardware
601 * @tport: tty port to activate
602 * @tty: tty bound to this port
604 * Activate a tty port. The port locking guarantees us this will be
605 * run exactly once per set of opens, and if successful will see the
606 * shutdown method run exactly once to match. Start up and shutdown are
607 * protected from each other by the internal locking and will not run
608 * at the same time even during a hangup event.
610 * If we successfully start up the port we take an extra kref as we
611 * will keep it around until shutdown when the kref is dropped.
614 static int sdio_uart_activate(struct tty_port
*tport
, struct tty_struct
*tty
)
616 struct sdio_uart_port
*port
=
617 container_of(tport
, struct sdio_uart_port
, port
);
621 * Set the TTY IO error marker - we will only clear this
622 * once we have successfully opened the port.
624 set_bit(TTY_IO_ERROR
, &tty
->flags
);
626 kfifo_reset(&port
->xmit_fifo
);
628 ret
= sdio_uart_claim_func(port
);
631 ret
= sdio_enable_func(port
->func
);
634 ret
= sdio_claim_irq(port
->func
, sdio_uart_irq
);
639 * Clear the FIFO buffers and disable them.
640 * (they will be reenabled in sdio_change_speed())
642 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
643 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
644 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
645 sdio_out(port
, UART_FCR
, 0);
648 * Clear the interrupt registers.
650 (void) sdio_in(port
, UART_LSR
);
651 (void) sdio_in(port
, UART_RX
);
652 (void) sdio_in(port
, UART_IIR
);
653 (void) sdio_in(port
, UART_MSR
);
656 * Now, initialize the UART
658 sdio_out(port
, UART_LCR
, UART_LCR_WLEN8
);
660 port
->ier
= UART_IER_RLSI
|UART_IER_RDI
|UART_IER_RTOIE
|UART_IER_UUE
;
661 port
->mctrl
= TIOCM_OUT2
;
663 sdio_uart_change_speed(port
, &tty
->termios
, NULL
);
665 if (tty
->termios
.c_cflag
& CBAUD
)
666 sdio_uart_set_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
668 if (tty
->termios
.c_cflag
& CRTSCTS
)
669 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
))
672 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
674 /* Kick the IRQ handler once while we're still holding the host lock */
675 sdio_uart_irq(port
->func
);
677 sdio_uart_release_func(port
);
681 sdio_disable_func(port
->func
);
683 sdio_uart_release_func(port
);
688 * sdio_uart_shutdown - stop hardware
689 * @tport: tty port to shut down
691 * Deactivate a tty port. The port locking guarantees us this will be
692 * run only if a successful matching activate already ran. The two are
693 * protected from each other by the internal locking and will not run
694 * at the same time even during a hangup event.
697 static void sdio_uart_shutdown(struct tty_port
*tport
)
699 struct sdio_uart_port
*port
=
700 container_of(tport
, struct sdio_uart_port
, port
);
703 ret
= sdio_uart_claim_func(port
);
707 sdio_uart_stop_rx(port
);
709 /* Disable interrupts from this port */
710 sdio_release_irq(port
->func
);
712 sdio_out(port
, UART_IER
, 0);
714 sdio_uart_clear_mctrl(port
, TIOCM_OUT2
);
716 /* Disable break condition and FIFOs. */
717 port
->lcr
&= ~UART_LCR_SBC
;
718 sdio_out(port
, UART_LCR
, port
->lcr
);
719 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
720 UART_FCR_CLEAR_RCVR
|
721 UART_FCR_CLEAR_XMIT
);
722 sdio_out(port
, UART_FCR
, 0);
724 sdio_disable_func(port
->func
);
726 sdio_uart_release_func(port
);
729 static void sdio_uart_port_destroy(struct tty_port
*tport
)
731 struct sdio_uart_port
*port
=
732 container_of(tport
, struct sdio_uart_port
, port
);
733 kfifo_free(&port
->xmit_fifo
);
738 * sdio_uart_install - install method
739 * @driver: the driver in use (sdio_uart in our case)
740 * @tty: the tty being bound
742 * Look up and bind the tty and the driver together. Initialize
743 * any needed private data (in our case the termios)
746 static int sdio_uart_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
748 int idx
= tty
->index
;
749 struct sdio_uart_port
*port
= sdio_uart_port_get(idx
);
750 int ret
= tty_standard_install(driver
, tty
);
753 /* This is the ref sdio_uart_port get provided */
754 tty
->driver_data
= port
;
756 sdio_uart_port_put(port
);
761 * sdio_uart_cleanup - called on the last tty kref drop
762 * @tty: the tty being destroyed
764 * Called asynchronously when the last reference to the tty is dropped.
765 * We cannot destroy the tty->driver_data port kref until this point
768 static void sdio_uart_cleanup(struct tty_struct
*tty
)
770 struct sdio_uart_port
*port
= tty
->driver_data
;
771 tty
->driver_data
= NULL
; /* Bug trap */
772 sdio_uart_port_put(port
);
776 * Open/close/hangup is now entirely boilerplate
779 static int sdio_uart_open(struct tty_struct
*tty
, struct file
*filp
)
781 struct sdio_uart_port
*port
= tty
->driver_data
;
782 return tty_port_open(&port
->port
, tty
, filp
);
785 static void sdio_uart_close(struct tty_struct
*tty
, struct file
* filp
)
787 struct sdio_uart_port
*port
= tty
->driver_data
;
788 tty_port_close(&port
->port
, tty
, filp
);
791 static void sdio_uart_hangup(struct tty_struct
*tty
)
793 struct sdio_uart_port
*port
= tty
->driver_data
;
794 tty_port_hangup(&port
->port
);
797 static int sdio_uart_write(struct tty_struct
*tty
, const unsigned char *buf
,
800 struct sdio_uart_port
*port
= tty
->driver_data
;
806 ret
= kfifo_in_locked(&port
->xmit_fifo
, buf
, count
, &port
->write_lock
);
807 if (!(port
->ier
& UART_IER_THRI
)) {
808 int err
= sdio_uart_claim_func(port
);
810 sdio_uart_start_tx(port
);
811 sdio_uart_irq(port
->func
);
812 sdio_uart_release_func(port
);
820 static int sdio_uart_write_room(struct tty_struct
*tty
)
822 struct sdio_uart_port
*port
= tty
->driver_data
;
823 return FIFO_SIZE
- kfifo_len(&port
->xmit_fifo
);
826 static int sdio_uart_chars_in_buffer(struct tty_struct
*tty
)
828 struct sdio_uart_port
*port
= tty
->driver_data
;
829 return kfifo_len(&port
->xmit_fifo
);
832 static void sdio_uart_send_xchar(struct tty_struct
*tty
, char ch
)
834 struct sdio_uart_port
*port
= tty
->driver_data
;
837 if (ch
&& !(port
->ier
& UART_IER_THRI
)) {
838 if (sdio_uart_claim_func(port
) != 0)
840 sdio_uart_start_tx(port
);
841 sdio_uart_irq(port
->func
);
842 sdio_uart_release_func(port
);
846 static void sdio_uart_throttle(struct tty_struct
*tty
)
848 struct sdio_uart_port
*port
= tty
->driver_data
;
850 if (!I_IXOFF(tty
) && !(tty
->termios
.c_cflag
& CRTSCTS
))
853 if (sdio_uart_claim_func(port
) != 0)
857 port
->x_char
= STOP_CHAR(tty
);
858 sdio_uart_start_tx(port
);
861 if (tty
->termios
.c_cflag
& CRTSCTS
)
862 sdio_uart_clear_mctrl(port
, TIOCM_RTS
);
864 sdio_uart_irq(port
->func
);
865 sdio_uart_release_func(port
);
868 static void sdio_uart_unthrottle(struct tty_struct
*tty
)
870 struct sdio_uart_port
*port
= tty
->driver_data
;
872 if (!I_IXOFF(tty
) && !(tty
->termios
.c_cflag
& CRTSCTS
))
875 if (sdio_uart_claim_func(port
) != 0)
882 port
->x_char
= START_CHAR(tty
);
883 sdio_uart_start_tx(port
);
887 if (tty
->termios
.c_cflag
& CRTSCTS
)
888 sdio_uart_set_mctrl(port
, TIOCM_RTS
);
890 sdio_uart_irq(port
->func
);
891 sdio_uart_release_func(port
);
894 static void sdio_uart_set_termios(struct tty_struct
*tty
,
895 struct ktermios
*old_termios
)
897 struct sdio_uart_port
*port
= tty
->driver_data
;
898 unsigned int cflag
= tty
->termios
.c_cflag
;
900 if (sdio_uart_claim_func(port
) != 0)
903 sdio_uart_change_speed(port
, &tty
->termios
, old_termios
);
905 /* Handle transition to B0 status */
906 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
907 sdio_uart_clear_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
909 /* Handle transition away from B0 status */
910 if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
911 unsigned int mask
= TIOCM_DTR
;
912 if (!(cflag
& CRTSCTS
) || !test_bit(TTY_THROTTLED
, &tty
->flags
))
914 sdio_uart_set_mctrl(port
, mask
);
917 /* Handle turning off CRTSCTS */
918 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
920 sdio_uart_start_tx(port
);
923 /* Handle turning on CRTSCTS */
924 if (!(old_termios
->c_cflag
& CRTSCTS
) && (cflag
& CRTSCTS
)) {
925 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
)) {
927 sdio_uart_stop_tx(port
);
931 sdio_uart_release_func(port
);
934 static int sdio_uart_break_ctl(struct tty_struct
*tty
, int break_state
)
936 struct sdio_uart_port
*port
= tty
->driver_data
;
939 result
= sdio_uart_claim_func(port
);
943 if (break_state
== -1)
944 port
->lcr
|= UART_LCR_SBC
;
946 port
->lcr
&= ~UART_LCR_SBC
;
947 sdio_out(port
, UART_LCR
, port
->lcr
);
949 sdio_uart_release_func(port
);
953 static int sdio_uart_tiocmget(struct tty_struct
*tty
)
955 struct sdio_uart_port
*port
= tty
->driver_data
;
958 result
= sdio_uart_claim_func(port
);
960 result
= port
->mctrl
| sdio_uart_get_mctrl(port
);
961 sdio_uart_release_func(port
);
967 static int sdio_uart_tiocmset(struct tty_struct
*tty
,
968 unsigned int set
, unsigned int clear
)
970 struct sdio_uart_port
*port
= tty
->driver_data
;
973 result
= sdio_uart_claim_func(port
);
975 sdio_uart_update_mctrl(port
, set
, clear
);
976 sdio_uart_release_func(port
);
982 static int sdio_uart_proc_show(struct seq_file
*m
, void *v
)
986 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
988 for (i
= 0; i
< UART_NR
; i
++) {
989 struct sdio_uart_port
*port
= sdio_uart_port_get(i
);
991 seq_printf(m
, "%d: uart:SDIO", i
);
992 if (capable(CAP_SYS_ADMIN
)) {
993 seq_printf(m
, " tx:%d rx:%d",
994 port
->icount
.tx
, port
->icount
.rx
);
995 if (port
->icount
.frame
)
996 seq_printf(m
, " fe:%d",
998 if (port
->icount
.parity
)
999 seq_printf(m
, " pe:%d",
1000 port
->icount
.parity
);
1001 if (port
->icount
.brk
)
1002 seq_printf(m
, " brk:%d",
1004 if (port
->icount
.overrun
)
1005 seq_printf(m
, " oe:%d",
1006 port
->icount
.overrun
);
1007 if (port
->icount
.cts
)
1008 seq_printf(m
, " cts:%d",
1010 if (port
->icount
.dsr
)
1011 seq_printf(m
, " dsr:%d",
1013 if (port
->icount
.rng
)
1014 seq_printf(m
, " rng:%d",
1016 if (port
->icount
.dcd
)
1017 seq_printf(m
, " dcd:%d",
1020 sdio_uart_port_put(port
);
1027 static int sdio_uart_proc_open(struct inode
*inode
, struct file
*file
)
1029 return single_open(file
, sdio_uart_proc_show
, NULL
);
1032 static const struct file_operations sdio_uart_proc_fops
= {
1033 .owner
= THIS_MODULE
,
1034 .open
= sdio_uart_proc_open
,
1036 .llseek
= seq_lseek
,
1037 .release
= single_release
,
1040 static const struct tty_port_operations sdio_uart_port_ops
= {
1041 .dtr_rts
= uart_dtr_rts
,
1042 .carrier_raised
= uart_carrier_raised
,
1043 .shutdown
= sdio_uart_shutdown
,
1044 .activate
= sdio_uart_activate
,
1045 .destruct
= sdio_uart_port_destroy
,
1048 static const struct tty_operations sdio_uart_ops
= {
1049 .open
= sdio_uart_open
,
1050 .close
= sdio_uart_close
,
1051 .write
= sdio_uart_write
,
1052 .write_room
= sdio_uart_write_room
,
1053 .chars_in_buffer
= sdio_uart_chars_in_buffer
,
1054 .send_xchar
= sdio_uart_send_xchar
,
1055 .throttle
= sdio_uart_throttle
,
1056 .unthrottle
= sdio_uart_unthrottle
,
1057 .set_termios
= sdio_uart_set_termios
,
1058 .hangup
= sdio_uart_hangup
,
1059 .break_ctl
= sdio_uart_break_ctl
,
1060 .tiocmget
= sdio_uart_tiocmget
,
1061 .tiocmset
= sdio_uart_tiocmset
,
1062 .install
= sdio_uart_install
,
1063 .cleanup
= sdio_uart_cleanup
,
1064 .proc_fops
= &sdio_uart_proc_fops
,
1067 static struct tty_driver
*sdio_uart_tty_driver
;
1069 static int sdio_uart_probe(struct sdio_func
*func
,
1070 const struct sdio_device_id
*id
)
1072 struct sdio_uart_port
*port
;
1075 port
= kzalloc(sizeof(struct sdio_uart_port
), GFP_KERNEL
);
1079 if (func
->class == SDIO_CLASS_UART
) {
1080 pr_warning("%s: need info on UART class basic setup\n",
1081 sdio_func_id(func
));
1084 } else if (func
->class == SDIO_CLASS_GPS
) {
1086 * We need tuple 0x91. It contains SUBTPL_SIOREG
1087 * and SUBTPL_RCVCAPS.
1089 struct sdio_func_tuple
*tpl
;
1090 for (tpl
= func
->tuples
; tpl
; tpl
= tpl
->next
) {
1091 if (tpl
->code
!= 0x91)
1095 if (tpl
->data
[1] == 0) /* SUBTPL_SIOREG */
1100 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1101 sdio_func_id(func
));
1105 pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1106 sdio_func_id(func
), tpl
->data
[2], tpl
->data
[3]);
1107 port
->regs_offset
= (tpl
->data
[4] << 0) |
1108 (tpl
->data
[5] << 8) |
1109 (tpl
->data
[6] << 16);
1110 pr_debug("%s: regs offset = 0x%x\n",
1111 sdio_func_id(func
), port
->regs_offset
);
1112 port
->uartclk
= tpl
->data
[7] * 115200;
1113 if (port
->uartclk
== 0)
1114 port
->uartclk
= 115200;
1115 pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
1116 sdio_func_id(func
), port
->uartclk
,
1117 tpl
->data
[7], tpl
->data
[8] | (tpl
->data
[9] << 8));
1124 sdio_set_drvdata(func
, port
);
1125 tty_port_init(&port
->port
);
1126 port
->port
.ops
= &sdio_uart_port_ops
;
1128 ret
= sdio_uart_add_port(port
);
1133 dev
= tty_port_register_device(&port
->port
,
1134 sdio_uart_tty_driver
, port
->index
, &func
->dev
);
1136 sdio_uart_port_remove(port
);
1144 static void sdio_uart_remove(struct sdio_func
*func
)
1146 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
1148 tty_unregister_device(sdio_uart_tty_driver
, port
->index
);
1149 sdio_uart_port_remove(port
);
1152 static const struct sdio_device_id sdio_uart_ids
[] = {
1153 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART
) },
1154 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS
) },
1155 { /* end: all zeroes */ },
1158 MODULE_DEVICE_TABLE(sdio
, sdio_uart_ids
);
1160 static struct sdio_driver sdio_uart_driver
= {
1161 .probe
= sdio_uart_probe
,
1162 .remove
= sdio_uart_remove
,
1163 .name
= "sdio_uart",
1164 .id_table
= sdio_uart_ids
,
1167 static int __init
sdio_uart_init(void)
1170 struct tty_driver
*tty_drv
;
1172 sdio_uart_tty_driver
= tty_drv
= alloc_tty_driver(UART_NR
);
1176 tty_drv
->driver_name
= "sdio_uart";
1177 tty_drv
->name
= "ttySDIO";
1178 tty_drv
->major
= 0; /* dynamically allocated */
1179 tty_drv
->minor_start
= 0;
1180 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1181 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1182 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1183 tty_drv
->init_termios
= tty_std_termios
;
1184 tty_drv
->init_termios
.c_cflag
= B4800
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1185 tty_drv
->init_termios
.c_ispeed
= 4800;
1186 tty_drv
->init_termios
.c_ospeed
= 4800;
1187 tty_set_operations(tty_drv
, &sdio_uart_ops
);
1189 ret
= tty_register_driver(tty_drv
);
1193 ret
= sdio_register_driver(&sdio_uart_driver
);
1200 tty_unregister_driver(tty_drv
);
1202 put_tty_driver(tty_drv
);
1206 static void __exit
sdio_uart_exit(void)
1208 sdio_unregister_driver(&sdio_uart_driver
);
1209 tty_unregister_driver(sdio_uart_tty_driver
);
1210 put_tty_driver(sdio_uart_tty_driver
);
1213 module_init(sdio_uart_init
);
1214 module_exit(sdio_uart_exit
);
1216 MODULE_AUTHOR("Nicolas Pitre");
1217 MODULE_LICENSE("GPL");