blob: 894078be0b9689355b575f7fce4c2ee8904d382c [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>
Nicolas Pitre6e418a92007-06-30 02:04:21 -040037#include <linux/tty.h>
38#include <linux/tty_flip.h>
Alan Cox8b197a52010-02-08 10:08:39 +000039#include <linux/kfifo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Nicolas Pitre6e418a92007-06-30 02:04:21 -040041
42#include <linux/mmc/core.h>
43#include <linux/mmc/card.h>
44#include <linux/mmc/sdio_func.h>
45#include <linux/mmc/sdio_ids.h>
46
47
48#define UART_NR 8 /* Number of UARTs this driver can handle */
49
50
Alan Cox8b197a52010-02-08 10:08:39 +000051#define FIFO_SIZE PAGE_SIZE
Nicolas Pitre6e418a92007-06-30 02:04:21 -040052#define WAKEUP_CHARS 256
53
Nicolas Pitre6e418a92007-06-30 02:04:21 -040054struct uart_icount {
55 __u32 cts;
56 __u32 dsr;
57 __u32 rng;
58 __u32 dcd;
59 __u32 rx;
60 __u32 tx;
61 __u32 frame;
62 __u32 overrun;
63 __u32 parity;
64 __u32 brk;
65};
66
67struct sdio_uart_port {
Alan Coxb5849b12009-11-05 13:28:06 +000068 struct tty_port port;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040069 unsigned int index;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040070 struct sdio_func *func;
71 struct mutex func_lock;
Nicolas Pitre15b82b42007-08-20 17:17:37 -040072 struct task_struct *in_sdio_uart_irq;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040073 unsigned int regs_offset;
Alan Cox8b197a52010-02-08 10:08:39 +000074 struct kfifo xmit_fifo;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040075 spinlock_t write_lock;
76 struct uart_icount icount;
77 unsigned int uartclk;
78 unsigned int mctrl;
Alan Cox4b3b49b2009-11-30 13:16:36 +000079 unsigned int rx_mctrl;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040080 unsigned int read_status_mask;
81 unsigned int ignore_status_mask;
82 unsigned char x_char;
83 unsigned char ier;
84 unsigned char lcr;
85};
86
87static struct sdio_uart_port *sdio_uart_table[UART_NR];
88static DEFINE_SPINLOCK(sdio_uart_table_lock);
89
90static int sdio_uart_add_port(struct sdio_uart_port *port)
91{
92 int index, ret = -EBUSY;
93
Nicolas Pitre6e418a92007-06-30 02:04:21 -040094 mutex_init(&port->func_lock);
95 spin_lock_init(&port->write_lock);
Alan Cox8b197a52010-02-08 10:08:39 +000096 if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
97 return -ENOMEM;
Nicolas Pitre6e418a92007-06-30 02:04:21 -040098
99 spin_lock(&sdio_uart_table_lock);
100 for (index = 0; index < UART_NR; index++) {
101 if (!sdio_uart_table[index]) {
102 port->index = index;
103 sdio_uart_table[index] = port;
104 ret = 0;
105 break;
106 }
107 }
108 spin_unlock(&sdio_uart_table_lock);
109
110 return ret;
111}
112
113static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
114{
115 struct sdio_uart_port *port;
116
117 if (index >= UART_NR)
118 return NULL;
119
120 spin_lock(&sdio_uart_table_lock);
121 port = sdio_uart_table[index];
122 if (port)
Jiri Slabye70c6772012-11-15 09:49:52 +0100123 tty_port_get(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400124 spin_unlock(&sdio_uart_table_lock);
125
126 return port;
127}
128
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400129static void sdio_uart_port_put(struct sdio_uart_port *port)
130{
Jiri Slabye70c6772012-11-15 09:49:52 +0100131 tty_port_put(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400132}
133
134static void sdio_uart_port_remove(struct sdio_uart_port *port)
135{
136 struct sdio_func *func;
Alan Cox584abc32009-11-30 13:16:09 +0000137 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400138
139 BUG_ON(sdio_uart_table[port->index] != port);
140
141 spin_lock(&sdio_uart_table_lock);
142 sdio_uart_table[port->index] = NULL;
143 spin_unlock(&sdio_uart_table_lock);
144
145 /*
146 * We're killing a port that potentially still is in use by
147 * the tty layer. Be careful to prevent any further access
148 * to the SDIO function and arrange for the tty layer to
149 * give up on that port ASAP.
150 * Beware: the lock ordering is critical.
151 */
Alan Cox0a68f642009-11-05 13:28:38 +0000152 mutex_lock(&port->port.mutex);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400153 mutex_lock(&port->func_lock);
154 func = port->func;
155 sdio_claim_host(func);
156 port->func = NULL;
157 mutex_unlock(&port->func_lock);
Alan Cox584abc32009-11-30 13:16:09 +0000158 tty = tty_port_tty_get(&port->port);
159 /* tty_hangup is async so is this safe as is ?? */
160 if (tty) {
161 tty_hangup(tty);
Alan Cox530646f2009-11-05 13:28:29 +0000162 tty_kref_put(tty);
163 }
Alan Cox0a68f642009-11-05 13:28:38 +0000164 mutex_unlock(&port->port.mutex);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400165 sdio_release_irq(func);
166 sdio_disable_func(func);
167 sdio_release_host(func);
168
169 sdio_uart_port_put(port);
170}
171
172static int sdio_uart_claim_func(struct sdio_uart_port *port)
173{
174 mutex_lock(&port->func_lock);
175 if (unlikely(!port->func)) {
176 mutex_unlock(&port->func_lock);
177 return -ENODEV;
178 }
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400179 if (likely(port->in_sdio_uart_irq != current))
180 sdio_claim_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400181 mutex_unlock(&port->func_lock);
182 return 0;
183}
184
185static inline void sdio_uart_release_func(struct sdio_uart_port *port)
186{
Nicolas Pitre15b82b42007-08-20 17:17:37 -0400187 if (likely(port->in_sdio_uart_irq != current))
188 sdio_release_host(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400189}
190
191static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
192{
193 unsigned char c;
194 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
195 return c;
196}
197
198static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
199{
200 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
201}
202
203static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
204{
205 unsigned char status;
206 unsigned int ret;
207
Alan Cox4b3b49b2009-11-30 13:16:36 +0000208 /* FIXME: What stops this losing the delta bits and breaking
209 sdio_uart_check_modem_status ? */
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400210 status = sdio_in(port, UART_MSR);
211
212 ret = 0;
213 if (status & UART_MSR_DCD)
214 ret |= TIOCM_CAR;
215 if (status & UART_MSR_RI)
216 ret |= TIOCM_RNG;
217 if (status & UART_MSR_DSR)
218 ret |= TIOCM_DSR;
219 if (status & UART_MSR_CTS)
220 ret |= TIOCM_CTS;
221 return ret;
222}
223
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300224static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
225 unsigned int mctrl)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400226{
227 unsigned char mcr = 0;
228
229 if (mctrl & TIOCM_RTS)
230 mcr |= UART_MCR_RTS;
231 if (mctrl & TIOCM_DTR)
232 mcr |= UART_MCR_DTR;
233 if (mctrl & TIOCM_OUT1)
234 mcr |= UART_MCR_OUT1;
235 if (mctrl & TIOCM_OUT2)
236 mcr |= UART_MCR_OUT2;
237 if (mctrl & TIOCM_LOOP)
238 mcr |= UART_MCR_LOOP;
239
240 sdio_out(port, UART_MCR, mcr);
241}
242
243static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
244 unsigned int set, unsigned int clear)
245{
246 unsigned int old;
247
248 old = port->mctrl;
249 port->mctrl = (old & ~clear) | set;
250 if (old != port->mctrl)
251 sdio_uart_write_mctrl(port, port->mctrl);
252}
253
254#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
255#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
256
257static void sdio_uart_change_speed(struct sdio_uart_port *port,
258 struct ktermios *termios,
259 struct ktermios *old)
260{
261 unsigned char cval, fcr = 0;
262 unsigned int baud, quot;
263
264 switch (termios->c_cflag & CSIZE) {
265 case CS5:
266 cval = UART_LCR_WLEN5;
267 break;
268 case CS6:
269 cval = UART_LCR_WLEN6;
270 break;
271 case CS7:
272 cval = UART_LCR_WLEN7;
273 break;
274 default:
275 case CS8:
276 cval = UART_LCR_WLEN8;
277 break;
278 }
279
280 if (termios->c_cflag & CSTOPB)
281 cval |= UART_LCR_STOP;
282 if (termios->c_cflag & PARENB)
283 cval |= UART_LCR_PARITY;
284 if (!(termios->c_cflag & PARODD))
285 cval |= UART_LCR_EPAR;
286
287 for (;;) {
288 baud = tty_termios_baud_rate(termios);
289 if (baud == 0)
290 baud = 9600; /* Special case: B0 rate. */
291 if (baud <= port->uartclk)
292 break;
293 /*
294 * Oops, the quotient was zero. Try again with the old
295 * baud rate if possible, otherwise default to 9600.
296 */
297 termios->c_cflag &= ~CBAUD;
298 if (old) {
299 termios->c_cflag |= old->c_cflag & CBAUD;
300 old = NULL;
301 } else
302 termios->c_cflag |= B9600;
303 }
304 quot = (2 * port->uartclk + baud) / (2 * baud);
305
306 if (baud < 2400)
307 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
308 else
309 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
310
311 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
312 if (termios->c_iflag & INPCK)
313 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
314 if (termios->c_iflag & (BRKINT | PARMRK))
315 port->read_status_mask |= UART_LSR_BI;
316
317 /*
318 * Characters to ignore
319 */
320 port->ignore_status_mask = 0;
321 if (termios->c_iflag & IGNPAR)
322 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
323 if (termios->c_iflag & IGNBRK) {
324 port->ignore_status_mask |= UART_LSR_BI;
325 /*
326 * If we're ignoring parity and break indicators,
327 * ignore overruns too (for real raw support).
328 */
329 if (termios->c_iflag & IGNPAR)
330 port->ignore_status_mask |= UART_LSR_OE;
331 }
332
333 /*
334 * ignore all characters if CREAD is not set
335 */
336 if ((termios->c_cflag & CREAD) == 0)
337 port->ignore_status_mask |= UART_LSR_DR;
338
339 /*
340 * CTS flow control flag and modem status interrupts
341 */
342 port->ier &= ~UART_IER_MSI;
343 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
344 port->ier |= UART_IER_MSI;
345
346 port->lcr = cval;
347
348 sdio_out(port, UART_IER, port->ier);
349 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
350 sdio_out(port, UART_DLL, quot & 0xff);
351 sdio_out(port, UART_DLM, quot >> 8);
352 sdio_out(port, UART_LCR, cval);
353 sdio_out(port, UART_FCR, fcr);
354
355 sdio_uart_write_mctrl(port, port->mctrl);
356}
357
358static void sdio_uart_start_tx(struct sdio_uart_port *port)
359{
360 if (!(port->ier & UART_IER_THRI)) {
361 port->ier |= UART_IER_THRI;
362 sdio_out(port, UART_IER, port->ier);
363 }
364}
365
366static void sdio_uart_stop_tx(struct sdio_uart_port *port)
367{
368 if (port->ier & UART_IER_THRI) {
369 port->ier &= ~UART_IER_THRI;
370 sdio_out(port, UART_IER, port->ier);
371 }
372}
373
374static void sdio_uart_stop_rx(struct sdio_uart_port *port)
375{
376 port->ier &= ~UART_IER_RLSI;
377 port->read_status_mask &= ~UART_LSR_DR;
378 sdio_out(port, UART_IER, port->ier);
379}
380
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300381static void sdio_uart_receive_chars(struct sdio_uart_port *port,
382 unsigned int *status)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400383{
Alan Cox530646f2009-11-05 13:28:29 +0000384 struct tty_struct *tty = tty_port_tty_get(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400385 unsigned int ch, flag;
386 int max_count = 256;
387
388 do {
389 ch = sdio_in(port, UART_RX);
390 flag = TTY_NORMAL;
391 port->icount.rx++;
392
393 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300394 UART_LSR_FE | UART_LSR_OE))) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400395 /*
396 * For statistics only
397 */
398 if (*status & UART_LSR_BI) {
399 *status &= ~(UART_LSR_FE | UART_LSR_PE);
400 port->icount.brk++;
401 } else if (*status & UART_LSR_PE)
402 port->icount.parity++;
403 else if (*status & UART_LSR_FE)
404 port->icount.frame++;
405 if (*status & UART_LSR_OE)
406 port->icount.overrun++;
407
408 /*
409 * Mask off conditions which should be ignored.
410 */
411 *status &= port->read_status_mask;
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300412 if (*status & UART_LSR_BI)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400413 flag = TTY_BREAK;
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300414 else if (*status & UART_LSR_PE)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400415 flag = TTY_PARITY;
416 else if (*status & UART_LSR_FE)
417 flag = TTY_FRAME;
418 }
419
420 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
Alan Cox530646f2009-11-05 13:28:29 +0000421 if (tty)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100422 tty_insert_flip_char(&port->port, ch, flag);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400423
424 /*
425 * Overrun is special. Since it's reported immediately,
426 * it doesn't affect the current character.
427 */
428 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
Alan Cox530646f2009-11-05 13:28:29 +0000429 if (tty)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100430 tty_insert_flip_char(&port->port, 0,
431 TTY_OVERRUN);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400432
433 *status = sdio_in(port, UART_LSR);
434 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Alan Cox530646f2009-11-05 13:28:29 +0000435 if (tty) {
436 tty_flip_buffer_push(tty);
437 tty_kref_put(tty);
438 }
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400439}
440
441static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
442{
Alan Cox8b197a52010-02-08 10:08:39 +0000443 struct kfifo *xmit = &port->xmit_fifo;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400444 int count;
Alan Cox530646f2009-11-05 13:28:29 +0000445 struct tty_struct *tty;
Alan Cox8b197a52010-02-08 10:08:39 +0000446 u8 iobuf[16];
447 int len;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400448
449 if (port->x_char) {
450 sdio_out(port, UART_TX, port->x_char);
451 port->icount.tx++;
452 port->x_char = 0;
453 return;
454 }
Alan Cox530646f2009-11-05 13:28:29 +0000455
456 tty = tty_port_tty_get(&port->port);
457
Alan Cox8b197a52010-02-08 10:08:39 +0000458 if (tty == NULL || !kfifo_len(xmit) ||
Alan Coxc271cf32009-11-30 13:16:25 +0000459 tty->stopped || tty->hw_stopped) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400460 sdio_uart_stop_tx(port);
Alan Cox530646f2009-11-05 13:28:29 +0000461 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400462 return;
463 }
464
Alan Cox8b197a52010-02-08 10:08:39 +0000465 len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
466 for (count = 0; count < len; count++) {
467 sdio_out(port, UART_TX, iobuf[count]);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400468 port->icount.tx++;
Alan Cox8b197a52010-02-08 10:08:39 +0000469 }
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400470
Alan Cox8b197a52010-02-08 10:08:39 +0000471 len = kfifo_len(xmit);
472 if (len < WAKEUP_CHARS) {
Alan Coxb5849b12009-11-05 13:28:06 +0000473 tty_wakeup(tty);
Alan Cox8b197a52010-02-08 10:08:39 +0000474 if (len == 0)
475 sdio_uart_stop_tx(port);
476 }
Alan Cox530646f2009-11-05 13:28:29 +0000477 tty_kref_put(tty);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400478}
479
480static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
481{
482 int status;
Alan Cox530646f2009-11-05 13:28:29 +0000483 struct tty_struct *tty;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400484
485 status = sdio_in(port, UART_MSR);
486
487 if ((status & UART_MSR_ANY_DELTA) == 0)
488 return;
489
490 if (status & UART_MSR_TERI)
491 port->icount.rng++;
492 if (status & UART_MSR_DDSR)
493 port->icount.dsr++;
Alan Cox4b3b49b2009-11-30 13:16:36 +0000494 if (status & UART_MSR_DDCD) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400495 port->icount.dcd++;
Alan Cox4b3b49b2009-11-30 13:16:36 +0000496 /* DCD raise - wake for open */
497 if (status & UART_MSR_DCD)
498 wake_up_interruptible(&port->port.open_wait);
499 else {
500 /* DCD drop - hang up if tty attached */
501 tty = tty_port_tty_get(&port->port);
502 if (tty) {
503 tty_hangup(tty);
504 tty_kref_put(tty);
505 }
506 }
507 }
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400508 if (status & UART_MSR_DCTS) {
509 port->icount.cts++;
Alan Cox530646f2009-11-05 13:28:29 +0000510 tty = tty_port_tty_get(&port->port);
Alan Coxadc8d742012-07-14 15:31:47 +0100511 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 Cox4b3b49b2009-11-30 13:16:36 +0000563static int uart_carrier_raised(struct tty_port *tport)
564{
565 struct sdio_uart_port *port =
566 container_of(tport, struct sdio_uart_port, port);
567 unsigned int ret = sdio_uart_claim_func(port);
Adam Buchbinderc9404c92009-12-18 15:40:42 -0500568 if (ret) /* Missing hardware shouldn't block for carrier */
Alan Cox4b3b49b2009-11-30 13:16:36 +0000569 return 1;
570 ret = sdio_uart_get_mctrl(port);
571 sdio_uart_release_func(port);
572 if (ret & TIOCM_CAR)
573 return 1;
574 return 0;
575}
576
Alan Cox584abc32009-11-30 13:16:09 +0000577/**
578 * uart_dtr_rts - port helper to set uart signals
579 * @tport: tty port to be updated
580 * @onoff: set to turn on DTR/RTS
581 *
582 * Called by the tty port helpers when the modem signals need to be
583 * adjusted during an open, close and hangup.
584 */
Alan Cox530646f2009-11-05 13:28:29 +0000585
Alan Cox584abc32009-11-30 13:16:09 +0000586static void uart_dtr_rts(struct tty_port *tport, int onoff)
587{
588 struct sdio_uart_port *port =
589 container_of(tport, struct sdio_uart_port, port);
Alan Cox1f100b32009-11-30 13:16:30 +0000590 int ret = sdio_uart_claim_func(port);
591 if (ret)
592 return;
Alan Cox584abc32009-11-30 13:16:09 +0000593 if (onoff == 0)
594 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
595 else
596 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
Alan Cox1f100b32009-11-30 13:16:30 +0000597 sdio_uart_release_func(port);
Alan Cox584abc32009-11-30 13:16:09 +0000598}
599
600/**
601 * sdio_uart_activate - start up hardware
602 * @tport: tty port to activate
603 * @tty: tty bound to this port
604 *
605 * Activate a tty port. The port locking guarantees us this will be
606 * run exactly once per set of opens, and if successful will see the
607 * shutdown method run exactly once to match. Start up and shutdown are
608 * protected from each other by the internal locking and will not run
609 * at the same time even during a hangup event.
610 *
611 * If we successfully start up the port we take an extra kref as we
612 * will keep it around until shutdown when the kref is dropped.
613 */
614
615static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
616{
617 struct sdio_uart_port *port =
618 container_of(tport, struct sdio_uart_port, port);
Alan Cox584abc32009-11-30 13:16:09 +0000619 int ret;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400620
621 /*
622 * Set the TTY IO error marker - we will only clear this
623 * once we have successfully opened the port.
624 */
Alan Coxb5849b12009-11-05 13:28:06 +0000625 set_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400626
Alan Cox8b197a52010-02-08 10:08:39 +0000627 kfifo_reset(&port->xmit_fifo);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400628
629 ret = sdio_uart_claim_func(port);
630 if (ret)
Alan Cox8b197a52010-02-08 10:08:39 +0000631 return ret;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400632 ret = sdio_enable_func(port->func);
633 if (ret)
Alan Cox8b197a52010-02-08 10:08:39 +0000634 goto err1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400635 ret = sdio_claim_irq(port->func, sdio_uart_irq);
636 if (ret)
Alan Cox8b197a52010-02-08 10:08:39 +0000637 goto err2;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400638
639 /*
640 * Clear the FIFO buffers and disable them.
641 * (they will be reenabled in sdio_change_speed())
642 */
643 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
644 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300645 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400646 sdio_out(port, UART_FCR, 0);
647
648 /*
649 * Clear the interrupt registers.
650 */
651 (void) sdio_in(port, UART_LSR);
652 (void) sdio_in(port, UART_RX);
653 (void) sdio_in(port, UART_IIR);
654 (void) sdio_in(port, UART_MSR);
655
656 /*
657 * Now, initialize the UART
658 */
659 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
660
Alan Coxc271cf32009-11-30 13:16:25 +0000661 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400662 port->mctrl = TIOCM_OUT2;
663
Alan Coxadc8d742012-07-14 15:31:47 +0100664 sdio_uart_change_speed(port, &tty->termios, NULL);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400665
Alan Coxadc8d742012-07-14 15:31:47 +0100666 if (tty->termios.c_cflag & CBAUD)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400667 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
668
Alan Coxadc8d742012-07-14 15:31:47 +0100669 if (tty->termios.c_cflag & CRTSCTS)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400670 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
Alan Coxb5849b12009-11-05 13:28:06 +0000671 tty->hw_stopped = 1;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400672
Nicolas Pitre0395b482009-11-05 13:28:17 +0000673 clear_bit(TTY_IO_ERROR, &tty->flags);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400674
675 /* Kick the IRQ handler once while we're still holding the host lock */
676 sdio_uart_irq(port->func);
677
678 sdio_uart_release_func(port);
679 return 0;
680
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400681err2:
Alan Cox8b197a52010-02-08 10:08:39 +0000682 sdio_disable_func(port->func);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400683err1:
Alan Cox8b197a52010-02-08 10:08:39 +0000684 sdio_uart_release_func(port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400685 return ret;
686}
687
Alan Cox584abc32009-11-30 13:16:09 +0000688/**
689 * sdio_uart_shutdown - stop hardware
690 * @tport: tty port to shut down
691 *
692 * Deactivate a tty port. The port locking guarantees us this will be
693 * run only if a successful matching activate already ran. The two are
694 * protected from each other by the internal locking and will not run
695 * at the same time even during a hangup event.
696 */
697
698static void sdio_uart_shutdown(struct tty_port *tport)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400699{
Alan Cox584abc32009-11-30 13:16:09 +0000700 struct sdio_uart_port *port =
701 container_of(tport, struct sdio_uart_port, port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400702 int ret;
703
704 ret = sdio_uart_claim_func(port);
705 if (ret)
Alan Cox8b197a52010-02-08 10:08:39 +0000706 return;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400707
708 sdio_uart_stop_rx(port);
709
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300710 /* Disable interrupts from this port */
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400711 sdio_release_irq(port->func);
712 port->ier = 0;
713 sdio_out(port, UART_IER, 0);
714
715 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
716
717 /* Disable break condition and FIFOs. */
718 port->lcr &= ~UART_LCR_SBC;
719 sdio_out(port, UART_LCR, port->lcr);
720 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
721 UART_FCR_CLEAR_RCVR |
722 UART_FCR_CLEAR_XMIT);
723 sdio_out(port, UART_FCR, 0);
724
725 sdio_disable_func(port->func);
726
727 sdio_uart_release_func(port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400728}
729
Jiri Slabye70c6772012-11-15 09:49:52 +0100730static void sdio_uart_port_destroy(struct tty_port *tport)
731{
732 struct sdio_uart_port *port =
733 container_of(tport, struct sdio_uart_port, port);
734 kfifo_free(&port->xmit_fifo);
735 kfree(port);
736}
737
Alan Cox584abc32009-11-30 13:16:09 +0000738/**
739 * sdio_uart_install - install method
740 * @driver: the driver in use (sdio_uart in our case)
741 * @tty: the tty being bound
742 *
743 * Look up and bind the tty and the driver together. Initialize
744 * any needed private data (in our case the termios)
745 */
746
747static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
748{
749 int idx = tty->index;
750 struct sdio_uart_port *port = sdio_uart_port_get(idx);
Jiri Slaby81f58352012-01-30 21:14:30 +0100751 int ret = tty_standard_install(driver, tty);
Alan Cox584abc32009-11-30 13:16:09 +0000752
Jiri Slaby81f58352012-01-30 21:14:30 +0100753 if (ret == 0)
Alan Cox584abc32009-11-30 13:16:09 +0000754 /* This is the ref sdio_uart_port get provided */
755 tty->driver_data = port;
Jiri Slaby81f58352012-01-30 21:14:30 +0100756 else
Alan Cox584abc32009-11-30 13:16:09 +0000757 sdio_uart_port_put(port);
758 return ret;
Alan Cox584abc32009-11-30 13:16:09 +0000759}
760
761/**
762 * sdio_uart_cleanup - called on the last tty kref drop
763 * @tty: the tty being destroyed
764 *
765 * Called asynchronously when the last reference to the tty is dropped.
766 * We cannot destroy the tty->driver_data port kref until this point
767 */
768
769static void sdio_uart_cleanup(struct tty_struct *tty)
770{
771 struct sdio_uart_port *port = tty->driver_data;
772 tty->driver_data = NULL; /* Bug trap */
773 sdio_uart_port_put(port);
774}
775
776/*
777 * Open/close/hangup is now entirely boilerplate
778 */
779
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300780static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400781{
Alan Cox584abc32009-11-30 13:16:09 +0000782 struct sdio_uart_port *port = tty->driver_data;
783 return tty_port_open(&port->port, tty, filp);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400784}
785
786static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
787{
788 struct sdio_uart_port *port = tty->driver_data;
Alan Cox584abc32009-11-30 13:16:09 +0000789 tty_port_close(&port->port, tty, filp);
790}
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400791
Alan Cox584abc32009-11-30 13:16:09 +0000792static void sdio_uart_hangup(struct tty_struct *tty)
793{
794 struct sdio_uart_port *port = tty->driver_data;
795 tty_port_hangup(&port->port);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400796}
797
Alan Coxc271cf32009-11-30 13:16:25 +0000798static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400799 int count)
800{
801 struct sdio_uart_port *port = tty->driver_data;
Alan Cox8b197a52010-02-08 10:08:39 +0000802 int ret;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400803
804 if (!port->func)
805 return -ENODEV;
806
Alan Cox8b197a52010-02-08 10:08:39 +0000807 ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
Alan Coxc271cf32009-11-30 13:16:25 +0000808 if (!(port->ier & UART_IER_THRI)) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400809 int err = sdio_uart_claim_func(port);
810 if (!err) {
811 sdio_uart_start_tx(port);
812 sdio_uart_irq(port->func);
813 sdio_uart_release_func(port);
814 } else
815 ret = err;
816 }
817
818 return ret;
819}
820
821static int sdio_uart_write_room(struct tty_struct *tty)
822{
823 struct sdio_uart_port *port = tty->driver_data;
Alan Cox8b197a52010-02-08 10:08:39 +0000824 return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400825}
826
827static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
828{
829 struct sdio_uart_port *port = tty->driver_data;
Alan Cox8b197a52010-02-08 10:08:39 +0000830 return kfifo_len(&port->xmit_fifo);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400831}
832
833static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
834{
835 struct sdio_uart_port *port = tty->driver_data;
836
837 port->x_char = ch;
838 if (ch && !(port->ier & UART_IER_THRI)) {
839 if (sdio_uart_claim_func(port) != 0)
840 return;
841 sdio_uart_start_tx(port);
842 sdio_uart_irq(port->func);
843 sdio_uart_release_func(port);
844 }
845}
846
847static void sdio_uart_throttle(struct tty_struct *tty)
848{
849 struct sdio_uart_port *port = tty->driver_data;
850
Alan Coxadc8d742012-07-14 15:31:47 +0100851 if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS))
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400852 return;
853
854 if (sdio_uart_claim_func(port) != 0)
855 return;
856
857 if (I_IXOFF(tty)) {
858 port->x_char = STOP_CHAR(tty);
859 sdio_uart_start_tx(port);
860 }
861
Alan Coxadc8d742012-07-14 15:31:47 +0100862 if (tty->termios.c_cflag & CRTSCTS)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400863 sdio_uart_clear_mctrl(port, TIOCM_RTS);
864
865 sdio_uart_irq(port->func);
866 sdio_uart_release_func(port);
867}
868
869static void sdio_uart_unthrottle(struct tty_struct *tty)
870{
871 struct sdio_uart_port *port = tty->driver_data;
872
Alan Coxadc8d742012-07-14 15:31:47 +0100873 if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS))
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400874 return;
875
876 if (sdio_uart_claim_func(port) != 0)
877 return;
878
879 if (I_IXOFF(tty)) {
880 if (port->x_char) {
881 port->x_char = 0;
882 } else {
883 port->x_char = START_CHAR(tty);
884 sdio_uart_start_tx(port);
885 }
886 }
887
Alan Coxadc8d742012-07-14 15:31:47 +0100888 if (tty->termios.c_cflag & CRTSCTS)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400889 sdio_uart_set_mctrl(port, TIOCM_RTS);
890
891 sdio_uart_irq(port->func);
892 sdio_uart_release_func(port);
893}
894
Alan Coxc271cf32009-11-30 13:16:25 +0000895static void sdio_uart_set_termios(struct tty_struct *tty,
896 struct ktermios *old_termios)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400897{
898 struct sdio_uart_port *port = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +0100899 unsigned int cflag = tty->termios.c_cflag;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400900
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400901 if (sdio_uart_claim_func(port) != 0)
902 return;
903
Alan Coxadc8d742012-07-14 15:31:47 +0100904 sdio_uart_change_speed(port, &tty->termios, old_termios);
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400905
906 /* Handle transition to B0 status */
907 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
908 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
909
910 /* Handle transition away from B0 status */
911 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
912 unsigned int mask = TIOCM_DTR;
913 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
914 mask |= TIOCM_RTS;
915 sdio_uart_set_mctrl(port, mask);
916 }
917
918 /* Handle turning off CRTSCTS */
919 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
920 tty->hw_stopped = 0;
921 sdio_uart_start_tx(port);
922 }
923
924 /* Handle turning on CRTSCTS */
925 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
926 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
927 tty->hw_stopped = 1;
928 sdio_uart_stop_tx(port);
929 }
930 }
931
932 sdio_uart_release_func(port);
933}
934
David Howellsc43d8632008-07-10 12:28:48 +0100935static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400936{
937 struct sdio_uart_port *port = tty->driver_data;
David Howellsc43d8632008-07-10 12:28:48 +0100938 int result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400939
David Howellsc43d8632008-07-10 12:28:48 +0100940 result = sdio_uart_claim_func(port);
941 if (result != 0)
942 return result;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400943
944 if (break_state == -1)
945 port->lcr |= UART_LCR_SBC;
946 else
947 port->lcr &= ~UART_LCR_SBC;
948 sdio_out(port, UART_LCR, port->lcr);
949
950 sdio_uart_release_func(port);
David Howellsc43d8632008-07-10 12:28:48 +0100951 return 0;
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400952}
953
Alan Cox60b33c12011-02-14 16:26:14 +0000954static int sdio_uart_tiocmget(struct tty_struct *tty)
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400955{
956 struct sdio_uart_port *port = tty->driver_data;
957 int result;
958
959 result = sdio_uart_claim_func(port);
960 if (!result) {
961 result = port->mctrl | sdio_uart_get_mctrl(port);
962 sdio_uart_release_func(port);
963 }
964
965 return result;
966}
967
Alan Cox20b9d172011-02-14 16:26:50 +0000968static int sdio_uart_tiocmset(struct tty_struct *tty,
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400969 unsigned int set, unsigned int clear)
970{
971 struct sdio_uart_port *port = tty->driver_data;
972 int result;
973
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300974 result = sdio_uart_claim_func(port);
Alan Coxc271cf32009-11-30 13:16:25 +0000975 if (!result) {
Nicolas Pitre6e418a92007-06-30 02:04:21 -0400976 sdio_uart_update_mctrl(port, set, clear);
977 sdio_uart_release_func(port);
978 }
979
980 return result;
981}
982
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700983static int sdio_uart_proc_show(struct seq_file *m, void *v)
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400984{
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700985 int i;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400986
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700987 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400988 "", "", "");
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700989 for (i = 0; i < UART_NR; i++) {
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400990 struct sdio_uart_port *port = sdio_uart_port_get(i);
991 if (port) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700992 seq_printf(m, "%d: uart:SDIO", i);
Alan Coxc271cf32009-11-30 13:16:25 +0000993 if (capable(CAP_SYS_ADMIN)) {
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700994 seq_printf(m, " tx:%d rx:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300995 port->icount.tx, port->icount.rx);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400996 if (port->icount.frame)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -0700997 seq_printf(m, " fe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -0300998 port->icount.frame);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -0400999 if (port->icount.parity)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001000 seq_printf(m, " pe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001001 port->icount.parity);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001002 if (port->icount.brk)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001003 seq_printf(m, " brk:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001004 port->icount.brk);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001005 if (port->icount.overrun)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001006 seq_printf(m, " oe:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001007 port->icount.overrun);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001008 if (port->icount.cts)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001009 seq_printf(m, " cts:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001010 port->icount.cts);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001011 if (port->icount.dsr)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001012 seq_printf(m, " dsr:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001013 port->icount.dsr);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001014 if (port->icount.rng)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001015 seq_printf(m, " rng:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001016 port->icount.rng);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001017 if (port->icount.dcd)
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001018 seq_printf(m, " dcd:%d",
Thadeu Lima de Souza Cascardo1e04b7a2009-10-15 16:44:00 -03001019 port->icount.dcd);
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001020 }
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001021 sdio_uart_port_put(port);
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001022 seq_putc(m, '\n');
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001023 }
1024 }
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001025 return 0;
Nicolas Pitre5ed334a2007-07-04 23:40:34 -04001026}
1027
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001028static int sdio_uart_proc_open(struct inode *inode, struct file *file)
1029{
1030 return single_open(file, sdio_uart_proc_show, NULL);
1031}
1032
1033static const struct file_operations sdio_uart_proc_fops = {
1034 .owner = THIS_MODULE,
1035 .open = sdio_uart_proc_open,
1036 .read = seq_read,
1037 .llseek = seq_lseek,
1038 .release = single_release,
1039};
1040
Alan Cox584abc32009-11-30 13:16:09 +00001041static const struct tty_port_operations sdio_uart_port_ops = {
1042 .dtr_rts = uart_dtr_rts,
Alan Cox4b3b49b2009-11-30 13:16:36 +00001043 .carrier_raised = uart_carrier_raised,
Alan Cox584abc32009-11-30 13:16:09 +00001044 .shutdown = sdio_uart_shutdown,
1045 .activate = sdio_uart_activate,
Jiri Slabye70c6772012-11-15 09:49:52 +01001046 .destruct = sdio_uart_port_destroy,
Alan Cox584abc32009-11-30 13:16:09 +00001047};
1048
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001049static const struct tty_operations sdio_uart_ops = {
1050 .open = sdio_uart_open,
1051 .close = sdio_uart_close,
1052 .write = sdio_uart_write,
1053 .write_room = sdio_uart_write_room,
1054 .chars_in_buffer = sdio_uart_chars_in_buffer,
1055 .send_xchar = sdio_uart_send_xchar,
1056 .throttle = sdio_uart_throttle,
1057 .unthrottle = sdio_uart_unthrottle,
1058 .set_termios = sdio_uart_set_termios,
Alan Cox584abc32009-11-30 13:16:09 +00001059 .hangup = sdio_uart_hangup,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001060 .break_ctl = sdio_uart_break_ctl,
1061 .tiocmget = sdio_uart_tiocmget,
1062 .tiocmset = sdio_uart_tiocmset,
Alan Cox584abc32009-11-30 13:16:09 +00001063 .install = sdio_uart_install,
1064 .cleanup = sdio_uart_cleanup,
Alexey Dobriyan201a50b2009-03-31 15:19:20 -07001065 .proc_fops = &sdio_uart_proc_fops,
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001066};
1067
1068static struct tty_driver *sdio_uart_tty_driver;
1069
1070static int sdio_uart_probe(struct sdio_func *func,
1071 const struct sdio_device_id *id)
1072{
1073 struct sdio_uart_port *port;
1074 int ret;
1075
1076 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1077 if (!port)
1078 return -ENOMEM;
1079
1080 if (func->class == SDIO_CLASS_UART) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301081 pr_warning("%s: need info on UART class basic setup\n",
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001082 sdio_func_id(func));
1083 kfree(port);
1084 return -ENOSYS;
1085 } else if (func->class == SDIO_CLASS_GPS) {
1086 /*
1087 * We need tuple 0x91. It contains SUBTPL_SIOREG
1088 * and SUBTPL_RCVCAPS.
1089 */
1090 struct sdio_func_tuple *tpl;
1091 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1092 if (tpl->code != 0x91)
1093 continue;
1094 if (tpl->size < 10)
1095 continue;
1096 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1097 break;
1098 }
1099 if (!tpl) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301100 pr_warning(
Alan Coxc271cf32009-11-30 13:16:25 +00001101 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001102 sdio_func_id(func));
1103 kfree(port);
1104 return -EINVAL;
1105 }
Girish K Sa3c76eb2011-10-11 11:44:09 +05301106 pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001107 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1108 port->regs_offset = (tpl->data[4] << 0) |
1109 (tpl->data[5] << 8) |
1110 (tpl->data[6] << 16);
Girish K Sa3c76eb2011-10-11 11:44:09 +05301111 pr_debug("%s: regs offset = 0x%x\n",
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001112 sdio_func_id(func), port->regs_offset);
1113 port->uartclk = tpl->data[7] * 115200;
1114 if (port->uartclk == 0)
1115 port->uartclk = 115200;
Girish K Sa3c76eb2011-10-11 11:44:09 +05301116 pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001117 sdio_func_id(func), port->uartclk,
1118 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1119 } else {
1120 kfree(port);
1121 return -EINVAL;
1122 }
1123
1124 port->func = func;
1125 sdio_set_drvdata(func, port);
Alan Coxb5849b12009-11-05 13:28:06 +00001126 tty_port_init(&port->port);
Alan Cox584abc32009-11-30 13:16:09 +00001127 port->port.ops = &sdio_uart_port_ops;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001128
1129 ret = sdio_uart_add_port(port);
1130 if (ret) {
1131 kfree(port);
1132 } else {
1133 struct device *dev;
Jiri Slaby734cc172012-08-07 21:47:47 +02001134 dev = tty_port_register_device(&port->port,
1135 sdio_uart_tty_driver, port->index, &func->dev);
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001136 if (IS_ERR(dev)) {
1137 sdio_uart_port_remove(port);
1138 ret = PTR_ERR(dev);
1139 }
1140 }
1141
1142 return ret;
1143}
1144
1145static void sdio_uart_remove(struct sdio_func *func)
1146{
1147 struct sdio_uart_port *port = sdio_get_drvdata(func);
1148
1149 tty_unregister_device(sdio_uart_tty_driver, port->index);
1150 sdio_uart_port_remove(port);
1151}
1152
1153static const struct sdio_device_id sdio_uart_ids[] = {
1154 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1155 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1156 { /* end: all zeroes */ },
1157};
1158
1159MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1160
1161static struct sdio_driver sdio_uart_driver = {
1162 .probe = sdio_uart_probe,
1163 .remove = sdio_uart_remove,
1164 .name = "sdio_uart",
1165 .id_table = sdio_uart_ids,
1166};
1167
1168static int __init sdio_uart_init(void)
1169{
1170 int ret;
1171 struct tty_driver *tty_drv;
1172
1173 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1174 if (!tty_drv)
1175 return -ENOMEM;
1176
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001177 tty_drv->driver_name = "sdio_uart";
1178 tty_drv->name = "ttySDIO";
1179 tty_drv->major = 0; /* dynamically allocated */
1180 tty_drv->minor_start = 0;
1181 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1182 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1183 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1184 tty_drv->init_termios = tty_std_termios;
1185 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
Nicolas Pitre2ba30ee2007-08-15 13:27:29 -04001186 tty_drv->init_termios.c_ispeed = 4800;
1187 tty_drv->init_termios.c_ospeed = 4800;
Nicolas Pitre6e418a92007-06-30 02:04:21 -04001188 tty_set_operations(tty_drv, &sdio_uart_ops);
1189
1190 ret = tty_register_driver(tty_drv);
1191 if (ret)
1192 goto err1;
1193
1194 ret = sdio_register_driver(&sdio_uart_driver);
1195 if (ret)
1196 goto err2;
1197
1198 return 0;
1199
1200err2:
1201 tty_unregister_driver(tty_drv);
1202err1:
1203 put_tty_driver(tty_drv);
1204 return ret;
1205}
1206
1207static void __exit sdio_uart_exit(void)
1208{
1209 sdio_unregister_driver(&sdio_uart_driver);
1210 tty_unregister_driver(sdio_uart_tty_driver);
1211 put_tty_driver(sdio_uart_tty_driver);
1212}
1213
1214module_init(sdio_uart_init);
1215module_exit(sdio_uart_exit);
1216
1217MODULE_AUTHOR("Nicolas Pitre");
1218MODULE_LICENSE("GPL");