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 driver
19 * -- Add true modem contol line query capability. Currently we track the
20 * states reported by the interrupt and the states we request.
21 * -- Add error reporting back to application for UART error conditions.
22 * Just point me at how to implement this and I'll do it. I've put the
23 * framework in, but haven't analyzed the "tty_flip" interface yet.
24 * -- Add support for flush commands
25 * -- Add everything that is missing :)
28 * compressed all the differnent device entries into 1.
31 * switched from using spinlock to a semaphore, which fixes lots of problems.
34 * - Identify version on module load.
37 * - Added support for the GoHubs GO-COM232 device which is the same as the
41 * - Added support for the old Belkin and Peracom devices.
42 * - Made the port able to be opened multiple times.
43 * - Added some defaults incase the line settings are things these devices
46 * 18-Oct-2000 William Greathouse
47 * Released into the wild (linux-usb-devel)
49 * 17-Oct-2000 William Greathouse
50 * Add code to recognize firmware version and set hardware flow control
51 * appropriately. Belkin states that firmware prior to 3.05 does not
52 * operate correctly in hardware handshake mode. I have verified this
53 * on firmware 2.05 -- for both RTS and DTR input flow control, the control
54 * line is not reset. The test performed by the Belkin Win* driver is
55 * to enable hardware flow control for firmware 2.06 or greater and
56 * for 1.00 or prior. I am only enabling for 2.06 or greater.
58 * 12-Oct-2000 William Greathouse
59 * First cut at supporting Belkin USB Serial Adapter F5U103
60 * I did not have a copy of the original work to support this
61 * adapter, so pardon any stupid mistakes. All of the information
62 * I am using to write this driver was acquired by using a modified
63 * UsbSnoop on Windows2000 and from examining the other USB drivers.
66 #include <linux/kernel.h>
67 #include <linux/errno.h>
68 #include <linux/init.h>
69 #include <linux/slab.h>
70 #include <linux/tty.h>
71 #include <linux/tty_driver.h>
72 #include <linux/tty_flip.h>
73 #include <linux/module.h>
74 #include <linux/spinlock.h>
75 #include <asm/uaccess.h>
76 #include <linux/usb.h>
77 #include <linux/usb/serial.h>
78 #include "belkin_sa.h"
85 #define DRIVER_VERSION "v1.2"
86 #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
87 #define DRIVER_DESC "USB Belkin Serial converter driver"
89 /* function prototypes for a Belkin USB Serial Adapter F5U103 */
90 static int belkin_sa_startup (struct usb_serial
*serial
);
91 static void belkin_sa_shutdown (struct usb_serial
*serial
);
92 static int belkin_sa_open (struct usb_serial_port
*port
, struct file
*filp
);
93 static void belkin_sa_close (struct usb_serial_port
*port
, struct file
*filp
);
94 static void belkin_sa_read_int_callback (struct urb
*urb
);
95 static void belkin_sa_set_termios (struct usb_serial_port
*port
, struct ktermios
* old
);
96 static int belkin_sa_ioctl (struct usb_serial_port
*port
, struct file
* file
, unsigned int cmd
, unsigned long arg
);
97 static void belkin_sa_break_ctl (struct usb_serial_port
*port
, int break_state
);
98 static int belkin_sa_tiocmget (struct usb_serial_port
*port
, struct file
*file
);
99 static int belkin_sa_tiocmset (struct usb_serial_port
*port
, struct file
*file
, unsigned int set
, unsigned int clear
);
102 static struct usb_device_id id_table_combined
[] = {
103 { USB_DEVICE(BELKIN_SA_VID
, BELKIN_SA_PID
) },
104 { USB_DEVICE(BELKIN_OLD_VID
, BELKIN_OLD_PID
) },
105 { USB_DEVICE(PERACOM_VID
, PERACOM_PID
) },
106 { USB_DEVICE(GOHUBS_VID
, GOHUBS_PID
) },
107 { USB_DEVICE(GOHUBS_VID
, HANDYLINK_PID
) },
108 { USB_DEVICE(BELKIN_DOCKSTATION_VID
, BELKIN_DOCKSTATION_PID
) },
109 { } /* Terminating entry */
112 MODULE_DEVICE_TABLE (usb
, id_table_combined
);
114 static struct usb_driver belkin_driver
= {
116 .probe
= usb_serial_probe
,
117 .disconnect
= usb_serial_disconnect
,
118 .id_table
= id_table_combined
,
122 /* All of the device info needed for the serial converters */
123 static struct usb_serial_driver belkin_device
= {
125 .owner
= THIS_MODULE
,
128 .description
= "Belkin / Peracom / GoHubs USB Serial Adapter",
129 .usb_driver
= &belkin_driver
,
130 .id_table
= id_table_combined
,
131 .num_interrupt_in
= 1,
135 .open
= belkin_sa_open
,
136 .close
= belkin_sa_close
,
137 .read_int_callback
= belkin_sa_read_int_callback
, /* How we get the status info */
138 .ioctl
= belkin_sa_ioctl
,
139 .set_termios
= belkin_sa_set_termios
,
140 .break_ctl
= belkin_sa_break_ctl
,
141 .tiocmget
= belkin_sa_tiocmget
,
142 .tiocmset
= belkin_sa_tiocmset
,
143 .attach
= belkin_sa_startup
,
144 .shutdown
= belkin_sa_shutdown
,
148 struct belkin_sa_private
{
150 unsigned long control_state
;
151 unsigned char last_lsr
;
152 unsigned char last_msr
;
153 int bad_flow_control
;
158 * ***************************************************************************
159 * Belkin USB Serial Adapter F5U103 specific driver functions
160 * ***************************************************************************
163 #define WDR_TIMEOUT 5000 /* default urb timeout */
165 /* assumes that struct usb_serial *serial is available */
166 #define BSA_USB_CMD(c,v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
167 (c), BELKIN_SA_SET_REQUEST_TYPE, \
168 (v), 0, NULL, 0, WDR_TIMEOUT)
170 /* do some startup allocations not currently performed by usb_serial_probe() */
171 static int belkin_sa_startup (struct usb_serial
*serial
)
173 struct usb_device
*dev
= serial
->dev
;
174 struct belkin_sa_private
*priv
;
176 /* allocate the private data structure */
177 priv
= kmalloc(sizeof(struct belkin_sa_private
), GFP_KERNEL
);
179 return (-1); /* error */
180 /* set initial values for control structures */
181 spin_lock_init(&priv
->lock
);
182 priv
->control_state
= 0;
185 /* see comments at top of file */
186 priv
->bad_flow_control
= (le16_to_cpu(dev
->descriptor
.bcdDevice
) <= 0x0206) ? 1 : 0;
187 info("bcdDevice: %04x, bfc: %d", le16_to_cpu(dev
->descriptor
.bcdDevice
), priv
->bad_flow_control
);
189 init_waitqueue_head(&serial
->port
[0]->write_wait
);
190 usb_set_serial_port_data(serial
->port
[0], priv
);
196 static void belkin_sa_shutdown (struct usb_serial
*serial
)
198 struct belkin_sa_private
*priv
;
201 dbg ("%s", __FUNCTION__
);
203 /* stop reads and writes on all ports */
204 for (i
=0; i
< serial
->num_ports
; ++i
) {
205 /* My special items, the standard routines free my urbs */
206 priv
= usb_get_serial_port_data(serial
->port
[i
]);
212 static int belkin_sa_open (struct usb_serial_port
*port
, struct file
*filp
)
216 dbg("%s port %d", __FUNCTION__
, port
->number
);
218 /*Start reading from the device*/
219 /* TODO: Look at possibility of submitting multiple URBs to device to
220 * enhance buffering. Win trace shows 16 initial read URBs.
222 port
->read_urb
->dev
= port
->serial
->dev
;
223 retval
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
225 err("usb_submit_urb(read bulk) failed");
229 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
230 retval
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
232 usb_kill_urb(port
->read_urb
);
233 err(" usb_submit_urb(read int) failed");
238 } /* belkin_sa_open */
241 static void belkin_sa_close (struct usb_serial_port
*port
, struct file
*filp
)
243 dbg("%s port %d", __FUNCTION__
, port
->number
);
245 /* shutdown our bulk reads and writes */
246 usb_kill_urb(port
->write_urb
);
247 usb_kill_urb(port
->read_urb
);
248 usb_kill_urb(port
->interrupt_in_urb
);
249 } /* belkin_sa_close */
252 static void belkin_sa_read_int_callback (struct urb
*urb
)
254 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
255 struct belkin_sa_private
*priv
;
256 unsigned char *data
= urb
->transfer_buffer
;
258 int status
= urb
->status
;
268 /* this urb is terminated, clean up */
269 dbg("%s - urb shutting down with status: %d",
270 __FUNCTION__
, status
);
273 dbg("%s - nonzero urb status received: %d",
274 __FUNCTION__
, status
);
278 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
, urb
->actual_length
, data
);
280 /* Handle known interrupt data */
281 /* ignore data[0] and data[1] */
283 priv
= usb_get_serial_port_data(port
);
284 spin_lock_irqsave(&priv
->lock
, flags
);
285 priv
->last_msr
= data
[BELKIN_SA_MSR_INDEX
];
287 /* Record Control Line states */
288 if (priv
->last_msr
& BELKIN_SA_MSR_DSR
)
289 priv
->control_state
|= TIOCM_DSR
;
291 priv
->control_state
&= ~TIOCM_DSR
;
293 if (priv
->last_msr
& BELKIN_SA_MSR_CTS
)
294 priv
->control_state
|= TIOCM_CTS
;
296 priv
->control_state
&= ~TIOCM_CTS
;
298 if (priv
->last_msr
& BELKIN_SA_MSR_RI
)
299 priv
->control_state
|= TIOCM_RI
;
301 priv
->control_state
&= ~TIOCM_RI
;
303 if (priv
->last_msr
& BELKIN_SA_MSR_CD
)
304 priv
->control_state
|= TIOCM_CD
;
306 priv
->control_state
&= ~TIOCM_CD
;
308 /* Now to report any errors */
309 priv
->last_lsr
= data
[BELKIN_SA_LSR_INDEX
];
312 * fill in the flip buffer here, but I do not know the relation
313 * to the current/next receive buffer or characters. I need
314 * to look in to this before committing any code.
316 if (priv
->last_lsr
& BELKIN_SA_LSR_ERR
) {
319 if (priv
->last_lsr
& BELKIN_SA_LSR_OE
) {
322 if (priv
->last_lsr
& BELKIN_SA_LSR_PE
) {
325 if (priv
->last_lsr
& BELKIN_SA_LSR_FE
) {
327 /* Break Indicator */
328 if (priv
->last_lsr
& BELKIN_SA_LSR_BI
) {
332 spin_unlock_irqrestore(&priv
->lock
, flags
);
334 retval
= usb_submit_urb (urb
, GFP_ATOMIC
);
336 err ("%s - usb_submit_urb failed with result %d",
337 __FUNCTION__
, retval
);
340 static void belkin_sa_set_termios (struct usb_serial_port
*port
, struct ktermios
*old_termios
)
342 struct usb_serial
*serial
= port
->serial
;
343 struct belkin_sa_private
*priv
= usb_get_serial_port_data(port
);
346 unsigned int old_iflag
= 0;
347 unsigned int old_cflag
= 0;
348 __u16 urb_value
= 0; /* Will hold the new flags */
350 unsigned long control_state
;
351 int bad_flow_control
;
353 if ((!port
->tty
) || (!port
->tty
->termios
)) {
354 dbg ("%s - no tty or termios structure", __FUNCTION__
);
358 iflag
= port
->tty
->termios
->c_iflag
;
359 cflag
= port
->tty
->termios
->c_cflag
;
361 /* get a local copy of the current port settings */
362 spin_lock_irqsave(&priv
->lock
, flags
);
363 control_state
= priv
->control_state
;
364 bad_flow_control
= priv
->bad_flow_control
;
365 spin_unlock_irqrestore(&priv
->lock
, flags
);
367 /* check that they really want us to change something */
369 if ((cflag
== old_termios
->c_cflag
) &&
370 (RELEVANT_IFLAG(port
->tty
->termios
->c_iflag
) == RELEVANT_IFLAG(old_termios
->c_iflag
))) {
371 dbg("%s - nothing to change...", __FUNCTION__
);
374 old_iflag
= old_termios
->c_iflag
;
375 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
, 1) < 0)
388 err("Set RTS error");
391 switch(cflag
& CBAUD
) {
392 case B0
: /* handled below */ break;
393 case B300
: urb_value
= BELKIN_SA_BAUD(300); break;
394 case B600
: urb_value
= BELKIN_SA_BAUD(600); break;
395 case B1200
: urb_value
= BELKIN_SA_BAUD(1200); break;
396 case B2400
: urb_value
= BELKIN_SA_BAUD(2400); break;
397 case B4800
: urb_value
= BELKIN_SA_BAUD(4800); break;
398 case B9600
: urb_value
= BELKIN_SA_BAUD(9600); break;
399 case B19200
: urb_value
= BELKIN_SA_BAUD(19200); break;
400 case B38400
: urb_value
= BELKIN_SA_BAUD(38400); break;
401 case B57600
: urb_value
= BELKIN_SA_BAUD(57600); break;
402 case B115200
: urb_value
= BELKIN_SA_BAUD(115200); break;
403 case B230400
: urb_value
= BELKIN_SA_BAUD(230400); break;
404 default: err("BELKIN USB Serial Adapter: unsupported baudrate request, using default of 9600");
405 urb_value
= BELKIN_SA_BAUD(9600); break;
407 if ((cflag
& CBAUD
) != B0
) {
408 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST
, urb_value
) < 0)
409 err("Set baudrate error");
411 /* Disable flow control */
412 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST
, BELKIN_SA_FLOW_NONE
) < 0)
413 err("Disable flowcontrol error");
415 /* Drop RTS and DTR */
416 control_state
&= ~(TIOCM_DTR
| TIOCM_RTS
);
417 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST
, 0) < 0)
418 err("DTR LOW error");
419 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
, 0) < 0)
420 err("RTS LOW error");
425 if( (cflag
&(PARENB
|PARODD
)) != (old_cflag
&(PARENB
|PARODD
)) ) {
427 urb_value
= (cflag
& PARODD
) ? BELKIN_SA_PARITY_ODD
: BELKIN_SA_PARITY_EVEN
;
429 urb_value
= BELKIN_SA_PARITY_NONE
;
430 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST
, urb_value
) < 0)
431 err("Set parity error");
434 /* set the number of data bits */
435 if( (cflag
&CSIZE
) != (old_cflag
&CSIZE
) ) {
436 switch (cflag
& CSIZE
) {
437 case CS5
: urb_value
= BELKIN_SA_DATA_BITS(5); break;
438 case CS6
: urb_value
= BELKIN_SA_DATA_BITS(6); break;
439 case CS7
: urb_value
= BELKIN_SA_DATA_BITS(7); break;
440 case CS8
: urb_value
= BELKIN_SA_DATA_BITS(8); break;
441 default: err("CSIZE was not CS5-CS8, using default of 8");
442 urb_value
= BELKIN_SA_DATA_BITS(8);
445 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST
, urb_value
) < 0)
446 err("Set data bits error");
449 /* set the number of stop bits */
450 if( (cflag
&CSTOPB
) != (old_cflag
&CSTOPB
) ) {
451 urb_value
= (cflag
& CSTOPB
) ? BELKIN_SA_STOP_BITS(2) : BELKIN_SA_STOP_BITS(1);
452 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST
, urb_value
) < 0)
453 err("Set stop bits error");
456 /* Set flow control */
457 if( (iflag
&IXOFF
) != (old_iflag
&IXOFF
)
458 || (iflag
&IXON
) != (old_iflag
&IXON
)
459 || (cflag
&CRTSCTS
) != (old_cflag
&CRTSCTS
) ) {
461 if ((iflag
& IXOFF
) || (iflag
& IXON
))
462 urb_value
|= (BELKIN_SA_FLOW_OXON
| BELKIN_SA_FLOW_IXON
);
464 urb_value
&= ~(BELKIN_SA_FLOW_OXON
| BELKIN_SA_FLOW_IXON
);
467 urb_value
|= (BELKIN_SA_FLOW_OCTS
| BELKIN_SA_FLOW_IRTS
);
469 urb_value
&= ~(BELKIN_SA_FLOW_OCTS
| BELKIN_SA_FLOW_IRTS
);
471 if (bad_flow_control
)
472 urb_value
&= ~(BELKIN_SA_FLOW_IRTS
);
474 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST
, urb_value
) < 0)
475 err("Set flow control error");
478 /* save off the modified port settings */
479 spin_lock_irqsave(&priv
->lock
, flags
);
480 priv
->control_state
= control_state
;
481 spin_unlock_irqrestore(&priv
->lock
, flags
);
482 } /* belkin_sa_set_termios */
485 static void belkin_sa_break_ctl( struct usb_serial_port
*port
, int break_state
)
487 struct usb_serial
*serial
= port
->serial
;
489 if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST
, break_state
? 1 : 0) < 0)
490 err("Set break_ctl %d", break_state
);
494 static int belkin_sa_tiocmget (struct usb_serial_port
*port
, struct file
*file
)
496 struct belkin_sa_private
*priv
= usb_get_serial_port_data(port
);
497 unsigned long control_state
;
500 dbg("%s", __FUNCTION__
);
502 spin_lock_irqsave(&priv
->lock
, flags
);
503 control_state
= priv
->control_state
;
504 spin_unlock_irqrestore(&priv
->lock
, flags
);
506 return control_state
;
510 static int belkin_sa_tiocmset (struct usb_serial_port
*port
, struct file
*file
,
511 unsigned int set
, unsigned int clear
)
513 struct usb_serial
*serial
= port
->serial
;
514 struct belkin_sa_private
*priv
= usb_get_serial_port_data(port
);
515 unsigned long control_state
;
521 dbg("%s", __FUNCTION__
);
523 spin_lock_irqsave(&priv
->lock
, flags
);
524 control_state
= priv
->control_state
;
526 if (set
& TIOCM_RTS
) {
527 control_state
|= TIOCM_RTS
;
530 if (set
& TIOCM_DTR
) {
531 control_state
|= TIOCM_DTR
;
534 if (clear
& TIOCM_RTS
) {
535 control_state
&= ~TIOCM_RTS
;
538 if (clear
& TIOCM_DTR
) {
539 control_state
&= ~TIOCM_DTR
;
543 priv
->control_state
= control_state
;
544 spin_unlock_irqrestore(&priv
->lock
, flags
);
546 retval
= BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
, rts
);
548 err("Set RTS error %d", retval
);
552 retval
= BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST
, dtr
);
554 err("Set DTR error %d", retval
);
562 static int belkin_sa_ioctl (struct usb_serial_port
*port
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
566 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
571 /* return count of modemline transitions */
576 dbg("belkin_sa_ioctl arg not supported - 0x%04x",cmd
);
577 return(-ENOIOCTLCMD
);
581 } /* belkin_sa_ioctl */
584 static int __init
belkin_sa_init (void)
587 retval
= usb_serial_register(&belkin_device
);
589 goto failed_usb_serial_register
;
590 retval
= usb_register(&belkin_driver
);
592 goto failed_usb_register
;
593 info(DRIVER_DESC
" " DRIVER_VERSION
);
596 usb_serial_deregister(&belkin_device
);
597 failed_usb_serial_register
:
602 static void __exit
belkin_sa_exit (void)
604 usb_deregister (&belkin_driver
);
605 usb_serial_deregister (&belkin_device
);
609 module_init (belkin_sa_init
);
610 module_exit (belkin_sa_exit
);
612 MODULE_AUTHOR( DRIVER_AUTHOR
);
613 MODULE_DESCRIPTION( DRIVER_DESC
);
614 MODULE_VERSION( DRIVER_VERSION
);
615 MODULE_LICENSE("GPL");
617 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
618 MODULE_PARM_DESC(debug
, "Debug enabled or not");