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.h>
32 #include <linux/serial_reg.h>
33 #include <linux/circ_buf.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/kfifo.h>
37 #include <linux/slab.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 FIFO_SIZE PAGE_SIZE
49 #define WAKEUP_CHARS 256
64 struct sdio_uart_port
{
67 struct sdio_func
*func
;
68 struct mutex func_lock
;
69 struct task_struct
*in_sdio_uart_irq
;
70 unsigned int regs_offset
;
71 struct kfifo xmit_fifo
;
72 spinlock_t write_lock
;
73 struct uart_icount icount
;
76 unsigned int rx_mctrl
;
77 unsigned int read_status_mask
;
78 unsigned int ignore_status_mask
;
84 static struct sdio_uart_port
*sdio_uart_table
[UART_NR
];
85 static DEFINE_SPINLOCK(sdio_uart_table_lock
);
87 static int sdio_uart_add_port(struct sdio_uart_port
*port
)
89 int index
, ret
= -EBUSY
;
91 mutex_init(&port
->func_lock
);
92 spin_lock_init(&port
->write_lock
);
93 if (kfifo_alloc(&port
->xmit_fifo
, FIFO_SIZE
, GFP_KERNEL
))
96 spin_lock(&sdio_uart_table_lock
);
97 for (index
= 0; index
< UART_NR
; index
++) {
98 if (!sdio_uart_table
[index
]) {
100 sdio_uart_table
[index
] = port
;
105 spin_unlock(&sdio_uart_table_lock
);
110 static struct sdio_uart_port
*sdio_uart_port_get(unsigned index
)
112 struct sdio_uart_port
*port
;
114 if (index
>= UART_NR
)
117 spin_lock(&sdio_uart_table_lock
);
118 port
= sdio_uart_table
[index
];
120 tty_port_get(&port
->port
);
121 spin_unlock(&sdio_uart_table_lock
);
126 static void sdio_uart_port_put(struct sdio_uart_port
*port
)
128 tty_port_put(&port
->port
);
131 static void sdio_uart_port_remove(struct sdio_uart_port
*port
)
133 struct sdio_func
*func
;
135 spin_lock(&sdio_uart_table_lock
);
136 sdio_uart_table
[port
->index
] = NULL
;
137 spin_unlock(&sdio_uart_table_lock
);
140 * We're killing a port that potentially still is in use by
141 * the tty layer. Be careful to prevent any further access
142 * to the SDIO function and arrange for the tty layer to
143 * give up on that port ASAP.
144 * Beware: the lock ordering is critical.
146 mutex_lock(&port
->port
.mutex
);
147 mutex_lock(&port
->func_lock
);
149 sdio_claim_host(func
);
151 mutex_unlock(&port
->func_lock
);
152 /* tty_hangup is async so is this safe as is ?? */
153 tty_port_tty_hangup(&port
->port
, false);
154 mutex_unlock(&port
->port
.mutex
);
155 sdio_release_irq(func
);
156 sdio_disable_func(func
);
157 sdio_release_host(func
);
159 sdio_uart_port_put(port
);
162 static int sdio_uart_claim_func(struct sdio_uart_port
*port
)
164 mutex_lock(&port
->func_lock
);
165 if (unlikely(!port
->func
)) {
166 mutex_unlock(&port
->func_lock
);
169 if (likely(port
->in_sdio_uart_irq
!= current
))
170 sdio_claim_host(port
->func
);
171 mutex_unlock(&port
->func_lock
);
175 static inline void sdio_uart_release_func(struct sdio_uart_port
*port
)
177 if (likely(port
->in_sdio_uart_irq
!= current
))
178 sdio_release_host(port
->func
);
181 static inline u8
sdio_in(struct sdio_uart_port
*port
, int offset
)
183 return sdio_readb(port
->func
, port
->regs_offset
+ offset
, NULL
);
186 static inline void sdio_out(struct sdio_uart_port
*port
, int offset
, int value
)
188 sdio_writeb(port
->func
, value
, port
->regs_offset
+ offset
, NULL
);
191 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port
*port
)
196 /* FIXME: What stops this losing the delta bits and breaking
197 sdio_uart_check_modem_status ? */
198 status
= sdio_in(port
, UART_MSR
);
201 if (status
& UART_MSR_DCD
)
203 if (status
& UART_MSR_RI
)
205 if (status
& UART_MSR_DSR
)
207 if (status
& UART_MSR_CTS
)
212 static void sdio_uart_write_mctrl(struct sdio_uart_port
*port
,
215 unsigned char mcr
= 0;
217 if (mctrl
& TIOCM_RTS
)
219 if (mctrl
& TIOCM_DTR
)
221 if (mctrl
& TIOCM_OUT1
)
222 mcr
|= UART_MCR_OUT1
;
223 if (mctrl
& TIOCM_OUT2
)
224 mcr
|= UART_MCR_OUT2
;
225 if (mctrl
& TIOCM_LOOP
)
226 mcr
|= UART_MCR_LOOP
;
228 sdio_out(port
, UART_MCR
, mcr
);
231 static inline void sdio_uart_update_mctrl(struct sdio_uart_port
*port
,
232 unsigned int set
, unsigned int clear
)
237 port
->mctrl
= (old
& ~clear
) | set
;
238 if (old
!= port
->mctrl
)
239 sdio_uart_write_mctrl(port
, port
->mctrl
);
242 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
243 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
245 static void sdio_uart_change_speed(struct sdio_uart_port
*port
,
246 struct ktermios
*termios
,
247 const struct ktermios
*old
)
249 unsigned char cval
, fcr
= 0;
250 unsigned int baud
, quot
;
252 cval
= UART_LCR_WLEN(tty_get_char_size(termios
->c_cflag
));
254 if (termios
->c_cflag
& CSTOPB
)
255 cval
|= UART_LCR_STOP
;
256 if (termios
->c_cflag
& PARENB
)
257 cval
|= UART_LCR_PARITY
;
258 if (!(termios
->c_cflag
& PARODD
))
259 cval
|= UART_LCR_EPAR
;
262 baud
= tty_termios_baud_rate(termios
);
264 baud
= 9600; /* Special case: B0 rate. */
265 if (baud
<= port
->uartclk
)
268 * Oops, the quotient was zero. Try again with the old
269 * baud rate if possible, otherwise default to 9600.
271 termios
->c_cflag
&= ~CBAUD
;
273 termios
->c_cflag
|= old
->c_cflag
& CBAUD
;
276 termios
->c_cflag
|= B9600
;
278 quot
= (2 * port
->uartclk
+ baud
) / (2 * baud
);
281 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_1
;
283 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_R_TRIG_10
;
285 port
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
286 if (termios
->c_iflag
& INPCK
)
287 port
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
288 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
289 port
->read_status_mask
|= UART_LSR_BI
;
292 * Characters to ignore
294 port
->ignore_status_mask
= 0;
295 if (termios
->c_iflag
& IGNPAR
)
296 port
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
297 if (termios
->c_iflag
& IGNBRK
) {
298 port
->ignore_status_mask
|= UART_LSR_BI
;
300 * If we're ignoring parity and break indicators,
301 * ignore overruns too (for real raw support).
303 if (termios
->c_iflag
& IGNPAR
)
304 port
->ignore_status_mask
|= UART_LSR_OE
;
308 * ignore all characters if CREAD is not set
310 if ((termios
->c_cflag
& CREAD
) == 0)
311 port
->ignore_status_mask
|= UART_LSR_DR
;
314 * CTS flow control flag and modem status interrupts
316 port
->ier
&= ~UART_IER_MSI
;
317 if ((termios
->c_cflag
& CRTSCTS
) || !(termios
->c_cflag
& CLOCAL
))
318 port
->ier
|= UART_IER_MSI
;
322 sdio_out(port
, UART_IER
, port
->ier
);
323 sdio_out(port
, UART_LCR
, cval
| UART_LCR_DLAB
);
324 sdio_out(port
, UART_DLL
, quot
& 0xff);
325 sdio_out(port
, UART_DLM
, quot
>> 8);
326 sdio_out(port
, UART_LCR
, cval
);
327 sdio_out(port
, UART_FCR
, fcr
);
329 sdio_uart_write_mctrl(port
, port
->mctrl
);
332 static void sdio_uart_start_tx(struct sdio_uart_port
*port
)
334 if (!(port
->ier
& UART_IER_THRI
)) {
335 port
->ier
|= UART_IER_THRI
;
336 sdio_out(port
, UART_IER
, port
->ier
);
340 static void sdio_uart_stop_tx(struct sdio_uart_port
*port
)
342 if (port
->ier
& UART_IER_THRI
) {
343 port
->ier
&= ~UART_IER_THRI
;
344 sdio_out(port
, UART_IER
, port
->ier
);
348 static void sdio_uart_stop_rx(struct sdio_uart_port
*port
)
350 port
->ier
&= ~UART_IER_RLSI
;
351 port
->read_status_mask
&= ~UART_LSR_DR
;
352 sdio_out(port
, UART_IER
, port
->ier
);
355 static void sdio_uart_receive_chars(struct sdio_uart_port
*port
, u8
*status
)
360 u8 ch
= sdio_in(port
, UART_RX
);
361 u8 flag
= TTY_NORMAL
;
364 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
365 UART_LSR_FE
| UART_LSR_OE
))) {
367 * For statistics only
369 if (*status
& UART_LSR_BI
) {
370 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
372 } else if (*status
& UART_LSR_PE
)
373 port
->icount
.parity
++;
374 else if (*status
& UART_LSR_FE
)
375 port
->icount
.frame
++;
376 if (*status
& UART_LSR_OE
)
377 port
->icount
.overrun
++;
380 * Mask off conditions which should be ignored.
382 *status
&= port
->read_status_mask
;
383 if (*status
& UART_LSR_BI
)
385 else if (*status
& UART_LSR_PE
)
387 else if (*status
& UART_LSR_FE
)
391 if ((*status
& port
->ignore_status_mask
& ~UART_LSR_OE
) == 0)
392 tty_insert_flip_char(&port
->port
, ch
, flag
);
395 * Overrun is special. Since it's reported immediately,
396 * it doesn't affect the current character.
398 if (*status
& ~port
->ignore_status_mask
& UART_LSR_OE
)
399 tty_insert_flip_char(&port
->port
, 0, TTY_OVERRUN
);
401 *status
= sdio_in(port
, UART_LSR
);
402 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
404 tty_flip_buffer_push(&port
->port
);
407 static void sdio_uart_transmit_chars(struct sdio_uart_port
*port
)
409 struct kfifo
*xmit
= &port
->xmit_fifo
;
411 struct tty_struct
*tty
;
416 sdio_out(port
, UART_TX
, port
->x_char
);
422 tty
= tty_port_tty_get(&port
->port
);
424 if (tty
== NULL
|| !kfifo_len(xmit
) ||
425 tty
->flow
.stopped
|| tty
->hw_stopped
) {
426 sdio_uart_stop_tx(port
);
431 len
= kfifo_out_locked(xmit
, iobuf
, 16, &port
->write_lock
);
432 for (count
= 0; count
< len
; count
++) {
433 sdio_out(port
, UART_TX
, iobuf
[count
]);
437 len
= kfifo_len(xmit
);
438 if (len
< WAKEUP_CHARS
) {
441 sdio_uart_stop_tx(port
);
446 static void sdio_uart_check_modem_status(struct sdio_uart_port
*port
)
448 struct tty_struct
*tty
;
451 status
= sdio_in(port
, UART_MSR
);
453 if ((status
& UART_MSR_ANY_DELTA
) == 0)
456 if (status
& UART_MSR_TERI
)
458 if (status
& UART_MSR_DDSR
)
460 if (status
& UART_MSR_DDCD
) {
462 /* DCD raise - wake for open */
463 if (status
& UART_MSR_DCD
)
464 wake_up_interruptible(&port
->port
.open_wait
);
466 /* DCD drop - hang up if tty attached */
467 tty_port_tty_hangup(&port
->port
, false);
470 if (status
& UART_MSR_DCTS
) {
472 tty
= tty_port_tty_get(&port
->port
);
473 if (tty
&& C_CRTSCTS(tty
)) {
474 int cts
= (status
& UART_MSR_CTS
);
475 if (tty
->hw_stopped
) {
477 tty
->hw_stopped
= false;
478 sdio_uart_start_tx(port
);
483 tty
->hw_stopped
= true;
484 sdio_uart_stop_tx(port
);
493 * This handles the interrupt from one port.
495 static void sdio_uart_irq(struct sdio_func
*func
)
497 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
501 * In a few places sdio_uart_irq() is called directly instead of
502 * waiting for the actual interrupt to be raised and the SDIO IRQ
503 * thread scheduled in order to reduce latency. However, some
504 * interaction with the tty core may end up calling us back
505 * (serial echo, flow control, etc.) through those same places
506 * causing undesirable effects. Let's stop the recursion here.
508 if (unlikely(port
->in_sdio_uart_irq
== current
))
511 iir
= sdio_in(port
, UART_IIR
);
512 if (iir
& UART_IIR_NO_INT
)
515 port
->in_sdio_uart_irq
= current
;
516 lsr
= sdio_in(port
, UART_LSR
);
517 if (lsr
& UART_LSR_DR
)
518 sdio_uart_receive_chars(port
, &lsr
);
519 sdio_uart_check_modem_status(port
);
520 if (lsr
& UART_LSR_THRE
)
521 sdio_uart_transmit_chars(port
);
522 port
->in_sdio_uart_irq
= NULL
;
525 static bool uart_carrier_raised(struct tty_port
*tport
)
527 struct sdio_uart_port
*port
=
528 container_of(tport
, struct sdio_uart_port
, port
);
529 unsigned int ret
= sdio_uart_claim_func(port
);
530 if (ret
) /* Missing hardware shouldn't block for carrier */
532 ret
= sdio_uart_get_mctrl(port
);
533 sdio_uart_release_func(port
);
535 return ret
& TIOCM_CAR
;
539 * uart_dtr_rts - port helper to set uart signals
540 * @tport: tty port to be updated
541 * @active: set to turn on DTR/RTS
543 * Called by the tty port helpers when the modem signals need to be
544 * adjusted during an open, close and hangup.
547 static void uart_dtr_rts(struct tty_port
*tport
, bool active
)
549 struct sdio_uart_port
*port
=
550 container_of(tport
, struct sdio_uart_port
, port
);
551 int ret
= sdio_uart_claim_func(port
);
555 sdio_uart_clear_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
557 sdio_uart_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
558 sdio_uart_release_func(port
);
562 * sdio_uart_activate - start up hardware
563 * @tport: tty port to activate
564 * @tty: tty bound to this port
566 * Activate a tty port. The port locking guarantees us this will be
567 * run exactly once per set of opens, and if successful will see the
568 * shutdown method run exactly once to match. Start up and shutdown are
569 * protected from each other by the internal locking and will not run
570 * at the same time even during a hangup event.
572 * If we successfully start up the port we take an extra kref as we
573 * will keep it around until shutdown when the kref is dropped.
576 static int sdio_uart_activate(struct tty_port
*tport
, struct tty_struct
*tty
)
578 struct sdio_uart_port
*port
=
579 container_of(tport
, struct sdio_uart_port
, port
);
583 * Set the TTY IO error marker - we will only clear this
584 * once we have successfully opened the port.
586 set_bit(TTY_IO_ERROR
, &tty
->flags
);
588 kfifo_reset(&port
->xmit_fifo
);
590 ret
= sdio_uart_claim_func(port
);
593 ret
= sdio_enable_func(port
->func
);
596 ret
= sdio_claim_irq(port
->func
, sdio_uart_irq
);
601 * Clear the FIFO buffers and disable them.
602 * (they will be reenabled in sdio_change_speed())
604 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
605 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
606 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
607 sdio_out(port
, UART_FCR
, 0);
610 * Clear the interrupt registers.
612 (void) sdio_in(port
, UART_LSR
);
613 (void) sdio_in(port
, UART_RX
);
614 (void) sdio_in(port
, UART_IIR
);
615 (void) sdio_in(port
, UART_MSR
);
618 * Now, initialize the UART
620 sdio_out(port
, UART_LCR
, UART_LCR_WLEN8
);
622 port
->ier
= UART_IER_RLSI
|UART_IER_RDI
|UART_IER_RTOIE
|UART_IER_UUE
;
623 port
->mctrl
= TIOCM_OUT2
;
625 sdio_uart_change_speed(port
, &tty
->termios
, NULL
);
628 sdio_uart_set_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
631 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
))
632 tty
->hw_stopped
= true;
634 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
636 /* Kick the IRQ handler once while we're still holding the host lock */
637 sdio_uart_irq(port
->func
);
639 sdio_uart_release_func(port
);
643 sdio_disable_func(port
->func
);
645 sdio_uart_release_func(port
);
650 * sdio_uart_shutdown - stop hardware
651 * @tport: tty port to shut down
653 * Deactivate a tty port. The port locking guarantees us this will be
654 * run only if a successful matching activate already ran. The two are
655 * protected from each other by the internal locking and will not run
656 * at the same time even during a hangup event.
659 static void sdio_uart_shutdown(struct tty_port
*tport
)
661 struct sdio_uart_port
*port
=
662 container_of(tport
, struct sdio_uart_port
, port
);
665 ret
= sdio_uart_claim_func(port
);
669 sdio_uart_stop_rx(port
);
671 /* Disable interrupts from this port */
672 sdio_release_irq(port
->func
);
674 sdio_out(port
, UART_IER
, 0);
676 sdio_uart_clear_mctrl(port
, TIOCM_OUT2
);
678 /* Disable break condition and FIFOs. */
679 port
->lcr
&= ~UART_LCR_SBC
;
680 sdio_out(port
, UART_LCR
, port
->lcr
);
681 sdio_out(port
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
682 UART_FCR_CLEAR_RCVR
|
683 UART_FCR_CLEAR_XMIT
);
684 sdio_out(port
, UART_FCR
, 0);
686 sdio_disable_func(port
->func
);
688 sdio_uart_release_func(port
);
691 static void sdio_uart_port_destroy(struct tty_port
*tport
)
693 struct sdio_uart_port
*port
=
694 container_of(tport
, struct sdio_uart_port
, port
);
695 kfifo_free(&port
->xmit_fifo
);
700 * sdio_uart_install - install method
701 * @driver: the driver in use (sdio_uart in our case)
702 * @tty: the tty being bound
704 * Look up and bind the tty and the driver together. Initialize
705 * any needed private data (in our case the termios)
708 static int sdio_uart_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
710 int idx
= tty
->index
;
711 struct sdio_uart_port
*port
= sdio_uart_port_get(idx
);
712 int ret
= tty_standard_install(driver
, tty
);
715 /* This is the ref sdio_uart_port get provided */
716 tty
->driver_data
= port
;
718 sdio_uart_port_put(port
);
723 * sdio_uart_cleanup - called on the last tty kref drop
724 * @tty: the tty being destroyed
726 * Called asynchronously when the last reference to the tty is dropped.
727 * We cannot destroy the tty->driver_data port kref until this point
730 static void sdio_uart_cleanup(struct tty_struct
*tty
)
732 struct sdio_uart_port
*port
= tty
->driver_data
;
733 tty
->driver_data
= NULL
; /* Bug trap */
734 sdio_uart_port_put(port
);
738 * Open/close/hangup is now entirely boilerplate
741 static int sdio_uart_open(struct tty_struct
*tty
, struct file
*filp
)
743 struct sdio_uart_port
*port
= tty
->driver_data
;
744 return tty_port_open(&port
->port
, tty
, filp
);
747 static void sdio_uart_close(struct tty_struct
*tty
, struct file
* filp
)
749 struct sdio_uart_port
*port
= tty
->driver_data
;
750 tty_port_close(&port
->port
, tty
, filp
);
753 static void sdio_uart_hangup(struct tty_struct
*tty
)
755 struct sdio_uart_port
*port
= tty
->driver_data
;
756 tty_port_hangup(&port
->port
);
759 static ssize_t
sdio_uart_write(struct tty_struct
*tty
, const u8
*buf
,
762 struct sdio_uart_port
*port
= tty
->driver_data
;
768 ret
= kfifo_in_locked(&port
->xmit_fifo
, buf
, count
, &port
->write_lock
);
769 if (!(port
->ier
& UART_IER_THRI
)) {
770 int err
= sdio_uart_claim_func(port
);
772 sdio_uart_start_tx(port
);
773 sdio_uart_irq(port
->func
);
774 sdio_uart_release_func(port
);
782 static unsigned int sdio_uart_write_room(struct tty_struct
*tty
)
784 struct sdio_uart_port
*port
= tty
->driver_data
;
785 return FIFO_SIZE
- kfifo_len(&port
->xmit_fifo
);
788 static unsigned int sdio_uart_chars_in_buffer(struct tty_struct
*tty
)
790 struct sdio_uart_port
*port
= tty
->driver_data
;
791 return kfifo_len(&port
->xmit_fifo
);
794 static void sdio_uart_send_xchar(struct tty_struct
*tty
, u8 ch
)
796 struct sdio_uart_port
*port
= tty
->driver_data
;
799 if (ch
&& !(port
->ier
& UART_IER_THRI
)) {
800 if (sdio_uart_claim_func(port
) != 0)
802 sdio_uart_start_tx(port
);
803 sdio_uart_irq(port
->func
);
804 sdio_uart_release_func(port
);
808 static void sdio_uart_throttle(struct tty_struct
*tty
)
810 struct sdio_uart_port
*port
= tty
->driver_data
;
812 if (!I_IXOFF(tty
) && !C_CRTSCTS(tty
))
815 if (sdio_uart_claim_func(port
) != 0)
819 port
->x_char
= STOP_CHAR(tty
);
820 sdio_uart_start_tx(port
);
824 sdio_uart_clear_mctrl(port
, TIOCM_RTS
);
826 sdio_uart_irq(port
->func
);
827 sdio_uart_release_func(port
);
830 static void sdio_uart_unthrottle(struct tty_struct
*tty
)
832 struct sdio_uart_port
*port
= tty
->driver_data
;
834 if (!I_IXOFF(tty
) && !C_CRTSCTS(tty
))
837 if (sdio_uart_claim_func(port
) != 0)
844 port
->x_char
= START_CHAR(tty
);
845 sdio_uart_start_tx(port
);
850 sdio_uart_set_mctrl(port
, TIOCM_RTS
);
852 sdio_uart_irq(port
->func
);
853 sdio_uart_release_func(port
);
856 static void sdio_uart_set_termios(struct tty_struct
*tty
,
857 const struct ktermios
*old_termios
)
859 struct sdio_uart_port
*port
= tty
->driver_data
;
860 unsigned int cflag
= tty
->termios
.c_cflag
;
862 if (sdio_uart_claim_func(port
) != 0)
865 sdio_uart_change_speed(port
, &tty
->termios
, old_termios
);
867 /* Handle transition to B0 status */
868 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
869 sdio_uart_clear_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
871 /* Handle transition away from B0 status */
872 if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
873 unsigned int mask
= TIOCM_DTR
;
874 if (!(cflag
& CRTSCTS
) || !tty_throttled(tty
))
876 sdio_uart_set_mctrl(port
, mask
);
879 /* Handle turning off CRTSCTS */
880 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
881 tty
->hw_stopped
= false;
882 sdio_uart_start_tx(port
);
885 /* Handle turning on CRTSCTS */
886 if (!(old_termios
->c_cflag
& CRTSCTS
) && (cflag
& CRTSCTS
)) {
887 if (!(sdio_uart_get_mctrl(port
) & TIOCM_CTS
)) {
888 tty
->hw_stopped
= true;
889 sdio_uart_stop_tx(port
);
893 sdio_uart_release_func(port
);
896 static int sdio_uart_break_ctl(struct tty_struct
*tty
, int break_state
)
898 struct sdio_uart_port
*port
= tty
->driver_data
;
901 result
= sdio_uart_claim_func(port
);
905 if (break_state
== -1)
906 port
->lcr
|= UART_LCR_SBC
;
908 port
->lcr
&= ~UART_LCR_SBC
;
909 sdio_out(port
, UART_LCR
, port
->lcr
);
911 sdio_uart_release_func(port
);
915 static int sdio_uart_tiocmget(struct tty_struct
*tty
)
917 struct sdio_uart_port
*port
= tty
->driver_data
;
920 result
= sdio_uart_claim_func(port
);
922 result
= port
->mctrl
| sdio_uart_get_mctrl(port
);
923 sdio_uart_release_func(port
);
929 static int sdio_uart_tiocmset(struct tty_struct
*tty
,
930 unsigned int set
, unsigned int clear
)
932 struct sdio_uart_port
*port
= tty
->driver_data
;
935 result
= sdio_uart_claim_func(port
);
937 sdio_uart_update_mctrl(port
, set
, clear
);
938 sdio_uart_release_func(port
);
944 static int sdio_uart_proc_show(struct seq_file
*m
, void *v
)
948 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
950 for (i
= 0; i
< UART_NR
; i
++) {
951 struct sdio_uart_port
*port
= sdio_uart_port_get(i
);
953 seq_printf(m
, "%d: uart:SDIO", i
);
954 if (capable(CAP_SYS_ADMIN
)) {
955 seq_printf(m
, " tx:%d rx:%d",
956 port
->icount
.tx
, port
->icount
.rx
);
957 if (port
->icount
.frame
)
958 seq_printf(m
, " fe:%d",
960 if (port
->icount
.parity
)
961 seq_printf(m
, " pe:%d",
962 port
->icount
.parity
);
963 if (port
->icount
.brk
)
964 seq_printf(m
, " brk:%d",
966 if (port
->icount
.overrun
)
967 seq_printf(m
, " oe:%d",
968 port
->icount
.overrun
);
969 if (port
->icount
.cts
)
970 seq_printf(m
, " cts:%d",
972 if (port
->icount
.dsr
)
973 seq_printf(m
, " dsr:%d",
975 if (port
->icount
.rng
)
976 seq_printf(m
, " rng:%d",
978 if (port
->icount
.dcd
)
979 seq_printf(m
, " dcd:%d",
982 sdio_uart_port_put(port
);
989 static const struct tty_port_operations sdio_uart_port_ops
= {
990 .dtr_rts
= uart_dtr_rts
,
991 .carrier_raised
= uart_carrier_raised
,
992 .shutdown
= sdio_uart_shutdown
,
993 .activate
= sdio_uart_activate
,
994 .destruct
= sdio_uart_port_destroy
,
997 static const struct tty_operations sdio_uart_ops
= {
998 .open
= sdio_uart_open
,
999 .close
= sdio_uart_close
,
1000 .write
= sdio_uart_write
,
1001 .write_room
= sdio_uart_write_room
,
1002 .chars_in_buffer
= sdio_uart_chars_in_buffer
,
1003 .send_xchar
= sdio_uart_send_xchar
,
1004 .throttle
= sdio_uart_throttle
,
1005 .unthrottle
= sdio_uart_unthrottle
,
1006 .set_termios
= sdio_uart_set_termios
,
1007 .hangup
= sdio_uart_hangup
,
1008 .break_ctl
= sdio_uart_break_ctl
,
1009 .tiocmget
= sdio_uart_tiocmget
,
1010 .tiocmset
= sdio_uart_tiocmset
,
1011 .install
= sdio_uart_install
,
1012 .cleanup
= sdio_uart_cleanup
,
1013 .proc_show
= sdio_uart_proc_show
,
1016 static struct tty_driver
*sdio_uart_tty_driver
;
1018 static int sdio_uart_probe(struct sdio_func
*func
,
1019 const struct sdio_device_id
*id
)
1021 struct sdio_uart_port
*port
;
1024 port
= kzalloc(sizeof(struct sdio_uart_port
), GFP_KERNEL
);
1028 if (func
->class == SDIO_CLASS_UART
) {
1029 pr_warn("%s: need info on UART class basic setup\n",
1030 sdio_func_id(func
));
1033 } else if (func
->class == SDIO_CLASS_GPS
) {
1035 * We need tuple 0x91. It contains SUBTPL_SIOREG
1036 * and SUBTPL_RCVCAPS.
1038 struct sdio_func_tuple
*tpl
;
1039 for (tpl
= func
->tuples
; tpl
; tpl
= tpl
->next
) {
1040 if (tpl
->code
!= 0x91)
1044 if (tpl
->data
[1] == 0) /* SUBTPL_SIOREG */
1048 pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1049 sdio_func_id(func
));
1053 pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1054 sdio_func_id(func
), tpl
->data
[2], tpl
->data
[3]);
1055 port
->regs_offset
= (tpl
->data
[4] << 0) |
1056 (tpl
->data
[5] << 8) |
1057 (tpl
->data
[6] << 16);
1058 pr_debug("%s: regs offset = 0x%x\n",
1059 sdio_func_id(func
), port
->regs_offset
);
1060 port
->uartclk
= tpl
->data
[7] * 115200;
1061 if (port
->uartclk
== 0)
1062 port
->uartclk
= 115200;
1063 pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
1064 sdio_func_id(func
), port
->uartclk
,
1065 tpl
->data
[7], tpl
->data
[8] | (tpl
->data
[9] << 8));
1072 sdio_set_drvdata(func
, port
);
1073 tty_port_init(&port
->port
);
1074 port
->port
.ops
= &sdio_uart_port_ops
;
1076 ret
= sdio_uart_add_port(port
);
1081 dev
= tty_port_register_device(&port
->port
,
1082 sdio_uart_tty_driver
, port
->index
, &func
->dev
);
1084 sdio_uart_port_remove(port
);
1092 static void sdio_uart_remove(struct sdio_func
*func
)
1094 struct sdio_uart_port
*port
= sdio_get_drvdata(func
);
1096 tty_unregister_device(sdio_uart_tty_driver
, port
->index
);
1097 sdio_uart_port_remove(port
);
1100 static const struct sdio_device_id sdio_uart_ids
[] = {
1101 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART
) },
1102 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS
) },
1103 { /* end: all zeroes */ },
1106 MODULE_DEVICE_TABLE(sdio
, sdio_uart_ids
);
1108 static struct sdio_driver sdio_uart_driver
= {
1109 .probe
= sdio_uart_probe
,
1110 .remove
= sdio_uart_remove
,
1111 .name
= "sdio_uart",
1112 .id_table
= sdio_uart_ids
,
1115 static int __init
sdio_uart_init(void)
1118 struct tty_driver
*tty_drv
;
1120 sdio_uart_tty_driver
= tty_drv
= tty_alloc_driver(UART_NR
,
1121 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
);
1122 if (IS_ERR(tty_drv
))
1123 return PTR_ERR(tty_drv
);
1125 tty_drv
->driver_name
= "sdio_uart";
1126 tty_drv
->name
= "ttySDIO";
1127 tty_drv
->major
= 0; /* dynamically allocated */
1128 tty_drv
->minor_start
= 0;
1129 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1130 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1131 tty_drv
->init_termios
= tty_std_termios
;
1132 tty_drv
->init_termios
.c_cflag
= B4800
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1133 tty_drv
->init_termios
.c_ispeed
= 4800;
1134 tty_drv
->init_termios
.c_ospeed
= 4800;
1135 tty_set_operations(tty_drv
, &sdio_uart_ops
);
1137 ret
= tty_register_driver(tty_drv
);
1141 ret
= sdio_register_driver(&sdio_uart_driver
);
1148 tty_unregister_driver(tty_drv
);
1150 tty_driver_kref_put(tty_drv
);
1154 static void __exit
sdio_uart_exit(void)
1156 sdio_unregister_driver(&sdio_uart_driver
);
1157 tty_unregister_driver(sdio_uart_tty_driver
);
1158 tty_driver_kref_put(sdio_uart_tty_driver
);
1161 module_init(sdio_uart_init
);
1162 module_exit(sdio_uart_exit
);
1164 MODULE_AUTHOR("Nicolas Pitre");
1165 MODULE_DESCRIPTION("SDIO UART/GPS driver");
1166 MODULE_LICENSE("GPL");