1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * SDIO UART/GPS driver
4 *
5 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
6 * by Russell King.
7 *
8 * Author: Nicolas Pitre
9 * Created: June 15, 2007
10 * Copyright: MontaVista Software, Inc.
11 */
12
13 /*
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.
19 *
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.
23 */
24
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>
38
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>
43
44
45 #define UART_NR 8 /* Number of UARTs this driver can handle */
46
47
48 #define FIFO_SIZE PAGE_SIZE
49 #define WAKEUP_CHARS 256
50
51 struct uart_icount {
52 __u32 cts;
53 __u32 dsr;
54 __u32 rng;
55 __u32 dcd;
56 __u32 rx;
57 __u32 tx;
58 __u32 frame;
59 __u32 overrun;
60 __u32 parity;
61 __u32 brk;
62 };
63
64 struct sdio_uart_port {
65 struct tty_port port;
66 unsigned int index;
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;
74 unsigned int uartclk;
75 unsigned int mctrl;
76 unsigned int rx_mctrl;
77 unsigned int read_status_mask;
78 unsigned int ignore_status_mask;
79 unsigned char x_char;
80 unsigned char ier;
81 unsigned char lcr;
82 };
83
84 static struct sdio_uart_port *sdio_uart_table[UART_NR];
85 static DEFINE_SPINLOCK(sdio_uart_table_lock);
86
sdio_uart_add_port(struct sdio_uart_port * port)87 static int sdio_uart_add_port(struct sdio_uart_port *port)
88 {
89 int index, ret = -EBUSY;
90
91 mutex_init(&port->func_lock);
92 spin_lock_init(&port->write_lock);
93 if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
94 return -ENOMEM;
95
96 spin_lock(&sdio_uart_table_lock);
97 for (index = 0; index < UART_NR; index++) {
98 if (!sdio_uart_table[index]) {
99 port->index = index;
100 sdio_uart_table[index] = port;
101 ret = 0;
102 break;
103 }
104 }
105 spin_unlock(&sdio_uart_table_lock);
106
107 if (ret)
108 kfifo_free(&port->xmit_fifo);
109
110 return ret;
111 }
112
sdio_uart_port_get(unsigned index)113 static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
114 {
115 struct sdio_uart_port *port;
116
117 if (index >= UART_NR)
118 return NULL;
119
120 spin_lock(&sdio_uart_table_lock);
121 port = sdio_uart_table[index];
122 if (port)
123 tty_port_get(&port->port);
124 spin_unlock(&sdio_uart_table_lock);
125
126 return port;
127 }
128
sdio_uart_port_put(struct sdio_uart_port * port)129 static void sdio_uart_port_put(struct sdio_uart_port *port)
130 {
131 tty_port_put(&port->port);
132 }
133
sdio_uart_port_remove(struct sdio_uart_port * port)134 static void sdio_uart_port_remove(struct sdio_uart_port *port)
135 {
136 struct sdio_func *func;
137
138 spin_lock(&sdio_uart_table_lock);
139 sdio_uart_table[port->index] = NULL;
140 spin_unlock(&sdio_uart_table_lock);
141
142 /*
143 * We're killing a port that potentially still is in use by
144 * the tty layer. Be careful to prevent any further access
145 * to the SDIO function and arrange for the tty layer to
146 * give up on that port ASAP.
147 * Beware: the lock ordering is critical.
148 */
149 mutex_lock(&port->port.mutex);
150 mutex_lock(&port->func_lock);
151 func = port->func;
152 sdio_claim_host(func);
153 port->func = NULL;
154 mutex_unlock(&port->func_lock);
155 /* tty_hangup is async so is this safe as is ?? */
156 tty_port_tty_hangup(&port->port, false);
157 mutex_unlock(&port->port.mutex);
158 sdio_release_irq(func);
159 sdio_disable_func(func);
160 sdio_release_host(func);
161
162 sdio_uart_port_put(port);
163 }
164
sdio_uart_claim_func(struct sdio_uart_port * port)165 static int sdio_uart_claim_func(struct sdio_uart_port *port)
166 {
167 mutex_lock(&port->func_lock);
168 if (unlikely(!port->func)) {
169 mutex_unlock(&port->func_lock);
170 return -ENODEV;
171 }
172 if (likely(port->in_sdio_uart_irq != current))
173 sdio_claim_host(port->func);
174 mutex_unlock(&port->func_lock);
175 return 0;
176 }
177
sdio_uart_release_func(struct sdio_uart_port * port)178 static inline void sdio_uart_release_func(struct sdio_uart_port *port)
179 {
180 if (likely(port->in_sdio_uart_irq != current))
181 sdio_release_host(port->func);
182 }
183
sdio_in(struct sdio_uart_port * port,int offset)184 static inline u8 sdio_in(struct sdio_uart_port *port, int offset)
185 {
186 return sdio_readb(port->func, port->regs_offset + offset, NULL);
187 }
188
sdio_out(struct sdio_uart_port * port,int offset,int value)189 static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
190 {
191 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
192 }
193
sdio_uart_get_mctrl(struct sdio_uart_port * port)194 static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
195 {
196 unsigned int ret;
197 u8 status;
198
199 /* FIXME: What stops this losing the delta bits and breaking
200 sdio_uart_check_modem_status ? */
201 status = sdio_in(port, UART_MSR);
202
203 ret = 0;
204 if (status & UART_MSR_DCD)
205 ret |= TIOCM_CAR;
206 if (status & UART_MSR_RI)
207 ret |= TIOCM_RNG;
208 if (status & UART_MSR_DSR)
209 ret |= TIOCM_DSR;
210 if (status & UART_MSR_CTS)
211 ret |= TIOCM_CTS;
212 return ret;
213 }
214
sdio_uart_write_mctrl(struct sdio_uart_port * port,unsigned int mctrl)215 static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
216 unsigned int mctrl)
217 {
218 unsigned char mcr = 0;
219
220 if (mctrl & TIOCM_RTS)
221 mcr |= UART_MCR_RTS;
222 if (mctrl & TIOCM_DTR)
223 mcr |= UART_MCR_DTR;
224 if (mctrl & TIOCM_OUT1)
225 mcr |= UART_MCR_OUT1;
226 if (mctrl & TIOCM_OUT2)
227 mcr |= UART_MCR_OUT2;
228 if (mctrl & TIOCM_LOOP)
229 mcr |= UART_MCR_LOOP;
230
231 sdio_out(port, UART_MCR, mcr);
232 }
233
sdio_uart_update_mctrl(struct sdio_uart_port * port,unsigned int set,unsigned int clear)234 static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
235 unsigned int set, unsigned int clear)
236 {
237 unsigned int old;
238
239 old = port->mctrl;
240 port->mctrl = (old & ~clear) | set;
241 if (old != port->mctrl)
242 sdio_uart_write_mctrl(port, port->mctrl);
243 }
244
245 #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
246 #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
247
sdio_uart_change_speed(struct sdio_uart_port * port,struct ktermios * termios,const struct ktermios * old)248 static void sdio_uart_change_speed(struct sdio_uart_port *port,
249 struct ktermios *termios,
250 const struct ktermios *old)
251 {
252 unsigned char cval, fcr = 0;
253 unsigned int baud, quot;
254
255 cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
256
257 if (termios->c_cflag & CSTOPB)
258 cval |= UART_LCR_STOP;
259 if (termios->c_cflag & PARENB)
260 cval |= UART_LCR_PARITY;
261 if (!(termios->c_cflag & PARODD))
262 cval |= UART_LCR_EPAR;
263
264 for (;;) {
265 baud = tty_termios_baud_rate(termios);
266 if (baud == 0)
267 baud = 9600; /* Special case: B0 rate. */
268 if (baud <= port->uartclk)
269 break;
270 /*
271 * Oops, the quotient was zero. Try again with the old
272 * baud rate if possible, otherwise default to 9600.
273 */
274 termios->c_cflag &= ~CBAUD;
275 if (old) {
276 termios->c_cflag |= old->c_cflag & CBAUD;
277 old = NULL;
278 } else
279 termios->c_cflag |= B9600;
280 }
281 quot = (2 * port->uartclk + baud) / (2 * baud);
282
283 if (baud < 2400)
284 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
285 else
286 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
287
288 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
289 if (termios->c_iflag & INPCK)
290 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
291 if (termios->c_iflag & (BRKINT | PARMRK))
292 port->read_status_mask |= UART_LSR_BI;
293
294 /*
295 * Characters to ignore
296 */
297 port->ignore_status_mask = 0;
298 if (termios->c_iflag & IGNPAR)
299 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
300 if (termios->c_iflag & IGNBRK) {
301 port->ignore_status_mask |= UART_LSR_BI;
302 /*
303 * If we're ignoring parity and break indicators,
304 * ignore overruns too (for real raw support).
305 */
306 if (termios->c_iflag & IGNPAR)
307 port->ignore_status_mask |= UART_LSR_OE;
308 }
309
310 /*
311 * ignore all characters if CREAD is not set
312 */
313 if ((termios->c_cflag & CREAD) == 0)
314 port->ignore_status_mask |= UART_LSR_DR;
315
316 /*
317 * CTS flow control flag and modem status interrupts
318 */
319 port->ier &= ~UART_IER_MSI;
320 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
321 port->ier |= UART_IER_MSI;
322
323 port->lcr = cval;
324
325 sdio_out(port, UART_IER, port->ier);
326 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
327 sdio_out(port, UART_DLL, quot & 0xff);
328 sdio_out(port, UART_DLM, quot >> 8);
329 sdio_out(port, UART_LCR, cval);
330 sdio_out(port, UART_FCR, fcr);
331
332 sdio_uart_write_mctrl(port, port->mctrl);
333 }
334
sdio_uart_start_tx(struct sdio_uart_port * port)335 static void sdio_uart_start_tx(struct sdio_uart_port *port)
336 {
337 if (!(port->ier & UART_IER_THRI)) {
338 port->ier |= UART_IER_THRI;
339 sdio_out(port, UART_IER, port->ier);
340 }
341 }
342
sdio_uart_stop_tx(struct sdio_uart_port * port)343 static void sdio_uart_stop_tx(struct sdio_uart_port *port)
344 {
345 if (port->ier & UART_IER_THRI) {
346 port->ier &= ~UART_IER_THRI;
347 sdio_out(port, UART_IER, port->ier);
348 }
349 }
350
sdio_uart_stop_rx(struct sdio_uart_port * port)351 static void sdio_uart_stop_rx(struct sdio_uart_port *port)
352 {
353 port->ier &= ~UART_IER_RLSI;
354 port->read_status_mask &= ~UART_LSR_DR;
355 sdio_out(port, UART_IER, port->ier);
356 }
357
sdio_uart_receive_chars(struct sdio_uart_port * port,u8 * status)358 static void sdio_uart_receive_chars(struct sdio_uart_port *port, u8 *status)
359 {
360 int max_count = 256;
361
362 do {
363 u8 ch = sdio_in(port, UART_RX);
364 u8 flag = TTY_NORMAL;
365 port->icount.rx++;
366
367 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
368 UART_LSR_FE | UART_LSR_OE))) {
369 /*
370 * For statistics only
371 */
372 if (*status & UART_LSR_BI) {
373 *status &= ~(UART_LSR_FE | UART_LSR_PE);
374 port->icount.brk++;
375 } else if (*status & UART_LSR_PE)
376 port->icount.parity++;
377 else if (*status & UART_LSR_FE)
378 port->icount.frame++;
379 if (*status & UART_LSR_OE)
380 port->icount.overrun++;
381
382 /*
383 * Mask off conditions which should be ignored.
384 */
385 *status &= port->read_status_mask;
386 if (*status & UART_LSR_BI)
387 flag = TTY_BREAK;
388 else if (*status & UART_LSR_PE)
389 flag = TTY_PARITY;
390 else if (*status & UART_LSR_FE)
391 flag = TTY_FRAME;
392 }
393
394 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
395 tty_insert_flip_char(&port->port, ch, flag);
396
397 /*
398 * Overrun is special. Since it's reported immediately,
399 * it doesn't affect the current character.
400 */
401 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
402 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
403
404 *status = sdio_in(port, UART_LSR);
405 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
406
407 tty_flip_buffer_push(&port->port);
408 }
409
sdio_uart_transmit_chars(struct sdio_uart_port * port)410 static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
411 {
412 struct kfifo *xmit = &port->xmit_fifo;
413 int count;
414 struct tty_struct *tty;
415 u8 iobuf[16];
416 int len;
417
418 if (port->x_char) {
419 sdio_out(port, UART_TX, port->x_char);
420 port->icount.tx++;
421 port->x_char = 0;
422 return;
423 }
424
425 tty = tty_port_tty_get(&port->port);
426
427 if (tty == NULL || !kfifo_len(xmit) ||
428 tty->flow.stopped || tty->hw_stopped) {
429 sdio_uart_stop_tx(port);
430 tty_kref_put(tty);
431 return;
432 }
433
434 len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
435 for (count = 0; count < len; count++) {
436 sdio_out(port, UART_TX, iobuf[count]);
437 port->icount.tx++;
438 }
439
440 len = kfifo_len(xmit);
441 if (len < WAKEUP_CHARS) {
442 tty_wakeup(tty);
443 if (len == 0)
444 sdio_uart_stop_tx(port);
445 }
446 tty_kref_put(tty);
447 }
448
sdio_uart_check_modem_status(struct sdio_uart_port * port)449 static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
450 {
451 struct tty_struct *tty;
452 u8 status;
453
454 status = sdio_in(port, UART_MSR);
455
456 if ((status & UART_MSR_ANY_DELTA) == 0)
457 return;
458
459 if (status & UART_MSR_TERI)
460 port->icount.rng++;
461 if (status & UART_MSR_DDSR)
462 port->icount.dsr++;
463 if (status & UART_MSR_DDCD) {
464 port->icount.dcd++;
465 /* DCD raise - wake for open */
466 if (status & UART_MSR_DCD)
467 wake_up_interruptible(&port->port.open_wait);
468 else {
469 /* DCD drop - hang up if tty attached */
470 tty_port_tty_hangup(&port->port, false);
471 }
472 }
473 if (status & UART_MSR_DCTS) {
474 port->icount.cts++;
475 tty = tty_port_tty_get(&port->port);
476 if (tty && C_CRTSCTS(tty)) {
477 bool cts = status & UART_MSR_CTS;
478 if (tty->hw_stopped) {
479 if (cts) {
480 tty->hw_stopped = false;
481 sdio_uart_start_tx(port);
482 tty_wakeup(tty);
483 }
484 } else {
485 if (!cts) {
486 tty->hw_stopped = true;
487 sdio_uart_stop_tx(port);
488 }
489 }
490 }
491 tty_kref_put(tty);
492 }
493 }
494
495 /*
496 * This handles the interrupt from one port.
497 */
sdio_uart_irq(struct sdio_func * func)498 static void sdio_uart_irq(struct sdio_func *func)
499 {
500 struct sdio_uart_port *port = sdio_get_drvdata(func);
501 u8 iir, lsr;
502
503 /*
504 * In a few places sdio_uart_irq() is called directly instead of
505 * waiting for the actual interrupt to be raised and the SDIO IRQ
506 * thread scheduled in order to reduce latency. However, some
507 * interaction with the tty core may end up calling us back
508 * (serial echo, flow control, etc.) through those same places
509 * causing undesirable effects. Let's stop the recursion here.
510 */
511 if (unlikely(port->in_sdio_uart_irq == current))
512 return;
513
514 iir = sdio_in(port, UART_IIR);
515 if (iir & UART_IIR_NO_INT)
516 return;
517
518 port->in_sdio_uart_irq = current;
519 lsr = sdio_in(port, UART_LSR);
520 if (lsr & UART_LSR_DR)
521 sdio_uart_receive_chars(port, &lsr);
522 sdio_uart_check_modem_status(port);
523 if (lsr & UART_LSR_THRE)
524 sdio_uart_transmit_chars(port);
525 port->in_sdio_uart_irq = NULL;
526 }
527
uart_carrier_raised(struct tty_port * tport)528 static bool uart_carrier_raised(struct tty_port *tport)
529 {
530 struct sdio_uart_port *port =
531 container_of(tport, struct sdio_uart_port, port);
532 unsigned int ret = sdio_uart_claim_func(port);
533 if (ret) /* Missing hardware shouldn't block for carrier */
534 return 1;
535 ret = sdio_uart_get_mctrl(port);
536 sdio_uart_release_func(port);
537
538 return ret & TIOCM_CAR;
539 }
540
541 /**
542 * uart_dtr_rts - port helper to set uart signals
543 * @tport: tty port to be updated
544 * @active: set to turn on DTR/RTS
545 *
546 * Called by the tty port helpers when the modem signals need to be
547 * adjusted during an open, close and hangup.
548 */
549
uart_dtr_rts(struct tty_port * tport,bool active)550 static void uart_dtr_rts(struct tty_port *tport, bool active)
551 {
552 struct sdio_uart_port *port =
553 container_of(tport, struct sdio_uart_port, port);
554 int ret = sdio_uart_claim_func(port);
555 if (ret)
556 return;
557 if (!active)
558 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
559 else
560 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
561 sdio_uart_release_func(port);
562 }
563
564 /**
565 * sdio_uart_activate - start up hardware
566 * @tport: tty port to activate
567 * @tty: tty bound to this port
568 *
569 * Activate a tty port. The port locking guarantees us this will be
570 * run exactly once per set of opens, and if successful will see the
571 * shutdown method run exactly once to match. Start up and shutdown are
572 * protected from each other by the internal locking and will not run
573 * at the same time even during a hangup event.
574 *
575 * If we successfully start up the port we take an extra kref as we
576 * will keep it around until shutdown when the kref is dropped.
577 */
578
sdio_uart_activate(struct tty_port * tport,struct tty_struct * tty)579 static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
580 {
581 struct sdio_uart_port *port =
582 container_of(tport, struct sdio_uart_port, port);
583 int ret;
584
585 /*
586 * Set the TTY IO error marker - we will only clear this
587 * once we have successfully opened the port.
588 */
589 set_bit(TTY_IO_ERROR, &tty->flags);
590
591 kfifo_reset(&port->xmit_fifo);
592
593 ret = sdio_uart_claim_func(port);
594 if (ret)
595 return ret;
596 ret = sdio_enable_func(port->func);
597 if (ret)
598 goto err1;
599 ret = sdio_claim_irq(port->func, sdio_uart_irq);
600 if (ret)
601 goto err2;
602
603 /*
604 * Clear the FIFO buffers and disable them.
605 * (they will be reenabled in sdio_change_speed())
606 */
607 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
608 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
609 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
610 sdio_out(port, UART_FCR, 0);
611
612 /*
613 * Clear the interrupt registers.
614 */
615 (void) sdio_in(port, UART_LSR);
616 (void) sdio_in(port, UART_RX);
617 (void) sdio_in(port, UART_IIR);
618 (void) sdio_in(port, UART_MSR);
619
620 /*
621 * Now, initialize the UART
622 */
623 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
624
625 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
626 port->mctrl = TIOCM_OUT2;
627
628 sdio_uart_change_speed(port, &tty->termios, NULL);
629
630 if (C_BAUD(tty))
631 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
632
633 if (C_CRTSCTS(tty))
634 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
635 tty->hw_stopped = true;
636
637 clear_bit(TTY_IO_ERROR, &tty->flags);
638
639 /* Kick the IRQ handler once while we're still holding the host lock */
640 sdio_uart_irq(port->func);
641
642 sdio_uart_release_func(port);
643 return 0;
644
645 err2:
646 sdio_disable_func(port->func);
647 err1:
648 sdio_uart_release_func(port);
649 return ret;
650 }
651
652 /**
653 * sdio_uart_shutdown - stop hardware
654 * @tport: tty port to shut down
655 *
656 * Deactivate a tty port. The port locking guarantees us this will be
657 * run only if a successful matching activate already ran. The two are
658 * protected from each other by the internal locking and will not run
659 * at the same time even during a hangup event.
660 */
661
sdio_uart_shutdown(struct tty_port * tport)662 static void sdio_uart_shutdown(struct tty_port *tport)
663 {
664 struct sdio_uart_port *port =
665 container_of(tport, struct sdio_uart_port, port);
666 int ret;
667
668 ret = sdio_uart_claim_func(port);
669 if (ret)
670 return;
671
672 sdio_uart_stop_rx(port);
673
674 /* Disable interrupts from this port */
675 sdio_release_irq(port->func);
676 port->ier = 0;
677 sdio_out(port, UART_IER, 0);
678
679 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
680
681 /* Disable break condition and FIFOs. */
682 port->lcr &= ~UART_LCR_SBC;
683 sdio_out(port, UART_LCR, port->lcr);
684 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
685 UART_FCR_CLEAR_RCVR |
686 UART_FCR_CLEAR_XMIT);
687 sdio_out(port, UART_FCR, 0);
688
689 sdio_disable_func(port->func);
690
691 sdio_uart_release_func(port);
692 }
693
sdio_uart_port_destroy(struct tty_port * tport)694 static void sdio_uart_port_destroy(struct tty_port *tport)
695 {
696 struct sdio_uart_port *port =
697 container_of(tport, struct sdio_uart_port, port);
698 kfifo_free(&port->xmit_fifo);
699 kfree(port);
700 }
701
702 /**
703 * sdio_uart_install - install method
704 * @driver: the driver in use (sdio_uart in our case)
705 * @tty: the tty being bound
706 *
707 * Look up and bind the tty and the driver together. Initialize
708 * any needed private data (in our case the termios)
709 */
710
sdio_uart_install(struct tty_driver * driver,struct tty_struct * tty)711 static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
712 {
713 int idx = tty->index;
714 struct sdio_uart_port *port = sdio_uart_port_get(idx);
715 int ret = tty_standard_install(driver, tty);
716
717 if (ret == 0)
718 /* This is the ref sdio_uart_port get provided */
719 tty->driver_data = port;
720 else
721 sdio_uart_port_put(port);
722 return ret;
723 }
724
725 /**
726 * sdio_uart_cleanup - called on the last tty kref drop
727 * @tty: the tty being destroyed
728 *
729 * Called asynchronously when the last reference to the tty is dropped.
730 * We cannot destroy the tty->driver_data port kref until this point
731 */
732
sdio_uart_cleanup(struct tty_struct * tty)733 static void sdio_uart_cleanup(struct tty_struct *tty)
734 {
735 struct sdio_uart_port *port = tty->driver_data;
736 tty->driver_data = NULL; /* Bug trap */
737 sdio_uart_port_put(port);
738 }
739
740 /*
741 * Open/close/hangup is now entirely boilerplate
742 */
743
sdio_uart_open(struct tty_struct * tty,struct file * filp)744 static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
745 {
746 struct sdio_uart_port *port = tty->driver_data;
747 return tty_port_open(&port->port, tty, filp);
748 }
749
sdio_uart_close(struct tty_struct * tty,struct file * filp)750 static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
751 {
752 struct sdio_uart_port *port = tty->driver_data;
753 tty_port_close(&port->port, tty, filp);
754 }
755
sdio_uart_hangup(struct tty_struct * tty)756 static void sdio_uart_hangup(struct tty_struct *tty)
757 {
758 struct sdio_uart_port *port = tty->driver_data;
759 tty_port_hangup(&port->port);
760 }
761
sdio_uart_write(struct tty_struct * tty,const u8 * buf,size_t count)762 static ssize_t sdio_uart_write(struct tty_struct *tty, const u8 *buf,
763 size_t count)
764 {
765 struct sdio_uart_port *port = tty->driver_data;
766 int ret;
767
768 if (!port->func)
769 return -ENODEV;
770
771 ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
772 if (!(port->ier & UART_IER_THRI)) {
773 int err = sdio_uart_claim_func(port);
774 if (!err) {
775 sdio_uart_start_tx(port);
776 sdio_uart_irq(port->func);
777 sdio_uart_release_func(port);
778 } else
779 ret = err;
780 }
781
782 return ret;
783 }
784
sdio_uart_write_room(struct tty_struct * tty)785 static unsigned int sdio_uart_write_room(struct tty_struct *tty)
786 {
787 struct sdio_uart_port *port = tty->driver_data;
788 return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
789 }
790
sdio_uart_chars_in_buffer(struct tty_struct * tty)791 static unsigned int sdio_uart_chars_in_buffer(struct tty_struct *tty)
792 {
793 struct sdio_uart_port *port = tty->driver_data;
794 return kfifo_len(&port->xmit_fifo);
795 }
796
sdio_uart_send_xchar(struct tty_struct * tty,u8 ch)797 static void sdio_uart_send_xchar(struct tty_struct *tty, u8 ch)
798 {
799 struct sdio_uart_port *port = tty->driver_data;
800
801 port->x_char = ch;
802 if (ch && !(port->ier & UART_IER_THRI)) {
803 if (sdio_uart_claim_func(port) != 0)
804 return;
805 sdio_uart_start_tx(port);
806 sdio_uart_irq(port->func);
807 sdio_uart_release_func(port);
808 }
809 }
810
sdio_uart_throttle(struct tty_struct * tty)811 static void sdio_uart_throttle(struct tty_struct *tty)
812 {
813 struct sdio_uart_port *port = tty->driver_data;
814
815 if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
816 return;
817
818 if (sdio_uart_claim_func(port) != 0)
819 return;
820
821 if (I_IXOFF(tty)) {
822 port->x_char = STOP_CHAR(tty);
823 sdio_uart_start_tx(port);
824 }
825
826 if (C_CRTSCTS(tty))
827 sdio_uart_clear_mctrl(port, TIOCM_RTS);
828
829 sdio_uart_irq(port->func);
830 sdio_uart_release_func(port);
831 }
832
sdio_uart_unthrottle(struct tty_struct * tty)833 static void sdio_uart_unthrottle(struct tty_struct *tty)
834 {
835 struct sdio_uart_port *port = tty->driver_data;
836
837 if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
838 return;
839
840 if (sdio_uart_claim_func(port) != 0)
841 return;
842
843 if (I_IXOFF(tty)) {
844 if (port->x_char) {
845 port->x_char = 0;
846 } else {
847 port->x_char = START_CHAR(tty);
848 sdio_uart_start_tx(port);
849 }
850 }
851
852 if (C_CRTSCTS(tty))
853 sdio_uart_set_mctrl(port, TIOCM_RTS);
854
855 sdio_uart_irq(port->func);
856 sdio_uart_release_func(port);
857 }
858
sdio_uart_set_termios(struct tty_struct * tty,const struct ktermios * old_termios)859 static void sdio_uart_set_termios(struct tty_struct *tty,
860 const struct ktermios *old_termios)
861 {
862 struct sdio_uart_port *port = tty->driver_data;
863 unsigned int cflag = tty->termios.c_cflag;
864
865 if (sdio_uart_claim_func(port) != 0)
866 return;
867
868 sdio_uart_change_speed(port, &tty->termios, old_termios);
869
870 /* Handle transition to B0 status */
871 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
872 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
873
874 /* Handle transition away from B0 status */
875 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
876 unsigned int mask = TIOCM_DTR;
877 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
878 mask |= TIOCM_RTS;
879 sdio_uart_set_mctrl(port, mask);
880 }
881
882 /* Handle turning off CRTSCTS */
883 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
884 tty->hw_stopped = false;
885 sdio_uart_start_tx(port);
886 }
887
888 /* Handle turning on CRTSCTS */
889 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
890 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
891 tty->hw_stopped = true;
892 sdio_uart_stop_tx(port);
893 }
894 }
895
896 sdio_uart_release_func(port);
897 }
898
sdio_uart_break_ctl(struct tty_struct * tty,int break_state)899 static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
900 {
901 struct sdio_uart_port *port = tty->driver_data;
902 int result;
903
904 result = sdio_uart_claim_func(port);
905 if (result != 0)
906 return result;
907
908 if (break_state == -1)
909 port->lcr |= UART_LCR_SBC;
910 else
911 port->lcr &= ~UART_LCR_SBC;
912 sdio_out(port, UART_LCR, port->lcr);
913
914 sdio_uart_release_func(port);
915 return 0;
916 }
917
sdio_uart_tiocmget(struct tty_struct * tty)918 static int sdio_uart_tiocmget(struct tty_struct *tty)
919 {
920 struct sdio_uart_port *port = tty->driver_data;
921 int result;
922
923 result = sdio_uart_claim_func(port);
924 if (!result) {
925 result = port->mctrl | sdio_uart_get_mctrl(port);
926 sdio_uart_release_func(port);
927 }
928
929 return result;
930 }
931
sdio_uart_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)932 static int sdio_uart_tiocmset(struct tty_struct *tty,
933 unsigned int set, unsigned int clear)
934 {
935 struct sdio_uart_port *port = tty->driver_data;
936 int result;
937
938 result = sdio_uart_claim_func(port);
939 if (!result) {
940 sdio_uart_update_mctrl(port, set, clear);
941 sdio_uart_release_func(port);
942 }
943
944 return result;
945 }
946
sdio_uart_proc_show(struct seq_file * m,void * v)947 static int sdio_uart_proc_show(struct seq_file *m, void *v)
948 {
949 int i;
950
951 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
952 "", "", "");
953 for (i = 0; i < UART_NR; i++) {
954 struct sdio_uart_port *port = sdio_uart_port_get(i);
955 if (port) {
956 seq_printf(m, "%d: uart:SDIO", i);
957 if (capable(CAP_SYS_ADMIN)) {
958 seq_printf(m, " tx:%d rx:%d",
959 port->icount.tx, port->icount.rx);
960 if (port->icount.frame)
961 seq_printf(m, " fe:%d",
962 port->icount.frame);
963 if (port->icount.parity)
964 seq_printf(m, " pe:%d",
965 port->icount.parity);
966 if (port->icount.brk)
967 seq_printf(m, " brk:%d",
968 port->icount.brk);
969 if (port->icount.overrun)
970 seq_printf(m, " oe:%d",
971 port->icount.overrun);
972 if (port->icount.cts)
973 seq_printf(m, " cts:%d",
974 port->icount.cts);
975 if (port->icount.dsr)
976 seq_printf(m, " dsr:%d",
977 port->icount.dsr);
978 if (port->icount.rng)
979 seq_printf(m, " rng:%d",
980 port->icount.rng);
981 if (port->icount.dcd)
982 seq_printf(m, " dcd:%d",
983 port->icount.dcd);
984 }
985 sdio_uart_port_put(port);
986 seq_putc(m, '\n');
987 }
988 }
989 return 0;
990 }
991
992 static const struct tty_port_operations sdio_uart_port_ops = {
993 .dtr_rts = uart_dtr_rts,
994 .carrier_raised = uart_carrier_raised,
995 .shutdown = sdio_uart_shutdown,
996 .activate = sdio_uart_activate,
997 .destruct = sdio_uart_port_destroy,
998 };
999
1000 static const struct tty_operations sdio_uart_ops = {
1001 .open = sdio_uart_open,
1002 .close = sdio_uart_close,
1003 .write = sdio_uart_write,
1004 .write_room = sdio_uart_write_room,
1005 .chars_in_buffer = sdio_uart_chars_in_buffer,
1006 .send_xchar = sdio_uart_send_xchar,
1007 .throttle = sdio_uart_throttle,
1008 .unthrottle = sdio_uart_unthrottle,
1009 .set_termios = sdio_uart_set_termios,
1010 .hangup = sdio_uart_hangup,
1011 .break_ctl = sdio_uart_break_ctl,
1012 .tiocmget = sdio_uart_tiocmget,
1013 .tiocmset = sdio_uart_tiocmset,
1014 .install = sdio_uart_install,
1015 .cleanup = sdio_uart_cleanup,
1016 .proc_show = sdio_uart_proc_show,
1017 };
1018
1019 static struct tty_driver *sdio_uart_tty_driver;
1020
sdio_uart_probe(struct sdio_func * func,const struct sdio_device_id * id)1021 static int sdio_uart_probe(struct sdio_func *func,
1022 const struct sdio_device_id *id)
1023 {
1024 struct sdio_uart_port *port;
1025 int ret;
1026
1027 port = kzalloc_obj(struct sdio_uart_port);
1028 if (!port)
1029 return -ENOMEM;
1030
1031 if (func->class == SDIO_CLASS_UART) {
1032 pr_warn("%s: need info on UART class basic setup\n",
1033 sdio_func_id(func));
1034 kfree(port);
1035 return -ENOSYS;
1036 } else if (func->class == SDIO_CLASS_GPS) {
1037 /*
1038 * We need tuple 0x91. It contains SUBTPL_SIOREG
1039 * and SUBTPL_RCVCAPS.
1040 */
1041 struct sdio_func_tuple *tpl;
1042 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1043 if (tpl->code != 0x91)
1044 continue;
1045 if (tpl->size < 10)
1046 continue;
1047 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1048 break;
1049 }
1050 if (!tpl) {
1051 pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1052 sdio_func_id(func));
1053 kfree(port);
1054 return -EINVAL;
1055 }
1056 pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1057 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1058 port->regs_offset = (tpl->data[4] << 0) |
1059 (tpl->data[5] << 8) |
1060 (tpl->data[6] << 16);
1061 pr_debug("%s: regs offset = 0x%x\n",
1062 sdio_func_id(func), port->regs_offset);
1063 port->uartclk = tpl->data[7] * 115200;
1064 if (port->uartclk == 0)
1065 port->uartclk = 115200;
1066 pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
1067 sdio_func_id(func), port->uartclk,
1068 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1069 } else {
1070 kfree(port);
1071 return -EINVAL;
1072 }
1073
1074 port->func = func;
1075 sdio_set_drvdata(func, port);
1076 tty_port_init(&port->port);
1077 port->port.ops = &sdio_uart_port_ops;
1078
1079 ret = sdio_uart_add_port(port);
1080 if (ret) {
1081 kfree(port);
1082 } else {
1083 struct device *dev;
1084 dev = tty_port_register_device(&port->port,
1085 sdio_uart_tty_driver, port->index, &func->dev);
1086 if (IS_ERR(dev)) {
1087 sdio_uart_port_remove(port);
1088 ret = PTR_ERR(dev);
1089 }
1090 }
1091
1092 return ret;
1093 }
1094
sdio_uart_remove(struct sdio_func * func)1095 static void sdio_uart_remove(struct sdio_func *func)
1096 {
1097 struct sdio_uart_port *port = sdio_get_drvdata(func);
1098
1099 tty_unregister_device(sdio_uart_tty_driver, port->index);
1100 sdio_uart_port_remove(port);
1101 }
1102
1103 static const struct sdio_device_id sdio_uart_ids[] = {
1104 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1105 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1106 { /* end: all zeroes */ },
1107 };
1108
1109 MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1110
1111 static struct sdio_driver sdio_uart_driver = {
1112 .probe = sdio_uart_probe,
1113 .remove = sdio_uart_remove,
1114 .name = "sdio_uart",
1115 .id_table = sdio_uart_ids,
1116 };
1117
sdio_uart_init(void)1118 static int __init sdio_uart_init(void)
1119 {
1120 int ret;
1121 struct tty_driver *tty_drv;
1122
1123 sdio_uart_tty_driver = tty_drv = tty_alloc_driver(UART_NR,
1124 TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV);
1125 if (IS_ERR(tty_drv))
1126 return PTR_ERR(tty_drv);
1127
1128 tty_drv->driver_name = "sdio_uart";
1129 tty_drv->name = "ttySDIO";
1130 tty_drv->major = 0; /* dynamically allocated */
1131 tty_drv->minor_start = 0;
1132 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1133 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1134 tty_drv->init_termios = tty_std_termios;
1135 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1136 tty_drv->init_termios.c_ispeed = 4800;
1137 tty_drv->init_termios.c_ospeed = 4800;
1138 tty_set_operations(tty_drv, &sdio_uart_ops);
1139
1140 ret = tty_register_driver(tty_drv);
1141 if (ret)
1142 goto err1;
1143
1144 ret = sdio_register_driver(&sdio_uart_driver);
1145 if (ret)
1146 goto err2;
1147
1148 return 0;
1149
1150 err2:
1151 tty_unregister_driver(tty_drv);
1152 err1:
1153 tty_driver_kref_put(tty_drv);
1154 return ret;
1155 }
1156
sdio_uart_exit(void)1157 static void __exit sdio_uart_exit(void)
1158 {
1159 sdio_unregister_driver(&sdio_uart_driver);
1160 tty_unregister_driver(sdio_uart_tty_driver);
1161 tty_driver_kref_put(sdio_uart_tty_driver);
1162 }
1163
1164 module_init(sdio_uart_init);
1165 module_exit(sdio_uart_exit);
1166
1167 MODULE_AUTHOR("Nicolas Pitre");
1168 MODULE_DESCRIPTION("SDIO UART/GPS driver");
1169 MODULE_LICENSE("GPL");
1170