]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/card/sdio_uart.c
eeea84c309e691c8a7c838b527b95c7ce50c296c
[linux-2.6-omap-h63xx.git] / drivers / mmc / card / sdio_uart.c
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>
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
61 struct 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
74 struct 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;
82         struct task_struct      *in_sdio_uart_irq;
83         unsigned int            regs_offset;
84         struct circ_buf         xmit;
85         spinlock_t              write_lock;
86         struct uart_icount      icount;
87         unsigned int            uartclk;
88         unsigned int            mctrl;
89         unsigned int            read_status_mask;
90         unsigned int            ignore_status_mask;
91         unsigned char           x_char;
92         unsigned char           ier;
93         unsigned char           lcr;
94 };
95
96 static struct sdio_uart_port *sdio_uart_table[UART_NR];
97 static DEFINE_SPINLOCK(sdio_uart_table_lock);
98
99 static 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
122 static 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
138 static 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
145 static void sdio_uart_port_put(struct sdio_uart_port *port)
146 {
147         kref_put(&port->kref, sdio_uart_port_destroy);
148 }
149
150 static 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
183 static 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         }
190         if (likely(port->in_sdio_uart_irq != current))
191                 sdio_claim_host(port->func);
192         mutex_unlock(&port->func_lock);
193         return 0;
194 }
195
196 static inline void sdio_uart_release_func(struct sdio_uart_port *port)
197 {
198         if (likely(port->in_sdio_uart_irq != current))
199                 sdio_release_host(port->func);
200 }
201
202 static 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
209 static 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
214 static 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
233 static 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
251 static 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
265 static 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
366 static 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
374 static 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
382 static 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
389 static void sdio_uart_receive_chars(struct sdio_uart_port *port, unsigned int *status)
390 {
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
442 static 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
474 static 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  */
512 static 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
517         /*
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
528         iir = sdio_in(port, UART_IIR);
529         if (iir & UART_IIR_NO_INT)
530                 return;
531
532         port->in_sdio_uart_irq = current;
533         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);
539         port->in_sdio_uart_irq = NULL;
540 }
541
542 static 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
612 err3:
613         sdio_disable_func(port->func);
614 err2:
615         sdio_uart_release_func(port);
616 err1:
617         free_page((unsigned long)port->xmit.buf);
618         return ret;
619 }
620
621 static 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
656 skip:
657         /* Free the transmit buffer page. */
658         free_page((unsigned long)port->xmit.buf);
659 }
660
661 static 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
699 static 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
731 static 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
769 static 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
775 static 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
781 static 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
795 static 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
817 static 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
843 static 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
888 static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
889 {
890         struct sdio_uart_port *port = tty->driver_data;
891
892         if (sdio_uart_claim_func(port) != 0)
893                 return;
894
895         if (break_state == -1)
896                 port->lcr |= UART_LCR_SBC;
897         else
898                 port->lcr &= ~UART_LCR_SBC;
899         sdio_out(port, UART_LCR, port->lcr);
900
901         sdio_uart_release_func(port);
902 }
903
904 static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
905 {
906         struct sdio_uart_port *port = tty->driver_data;
907         int result;
908
909         result = sdio_uart_claim_func(port);
910         if (!result) {
911                 result = port->mctrl | sdio_uart_get_mctrl(port);
912                 sdio_uart_release_func(port);
913         }
914
915         return result;
916 }
917
918 static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
919                               unsigned int set, unsigned int clear)
920 {
921         struct sdio_uart_port *port = tty->driver_data;
922         int result;
923
924         result =sdio_uart_claim_func(port);
925         if(!result) {
926                 sdio_uart_update_mctrl(port, set, clear);
927                 sdio_uart_release_func(port);
928         }
929
930         return result;
931 }
932
933 static int sdio_uart_read_proc(char *page, char **start, off_t off,
934                                int count, int *eof, void *data)
935 {
936         int i, len = 0;
937         off_t begin = 0;
938
939         len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
940                        "", "", "");
941         for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
942                 struct sdio_uart_port *port = sdio_uart_port_get(i);
943                 if (port) {
944                         len += sprintf(page+len, "%d: uart:SDIO", i);
945                         if(capable(CAP_SYS_ADMIN)) {
946                                 len += sprintf(page + len, " tx:%d rx:%d",
947                                                port->icount.tx, port->icount.rx);
948                                 if (port->icount.frame)
949                                         len += sprintf(page + len, " fe:%d",
950                                                        port->icount.frame);
951                                 if (port->icount.parity)
952                                         len += sprintf(page + len, " pe:%d",
953                                                        port->icount.parity);
954                                 if (port->icount.brk)
955                                         len += sprintf(page + len, " brk:%d",
956                                                        port->icount.brk);
957                                 if (port->icount.overrun)
958                                         len += sprintf(page + len, " oe:%d",
959                                                        port->icount.overrun);
960                                 if (port->icount.cts)
961                                         len += sprintf(page + len, " cts:%d",
962                                                        port->icount.cts);
963                                 if (port->icount.dsr)
964                                         len += sprintf(page + len, " dsr:%d",
965                                                        port->icount.dsr);
966                                 if (port->icount.rng)
967                                         len += sprintf(page + len, " rng:%d",
968                                                        port->icount.rng);
969                                 if (port->icount.dcd)
970                                         len += sprintf(page + len, " dcd:%d",
971                                                        port->icount.dcd);
972                         }
973                         strcat(page, "\n");
974                         len++;
975                         sdio_uart_port_put(port);
976                 }
977
978                 if (len + begin > off + count)
979                         goto done;
980                 if (len + begin < off) {
981                         begin += len;
982                         len = 0;
983                 }
984         }
985         *eof = 1;
986
987 done:
988         if (off >= len + begin)
989                 return 0;
990         *start = page + (off - begin);
991         return (count < begin + len - off) ? count : (begin + len - off);
992 }
993
994 static const struct tty_operations sdio_uart_ops = {
995         .open                   = sdio_uart_open,
996         .close                  = sdio_uart_close,
997         .write                  = sdio_uart_write,
998         .write_room             = sdio_uart_write_room,
999         .chars_in_buffer        = sdio_uart_chars_in_buffer,
1000         .send_xchar             = sdio_uart_send_xchar,
1001         .throttle               = sdio_uart_throttle,
1002         .unthrottle             = sdio_uart_unthrottle,
1003         .set_termios            = sdio_uart_set_termios,
1004         .break_ctl              = sdio_uart_break_ctl,
1005         .tiocmget               = sdio_uart_tiocmget,
1006         .tiocmset               = sdio_uart_tiocmset,
1007         .read_proc              = sdio_uart_read_proc,
1008 };
1009
1010 static struct tty_driver *sdio_uart_tty_driver;
1011
1012 static int sdio_uart_probe(struct sdio_func *func,
1013                            const struct sdio_device_id *id)
1014 {
1015         struct sdio_uart_port *port;
1016         int ret;
1017
1018         port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1019         if (!port)
1020                 return -ENOMEM;
1021
1022         if (func->class == SDIO_CLASS_UART) {
1023                 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1024                        sdio_func_id(func));
1025                 kfree(port);
1026                 return -ENOSYS;
1027         } else if (func->class == SDIO_CLASS_GPS) {
1028                 /*
1029                  * We need tuple 0x91.  It contains SUBTPL_SIOREG
1030                  * and SUBTPL_RCVCAPS.
1031                  */
1032                 struct sdio_func_tuple *tpl;
1033                 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1034                         if (tpl->code != 0x91)
1035                                 continue;
1036                         if (tpl->size < 10)
1037                                 continue;
1038                         if (tpl->data[1] == 0)  /* SUBTPL_SIOREG */
1039                                 break;
1040                 }
1041                 if (!tpl) {
1042                         printk(KERN_WARNING
1043                                "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1044                                sdio_func_id(func));
1045                         kfree(port);
1046                         return -EINVAL;
1047                 }
1048                 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1049                        sdio_func_id(func), tpl->data[2], tpl->data[3]);
1050                 port->regs_offset = (tpl->data[4] << 0) |
1051                                     (tpl->data[5] << 8) |
1052                                     (tpl->data[6] << 16);
1053                 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1054                        sdio_func_id(func), port->regs_offset);
1055                 port->uartclk = tpl->data[7] * 115200;
1056                 if (port->uartclk == 0)
1057                         port->uartclk = 115200;
1058                 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1059                        sdio_func_id(func), port->uartclk,
1060                        tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1061         } else {
1062                 kfree(port);
1063                 return -EINVAL;
1064         }
1065
1066         port->func = func;
1067         sdio_set_drvdata(func, port);
1068
1069         ret = sdio_uart_add_port(port);
1070         if (ret) {
1071                 kfree(port);
1072         } else {
1073                 struct device *dev;
1074                 dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
1075                 if (IS_ERR(dev)) {
1076                         sdio_uart_port_remove(port);
1077                         ret = PTR_ERR(dev);
1078                 }
1079         }
1080
1081         return ret;
1082 }
1083
1084 static void sdio_uart_remove(struct sdio_func *func)
1085 {
1086         struct sdio_uart_port *port = sdio_get_drvdata(func);
1087
1088         tty_unregister_device(sdio_uart_tty_driver, port->index);
1089         sdio_uart_port_remove(port);
1090 }
1091
1092 static const struct sdio_device_id sdio_uart_ids[] = {
1093         { SDIO_DEVICE_CLASS(SDIO_CLASS_UART)            },
1094         { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS)             },
1095         { /* end: all zeroes */                         },
1096 };
1097
1098 MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1099
1100 static struct sdio_driver sdio_uart_driver = {
1101         .probe          = sdio_uart_probe,
1102         .remove         = sdio_uart_remove,
1103         .name           = "sdio_uart",
1104         .id_table       = sdio_uart_ids,
1105 };
1106
1107 static int __init sdio_uart_init(void)
1108 {
1109         int ret;
1110         struct tty_driver *tty_drv;
1111
1112         sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1113         if (!tty_drv)
1114                 return -ENOMEM;
1115
1116         tty_drv->owner = THIS_MODULE;
1117         tty_drv->driver_name = "sdio_uart";
1118         tty_drv->name =   "ttySDIO";
1119         tty_drv->major = 0;  /* dynamically allocated */
1120         tty_drv->minor_start = 0;
1121         tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1122         tty_drv->subtype = SERIAL_TYPE_NORMAL;
1123         tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1124         tty_drv->init_termios = tty_std_termios;
1125         tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1126         tty_drv->init_termios.c_ispeed = 4800;
1127         tty_drv->init_termios.c_ospeed = 4800;
1128         tty_set_operations(tty_drv, &sdio_uart_ops);
1129
1130         ret = tty_register_driver(tty_drv);
1131         if (ret)
1132                 goto err1;
1133
1134         ret = sdio_register_driver(&sdio_uart_driver);
1135         if (ret)
1136                 goto err2;
1137
1138         return 0;
1139
1140 err2:
1141         tty_unregister_driver(tty_drv);
1142 err1:
1143         put_tty_driver(tty_drv);
1144         return ret;
1145 }
1146
1147 static void __exit sdio_uart_exit(void)
1148 {
1149         sdio_unregister_driver(&sdio_uart_driver);
1150         tty_unregister_driver(sdio_uart_tty_driver);
1151         put_tty_driver(sdio_uart_tty_driver);
1152 }
1153
1154 module_init(sdio_uart_init);
1155 module_exit(sdio_uart_exit);
1156
1157 MODULE_AUTHOR("Nicolas Pitre");
1158 MODULE_LICENSE("GPL");