2 * Belkin USB Serial Adapter Driver
4 * Copyright (C) 2000 William Greathouse (wgreathouse@smva.com)
5 * Copyright (C) 2000-2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2010 Johan Hovold (jhovold@gmail.com)
8 * This program is largely derived from work by the linux-usb group
9 * and associated source files. Please see the usb/serial files for
10 * individual credits and copyrights.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * See Documentation/usb/usb-serial.txt for more information on using this
21 * -- Add true modem contol line query capability. Currently we track the
22 * states reported by the interrupt and the states we request.
23 * -- Add support for flush commands
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/tty.h>
31 #include <linux/tty_driver.h>
32 #include <linux/tty_flip.h>
33 #include <linux/module.h>
34 #include <linux/spinlock.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
38 #include "belkin_sa.h"
45 #define DRIVER_VERSION "v1.3"
46 #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
47 #define DRIVER_DESC "USB Belkin Serial converter driver"
49 /* function prototypes for a Belkin USB Serial Adapter F5U103 */
50 static int belkin_sa_startup(struct usb_serial *serial);
51 static void belkin_sa_release(struct usb_serial *serial);
52 static int belkin_sa_open(struct tty_struct *tty,
53 struct usb_serial_port *port);
54 static void belkin_sa_close(struct usb_serial_port *port);
55 static void belkin_sa_read_int_callback(struct urb *urb);
56 static void belkin_sa_process_read_urb(struct urb *urb);
57 static void belkin_sa_set_termios(struct tty_struct *tty,
58 struct usb_serial_port *port, struct ktermios * old);
59 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
60 static int belkin_sa_tiocmget(struct tty_struct *tty);
61 static int belkin_sa_tiocmset(struct tty_struct *tty,
62 unsigned int set, unsigned int clear);
65 static const struct usb_device_id id_table_combined[] = {
66 { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
67 { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
68 { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
69 { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
70 { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
71 { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
72 { } /* Terminating entry */
74 MODULE_DEVICE_TABLE(usb, id_table_combined);
76 static struct usb_driver belkin_driver = {
78 .disconnect = usb_serial_disconnect,
79 .id_table = id_table_combined,
82 /* All of the device info needed for the serial converters */
83 static struct usb_serial_driver belkin_device = {
88 .description = "Belkin / Peracom / GoHubs USB Serial Adapter",
89 .id_table = id_table_combined,
91 .open = belkin_sa_open,
92 .close = belkin_sa_close,
93 .read_int_callback = belkin_sa_read_int_callback,
94 .process_read_urb = belkin_sa_process_read_urb,
95 .set_termios = belkin_sa_set_termios,
96 .break_ctl = belkin_sa_break_ctl,
97 .tiocmget = belkin_sa_tiocmget,
98 .tiocmset = belkin_sa_tiocmset,
99 .attach = belkin_sa_startup,
100 .release = belkin_sa_release,
103 static struct usb_serial_driver * const serial_drivers[] = {
107 struct belkin_sa_private {
109 unsigned long control_state;
110 unsigned char last_lsr;
111 unsigned char last_msr;
112 int bad_flow_control;
117 * ***************************************************************************
118 * Belkin USB Serial Adapter F5U103 specific driver functions
119 * ***************************************************************************
122 #define WDR_TIMEOUT 5000 /* default urb timeout */
124 /* assumes that struct usb_serial *serial is available */
125 #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
126 (c), BELKIN_SA_SET_REQUEST_TYPE, \
127 (v), 0, NULL, 0, WDR_TIMEOUT)
129 /* do some startup allocations not currently performed by usb_serial_probe() */
130 static int belkin_sa_startup(struct usb_serial *serial)
132 struct usb_device *dev = serial->dev;
133 struct belkin_sa_private *priv;
135 /* allocate the private data structure */
136 priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
138 return -1; /* error */
139 /* set initial values for control structures */
140 spin_lock_init(&priv->lock);
141 priv->control_state = 0;
144 /* see comments at top of file */
145 priv->bad_flow_control =
146 (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
147 dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
148 le16_to_cpu(dev->descriptor.bcdDevice),
149 priv->bad_flow_control);
151 init_waitqueue_head(&serial->port[0]->write_wait);
152 usb_set_serial_port_data(serial->port[0], priv);
157 static void belkin_sa_release(struct usb_serial *serial)
161 for (i = 0; i < serial->num_ports; ++i)
162 kfree(usb_get_serial_port_data(serial->port[i]));
165 static int belkin_sa_open(struct tty_struct *tty,
166 struct usb_serial_port *port)
170 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
172 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
176 retval = usb_serial_generic_open(tty, port);
178 usb_kill_urb(port->interrupt_in_urb);
183 static void belkin_sa_close(struct usb_serial_port *port)
185 usb_serial_generic_close(port);
186 usb_kill_urb(port->interrupt_in_urb);
189 static void belkin_sa_read_int_callback(struct urb *urb)
191 struct usb_serial_port *port = urb->context;
192 struct belkin_sa_private *priv;
193 unsigned char *data = urb->transfer_buffer;
195 int status = urb->status;
205 /* this urb is terminated, clean up */
206 dbg("%s - urb shutting down with status: %d",
210 dbg("%s - nonzero urb status received: %d",
215 usb_serial_debug_data(debug, &port->dev, __func__,
216 urb->actual_length, data);
218 /* Handle known interrupt data */
219 /* ignore data[0] and data[1] */
221 priv = usb_get_serial_port_data(port);
222 spin_lock_irqsave(&priv->lock, flags);
223 priv->last_msr = data[BELKIN_SA_MSR_INDEX];
225 /* Record Control Line states */
226 if (priv->last_msr & BELKIN_SA_MSR_DSR)
227 priv->control_state |= TIOCM_DSR;
229 priv->control_state &= ~TIOCM_DSR;
231 if (priv->last_msr & BELKIN_SA_MSR_CTS)
232 priv->control_state |= TIOCM_CTS;
234 priv->control_state &= ~TIOCM_CTS;
236 if (priv->last_msr & BELKIN_SA_MSR_RI)
237 priv->control_state |= TIOCM_RI;
239 priv->control_state &= ~TIOCM_RI;
241 if (priv->last_msr & BELKIN_SA_MSR_CD)
242 priv->control_state |= TIOCM_CD;
244 priv->control_state &= ~TIOCM_CD;
246 priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
247 spin_unlock_irqrestore(&priv->lock, flags);
249 retval = usb_submit_urb(urb, GFP_ATOMIC);
251 dev_err(&port->dev, "%s - usb_submit_urb failed with "
252 "result %d\n", __func__, retval);
255 static void belkin_sa_process_read_urb(struct urb *urb)
257 struct usb_serial_port *port = urb->context;
258 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
259 struct tty_struct *tty;
260 unsigned char *data = urb->transfer_buffer;
262 unsigned char status;
265 /* Update line status */
266 tty_flag = TTY_NORMAL;
268 spin_lock_irqsave(&priv->lock, flags);
269 status = priv->last_lsr;
270 priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
271 spin_unlock_irqrestore(&priv->lock, flags);
273 if (!urb->actual_length)
276 tty = tty_port_tty_get(&port->port);
280 if (status & BELKIN_SA_LSR_ERR) {
281 /* Break takes precedence over parity, which takes precedence
282 * over framing errors. */
283 if (status & BELKIN_SA_LSR_BI)
284 tty_flag = TTY_BREAK;
285 else if (status & BELKIN_SA_LSR_PE)
286 tty_flag = TTY_PARITY;
287 else if (status & BELKIN_SA_LSR_FE)
288 tty_flag = TTY_FRAME;
289 dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
291 /* Overrun is special, not associated with a char. */
292 if (status & BELKIN_SA_LSR_OE)
293 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
296 tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
298 tty_flip_buffer_push(tty);
302 static void belkin_sa_set_termios(struct tty_struct *tty,
303 struct usb_serial_port *port, struct ktermios *old_termios)
305 struct usb_serial *serial = port->serial;
306 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
309 unsigned int old_iflag = 0;
310 unsigned int old_cflag = 0;
311 __u16 urb_value = 0; /* Will hold the new flags */
313 unsigned long control_state;
314 int bad_flow_control;
316 struct ktermios *termios = tty->termios;
318 iflag = termios->c_iflag;
319 cflag = termios->c_cflag;
321 termios->c_cflag &= ~CMSPAR;
323 /* get a local copy of the current port settings */
324 spin_lock_irqsave(&priv->lock, flags);
325 control_state = priv->control_state;
326 bad_flow_control = priv->bad_flow_control;
327 spin_unlock_irqrestore(&priv->lock, flags);
329 old_iflag = old_termios->c_iflag;
330 old_cflag = old_termios->c_cflag;
332 /* Set the baud rate */
333 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
334 /* reassert DTR and (maybe) RTS on transition from B0 */
335 if ((old_cflag & CBAUD) == B0) {
336 control_state |= (TIOCM_DTR|TIOCM_RTS);
337 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
338 dev_err(&port->dev, "Set DTR error\n");
339 /* don't set RTS if using hardware flow control */
340 if (!(old_cflag & CRTSCTS))
341 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
343 dev_err(&port->dev, "Set RTS error\n");
347 baud = tty_get_baud_rate(tty);
349 urb_value = BELKIN_SA_BAUD(baud);
350 /* Clip to maximum speed */
353 /* Turn it back into a resulting real baud rate */
354 baud = BELKIN_SA_BAUD(urb_value);
356 /* Report the actual baud rate back to the caller */
357 tty_encode_baud_rate(tty, baud, baud);
358 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
359 dev_err(&port->dev, "Set baudrate error\n");
361 /* Disable flow control */
362 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
363 BELKIN_SA_FLOW_NONE) < 0)
364 dev_err(&port->dev, "Disable flowcontrol error\n");
365 /* Drop RTS and DTR */
366 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
367 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
368 dev_err(&port->dev, "DTR LOW error\n");
369 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
370 dev_err(&port->dev, "RTS LOW error\n");
374 if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
376 urb_value = (cflag & PARODD) ? BELKIN_SA_PARITY_ODD
377 : BELKIN_SA_PARITY_EVEN;
379 urb_value = BELKIN_SA_PARITY_NONE;
380 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
381 dev_err(&port->dev, "Set parity error\n");
384 /* set the number of data bits */
385 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
386 switch (cflag & CSIZE) {
388 urb_value = BELKIN_SA_DATA_BITS(5);
391 urb_value = BELKIN_SA_DATA_BITS(6);
394 urb_value = BELKIN_SA_DATA_BITS(7);
397 urb_value = BELKIN_SA_DATA_BITS(8);
400 dbg("CSIZE was not CS5-CS8, using default of 8");
401 urb_value = BELKIN_SA_DATA_BITS(8);
404 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
405 dev_err(&port->dev, "Set data bits error\n");
408 /* set the number of stop bits */
409 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
410 urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
411 : BELKIN_SA_STOP_BITS(1);
412 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
414 dev_err(&port->dev, "Set stop bits error\n");
417 /* Set flow control */
418 if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
419 ((cflag ^ old_cflag) & CRTSCTS)) {
421 if ((iflag & IXOFF) || (iflag & IXON))
422 urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
424 urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
427 urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
429 urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
431 if (bad_flow_control)
432 urb_value &= ~(BELKIN_SA_FLOW_IRTS);
434 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
435 dev_err(&port->dev, "Set flow control error\n");
438 /* save off the modified port settings */
439 spin_lock_irqsave(&priv->lock, flags);
440 priv->control_state = control_state;
441 spin_unlock_irqrestore(&priv->lock, flags);
444 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
446 struct usb_serial_port *port = tty->driver_data;
447 struct usb_serial *serial = port->serial;
449 if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
450 dev_err(&port->dev, "Set break_ctl %d\n", break_state);
453 static int belkin_sa_tiocmget(struct tty_struct *tty)
455 struct usb_serial_port *port = tty->driver_data;
456 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
457 unsigned long control_state;
460 spin_lock_irqsave(&priv->lock, flags);
461 control_state = priv->control_state;
462 spin_unlock_irqrestore(&priv->lock, flags);
464 return control_state;
467 static int belkin_sa_tiocmset(struct tty_struct *tty,
468 unsigned int set, unsigned int clear)
470 struct usb_serial_port *port = tty->driver_data;
471 struct usb_serial *serial = port->serial;
472 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
473 unsigned long control_state;
479 spin_lock_irqsave(&priv->lock, flags);
480 control_state = priv->control_state;
482 if (set & TIOCM_RTS) {
483 control_state |= TIOCM_RTS;
486 if (set & TIOCM_DTR) {
487 control_state |= TIOCM_DTR;
490 if (clear & TIOCM_RTS) {
491 control_state &= ~TIOCM_RTS;
494 if (clear & TIOCM_DTR) {
495 control_state &= ~TIOCM_DTR;
499 priv->control_state = control_state;
500 spin_unlock_irqrestore(&priv->lock, flags);
502 retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
504 dev_err(&port->dev, "Set RTS error %d\n", retval);
508 retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
510 dev_err(&port->dev, "Set DTR error %d\n", retval);
517 module_usb_serial_driver(belkin_driver, serial_drivers);
519 MODULE_AUTHOR(DRIVER_AUTHOR);
520 MODULE_DESCRIPTION(DRIVER_DESC);
521 MODULE_VERSION(DRIVER_VERSION);
522 MODULE_LICENSE("GPL");
524 module_param(debug, bool, S_IRUGO | S_IWUSR);
525 MODULE_PARM_DESC(debug, "Debug enabled or not");