Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver |
| 3 | * |
| 4 | * Based on drivers/serial/8250.c and drivers/serial/serial_core.c |
| 5 | * by Russell King. |
| 6 | * |
| 7 | * Author: Nicolas Pitre |
| 8 | * Created: June 15, 2007 |
| 9 | * Copyright: MontaVista Software, Inc. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or (at |
| 14 | * your option) any later version. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Note: Although this driver assumes a 16550A-like UART implementation, |
| 19 | * it is not possible to leverage the common 8250/16550 driver, nor the |
| 20 | * core UART infrastructure, as they assumes direct access to the hardware |
| 21 | * registers, often under a spinlock. This is not possible in the SDIO |
| 22 | * context as SDIO access functions must be able to sleep. |
| 23 | * |
| 24 | * Because we need to lock the SDIO host to ensure an exclusive access to |
| 25 | * the card, we simply rely on that lock to also prevent and serialize |
| 26 | * concurrent access to the same port. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/kernel.h> |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 32 | #include <linux/sched.h> |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 34 | #include <linux/seq_file.h> |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 35 | #include <linux/serial_reg.h> |
| 36 | #include <linux/circ_buf.h> |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 37 | #include <linux/tty.h> |
| 38 | #include <linux/tty_flip.h> |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 39 | #include <linux/kfifo.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 41 | |
| 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 Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 51 | #define FIFO_SIZE PAGE_SIZE |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 52 | #define WAKEUP_CHARS 256 |
| 53 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 54 | struct 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 | |
| 67 | struct sdio_uart_port { |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 68 | struct tty_port port; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 69 | unsigned int index; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 70 | struct sdio_func *func; |
| 71 | struct mutex func_lock; |
Nicolas Pitre | 15b82b4 | 2007-08-20 17:17:37 -0400 | [diff] [blame] | 72 | struct task_struct *in_sdio_uart_irq; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 73 | unsigned int regs_offset; |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 74 | struct kfifo xmit_fifo; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 75 | spinlock_t write_lock; |
| 76 | struct uart_icount icount; |
| 77 | unsigned int uartclk; |
| 78 | unsigned int mctrl; |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 79 | unsigned int rx_mctrl; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 80 | 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 | |
| 87 | static struct sdio_uart_port *sdio_uart_table[UART_NR]; |
| 88 | static DEFINE_SPINLOCK(sdio_uart_table_lock); |
| 89 | |
| 90 | static int sdio_uart_add_port(struct sdio_uart_port *port) |
| 91 | { |
| 92 | int index, ret = -EBUSY; |
| 93 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 94 | mutex_init(&port->func_lock); |
| 95 | spin_lock_init(&port->write_lock); |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 96 | if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL)) |
| 97 | return -ENOMEM; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 98 | |
| 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 | |
| 113 | static 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 Slaby | e70c677 | 2012-11-15 09:49:52 +0100 | [diff] [blame] | 123 | tty_port_get(&port->port); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 124 | spin_unlock(&sdio_uart_table_lock); |
| 125 | |
| 126 | return port; |
| 127 | } |
| 128 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 129 | static void sdio_uart_port_put(struct sdio_uart_port *port) |
| 130 | { |
Jiri Slaby | e70c677 | 2012-11-15 09:49:52 +0100 | [diff] [blame] | 131 | tty_port_put(&port->port); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void sdio_uart_port_remove(struct sdio_uart_port *port) |
| 135 | { |
| 136 | struct sdio_func *func; |
| 137 | |
| 138 | BUG_ON(sdio_uart_table[port->index] != port); |
| 139 | |
| 140 | spin_lock(&sdio_uart_table_lock); |
| 141 | sdio_uart_table[port->index] = NULL; |
| 142 | spin_unlock(&sdio_uart_table_lock); |
| 143 | |
| 144 | /* |
| 145 | * We're killing a port that potentially still is in use by |
| 146 | * the tty layer. Be careful to prevent any further access |
| 147 | * to the SDIO function and arrange for the tty layer to |
| 148 | * give up on that port ASAP. |
| 149 | * Beware: the lock ordering is critical. |
| 150 | */ |
Alan Cox | 0a68f64 | 2009-11-05 13:28:38 +0000 | [diff] [blame] | 151 | mutex_lock(&port->port.mutex); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 152 | mutex_lock(&port->func_lock); |
| 153 | func = port->func; |
| 154 | sdio_claim_host(func); |
| 155 | port->func = NULL; |
| 156 | mutex_unlock(&port->func_lock); |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 157 | /* tty_hangup is async so is this safe as is ?? */ |
Jiri Slaby | aa27a09 | 2013-03-07 13:12:30 +0100 | [diff] [blame] | 158 | tty_port_tty_hangup(&port->port, false); |
Alan Cox | 0a68f64 | 2009-11-05 13:28:38 +0000 | [diff] [blame] | 159 | mutex_unlock(&port->port.mutex); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 160 | sdio_release_irq(func); |
| 161 | sdio_disable_func(func); |
| 162 | sdio_release_host(func); |
| 163 | |
| 164 | sdio_uart_port_put(port); |
| 165 | } |
| 166 | |
| 167 | static int sdio_uart_claim_func(struct sdio_uart_port *port) |
| 168 | { |
| 169 | mutex_lock(&port->func_lock); |
| 170 | if (unlikely(!port->func)) { |
| 171 | mutex_unlock(&port->func_lock); |
| 172 | return -ENODEV; |
| 173 | } |
Nicolas Pitre | 15b82b4 | 2007-08-20 17:17:37 -0400 | [diff] [blame] | 174 | if (likely(port->in_sdio_uart_irq != current)) |
| 175 | sdio_claim_host(port->func); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 176 | mutex_unlock(&port->func_lock); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static inline void sdio_uart_release_func(struct sdio_uart_port *port) |
| 181 | { |
Nicolas Pitre | 15b82b4 | 2007-08-20 17:17:37 -0400 | [diff] [blame] | 182 | if (likely(port->in_sdio_uart_irq != current)) |
| 183 | sdio_release_host(port->func); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset) |
| 187 | { |
| 188 | unsigned char c; |
| 189 | c = sdio_readb(port->func, port->regs_offset + offset, NULL); |
| 190 | return c; |
| 191 | } |
| 192 | |
| 193 | static inline void sdio_out(struct sdio_uart_port *port, int offset, int value) |
| 194 | { |
| 195 | sdio_writeb(port->func, value, port->regs_offset + offset, NULL); |
| 196 | } |
| 197 | |
| 198 | static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port) |
| 199 | { |
| 200 | unsigned char status; |
| 201 | unsigned int ret; |
| 202 | |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 203 | /* FIXME: What stops this losing the delta bits and breaking |
| 204 | sdio_uart_check_modem_status ? */ |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 205 | status = sdio_in(port, UART_MSR); |
| 206 | |
| 207 | ret = 0; |
| 208 | if (status & UART_MSR_DCD) |
| 209 | ret |= TIOCM_CAR; |
| 210 | if (status & UART_MSR_RI) |
| 211 | ret |= TIOCM_RNG; |
| 212 | if (status & UART_MSR_DSR) |
| 213 | ret |= TIOCM_DSR; |
| 214 | if (status & UART_MSR_CTS) |
| 215 | ret |= TIOCM_CTS; |
| 216 | return ret; |
| 217 | } |
| 218 | |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 219 | static void sdio_uart_write_mctrl(struct sdio_uart_port *port, |
| 220 | unsigned int mctrl) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 221 | { |
| 222 | unsigned char mcr = 0; |
| 223 | |
| 224 | if (mctrl & TIOCM_RTS) |
| 225 | mcr |= UART_MCR_RTS; |
| 226 | if (mctrl & TIOCM_DTR) |
| 227 | mcr |= UART_MCR_DTR; |
| 228 | if (mctrl & TIOCM_OUT1) |
| 229 | mcr |= UART_MCR_OUT1; |
| 230 | if (mctrl & TIOCM_OUT2) |
| 231 | mcr |= UART_MCR_OUT2; |
| 232 | if (mctrl & TIOCM_LOOP) |
| 233 | mcr |= UART_MCR_LOOP; |
| 234 | |
| 235 | sdio_out(port, UART_MCR, mcr); |
| 236 | } |
| 237 | |
| 238 | static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port, |
| 239 | unsigned int set, unsigned int clear) |
| 240 | { |
| 241 | unsigned int old; |
| 242 | |
| 243 | old = port->mctrl; |
| 244 | port->mctrl = (old & ~clear) | set; |
| 245 | if (old != port->mctrl) |
| 246 | sdio_uart_write_mctrl(port, port->mctrl); |
| 247 | } |
| 248 | |
| 249 | #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0) |
| 250 | #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x) |
| 251 | |
| 252 | static void sdio_uart_change_speed(struct sdio_uart_port *port, |
| 253 | struct ktermios *termios, |
| 254 | struct ktermios *old) |
| 255 | { |
| 256 | unsigned char cval, fcr = 0; |
| 257 | unsigned int baud, quot; |
| 258 | |
| 259 | switch (termios->c_cflag & CSIZE) { |
| 260 | case CS5: |
| 261 | cval = UART_LCR_WLEN5; |
| 262 | break; |
| 263 | case CS6: |
| 264 | cval = UART_LCR_WLEN6; |
| 265 | break; |
| 266 | case CS7: |
| 267 | cval = UART_LCR_WLEN7; |
| 268 | break; |
| 269 | default: |
| 270 | case CS8: |
| 271 | cval = UART_LCR_WLEN8; |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | if (termios->c_cflag & CSTOPB) |
| 276 | cval |= UART_LCR_STOP; |
| 277 | if (termios->c_cflag & PARENB) |
| 278 | cval |= UART_LCR_PARITY; |
| 279 | if (!(termios->c_cflag & PARODD)) |
| 280 | cval |= UART_LCR_EPAR; |
| 281 | |
| 282 | for (;;) { |
| 283 | baud = tty_termios_baud_rate(termios); |
| 284 | if (baud == 0) |
| 285 | baud = 9600; /* Special case: B0 rate. */ |
| 286 | if (baud <= port->uartclk) |
| 287 | break; |
| 288 | /* |
| 289 | * Oops, the quotient was zero. Try again with the old |
| 290 | * baud rate if possible, otherwise default to 9600. |
| 291 | */ |
| 292 | termios->c_cflag &= ~CBAUD; |
| 293 | if (old) { |
| 294 | termios->c_cflag |= old->c_cflag & CBAUD; |
| 295 | old = NULL; |
| 296 | } else |
| 297 | termios->c_cflag |= B9600; |
| 298 | } |
| 299 | quot = (2 * port->uartclk + baud) / (2 * baud); |
| 300 | |
| 301 | if (baud < 2400) |
| 302 | fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1; |
| 303 | else |
| 304 | fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10; |
| 305 | |
| 306 | port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR; |
| 307 | if (termios->c_iflag & INPCK) |
| 308 | port->read_status_mask |= UART_LSR_FE | UART_LSR_PE; |
| 309 | if (termios->c_iflag & (BRKINT | PARMRK)) |
| 310 | port->read_status_mask |= UART_LSR_BI; |
| 311 | |
| 312 | /* |
| 313 | * Characters to ignore |
| 314 | */ |
| 315 | port->ignore_status_mask = 0; |
| 316 | if (termios->c_iflag & IGNPAR) |
| 317 | port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; |
| 318 | if (termios->c_iflag & IGNBRK) { |
| 319 | port->ignore_status_mask |= UART_LSR_BI; |
| 320 | /* |
| 321 | * If we're ignoring parity and break indicators, |
| 322 | * ignore overruns too (for real raw support). |
| 323 | */ |
| 324 | if (termios->c_iflag & IGNPAR) |
| 325 | port->ignore_status_mask |= UART_LSR_OE; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * ignore all characters if CREAD is not set |
| 330 | */ |
| 331 | if ((termios->c_cflag & CREAD) == 0) |
| 332 | port->ignore_status_mask |= UART_LSR_DR; |
| 333 | |
| 334 | /* |
| 335 | * CTS flow control flag and modem status interrupts |
| 336 | */ |
| 337 | port->ier &= ~UART_IER_MSI; |
| 338 | if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL)) |
| 339 | port->ier |= UART_IER_MSI; |
| 340 | |
| 341 | port->lcr = cval; |
| 342 | |
| 343 | sdio_out(port, UART_IER, port->ier); |
| 344 | sdio_out(port, UART_LCR, cval | UART_LCR_DLAB); |
| 345 | sdio_out(port, UART_DLL, quot & 0xff); |
| 346 | sdio_out(port, UART_DLM, quot >> 8); |
| 347 | sdio_out(port, UART_LCR, cval); |
| 348 | sdio_out(port, UART_FCR, fcr); |
| 349 | |
| 350 | sdio_uart_write_mctrl(port, port->mctrl); |
| 351 | } |
| 352 | |
| 353 | static void sdio_uart_start_tx(struct sdio_uart_port *port) |
| 354 | { |
| 355 | if (!(port->ier & UART_IER_THRI)) { |
| 356 | port->ier |= UART_IER_THRI; |
| 357 | sdio_out(port, UART_IER, port->ier); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void sdio_uart_stop_tx(struct sdio_uart_port *port) |
| 362 | { |
| 363 | if (port->ier & UART_IER_THRI) { |
| 364 | port->ier &= ~UART_IER_THRI; |
| 365 | sdio_out(port, UART_IER, port->ier); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static void sdio_uart_stop_rx(struct sdio_uart_port *port) |
| 370 | { |
| 371 | port->ier &= ~UART_IER_RLSI; |
| 372 | port->read_status_mask &= ~UART_LSR_DR; |
| 373 | sdio_out(port, UART_IER, port->ier); |
| 374 | } |
| 375 | |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 376 | static void sdio_uart_receive_chars(struct sdio_uart_port *port, |
| 377 | unsigned int *status) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 378 | { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 379 | unsigned int ch, flag; |
| 380 | int max_count = 256; |
| 381 | |
| 382 | do { |
| 383 | ch = sdio_in(port, UART_RX); |
| 384 | flag = TTY_NORMAL; |
| 385 | port->icount.rx++; |
| 386 | |
| 387 | if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE | |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 388 | UART_LSR_FE | UART_LSR_OE))) { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 389 | /* |
| 390 | * For statistics only |
| 391 | */ |
| 392 | if (*status & UART_LSR_BI) { |
| 393 | *status &= ~(UART_LSR_FE | UART_LSR_PE); |
| 394 | port->icount.brk++; |
| 395 | } else if (*status & UART_LSR_PE) |
| 396 | port->icount.parity++; |
| 397 | else if (*status & UART_LSR_FE) |
| 398 | port->icount.frame++; |
| 399 | if (*status & UART_LSR_OE) |
| 400 | port->icount.overrun++; |
| 401 | |
| 402 | /* |
| 403 | * Mask off conditions which should be ignored. |
| 404 | */ |
| 405 | *status &= port->read_status_mask; |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 406 | if (*status & UART_LSR_BI) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 407 | flag = TTY_BREAK; |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 408 | else if (*status & UART_LSR_PE) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 409 | flag = TTY_PARITY; |
| 410 | else if (*status & UART_LSR_FE) |
| 411 | flag = TTY_FRAME; |
| 412 | } |
| 413 | |
| 414 | if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0) |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 415 | tty_insert_flip_char(&port->port, ch, flag); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * Overrun is special. Since it's reported immediately, |
| 419 | * it doesn't affect the current character. |
| 420 | */ |
| 421 | if (*status & ~port->ignore_status_mask & UART_LSR_OE) |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 422 | tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 423 | |
| 424 | *status = sdio_in(port, UART_LSR); |
| 425 | } while ((*status & UART_LSR_DR) && (max_count-- > 0)); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 426 | |
| 427 | tty_flip_buffer_push(&port->port); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static void sdio_uart_transmit_chars(struct sdio_uart_port *port) |
| 431 | { |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 432 | struct kfifo *xmit = &port->xmit_fifo; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 433 | int count; |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 434 | struct tty_struct *tty; |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 435 | u8 iobuf[16]; |
| 436 | int len; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 437 | |
| 438 | if (port->x_char) { |
| 439 | sdio_out(port, UART_TX, port->x_char); |
| 440 | port->icount.tx++; |
| 441 | port->x_char = 0; |
| 442 | return; |
| 443 | } |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 444 | |
| 445 | tty = tty_port_tty_get(&port->port); |
| 446 | |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 447 | if (tty == NULL || !kfifo_len(xmit) || |
Alan Cox | c271cf3 | 2009-11-30 13:16:25 +0000 | [diff] [blame] | 448 | tty->stopped || tty->hw_stopped) { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 449 | sdio_uart_stop_tx(port); |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 450 | tty_kref_put(tty); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 451 | return; |
| 452 | } |
| 453 | |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 454 | len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock); |
| 455 | for (count = 0; count < len; count++) { |
| 456 | sdio_out(port, UART_TX, iobuf[count]); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 457 | port->icount.tx++; |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 458 | } |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 459 | |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 460 | len = kfifo_len(xmit); |
| 461 | if (len < WAKEUP_CHARS) { |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 462 | tty_wakeup(tty); |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 463 | if (len == 0) |
| 464 | sdio_uart_stop_tx(port); |
| 465 | } |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 466 | tty_kref_put(tty); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void sdio_uart_check_modem_status(struct sdio_uart_port *port) |
| 470 | { |
| 471 | int status; |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 472 | struct tty_struct *tty; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 473 | |
| 474 | status = sdio_in(port, UART_MSR); |
| 475 | |
| 476 | if ((status & UART_MSR_ANY_DELTA) == 0) |
| 477 | return; |
| 478 | |
| 479 | if (status & UART_MSR_TERI) |
| 480 | port->icount.rng++; |
| 481 | if (status & UART_MSR_DDSR) |
| 482 | port->icount.dsr++; |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 483 | if (status & UART_MSR_DDCD) { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 484 | port->icount.dcd++; |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 485 | /* DCD raise - wake for open */ |
| 486 | if (status & UART_MSR_DCD) |
| 487 | wake_up_interruptible(&port->port.open_wait); |
| 488 | else { |
| 489 | /* DCD drop - hang up if tty attached */ |
Jiri Slaby | aa27a09 | 2013-03-07 13:12:30 +0100 | [diff] [blame] | 490 | tty_port_tty_hangup(&port->port, false); |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 493 | if (status & UART_MSR_DCTS) { |
| 494 | port->icount.cts++; |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 495 | tty = tty_port_tty_get(&port->port); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 496 | if (tty && (tty->termios.c_cflag & CRTSCTS)) { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 497 | int cts = (status & UART_MSR_CTS); |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 498 | if (tty->hw_stopped) { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 499 | if (cts) { |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 500 | tty->hw_stopped = 0; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 501 | sdio_uart_start_tx(port); |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 502 | tty_wakeup(tty); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 503 | } |
| 504 | } else { |
| 505 | if (!cts) { |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 506 | tty->hw_stopped = 1; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 507 | sdio_uart_stop_tx(port); |
| 508 | } |
| 509 | } |
| 510 | } |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 511 | tty_kref_put(tty); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * This handles the interrupt from one port. |
| 517 | */ |
| 518 | static void sdio_uart_irq(struct sdio_func *func) |
| 519 | { |
| 520 | struct sdio_uart_port *port = sdio_get_drvdata(func); |
| 521 | unsigned int iir, lsr; |
| 522 | |
Nicolas Pitre | 15b82b4 | 2007-08-20 17:17:37 -0400 | [diff] [blame] | 523 | /* |
| 524 | * In a few places sdio_uart_irq() is called directly instead of |
| 525 | * waiting for the actual interrupt to be raised and the SDIO IRQ |
| 526 | * thread scheduled in order to reduce latency. However, some |
| 527 | * interaction with the tty core may end up calling us back |
| 528 | * (serial echo, flow control, etc.) through those same places |
| 529 | * causing undesirable effects. Let's stop the recursion here. |
| 530 | */ |
| 531 | if (unlikely(port->in_sdio_uart_irq == current)) |
| 532 | return; |
| 533 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 534 | iir = sdio_in(port, UART_IIR); |
| 535 | if (iir & UART_IIR_NO_INT) |
| 536 | return; |
Nicolas Pitre | 15b82b4 | 2007-08-20 17:17:37 -0400 | [diff] [blame] | 537 | |
| 538 | port->in_sdio_uart_irq = current; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 539 | lsr = sdio_in(port, UART_LSR); |
| 540 | if (lsr & UART_LSR_DR) |
| 541 | sdio_uart_receive_chars(port, &lsr); |
| 542 | sdio_uart_check_modem_status(port); |
| 543 | if (lsr & UART_LSR_THRE) |
| 544 | sdio_uart_transmit_chars(port); |
Nicolas Pitre | 15b82b4 | 2007-08-20 17:17:37 -0400 | [diff] [blame] | 545 | port->in_sdio_uart_irq = NULL; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 548 | static int uart_carrier_raised(struct tty_port *tport) |
| 549 | { |
| 550 | struct sdio_uart_port *port = |
| 551 | container_of(tport, struct sdio_uart_port, port); |
| 552 | unsigned int ret = sdio_uart_claim_func(port); |
Adam Buchbinder | c9404c9 | 2009-12-18 15:40:42 -0500 | [diff] [blame] | 553 | if (ret) /* Missing hardware shouldn't block for carrier */ |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 554 | return 1; |
| 555 | ret = sdio_uart_get_mctrl(port); |
| 556 | sdio_uart_release_func(port); |
| 557 | if (ret & TIOCM_CAR) |
| 558 | return 1; |
| 559 | return 0; |
| 560 | } |
| 561 | |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 562 | /** |
| 563 | * uart_dtr_rts - port helper to set uart signals |
| 564 | * @tport: tty port to be updated |
| 565 | * @onoff: set to turn on DTR/RTS |
| 566 | * |
| 567 | * Called by the tty port helpers when the modem signals need to be |
| 568 | * adjusted during an open, close and hangup. |
| 569 | */ |
Alan Cox | 530646f | 2009-11-05 13:28:29 +0000 | [diff] [blame] | 570 | |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 571 | static void uart_dtr_rts(struct tty_port *tport, int onoff) |
| 572 | { |
| 573 | struct sdio_uart_port *port = |
| 574 | container_of(tport, struct sdio_uart_port, port); |
Alan Cox | 1f100b3 | 2009-11-30 13:16:30 +0000 | [diff] [blame] | 575 | int ret = sdio_uart_claim_func(port); |
| 576 | if (ret) |
| 577 | return; |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 578 | if (onoff == 0) |
| 579 | sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS); |
| 580 | else |
| 581 | sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS); |
Alan Cox | 1f100b3 | 2009-11-30 13:16:30 +0000 | [diff] [blame] | 582 | sdio_uart_release_func(port); |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /** |
| 586 | * sdio_uart_activate - start up hardware |
| 587 | * @tport: tty port to activate |
| 588 | * @tty: tty bound to this port |
| 589 | * |
| 590 | * Activate a tty port. The port locking guarantees us this will be |
| 591 | * run exactly once per set of opens, and if successful will see the |
| 592 | * shutdown method run exactly once to match. Start up and shutdown are |
| 593 | * protected from each other by the internal locking and will not run |
| 594 | * at the same time even during a hangup event. |
| 595 | * |
| 596 | * If we successfully start up the port we take an extra kref as we |
| 597 | * will keep it around until shutdown when the kref is dropped. |
| 598 | */ |
| 599 | |
| 600 | static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty) |
| 601 | { |
| 602 | struct sdio_uart_port *port = |
| 603 | container_of(tport, struct sdio_uart_port, port); |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 604 | int ret; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Set the TTY IO error marker - we will only clear this |
| 608 | * once we have successfully opened the port. |
| 609 | */ |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 610 | set_bit(TTY_IO_ERROR, &tty->flags); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 611 | |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 612 | kfifo_reset(&port->xmit_fifo); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 613 | |
| 614 | ret = sdio_uart_claim_func(port); |
| 615 | if (ret) |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 616 | return ret; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 617 | ret = sdio_enable_func(port->func); |
| 618 | if (ret) |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 619 | goto err1; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 620 | ret = sdio_claim_irq(port->func, sdio_uart_irq); |
| 621 | if (ret) |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 622 | goto err2; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * Clear the FIFO buffers and disable them. |
| 626 | * (they will be reenabled in sdio_change_speed()) |
| 627 | */ |
| 628 | sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO); |
| 629 | sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO | |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 630 | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 631 | sdio_out(port, UART_FCR, 0); |
| 632 | |
| 633 | /* |
| 634 | * Clear the interrupt registers. |
| 635 | */ |
| 636 | (void) sdio_in(port, UART_LSR); |
| 637 | (void) sdio_in(port, UART_RX); |
| 638 | (void) sdio_in(port, UART_IIR); |
| 639 | (void) sdio_in(port, UART_MSR); |
| 640 | |
| 641 | /* |
| 642 | * Now, initialize the UART |
| 643 | */ |
| 644 | sdio_out(port, UART_LCR, UART_LCR_WLEN8); |
| 645 | |
Alan Cox | c271cf3 | 2009-11-30 13:16:25 +0000 | [diff] [blame] | 646 | port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 647 | port->mctrl = TIOCM_OUT2; |
| 648 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 649 | sdio_uart_change_speed(port, &tty->termios, NULL); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 650 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 651 | if (tty->termios.c_cflag & CBAUD) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 652 | sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR); |
| 653 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 654 | if (tty->termios.c_cflag & CRTSCTS) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 655 | if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 656 | tty->hw_stopped = 1; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 657 | |
Nicolas Pitre | 0395b48 | 2009-11-05 13:28:17 +0000 | [diff] [blame] | 658 | clear_bit(TTY_IO_ERROR, &tty->flags); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 659 | |
| 660 | /* Kick the IRQ handler once while we're still holding the host lock */ |
| 661 | sdio_uart_irq(port->func); |
| 662 | |
| 663 | sdio_uart_release_func(port); |
| 664 | return 0; |
| 665 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 666 | err2: |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 667 | sdio_disable_func(port->func); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 668 | err1: |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 669 | sdio_uart_release_func(port); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 670 | return ret; |
| 671 | } |
| 672 | |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 673 | /** |
| 674 | * sdio_uart_shutdown - stop hardware |
| 675 | * @tport: tty port to shut down |
| 676 | * |
| 677 | * Deactivate a tty port. The port locking guarantees us this will be |
| 678 | * run only if a successful matching activate already ran. The two are |
| 679 | * protected from each other by the internal locking and will not run |
| 680 | * at the same time even during a hangup event. |
| 681 | */ |
| 682 | |
| 683 | static void sdio_uart_shutdown(struct tty_port *tport) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 684 | { |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 685 | struct sdio_uart_port *port = |
| 686 | container_of(tport, struct sdio_uart_port, port); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 687 | int ret; |
| 688 | |
| 689 | ret = sdio_uart_claim_func(port); |
| 690 | if (ret) |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 691 | return; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 692 | |
| 693 | sdio_uart_stop_rx(port); |
| 694 | |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 695 | /* Disable interrupts from this port */ |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 696 | sdio_release_irq(port->func); |
| 697 | port->ier = 0; |
| 698 | sdio_out(port, UART_IER, 0); |
| 699 | |
| 700 | sdio_uart_clear_mctrl(port, TIOCM_OUT2); |
| 701 | |
| 702 | /* Disable break condition and FIFOs. */ |
| 703 | port->lcr &= ~UART_LCR_SBC; |
| 704 | sdio_out(port, UART_LCR, port->lcr); |
| 705 | sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO | |
| 706 | UART_FCR_CLEAR_RCVR | |
| 707 | UART_FCR_CLEAR_XMIT); |
| 708 | sdio_out(port, UART_FCR, 0); |
| 709 | |
| 710 | sdio_disable_func(port->func); |
| 711 | |
| 712 | sdio_uart_release_func(port); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Jiri Slaby | e70c677 | 2012-11-15 09:49:52 +0100 | [diff] [blame] | 715 | static void sdio_uart_port_destroy(struct tty_port *tport) |
| 716 | { |
| 717 | struct sdio_uart_port *port = |
| 718 | container_of(tport, struct sdio_uart_port, port); |
| 719 | kfifo_free(&port->xmit_fifo); |
| 720 | kfree(port); |
| 721 | } |
| 722 | |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 723 | /** |
| 724 | * sdio_uart_install - install method |
| 725 | * @driver: the driver in use (sdio_uart in our case) |
| 726 | * @tty: the tty being bound |
| 727 | * |
| 728 | * Look up and bind the tty and the driver together. Initialize |
| 729 | * any needed private data (in our case the termios) |
| 730 | */ |
| 731 | |
| 732 | static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty) |
| 733 | { |
| 734 | int idx = tty->index; |
| 735 | struct sdio_uart_port *port = sdio_uart_port_get(idx); |
Jiri Slaby | 81f5835 | 2012-01-30 21:14:30 +0100 | [diff] [blame] | 736 | int ret = tty_standard_install(driver, tty); |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 737 | |
Jiri Slaby | 81f5835 | 2012-01-30 21:14:30 +0100 | [diff] [blame] | 738 | if (ret == 0) |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 739 | /* This is the ref sdio_uart_port get provided */ |
| 740 | tty->driver_data = port; |
Jiri Slaby | 81f5835 | 2012-01-30 21:14:30 +0100 | [diff] [blame] | 741 | else |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 742 | sdio_uart_port_put(port); |
| 743 | return ret; |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /** |
| 747 | * sdio_uart_cleanup - called on the last tty kref drop |
| 748 | * @tty: the tty being destroyed |
| 749 | * |
| 750 | * Called asynchronously when the last reference to the tty is dropped. |
| 751 | * We cannot destroy the tty->driver_data port kref until this point |
| 752 | */ |
| 753 | |
| 754 | static void sdio_uart_cleanup(struct tty_struct *tty) |
| 755 | { |
| 756 | struct sdio_uart_port *port = tty->driver_data; |
| 757 | tty->driver_data = NULL; /* Bug trap */ |
| 758 | sdio_uart_port_put(port); |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Open/close/hangup is now entirely boilerplate |
| 763 | */ |
| 764 | |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 765 | static int sdio_uart_open(struct tty_struct *tty, struct file *filp) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 766 | { |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 767 | struct sdio_uart_port *port = tty->driver_data; |
| 768 | return tty_port_open(&port->port, tty, filp); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | static void sdio_uart_close(struct tty_struct *tty, struct file * filp) |
| 772 | { |
| 773 | struct sdio_uart_port *port = tty->driver_data; |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 774 | tty_port_close(&port->port, tty, filp); |
| 775 | } |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 776 | |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 777 | static void sdio_uart_hangup(struct tty_struct *tty) |
| 778 | { |
| 779 | struct sdio_uart_port *port = tty->driver_data; |
| 780 | tty_port_hangup(&port->port); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 781 | } |
| 782 | |
Alan Cox | c271cf3 | 2009-11-30 13:16:25 +0000 | [diff] [blame] | 783 | static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf, |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 784 | int count) |
| 785 | { |
| 786 | struct sdio_uart_port *port = tty->driver_data; |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 787 | int ret; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 788 | |
| 789 | if (!port->func) |
| 790 | return -ENODEV; |
| 791 | |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 792 | ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock); |
Alan Cox | c271cf3 | 2009-11-30 13:16:25 +0000 | [diff] [blame] | 793 | if (!(port->ier & UART_IER_THRI)) { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 794 | int err = sdio_uart_claim_func(port); |
| 795 | if (!err) { |
| 796 | sdio_uart_start_tx(port); |
| 797 | sdio_uart_irq(port->func); |
| 798 | sdio_uart_release_func(port); |
| 799 | } else |
| 800 | ret = err; |
| 801 | } |
| 802 | |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | static int sdio_uart_write_room(struct tty_struct *tty) |
| 807 | { |
| 808 | struct sdio_uart_port *port = tty->driver_data; |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 809 | return FIFO_SIZE - kfifo_len(&port->xmit_fifo); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static int sdio_uart_chars_in_buffer(struct tty_struct *tty) |
| 813 | { |
| 814 | struct sdio_uart_port *port = tty->driver_data; |
Alan Cox | 8b197a5 | 2010-02-08 10:08:39 +0000 | [diff] [blame] | 815 | return kfifo_len(&port->xmit_fifo); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static void sdio_uart_send_xchar(struct tty_struct *tty, char ch) |
| 819 | { |
| 820 | struct sdio_uart_port *port = tty->driver_data; |
| 821 | |
| 822 | port->x_char = ch; |
| 823 | if (ch && !(port->ier & UART_IER_THRI)) { |
| 824 | if (sdio_uart_claim_func(port) != 0) |
| 825 | return; |
| 826 | sdio_uart_start_tx(port); |
| 827 | sdio_uart_irq(port->func); |
| 828 | sdio_uart_release_func(port); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | static void sdio_uart_throttle(struct tty_struct *tty) |
| 833 | { |
| 834 | struct sdio_uart_port *port = tty->driver_data; |
| 835 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 836 | if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS)) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 837 | return; |
| 838 | |
| 839 | if (sdio_uart_claim_func(port) != 0) |
| 840 | return; |
| 841 | |
| 842 | if (I_IXOFF(tty)) { |
| 843 | port->x_char = STOP_CHAR(tty); |
| 844 | sdio_uart_start_tx(port); |
| 845 | } |
| 846 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 847 | if (tty->termios.c_cflag & CRTSCTS) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 848 | sdio_uart_clear_mctrl(port, TIOCM_RTS); |
| 849 | |
| 850 | sdio_uart_irq(port->func); |
| 851 | sdio_uart_release_func(port); |
| 852 | } |
| 853 | |
| 854 | static void sdio_uart_unthrottle(struct tty_struct *tty) |
| 855 | { |
| 856 | struct sdio_uart_port *port = tty->driver_data; |
| 857 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 858 | if (!I_IXOFF(tty) && !(tty->termios.c_cflag & CRTSCTS)) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 859 | return; |
| 860 | |
| 861 | if (sdio_uart_claim_func(port) != 0) |
| 862 | return; |
| 863 | |
| 864 | if (I_IXOFF(tty)) { |
| 865 | if (port->x_char) { |
| 866 | port->x_char = 0; |
| 867 | } else { |
| 868 | port->x_char = START_CHAR(tty); |
| 869 | sdio_uart_start_tx(port); |
| 870 | } |
| 871 | } |
| 872 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 873 | if (tty->termios.c_cflag & CRTSCTS) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 874 | sdio_uart_set_mctrl(port, TIOCM_RTS); |
| 875 | |
| 876 | sdio_uart_irq(port->func); |
| 877 | sdio_uart_release_func(port); |
| 878 | } |
| 879 | |
Alan Cox | c271cf3 | 2009-11-30 13:16:25 +0000 | [diff] [blame] | 880 | static void sdio_uart_set_termios(struct tty_struct *tty, |
| 881 | struct ktermios *old_termios) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 882 | { |
| 883 | struct sdio_uart_port *port = tty->driver_data; |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 884 | unsigned int cflag = tty->termios.c_cflag; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 885 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 886 | if (sdio_uart_claim_func(port) != 0) |
| 887 | return; |
| 888 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 889 | sdio_uart_change_speed(port, &tty->termios, old_termios); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 890 | |
| 891 | /* Handle transition to B0 status */ |
| 892 | if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) |
| 893 | sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR); |
| 894 | |
| 895 | /* Handle transition away from B0 status */ |
| 896 | if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { |
| 897 | unsigned int mask = TIOCM_DTR; |
| 898 | if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags)) |
| 899 | mask |= TIOCM_RTS; |
| 900 | sdio_uart_set_mctrl(port, mask); |
| 901 | } |
| 902 | |
| 903 | /* Handle turning off CRTSCTS */ |
| 904 | if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) { |
| 905 | tty->hw_stopped = 0; |
| 906 | sdio_uart_start_tx(port); |
| 907 | } |
| 908 | |
| 909 | /* Handle turning on CRTSCTS */ |
| 910 | if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) { |
| 911 | if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) { |
| 912 | tty->hw_stopped = 1; |
| 913 | sdio_uart_stop_tx(port); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | sdio_uart_release_func(port); |
| 918 | } |
| 919 | |
David Howells | c43d863 | 2008-07-10 12:28:48 +0100 | [diff] [blame] | 920 | static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 921 | { |
| 922 | struct sdio_uart_port *port = tty->driver_data; |
David Howells | c43d863 | 2008-07-10 12:28:48 +0100 | [diff] [blame] | 923 | int result; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 924 | |
David Howells | c43d863 | 2008-07-10 12:28:48 +0100 | [diff] [blame] | 925 | result = sdio_uart_claim_func(port); |
| 926 | if (result != 0) |
| 927 | return result; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 928 | |
| 929 | if (break_state == -1) |
| 930 | port->lcr |= UART_LCR_SBC; |
| 931 | else |
| 932 | port->lcr &= ~UART_LCR_SBC; |
| 933 | sdio_out(port, UART_LCR, port->lcr); |
| 934 | |
| 935 | sdio_uart_release_func(port); |
David Howells | c43d863 | 2008-07-10 12:28:48 +0100 | [diff] [blame] | 936 | return 0; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 937 | } |
| 938 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 939 | static int sdio_uart_tiocmget(struct tty_struct *tty) |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 940 | { |
| 941 | struct sdio_uart_port *port = tty->driver_data; |
| 942 | int result; |
| 943 | |
| 944 | result = sdio_uart_claim_func(port); |
| 945 | if (!result) { |
| 946 | result = port->mctrl | sdio_uart_get_mctrl(port); |
| 947 | sdio_uart_release_func(port); |
| 948 | } |
| 949 | |
| 950 | return result; |
| 951 | } |
| 952 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 953 | static int sdio_uart_tiocmset(struct tty_struct *tty, |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 954 | unsigned int set, unsigned int clear) |
| 955 | { |
| 956 | struct sdio_uart_port *port = tty->driver_data; |
| 957 | int result; |
| 958 | |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 959 | result = sdio_uart_claim_func(port); |
Alan Cox | c271cf3 | 2009-11-30 13:16:25 +0000 | [diff] [blame] | 960 | if (!result) { |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 961 | sdio_uart_update_mctrl(port, set, clear); |
| 962 | sdio_uart_release_func(port); |
| 963 | } |
| 964 | |
| 965 | return result; |
| 966 | } |
| 967 | |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 968 | static int sdio_uart_proc_show(struct seq_file *m, void *v) |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 969 | { |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 970 | int i; |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 971 | |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 972 | seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 973 | "", "", ""); |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 974 | for (i = 0; i < UART_NR; i++) { |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 975 | struct sdio_uart_port *port = sdio_uart_port_get(i); |
| 976 | if (port) { |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 977 | seq_printf(m, "%d: uart:SDIO", i); |
Alan Cox | c271cf3 | 2009-11-30 13:16:25 +0000 | [diff] [blame] | 978 | if (capable(CAP_SYS_ADMIN)) { |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 979 | seq_printf(m, " tx:%d rx:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 980 | port->icount.tx, port->icount.rx); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 981 | if (port->icount.frame) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 982 | seq_printf(m, " fe:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 983 | port->icount.frame); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 984 | if (port->icount.parity) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 985 | seq_printf(m, " pe:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 986 | port->icount.parity); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 987 | if (port->icount.brk) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 988 | seq_printf(m, " brk:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 989 | port->icount.brk); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 990 | if (port->icount.overrun) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 991 | seq_printf(m, " oe:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 992 | port->icount.overrun); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 993 | if (port->icount.cts) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 994 | seq_printf(m, " cts:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 995 | port->icount.cts); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 996 | if (port->icount.dsr) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 997 | seq_printf(m, " dsr:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 998 | port->icount.dsr); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 999 | if (port->icount.rng) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 1000 | seq_printf(m, " rng:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 1001 | port->icount.rng); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 1002 | if (port->icount.dcd) |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 1003 | seq_printf(m, " dcd:%d", |
Thadeu Lima de Souza Cascardo | 1e04b7a | 2009-10-15 16:44:00 -0300 | [diff] [blame] | 1004 | port->icount.dcd); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 1005 | } |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 1006 | sdio_uart_port_put(port); |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 1007 | seq_putc(m, '\n'); |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 1008 | } |
| 1009 | } |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 1010 | return 0; |
Nicolas Pitre | 5ed334a | 2007-07-04 23:40:34 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 1013 | static int sdio_uart_proc_open(struct inode *inode, struct file *file) |
| 1014 | { |
| 1015 | return single_open(file, sdio_uart_proc_show, NULL); |
| 1016 | } |
| 1017 | |
| 1018 | static const struct file_operations sdio_uart_proc_fops = { |
| 1019 | .owner = THIS_MODULE, |
| 1020 | .open = sdio_uart_proc_open, |
| 1021 | .read = seq_read, |
| 1022 | .llseek = seq_lseek, |
| 1023 | .release = single_release, |
| 1024 | }; |
| 1025 | |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 1026 | static const struct tty_port_operations sdio_uart_port_ops = { |
| 1027 | .dtr_rts = uart_dtr_rts, |
Alan Cox | 4b3b49b | 2009-11-30 13:16:36 +0000 | [diff] [blame] | 1028 | .carrier_raised = uart_carrier_raised, |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 1029 | .shutdown = sdio_uart_shutdown, |
| 1030 | .activate = sdio_uart_activate, |
Jiri Slaby | e70c677 | 2012-11-15 09:49:52 +0100 | [diff] [blame] | 1031 | .destruct = sdio_uart_port_destroy, |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 1032 | }; |
| 1033 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1034 | static const struct tty_operations sdio_uart_ops = { |
| 1035 | .open = sdio_uart_open, |
| 1036 | .close = sdio_uart_close, |
| 1037 | .write = sdio_uart_write, |
| 1038 | .write_room = sdio_uart_write_room, |
| 1039 | .chars_in_buffer = sdio_uart_chars_in_buffer, |
| 1040 | .send_xchar = sdio_uart_send_xchar, |
| 1041 | .throttle = sdio_uart_throttle, |
| 1042 | .unthrottle = sdio_uart_unthrottle, |
| 1043 | .set_termios = sdio_uart_set_termios, |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 1044 | .hangup = sdio_uart_hangup, |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1045 | .break_ctl = sdio_uart_break_ctl, |
| 1046 | .tiocmget = sdio_uart_tiocmget, |
| 1047 | .tiocmset = sdio_uart_tiocmset, |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 1048 | .install = sdio_uart_install, |
| 1049 | .cleanup = sdio_uart_cleanup, |
Alexey Dobriyan | 201a50b | 2009-03-31 15:19:20 -0700 | [diff] [blame] | 1050 | .proc_fops = &sdio_uart_proc_fops, |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1051 | }; |
| 1052 | |
| 1053 | static struct tty_driver *sdio_uart_tty_driver; |
| 1054 | |
| 1055 | static int sdio_uart_probe(struct sdio_func *func, |
| 1056 | const struct sdio_device_id *id) |
| 1057 | { |
| 1058 | struct sdio_uart_port *port; |
| 1059 | int ret; |
| 1060 | |
| 1061 | port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL); |
| 1062 | if (!port) |
| 1063 | return -ENOMEM; |
| 1064 | |
| 1065 | if (func->class == SDIO_CLASS_UART) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 1066 | pr_warn("%s: need info on UART class basic setup\n", |
| 1067 | sdio_func_id(func)); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1068 | kfree(port); |
| 1069 | return -ENOSYS; |
| 1070 | } else if (func->class == SDIO_CLASS_GPS) { |
| 1071 | /* |
| 1072 | * We need tuple 0x91. It contains SUBTPL_SIOREG |
| 1073 | * and SUBTPL_RCVCAPS. |
| 1074 | */ |
| 1075 | struct sdio_func_tuple *tpl; |
| 1076 | for (tpl = func->tuples; tpl; tpl = tpl->next) { |
| 1077 | if (tpl->code != 0x91) |
| 1078 | continue; |
| 1079 | if (tpl->size < 10) |
| 1080 | continue; |
| 1081 | if (tpl->data[1] == 0) /* SUBTPL_SIOREG */ |
| 1082 | break; |
| 1083 | } |
| 1084 | if (!tpl) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 1085 | pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n", |
| 1086 | sdio_func_id(func)); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1087 | kfree(port); |
| 1088 | return -EINVAL; |
| 1089 | } |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1090 | pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n", |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1091 | sdio_func_id(func), tpl->data[2], tpl->data[3]); |
| 1092 | port->regs_offset = (tpl->data[4] << 0) | |
| 1093 | (tpl->data[5] << 8) | |
| 1094 | (tpl->data[6] << 16); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1095 | pr_debug("%s: regs offset = 0x%x\n", |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1096 | sdio_func_id(func), port->regs_offset); |
| 1097 | port->uartclk = tpl->data[7] * 115200; |
| 1098 | if (port->uartclk == 0) |
| 1099 | port->uartclk = 115200; |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1100 | pr_debug("%s: clk %d baudcode %u 4800-div %u\n", |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1101 | sdio_func_id(func), port->uartclk, |
| 1102 | tpl->data[7], tpl->data[8] | (tpl->data[9] << 8)); |
| 1103 | } else { |
| 1104 | kfree(port); |
| 1105 | return -EINVAL; |
| 1106 | } |
| 1107 | |
| 1108 | port->func = func; |
| 1109 | sdio_set_drvdata(func, port); |
Alan Cox | b5849b1 | 2009-11-05 13:28:06 +0000 | [diff] [blame] | 1110 | tty_port_init(&port->port); |
Alan Cox | 584abc3 | 2009-11-30 13:16:09 +0000 | [diff] [blame] | 1111 | port->port.ops = &sdio_uart_port_ops; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1112 | |
| 1113 | ret = sdio_uart_add_port(port); |
| 1114 | if (ret) { |
| 1115 | kfree(port); |
| 1116 | } else { |
| 1117 | struct device *dev; |
Jiri Slaby | 734cc17 | 2012-08-07 21:47:47 +0200 | [diff] [blame] | 1118 | dev = tty_port_register_device(&port->port, |
| 1119 | sdio_uart_tty_driver, port->index, &func->dev); |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1120 | if (IS_ERR(dev)) { |
| 1121 | sdio_uart_port_remove(port); |
| 1122 | ret = PTR_ERR(dev); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
| 1129 | static void sdio_uart_remove(struct sdio_func *func) |
| 1130 | { |
| 1131 | struct sdio_uart_port *port = sdio_get_drvdata(func); |
| 1132 | |
| 1133 | tty_unregister_device(sdio_uart_tty_driver, port->index); |
| 1134 | sdio_uart_port_remove(port); |
| 1135 | } |
| 1136 | |
| 1137 | static const struct sdio_device_id sdio_uart_ids[] = { |
| 1138 | { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) }, |
| 1139 | { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) }, |
| 1140 | { /* end: all zeroes */ }, |
| 1141 | }; |
| 1142 | |
| 1143 | MODULE_DEVICE_TABLE(sdio, sdio_uart_ids); |
| 1144 | |
| 1145 | static struct sdio_driver sdio_uart_driver = { |
| 1146 | .probe = sdio_uart_probe, |
| 1147 | .remove = sdio_uart_remove, |
| 1148 | .name = "sdio_uart", |
| 1149 | .id_table = sdio_uart_ids, |
| 1150 | }; |
| 1151 | |
| 1152 | static int __init sdio_uart_init(void) |
| 1153 | { |
| 1154 | int ret; |
| 1155 | struct tty_driver *tty_drv; |
| 1156 | |
| 1157 | sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR); |
| 1158 | if (!tty_drv) |
| 1159 | return -ENOMEM; |
| 1160 | |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1161 | tty_drv->driver_name = "sdio_uart"; |
| 1162 | tty_drv->name = "ttySDIO"; |
| 1163 | tty_drv->major = 0; /* dynamically allocated */ |
| 1164 | tty_drv->minor_start = 0; |
| 1165 | tty_drv->type = TTY_DRIVER_TYPE_SERIAL; |
| 1166 | tty_drv->subtype = SERIAL_TYPE_NORMAL; |
| 1167 | tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
| 1168 | tty_drv->init_termios = tty_std_termios; |
| 1169 | tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL; |
Nicolas Pitre | 2ba30ee | 2007-08-15 13:27:29 -0400 | [diff] [blame] | 1170 | tty_drv->init_termios.c_ispeed = 4800; |
| 1171 | tty_drv->init_termios.c_ospeed = 4800; |
Nicolas Pitre | 6e418a9 | 2007-06-30 02:04:21 -0400 | [diff] [blame] | 1172 | tty_set_operations(tty_drv, &sdio_uart_ops); |
| 1173 | |
| 1174 | ret = tty_register_driver(tty_drv); |
| 1175 | if (ret) |
| 1176 | goto err1; |
| 1177 | |
| 1178 | ret = sdio_register_driver(&sdio_uart_driver); |
| 1179 | if (ret) |
| 1180 | goto err2; |
| 1181 | |
| 1182 | return 0; |
| 1183 | |
| 1184 | err2: |
| 1185 | tty_unregister_driver(tty_drv); |
| 1186 | err1: |
| 1187 | put_tty_driver(tty_drv); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
| 1191 | static void __exit sdio_uart_exit(void) |
| 1192 | { |
| 1193 | sdio_unregister_driver(&sdio_uart_driver); |
| 1194 | tty_unregister_driver(sdio_uart_tty_driver); |
| 1195 | put_tty_driver(sdio_uart_tty_driver); |
| 1196 | } |
| 1197 | |
| 1198 | module_init(sdio_uart_init); |
| 1199 | module_exit(sdio_uart_exit); |
| 1200 | |
| 1201 | MODULE_AUTHOR("Nicolas Pitre"); |
| 1202 | MODULE_LICENSE("GPL"); |