blob: 95027b03c6463ba3c45fbfea2e7f78f9c89159bb [file] [log] [blame]
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001/*
2 * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
3 *
4 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
5 * by Russell King.
6 *
7 * Author: Nicolas Pitre
8 * Created: June 15, 2007
9 * Copyright: MontaVista Software, Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 */
16
17/*
18 * Note: Although this driver assumes a 16550A-like UART implementation,
19 * it is not possible to leverage the common 8250/16550 driver, nor the
20 * core UART infrastructure, as they assumes direct access to the hardware
21 * registers, often under a spinlock. This is not possible in the SDIO
22 * context as SDIO access functions must be able to sleep.
23 *
24 * Because we need to lock the SDIO host to ensure an exclusive access to
25 * the card, we simply rely on that lock to also prevent and serialize
26 * concurrent access to the same port.
27 */
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/kernel.h>
32#include <linux/mutex.h>
Alexey Dobriyan201a50b2009-03-31 15:19:20 -070033#include <linux/seq_file.h>
Nicolas Pitre6e418a92007-06-30 02:04:21 -040034#include <linux/serial_reg.h>
35#include <linux/circ_buf.h>
36#include <linux/gfp.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39
40#include <linux/mmc/core.h>
41#include <linux/mmc/card.h>
42#include <linux/mmc/sdio_func.h>
43#include <linux/mmc/sdio_ids.h>
44
45
46#define UART_NR 8 /* Number of UARTs this driver can handle */
47
48
49#define UART_XMIT_SIZE PAGE_SIZE
50#define WAKEUP_CHARS 256
51
52#define circ_empty(circ) ((circ)->head == (circ)->tail)
53#define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
54
55#define circ_chars_pending(circ) \
56 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
57
58#define circ_chars_free(circ) \
59 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
60
61
62struct uart_icount {
63 __u32 cts;
64 __u32 dsr;
65 __u32 rng;
66 __u32 dcd;
67 __u32 rx;
68 __u32 tx;
69 __u32 frame;
70 __u32 overrun;
71 __u32 parity;
72 __u32 brk;
73};
74
75struct sdio_uart_port {
Alan Coxb5849b12009-11-05 13:28:06 +000076 struct tty_port port;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040077 struct kref kref;
78 struct tty_struct *tty;
79 unsigned int index;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040080 struct sdio_func *func;
81 struct mutex func_lock;
Nicolas Pitre15b82b42007-08-20 17:17:37 -040082 struct task_struct *in_sdio_uart_irq;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040083 unsigned int regs_offset;
84 struct circ_buf xmit;
85 spinlock_t write_lock;
86 struct uart_icount icount;
87 unsigned int uartclk;
88 unsigned int mctrl;
89 unsigned int read_status_mask;
90 unsigned int ignore_status_mask;
91 unsigned char x_char;
92 unsigned char ier;
93 unsigned char lcr;
94};
95
96static struct sdio_uart_port *sdio_uart_table[UART_NR];
97static DEFINE_SPINLOCK(sdio_uart_table_lock);
98
99static int sdio_uart_add_port(struct sdio_uart_port *port)
100{
101 int index, ret = -EBUSY;
102
103 kref_init(&port->kref);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400104 mutex_init(&port->func_lock);
105 spin_lock_init(&port->write_lock);
106
107 spin_lock(&sdio_uart_table_lock);
108 for (index = 0; index < UART_NR; index++) {
109 if (!sdio_uart_table[index]) {
110 port->index = index;
111 sdio_uart_table[index] = port;
112 ret = 0;
113 break;
114 }
115 }
116 spin_unlock(&sdio_uart_table_lock);
117
118 return ret;
119}
120
121static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
122{
123 struct sdio_uart_port *port;
124
125 if (index >= UART_NR)
126 return NULL;
127
128 spin_lock(&sdio_uart_table_lock);
129 port = sdio_uart_table[index];
130 if (port)
131 kref_get(&port->kref);
132 spin_unlock(&sdio_uart_table_lock);
133
134 return port;
135}
136
137static void sdio_uart_port_destroy(struct kref *kref)
138{
139 struct sdio_uart_port *port =
140 container_of(kref, struct sdio_uart_port, kref);
141 kfree(port);
142}
143
144static void sdio_uart_port_put(struct sdio_uart_port *port)
145{
146 kref_put(&port->kref, sdio_uart_port_destroy);
147}
148
149static void sdio_uart_port_remove(struct sdio_uart_port *port)
150{
151 struct sdio_func *func;
Alan Cox584abc32009-11-30 13:16:09 +0000152 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400153
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 */
Alan Cox0a68f642009-11-05 13:28:38 +0000167 mutex_lock(&port->port.mutex);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400168 mutex_lock(&port->func_lock);
169 func = port->func;
170 sdio_claim_host(func);
171 port->func = NULL;
172 mutex_unlock(&port->func_lock);
Alan Cox584abc32009-11-30 13:16:09 +0000173 tty = tty_port_tty_get(&port->port);
174 /* tty_hangup is async so is this safe as is ?? */
175 if (tty) {
176 tty_hangup(tty);
Alan Cox530646f2009-11-05 13:28:29 +0000177 tty_kref_put(tty);
178 }
Alan Cox0a68f642009-11-05 13:28:38 +0000179 mutex_unlock(&port->port.mutex);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400180 sdio_release_irq(func);
181 sdio_disable_func(func);
182 sdio_release_host(func);
183
184 sdio_uart_port_put(port);
185}
186
187static int sdio_uart_claim_func(struct sdio_uart_port *port)
188{
189 mutex_lock(&port->func_lock);
190 if (unlikely(!port->func)) {
191 mutex_unlock(&port->func_lock);
192 return -ENODEV;
193 }
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400194 if (likely(port->in_sdio_uart_irq != current))
195 sdio_claim_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400196 mutex_unlock(&port->func_lock);
197 return 0;
198}
199
200static inline void sdio_uart_release_func(struct sdio_uart_port *port)
201{
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400202 if (likely(port->in_sdio_uart_irq != current))
203 sdio_release_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400204}
205
206static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
207{
208 unsigned char c;
209 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
210 return c;
211}
212
213static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
214{
215 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
216}
217
218static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
219{
220 unsigned char status;
221 unsigned int ret;
222
223 status = sdio_in(port, UART_MSR);
224
225 ret = 0;
226 if (status & UART_MSR_DCD)
227 ret |= TIOCM_CAR;
228 if (status & UART_MSR_RI)
229 ret |= TIOCM_RNG;
230 if (status & UART_MSR_DSR)
231 ret |= TIOCM_DSR;
232 if (status & UART_MSR_CTS)
233 ret |= TIOCM_CTS;
234 return ret;
235}
236
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300237static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
238 unsigned int mctrl)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400239{
240 unsigned char mcr = 0;
241
242 if (mctrl & TIOCM_RTS)
243 mcr |= UART_MCR_RTS;
244 if (mctrl & TIOCM_DTR)
245 mcr |= UART_MCR_DTR;
246 if (mctrl & TIOCM_OUT1)
247 mcr |= UART_MCR_OUT1;
248 if (mctrl & TIOCM_OUT2)
249 mcr |= UART_MCR_OUT2;
250 if (mctrl & TIOCM_LOOP)
251 mcr |= UART_MCR_LOOP;
252
253 sdio_out(port, UART_MCR, mcr);
254}
255
256static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
257 unsigned int set, unsigned int clear)
258{
259 unsigned int old;
260
261 old = port->mctrl;
262 port->mctrl = (old & ~clear) | set;
263 if (old != port->mctrl)
264 sdio_uart_write_mctrl(port, port->mctrl);
265}
266
267#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
268#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
269
270static void sdio_uart_change_speed(struct sdio_uart_port *port,
271 struct ktermios *termios,
272 struct ktermios *old)
273{
274 unsigned char cval, fcr = 0;
275 unsigned int baud, quot;
276
277 switch (termios->c_cflag & CSIZE) {
278 case CS5:
279 cval = UART_LCR_WLEN5;
280 break;
281 case CS6:
282 cval = UART_LCR_WLEN6;
283 break;
284 case CS7:
285 cval = UART_LCR_WLEN7;
286 break;
287 default:
288 case CS8:
289 cval = UART_LCR_WLEN8;
290 break;
291 }
292
293 if (termios->c_cflag & CSTOPB)
294 cval |= UART_LCR_STOP;
295 if (termios->c_cflag & PARENB)
296 cval |= UART_LCR_PARITY;
297 if (!(termios->c_cflag & PARODD))
298 cval |= UART_LCR_EPAR;
299
300 for (;;) {
301 baud = tty_termios_baud_rate(termios);
302 if (baud == 0)
303 baud = 9600; /* Special case: B0 rate. */
304 if (baud <= port->uartclk)
305 break;
306 /*
307 * Oops, the quotient was zero. Try again with the old
308 * baud rate if possible, otherwise default to 9600.
309 */
310 termios->c_cflag &= ~CBAUD;
311 if (old) {
312 termios->c_cflag |= old->c_cflag & CBAUD;
313 old = NULL;
314 } else
315 termios->c_cflag |= B9600;
316 }
317 quot = (2 * port->uartclk + baud) / (2 * baud);
318
319 if (baud < 2400)
320 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
321 else
322 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
323
324 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
325 if (termios->c_iflag & INPCK)
326 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
327 if (termios->c_iflag & (BRKINT | PARMRK))
328 port->read_status_mask |= UART_LSR_BI;
329
330 /*
331 * Characters to ignore
332 */
333 port->ignore_status_mask = 0;
334 if (termios->c_iflag & IGNPAR)
335 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
336 if (termios->c_iflag & IGNBRK) {
337 port->ignore_status_mask |= UART_LSR_BI;
338 /*
339 * If we're ignoring parity and break indicators,
340 * ignore overruns too (for real raw support).
341 */
342 if (termios->c_iflag & IGNPAR)
343 port->ignore_status_mask |= UART_LSR_OE;
344 }
345
346 /*
347 * ignore all characters if CREAD is not set
348 */
349 if ((termios->c_cflag & CREAD) == 0)
350 port->ignore_status_mask |= UART_LSR_DR;
351
352 /*
353 * CTS flow control flag and modem status interrupts
354 */
355 port->ier &= ~UART_IER_MSI;
356 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
357 port->ier |= UART_IER_MSI;
358
359 port->lcr = cval;
360
361 sdio_out(port, UART_IER, port->ier);
362 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
363 sdio_out(port, UART_DLL, quot & 0xff);
364 sdio_out(port, UART_DLM, quot >> 8);
365 sdio_out(port, UART_LCR, cval);
366 sdio_out(port, UART_FCR, fcr);
367
368 sdio_uart_write_mctrl(port, port->mctrl);
369}
370
371static void sdio_uart_start_tx(struct sdio_uart_port *port)
372{
373 if (!(port->ier & UART_IER_THRI)) {
374 port->ier |= UART_IER_THRI;
375 sdio_out(port, UART_IER, port->ier);
376 }
377}
378
379static void sdio_uart_stop_tx(struct sdio_uart_port *port)
380{
381 if (port->ier & UART_IER_THRI) {
382 port->ier &= ~UART_IER_THRI;
383 sdio_out(port, UART_IER, port->ier);
384 }
385}
386
387static void sdio_uart_stop_rx(struct sdio_uart_port *port)
388{
389 port->ier &= ~UART_IER_RLSI;
390 port->read_status_mask &= ~UART_LSR_DR;
391 sdio_out(port, UART_IER, port->ier);
392}
393
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300394static void sdio_uart_receive_chars(struct sdio_uart_port *port,
395 unsigned int *status)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400396{
Alan Cox530646f2009-11-05 13:28:29 +0000397 struct tty_struct *tty = tty_port_tty_get(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400398 unsigned int ch, flag;
399 int max_count = 256;
400
401 do {
402 ch = sdio_in(port, UART_RX);
403 flag = TTY_NORMAL;
404 port->icount.rx++;
405
406 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300407 UART_LSR_FE | UART_LSR_OE))) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400408 /*
409 * For statistics only
410 */
411 if (*status & UART_LSR_BI) {
412 *status &= ~(UART_LSR_FE | UART_LSR_PE);
413 port->icount.brk++;
414 } else if (*status & UART_LSR_PE)
415 port->icount.parity++;
416 else if (*status & UART_LSR_FE)
417 port->icount.frame++;
418 if (*status & UART_LSR_OE)
419 port->icount.overrun++;
420
421 /*
422 * Mask off conditions which should be ignored.
423 */
424 *status &= port->read_status_mask;
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300425 if (*status & UART_LSR_BI)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400426 flag = TTY_BREAK;
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300427 else if (*status & UART_LSR_PE)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400428 flag = TTY_PARITY;
429 else if (*status & UART_LSR_FE)
430 flag = TTY_FRAME;
431 }
432
433 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
Alan Cox530646f2009-11-05 13:28:29 +0000434 if (tty)
435 tty_insert_flip_char(tty, ch, flag);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400436
437 /*
438 * Overrun is special. Since it's reported immediately,
439 * it doesn't affect the current character.
440 */
441 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
Alan Cox530646f2009-11-05 13:28:29 +0000442 if (tty)
443 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400444
445 *status = sdio_in(port, UART_LSR);
446 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Alan Cox530646f2009-11-05 13:28:29 +0000447 if (tty) {
448 tty_flip_buffer_push(tty);
449 tty_kref_put(tty);
450 }
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400451}
452
453static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
454{
455 struct circ_buf *xmit = &port->xmit;
456 int count;
Alan Cox530646f2009-11-05 13:28:29 +0000457 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400458
459 if (port->x_char) {
460 sdio_out(port, UART_TX, port->x_char);
461 port->icount.tx++;
462 port->x_char = 0;
463 return;
464 }
Alan Cox530646f2009-11-05 13:28:29 +0000465
466 tty = tty_port_tty_get(&port->port);
467
468 if (tty == NULL || circ_empty(xmit) || tty->stopped || tty->hw_stopped) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400469 sdio_uart_stop_tx(port);
Alan Cox530646f2009-11-05 13:28:29 +0000470 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400471 return;
472 }
473
474 count = 16;
475 do {
476 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
477 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
478 port->icount.tx++;
479 if (circ_empty(xmit))
480 break;
481 } while (--count > 0);
482
483 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
Alan Coxb5849b12009-11-05 13:28:06 +0000484 tty_wakeup(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400485
486 if (circ_empty(xmit))
487 sdio_uart_stop_tx(port);
Alan Cox530646f2009-11-05 13:28:29 +0000488 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400489}
490
491static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
492{
493 int status;
Alan Cox530646f2009-11-05 13:28:29 +0000494 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400495
496 status = sdio_in(port, UART_MSR);
497
498 if ((status & UART_MSR_ANY_DELTA) == 0)
499 return;
500
501 if (status & UART_MSR_TERI)
502 port->icount.rng++;
503 if (status & UART_MSR_DDSR)
504 port->icount.dsr++;
505 if (status & UART_MSR_DDCD)
506 port->icount.dcd++;
507 if (status & UART_MSR_DCTS) {
508 port->icount.cts++;
Alan Cox530646f2009-11-05 13:28:29 +0000509 tty = tty_port_tty_get(&port->port);
510 if (tty && (tty->termios->c_cflag & CRTSCTS)) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400511 int cts = (status & UART_MSR_CTS);
Alan Coxb5849b12009-11-05 13:28:06 +0000512 if (tty->hw_stopped) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400513 if (cts) {
Alan Coxb5849b12009-11-05 13:28:06 +0000514 tty->hw_stopped = 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400515 sdio_uart_start_tx(port);
Alan Coxb5849b12009-11-05 13:28:06 +0000516 tty_wakeup(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400517 }
518 } else {
519 if (!cts) {
Alan Coxb5849b12009-11-05 13:28:06 +0000520 tty->hw_stopped = 1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400521 sdio_uart_stop_tx(port);
522 }
523 }
524 }
Alan Cox530646f2009-11-05 13:28:29 +0000525 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400526 }
527}
528
529/*
530 * This handles the interrupt from one port.
531 */
532static void sdio_uart_irq(struct sdio_func *func)
533{
534 struct sdio_uart_port *port = sdio_get_drvdata(func);
535 unsigned int iir, lsr;
536
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400537 /*
538 * In a few places sdio_uart_irq() is called directly instead of
539 * waiting for the actual interrupt to be raised and the SDIO IRQ
540 * thread scheduled in order to reduce latency. However, some
541 * interaction with the tty core may end up calling us back
542 * (serial echo, flow control, etc.) through those same places
543 * causing undesirable effects. Let's stop the recursion here.
544 */
545 if (unlikely(port->in_sdio_uart_irq == current))
546 return;
547
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400548 iir = sdio_in(port, UART_IIR);
549 if (iir & UART_IIR_NO_INT)
550 return;
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400551
552 port->in_sdio_uart_irq = current;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400553 lsr = sdio_in(port, UART_LSR);
554 if (lsr & UART_LSR_DR)
555 sdio_uart_receive_chars(port, &lsr);
556 sdio_uart_check_modem_status(port);
557 if (lsr & UART_LSR_THRE)
558 sdio_uart_transmit_chars(port);
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400559 port->in_sdio_uart_irq = NULL;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400560}
561
Alan Cox584abc32009-11-30 13:16:09 +0000562/**
563 * uart_dtr_rts - port helper to set uart signals
564 * @tport: tty port to be updated
565 * @onoff: set to turn on DTR/RTS
566 *
567 * Called by the tty port helpers when the modem signals need to be
568 * adjusted during an open, close and hangup.
569 */
Alan Cox530646f2009-11-05 13:28:29 +0000570
Alan Cox584abc32009-11-30 13:16:09 +0000571static void uart_dtr_rts(struct tty_port *tport, int onoff)
572{
573 struct sdio_uart_port *port =
574 container_of(tport, struct sdio_uart_port, port);
575 if (onoff == 0)
576 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
577 else
578 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
579}
580
581/**
582 * sdio_uart_activate - start up hardware
583 * @tport: tty port to activate
584 * @tty: tty bound to this port
585 *
586 * Activate a tty port. The port locking guarantees us this will be
587 * run exactly once per set of opens, and if successful will see the
588 * shutdown method run exactly once to match. Start up and shutdown are
589 * protected from each other by the internal locking and will not run
590 * at the same time even during a hangup event.
591 *
592 * If we successfully start up the port we take an extra kref as we
593 * will keep it around until shutdown when the kref is dropped.
594 */
595
596static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
597{
598 struct sdio_uart_port *port =
599 container_of(tport, struct sdio_uart_port, port);
600 unsigned long page;
601 int ret;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400602
603 /*
604 * Set the TTY IO error marker - we will only clear this
605 * once we have successfully opened the port.
606 */
Alan Coxb5849b12009-11-05 13:28:06 +0000607 set_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400608
609 /* Initialise and allocate the transmit buffer. */
610 page = __get_free_page(GFP_KERNEL);
611 if (!page)
Alan Cox584abc32009-11-30 13:16:09 +0000612 return -ENOMEM;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400613 port->xmit.buf = (unsigned char *)page;
614 circ_clear(&port->xmit);
615
616 ret = sdio_uart_claim_func(port);
617 if (ret)
618 goto err1;
619 ret = sdio_enable_func(port->func);
620 if (ret)
621 goto err2;
622 ret = sdio_claim_irq(port->func, sdio_uart_irq);
623 if (ret)
624 goto err3;
625
626 /*
627 * Clear the FIFO buffers and disable them.
628 * (they will be reenabled in sdio_change_speed())
629 */
630 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
631 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300632 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400633 sdio_out(port, UART_FCR, 0);
634
635 /*
636 * Clear the interrupt registers.
637 */
638 (void) sdio_in(port, UART_LSR);
639 (void) sdio_in(port, UART_RX);
640 (void) sdio_in(port, UART_IIR);
641 (void) sdio_in(port, UART_MSR);
642
643 /*
644 * Now, initialize the UART
645 */
646 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
647
648 port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
649 port->mctrl = TIOCM_OUT2;
650
Alan Coxb5849b12009-11-05 13:28:06 +0000651 sdio_uart_change_speed(port, tty->termios, NULL);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400652
Alan Coxb5849b12009-11-05 13:28:06 +0000653 if (tty->termios->c_cflag & CBAUD)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400654 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
655
Alan Coxb5849b12009-11-05 13:28:06 +0000656 if (tty->termios->c_cflag & CRTSCTS)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400657 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
Alan Coxb5849b12009-11-05 13:28:06 +0000658 tty->hw_stopped = 1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400659
Nicolas Pitre0395b482009-11-05 13:28:17 +0000660 clear_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400661
662 /* Kick the IRQ handler once while we're still holding the host lock */
663 sdio_uart_irq(port->func);
664
665 sdio_uart_release_func(port);
666 return 0;
667
668err3:
669 sdio_disable_func(port->func);
670err2:
671 sdio_uart_release_func(port);
672err1:
673 free_page((unsigned long)port->xmit.buf);
674 return ret;
675}
676
Alan Cox584abc32009-11-30 13:16:09 +0000677
678/**
679 * sdio_uart_shutdown - stop hardware
680 * @tport: tty port to shut down
681 *
682 * Deactivate a tty port. The port locking guarantees us this will be
683 * run only if a successful matching activate already ran. The two are
684 * protected from each other by the internal locking and will not run
685 * at the same time even during a hangup event.
686 */
687
688static void sdio_uart_shutdown(struct tty_port *tport)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400689{
Alan Cox584abc32009-11-30 13:16:09 +0000690 struct sdio_uart_port *port =
691 container_of(tport, struct sdio_uart_port, port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400692 int ret;
693
694 ret = sdio_uart_claim_func(port);
695 if (ret)
696 goto skip;
697
698 sdio_uart_stop_rx(port);
699
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300700 /* Disable interrupts from this port */
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400701 sdio_release_irq(port->func);
702 port->ier = 0;
703 sdio_out(port, UART_IER, 0);
704
705 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
706
707 /* Disable break condition and FIFOs. */
708 port->lcr &= ~UART_LCR_SBC;
709 sdio_out(port, UART_LCR, port->lcr);
710 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
711 UART_FCR_CLEAR_RCVR |
712 UART_FCR_CLEAR_XMIT);
713 sdio_out(port, UART_FCR, 0);
714
715 sdio_disable_func(port->func);
716
717 sdio_uart_release_func(port);
718
719skip:
720 /* Free the transmit buffer page. */
721 free_page((unsigned long)port->xmit.buf);
722}
723
Alan Cox584abc32009-11-30 13:16:09 +0000724/**
725 * sdio_uart_install - install method
726 * @driver: the driver in use (sdio_uart in our case)
727 * @tty: the tty being bound
728 *
729 * Look up and bind the tty and the driver together. Initialize
730 * any needed private data (in our case the termios)
731 */
732
733static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
734{
735 int idx = tty->index;
736 struct sdio_uart_port *port = sdio_uart_port_get(idx);
737 int ret = tty_init_termios(tty);
738
739 if (ret == 0) {
740 tty_driver_kref_get(driver);
741 tty->count++;
742 /* This is the ref sdio_uart_port get provided */
743 tty->driver_data = port;
744 driver->ttys[idx] = tty;
745 } else
746 sdio_uart_port_put(port);
747 return ret;
748
749}
750
751/**
752 * sdio_uart_cleanup - called on the last tty kref drop
753 * @tty: the tty being destroyed
754 *
755 * Called asynchronously when the last reference to the tty is dropped.
756 * We cannot destroy the tty->driver_data port kref until this point
757 */
758
759static void sdio_uart_cleanup(struct tty_struct *tty)
760{
761 struct sdio_uart_port *port = tty->driver_data;
762 tty->driver_data = NULL; /* Bug trap */
763 sdio_uart_port_put(port);
764}
765
766/*
767 * Open/close/hangup is now entirely boilerplate
768 */
769
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300770static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400771{
Alan Cox584abc32009-11-30 13:16:09 +0000772 struct sdio_uart_port *port = tty->driver_data;
773 return tty_port_open(&port->port, tty, filp);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400774}
775
776static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
777{
778 struct sdio_uart_port *port = tty->driver_data;
Alan Cox584abc32009-11-30 13:16:09 +0000779 tty_port_close(&port->port, tty, filp);
780}
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400781
Alan Cox584abc32009-11-30 13:16:09 +0000782static void sdio_uart_hangup(struct tty_struct *tty)
783{
784 struct sdio_uart_port *port = tty->driver_data;
785 tty_port_hangup(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400786}
787
788static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
789 int count)
790{
791 struct sdio_uart_port *port = tty->driver_data;
792 struct circ_buf *circ = &port->xmit;
793 int c, ret = 0;
794
795 if (!port->func)
796 return -ENODEV;
797
798 spin_lock(&port->write_lock);
799 while (1) {
800 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
801 if (count < c)
802 c = count;
803 if (c <= 0)
804 break;
805 memcpy(circ->buf + circ->head, buf, c);
806 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
807 buf += c;
808 count -= c;
809 ret += c;
810 }
811 spin_unlock(&port->write_lock);
812
813 if ( !(port->ier & UART_IER_THRI)) {
814 int err = sdio_uart_claim_func(port);
815 if (!err) {
816 sdio_uart_start_tx(port);
817 sdio_uart_irq(port->func);
818 sdio_uart_release_func(port);
819 } else
820 ret = err;
821 }
822
823 return ret;
824}
825
826static int sdio_uart_write_room(struct tty_struct *tty)
827{
828 struct sdio_uart_port *port = tty->driver_data;
829 return port ? circ_chars_free(&port->xmit) : 0;
830}
831
832static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
833{
834 struct sdio_uart_port *port = tty->driver_data;
835 return port ? circ_chars_pending(&port->xmit) : 0;
836}
837
838static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
839{
840 struct sdio_uart_port *port = tty->driver_data;
841
842 port->x_char = ch;
843 if (ch && !(port->ier & UART_IER_THRI)) {
844 if (sdio_uart_claim_func(port) != 0)
845 return;
846 sdio_uart_start_tx(port);
847 sdio_uart_irq(port->func);
848 sdio_uart_release_func(port);
849 }
850}
851
852static void sdio_uart_throttle(struct tty_struct *tty)
853{
854 struct sdio_uart_port *port = tty->driver_data;
855
856 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
857 return;
858
859 if (sdio_uart_claim_func(port) != 0)
860 return;
861
862 if (I_IXOFF(tty)) {
863 port->x_char = STOP_CHAR(tty);
864 sdio_uart_start_tx(port);
865 }
866
867 if (tty->termios->c_cflag & CRTSCTS)
868 sdio_uart_clear_mctrl(port, TIOCM_RTS);
869
870 sdio_uart_irq(port->func);
871 sdio_uart_release_func(port);
872}
873
874static void sdio_uart_unthrottle(struct tty_struct *tty)
875{
876 struct sdio_uart_port *port = tty->driver_data;
877
878 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
879 return;
880
881 if (sdio_uart_claim_func(port) != 0)
882 return;
883
884 if (I_IXOFF(tty)) {
885 if (port->x_char) {
886 port->x_char = 0;
887 } else {
888 port->x_char = START_CHAR(tty);
889 sdio_uart_start_tx(port);
890 }
891 }
892
893 if (tty->termios->c_cflag & CRTSCTS)
894 sdio_uart_set_mctrl(port, TIOCM_RTS);
895
896 sdio_uart_irq(port->func);
897 sdio_uart_release_func(port);
898}
899
900static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
901{
902 struct sdio_uart_port *port = tty->driver_data;
903 unsigned int cflag = tty->termios->c_cflag;
904
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300905#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400906
907 if ((cflag ^ old_termios->c_cflag) == 0 &&
908 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
909 return;
910
911 if (sdio_uart_claim_func(port) != 0)
912 return;
913
914 sdio_uart_change_speed(port, tty->termios, old_termios);
915
916 /* Handle transition to B0 status */
917 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
918 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
919
920 /* Handle transition away from B0 status */
921 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
922 unsigned int mask = TIOCM_DTR;
923 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
924 mask |= TIOCM_RTS;
925 sdio_uart_set_mctrl(port, mask);
926 }
927
928 /* Handle turning off CRTSCTS */
929 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
930 tty->hw_stopped = 0;
931 sdio_uart_start_tx(port);
932 }
933
934 /* Handle turning on CRTSCTS */
935 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
936 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
937 tty->hw_stopped = 1;
938 sdio_uart_stop_tx(port);
939 }
940 }
941
942 sdio_uart_release_func(port);
943}
944
David Howellsc43d8632008-07-10 12:28:48 +0100945static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400946{
947 struct sdio_uart_port *port = tty->driver_data;
David Howellsc43d8632008-07-10 12:28:48 +0100948 int result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400949
David Howellsc43d8632008-07-10 12:28:48 +0100950 result = sdio_uart_claim_func(port);
951 if (result != 0)
952 return result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400953
954 if (break_state == -1)
955 port->lcr |= UART_LCR_SBC;
956 else
957 port->lcr &= ~UART_LCR_SBC;
958 sdio_out(port, UART_LCR, port->lcr);
959
960 sdio_uart_release_func(port);
David Howellsc43d8632008-07-10 12:28:48 +0100961 return 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400962}
963
964static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
965{
966 struct sdio_uart_port *port = tty->driver_data;
967 int result;
968
969 result = sdio_uart_claim_func(port);
970 if (!result) {
971 result = port->mctrl | sdio_uart_get_mctrl(port);
972 sdio_uart_release_func(port);
973 }
974
975 return result;
976}
977
978static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
979 unsigned int set, unsigned int clear)
980{
981 struct sdio_uart_port *port = tty->driver_data;
982 int result;
983
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300984 result = sdio_uart_claim_func(port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400985 if(!result) {
986 sdio_uart_update_mctrl(port, set, clear);
987 sdio_uart_release_func(port);
988 }
989
990 return result;
991}
992
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700993static int sdio_uart_proc_show(struct seq_file *m, void *v)
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400994{
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700995 int i;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400996
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700997 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400998 "", "", "");
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700999 for (i = 0; i < UART_NR; i++) {
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001000 struct sdio_uart_port *port = sdio_uart_port_get(i);
1001 if (port) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001002 seq_printf(m, "%d: uart:SDIO", i);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001003 if(capable(CAP_SYS_ADMIN)) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001004 seq_printf(m, " tx:%d rx:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001005 port->icount.tx, port->icount.rx);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001006 if (port->icount.frame)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001007 seq_printf(m, " fe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001008 port->icount.frame);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001009 if (port->icount.parity)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001010 seq_printf(m, " pe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001011 port->icount.parity);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001012 if (port->icount.brk)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001013 seq_printf(m, " brk:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001014 port->icount.brk);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001015 if (port->icount.overrun)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001016 seq_printf(m, " oe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001017 port->icount.overrun);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001018 if (port->icount.cts)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001019 seq_printf(m, " cts:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001020 port->icount.cts);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001021 if (port->icount.dsr)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001022 seq_printf(m, " dsr:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001023 port->icount.dsr);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001024 if (port->icount.rng)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001025 seq_printf(m, " rng:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001026 port->icount.rng);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001027 if (port->icount.dcd)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001028 seq_printf(m, " dcd:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001029 port->icount.dcd);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001030 }
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001031 sdio_uart_port_put(port);
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001032 seq_putc(m, '\n');
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001033 }
1034 }
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001035 return 0;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001036}
1037
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001038static int sdio_uart_proc_open(struct inode *inode, struct file *file)
1039{
1040 return single_open(file, sdio_uart_proc_show, NULL);
1041}
1042
1043static const struct file_operations sdio_uart_proc_fops = {
1044 .owner = THIS_MODULE,
1045 .open = sdio_uart_proc_open,
1046 .read = seq_read,
1047 .llseek = seq_lseek,
1048 .release = single_release,
1049};
1050
Alan Cox584abc32009-11-30 13:16:09 +00001051static const struct tty_port_operations sdio_uart_port_ops = {
1052 .dtr_rts = uart_dtr_rts,
1053 .shutdown = sdio_uart_shutdown,
1054 .activate = sdio_uart_activate,
1055};
1056
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001057static const struct tty_operations sdio_uart_ops = {
1058 .open = sdio_uart_open,
1059 .close = sdio_uart_close,
1060 .write = sdio_uart_write,
1061 .write_room = sdio_uart_write_room,
1062 .chars_in_buffer = sdio_uart_chars_in_buffer,
1063 .send_xchar = sdio_uart_send_xchar,
1064 .throttle = sdio_uart_throttle,
1065 .unthrottle = sdio_uart_unthrottle,
1066 .set_termios = sdio_uart_set_termios,
Alan Cox584abc32009-11-30 13:16:09 +00001067 .hangup = sdio_uart_hangup,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001068 .break_ctl = sdio_uart_break_ctl,
1069 .tiocmget = sdio_uart_tiocmget,
1070 .tiocmset = sdio_uart_tiocmset,
Alan Cox584abc32009-11-30 13:16:09 +00001071 .install = sdio_uart_install,
1072 .cleanup = sdio_uart_cleanup,
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001073 .proc_fops = &sdio_uart_proc_fops,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001074};
1075
1076static struct tty_driver *sdio_uart_tty_driver;
1077
1078static int sdio_uart_probe(struct sdio_func *func,
1079 const struct sdio_device_id *id)
1080{
1081 struct sdio_uart_port *port;
1082 int ret;
1083
1084 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1085 if (!port)
1086 return -ENOMEM;
1087
1088 if (func->class == SDIO_CLASS_UART) {
1089 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1090 sdio_func_id(func));
1091 kfree(port);
1092 return -ENOSYS;
1093 } else if (func->class == SDIO_CLASS_GPS) {
1094 /*
1095 * We need tuple 0x91. It contains SUBTPL_SIOREG
1096 * and SUBTPL_RCVCAPS.
1097 */
1098 struct sdio_func_tuple *tpl;
1099 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1100 if (tpl->code != 0x91)
1101 continue;
1102 if (tpl->size < 10)
1103 continue;
1104 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1105 break;
1106 }
1107 if (!tpl) {
1108 printk(KERN_WARNING
1109 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1110 sdio_func_id(func));
1111 kfree(port);
1112 return -EINVAL;
1113 }
1114 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1115 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1116 port->regs_offset = (tpl->data[4] << 0) |
1117 (tpl->data[5] << 8) |
1118 (tpl->data[6] << 16);
1119 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1120 sdio_func_id(func), port->regs_offset);
1121 port->uartclk = tpl->data[7] * 115200;
1122 if (port->uartclk == 0)
1123 port->uartclk = 115200;
1124 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1125 sdio_func_id(func), port->uartclk,
1126 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1127 } else {
1128 kfree(port);
1129 return -EINVAL;
1130 }
1131
1132 port->func = func;
1133 sdio_set_drvdata(func, port);
Alan Coxb5849b12009-11-05 13:28:06 +00001134 tty_port_init(&port->port);
Alan Cox584abc32009-11-30 13:16:09 +00001135 port->port.ops = &sdio_uart_port_ops;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001136
1137 ret = sdio_uart_add_port(port);
1138 if (ret) {
1139 kfree(port);
1140 } else {
1141 struct device *dev;
1142 dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
1143 if (IS_ERR(dev)) {
1144 sdio_uart_port_remove(port);
1145 ret = PTR_ERR(dev);
1146 }
1147 }
1148
1149 return ret;
1150}
1151
1152static void sdio_uart_remove(struct sdio_func *func)
1153{
1154 struct sdio_uart_port *port = sdio_get_drvdata(func);
1155
1156 tty_unregister_device(sdio_uart_tty_driver, port->index);
1157 sdio_uart_port_remove(port);
1158}
1159
1160static const struct sdio_device_id sdio_uart_ids[] = {
1161 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1162 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1163 { /* end: all zeroes */ },
1164};
1165
1166MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1167
1168static struct sdio_driver sdio_uart_driver = {
1169 .probe = sdio_uart_probe,
1170 .remove = sdio_uart_remove,
1171 .name = "sdio_uart",
1172 .id_table = sdio_uart_ids,
1173};
1174
1175static int __init sdio_uart_init(void)
1176{
1177 int ret;
1178 struct tty_driver *tty_drv;
1179
1180 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1181 if (!tty_drv)
1182 return -ENOMEM;
1183
1184 tty_drv->owner = THIS_MODULE;
1185 tty_drv->driver_name = "sdio_uart";
1186 tty_drv->name = "ttySDIO";
1187 tty_drv->major = 0; /* dynamically allocated */
1188 tty_drv->minor_start = 0;
1189 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1190 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1191 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1192 tty_drv->init_termios = tty_std_termios;
1193 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
Nicolas Pitre2ba30ee2007-08-15 13:27:29 -04001194 tty_drv->init_termios.c_ispeed = 4800;
1195 tty_drv->init_termios.c_ospeed = 4800;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001196 tty_set_operations(tty_drv, &sdio_uart_ops);
1197
1198 ret = tty_register_driver(tty_drv);
1199 if (ret)
1200 goto err1;
1201
1202 ret = sdio_register_driver(&sdio_uart_driver);
1203 if (ret)
1204 goto err2;
1205
1206 return 0;
1207
1208err2:
1209 tty_unregister_driver(tty_drv);
1210err1:
1211 put_tty_driver(tty_drv);
1212 return ret;
1213}
1214
1215static void __exit sdio_uart_exit(void)
1216{
1217 sdio_unregister_driver(&sdio_uart_driver);
1218 tty_unregister_driver(sdio_uart_tty_driver);
1219 put_tty_driver(sdio_uart_tty_driver);
1220}
1221
1222module_init(sdio_uart_init);
1223module_exit(sdio_uart_exit);
1224
1225MODULE_AUTHOR("Nicolas Pitre");
1226MODULE_LICENSE("GPL");