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