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