]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/usb/serial/belkin_sa.c
eb12616dbf6fa803b34d52491fa830735dac0863
[linux-beck.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  *  Copyright (C) 2010          Johan Hovold (jhovold@gmail.com)
7  *
8  *  This program is largely derived from work by the linux-usb group
9  *  and associated source files.  Please see the usb/serial files for
10  *  individual credits and copyrights.
11  *
12  *      This program is free software; you can redistribute it and/or modify
13  *      it under the terms of the GNU General Public License as published by
14  *      the Free Software Foundation; either version 2 of the License, or
15  *      (at your option) any later version.
16  *
17  * See Documentation/usb/usb-serial.txt for more information on using this
18  * driver
19  *
20  * TODO:
21  * -- Add true modem contol line query capability.  Currently we track the
22  *    states reported by the interrupt and the states we request.
23  * -- Add support for flush commands
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/tty.h>
31 #include <linux/tty_driver.h>
32 #include <linux/tty_flip.h>
33 #include <linux/module.h>
34 #include <linux/spinlock.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
38 #include "belkin_sa.h"
39
40 static bool debug;
41
42 /*
43  * Version Information
44  */
45 #define DRIVER_VERSION "v1.3"
46 #define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
47 #define DRIVER_DESC "USB Belkin Serial converter driver"
48
49 /* function prototypes for a Belkin USB Serial Adapter F5U103 */
50 static int  belkin_sa_startup(struct usb_serial *serial);
51 static void belkin_sa_release(struct usb_serial *serial);
52 static int  belkin_sa_open(struct tty_struct *tty,
53                         struct usb_serial_port *port);
54 static void belkin_sa_close(struct usb_serial_port *port);
55 static void belkin_sa_read_int_callback(struct urb *urb);
56 static void belkin_sa_process_read_urb(struct urb *urb);
57 static void belkin_sa_set_termios(struct tty_struct *tty,
58                         struct usb_serial_port *port, struct ktermios * old);
59 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
60 static int  belkin_sa_tiocmget(struct tty_struct *tty);
61 static int  belkin_sa_tiocmset(struct tty_struct *tty,
62                                         unsigned int set, unsigned int clear);
63
64
65 static const struct usb_device_id id_table_combined[] = {
66         { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
67         { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
68         { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
69         { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
70         { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
71         { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
72         { }     /* Terminating entry */
73 };
74 MODULE_DEVICE_TABLE(usb, id_table_combined);
75
76 static struct usb_driver belkin_driver = {
77         .name =         "belkin",
78         .disconnect =   usb_serial_disconnect,
79         .id_table =     id_table_combined,
80 };
81
82 /* All of the device info needed for the serial converters */
83 static struct usb_serial_driver belkin_device = {
84         .driver = {
85                 .owner =        THIS_MODULE,
86                 .name =         "belkin",
87         },
88         .description =          "Belkin / Peracom / GoHubs USB Serial Adapter",
89         .id_table =             id_table_combined,
90         .num_ports =            1,
91         .open =                 belkin_sa_open,
92         .close =                belkin_sa_close,
93         .read_int_callback =    belkin_sa_read_int_callback,
94         .process_read_urb =     belkin_sa_process_read_urb,
95         .set_termios =          belkin_sa_set_termios,
96         .break_ctl =            belkin_sa_break_ctl,
97         .tiocmget =             belkin_sa_tiocmget,
98         .tiocmset =             belkin_sa_tiocmset,
99         .attach =               belkin_sa_startup,
100         .release =              belkin_sa_release,
101 };
102
103 static struct usb_serial_driver * const serial_drivers[] = {
104         &belkin_device, NULL
105 };
106
107 struct belkin_sa_private {
108         spinlock_t              lock;
109         unsigned long           control_state;
110         unsigned char           last_lsr;
111         unsigned char           last_msr;
112         int                     bad_flow_control;
113 };
114
115
116 /*
117  * ***************************************************************************
118  * Belkin USB Serial Adapter F5U103 specific driver functions
119  * ***************************************************************************
120  */
121
122 #define WDR_TIMEOUT 5000 /* default urb timeout */
123
124 /* assumes that struct usb_serial *serial is available */
125 #define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
126                                             (c), BELKIN_SA_SET_REQUEST_TYPE, \
127                                             (v), 0, NULL, 0, WDR_TIMEOUT)
128
129 /* do some startup allocations not currently performed by usb_serial_probe() */
130 static int belkin_sa_startup(struct usb_serial *serial)
131 {
132         struct usb_device *dev = serial->dev;
133         struct belkin_sa_private *priv;
134
135         /* allocate the private data structure */
136         priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
137         if (!priv)
138                 return -1; /* error */
139         /* set initial values for control structures */
140         spin_lock_init(&priv->lock);
141         priv->control_state = 0;
142         priv->last_lsr = 0;
143         priv->last_msr = 0;
144         /* see comments at top of file */
145         priv->bad_flow_control =
146                 (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
147         dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
148                                         le16_to_cpu(dev->descriptor.bcdDevice),
149                                         priv->bad_flow_control);
150
151         init_waitqueue_head(&serial->port[0]->write_wait);
152         usb_set_serial_port_data(serial->port[0], priv);
153
154         return 0;
155 }
156
157 static void belkin_sa_release(struct usb_serial *serial)
158 {
159         int i;
160
161         for (i = 0; i < serial->num_ports; ++i)
162                 kfree(usb_get_serial_port_data(serial->port[i]));
163 }
164
165 static int belkin_sa_open(struct tty_struct *tty,
166                                         struct usb_serial_port *port)
167 {
168         int retval;
169
170         retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
171         if (retval) {
172                 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
173                 return retval;
174         }
175
176         retval = usb_serial_generic_open(tty, port);
177         if (retval)
178                 usb_kill_urb(port->interrupt_in_urb);
179
180         return retval;
181 }
182
183 static void belkin_sa_close(struct usb_serial_port *port)
184 {
185         usb_serial_generic_close(port);
186         usb_kill_urb(port->interrupt_in_urb);
187 }
188
189 static void belkin_sa_read_int_callback(struct urb *urb)
190 {
191         struct usb_serial_port *port = urb->context;
192         struct belkin_sa_private *priv;
193         unsigned char *data = urb->transfer_buffer;
194         int retval;
195         int status = urb->status;
196         unsigned long flags;
197
198         switch (status) {
199         case 0:
200                 /* success */
201                 break;
202         case -ECONNRESET:
203         case -ENOENT:
204         case -ESHUTDOWN:
205                 /* this urb is terminated, clean up */
206                 dbg("%s - urb shutting down with status: %d",
207                     __func__, status);
208                 return;
209         default:
210                 dbg("%s - nonzero urb status received: %d",
211                     __func__, status);
212                 goto exit;
213         }
214
215         usb_serial_debug_data(debug, &port->dev, __func__,
216                                         urb->actual_length, data);
217
218         /* Handle known interrupt data */
219         /* ignore data[0] and data[1] */
220
221         priv = usb_get_serial_port_data(port);
222         spin_lock_irqsave(&priv->lock, flags);
223         priv->last_msr = data[BELKIN_SA_MSR_INDEX];
224
225         /* Record Control Line states */
226         if (priv->last_msr & BELKIN_SA_MSR_DSR)
227                 priv->control_state |= TIOCM_DSR;
228         else
229                 priv->control_state &= ~TIOCM_DSR;
230
231         if (priv->last_msr & BELKIN_SA_MSR_CTS)
232                 priv->control_state |= TIOCM_CTS;
233         else
234                 priv->control_state &= ~TIOCM_CTS;
235
236         if (priv->last_msr & BELKIN_SA_MSR_RI)
237                 priv->control_state |= TIOCM_RI;
238         else
239                 priv->control_state &= ~TIOCM_RI;
240
241         if (priv->last_msr & BELKIN_SA_MSR_CD)
242                 priv->control_state |= TIOCM_CD;
243         else
244                 priv->control_state &= ~TIOCM_CD;
245
246         priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
247         spin_unlock_irqrestore(&priv->lock, flags);
248 exit:
249         retval = usb_submit_urb(urb, GFP_ATOMIC);
250         if (retval)
251                 dev_err(&port->dev, "%s - usb_submit_urb failed with "
252                         "result %d\n", __func__, retval);
253 }
254
255 static void belkin_sa_process_read_urb(struct urb *urb)
256 {
257         struct usb_serial_port *port = urb->context;
258         struct belkin_sa_private *priv = usb_get_serial_port_data(port);
259         struct tty_struct *tty;
260         unsigned char *data = urb->transfer_buffer;
261         unsigned long flags;
262         unsigned char status;
263         char tty_flag;
264
265         /* Update line status */
266         tty_flag = TTY_NORMAL;
267
268         spin_lock_irqsave(&priv->lock, flags);
269         status = priv->last_lsr;
270         priv->last_lsr &= ~BELKIN_SA_LSR_ERR;
271         spin_unlock_irqrestore(&priv->lock, flags);
272
273         if (!urb->actual_length)
274                 return;
275
276         tty = tty_port_tty_get(&port->port);
277         if (!tty)
278                 return;
279
280         if (status & BELKIN_SA_LSR_ERR) {
281                 /* Break takes precedence over parity, which takes precedence
282                  * over framing errors. */
283                 if (status & BELKIN_SA_LSR_BI)
284                         tty_flag = TTY_BREAK;
285                 else if (status & BELKIN_SA_LSR_PE)
286                         tty_flag = TTY_PARITY;
287                 else if (status & BELKIN_SA_LSR_FE)
288                         tty_flag = TTY_FRAME;
289                 dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
290
291                 /* Overrun is special, not associated with a char. */
292                 if (status & BELKIN_SA_LSR_OE)
293                         tty_insert_flip_char(tty, 0, TTY_OVERRUN);
294         }
295
296         tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
297                                                         urb->actual_length);
298         tty_flip_buffer_push(tty);
299         tty_kref_put(tty);
300 }
301
302 static void belkin_sa_set_termios(struct tty_struct *tty,
303                 struct usb_serial_port *port, struct ktermios *old_termios)
304 {
305         struct usb_serial *serial = port->serial;
306         struct belkin_sa_private *priv = usb_get_serial_port_data(port);
307         unsigned int iflag;
308         unsigned int cflag;
309         unsigned int old_iflag = 0;
310         unsigned int old_cflag = 0;
311         __u16 urb_value = 0; /* Will hold the new flags */
312         unsigned long flags;
313         unsigned long control_state;
314         int bad_flow_control;
315         speed_t baud;
316         struct ktermios *termios = tty->termios;
317
318         iflag = termios->c_iflag;
319         cflag = termios->c_cflag;
320
321         termios->c_cflag &= ~CMSPAR;
322
323         /* get a local copy of the current port settings */
324         spin_lock_irqsave(&priv->lock, flags);
325         control_state = priv->control_state;
326         bad_flow_control = priv->bad_flow_control;
327         spin_unlock_irqrestore(&priv->lock, flags);
328
329         old_iflag = old_termios->c_iflag;
330         old_cflag = old_termios->c_cflag;
331
332         /* Set the baud rate */
333         if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
334                 /* reassert DTR and (maybe) RTS on transition from B0 */
335                 if ((old_cflag & CBAUD) == B0) {
336                         control_state |= (TIOCM_DTR|TIOCM_RTS);
337                         if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
338                                 dev_err(&port->dev, "Set DTR error\n");
339                         /* don't set RTS if using hardware flow control */
340                         if (!(old_cflag & CRTSCTS))
341                                 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
342                                                                 , 1) < 0)
343                                         dev_err(&port->dev, "Set RTS error\n");
344                 }
345         }
346
347         baud = tty_get_baud_rate(tty);
348         if (baud) {
349                 urb_value = BELKIN_SA_BAUD(baud);
350                 /* Clip to maximum speed */
351                 if (urb_value == 0)
352                         urb_value = 1;
353                 /* Turn it back into a resulting real baud rate */
354                 baud = BELKIN_SA_BAUD(urb_value);
355
356                 /* Report the actual baud rate back to the caller */
357                 tty_encode_baud_rate(tty, baud, baud);
358                 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
359                         dev_err(&port->dev, "Set baudrate error\n");
360         } else {
361                 /* Disable flow control */
362                 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
363                                                 BELKIN_SA_FLOW_NONE) < 0)
364                         dev_err(&port->dev, "Disable flowcontrol error\n");
365                 /* Drop RTS and DTR */
366                 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
367                 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
368                         dev_err(&port->dev, "DTR LOW error\n");
369                 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
370                         dev_err(&port->dev, "RTS LOW error\n");
371         }
372
373         /* set the parity */
374         if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
375                 if (cflag & PARENB)
376                         urb_value = (cflag & PARODD) ?  BELKIN_SA_PARITY_ODD
377                                                 : BELKIN_SA_PARITY_EVEN;
378                 else
379                         urb_value = BELKIN_SA_PARITY_NONE;
380                 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
381                         dev_err(&port->dev, "Set parity error\n");
382         }
383
384         /* set the number of data bits */
385         if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
386                 switch (cflag & CSIZE) {
387                 case CS5:
388                         urb_value = BELKIN_SA_DATA_BITS(5);
389                         break;
390                 case CS6:
391                         urb_value = BELKIN_SA_DATA_BITS(6);
392                         break;
393                 case CS7:
394                         urb_value = BELKIN_SA_DATA_BITS(7);
395                         break;
396                 case CS8:
397                         urb_value = BELKIN_SA_DATA_BITS(8);
398                         break;
399                 default:
400                         dbg("CSIZE was not CS5-CS8, using default of 8");
401                         urb_value = BELKIN_SA_DATA_BITS(8);
402                         break;
403                 }
404                 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
405                         dev_err(&port->dev, "Set data bits error\n");
406         }
407
408         /* set the number of stop bits */
409         if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
410                 urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
411                                                 : BELKIN_SA_STOP_BITS(1);
412                 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
413                                                         urb_value) < 0)
414                         dev_err(&port->dev, "Set stop bits error\n");
415         }
416
417         /* Set flow control */
418         if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
419                 ((cflag ^ old_cflag) & CRTSCTS)) {
420                 urb_value = 0;
421                 if ((iflag & IXOFF) || (iflag & IXON))
422                         urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
423                 else
424                         urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
425
426                 if (cflag & CRTSCTS)
427                         urb_value |=  (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
428                 else
429                         urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
430
431                 if (bad_flow_control)
432                         urb_value &= ~(BELKIN_SA_FLOW_IRTS);
433
434                 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
435                         dev_err(&port->dev, "Set flow control error\n");
436         }
437
438         /* save off the modified port settings */
439         spin_lock_irqsave(&priv->lock, flags);
440         priv->control_state = control_state;
441         spin_unlock_irqrestore(&priv->lock, flags);
442 }
443
444 static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
445 {
446         struct usb_serial_port *port = tty->driver_data;
447         struct usb_serial *serial = port->serial;
448
449         if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
450                 dev_err(&port->dev, "Set break_ctl %d\n", break_state);
451 }
452
453 static int belkin_sa_tiocmget(struct tty_struct *tty)
454 {
455         struct usb_serial_port *port = tty->driver_data;
456         struct belkin_sa_private *priv = usb_get_serial_port_data(port);
457         unsigned long control_state;
458         unsigned long flags;
459
460         spin_lock_irqsave(&priv->lock, flags);
461         control_state = priv->control_state;
462         spin_unlock_irqrestore(&priv->lock, flags);
463
464         return control_state;
465 }
466
467 static int belkin_sa_tiocmset(struct tty_struct *tty,
468                                unsigned int set, unsigned int clear)
469 {
470         struct usb_serial_port *port = tty->driver_data;
471         struct usb_serial *serial = port->serial;
472         struct belkin_sa_private *priv = usb_get_serial_port_data(port);
473         unsigned long control_state;
474         unsigned long flags;
475         int retval;
476         int rts = 0;
477         int dtr = 0;
478
479         spin_lock_irqsave(&priv->lock, flags);
480         control_state = priv->control_state;
481
482         if (set & TIOCM_RTS) {
483                 control_state |= TIOCM_RTS;
484                 rts = 1;
485         }
486         if (set & TIOCM_DTR) {
487                 control_state |= TIOCM_DTR;
488                 dtr = 1;
489         }
490         if (clear & TIOCM_RTS) {
491                 control_state &= ~TIOCM_RTS;
492                 rts = 0;
493         }
494         if (clear & TIOCM_DTR) {
495                 control_state &= ~TIOCM_DTR;
496                 dtr = 0;
497         }
498
499         priv->control_state = control_state;
500         spin_unlock_irqrestore(&priv->lock, flags);
501
502         retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
503         if (retval < 0) {
504                 dev_err(&port->dev, "Set RTS error %d\n", retval);
505                 goto exit;
506         }
507
508         retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
509         if (retval < 0) {
510                 dev_err(&port->dev, "Set DTR error %d\n", retval);
511                 goto exit;
512         }
513 exit:
514         return retval;
515 }
516
517 module_usb_serial_driver(belkin_driver, serial_drivers);
518
519 MODULE_AUTHOR(DRIVER_AUTHOR);
520 MODULE_DESCRIPTION(DRIVER_DESC);
521 MODULE_VERSION(DRIVER_VERSION);
522 MODULE_LICENSE("GPL");
523
524 module_param(debug, bool, S_IRUGO | S_IWUSR);
525 MODULE_PARM_DESC(debug, "Debug enabled or not");