USB: serial: sierra driver zero length packet fix
[linux-2.6-block.git] / drivers / usb / serial / sierra.c
CommitLineData
69de51fd 1/*
033a3fb9
KL
2 USB Driver for Sierra Wireless
3
40d2ff32
EP
4 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>,
5
6 Copyright (C) 2008, 2009 Elina Pasheva, Matthew Safar, Rory Filer
7 <linux@sierrawireless.com>
033a3fb9
KL
8
9 IMPORTANT DISCLAIMER: This driver is not commercially supported by
10 Sierra Wireless. Use at your own risk.
11
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.
15
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>
033a3fb9
KL
18*/
19
238ebd13 20#define DRIVER_VERSION "v.1.3.7"
40d2ff32 21#define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
033a3fb9 22#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
69de51fd
KL
23
24#include <linux/kernel.h>
033a3fb9
KL
25#include <linux/jiffies.h>
26#include <linux/errno.h>
69de51fd 27#include <linux/tty.h>
033a3fb9 28#include <linux/tty_flip.h>
69de51fd
KL
29#include <linux/module.h>
30#include <linux/usb.h>
a969888c 31#include <linux/usb/serial.h>
69de51fd 32
228426ed
KL
33#define SWIMS_USB_REQUEST_SetPower 0x00
34#define SWIMS_USB_REQUEST_SetNmea 0x07
112225b1 35
b748bb71
EP
36#define N_IN_URB 8
37#define N_OUT_URB 64
112225b1
KL
38#define IN_BUFLEN 4096
39
40d2ff32
EP
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 */
44
112225b1 45static int debug;
228426ed 46static int nmea;
112225b1 47
4db2299d
EP
48/* Used in interface blacklisting */
49struct sierra_iface_info {
50 const u32 infolen; /* number of interface numbers on blacklist */
51 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
52};
53
82a24e68 54static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
112225b1
KL
55{
56 int result;
5d44b361 57 dev_dbg(&udev->dev, "%s\n", __func__);
112225b1 58 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
228426ed 59 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
f3564de4 60 USB_TYPE_VENDOR, /* __u8 request type */
228426ed
KL
61 swiState, /* __u16 value */
62 0, /* __u16 index */
63 NULL, /* void *data */
64 0, /* __u16 size */
65 USB_CTRL_SET_TIMEOUT); /* int timeout */
112225b1
KL
66 return result;
67}
68
228426ed 69static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
112225b1
KL
70{
71 int result;
5d44b361 72 dev_dbg(&udev->dev, "%s\n", __func__);
228426ed
KL
73 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
74 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
f3564de4 75 USB_TYPE_VENDOR, /* __u8 request type */
228426ed
KL
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;
82}
83
84static int sierra_calc_num_ports(struct usb_serial *serial)
85{
86 int result;
87 int *num_ports = usb_get_serial_data(serial);
5d44b361 88 dev_dbg(&serial->dev->dev, "%s\n", __func__);
228426ed
KL
89
90 result = *num_ports;
91
92 if (result) {
93 kfree(num_ports);
94 usb_set_serial_data(serial, NULL);
95 }
96
97 return result;
98}
99
4db2299d
EP
100static int is_blacklisted(const u8 ifnum,
101 const struct sierra_iface_info *blacklist)
102{
103 const u8 *info;
104 int i;
105
106 if (blacklist) {
107 info = blacklist->ifaceinfo;
108
109 for (i = 0; i < blacklist->infolen; i++) {
110 if (info[i] == ifnum)
111 return 1;
112 }
113 }
114 return 0;
115}
116
7106967e
KL
117static int sierra_calc_interface(struct usb_serial *serial)
118{
4e0fee82
KL
119 int interface;
120 struct usb_interface *p_interface;
121 struct usb_host_interface *p_host_interface;
5d44b361 122 dev_dbg(&serial->dev->dev, "%s\n", __func__);
7106967e 123
4e0fee82
KL
124 /* Get the interface structure pointer from the serial struct */
125 p_interface = serial->interface;
7106967e 126
4e0fee82
KL
127 /* Get a pointer to the host interface structure */
128 p_host_interface = p_interface->cur_altsetting;
7106967e 129
4e0fee82
KL
130 /* read the interface descriptor for this active altsetting
131 * to find out the interface number we are on
132 */
133 interface = p_host_interface->desc.bInterfaceNumber;
7106967e 134
4e0fee82 135 return interface;
7106967e
KL
136}
137
228426ed
KL
138static int sierra_probe(struct usb_serial *serial,
139 const struct usb_device_id *id)
140{
141 int result = 0;
112225b1 142 struct usb_device *udev;
228426ed
KL
143 int *num_ports;
144 u8 ifnum;
e3173b22
KL
145 u8 numendpoints;
146
5d44b361 147 dev_dbg(&serial->dev->dev, "%s\n", __func__);
112225b1 148
228426ed
KL
149 num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
150 if (!num_ports)
151 return -ENOMEM;
152
153 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
e3173b22 154 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
228426ed 155 udev = serial->dev;
112225b1 156
e3173b22
KL
157 /* Figure out the interface number from the serial structure */
158 ifnum = sierra_calc_interface(serial);
7106967e 159
e3173b22
KL
160 /*
161 * If this interface supports more than 1 alternate
162 * select the 2nd one
163 */
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);
169 }
7106967e 170
e3173b22 171 /* Dummy interface present on some SKUs should be ignored */
0585e4df 172 if (ifnum == 0x99)
228426ed 173 *num_ports = 0;
e3173b22
KL
174 else if (numendpoints <= 3)
175 *num_ports = 1;
228426ed 176 else
e3173b22
KL
177 *num_ports = (numendpoints-1)/2;
178
228426ed
KL
179 /*
180 * save off our num_ports info so that we can use it in the
181 * calc_num_ports callback
182 */
183 usb_set_serial_data(serial, (void *)num_ports);
112225b1 184
4db2299d
EP
185 /* ifnum could have changed - by calling usb_set_interface */
186 ifnum = sierra_calc_interface(serial);
187
188 if (is_blacklisted(ifnum,
189 (struct sierra_iface_info *)id->driver_info)) {
190 dev_dbg(&serial->dev->dev,
191 "Ignoring blacklisted interface #%d\n", ifnum);
192 return -ENODEV;
193 }
194
228426ed 195 return result;
112225b1 196}
033a3fb9 197
4db2299d
EP
198static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
199static const struct sierra_iface_info direct_ip_interface_blacklist = {
200 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
201 .ifaceinfo = direct_ip_non_serial_ifaces,
202};
203
69de51fd 204static struct usb_device_id id_table [] = {
e43062dd 205 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
69de51fd 206 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
e43062dd 207 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
f8834f1f 208 { USB_DEVICE(0x03f0, 0x1b1d) }, /* HP ev2200 a.k.a MC5720 */
69de51fd 209 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
4e0fee82 210 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
b9e13ac3 211 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
69de51fd 212 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
e43062dd 213 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
9454c46a 214 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
4e0fee82
KL
215 /* Sierra Wireless C597 */
216 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
e3173b22
KL
217 /* Sierra Wireless Device */
218 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
219 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless Device */
73b2c205
KL
220 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless Device */
221 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless Device */
9454c46a 222
69de51fd 223 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
e43062dd 224 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
69de51fd 225 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
9454c46a 226 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
4e0fee82 227 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Lenovo) */
7f170a63 228 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
8f7f85e9 229 { USB_DEVICE(0x03f0, 0x1e1d) }, /* HP hs2300 a.k.a MC8775 */
69de51fd 230 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
7106967e 231 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
4e0fee82
KL
232 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
233 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
b77a5c70 234 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
e3173b22 235 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
4db2299d
EP
236 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
237 { USB_DEVICE(0x1199, 0x683C) },
238 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
239 /* Sierra Wireless MC8790, MC8791, MC8792 */
240 { USB_DEVICE(0x1199, 0x683E) },
9454c46a
KL
241 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
242 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
243 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
244 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
6835b32c 245 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
62aad3ab 246 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
e3173b22
KL
247 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
248 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
249 /* Sierra Wireless C885 */
250 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
251 /* Sierra Wireless Device */
252 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
253 /* Sierra Wireless Device */
73b2c205
KL
254 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
255 /* Sierra Wireless Device */
e3173b22
KL
256 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
257
258 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
259 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
112225b1 260
4db2299d
EP
261 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
262 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
263 },
264
033a3fb9
KL
265 { }
266};
964ee1de 267MODULE_DEVICE_TABLE(usb, id_table);
033a3fb9 268
69de51fd 269static struct usb_driver sierra_driver = {
033a3fb9 270 .name = "sierra",
228426ed 271 .probe = usb_serial_probe,
033a3fb9
KL
272 .disconnect = usb_serial_disconnect,
273 .id_table = id_table,
964ee1de 274 .no_dynamic_id = 1,
033a3fb9
KL
275};
276
033a3fb9 277struct sierra_port_private {
17c23274
GKH
278 spinlock_t lock; /* lock the structure */
279 int outstanding_urbs; /* number of out urbs in flight */
280
4f4f9c53 281 /* Input endpoints and buffers for this port */
033a3fb9 282 struct urb *in_urbs[N_IN_URB];
033a3fb9
KL
283
284 /* Settings for the port */
285 int rts_state; /* Handshaking pins (outputs) */
286 int dtr_state;
287 int cts_state; /* Handshaking pins (inputs) */
288 int dsr_state;
289 int dcd_state;
290 int ri_state;
033a3fb9
KL
291};
292
335f8514 293static int sierra_send_setup(struct usb_serial_port *port)
69de51fd 294{
964ee1de
GKH
295 struct usb_serial *serial = port->serial;
296 struct sierra_port_private *portdata;
228426ed 297 __u16 interface = 0;
335f8514 298 int val = 0;
033a3fb9 299
5d44b361 300 dev_dbg(&port->dev, "%s\n", __func__);
033a3fb9 301
964ee1de 302 portdata = usb_get_serial_port_data(port);
033a3fb9 303
335f8514
AC
304 if (portdata->dtr_state)
305 val |= 0x01;
306 if (portdata->rts_state)
307 val |= 0x02;
308
309 /* If composite device then properly report interface */
00b040de 310 if (serial->num_ports == 1) {
335f8514 311 interface = sierra_calc_interface(serial);
00b040de
AC
312 /* Control message is sent only to interfaces with
313 * interrupt_in endpoints
314 */
315 if (port->interrupt_in_urb) {
316 /* send control message */
317 return usb_control_msg(serial->dev,
318 usb_rcvctrlpipe(serial->dev, 0),
319 0x22, 0x21, val, interface,
320 NULL, 0, USB_CTRL_SET_TIMEOUT);
321 }
322 }
335f8514
AC
323
324 /* Otherwise the need to do non-composite mapping */
325 else {
326 if (port->bulk_out_endpointAddress == 2)
327 interface = 0;
328 else if (port->bulk_out_endpointAddress == 4)
329 interface = 1;
330 else if (port->bulk_out_endpointAddress == 5)
331 interface = 2;
00b040de 332 return usb_control_msg(serial->dev,
335f8514
AC
333 usb_rcvctrlpipe(serial->dev, 0),
334 0x22, 0x21, val, interface,
335 NULL, 0, USB_CTRL_SET_TIMEOUT);
00b040de 336 }
964ee1de 337 return 0;
69de51fd
KL
338}
339
95da310e
AC
340static void sierra_set_termios(struct tty_struct *tty,
341 struct usb_serial_port *port, struct ktermios *old_termios)
033a3fb9 342{
5d44b361 343 dev_dbg(&port->dev, "%s\n", __func__);
95da310e 344 tty_termios_copy_hw(tty->termios, old_termios);
335f8514 345 sierra_send_setup(port);
033a3fb9
KL
346}
347
95da310e 348static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
033a3fb9 349{
95da310e 350 struct usb_serial_port *port = tty->driver_data;
033a3fb9
KL
351 unsigned int value;
352 struct sierra_port_private *portdata;
353
5d44b361 354 dev_dbg(&port->dev, "%s\n", __func__);
033a3fb9
KL
355 portdata = usb_get_serial_port_data(port);
356
357 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
358 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
359 ((portdata->cts_state) ? TIOCM_CTS : 0) |
360 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
361 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
362 ((portdata->ri_state) ? TIOCM_RNG : 0);
363
364 return value;
365}
366
95da310e 367static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
033a3fb9
KL
368 unsigned int set, unsigned int clear)
369{
95da310e 370 struct usb_serial_port *port = tty->driver_data;
033a3fb9
KL
371 struct sierra_port_private *portdata;
372
373 portdata = usb_get_serial_port_data(port);
374
375 if (set & TIOCM_RTS)
376 portdata->rts_state = 1;
377 if (set & TIOCM_DTR)
378 portdata->dtr_state = 1;
379
380 if (clear & TIOCM_RTS)
381 portdata->rts_state = 0;
382 if (clear & TIOCM_DTR)
383 portdata->dtr_state = 0;
335f8514 384 return sierra_send_setup(port);
033a3fb9
KL
385}
386
b9a44bc1
EP
387static void sierra_release_urb(struct urb *urb)
388{
389 struct usb_serial_port *port;
390 if (urb) {
391 port = urb->context;
392 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
393 kfree(urb->transfer_buffer);
394 usb_free_urb(urb);
395 }
396}
397
17c23274
GKH
398static void sierra_outdat_callback(struct urb *urb)
399{
400 struct usb_serial_port *port = urb->context;
401 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
402 int status = urb->status;
403 unsigned long flags;
404
5d44b361 405 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
17c23274
GKH
406
407 /* free up the transfer buffer, as usb_free_urb() does not do this */
408 kfree(urb->transfer_buffer);
409
410 if (status)
7384a922 411 dev_dbg(&port->dev, "%s - nonzero write bulk status "
5d44b361 412 "received: %d\n", __func__, status);
17c23274
GKH
413
414 spin_lock_irqsave(&portdata->lock, flags);
415 --portdata->outstanding_urbs;
416 spin_unlock_irqrestore(&portdata->lock, flags);
417
418 usb_serial_port_softint(port);
419}
420
033a3fb9 421/* Write */
95da310e
AC
422static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
423 const unsigned char *buf, int count)
033a3fb9 424{
17c23274
GKH
425 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
426 struct usb_serial *serial = port->serial;
427 unsigned long flags;
428 unsigned char *buffer;
429 struct urb *urb;
40d2ff32
EP
430 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
431 int retval = 0;
432
433 /* verify that we actually have some data to write */
434 if (count == 0)
435 return 0;
033a3fb9
KL
436
437 portdata = usb_get_serial_port_data(port);
438
40d2ff32 439 dev_dbg(&port->dev, "%s: write (%d bytes)\n", __func__, writesize);
033a3fb9 440
17c23274 441 spin_lock_irqsave(&portdata->lock, flags);
40d2ff32
EP
442 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
443 portdata->outstanding_urbs);
17c23274
GKH
444 if (portdata->outstanding_urbs > N_OUT_URB) {
445 spin_unlock_irqrestore(&portdata->lock, flags);
7384a922 446 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
17c23274
GKH
447 return 0;
448 }
449 portdata->outstanding_urbs++;
40d2ff32
EP
450 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
451 portdata->outstanding_urbs);
17c23274
GKH
452 spin_unlock_irqrestore(&portdata->lock, flags);
453
40d2ff32 454 buffer = kmalloc(writesize, GFP_ATOMIC);
17c23274
GKH
455 if (!buffer) {
456 dev_err(&port->dev, "out of memory\n");
40d2ff32 457 retval = -ENOMEM;
17c23274
GKH
458 goto error_no_buffer;
459 }
033a3fb9 460
17c23274
GKH
461 urb = usb_alloc_urb(0, GFP_ATOMIC);
462 if (!urb) {
463 dev_err(&port->dev, "no more free urbs\n");
40d2ff32 464 retval = -ENOMEM;
17c23274
GKH
465 goto error_no_urb;
466 }
033a3fb9 467
40d2ff32 468 memcpy(buffer, buf, writesize);
033a3fb9 469
40d2ff32 470 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
17c23274
GKH
471
472 usb_fill_bulk_urb(urb, serial->dev,
473 usb_sndbulkpipe(serial->dev,
474 port->bulk_out_endpointAddress),
40d2ff32 475 buffer, writesize, sierra_outdat_callback, port);
17c23274 476
238ebd13
EP
477 /* Handle the need to send a zero length packet */
478 urb->transfer_flags |= URB_ZERO_PACKET;
479
17c23274 480 /* send it down the pipe */
40d2ff32
EP
481 retval = usb_submit_urb(urb, GFP_ATOMIC);
482 if (retval) {
17c23274 483 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
40d2ff32 484 "with status = %d\n", __func__, retval);
17c23274 485 goto error;
033a3fb9
KL
486 }
487
17c23274
GKH
488 /* we are done with this urb, so let the host driver
489 * really free it when it is finished with it */
490 usb_free_urb(urb);
491
40d2ff32 492 return writesize;
17c23274
GKH
493error:
494 usb_free_urb(urb);
495error_no_urb:
496 kfree(buffer);
497error_no_buffer:
498 spin_lock_irqsave(&portdata->lock, flags);
499 --portdata->outstanding_urbs;
40d2ff32
EP
500 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
501 portdata->outstanding_urbs);
17c23274 502 spin_unlock_irqrestore(&portdata->lock, flags);
40d2ff32 503 return retval;
033a3fb9
KL
504}
505
506static void sierra_indat_callback(struct urb *urb)
507{
508 int err;
509 int endpoint;
510 struct usb_serial_port *port;
511 struct tty_struct *tty;
512 unsigned char *data = urb->transfer_buffer;
17dd2215 513 int status = urb->status;
033a3fb9 514
033a3fb9 515 endpoint = usb_pipeendpoint(urb->pipe);
b0cda8c5
EP
516 port = urb->context;
517
518 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
033a3fb9 519
17dd2215 520 if (status) {
7384a922 521 dev_dbg(&port->dev, "%s: nonzero status: %d on"
5d44b361 522 " endpoint %02x\n", __func__, status, endpoint);
033a3fb9 523 } else {
033a3fb9 524 if (urb->actual_length) {
d95186d1 525 tty = tty_port_tty_get(&port->port);
b0cda8c5 526
033a3fb9
KL
527 tty_buffer_request_room(tty, urb->actual_length);
528 tty_insert_flip_string(tty, data, urb->actual_length);
529 tty_flip_buffer_push(tty);
b0cda8c5 530
4a90f09b 531 tty_kref_put(tty);
b0cda8c5
EP
532 usb_serial_debug_data(debug, &port->dev, __func__,
533 urb->actual_length, data);
534 } else {
7384a922 535 dev_dbg(&port->dev, "%s: empty read urb"
5d44b361 536 " received\n", __func__);
033a3fb9
KL
537 }
538 }
b0cda8c5
EP
539
540 /* Resubmit urb so we continue receiving */
541 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
542 err = usb_submit_urb(urb, GFP_ATOMIC);
543 if (err)
544 dev_err(&port->dev, "resubmit read urb failed."
545 "(%d)\n", err);
546 }
547
033a3fb9
KL
548 return;
549}
550
033a3fb9
KL
551static void sierra_instat_callback(struct urb *urb)
552{
553 int err;
17dd2215 554 int status = urb->status;
cdc97792 555 struct usb_serial_port *port = urb->context;
033a3fb9
KL
556 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
557 struct usb_serial *serial = port->serial;
558
5d44b361
EP
559 dev_dbg(&port->dev, "%s\n", __func__);
560 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
7384a922 561 urb, port, portdata);
033a3fb9 562
17dd2215 563 if (status == 0) {
033a3fb9
KL
564 struct usb_ctrlrequest *req_pkt =
565 (struct usb_ctrlrequest *)urb->transfer_buffer;
566
567 if (!req_pkt) {
7384a922
KL
568 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
569 __func__);
033a3fb9
KL
570 return;
571 }
572 if ((req_pkt->bRequestType == 0xA1) &&
573 (req_pkt->bRequest == 0x20)) {
574 int old_dcd_state;
575 unsigned char signals = *((unsigned char *)
576 urb->transfer_buffer +
577 sizeof(struct usb_ctrlrequest));
4a90f09b 578 struct tty_struct *tty;
033a3fb9 579
5d44b361 580 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
7384a922 581 signals);
033a3fb9
KL
582
583 old_dcd_state = portdata->dcd_state;
584 portdata->cts_state = 1;
585 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
586 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
587 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
588
4a90f09b
AC
589 tty = tty_port_tty_get(&port->port);
590 if (tty && !C_CLOCAL(tty) &&
033a3fb9 591 old_dcd_state && !portdata->dcd_state)
4a90f09b
AC
592 tty_hangup(tty);
593 tty_kref_put(tty);
033a3fb9 594 } else {
5d44b361 595 dev_dbg(&port->dev, "%s: type %x req %x\n",
7384a922
KL
596 __func__, req_pkt->bRequestType,
597 req_pkt->bRequest);
033a3fb9
KL
598 }
599 } else
5d44b361 600 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
033a3fb9
KL
601
602 /* Resubmit urb so we continue receiving IRQ data */
17dd2215 603 if (status != -ESHUTDOWN) {
033a3fb9
KL
604 urb->dev = serial->dev;
605 err = usb_submit_urb(urb, GFP_ATOMIC);
606 if (err)
7384a922 607 dev_dbg(&port->dev, "%s: resubmit intr urb "
5d44b361 608 "failed. (%d)\n", __func__, err);
033a3fb9
KL
609 }
610}
611
95da310e 612static int sierra_write_room(struct tty_struct *tty)
033a3fb9 613{
95da310e 614 struct usb_serial_port *port = tty->driver_data;
17c23274
GKH
615 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
616 unsigned long flags;
033a3fb9 617
5d44b361 618 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
033a3fb9 619
17c23274
GKH
620 /* try to give a good number back based on if we have any free urbs at
621 * this point in time */
622 spin_lock_irqsave(&portdata->lock, flags);
623 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
624 spin_unlock_irqrestore(&portdata->lock, flags);
7384a922 625 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
17c23274 626 return 0;
033a3fb9 627 }
17c23274 628 spin_unlock_irqrestore(&portdata->lock, flags);
033a3fb9 629
17c23274 630 return 2048;
033a3fb9
KL
631}
632
b9a44bc1 633static void sierra_stop_rx_urbs(struct usb_serial_port *port)
033a3fb9 634{
9e85c5f6 635 int i;
b9a44bc1 636 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
033a3fb9 637
b9a44bc1
EP
638 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
639 usb_kill_urb(portdata->in_urbs[i]);
033a3fb9 640
b9a44bc1
EP
641 usb_kill_urb(port->interrupt_in_urb);
642}
033a3fb9 643
b9a44bc1
EP
644static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
645{
646 int ok_cnt;
647 int err = -EINVAL;
648 int i;
649 struct urb *urb;
650 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
033a3fb9 651
b9a44bc1
EP
652 ok_cnt = 0;
653 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
033a3fb9 654 urb = portdata->in_urbs[i];
9e85c5f6 655 if (!urb)
033a3fb9 656 continue;
b9a44bc1
EP
657 err = usb_submit_urb(urb, mem_flags);
658 if (err) {
659 dev_err(&port->dev, "%s: submit urb failed: %d\n",
660 __func__, err);
661 } else {
662 ok_cnt++;
033a3fb9 663 }
b9a44bc1 664 }
033a3fb9 665
b9a44bc1
EP
666 if (ok_cnt && port->interrupt_in_urb) {
667 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
668 if (err) {
669 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
670 __func__, err);
033a3fb9
KL
671 }
672 }
673
b9a44bc1
EP
674 if (ok_cnt > 0) /* at least one rx urb submitted */
675 return 0;
676 else
677 return err;
678}
679
680static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
681 int dir, void *ctx, int len,
682 gfp_t mem_flags,
683 usb_complete_t callback)
684{
685 struct urb *urb;
686 u8 *buf;
687
688 if (endpoint == -1)
689 return NULL;
033a3fb9 690
b9a44bc1
EP
691 urb = usb_alloc_urb(0, mem_flags);
692 if (urb == NULL) {
693 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
694 __func__, endpoint);
695 return NULL;
9e85c5f6 696 }
b9a44bc1
EP
697
698 buf = kmalloc(len, mem_flags);
699 if (buf) {
700 /* Fill URB using supplied data */
701 usb_fill_bulk_urb(urb, serial->dev,
702 usb_sndbulkpipe(serial->dev, endpoint) | dir,
703 buf, len, callback, ctx);
704
705 /* debug */
706 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
707 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
708 } else {
709 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
710 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
711
712 sierra_release_urb(urb);
713 urb = NULL;
714 }
715
716 return urb;
033a3fb9
KL
717}
718
b9a44bc1 719static void sierra_close(struct usb_serial_port *port)
033a3fb9 720{
b9a44bc1 721 int i;
033a3fb9
KL
722 struct usb_serial *serial = port->serial;
723 struct sierra_port_private *portdata;
724
b9a44bc1 725 dev_dbg(&port->dev, "%s\n", __func__);
033a3fb9 726 portdata = usb_get_serial_port_data(port);
b9a44bc1
EP
727
728 portdata->rts_state = 0;
729 portdata->dtr_state = 0;
033a3fb9
KL
730
731 if (serial->dev) {
e33fe4d8
ON
732 mutex_lock(&serial->disc_mutex);
733 if (!serial->disconnected)
335f8514 734 sierra_send_setup(port);
e33fe4d8 735 mutex_unlock(&serial->disc_mutex);
b9a44bc1
EP
736
737 /* Stop reading urbs */
738 sierra_stop_rx_urbs(port);
739 /* .. and release them */
740 for (i = 0; i < N_IN_URB; i++) {
741 sierra_release_urb(portdata->in_urbs[i]);
742 portdata->in_urbs[i] = NULL;
743 }
335f8514
AC
744 }
745}
746
b9a44bc1
EP
747static int sierra_open(struct tty_struct *tty,
748 struct usb_serial_port *port, struct file *filp)
335f8514 749{
b9a44bc1
EP
750 struct sierra_port_private *portdata;
751 struct usb_serial *serial = port->serial;
335f8514 752 int i;
b9a44bc1
EP
753 int err;
754 int endpoint;
755 struct urb *urb;
756
757 portdata = usb_get_serial_port_data(port);
758
5d44b361 759 dev_dbg(&port->dev, "%s\n", __func__);
b9a44bc1
EP
760
761 /* Set some sane defaults */
762 portdata->rts_state = 1;
763 portdata->dtr_state = 1;
764
765
766 endpoint = port->bulk_in_endpointAddress;
767 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
768 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
769 IN_BUFLEN, GFP_KERNEL,
770 sierra_indat_callback);
771 portdata->in_urbs[i] = urb;
772 }
773 /* clear halt condition */
774 usb_clear_halt(serial->dev,
775 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
776
777 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
778 if (err) {
779 /* get rid of everything as in close */
780 sierra_close(port);
781 return err;
782 }
783 sierra_send_setup(port);
784
785 return 0;
786}
787
788
789static void sierra_dtr_rts(struct usb_serial_port *port, int on)
790{
335f8514
AC
791 struct usb_serial *serial = port->serial;
792 struct sierra_port_private *portdata;
033a3fb9 793
335f8514 794 portdata = usb_get_serial_port_data(port);
b9a44bc1
EP
795 portdata->rts_state = on;
796 portdata->dtr_state = on;
335f8514
AC
797
798 if (serial->dev) {
b9a44bc1
EP
799 mutex_lock(&serial->disc_mutex);
800 if (!serial->disconnected)
801 sierra_send_setup(port);
802 mutex_unlock(&serial->disc_mutex);
033a3fb9 803 }
033a3fb9
KL
804}
805
033a3fb9
KL
806static int sierra_startup(struct usb_serial *serial)
807{
033a3fb9
KL
808 struct usb_serial_port *port;
809 struct sierra_port_private *portdata;
9e85c5f6 810 int i;
033a3fb9 811
5d44b361 812 dev_dbg(&serial->dev->dev, "%s\n", __func__);
033a3fb9 813
228426ed 814 /* Set Device mode to D0 */
112225b1
KL
815 sierra_set_power_state(serial->dev, 0x0000);
816
228426ed
KL
817 /* Check NMEA and set */
818 if (nmea)
819 sierra_vsc_set_nmea(serial->dev, 1);
820
033a3fb9
KL
821 /* Now setup per port private data */
822 for (i = 0; i < serial->num_ports; i++) {
823 port = serial->port[i];
824 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
825 if (!portdata) {
7384a922 826 dev_dbg(&port->dev, "%s: kmalloc for "
5d44b361 827 "sierra_port_private (%d) failed!.\n",
7384a922 828 __func__, i);
17c23274 829 return -ENOMEM;
033a3fb9 830 }
17c23274 831 spin_lock_init(&portdata->lock);
b9a44bc1 832 /* Set the port private data pointer */
033a3fb9 833 usb_set_serial_port_data(port, portdata);
033a3fb9
KL
834 }
835
17c23274 836 return 0;
033a3fb9
KL
837}
838
839static void sierra_shutdown(struct usb_serial *serial)
840{
b9a44bc1 841 int i;
033a3fb9
KL
842 struct usb_serial_port *port;
843 struct sierra_port_private *portdata;
844
5d44b361 845 dev_dbg(&serial->dev->dev, "%s\n", __func__);
033a3fb9 846
033a3fb9
KL
847 for (i = 0; i < serial->num_ports; ++i) {
848 port = serial->port[i];
f094e4f3
GKH
849 if (!port)
850 continue;
033a3fb9 851 portdata = usb_get_serial_port_data(port);
f094e4f3
GKH
852 if (!portdata)
853 continue;
9e85c5f6
GKH
854 kfree(portdata);
855 usb_set_serial_port_data(port, NULL);
033a3fb9
KL
856 }
857}
858
228426ed 859static struct usb_serial_driver sierra_device = {
964ee1de
GKH
860 .driver = {
861 .owner = THIS_MODULE,
4e0fee82 862 .name = "sierra",
964ee1de 863 },
228426ed
KL
864 .description = "Sierra USB modem",
865 .id_table = id_table,
d9b1b787 866 .usb_driver = &sierra_driver,
228426ed
KL
867 .calc_num_ports = sierra_calc_num_ports,
868 .probe = sierra_probe,
964ee1de
GKH
869 .open = sierra_open,
870 .close = sierra_close,
335f8514 871 .dtr_rts = sierra_dtr_rts,
964ee1de
GKH
872 .write = sierra_write,
873 .write_room = sierra_write_room,
964ee1de 874 .set_termios = sierra_set_termios,
964ee1de
GKH
875 .tiocmget = sierra_tiocmget,
876 .tiocmset = sierra_tiocmset,
877 .attach = sierra_startup,
878 .shutdown = sierra_shutdown,
879 .read_int_callback = sierra_instat_callback,
880};
881
882/* Functions used by new usb-serial code. */
883static int __init sierra_init(void)
884{
885 int retval;
228426ed 886 retval = usb_serial_register(&sierra_device);
964ee1de 887 if (retval)
228426ed 888 goto failed_device_register;
964ee1de
GKH
889
890
891 retval = usb_register(&sierra_driver);
892 if (retval)
893 goto failed_driver_register;
894
c197a8db
GKH
895 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
896 DRIVER_DESC "\n");
964ee1de
GKH
897
898 return 0;
899
900failed_driver_register:
228426ed
KL
901 usb_serial_deregister(&sierra_device);
902failed_device_register:
964ee1de
GKH
903 return retval;
904}
905
906static void __exit sierra_exit(void)
907{
9660ea36 908 usb_deregister(&sierra_driver);
228426ed 909 usb_serial_deregister(&sierra_device);
964ee1de
GKH
910}
911
912module_init(sierra_init);
913module_exit(sierra_exit);
914
033a3fb9
KL
915MODULE_AUTHOR(DRIVER_AUTHOR);
916MODULE_DESCRIPTION(DRIVER_DESC);
917MODULE_VERSION(DRIVER_VERSION);
69de51fd 918MODULE_LICENSE("GPL");
033a3fb9 919
4e0fee82 920module_param(nmea, bool, S_IRUGO | S_IWUSR);
228426ed
KL
921MODULE_PARM_DESC(nmea, "NMEA streaming");
922
033a3fb9
KL
923module_param(debug, bool, S_IRUGO | S_IWUSR);
924MODULE_PARM_DESC(debug, "Debug messages");