1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
8 * Author: Nicolas Pitre
9 * Created: June 15, 2007
10 * Copyright: MontaVista Software, Inc.
14 * Note: Although this driver assumes a 16550A-like UART implementation,
15 * it is not possible to leverage the common 8250/16550 driver, nor the
16 * core UART infrastructure, as they assumes direct access to the hardware
17 * registers, often under a spinlock. This is not possible in the SDIO
18 * context as SDIO access functions must be able to sleep.
20 * Because we need to lock the SDIO host to ensure an exclusive access to
21 * the card, we simply rely on that lock to also prevent and serialize
22 * concurrent access to the same port.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/mutex.h>
30 #include <linux/seq_file.h>
31 #include <linux/serial_reg.h>
32 #include <linux/circ_buf.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/kfifo.h>
36 #include <linux/slab.h>
38 #include <linux/mmc/core.h>
39 #include <linux/mmc/card.h>
40 #include <linux/mmc/sdio_func.h>
41 #include <linux/mmc/sdio_ids.h>
44 #define UART_NR 8 /* Number of UARTs this driver can handle */
47 #define FIFO_SIZE PAGE_SIZE
48 #define WAKEUP_CHARS 256
63 struct sdio_uart_port
{
66 struct sdio_func
*func
;
67 struct mutex func_lock
;
68 struct task_struct
*in_sdio_uart_irq
;
69 unsigned int regs_offset
;
70 struct kfifo xmit_fifo
;
71 spinlock_t write_lock
;
72 struct uart_icount icount
;
75 unsigned int rx_mctrl
;
76 unsigned int read_status_mask
;
77 unsigned int ignore_status_mask
;
83 static struct sdio_uart_port
*sdio_uart_table
[UART_NR
];
84 static DEFINE_SPINLOCK(sdio_uart_table_lock
);
86 static int sdio_uart_add_port(struct sdio_uart_port
*port
)
88 int index
, ret
= -EBUSY
;
90 mutex_init(&port
->func_lock
);
91 spin_lock_init(&port
->write_lock
);
92 if (kfifo_alloc(&port
->xmit_fifo
, FIFO_SIZE
, GFP_KERNEL
))
95 spin_lock(&sdio_uart_table_lock
);
96 for (index
= 0; index
< UART_NR
; index
++) {
97 if (!sdio_uart_table
[index
]) {
99 sdio_uart_table
[index
] = port
;
104 spin_unlock(&sdio_uart_table_lock
);
109 static struct sdio_uart_port
*sdio_uart_port_get(unsigned index
)
111 struct sdio_uart_port
*port
;
113 if (index
>= UART_NR
)
116 spin_lock(&sdio_uart_table_lock
);
117 port
= sdio_uart_table
[index
];
119 tty_port_get(&port
->port
);
120 spin_unlock(&sdio_uart_table_lock
);
125 static void sdio_uart_port_put(struct sdio_uart_port
*port
)
127 tty_port_put(&port
->port
);
130 static void sdio_uart_port_remove(struct sdio_uart_port
*port
)
132 struct sdio_func
*func
;
134 spin_lock(&sdio_uart_table_lock
);
135 sdio_uart_table
[port
->index
] = NULL
;
136 spin_unlock(&sdio_uart_table_lock
);
139 * We're killing a port that potentially still is in use by
140 * the tty layer. Be careful to prevent any further access
141 * to the SDIO function and arrange for the tty layer to
142 * give up on that port ASAP.
143 * Beware: the lock ordering is critical.
145 mutex_lock(&port
->port
.mutex
);
146 mutex_lock(&port
->func_lock
);
148 sdio_claim_host(func
);
150 mutex_unlock(&port
->func_lock
);
151 /* tty_hangup is async so is this safe as is ?? */
152 tty_port_tty_hangup(&port
->port
, false);
153 mutex_unlock(&port
->port
.mutex
);
154 sdio_release_irq(func
);
155 sdio_disable_func(func
);
156 sdio_release_host(func
);
158 sdio_uart_port_put(port
);
161 static int sdio_uart_claim_func(struct sdio_uart_port
*port
)
163 mutex_lock(&port
->func_lock
);
164 if (unlikely(!port
->func
)) {
165 mutex_unlock(&port
->func_lock
);
168 if (likely(port
->in_sdio_uart_irq
!= current
))
169 sdio_claim_host(port
->func
);
170 mutex_unlock(&port
->func_lock
);
174 static inline void sdio_uart_release_func(struct sdio_uart_port
*port
)
176 if (likely(port
->in_sdio_uart_irq
!= current
))
177 sdio_release_host(port
->func
);
180 static inline unsigned int sdio_in(struct sdio_uart_port
*port
, int offset
)
183 c
= sdio_readb(port
->func
, port
->regs_offset
+ offset
, NULL
);
187 static inline void sdio_out(struct sdio_uart_port
*port
, int offset
, int value
)
189 sdio_writeb(port
->func
, value
, port
->regs_offset
+ offset
, NULL
);
192 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port
*port
)
194 unsigned char status
;
197 /* FIXME: What stops this losing the delta bits and breaking
198 sdio_uart_check_modem_status ? */
199 status
= sdio_in(port
, UART_MSR
);
202 if (status
& UART_MSR_DCD
)
204 if (status
& UART_MSR_RI
)
206 if (status
& UART_MSR_DSR
)
208 if (status
& UART_MSR_CTS
)
213 static void sdio_uart_write_mctrl(struct sdio_uart_port
*port
,
216 unsigned char mcr
= 0;
218 if (mctrl
& TIOCM_RTS
)
220 if (mctrl
& TIOCM_DTR
)
222 if (mctrl
& TIOCM_OUT1
)
223 mcr
|= UART_MCR_OUT1
;
224 if (mctrl
& TIOCM_OUT2
)
225 mcr
|= UART_MCR_OUT2
;
226 if (mctrl
& TIOCM_LOOP
)
227 mcr
|= UART_MCR_LOOP
;
229 sdio_out(port
, UART_MCR
, mcr
);
232 static inline void sdio_uart_update_mctrl(struct sdio_uart_port
*port
,
233 unsigned int set
, unsigned int clear
)
238 port
->mctrl
= (old
& ~clear
) | set
;
239 if (old
!= port
->mctrl
)
240 sdio_uart_write_mctrl(port
, port
->mctrl
);
243 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
244 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
246 static void sdio_uart_change_speed(struct sdio_uart_port
*port
,
247 struct ktermios
*termios
,
248 struct ktermios
*old
)
250 unsigned char cval
, fcr
= 0;
251 unsigned int baud
, quot
;
253 switch (termios
->c_cflag
& CSIZE
) {
255 cval
= UART_LCR_WLEN5
;
258 cval
= UART_LCR_WLEN6
;
261 cval
= UART_LCR_WLEN7
;
265 cval
= UART_LCR_WLEN8
;
269 if (termios
->c_cflag
& CSTOPB
)
270 cval
|= UART_LCR_STOP
;
271 if (termios
->c_cflag
& PARENB
)
272 cval
|= UART_LCR_PARITY
;
273 if (!(termios
->c_cflag
& PARODD
))
274 cval
|= UART_LCR_EPAR
;
277 baud
= tty_termios_baud_rate(termios
);
279 baud
= 9600; /* Special case: B0 rate. */
280 if (baud
<= port
->uartclk
)
283 * Oops, the quotient was zero. Try again with the old
284 * baud rate if possible, otherwise default to 9600.
286 termios
->c_cflag
&= ~CBAUD
;
288 termios
->c_cflag
|= old
->c_cflag
& CBAUD
;
291 termios
->c_cflag
|= B9600
;
293 quot
= (2 * port
->uartclk
+ baud
) / (2 * baud
);
296 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_1
;
298 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_R_TRIG_10
;
300 port
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
301 if (termios
->c_iflag
& INPCK
)
302 port
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
303 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
304 port
->read_status_mask
|= UART_LSR_BI
;
307 * Characters to ignore
309 port
->ignore_status_mask
= 0;
310 if (termios
->c_iflag
& IGNPAR
)
311 port
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
312 if (termios
->c_iflag
& IGNBRK
) {
313 port
->ignore_status_mask
|= UART_LSR_BI
;
315 * If we're ignoring parity and break indicators,
316 * ignore overruns too (for real raw support).
318 if (termios
->c_iflag
& IGNPAR
)
319 port
->ignore_status_mask
|= UART_LSR_OE
;
323 * ignore all characters if CREAD is not set
325 if ((termios
->c_cflag
& CREAD
) == 0)
326 port
->ignore_status_mask
|= UART_LSR_DR
;
329 * CTS flow control flag and modem status interrupts
331 port
->ier
&= ~UART_IER_MSI
;
332 if ((termios
->c_cflag
& CRTSCTS
) || !(termios
->c_cflag
& CLOCAL
))
333 port
->ier
|= UART_IER_MSI
;
337 sdio_out(port
, UART_IER
, port
->ier
);
338 sdio_out(port
, UART_LCR
, cval
| UART_LCR_DLAB
);
339 sdio_out(port
, UART_DLL
, quot
& 0xff);
340 sdio_out(port
, UART_DLM
, quot
>> 8);
341 sdio_out(port
, UART_LCR
, cval
);
342 sdio_out(port
, UART_FCR
, fcr
);
344 sdio_uart_write_mctrl(port
, port
->mctrl
);
347 static void sdio_uart_start_tx(struct sdio_uart_port
*port
)
349 if (!(port
->ier
& UART_IER_THRI
)) {
350 port
->ier
|= UART_IER_THRI
;
351 sdio_out(port
, UART_IER
, port
->ier
);
355 static void sdio_uart_stop_tx(struct sdio_uart_port
*port
)
357 if (port
->ier
& UART_IER_THRI
) {
358 port
->ier
&= ~UART_IER_THRI
;
359 sdio_out(port
, UART_IER
, port
->ier
);
363 static void sdio_uart_stop_rx(struct sdio_uart_port
*port
)
365 port
->ier
&= ~UART_IER_RLSI
;
366 port
->read_status_mask
&= ~UART_LSR_DR
;
367 sdio_out(port
, UART_IER
, port
->ier
);
370 static void sdio_uart_receive_chars(struct sdio_uart_port
*port
,
371 unsigned int *status
)
373 unsigned int ch
, flag
;
377 ch
= sdio_in(port
, UART_RX
);
381 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
382 UART_LSR_FE
| UART_LSR_OE
))) {
384 * For statistics only
386 if (*status
& UART_LSR_BI
) {
387 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
389 } else if (*status
& UART_LSR_PE
)
390 port
->icount
.parity
++;
391 else if (*status
& UART_LSR_FE
)
392 port
->icount
.frame
++;
393 if (*status
& UART_LSR_OE
)
394 port
->icount
.overrun
++;
397 * Mask off conditions which should be ignored.
399 *status
&= port
->read_status_mask
;
400 if (*status
& UART_LSR_BI
)
402 else if (*status
& UART_LSR_PE
)
404 else if (*status
& UART_LSR_FE
)
408 if ((*status
& port
->ignore_status_mask
& ~UART_LSR_OE
) == 0)
409 tty_insert_flip_char(&port
->port
, ch
, flag
);
412 * Overrun is special. Since it's reported immediately,
413 * it doesn't affect the current character.
415 if (*status
& ~port
->ignore_status_mask
& UART_LSR_OE
)
416 tty_insert_flip_char(&port
->port
, 0, TTY_OVERRUN
);
418 *status
= sdio_in(port
, UART_LSR
);
419 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
421 tty_flip_buffer_push(&port
->port
);
424 static void sdio_uart_transmit_chars(struct sdio_uart_port
*port
)
426 struct kfifo
*xmit
= &port
->xmit_fifo
;
428 struct tty_struct
*tty
;
433 sdio_out(port
, UART_TX
, port
->x_char
);
439 tty
= tty_port_tty_get(&port
->port
);
441 if (tty
== NULL
|| !kfifo_len(xmit
) ||
442 tty
->stopped
|| tty
->hw_stopped
) {
443 sdio_uart_stop_tx(port
);
448 len
= kfifo_out_locked(xmit
, iobuf
, 16, &port
->write_lock
);
449 for (count
= 0; count
< len
; count
++) {
450 sdio_out(port
, UART_TX
, iobuf
[count
]);
454 len
= kfifo_len(xmit
);
455 if (len
< WAKEUP_CHARS
) {
458 sdio_uart_stop_tx(port
);
463 static void sdio_uart_check_modem_status(struct sdio_uart_port
*port
)
466 struct tty_struct
*tty
;
468 status
= sdio_in(port
, UART_MSR
);
470 if ((status
& UART_MSR_ANY_DELTA
) == 0)
473 if (status
& UART_MSR_TERI
)
475 if (status
& UART_MSR_DDSR
)
477 if (status
& UART_MSR_DDCD
) {
479 /* DCD raise - wake for open */
480 if (status
& UART_MSR_DCD
)
481 wake_up_interruptible(&port
->port
.open_wait
);
483 /* DCD drop - hang up if tty attached */
484 tty_port_tty_hangup(&port
->port
, false);
487 if (status
& UART_MSR_DCTS
) {
489 tty
= tty_port_tty_get(&port
->port
);
490 if (tty
&& C_CRTSCTS(tty
)) {
491 int cts
= (status
& UART_MSR_CTS
);
492 if (tty
->hw_stopped
) {
495 sdio_uart_start_tx(port
);
501 sdio_uart_stop_tx(port
);
510 * This handles the interrupt from one port.
512 static void sdio_uart_irq(struct sdio_func
*func
)
514 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
515 unsigned int iir
, lsr
;
518 * In a few places sdio_uart_irq() is called directly instead of
519 * waiting for the actual interrupt to be raised and the SDIO IRQ
520 * thread scheduled in order to reduce latency. However, some
521 * interaction with the tty core may end up calling us back
522 * (serial echo, flow control, etc.) through those same places
523 * causing undesirable effects. Let's stop the recursion here.
525 if (unlikely(port
->in_sdio_uart_irq
== current
))
528 iir
= sdio_in(port
, UART_IIR
);
529 if (iir
& UART_IIR_NO_INT
)
532 port
->in_sdio_uart_irq
= current
;
533 lsr
= sdio_in(port
, UART_LSR
);
534 if (lsr
& UART_LSR_DR
)
535 sdio_uart_receive_chars(port
, &lsr
);
536 sdio_uart_check_modem_status(port
);
537 if (lsr
& UART_LSR_THRE
)
538 sdio_uart_transmit_chars(port
);
539 port
->in_sdio_uart_irq
= NULL
;
542 static int uart_carrier_raised(struct tty_port
*tport
)
544 struct sdio_uart_port
*port
=
545 container_of(tport
, struct sdio_uart_port
, port
);
546 unsigned int ret
= sdio_uart_claim_func(port
);
547 if (ret
) /* Missing hardware shouldn't block for carrier */
549 ret
= sdio_uart_get_mctrl(port
);
550 sdio_uart_release_func(port
);
557 * uart_dtr_rts - port helper to set uart signals
558 * @tport: tty port to be updated
559 * @onoff: set to turn on DTR/RTS
561 * Called by the tty port helpers when the modem signals need to be
562 * adjusted during an open, close and hangup.
565 static void uart_dtr_rts(struct tty_port
*tport
, int onoff
)
567 struct sdio_uart_port
*port
=
568 container_of(tport
, struct sdio_uart_port
, port
);
569 int ret
= sdio_uart_claim_func(port
);
573 sdio_uart_clear_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
575 sdio_uart_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
576 sdio_uart_release_func(port
);
580 * sdio_uart_activate - start up hardware
581 * @tport: tty port to activate
582 * @tty: tty bound to this port
584 * Activate a tty port. The port locking guarantees us this will be
585 * run exactly once per set of opens, and if successful will see the
586 * shutdown method run exactly once to match. Start up and shutdown are
587 * protected from each other by the internal locking and will not run
588 * at the same time even during a hangup event.
590 * If we successfully start up the port we take an extra kref as we
591 * will keep it around until shutdown when the kref is dropped.
594 static int sdio_uart_activate(struct tty_port
*tport
, struct tty_struct
*tty
)
596 struct sdio_uart_port
*port
=
597 container_of(tport
, struct sdio_uart_port
, port
);
601 * Set the TTY IO error marker - we will only clear this
602 * once we have successfully opened the port.
604 set_bit(TTY_IO_ERROR
, &tty
->flags
);
606 kfifo_reset(&port
->xmit_fifo
);
608 ret
= sdio_uart_claim_func(port
);
611 ret
= sdio_enable_func(port
->func
);
614 ret
= sdio_claim_irq(port
->func
, sdio_uart_irq
);
619 * Clear the FIFO buffers and disable them.
620 * (they will be reenabled in sdio_change_speed())
622 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
623 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
624 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
625 sdio_out(port
, UART_FCR
, 0);
628 * Clear the interrupt registers.
630 (void) sdio_in(port
, UART_LSR
);
631 (void) sdio_in(port
, UART_RX
);
632 (void) sdio_in(port
, UART_IIR
);
633 (void) sdio_in(port
, UART_MSR
);
636 * Now, initialize the UART
638 sdio_out(port
, UART_LCR
, UART_LCR_WLEN8
);
640 port
->ier
= UART_IER_RLSI
|UART_IER_RDI
|UART_IER_RTOIE
|UART_IER_UUE
;
641 port
->mctrl
= TIOCM_OUT2
;
643 sdio_uart_change_speed(port
, &tty
->termios
, NULL
);
646 sdio_uart_set_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
649 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
))
652 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
654 /* Kick the IRQ handler once while we're still holding the host lock */
655 sdio_uart_irq(port
->func
);
657 sdio_uart_release_func(port
);
661 sdio_disable_func(port
->func
);
663 sdio_uart_release_func(port
);
668 * sdio_uart_shutdown - stop hardware
669 * @tport: tty port to shut down
671 * Deactivate a tty port. The port locking guarantees us this will be
672 * run only if a successful matching activate already ran. The two are
673 * protected from each other by the internal locking and will not run
674 * at the same time even during a hangup event.
677 static void sdio_uart_shutdown(struct tty_port
*tport
)
679 struct sdio_uart_port
*port
=
680 container_of(tport
, struct sdio_uart_port
, port
);
683 ret
= sdio_uart_claim_func(port
);
687 sdio_uart_stop_rx(port
);
689 /* Disable interrupts from this port */
690 sdio_release_irq(port
->func
);
692 sdio_out(port
, UART_IER
, 0);
694 sdio_uart_clear_mctrl(port
, TIOCM_OUT2
);
696 /* Disable break condition and FIFOs. */
697 port
->lcr
&= ~UART_LCR_SBC
;
698 sdio_out(port
, UART_LCR
, port
->lcr
);
699 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
700 UART_FCR_CLEAR_RCVR
|
701 UART_FCR_CLEAR_XMIT
);
702 sdio_out(port
, UART_FCR
, 0);
704 sdio_disable_func(port
->func
);
706 sdio_uart_release_func(port
);
709 static void sdio_uart_port_destroy(struct tty_port
*tport
)
711 struct sdio_uart_port
*port
=
712 container_of(tport
, struct sdio_uart_port
, port
);
713 kfifo_free(&port
->xmit_fifo
);
718 * sdio_uart_install - install method
719 * @driver: the driver in use (sdio_uart in our case)
720 * @tty: the tty being bound
722 * Look up and bind the tty and the driver together. Initialize
723 * any needed private data (in our case the termios)
726 static int sdio_uart_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
728 int idx
= tty
->index
;
729 struct sdio_uart_port
*port
= sdio_uart_port_get(idx
);
730 int ret
= tty_standard_install(driver
, tty
);
733 /* This is the ref sdio_uart_port get provided */
734 tty
->driver_data
= port
;
736 sdio_uart_port_put(port
);
741 * sdio_uart_cleanup - called on the last tty kref drop
742 * @tty: the tty being destroyed
744 * Called asynchronously when the last reference to the tty is dropped.
745 * We cannot destroy the tty->driver_data port kref until this point
748 static void sdio_uart_cleanup(struct tty_struct
*tty
)
750 struct sdio_uart_port
*port
= tty
->driver_data
;
751 tty
->driver_data
= NULL
; /* Bug trap */
752 sdio_uart_port_put(port
);
756 * Open/close/hangup is now entirely boilerplate
759 static int sdio_uart_open(struct tty_struct
*tty
, struct file
*filp
)
761 struct sdio_uart_port
*port
= tty
->driver_data
;
762 return tty_port_open(&port
->port
, tty
, filp
);
765 static void sdio_uart_close(struct tty_struct
*tty
, struct file
* filp
)
767 struct sdio_uart_port
*port
= tty
->driver_data
;
768 tty_port_close(&port
->port
, tty
, filp
);
771 static void sdio_uart_hangup(struct tty_struct
*tty
)
773 struct sdio_uart_port
*port
= tty
->driver_data
;
774 tty_port_hangup(&port
->port
);
777 static int sdio_uart_write(struct tty_struct
*tty
, const unsigned char *buf
,
780 struct sdio_uart_port
*port
= tty
->driver_data
;
786 ret
= kfifo_in_locked(&port
->xmit_fifo
, buf
, count
, &port
->write_lock
);
787 if (!(port
->ier
& UART_IER_THRI
)) {
788 int err
= sdio_uart_claim_func(port
);
790 sdio_uart_start_tx(port
);
791 sdio_uart_irq(port
->func
);
792 sdio_uart_release_func(port
);
800 static int sdio_uart_write_room(struct tty_struct
*tty
)
802 struct sdio_uart_port
*port
= tty
->driver_data
;
803 return FIFO_SIZE
- kfifo_len(&port
->xmit_fifo
);
806 static int sdio_uart_chars_in_buffer(struct tty_struct
*tty
)
808 struct sdio_uart_port
*port
= tty
->driver_data
;
809 return kfifo_len(&port
->xmit_fifo
);
812 static void sdio_uart_send_xchar(struct tty_struct
*tty
, char ch
)
814 struct sdio_uart_port
*port
= tty
->driver_data
;
817 if (ch
&& !(port
->ier
& UART_IER_THRI
)) {
818 if (sdio_uart_claim_func(port
) != 0)
820 sdio_uart_start_tx(port
);
821 sdio_uart_irq(port
->func
);
822 sdio_uart_release_func(port
);
826 static void sdio_uart_throttle(struct tty_struct
*tty
)
828 struct sdio_uart_port
*port
= tty
->driver_data
;
830 if (!I_IXOFF(tty
) && !C_CRTSCTS(tty
))
833 if (sdio_uart_claim_func(port
) != 0)
837 port
->x_char
= STOP_CHAR(tty
);
838 sdio_uart_start_tx(port
);
842 sdio_uart_clear_mctrl(port
, TIOCM_RTS
);
844 sdio_uart_irq(port
->func
);
845 sdio_uart_release_func(port
);
848 static void sdio_uart_unthrottle(struct tty_struct
*tty
)
850 struct sdio_uart_port
*port
= tty
->driver_data
;
852 if (!I_IXOFF(tty
) && !C_CRTSCTS(tty
))
855 if (sdio_uart_claim_func(port
) != 0)
862 port
->x_char
= START_CHAR(tty
);
863 sdio_uart_start_tx(port
);
868 sdio_uart_set_mctrl(port
, TIOCM_RTS
);
870 sdio_uart_irq(port
->func
);
871 sdio_uart_release_func(port
);
874 static void sdio_uart_set_termios(struct tty_struct
*tty
,
875 struct ktermios
*old_termios
)
877 struct sdio_uart_port
*port
= tty
->driver_data
;
878 unsigned int cflag
= tty
->termios
.c_cflag
;
880 if (sdio_uart_claim_func(port
) != 0)
883 sdio_uart_change_speed(port
, &tty
->termios
, old_termios
);
885 /* Handle transition to B0 status */
886 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
887 sdio_uart_clear_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
889 /* Handle transition away from B0 status */
890 if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
891 unsigned int mask
= TIOCM_DTR
;
892 if (!(cflag
& CRTSCTS
) || !tty_throttled(tty
))
894 sdio_uart_set_mctrl(port
, mask
);
897 /* Handle turning off CRTSCTS */
898 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
900 sdio_uart_start_tx(port
);
903 /* Handle turning on CRTSCTS */
904 if (!(old_termios
->c_cflag
& CRTSCTS
) && (cflag
& CRTSCTS
)) {
905 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
)) {
907 sdio_uart_stop_tx(port
);
911 sdio_uart_release_func(port
);
914 static int sdio_uart_break_ctl(struct tty_struct
*tty
, int break_state
)
916 struct sdio_uart_port
*port
= tty
->driver_data
;
919 result
= sdio_uart_claim_func(port
);
923 if (break_state
== -1)
924 port
->lcr
|= UART_LCR_SBC
;
926 port
->lcr
&= ~UART_LCR_SBC
;
927 sdio_out(port
, UART_LCR
, port
->lcr
);
929 sdio_uart_release_func(port
);
933 static int sdio_uart_tiocmget(struct tty_struct
*tty
)
935 struct sdio_uart_port
*port
= tty
->driver_data
;
938 result
= sdio_uart_claim_func(port
);
940 result
= port
->mctrl
| sdio_uart_get_mctrl(port
);
941 sdio_uart_release_func(port
);
947 static int sdio_uart_tiocmset(struct tty_struct
*tty
,
948 unsigned int set
, unsigned int clear
)
950 struct sdio_uart_port
*port
= tty
->driver_data
;
953 result
= sdio_uart_claim_func(port
);
955 sdio_uart_update_mctrl(port
, set
, clear
);
956 sdio_uart_release_func(port
);
962 static int sdio_uart_proc_show(struct seq_file
*m
, void *v
)
966 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
968 for (i
= 0; i
< UART_NR
; i
++) {
969 struct sdio_uart_port
*port
= sdio_uart_port_get(i
);
971 seq_printf(m
, "%d: uart:SDIO", i
);
972 if (capable(CAP_SYS_ADMIN
)) {
973 seq_printf(m
, " tx:%d rx:%d",
974 port
->icount
.tx
, port
->icount
.rx
);
975 if (port
->icount
.frame
)
976 seq_printf(m
, " fe:%d",
978 if (port
->icount
.parity
)
979 seq_printf(m
, " pe:%d",
980 port
->icount
.parity
);
981 if (port
->icount
.brk
)
982 seq_printf(m
, " brk:%d",
984 if (port
->icount
.overrun
)
985 seq_printf(m
, " oe:%d",
986 port
->icount
.overrun
);
987 if (port
->icount
.cts
)
988 seq_printf(m
, " cts:%d",
990 if (port
->icount
.dsr
)
991 seq_printf(m
, " dsr:%d",
993 if (port
->icount
.rng
)
994 seq_printf(m
, " rng:%d",
996 if (port
->icount
.dcd
)
997 seq_printf(m
, " dcd:%d",
1000 sdio_uart_port_put(port
);
1007 static const struct tty_port_operations sdio_uart_port_ops
= {
1008 .dtr_rts
= uart_dtr_rts
,
1009 .carrier_raised
= uart_carrier_raised
,
1010 .shutdown
= sdio_uart_shutdown
,
1011 .activate
= sdio_uart_activate
,
1012 .destruct
= sdio_uart_port_destroy
,
1015 static const struct tty_operations sdio_uart_ops
= {
1016 .open
= sdio_uart_open
,
1017 .close
= sdio_uart_close
,
1018 .write
= sdio_uart_write
,
1019 .write_room
= sdio_uart_write_room
,
1020 .chars_in_buffer
= sdio_uart_chars_in_buffer
,
1021 .send_xchar
= sdio_uart_send_xchar
,
1022 .throttle
= sdio_uart_throttle
,
1023 .unthrottle
= sdio_uart_unthrottle
,
1024 .set_termios
= sdio_uart_set_termios
,
1025 .hangup
= sdio_uart_hangup
,
1026 .break_ctl
= sdio_uart_break_ctl
,
1027 .tiocmget
= sdio_uart_tiocmget
,
1028 .tiocmset
= sdio_uart_tiocmset
,
1029 .install
= sdio_uart_install
,
1030 .cleanup
= sdio_uart_cleanup
,
1031 .proc_show
= sdio_uart_proc_show
,
1034 static struct tty_driver
*sdio_uart_tty_driver
;
1036 static int sdio_uart_probe(struct sdio_func
*func
,
1037 const struct sdio_device_id
*id
)
1039 struct sdio_uart_port
*port
;
1042 port
= kzalloc(sizeof(struct sdio_uart_port
), GFP_KERNEL
);
1046 if (func
->class == SDIO_CLASS_UART
) {
1047 pr_warn("%s: need info on UART class basic setup\n",
1048 sdio_func_id(func
));
1051 } else if (func
->class == SDIO_CLASS_GPS
) {
1053 * We need tuple 0x91. It contains SUBTPL_SIOREG
1054 * and SUBTPL_RCVCAPS.
1056 struct sdio_func_tuple
*tpl
;
1057 for (tpl
= func
->tuples
; tpl
; tpl
= tpl
->next
) {
1058 if (tpl
->code
!= 0x91)
1062 if (tpl
->data
[1] == 0) /* SUBTPL_SIOREG */
1066 pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1067 sdio_func_id(func
));
1071 pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1072 sdio_func_id(func
), tpl
->data
[2], tpl
->data
[3]);
1073 port
->regs_offset
= (tpl
->data
[4] << 0) |
1074 (tpl
->data
[5] << 8) |
1075 (tpl
->data
[6] << 16);
1076 pr_debug("%s: regs offset = 0x%x\n",
1077 sdio_func_id(func
), port
->regs_offset
);
1078 port
->uartclk
= tpl
->data
[7] * 115200;
1079 if (port
->uartclk
== 0)
1080 port
->uartclk
= 115200;
1081 pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
1082 sdio_func_id(func
), port
->uartclk
,
1083 tpl
->data
[7], tpl
->data
[8] | (tpl
->data
[9] << 8));
1090 sdio_set_drvdata(func
, port
);
1091 tty_port_init(&port
->port
);
1092 port
->port
.ops
= &sdio_uart_port_ops
;
1094 ret
= sdio_uart_add_port(port
);
1099 dev
= tty_port_register_device(&port
->port
,
1100 sdio_uart_tty_driver
, port
->index
, &func
->dev
);
1102 sdio_uart_port_remove(port
);
1110 static void sdio_uart_remove(struct sdio_func
*func
)
1112 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
1114 tty_unregister_device(sdio_uart_tty_driver
, port
->index
);
1115 sdio_uart_port_remove(port
);
1118 static const struct sdio_device_id sdio_uart_ids
[] = {
1119 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART
) },
1120 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS
) },
1121 { /* end: all zeroes */ },
1124 MODULE_DEVICE_TABLE(sdio
, sdio_uart_ids
);
1126 static struct sdio_driver sdio_uart_driver
= {
1127 .probe
= sdio_uart_probe
,
1128 .remove
= sdio_uart_remove
,
1129 .name
= "sdio_uart",
1130 .id_table
= sdio_uart_ids
,
1133 static int __init
sdio_uart_init(void)
1136 struct tty_driver
*tty_drv
;
1138 sdio_uart_tty_driver
= tty_drv
= alloc_tty_driver(UART_NR
);
1142 tty_drv
->driver_name
= "sdio_uart";
1143 tty_drv
->name
= "ttySDIO";
1144 tty_drv
->major
= 0; /* dynamically allocated */
1145 tty_drv
->minor_start
= 0;
1146 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1147 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1148 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1149 tty_drv
->init_termios
= tty_std_termios
;
1150 tty_drv
->init_termios
.c_cflag
= B4800
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1151 tty_drv
->init_termios
.c_ispeed
= 4800;
1152 tty_drv
->init_termios
.c_ospeed
= 4800;
1153 tty_set_operations(tty_drv
, &sdio_uart_ops
);
1155 ret
= tty_register_driver(tty_drv
);
1159 ret
= sdio_register_driver(&sdio_uart_driver
);
1166 tty_unregister_driver(tty_drv
);
1168 put_tty_driver(tty_drv
);
1172 static void __exit
sdio_uart_exit(void)
1174 sdio_unregister_driver(&sdio_uart_driver
);
1175 tty_unregister_driver(sdio_uart_tty_driver
);
1176 put_tty_driver(sdio_uart_tty_driver
);
1179 module_init(sdio_uart_init
);
1180 module_exit(sdio_uart_exit
);
1182 MODULE_AUTHOR("Nicolas Pitre");
1183 MODULE_LICENSE("GPL");