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