blob: 2a13db54ffc41b72cbae22a222e9616538de7d7f [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>
Alan Cox4b3b49b2009-11-30 13:16:36 +000032#include <linux/sched.h>
Nicolas Pitre6e418a92007-06-30 02:04:21 -040033#include <linux/mutex.h>
Alexey Dobriyan201a50b2009-03-31 15:19:20 -070034#include <linux/seq_file.h>
Nicolas Pitre6e418a92007-06-30 02:04:21 -040035#include <linux/serial_reg.h>
36#include <linux/circ_buf.h>
37#include <linux/gfp.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40
41#include <linux/mmc/core.h>
42#include <linux/mmc/card.h>
43#include <linux/mmc/sdio_func.h>
44#include <linux/mmc/sdio_ids.h>
45
46
47#define UART_NR 8 /* Number of UARTs this driver can handle */
48
49
50#define UART_XMIT_SIZE PAGE_SIZE
51#define WAKEUP_CHARS 256
52
53#define circ_empty(circ) ((circ)->head == (circ)->tail)
54#define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
55
56#define circ_chars_pending(circ) \
57 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
58
59#define circ_chars_free(circ) \
60 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
61
62
63struct uart_icount {
64 __u32 cts;
65 __u32 dsr;
66 __u32 rng;
67 __u32 dcd;
68 __u32 rx;
69 __u32 tx;
70 __u32 frame;
71 __u32 overrun;
72 __u32 parity;
73 __u32 brk;
74};
75
76struct sdio_uart_port {
Alan Coxb5849b12009-11-05 13:28:06 +000077 struct tty_port port;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040078 struct kref kref;
79 struct tty_struct *tty;
80 unsigned int index;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040081 struct sdio_func *func;
82 struct mutex func_lock;
Nicolas Pitre15b82b42007-08-20 17:17:37 -040083 struct task_struct *in_sdio_uart_irq;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040084 unsigned int regs_offset;
85 struct circ_buf xmit;
86 spinlock_t write_lock;
87 struct uart_icount icount;
88 unsigned int uartclk;
89 unsigned int mctrl;
Alan Cox4b3b49b2009-11-30 13:16:36 +000090 unsigned int rx_mctrl;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040091 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
98static struct sdio_uart_port *sdio_uart_table[UART_NR];
99static DEFINE_SPINLOCK(sdio_uart_table_lock);
100
101static int sdio_uart_add_port(struct sdio_uart_port *port)
102{
103 int index, ret = -EBUSY;
104
105 kref_init(&port->kref);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400106 mutex_init(&port->func_lock);
107 spin_lock_init(&port->write_lock);
108
109 spin_lock(&sdio_uart_table_lock);
110 for (index = 0; index < UART_NR; index++) {
111 if (!sdio_uart_table[index]) {
112 port->index = index;
113 sdio_uart_table[index] = port;
114 ret = 0;
115 break;
116 }
117 }
118 spin_unlock(&sdio_uart_table_lock);
119
120 return ret;
121}
122
123static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
124{
125 struct sdio_uart_port *port;
126
127 if (index >= UART_NR)
128 return NULL;
129
130 spin_lock(&sdio_uart_table_lock);
131 port = sdio_uart_table[index];
132 if (port)
133 kref_get(&port->kref);
134 spin_unlock(&sdio_uart_table_lock);
135
136 return port;
137}
138
139static void sdio_uart_port_destroy(struct kref *kref)
140{
141 struct sdio_uart_port *port =
142 container_of(kref, struct sdio_uart_port, kref);
143 kfree(port);
144}
145
146static void sdio_uart_port_put(struct sdio_uart_port *port)
147{
148 kref_put(&port->kref, sdio_uart_port_destroy);
149}
150
151static void sdio_uart_port_remove(struct sdio_uart_port *port)
152{
153 struct sdio_func *func;
Alan Cox584abc32009-11-30 13:16:09 +0000154 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400155
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 */
Alan Cox0a68f642009-11-05 13:28:38 +0000169 mutex_lock(&port->port.mutex);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400170 mutex_lock(&port->func_lock);
171 func = port->func;
172 sdio_claim_host(func);
173 port->func = NULL;
174 mutex_unlock(&port->func_lock);
Alan Cox584abc32009-11-30 13:16:09 +0000175 tty = tty_port_tty_get(&port->port);
176 /* tty_hangup is async so is this safe as is ?? */
177 if (tty) {
178 tty_hangup(tty);
Alan Cox530646f2009-11-05 13:28:29 +0000179 tty_kref_put(tty);
180 }
Alan Cox0a68f642009-11-05 13:28:38 +0000181 mutex_unlock(&port->port.mutex);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400182 sdio_release_irq(func);
183 sdio_disable_func(func);
184 sdio_release_host(func);
185
186 sdio_uart_port_put(port);
187}
188
189static int sdio_uart_claim_func(struct sdio_uart_port *port)
190{
191 mutex_lock(&port->func_lock);
192 if (unlikely(!port->func)) {
193 mutex_unlock(&port->func_lock);
194 return -ENODEV;
195 }
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400196 if (likely(port->in_sdio_uart_irq != current))
197 sdio_claim_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400198 mutex_unlock(&port->func_lock);
199 return 0;
200}
201
202static inline void sdio_uart_release_func(struct sdio_uart_port *port)
203{
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400204 if (likely(port->in_sdio_uart_irq != current))
205 sdio_release_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400206}
207
208static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
209{
210 unsigned char c;
211 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
212 return c;
213}
214
215static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
216{
217 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
218}
219
220static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
221{
222 unsigned char status;
223 unsigned int ret;
224
Alan Cox4b3b49b2009-11-30 13:16:36 +0000225 /* FIXME: What stops this losing the delta bits and breaking
226 sdio_uart_check_modem_status ? */
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400227 status = sdio_in(port, UART_MSR);
228
229 ret = 0;
230 if (status & UART_MSR_DCD)
231 ret |= TIOCM_CAR;
232 if (status & UART_MSR_RI)
233 ret |= TIOCM_RNG;
234 if (status & UART_MSR_DSR)
235 ret |= TIOCM_DSR;
236 if (status & UART_MSR_CTS)
237 ret |= TIOCM_CTS;
238 return ret;
239}
240
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300241static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
242 unsigned int mctrl)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400243{
244 unsigned char mcr = 0;
245
246 if (mctrl & TIOCM_RTS)
247 mcr |= UART_MCR_RTS;
248 if (mctrl & TIOCM_DTR)
249 mcr |= UART_MCR_DTR;
250 if (mctrl & TIOCM_OUT1)
251 mcr |= UART_MCR_OUT1;
252 if (mctrl & TIOCM_OUT2)
253 mcr |= UART_MCR_OUT2;
254 if (mctrl & TIOCM_LOOP)
255 mcr |= UART_MCR_LOOP;
256
257 sdio_out(port, UART_MCR, mcr);
258}
259
260static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
261 unsigned int set, unsigned int clear)
262{
263 unsigned int old;
264
265 old = port->mctrl;
266 port->mctrl = (old & ~clear) | set;
267 if (old != port->mctrl)
268 sdio_uart_write_mctrl(port, port->mctrl);
269}
270
271#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
272#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
273
274static void sdio_uart_change_speed(struct sdio_uart_port *port,
275 struct ktermios *termios,
276 struct ktermios *old)
277{
278 unsigned char cval, fcr = 0;
279 unsigned int baud, quot;
280
281 switch (termios->c_cflag & CSIZE) {
282 case CS5:
283 cval = UART_LCR_WLEN5;
284 break;
285 case CS6:
286 cval = UART_LCR_WLEN6;
287 break;
288 case CS7:
289 cval = UART_LCR_WLEN7;
290 break;
291 default:
292 case CS8:
293 cval = UART_LCR_WLEN8;
294 break;
295 }
296
297 if (termios->c_cflag & CSTOPB)
298 cval |= UART_LCR_STOP;
299 if (termios->c_cflag & PARENB)
300 cval |= UART_LCR_PARITY;
301 if (!(termios->c_cflag & PARODD))
302 cval |= UART_LCR_EPAR;
303
304 for (;;) {
305 baud = tty_termios_baud_rate(termios);
306 if (baud == 0)
307 baud = 9600; /* Special case: B0 rate. */
308 if (baud <= port->uartclk)
309 break;
310 /*
311 * Oops, the quotient was zero. Try again with the old
312 * baud rate if possible, otherwise default to 9600.
313 */
314 termios->c_cflag &= ~CBAUD;
315 if (old) {
316 termios->c_cflag |= old->c_cflag & CBAUD;
317 old = NULL;
318 } else
319 termios->c_cflag |= B9600;
320 }
321 quot = (2 * port->uartclk + baud) / (2 * baud);
322
323 if (baud < 2400)
324 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
325 else
326 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
327
328 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
329 if (termios->c_iflag & INPCK)
330 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
331 if (termios->c_iflag & (BRKINT | PARMRK))
332 port->read_status_mask |= UART_LSR_BI;
333
334 /*
335 * Characters to ignore
336 */
337 port->ignore_status_mask = 0;
338 if (termios->c_iflag & IGNPAR)
339 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
340 if (termios->c_iflag & IGNBRK) {
341 port->ignore_status_mask |= UART_LSR_BI;
342 /*
343 * If we're ignoring parity and break indicators,
344 * ignore overruns too (for real raw support).
345 */
346 if (termios->c_iflag & IGNPAR)
347 port->ignore_status_mask |= UART_LSR_OE;
348 }
349
350 /*
351 * ignore all characters if CREAD is not set
352 */
353 if ((termios->c_cflag & CREAD) == 0)
354 port->ignore_status_mask |= UART_LSR_DR;
355
356 /*
357 * CTS flow control flag and modem status interrupts
358 */
359 port->ier &= ~UART_IER_MSI;
360 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
361 port->ier |= UART_IER_MSI;
362
363 port->lcr = cval;
364
365 sdio_out(port, UART_IER, port->ier);
366 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
367 sdio_out(port, UART_DLL, quot & 0xff);
368 sdio_out(port, UART_DLM, quot >> 8);
369 sdio_out(port, UART_LCR, cval);
370 sdio_out(port, UART_FCR, fcr);
371
372 sdio_uart_write_mctrl(port, port->mctrl);
373}
374
375static void sdio_uart_start_tx(struct sdio_uart_port *port)
376{
377 if (!(port->ier & UART_IER_THRI)) {
378 port->ier |= UART_IER_THRI;
379 sdio_out(port, UART_IER, port->ier);
380 }
381}
382
383static void sdio_uart_stop_tx(struct sdio_uart_port *port)
384{
385 if (port->ier & UART_IER_THRI) {
386 port->ier &= ~UART_IER_THRI;
387 sdio_out(port, UART_IER, port->ier);
388 }
389}
390
391static void sdio_uart_stop_rx(struct sdio_uart_port *port)
392{
393 port->ier &= ~UART_IER_RLSI;
394 port->read_status_mask &= ~UART_LSR_DR;
395 sdio_out(port, UART_IER, port->ier);
396}
397
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300398static void sdio_uart_receive_chars(struct sdio_uart_port *port,
399 unsigned int *status)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400400{
Alan Cox530646f2009-11-05 13:28:29 +0000401 struct tty_struct *tty = tty_port_tty_get(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400402 unsigned int ch, flag;
403 int max_count = 256;
404
405 do {
406 ch = sdio_in(port, UART_RX);
407 flag = TTY_NORMAL;
408 port->icount.rx++;
409
410 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300411 UART_LSR_FE | UART_LSR_OE))) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400412 /*
413 * For statistics only
414 */
415 if (*status & UART_LSR_BI) {
416 *status &= ~(UART_LSR_FE | UART_LSR_PE);
417 port->icount.brk++;
418 } else if (*status & UART_LSR_PE)
419 port->icount.parity++;
420 else if (*status & UART_LSR_FE)
421 port->icount.frame++;
422 if (*status & UART_LSR_OE)
423 port->icount.overrun++;
424
425 /*
426 * Mask off conditions which should be ignored.
427 */
428 *status &= port->read_status_mask;
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300429 if (*status & UART_LSR_BI)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400430 flag = TTY_BREAK;
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300431 else if (*status & UART_LSR_PE)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400432 flag = TTY_PARITY;
433 else if (*status & UART_LSR_FE)
434 flag = TTY_FRAME;
435 }
436
437 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
Alan Cox530646f2009-11-05 13:28:29 +0000438 if (tty)
439 tty_insert_flip_char(tty, ch, flag);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400440
441 /*
442 * Overrun is special. Since it's reported immediately,
443 * it doesn't affect the current character.
444 */
445 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
Alan Cox530646f2009-11-05 13:28:29 +0000446 if (tty)
447 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400448
449 *status = sdio_in(port, UART_LSR);
450 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Alan Cox530646f2009-11-05 13:28:29 +0000451 if (tty) {
452 tty_flip_buffer_push(tty);
453 tty_kref_put(tty);
454 }
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400455}
456
457static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
458{
459 struct circ_buf *xmit = &port->xmit;
460 int count;
Alan Cox530646f2009-11-05 13:28:29 +0000461 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400462
463 if (port->x_char) {
464 sdio_out(port, UART_TX, port->x_char);
465 port->icount.tx++;
466 port->x_char = 0;
467 return;
468 }
Alan Cox530646f2009-11-05 13:28:29 +0000469
470 tty = tty_port_tty_get(&port->port);
471
Alan Coxc271cf32009-11-30 13:16:25 +0000472 if (tty == NULL || circ_empty(xmit) ||
473 tty->stopped || tty->hw_stopped) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400474 sdio_uart_stop_tx(port);
Alan Cox530646f2009-11-05 13:28:29 +0000475 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400476 return;
477 }
478
479 count = 16;
480 do {
481 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
482 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
483 port->icount.tx++;
484 if (circ_empty(xmit))
485 break;
486 } while (--count > 0);
487
488 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
Alan Coxb5849b12009-11-05 13:28:06 +0000489 tty_wakeup(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400490
491 if (circ_empty(xmit))
492 sdio_uart_stop_tx(port);
Alan Cox530646f2009-11-05 13:28:29 +0000493 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400494}
495
496static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
497{
498 int status;
Alan Cox530646f2009-11-05 13:28:29 +0000499 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400500
501 status = sdio_in(port, UART_MSR);
502
503 if ((status & UART_MSR_ANY_DELTA) == 0)
504 return;
505
506 if (status & UART_MSR_TERI)
507 port->icount.rng++;
508 if (status & UART_MSR_DDSR)
509 port->icount.dsr++;
Alan Cox4b3b49b2009-11-30 13:16:36 +0000510 if (status & UART_MSR_DDCD) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400511 port->icount.dcd++;
Alan Cox4b3b49b2009-11-30 13:16:36 +0000512 /* DCD raise - wake for open */
513 if (status & UART_MSR_DCD)
514 wake_up_interruptible(&port->port.open_wait);
515 else {
516 /* DCD drop - hang up if tty attached */
517 tty = tty_port_tty_get(&port->port);
518 if (tty) {
519 tty_hangup(tty);
520 tty_kref_put(tty);
521 }
522 }
523 }
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400524 if (status & UART_MSR_DCTS) {
525 port->icount.cts++;
Alan Cox530646f2009-11-05 13:28:29 +0000526 tty = tty_port_tty_get(&port->port);
527 if (tty && (tty->termios->c_cflag & CRTSCTS)) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400528 int cts = (status & UART_MSR_CTS);
Alan Coxb5849b12009-11-05 13:28:06 +0000529 if (tty->hw_stopped) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400530 if (cts) {
Alan Coxb5849b12009-11-05 13:28:06 +0000531 tty->hw_stopped = 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400532 sdio_uart_start_tx(port);
Alan Coxb5849b12009-11-05 13:28:06 +0000533 tty_wakeup(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400534 }
535 } else {
536 if (!cts) {
Alan Coxb5849b12009-11-05 13:28:06 +0000537 tty->hw_stopped = 1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400538 sdio_uart_stop_tx(port);
539 }
540 }
541 }
Alan Cox530646f2009-11-05 13:28:29 +0000542 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400543 }
544}
545
546/*
547 * This handles the interrupt from one port.
548 */
549static void sdio_uart_irq(struct sdio_func *func)
550{
551 struct sdio_uart_port *port = sdio_get_drvdata(func);
552 unsigned int iir, lsr;
553
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400554 /*
555 * In a few places sdio_uart_irq() is called directly instead of
556 * waiting for the actual interrupt to be raised and the SDIO IRQ
557 * thread scheduled in order to reduce latency. However, some
558 * interaction with the tty core may end up calling us back
559 * (serial echo, flow control, etc.) through those same places
560 * causing undesirable effects. Let's stop the recursion here.
561 */
562 if (unlikely(port->in_sdio_uart_irq == current))
563 return;
564
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400565 iir = sdio_in(port, UART_IIR);
566 if (iir & UART_IIR_NO_INT)
567 return;
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400568
569 port->in_sdio_uart_irq = current;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400570 lsr = sdio_in(port, UART_LSR);
571 if (lsr & UART_LSR_DR)
572 sdio_uart_receive_chars(port, &lsr);
573 sdio_uart_check_modem_status(port);
574 if (lsr & UART_LSR_THRE)
575 sdio_uart_transmit_chars(port);
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400576 port->in_sdio_uart_irq = NULL;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400577}
578
Alan Cox4b3b49b2009-11-30 13:16:36 +0000579static int uart_carrier_raised(struct tty_port *tport)
580{
581 struct sdio_uart_port *port =
582 container_of(tport, struct sdio_uart_port, port);
583 unsigned int ret = sdio_uart_claim_func(port);
584 if (ret) /* Missing hardware shoudn't block for carrier */
585 return 1;
586 ret = sdio_uart_get_mctrl(port);
587 sdio_uart_release_func(port);
588 if (ret & TIOCM_CAR)
589 return 1;
590 return 0;
591}
592
Alan Cox584abc32009-11-30 13:16:09 +0000593/**
594 * uart_dtr_rts - port helper to set uart signals
595 * @tport: tty port to be updated
596 * @onoff: set to turn on DTR/RTS
597 *
598 * Called by the tty port helpers when the modem signals need to be
599 * adjusted during an open, close and hangup.
600 */
Alan Cox530646f2009-11-05 13:28:29 +0000601
Alan Cox584abc32009-11-30 13:16:09 +0000602static void uart_dtr_rts(struct tty_port *tport, int onoff)
603{
604 struct sdio_uart_port *port =
605 container_of(tport, struct sdio_uart_port, port);
606 if (onoff == 0)
607 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
608 else
609 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
610}
611
612/**
613 * sdio_uart_activate - start up hardware
614 * @tport: tty port to activate
615 * @tty: tty bound to this port
616 *
617 * Activate a tty port. The port locking guarantees us this will be
618 * run exactly once per set of opens, and if successful will see the
619 * shutdown method run exactly once to match. Start up and shutdown are
620 * protected from each other by the internal locking and will not run
621 * at the same time even during a hangup event.
622 *
623 * If we successfully start up the port we take an extra kref as we
624 * will keep it around until shutdown when the kref is dropped.
625 */
626
627static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
628{
629 struct sdio_uart_port *port =
630 container_of(tport, struct sdio_uart_port, port);
631 unsigned long page;
632 int ret;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400633
634 /*
635 * Set the TTY IO error marker - we will only clear this
636 * once we have successfully opened the port.
637 */
Alan Coxb5849b12009-11-05 13:28:06 +0000638 set_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400639
640 /* Initialise and allocate the transmit buffer. */
641 page = __get_free_page(GFP_KERNEL);
642 if (!page)
Alan Cox584abc32009-11-30 13:16:09 +0000643 return -ENOMEM;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400644 port->xmit.buf = (unsigned char *)page;
645 circ_clear(&port->xmit);
646
647 ret = sdio_uart_claim_func(port);
648 if (ret)
649 goto err1;
650 ret = sdio_enable_func(port->func);
651 if (ret)
652 goto err2;
653 ret = sdio_claim_irq(port->func, sdio_uart_irq);
654 if (ret)
655 goto err3;
656
657 /*
658 * Clear the FIFO buffers and disable them.
659 * (they will be reenabled in sdio_change_speed())
660 */
661 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
662 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300663 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400664 sdio_out(port, UART_FCR, 0);
665
666 /*
667 * Clear the interrupt registers.
668 */
669 (void) sdio_in(port, UART_LSR);
670 (void) sdio_in(port, UART_RX);
671 (void) sdio_in(port, UART_IIR);
672 (void) sdio_in(port, UART_MSR);
673
674 /*
675 * Now, initialize the UART
676 */
677 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
678
Alan Coxc271cf32009-11-30 13:16:25 +0000679 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400680 port->mctrl = TIOCM_OUT2;
681
Alan Coxb5849b12009-11-05 13:28:06 +0000682 sdio_uart_change_speed(port, tty->termios, NULL);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400683
Alan Coxb5849b12009-11-05 13:28:06 +0000684 if (tty->termios->c_cflag & CBAUD)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400685 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
686
Alan Coxb5849b12009-11-05 13:28:06 +0000687 if (tty->termios->c_cflag & CRTSCTS)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400688 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
Alan Coxb5849b12009-11-05 13:28:06 +0000689 tty->hw_stopped = 1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400690
Nicolas Pitre0395b482009-11-05 13:28:17 +0000691 clear_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400692
693 /* Kick the IRQ handler once while we're still holding the host lock */
694 sdio_uart_irq(port->func);
695
696 sdio_uart_release_func(port);
697 return 0;
698
699err3:
700 sdio_disable_func(port->func);
701err2:
702 sdio_uart_release_func(port);
703err1:
704 free_page((unsigned long)port->xmit.buf);
705 return ret;
706}
707
Alan Cox584abc32009-11-30 13:16:09 +0000708/**
709 * sdio_uart_shutdown - stop hardware
710 * @tport: tty port to shut down
711 *
712 * Deactivate a tty port. The port locking guarantees us this will be
713 * run only if a successful matching activate already ran. The two are
714 * protected from each other by the internal locking and will not run
715 * at the same time even during a hangup event.
716 */
717
718static void sdio_uart_shutdown(struct tty_port *tport)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400719{
Alan Cox584abc32009-11-30 13:16:09 +0000720 struct sdio_uart_port *port =
721 container_of(tport, struct sdio_uart_port, port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400722 int ret;
723
724 ret = sdio_uart_claim_func(port);
725 if (ret)
726 goto skip;
727
728 sdio_uart_stop_rx(port);
729
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300730 /* Disable interrupts from this port */
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400731 sdio_release_irq(port->func);
732 port->ier = 0;
733 sdio_out(port, UART_IER, 0);
734
735 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
736
737 /* Disable break condition and FIFOs. */
738 port->lcr &= ~UART_LCR_SBC;
739 sdio_out(port, UART_LCR, port->lcr);
740 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
741 UART_FCR_CLEAR_RCVR |
742 UART_FCR_CLEAR_XMIT);
743 sdio_out(port, UART_FCR, 0);
744
745 sdio_disable_func(port->func);
746
747 sdio_uart_release_func(port);
748
749skip:
750 /* Free the transmit buffer page. */
751 free_page((unsigned long)port->xmit.buf);
752}
753
Alan Cox584abc32009-11-30 13:16:09 +0000754/**
755 * sdio_uart_install - install method
756 * @driver: the driver in use (sdio_uart in our case)
757 * @tty: the tty being bound
758 *
759 * Look up and bind the tty and the driver together. Initialize
760 * any needed private data (in our case the termios)
761 */
762
763static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
764{
765 int idx = tty->index;
766 struct sdio_uart_port *port = sdio_uart_port_get(idx);
767 int ret = tty_init_termios(tty);
768
769 if (ret == 0) {
770 tty_driver_kref_get(driver);
771 tty->count++;
772 /* This is the ref sdio_uart_port get provided */
773 tty->driver_data = port;
774 driver->ttys[idx] = tty;
775 } else
776 sdio_uart_port_put(port);
777 return ret;
Alan Cox584abc32009-11-30 13:16:09 +0000778}
779
780/**
781 * sdio_uart_cleanup - called on the last tty kref drop
782 * @tty: the tty being destroyed
783 *
784 * Called asynchronously when the last reference to the tty is dropped.
785 * We cannot destroy the tty->driver_data port kref until this point
786 */
787
788static void sdio_uart_cleanup(struct tty_struct *tty)
789{
790 struct sdio_uart_port *port = tty->driver_data;
791 tty->driver_data = NULL; /* Bug trap */
792 sdio_uart_port_put(port);
793}
794
795/*
796 * Open/close/hangup is now entirely boilerplate
797 */
798
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300799static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400800{
Alan Cox584abc32009-11-30 13:16:09 +0000801 struct sdio_uart_port *port = tty->driver_data;
802 return tty_port_open(&port->port, tty, filp);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400803}
804
805static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
806{
807 struct sdio_uart_port *port = tty->driver_data;
Alan Cox584abc32009-11-30 13:16:09 +0000808 tty_port_close(&port->port, tty, filp);
809}
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400810
Alan Cox584abc32009-11-30 13:16:09 +0000811static void sdio_uart_hangup(struct tty_struct *tty)
812{
813 struct sdio_uart_port *port = tty->driver_data;
814 tty_port_hangup(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400815}
816
Alan Coxc271cf32009-11-30 13:16:25 +0000817static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400818 int count)
819{
820 struct sdio_uart_port *port = tty->driver_data;
821 struct circ_buf *circ = &port->xmit;
822 int c, ret = 0;
823
824 if (!port->func)
825 return -ENODEV;
826
827 spin_lock(&port->write_lock);
828 while (1) {
829 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
830 if (count < c)
831 c = count;
832 if (c <= 0)
833 break;
834 memcpy(circ->buf + circ->head, buf, c);
835 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
836 buf += c;
837 count -= c;
838 ret += c;
839 }
840 spin_unlock(&port->write_lock);
841
Alan Coxc271cf32009-11-30 13:16:25 +0000842 if (!(port->ier & UART_IER_THRI)) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400843 int err = sdio_uart_claim_func(port);
844 if (!err) {
845 sdio_uart_start_tx(port);
846 sdio_uart_irq(port->func);
847 sdio_uart_release_func(port);
848 } else
849 ret = err;
850 }
851
852 return ret;
853}
854
855static int sdio_uart_write_room(struct tty_struct *tty)
856{
857 struct sdio_uart_port *port = tty->driver_data;
858 return port ? circ_chars_free(&port->xmit) : 0;
859}
860
861static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
862{
863 struct sdio_uart_port *port = tty->driver_data;
864 return port ? circ_chars_pending(&port->xmit) : 0;
865}
866
867static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
868{
869 struct sdio_uart_port *port = tty->driver_data;
870
871 port->x_char = ch;
872 if (ch && !(port->ier & UART_IER_THRI)) {
873 if (sdio_uart_claim_func(port) != 0)
874 return;
875 sdio_uart_start_tx(port);
876 sdio_uart_irq(port->func);
877 sdio_uart_release_func(port);
878 }
879}
880
881static void sdio_uart_throttle(struct tty_struct *tty)
882{
883 struct sdio_uart_port *port = tty->driver_data;
884
885 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
886 return;
887
888 if (sdio_uart_claim_func(port) != 0)
889 return;
890
891 if (I_IXOFF(tty)) {
892 port->x_char = STOP_CHAR(tty);
893 sdio_uart_start_tx(port);
894 }
895
896 if (tty->termios->c_cflag & CRTSCTS)
897 sdio_uart_clear_mctrl(port, TIOCM_RTS);
898
899 sdio_uart_irq(port->func);
900 sdio_uart_release_func(port);
901}
902
903static void sdio_uart_unthrottle(struct tty_struct *tty)
904{
905 struct sdio_uart_port *port = tty->driver_data;
906
907 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
908 return;
909
910 if (sdio_uart_claim_func(port) != 0)
911 return;
912
913 if (I_IXOFF(tty)) {
914 if (port->x_char) {
915 port->x_char = 0;
916 } else {
917 port->x_char = START_CHAR(tty);
918 sdio_uart_start_tx(port);
919 }
920 }
921
922 if (tty->termios->c_cflag & CRTSCTS)
923 sdio_uart_set_mctrl(port, TIOCM_RTS);
924
925 sdio_uart_irq(port->func);
926 sdio_uart_release_func(port);
927}
928
Alan Coxc271cf32009-11-30 13:16:25 +0000929static void sdio_uart_set_termios(struct tty_struct *tty,
930 struct ktermios *old_termios)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400931{
932 struct sdio_uart_port *port = tty->driver_data;
933 unsigned int cflag = tty->termios->c_cflag;
934
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400935 if (sdio_uart_claim_func(port) != 0)
936 return;
937
938 sdio_uart_change_speed(port, tty->termios, old_termios);
939
940 /* Handle transition to B0 status */
941 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
942 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
943
944 /* Handle transition away from B0 status */
945 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
946 unsigned int mask = TIOCM_DTR;
947 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
948 mask |= TIOCM_RTS;
949 sdio_uart_set_mctrl(port, mask);
950 }
951
952 /* Handle turning off CRTSCTS */
953 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
954 tty->hw_stopped = 0;
955 sdio_uart_start_tx(port);
956 }
957
958 /* Handle turning on CRTSCTS */
959 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
960 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
961 tty->hw_stopped = 1;
962 sdio_uart_stop_tx(port);
963 }
964 }
965
966 sdio_uart_release_func(port);
967}
968
David Howellsc43d86362008-07-10 12:28:48 +0100969static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400970{
971 struct sdio_uart_port *port = tty->driver_data;
David Howellsc43d86362008-07-10 12:28:48 +0100972 int result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400973
David Howellsc43d86362008-07-10 12:28:48 +0100974 result = sdio_uart_claim_func(port);
975 if (result != 0)
976 return result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400977
978 if (break_state == -1)
979 port->lcr |= UART_LCR_SBC;
980 else
981 port->lcr &= ~UART_LCR_SBC;
982 sdio_out(port, UART_LCR, port->lcr);
983
984 sdio_uart_release_func(port);
David Howellsc43d86362008-07-10 12:28:48 +0100985 return 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400986}
987
988static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
989{
990 struct sdio_uart_port *port = tty->driver_data;
991 int result;
992
993 result = sdio_uart_claim_func(port);
994 if (!result) {
995 result = port->mctrl | sdio_uart_get_mctrl(port);
996 sdio_uart_release_func(port);
997 }
998
999 return result;
1000}
1001
1002static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
1003 unsigned int set, unsigned int clear)
1004{
1005 struct sdio_uart_port *port = tty->driver_data;
1006 int result;
1007
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001008 result = sdio_uart_claim_func(port);
Alan Coxc271cf32009-11-30 13:16:25 +00001009 if (!result) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001010 sdio_uart_update_mctrl(port, set, clear);
1011 sdio_uart_release_func(port);
1012 }
1013
1014 return result;
1015}
1016
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001017static int sdio_uart_proc_show(struct seq_file *m, void *v)
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001018{
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001019 int i;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001020
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001021 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001022 "", "", "");
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001023 for (i = 0; i < UART_NR; i++) {
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001024 struct sdio_uart_port *port = sdio_uart_port_get(i);
1025 if (port) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001026 seq_printf(m, "%d: uart:SDIO", i);
Alan Coxc271cf32009-11-30 13:16:25 +00001027 if (capable(CAP_SYS_ADMIN)) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001028 seq_printf(m, " tx:%d rx:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001029 port->icount.tx, port->icount.rx);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001030 if (port->icount.frame)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001031 seq_printf(m, " fe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001032 port->icount.frame);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001033 if (port->icount.parity)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001034 seq_printf(m, " pe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001035 port->icount.parity);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001036 if (port->icount.brk)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001037 seq_printf(m, " brk:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001038 port->icount.brk);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001039 if (port->icount.overrun)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001040 seq_printf(m, " oe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001041 port->icount.overrun);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001042 if (port->icount.cts)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001043 seq_printf(m, " cts:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001044 port->icount.cts);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001045 if (port->icount.dsr)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001046 seq_printf(m, " dsr:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001047 port->icount.dsr);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001048 if (port->icount.rng)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001049 seq_printf(m, " rng:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001050 port->icount.rng);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001051 if (port->icount.dcd)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001052 seq_printf(m, " dcd:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001053 port->icount.dcd);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001054 }
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001055 sdio_uart_port_put(port);
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001056 seq_putc(m, '\n');
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001057 }
1058 }
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001059 return 0;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001060}
1061
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001062static int sdio_uart_proc_open(struct inode *inode, struct file *file)
1063{
1064 return single_open(file, sdio_uart_proc_show, NULL);
1065}
1066
1067static const struct file_operations sdio_uart_proc_fops = {
1068 .owner = THIS_MODULE,
1069 .open = sdio_uart_proc_open,
1070 .read = seq_read,
1071 .llseek = seq_lseek,
1072 .release = single_release,
1073};
1074
Alan Cox584abc32009-11-30 13:16:09 +00001075static const struct tty_port_operations sdio_uart_port_ops = {
1076 .dtr_rts = uart_dtr_rts,
Alan Cox4b3b49b2009-11-30 13:16:36 +00001077 .carrier_raised = uart_carrier_raised,
Alan Cox584abc32009-11-30 13:16:09 +00001078 .shutdown = sdio_uart_shutdown,
1079 .activate = sdio_uart_activate,
1080};
1081
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001082static const struct tty_operations sdio_uart_ops = {
1083 .open = sdio_uart_open,
1084 .close = sdio_uart_close,
1085 .write = sdio_uart_write,
1086 .write_room = sdio_uart_write_room,
1087 .chars_in_buffer = sdio_uart_chars_in_buffer,
1088 .send_xchar = sdio_uart_send_xchar,
1089 .throttle = sdio_uart_throttle,
1090 .unthrottle = sdio_uart_unthrottle,
1091 .set_termios = sdio_uart_set_termios,
Alan Cox584abc32009-11-30 13:16:09 +00001092 .hangup = sdio_uart_hangup,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001093 .break_ctl = sdio_uart_break_ctl,
1094 .tiocmget = sdio_uart_tiocmget,
1095 .tiocmset = sdio_uart_tiocmset,
Alan Cox584abc32009-11-30 13:16:09 +00001096 .install = sdio_uart_install,
1097 .cleanup = sdio_uart_cleanup,
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001098 .proc_fops = &sdio_uart_proc_fops,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001099};
1100
1101static struct tty_driver *sdio_uart_tty_driver;
1102
1103static int sdio_uart_probe(struct sdio_func *func,
1104 const struct sdio_device_id *id)
1105{
1106 struct sdio_uart_port *port;
1107 int ret;
1108
1109 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1110 if (!port)
1111 return -ENOMEM;
1112
1113 if (func->class == SDIO_CLASS_UART) {
1114 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1115 sdio_func_id(func));
1116 kfree(port);
1117 return -ENOSYS;
1118 } else if (func->class == SDIO_CLASS_GPS) {
1119 /*
1120 * We need tuple 0x91. It contains SUBTPL_SIOREG
1121 * and SUBTPL_RCVCAPS.
1122 */
1123 struct sdio_func_tuple *tpl;
1124 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1125 if (tpl->code != 0x91)
1126 continue;
1127 if (tpl->size < 10)
1128 continue;
1129 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1130 break;
1131 }
1132 if (!tpl) {
1133 printk(KERN_WARNING
Alan Coxc271cf32009-11-30 13:16:25 +00001134 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001135 sdio_func_id(func));
1136 kfree(port);
1137 return -EINVAL;
1138 }
1139 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1140 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1141 port->regs_offset = (tpl->data[4] << 0) |
1142 (tpl->data[5] << 8) |
1143 (tpl->data[6] << 16);
1144 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1145 sdio_func_id(func), port->regs_offset);
1146 port->uartclk = tpl->data[7] * 115200;
1147 if (port->uartclk == 0)
1148 port->uartclk = 115200;
1149 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1150 sdio_func_id(func), port->uartclk,
1151 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1152 } else {
1153 kfree(port);
1154 return -EINVAL;
1155 }
1156
1157 port->func = func;
1158 sdio_set_drvdata(func, port);
Alan Coxb5849b12009-11-05 13:28:06 +00001159 tty_port_init(&port->port);
Alan Cox584abc32009-11-30 13:16:09 +00001160 port->port.ops = &sdio_uart_port_ops;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001161
1162 ret = sdio_uart_add_port(port);
1163 if (ret) {
1164 kfree(port);
1165 } else {
1166 struct device *dev;
Alan Coxc271cf32009-11-30 13:16:25 +00001167 dev = tty_register_device(sdio_uart_tty_driver,
1168 port->index, &func->dev);
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001169 if (IS_ERR(dev)) {
1170 sdio_uart_port_remove(port);
1171 ret = PTR_ERR(dev);
1172 }
1173 }
1174
1175 return ret;
1176}
1177
1178static void sdio_uart_remove(struct sdio_func *func)
1179{
1180 struct sdio_uart_port *port = sdio_get_drvdata(func);
1181
1182 tty_unregister_device(sdio_uart_tty_driver, port->index);
1183 sdio_uart_port_remove(port);
1184}
1185
1186static const struct sdio_device_id sdio_uart_ids[] = {
1187 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1188 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1189 { /* end: all zeroes */ },
1190};
1191
1192MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1193
1194static struct sdio_driver sdio_uart_driver = {
1195 .probe = sdio_uart_probe,
1196 .remove = sdio_uart_remove,
1197 .name = "sdio_uart",
1198 .id_table = sdio_uart_ids,
1199};
1200
1201static int __init sdio_uart_init(void)
1202{
1203 int ret;
1204 struct tty_driver *tty_drv;
1205
1206 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1207 if (!tty_drv)
1208 return -ENOMEM;
1209
1210 tty_drv->owner = THIS_MODULE;
1211 tty_drv->driver_name = "sdio_uart";
1212 tty_drv->name = "ttySDIO";
1213 tty_drv->major = 0; /* dynamically allocated */
1214 tty_drv->minor_start = 0;
1215 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1216 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1217 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1218 tty_drv->init_termios = tty_std_termios;
1219 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
Nicolas Pitre2ba30ee2007-08-15 13:27:29 -04001220 tty_drv->init_termios.c_ispeed = 4800;
1221 tty_drv->init_termios.c_ospeed = 4800;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001222 tty_set_operations(tty_drv, &sdio_uart_ops);
1223
1224 ret = tty_register_driver(tty_drv);
1225 if (ret)
1226 goto err1;
1227
1228 ret = sdio_register_driver(&sdio_uart_driver);
1229 if (ret)
1230 goto err2;
1231
1232 return 0;
1233
1234err2:
1235 tty_unregister_driver(tty_drv);
1236err1:
1237 put_tty_driver(tty_drv);
1238 return ret;
1239}
1240
1241static void __exit sdio_uart_exit(void)
1242{
1243 sdio_unregister_driver(&sdio_uart_driver);
1244 tty_unregister_driver(sdio_uart_tty_driver);
1245 put_tty_driver(sdio_uart_tty_driver);
1246}
1247
1248module_init(sdio_uart_init);
1249module_exit(sdio_uart_exit);
1250
1251MODULE_AUTHOR("Nicolas Pitre");
1252MODULE_LICENSE("GPL");