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