K2.6 patches and update.
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / usb / serial / sierra.c
blob1c5983f7dcacfa1ec56e977a21f8325e0b15eeb7
1 /*
2 USB Driver for Sierra Wireless
4 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>
6 Copyright (C) 2008 - 2011 Elina Pasheva, Matthew Safar, Rory Filer
7 <linux@sierrawireless.com>
9 IMPORTANT DISCLAIMER: This driver is not commercially supported by
10 Sierra Wireless. Use at your own risk.
12 This driver is free software; you can redistribute it and/or modify
13 it under the terms of Version 2 of the GNU General Public License as
14 published by the Free Software Foundation.
16 Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
17 Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
19 Back ported to kernel 2.6.23
21 /* Uncomment to log function calls */
22 /* #define DEBUG */
23 #define DRIVER_VERSION "v.1.7.40"
24 #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
25 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
27 #include <linux/kernel.h>
28 #include <linux/jiffies.h>
29 #include <linux/errno.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
36 #define SWIMS_USB_REQUEST_SetPower 0x00
37 #define SWIMS_USB_REQUEST_SetNmea 0x07
38 #define SWIMS_USB_REQUEST_SetMode 0x0B
39 #define SWIMS_SET_MODE_Modem 0x0001
40 #define USB_REQUEST_TYPE_CLASS 0xA1
41 #define USB_REQUEST_IFACE 0x20
43 #define N_IN_URB_HM 8
44 #define N_OUT_URB_HM 64
45 #define N_IN_URB 4
46 #define N_OUT_URB 4
47 #define IN_BUFLEN 4096
49 #define MAX_TRANSFER (PAGE_SIZE - 512)
50 /* MAX_TRANSFER is chosen so that the VM is not stressed by
51 allocations > PAGE_SIZE and the number of packets in a page
52 is an integer 512 is the largest possible packet on EHCI */
54 /* PORTION_LEN defines the length of device attribute buffer */
55 #define PORTION_LEN 4096
57 static int debug;
58 static int nmea;
59 static int truinstall = 1;
60 static int suspend_support;
62 enum devicetype {
63 DEVICE_MODEM = 0,
64 DEVICE_INSTALLER = 1,
67 /* sysfs attributes */
68 static int sierra_create_sysfs_attrs(struct usb_serial_port *port);
69 static int sierra_remove_sysfs_attrs(struct usb_serial_port *port);
71 /* Used in interface blacklisting */
72 struct sierra_iface_info {
73 const u32 infolen; /* number of interface numbers on blacklist */
74 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
77 /* static device type specific data */
78 struct sierra_device_static_info {
79 const enum devicetype dev_type;
80 const struct sierra_iface_info iface_blacklist;
83 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
85 int result;
86 dev_dbg(&udev->dev, "%s\n", __func__);
87 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
88 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
89 USB_TYPE_VENDOR, /* __u8 request type */
90 swiState, /* __u16 value */
91 0, /* __u16 index */
92 NULL, /* void *data */
93 0, /* __u16 size */
94 USB_CTRL_SET_TIMEOUT); /* int timeout */
95 return result;
98 static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
100 int result;
101 dev_dbg(&udev->dev, "%s\n", "DEVICE MODE SWITCH");
102 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
103 SWIMS_USB_REQUEST_SetMode, /* __u8 request */
104 USB_TYPE_VENDOR, /* __u8 request type */
105 eSWocMode, /* __u16 value */
106 0x0000, /* __u16 index */
107 NULL, /* void *data */
108 0, /* __u16 size */
109 USB_CTRL_SET_TIMEOUT); /* int timeout */
110 return result;
113 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
115 int result;
116 dev_dbg(&udev->dev, "%s\n", __func__);
117 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
118 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
119 USB_TYPE_VENDOR, /* __u8 request type */
120 enable, /* __u16 value */
121 0x0000, /* __u16 index */
122 NULL, /* void *data */
123 0, /* __u16 size */
124 USB_CTRL_SET_TIMEOUT); /* int timeout */
125 return result;
128 static int sierra_calc_num_ports(struct usb_serial *serial)
130 int num_ports = 0;
131 u8 ifnum, numendpoints;
133 dev_dbg(&serial->dev->dev, "%s\n", __func__);
135 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
136 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
138 /* Dummy interface present on some SKUs should be ignored */
139 if (ifnum == 0x99)
140 num_ports = 0;
141 else if (numendpoints <= 3)
142 num_ports = 1;
143 else
144 num_ports = (numendpoints-1)/2;
145 return num_ports;
148 static int is_blacklisted(const u8 ifnum,
149 const struct sierra_iface_info *blacklist)
151 const u8 *info;
152 int i;
154 if (blacklist) {
155 info = blacklist->ifaceinfo;
157 for (i = 0; i < blacklist->infolen; i++) {
158 if (info[i] == ifnum)
159 return 1;
162 return 0;
165 static int is_himemory(const u8 ifnum,
166 const struct sierra_iface_info *himemorylist)
168 const u8 *info;
169 int i;
171 if (himemorylist) {
172 info = himemorylist->ifaceinfo;
174 for (i=0; i < himemorylist->infolen; i++) {
175 if (info[i] == ifnum)
176 return 1;
179 return 0;
182 static int sierra_calc_interface(struct usb_serial *serial)
184 int interface;
185 struct usb_interface *p_interface;
186 struct usb_host_interface *p_host_interface;
187 dev_dbg(&serial->dev->dev, "%s\n", __func__);
189 /* Get the interface structure pointer from the serial struct */
190 p_interface = serial->interface;
192 /* Get a pointer to the host interface structure */
193 p_host_interface = p_interface->cur_altsetting;
195 /* read the interface descriptor for this active altsetting
196 * to find out the interface number we are on
198 interface = p_host_interface->desc.bInterfaceNumber;
200 return interface;
203 static int sierra_probe(struct usb_serial *serial,
204 const struct usb_device_id *id)
206 const struct sierra_device_static_info *info;
207 int result = 0;
208 struct usb_device *udev;
209 u8 ifnum, ifclass;
211 udev = serial->dev;
212 dev_dbg(&udev->dev, "%s\n", __func__);
214 /* Check TRU-Install first */
215 info = (const struct sierra_device_static_info *)id->driver_info;
216 ifclass = serial->interface->cur_altsetting->desc.bInterfaceClass;
217 if (ifclass == USB_CLASS_MASS_STORAGE) {
218 /* If TRU-Install support is enabled, force to modem mode */
219 if (truinstall && info && info->dev_type == DEVICE_INSTALLER) {
220 dev_dbg(&udev->dev, "%s\n", "FOUND TRU-INSTALL DEVICE");
221 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
223 return -ENODEV;
226 ifnum = sierra_calc_interface(serial);
227 if (info && is_blacklisted(ifnum, &info->iface_blacklist)) {
228 dev_dbg(&serial->dev->dev,
229 "Ignoring blacklisted interface #%d\n", ifnum);
230 return -ENODEV;
234 * If this interface supports more than 1 alternate
235 * select the 2nd one
237 if (serial->interface->num_altsetting == 2) {
238 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
239 ifnum);
240 /* We know the alternate setting is 1 for the MC8785 */
241 usb_set_interface(udev, ifnum, 1);
243 /* Be careful here, The ifnum, ifclass etc. might be incorrect, because
244 * of the usb_set_interface call. (all obtained using
245 * serial->interface->cur_altsetting that was changed by that call)
248 return result;
251 static const struct sierra_device_static_info tru_inst_info = {
252 .dev_type = DEVICE_INSTALLER,
255 /* interfaces with higher memory requirements */
256 static const u8 hi_memory_typeA_ifaces[] = { 0, 2 };
257 static const struct sierra_iface_info typeA_interface_list = {
258 .infolen = ARRAY_SIZE(hi_memory_typeA_ifaces),
259 .ifaceinfo = hi_memory_typeA_ifaces,
262 static const u8 hi_memory_typeB_ifaces[] = { 3, 4, 5, 6 };
263 static const struct sierra_iface_info typeB_interface_list = {
264 .infolen = ARRAY_SIZE(hi_memory_typeB_ifaces),
265 .ifaceinfo = hi_memory_typeB_ifaces,
268 /* 'blacklist' of interfaces not served by this driver */
269 static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
270 static const struct sierra_device_static_info direct_ip_interface_blacklist = {
271 .dev_type = DEVICE_MODEM,
272 .iface_blacklist = {
273 .infolen = ARRAY_SIZE( direct_ip_non_serial_ifaces ),
274 .ifaceinfo = direct_ip_non_serial_ifaces,
278 static const struct usb_device_id id_table [] = {
279 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
280 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
281 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
282 { USB_DEVICE(0x03F0, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
284 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
285 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
286 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
287 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
288 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
289 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
290 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
291 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
292 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
293 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
294 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
295 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
296 { USB_DEVICE(0x1199, 0x0301) }, /* Sierra Wireless USB Dongle 250U/3G */
297 /* Sierra Wireless MC5728 */
298 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0400, 0xFF, 0xFF, 0xFF) },
300 /* Sierra Wireless C597 */
301 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
302 /* Sierra Wireless T598 */
303 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
304 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
305 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
306 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
307 { USB_DEVICE(0x114F, 0x6000) }, /* Sierra Wireless Q26 Elite */
309 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
310 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
311 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
312 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
313 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
314 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
315 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
316 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
317 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
318 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
319 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
320 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
321 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
322 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
323 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
324 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
325 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
326 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
327 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
328 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
329 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
330 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
331 { USB_DEVICE(0x1199, 0x683C) },
332 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
333 /* Sierra Wireless MC8790, MC8791, MC8792 */
334 { USB_DEVICE(0x1199, 0x683E) },
335 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
336 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
337 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
338 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
339 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
340 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
341 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
342 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
343 /* Sierra Wireless C885 */
344 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
345 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
346 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
347 /* Sierra Wireless C22/C33 */
348 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
349 /* Sierra Wireless HSPA Non-Composite Device */
350 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
351 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
352 /* Sierra Wireless Direct IP modems */
353 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
354 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
356 /* AT&T Direct IP modems */
357 { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
358 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
360 /* Sierra Wireless Direct IP LTE modems */
361 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
362 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
364 /* AT&T Direct IP LTE modems */
365 { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
366 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
368 /* Sierra Wireless TRU install devices */
369 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0xFFF, 0x08, 0x06, 0x50),
370 .driver_info = (kernel_ulong_t)&tru_inst_info
372 /* Wireless 5720 VZW Mobile Broadband (EVDO Rev-A) Minicard GPS Port */
373 { USB_DEVICE(0x413C, 0x8133) },
377 MODULE_DEVICE_TABLE(usb, id_table);
379 /* per port private data */
380 struct sierra_port_private {
381 spinlock_t lock; /* lock the structure */
382 int outstanding_urbs; /* number of out urbs in flight */
383 struct usb_anchor submitted; /* in case we need to retract our
384 * submissions */
385 int suspend_status; /* indicates whether device power has been
386 * suspended */
387 int num_out_urbs;
388 int num_in_urbs;
389 /* Input endpoints and buffers for this port */
390 struct urb *in_urbs[N_IN_URB_HM];
392 /* Settings for the port */
393 int rts_state; /* Handshaking pins (outputs) */
394 int dtr_state;
395 int cts_state; /* Handshaking pins (inputs) */
396 int dsr_state;
397 int dcd_state;
398 int ri_state;
401 static int sierra_send_setup(struct usb_serial_port *port)
403 struct usb_serial *serial = port->serial;
404 struct sierra_port_private *portdata;
405 __u16 interface = 0;
407 dev_dbg(&port->dev, "%s\n", __func__);
409 portdata = usb_get_serial_port_data(port);
411 if (port->tty) {
413 int val = 0;
414 if (portdata->dtr_state)
415 val |= 0x01;
416 if (portdata->rts_state)
417 val |= 0x02;
419 /* If composite device then properly report interface */
420 if (serial->num_ports == 1) {
421 interface = sierra_calc_interface(serial);
422 /* Control message is send only to interfaces with
423 * interrupt_in endpoints
425 if(port->interrupt_in_urb) {
426 /* send control message */
427 return usb_control_msg(serial->dev,
428 usb_rcvctrlpipe(serial->dev, 0),
429 0x22, 0x21, val, interface,
430 NULL, 0, USB_CTRL_SET_TIMEOUT);
434 /* Otherwise the need to do non-composite mapping */
435 else {
436 if (port->bulk_out_endpointAddress == 2)
437 interface = 0;
438 else if (port->bulk_out_endpointAddress == 4)
439 interface = 1;
440 else if (port->bulk_out_endpointAddress == 5)
441 interface = 2;
443 return usb_control_msg(serial->dev,
444 usb_rcvctrlpipe(serial->dev, 0),
445 0x22, 0x21, val, interface,
446 NULL, 0, USB_CTRL_SET_TIMEOUT);
451 return 0;
454 static void sierra_set_termios(struct usb_serial_port *port,
455 struct ktermios *old_termios)
457 dev_dbg(&port->dev, "%s\n", __func__);
458 sierra_send_setup(port);
461 static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
463 unsigned int value;
464 struct sierra_port_private *portdata;
466 dev_dbg(&port->dev, "%s\n", __func__);
467 portdata = usb_get_serial_port_data(port);
469 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
470 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
471 ((portdata->cts_state) ? TIOCM_CTS : 0) |
472 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
473 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
474 ((portdata->ri_state) ? TIOCM_RNG : 0);
476 return value;
479 static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
480 unsigned int set, unsigned int clear)
482 struct sierra_port_private *portdata;
484 portdata = usb_get_serial_port_data(port);
486 if (set & TIOCM_RTS)
487 portdata->rts_state = 1;
488 if (set & TIOCM_DTR)
489 portdata->dtr_state = 1;
491 if (clear & TIOCM_RTS)
492 portdata->rts_state = 0;
493 if (clear & TIOCM_DTR)
494 portdata->dtr_state = 0;
495 return sierra_send_setup(port);
497 static void sierra_release_urb(struct urb *urb)
499 struct usb_serial_port *port;
500 if (urb) {
501 port = urb->context;
502 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
503 if (urb->transfer_buffer)
504 kfree(urb->transfer_buffer);
505 usb_free_urb(urb);
509 /* Sysfs Attributes */
511 static ssize_t show_suspend_status(struct device *dev,
512 struct device_attribute *attr, char *buf)
514 struct usb_serial_port *port;
515 struct sierra_port_private *portdata;
517 port = to_usb_serial_port(dev);
518 portdata = usb_get_serial_port_data(port);
520 return snprintf(buf, PORTION_LEN, "%i\n", portdata->suspend_status);
523 static DEVICE_ATTR(suspend_status, S_IWUSR | S_IRUGO, show_suspend_status,
524 NULL);
526 static int sierra_create_sysfs_attrs(struct usb_serial_port *port)
528 return device_create_file(&port->dev, &dev_attr_suspend_status);
531 static int sierra_remove_sysfs_attrs(struct usb_serial_port *port)
533 device_remove_file(&port->dev, &dev_attr_suspend_status);
534 return 0;
537 static void sierra_outdat_callback(struct urb *urb)
539 struct usb_serial_port *port = urb->context;
540 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
541 int status = urb->status;
542 unsigned long flags;
544 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
546 /* free up the transfer buffer, as usb_free_urb() does not do this */
547 kfree(urb->transfer_buffer);
549 if (status)
550 dev_dbg(&port->dev, "%s - nonzero write bulk status "
551 "received: %d\n", __func__, status);
553 spin_lock_irqsave(&portdata->lock, flags);
554 --portdata->outstanding_urbs;
555 spin_unlock_irqrestore(&portdata->lock, flags);
557 usb_serial_port_softint(port);
560 /* Write */
561 static int sierra_write(struct usb_serial_port *port,
562 const unsigned char *buf, int count)
564 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
565 struct usb_serial *serial = port->serial;
566 unsigned long flags;
567 unsigned char *buffer;
568 struct urb *urb;
569 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
570 int retval = 0;
572 /* verify that we actually have some data to write */
573 if (count == 0)
574 return 0;
576 dev_dbg(&port->dev, "%s: write (%zu bytes)\n", __func__, writesize);
578 spin_lock_irqsave(&portdata->lock, flags);
579 if (portdata->outstanding_urbs > portdata->num_out_urbs) {
580 spin_unlock_irqrestore(&portdata->lock, flags);
581 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
582 return 0;
584 portdata->outstanding_urbs++;
585 spin_unlock_irqrestore(&portdata->lock, flags);
587 buffer = kmalloc(writesize, GFP_ATOMIC);
588 if (!buffer) {
589 dev_err(&port->dev, "out of memory\n");
590 retval = -ENOMEM;
591 goto error_no_buffer;
594 urb = usb_alloc_urb(0, GFP_ATOMIC);
595 if (!urb) {
596 dev_err(&port->dev, "no more free urbs\n");
597 retval = -ENOMEM;
598 goto error_no_urb;
601 memcpy(buffer, buf, writesize);
603 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
605 usb_fill_bulk_urb(urb, serial->dev,
606 usb_sndbulkpipe(serial->dev,
607 port->bulk_out_endpointAddress),
608 buffer, writesize, sierra_outdat_callback, port);
610 /* Handle the need to send a zero length packet */
611 urb->transfer_flags |= URB_ZERO_PACKET;
612 usb_anchor_urb(urb, &portdata->submitted);
614 /* send it down the pipe */
615 retval = usb_submit_urb(urb, GFP_ATOMIC);
616 if (retval) {
617 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
618 "with status = %d\n", __func__, retval);
619 goto error_anchor;
622 /* release our reference to this urb, the USB core will eventually
623 * free it entirely */
624 usb_free_urb(urb);
626 return writesize;
628 error_anchor:
629 usb_unanchor_urb(urb);
630 usb_free_urb(urb);
631 error_no_urb:
632 kfree(buffer);
633 error_no_buffer:
634 spin_lock_irqsave(&portdata->lock, flags);
635 --portdata->outstanding_urbs;
636 spin_unlock_irqrestore(&portdata->lock, flags);
637 return retval;
640 static void sierra_indat_callback(struct urb *urb)
642 int err;
643 int endpoint;
644 struct usb_serial_port *port = urb->context;
645 struct tty_struct *tty;
646 unsigned char *data = urb->transfer_buffer;
647 int status = urb->status;
649 endpoint = usb_pipeendpoint(urb->pipe);
651 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
653 if (status) {
654 dev_dbg(&port->dev, "%s: nonzero status: %d on"
655 " endpoint %02x\n", __func__, status, endpoint);
656 } else {
657 tty = port->tty;
658 if (tty) {
659 if (urb->actual_length) {
660 tty_buffer_request_room(tty,
661 urb->actual_length);
662 tty_insert_flip_string(tty, data,
663 urb->actual_length);
664 tty_flip_buffer_push(tty);
665 usb_serial_debug_data(debug,&port->dev,
666 __func__, urb->actual_length, data);
667 } else {
668 dev_dbg(&port->dev, "%s: empty read urb"
669 " received\n", __func__);
674 /* Resubmit urb so we continue receiving */
675 if (port->open_count && status != -ESHUTDOWN && status != -ENOENT) {
676 err = usb_submit_urb(urb, GFP_ATOMIC);
677 if (err)
678 dev_err(&port->dev, "resubmit read urb failed."
679 "(%d)\n", err);
682 return;
685 static void sierra_instat_callback(struct urb *urb)
687 int err;
688 int status = urb->status;
689 struct usb_serial_port *port = urb->context;
690 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
691 struct usb_serial *serial = port->serial;
693 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
694 urb, port, portdata);
696 if (status == 0) {
697 struct usb_ctrlrequest *req_pkt =
698 (struct usb_ctrlrequest *)urb->transfer_buffer;
700 const u16 *sigp = (u16 *)(req_pkt + 1);
701 /* usb_ctrlrequest we parsed is followed by two bytes of data
702 * make sure we received that many bytes
704 if (urb->actual_length >= sizeof(*req_pkt) + sizeof(*sigp) &&
705 req_pkt->bRequestType == USB_REQUEST_TYPE_CLASS &&
706 req_pkt->bRequest == USB_REQUEST_IFACE) {
707 int old_dcd_state;
708 unsigned char signals = *((unsigned char *)
709 urb->transfer_buffer +
710 sizeof(struct usb_ctrlrequest));
712 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
713 signals);
715 old_dcd_state = portdata->dcd_state;
716 /* Note: CTS from modem is in reverse logic! */
717 portdata->cts_state = ((signals & 0x100) ? 0 : 1);
718 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
719 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
720 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
722 if (port->tty && !C_CLOCAL(port->tty) &&
723 old_dcd_state && !portdata->dcd_state)
724 tty_hangup(port->tty);
725 } else {
726 /* dump the data we don't understand to log */
727 usb_serial_debug_data(1, &port->dev, __func__,
728 urb->actual_length, urb->transfer_buffer);
730 } else
731 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
733 /* Resubmit urb so we continue receiving IRQ data */
734 if (port->open_count &&
735 status != -ESHUTDOWN && status != -ENOENT && status != -ENODEV) {
736 urb->dev = serial->dev;
737 err = usb_submit_urb(urb, GFP_ATOMIC);
738 if (err && err != -ENODEV)
739 dev_err(&port->dev, "%s: resubmit intr urb "
740 "failed. (%d)\n", __func__, err);
744 static int sierra_write_room(struct usb_serial_port *port)
746 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
747 unsigned long flags;
748 int retval;
750 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
752 /* try to give a good number back based on if we have any free urbs at
753 * this point in time */
754 retval = MAX_TRANSFER;
756 spin_lock_irqsave(&portdata->lock, flags);
757 if (portdata->outstanding_urbs >= portdata->num_out_urbs) {
758 retval = 0;
760 spin_unlock_irqrestore(&portdata->lock, flags);
762 return retval;
765 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
767 int i;
768 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
770 for (i = 0; i < portdata->num_in_urbs; i++) {
771 usb_kill_urb(portdata->in_urbs[i]);
773 usb_kill_urb(port->interrupt_in_urb);
776 static int sierra_submit_rx_urbs(struct usb_serial_port *port)
778 int ok_cnt;
779 int err = -EINVAL;
780 int i;
781 struct urb * urb;
782 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
784 ok_cnt = 0;
785 for (i = 0; i < portdata->num_in_urbs; i++) {
786 urb = portdata->in_urbs[i];
787 if (!urb)
788 continue;
789 err = usb_submit_urb(urb, GFP_KERNEL);
790 if (err) {
791 dev_err(&port->dev, "%s: submit urb failed: %d\n",
792 __func__, err );
793 } else {
794 ok_cnt ++;
798 if (ok_cnt && port->interrupt_in_urb) {
799 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
800 if (err) {
801 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
802 __func__, err );
806 if (ok_cnt > 0) /* at least one rx urb submitted */
807 return 0;
808 else
809 return err;
812 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
813 int dir, void *ctx, int len,
814 usb_complete_t callback)
816 struct urb *urb;
817 u8 *buf;
819 if (endpoint == -1)
820 return NULL;
822 urb = usb_alloc_urb( 0, GFP_KERNEL );
823 if (urb == NULL) {
824 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
825 __func__, endpoint);
826 return NULL;
829 buf = kmalloc(len, GFP_KERNEL);
830 if (buf)
832 /* Fill URB using supplied data */
833 usb_fill_bulk_urb(urb, serial->dev,
834 usb_sndbulkpipe(serial->dev, endpoint) | dir,
835 buf, len, callback, ctx);
837 /* debug */
838 dev_dbg(&serial->dev->dev,"%s %c u:%p d:%p\n", __func__,
839 dir == USB_DIR_IN?'i':'o', urb, buf );
840 } else {
841 dev_dbg(&serial->dev->dev,"%s %c u:%p d:%p\n", __func__,
842 dir == USB_DIR_IN?'i':'o', urb, buf );
844 sierra_release_urb(urb);
845 urb = NULL;
848 return urb;
851 static void sierra_close(struct usb_serial_port *port, struct file *filp)
853 int i;
854 struct usb_serial *serial = port->serial;
855 struct sierra_port_private *portdata;
856 int time;
858 dev_dbg(&port->dev, "%s\n", __func__);
859 portdata = usb_get_serial_port_data(port);
861 portdata->rts_state = 0;
862 portdata->dtr_state = 0;
864 if (serial->dev) {
865 sierra_send_setup(port);
867 /* Stop reading urbs */
868 sierra_stop_rx_urbs(port);
869 /* .. and release them */
870 for (i = 0; i < portdata->num_in_urbs; i++) {
871 sierra_release_urb(portdata->in_urbs[i]);
872 portdata->in_urbs[i] = NULL;
875 time = usb_wait_anchor_empty_timeout(&portdata->submitted,1000);
876 if (!time) {
877 usb_kill_anchored_urbs(&portdata->submitted);
881 port->tty = NULL;
884 static int sierra_open(struct usb_serial_port *port, struct file *filp)
886 struct sierra_port_private *portdata;
887 struct usb_serial *serial = port->serial;
888 int i;
889 int err;
890 int endpoint;
891 struct urb *urb;
893 portdata = usb_get_serial_port_data(port);
895 dev_dbg(&port->dev, "%s\n", __func__);
897 /* Set some sane defaults */
898 portdata->rts_state = 1;
899 portdata->dtr_state = 1;
901 endpoint = port->bulk_in_endpointAddress;
903 for (i = 0; i < portdata->num_in_urbs; i++) {
904 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
905 IN_BUFLEN, sierra_indat_callback);
906 portdata->in_urbs[i] = urb;
908 /* clear halt condition */
909 usb_clear_halt(serial->dev,
910 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
912 err = sierra_submit_rx_urbs(port);
913 if (err) {
914 /* get rid of everything as in close */
915 sierra_close(port, filp);
916 return err;
918 sierra_send_setup(port);
920 return 0;
923 static int sierra_startup(struct usb_serial *serial)
925 struct usb_serial_port *port = NULL;
926 struct sierra_port_private *portdata = NULL;
927 struct sierra_iface_info *himemoryp = NULL;
928 int i;
929 u8 ifnum;
931 dev_dbg(&serial->dev->dev, "%s\n", __func__);
933 /* Set Device mode to D0 */
934 sierra_set_power_state(serial->dev, 0x0000);
936 /* Check NMEA and set */
937 if (nmea)
938 sierra_vsc_set_nmea(serial->dev, 1);
940 if (serial->num_ports) {
941 /* Note: One big piece of memory is allocated for all ports
942 * private data in one shot. This memory is split into equal
943 * pieces for each port.
945 portdata = (struct sierra_port_private *)kzalloc
946 (sizeof(*portdata) * serial->num_ports, GFP_KERNEL);
947 if (!portdata) {
948 dev_dbg(&serial->dev->dev, "%s: No memory!\n", __func__);
949 return -ENOMEM;
953 /* Now setup per port private data */
954 /* Note that the private space for each port is accessed by
955 * advancing the private data pointer accordingly
957 for (i = 0; i < serial->num_ports; i++, portdata++) {
958 port = serial->port[i];
959 /* initialize selected members of private data because these
960 * may be referred to right away */
961 spin_lock_init(&portdata->lock);
962 init_usb_anchor(&portdata->submitted);
963 portdata->suspend_status = 0;
964 portdata->cts_state = 1;
965 ifnum = i;
966 /* Assume low memory requirements */
967 portdata->num_out_urbs = N_OUT_URB;
968 portdata->num_in_urbs = N_IN_URB;
970 /* Determine actual memory requirements */
971 if (serial->num_ports == 1) {
972 /* Get interface number for composite device */
973 ifnum = sierra_calc_interface(serial);
974 himemoryp =
975 (struct sierra_iface_info *)&typeB_interface_list;
976 if (is_himemory(ifnum, himemoryp)) {
977 portdata->num_out_urbs = N_OUT_URB_HM;
978 portdata->num_in_urbs = N_IN_URB_HM;
981 else {
982 himemoryp =
983 (struct sierra_iface_info *)&typeA_interface_list;
984 if (is_himemory(i, himemoryp)) {
985 portdata->num_out_urbs = N_OUT_URB_HM;
986 portdata->num_in_urbs = N_IN_URB_HM;
989 dev_dbg(&serial->dev->dev,
990 "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
991 ifnum,portdata->num_in_urbs, portdata->num_out_urbs );
992 /* Set the port private data pointer */
993 usb_set_serial_port_data(port, portdata);
995 return 0;
998 static void sierra_shutdown(struct usb_serial *serial)
1000 int i;
1001 struct usb_serial_port *port;
1003 dev_dbg(&serial->dev->dev, "%s\n", __func__);
1004 if (serial->num_ports > 0) {
1005 port = serial->port[0];
1006 if (port)
1007 /* Note: The entire piece of memory that was allocated
1008 * in the startup routine can be released by passing
1009 * a pointer to the beginning of the piece.
1010 * This address corresponds to the address of the chunk
1011 * that was given to port 0.
1013 kfree(usb_get_serial_port_data(port));
1016 for (i = 0; i < serial->num_ports; ++i) {
1017 port = serial->port[i];
1018 if (!port)
1019 continue;
1020 usb_set_serial_port_data(port, NULL);
1024 #ifdef CONFIG_PM
1025 int sierra_suspend(struct usb_serial *serial, pm_message_t message)
1027 int i;
1028 struct usb_serial_port *port;
1029 struct sierra_port_private *portdata;
1030 unsigned long flags;
1031 int time;
1033 dev_dbg(&serial->dev->dev, "%s\n", __func__);
1035 for (i = 0; i < serial->num_ports; ++i) {
1036 port = serial->port[i];
1037 if (!port)
1038 continue;
1039 portdata = usb_get_serial_port_data(port);
1040 if (!portdata)
1041 continue;
1042 /* indicate suspended power mode */
1043 portdata->suspend_status = 1;
1044 sierra_stop_rx_urbs(port);
1045 /* release tx urbs */
1046 time = usb_wait_anchor_empty_timeout(&portdata->submitted,1000);
1047 if (!time) {
1048 usb_kill_anchored_urbs(&portdata->submitted);
1049 spin_lock_irqsave(&portdata->lock, flags);
1050 /* set outstanding tx urbs counter to 0 */
1051 portdata->outstanding_urbs = 0;
1052 spin_unlock_irqrestore(&portdata->lock, flags);
1054 } /* end for loop */
1055 return 0;
1058 int sierra_resume(struct usb_serial *serial)
1060 int i;
1061 struct usb_serial_port *port;
1062 struct sierra_port_private *portdata;
1064 dev_dbg(&serial->dev->dev, "%s\n", __func__);
1066 for (i=0; i < serial->num_ports ; i++) {
1067 port = serial->port[i];
1068 if (!port)
1069 continue;
1070 portdata = usb_get_serial_port_data(port);
1071 if (!portdata)
1072 continue;
1073 sierra_submit_rx_urbs(port);
1074 /* indicate non-suspended power mode */
1075 portdata->suspend_status = 0;
1077 return 0;
1079 #else
1080 #define sierra_suspend NULL
1081 #define sierra_resume NULL
1082 #endif
1084 static struct usb_driver sierra_driver = {
1085 .name = "sierra",
1086 .probe = usb_serial_probe,
1087 .disconnect = usb_serial_disconnect,
1088 .suspend = usb_serial_suspend,
1089 .resume = usb_serial_resume,
1090 .id_table = id_table,
1092 .no_dynamic_id = 1,
1093 .supports_autosuspend = 1,
1096 static struct usb_serial_driver sierra_device = {
1097 .driver = {
1098 .owner = THIS_MODULE,
1099 .name = "sierra",
1101 .description = "Sierra USB modem",
1102 .id_table = id_table,
1103 .usb_driver = &sierra_driver,
1104 .num_interrupt_in = NUM_DONT_CARE,
1105 .num_bulk_in = NUM_DONT_CARE,
1106 .num_bulk_out = NUM_DONT_CARE,
1107 .calc_num_ports = sierra_calc_num_ports,
1108 .probe = sierra_probe,
1109 .open = sierra_open,
1110 .close = sierra_close,
1111 .write = sierra_write,
1112 .write_room = sierra_write_room,
1113 .set_termios = sierra_set_termios,
1114 .tiocmget = sierra_tiocmget,
1115 .tiocmset = sierra_tiocmset,
1116 .attach = sierra_startup,
1117 .shutdown = sierra_shutdown,
1118 .port_probe = sierra_create_sysfs_attrs,
1119 .port_remove = sierra_remove_sysfs_attrs,
1120 .read_int_callback = sierra_instat_callback,
1121 .suspend = sierra_suspend,
1122 .resume = sierra_resume,
1125 /* Functions used by new usb-serial code. */
1126 static int __init sierra_init(void)
1128 int retval;
1129 retval = usb_serial_register(&sierra_device);
1130 if (retval)
1131 goto failed_device_register;
1134 retval = usb_register(&sierra_driver);
1135 if (retval)
1136 goto failed_driver_register;
1138 info(DRIVER_DESC ": " DRIVER_VERSION);
1140 return 0;
1142 failed_driver_register:
1143 usb_serial_deregister(&sierra_device);
1144 failed_device_register:
1145 return retval;
1148 static void __exit sierra_exit(void)
1150 usb_deregister(&sierra_driver);
1151 usb_serial_deregister(&sierra_device);
1154 module_init(sierra_init);
1155 module_exit(sierra_exit);
1157 MODULE_AUTHOR(DRIVER_AUTHOR);
1158 MODULE_DESCRIPTION(DRIVER_DESC);
1159 MODULE_VERSION(DRIVER_VERSION);
1160 MODULE_LICENSE("GPL");
1162 module_param(truinstall, bool, S_IRUGO | S_IWUSR);
1163 MODULE_PARM_DESC(truinstall, "TRU-Install support");
1165 module_param(nmea, bool, S_IRUGO | S_IWUSR);
1166 MODULE_PARM_DESC(nmea, "NMEA streaming");
1168 module_param(debug, bool, S_IRUGO | S_IWUSR);
1169 MODULE_PARM_DESC(debug, "Debug messages");