tty: sdio_uart: Style fixes
[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
c271cf37
AC
468 if (tty == NULL || circ_empty(xmit) ||
469 tty->stopped || tty->hw_stopped) {
6e418a9d 470 sdio_uart_stop_tx(port);
530646f4 471 tty_kref_put(tty);
6e418a9d
NP
472 return;
473 }
474
475 count = 16;
476 do {
477 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
478 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
479 port->icount.tx++;
480 if (circ_empty(xmit))
481 break;
482 } while (--count > 0);
483
484 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
b5849b1a 485 tty_wakeup(tty);
6e418a9d
NP
486
487 if (circ_empty(xmit))
488 sdio_uart_stop_tx(port);
530646f4 489 tty_kref_put(tty);
6e418a9d
NP
490}
491
492static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
493{
494 int status;
530646f4 495 struct tty_struct *tty;
6e418a9d
NP
496
497 status = sdio_in(port, UART_MSR);
498
499 if ((status & UART_MSR_ANY_DELTA) == 0)
500 return;
501
502 if (status & UART_MSR_TERI)
503 port->icount.rng++;
504 if (status & UART_MSR_DDSR)
505 port->icount.dsr++;
506 if (status & UART_MSR_DDCD)
507 port->icount.dcd++;
508 if (status & UART_MSR_DCTS) {
509 port->icount.cts++;
530646f4
AC
510 tty = tty_port_tty_get(&port->port);
511 if (tty && (tty->termios->c_cflag & CRTSCTS)) {
6e418a9d 512 int cts = (status & UART_MSR_CTS);
b5849b1a 513 if (tty->hw_stopped) {
6e418a9d 514 if (cts) {
b5849b1a 515 tty->hw_stopped = 0;
6e418a9d 516 sdio_uart_start_tx(port);
b5849b1a 517 tty_wakeup(tty);
6e418a9d
NP
518 }
519 } else {
520 if (!cts) {
b5849b1a 521 tty->hw_stopped = 1;
6e418a9d
NP
522 sdio_uart_stop_tx(port);
523 }
524 }
525 }
530646f4 526 tty_kref_put(tty);
6e418a9d
NP
527 }
528}
529
530/*
531 * This handles the interrupt from one port.
532 */
533static void sdio_uart_irq(struct sdio_func *func)
534{
535 struct sdio_uart_port *port = sdio_get_drvdata(func);
536 unsigned int iir, lsr;
537
15b82b46
NP
538 /*
539 * In a few places sdio_uart_irq() is called directly instead of
540 * waiting for the actual interrupt to be raised and the SDIO IRQ
541 * thread scheduled in order to reduce latency. However, some
542 * interaction with the tty core may end up calling us back
543 * (serial echo, flow control, etc.) through those same places
544 * causing undesirable effects. Let's stop the recursion here.
545 */
546 if (unlikely(port->in_sdio_uart_irq == current))
547 return;
548
6e418a9d
NP
549 iir = sdio_in(port, UART_IIR);
550 if (iir & UART_IIR_NO_INT)
551 return;
15b82b46
NP
552
553 port->in_sdio_uart_irq = current;
6e418a9d
NP
554 lsr = sdio_in(port, UART_LSR);
555 if (lsr & UART_LSR_DR)
556 sdio_uart_receive_chars(port, &lsr);
557 sdio_uart_check_modem_status(port);
558 if (lsr & UART_LSR_THRE)
559 sdio_uart_transmit_chars(port);
15b82b46 560 port->in_sdio_uart_irq = NULL;
6e418a9d
NP
561}
562
584abc37
AC
563/**
564 * uart_dtr_rts - port helper to set uart signals
565 * @tport: tty port to be updated
566 * @onoff: set to turn on DTR/RTS
567 *
568 * Called by the tty port helpers when the modem signals need to be
569 * adjusted during an open, close and hangup.
570 */
571
572static void uart_dtr_rts(struct tty_port *tport, int onoff)
6e418a9d 573{
584abc37
AC
574 struct sdio_uart_port *port =
575 container_of(tport, struct sdio_uart_port, port);
576 if (onoff == 0)
577 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
578 else
579 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
580}
530646f4 581
584abc37
AC
582/**
583 * sdio_uart_activate - start up hardware
584 * @tport: tty port to activate
585 * @tty: tty bound to this port
586 *
587 * Activate a tty port. The port locking guarantees us this will be
588 * run exactly once per set of opens, and if successful will see the
589 * shutdown method run exactly once to match. Start up and shutdown are
590 * protected from each other by the internal locking and will not run
591 * at the same time even during a hangup event.
592 *
593 * If we successfully start up the port we take an extra kref as we
594 * will keep it around until shutdown when the kref is dropped.
595 */
596
597static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
598{
599 struct sdio_uart_port *port =
600 container_of(tport, struct sdio_uart_port, port);
601 unsigned long page;
602 int ret;
6e418a9d
NP
603
604 /*
605 * Set the TTY IO error marker - we will only clear this
606 * once we have successfully opened the port.
607 */
b5849b1a 608 set_bit(TTY_IO_ERROR, &tty->flags);
6e418a9d
NP
609
610 /* Initialise and allocate the transmit buffer. */
611 page = __get_free_page(GFP_KERNEL);
612 if (!page)
584abc37 613 return -ENOMEM;
6e418a9d
NP
614 port->xmit.buf = (unsigned char *)page;
615 circ_clear(&port->xmit);
616
617 ret = sdio_uart_claim_func(port);
618 if (ret)
619 goto err1;
620 ret = sdio_enable_func(port->func);
621 if (ret)
622 goto err2;
623 ret = sdio_claim_irq(port->func, sdio_uart_irq);
624 if (ret)
625 goto err3;
626
627 /*
628 * Clear the FIFO buffers and disable them.
629 * (they will be reenabled in sdio_change_speed())
630 */
631 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
632 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
1e04b7ae 633 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
6e418a9d
NP
634 sdio_out(port, UART_FCR, 0);
635
636 /*
637 * Clear the interrupt registers.
638 */
639 (void) sdio_in(port, UART_LSR);
640 (void) sdio_in(port, UART_RX);
641 (void) sdio_in(port, UART_IIR);
642 (void) sdio_in(port, UART_MSR);
643
644 /*
645 * Now, initialize the UART
646 */
647 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
648
c271cf37 649 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
6e418a9d
NP
650 port->mctrl = TIOCM_OUT2;
651
b5849b1a 652 sdio_uart_change_speed(port, tty->termios, NULL);
6e418a9d 653
b5849b1a 654 if (tty->termios->c_cflag & CBAUD)
6e418a9d
NP
655 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
656
b5849b1a 657 if (tty->termios->c_cflag & CRTSCTS)
6e418a9d 658 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
b5849b1a 659 tty->hw_stopped = 1;
6e418a9d 660
0395b48c 661 clear_bit(TTY_IO_ERROR, &tty->flags);
6e418a9d
NP
662
663 /* Kick the IRQ handler once while we're still holding the host lock */
664 sdio_uart_irq(port->func);
665
666 sdio_uart_release_func(port);
667 return 0;
668
669err3:
670 sdio_disable_func(port->func);
671err2:
672 sdio_uart_release_func(port);
673err1:
674 free_page((unsigned long)port->xmit.buf);
675 return ret;
676}
677
584abc37
AC
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
NP
748}
749
584abc37
AC
750/**
751 * sdio_uart_cleanup - called on the last tty kref drop
752 * @tty: the tty being destroyed
753 *
754 * Called asynchronously when the last reference to the tty is dropped.
755 * We cannot destroy the tty->driver_data port kref until this point
756 */
757
758static void sdio_uart_cleanup(struct tty_struct *tty)
6e418a9d
NP
759{
760 struct sdio_uart_port *port = tty->driver_data;
584abc37
AC
761 tty->driver_data = NULL; /* Bug trap */
762 sdio_uart_port_put(port);
763}
6e418a9d 764
584abc37
AC
765/*
766 * Open/close/hangup is now entirely boilerplate
767 */
6e418a9d 768
584abc37
AC
769static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
770{
771 struct sdio_uart_port *port = tty->driver_data;
772 return tty_port_open(&port->port, tty, filp);
773}
6e418a9d 774
584abc37
AC
775static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
776{
777 struct sdio_uart_port *port = tty->driver_data;
778 tty_port_close(&port->port, tty, filp);
779}
6e418a9d 780
584abc37
AC
781static void sdio_uart_hangup(struct tty_struct *tty)
782{
783 struct sdio_uart_port *port = tty->driver_data;
784 tty_port_hangup(&port->port);
6e418a9d
NP
785}
786
c271cf37 787static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
6e418a9d
NP
788 int count)
789{
790 struct sdio_uart_port *port = tty->driver_data;
791 struct circ_buf *circ = &port->xmit;
792 int c, ret = 0;
793
794 if (!port->func)
795 return -ENODEV;
796
797 spin_lock(&port->write_lock);
798 while (1) {
799 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
800 if (count < c)
801 c = count;
802 if (c <= 0)
803 break;
804 memcpy(circ->buf + circ->head, buf, c);
805 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
806 buf += c;
807 count -= c;
808 ret += c;
809 }
810 spin_unlock(&port->write_lock);
811
c271cf37 812 if (!(port->ier & UART_IER_THRI)) {
6e418a9d
NP
813 int err = sdio_uart_claim_func(port);
814 if (!err) {
815 sdio_uart_start_tx(port);
816 sdio_uart_irq(port->func);
817 sdio_uart_release_func(port);
818 } else
819 ret = err;
820 }
821
822 return ret;
823}
824
825static int sdio_uart_write_room(struct tty_struct *tty)
826{
827 struct sdio_uart_port *port = tty->driver_data;
828 return port ? circ_chars_free(&port->xmit) : 0;
829}
830
831static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
832{
833 struct sdio_uart_port *port = tty->driver_data;
834 return port ? circ_chars_pending(&port->xmit) : 0;
835}
836
837static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
838{
839 struct sdio_uart_port *port = tty->driver_data;
840
841 port->x_char = ch;
842 if (ch && !(port->ier & UART_IER_THRI)) {
843 if (sdio_uart_claim_func(port) != 0)
844 return;
845 sdio_uart_start_tx(port);
846 sdio_uart_irq(port->func);
847 sdio_uart_release_func(port);
848 }
849}
850
851static void sdio_uart_throttle(struct tty_struct *tty)
852{
853 struct sdio_uart_port *port = tty->driver_data;
854
855 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
856 return;
857
858 if (sdio_uart_claim_func(port) != 0)
859 return;
860
861 if (I_IXOFF(tty)) {
862 port->x_char = STOP_CHAR(tty);
863 sdio_uart_start_tx(port);
864 }
865
866 if (tty->termios->c_cflag & CRTSCTS)
867 sdio_uart_clear_mctrl(port, TIOCM_RTS);
868
869 sdio_uart_irq(port->func);
870 sdio_uart_release_func(port);
871}
872
873static void sdio_uart_unthrottle(struct tty_struct *tty)
874{
875 struct sdio_uart_port *port = tty->driver_data;
876
877 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
878 return;
879
880 if (sdio_uart_claim_func(port) != 0)
881 return;
882
883 if (I_IXOFF(tty)) {
884 if (port->x_char) {
885 port->x_char = 0;
886 } else {
887 port->x_char = START_CHAR(tty);
888 sdio_uart_start_tx(port);
889 }
890 }
891
892 if (tty->termios->c_cflag & CRTSCTS)
893 sdio_uart_set_mctrl(port, TIOCM_RTS);
894
895 sdio_uart_irq(port->func);
896 sdio_uart_release_func(port);
897}
898
c271cf37
AC
899static void sdio_uart_set_termios(struct tty_struct *tty,
900 struct ktermios *old_termios)
6e418a9d
NP
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);
c271cf37 979 if (!result) {
6e418a9d
NP
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);
c271cf37 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
c271cf37 1103 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
6e418a9d
NP
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;
c271cf37
AC
1136 dev = tty_register_device(sdio_uart_tty_driver,
1137 port->index, &func->dev);
6e418a9d
NP
1138 if (IS_ERR(dev)) {
1139 sdio_uart_port_remove(port);
1140 ret = PTR_ERR(dev);
1141 }
1142 }
1143
1144 return ret;
1145}
1146
1147static void sdio_uart_remove(struct sdio_func *func)
1148{
1149 struct sdio_uart_port *port = sdio_get_drvdata(func);
1150
1151 tty_unregister_device(sdio_uart_tty_driver, port->index);
1152 sdio_uart_port_remove(port);
1153}
1154
1155static const struct sdio_device_id sdio_uart_ids[] = {
1156 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1157 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1158 { /* end: all zeroes */ },
1159};
1160
1161MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1162
1163static struct sdio_driver sdio_uart_driver = {
1164 .probe = sdio_uart_probe,
1165 .remove = sdio_uart_remove,
1166 .name = "sdio_uart",
1167 .id_table = sdio_uart_ids,
1168};
1169
1170static int __init sdio_uart_init(void)
1171{
1172 int ret;
1173 struct tty_driver *tty_drv;
1174
1175 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1176 if (!tty_drv)
1177 return -ENOMEM;
1178
1179 tty_drv->owner = THIS_MODULE;
1180 tty_drv->driver_name = "sdio_uart";
1181 tty_drv->name = "ttySDIO";
1182 tty_drv->major = 0; /* dynamically allocated */
1183 tty_drv->minor_start = 0;
1184 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1185 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1186 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1187 tty_drv->init_termios = tty_std_termios;
1188 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
2ba30eed
NP
1189 tty_drv->init_termios.c_ispeed = 4800;
1190 tty_drv->init_termios.c_ospeed = 4800;
6e418a9d
NP
1191 tty_set_operations(tty_drv, &sdio_uart_ops);
1192
1193 ret = tty_register_driver(tty_drv);
1194 if (ret)
1195 goto err1;
1196
1197 ret = sdio_register_driver(&sdio_uart_driver);
1198 if (ret)
1199 goto err2;
1200
1201 return 0;
1202
1203err2:
1204 tty_unregister_driver(tty_drv);
1205err1:
1206 put_tty_driver(tty_drv);
1207 return ret;
1208}
1209
1210static void __exit sdio_uart_exit(void)
1211{
1212 sdio_unregister_driver(&sdio_uart_driver);
1213 tty_unregister_driver(sdio_uart_tty_driver);
1214 put_tty_driver(sdio_uart_tty_driver);
1215}
1216
1217module_init(sdio_uart_init);
1218module_exit(sdio_uart_exit);
1219
1220MODULE_AUTHOR("Nicolas Pitre");
1221MODULE_LICENSE("GPL");
This page took 0.42331 seconds and 5 git commands to generate.