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