serial: bcm63xx_uart: allow more than one uart to be registered.
[deliverable/linux.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);
1f100b32
AC
606 int ret = sdio_uart_claim_func(port);
607 if (ret)
608 return;
584abc37
AC
609 if (onoff == 0)
610 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
611 else
612 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1f100b32 613 sdio_uart_release_func(port);
584abc37 614}
530646f4 615
584abc37
AC
616/**
617 * sdio_uart_activate - start up hardware
618 * @tport: tty port to activate
619 * @tty: tty bound to this port
620 *
621 * Activate a tty port. The port locking guarantees us this will be
622 * run exactly once per set of opens, and if successful will see the
623 * shutdown method run exactly once to match. Start up and shutdown are
624 * protected from each other by the internal locking and will not run
625 * at the same time even during a hangup event.
626 *
627 * If we successfully start up the port we take an extra kref as we
628 * will keep it around until shutdown when the kref is dropped.
629 */
630
631static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
632{
633 struct sdio_uart_port *port =
634 container_of(tport, struct sdio_uart_port, port);
635 unsigned long page;
636 int ret;
6e418a9d
NP
637
638 /*
639 * Set the TTY IO error marker - we will only clear this
640 * once we have successfully opened the port.
641 */
b5849b1a 642 set_bit(TTY_IO_ERROR, &tty->flags);
6e418a9d
NP
643
644 /* Initialise and allocate the transmit buffer. */
645 page = __get_free_page(GFP_KERNEL);
646 if (!page)
584abc37 647 return -ENOMEM;
6e418a9d
NP
648 port->xmit.buf = (unsigned char *)page;
649 circ_clear(&port->xmit);
650
651 ret = sdio_uart_claim_func(port);
652 if (ret)
653 goto err1;
654 ret = sdio_enable_func(port->func);
655 if (ret)
656 goto err2;
657 ret = sdio_claim_irq(port->func, sdio_uart_irq);
658 if (ret)
659 goto err3;
660
661 /*
662 * Clear the FIFO buffers and disable them.
663 * (they will be reenabled in sdio_change_speed())
664 */
665 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
666 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
1e04b7ae 667 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
6e418a9d
NP
668 sdio_out(port, UART_FCR, 0);
669
670 /*
671 * Clear the interrupt registers.
672 */
673 (void) sdio_in(port, UART_LSR);
674 (void) sdio_in(port, UART_RX);
675 (void) sdio_in(port, UART_IIR);
676 (void) sdio_in(port, UART_MSR);
677
678 /*
679 * Now, initialize the UART
680 */
681 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
682
c271cf37 683 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
6e418a9d
NP
684 port->mctrl = TIOCM_OUT2;
685
b5849b1a 686 sdio_uart_change_speed(port, tty->termios, NULL);
6e418a9d 687
b5849b1a 688 if (tty->termios->c_cflag & CBAUD)
6e418a9d
NP
689 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
690
b5849b1a 691 if (tty->termios->c_cflag & CRTSCTS)
6e418a9d 692 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
b5849b1a 693 tty->hw_stopped = 1;
6e418a9d 694
0395b48c 695 clear_bit(TTY_IO_ERROR, &tty->flags);
6e418a9d
NP
696
697 /* Kick the IRQ handler once while we're still holding the host lock */
698 sdio_uart_irq(port->func);
699
700 sdio_uart_release_func(port);
701 return 0;
702
703err3:
704 sdio_disable_func(port->func);
705err2:
706 sdio_uart_release_func(port);
707err1:
708 free_page((unsigned long)port->xmit.buf);
709 return ret;
710}
711
584abc37
AC
712/**
713 * sdio_uart_shutdown - stop hardware
714 * @tport: tty port to shut down
715 *
716 * Deactivate a tty port. The port locking guarantees us this will be
717 * run only if a successful matching activate already ran. The two are
718 * protected from each other by the internal locking and will not run
719 * at the same time even during a hangup event.
720 */
721
722static void sdio_uart_shutdown(struct tty_port *tport)
6e418a9d 723{
584abc37
AC
724 struct sdio_uart_port *port =
725 container_of(tport, struct sdio_uart_port, port);
6e418a9d
NP
726 int ret;
727
728 ret = sdio_uart_claim_func(port);
729 if (ret)
730 goto skip;
731
732 sdio_uart_stop_rx(port);
733
1e04b7ae 734 /* Disable interrupts from this port */
6e418a9d
NP
735 sdio_release_irq(port->func);
736 port->ier = 0;
737 sdio_out(port, UART_IER, 0);
738
739 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
740
741 /* Disable break condition and FIFOs. */
742 port->lcr &= ~UART_LCR_SBC;
743 sdio_out(port, UART_LCR, port->lcr);
744 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
745 UART_FCR_CLEAR_RCVR |
746 UART_FCR_CLEAR_XMIT);
747 sdio_out(port, UART_FCR, 0);
748
749 sdio_disable_func(port->func);
750
751 sdio_uart_release_func(port);
752
753skip:
754 /* Free the transmit buffer page. */
755 free_page((unsigned long)port->xmit.buf);
756}
757
584abc37
AC
758/**
759 * sdio_uart_install - install method
760 * @driver: the driver in use (sdio_uart in our case)
761 * @tty: the tty being bound
762 *
763 * Look up and bind the tty and the driver together. Initialize
764 * any needed private data (in our case the termios)
765 */
6e418a9d 766
584abc37
AC
767static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
768{
769 int idx = tty->index;
770 struct sdio_uart_port *port = sdio_uart_port_get(idx);
771 int ret = tty_init_termios(tty);
772
773 if (ret == 0) {
774 tty_driver_kref_get(driver);
775 tty->count++;
776 /* This is the ref sdio_uart_port get provided */
777 tty->driver_data = port;
778 driver->ttys[idx] = tty;
779 } else
6e418a9d 780 sdio_uart_port_put(port);
584abc37 781 return ret;
6e418a9d
NP
782}
783
584abc37
AC
784/**
785 * sdio_uart_cleanup - called on the last tty kref drop
786 * @tty: the tty being destroyed
787 *
788 * Called asynchronously when the last reference to the tty is dropped.
789 * We cannot destroy the tty->driver_data port kref until this point
790 */
791
792static void sdio_uart_cleanup(struct tty_struct *tty)
6e418a9d
NP
793{
794 struct sdio_uart_port *port = tty->driver_data;
584abc37
AC
795 tty->driver_data = NULL; /* Bug trap */
796 sdio_uart_port_put(port);
797}
6e418a9d 798
584abc37
AC
799/*
800 * Open/close/hangup is now entirely boilerplate
801 */
6e418a9d 802
584abc37
AC
803static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
804{
805 struct sdio_uart_port *port = tty->driver_data;
806 return tty_port_open(&port->port, tty, filp);
807}
6e418a9d 808
584abc37
AC
809static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
810{
811 struct sdio_uart_port *port = tty->driver_data;
812 tty_port_close(&port->port, tty, filp);
813}
6e418a9d 814
584abc37
AC
815static void sdio_uart_hangup(struct tty_struct *tty)
816{
817 struct sdio_uart_port *port = tty->driver_data;
818 tty_port_hangup(&port->port);
6e418a9d
NP
819}
820
c271cf37 821static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
6e418a9d
NP
822 int count)
823{
824 struct sdio_uart_port *port = tty->driver_data;
825 struct circ_buf *circ = &port->xmit;
826 int c, ret = 0;
827
828 if (!port->func)
829 return -ENODEV;
830
831 spin_lock(&port->write_lock);
832 while (1) {
833 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
834 if (count < c)
835 c = count;
836 if (c <= 0)
837 break;
838 memcpy(circ->buf + circ->head, buf, c);
839 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
840 buf += c;
841 count -= c;
842 ret += c;
843 }
844 spin_unlock(&port->write_lock);
845
c271cf37 846 if (!(port->ier & UART_IER_THRI)) {
6e418a9d
NP
847 int err = sdio_uart_claim_func(port);
848 if (!err) {
849 sdio_uart_start_tx(port);
850 sdio_uart_irq(port->func);
851 sdio_uart_release_func(port);
852 } else
853 ret = err;
854 }
855
856 return ret;
857}
858
859static int sdio_uart_write_room(struct tty_struct *tty)
860{
861 struct sdio_uart_port *port = tty->driver_data;
862 return port ? circ_chars_free(&port->xmit) : 0;
863}
864
865static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
866{
867 struct sdio_uart_port *port = tty->driver_data;
868 return port ? circ_chars_pending(&port->xmit) : 0;
869}
870
871static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
872{
873 struct sdio_uart_port *port = tty->driver_data;
874
875 port->x_char = ch;
876 if (ch && !(port->ier & UART_IER_THRI)) {
877 if (sdio_uart_claim_func(port) != 0)
878 return;
879 sdio_uart_start_tx(port);
880 sdio_uart_irq(port->func);
881 sdio_uart_release_func(port);
882 }
883}
884
885static void sdio_uart_throttle(struct tty_struct *tty)
886{
887 struct sdio_uart_port *port = tty->driver_data;
888
889 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
890 return;
891
892 if (sdio_uart_claim_func(port) != 0)
893 return;
894
895 if (I_IXOFF(tty)) {
896 port->x_char = STOP_CHAR(tty);
897 sdio_uart_start_tx(port);
898 }
899
900 if (tty->termios->c_cflag & CRTSCTS)
901 sdio_uart_clear_mctrl(port, TIOCM_RTS);
902
903 sdio_uart_irq(port->func);
904 sdio_uart_release_func(port);
905}
906
907static void sdio_uart_unthrottle(struct tty_struct *tty)
908{
909 struct sdio_uart_port *port = tty->driver_data;
910
911 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
912 return;
913
914 if (sdio_uart_claim_func(port) != 0)
915 return;
916
917 if (I_IXOFF(tty)) {
918 if (port->x_char) {
919 port->x_char = 0;
920 } else {
921 port->x_char = START_CHAR(tty);
922 sdio_uart_start_tx(port);
923 }
924 }
925
926 if (tty->termios->c_cflag & CRTSCTS)
927 sdio_uart_set_mctrl(port, TIOCM_RTS);
928
929 sdio_uart_irq(port->func);
930 sdio_uart_release_func(port);
931}
932
c271cf37
AC
933static void sdio_uart_set_termios(struct tty_struct *tty,
934 struct ktermios *old_termios)
6e418a9d
NP
935{
936 struct sdio_uart_port *port = tty->driver_data;
937 unsigned int cflag = tty->termios->c_cflag;
938
6e418a9d
NP
939 if (sdio_uart_claim_func(port) != 0)
940 return;
941
942 sdio_uart_change_speed(port, tty->termios, old_termios);
943
944 /* Handle transition to B0 status */
945 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
946 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
947
948 /* Handle transition away from B0 status */
949 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
950 unsigned int mask = TIOCM_DTR;
951 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
952 mask |= TIOCM_RTS;
953 sdio_uart_set_mctrl(port, mask);
954 }
955
956 /* Handle turning off CRTSCTS */
957 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
958 tty->hw_stopped = 0;
959 sdio_uart_start_tx(port);
960 }
961
962 /* Handle turning on CRTSCTS */
963 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
964 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
965 tty->hw_stopped = 1;
966 sdio_uart_stop_tx(port);
967 }
968 }
969
970 sdio_uart_release_func(port);
971}
972
c43d8636 973static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
6e418a9d
NP
974{
975 struct sdio_uart_port *port = tty->driver_data;
c43d8636 976 int result;
6e418a9d 977
c43d8636
DH
978 result = sdio_uart_claim_func(port);
979 if (result != 0)
980 return result;
6e418a9d
NP
981
982 if (break_state == -1)
983 port->lcr |= UART_LCR_SBC;
984 else
985 port->lcr &= ~UART_LCR_SBC;
986 sdio_out(port, UART_LCR, port->lcr);
987
988 sdio_uart_release_func(port);
c43d8636 989 return 0;
6e418a9d
NP
990}
991
992static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
993{
994 struct sdio_uart_port *port = tty->driver_data;
995 int result;
996
997 result = sdio_uart_claim_func(port);
998 if (!result) {
999 result = port->mctrl | sdio_uart_get_mctrl(port);
1000 sdio_uart_release_func(port);
1001 }
1002
1003 return result;
1004}
1005
1006static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
1007 unsigned int set, unsigned int clear)
1008{
1009 struct sdio_uart_port *port = tty->driver_data;
1010 int result;
1011
1e04b7ae 1012 result = sdio_uart_claim_func(port);
c271cf37 1013 if (!result) {
6e418a9d
NP
1014 sdio_uart_update_mctrl(port, set, clear);
1015 sdio_uart_release_func(port);
1016 }
1017
1018 return result;
1019}
1020
201a50ba 1021static int sdio_uart_proc_show(struct seq_file *m, void *v)
5ed334a1 1022{
201a50ba 1023 int i;
5ed334a1 1024
201a50ba 1025 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
5ed334a1 1026 "", "", "");
201a50ba 1027 for (i = 0; i < UART_NR; i++) {
5ed334a1
NP
1028 struct sdio_uart_port *port = sdio_uart_port_get(i);
1029 if (port) {
201a50ba 1030 seq_printf(m, "%d: uart:SDIO", i);
c271cf37 1031 if (capable(CAP_SYS_ADMIN)) {
201a50ba 1032 seq_printf(m, " tx:%d rx:%d",
1e04b7ae 1033 port->icount.tx, port->icount.rx);
5ed334a1 1034 if (port->icount.frame)
201a50ba 1035 seq_printf(m, " fe:%d",
1e04b7ae 1036 port->icount.frame);
5ed334a1 1037 if (port->icount.parity)
201a50ba 1038 seq_printf(m, " pe:%d",
1e04b7ae 1039 port->icount.parity);
5ed334a1 1040 if (port->icount.brk)
201a50ba 1041 seq_printf(m, " brk:%d",
1e04b7ae 1042 port->icount.brk);
5ed334a1 1043 if (port->icount.overrun)
201a50ba 1044 seq_printf(m, " oe:%d",
1e04b7ae 1045 port->icount.overrun);
5ed334a1 1046 if (port->icount.cts)
201a50ba 1047 seq_printf(m, " cts:%d",
1e04b7ae 1048 port->icount.cts);
5ed334a1 1049 if (port->icount.dsr)
201a50ba 1050 seq_printf(m, " dsr:%d",
1e04b7ae 1051 port->icount.dsr);
5ed334a1 1052 if (port->icount.rng)
201a50ba 1053 seq_printf(m, " rng:%d",
1e04b7ae 1054 port->icount.rng);
5ed334a1 1055 if (port->icount.dcd)
201a50ba 1056 seq_printf(m, " dcd:%d",
1e04b7ae 1057 port->icount.dcd);
5ed334a1 1058 }
5ed334a1 1059 sdio_uart_port_put(port);
201a50ba 1060 seq_putc(m, '\n');
5ed334a1
NP
1061 }
1062 }
201a50ba
AD
1063 return 0;
1064}
5ed334a1 1065
201a50ba
AD
1066static int sdio_uart_proc_open(struct inode *inode, struct file *file)
1067{
1068 return single_open(file, sdio_uart_proc_show, NULL);
5ed334a1
NP
1069}
1070
201a50ba
AD
1071static const struct file_operations sdio_uart_proc_fops = {
1072 .owner = THIS_MODULE,
1073 .open = sdio_uart_proc_open,
1074 .read = seq_read,
1075 .llseek = seq_lseek,
1076 .release = single_release,
1077};
1078
584abc37
AC
1079static const struct tty_port_operations sdio_uart_port_ops = {
1080 .dtr_rts = uart_dtr_rts,
4b3b49bb 1081 .carrier_raised = uart_carrier_raised,
584abc37
AC
1082 .shutdown = sdio_uart_shutdown,
1083 .activate = sdio_uart_activate,
1084};
1085
6e418a9d
NP
1086static const struct tty_operations sdio_uart_ops = {
1087 .open = sdio_uart_open,
1088 .close = sdio_uart_close,
1089 .write = sdio_uart_write,
1090 .write_room = sdio_uart_write_room,
1091 .chars_in_buffer = sdio_uart_chars_in_buffer,
1092 .send_xchar = sdio_uart_send_xchar,
1093 .throttle = sdio_uart_throttle,
1094 .unthrottle = sdio_uart_unthrottle,
1095 .set_termios = sdio_uart_set_termios,
584abc37 1096 .hangup = sdio_uart_hangup,
6e418a9d
NP
1097 .break_ctl = sdio_uart_break_ctl,
1098 .tiocmget = sdio_uart_tiocmget,
1099 .tiocmset = sdio_uart_tiocmset,
584abc37
AC
1100 .install = sdio_uart_install,
1101 .cleanup = sdio_uart_cleanup,
201a50ba 1102 .proc_fops = &sdio_uart_proc_fops,
6e418a9d
NP
1103};
1104
1105static struct tty_driver *sdio_uart_tty_driver;
1106
1107static int sdio_uart_probe(struct sdio_func *func,
1108 const struct sdio_device_id *id)
1109{
1110 struct sdio_uart_port *port;
1111 int ret;
1112
1113 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1114 if (!port)
1115 return -ENOMEM;
1116
1117 if (func->class == SDIO_CLASS_UART) {
1118 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1119 sdio_func_id(func));
1120 kfree(port);
1121 return -ENOSYS;
1122 } else if (func->class == SDIO_CLASS_GPS) {
1123 /*
1124 * We need tuple 0x91. It contains SUBTPL_SIOREG
1125 * and SUBTPL_RCVCAPS.
1126 */
1127 struct sdio_func_tuple *tpl;
1128 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1129 if (tpl->code != 0x91)
1130 continue;
1131 if (tpl->size < 10)
1132 continue;
1133 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1134 break;
1135 }
1136 if (!tpl) {
1137 printk(KERN_WARNING
c271cf37 1138 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
6e418a9d
NP
1139 sdio_func_id(func));
1140 kfree(port);
1141 return -EINVAL;
1142 }
1143 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1144 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1145 port->regs_offset = (tpl->data[4] << 0) |
1146 (tpl->data[5] << 8) |
1147 (tpl->data[6] << 16);
1148 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1149 sdio_func_id(func), port->regs_offset);
1150 port->uartclk = tpl->data[7] * 115200;
1151 if (port->uartclk == 0)
1152 port->uartclk = 115200;
1153 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1154 sdio_func_id(func), port->uartclk,
1155 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1156 } else {
1157 kfree(port);
1158 return -EINVAL;
1159 }
1160
1161 port->func = func;
1162 sdio_set_drvdata(func, port);
b5849b1a 1163 tty_port_init(&port->port);
584abc37 1164 port->port.ops = &sdio_uart_port_ops;
6e418a9d
NP
1165
1166 ret = sdio_uart_add_port(port);
1167 if (ret) {
1168 kfree(port);
1169 } else {
1170 struct device *dev;
c271cf37
AC
1171 dev = tty_register_device(sdio_uart_tty_driver,
1172 port->index, &func->dev);
6e418a9d
NP
1173 if (IS_ERR(dev)) {
1174 sdio_uart_port_remove(port);
1175 ret = PTR_ERR(dev);
1176 }
1177 }
1178
1179 return ret;
1180}
1181
1182static void sdio_uart_remove(struct sdio_func *func)
1183{
1184 struct sdio_uart_port *port = sdio_get_drvdata(func);
1185
1186 tty_unregister_device(sdio_uart_tty_driver, port->index);
1187 sdio_uart_port_remove(port);
1188}
1189
1190static const struct sdio_device_id sdio_uart_ids[] = {
1191 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1192 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1193 { /* end: all zeroes */ },
1194};
1195
1196MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1197
1198static struct sdio_driver sdio_uart_driver = {
1199 .probe = sdio_uart_probe,
1200 .remove = sdio_uart_remove,
1201 .name = "sdio_uart",
1202 .id_table = sdio_uart_ids,
1203};
1204
1205static int __init sdio_uart_init(void)
1206{
1207 int ret;
1208 struct tty_driver *tty_drv;
1209
1210 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1211 if (!tty_drv)
1212 return -ENOMEM;
1213
1214 tty_drv->owner = THIS_MODULE;
1215 tty_drv->driver_name = "sdio_uart";
1216 tty_drv->name = "ttySDIO";
1217 tty_drv->major = 0; /* dynamically allocated */
1218 tty_drv->minor_start = 0;
1219 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1220 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1221 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1222 tty_drv->init_termios = tty_std_termios;
1223 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
2ba30eed
NP
1224 tty_drv->init_termios.c_ispeed = 4800;
1225 tty_drv->init_termios.c_ospeed = 4800;
6e418a9d
NP
1226 tty_set_operations(tty_drv, &sdio_uart_ops);
1227
1228 ret = tty_register_driver(tty_drv);
1229 if (ret)
1230 goto err1;
1231
1232 ret = sdio_register_driver(&sdio_uart_driver);
1233 if (ret)
1234 goto err2;
1235
1236 return 0;
1237
1238err2:
1239 tty_unregister_driver(tty_drv);
1240err1:
1241 put_tty_driver(tty_drv);
1242 return ret;
1243}
1244
1245static void __exit sdio_uart_exit(void)
1246{
1247 sdio_unregister_driver(&sdio_uart_driver);
1248 tty_unregister_driver(sdio_uart_tty_driver);
1249 put_tty_driver(sdio_uart_tty_driver);
1250}
1251
1252module_init(sdio_uart_init);
1253module_exit(sdio_uart_exit);
1254
1255MODULE_AUTHOR("Nicolas Pitre");
1256MODULE_LICENSE("GPL");
This page took 0.316349 seconds and 5 git commands to generate.