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