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 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; 50 wait_queue_head_t delta_msr_wait; 51 u8 line_control; 52 u8 line_status; 53 }; 54 55 static void f81232_update_line_status(struct usb_serial_port *port, 56 unsigned char *data, 57 unsigned int actual_length) 58 { 59 } 60 61 static void f81232_read_int_callback(struct urb *urb) 62 { 63 struct usb_serial_port *port = urb->context; 64 unsigned char *data = urb->transfer_buffer; 65 unsigned int actual_length = urb->actual_length; 66 int status = urb->status; 67 int retval; 68 69 switch (status) { 70 case 0: 71 /* success */ 72 break; 73 case -ECONNRESET: 74 case -ENOENT: 75 case -ESHUTDOWN: 76 /* this urb is terminated, clean up */ 77 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", 78 __func__, status); 79 return; 80 default: 81 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", 82 __func__, status); 83 goto exit; 84 } 85 86 usb_serial_debug_data(&port->dev, __func__, 87 urb->actual_length, urb->transfer_buffer); 88 89 f81232_update_line_status(port, data, actual_length); 90 91 exit: 92 retval = usb_submit_urb(urb, GFP_ATOMIC); 93 if (retval) 94 dev_err(&urb->dev->dev, 95 "%s - usb_submit_urb failed with result %d\n", 96 __func__, retval); 97 } 98 99 static void f81232_process_read_urb(struct urb *urb) 100 { 101 struct usb_serial_port *port = urb->context; 102 struct f81232_private *priv = usb_get_serial_port_data(port); 103 unsigned char *data = urb->transfer_buffer; 104 char tty_flag = TTY_NORMAL; 105 unsigned long flags; 106 u8 line_status; 107 int i; 108 109 /* update line status */ 110 spin_lock_irqsave(&priv->lock, flags); 111 line_status = priv->line_status; 112 priv->line_status &= ~UART_STATE_TRANSIENT_MASK; 113 spin_unlock_irqrestore(&priv->lock, flags); 114 wake_up_interruptible(&priv->delta_msr_wait); 115 116 if (!urb->actual_length) 117 return; 118 119 /* break takes precedence over parity, */ 120 /* which takes precedence over framing errors */ 121 if (line_status & UART_BREAK_ERROR) 122 tty_flag = TTY_BREAK; 123 else if (line_status & UART_PARITY_ERROR) 124 tty_flag = TTY_PARITY; 125 else if (line_status & UART_FRAME_ERROR) 126 tty_flag = TTY_FRAME; 127 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag); 128 129 /* overrun is special, not associated with a char */ 130 if (line_status & UART_OVERRUN_ERROR) 131 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); 132 133 if (port->port.console && port->sysrq) { 134 for (i = 0; i < urb->actual_length; ++i) 135 if (!usb_serial_handle_sysrq_char(port, data[i])) 136 tty_insert_flip_char(&port->port, data[i], 137 tty_flag); 138 } else { 139 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag, 140 urb->actual_length); 141 } 142 143 tty_flip_buffer_push(&port->port); 144 } 145 146 static int set_control_lines(struct usb_device *dev, u8 value) 147 { 148 /* FIXME - Stubbed out for now */ 149 return 0; 150 } 151 152 static void f81232_break_ctl(struct tty_struct *tty, int break_state) 153 { 154 /* FIXME - Stubbed out for now */ 155 156 /* 157 * break_state = -1 to turn on break, and 0 to turn off break 158 * see drivers/char/tty_io.c to see it used. 159 * last_set_data_urb_value NEVER has the break bit set in it. 160 */ 161 } 162 163 static void f81232_set_termios(struct tty_struct *tty, 164 struct usb_serial_port *port, struct ktermios *old_termios) 165 { 166 /* FIXME - Stubbed out for now */ 167 168 /* Don't change anything if nothing has changed */ 169 if (!tty_termios_hw_change(&tty->termios, old_termios)) 170 return; 171 172 /* Do the real work here... */ 173 tty_termios_copy_hw(&tty->termios, old_termios); 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 { 191 struct ktermios tmp_termios; 192 int result; 193 194 /* Setup termios */ 195 if (tty) 196 f81232_set_termios(tty, port, &tmp_termios); 197 198 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 199 if (result) { 200 dev_err(&port->dev, "%s - failed submitting interrupt urb," 201 " error %d\n", __func__, result); 202 return result; 203 } 204 205 result = usb_serial_generic_open(tty, port); 206 if (result) { 207 usb_kill_urb(port->interrupt_in_urb); 208 return result; 209 } 210 211 port->port.drain_delay = 256; 212 return 0; 213 } 214 215 static void f81232_close(struct usb_serial_port *port) 216 { 217 usb_serial_generic_close(port); 218 usb_kill_urb(port->interrupt_in_urb); 219 } 220 221 static void f81232_dtr_rts(struct usb_serial_port *port, int on) 222 { 223 struct f81232_private *priv = usb_get_serial_port_data(port); 224 unsigned long flags; 225 u8 control; 226 227 spin_lock_irqsave(&priv->lock, flags); 228 /* Change DTR and RTS */ 229 if (on) 230 priv->line_control |= (CONTROL_DTR | CONTROL_RTS); 231 else 232 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); 233 control = priv->line_control; 234 spin_unlock_irqrestore(&priv->lock, flags); 235 set_control_lines(port->serial->dev, control); 236 } 237 238 static int f81232_carrier_raised(struct usb_serial_port *port) 239 { 240 struct f81232_private *priv = usb_get_serial_port_data(port); 241 if (priv->line_status & UART_DCD) 242 return 1; 243 return 0; 244 } 245 246 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) 247 { 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) { 259 interruptible_sleep_on(&priv->delta_msr_wait); 260 /* see if a signal did it */ 261 if (signal_pending(current)) 262 return -ERESTARTSYS; 263 264 spin_lock_irqsave(&priv->lock, flags); 265 status = priv->line_status; 266 spin_unlock_irqrestore(&priv->lock, flags); 267 268 changed = prevstatus ^ status; 269 270 if (((arg & TIOCM_RNG) && (changed & UART_RING)) || 271 ((arg & TIOCM_DSR) && (changed & UART_DSR)) || 272 ((arg & TIOCM_CD) && (changed & UART_DCD)) || 273 ((arg & TIOCM_CTS) && (changed & UART_CTS))) { 274 return 0; 275 } 276 prevstatus = status; 277 } 278 /* NOTREACHED */ 279 return 0; 280 } 281 282 static int f81232_ioctl(struct tty_struct *tty, 283 unsigned int cmd, unsigned long arg) 284 { 285 struct serial_struct ser; 286 struct usb_serial_port *port = tty->driver_data; 287 288 dev_dbg(&port->dev, "%s (%d) cmd = 0x%04x\n", __func__, 289 port->number, cmd); 290 291 switch (cmd) { 292 case TIOCGSERIAL: 293 memset(&ser, 0, sizeof ser); 294 ser.type = PORT_16654; 295 ser.line = port->serial->minor; 296 ser.port = port->number; 297 ser.baud_base = 460800; 298 299 if (copy_to_user((void __user *)arg, &ser, sizeof ser)) 300 return -EFAULT; 301 302 return 0; 303 304 case TIOCMIWAIT: 305 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, 306 port->number); 307 return wait_modem_info(port, arg); 308 default: 309 dev_dbg(&port->dev, "%s not supported = 0x%04x\n", 310 __func__, cmd); 311 break; 312 } 313 return -ENOIOCTLCMD; 314 } 315 316 static int f81232_port_probe(struct usb_serial_port *port) 317 { 318 struct f81232_private *priv; 319 320 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 321 if (!priv) 322 return -ENOMEM; 323 324 spin_lock_init(&priv->lock); 325 init_waitqueue_head(&priv->delta_msr_wait); 326 327 usb_set_serial_port_data(port, priv); 328 329 return 0; 330 } 331 332 static int f81232_port_remove(struct usb_serial_port *port) 333 { 334 struct f81232_private *priv; 335 336 priv = usb_get_serial_port_data(port); 337 kfree(priv); 338 339 return 0; 340 } 341 342 static struct usb_serial_driver f81232_device = { 343 .driver = { 344 .owner = THIS_MODULE, 345 .name = "f81232", 346 }, 347 .id_table = id_table, 348 .num_ports = 1, 349 .bulk_in_size = 256, 350 .bulk_out_size = 256, 351 .open = f81232_open, 352 .close = f81232_close, 353 .dtr_rts = f81232_dtr_rts, 354 .carrier_raised = f81232_carrier_raised, 355 .ioctl = f81232_ioctl, 356 .break_ctl = f81232_break_ctl, 357 .set_termios = f81232_set_termios, 358 .tiocmget = f81232_tiocmget, 359 .tiocmset = f81232_tiocmset, 360 .process_read_urb = f81232_process_read_urb, 361 .read_int_callback = f81232_read_int_callback, 362 .port_probe = f81232_port_probe, 363 .port_remove = f81232_port_remove, 364 }; 365 366 static struct usb_serial_driver * const serial_drivers[] = { 367 &f81232_device, 368 NULL, 369 }; 370 371 module_usb_serial_driver(serial_drivers, id_table); 372 373 MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver"); 374 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org"); 375 MODULE_LICENSE("GPL v2"); 376