MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / serial / option.c.2.6.23
blobb319e6a9d522616cea1136ff1005fe6770846d7d
1 /*
2   USB Driver for GSM modems
4   Copyright (C) 2005  Matthias Urlichs <smurf@smurf.noris.de>
6   This driver is free software; you can redistribute it and/or modify
7   it under the terms of Version 2 of the GNU General Public License as
8   published by the Free Software Foundation.
10   Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
12   History: see the git log.
14   Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
16   This driver exists because the "normal" serial driver doesn't work too well
17   with GSM modems. Issues:
18   - data loss -- one single Receive URB is not nearly enough
19   - nonstandard flow (Option devices) control
20   - controlling the baud rate doesn't make sense
22   This driver is named "option" because the most common device it's
23   used for is a PC-Card (with an internal OHCI-USB interface, behind
24   which the GSM interface sits), made by Option Inc.
26   Some of the "one port" devices actually exhibit multiple USB instances
27   on the USB bus. This is not a bug, these ports are used for different
28   device features.
31 #define DRIVER_VERSION "v0.7.1"
32 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
33 #define DRIVER_DESC "USB Driver for GSM modems"
35 #include <linux/kernel.h>
36 #include <linux/jiffies.h>
37 #include <linux/errno.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/bitops.h>
42 #include <linux/usb.h>
43 //#include <linux/usb/serial.h>
44 #include "usb-serial.h"
46 /* Function prototypes */
47 static int  option_open(struct usb_serial_port *port, struct file *filp);
48 static void option_close(struct usb_serial_port *port, struct file *filp);
49 static int  option_startup(struct usb_serial *serial);
50 static void option_shutdown(struct usb_serial *serial);
51 static void option_rx_throttle(struct usb_serial_port *port);
52 static void option_rx_unthrottle(struct usb_serial_port *port);
53 static int  option_write_room(struct usb_serial_port *port);
55 static void option_instat_callback(struct urb *urb);
57 static int option_write(struct usb_serial_port *port,
58                         const unsigned char *buf, int count);
60 static int  option_chars_in_buffer(struct usb_serial_port *port);
61 static int  option_ioctl(struct usb_serial_port *port, struct file *file,
62                         unsigned int cmd, unsigned long arg);
63 static void option_set_termios(struct usb_serial_port *port,
64                                 struct termios *old);
65 static void option_break_ctl(struct usb_serial_port *port, int break_state);
66 static int  option_tiocmget(struct usb_serial_port *port, struct file *file);
67 static int  option_tiocmset(struct usb_serial_port *port, struct file *file,
68                                 unsigned int set, unsigned int clear);
69 static int  option_send_setup(struct usb_serial_port *port);
71 /* Vendor and product IDs */
72 #define OPTION_VENDOR_ID                        0x0AF0
73 #define OPTION_PRODUCT_COLT                     0x5000
74 #define OPTION_PRODUCT_RICOLA                   0x6000
75 #define OPTION_PRODUCT_RICOLA_LIGHT             0x6100
76 #define OPTION_PRODUCT_RICOLA_QUAD              0x6200
77 #define OPTION_PRODUCT_RICOLA_QUAD_LIGHT        0x6300
78 #define OPTION_PRODUCT_RICOLA_NDIS              0x6050
79 #define OPTION_PRODUCT_RICOLA_NDIS_LIGHT        0x6150
80 #define OPTION_PRODUCT_RICOLA_NDIS_QUAD         0x6250
81 #define OPTION_PRODUCT_RICOLA_NDIS_QUAD_LIGHT   0x6350
82 #define OPTION_PRODUCT_COBRA                    0x6500
83 #define OPTION_PRODUCT_COBRA_BUS                0x6501
84 #define OPTION_PRODUCT_VIPER                    0x6600
85 #define OPTION_PRODUCT_VIPER_BUS                0x6601
86 #define OPTION_PRODUCT_GT_MAX_READY             0x6701
87 #define OPTION_PRODUCT_GT_MAX                   0x6711
88 #define OPTION_PRODUCT_FUJI_MODEM_LIGHT         0x6721
89 #define OPTION_PRODUCT_FUJI_MODEM_GT            0x6741
90 #define OPTION_PRODUCT_FUJI_MODEM_EX            0x6761
91 #define OPTION_PRODUCT_FUJI_NETWORK_LIGHT       0x6731
92 #define OPTION_PRODUCT_FUJI_NETWORK_GT          0x6751
93 #define OPTION_PRODUCT_FUJI_NETWORK_EX          0x6771
94 #define OPTION_PRODUCT_KOI_MODEM                0x6800
95 #define OPTION_PRODUCT_KOI_NETWORK              0x6811
96 #define OPTION_PRODUCT_SCORPION_MODEM           0x6901
97 #define OPTION_PRODUCT_SCORPION_NETWORK         0x6911
98 #define OPTION_PRODUCT_ETNA_MODEM               0x7001
99 #define OPTION_PRODUCT_ETNA_NETWORK             0x7011
100 #define OPTION_PRODUCT_ETNA_MODEM_LITE          0x7021
101 #define OPTION_PRODUCT_ETNA_MODEM_GT            0x7041
102 #define OPTION_PRODUCT_ETNA_MODEM_EX            0x7061
103 #define OPTION_PRODUCT_ETNA_NETWORK_LITE        0x7031
104 #define OPTION_PRODUCT_ETNA_NETWORK_GT          0x7051
105 #define OPTION_PRODUCT_ETNA_NETWORK_EX          0x7071
106 #define OPTION_PRODUCT_ETNA_KOI_MODEM           0x7100
107 #define OPTION_PRODUCT_ETNA_KOI_NETWORK         0x7111
109 #define HUAWEI_VENDOR_ID                        0x12D1
110 #define HUAWEI_PRODUCT_E600                     0x1001
111 #define HUAWEI_PRODUCT_E220                     0x1003
112 #define HUAWEI_PRODUCT_E220BIS                  0x1004
114 #define NOVATELWIRELESS_VENDOR_ID               0x1410
115 #define DELL_VENDOR_ID                          0x413C
117 #define ANYDATA_VENDOR_ID                       0x16d5
118 #define ANYDATA_PRODUCT_ADU_E100A               0x6501
119 #define ANYDATA_PRODUCT_ADU_500A                0x6502
121 #define BANDRICH_VENDOR_ID                      0x1A8D
122 #define BANDRICH_PRODUCT_C100_1                 0x1002
123 #define BANDRICH_PRODUCT_C100_2                 0x1003
125 static struct usb_device_id option_ids[] = {
126         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COLT) },
127         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA) },
128         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA_LIGHT) },
129         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA_QUAD) },
130         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA_QUAD_LIGHT) },
131         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA_NDIS) },
132         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA_NDIS_LIGHT) },
133         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA_NDIS_QUAD) },
134         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_RICOLA_NDIS_QUAD_LIGHT) },
135         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
136         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA_BUS) },
137         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_VIPER) },
138         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_VIPER_BUS) },
139         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_GT_MAX_READY) },
140         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_GT_MAX) },
141         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUJI_MODEM_LIGHT) },
142         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUJI_MODEM_GT) },
143         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUJI_MODEM_EX) },
144         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUJI_NETWORK_LIGHT) },
145         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUJI_NETWORK_GT) },
146         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUJI_NETWORK_EX) },
147         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_KOI_MODEM) },
148         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_KOI_NETWORK) },
149         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_SCORPION_MODEM) },
150         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_SCORPION_NETWORK) },
151         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_MODEM) },
152         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_NETWORK) },
153         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_MODEM_LITE) },
154         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_MODEM_GT) },
155         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_MODEM_EX) },
156         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_NETWORK_LITE) },
157         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_NETWORK_GT) },
158         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_NETWORK_EX) },
159         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_KOI_MODEM) },
160         { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_ETNA_KOI_NETWORK) },
161         { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
162         { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E220) },
163         { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E220BIS) },
164         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1100) }, /* Novatel Merlin XS620/S640 */
165         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1110) }, /* Novatel Merlin S620 */
166         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1120) }, /* Novatel Merlin EX720 */
167         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1130) }, /* Novatel Merlin S720 */
168         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1400) }, /* Novatel U730 */
169         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1410) }, /* Novatel U740 */
170         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1420) }, /* Novatel EU870 */
171         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
172         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2100) }, /* Novatel EV620 CDMA/EV-DO */
173         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2110) }, /* Novatel Merlin ES620 / Merlin ES720 / Ovation U720 */
174         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2130) }, /* Novatel Merlin ES620 SM Bus */
175         { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, 0x2410) }, /* Novatel EU740 */
176         { USB_DEVICE(DELL_VENDOR_ID, 0x8114) }, /* Dell Wireless 5700 Mobile Broadband CDMA/EVDO Mini-Card == Novatel Expedite EV620 CDMA/EV-DO */
177         { USB_DEVICE(DELL_VENDOR_ID, 0x8115) }, /* Dell Wireless 5500 Mobile Broadband HSDPA Mini-Card == Novatel Expedite EU740 HSDPA/3G */
178         { USB_DEVICE(DELL_VENDOR_ID, 0x8116) }, /* Dell Wireless 5505 Mobile Broadband HSDPA Mini-Card == Novatel Expedite EU740 HSDPA/3G */
179         { USB_DEVICE(DELL_VENDOR_ID, 0x8117) }, /* Dell Wireless 5700 Mobile Broadband CDMA/EVDO ExpressCard == Novatel Merlin XV620 CDMA/EV-DO */
180         { USB_DEVICE(DELL_VENDOR_ID, 0x8118) }, /* Dell Wireless 5510 Mobile Broadband HSDPA ExpressCard == Novatel Merlin XU870 HSDPA/3G */
181         { USB_DEVICE(DELL_VENDOR_ID, 0x8128) }, /* Dell Wireless 5700 Mobile Broadband CDMA/EVDO Mini-Card == Novatel Expedite E720 CDMA/EV-DO */
182         { USB_DEVICE(DELL_VENDOR_ID, 0x8137) }, /* Dell Wireless HSDPA 5520 */
183         { USB_DEVICE(ANYDATA_VENDOR_ID, ANYDATA_PRODUCT_ADU_E100A) },
184         { USB_DEVICE(ANYDATA_VENDOR_ID, ANYDATA_PRODUCT_ADU_500A) },
185         { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_1) },
186         { USB_DEVICE(BANDRICH_VENDOR_ID, BANDRICH_PRODUCT_C100_2) },
187         { } /* Terminating entry */
189 MODULE_DEVICE_TABLE(usb, option_ids);
191 static struct usb_driver option_driver = {
192         .name       = "option",
193         .probe      = usb_serial_probe,
194         .disconnect = usb_serial_disconnect,
195         .id_table   = option_ids,
196 #if 1  // Add by Jared 03-13-2008
197         .owner      = THIS_MODULE,
198 #else
199         .no_dynamic_id =        1,
200 #endif
204 /* The card has three separate interfaces, which the serial driver
205  * recognizes separately, thus num_port=1.
206  */
208 static struct usb_serial_device_type option_1port_device = {
209         /*.driver = {
210                 .owner =        THIS_MODULE,
211                 .name =         "option1",
212         },
213         .description       = "GSM modem (1-port)",*/
214         .owner      = THIS_MODULE,
215         .name              = "Option 3G data card",
216         .short_name        = "option",
217         //.usb_driver        = &option_driver,
218         .id_table          = option_ids,
219         .num_interrupt_in  = NUM_DONT_CARE,
220         .num_bulk_in       = NUM_DONT_CARE,
221         .num_bulk_out      = NUM_DONT_CARE,
222         .num_ports         = 1,
223         .open              = option_open,
224         .close             = option_close,
225         .write             = option_write,
226         .write_room        = option_write_room,
227         .chars_in_buffer   = option_chars_in_buffer,
228         .throttle          = option_rx_throttle,
229         .unthrottle        = option_rx_unthrottle,
230         .ioctl             = option_ioctl,
231         .set_termios       = option_set_termios,
232         .break_ctl         = option_break_ctl,
233         .tiocmget          = option_tiocmget,
234         .tiocmset          = option_tiocmset,
235         .attach            = option_startup,
236         .shutdown          = option_shutdown,
237         .read_int_callback = option_instat_callback,
240 #ifdef CONFIG_USB_DEBUG
241 static int debug;
242 #else
243 #define debug 0
244 #endif
246 /* per port private data */
248 #define N_IN_URB 4
249 #define N_OUT_URB 1
250 #define IN_BUFLEN 4096
251 #define OUT_BUFLEN 128
253 struct option_port_private {
254         /* Input endpoints and buffer for this port */
255         struct urb *in_urbs[N_IN_URB];
256         char in_buffer[N_IN_URB][IN_BUFLEN];
257         /* Output endpoints and buffer for this port */
258         struct urb *out_urbs[N_OUT_URB];
259         char out_buffer[N_OUT_URB][OUT_BUFLEN];
260         unsigned long out_busy;         /* Bit vector of URBs in use */
262         /* Settings for the port */
263         int rts_state;  /* Handshaking pins (outputs) */
264         int dtr_state;
265         int cts_state;  /* Handshaking pins (inputs) */
266         int dsr_state;
267         int dcd_state;
268         int ri_state;
270         unsigned long tx_start_time[N_OUT_URB];
273 /* Functions used by new usb-serial code. */
274 static int __init option_init(void)
276         int retval;
277         retval = usb_serial_register(&option_1port_device);
278         if (retval) {
279 printk("%s[%d]%d\n",__FUNCTION__,__LINE__,retval);
280                 goto failed_1port_device_register;
281         }
282         retval = usb_register(&option_driver);
283         if (retval) {
284 printk("%s[%d]%d\n",__FUNCTION__,__LINE__,retval);
285                 goto failed_driver_register;
286         }
288         info(DRIVER_DESC ": " DRIVER_VERSION);
290         return 0;
292 failed_driver_register:
293         usb_serial_deregister (&option_1port_device);
294 failed_1port_device_register:
295         return retval;
298 static void __exit option_exit(void)
300         usb_deregister (&option_driver);
301         usb_serial_deregister (&option_1port_device);
304 module_init(option_init);
305 module_exit(option_exit);
307 static void option_rx_throttle(struct usb_serial_port *port)
309         dbg("%s", __FUNCTION__);
312 static void option_rx_unthrottle(struct usb_serial_port *port)
314         dbg("%s", __FUNCTION__);
317 static void option_break_ctl(struct usb_serial_port *port, int break_state)
319         /* Unfortunately, I don't know how to send a break */
320         dbg("%s", __FUNCTION__);
323 static void option_set_termios(struct usb_serial_port *port,
324                         struct termios *old_termios)
326         dbg("%s", __FUNCTION__);
328         option_send_setup(port);
331 static int option_tiocmget(struct usb_serial_port *port, struct file *file)
333         unsigned int value;
334         struct option_port_private *portdata;
336         portdata = usb_get_serial_port_data(port);
338         value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
339                 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
340                 ((portdata->cts_state) ? TIOCM_CTS : 0) |
341                 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
342                 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
343                 ((portdata->ri_state) ? TIOCM_RNG : 0);
345         return value;
348 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
349                         unsigned int set, unsigned int clear)
351         struct option_port_private *portdata;
353         portdata = usb_get_serial_port_data(port);
355         if (set & TIOCM_RTS)
356                 portdata->rts_state = 1;
357         if (set & TIOCM_DTR)
358                 portdata->dtr_state = 1;
360         if (clear & TIOCM_RTS)
361                 portdata->rts_state = 0;
362         if (clear & TIOCM_DTR)
363                 portdata->dtr_state = 0;
364         return option_send_setup(port);
367 static int option_ioctl(struct usb_serial_port *port, struct file *file,
368                         unsigned int cmd, unsigned long arg)
370         return -ENOIOCTLCMD;
373 /* Write */
374 static int option_write(struct usb_serial_port *port,
375                         const unsigned char *buf, int count)
377         struct option_port_private *portdata;
378         int i;
379         int left, todo;
380         struct urb *this_urb = NULL; /* spurious */
381         int err;
382 printk("option_write\n");
383         portdata = usb_get_serial_port_data(port);
385         dbg("%s: write (%d chars)", __FUNCTION__, count);
387         i = 0;
388         left = count;
389         for (i=0; left > 0 && i < N_OUT_URB; i++) {
390                 todo = left;
391                 if (todo > OUT_BUFLEN)
392                         todo = OUT_BUFLEN;
394                 this_urb = portdata->out_urbs[i];
395                 if (test_and_set_bit(i, &portdata->out_busy)) {
396                         if (time_before(jiffies,
397                                         portdata->tx_start_time[i] + 10 * HZ))
398                                 continue;
399                         usb_unlink_urb(this_urb);
400                         continue;
401                 }
402                 if (this_urb->status != 0)
403                         dbg("usb_write %p failed (err=%d)",
404                                 this_urb, this_urb->status);
406                 dbg("%s: endpoint %d buf %d", __FUNCTION__,
407                         usb_pipeendpoint(this_urb->pipe), i);
409                 /* send the data */
410                 memcpy (this_urb->transfer_buffer, buf, todo);
411                 this_urb->transfer_buffer_length = todo;
413                 this_urb->dev = port->serial->dev;
414                 err = usb_submit_urb(this_urb, GFP_ATOMIC);
415                 if (err) {
416                         dbg("usb_submit_urb %p (write bulk) failed "
417                                 "(%d, has %d)", this_urb,
418                                 err, this_urb->status);
419                         clear_bit(i, &portdata->out_busy);
420                         continue;
421                 }
422                 portdata->tx_start_time[i] = jiffies;
423                 buf += todo;
424                 left -= todo;
425         }
427         count -= left;
428         dbg("%s: wrote (did %d)", __FUNCTION__, count);
429         return count;
432 static void option_indat_callback(struct urb *urb)
434         int err;
435         int endpoint;
436         struct usb_serial_port *port;
437         struct tty_struct *tty;
438         unsigned char *data = urb->transfer_buffer;
439         int status = urb->status;
440 #if 1  // Masked by Jared 03-13-2008
441         int i;
442 #endif
443 printk("option_indat_callback\n");
444         dbg("%s: %p", __FUNCTION__, urb);
446         endpoint = usb_pipeendpoint(urb->pipe);
447         port = (struct usb_serial_port *) urb->context;
449         if (status) {
450                 dbg("%s: nonzero status: %d on endpoint %02x.",
451                     __FUNCTION__, status, endpoint);
452         } else {
453                 tty = port->tty;
454                 if (urb->actual_length) {
455 #if 0  // Masked by Jared 03-13-2008
456                         tty_buffer_request_room(tty, urb->actual_length);
457                         tty_insert_flip_string(tty, data, urb->actual_length);
458 #else
459                         for(i=0; i<urb->actual_length; i++) {
460                                 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
461                                         tty_flip_buffer_push(tty);
462                                 }
463                                 tty_insert_flip_char(tty, data[i], 0);
464                         }
465 #endif
467                         tty_flip_buffer_push(tty);
468                 } else {
469                         dbg("%s: empty read urb received", __FUNCTION__);
470                 }
472                 /* Resubmit urb so we continue receiving */
473                 if (port->open_count && status != -ESHUTDOWN) {
474                         err = usb_submit_urb(urb, GFP_ATOMIC);
475                         if (err)
476                                 printk(KERN_ERR "%s: resubmit read urb failed. "
477                                         "(%d)", __FUNCTION__, err);
478                 }
479         }
480         return;
483 static void option_outdat_callback(struct urb *urb)
485         struct usb_serial_port *port;
486         struct option_port_private *portdata;
487         int i;
489 printk("%s[%d\n",__FUNCTION__,__LINE__);
490         dbg("%s", __FUNCTION__);
492         port = (struct usb_serial_port *) urb->context;
494         usb_serial_port_softint(port);
496         portdata = usb_get_serial_port_data(port);
497         for (i = 0; i < N_OUT_URB; ++i) {
498                 if (portdata->out_urbs[i] == urb) {
499                         smp_mb__before_clear_bit();
500                         clear_bit(i, &portdata->out_busy);
501                         break;
502                 }
503         }
506 static void option_instat_callback(struct urb *urb)
508         int err;
509         int status = urb->status;
510         struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
511         struct option_port_private *portdata = usb_get_serial_port_data(port);
512         struct usb_serial *serial = port->serial;
514 printk("%s[%d\n",__FUNCTION__,__LINE__);
515         dbg("%s", __FUNCTION__);
516         dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
518         if (status == 0) {
519                 struct usb_ctrlrequest *req_pkt =
520                                 (struct usb_ctrlrequest *)urb->transfer_buffer;
522                 if (!req_pkt) {
523                         dbg("%s: NULL req_pkt\n", __FUNCTION__);
524                         return;
525                 }
526                 if ((req_pkt->bRequestType == 0xA1) &&
527                                 (req_pkt->bRequest == 0x20)) {
528                         int old_dcd_state;
529                         unsigned char signals = *((unsigned char *)
530                                         urb->transfer_buffer +
531                                         sizeof(struct usb_ctrlrequest));
533                         dbg("%s: signal x%x", __FUNCTION__, signals);
535                         old_dcd_state = portdata->dcd_state;
536                         portdata->cts_state = 1;
537                         portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
538                         portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
539                         portdata->ri_state = ((signals & 0x08) ? 1 : 0);
541                         if (port->tty && !C_CLOCAL(port->tty) &&
542                                         old_dcd_state && !portdata->dcd_state)
543                                 tty_hangup(port->tty);
544                 } else {
545                         dbg("%s: type %x req %x", __FUNCTION__,
546                                 req_pkt->bRequestType,req_pkt->bRequest);
547                 }
548         } else
549                 dbg("%s: error %d", __FUNCTION__, status);
551         /* Resubmit urb so we continue receiving IRQ data */
552         if (status != -ESHUTDOWN) {
553                 urb->dev = serial->dev;
554                 err = usb_submit_urb(urb, GFP_ATOMIC);
555                 if (err)
556                         dbg("%s: resubmit intr urb failed. (%d)",
557                                 __FUNCTION__, err);
558         }
561 static int option_write_room(struct usb_serial_port *port)
563         struct option_port_private *portdata;
564         int i;
565         int data_len = 0;
566         struct urb *this_urb;
568         portdata = usb_get_serial_port_data(port);
570         for (i=0; i < N_OUT_URB; i++) {
571                 this_urb = portdata->out_urbs[i];
572                 if (this_urb && !test_bit(i, &portdata->out_busy))
573                         data_len += OUT_BUFLEN;
574         }
576         dbg("%s: %d", __FUNCTION__, data_len);
577         return data_len;
580 static int option_chars_in_buffer(struct usb_serial_port *port)
582         struct option_port_private *portdata;
583         int i;
584         int data_len = 0;
585         struct urb *this_urb;
587         portdata = usb_get_serial_port_data(port);
589         for (i=0; i < N_OUT_URB; i++) {
590                 this_urb = portdata->out_urbs[i];
591                 if (this_urb && test_bit(i, &portdata->out_busy))
592                         data_len += this_urb->transfer_buffer_length;
593         }
594         dbg("%s: %d", __FUNCTION__, data_len);
595         return data_len;
598 static int option_open(struct usb_serial_port *port, struct file *filp)
600         struct option_port_private *portdata;
601         struct usb_serial *serial = port->serial;
602         int i, err;
603         struct urb *urb;
605         portdata = usb_get_serial_port_data(port);
607         dbg("%s", __FUNCTION__);
609         /* Set some sane defaults */
610         portdata->rts_state = 1;
611         portdata->dtr_state = 1;
613         /* Reset low level data toggle and start reading from endpoints */
614         for (i = 0; i < N_IN_URB; i++) {
615                 urb = portdata->in_urbs[i];
616                 if (! urb)
617                         continue;
618                 if (urb->dev != serial->dev) {
619                         dbg("%s: dev %p != %p", __FUNCTION__,
620                                 urb->dev, serial->dev);
621                         continue;
622                 }
624                 /*
625                  * make sure endpoint data toggle is synchronized with the
626                  * device
627                  */
628                 usb_clear_halt(urb->dev, urb->pipe);
630                 err = usb_submit_urb(urb, GFP_KERNEL);
631                 if (err) {
632                         dbg("%s: submit urb %d failed (%d) %d",
633                                 __FUNCTION__, i, err,
634                                 urb->transfer_buffer_length);
635                 }
636         }
638         /* Reset low level data toggle on out endpoints */
639         for (i = 0; i < N_OUT_URB; i++) {
640                 urb = portdata->out_urbs[i];
641                 if (! urb)
642                         continue;
643                 urb->dev = serial->dev;
644                 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
645                                 usb_pipeout(urb->pipe), 0); */
646         }
648         port->tty->low_latency = 1;
650         option_send_setup(port);
652         return (0);
655 static void option_close(struct usb_serial_port *port, struct file *filp)
657         int i;
658         struct usb_serial *serial = port->serial;
659         struct option_port_private *portdata;
661         dbg("%s", __FUNCTION__);
662         portdata = usb_get_serial_port_data(port);
664         portdata->rts_state = 0;
665         portdata->dtr_state = 0;
667         if (serial->dev) {
668                 option_send_setup(port);
670                 /* Stop reading/writing urbs */
671                 for (i = 0; i < N_IN_URB; i++)
672                         usb_kill_urb(portdata->in_urbs[i]);
673                 for (i = 0; i < N_OUT_URB; i++)
674                         usb_kill_urb(portdata->out_urbs[i]);
675         }
676         port->tty = NULL;
679 /* Helper functions used by option_setup_urbs */
680 static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
681                 int dir, void *ctx, char *buf, int len,
682                 void (*callback)(struct urb *))
684         struct urb *urb;
686         if (endpoint == -1)
687                 return NULL;            /* endpoint not needed */
689         urb = usb_alloc_urb(0, GFP_KERNEL);             /* No ISO */
690         if (urb == NULL) {
691                 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
692                 return NULL;
693         }
695                 /* Fill URB using supplied data. */
696         usb_fill_bulk_urb(urb, serial->dev,
697                       usb_sndbulkpipe(serial->dev, endpoint) | dir,
698                       buf, len, callback, ctx);
700         return urb;
703 /* Setup urbs */
704 static void option_setup_urbs(struct usb_serial *serial)
706         int i,j;
707         struct usb_serial_port *port;
708         struct option_port_private *portdata;
710         dbg("%s", __FUNCTION__);
712         for (i = 0; i < serial->num_ports; i++) {
713                 port = serial->port[i];
714                 portdata = usb_get_serial_port_data(port);
716         /* Do indat endpoints first */
717                 for (j = 0; j < N_IN_URB; ++j) {
718                         portdata->in_urbs[j] = option_setup_urb (serial,
719                         port->bulk_in_endpointAddress, USB_DIR_IN, port,
720                         portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
721                 }
723                 /* outdat endpoints */
724                 for (j = 0; j < N_OUT_URB; ++j) {
725                         portdata->out_urbs[j] = option_setup_urb (serial,
726                         port->bulk_out_endpointAddress, USB_DIR_OUT, port,
727                         portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
728                 }
729         }
732 static int option_send_setup(struct usb_serial_port *port)
734         struct usb_serial *serial = port->serial;
735         struct option_port_private *portdata;
737         dbg("%s", __FUNCTION__);
739         if (port->number != 0)
740                 return 0;
742         portdata = usb_get_serial_port_data(port);
744         if (port->tty) {
745                 int val = 0;
746                 if (portdata->dtr_state)
747                         val |= 0x01;
748                 if (portdata->rts_state)
749                         val |= 0x02;
751                 return usb_control_msg(serial->dev,
752                                 usb_rcvctrlpipe(serial->dev, 0),
753                                 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
754         }
756         return 0;
759 static int option_startup(struct usb_serial *serial)
761         int i, err;
762         struct usb_serial_port *port;
763         struct option_port_private *portdata;
765         dbg("%s", __FUNCTION__);
767         /* Now setup per port private data */
768         for (i = 0; i < serial->num_ports; i++) {
769                 port = serial->port[i];
770 #if 0  // Masked by Jared 03-13-2008
771                 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
772 #else
773                 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
774                 memset(portdata, 0, sizeof(*portdata));
775 #endif
776                 if (!portdata) {
777                         dbg("%s: kmalloc for option_port_private (%d) failed!.",
778                                         __FUNCTION__, i);
779                         return (1);
780                 }
782                 usb_set_serial_port_data(port, portdata);
784                 if (! port->interrupt_in_urb)
785                         continue;
786                 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
787                 if (err)
788                         dbg("%s: submit irq_in urb failed %d",
789                                 __FUNCTION__, err);
790         }
792         option_setup_urbs(serial);
794         return (0);
797 static void option_shutdown(struct usb_serial *serial)
799         int i, j;
800         struct usb_serial_port *port;
801         struct option_port_private *portdata;
803         dbg("%s", __FUNCTION__);
805         /* Stop reading/writing urbs */
806         for (i = 0; i < serial->num_ports; ++i) {
807                 port = serial->port[i];
808                 portdata = usb_get_serial_port_data(port);
809                 for (j = 0; j < N_IN_URB; j++)
810                         usb_kill_urb(portdata->in_urbs[j]);
811                 for (j = 0; j < N_OUT_URB; j++)
812                         usb_kill_urb(portdata->out_urbs[j]);
813         }
815         /* Now free them */
816         for (i = 0; i < serial->num_ports; ++i) {
817                 port = serial->port[i];
818                 portdata = usb_get_serial_port_data(port);
820                 for (j = 0; j < N_IN_URB; j++) {
821                         if (portdata->in_urbs[j]) {
822                                 usb_free_urb(portdata->in_urbs[j]);
823                                 portdata->in_urbs[j] = NULL;
824                         }
825                 }
826                 for (j = 0; j < N_OUT_URB; j++) {
827                         if (portdata->out_urbs[j]) {
828                                 usb_free_urb(portdata->out_urbs[j]);
829                                 portdata->out_urbs[j] = NULL;
830                         }
831                 }
832         }
834         /* Now free per port private data */
835         for (i = 0; i < serial->num_ports; i++) {
836                 port = serial->port[i];
837                 kfree(usb_get_serial_port_data(port));
838         }
841 MODULE_AUTHOR(DRIVER_AUTHOR);
842 MODULE_DESCRIPTION(DRIVER_DESC);
843 MODULE_VERSION(DRIVER_VERSION);
844 MODULE_LICENSE("GPL");
846 #ifdef CONFIG_USB_DEBUG
847 module_param(debug, bool, S_IRUGO | S_IWUSR);
848 MODULE_PARM_DESC(debug, "Debug messages");
849 #endif