USB: serial: sierra driver send_setup() autopm fix
[linux-2.6/verdex.git] / drivers / usb / serial / sierra.c
blobc5fbaa5dde05fd3fcf1198ace40885aa806d95d4
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.7"
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 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
56 int result;
57 dev_dbg(&udev->dev, "%s\n", __func__);
58 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
59 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
60 USB_TYPE_VENDOR, /* __u8 request type */
61 swiState, /* __u16 value */
62 0, /* __u16 index */
63 NULL, /* void *data */
64 0, /* __u16 size */
65 USB_CTRL_SET_TIMEOUT); /* int timeout */
66 return result;
69 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
71 int result;
72 dev_dbg(&udev->dev, "%s\n", __func__);
73 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
74 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
75 USB_TYPE_VENDOR, /* __u8 request type */
76 enable, /* __u16 value */
77 0x0000, /* __u16 index */
78 NULL, /* void *data */
79 0, /* __u16 size */
80 USB_CTRL_SET_TIMEOUT); /* int timeout */
81 return result;
84 static int sierra_calc_num_ports(struct usb_serial *serial)
86 int num_ports = 0;
87 u8 ifnum, numendpoints;
89 dev_dbg(&serial->dev->dev, "%s\n", __func__);
91 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
92 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
94 /* Dummy interface present on some SKUs should be ignored */
95 if (ifnum == 0x99)
96 num_ports = 0;
97 else if (numendpoints <= 3)
98 num_ports = 1;
99 else
100 num_ports = (numendpoints-1)/2;
101 return num_ports;
104 static int is_blacklisted(const u8 ifnum,
105 const struct sierra_iface_info *blacklist)
107 const u8 *info;
108 int i;
110 if (blacklist) {
111 info = blacklist->ifaceinfo;
113 for (i = 0; i < blacklist->infolen; i++) {
114 if (info[i] == ifnum)
115 return 1;
118 return 0;
121 static int sierra_calc_interface(struct usb_serial *serial)
123 int interface;
124 struct usb_interface *p_interface;
125 struct usb_host_interface *p_host_interface;
126 dev_dbg(&serial->dev->dev, "%s\n", __func__);
128 /* Get the interface structure pointer from the serial struct */
129 p_interface = serial->interface;
131 /* Get a pointer to the host interface structure */
132 p_host_interface = p_interface->cur_altsetting;
134 /* read the interface descriptor for this active altsetting
135 * to find out the interface number we are on
137 interface = p_host_interface->desc.bInterfaceNumber;
139 return interface;
142 static int sierra_probe(struct usb_serial *serial,
143 const struct usb_device_id *id)
145 int result = 0;
146 struct usb_device *udev;
147 u8 ifnum;
149 udev = serial->dev;
150 dev_dbg(&udev->dev, "%s\n", __func__);
152 ifnum = sierra_calc_interface(serial);
154 * If this interface supports more than 1 alternate
155 * select the 2nd one
157 if (serial->interface->num_altsetting == 2) {
158 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
159 ifnum);
160 /* We know the alternate setting is 1 for the MC8785 */
161 usb_set_interface(udev, ifnum, 1);
164 /* ifnum could have changed - by calling usb_set_interface */
165 ifnum = sierra_calc_interface(serial);
167 if (is_blacklisted(ifnum,
168 (struct sierra_iface_info *)id->driver_info)) {
169 dev_dbg(&serial->dev->dev,
170 "Ignoring blacklisted interface #%d\n", ifnum);
171 return -ENODEV;
174 return result;
177 static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
178 static const struct sierra_iface_info direct_ip_interface_blacklist = {
179 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
180 .ifaceinfo = direct_ip_non_serial_ifaces,
183 static struct usb_device_id id_table [] = {
184 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
185 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
186 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
188 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
189 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
190 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
191 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
192 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
193 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
194 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
195 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
196 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
197 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
198 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
199 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
200 /* Sierra Wireless C597 */
201 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
202 /* Sierra Wireless T598 */
203 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
204 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
205 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
206 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
207 { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
209 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
210 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
211 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
212 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
213 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
214 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
215 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
216 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
217 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
218 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
219 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
220 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
221 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
222 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
223 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
224 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
225 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
226 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
227 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
228 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
229 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
230 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
231 { USB_DEVICE(0x1199, 0x683C) },
232 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
233 /* Sierra Wireless MC8790, MC8791, MC8792 */
234 { USB_DEVICE(0x1199, 0x683E) },
235 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
236 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
237 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
238 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
239 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
240 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
241 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
242 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
243 /* Sierra Wireless C885 */
244 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
245 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
246 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
247 /* Sierra Wireless C22/C33 */
248 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
249 /* Sierra Wireless HSPA Non-Composite Device */
250 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
251 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
252 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
253 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
258 MODULE_DEVICE_TABLE(usb, id_table);
260 static struct usb_driver sierra_driver = {
261 .name = "sierra",
262 .probe = usb_serial_probe,
263 .disconnect = usb_serial_disconnect,
264 .id_table = id_table,
265 .no_dynamic_id = 1,
268 struct sierra_port_private {
269 spinlock_t lock; /* lock the structure */
270 int outstanding_urbs; /* number of out urbs in flight */
272 /* Input endpoints and buffers for this port */
273 struct urb *in_urbs[N_IN_URB];
275 /* Settings for the port */
276 int rts_state; /* Handshaking pins (outputs) */
277 int dtr_state;
278 int cts_state; /* Handshaking pins (inputs) */
279 int dsr_state;
280 int dcd_state;
281 int ri_state;
284 static int sierra_send_setup(struct usb_serial_port *port)
286 struct usb_serial *serial = port->serial;
287 struct sierra_port_private *portdata;
288 __u16 interface = 0;
289 int val = 0;
290 int do_send = 0;
291 int retval;
293 dev_dbg(&port->dev, "%s\n", __func__);
295 portdata = usb_get_serial_port_data(port);
297 if (portdata->dtr_state)
298 val |= 0x01;
299 if (portdata->rts_state)
300 val |= 0x02;
302 /* If composite device then properly report interface */
303 if (serial->num_ports == 1) {
304 interface = sierra_calc_interface(serial);
305 /* Control message is sent only to interfaces with
306 * interrupt_in endpoints
308 if (port->interrupt_in_urb) {
309 /* send control message */
310 do_send = 1;
314 /* Otherwise the need to do non-composite mapping */
315 else {
316 if (port->bulk_out_endpointAddress == 2)
317 interface = 0;
318 else if (port->bulk_out_endpointAddress == 4)
319 interface = 1;
320 else if (port->bulk_out_endpointAddress == 5)
321 interface = 2;
323 do_send = 1;
325 if (!do_send)
326 return 0;
328 usb_autopm_get_interface(serial->interface);
329 retval = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
330 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
331 usb_autopm_put_interface(serial->interface);
333 return retval;
336 static void sierra_set_termios(struct tty_struct *tty,
337 struct usb_serial_port *port, struct ktermios *old_termios)
339 dev_dbg(&port->dev, "%s\n", __func__);
340 tty_termios_copy_hw(tty->termios, old_termios);
341 sierra_send_setup(port);
344 static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
346 struct usb_serial_port *port = tty->driver_data;
347 unsigned int value;
348 struct sierra_port_private *portdata;
350 dev_dbg(&port->dev, "%s\n", __func__);
351 portdata = usb_get_serial_port_data(port);
353 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
354 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
355 ((portdata->cts_state) ? TIOCM_CTS : 0) |
356 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
357 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
358 ((portdata->ri_state) ? TIOCM_RNG : 0);
360 return value;
363 static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
364 unsigned int set, unsigned int clear)
366 struct usb_serial_port *port = tty->driver_data;
367 struct sierra_port_private *portdata;
369 portdata = usb_get_serial_port_data(port);
371 if (set & TIOCM_RTS)
372 portdata->rts_state = 1;
373 if (set & TIOCM_DTR)
374 portdata->dtr_state = 1;
376 if (clear & TIOCM_RTS)
377 portdata->rts_state = 0;
378 if (clear & TIOCM_DTR)
379 portdata->dtr_state = 0;
380 return sierra_send_setup(port);
383 static void sierra_release_urb(struct urb *urb)
385 struct usb_serial_port *port;
386 if (urb) {
387 port = urb->context;
388 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
389 kfree(urb->transfer_buffer);
390 usb_free_urb(urb);
394 static void sierra_outdat_callback(struct urb *urb)
396 struct usb_serial_port *port = urb->context;
397 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
398 int status = urb->status;
399 unsigned long flags;
401 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
403 /* free up the transfer buffer, as usb_free_urb() does not do this */
404 kfree(urb->transfer_buffer);
406 if (status)
407 dev_dbg(&port->dev, "%s - nonzero write bulk status "
408 "received: %d\n", __func__, status);
410 spin_lock_irqsave(&portdata->lock, flags);
411 --portdata->outstanding_urbs;
412 spin_unlock_irqrestore(&portdata->lock, flags);
414 usb_serial_port_softint(port);
417 /* Write */
418 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
419 const unsigned char *buf, int count)
421 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
422 struct usb_serial *serial = port->serial;
423 unsigned long flags;
424 unsigned char *buffer;
425 struct urb *urb;
426 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
427 int retval = 0;
429 /* verify that we actually have some data to write */
430 if (count == 0)
431 return 0;
433 portdata = usb_get_serial_port_data(port);
435 dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
437 spin_lock_irqsave(&portdata->lock, flags);
438 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
439 portdata->outstanding_urbs);
440 if (portdata->outstanding_urbs > N_OUT_URB) {
441 spin_unlock_irqrestore(&portdata->lock, flags);
442 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
443 return 0;
445 portdata->outstanding_urbs++;
446 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
447 portdata->outstanding_urbs);
448 spin_unlock_irqrestore(&portdata->lock, flags);
450 buffer = kmalloc(writesize, GFP_ATOMIC);
451 if (!buffer) {
452 dev_err(&port->dev, "out of memory\n");
453 retval = -ENOMEM;
454 goto error_no_buffer;
457 urb = usb_alloc_urb(0, GFP_ATOMIC);
458 if (!urb) {
459 dev_err(&port->dev, "no more free urbs\n");
460 retval = -ENOMEM;
461 goto error_no_urb;
464 memcpy(buffer, buf, writesize);
466 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
468 usb_fill_bulk_urb(urb, serial->dev,
469 usb_sndbulkpipe(serial->dev,
470 port->bulk_out_endpointAddress),
471 buffer, writesize, sierra_outdat_callback, port);
473 /* Handle the need to send a zero length packet */
474 urb->transfer_flags |= URB_ZERO_PACKET;
476 /* send it down the pipe */
477 retval = usb_submit_urb(urb, GFP_ATOMIC);
478 if (retval) {
479 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
480 "with status = %d\n", __func__, retval);
481 goto error;
484 /* we are done with this urb, so let the host driver
485 * really free it when it is finished with it */
486 usb_free_urb(urb);
488 return writesize;
489 error:
490 usb_free_urb(urb);
491 error_no_urb:
492 kfree(buffer);
493 error_no_buffer:
494 spin_lock_irqsave(&portdata->lock, flags);
495 --portdata->outstanding_urbs;
496 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
497 portdata->outstanding_urbs);
498 spin_unlock_irqrestore(&portdata->lock, flags);
499 return retval;
502 static void sierra_indat_callback(struct urb *urb)
504 int err;
505 int endpoint;
506 struct usb_serial_port *port;
507 struct tty_struct *tty;
508 unsigned char *data = urb->transfer_buffer;
509 int status = urb->status;
511 endpoint = usb_pipeendpoint(urb->pipe);
512 port = urb->context;
514 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
516 if (status) {
517 dev_dbg(&port->dev, "%s: nonzero status: %d on"
518 " endpoint %02x\n", __func__, status, endpoint);
519 } else {
520 if (urb->actual_length) {
521 tty = tty_port_tty_get(&port->port);
523 tty_buffer_request_room(tty, urb->actual_length);
524 tty_insert_flip_string(tty, data, urb->actual_length);
525 tty_flip_buffer_push(tty);
527 tty_kref_put(tty);
528 usb_serial_debug_data(debug, &port->dev, __func__,
529 urb->actual_length, data);
530 } else {
531 dev_dbg(&port->dev, "%s: empty read urb"
532 " received\n", __func__);
536 /* Resubmit urb so we continue receiving */
537 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
538 err = usb_submit_urb(urb, GFP_ATOMIC);
539 if (err)
540 dev_err(&port->dev, "resubmit read urb failed."
541 "(%d)\n", err);
544 return;
547 static void sierra_instat_callback(struct urb *urb)
549 int err;
550 int status = urb->status;
551 struct usb_serial_port *port = urb->context;
552 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
553 struct usb_serial *serial = port->serial;
555 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
556 urb, port, portdata);
558 if (status == 0) {
559 struct usb_ctrlrequest *req_pkt =
560 (struct usb_ctrlrequest *)urb->transfer_buffer;
562 if (!req_pkt) {
563 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
564 __func__);
565 return;
567 if ((req_pkt->bRequestType == 0xA1) &&
568 (req_pkt->bRequest == 0x20)) {
569 int old_dcd_state;
570 unsigned char signals = *((unsigned char *)
571 urb->transfer_buffer +
572 sizeof(struct usb_ctrlrequest));
573 struct tty_struct *tty;
575 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
576 signals);
578 old_dcd_state = portdata->dcd_state;
579 portdata->cts_state = 1;
580 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
581 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
582 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
584 tty = tty_port_tty_get(&port->port);
585 if (tty && !C_CLOCAL(tty) &&
586 old_dcd_state && !portdata->dcd_state)
587 tty_hangup(tty);
588 tty_kref_put(tty);
589 } else {
590 dev_dbg(&port->dev, "%s: type %x req %x\n",
591 __func__, req_pkt->bRequestType,
592 req_pkt->bRequest);
594 } else
595 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
597 /* Resubmit urb so we continue receiving IRQ data */
598 if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
599 urb->dev = serial->dev;
600 err = usb_submit_urb(urb, GFP_ATOMIC);
601 if (err)
602 dev_err(&port->dev, "%s: resubmit intr urb "
603 "failed. (%d)\n", __func__, err);
607 static int sierra_write_room(struct tty_struct *tty)
609 struct usb_serial_port *port = tty->driver_data;
610 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
611 unsigned long flags;
613 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
615 /* try to give a good number back based on if we have any free urbs at
616 * this point in time */
617 spin_lock_irqsave(&portdata->lock, flags);
618 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
619 spin_unlock_irqrestore(&portdata->lock, flags);
620 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
621 return 0;
623 spin_unlock_irqrestore(&portdata->lock, flags);
625 return 2048;
628 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
630 int i;
631 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
633 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
634 usb_kill_urb(portdata->in_urbs[i]);
636 usb_kill_urb(port->interrupt_in_urb);
639 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
641 int ok_cnt;
642 int err = -EINVAL;
643 int i;
644 struct urb *urb;
645 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
647 ok_cnt = 0;
648 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
649 urb = portdata->in_urbs[i];
650 if (!urb)
651 continue;
652 err = usb_submit_urb(urb, mem_flags);
653 if (err) {
654 dev_err(&port->dev, "%s: submit urb failed: %d\n",
655 __func__, err);
656 } else {
657 ok_cnt++;
661 if (ok_cnt && port->interrupt_in_urb) {
662 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
663 if (err) {
664 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
665 __func__, err);
669 if (ok_cnt > 0) /* at least one rx urb submitted */
670 return 0;
671 else
672 return err;
675 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
676 int dir, void *ctx, int len,
677 gfp_t mem_flags,
678 usb_complete_t callback)
680 struct urb *urb;
681 u8 *buf;
683 if (endpoint == -1)
684 return NULL;
686 urb = usb_alloc_urb(0, mem_flags);
687 if (urb == NULL) {
688 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
689 __func__, endpoint);
690 return NULL;
693 buf = kmalloc(len, mem_flags);
694 if (buf) {
695 /* Fill URB using supplied data */
696 usb_fill_bulk_urb(urb, serial->dev,
697 usb_sndbulkpipe(serial->dev, endpoint) | dir,
698 buf, len, callback, ctx);
700 /* debug */
701 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
702 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
703 } else {
704 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
705 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
707 sierra_release_urb(urb);
708 urb = NULL;
711 return urb;
714 static void sierra_close(struct usb_serial_port *port)
716 int i;
717 struct usb_serial *serial = port->serial;
718 struct sierra_port_private *portdata;
720 dev_dbg(&port->dev, "%s\n", __func__);
721 portdata = usb_get_serial_port_data(port);
723 portdata->rts_state = 0;
724 portdata->dtr_state = 0;
726 if (serial->dev) {
727 mutex_lock(&serial->disc_mutex);
728 if (!serial->disconnected)
729 sierra_send_setup(port);
730 mutex_unlock(&serial->disc_mutex);
732 /* Stop reading urbs */
733 sierra_stop_rx_urbs(port);
734 /* .. and release them */
735 for (i = 0; i < N_IN_URB; i++) {
736 sierra_release_urb(portdata->in_urbs[i]);
737 portdata->in_urbs[i] = NULL;
742 static int sierra_open(struct tty_struct *tty,
743 struct usb_serial_port *port, struct file *filp)
745 struct sierra_port_private *portdata;
746 struct usb_serial *serial = port->serial;
747 int i;
748 int err;
749 int endpoint;
750 struct urb *urb;
752 portdata = usb_get_serial_port_data(port);
754 dev_dbg(&port->dev, "%s\n", __func__);
756 /* Set some sane defaults */
757 portdata->rts_state = 1;
758 portdata->dtr_state = 1;
761 endpoint = port->bulk_in_endpointAddress;
762 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
763 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
764 IN_BUFLEN, GFP_KERNEL,
765 sierra_indat_callback);
766 portdata->in_urbs[i] = urb;
768 /* clear halt condition */
769 usb_clear_halt(serial->dev,
770 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
772 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
773 if (err) {
774 /* get rid of everything as in close */
775 sierra_close(port);
776 return err;
778 sierra_send_setup(port);
780 return 0;
784 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
786 struct usb_serial *serial = port->serial;
787 struct sierra_port_private *portdata;
789 portdata = usb_get_serial_port_data(port);
790 portdata->rts_state = on;
791 portdata->dtr_state = on;
793 if (serial->dev) {
794 mutex_lock(&serial->disc_mutex);
795 if (!serial->disconnected)
796 sierra_send_setup(port);
797 mutex_unlock(&serial->disc_mutex);
801 static int sierra_startup(struct usb_serial *serial)
803 struct usb_serial_port *port;
804 struct sierra_port_private *portdata;
805 int i;
807 dev_dbg(&serial->dev->dev, "%s\n", __func__);
809 /* Set Device mode to D0 */
810 sierra_set_power_state(serial->dev, 0x0000);
812 /* Check NMEA and set */
813 if (nmea)
814 sierra_vsc_set_nmea(serial->dev, 1);
816 /* Now setup per port private data */
817 for (i = 0; i < serial->num_ports; i++) {
818 port = serial->port[i];
819 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
820 if (!portdata) {
821 dev_dbg(&port->dev, "%s: kmalloc for "
822 "sierra_port_private (%d) failed!.\n",
823 __func__, i);
824 return -ENOMEM;
826 spin_lock_init(&portdata->lock);
827 /* Set the port private data pointer */
828 usb_set_serial_port_data(port, portdata);
831 return 0;
834 static void sierra_release(struct usb_serial *serial)
836 int i;
837 struct usb_serial_port *port;
838 struct sierra_port_private *portdata;
840 dev_dbg(&serial->dev->dev, "%s\n", __func__);
842 for (i = 0; i < serial->num_ports; ++i) {
843 port = serial->port[i];
844 if (!port)
845 continue;
846 portdata = usb_get_serial_port_data(port);
847 if (!portdata)
848 continue;
849 kfree(portdata);
853 static struct usb_serial_driver sierra_device = {
854 .driver = {
855 .owner = THIS_MODULE,
856 .name = "sierra",
858 .description = "Sierra USB modem",
859 .id_table = id_table,
860 .usb_driver = &sierra_driver,
861 .calc_num_ports = sierra_calc_num_ports,
862 .probe = sierra_probe,
863 .open = sierra_open,
864 .close = sierra_close,
865 .dtr_rts = sierra_dtr_rts,
866 .write = sierra_write,
867 .write_room = sierra_write_room,
868 .set_termios = sierra_set_termios,
869 .tiocmget = sierra_tiocmget,
870 .tiocmset = sierra_tiocmset,
871 .attach = sierra_startup,
872 .release = sierra_release,
873 .read_int_callback = sierra_instat_callback,
876 /* Functions used by new usb-serial code. */
877 static int __init sierra_init(void)
879 int retval;
880 retval = usb_serial_register(&sierra_device);
881 if (retval)
882 goto failed_device_register;
885 retval = usb_register(&sierra_driver);
886 if (retval)
887 goto failed_driver_register;
889 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
890 DRIVER_DESC "\n");
892 return 0;
894 failed_driver_register:
895 usb_serial_deregister(&sierra_device);
896 failed_device_register:
897 return retval;
900 static void __exit sierra_exit(void)
902 usb_deregister(&sierra_driver);
903 usb_serial_deregister(&sierra_device);
906 module_init(sierra_init);
907 module_exit(sierra_exit);
909 MODULE_AUTHOR(DRIVER_AUTHOR);
910 MODULE_DESCRIPTION(DRIVER_DESC);
911 MODULE_VERSION(DRIVER_VERSION);
912 MODULE_LICENSE("GPL");
914 module_param(nmea, bool, S_IRUGO | S_IWUSR);
915 MODULE_PARM_DESC(nmea, "NMEA streaming");
917 module_param(debug, bool, S_IRUGO | S_IWUSR);
918 MODULE_PARM_DESC(debug, "Debug messages");