whiteheat: fix bugs found in the tidy and audit
[deliverable/linux.git] / drivers / usb / serial / belkin_sa.c
CommitLineData
1da177e4
LT
1/*
2 * Belkin USB Serial Adapter Driver
3 *
4 * Copyright (C) 2000 William Greathouse (wgreathouse@smva.com)
5 * Copyright (C) 2000-2001 Greg Kroah-Hartman (greg@kroah.com)
6 *
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.
10 *
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.
15 *
16 * See Documentation/usb/usb-serial.txt for more information on using this driver
17 *
18 * TODO:
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 :)
26 *
27 * 27-Nov-2001 gkh
28 * compressed all the differnent device entries into 1.
29 *
30 * 30-May-2001 gkh
31 * switched from using spinlock to a semaphore, which fixes lots of problems.
32 *
33 * 08-Apr-2001 gb
34 * - Identify version on module load.
35 *
36 * 12-Mar-2001 gkh
37 * - Added support for the GoHubs GO-COM232 device which is the same as the
38 * Peracom device.
39 *
40 * 06-Nov-2000 gkh
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
44 * can't support.
45 *
46 * 18-Oct-2000 William Greathouse
47 * Released into the wild (linux-usb-devel)
48 *
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.
57 *
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.
64 */
65
1da177e4
LT
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>
a969888c 77#include <linux/usb/serial.h>
1da177e4
LT
78#include "belkin_sa.h"
79
80static int debug;
81
82/*
83 * Version Information
84 */
85#define DRIVER_VERSION "v1.2"
86#define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
87#define DRIVER_DESC "USB Belkin Serial converter driver"
88
89/* function prototypes for a Belkin USB Serial Adapter F5U103 */
90static int belkin_sa_startup (struct usb_serial *serial);
91static void belkin_sa_shutdown (struct usb_serial *serial);
95da310e
AC
92static int belkin_sa_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
93static void belkin_sa_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
7d12e780 94static void belkin_sa_read_int_callback (struct urb *urb);
95da310e
AC
95static void belkin_sa_set_termios (struct tty_struct *tty, struct usb_serial_port *port, struct ktermios * old);
96static void belkin_sa_break_ctl (struct tty_struct *tty, int break_state );
97static int belkin_sa_tiocmget (struct tty_struct *tty, struct file *file);
98static int belkin_sa_tiocmset (struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
1da177e4
LT
99
100
101static struct usb_device_id id_table_combined [] = {
102 { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
103 { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
104 { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
105 { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
106 { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
107 { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
108 { } /* Terminating entry */
109};
110
111MODULE_DEVICE_TABLE (usb, id_table_combined);
112
113static struct usb_driver belkin_driver = {
1da177e4
LT
114 .name = "belkin",
115 .probe = usb_serial_probe,
116 .disconnect = usb_serial_disconnect,
117 .id_table = id_table_combined,
ba9dc657 118 .no_dynamic_id = 1,
1da177e4
LT
119};
120
121/* All of the device info needed for the serial converters */
ea65370d 122static struct usb_serial_driver belkin_device = {
18fcac35
GKH
123 .driver = {
124 .owner = THIS_MODULE,
269bda1c 125 .name = "belkin",
18fcac35 126 },
269bda1c 127 .description = "Belkin / Peracom / GoHubs USB Serial Adapter",
d9b1b787 128 .usb_driver = &belkin_driver,
1da177e4 129 .id_table = id_table_combined,
1da177e4
LT
130 .num_ports = 1,
131 .open = belkin_sa_open,
132 .close = belkin_sa_close,
133 .read_int_callback = belkin_sa_read_int_callback, /* How we get the status info */
1da177e4
LT
134 .set_termios = belkin_sa_set_termios,
135 .break_ctl = belkin_sa_break_ctl,
136 .tiocmget = belkin_sa_tiocmget,
137 .tiocmset = belkin_sa_tiocmset,
138 .attach = belkin_sa_startup,
139 .shutdown = belkin_sa_shutdown,
140};
141
142
143struct belkin_sa_private {
144 spinlock_t lock;
145 unsigned long control_state;
146 unsigned char last_lsr;
147 unsigned char last_msr;
148 int bad_flow_control;
149};
150
151
152/*
153 * ***************************************************************************
154 * Belkin USB Serial Adapter F5U103 specific driver functions
155 * ***************************************************************************
156 */
157
158#define WDR_TIMEOUT 5000 /* default urb timeout */
159
160/* assumes that struct usb_serial *serial is available */
161#define BSA_USB_CMD(c,v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
162 (c), BELKIN_SA_SET_REQUEST_TYPE, \
163 (v), 0, NULL, 0, WDR_TIMEOUT)
164
165/* do some startup allocations not currently performed by usb_serial_probe() */
166static int belkin_sa_startup (struct usb_serial *serial)
167{
168 struct usb_device *dev = serial->dev;
169 struct belkin_sa_private *priv;
170
171 /* allocate the private data structure */
172 priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
173 if (!priv)
174 return (-1); /* error */
175 /* set initial values for control structures */
176 spin_lock_init(&priv->lock);
177 priv->control_state = 0;
178 priv->last_lsr = 0;
179 priv->last_msr = 0;
180 /* see comments at top of file */
181 priv->bad_flow_control = (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
182 info("bcdDevice: %04x, bfc: %d", le16_to_cpu(dev->descriptor.bcdDevice), priv->bad_flow_control);
183
184 init_waitqueue_head(&serial->port[0]->write_wait);
185 usb_set_serial_port_data(serial->port[0], priv);
186
187 return (0);
188}
189
190
95da310e 191static void belkin_sa_shutdown(struct usb_serial *serial)
1da177e4
LT
192{
193 struct belkin_sa_private *priv;
194 int i;
195
441b62c1 196 dbg ("%s", __func__);
1da177e4
LT
197
198 /* stop reads and writes on all ports */
199 for (i=0; i < serial->num_ports; ++i) {
200 /* My special items, the standard routines free my urbs */
201 priv = usb_get_serial_port_data(serial->port[i]);
1bc3c9e1 202 kfree(priv);
1da177e4
LT
203 }
204}
205
206
95da310e 207static int belkin_sa_open(struct tty_struct *tty, struct usb_serial_port *port, struct file *filp)
1da177e4
LT
208{
209 int retval = 0;
210
441b62c1 211 dbg("%s port %d", __func__, port->number);
1da177e4
LT
212
213 /*Start reading from the device*/
214 /* TODO: Look at possibility of submitting multiple URBs to device to
215 * enhance buffering. Win trace shows 16 initial read URBs.
216 */
217 port->read_urb->dev = port->serial->dev;
218 retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
219 if (retval) {
220 err("usb_submit_urb(read bulk) failed");
221 goto exit;
222 }
223
224 port->interrupt_in_urb->dev = port->serial->dev;
225 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
226 if (retval) {
227 usb_kill_urb(port->read_urb);
228 err(" usb_submit_urb(read int) failed");
229 }
230
231exit:
232 return retval;
233} /* belkin_sa_open */
234
235
95da310e
AC
236static void belkin_sa_close (struct tty_struct *tty,
237 struct usb_serial_port *port, struct file *filp)
1da177e4 238{
441b62c1 239 dbg("%s port %d", __func__, port->number);
1da177e4
LT
240
241 /* shutdown our bulk reads and writes */
242 usb_kill_urb(port->write_urb);
243 usb_kill_urb(port->read_urb);
244 usb_kill_urb(port->interrupt_in_urb);
245} /* belkin_sa_close */
246
247
95da310e 248static void belkin_sa_read_int_callback(struct urb *urb)
1da177e4 249{
cdc97792 250 struct usb_serial_port *port = urb->context;
1da177e4
LT
251 struct belkin_sa_private *priv;
252 unsigned char *data = urb->transfer_buffer;
253 int retval;
f26aad25 254 int status = urb->status;
1da177e4
LT
255 unsigned long flags;
256
f26aad25 257 switch (status) {
1da177e4
LT
258 case 0:
259 /* success */
260 break;
261 case -ECONNRESET:
262 case -ENOENT:
263 case -ESHUTDOWN:
264 /* this urb is terminated, clean up */
f26aad25 265 dbg("%s - urb shutting down with status: %d",
441b62c1 266 __func__, status);
1da177e4
LT
267 return;
268 default:
f26aad25 269 dbg("%s - nonzero urb status received: %d",
441b62c1 270 __func__, status);
1da177e4
LT
271 goto exit;
272 }
273
441b62c1 274 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
1da177e4
LT
275
276 /* Handle known interrupt data */
277 /* ignore data[0] and data[1] */
278
279 priv = usb_get_serial_port_data(port);
280 spin_lock_irqsave(&priv->lock, flags);
281 priv->last_msr = data[BELKIN_SA_MSR_INDEX];
282
283 /* Record Control Line states */
284 if (priv->last_msr & BELKIN_SA_MSR_DSR)
285 priv->control_state |= TIOCM_DSR;
286 else
287 priv->control_state &= ~TIOCM_DSR;
288
289 if (priv->last_msr & BELKIN_SA_MSR_CTS)
290 priv->control_state |= TIOCM_CTS;
291 else
292 priv->control_state &= ~TIOCM_CTS;
293
294 if (priv->last_msr & BELKIN_SA_MSR_RI)
295 priv->control_state |= TIOCM_RI;
296 else
297 priv->control_state &= ~TIOCM_RI;
298
299 if (priv->last_msr & BELKIN_SA_MSR_CD)
300 priv->control_state |= TIOCM_CD;
301 else
302 priv->control_state &= ~TIOCM_CD;
303
304 /* Now to report any errors */
305 priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
306#if 0
307 /*
308 * fill in the flip buffer here, but I do not know the relation
309 * to the current/next receive buffer or characters. I need
310 * to look in to this before committing any code.
311 */
312 if (priv->last_lsr & BELKIN_SA_LSR_ERR) {
95da310e 313 tty = port->port.tty;
1da177e4
LT
314 /* Overrun Error */
315 if (priv->last_lsr & BELKIN_SA_LSR_OE) {
316 }
317 /* Parity Error */
318 if (priv->last_lsr & BELKIN_SA_LSR_PE) {
319 }
320 /* Framing Error */
321 if (priv->last_lsr & BELKIN_SA_LSR_FE) {
322 }
323 /* Break Indicator */
324 if (priv->last_lsr & BELKIN_SA_LSR_BI) {
325 }
326 }
327#endif
328 spin_unlock_irqrestore(&priv->lock, flags);
329exit:
330 retval = usb_submit_urb (urb, GFP_ATOMIC);
331 if (retval)
332 err ("%s - usb_submit_urb failed with result %d",
441b62c1 333 __func__, retval);
1da177e4
LT
334}
335
95da310e
AC
336static void belkin_sa_set_termios(struct tty_struct *tty,
337 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4
LT
338{
339 struct usb_serial *serial = port->serial;
340 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
341 unsigned int iflag;
342 unsigned int cflag;
343 unsigned int old_iflag = 0;
344 unsigned int old_cflag = 0;
345 __u16 urb_value = 0; /* Will hold the new flags */
346 unsigned long flags;
347 unsigned long control_state;
348 int bad_flow_control;
9a8baec7 349 speed_t baud;
95da310e 350 struct ktermios *termios = tty->termios;
1da177e4 351
3ec466b4
AC
352 iflag = termios->c_iflag;
353 cflag = termios->c_cflag;
1da177e4 354
3ec466b4 355 termios->c_cflag &= ~CMSPAR;
1da177e4
LT
356
357 /* get a local copy of the current port settings */
358 spin_lock_irqsave(&priv->lock, flags);
359 control_state = priv->control_state;
360 bad_flow_control = priv->bad_flow_control;
361 spin_unlock_irqrestore(&priv->lock, flags);
362
9a8baec7
AC
363 old_iflag = old_termios->c_iflag;
364 old_cflag = old_termios->c_cflag;
1da177e4
LT
365
366 /* Set the baud rate */
3ec466b4 367 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
1da177e4
LT
368 /* reassert DTR and (maybe) RTS on transition from B0 */
369 if( (old_cflag&CBAUD) == B0 ) {
370 control_state |= (TIOCM_DTR|TIOCM_RTS);
371 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
372 err("Set DTR error");
373 /* don't set RTS if using hardware flow control */
3ec466b4 374 if (!(old_cflag & CRTSCTS))
1da177e4
LT
375 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 1) < 0)
376 err("Set RTS error");
377 }
9a8baec7 378 }
1da177e4 379
95da310e 380 baud = tty_get_baud_rate(tty);
3ec466b4
AC
381 if (baud) {
382 urb_value = BELKIN_SA_BAUD(baud);
383 /* Clip to maximum speed */
384 if (urb_value == 0)
385 urb_value = 1;
386 /* Turn it back into a resulting real baud rate */
387 baud = BELKIN_SA_BAUD(urb_value);
388
389 /* Report the actual baud rate back to the caller */
95da310e 390 tty_encode_baud_rate(tty, baud, baud);
9a8baec7
AC
391 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
392 err("Set baudrate error");
393 } else {
394 /* Disable flow control */
395 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, BELKIN_SA_FLOW_NONE) < 0)
396 err("Disable flowcontrol error");
397 /* Drop RTS and DTR */
398 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
399 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
400 err("DTR LOW error");
401 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
402 err("RTS LOW error");
1da177e4
LT
403 }
404
405 /* set the parity */
406 if( (cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD)) ) {
407 if (cflag & PARENB)
408 urb_value = (cflag & PARODD) ? BELKIN_SA_PARITY_ODD : BELKIN_SA_PARITY_EVEN;
409 else
410 urb_value = BELKIN_SA_PARITY_NONE;
411 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
412 err("Set parity error");
413 }
414
415 /* set the number of data bits */
416 if( (cflag&CSIZE) != (old_cflag&CSIZE) ) {
417 switch (cflag & CSIZE) {
418 case CS5: urb_value = BELKIN_SA_DATA_BITS(5); break;
419 case CS6: urb_value = BELKIN_SA_DATA_BITS(6); break;
420 case CS7: urb_value = BELKIN_SA_DATA_BITS(7); break;
421 case CS8: urb_value = BELKIN_SA_DATA_BITS(8); break;
9a8baec7 422 default: dbg("CSIZE was not CS5-CS8, using default of 8");
1da177e4
LT
423 urb_value = BELKIN_SA_DATA_BITS(8);
424 break;
425 }
426 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
427 err("Set data bits error");
428 }
429
430 /* set the number of stop bits */
431 if( (cflag&CSTOPB) != (old_cflag&CSTOPB) ) {
432 urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2) : BELKIN_SA_STOP_BITS(1);
433 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST, urb_value) < 0)
434 err("Set stop bits error");
435 }
436
437 /* Set flow control */
438 if( (iflag&IXOFF) != (old_iflag&IXOFF)
439 || (iflag&IXON) != (old_iflag&IXON)
440 || (cflag&CRTSCTS) != (old_cflag&CRTSCTS) ) {
441 urb_value = 0;
442 if ((iflag & IXOFF) || (iflag & IXON))
443 urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
444 else
445 urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
446
447 if (cflag & CRTSCTS)
448 urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
449 else
450 urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
451
452 if (bad_flow_control)
453 urb_value &= ~(BELKIN_SA_FLOW_IRTS);
454
455 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
456 err("Set flow control error");
457 }
458
459 /* save off the modified port settings */
460 spin_lock_irqsave(&priv->lock, flags);
461 priv->control_state = control_state;
462 spin_unlock_irqrestore(&priv->lock, flags);
463} /* belkin_sa_set_termios */
464
465
95da310e 466static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
1da177e4 467{
95da310e 468 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
469 struct usb_serial *serial = port->serial;
470
471 if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
472 err("Set break_ctl %d", break_state);
473}
474
475
95da310e 476static int belkin_sa_tiocmget(struct tty_struct *tty, struct file *file)
1da177e4 477{
95da310e 478 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
479 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
480 unsigned long control_state;
481 unsigned long flags;
482
441b62c1 483 dbg("%s", __func__);
1da177e4
LT
484
485 spin_lock_irqsave(&priv->lock, flags);
486 control_state = priv->control_state;
487 spin_unlock_irqrestore(&priv->lock, flags);
488
489 return control_state;
490}
491
492
95da310e 493static int belkin_sa_tiocmset(struct tty_struct *tty, struct file *file,
1da177e4
LT
494 unsigned int set, unsigned int clear)
495{
95da310e 496 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
497 struct usb_serial *serial = port->serial;
498 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
499 unsigned long control_state;
500 unsigned long flags;
501 int retval;
502 int rts = 0;
503 int dtr = 0;
504
441b62c1 505 dbg("%s", __func__);
1da177e4
LT
506
507 spin_lock_irqsave(&priv->lock, flags);
508 control_state = priv->control_state;
509
510 if (set & TIOCM_RTS) {
511 control_state |= TIOCM_RTS;
512 rts = 1;
513 }
514 if (set & TIOCM_DTR) {
515 control_state |= TIOCM_DTR;
516 dtr = 1;
517 }
518 if (clear & TIOCM_RTS) {
519 control_state &= ~TIOCM_RTS;
520 rts = 0;
521 }
522 if (clear & TIOCM_DTR) {
523 control_state &= ~TIOCM_DTR;
524 dtr = 0;
525 }
526
527 priv->control_state = control_state;
528 spin_unlock_irqrestore(&priv->lock, flags);
529
530 retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
531 if (retval < 0) {
532 err("Set RTS error %d", retval);
533 goto exit;
534 }
535
536 retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
537 if (retval < 0) {
538 err("Set DTR error %d", retval);
539 goto exit;
540 }
541exit:
542 return retval;
543}
544
545
95da310e 546static int __init belkin_sa_init(void)
1da177e4
LT
547{
548 int retval;
549 retval = usb_serial_register(&belkin_device);
550 if (retval)
551 goto failed_usb_serial_register;
552 retval = usb_register(&belkin_driver);
553 if (retval)
554 goto failed_usb_register;
555 info(DRIVER_DESC " " DRIVER_VERSION);
556 return 0;
557failed_usb_register:
558 usb_serial_deregister(&belkin_device);
559failed_usb_serial_register:
560 return retval;
561}
562
563
564static void __exit belkin_sa_exit (void)
565{
566 usb_deregister (&belkin_driver);
95da310e 567 usb_serial_deregister(&belkin_device);
1da177e4
LT
568}
569
570
571module_init (belkin_sa_init);
572module_exit (belkin_sa_exit);
573
574MODULE_AUTHOR( DRIVER_AUTHOR );
575MODULE_DESCRIPTION( DRIVER_DESC );
576MODULE_VERSION( DRIVER_VERSION );
577MODULE_LICENSE("GPL");
578
579module_param(debug, bool, S_IRUGO | S_IWUSR);
580MODULE_PARM_DESC(debug, "Debug enabled or not");
This page took 0.425814 seconds and 5 git commands to generate.