Merge git://git.infradead.org/battery-2.6
[pandora-kernel.git] / drivers / usb / serial / belkin_sa.c
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
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"
79
80 static 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 */
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);
100
101
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 */
110 };
111
112 MODULE_DEVICE_TABLE (usb, id_table_combined);
113
114 static struct usb_driver belkin_driver = {
115         .name =         "belkin",
116         .probe =        usb_serial_probe,
117         .disconnect =   usb_serial_disconnect,
118         .id_table =     id_table_combined,
119         .no_dynamic_id =        1,
120 };
121
122 /* All of the device info needed for the serial converters */
123 static struct usb_serial_driver belkin_device = {
124         .driver = {
125                 .owner =        THIS_MODULE,
126                 .name =         "belkin",
127         },
128         .description =          "Belkin / Peracom / GoHubs USB Serial Adapter",
129         .usb_driver =           &belkin_driver,
130         .id_table =             id_table_combined,
131         .num_ports =            1,
132         .open =                 belkin_sa_open,
133         .close =                belkin_sa_close,
134         .read_int_callback =    belkin_sa_read_int_callback,    /* How we get the status info */
135         .ioctl =                belkin_sa_ioctl,
136         .set_termios =          belkin_sa_set_termios,
137         .break_ctl =            belkin_sa_break_ctl,
138         .tiocmget =             belkin_sa_tiocmget,
139         .tiocmset =             belkin_sa_tiocmset,
140         .attach =               belkin_sa_startup,
141         .shutdown =             belkin_sa_shutdown,
142 };
143
144
145 struct belkin_sa_private {
146         spinlock_t              lock;
147         unsigned long           control_state;
148         unsigned char           last_lsr;
149         unsigned char           last_msr;
150         int                     bad_flow_control;
151 };
152
153
154 /*
155  * ***************************************************************************
156  * Belkin USB Serial Adapter F5U103 specific driver functions
157  * ***************************************************************************
158  */
159
160 #define WDR_TIMEOUT 5000 /* default urb timeout */
161
162 /* assumes that struct usb_serial *serial is available */
163 #define BSA_USB_CMD(c,v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
164                                             (c), BELKIN_SA_SET_REQUEST_TYPE, \
165                                             (v), 0, NULL, 0, WDR_TIMEOUT)
166
167 /* do some startup allocations not currently performed by usb_serial_probe() */
168 static int belkin_sa_startup (struct usb_serial *serial)
169 {
170         struct usb_device *dev = serial->dev;
171         struct belkin_sa_private *priv;
172
173         /* allocate the private data structure */
174         priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
175         if (!priv)
176                 return (-1); /* error */
177         /* set initial values for control structures */
178         spin_lock_init(&priv->lock);
179         priv->control_state = 0;
180         priv->last_lsr = 0;
181         priv->last_msr = 0;
182         /* see comments at top of file */
183         priv->bad_flow_control = (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
184         info("bcdDevice: %04x, bfc: %d", le16_to_cpu(dev->descriptor.bcdDevice), priv->bad_flow_control);
185
186         init_waitqueue_head(&serial->port[0]->write_wait);
187         usb_set_serial_port_data(serial->port[0], priv);
188         
189         return (0);
190 }
191
192
193 static void belkin_sa_shutdown (struct usb_serial *serial)
194 {
195         struct belkin_sa_private *priv;
196         int i;
197         
198         dbg ("%s", __func__);
199
200         /* stop reads and writes on all ports */
201         for (i=0; i < serial->num_ports; ++i) {
202                 /* My special items, the standard routines free my urbs */
203                 priv = usb_get_serial_port_data(serial->port[i]);
204                 kfree(priv);
205         }
206 }
207
208
209 static int  belkin_sa_open (struct usb_serial_port *port, struct file *filp)
210 {
211         int retval = 0;
212
213         dbg("%s port %d", __func__, port->number);
214
215         /*Start reading from the device*/
216         /* TODO: Look at possibility of submitting multiple URBs to device to
217          *       enhance buffering.  Win trace shows 16 initial read URBs.
218          */
219         port->read_urb->dev = port->serial->dev;
220         retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
221         if (retval) {
222                 err("usb_submit_urb(read bulk) failed");
223                 goto exit;
224         }
225
226         port->interrupt_in_urb->dev = port->serial->dev;
227         retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
228         if (retval) {
229                 usb_kill_urb(port->read_urb);
230                 err(" usb_submit_urb(read int) failed");
231         }
232
233 exit:
234         return retval;
235 } /* belkin_sa_open */
236
237
238 static void belkin_sa_close (struct usb_serial_port *port, struct file *filp)
239 {
240         dbg("%s port %d", __func__, port->number);
241
242         /* shutdown our bulk reads and writes */
243         usb_kill_urb(port->write_urb);
244         usb_kill_urb(port->read_urb);
245         usb_kill_urb(port->interrupt_in_urb);
246 } /* belkin_sa_close */
247
248
249 static void belkin_sa_read_int_callback (struct urb *urb)
250 {
251         struct usb_serial_port *port = urb->context;
252         struct belkin_sa_private *priv;
253         unsigned char *data = urb->transfer_buffer;
254         int retval;
255         int status = urb->status;
256         unsigned long flags;
257
258         switch (status) {
259         case 0:
260                 /* success */
261                 break;
262         case -ECONNRESET:
263         case -ENOENT:
264         case -ESHUTDOWN:
265                 /* this urb is terminated, clean up */
266                 dbg("%s - urb shutting down with status: %d",
267                     __func__, status);
268                 return;
269         default:
270                 dbg("%s - nonzero urb status received: %d",
271                     __func__, status);
272                 goto exit;
273         }
274
275         usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
276
277         /* Handle known interrupt data */
278         /* ignore data[0] and data[1] */
279
280         priv = usb_get_serial_port_data(port);
281         spin_lock_irqsave(&priv->lock, flags);
282         priv->last_msr = data[BELKIN_SA_MSR_INDEX];
283         
284         /* Record Control Line states */
285         if (priv->last_msr & BELKIN_SA_MSR_DSR)
286                 priv->control_state |= TIOCM_DSR;
287         else
288                 priv->control_state &= ~TIOCM_DSR;
289
290         if (priv->last_msr & BELKIN_SA_MSR_CTS)
291                 priv->control_state |= TIOCM_CTS;
292         else
293                 priv->control_state &= ~TIOCM_CTS;
294
295         if (priv->last_msr & BELKIN_SA_MSR_RI)
296                 priv->control_state |= TIOCM_RI;
297         else
298                 priv->control_state &= ~TIOCM_RI;
299
300         if (priv->last_msr & BELKIN_SA_MSR_CD)
301                 priv->control_state |= TIOCM_CD;
302         else
303                 priv->control_state &= ~TIOCM_CD;
304
305         /* Now to report any errors */
306         priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
307 #if 0
308         /*
309          * fill in the flip buffer here, but I do not know the relation
310          * to the current/next receive buffer or characters.  I need
311          * to look in to this before committing any code.
312          */
313         if (priv->last_lsr & BELKIN_SA_LSR_ERR) {
314                 tty = port->tty;
315                 /* Overrun Error */
316                 if (priv->last_lsr & BELKIN_SA_LSR_OE) {
317                 }
318                 /* Parity Error */
319                 if (priv->last_lsr & BELKIN_SA_LSR_PE) {
320                 }
321                 /* Framing Error */
322                 if (priv->last_lsr & BELKIN_SA_LSR_FE) {
323                 }
324                 /* Break Indicator */
325                 if (priv->last_lsr & BELKIN_SA_LSR_BI) {
326                 }
327         }
328 #endif
329         spin_unlock_irqrestore(&priv->lock, flags);
330 exit:
331         retval = usb_submit_urb (urb, GFP_ATOMIC);
332         if (retval)
333                 err ("%s - usb_submit_urb failed with result %d",
334                      __func__, retval);
335 }
336
337 static void belkin_sa_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
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;
349         speed_t baud;
350         struct ktermios *termios = port->tty->termios;
351         
352         iflag = termios->c_iflag;
353         cflag = termios->c_cflag;
354
355         termios->c_cflag &= ~CMSPAR;
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         
363         old_iflag = old_termios->c_iflag;
364         old_cflag = old_termios->c_cflag;
365
366         /* Set the baud rate */
367         if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
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 */
374                         if (!(old_cflag & CRTSCTS))
375                                 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 1) < 0)
376                                         err("Set RTS error");
377                 }
378         }
379
380         baud = tty_get_baud_rate(port->tty);
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 */
390                 tty_encode_baud_rate(port->tty, baud, baud);
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");
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;
422                         default: dbg("CSIZE was not CS5-CS8, using default of 8");
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
466 static void belkin_sa_break_ctl( struct usb_serial_port *port, int break_state )
467 {
468         struct usb_serial *serial = port->serial;
469
470         if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
471                 err("Set break_ctl %d", break_state);
472 }
473
474
475 static int belkin_sa_tiocmget (struct usb_serial_port *port, struct file *file)
476 {
477         struct belkin_sa_private *priv = usb_get_serial_port_data(port);
478         unsigned long control_state;
479         unsigned long flags;
480         
481         dbg("%s", __func__);
482
483         spin_lock_irqsave(&priv->lock, flags);
484         control_state = priv->control_state;
485         spin_unlock_irqrestore(&priv->lock, flags);
486
487         return control_state;
488 }
489
490
491 static int belkin_sa_tiocmset (struct usb_serial_port *port, struct file *file,
492                                unsigned int set, unsigned int clear)
493 {
494         struct usb_serial *serial = port->serial;
495         struct belkin_sa_private *priv = usb_get_serial_port_data(port);
496         unsigned long control_state;
497         unsigned long flags;
498         int retval;
499         int rts = 0;
500         int dtr = 0;
501         
502         dbg("%s", __func__);
503
504         spin_lock_irqsave(&priv->lock, flags);
505         control_state = priv->control_state;
506
507         if (set & TIOCM_RTS) {
508                 control_state |= TIOCM_RTS;
509                 rts = 1;
510         }
511         if (set & TIOCM_DTR) {
512                 control_state |= TIOCM_DTR;
513                 dtr = 1;
514         }
515         if (clear & TIOCM_RTS) {
516                 control_state &= ~TIOCM_RTS;
517                 rts = 0;
518         }
519         if (clear & TIOCM_DTR) {
520                 control_state &= ~TIOCM_DTR;
521                 dtr = 0;
522         }
523
524         priv->control_state = control_state;
525         spin_unlock_irqrestore(&priv->lock, flags);
526
527         retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
528         if (retval < 0) {
529                 err("Set RTS error %d", retval);
530                 goto exit;
531         }
532
533         retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
534         if (retval < 0) {
535                 err("Set DTR error %d", retval);
536                 goto exit;
537         }
538 exit:
539         return retval;
540 }
541
542
543 static int belkin_sa_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
544 {
545         switch (cmd) {
546         case TIOCMIWAIT:
547                 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
548                 /* TODO */
549                 return( 0 );
550
551         case TIOCGICOUNT:
552                 /* return count of modemline transitions */
553                 /* TODO */
554                 return 0;
555
556         default:
557                 dbg("belkin_sa_ioctl arg not supported - 0x%04x",cmd);
558                 return(-ENOIOCTLCMD);
559                 break;
560         }
561         return 0;
562 } /* belkin_sa_ioctl */
563
564
565 static int __init belkin_sa_init (void)
566 {
567         int retval;
568         retval = usb_serial_register(&belkin_device);
569         if (retval)
570                 goto failed_usb_serial_register;
571         retval = usb_register(&belkin_driver);
572         if (retval)
573                 goto failed_usb_register;
574         info(DRIVER_DESC " " DRIVER_VERSION);
575         return 0;
576 failed_usb_register:
577         usb_serial_deregister(&belkin_device);
578 failed_usb_serial_register:
579         return retval;
580 }
581
582
583 static void __exit belkin_sa_exit (void)
584 {
585         usb_deregister (&belkin_driver);
586         usb_serial_deregister (&belkin_device);
587 }
588
589
590 module_init (belkin_sa_init);
591 module_exit (belkin_sa_exit);
592
593 MODULE_AUTHOR( DRIVER_AUTHOR );
594 MODULE_DESCRIPTION( DRIVER_DESC );
595 MODULE_VERSION( DRIVER_VERSION );
596 MODULE_LICENSE("GPL");
597
598 module_param(debug, bool, S_IRUGO | S_IWUSR);
599 MODULE_PARM_DESC(debug, "Debug enabled or not");