blob: b999972eaf289b01defa4468d34d6b30e255e08a [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
Alan Coxc271cf32009-11-30 13:16:25 +0000468 if (tty == NULL || circ_empty(xmit) ||
469 tty->stopped || tty->hw_stopped) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400470 sdio_uart_stop_tx(port);
Alan Cox530646f2009-11-05 13:28:29 +0000471 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400472 return;
473 }
474
475 count = 16;
476 do {
477 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
478 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
479 port->icount.tx++;
480 if (circ_empty(xmit))
481 break;
482 } while (--count > 0);
483
484 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
Alan Coxb5849b12009-11-05 13:28:06 +0000485 tty_wakeup(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400486
487 if (circ_empty(xmit))
488 sdio_uart_stop_tx(port);
Alan Cox530646f2009-11-05 13:28:29 +0000489 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400490}
491
492static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
493{
494 int status;
Alan Cox530646f2009-11-05 13:28:29 +0000495 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400496
497 status = sdio_in(port, UART_MSR);
498
499 if ((status & UART_MSR_ANY_DELTA) == 0)
500 return;
501
502 if (status & UART_MSR_TERI)
503 port->icount.rng++;
504 if (status & UART_MSR_DDSR)
505 port->icount.dsr++;
506 if (status & UART_MSR_DDCD)
507 port->icount.dcd++;
508 if (status & UART_MSR_DCTS) {
509 port->icount.cts++;
Alan Cox530646f2009-11-05 13:28:29 +0000510 tty = tty_port_tty_get(&port->port);
511 if (tty && (tty->termios->c_cflag & CRTSCTS)) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400512 int cts = (status & UART_MSR_CTS);
Alan Coxb5849b12009-11-05 13:28:06 +0000513 if (tty->hw_stopped) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400514 if (cts) {
Alan Coxb5849b12009-11-05 13:28:06 +0000515 tty->hw_stopped = 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400516 sdio_uart_start_tx(port);
Alan Coxb5849b12009-11-05 13:28:06 +0000517 tty_wakeup(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400518 }
519 } else {
520 if (!cts) {
Alan Coxb5849b12009-11-05 13:28:06 +0000521 tty->hw_stopped = 1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400522 sdio_uart_stop_tx(port);
523 }
524 }
525 }
Alan Cox530646f2009-11-05 13:28:29 +0000526 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400527 }
528}
529
530/*
531 * This handles the interrupt from one port.
532 */
533static void sdio_uart_irq(struct sdio_func *func)
534{
535 struct sdio_uart_port *port = sdio_get_drvdata(func);
536 unsigned int iir, lsr;
537
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400538 /*
539 * In a few places sdio_uart_irq() is called directly instead of
540 * waiting for the actual interrupt to be raised and the SDIO IRQ
541 * thread scheduled in order to reduce latency. However, some
542 * interaction with the tty core may end up calling us back
543 * (serial echo, flow control, etc.) through those same places
544 * causing undesirable effects. Let's stop the recursion here.
545 */
546 if (unlikely(port->in_sdio_uart_irq == current))
547 return;
548
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400549 iir = sdio_in(port, UART_IIR);
550 if (iir & UART_IIR_NO_INT)
551 return;
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400552
553 port->in_sdio_uart_irq = current;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400554 lsr = sdio_in(port, UART_LSR);
555 if (lsr & UART_LSR_DR)
556 sdio_uart_receive_chars(port, &lsr);
557 sdio_uart_check_modem_status(port);
558 if (lsr & UART_LSR_THRE)
559 sdio_uart_transmit_chars(port);
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400560 port->in_sdio_uart_irq = NULL;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400561}
562
Alan Cox584abc32009-11-30 13:16:09 +0000563/**
564 * uart_dtr_rts - port helper to set uart signals
565 * @tport: tty port to be updated
566 * @onoff: set to turn on DTR/RTS
567 *
568 * Called by the tty port helpers when the modem signals need to be
569 * adjusted during an open, close and hangup.
570 */
Alan Cox530646f2009-11-05 13:28:29 +0000571
Alan Cox584abc32009-11-30 13:16:09 +0000572static void uart_dtr_rts(struct tty_port *tport, int onoff)
573{
574 struct sdio_uart_port *port =
575 container_of(tport, struct sdio_uart_port, port);
576 if (onoff == 0)
577 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
578 else
579 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
580}
581
582/**
583 * sdio_uart_activate - start up hardware
584 * @tport: tty port to activate
585 * @tty: tty bound to this port
586 *
587 * Activate a tty port. The port locking guarantees us this will be
588 * run exactly once per set of opens, and if successful will see the
589 * shutdown method run exactly once to match. Start up and shutdown are
590 * protected from each other by the internal locking and will not run
591 * at the same time even during a hangup event.
592 *
593 * If we successfully start up the port we take an extra kref as we
594 * will keep it around until shutdown when the kref is dropped.
595 */
596
597static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
598{
599 struct sdio_uart_port *port =
600 container_of(tport, struct sdio_uart_port, port);
601 unsigned long page;
602 int ret;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400603
604 /*
605 * Set the TTY IO error marker - we will only clear this
606 * once we have successfully opened the port.
607 */
Alan Coxb5849b12009-11-05 13:28:06 +0000608 set_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400609
610 /* Initialise and allocate the transmit buffer. */
611 page = __get_free_page(GFP_KERNEL);
612 if (!page)
Alan Cox584abc32009-11-30 13:16:09 +0000613 return -ENOMEM;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400614 port->xmit.buf = (unsigned char *)page;
615 circ_clear(&port->xmit);
616
617 ret = sdio_uart_claim_func(port);
618 if (ret)
619 goto err1;
620 ret = sdio_enable_func(port->func);
621 if (ret)
622 goto err2;
623 ret = sdio_claim_irq(port->func, sdio_uart_irq);
624 if (ret)
625 goto err3;
626
627 /*
628 * Clear the FIFO buffers and disable them.
629 * (they will be reenabled in sdio_change_speed())
630 */
631 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
632 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300633 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400634 sdio_out(port, UART_FCR, 0);
635
636 /*
637 * Clear the interrupt registers.
638 */
639 (void) sdio_in(port, UART_LSR);
640 (void) sdio_in(port, UART_RX);
641 (void) sdio_in(port, UART_IIR);
642 (void) sdio_in(port, UART_MSR);
643
644 /*
645 * Now, initialize the UART
646 */
647 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
648
Alan Coxc271cf32009-11-30 13:16:25 +0000649 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400650 port->mctrl = TIOCM_OUT2;
651
Alan Coxb5849b12009-11-05 13:28:06 +0000652 sdio_uart_change_speed(port, tty->termios, NULL);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400653
Alan Coxb5849b12009-11-05 13:28:06 +0000654 if (tty->termios->c_cflag & CBAUD)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400655 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
656
Alan Coxb5849b12009-11-05 13:28:06 +0000657 if (tty->termios->c_cflag & CRTSCTS)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400658 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
Alan Coxb5849b12009-11-05 13:28:06 +0000659 tty->hw_stopped = 1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400660
Nicolas Pitre0395b482009-11-05 13:28:17 +0000661 clear_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400662
663 /* Kick the IRQ handler once while we're still holding the host lock */
664 sdio_uart_irq(port->func);
665
666 sdio_uart_release_func(port);
667 return 0;
668
669err3:
670 sdio_disable_func(port->func);
671err2:
672 sdio_uart_release_func(port);
673err1:
674 free_page((unsigned long)port->xmit.buf);
675 return ret;
676}
677
Alan Cox584abc32009-11-30 13:16:09 +0000678/**
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;
Alan Cox584abc32009-11-30 13:16:09 +0000748}
749
750/**
751 * sdio_uart_cleanup - called on the last tty kref drop
752 * @tty: the tty being destroyed
753 *
754 * Called asynchronously when the last reference to the tty is dropped.
755 * We cannot destroy the tty->driver_data port kref until this point
756 */
757
758static void sdio_uart_cleanup(struct tty_struct *tty)
759{
760 struct sdio_uart_port *port = tty->driver_data;
761 tty->driver_data = NULL; /* Bug trap */
762 sdio_uart_port_put(port);
763}
764
765/*
766 * Open/close/hangup is now entirely boilerplate
767 */
768
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300769static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400770{
Alan Cox584abc32009-11-30 13:16:09 +0000771 struct sdio_uart_port *port = tty->driver_data;
772 return tty_port_open(&port->port, tty, filp);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400773}
774
775static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
776{
777 struct sdio_uart_port *port = tty->driver_data;
Alan Cox584abc32009-11-30 13:16:09 +0000778 tty_port_close(&port->port, tty, filp);
779}
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400780
Alan Cox584abc32009-11-30 13:16:09 +0000781static void sdio_uart_hangup(struct tty_struct *tty)
782{
783 struct sdio_uart_port *port = tty->driver_data;
784 tty_port_hangup(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400785}
786
Alan Coxc271cf32009-11-30 13:16:25 +0000787static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400788 int count)
789{
790 struct sdio_uart_port *port = tty->driver_data;
791 struct circ_buf *circ = &port->xmit;
792 int c, ret = 0;
793
794 if (!port->func)
795 return -ENODEV;
796
797 spin_lock(&port->write_lock);
798 while (1) {
799 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
800 if (count < c)
801 c = count;
802 if (c <= 0)
803 break;
804 memcpy(circ->buf + circ->head, buf, c);
805 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
806 buf += c;
807 count -= c;
808 ret += c;
809 }
810 spin_unlock(&port->write_lock);
811
Alan Coxc271cf32009-11-30 13:16:25 +0000812 if (!(port->ier & UART_IER_THRI)) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400813 int err = sdio_uart_claim_func(port);
814 if (!err) {
815 sdio_uart_start_tx(port);
816 sdio_uart_irq(port->func);
817 sdio_uart_release_func(port);
818 } else
819 ret = err;
820 }
821
822 return ret;
823}
824
825static int sdio_uart_write_room(struct tty_struct *tty)
826{
827 struct sdio_uart_port *port = tty->driver_data;
828 return port ? circ_chars_free(&port->xmit) : 0;
829}
830
831static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
832{
833 struct sdio_uart_port *port = tty->driver_data;
834 return port ? circ_chars_pending(&port->xmit) : 0;
835}
836
837static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
838{
839 struct sdio_uart_port *port = tty->driver_data;
840
841 port->x_char = ch;
842 if (ch && !(port->ier & UART_IER_THRI)) {
843 if (sdio_uart_claim_func(port) != 0)
844 return;
845 sdio_uart_start_tx(port);
846 sdio_uart_irq(port->func);
847 sdio_uart_release_func(port);
848 }
849}
850
851static void sdio_uart_throttle(struct tty_struct *tty)
852{
853 struct sdio_uart_port *port = tty->driver_data;
854
855 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
856 return;
857
858 if (sdio_uart_claim_func(port) != 0)
859 return;
860
861 if (I_IXOFF(tty)) {
862 port->x_char = STOP_CHAR(tty);
863 sdio_uart_start_tx(port);
864 }
865
866 if (tty->termios->c_cflag & CRTSCTS)
867 sdio_uart_clear_mctrl(port, TIOCM_RTS);
868
869 sdio_uart_irq(port->func);
870 sdio_uart_release_func(port);
871}
872
873static void sdio_uart_unthrottle(struct tty_struct *tty)
874{
875 struct sdio_uart_port *port = tty->driver_data;
876
877 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
878 return;
879
880 if (sdio_uart_claim_func(port) != 0)
881 return;
882
883 if (I_IXOFF(tty)) {
884 if (port->x_char) {
885 port->x_char = 0;
886 } else {
887 port->x_char = START_CHAR(tty);
888 sdio_uart_start_tx(port);
889 }
890 }
891
892 if (tty->termios->c_cflag & CRTSCTS)
893 sdio_uart_set_mctrl(port, TIOCM_RTS);
894
895 sdio_uart_irq(port->func);
896 sdio_uart_release_func(port);
897}
898
Alan Coxc271cf32009-11-30 13:16:25 +0000899static void sdio_uart_set_termios(struct tty_struct *tty,
900 struct ktermios *old_termios)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400901{
902 struct sdio_uart_port *port = tty->driver_data;
903 unsigned int cflag = tty->termios->c_cflag;
904
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400905 if (sdio_uart_claim_func(port) != 0)
906 return;
907
908 sdio_uart_change_speed(port, tty->termios, old_termios);
909
910 /* Handle transition to B0 status */
911 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
912 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
913
914 /* Handle transition away from B0 status */
915 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
916 unsigned int mask = TIOCM_DTR;
917 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
918 mask |= TIOCM_RTS;
919 sdio_uart_set_mctrl(port, mask);
920 }
921
922 /* Handle turning off CRTSCTS */
923 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
924 tty->hw_stopped = 0;
925 sdio_uart_start_tx(port);
926 }
927
928 /* Handle turning on CRTSCTS */
929 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
930 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
931 tty->hw_stopped = 1;
932 sdio_uart_stop_tx(port);
933 }
934 }
935
936 sdio_uart_release_func(port);
937}
938
David Howellsc43d8632008-07-10 12:28:48 +0100939static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400940{
941 struct sdio_uart_port *port = tty->driver_data;
David Howellsc43d8632008-07-10 12:28:48 +0100942 int result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400943
David Howellsc43d8632008-07-10 12:28:48 +0100944 result = sdio_uart_claim_func(port);
945 if (result != 0)
946 return result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400947
948 if (break_state == -1)
949 port->lcr |= UART_LCR_SBC;
950 else
951 port->lcr &= ~UART_LCR_SBC;
952 sdio_out(port, UART_LCR, port->lcr);
953
954 sdio_uart_release_func(port);
David Howellsc43d8632008-07-10 12:28:48 +0100955 return 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400956}
957
958static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
959{
960 struct sdio_uart_port *port = tty->driver_data;
961 int result;
962
963 result = sdio_uart_claim_func(port);
964 if (!result) {
965 result = port->mctrl | sdio_uart_get_mctrl(port);
966 sdio_uart_release_func(port);
967 }
968
969 return result;
970}
971
972static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
973 unsigned int set, unsigned int clear)
974{
975 struct sdio_uart_port *port = tty->driver_data;
976 int result;
977
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300978 result = sdio_uart_claim_func(port);
Alan Coxc271cf32009-11-30 13:16:25 +0000979 if (!result) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400980 sdio_uart_update_mctrl(port, set, clear);
981 sdio_uart_release_func(port);
982 }
983
984 return result;
985}
986
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700987static int sdio_uart_proc_show(struct seq_file *m, void *v)
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400988{
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700989 int i;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400990
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700991 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400992 "", "", "");
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700993 for (i = 0; i < UART_NR; i++) {
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400994 struct sdio_uart_port *port = sdio_uart_port_get(i);
995 if (port) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700996 seq_printf(m, "%d: uart:SDIO", i);
Alan Coxc271cf32009-11-30 13:16:25 +0000997 if (capable(CAP_SYS_ADMIN)) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700998 seq_printf(m, " tx:%d rx:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300999 port->icount.tx, port->icount.rx);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001000 if (port->icount.frame)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001001 seq_printf(m, " fe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001002 port->icount.frame);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001003 if (port->icount.parity)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001004 seq_printf(m, " pe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001005 port->icount.parity);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001006 if (port->icount.brk)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001007 seq_printf(m, " brk:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001008 port->icount.brk);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001009 if (port->icount.overrun)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001010 seq_printf(m, " oe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001011 port->icount.overrun);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001012 if (port->icount.cts)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001013 seq_printf(m, " cts:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001014 port->icount.cts);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001015 if (port->icount.dsr)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001016 seq_printf(m, " dsr:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001017 port->icount.dsr);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001018 if (port->icount.rng)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001019 seq_printf(m, " rng:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001020 port->icount.rng);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001021 if (port->icount.dcd)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001022 seq_printf(m, " dcd:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001023 port->icount.dcd);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001024 }
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001025 sdio_uart_port_put(port);
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001026 seq_putc(m, '\n');
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001027 }
1028 }
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001029 return 0;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001030}
1031
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001032static int sdio_uart_proc_open(struct inode *inode, struct file *file)
1033{
1034 return single_open(file, sdio_uart_proc_show, NULL);
1035}
1036
1037static const struct file_operations sdio_uart_proc_fops = {
1038 .owner = THIS_MODULE,
1039 .open = sdio_uart_proc_open,
1040 .read = seq_read,
1041 .llseek = seq_lseek,
1042 .release = single_release,
1043};
1044
Alan Cox584abc32009-11-30 13:16:09 +00001045static const struct tty_port_operations sdio_uart_port_ops = {
1046 .dtr_rts = uart_dtr_rts,
1047 .shutdown = sdio_uart_shutdown,
1048 .activate = sdio_uart_activate,
1049};
1050
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001051static const struct tty_operations sdio_uart_ops = {
1052 .open = sdio_uart_open,
1053 .close = sdio_uart_close,
1054 .write = sdio_uart_write,
1055 .write_room = sdio_uart_write_room,
1056 .chars_in_buffer = sdio_uart_chars_in_buffer,
1057 .send_xchar = sdio_uart_send_xchar,
1058 .throttle = sdio_uart_throttle,
1059 .unthrottle = sdio_uart_unthrottle,
1060 .set_termios = sdio_uart_set_termios,
Alan Cox584abc32009-11-30 13:16:09 +00001061 .hangup = sdio_uart_hangup,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001062 .break_ctl = sdio_uart_break_ctl,
1063 .tiocmget = sdio_uart_tiocmget,
1064 .tiocmset = sdio_uart_tiocmset,
Alan Cox584abc32009-11-30 13:16:09 +00001065 .install = sdio_uart_install,
1066 .cleanup = sdio_uart_cleanup,
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001067 .proc_fops = &sdio_uart_proc_fops,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001068};
1069
1070static struct tty_driver *sdio_uart_tty_driver;
1071
1072static int sdio_uart_probe(struct sdio_func *func,
1073 const struct sdio_device_id *id)
1074{
1075 struct sdio_uart_port *port;
1076 int ret;
1077
1078 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1079 if (!port)
1080 return -ENOMEM;
1081
1082 if (func->class == SDIO_CLASS_UART) {
1083 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1084 sdio_func_id(func));
1085 kfree(port);
1086 return -ENOSYS;
1087 } else if (func->class == SDIO_CLASS_GPS) {
1088 /*
1089 * We need tuple 0x91. It contains SUBTPL_SIOREG
1090 * and SUBTPL_RCVCAPS.
1091 */
1092 struct sdio_func_tuple *tpl;
1093 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1094 if (tpl->code != 0x91)
1095 continue;
1096 if (tpl->size < 10)
1097 continue;
1098 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1099 break;
1100 }
1101 if (!tpl) {
1102 printk(KERN_WARNING
Alan Coxc271cf32009-11-30 13:16:25 +00001103 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001104 sdio_func_id(func));
1105 kfree(port);
1106 return -EINVAL;
1107 }
1108 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1109 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1110 port->regs_offset = (tpl->data[4] << 0) |
1111 (tpl->data[5] << 8) |
1112 (tpl->data[6] << 16);
1113 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1114 sdio_func_id(func), port->regs_offset);
1115 port->uartclk = tpl->data[7] * 115200;
1116 if (port->uartclk == 0)
1117 port->uartclk = 115200;
1118 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1119 sdio_func_id(func), port->uartclk,
1120 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1121 } else {
1122 kfree(port);
1123 return -EINVAL;
1124 }
1125
1126 port->func = func;
1127 sdio_set_drvdata(func, port);
Alan Coxb5849b12009-11-05 13:28:06 +00001128 tty_port_init(&port->port);
Alan Cox584abc32009-11-30 13:16:09 +00001129 port->port.ops = &sdio_uart_port_ops;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001130
1131 ret = sdio_uart_add_port(port);
1132 if (ret) {
1133 kfree(port);
1134 } else {
1135 struct device *dev;
Alan Coxc271cf32009-11-30 13:16:25 +00001136 dev = tty_register_device(sdio_uart_tty_driver,
1137 port->index, &func->dev);
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001138 if (IS_ERR(dev)) {
1139 sdio_uart_port_remove(port);
1140 ret = PTR_ERR(dev);
1141 }
1142 }
1143
1144 return ret;
1145}
1146
1147static void sdio_uart_remove(struct sdio_func *func)
1148{
1149 struct sdio_uart_port *port = sdio_get_drvdata(func);
1150
1151 tty_unregister_device(sdio_uart_tty_driver, port->index);
1152 sdio_uart_port_remove(port);
1153}
1154
1155static const struct sdio_device_id sdio_uart_ids[] = {
1156 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1157 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1158 { /* end: all zeroes */ },
1159};
1160
1161MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1162
1163static struct sdio_driver sdio_uart_driver = {
1164 .probe = sdio_uart_probe,
1165 .remove = sdio_uart_remove,
1166 .name = "sdio_uart",
1167 .id_table = sdio_uart_ids,
1168};
1169
1170static int __init sdio_uart_init(void)
1171{
1172 int ret;
1173 struct tty_driver *tty_drv;
1174
1175 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1176 if (!tty_drv)
1177 return -ENOMEM;
1178
1179 tty_drv->owner = THIS_MODULE;
1180 tty_drv->driver_name = "sdio_uart";
1181 tty_drv->name = "ttySDIO";
1182 tty_drv->major = 0; /* dynamically allocated */
1183 tty_drv->minor_start = 0;
1184 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1185 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1186 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1187 tty_drv->init_termios = tty_std_termios;
1188 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
Nicolas Pitre2ba30ee2007-08-15 13:27:29 -04001189 tty_drv->init_termios.c_ispeed = 4800;
1190 tty_drv->init_termios.c_ospeed = 4800;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001191 tty_set_operations(tty_drv, &sdio_uart_ops);
1192
1193 ret = tty_register_driver(tty_drv);
1194 if (ret)
1195 goto err1;
1196
1197 ret = sdio_register_driver(&sdio_uart_driver);
1198 if (ret)
1199 goto err2;
1200
1201 return 0;
1202
1203err2:
1204 tty_unregister_driver(tty_drv);
1205err1:
1206 put_tty_driver(tty_drv);
1207 return ret;
1208}
1209
1210static void __exit sdio_uart_exit(void)
1211{
1212 sdio_unregister_driver(&sdio_uart_driver);
1213 tty_unregister_driver(sdio_uart_tty_driver);
1214 put_tty_driver(sdio_uart_tty_driver);
1215}
1216
1217module_init(sdio_uart_init);
1218module_exit(sdio_uart_exit);
1219
1220MODULE_AUTHOR("Nicolas Pitre");
1221MODULE_LICENSE("GPL");