more changes on original files
[linux-2.4.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/config.h>
67 #include <linux/kernel.h>
68 #include <linux/errno.h>
69 #include <linux/init.h>
70 #include <linux/slab.h>
71 #include <linux/tty.h>
72 #include <linux/tty_driver.h>
73 #include <linux/tty_flip.h>
74 #include <linux/module.h>
75 #include <linux/spinlock.h>
76 #include <asm/uaccess.h>
77 #include <linux/usb.h>
78
79 #ifdef CONFIG_USB_SERIAL_DEBUG
80         static int debug = 1;
81 #else
82         static int debug;
83 #endif
84
85 #include "usb-serial.h"
86 #include "belkin_sa.h"
87
88 /*
89  * Version Information
90  */
91 #define DRIVER_VERSION "v1.2"
92 #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
93 #define DRIVER_DESC "USB Belkin Serial converter driver"
94
95 /* function prototypes for a Belkin USB Serial Adapter F5U103 */
96 static int  belkin_sa_startup           (struct usb_serial *serial);
97 static void belkin_sa_shutdown          (struct usb_serial *serial);
98 static int  belkin_sa_open              (struct usb_serial_port *port, struct file *filp);
99 static void belkin_sa_close             (struct usb_serial_port *port, struct file *filp);
100 static void belkin_sa_read_int_callback (struct urb *urb);
101 static void belkin_sa_set_termios       (struct usb_serial_port *port, struct termios * old);
102 static int  belkin_sa_ioctl             (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
103 static void belkin_sa_break_ctl         (struct usb_serial_port *port, int break_state );
104
105
106 static 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 */
114 };
115
116 MODULE_DEVICE_TABLE (usb, id_table_combined);
117
118 /* All of the device info needed for the serial converters */
119 static struct usb_serial_device_type belkin_device = {
120         .owner =                THIS_MODULE,
121         .name =                 "Belkin / Peracom / GoHubs USB Serial Adapter",
122         .id_table =             id_table_combined,
123         .num_interrupt_in =     1,
124         .num_bulk_in =          1,
125         .num_bulk_out =         1,
126         .num_ports =            1,
127         .open =                 belkin_sa_open,
128         .close =                belkin_sa_close,
129         .read_int_callback =    belkin_sa_read_int_callback,    /* How we get the status info */
130         .ioctl =                belkin_sa_ioctl,
131         .set_termios =          belkin_sa_set_termios,
132         .break_ctl =            belkin_sa_break_ctl,
133         .startup =              belkin_sa_startup,
134         .shutdown =             belkin_sa_shutdown,
135 };
136
137
138 struct belkin_sa_private {
139         unsigned long           control_state;
140         unsigned char           last_lsr;
141         unsigned char           last_msr;
142         int                     bad_flow_control;
143 };
144
145
146 /*
147  * ***************************************************************************
148  * Belkin USB Serial Adapter F5U103 specific driver functions
149  * ***************************************************************************
150  */
151
152 #define WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
153
154 /* assumes that struct usb_serial *serial is available */
155 #define BSA_USB_CMD(c,v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
156                                             (c), BELKIN_SA_SET_REQUEST_TYPE, \
157                                             (v), 0, NULL, 0, WDR_TIMEOUT)
158
159 /* do some startup allocations not currently performed by usb_serial_probe() */
160 static int belkin_sa_startup (struct usb_serial *serial)
161 {
162         struct usb_device *dev = serial->dev;
163         struct belkin_sa_private *priv;
164
165         /* allocate the private data structure */
166         serial->port->private = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
167         if (!serial->port->private)
168                 return (-1); /* error */
169         priv = (struct belkin_sa_private *)serial->port->private;
170         /* set initial values for control structures */
171         priv->control_state = 0;
172         priv->last_lsr = 0;
173         priv->last_msr = 0;
174         /* see comments at top of file */
175         priv->bad_flow_control = (dev->descriptor.bcdDevice <= 0x0206) ? 1 : 0;
176         info("bcdDevice: %04x, bfc: %d", dev->descriptor.bcdDevice, priv->bad_flow_control);
177
178         init_waitqueue_head(&serial->port->write_wait);
179         
180         return (0);
181 }
182
183
184 static void belkin_sa_shutdown (struct usb_serial *serial)
185 {
186         int i;
187         
188         dbg ("%s", __FUNCTION__);
189
190         /* stop reads and writes on all ports */
191         for (i=0; i < serial->num_ports; ++i) {
192                 /* My special items, the standard routines free my urbs */
193                 if (serial->port[i].private)
194                         kfree(serial->port[i].private);
195         }
196 }
197
198
199 static int  belkin_sa_open (struct usb_serial_port *port, struct file *filp)
200 {
201         int retval = 0;
202
203         dbg("%s port %d", __FUNCTION__, port->number);
204
205         /*Start reading from the device*/
206         /* TODO: Look at possibility of submitting mulitple URBs to device to
207          *       enhance buffering.  Win trace shows 16 initial read URBs.
208          */
209         port->read_urb->dev = port->serial->dev;
210         retval = usb_submit_urb(port->read_urb);
211         if (retval) {
212                 err("usb_submit_urb(read bulk) failed");
213                 goto exit;
214         }
215
216         port->interrupt_in_urb->dev = port->serial->dev;
217         retval = usb_submit_urb(port->interrupt_in_urb);
218         if (retval)
219                 err(" usb_submit_urb(read int) failed");
220
221 exit:
222         return retval;
223 } /* belkin_sa_open */
224
225
226 static void belkin_sa_close (struct usb_serial_port *port, struct file *filp)
227 {
228         struct usb_serial *serial;
229
230         if (port_paranoia_check (port, __FUNCTION__))
231                 return;
232
233         serial = get_usb_serial (port, __FUNCTION__);
234         if (!serial)
235                 return;
236
237         dbg("%s port %d", __FUNCTION__, port->number);
238
239         if (serial->dev) {
240                 /* shutdown our bulk reads and writes */
241                 usb_unlink_urb (port->write_urb);
242                 usb_unlink_urb (port->read_urb);
243                 usb_unlink_urb (port->interrupt_in_urb);
244         }
245 } /* belkin_sa_close */
246
247
248 static void belkin_sa_read_int_callback (struct urb *urb)
249 {
250         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
251         struct belkin_sa_private *priv;
252         struct usb_serial *serial;
253         unsigned char *data = urb->transfer_buffer;
254
255         /* the urb might have been killed. */
256         if (urb->status)
257                 return;
258         
259         if (port_paranoia_check (port, __FUNCTION__)) return;
260
261         serial = port->serial;
262         if (serial_paranoia_check (serial, __FUNCTION__)) return;
263         
264         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
265
266         /* Handle known interrupt data */
267         /* ignore data[0] and data[1] */
268
269         priv = (struct belkin_sa_private *)port->private;
270         priv->last_msr = data[BELKIN_SA_MSR_INDEX];
271         
272         /* Record Control Line states */
273         if (priv->last_msr & BELKIN_SA_MSR_DSR)
274                 priv->control_state |= TIOCM_DSR;
275         else
276                 priv->control_state &= ~TIOCM_DSR;
277
278         if (priv->last_msr & BELKIN_SA_MSR_CTS)
279                 priv->control_state |= TIOCM_CTS;
280         else
281                 priv->control_state &= ~TIOCM_CTS;
282
283         if (priv->last_msr & BELKIN_SA_MSR_RI)
284                 priv->control_state |= TIOCM_RI;
285         else
286                 priv->control_state &= ~TIOCM_RI;
287
288         if (priv->last_msr & BELKIN_SA_MSR_CD)
289                 priv->control_state |= TIOCM_CD;
290         else
291                 priv->control_state &= ~TIOCM_CD;
292
293         /* Now to report any errors */
294         priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
295 #if 0
296         /*
297          * fill in the flip buffer here, but I do not know the relation
298          * to the current/next receive buffer or characters.  I need
299          * to look in to this before committing any code.
300          */
301         if (priv->last_lsr & BELKIN_SA_LSR_ERR) {
302                 tty = port->tty;
303                 /* Overrun Error */
304                 if (priv->last_lsr & BELKIN_SA_LSR_OE) {
305                 }
306                 /* Parity Error */
307                 if (priv->last_lsr & BELKIN_SA_LSR_PE) {
308                 }
309                 /* Framing Error */
310                 if (priv->last_lsr & BELKIN_SA_LSR_FE) {
311                 }
312                 /* Break Indicator */
313                 if (priv->last_lsr & BELKIN_SA_LSR_BI) {
314                 }
315         }
316 #endif
317
318         /* INT urbs are automatically re-submitted */
319 }
320
321 static void belkin_sa_set_termios (struct usb_serial_port *port, struct termios *old_termios)
322 {
323         struct usb_serial *serial = port->serial;
324         struct belkin_sa_private *priv = (struct belkin_sa_private *)port->private;
325         unsigned int iflag;
326         unsigned int cflag;
327         unsigned int old_iflag = 0;
328         unsigned int old_cflag = 0;
329         __u16 urb_value = 0; /* Will hold the new flags */
330         
331         if ((!port->tty) || (!port->tty->termios)) {
332                 dbg ("%s - no tty or termios structure", __FUNCTION__);
333                 return;
334         }
335
336         iflag = port->tty->termios->c_iflag;
337         cflag = port->tty->termios->c_cflag;
338
339         /* check that they really want us to change something */
340         if (old_termios) {
341                 if ((cflag == old_termios->c_cflag) &&
342                     (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
343                         dbg("%s - nothing to change...", __FUNCTION__);
344                         return;
345                 }
346                 old_iflag = old_termios->c_iflag;
347                 old_cflag = old_termios->c_cflag;
348         }
349
350         /* Set the baud rate */
351         if( (cflag&CBAUD) != (old_cflag&CBAUD) ) {
352                 /* reassert DTR and (maybe) RTS on transition from B0 */
353                 if( (old_cflag&CBAUD) == B0 ) {
354                         priv->control_state |= (TIOCM_DTR|TIOCM_RTS);
355                         if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
356                                 err("Set DTR error");
357                         /* don't set RTS if using hardware flow control */
358                         if (!(old_cflag&CRTSCTS) )
359                                 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 1) < 0)
360                                         err("Set RTS error");
361                 }
362
363                 switch(cflag & CBAUD) {
364                         case B0: /* handled below */ break;
365                         case B300: urb_value = BELKIN_SA_BAUD(300); break;
366                         case B600: urb_value = BELKIN_SA_BAUD(600); break;
367                         case B1200: urb_value = BELKIN_SA_BAUD(1200); break;
368                         case B2400: urb_value = BELKIN_SA_BAUD(2400); break;
369                         case B4800: urb_value = BELKIN_SA_BAUD(4800); break;
370                         case B9600: urb_value = BELKIN_SA_BAUD(9600); break;
371                         case B19200: urb_value = BELKIN_SA_BAUD(19200); break;
372                         case B38400: urb_value = BELKIN_SA_BAUD(38400); break;
373                         case B57600: urb_value = BELKIN_SA_BAUD(57600); break;
374                         case B115200: urb_value = BELKIN_SA_BAUD(115200); break;
375                         case B230400: urb_value = BELKIN_SA_BAUD(230400); break;
376                         default: err("BELKIN USB Serial Adapter: unsupported baudrate request, using default of 9600");
377                                 urb_value = BELKIN_SA_BAUD(9600); break;
378                 }
379                 if ((cflag & CBAUD) != B0 ) {
380                         if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
381                                 err("Set baudrate error");
382                 } else {
383                         /* Disable flow control */
384                         if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, BELKIN_SA_FLOW_NONE) < 0)
385                                 err("Disable flowcontrol error");
386
387                         /* Drop RTS and DTR */
388                         priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
389                         if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
390                                 err("DTR LOW error");
391                         if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
392                                 err("RTS LOW error");
393                 }
394         }
395
396         /* set the parity */
397         if( (cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD)) ) {
398                 if (cflag & PARENB)
399                         urb_value = (cflag & PARODD) ?  BELKIN_SA_PARITY_ODD : BELKIN_SA_PARITY_EVEN;
400                 else
401                         urb_value = BELKIN_SA_PARITY_NONE;
402                 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
403                         err("Set parity error");
404         }
405
406         /* set the number of data bits */
407         if( (cflag&CSIZE) != (old_cflag&CSIZE) ) {
408                 switch (cflag & CSIZE) {
409                         case CS5: urb_value = BELKIN_SA_DATA_BITS(5); break;
410                         case CS6: urb_value = BELKIN_SA_DATA_BITS(6); break;
411                         case CS7: urb_value = BELKIN_SA_DATA_BITS(7); break;
412                         case CS8: urb_value = BELKIN_SA_DATA_BITS(8); break;
413                         default: err("CSIZE was not CS5-CS8, using default of 8");
414                                 urb_value = BELKIN_SA_DATA_BITS(8);
415                                 break;
416                 }
417                 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
418                         err("Set data bits error");
419         }
420
421         /* set the number of stop bits */
422         if( (cflag&CSTOPB) != (old_cflag&CSTOPB) ) {
423                 urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2) : BELKIN_SA_STOP_BITS(1);
424                 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST, urb_value) < 0)
425                         err("Set stop bits error");
426         }
427
428         /* Set flow control */
429         if( (iflag&IXOFF)   != (old_iflag&IXOFF)
430         ||      (iflag&IXON)    != (old_iflag&IXON)
431         ||  (cflag&CRTSCTS) != (old_cflag&CRTSCTS) ) {
432                 urb_value = 0;
433                 if ((iflag & IXOFF) || (iflag & IXON))
434                         urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
435                 else
436                         urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
437
438                 if (cflag & CRTSCTS)
439                         urb_value |=  (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
440                 else
441                         urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
442
443                 if (priv->bad_flow_control)
444                         urb_value &= ~(BELKIN_SA_FLOW_IRTS);
445
446                 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
447                         err("Set flow control error");
448         }
449 } /* belkin_sa_set_termios */
450
451
452 static void belkin_sa_break_ctl( struct usb_serial_port *port, int break_state )
453 {
454         struct usb_serial *serial = port->serial;
455
456         if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
457                 err("Set break_ctl %d", break_state);
458 }
459
460
461 static int belkin_sa_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
462 {
463         struct usb_serial *serial = port->serial;
464         __u16 urb_value; /* Will hold the new flags */
465         struct belkin_sa_private *priv = (struct belkin_sa_private *)port->private;
466         int  ret, mask;
467         
468         /* Based on code from acm.c and others */
469         switch (cmd) {
470         case TIOCMGET:
471                 return put_user(priv->control_state, (unsigned long *) arg);
472                 break;
473
474         case TIOCMSET: /* Turns on and off the lines as specified by the mask */
475         case TIOCMBIS: /* turns on (Sets) the lines as specified by the mask */
476         case TIOCMBIC: /* turns off (Clears) the lines as specified by the mask */
477                 if (get_user(mask, (unsigned long *) arg))
478                         return -EFAULT;
479
480                 if ((cmd == TIOCMSET) || (mask & TIOCM_RTS)) {
481                         /* RTS needs set */
482                         urb_value = ((cmd == TIOCMSET) && (mask & TIOCM_RTS)) || (cmd == TIOCMBIS) ? 1 : 0;
483                         if (urb_value)
484                                 priv->control_state |= TIOCM_RTS;
485                         else
486                                 priv->control_state &= ~TIOCM_RTS;
487
488                         if ((ret = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, urb_value)) < 0) {
489                                 err("Set RTS error %d", ret);
490                                 return(ret);
491                         }
492                 }
493
494                 if ((cmd == TIOCMSET) || (mask & TIOCM_DTR)) {
495                         /* DTR needs set */
496                         urb_value = ((cmd == TIOCMSET) && (mask & TIOCM_DTR)) || (cmd == TIOCMBIS) ? 1 : 0;
497                         if (urb_value)
498                                 priv->control_state |= TIOCM_DTR;
499                         else
500                                 priv->control_state &= ~TIOCM_DTR;
501                         if ((ret = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, urb_value)) < 0) {
502                                 err("Set DTR error %d", ret);
503                                 return(ret);
504                         }
505                 }
506                 break;
507                                         
508         case TIOCMIWAIT:
509                 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
510                 /* TODO */
511                 return( 0 );
512
513         case TIOCGICOUNT:
514                 /* return count of modemline transitions */
515                 /* TODO */
516                 return 0;
517
518         default:
519                 dbg("belkin_sa_ioctl arg not supported - 0x%04x",cmd);
520                 return(-ENOIOCTLCMD);
521                 break;
522         }
523         return 0;
524 } /* belkin_sa_ioctl */
525
526
527 static int __init belkin_sa_init (void)
528 {
529         usb_serial_register (&belkin_device);
530         info(DRIVER_DESC " " DRIVER_VERSION);
531         return 0;
532 }
533
534
535 static void __exit belkin_sa_exit (void)
536 {
537         usb_serial_deregister (&belkin_device);
538 }
539
540
541 module_init (belkin_sa_init);
542 module_exit (belkin_sa_exit);
543
544 MODULE_AUTHOR( DRIVER_AUTHOR );
545 MODULE_DESCRIPTION( DRIVER_DESC );
546 MODULE_LICENSE("GPL");
547
548 MODULE_PARM(debug, "i");
549 MODULE_PARM_DESC(debug, "Debug enabled or not");
550