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