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