USB: Add PID for Sierra 250U to drivers/usb/serial/sierra.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / serial / sierra.c
blob328578bdb455161bdcb9008317c2e8d8111e5bc1
1 /*
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
36 #define N_IN_URB 8
37 #define N_OUT_URB 64
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 */
45 static int debug;
46 static int nmea;
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 {
55 spinlock_t susp_lock;
56 unsigned int suspended:1;
57 int in_flight;
60 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
62 int result;
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 */
68 0, /* __u16 index */
69 NULL, /* void *data */
70 0, /* __u16 size */
71 USB_CTRL_SET_TIMEOUT); /* int timeout */
72 return result;
75 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
77 int result;
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 */
85 0, /* __u16 size */
86 USB_CTRL_SET_TIMEOUT); /* int timeout */
87 return result;
90 static int sierra_calc_num_ports(struct usb_serial *serial)
92 int num_ports = 0;
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 */
101 if (ifnum == 0x99)
102 num_ports = 0;
103 else if (numendpoints <= 3)
104 num_ports = 1;
105 else
106 num_ports = (numendpoints-1)/2;
107 return num_ports;
110 static int is_blacklisted(const u8 ifnum,
111 const struct sierra_iface_info *blacklist)
113 const u8 *info;
114 int i;
116 if (blacklist) {
117 info = blacklist->ifaceinfo;
119 for (i = 0; i < blacklist->infolen; i++) {
120 if (info[i] == ifnum)
121 return 1;
124 return 0;
127 static int sierra_calc_interface(struct usb_serial *serial)
129 int interface;
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;
145 return interface;
148 static int sierra_probe(struct usb_serial *serial,
149 const struct usb_device_id *id)
151 int result = 0;
152 struct usb_device *udev;
153 struct sierra_intf_private *data;
154 u8 ifnum;
156 udev = serial->dev;
157 dev_dbg(&udev->dev, "%s\n", __func__);
159 ifnum = sierra_calc_interface(serial);
161 * If this interface supports more than 1 alternate
162 * select the 2nd one
164 if (serial->interface->num_altsetting == 2) {
165 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
166 ifnum);
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);
178 return -ENODEV;
181 data = serial->private = kzalloc(sizeof(struct sierra_intf_private), GFP_KERNEL);
182 if (!data)
183 return -ENOMEM;
184 spin_lock_init(&data->susp_lock);
186 return result;
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, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
199 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
201 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
202 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
203 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
204 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
205 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
206 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
207 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
208 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
209 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
210 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
211 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
212 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
213 { USB_DEVICE(0x1199, 0x0301) }, /* Sierra Wireless USB Dongle 250U */
214 /* Sierra Wireless C597 */
215 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
216 /* Sierra Wireless T598 */
217 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
218 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
219 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
220 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
221 { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
223 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
224 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
225 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
226 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
227 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
228 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
229 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
230 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
231 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
232 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
233 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
234 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
235 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
236 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
237 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
238 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
239 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
240 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
241 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
242 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
243 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
244 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
245 { USB_DEVICE(0x1199, 0x683C) },
246 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
247 /* Sierra Wireless MC8790, MC8791, MC8792 */
248 { USB_DEVICE(0x1199, 0x683E) },
249 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
250 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
251 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
252 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
253 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
254 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
255 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
256 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
257 /* Sierra Wireless C885 */
258 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
259 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
260 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
261 /* Sierra Wireless C22/C33 */
262 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
263 /* Sierra Wireless HSPA Non-Composite Device */
264 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
265 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
266 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
267 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
272 MODULE_DEVICE_TABLE(usb, id_table);
274 static struct usb_driver sierra_driver = {
275 .name = "sierra",
276 .probe = usb_serial_probe,
277 .disconnect = usb_serial_disconnect,
278 .suspend = usb_serial_suspend,
279 .resume = usb_serial_resume,
280 .id_table = id_table,
281 .no_dynamic_id = 1,
282 .supports_autosuspend = 1,
285 struct sierra_port_private {
286 spinlock_t lock; /* lock the structure */
287 int outstanding_urbs; /* number of out urbs in flight */
288 struct usb_anchor active;
289 struct usb_anchor delayed;
291 /* Input endpoints and buffers for this port */
292 struct urb *in_urbs[N_IN_URB];
294 /* Settings for the port */
295 int rts_state; /* Handshaking pins (outputs) */
296 int dtr_state;
297 int cts_state; /* Handshaking pins (inputs) */
298 int dsr_state;
299 int dcd_state;
300 int ri_state;
301 unsigned int opened:1;
304 static int sierra_send_setup(struct usb_serial_port *port)
306 struct usb_serial *serial = port->serial;
307 struct sierra_port_private *portdata;
308 __u16 interface = 0;
309 int val = 0;
310 int do_send = 0;
311 int retval;
313 dev_dbg(&port->dev, "%s\n", __func__);
315 portdata = usb_get_serial_port_data(port);
317 if (portdata->dtr_state)
318 val |= 0x01;
319 if (portdata->rts_state)
320 val |= 0x02;
322 /* If composite device then properly report interface */
323 if (serial->num_ports == 1) {
324 interface = sierra_calc_interface(serial);
325 /* Control message is sent only to interfaces with
326 * interrupt_in endpoints
328 if (port->interrupt_in_urb) {
329 /* send control message */
330 do_send = 1;
334 /* Otherwise the need to do non-composite mapping */
335 else {
336 if (port->bulk_out_endpointAddress == 2)
337 interface = 0;
338 else if (port->bulk_out_endpointAddress == 4)
339 interface = 1;
340 else if (port->bulk_out_endpointAddress == 5)
341 interface = 2;
343 do_send = 1;
345 if (!do_send)
346 return 0;
348 usb_autopm_get_interface(serial->interface);
349 retval = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
350 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
351 usb_autopm_put_interface(serial->interface);
353 return retval;
356 static void sierra_set_termios(struct tty_struct *tty,
357 struct usb_serial_port *port, struct ktermios *old_termios)
359 dev_dbg(&port->dev, "%s\n", __func__);
360 tty_termios_copy_hw(tty->termios, old_termios);
361 sierra_send_setup(port);
364 static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
366 struct usb_serial_port *port = tty->driver_data;
367 unsigned int value;
368 struct sierra_port_private *portdata;
370 dev_dbg(&port->dev, "%s\n", __func__);
371 portdata = usb_get_serial_port_data(port);
373 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
374 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
375 ((portdata->cts_state) ? TIOCM_CTS : 0) |
376 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
377 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
378 ((portdata->ri_state) ? TIOCM_RNG : 0);
380 return value;
383 static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
384 unsigned int set, unsigned int clear)
386 struct usb_serial_port *port = tty->driver_data;
387 struct sierra_port_private *portdata;
389 portdata = usb_get_serial_port_data(port);
391 if (set & TIOCM_RTS)
392 portdata->rts_state = 1;
393 if (set & TIOCM_DTR)
394 portdata->dtr_state = 1;
396 if (clear & TIOCM_RTS)
397 portdata->rts_state = 0;
398 if (clear & TIOCM_DTR)
399 portdata->dtr_state = 0;
400 return sierra_send_setup(port);
403 static void sierra_release_urb(struct urb *urb)
405 struct usb_serial_port *port;
406 if (urb) {
407 port = urb->context;
408 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
409 kfree(urb->transfer_buffer);
410 usb_free_urb(urb);
414 static void sierra_outdat_callback(struct urb *urb)
416 struct usb_serial_port *port = urb->context;
417 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
418 struct sierra_intf_private *intfdata;
419 int status = urb->status;
421 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
422 intfdata = port->serial->private;
424 /* free up the transfer buffer, as usb_free_urb() does not do this */
425 kfree(urb->transfer_buffer);
426 usb_autopm_put_interface_async(port->serial->interface);
427 if (status)
428 dev_dbg(&port->dev, "%s - nonzero write bulk status "
429 "received: %d\n", __func__, status);
431 spin_lock(&portdata->lock);
432 --portdata->outstanding_urbs;
433 spin_unlock(&portdata->lock);
434 spin_lock(&intfdata->susp_lock);
435 --intfdata->in_flight;
436 spin_unlock(&intfdata->susp_lock);
438 usb_serial_port_softint(port);
441 /* Write */
442 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
443 const unsigned char *buf, int count)
445 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
446 struct sierra_intf_private *intfdata;
447 struct usb_serial *serial = port->serial;
448 unsigned long flags;
449 unsigned char *buffer;
450 struct urb *urb;
451 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
452 int retval = 0;
454 /* verify that we actually have some data to write */
455 if (count == 0)
456 return 0;
458 portdata = usb_get_serial_port_data(port);
459 intfdata = serial->private;
461 dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
462 spin_lock_irqsave(&portdata->lock, flags);
463 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
464 portdata->outstanding_urbs);
465 if (portdata->outstanding_urbs > N_OUT_URB) {
466 spin_unlock_irqrestore(&portdata->lock, flags);
467 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
468 return 0;
470 portdata->outstanding_urbs++;
471 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
472 portdata->outstanding_urbs);
473 spin_unlock_irqrestore(&portdata->lock, flags);
475 retval = usb_autopm_get_interface_async(serial->interface);
476 if (retval < 0) {
477 spin_lock_irqsave(&portdata->lock, flags);
478 portdata->outstanding_urbs--;
479 spin_unlock_irqrestore(&portdata->lock, flags);
480 goto error_simple;
483 buffer = kmalloc(writesize, GFP_ATOMIC);
484 if (!buffer) {
485 dev_err(&port->dev, "out of memory\n");
486 retval = -ENOMEM;
487 goto error_no_buffer;
490 urb = usb_alloc_urb(0, GFP_ATOMIC);
491 if (!urb) {
492 dev_err(&port->dev, "no more free urbs\n");
493 retval = -ENOMEM;
494 goto error_no_urb;
497 memcpy(buffer, buf, writesize);
499 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
501 usb_fill_bulk_urb(urb, serial->dev,
502 usb_sndbulkpipe(serial->dev,
503 port->bulk_out_endpointAddress),
504 buffer, writesize, sierra_outdat_callback, port);
506 /* Handle the need to send a zero length packet */
507 urb->transfer_flags |= URB_ZERO_PACKET;
509 spin_lock_irqsave(&intfdata->susp_lock, flags);
511 if (intfdata->suspended) {
512 usb_anchor_urb(urb, &portdata->delayed);
513 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
514 goto skip_power;
515 } else {
516 usb_anchor_urb(urb, &portdata->active);
518 /* send it down the pipe */
519 retval = usb_submit_urb(urb, GFP_ATOMIC);
520 if (retval) {
521 usb_unanchor_urb(urb);
522 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
523 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
524 "with status = %d\n", __func__, retval);
525 goto error;
526 } else {
527 intfdata->in_flight++;
528 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
531 skip_power:
532 /* we are done with this urb, so let the host driver
533 * really free it when it is finished with it */
534 usb_free_urb(urb);
536 return writesize;
537 error:
538 usb_free_urb(urb);
539 error_no_urb:
540 kfree(buffer);
541 error_no_buffer:
542 spin_lock_irqsave(&portdata->lock, flags);
543 --portdata->outstanding_urbs;
544 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
545 portdata->outstanding_urbs);
546 spin_unlock_irqrestore(&portdata->lock, flags);
547 usb_autopm_put_interface_async(serial->interface);
548 error_simple:
549 return retval;
552 static void sierra_indat_callback(struct urb *urb)
554 int err;
555 int endpoint;
556 struct usb_serial_port *port;
557 struct tty_struct *tty;
558 unsigned char *data = urb->transfer_buffer;
559 int status = urb->status;
561 endpoint = usb_pipeendpoint(urb->pipe);
562 port = urb->context;
564 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
566 if (status) {
567 dev_dbg(&port->dev, "%s: nonzero status: %d on"
568 " endpoint %02x\n", __func__, status, endpoint);
569 } else {
570 if (urb->actual_length) {
571 tty = tty_port_tty_get(&port->port);
572 if (tty) {
573 tty_buffer_request_room(tty,
574 urb->actual_length);
575 tty_insert_flip_string(tty, data,
576 urb->actual_length);
577 tty_flip_buffer_push(tty);
579 tty_kref_put(tty);
580 usb_serial_debug_data(debug, &port->dev,
581 __func__, urb->actual_length, data);
583 } else {
584 dev_dbg(&port->dev, "%s: empty read urb"
585 " received\n", __func__);
589 /* Resubmit urb so we continue receiving */
590 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
591 usb_mark_last_busy(port->serial->dev);
592 err = usb_submit_urb(urb, GFP_ATOMIC);
593 if (err)
594 dev_err(&port->dev, "resubmit read urb failed."
595 "(%d)\n", err);
598 return;
601 static void sierra_instat_callback(struct urb *urb)
603 int err;
604 int status = urb->status;
605 struct usb_serial_port *port = urb->context;
606 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
607 struct usb_serial *serial = port->serial;
609 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
610 urb, port, portdata);
612 if (status == 0) {
613 struct usb_ctrlrequest *req_pkt =
614 (struct usb_ctrlrequest *)urb->transfer_buffer;
616 if (!req_pkt) {
617 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
618 __func__);
619 return;
621 if ((req_pkt->bRequestType == 0xA1) &&
622 (req_pkt->bRequest == 0x20)) {
623 int old_dcd_state;
624 unsigned char signals = *((unsigned char *)
625 urb->transfer_buffer +
626 sizeof(struct usb_ctrlrequest));
627 struct tty_struct *tty;
629 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
630 signals);
632 old_dcd_state = portdata->dcd_state;
633 portdata->cts_state = 1;
634 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
635 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
636 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
638 tty = tty_port_tty_get(&port->port);
639 if (tty && !C_CLOCAL(tty) &&
640 old_dcd_state && !portdata->dcd_state)
641 tty_hangup(tty);
642 tty_kref_put(tty);
643 } else {
644 dev_dbg(&port->dev, "%s: type %x req %x\n",
645 __func__, req_pkt->bRequestType,
646 req_pkt->bRequest);
648 } else
649 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
651 /* Resubmit urb so we continue receiving IRQ data */
652 if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
653 usb_mark_last_busy(serial->dev);
654 urb->dev = serial->dev;
655 err = usb_submit_urb(urb, GFP_ATOMIC);
656 if (err)
657 dev_err(&port->dev, "%s: resubmit intr urb "
658 "failed. (%d)\n", __func__, err);
662 static int sierra_write_room(struct tty_struct *tty)
664 struct usb_serial_port *port = tty->driver_data;
665 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
666 unsigned long flags;
668 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
670 /* try to give a good number back based on if we have any free urbs at
671 * this point in time */
672 spin_lock_irqsave(&portdata->lock, flags);
673 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
674 spin_unlock_irqrestore(&portdata->lock, flags);
675 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
676 return 0;
678 spin_unlock_irqrestore(&portdata->lock, flags);
680 return 2048;
683 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
685 int i;
686 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
688 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
689 usb_kill_urb(portdata->in_urbs[i]);
691 usb_kill_urb(port->interrupt_in_urb);
694 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
696 int ok_cnt;
697 int err = -EINVAL;
698 int i;
699 struct urb *urb;
700 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
702 ok_cnt = 0;
703 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
704 urb = portdata->in_urbs[i];
705 if (!urb)
706 continue;
707 err = usb_submit_urb(urb, mem_flags);
708 if (err) {
709 dev_err(&port->dev, "%s: submit urb failed: %d\n",
710 __func__, err);
711 } else {
712 ok_cnt++;
716 if (ok_cnt && port->interrupt_in_urb) {
717 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
718 if (err) {
719 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
720 __func__, err);
724 if (ok_cnt > 0) /* at least one rx urb submitted */
725 return 0;
726 else
727 return err;
730 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
731 int dir, void *ctx, int len,
732 gfp_t mem_flags,
733 usb_complete_t callback)
735 struct urb *urb;
736 u8 *buf;
738 if (endpoint == -1)
739 return NULL;
741 urb = usb_alloc_urb(0, mem_flags);
742 if (urb == NULL) {
743 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
744 __func__, endpoint);
745 return NULL;
748 buf = kmalloc(len, mem_flags);
749 if (buf) {
750 /* Fill URB using supplied data */
751 usb_fill_bulk_urb(urb, serial->dev,
752 usb_sndbulkpipe(serial->dev, endpoint) | dir,
753 buf, len, callback, ctx);
755 /* debug */
756 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
757 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
758 } else {
759 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
760 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
762 sierra_release_urb(urb);
763 urb = NULL;
766 return urb;
769 static void sierra_close(struct usb_serial_port *port)
771 int i;
772 struct usb_serial *serial = port->serial;
773 struct sierra_port_private *portdata;
774 struct sierra_intf_private *intfdata = port->serial->private;
777 dev_dbg(&port->dev, "%s\n", __func__);
778 portdata = usb_get_serial_port_data(port);
780 portdata->rts_state = 0;
781 portdata->dtr_state = 0;
783 if (serial->dev) {
784 mutex_lock(&serial->disc_mutex);
785 if (!serial->disconnected) {
786 serial->interface->needs_remote_wakeup = 0;
787 usb_autopm_get_interface(serial->interface);
788 sierra_send_setup(port);
790 mutex_unlock(&serial->disc_mutex);
791 spin_lock_irq(&intfdata->susp_lock);
792 portdata->opened = 0;
793 spin_unlock_irq(&intfdata->susp_lock);
796 /* Stop reading urbs */
797 sierra_stop_rx_urbs(port);
798 /* .. and release them */
799 for (i = 0; i < N_IN_URB; i++) {
800 sierra_release_urb(portdata->in_urbs[i]);
801 portdata->in_urbs[i] = NULL;
806 static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
808 struct sierra_port_private *portdata;
809 struct usb_serial *serial = port->serial;
810 struct sierra_intf_private *intfdata = serial->private;
811 int i;
812 int err;
813 int endpoint;
814 struct urb *urb;
816 portdata = usb_get_serial_port_data(port);
818 dev_dbg(&port->dev, "%s\n", __func__);
820 /* Set some sane defaults */
821 portdata->rts_state = 1;
822 portdata->dtr_state = 1;
825 endpoint = port->bulk_in_endpointAddress;
826 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
827 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
828 IN_BUFLEN, GFP_KERNEL,
829 sierra_indat_callback);
830 portdata->in_urbs[i] = urb;
832 /* clear halt condition */
833 usb_clear_halt(serial->dev,
834 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
836 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
837 if (err) {
838 /* get rid of everything as in close */
839 sierra_close(port);
840 /* restore balance for autopm */
841 usb_autopm_put_interface(serial->interface);
842 return err;
844 sierra_send_setup(port);
846 serial->interface->needs_remote_wakeup = 1;
847 spin_lock_irq(&intfdata->susp_lock);
848 portdata->opened = 1;
849 spin_unlock_irq(&intfdata->susp_lock);
850 usb_autopm_put_interface(serial->interface);
852 return 0;
856 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
858 struct usb_serial *serial = port->serial;
859 struct sierra_port_private *portdata;
861 portdata = usb_get_serial_port_data(port);
862 portdata->rts_state = on;
863 portdata->dtr_state = on;
865 if (serial->dev) {
866 mutex_lock(&serial->disc_mutex);
867 if (!serial->disconnected)
868 sierra_send_setup(port);
869 mutex_unlock(&serial->disc_mutex);
873 static int sierra_startup(struct usb_serial *serial)
875 struct usb_serial_port *port;
876 struct sierra_port_private *portdata;
877 int i;
879 dev_dbg(&serial->dev->dev, "%s\n", __func__);
881 /* Set Device mode to D0 */
882 sierra_set_power_state(serial->dev, 0x0000);
884 /* Check NMEA and set */
885 if (nmea)
886 sierra_vsc_set_nmea(serial->dev, 1);
888 /* Now setup per port private data */
889 for (i = 0; i < serial->num_ports; i++) {
890 port = serial->port[i];
891 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
892 if (!portdata) {
893 dev_dbg(&port->dev, "%s: kmalloc for "
894 "sierra_port_private (%d) failed!.\n",
895 __func__, i);
896 return -ENOMEM;
898 spin_lock_init(&portdata->lock);
899 init_usb_anchor(&portdata->active);
900 init_usb_anchor(&portdata->delayed);
901 /* Set the port private data pointer */
902 usb_set_serial_port_data(port, portdata);
905 return 0;
908 static void sierra_release(struct usb_serial *serial)
910 int i;
911 struct usb_serial_port *port;
912 struct sierra_port_private *portdata;
914 dev_dbg(&serial->dev->dev, "%s\n", __func__);
916 for (i = 0; i < serial->num_ports; ++i) {
917 port = serial->port[i];
918 if (!port)
919 continue;
920 portdata = usb_get_serial_port_data(port);
921 if (!portdata)
922 continue;
923 kfree(portdata);
927 #ifdef CONFIG_PM
928 static void stop_read_write_urbs(struct usb_serial *serial)
930 int i;
931 struct usb_serial_port *port;
932 struct sierra_port_private *portdata;
934 /* Stop reading/writing urbs */
935 for (i = 0; i < serial->num_ports; ++i) {
936 port = serial->port[i];
937 portdata = usb_get_serial_port_data(port);
938 sierra_stop_rx_urbs(port);
939 usb_kill_anchored_urbs(&portdata->active);
943 static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
945 struct sierra_intf_private *intfdata;
946 int b;
948 if (serial->dev->auto_pm) {
949 intfdata = serial->private;
950 spin_lock_irq(&intfdata->susp_lock);
951 b = intfdata->in_flight;
953 if (b) {
954 spin_unlock_irq(&intfdata->susp_lock);
955 return -EBUSY;
956 } else {
957 intfdata->suspended = 1;
958 spin_unlock_irq(&intfdata->susp_lock);
961 stop_read_write_urbs(serial);
963 return 0;
966 static int sierra_resume(struct usb_serial *serial)
968 struct usb_serial_port *port;
969 struct sierra_intf_private *intfdata = serial->private;
970 struct sierra_port_private *portdata;
971 struct urb *urb;
972 int ec = 0;
973 int i, err;
975 spin_lock_irq(&intfdata->susp_lock);
976 for (i = 0; i < serial->num_ports; i++) {
977 port = serial->port[i];
978 portdata = usb_get_serial_port_data(port);
980 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
981 usb_anchor_urb(urb, &portdata->active);
982 intfdata->in_flight++;
983 err = usb_submit_urb(urb, GFP_ATOMIC);
984 if (err < 0) {
985 intfdata->in_flight--;
986 usb_unanchor_urb(urb);
987 usb_scuttle_anchored_urbs(&portdata->delayed);
988 break;
992 if (portdata->opened) {
993 err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
994 if (err)
995 ec++;
998 intfdata->suspended = 0;
999 spin_unlock_irq(&intfdata->susp_lock);
1001 return ec ? -EIO : 0;
1003 #else
1004 #define sierra_suspend NULL
1005 #define sierra_resume NULL
1006 #endif
1008 static struct usb_serial_driver sierra_device = {
1009 .driver = {
1010 .owner = THIS_MODULE,
1011 .name = "sierra",
1013 .description = "Sierra USB modem",
1014 .id_table = id_table,
1015 .usb_driver = &sierra_driver,
1016 .calc_num_ports = sierra_calc_num_ports,
1017 .probe = sierra_probe,
1018 .open = sierra_open,
1019 .close = sierra_close,
1020 .dtr_rts = sierra_dtr_rts,
1021 .write = sierra_write,
1022 .write_room = sierra_write_room,
1023 .set_termios = sierra_set_termios,
1024 .tiocmget = sierra_tiocmget,
1025 .tiocmset = sierra_tiocmset,
1026 .attach = sierra_startup,
1027 .release = sierra_release,
1028 .suspend = sierra_suspend,
1029 .resume = sierra_resume,
1030 .read_int_callback = sierra_instat_callback,
1033 /* Functions used by new usb-serial code. */
1034 static int __init sierra_init(void)
1036 int retval;
1037 retval = usb_serial_register(&sierra_device);
1038 if (retval)
1039 goto failed_device_register;
1042 retval = usb_register(&sierra_driver);
1043 if (retval)
1044 goto failed_driver_register;
1046 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1047 DRIVER_DESC "\n");
1049 return 0;
1051 failed_driver_register:
1052 usb_serial_deregister(&sierra_device);
1053 failed_device_register:
1054 return retval;
1057 static void __exit sierra_exit(void)
1059 usb_deregister(&sierra_driver);
1060 usb_serial_deregister(&sierra_device);
1063 module_init(sierra_init);
1064 module_exit(sierra_exit);
1066 MODULE_AUTHOR(DRIVER_AUTHOR);
1067 MODULE_DESCRIPTION(DRIVER_DESC);
1068 MODULE_VERSION(DRIVER_VERSION);
1069 MODULE_LICENSE("GPL");
1071 module_param(nmea, bool, S_IRUGO | S_IWUSR);
1072 MODULE_PARM_DESC(nmea, "NMEA streaming");
1074 module_param(debug, bool, S_IRUGO | S_IWUSR);
1075 MODULE_PARM_DESC(debug, "Debug messages");