2 USB Driver for Sierra Wireless
4 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>,
6 Copyright (C) 2008, 2009 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>
20 #define DRIVER_VERSION "v.1.3.8"
21 #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
22 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
24 #include <linux/kernel.h>
25 #include <linux/jiffies.h>
26 #include <linux/errno.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
33 #define SWIMS_USB_REQUEST_SetPower 0x00
34 #define SWIMS_USB_REQUEST_SetNmea 0x07
38 #define IN_BUFLEN 4096
40 #define MAX_TRANSFER (PAGE_SIZE - 512)
41 /* MAX_TRANSFER is chosen so that the VM is not stressed by
42 allocations > PAGE_SIZE and the number of packets in a page
43 is an integer 512 is the largest possible packet on EHCI */
48 /* Used in interface blacklisting */
49 struct sierra_iface_info {
50 const u32 infolen; /* number of interface numbers on blacklist */
51 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
54 struct sierra_intf_private {
56 unsigned int suspended:1;
60 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
63 dev_dbg(&udev->dev, "%s\n", __func__);
64 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
65 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
66 USB_TYPE_VENDOR, /* __u8 request type */
67 swiState, /* __u16 value */
69 NULL, /* void *data */
71 USB_CTRL_SET_TIMEOUT); /* int timeout */
75 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
78 dev_dbg(&udev->dev, "%s\n", __func__);
79 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
80 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
81 USB_TYPE_VENDOR, /* __u8 request type */
82 enable, /* __u16 value */
83 0x0000, /* __u16 index */
84 NULL, /* void *data */
86 USB_CTRL_SET_TIMEOUT); /* int timeout */
90 static int sierra_calc_num_ports(struct usb_serial *serial)
93 u8 ifnum, numendpoints;
95 dev_dbg(&serial->dev->dev, "%s\n", __func__);
97 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
98 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
100 /* Dummy interface present on some SKUs should be ignored */
103 else if (numendpoints <= 3)
106 num_ports = (numendpoints-1)/2;
110 static int is_blacklisted(const u8 ifnum,
111 const struct sierra_iface_info *blacklist)
117 info = blacklist->ifaceinfo;
119 for (i = 0; i < blacklist->infolen; i++) {
120 if (info[i] == ifnum)
127 static int sierra_calc_interface(struct usb_serial *serial)
130 struct usb_interface *p_interface;
131 struct usb_host_interface *p_host_interface;
132 dev_dbg(&serial->dev->dev, "%s\n", __func__);
134 /* Get the interface structure pointer from the serial struct */
135 p_interface = serial->interface;
137 /* Get a pointer to the host interface structure */
138 p_host_interface = p_interface->cur_altsetting;
140 /* read the interface descriptor for this active altsetting
141 * to find out the interface number we are on
143 interface = p_host_interface->desc.bInterfaceNumber;
148 static int sierra_probe(struct usb_serial *serial,
149 const struct usb_device_id *id)
152 struct usb_device *udev;
153 struct sierra_intf_private *data;
157 dev_dbg(&udev->dev, "%s\n", __func__);
159 ifnum = sierra_calc_interface(serial);
161 * If this interface supports more than 1 alternate
164 if (serial->interface->num_altsetting == 2) {
165 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
167 /* We know the alternate setting is 1 for the MC8785 */
168 usb_set_interface(udev, ifnum, 1);
171 /* ifnum could have changed - by calling usb_set_interface */
172 ifnum = sierra_calc_interface(serial);
174 if (is_blacklisted(ifnum,
175 (struct sierra_iface_info *)id->driver_info)) {
176 dev_dbg(&serial->dev->dev,
177 "Ignoring blacklisted interface #%d\n", ifnum);
181 data = serial->private = kzalloc(sizeof(struct sierra_intf_private), GFP_KERNEL);
184 spin_lock_init(&data->susp_lock);
189 static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
190 static const struct sierra_iface_info direct_ip_interface_blacklist = {
191 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
192 .ifaceinfo = direct_ip_non_serial_ifaces,
195 static struct usb_device_id id_table [] = {
196 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
197 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
198 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
200 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
201 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
202 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
203 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
204 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
205 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
206 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
207 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
208 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
209 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
210 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
211 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
212 /* Sierra Wireless C597 */
213 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
214 /* Sierra Wireless T598 */
215 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
216 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
217 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
218 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
219 { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
221 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
222 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
223 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
224 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
225 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
226 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
227 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
228 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
229 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
230 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
231 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
232 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
233 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
234 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
235 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
236 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
237 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
238 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
239 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
240 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
241 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
242 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
243 { USB_DEVICE(0x1199, 0x683C) },
244 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
245 /* Sierra Wireless MC8790, MC8791, MC8792 */
246 { USB_DEVICE(0x1199, 0x683E) },
247 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
248 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
249 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
250 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
251 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
252 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
253 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
254 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
255 /* Sierra Wireless C885 */
256 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
257 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
258 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
259 /* Sierra Wireless C22/C33 */
260 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
261 /* Sierra Wireless HSPA Non-Composite Device */
262 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
263 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
264 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
265 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
270 MODULE_DEVICE_TABLE(usb, id_table);
272 static struct usb_driver sierra_driver = {
274 .probe = usb_serial_probe,
275 .disconnect = usb_serial_disconnect,
276 .suspend = usb_serial_suspend,
277 .resume = usb_serial_resume,
278 .id_table = id_table,
280 .supports_autosuspend = 1,
283 struct sierra_port_private {
284 spinlock_t lock; /* lock the structure */
285 int outstanding_urbs; /* number of out urbs in flight */
286 struct usb_anchor active;
287 struct usb_anchor delayed;
289 /* Input endpoints and buffers for this port */
290 struct urb *in_urbs[N_IN_URB];
292 /* Settings for the port */
293 int rts_state; /* Handshaking pins (outputs) */
295 int cts_state; /* Handshaking pins (inputs) */
299 unsigned int opened:1;
302 static int sierra_send_setup(struct usb_serial_port *port)
304 struct usb_serial *serial = port->serial;
305 struct sierra_port_private *portdata;
311 dev_dbg(&port->dev, "%s\n", __func__);
313 portdata = usb_get_serial_port_data(port);
315 if (portdata->dtr_state)
317 if (portdata->rts_state)
320 /* If composite device then properly report interface */
321 if (serial->num_ports == 1) {
322 interface = sierra_calc_interface(serial);
323 /* Control message is sent only to interfaces with
324 * interrupt_in endpoints
326 if (port->interrupt_in_urb) {
327 /* send control message */
332 /* Otherwise the need to do non-composite mapping */
334 if (port->bulk_out_endpointAddress == 2)
336 else if (port->bulk_out_endpointAddress == 4)
338 else if (port->bulk_out_endpointAddress == 5)
346 usb_autopm_get_interface(serial->interface);
347 retval = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
348 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
349 usb_autopm_put_interface(serial->interface);
354 static void sierra_set_termios(struct tty_struct *tty,
355 struct usb_serial_port *port, struct ktermios *old_termios)
357 dev_dbg(&port->dev, "%s\n", __func__);
358 tty_termios_copy_hw(tty->termios, old_termios);
359 sierra_send_setup(port);
362 static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
364 struct usb_serial_port *port = tty->driver_data;
366 struct sierra_port_private *portdata;
368 dev_dbg(&port->dev, "%s\n", __func__);
369 portdata = usb_get_serial_port_data(port);
371 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
372 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
373 ((portdata->cts_state) ? TIOCM_CTS : 0) |
374 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
375 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
376 ((portdata->ri_state) ? TIOCM_RNG : 0);
381 static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
382 unsigned int set, unsigned int clear)
384 struct usb_serial_port *port = tty->driver_data;
385 struct sierra_port_private *portdata;
387 portdata = usb_get_serial_port_data(port);
390 portdata->rts_state = 1;
392 portdata->dtr_state = 1;
394 if (clear & TIOCM_RTS)
395 portdata->rts_state = 0;
396 if (clear & TIOCM_DTR)
397 portdata->dtr_state = 0;
398 return sierra_send_setup(port);
401 static void sierra_release_urb(struct urb *urb)
403 struct usb_serial_port *port;
406 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
407 kfree(urb->transfer_buffer);
412 static void sierra_outdat_callback(struct urb *urb)
414 struct usb_serial_port *port = urb->context;
415 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
416 struct sierra_intf_private *intfdata;
417 int status = urb->status;
419 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
420 intfdata = port->serial->private;
422 /* free up the transfer buffer, as usb_free_urb() does not do this */
423 kfree(urb->transfer_buffer);
424 usb_autopm_put_interface_async(port->serial->interface);
426 dev_dbg(&port->dev, "%s - nonzero write bulk status "
427 "received: %d\n", __func__, status);
429 spin_lock(&portdata->lock);
430 --portdata->outstanding_urbs;
431 spin_unlock(&portdata->lock);
432 spin_lock(&intfdata->susp_lock);
433 --intfdata->in_flight;
434 spin_unlock(&intfdata->susp_lock);
436 usb_serial_port_softint(port);
440 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
441 const unsigned char *buf, int count)
443 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
444 struct sierra_intf_private *intfdata;
445 struct usb_serial *serial = port->serial;
447 unsigned char *buffer;
449 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
452 /* verify that we actually have some data to write */
456 portdata = usb_get_serial_port_data(port);
457 intfdata = serial->private;
459 dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
460 spin_lock_irqsave(&portdata->lock, flags);
461 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
462 portdata->outstanding_urbs);
463 if (portdata->outstanding_urbs > N_OUT_URB) {
464 spin_unlock_irqrestore(&portdata->lock, flags);
465 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
468 portdata->outstanding_urbs++;
469 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
470 portdata->outstanding_urbs);
471 spin_unlock_irqrestore(&portdata->lock, flags);
473 retval = usb_autopm_get_interface_async(serial->interface);
475 spin_lock_irqsave(&portdata->lock, flags);
476 portdata->outstanding_urbs--;
477 spin_unlock_irqrestore(&portdata->lock, flags);
481 buffer = kmalloc(writesize, GFP_ATOMIC);
483 dev_err(&port->dev, "out of memory\n");
485 goto error_no_buffer;
488 urb = usb_alloc_urb(0, GFP_ATOMIC);
490 dev_err(&port->dev, "no more free urbs\n");
495 memcpy(buffer, buf, writesize);
497 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
499 usb_fill_bulk_urb(urb, serial->dev,
500 usb_sndbulkpipe(serial->dev,
501 port->bulk_out_endpointAddress),
502 buffer, writesize, sierra_outdat_callback, port);
504 /* Handle the need to send a zero length packet */
505 urb->transfer_flags |= URB_ZERO_PACKET;
507 spin_lock_irqsave(&intfdata->susp_lock, flags);
509 if (intfdata->suspended) {
510 usb_anchor_urb(urb, &portdata->delayed);
511 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
514 usb_anchor_urb(urb, &portdata->active);
516 /* send it down the pipe */
517 retval = usb_submit_urb(urb, GFP_ATOMIC);
519 usb_unanchor_urb(urb);
520 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
521 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
522 "with status = %d\n", __func__, retval);
525 intfdata->in_flight++;
526 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
530 /* we are done with this urb, so let the host driver
531 * really free it when it is finished with it */
540 spin_lock_irqsave(&portdata->lock, flags);
541 --portdata->outstanding_urbs;
542 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
543 portdata->outstanding_urbs);
544 spin_unlock_irqrestore(&portdata->lock, flags);
545 usb_autopm_put_interface_async(serial->interface);
550 static void sierra_indat_callback(struct urb *urb)
554 struct usb_serial_port *port;
555 struct tty_struct *tty;
556 unsigned char *data = urb->transfer_buffer;
557 int status = urb->status;
559 endpoint = usb_pipeendpoint(urb->pipe);
562 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
565 dev_dbg(&port->dev, "%s: nonzero status: %d on"
566 " endpoint %02x\n", __func__, status, endpoint);
568 if (urb->actual_length) {
569 tty = tty_port_tty_get(&port->port);
571 tty_buffer_request_room(tty, urb->actual_length);
572 tty_insert_flip_string(tty, data, urb->actual_length);
573 tty_flip_buffer_push(tty);
576 usb_serial_debug_data(debug, &port->dev, __func__,
577 urb->actual_length, data);
579 dev_dbg(&port->dev, "%s: empty read urb"
580 " received\n", __func__);
584 /* Resubmit urb so we continue receiving */
585 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
586 usb_mark_last_busy(port->serial->dev);
587 err = usb_submit_urb(urb, GFP_ATOMIC);
589 dev_err(&port->dev, "resubmit read urb failed."
596 static void sierra_instat_callback(struct urb *urb)
599 int status = urb->status;
600 struct usb_serial_port *port = urb->context;
601 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
602 struct usb_serial *serial = port->serial;
604 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
605 urb, port, portdata);
608 struct usb_ctrlrequest *req_pkt =
609 (struct usb_ctrlrequest *)urb->transfer_buffer;
612 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
616 if ((req_pkt->bRequestType == 0xA1) &&
617 (req_pkt->bRequest == 0x20)) {
619 unsigned char signals = *((unsigned char *)
620 urb->transfer_buffer +
621 sizeof(struct usb_ctrlrequest));
622 struct tty_struct *tty;
624 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
627 old_dcd_state = portdata->dcd_state;
628 portdata->cts_state = 1;
629 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
630 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
631 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
633 tty = tty_port_tty_get(&port->port);
634 if (tty && !C_CLOCAL(tty) &&
635 old_dcd_state && !portdata->dcd_state)
639 dev_dbg(&port->dev, "%s: type %x req %x\n",
640 __func__, req_pkt->bRequestType,
644 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
646 /* Resubmit urb so we continue receiving IRQ data */
647 if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
648 usb_mark_last_busy(serial->dev);
649 urb->dev = serial->dev;
650 err = usb_submit_urb(urb, GFP_ATOMIC);
652 dev_err(&port->dev, "%s: resubmit intr urb "
653 "failed. (%d)\n", __func__, err);
657 static int sierra_write_room(struct tty_struct *tty)
659 struct usb_serial_port *port = tty->driver_data;
660 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
663 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
665 /* try to give a good number back based on if we have any free urbs at
666 * this point in time */
667 spin_lock_irqsave(&portdata->lock, flags);
668 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
669 spin_unlock_irqrestore(&portdata->lock, flags);
670 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
673 spin_unlock_irqrestore(&portdata->lock, flags);
678 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
681 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
683 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
684 usb_kill_urb(portdata->in_urbs[i]);
686 usb_kill_urb(port->interrupt_in_urb);
689 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
695 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
698 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
699 urb = portdata->in_urbs[i];
702 err = usb_submit_urb(urb, mem_flags);
704 dev_err(&port->dev, "%s: submit urb failed: %d\n",
711 if (ok_cnt && port->interrupt_in_urb) {
712 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
714 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
719 if (ok_cnt > 0) /* at least one rx urb submitted */
725 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
726 int dir, void *ctx, int len,
728 usb_complete_t callback)
736 urb = usb_alloc_urb(0, mem_flags);
738 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
743 buf = kmalloc(len, mem_flags);
745 /* Fill URB using supplied data */
746 usb_fill_bulk_urb(urb, serial->dev,
747 usb_sndbulkpipe(serial->dev, endpoint) | dir,
748 buf, len, callback, ctx);
751 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
752 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
754 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
755 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
757 sierra_release_urb(urb);
764 static void sierra_close(struct usb_serial_port *port)
767 struct usb_serial *serial = port->serial;
768 struct sierra_port_private *portdata;
769 struct sierra_intf_private *intfdata = port->serial->private;
772 dev_dbg(&port->dev, "%s\n", __func__);
773 portdata = usb_get_serial_port_data(port);
775 portdata->rts_state = 0;
776 portdata->dtr_state = 0;
779 mutex_lock(&serial->disc_mutex);
780 if (!serial->disconnected)
781 sierra_send_setup(port);
782 mutex_unlock(&serial->disc_mutex);
783 spin_lock_irq(&intfdata->susp_lock);
784 portdata->opened = 0;
785 spin_unlock_irq(&intfdata->susp_lock);
788 /* Stop reading urbs */
789 sierra_stop_rx_urbs(port);
790 /* .. and release them */
791 for (i = 0; i < N_IN_URB; i++) {
792 sierra_release_urb(portdata->in_urbs[i]);
793 portdata->in_urbs[i] = NULL;
795 usb_autopm_get_interface(serial->interface);
796 serial->interface->needs_remote_wakeup = 0;
800 static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
802 struct sierra_port_private *portdata;
803 struct usb_serial *serial = port->serial;
804 struct sierra_intf_private *intfdata = serial->private;
810 portdata = usb_get_serial_port_data(port);
812 dev_dbg(&port->dev, "%s\n", __func__);
814 /* Set some sane defaults */
815 portdata->rts_state = 1;
816 portdata->dtr_state = 1;
819 endpoint = port->bulk_in_endpointAddress;
820 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
821 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
822 IN_BUFLEN, GFP_KERNEL,
823 sierra_indat_callback);
824 portdata->in_urbs[i] = urb;
826 /* clear halt condition */
827 usb_clear_halt(serial->dev,
828 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
830 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
832 /* get rid of everything as in close */
836 sierra_send_setup(port);
838 serial->interface->needs_remote_wakeup = 1;
839 spin_lock_irq(&intfdata->susp_lock);
840 portdata->opened = 1;
841 spin_unlock_irq(&intfdata->susp_lock);
842 usb_autopm_put_interface(serial->interface);
848 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
850 struct usb_serial *serial = port->serial;
851 struct sierra_port_private *portdata;
853 portdata = usb_get_serial_port_data(port);
854 portdata->rts_state = on;
855 portdata->dtr_state = on;
858 mutex_lock(&serial->disc_mutex);
859 if (!serial->disconnected)
860 sierra_send_setup(port);
861 mutex_unlock(&serial->disc_mutex);
865 static int sierra_startup(struct usb_serial *serial)
867 struct usb_serial_port *port;
868 struct sierra_port_private *portdata;
871 dev_dbg(&serial->dev->dev, "%s\n", __func__);
873 /* Set Device mode to D0 */
874 sierra_set_power_state(serial->dev, 0x0000);
876 /* Check NMEA and set */
878 sierra_vsc_set_nmea(serial->dev, 1);
880 /* Now setup per port private data */
881 for (i = 0; i < serial->num_ports; i++) {
882 port = serial->port[i];
883 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
885 dev_dbg(&port->dev, "%s: kmalloc for "
886 "sierra_port_private (%d) failed!.\n",
890 spin_lock_init(&portdata->lock);
891 init_usb_anchor(&portdata->active);
892 init_usb_anchor(&portdata->delayed);
893 /* Set the port private data pointer */
894 usb_set_serial_port_data(port, portdata);
900 static void sierra_release(struct usb_serial *serial)
903 struct usb_serial_port *port;
904 struct sierra_port_private *portdata;
906 dev_dbg(&serial->dev->dev, "%s\n", __func__);
908 for (i = 0; i < serial->num_ports; ++i) {
909 port = serial->port[i];
912 portdata = usb_get_serial_port_data(port);
920 static void stop_read_write_urbs(struct usb_serial *serial)
923 struct usb_serial_port *port;
924 struct sierra_port_private *portdata;
926 /* Stop reading/writing urbs */
927 for (i = 0; i < serial->num_ports; ++i) {
928 port = serial->port[i];
929 portdata = usb_get_serial_port_data(port);
930 for (j = 0; j < N_IN_URB; j++)
931 usb_kill_urb(portdata->in_urbs[j]);
932 usb_kill_anchored_urbs(&portdata->active);
936 static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
938 struct sierra_intf_private *intfdata;
941 if (serial->dev->auto_pm) {
942 intfdata = serial->private;
943 spin_lock_irq(&intfdata->susp_lock);
944 b = intfdata->in_flight;
947 spin_unlock_irq(&intfdata->susp_lock);
950 intfdata->suspended = 1;
951 spin_unlock_irq(&intfdata->susp_lock);
954 stop_read_write_urbs(serial);
959 static int sierra_resume(struct usb_serial *serial)
961 struct usb_serial_port *port;
962 struct sierra_intf_private *intfdata = serial->private;
963 struct sierra_port_private *portdata;
968 spin_lock_irq(&intfdata->susp_lock);
969 for (i = 0; i < serial->num_ports; i++) {
970 port = serial->port[i];
971 portdata = usb_get_serial_port_data(port);
973 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
974 usb_anchor_urb(urb, &portdata->active);
975 intfdata->in_flight++;
976 err = usb_submit_urb(urb, GFP_ATOMIC);
978 intfdata->in_flight--;
979 usb_unanchor_urb(urb);
980 usb_scuttle_anchored_urbs(&portdata->delayed);
985 if (portdata->opened) {
986 err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
991 intfdata->suspended = 0;
992 spin_unlock_irq(&intfdata->susp_lock);
994 return ec ? -EIO : 0;
997 #define sierra_suspend NULL
998 #define sierra_resume NULL
1001 static struct usb_serial_driver sierra_device = {
1003 .owner = THIS_MODULE,
1006 .description = "Sierra USB modem",
1007 .id_table = id_table,
1008 .usb_driver = &sierra_driver,
1009 .calc_num_ports = sierra_calc_num_ports,
1010 .probe = sierra_probe,
1011 .open = sierra_open,
1012 .close = sierra_close,
1013 .dtr_rts = sierra_dtr_rts,
1014 .write = sierra_write,
1015 .write_room = sierra_write_room,
1016 .set_termios = sierra_set_termios,
1017 .tiocmget = sierra_tiocmget,
1018 .tiocmset = sierra_tiocmset,
1019 .attach = sierra_startup,
1020 .release = sierra_release,
1021 .suspend = sierra_suspend,
1022 .resume = sierra_resume,
1023 .read_int_callback = sierra_instat_callback,
1026 /* Functions used by new usb-serial code. */
1027 static int __init sierra_init(void)
1030 retval = usb_serial_register(&sierra_device);
1032 goto failed_device_register;
1035 retval = usb_register(&sierra_driver);
1037 goto failed_driver_register;
1039 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1044 failed_driver_register:
1045 usb_serial_deregister(&sierra_device);
1046 failed_device_register:
1050 static void __exit sierra_exit(void)
1052 usb_deregister(&sierra_driver);
1053 usb_serial_deregister(&sierra_device);
1056 module_init(sierra_init);
1057 module_exit(sierra_exit);
1059 MODULE_AUTHOR(DRIVER_AUTHOR);
1060 MODULE_DESCRIPTION(DRIVER_DESC);
1061 MODULE_VERSION(DRIVER_VERSION);
1062 MODULE_LICENSE("GPL");
1064 module_param(nmea, bool, S_IRUGO | S_IWUSR);
1065 MODULE_PARM_DESC(nmea, "NMEA streaming");
1067 module_param(debug, bool, S_IRUGO | S_IWUSR);
1068 MODULE_PARM_DESC(debug, "Debug messages");