ARC: [plat-eznps] spinlock aware for MTM
[linux/fpc-iii.git] / drivers / usb / serial / sierra.c
blob4c4ac4705ac03f8d850a653fe91e35b78bfe25b6
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>
19 /* Uncomment to log function calls */
20 /* #define DEBUG */
22 #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
23 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
25 #include <linux/kernel.h>
26 #include <linux/jiffies.h>
27 #include <linux/errno.h>
28 #include <linux/tty.h>
29 #include <linux/slab.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/usb.h>
33 #include <linux/usb/serial.h>
35 #define SWIMS_USB_REQUEST_SetPower 0x00
36 #define SWIMS_USB_REQUEST_SetNmea 0x07
38 #define N_IN_URB_HM 8
39 #define N_OUT_URB_HM 64
40 #define N_IN_URB 4
41 #define N_OUT_URB 4
42 #define IN_BUFLEN 4096
44 #define MAX_TRANSFER (PAGE_SIZE - 512)
45 /* MAX_TRANSFER is chosen so that the VM is not stressed by
46 allocations > PAGE_SIZE and the number of packets in a page
47 is an integer 512 is the largest possible packet on EHCI */
49 static bool nmea;
51 /* Used in interface blacklisting */
52 struct sierra_iface_info {
53 const u32 infolen; /* number of interface numbers on blacklist */
54 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
57 struct sierra_intf_private {
58 spinlock_t susp_lock;
59 unsigned int suspended:1;
60 int in_flight;
61 unsigned int open_ports;
64 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
66 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
67 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
68 USB_TYPE_VENDOR, /* __u8 request type */
69 swiState, /* __u16 value */
70 0, /* __u16 index */
71 NULL, /* void *data */
72 0, /* __u16 size */
73 USB_CTRL_SET_TIMEOUT); /* int timeout */
76 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
78 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
79 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
80 USB_TYPE_VENDOR, /* __u8 request type */
81 enable, /* __u16 value */
82 0x0000, /* __u16 index */
83 NULL, /* void *data */
84 0, /* __u16 size */
85 USB_CTRL_SET_TIMEOUT); /* int timeout */
88 static int sierra_calc_num_ports(struct usb_serial *serial,
89 struct usb_serial_endpoints *epds)
91 int num_ports = 0;
92 u8 ifnum, numendpoints;
94 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
95 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
97 /* Dummy interface present on some SKUs should be ignored */
98 if (ifnum == 0x99)
99 num_ports = 0;
100 else if (numendpoints <= 3)
101 num_ports = 1;
102 else
103 num_ports = (numendpoints-1)/2;
104 return num_ports;
107 static int is_blacklisted(const u8 ifnum,
108 const struct sierra_iface_info *blacklist)
110 const u8 *info;
111 int i;
113 if (blacklist) {
114 info = blacklist->ifaceinfo;
116 for (i = 0; i < blacklist->infolen; i++) {
117 if (info[i] == ifnum)
118 return 1;
121 return 0;
124 static int is_himemory(const u8 ifnum,
125 const struct sierra_iface_info *himemorylist)
127 const u8 *info;
128 int i;
130 if (himemorylist) {
131 info = himemorylist->ifaceinfo;
133 for (i=0; i < himemorylist->infolen; i++) {
134 if (info[i] == ifnum)
135 return 1;
138 return 0;
141 static u8 sierra_interface_num(struct usb_serial *serial)
143 return serial->interface->cur_altsetting->desc.bInterfaceNumber;
146 static int sierra_probe(struct usb_serial *serial,
147 const struct usb_device_id *id)
149 int result = 0;
150 struct usb_device *udev;
151 u8 ifnum;
153 udev = serial->dev;
154 ifnum = sierra_interface_num(serial);
157 * If this interface supports more than 1 alternate
158 * select the 2nd one
160 if (serial->interface->num_altsetting == 2) {
161 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
162 ifnum);
163 /* We know the alternate setting is 1 for the MC8785 */
164 usb_set_interface(udev, ifnum, 1);
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 /* interfaces with higher memory requirements */
178 static const u8 hi_memory_typeA_ifaces[] = { 0, 2 };
179 static const struct sierra_iface_info typeA_interface_list = {
180 .infolen = ARRAY_SIZE(hi_memory_typeA_ifaces),
181 .ifaceinfo = hi_memory_typeA_ifaces,
184 static const u8 hi_memory_typeB_ifaces[] = { 3, 4, 5, 6 };
185 static const struct sierra_iface_info typeB_interface_list = {
186 .infolen = ARRAY_SIZE(hi_memory_typeB_ifaces),
187 .ifaceinfo = hi_memory_typeB_ifaces,
190 /* 'blacklist' of interfaces not served by this driver */
191 static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11, 19, 20 };
192 static const struct sierra_iface_info direct_ip_interface_blacklist = {
193 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
194 .ifaceinfo = direct_ip_non_serial_ifaces,
197 static const struct usb_device_id id_table[] = {
198 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
199 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
200 { USB_DEVICE(0x03F0, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
201 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
203 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
204 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
205 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
206 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
207 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
208 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
209 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
210 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
211 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
212 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
213 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
214 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
215 { USB_DEVICE(0x1199, 0x0301) }, /* Sierra Wireless USB Dongle 250U */
216 /* Sierra Wireless C597 */
217 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
218 /* Sierra Wireless T598 */
219 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
220 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
221 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
222 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
223 { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
225 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
226 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
227 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
228 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
229 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
230 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
231 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
232 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
233 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
234 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
235 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
236 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
237 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
238 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
239 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
240 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
241 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
242 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
243 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
244 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
245 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
246 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
247 { USB_DEVICE(0x1199, 0x683C) },
248 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
249 /* Sierra Wireless MC8790, MC8791, MC8792 */
250 { USB_DEVICE(0x1199, 0x683E) },
251 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
252 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
253 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
254 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
255 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
256 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
257 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
258 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
259 /* Sierra Wireless C885 */
260 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
261 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
262 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
263 /* Sierra Wireless C22/C33 */
264 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
265 /* Sierra Wireless HSPA Non-Composite Device */
266 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
267 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
268 /* Sierra Wireless Direct IP modems */
269 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
270 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
272 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
273 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
275 { USB_DEVICE(0x1199, 0x68AB) }, /* Sierra Wireless AR8550 */
276 /* AT&T Direct IP LTE modems */
277 { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
278 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
280 /* Airprime/Sierra Wireless Direct IP modems */
281 { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
282 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
287 MODULE_DEVICE_TABLE(usb, id_table);
290 struct sierra_port_private {
291 spinlock_t lock; /* lock the structure */
292 int outstanding_urbs; /* number of out urbs in flight */
293 struct usb_anchor active;
294 struct usb_anchor delayed;
296 int num_out_urbs;
297 int num_in_urbs;
298 /* Input endpoints and buffers for this port */
299 struct urb *in_urbs[N_IN_URB_HM];
301 /* Settings for the port */
302 int rts_state; /* Handshaking pins (outputs) */
303 int dtr_state;
304 int cts_state; /* Handshaking pins (inputs) */
305 int dsr_state;
306 int dcd_state;
307 int ri_state;
310 static int sierra_send_setup(struct usb_serial_port *port)
312 struct usb_serial *serial = port->serial;
313 struct sierra_port_private *portdata;
314 __u16 interface = 0;
315 int val = 0;
316 int do_send = 0;
317 int retval;
319 portdata = usb_get_serial_port_data(port);
321 if (portdata->dtr_state)
322 val |= 0x01;
323 if (portdata->rts_state)
324 val |= 0x02;
326 /* If composite device then properly report interface */
327 if (serial->num_ports == 1) {
328 interface = sierra_interface_num(serial);
329 /* Control message is sent only to interfaces with
330 * interrupt_in endpoints
332 if (port->interrupt_in_urb) {
333 /* send control message */
334 do_send = 1;
338 /* Otherwise the need to do non-composite mapping */
339 else {
340 if (port->bulk_out_endpointAddress == 2)
341 interface = 0;
342 else if (port->bulk_out_endpointAddress == 4)
343 interface = 1;
344 else if (port->bulk_out_endpointAddress == 5)
345 interface = 2;
347 do_send = 1;
349 if (!do_send)
350 return 0;
352 retval = usb_autopm_get_interface(serial->interface);
353 if (retval < 0)
354 return retval;
356 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
357 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
358 usb_autopm_put_interface(serial->interface);
360 return retval;
363 static int sierra_tiocmget(struct tty_struct *tty)
365 struct usb_serial_port *port = tty->driver_data;
366 unsigned int value;
367 struct sierra_port_private *portdata;
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);
378 return value;
381 static int sierra_tiocmset(struct tty_struct *tty,
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);
389 if (set & TIOCM_RTS)
390 portdata->rts_state = 1;
391 if (set & TIOCM_DTR)
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 if (urb) {
404 kfree(urb->transfer_buffer);
405 usb_free_urb(urb);
409 static void sierra_outdat_callback(struct urb *urb)
411 struct usb_serial_port *port = urb->context;
412 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
413 struct sierra_intf_private *intfdata;
414 int status = urb->status;
416 intfdata = usb_get_serial_data(port->serial);
418 /* free up the transfer buffer, as usb_free_urb() does not do this */
419 kfree(urb->transfer_buffer);
420 usb_autopm_put_interface_async(port->serial->interface);
421 if (status)
422 dev_dbg(&port->dev, "%s - nonzero write bulk status "
423 "received: %d\n", __func__, status);
425 spin_lock(&portdata->lock);
426 --portdata->outstanding_urbs;
427 spin_unlock(&portdata->lock);
428 spin_lock(&intfdata->susp_lock);
429 --intfdata->in_flight;
430 spin_unlock(&intfdata->susp_lock);
432 usb_serial_port_softint(port);
435 /* Write */
436 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
437 const unsigned char *buf, int count)
439 struct sierra_port_private *portdata;
440 struct sierra_intf_private *intfdata;
441 struct usb_serial *serial = port->serial;
442 unsigned long flags;
443 unsigned char *buffer;
444 struct urb *urb;
445 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
446 int retval = 0;
448 /* verify that we actually have some data to write */
449 if (count == 0)
450 return 0;
452 portdata = usb_get_serial_port_data(port);
453 intfdata = usb_get_serial_data(serial);
455 dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
456 spin_lock_irqsave(&portdata->lock, flags);
457 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
458 portdata->outstanding_urbs);
459 if (portdata->outstanding_urbs > portdata->num_out_urbs) {
460 spin_unlock_irqrestore(&portdata->lock, flags);
461 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
462 return 0;
464 portdata->outstanding_urbs++;
465 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
466 portdata->outstanding_urbs);
467 spin_unlock_irqrestore(&portdata->lock, flags);
469 retval = usb_autopm_get_interface_async(serial->interface);
470 if (retval < 0) {
471 spin_lock_irqsave(&portdata->lock, flags);
472 portdata->outstanding_urbs--;
473 spin_unlock_irqrestore(&portdata->lock, flags);
474 goto error_simple;
477 buffer = kmalloc(writesize, GFP_ATOMIC);
478 if (!buffer) {
479 retval = -ENOMEM;
480 goto error_no_buffer;
483 urb = usb_alloc_urb(0, GFP_ATOMIC);
484 if (!urb) {
485 retval = -ENOMEM;
486 goto error_no_urb;
489 memcpy(buffer, buf, writesize);
491 usb_serial_debug_data(&port->dev, __func__, writesize, buffer);
493 usb_fill_bulk_urb(urb, serial->dev,
494 usb_sndbulkpipe(serial->dev,
495 port->bulk_out_endpointAddress),
496 buffer, writesize, sierra_outdat_callback, port);
498 /* Handle the need to send a zero length packet */
499 urb->transfer_flags |= URB_ZERO_PACKET;
501 spin_lock_irqsave(&intfdata->susp_lock, flags);
503 if (intfdata->suspended) {
504 usb_anchor_urb(urb, &portdata->delayed);
505 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
506 goto skip_power;
507 } else {
508 usb_anchor_urb(urb, &portdata->active);
510 /* send it down the pipe */
511 retval = usb_submit_urb(urb, GFP_ATOMIC);
512 if (retval) {
513 usb_unanchor_urb(urb);
514 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
515 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
516 "with status = %d\n", __func__, retval);
517 goto error;
518 } else {
519 intfdata->in_flight++;
520 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
523 skip_power:
524 /* we are done with this urb, so let the host driver
525 * really free it when it is finished with it */
526 usb_free_urb(urb);
528 return writesize;
529 error:
530 usb_free_urb(urb);
531 error_no_urb:
532 kfree(buffer);
533 error_no_buffer:
534 spin_lock_irqsave(&portdata->lock, flags);
535 --portdata->outstanding_urbs;
536 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
537 portdata->outstanding_urbs);
538 spin_unlock_irqrestore(&portdata->lock, flags);
539 usb_autopm_put_interface_async(serial->interface);
540 error_simple:
541 return retval;
544 static void sierra_indat_callback(struct urb *urb)
546 int err;
547 int endpoint;
548 struct usb_serial_port *port;
549 unsigned char *data = urb->transfer_buffer;
550 int status = urb->status;
552 endpoint = usb_pipeendpoint(urb->pipe);
553 port = urb->context;
555 if (status) {
556 dev_dbg(&port->dev, "%s: nonzero status: %d on"
557 " endpoint %02x\n", __func__, status, endpoint);
558 } else {
559 if (urb->actual_length) {
560 tty_insert_flip_string(&port->port, data,
561 urb->actual_length);
562 tty_flip_buffer_push(&port->port);
564 usb_serial_debug_data(&port->dev, __func__,
565 urb->actual_length, data);
566 } else {
567 dev_dbg(&port->dev, "%s: empty read urb"
568 " received\n", __func__);
572 /* Resubmit urb so we continue receiving */
573 if (status != -ESHUTDOWN && status != -EPERM) {
574 usb_mark_last_busy(port->serial->dev);
575 err = usb_submit_urb(urb, GFP_ATOMIC);
576 if (err && err != -EPERM)
577 dev_err(&port->dev, "resubmit read urb failed."
578 "(%d)\n", err);
582 static void sierra_instat_callback(struct urb *urb)
584 int err;
585 int status = urb->status;
586 struct usb_serial_port *port = urb->context;
587 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
588 struct usb_serial *serial = port->serial;
590 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
591 urb, port, portdata);
593 if (status == 0) {
594 struct usb_ctrlrequest *req_pkt =
595 (struct usb_ctrlrequest *)urb->transfer_buffer;
597 if (!req_pkt) {
598 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
599 __func__);
600 return;
602 if ((req_pkt->bRequestType == 0xA1) &&
603 (req_pkt->bRequest == 0x20)) {
604 int old_dcd_state;
605 unsigned char signals = *((unsigned char *)
606 urb->transfer_buffer +
607 sizeof(struct usb_ctrlrequest));
609 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
610 signals);
612 old_dcd_state = portdata->dcd_state;
613 portdata->cts_state = 1;
614 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
615 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
616 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
618 if (old_dcd_state && !portdata->dcd_state)
619 tty_port_tty_hangup(&port->port, true);
620 } else {
621 dev_dbg(&port->dev, "%s: type %x req %x\n",
622 __func__, req_pkt->bRequestType,
623 req_pkt->bRequest);
625 } else
626 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
628 /* Resubmit urb so we continue receiving IRQ data */
629 if (status != -ESHUTDOWN && status != -ENOENT) {
630 usb_mark_last_busy(serial->dev);
631 err = usb_submit_urb(urb, GFP_ATOMIC);
632 if (err && err != -EPERM)
633 dev_err(&port->dev, "%s: resubmit intr urb "
634 "failed. (%d)\n", __func__, err);
638 static int sierra_write_room(struct tty_struct *tty)
640 struct usb_serial_port *port = tty->driver_data;
641 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
642 unsigned long flags;
644 /* try to give a good number back based on if we have any free urbs at
645 * this point in time */
646 spin_lock_irqsave(&portdata->lock, flags);
647 if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
648 spin_unlock_irqrestore(&portdata->lock, flags);
649 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
650 return 0;
652 spin_unlock_irqrestore(&portdata->lock, flags);
654 return 2048;
657 static int sierra_chars_in_buffer(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);
661 unsigned long flags;
662 int chars;
664 /* NOTE: This overcounts somewhat. */
665 spin_lock_irqsave(&portdata->lock, flags);
666 chars = portdata->outstanding_urbs * MAX_TRANSFER;
667 spin_unlock_irqrestore(&portdata->lock, flags);
669 dev_dbg(&port->dev, "%s - %d\n", __func__, chars);
671 return chars;
674 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
676 int i;
677 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
679 for (i = 0; i < portdata->num_in_urbs; i++)
680 usb_kill_urb(portdata->in_urbs[i]);
682 usb_kill_urb(port->interrupt_in_urb);
685 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
687 int ok_cnt;
688 int err = -EINVAL;
689 int i;
690 struct urb *urb;
691 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
693 ok_cnt = 0;
694 for (i = 0; i < portdata->num_in_urbs; i++) {
695 urb = portdata->in_urbs[i];
696 if (!urb)
697 continue;
698 err = usb_submit_urb(urb, mem_flags);
699 if (err) {
700 dev_err(&port->dev, "%s: submit urb failed: %d\n",
701 __func__, err);
702 } else {
703 ok_cnt++;
707 if (ok_cnt && port->interrupt_in_urb) {
708 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
709 if (err) {
710 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
711 __func__, err);
715 if (ok_cnt > 0) /* at least one rx urb submitted */
716 return 0;
717 else
718 return err;
721 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
722 int dir, void *ctx, int len,
723 gfp_t mem_flags,
724 usb_complete_t callback)
726 struct urb *urb;
727 u8 *buf;
729 urb = usb_alloc_urb(0, mem_flags);
730 if (!urb)
731 return NULL;
733 buf = kmalloc(len, mem_flags);
734 if (buf) {
735 /* Fill URB using supplied data */
736 usb_fill_bulk_urb(urb, serial->dev,
737 usb_sndbulkpipe(serial->dev, endpoint) | dir,
738 buf, len, callback, ctx);
740 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
741 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
742 } else {
743 sierra_release_urb(urb);
744 urb = NULL;
747 return urb;
750 static void sierra_close(struct usb_serial_port *port)
752 int i;
753 struct usb_serial *serial = port->serial;
754 struct sierra_port_private *portdata;
755 struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
756 struct urb *urb;
758 portdata = usb_get_serial_port_data(port);
761 * Need to take susp_lock to make sure port is not already being
762 * resumed, but no need to hold it due to initialized
764 spin_lock_irq(&intfdata->susp_lock);
765 if (--intfdata->open_ports == 0)
766 serial->interface->needs_remote_wakeup = 0;
767 spin_unlock_irq(&intfdata->susp_lock);
769 for (;;) {
770 urb = usb_get_from_anchor(&portdata->delayed);
771 if (!urb)
772 break;
773 kfree(urb->transfer_buffer);
774 usb_free_urb(urb);
775 usb_autopm_put_interface_async(serial->interface);
776 spin_lock(&portdata->lock);
777 portdata->outstanding_urbs--;
778 spin_unlock(&portdata->lock);
781 sierra_stop_rx_urbs(port);
782 usb_kill_anchored_urbs(&portdata->active);
784 for (i = 0; i < portdata->num_in_urbs; i++) {
785 sierra_release_urb(portdata->in_urbs[i]);
786 portdata->in_urbs[i] = NULL;
789 usb_autopm_get_interface_no_resume(serial->interface);
792 static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
794 struct sierra_port_private *portdata;
795 struct usb_serial *serial = port->serial;
796 struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
797 int i;
798 int err;
799 int endpoint;
800 struct urb *urb;
802 portdata = usb_get_serial_port_data(port);
804 endpoint = port->bulk_in_endpointAddress;
805 for (i = 0; i < portdata->num_in_urbs; i++) {
806 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
807 IN_BUFLEN, GFP_KERNEL,
808 sierra_indat_callback);
809 portdata->in_urbs[i] = urb;
811 /* clear halt condition */
812 usb_clear_halt(serial->dev,
813 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
815 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
816 if (err)
817 goto err_submit;
819 spin_lock_irq(&intfdata->susp_lock);
820 if (++intfdata->open_ports == 1)
821 serial->interface->needs_remote_wakeup = 1;
822 spin_unlock_irq(&intfdata->susp_lock);
823 usb_autopm_put_interface(serial->interface);
825 return 0;
827 err_submit:
828 sierra_stop_rx_urbs(port);
830 for (i = 0; i < portdata->num_in_urbs; i++) {
831 sierra_release_urb(portdata->in_urbs[i]);
832 portdata->in_urbs[i] = NULL;
835 return err;
839 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
841 struct sierra_port_private *portdata;
843 portdata = usb_get_serial_port_data(port);
844 portdata->rts_state = on;
845 portdata->dtr_state = on;
847 sierra_send_setup(port);
850 static int sierra_startup(struct usb_serial *serial)
852 struct sierra_intf_private *intfdata;
854 intfdata = kzalloc(sizeof(*intfdata), GFP_KERNEL);
855 if (!intfdata)
856 return -ENOMEM;
858 spin_lock_init(&intfdata->susp_lock);
860 usb_set_serial_data(serial, intfdata);
862 /* Set Device mode to D0 */
863 sierra_set_power_state(serial->dev, 0x0000);
865 /* Check NMEA and set */
866 if (nmea)
867 sierra_vsc_set_nmea(serial->dev, 1);
869 return 0;
872 static void sierra_release(struct usb_serial *serial)
874 struct sierra_intf_private *intfdata;
876 intfdata = usb_get_serial_data(serial);
877 kfree(intfdata);
880 static int sierra_port_probe(struct usb_serial_port *port)
882 struct usb_serial *serial = port->serial;
883 struct sierra_port_private *portdata;
884 const struct sierra_iface_info *himemoryp;
885 u8 ifnum;
887 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
888 if (!portdata)
889 return -ENOMEM;
891 spin_lock_init(&portdata->lock);
892 init_usb_anchor(&portdata->active);
893 init_usb_anchor(&portdata->delayed);
895 /* Assume low memory requirements */
896 portdata->num_out_urbs = N_OUT_URB;
897 portdata->num_in_urbs = N_IN_URB;
899 /* Determine actual memory requirements */
900 if (serial->num_ports == 1) {
901 /* Get interface number for composite device */
902 ifnum = sierra_interface_num(serial);
903 himemoryp = &typeB_interface_list;
904 } else {
905 /* This is really the usb-serial port number of the interface
906 * rather than the interface number.
908 ifnum = port->port_number;
909 himemoryp = &typeA_interface_list;
912 if (is_himemory(ifnum, himemoryp)) {
913 portdata->num_out_urbs = N_OUT_URB_HM;
914 portdata->num_in_urbs = N_IN_URB_HM;
917 dev_dbg(&port->dev,
918 "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
919 ifnum, portdata->num_in_urbs, portdata->num_out_urbs);
921 usb_set_serial_port_data(port, portdata);
923 return 0;
926 static int sierra_port_remove(struct usb_serial_port *port)
928 struct sierra_port_private *portdata;
930 portdata = usb_get_serial_port_data(port);
931 usb_set_serial_port_data(port, NULL);
932 kfree(portdata);
934 return 0;
937 #ifdef CONFIG_PM
938 static void stop_read_write_urbs(struct usb_serial *serial)
940 int i;
941 struct usb_serial_port *port;
942 struct sierra_port_private *portdata;
944 /* Stop reading/writing urbs */
945 for (i = 0; i < serial->num_ports; ++i) {
946 port = serial->port[i];
947 portdata = usb_get_serial_port_data(port);
948 if (!portdata)
949 continue;
950 sierra_stop_rx_urbs(port);
951 usb_kill_anchored_urbs(&portdata->active);
955 static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
957 struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
959 spin_lock_irq(&intfdata->susp_lock);
960 if (PMSG_IS_AUTO(message)) {
961 if (intfdata->in_flight) {
962 spin_unlock_irq(&intfdata->susp_lock);
963 return -EBUSY;
966 intfdata->suspended = 1;
967 spin_unlock_irq(&intfdata->susp_lock);
969 stop_read_write_urbs(serial);
971 return 0;
974 /* Caller must hold susp_lock. */
975 static int sierra_submit_delayed_urbs(struct usb_serial_port *port)
977 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
978 struct sierra_intf_private *intfdata;
979 struct urb *urb;
980 int ec = 0;
981 int err;
983 intfdata = usb_get_serial_data(port->serial);
985 for (;;) {
986 urb = usb_get_from_anchor(&portdata->delayed);
987 if (!urb)
988 break;
990 usb_anchor_urb(urb, &portdata->active);
991 intfdata->in_flight++;
992 err = usb_submit_urb(urb, GFP_ATOMIC);
993 if (err) {
994 dev_err(&port->dev, "%s - submit urb failed: %d",
995 __func__, err);
996 ec++;
997 intfdata->in_flight--;
998 usb_unanchor_urb(urb);
999 kfree(urb->transfer_buffer);
1000 usb_free_urb(urb);
1002 spin_lock(&portdata->lock);
1003 portdata->outstanding_urbs--;
1004 spin_unlock(&portdata->lock);
1008 if (ec)
1009 return -EIO;
1011 return 0;
1014 static int sierra_resume(struct usb_serial *serial)
1016 struct usb_serial_port *port;
1017 struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
1018 int ec = 0;
1019 int i, err;
1021 spin_lock_irq(&intfdata->susp_lock);
1022 for (i = 0; i < serial->num_ports; i++) {
1023 port = serial->port[i];
1025 if (!tty_port_initialized(&port->port))
1026 continue;
1028 err = sierra_submit_delayed_urbs(port);
1029 if (err)
1030 ec++;
1032 err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
1033 if (err)
1034 ec++;
1036 intfdata->suspended = 0;
1037 spin_unlock_irq(&intfdata->susp_lock);
1039 return ec ? -EIO : 0;
1042 #else
1043 #define sierra_suspend NULL
1044 #define sierra_resume NULL
1045 #endif
1047 static struct usb_serial_driver sierra_device = {
1048 .driver = {
1049 .owner = THIS_MODULE,
1050 .name = "sierra",
1052 .description = "Sierra USB modem",
1053 .id_table = id_table,
1054 .calc_num_ports = sierra_calc_num_ports,
1055 .probe = sierra_probe,
1056 .open = sierra_open,
1057 .close = sierra_close,
1058 .dtr_rts = sierra_dtr_rts,
1059 .write = sierra_write,
1060 .write_room = sierra_write_room,
1061 .chars_in_buffer = sierra_chars_in_buffer,
1062 .tiocmget = sierra_tiocmget,
1063 .tiocmset = sierra_tiocmset,
1064 .attach = sierra_startup,
1065 .release = sierra_release,
1066 .port_probe = sierra_port_probe,
1067 .port_remove = sierra_port_remove,
1068 .suspend = sierra_suspend,
1069 .resume = sierra_resume,
1070 .read_int_callback = sierra_instat_callback,
1073 static struct usb_serial_driver * const serial_drivers[] = {
1074 &sierra_device, NULL
1077 module_usb_serial_driver(serial_drivers, id_table);
1079 MODULE_AUTHOR(DRIVER_AUTHOR);
1080 MODULE_DESCRIPTION(DRIVER_DESC);
1081 MODULE_LICENSE("GPL");
1083 module_param(nmea, bool, S_IRUGO | S_IWUSR);
1084 MODULE_PARM_DESC(nmea, "NMEA streaming");