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