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)
7 * This program is largely derived from work by the linux-usb group
8 * and associated source files. Please see the usb/serial files for
9 * individual credits and copyrights.
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
14 * (at your option) any later version.
16 * See Documentation/usb/usb-serial.txt for more information on using this
20 * -- Add true modem contol line query capability. Currently we track the
21 * states reported by the interrupt and the states we request.
22 * -- Add error reporting back to application for UART error conditions.
23 * Just point me at how to implement this and I'll do it. I've put the
24 * framework in, but haven't analyzed the "tty_flip" interface yet.
25 * -- Add support for flush commands
26 * -- Add everything that is missing :)
29 * compressed all the differnent device entries into 1.
32 * switched from using spinlock to a semaphore, which fixes lots of
36 * - Identify version on module load.
39 * - Added support for the GoHubs GO-COM232 device which is the same as the
43 * - Added support for the old Belkin and Peracom devices.
44 * - Made the port able to be opened multiple times.
45 * - Added some defaults incase the line settings are things these devices
48 * 18-Oct-2000 William Greathouse
49 * Released into the wild (linux-usb-devel)
51 * 17-Oct-2000 William Greathouse
52 * Add code to recognize firmware version and set hardware flow control
53 * appropriately. Belkin states that firmware prior to 3.05 does not
54 * operate correctly in hardware handshake mode. I have verified this
55 * on firmware 2.05 -- for both RTS and DTR input flow control, the control
56 * line is not reset. The test performed by the Belkin Win* driver is
57 * to enable hardware flow control for firmware 2.06 or greater and
58 * for 1.00 or prior. I am only enabling for 2.06 or greater.
60 * 12-Oct-2000 William Greathouse
61 * First cut at supporting Belkin USB Serial Adapter F5U103
62 * I did not have a copy of the original work to support this
63 * adapter, so pardon any stupid mistakes. All of the information
64 * I am using to write this driver was acquired by using a modified
65 * UsbSnoop on Windows2000 and from examining the other USB drivers.
68 #include <linux/kernel.h>
69 #include <linux/errno.h>
70 #include <linux/init.h>
71 #include <linux/slab.h>
72 #include <linux/tty.h>
73 #include <linux/tty_driver.h>
74 #include <linux/tty_flip.h>
75 #include <linux/module.h>
76 #include <linux/spinlock.h>
77 #include <linux/uaccess.h>
78 #include <linux/usb.h>
79 #include <linux/usb/serial.h>
80 #include "belkin_sa.h"
87 #define DRIVER_VERSION "v1.2"
88 #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
89 #define DRIVER_DESC "USB Belkin Serial converter driver"
91 /* function prototypes for a Belkin USB Serial Adapter F5U103 */
92 static int belkin_sa_startup(struct usb_serial *serial);
93 static void belkin_sa_shutdown(struct usb_serial *serial);
94 static int belkin_sa_open(struct tty_struct *tty,
95 struct usb_serial_port *port, struct file *filp);
96 static void belkin_sa_close(struct tty_struct *tty,
97 struct usb_serial_port *port, struct file *filp);
98 static void belkin_sa_read_int_callback(struct urb *urb);
99 static void belkin_sa_set_termios(struct tty_struct *tty,
100 struct usb_serial_port *port, struct ktermios * old);
101 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
102 static int belkin_sa_tiocmget(struct tty_struct *tty, struct file *file);
103 static int belkin_sa_tiocmset(struct tty_struct *tty, struct file *file,
104 unsigned int set, unsigned int clear);
107 static struct usb_device_id id_table_combined [] = {
108 { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
109 { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
110 { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
111 { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
112 { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
113 { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
114 { } /* Terminating entry */
117 MODULE_DEVICE_TABLE(usb, id_table_combined);
119 static struct usb_driver belkin_driver = {
121 .probe = usb_serial_probe,
122 .disconnect = usb_serial_disconnect,
123 .id_table = id_table_combined,
127 /* All of the device info needed for the serial converters */
128 static struct usb_serial_driver belkin_device = {
130 .owner = THIS_MODULE,
133 .description = "Belkin / Peracom / GoHubs USB Serial Adapter",
134 .usb_driver = &belkin_driver,
135 .id_table = id_table_combined,
137 .open = belkin_sa_open,
138 .close = belkin_sa_close,
139 .read_int_callback = belkin_sa_read_int_callback,
140 /* How we get the status info */
141 .set_termios = belkin_sa_set_termios,
142 .break_ctl = belkin_sa_break_ctl,
143 .tiocmget = belkin_sa_tiocmget,
144 .tiocmset = belkin_sa_tiocmset,
145 .attach = belkin_sa_startup,
146 .shutdown = belkin_sa_shutdown,
150 struct belkin_sa_private {
152 unsigned long control_state;
153 unsigned char last_lsr;
154 unsigned char last_msr;
155 int bad_flow_control;
160 * ***************************************************************************
161 * Belkin USB Serial Adapter F5U103 specific driver functions
162 * ***************************************************************************
165 #define WDR_TIMEOUT 5000 /* default urb timeout */
167 /* assumes that struct usb_serial *serial is available */
168 #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
169 (c), BELKIN_SA_SET_REQUEST_TYPE, \
170 (v), 0, NULL, 0, WDR_TIMEOUT)
172 /* do some startup allocations not currently performed by usb_serial_probe() */
173 static int belkin_sa_startup(struct usb_serial *serial)
175 struct usb_device *dev = serial->dev;
176 struct belkin_sa_private *priv;
178 /* allocate the private data structure */
179 priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
181 return -1; /* error */
182 /* set initial values for control structures */
183 spin_lock_init(&priv->lock);
184 priv->control_state = 0;
187 /* see comments at top of file */
188 priv->bad_flow_control =
189 (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
190 info("bcdDevice: %04x, bfc: %d",
191 le16_to_cpu(dev->descriptor.bcdDevice),
192 priv->bad_flow_control);
194 init_waitqueue_head(&serial->port[0]->write_wait);
195 usb_set_serial_port_data(serial->port[0], priv);
201 static void belkin_sa_shutdown(struct usb_serial *serial)
203 struct belkin_sa_private *priv;
208 /* stop reads and writes on all ports */
209 for (i = 0; i < serial->num_ports; ++i) {
210 /* My special items, the standard routines free my urbs */
211 priv = usb_get_serial_port_data(serial->port[i]);
217 static int belkin_sa_open(struct tty_struct *tty,
218 struct usb_serial_port *port, struct file *filp)
222 dbg("%s port %d", __func__, port->number);
224 /*Start reading from the device*/
225 /* TODO: Look at possibility of submitting multiple URBs to device to
226 * enhance buffering. Win trace shows 16 initial read URBs.
228 port->read_urb->dev = port->serial->dev;
229 retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
231 err("usb_submit_urb(read bulk) failed");
235 port->interrupt_in_urb->dev = port->serial->dev;
236 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
238 usb_kill_urb(port->read_urb);
239 err(" usb_submit_urb(read int) failed");
244 } /* belkin_sa_open */
247 static void belkin_sa_close(struct tty_struct *tty,
248 struct usb_serial_port *port, struct file *filp)
250 dbg("%s port %d", __func__, port->number);
252 /* shutdown our bulk reads and writes */
253 usb_kill_urb(port->write_urb);
254 usb_kill_urb(port->read_urb);
255 usb_kill_urb(port->interrupt_in_urb);
256 } /* belkin_sa_close */
259 static void belkin_sa_read_int_callback(struct urb *urb)
261 struct usb_serial_port *port = urb->context;
262 struct belkin_sa_private *priv;
263 unsigned char *data = urb->transfer_buffer;
265 int status = urb->status;
275 /* this urb is terminated, clean up */
276 dbg("%s - urb shutting down with status: %d",
280 dbg("%s - nonzero urb status received: %d",
285 usb_serial_debug_data(debug, &port->dev, __func__,
286 urb->actual_length, data);
288 /* Handle known interrupt data */
289 /* ignore data[0] and data[1] */
291 priv = usb_get_serial_port_data(port);
292 spin_lock_irqsave(&priv->lock, flags);
293 priv->last_msr = data[BELKIN_SA_MSR_INDEX];
295 /* Record Control Line states */
296 if (priv->last_msr & BELKIN_SA_MSR_DSR)
297 priv->control_state |= TIOCM_DSR;
299 priv->control_state &= ~TIOCM_DSR;
301 if (priv->last_msr & BELKIN_SA_MSR_CTS)
302 priv->control_state |= TIOCM_CTS;
304 priv->control_state &= ~TIOCM_CTS;
306 if (priv->last_msr & BELKIN_SA_MSR_RI)
307 priv->control_state |= TIOCM_RI;
309 priv->control_state &= ~TIOCM_RI;
311 if (priv->last_msr & BELKIN_SA_MSR_CD)
312 priv->control_state |= TIOCM_CD;
314 priv->control_state &= ~TIOCM_CD;
316 /* Now to report any errors */
317 priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
320 * fill in the flip buffer here, but I do not know the relation
321 * to the current/next receive buffer or characters. I need
322 * to look in to this before committing any code.
324 if (priv->last_lsr & BELKIN_SA_LSR_ERR) {
325 tty = port->port.tty;
327 if (priv->last_lsr & BELKIN_SA_LSR_OE) {
330 if (priv->last_lsr & BELKIN_SA_LSR_PE) {
333 if (priv->last_lsr & BELKIN_SA_LSR_FE) {
335 /* Break Indicator */
336 if (priv->last_lsr & BELKIN_SA_LSR_BI) {
340 spin_unlock_irqrestore(&priv->lock, flags);
342 retval = usb_submit_urb(urb, GFP_ATOMIC);
344 err("%s - usb_submit_urb failed with result %d",
348 static void belkin_sa_set_termios(struct tty_struct *tty,
349 struct usb_serial_port *port, struct ktermios *old_termios)
351 struct usb_serial *serial = port->serial;
352 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
355 unsigned int old_iflag = 0;
356 unsigned int old_cflag = 0;
357 __u16 urb_value = 0; /* Will hold the new flags */
359 unsigned long control_state;
360 int bad_flow_control;
362 struct ktermios *termios = tty->termios;
364 iflag = termios->c_iflag;
365 cflag = termios->c_cflag;
367 termios->c_cflag &= ~CMSPAR;
369 /* get a local copy of the current port settings */
370 spin_lock_irqsave(&priv->lock, flags);
371 control_state = priv->control_state;
372 bad_flow_control = priv->bad_flow_control;
373 spin_unlock_irqrestore(&priv->lock, flags);
375 old_iflag = old_termios->c_iflag;
376 old_cflag = old_termios->c_cflag;
378 /* Set the baud rate */
379 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
380 /* reassert DTR and (maybe) RTS on transition from B0 */
381 if ((old_cflag & CBAUD) == B0) {
382 control_state |= (TIOCM_DTR|TIOCM_RTS);
383 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
384 err("Set DTR error");
385 /* don't set RTS if using hardware flow control */
386 if (!(old_cflag & CRTSCTS))
387 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
389 err("Set RTS error");
393 baud = tty_get_baud_rate(tty);
395 urb_value = BELKIN_SA_BAUD(baud);
396 /* Clip to maximum speed */
399 /* Turn it back into a resulting real baud rate */
400 baud = BELKIN_SA_BAUD(urb_value);
402 /* Report the actual baud rate back to the caller */
403 tty_encode_baud_rate(tty, baud, baud);
404 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
405 err("Set baudrate error");
407 /* Disable flow control */
408 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
409 BELKIN_SA_FLOW_NONE) < 0)
410 err("Disable flowcontrol error");
411 /* Drop RTS and DTR */
412 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
413 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
414 err("DTR LOW error");
415 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
416 err("RTS LOW error");
420 if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
422 urb_value = (cflag & PARODD) ? BELKIN_SA_PARITY_ODD
423 : BELKIN_SA_PARITY_EVEN;
425 urb_value = BELKIN_SA_PARITY_NONE;
426 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
427 err("Set parity error");
430 /* set the number of data bits */
431 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
432 switch (cflag & CSIZE) {
434 urb_value = BELKIN_SA_DATA_BITS(5);
437 urb_value = BELKIN_SA_DATA_BITS(6);
440 urb_value = BELKIN_SA_DATA_BITS(7);
443 urb_value = BELKIN_SA_DATA_BITS(8);
445 default: dbg("CSIZE was not CS5-CS8, using default of 8");
446 urb_value = BELKIN_SA_DATA_BITS(8);
449 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
450 err("Set data bits error");
453 /* set the number of stop bits */
454 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
455 urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
456 : BELKIN_SA_STOP_BITS(1);
457 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
459 err("Set stop bits error");
462 /* Set flow control */
463 if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
464 ((cflag ^ old_cflag) & CRTSCTS)) {
466 if ((iflag & IXOFF) || (iflag & IXON))
467 urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
469 urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
472 urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
474 urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
476 if (bad_flow_control)
477 urb_value &= ~(BELKIN_SA_FLOW_IRTS);
479 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
480 err("Set flow control error");
483 /* save off the modified port settings */
484 spin_lock_irqsave(&priv->lock, flags);
485 priv->control_state = control_state;
486 spin_unlock_irqrestore(&priv->lock, flags);
487 } /* belkin_sa_set_termios */
490 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
492 struct usb_serial_port *port = tty->driver_data;
493 struct usb_serial *serial = port->serial;
495 if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
496 err("Set break_ctl %d", break_state);
500 static int belkin_sa_tiocmget(struct tty_struct *tty, struct file *file)
502 struct usb_serial_port *port = tty->driver_data;
503 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
504 unsigned long control_state;
509 spin_lock_irqsave(&priv->lock, flags);
510 control_state = priv->control_state;
511 spin_unlock_irqrestore(&priv->lock, flags);
513 return control_state;
517 static int belkin_sa_tiocmset(struct tty_struct *tty, struct file *file,
518 unsigned int set, unsigned int clear)
520 struct usb_serial_port *port = tty->driver_data;
521 struct usb_serial *serial = port->serial;
522 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
523 unsigned long control_state;
531 spin_lock_irqsave(&priv->lock, flags);
532 control_state = priv->control_state;
534 if (set & TIOCM_RTS) {
535 control_state |= TIOCM_RTS;
538 if (set & TIOCM_DTR) {
539 control_state |= TIOCM_DTR;
542 if (clear & TIOCM_RTS) {
543 control_state &= ~TIOCM_RTS;
546 if (clear & TIOCM_DTR) {
547 control_state &= ~TIOCM_DTR;
551 priv->control_state = control_state;
552 spin_unlock_irqrestore(&priv->lock, flags);
554 retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
556 err("Set RTS error %d", retval);
560 retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
562 err("Set DTR error %d", retval);
570 static int __init belkin_sa_init(void)
573 retval = usb_serial_register(&belkin_device);
575 goto failed_usb_serial_register;
576 retval = usb_register(&belkin_driver);
578 goto failed_usb_register;
579 info(DRIVER_DESC " " DRIVER_VERSION);
582 usb_serial_deregister(&belkin_device);
583 failed_usb_serial_register:
588 static void __exit belkin_sa_exit (void)
590 usb_deregister(&belkin_driver);
591 usb_serial_deregister(&belkin_device);
595 module_init(belkin_sa_init);
596 module_exit(belkin_sa_exit);
598 MODULE_AUTHOR(DRIVER_AUTHOR);
599 MODULE_DESCRIPTION(DRIVER_DESC);
600 MODULE_VERSION(DRIVER_VERSION);
601 MODULE_LICENSE("GPL");
603 module_param(debug, bool, S_IRUGO | S_IWUSR);
604 MODULE_PARM_DESC(debug, "Debug enabled or not");