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_release(struct usb_serial
*serial
);
94 static int belkin_sa_open(struct tty_struct
*tty
,
95 struct usb_serial_port
*port
);
96 static void belkin_sa_close(struct usb_serial_port
*port
);
97 static void belkin_sa_read_int_callback(struct urb
*urb
);
98 static void belkin_sa_set_termios(struct tty_struct
*tty
,
99 struct usb_serial_port
*port
, struct ktermios
* old
);
100 static void belkin_sa_break_ctl(struct tty_struct
*tty
, int break_state
);
101 static int belkin_sa_tiocmget(struct tty_struct
*tty
, struct file
*file
);
102 static int belkin_sa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
103 unsigned int set
, unsigned int clear
);
106 static const struct usb_device_id id_table_combined
[] = {
107 { USB_DEVICE(BELKIN_SA_VID
, BELKIN_SA_PID
) },
108 { USB_DEVICE(BELKIN_OLD_VID
, BELKIN_OLD_PID
) },
109 { USB_DEVICE(PERACOM_VID
, PERACOM_PID
) },
110 { USB_DEVICE(GOHUBS_VID
, GOHUBS_PID
) },
111 { USB_DEVICE(GOHUBS_VID
, HANDYLINK_PID
) },
112 { USB_DEVICE(BELKIN_DOCKSTATION_VID
, BELKIN_DOCKSTATION_PID
) },
113 { } /* Terminating entry */
115 MODULE_DEVICE_TABLE(usb
, id_table_combined
);
117 static struct usb_driver belkin_driver
= {
119 .probe
= usb_serial_probe
,
120 .disconnect
= usb_serial_disconnect
,
121 .id_table
= id_table_combined
,
125 /* All of the device info needed for the serial converters */
126 static struct usb_serial_driver belkin_device
= {
128 .owner
= THIS_MODULE
,
131 .description
= "Belkin / Peracom / GoHubs USB Serial Adapter",
132 .usb_driver
= &belkin_driver
,
133 .id_table
= id_table_combined
,
135 .open
= belkin_sa_open
,
136 .close
= belkin_sa_close
,
137 .read_int_callback
= belkin_sa_read_int_callback
,
138 /* How we get the status info */
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 .release
= belkin_sa_release
,
147 struct belkin_sa_private
{
149 unsigned long control_state
;
150 unsigned char last_lsr
;
151 unsigned char last_msr
;
152 int bad_flow_control
;
157 * ***************************************************************************
158 * Belkin USB Serial Adapter F5U103 specific driver functions
159 * ***************************************************************************
162 #define WDR_TIMEOUT 5000 /* default urb timeout */
164 /* assumes that struct usb_serial *serial is available */
165 #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
166 (c), BELKIN_SA_SET_REQUEST_TYPE, \
167 (v), 0, NULL, 0, WDR_TIMEOUT)
169 /* do some startup allocations not currently performed by usb_serial_probe() */
170 static int belkin_sa_startup(struct usb_serial
*serial
)
172 struct usb_device
*dev
= serial
->dev
;
173 struct belkin_sa_private
*priv
;
175 /* allocate the private data structure */
176 priv
= kmalloc(sizeof(struct belkin_sa_private
), GFP_KERNEL
);
178 return -1; /* error */
179 /* set initial values for control structures */
180 spin_lock_init(&priv
->lock
);
181 priv
->control_state
= 0;
184 /* see comments at top of file */
185 priv
->bad_flow_control
=
186 (le16_to_cpu(dev
->descriptor
.bcdDevice
) <= 0x0206) ? 1 : 0;
187 dev_info(&dev
->dev
, "bcdDevice: %04x, bfc: %d\n",
188 le16_to_cpu(dev
->descriptor
.bcdDevice
),
189 priv
->bad_flow_control
);
191 init_waitqueue_head(&serial
->port
[0]->write_wait
);
192 usb_set_serial_port_data(serial
->port
[0], priv
);
197 static void belkin_sa_release(struct usb_serial
*serial
)
203 for (i
= 0; i
< serial
->num_ports
; ++i
)
204 kfree(usb_get_serial_port_data(serial
->port
[i
]));
207 static int belkin_sa_open(struct tty_struct
*tty
,
208 struct usb_serial_port
*port
)
212 dbg("%s port %d", __func__
, port
->number
);
214 retval
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
216 dev_err(&port
->dev
, "usb_submit_urb(read int) failed\n");
220 retval
= usb_serial_generic_open(tty
, port
);
222 usb_kill_urb(port
->interrupt_in_urb
);
227 static void belkin_sa_close(struct usb_serial_port
*port
)
229 dbg("%s port %d", __func__
, port
->number
);
231 usb_serial_generic_close(port
);
232 usb_kill_urb(port
->interrupt_in_urb
);
235 static void belkin_sa_read_int_callback(struct urb
*urb
)
237 struct usb_serial_port
*port
= urb
->context
;
238 struct belkin_sa_private
*priv
;
239 unsigned char *data
= urb
->transfer_buffer
;
241 int status
= urb
->status
;
251 /* this urb is terminated, clean up */
252 dbg("%s - urb shutting down with status: %d",
256 dbg("%s - nonzero urb status received: %d",
261 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
262 urb
->actual_length
, data
);
264 /* Handle known interrupt data */
265 /* ignore data[0] and data[1] */
267 priv
= usb_get_serial_port_data(port
);
268 spin_lock_irqsave(&priv
->lock
, flags
);
269 priv
->last_msr
= data
[BELKIN_SA_MSR_INDEX
];
271 /* Record Control Line states */
272 if (priv
->last_msr
& BELKIN_SA_MSR_DSR
)
273 priv
->control_state
|= TIOCM_DSR
;
275 priv
->control_state
&= ~TIOCM_DSR
;
277 if (priv
->last_msr
& BELKIN_SA_MSR_CTS
)
278 priv
->control_state
|= TIOCM_CTS
;
280 priv
->control_state
&= ~TIOCM_CTS
;
282 if (priv
->last_msr
& BELKIN_SA_MSR_RI
)
283 priv
->control_state
|= TIOCM_RI
;
285 priv
->control_state
&= ~TIOCM_RI
;
287 if (priv
->last_msr
& BELKIN_SA_MSR_CD
)
288 priv
->control_state
|= TIOCM_CD
;
290 priv
->control_state
&= ~TIOCM_CD
;
292 /* Now to report any errors */
293 priv
->last_lsr
= data
[BELKIN_SA_LSR_INDEX
];
296 * fill in the flip buffer here, but I do not know the relation
297 * to the current/next receive buffer or characters. I need
298 * to look in to this before committing any code.
300 if (priv
->last_lsr
& BELKIN_SA_LSR_ERR
) {
301 tty
= tty_port_tty_get(&port
->port
);
303 if (priv
->last_lsr
& BELKIN_SA_LSR_OE
) {
306 if (priv
->last_lsr
& BELKIN_SA_LSR_PE
) {
309 if (priv
->last_lsr
& BELKIN_SA_LSR_FE
) {
311 /* Break Indicator */
312 if (priv
->last_lsr
& BELKIN_SA_LSR_BI
) {
317 spin_unlock_irqrestore(&priv
->lock
, flags
);
319 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
321 dev_err(&port
->dev
, "%s - usb_submit_urb failed with "
322 "result %d\n", __func__
, retval
);
325 static void belkin_sa_set_termios(struct tty_struct
*tty
,
326 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
328 struct usb_serial
*serial
= port
->serial
;
329 struct belkin_sa_private
*priv
= usb_get_serial_port_data(port
);
332 unsigned int old_iflag
= 0;
333 unsigned int old_cflag
= 0;
334 __u16 urb_value
= 0; /* Will hold the new flags */
336 unsigned long control_state
;
337 int bad_flow_control
;
339 struct ktermios
*termios
= tty
->termios
;
341 iflag
= termios
->c_iflag
;
342 cflag
= termios
->c_cflag
;
344 termios
->c_cflag
&= ~CMSPAR
;
346 /* get a local copy of the current port settings */
347 spin_lock_irqsave(&priv
->lock
, flags
);
348 control_state
= priv
->control_state
;
349 bad_flow_control
= priv
->bad_flow_control
;
350 spin_unlock_irqrestore(&priv
->lock
, flags
);
352 old_iflag
= old_termios
->c_iflag
;
353 old_cflag
= old_termios
->c_cflag
;
355 /* Set the baud rate */
356 if ((cflag
& CBAUD
) != (old_cflag
& CBAUD
)) {
357 /* reassert DTR and (maybe) RTS on transition from B0 */
358 if ((old_cflag
& CBAUD
) == B0
) {
359 control_state
|= (TIOCM_DTR
|TIOCM_RTS
);
360 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST
, 1) < 0)
361 dev_err(&port
->dev
, "Set DTR error\n");
362 /* don't set RTS if using hardware flow control */
363 if (!(old_cflag
& CRTSCTS
))
364 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
366 dev_err(&port
->dev
, "Set RTS error\n");
370 baud
= tty_get_baud_rate(tty
);
372 urb_value
= BELKIN_SA_BAUD(baud
);
373 /* Clip to maximum speed */
376 /* Turn it back into a resulting real baud rate */
377 baud
= BELKIN_SA_BAUD(urb_value
);
379 /* Report the actual baud rate back to the caller */
380 tty_encode_baud_rate(tty
, baud
, baud
);
381 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST
, urb_value
) < 0)
382 dev_err(&port
->dev
, "Set baudrate error\n");
384 /* Disable flow control */
385 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST
,
386 BELKIN_SA_FLOW_NONE
) < 0)
387 dev_err(&port
->dev
, "Disable flowcontrol error\n");
388 /* Drop RTS and DTR */
389 control_state
&= ~(TIOCM_DTR
| TIOCM_RTS
);
390 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST
, 0) < 0)
391 dev_err(&port
->dev
, "DTR LOW error\n");
392 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
, 0) < 0)
393 dev_err(&port
->dev
, "RTS LOW error\n");
397 if ((cflag
^ old_cflag
) & (PARENB
| PARODD
)) {
399 urb_value
= (cflag
& PARODD
) ? BELKIN_SA_PARITY_ODD
400 : BELKIN_SA_PARITY_EVEN
;
402 urb_value
= BELKIN_SA_PARITY_NONE
;
403 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST
, urb_value
) < 0)
404 dev_err(&port
->dev
, "Set parity error\n");
407 /* set the number of data bits */
408 if ((cflag
& CSIZE
) != (old_cflag
& CSIZE
)) {
409 switch (cflag
& CSIZE
) {
411 urb_value
= BELKIN_SA_DATA_BITS(5);
414 urb_value
= BELKIN_SA_DATA_BITS(6);
417 urb_value
= BELKIN_SA_DATA_BITS(7);
420 urb_value
= BELKIN_SA_DATA_BITS(8);
422 default: dbg("CSIZE was not CS5-CS8, using default of 8");
423 urb_value
= BELKIN_SA_DATA_BITS(8);
426 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST
, urb_value
) < 0)
427 dev_err(&port
->dev
, "Set data bits error\n");
430 /* set the number of stop bits */
431 if ((cflag
& CSTOPB
) != (old_cflag
& CSTOPB
)) {
432 urb_value
= (cflag
& CSTOPB
) ? BELKIN_SA_STOP_BITS(2)
433 : BELKIN_SA_STOP_BITS(1);
434 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST
,
436 dev_err(&port
->dev
, "Set stop bits error\n");
439 /* Set flow control */
440 if (((iflag
^ old_iflag
) & (IXOFF
| IXON
)) ||
441 ((cflag
^ old_cflag
) & CRTSCTS
)) {
443 if ((iflag
& IXOFF
) || (iflag
& IXON
))
444 urb_value
|= (BELKIN_SA_FLOW_OXON
| BELKIN_SA_FLOW_IXON
);
446 urb_value
&= ~(BELKIN_SA_FLOW_OXON
| BELKIN_SA_FLOW_IXON
);
449 urb_value
|= (BELKIN_SA_FLOW_OCTS
| BELKIN_SA_FLOW_IRTS
);
451 urb_value
&= ~(BELKIN_SA_FLOW_OCTS
| BELKIN_SA_FLOW_IRTS
);
453 if (bad_flow_control
)
454 urb_value
&= ~(BELKIN_SA_FLOW_IRTS
);
456 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST
, urb_value
) < 0)
457 dev_err(&port
->dev
, "Set flow control error\n");
460 /* save off the modified port settings */
461 spin_lock_irqsave(&priv
->lock
, flags
);
462 priv
->control_state
= control_state
;
463 spin_unlock_irqrestore(&priv
->lock
, flags
);
466 static void belkin_sa_break_ctl(struct tty_struct
*tty
, int break_state
)
468 struct usb_serial_port
*port
= tty
->driver_data
;
469 struct usb_serial
*serial
= port
->serial
;
471 if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST
, break_state
? 1 : 0) < 0)
472 dev_err(&port
->dev
, "Set break_ctl %d\n", break_state
);
475 static int belkin_sa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
477 struct usb_serial_port
*port
= tty
->driver_data
;
478 struct belkin_sa_private
*priv
= usb_get_serial_port_data(port
);
479 unsigned long control_state
;
484 spin_lock_irqsave(&priv
->lock
, flags
);
485 control_state
= priv
->control_state
;
486 spin_unlock_irqrestore(&priv
->lock
, flags
);
488 return control_state
;
491 static int belkin_sa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
492 unsigned int set
, unsigned int clear
)
494 struct usb_serial_port
*port
= tty
->driver_data
;
495 struct usb_serial
*serial
= port
->serial
;
496 struct belkin_sa_private
*priv
= usb_get_serial_port_data(port
);
497 unsigned long control_state
;
505 spin_lock_irqsave(&priv
->lock
, flags
);
506 control_state
= priv
->control_state
;
508 if (set
& TIOCM_RTS
) {
509 control_state
|= TIOCM_RTS
;
512 if (set
& TIOCM_DTR
) {
513 control_state
|= TIOCM_DTR
;
516 if (clear
& TIOCM_RTS
) {
517 control_state
&= ~TIOCM_RTS
;
520 if (clear
& TIOCM_DTR
) {
521 control_state
&= ~TIOCM_DTR
;
525 priv
->control_state
= control_state
;
526 spin_unlock_irqrestore(&priv
->lock
, flags
);
528 retval
= BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
, rts
);
530 dev_err(&port
->dev
, "Set RTS error %d\n", retval
);
534 retval
= BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST
, dtr
);
536 dev_err(&port
->dev
, "Set DTR error %d\n", retval
);
544 static int __init
belkin_sa_init(void)
547 retval
= usb_serial_register(&belkin_device
);
549 goto failed_usb_serial_register
;
550 retval
= usb_register(&belkin_driver
);
552 goto failed_usb_register
;
553 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
557 usb_serial_deregister(&belkin_device
);
558 failed_usb_serial_register
:
562 static void __exit
belkin_sa_exit (void)
564 usb_deregister(&belkin_driver
);
565 usb_serial_deregister(&belkin_device
);
569 module_init(belkin_sa_init
);
570 module_exit(belkin_sa_exit
);
572 MODULE_AUTHOR(DRIVER_AUTHOR
);
573 MODULE_DESCRIPTION(DRIVER_DESC
);
574 MODULE_VERSION(DRIVER_VERSION
);
575 MODULE_LICENSE("GPL");
577 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
578 MODULE_PARM_DESC(debug
, "Debug enabled or not");