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