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