blob: 78ad48718ab028e61b1d77d83e29de8efbe9930c [file] [log] [blame]
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001/*
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>
33#include <linux/serial_reg.h>
34#include <linux/circ_buf.h>
35#include <linux/gfp.h>
36#include <linux/tty.h>
37#include <linux/tty_flip.h>
38
39#include <linux/mmc/core.h>
40#include <linux/mmc/card.h>
41#include <linux/mmc/sdio_func.h>
42#include <linux/mmc/sdio_ids.h>
43
44
45#define UART_NR 8 /* Number of UARTs this driver can handle */
46
47
48#define UART_XMIT_SIZE PAGE_SIZE
49#define WAKEUP_CHARS 256
50
51#define circ_empty(circ) ((circ)->head == (circ)->tail)
52#define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
53
54#define circ_chars_pending(circ) \
55 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
56
57#define circ_chars_free(circ) \
58 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
59
60
61struct uart_icount {
62 __u32 cts;
63 __u32 dsr;
64 __u32 rng;
65 __u32 dcd;
66 __u32 rx;
67 __u32 tx;
68 __u32 frame;
69 __u32 overrun;
70 __u32 parity;
71 __u32 brk;
72};
73
74struct sdio_uart_port {
75 struct kref kref;
76 struct tty_struct *tty;
77 unsigned int index;
78 unsigned int opened;
79 struct mutex open_lock;
80 struct sdio_func *func;
81 struct mutex func_lock;
Nicolas Pitre15b82b42007-08-20 17:17:37 -040082 struct task_struct *in_sdio_uart_irq;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040083 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);
104 mutex_init(&port->open_lock);
105 mutex_init(&port->func_lock);
106 spin_lock_init(&port->write_lock);
107
108 spin_lock(&sdio_uart_table_lock);
109 for (index = 0; index < UART_NR; index++) {
110 if (!sdio_uart_table[index]) {
111 port->index = index;
112 sdio_uart_table[index] = port;
113 ret = 0;
114 break;
115 }
116 }
117 spin_unlock(&sdio_uart_table_lock);
118
119 return ret;
120}
121
122static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
123{
124 struct sdio_uart_port *port;
125
126 if (index >= UART_NR)
127 return NULL;
128
129 spin_lock(&sdio_uart_table_lock);
130 port = sdio_uart_table[index];
131 if (port)
132 kref_get(&port->kref);
133 spin_unlock(&sdio_uart_table_lock);
134
135 return port;
136}
137
138static void sdio_uart_port_destroy(struct kref *kref)
139{
140 struct sdio_uart_port *port =
141 container_of(kref, struct sdio_uart_port, kref);
142 kfree(port);
143}
144
145static void sdio_uart_port_put(struct sdio_uart_port *port)
146{
147 kref_put(&port->kref, sdio_uart_port_destroy);
148}
149
150static void sdio_uart_port_remove(struct sdio_uart_port *port)
151{
152 struct sdio_func *func;
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 */
167 mutex_lock(&port->open_lock);
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);
173 if (port->opened)
174 tty_hangup(port->tty);
175 mutex_unlock(&port->open_lock);
176 sdio_release_irq(func);
177 sdio_disable_func(func);
178 sdio_release_host(func);
179
180 sdio_uart_port_put(port);
181}
182
183static int sdio_uart_claim_func(struct sdio_uart_port *port)
184{
185 mutex_lock(&port->func_lock);
186 if (unlikely(!port->func)) {
187 mutex_unlock(&port->func_lock);
188 return -ENODEV;
189 }
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400190 if (likely(port->in_sdio_uart_irq != current))
191 sdio_claim_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400192 mutex_unlock(&port->func_lock);
193 return 0;
194}
195
196static inline void sdio_uart_release_func(struct sdio_uart_port *port)
197{
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400198 if (likely(port->in_sdio_uart_irq != current))
199 sdio_release_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400200}
201
202static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
203{
204 unsigned char c;
205 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
206 return c;
207}
208
209static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
210{
211 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
212}
213
214static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
215{
216 unsigned char status;
217 unsigned int ret;
218
219 status = sdio_in(port, UART_MSR);
220
221 ret = 0;
222 if (status & UART_MSR_DCD)
223 ret |= TIOCM_CAR;
224 if (status & UART_MSR_RI)
225 ret |= TIOCM_RNG;
226 if (status & UART_MSR_DSR)
227 ret |= TIOCM_DSR;
228 if (status & UART_MSR_CTS)
229 ret |= TIOCM_CTS;
230 return ret;
231}
232
233static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
234{
235 unsigned char mcr = 0;
236
237 if (mctrl & TIOCM_RTS)
238 mcr |= UART_MCR_RTS;
239 if (mctrl & TIOCM_DTR)
240 mcr |= UART_MCR_DTR;
241 if (mctrl & TIOCM_OUT1)
242 mcr |= UART_MCR_OUT1;
243 if (mctrl & TIOCM_OUT2)
244 mcr |= UART_MCR_OUT2;
245 if (mctrl & TIOCM_LOOP)
246 mcr |= UART_MCR_LOOP;
247
248 sdio_out(port, UART_MCR, mcr);
249}
250
251static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
252 unsigned int set, unsigned int clear)
253{
254 unsigned int old;
255
256 old = port->mctrl;
257 port->mctrl = (old & ~clear) | set;
258 if (old != port->mctrl)
259 sdio_uart_write_mctrl(port, port->mctrl);
260}
261
262#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
263#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
264
265static void sdio_uart_change_speed(struct sdio_uart_port *port,
266 struct ktermios *termios,
267 struct ktermios *old)
268{
269 unsigned char cval, fcr = 0;
270 unsigned int baud, quot;
271
272 switch (termios->c_cflag & CSIZE) {
273 case CS5:
274 cval = UART_LCR_WLEN5;
275 break;
276 case CS6:
277 cval = UART_LCR_WLEN6;
278 break;
279 case CS7:
280 cval = UART_LCR_WLEN7;
281 break;
282 default:
283 case CS8:
284 cval = UART_LCR_WLEN8;
285 break;
286 }
287
288 if (termios->c_cflag & CSTOPB)
289 cval |= UART_LCR_STOP;
290 if (termios->c_cflag & PARENB)
291 cval |= UART_LCR_PARITY;
292 if (!(termios->c_cflag & PARODD))
293 cval |= UART_LCR_EPAR;
294
295 for (;;) {
296 baud = tty_termios_baud_rate(termios);
297 if (baud == 0)
298 baud = 9600; /* Special case: B0 rate. */
299 if (baud <= port->uartclk)
300 break;
301 /*
302 * Oops, the quotient was zero. Try again with the old
303 * baud rate if possible, otherwise default to 9600.
304 */
305 termios->c_cflag &= ~CBAUD;
306 if (old) {
307 termios->c_cflag |= old->c_cflag & CBAUD;
308 old = NULL;
309 } else
310 termios->c_cflag |= B9600;
311 }
312 quot = (2 * port->uartclk + baud) / (2 * baud);
313
314 if (baud < 2400)
315 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
316 else
317 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
318
319 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
320 if (termios->c_iflag & INPCK)
321 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
322 if (termios->c_iflag & (BRKINT | PARMRK))
323 port->read_status_mask |= UART_LSR_BI;
324
325 /*
326 * Characters to ignore
327 */
328 port->ignore_status_mask = 0;
329 if (termios->c_iflag & IGNPAR)
330 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
331 if (termios->c_iflag & IGNBRK) {
332 port->ignore_status_mask |= UART_LSR_BI;
333 /*
334 * If we're ignoring parity and break indicators,
335 * ignore overruns too (for real raw support).
336 */
337 if (termios->c_iflag & IGNPAR)
338 port->ignore_status_mask |= UART_LSR_OE;
339 }
340
341 /*
342 * ignore all characters if CREAD is not set
343 */
344 if ((termios->c_cflag & CREAD) == 0)
345 port->ignore_status_mask |= UART_LSR_DR;
346
347 /*
348 * CTS flow control flag and modem status interrupts
349 */
350 port->ier &= ~UART_IER_MSI;
351 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
352 port->ier |= UART_IER_MSI;
353
354 port->lcr = cval;
355
356 sdio_out(port, UART_IER, port->ier);
357 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
358 sdio_out(port, UART_DLL, quot & 0xff);
359 sdio_out(port, UART_DLM, quot >> 8);
360 sdio_out(port, UART_LCR, cval);
361 sdio_out(port, UART_FCR, fcr);
362
363 sdio_uart_write_mctrl(port, port->mctrl);
364}
365
366static void sdio_uart_start_tx(struct sdio_uart_port *port)
367{
368 if (!(port->ier & UART_IER_THRI)) {
369 port->ier |= UART_IER_THRI;
370 sdio_out(port, UART_IER, port->ier);
371 }
372}
373
374static void sdio_uart_stop_tx(struct sdio_uart_port *port)
375{
376 if (port->ier & UART_IER_THRI) {
377 port->ier &= ~UART_IER_THRI;
378 sdio_out(port, UART_IER, port->ier);
379 }
380}
381
382static void sdio_uart_stop_rx(struct sdio_uart_port *port)
383{
384 port->ier &= ~UART_IER_RLSI;
385 port->read_status_mask &= ~UART_LSR_DR;
386 sdio_out(port, UART_IER, port->ier);
387}
388
Andre Haupta960d5d2007-11-21 12:33:45 +0100389static void sdio_uart_receive_chars(struct sdio_uart_port *port, unsigned int *status)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400390{
391 struct tty_struct *tty = port->tty;
392 unsigned int ch, flag;
393 int max_count = 256;
394
395 do {
396 ch = sdio_in(port, UART_RX);
397 flag = TTY_NORMAL;
398 port->icount.rx++;
399
400 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
401 UART_LSR_FE | UART_LSR_OE))) {
402 /*
403 * For statistics only
404 */
405 if (*status & UART_LSR_BI) {
406 *status &= ~(UART_LSR_FE | UART_LSR_PE);
407 port->icount.brk++;
408 } else if (*status & UART_LSR_PE)
409 port->icount.parity++;
410 else if (*status & UART_LSR_FE)
411 port->icount.frame++;
412 if (*status & UART_LSR_OE)
413 port->icount.overrun++;
414
415 /*
416 * Mask off conditions which should be ignored.
417 */
418 *status &= port->read_status_mask;
419 if (*status & UART_LSR_BI) {
420 flag = TTY_BREAK;
421 } else if (*status & UART_LSR_PE)
422 flag = TTY_PARITY;
423 else if (*status & UART_LSR_FE)
424 flag = TTY_FRAME;
425 }
426
427 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
428 tty_insert_flip_char(tty, ch, flag);
429
430 /*
431 * Overrun is special. Since it's reported immediately,
432 * it doesn't affect the current character.
433 */
434 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
435 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
436
437 *status = sdio_in(port, UART_LSR);
438 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
439 tty_flip_buffer_push(tty);
440}
441
442static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
443{
444 struct circ_buf *xmit = &port->xmit;
445 int count;
446
447 if (port->x_char) {
448 sdio_out(port, UART_TX, port->x_char);
449 port->icount.tx++;
450 port->x_char = 0;
451 return;
452 }
453 if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
454 sdio_uart_stop_tx(port);
455 return;
456 }
457
458 count = 16;
459 do {
460 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
461 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
462 port->icount.tx++;
463 if (circ_empty(xmit))
464 break;
465 } while (--count > 0);
466
467 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
468 tty_wakeup(port->tty);
469
470 if (circ_empty(xmit))
471 sdio_uart_stop_tx(port);
472}
473
474static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
475{
476 int status;
477
478 status = sdio_in(port, UART_MSR);
479
480 if ((status & UART_MSR_ANY_DELTA) == 0)
481 return;
482
483 if (status & UART_MSR_TERI)
484 port->icount.rng++;
485 if (status & UART_MSR_DDSR)
486 port->icount.dsr++;
487 if (status & UART_MSR_DDCD)
488 port->icount.dcd++;
489 if (status & UART_MSR_DCTS) {
490 port->icount.cts++;
491 if (port->tty->termios->c_cflag & CRTSCTS) {
492 int cts = (status & UART_MSR_CTS);
493 if (port->tty->hw_stopped) {
494 if (cts) {
495 port->tty->hw_stopped = 0;
496 sdio_uart_start_tx(port);
497 tty_wakeup(port->tty);
498 }
499 } else {
500 if (!cts) {
501 port->tty->hw_stopped = 1;
502 sdio_uart_stop_tx(port);
503 }
504 }
505 }
506 }
507}
508
509/*
510 * This handles the interrupt from one port.
511 */
512static void sdio_uart_irq(struct sdio_func *func)
513{
514 struct sdio_uart_port *port = sdio_get_drvdata(func);
515 unsigned int iir, lsr;
516
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400517 /*
518 * In a few places sdio_uart_irq() is called directly instead of
519 * waiting for the actual interrupt to be raised and the SDIO IRQ
520 * thread scheduled in order to reduce latency. However, some
521 * interaction with the tty core may end up calling us back
522 * (serial echo, flow control, etc.) through those same places
523 * causing undesirable effects. Let's stop the recursion here.
524 */
525 if (unlikely(port->in_sdio_uart_irq == current))
526 return;
527
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400528 iir = sdio_in(port, UART_IIR);
529 if (iir & UART_IIR_NO_INT)
530 return;
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400531
532 port->in_sdio_uart_irq = current;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400533 lsr = sdio_in(port, UART_LSR);
534 if (lsr & UART_LSR_DR)
535 sdio_uart_receive_chars(port, &lsr);
536 sdio_uart_check_modem_status(port);
537 if (lsr & UART_LSR_THRE)
538 sdio_uart_transmit_chars(port);
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400539 port->in_sdio_uart_irq = NULL;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400540}
541
542static int sdio_uart_startup(struct sdio_uart_port *port)
543{
544 unsigned long page;
545 int ret;
546
547 /*
548 * Set the TTY IO error marker - we will only clear this
549 * once we have successfully opened the port.
550 */
551 set_bit(TTY_IO_ERROR, &port->tty->flags);
552
553 /* Initialise and allocate the transmit buffer. */
554 page = __get_free_page(GFP_KERNEL);
555 if (!page)
556 return -ENOMEM;
557 port->xmit.buf = (unsigned char *)page;
558 circ_clear(&port->xmit);
559
560 ret = sdio_uart_claim_func(port);
561 if (ret)
562 goto err1;
563 ret = sdio_enable_func(port->func);
564 if (ret)
565 goto err2;
566 ret = sdio_claim_irq(port->func, sdio_uart_irq);
567 if (ret)
568 goto err3;
569
570 /*
571 * Clear the FIFO buffers and disable them.
572 * (they will be reenabled in sdio_change_speed())
573 */
574 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
575 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
576 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
577 sdio_out(port, UART_FCR, 0);
578
579 /*
580 * Clear the interrupt registers.
581 */
582 (void) sdio_in(port, UART_LSR);
583 (void) sdio_in(port, UART_RX);
584 (void) sdio_in(port, UART_IIR);
585 (void) sdio_in(port, UART_MSR);
586
587 /*
588 * Now, initialize the UART
589 */
590 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
591
592 port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
593 port->mctrl = TIOCM_OUT2;
594
595 sdio_uart_change_speed(port, port->tty->termios, NULL);
596
597 if (port->tty->termios->c_cflag & CBAUD)
598 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
599
600 if (port->tty->termios->c_cflag & CRTSCTS)
601 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
602 port->tty->hw_stopped = 1;
603
604 clear_bit(TTY_IO_ERROR, &port->tty->flags);
605
606 /* Kick the IRQ handler once while we're still holding the host lock */
607 sdio_uart_irq(port->func);
608
609 sdio_uart_release_func(port);
610 return 0;
611
612err3:
613 sdio_disable_func(port->func);
614err2:
615 sdio_uart_release_func(port);
616err1:
617 free_page((unsigned long)port->xmit.buf);
618 return ret;
619}
620
621static void sdio_uart_shutdown(struct sdio_uart_port *port)
622{
623 int ret;
624
625 ret = sdio_uart_claim_func(port);
626 if (ret)
627 goto skip;
628
629 sdio_uart_stop_rx(port);
630
631 /* TODO: wait here for TX FIFO to drain */
632
633 /* Turn off DTR and RTS early. */
634 if (port->tty->termios->c_cflag & HUPCL)
635 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
636
637 /* Disable interrupts from this port */
638 sdio_release_irq(port->func);
639 port->ier = 0;
640 sdio_out(port, UART_IER, 0);
641
642 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
643
644 /* Disable break condition and FIFOs. */
645 port->lcr &= ~UART_LCR_SBC;
646 sdio_out(port, UART_LCR, port->lcr);
647 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
648 UART_FCR_CLEAR_RCVR |
649 UART_FCR_CLEAR_XMIT);
650 sdio_out(port, UART_FCR, 0);
651
652 sdio_disable_func(port->func);
653
654 sdio_uart_release_func(port);
655
656skip:
657 /* Free the transmit buffer page. */
658 free_page((unsigned long)port->xmit.buf);
659}
660
661static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
662{
663 struct sdio_uart_port *port;
664 int ret;
665
666 port = sdio_uart_port_get(tty->index);
667 if (!port)
668 return -ENODEV;
669
670 mutex_lock(&port->open_lock);
671
672 /*
673 * Make sure not to mess up with a dead port
674 * which has not been closed yet.
675 */
676 if (tty->driver_data && tty->driver_data != port) {
677 mutex_unlock(&port->open_lock);
678 sdio_uart_port_put(port);
679 return -EBUSY;
680 }
681
682 if (!port->opened) {
683 tty->driver_data = port;
684 port->tty = tty;
685 ret = sdio_uart_startup(port);
686 if (ret) {
687 tty->driver_data = NULL;
688 port->tty = NULL;
689 mutex_unlock(&port->open_lock);
690 sdio_uart_port_put(port);
691 return ret;
692 }
693 }
694 port->opened++;
695 mutex_unlock(&port->open_lock);
696 return 0;
697}
698
699static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
700{
701 struct sdio_uart_port *port = tty->driver_data;
702
703 if (!port)
704 return;
705
706 mutex_lock(&port->open_lock);
707 BUG_ON(!port->opened);
708
709 /*
710 * This is messy. The tty layer calls us even when open()
711 * returned an error. Ignore this close request if tty->count
712 * is larger than port->count.
713 */
714 if (tty->count > port->opened) {
715 mutex_unlock(&port->open_lock);
716 return;
717 }
718
719 if (--port->opened == 0) {
720 tty->closing = 1;
721 sdio_uart_shutdown(port);
722 tty_ldisc_flush(tty);
723 port->tty = NULL;
724 tty->driver_data = NULL;
725 tty->closing = 0;
726 }
727 mutex_unlock(&port->open_lock);
728 sdio_uart_port_put(port);
729}
730
731static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
732 int count)
733{
734 struct sdio_uart_port *port = tty->driver_data;
735 struct circ_buf *circ = &port->xmit;
736 int c, ret = 0;
737
738 if (!port->func)
739 return -ENODEV;
740
741 spin_lock(&port->write_lock);
742 while (1) {
743 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
744 if (count < c)
745 c = count;
746 if (c <= 0)
747 break;
748 memcpy(circ->buf + circ->head, buf, c);
749 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
750 buf += c;
751 count -= c;
752 ret += c;
753 }
754 spin_unlock(&port->write_lock);
755
756 if ( !(port->ier & UART_IER_THRI)) {
757 int err = sdio_uart_claim_func(port);
758 if (!err) {
759 sdio_uart_start_tx(port);
760 sdio_uart_irq(port->func);
761 sdio_uart_release_func(port);
762 } else
763 ret = err;
764 }
765
766 return ret;
767}
768
769static int sdio_uart_write_room(struct tty_struct *tty)
770{
771 struct sdio_uart_port *port = tty->driver_data;
772 return port ? circ_chars_free(&port->xmit) : 0;
773}
774
775static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
776{
777 struct sdio_uart_port *port = tty->driver_data;
778 return port ? circ_chars_pending(&port->xmit) : 0;
779}
780
781static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
782{
783 struct sdio_uart_port *port = tty->driver_data;
784
785 port->x_char = ch;
786 if (ch && !(port->ier & UART_IER_THRI)) {
787 if (sdio_uart_claim_func(port) != 0)
788 return;
789 sdio_uart_start_tx(port);
790 sdio_uart_irq(port->func);
791 sdio_uart_release_func(port);
792 }
793}
794
795static void sdio_uart_throttle(struct tty_struct *tty)
796{
797 struct sdio_uart_port *port = tty->driver_data;
798
799 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
800 return;
801
802 if (sdio_uart_claim_func(port) != 0)
803 return;
804
805 if (I_IXOFF(tty)) {
806 port->x_char = STOP_CHAR(tty);
807 sdio_uart_start_tx(port);
808 }
809
810 if (tty->termios->c_cflag & CRTSCTS)
811 sdio_uart_clear_mctrl(port, TIOCM_RTS);
812
813 sdio_uart_irq(port->func);
814 sdio_uart_release_func(port);
815}
816
817static void sdio_uart_unthrottle(struct tty_struct *tty)
818{
819 struct sdio_uart_port *port = tty->driver_data;
820
821 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
822 return;
823
824 if (sdio_uart_claim_func(port) != 0)
825 return;
826
827 if (I_IXOFF(tty)) {
828 if (port->x_char) {
829 port->x_char = 0;
830 } else {
831 port->x_char = START_CHAR(tty);
832 sdio_uart_start_tx(port);
833 }
834 }
835
836 if (tty->termios->c_cflag & CRTSCTS)
837 sdio_uart_set_mctrl(port, TIOCM_RTS);
838
839 sdio_uart_irq(port->func);
840 sdio_uart_release_func(port);
841}
842
843static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
844{
845 struct sdio_uart_port *port = tty->driver_data;
846 unsigned int cflag = tty->termios->c_cflag;
847
848#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
849
850 if ((cflag ^ old_termios->c_cflag) == 0 &&
851 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
852 return;
853
854 if (sdio_uart_claim_func(port) != 0)
855 return;
856
857 sdio_uart_change_speed(port, tty->termios, old_termios);
858
859 /* Handle transition to B0 status */
860 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
861 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
862
863 /* Handle transition away from B0 status */
864 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
865 unsigned int mask = TIOCM_DTR;
866 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
867 mask |= TIOCM_RTS;
868 sdio_uart_set_mctrl(port, mask);
869 }
870
871 /* Handle turning off CRTSCTS */
872 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
873 tty->hw_stopped = 0;
874 sdio_uart_start_tx(port);
875 }
876
877 /* Handle turning on CRTSCTS */
878 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
879 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
880 tty->hw_stopped = 1;
881 sdio_uart_stop_tx(port);
882 }
883 }
884
885 sdio_uart_release_func(port);
886}
887
David Howellsc43d8632008-07-10 12:28:48 +0100888static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400889{
890 struct sdio_uart_port *port = tty->driver_data;
David Howellsc43d8632008-07-10 12:28:48 +0100891 int result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400892
David Howellsc43d8632008-07-10 12:28:48 +0100893 result = sdio_uart_claim_func(port);
894 if (result != 0)
895 return result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400896
897 if (break_state == -1)
898 port->lcr |= UART_LCR_SBC;
899 else
900 port->lcr &= ~UART_LCR_SBC;
901 sdio_out(port, UART_LCR, port->lcr);
902
903 sdio_uart_release_func(port);
David Howellsc43d8632008-07-10 12:28:48 +0100904 return 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400905}
906
907static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
908{
909 struct sdio_uart_port *port = tty->driver_data;
910 int result;
911
912 result = sdio_uart_claim_func(port);
913 if (!result) {
914 result = port->mctrl | sdio_uart_get_mctrl(port);
915 sdio_uart_release_func(port);
916 }
917
918 return result;
919}
920
921static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
922 unsigned int set, unsigned int clear)
923{
924 struct sdio_uart_port *port = tty->driver_data;
925 int result;
926
927 result =sdio_uart_claim_func(port);
928 if(!result) {
929 sdio_uart_update_mctrl(port, set, clear);
930 sdio_uart_release_func(port);
931 }
932
933 return result;
934}
935
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400936static int sdio_uart_read_proc(char *page, char **start, off_t off,
937 int count, int *eof, void *data)
938{
939 int i, len = 0;
940 off_t begin = 0;
941
942 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
943 "", "", "");
944 for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
945 struct sdio_uart_port *port = sdio_uart_port_get(i);
946 if (port) {
947 len += sprintf(page+len, "%d: uart:SDIO", i);
948 if(capable(CAP_SYS_ADMIN)) {
949 len += sprintf(page + len, " tx:%d rx:%d",
950 port->icount.tx, port->icount.rx);
951 if (port->icount.frame)
952 len += sprintf(page + len, " fe:%d",
953 port->icount.frame);
954 if (port->icount.parity)
955 len += sprintf(page + len, " pe:%d",
956 port->icount.parity);
957 if (port->icount.brk)
958 len += sprintf(page + len, " brk:%d",
959 port->icount.brk);
960 if (port->icount.overrun)
961 len += sprintf(page + len, " oe:%d",
962 port->icount.overrun);
963 if (port->icount.cts)
964 len += sprintf(page + len, " cts:%d",
965 port->icount.cts);
966 if (port->icount.dsr)
967 len += sprintf(page + len, " dsr:%d",
968 port->icount.dsr);
969 if (port->icount.rng)
970 len += sprintf(page + len, " rng:%d",
971 port->icount.rng);
972 if (port->icount.dcd)
973 len += sprintf(page + len, " dcd:%d",
974 port->icount.dcd);
975 }
976 strcat(page, "\n");
977 len++;
978 sdio_uart_port_put(port);
979 }
980
981 if (len + begin > off + count)
982 goto done;
983 if (len + begin < off) {
984 begin += len;
985 len = 0;
986 }
987 }
988 *eof = 1;
989
990done:
991 if (off >= len + begin)
992 return 0;
993 *start = page + (off - begin);
994 return (count < begin + len - off) ? count : (begin + len - off);
995}
996
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400997static 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,
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001010 .read_proc = sdio_uart_read_proc,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001011};
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;
Nicolas Pitre2ba30ee2007-08-15 13:27:29 -04001129 tty_drv->init_termios.c_ispeed = 4800;
1130 tty_drv->init_termios.c_ospeed = 4800;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001131 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");