Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Fintek F81232 USB to serial adaptor driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org) |
| 5 | * Copyright (C) 2012 Linux Foundation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License version 2 as published by |
| 9 | * the Free Software Foundation. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/tty.h> |
| 18 | #include <linux/tty_driver.h> |
| 19 | #include <linux/tty_flip.h> |
| 20 | #include <linux/serial.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/spinlock.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/usb.h> |
| 26 | #include <linux/usb/serial.h> |
| 27 | |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 28 | static const struct usb_device_id id_table[] = { |
| 29 | { USB_DEVICE(0x1934, 0x0706) }, |
| 30 | { } /* Terminating entry */ |
| 31 | }; |
| 32 | MODULE_DEVICE_TABLE(usb, id_table); |
| 33 | |
| 34 | #define CONTROL_DTR 0x01 |
| 35 | #define CONTROL_RTS 0x02 |
| 36 | |
| 37 | #define UART_STATE 0x08 |
| 38 | #define UART_STATE_TRANSIENT_MASK 0x74 |
| 39 | #define UART_DCD 0x01 |
| 40 | #define UART_DSR 0x02 |
| 41 | #define UART_BREAK_ERROR 0x04 |
| 42 | #define UART_RING 0x08 |
| 43 | #define UART_FRAME_ERROR 0x10 |
| 44 | #define UART_PARITY_ERROR 0x20 |
| 45 | #define UART_OVERRUN_ERROR 0x40 |
| 46 | #define UART_CTS 0x80 |
| 47 | |
| 48 | struct f81232_private { |
| 49 | spinlock_t lock; |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 50 | u8 line_control; |
| 51 | u8 line_status; |
| 52 | }; |
| 53 | |
| 54 | static void f81232_update_line_status(struct usb_serial_port *port, |
| 55 | unsigned char *data, |
| 56 | unsigned int actual_length) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | static void f81232_read_int_callback(struct urb *urb) |
| 61 | { |
| 62 | struct usb_serial_port *port = urb->context; |
| 63 | unsigned char *data = urb->transfer_buffer; |
| 64 | unsigned int actual_length = urb->actual_length; |
| 65 | int status = urb->status; |
| 66 | int retval; |
| 67 | |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 68 | switch (status) { |
| 69 | case 0: |
| 70 | /* success */ |
| 71 | break; |
| 72 | case -ECONNRESET: |
| 73 | case -ENOENT: |
| 74 | case -ESHUTDOWN: |
| 75 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | a94e9b9 | 2012-05-15 16:27:17 -0700 | [diff] [blame] | 76 | dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", |
| 77 | __func__, status); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 78 | return; |
| 79 | default: |
Greg Kroah-Hartman | a94e9b9 | 2012-05-15 16:27:17 -0700 | [diff] [blame] | 80 | dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", |
| 81 | __func__, status); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 82 | goto exit; |
| 83 | } |
| 84 | |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 85 | usb_serial_debug_data(&port->dev, __func__, |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 86 | urb->actual_length, urb->transfer_buffer); |
| 87 | |
| 88 | f81232_update_line_status(port, data, actual_length); |
| 89 | |
| 90 | exit: |
| 91 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 92 | if (retval) |
| 93 | dev_err(&urb->dev->dev, |
| 94 | "%s - usb_submit_urb failed with result %d\n", |
| 95 | __func__, retval); |
| 96 | } |
| 97 | |
| 98 | static void f81232_process_read_urb(struct urb *urb) |
| 99 | { |
| 100 | struct usb_serial_port *port = urb->context; |
| 101 | struct f81232_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 102 | unsigned char *data = urb->transfer_buffer; |
| 103 | char tty_flag = TTY_NORMAL; |
| 104 | unsigned long flags; |
| 105 | u8 line_status; |
| 106 | int i; |
| 107 | |
| 108 | /* update line status */ |
| 109 | spin_lock_irqsave(&priv->lock, flags); |
| 110 | line_status = priv->line_status; |
| 111 | priv->line_status &= ~UART_STATE_TRANSIENT_MASK; |
| 112 | spin_unlock_irqrestore(&priv->lock, flags); |
Johan Hovold | 3018bb5 | 2013-03-21 12:37:06 +0100 | [diff] [blame] | 113 | wake_up_interruptible(&port->port.delta_msr_wait); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 114 | |
| 115 | if (!urb->actual_length) |
| 116 | return; |
| 117 | |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 118 | /* break takes precedence over parity, */ |
| 119 | /* which takes precedence over framing errors */ |
| 120 | if (line_status & UART_BREAK_ERROR) |
| 121 | tty_flag = TTY_BREAK; |
| 122 | else if (line_status & UART_PARITY_ERROR) |
| 123 | tty_flag = TTY_PARITY; |
| 124 | else if (line_status & UART_FRAME_ERROR) |
| 125 | tty_flag = TTY_FRAME; |
Greg Kroah-Hartman | a94e9b9 | 2012-05-15 16:27:17 -0700 | [diff] [blame] | 126 | dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 127 | |
| 128 | /* overrun is special, not associated with a char */ |
| 129 | if (line_status & UART_OVERRUN_ERROR) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 130 | tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 131 | |
| 132 | if (port->port.console && port->sysrq) { |
| 133 | for (i = 0; i < urb->actual_length; ++i) |
| 134 | if (!usb_serial_handle_sysrq_char(port, data[i])) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 135 | tty_insert_flip_char(&port->port, data[i], |
| 136 | tty_flag); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 137 | } else { |
Jiri Slaby | 2f69335 | 2013-01-03 15:53:02 +0100 | [diff] [blame] | 138 | tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag, |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 139 | urb->actual_length); |
| 140 | } |
| 141 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 142 | tty_flip_buffer_push(&port->port); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static int set_control_lines(struct usb_device *dev, u8 value) |
| 146 | { |
| 147 | /* FIXME - Stubbed out for now */ |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void f81232_break_ctl(struct tty_struct *tty, int break_state) |
| 152 | { |
| 153 | /* FIXME - Stubbed out for now */ |
| 154 | |
| 155 | /* |
| 156 | * break_state = -1 to turn on break, and 0 to turn off break |
| 157 | * see drivers/char/tty_io.c to see it used. |
| 158 | * last_set_data_urb_value NEVER has the break bit set in it. |
| 159 | */ |
| 160 | } |
| 161 | |
| 162 | static void f81232_set_termios(struct tty_struct *tty, |
| 163 | struct usb_serial_port *port, struct ktermios *old_termios) |
| 164 | { |
| 165 | /* FIXME - Stubbed out for now */ |
| 166 | |
| 167 | /* Don't change anything if nothing has changed */ |
Johan Hovold | 2188672 | 2013-06-10 18:29:37 +0200 | [diff] [blame^] | 168 | if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios)) |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 169 | return; |
| 170 | |
| 171 | /* Do the real work here... */ |
Johan Hovold | 2188672 | 2013-06-10 18:29:37 +0200 | [diff] [blame^] | 172 | if (old_termios) |
| 173 | tty_termios_copy_hw(&tty->termios, old_termios); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static int f81232_tiocmget(struct tty_struct *tty) |
| 177 | { |
| 178 | /* FIXME - Stubbed out for now */ |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int f81232_tiocmset(struct tty_struct *tty, |
| 183 | unsigned int set, unsigned int clear) |
| 184 | { |
| 185 | /* FIXME - Stubbed out for now */ |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 190 | { |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 191 | int result; |
| 192 | |
| 193 | /* Setup termios */ |
| 194 | if (tty) |
Johan Hovold | 2188672 | 2013-06-10 18:29:37 +0200 | [diff] [blame^] | 195 | f81232_set_termios(tty, port, NULL); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 196 | |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 197 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 198 | if (result) { |
| 199 | dev_err(&port->dev, "%s - failed submitting interrupt urb," |
| 200 | " error %d\n", __func__, result); |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | result = usb_serial_generic_open(tty, port); |
| 205 | if (result) { |
| 206 | usb_kill_urb(port->interrupt_in_urb); |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | port->port.drain_delay = 256; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static void f81232_close(struct usb_serial_port *port) |
| 215 | { |
| 216 | usb_serial_generic_close(port); |
| 217 | usb_kill_urb(port->interrupt_in_urb); |
| 218 | } |
| 219 | |
| 220 | static void f81232_dtr_rts(struct usb_serial_port *port, int on) |
| 221 | { |
| 222 | struct f81232_private *priv = usb_get_serial_port_data(port); |
| 223 | unsigned long flags; |
| 224 | u8 control; |
| 225 | |
| 226 | spin_lock_irqsave(&priv->lock, flags); |
| 227 | /* Change DTR and RTS */ |
| 228 | if (on) |
| 229 | priv->line_control |= (CONTROL_DTR | CONTROL_RTS); |
| 230 | else |
| 231 | priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); |
| 232 | control = priv->line_control; |
| 233 | spin_unlock_irqrestore(&priv->lock, flags); |
| 234 | set_control_lines(port->serial->dev, control); |
| 235 | } |
| 236 | |
| 237 | static int f81232_carrier_raised(struct usb_serial_port *port) |
| 238 | { |
| 239 | struct f81232_private *priv = usb_get_serial_port_data(port); |
| 240 | if (priv->line_status & UART_DCD) |
| 241 | return 1; |
| 242 | return 0; |
| 243 | } |
| 244 | |
Johan Hovold | 4bb4e63 | 2013-03-21 12:37:05 +0100 | [diff] [blame] | 245 | static int f81232_tiocmiwait(struct tty_struct *tty, unsigned long arg) |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 246 | { |
Johan Hovold | 4bb4e63 | 2013-03-21 12:37:05 +0100 | [diff] [blame] | 247 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 248 | struct f81232_private *priv = usb_get_serial_port_data(port); |
| 249 | unsigned long flags; |
| 250 | unsigned int prevstatus; |
| 251 | unsigned int status; |
| 252 | unsigned int changed; |
| 253 | |
| 254 | spin_lock_irqsave(&priv->lock, flags); |
| 255 | prevstatus = priv->line_status; |
| 256 | spin_unlock_irqrestore(&priv->lock, flags); |
| 257 | |
| 258 | while (1) { |
Johan Hovold | 3018bb5 | 2013-03-21 12:37:06 +0100 | [diff] [blame] | 259 | interruptible_sleep_on(&port->port.delta_msr_wait); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 260 | /* see if a signal did it */ |
| 261 | if (signal_pending(current)) |
| 262 | return -ERESTARTSYS; |
| 263 | |
Johan Hovold | 508f940 | 2013-03-19 09:21:14 +0100 | [diff] [blame] | 264 | if (port->serial->disconnected) |
| 265 | return -EIO; |
| 266 | |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 267 | spin_lock_irqsave(&priv->lock, flags); |
| 268 | status = priv->line_status; |
| 269 | spin_unlock_irqrestore(&priv->lock, flags); |
| 270 | |
| 271 | changed = prevstatus ^ status; |
| 272 | |
| 273 | if (((arg & TIOCM_RNG) && (changed & UART_RING)) || |
| 274 | ((arg & TIOCM_DSR) && (changed & UART_DSR)) || |
| 275 | ((arg & TIOCM_CD) && (changed & UART_DCD)) || |
| 276 | ((arg & TIOCM_CTS) && (changed & UART_CTS))) { |
| 277 | return 0; |
| 278 | } |
| 279 | prevstatus = status; |
| 280 | } |
| 281 | /* NOTREACHED */ |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int f81232_ioctl(struct tty_struct *tty, |
| 286 | unsigned int cmd, unsigned long arg) |
| 287 | { |
| 288 | struct serial_struct ser; |
| 289 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | a94e9b9 | 2012-05-15 16:27:17 -0700 | [diff] [blame] | 290 | |
| 291 | dev_dbg(&port->dev, "%s (%d) cmd = 0x%04x\n", __func__, |
| 292 | port->number, cmd); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 293 | |
| 294 | switch (cmd) { |
| 295 | case TIOCGSERIAL: |
| 296 | memset(&ser, 0, sizeof ser); |
| 297 | ser.type = PORT_16654; |
| 298 | ser.line = port->serial->minor; |
| 299 | ser.port = port->number; |
| 300 | ser.baud_base = 460800; |
| 301 | |
| 302 | if (copy_to_user((void __user *)arg, &ser, sizeof ser)) |
| 303 | return -EFAULT; |
| 304 | |
| 305 | return 0; |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 306 | default: |
Greg Kroah-Hartman | a94e9b9 | 2012-05-15 16:27:17 -0700 | [diff] [blame] | 307 | dev_dbg(&port->dev, "%s not supported = 0x%04x\n", |
| 308 | __func__, cmd); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | return -ENOIOCTLCMD; |
| 312 | } |
| 313 | |
Johan Hovold | 3124d1d | 2012-10-17 13:34:56 +0200 | [diff] [blame] | 314 | static int f81232_port_probe(struct usb_serial_port *port) |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 315 | { |
| 316 | struct f81232_private *priv; |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 317 | |
Johan Hovold | 3124d1d | 2012-10-17 13:34:56 +0200 | [diff] [blame] | 318 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 319 | if (!priv) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | spin_lock_init(&priv->lock); |
Johan Hovold | 3124d1d | 2012-10-17 13:34:56 +0200 | [diff] [blame] | 323 | |
| 324 | usb_set_serial_port_data(port, priv); |
| 325 | |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 326 | return 0; |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Johan Hovold | 3124d1d | 2012-10-17 13:34:56 +0200 | [diff] [blame] | 329 | static int f81232_port_remove(struct usb_serial_port *port) |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 330 | { |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 331 | struct f81232_private *priv; |
| 332 | |
Johan Hovold | 3124d1d | 2012-10-17 13:34:56 +0200 | [diff] [blame] | 333 | priv = usb_get_serial_port_data(port); |
| 334 | kfree(priv); |
| 335 | |
| 336 | return 0; |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 339 | static struct usb_serial_driver f81232_device = { |
| 340 | .driver = { |
| 341 | .owner = THIS_MODULE, |
| 342 | .name = "f81232", |
| 343 | }, |
| 344 | .id_table = id_table, |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 345 | .num_ports = 1, |
| 346 | .bulk_in_size = 256, |
| 347 | .bulk_out_size = 256, |
| 348 | .open = f81232_open, |
| 349 | .close = f81232_close, |
| 350 | .dtr_rts = f81232_dtr_rts, |
| 351 | .carrier_raised = f81232_carrier_raised, |
| 352 | .ioctl = f81232_ioctl, |
| 353 | .break_ctl = f81232_break_ctl, |
| 354 | .set_termios = f81232_set_termios, |
| 355 | .tiocmget = f81232_tiocmget, |
| 356 | .tiocmset = f81232_tiocmset, |
Johan Hovold | 4bb4e63 | 2013-03-21 12:37:05 +0100 | [diff] [blame] | 357 | .tiocmiwait = f81232_tiocmiwait, |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 358 | .process_read_urb = f81232_process_read_urb, |
| 359 | .read_int_callback = f81232_read_int_callback, |
Johan Hovold | 3124d1d | 2012-10-17 13:34:56 +0200 | [diff] [blame] | 360 | .port_probe = f81232_port_probe, |
| 361 | .port_remove = f81232_port_remove, |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | static struct usb_serial_driver * const serial_drivers[] = { |
| 365 | &f81232_device, |
| 366 | NULL, |
| 367 | }; |
| 368 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 369 | module_usb_serial_driver(serial_drivers, id_table); |
Greg Kroah-Hartman | aac1fc3 | 2012-02-28 13:36:35 -0800 | [diff] [blame] | 370 | |
| 371 | MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver"); |
| 372 | MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org"); |
| 373 | MODULE_LICENSE("GPL v2"); |