blob: dda4d05fe237d69de8db06d32beb7db041f71359 [file] [log] [blame]
Kevin Lloyd69de51f2006-06-30 11:17:55 -07001/*
Kevin Lloyd033a3fb2006-10-13 23:53:21 -07002 USB Driver for Sierra Wireless
3
Kevin Lloyd228426e2008-01-10 11:11:04 -08004 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <linux@sierrawireless.com>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -07005
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>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070015*/
16
Oliver Neukum4f4f9c52008-03-14 00:53:24 -070017#define DRIVER_VERSION "v.1.2.8"
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070018#define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>"
19#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
Kevin Lloyd69de51f2006-06-30 11:17:55 -070020
21#include <linux/kernel.h>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070022#include <linux/jiffies.h>
23#include <linux/errno.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070024#include <linux/tty.h>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070025#include <linux/tty_flip.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070026#include <linux/module.h>
27#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070028#include <linux/usb/serial.h>
Kevin Lloyd228426e2008-01-10 11:11:04 -080029#include <linux/usb/ch9.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070030
Kevin Lloyd228426e2008-01-10 11:11:04 -080031#define SWIMS_USB_REQUEST_SetPower 0x00
32#define SWIMS_USB_REQUEST_SetNmea 0x07
Kevin Lloyd112225b2007-07-16 13:49:27 -070033#define SWIMS_USB_REQUEST_SetMode 0x0B
Kevin Lloyd228426e2008-01-10 11:11:04 -080034#define SWIMS_USB_REQUEST_TYPE_VSC_SET 0x40
Kevin Lloyd112225b2007-07-16 13:49:27 -070035#define SWIMS_SET_MODE_Modem 0x0001
36
37/* per port private data */
38#define N_IN_URB 4
39#define N_OUT_URB 4
40#define IN_BUFLEN 4096
41
42static int debug;
Kevin Lloyd228426e2008-01-10 11:11:04 -080043static int nmea;
44static int truinstall = 1;
Kevin Lloyd112225b2007-07-16 13:49:27 -070045
46enum devicetype {
47 DEVICE_3_PORT = 0,
48 DEVICE_1_PORT = 1,
49 DEVICE_INSTALLER = 2,
50};
51
Adrian Bunk82a24e62007-07-29 16:59:02 +020052static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
Kevin Lloyd112225b2007-07-16 13:49:27 -070053{
54 int result;
Joe Perches898eb712007-10-18 03:06:30 -070055 dev_dbg(&udev->dev, "%s", "SET POWER STATE\n");
Kevin Lloyd112225b2007-07-16 13:49:27 -070056 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Kevin Lloyd228426e2008-01-10 11:11:04 -080057 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
58 SWIMS_USB_REQUEST_TYPE_VSC_SET, /* __u8 request type */
59 swiState, /* __u16 value */
60 0, /* __u16 index */
61 NULL, /* void *data */
62 0, /* __u16 size */
63 USB_CTRL_SET_TIMEOUT); /* int timeout */
Kevin Lloyd112225b2007-07-16 13:49:27 -070064 return result;
65}
66
Kevin Lloyd228426e2008-01-10 11:11:04 -080067static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
Kevin Lloyd112225b2007-07-16 13:49:27 -070068{
69 int result;
Joe Perches898eb712007-10-18 03:06:30 -070070 dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH\n");
Kevin Lloyd112225b2007-07-16 13:49:27 -070071 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
72 SWIMS_USB_REQUEST_SetMode, /* __u8 request */
Kevin Lloyd228426e2008-01-10 11:11:04 -080073 SWIMS_USB_REQUEST_TYPE_VSC_SET, /* __u8 request type */
74 eSWocMode, /* __u16 value */
75 0x0000, /* __u16 index */
Kevin Lloyd112225b2007-07-16 13:49:27 -070076 NULL, /* void *data */
77 0, /* __u16 size */
78 USB_CTRL_SET_TIMEOUT); /* int timeout */
79 return result;
80}
81
Kevin Lloyd228426e2008-01-10 11:11:04 -080082static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
Kevin Lloyd112225b2007-07-16 13:49:27 -070083{
84 int result;
Kevin Lloyd228426e2008-01-10 11:11:04 -080085 dev_dbg(&udev->dev, "%s", "NMEA Enable sent\n");
86 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
87 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
88 SWIMS_USB_REQUEST_TYPE_VSC_SET, /* __u8 request type */
89 enable, /* __u16 value */
90 0x0000, /* __u16 index */
91 NULL, /* void *data */
92 0, /* __u16 size */
93 USB_CTRL_SET_TIMEOUT); /* int timeout */
94 return result;
95}
Kevin Lloyd112225b2007-07-16 13:49:27 -070096
Kevin Lloyd228426e2008-01-10 11:11:04 -080097static int sierra_calc_num_ports(struct usb_serial *serial)
98{
99 int result;
100 int *num_ports = usb_get_serial_data(serial);
Kevin Lloyd112225b2007-07-16 13:49:27 -0700101
Kevin Lloyd228426e2008-01-10 11:11:04 -0800102 result = *num_ports;
103
104 if (result) {
105 kfree(num_ports);
106 usb_set_serial_data(serial, NULL);
Kevin Lloyd112225b2007-07-16 13:49:27 -0700107 }
108
Kevin Lloyd228426e2008-01-10 11:11:04 -0800109 return result;
110}
111
112static int sierra_probe(struct usb_serial *serial,
113 const struct usb_device_id *id)
114{
115 int result = 0;
116 struct usb_device *udev;
117 int *num_ports;
118 u8 ifnum;
119
120 num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
121 if (!num_ports)
122 return -ENOMEM;
123
124 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
125 udev = serial->dev;
126
127 /* Check if in installer mode */
128 if (truinstall && id->driver_info == DEVICE_INSTALLER) {
129 dev_dbg(&udev->dev, "%s", "FOUND TRU-INSTALL DEVICE(SW)\n");
130 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
131 /* Don't bind to the device when in installer mode */
132 kfree(num_ports);
133 return -EIO;
134 } else if (id->driver_info == DEVICE_1_PORT)
135 *num_ports = 1;
136 else if (ifnum == 0x99)
137 *num_ports = 0;
138 else
139 *num_ports = 3;
140 /*
141 * save off our num_ports info so that we can use it in the
142 * calc_num_ports callback
143 */
144 usb_set_serial_data(serial, (void *)num_ports);
145
146 return result;
Kevin Lloyd112225b2007-07-16 13:49:27 -0700147}
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700148
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700149static struct usb_device_id id_table [] = {
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700150 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800151 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700152 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700153 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800154 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.comb9e13ac2007-12-04 11:37:12 -0700155 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800156 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
157 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700158 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800159 { USB_DEVICE(0x1199, 0x0023) }, /* Sierra Wireless AirCard */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700160
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700161 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800162 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700163 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700164 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin R Pageed0ccdb2007-12-13 01:10:48 +0000165 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700166 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700167 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
168 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
169 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
170 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
171 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
172 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800173 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600174 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700175
Kevin Lloyd6835b322008-01-10 11:11:04 -0800176 { USB_DEVICE(0x1199, 0x6468) }, /* Sierra Wireless MP3G - EVDO */
177 { USB_DEVICE(0x1199, 0x6469) }, /* Sierra Wireless MP3G - UMTS/HSPA */
178
Kevin Lloyd112225b2007-07-16 13:49:27 -0700179 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
180 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
181
182 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700183 { }
184};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700185MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700186
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700187static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700188 .name = "sierra",
Kevin Lloyd228426e2008-01-10 11:11:04 -0800189 .probe = usb_serial_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700190 .disconnect = usb_serial_disconnect,
191 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700192 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700193};
194
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700195struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900196 spinlock_t lock; /* lock the structure */
197 int outstanding_urbs; /* number of out urbs in flight */
198
Oliver Neukum4f4f9c52008-03-14 00:53:24 -0700199 /* Input endpoints and buffers for this port */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700200 struct urb *in_urbs[N_IN_URB];
Oliver Neukum4f4f9c52008-03-14 00:53:24 -0700201 char *in_buffer[N_IN_URB];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700202
203 /* Settings for the port */
204 int rts_state; /* Handshaking pins (outputs) */
205 int dtr_state;
206 int cts_state; /* Handshaking pins (inputs) */
207 int dsr_state;
208 int dcd_state;
209 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700210};
211
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700212static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700213{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700214 struct usb_serial *serial = port->serial;
215 struct sierra_port_private *portdata;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800216 __u16 interface = 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700217
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700218 dbg("%s", __FUNCTION__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700219
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700220 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700221
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700222 if (port->tty) {
223 int val = 0;
224 if (portdata->dtr_state)
225 val |= 0x01;
226 if (portdata->rts_state)
227 val |= 0x02;
228
Kevin Lloyd228426e2008-01-10 11:11:04 -0800229 /* Determine which port is targeted */
230 if (port->bulk_out_endpointAddress == 2)
231 interface = 0;
232 else if (port->bulk_out_endpointAddress == 4)
233 interface = 1;
234 else if (port->bulk_out_endpointAddress == 5)
235 interface = 2;
236
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700237 return usb_control_msg(serial->dev,
238 usb_rcvctrlpipe(serial->dev, 0),
Kevin Lloyd228426e2008-01-10 11:11:04 -0800239 0x22, 0x21, val, interface,
240 NULL, 0, USB_CTRL_SET_TIMEOUT);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700241 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700242
243 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700244}
245
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700246static void sierra_rx_throttle(struct usb_serial_port *port)
247{
248 dbg("%s", __FUNCTION__);
249}
250
251static void sierra_rx_unthrottle(struct usb_serial_port *port)
252{
253 dbg("%s", __FUNCTION__);
254}
255
256static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
257{
258 /* Unfortunately, I don't know how to send a break */
259 dbg("%s", __FUNCTION__);
260}
261
262static void sierra_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800263 struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700264{
265 dbg("%s", __FUNCTION__);
Alan Coxed1f12e2007-10-18 01:24:22 -0700266 tty_termios_copy_hw(port->tty->termios, old_termios);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700267 sierra_send_setup(port);
268}
269
270static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
271{
272 unsigned int value;
273 struct sierra_port_private *portdata;
274
275 portdata = usb_get_serial_port_data(port);
276
277 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
278 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
279 ((portdata->cts_state) ? TIOCM_CTS : 0) |
280 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
281 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
282 ((portdata->ri_state) ? TIOCM_RNG : 0);
283
284 return value;
285}
286
287static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
288 unsigned int set, unsigned int clear)
289{
290 struct sierra_port_private *portdata;
291
292 portdata = usb_get_serial_port_data(port);
293
294 if (set & TIOCM_RTS)
295 portdata->rts_state = 1;
296 if (set & TIOCM_DTR)
297 portdata->dtr_state = 1;
298
299 if (clear & TIOCM_RTS)
300 portdata->rts_state = 0;
301 if (clear & TIOCM_DTR)
302 portdata->dtr_state = 0;
303 return sierra_send_setup(port);
304}
305
306static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
307 unsigned int cmd, unsigned long arg)
308{
309 return -ENOIOCTLCMD;
310}
311
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900312static void sierra_outdat_callback(struct urb *urb)
313{
314 struct usb_serial_port *port = urb->context;
315 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
316 int status = urb->status;
317 unsigned long flags;
318
319 dbg("%s - port %d", __FUNCTION__, port->number);
320
321 /* free up the transfer buffer, as usb_free_urb() does not do this */
322 kfree(urb->transfer_buffer);
323
324 if (status)
325 dbg("%s - nonzero write bulk status received: %d",
326 __FUNCTION__, status);
327
328 spin_lock_irqsave(&portdata->lock, flags);
329 --portdata->outstanding_urbs;
330 spin_unlock_irqrestore(&portdata->lock, flags);
331
332 usb_serial_port_softint(port);
333}
334
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700335/* Write */
336static int sierra_write(struct usb_serial_port *port,
337 const unsigned char *buf, int count)
338{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900339 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
340 struct usb_serial *serial = port->serial;
341 unsigned long flags;
342 unsigned char *buffer;
343 struct urb *urb;
344 int status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700345
346 portdata = usb_get_serial_port_data(port);
347
348 dbg("%s: write (%d chars)", __FUNCTION__, count);
349
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900350 spin_lock_irqsave(&portdata->lock, flags);
351 if (portdata->outstanding_urbs > N_OUT_URB) {
352 spin_unlock_irqrestore(&portdata->lock, flags);
353 dbg("%s - write limit hit\n", __FUNCTION__);
354 return 0;
355 }
356 portdata->outstanding_urbs++;
357 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700358
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900359 buffer = kmalloc(count, GFP_ATOMIC);
360 if (!buffer) {
361 dev_err(&port->dev, "out of memory\n");
362 count = -ENOMEM;
363 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700364 }
365
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900366 urb = usb_alloc_urb(0, GFP_ATOMIC);
367 if (!urb) {
368 dev_err(&port->dev, "no more free urbs\n");
369 count = -ENOMEM;
370 goto error_no_urb;
371 }
372
373 memcpy(buffer, buf, count);
374
375 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
376
377 usb_fill_bulk_urb(urb, serial->dev,
378 usb_sndbulkpipe(serial->dev,
379 port->bulk_out_endpointAddress),
380 buffer, count, sierra_outdat_callback, port);
381
382 /* send it down the pipe */
383 status = usb_submit_urb(urb, GFP_ATOMIC);
384 if (status) {
385 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
386 "with status = %d\n", __FUNCTION__, status);
387 count = status;
388 goto error;
389 }
390
391 /* we are done with this urb, so let the host driver
392 * really free it when it is finished with it */
393 usb_free_urb(urb);
394
395 return count;
396error:
397 usb_free_urb(urb);
398error_no_urb:
399 kfree(buffer);
400error_no_buffer:
401 spin_lock_irqsave(&portdata->lock, flags);
402 --portdata->outstanding_urbs;
403 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700404 return count;
405}
406
407static void sierra_indat_callback(struct urb *urb)
408{
409 int err;
410 int endpoint;
411 struct usb_serial_port *port;
412 struct tty_struct *tty;
413 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700414 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700415
416 dbg("%s: %p", __FUNCTION__, urb);
417
418 endpoint = usb_pipeendpoint(urb->pipe);
419 port = (struct usb_serial_port *) urb->context;
420
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700421 if (status) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700422 dbg("%s: nonzero status: %d on endpoint %02x.",
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700423 __FUNCTION__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700424 } else {
425 tty = port->tty;
426 if (urb->actual_length) {
427 tty_buffer_request_room(tty, urb->actual_length);
428 tty_insert_flip_string(tty, data, urb->actual_length);
429 tty_flip_buffer_push(tty);
430 } else {
431 dbg("%s: empty read urb received", __FUNCTION__);
432 }
433
434 /* Resubmit urb so we continue receiving */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700435 if (port->open_count && status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700436 err = usb_submit_urb(urb, GFP_ATOMIC);
437 if (err)
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900438 dev_err(&port->dev, "resubmit read urb failed."
Joe Perches898eb712007-10-18 03:06:30 -0700439 "(%d)\n", err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700440 }
441 }
442 return;
443}
444
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700445static void sierra_instat_callback(struct urb *urb)
446{
447 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700448 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700449 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
450 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
451 struct usb_serial *serial = port->serial;
452
453 dbg("%s", __FUNCTION__);
454 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
455
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700456 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700457 struct usb_ctrlrequest *req_pkt =
458 (struct usb_ctrlrequest *)urb->transfer_buffer;
459
460 if (!req_pkt) {
461 dbg("%s: NULL req_pkt\n", __FUNCTION__);
462 return;
463 }
464 if ((req_pkt->bRequestType == 0xA1) &&
465 (req_pkt->bRequest == 0x20)) {
466 int old_dcd_state;
467 unsigned char signals = *((unsigned char *)
468 urb->transfer_buffer +
469 sizeof(struct usb_ctrlrequest));
470
471 dbg("%s: signal x%x", __FUNCTION__, signals);
472
473 old_dcd_state = portdata->dcd_state;
474 portdata->cts_state = 1;
475 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
476 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
477 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
478
479 if (port->tty && !C_CLOCAL(port->tty) &&
480 old_dcd_state && !portdata->dcd_state)
481 tty_hangup(port->tty);
482 } else {
483 dbg("%s: type %x req %x", __FUNCTION__,
484 req_pkt->bRequestType,req_pkt->bRequest);
485 }
486 } else
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700487 dbg("%s: error %d", __FUNCTION__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700488
489 /* Resubmit urb so we continue receiving IRQ data */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700490 if (status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700491 urb->dev = serial->dev;
492 err = usb_submit_urb(urb, GFP_ATOMIC);
493 if (err)
494 dbg("%s: resubmit intr urb failed. (%d)",
495 __FUNCTION__, err);
496 }
497}
498
499static int sierra_write_room(struct usb_serial_port *port)
500{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900501 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
502 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700503
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900504 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700505
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900506 /* try to give a good number back based on if we have any free urbs at
507 * this point in time */
508 spin_lock_irqsave(&portdata->lock, flags);
509 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
510 spin_unlock_irqrestore(&portdata->lock, flags);
511 dbg("%s - write limit hit\n", __FUNCTION__);
512 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700513 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900514 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700515
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900516 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700517}
518
519static int sierra_chars_in_buffer(struct usb_serial_port *port)
520{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900521 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700522
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900523 /*
524 * We can't really account for how much data we
525 * have sent out, but hasn't made it through to the
526 * device as we can't see the backend here, so just
527 * tell the tty layer that everything is flushed.
528 */
529 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700530}
531
532static int sierra_open(struct usb_serial_port *port, struct file *filp)
533{
534 struct sierra_port_private *portdata;
535 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900536 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700537 struct urb *urb;
Kevin Lloyde43062d2007-01-17 16:04:18 -0800538 int result;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700539
540 portdata = usb_get_serial_port_data(port);
541
542 dbg("%s", __FUNCTION__);
543
544 /* Set some sane defaults */
545 portdata->rts_state = 1;
546 portdata->dtr_state = 1;
547
548 /* Reset low level data toggle and start reading from endpoints */
549 for (i = 0; i < N_IN_URB; i++) {
550 urb = portdata->in_urbs[i];
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900551 if (!urb)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700552 continue;
553 if (urb->dev != serial->dev) {
554 dbg("%s: dev %p != %p", __FUNCTION__,
555 urb->dev, serial->dev);
556 continue;
557 }
558
559 /*
560 * make sure endpoint data toggle is synchronized with the
561 * device
562 */
563 usb_clear_halt(urb->dev, urb->pipe);
564
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900565 result = usb_submit_urb(urb, GFP_KERNEL);
566 if (result) {
Joe Perches898eb712007-10-18 03:06:30 -0700567 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900568 i, result, urb->transfer_buffer_length);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700569 }
570 }
571
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700572 port->tty->low_latency = 1;
573
574 sierra_send_setup(port);
575
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900576 /* start up the interrupt endpoint if we have one */
577 if (port->interrupt_in_urb) {
578 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
579 if (result)
Joe Perches898eb712007-10-18 03:06:30 -0700580 dev_err(&port->dev, "submit irq_in urb failed %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900581 result);
582 }
583 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700584}
585
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700586static void sierra_close(struct usb_serial_port *port, struct file *filp)
587{
588 int i;
589 struct usb_serial *serial = port->serial;
590 struct sierra_port_private *portdata;
591
592 dbg("%s", __FUNCTION__);
593 portdata = usb_get_serial_port_data(port);
594
595 portdata->rts_state = 0;
596 portdata->dtr_state = 0;
597
598 if (serial->dev) {
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100599 mutex_lock(&serial->disc_mutex);
600 if (!serial->disconnected)
601 sierra_send_setup(port);
602 mutex_unlock(&serial->disc_mutex);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700603
604 /* Stop reading/writing urbs */
605 for (i = 0; i < N_IN_URB; i++)
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900606 usb_kill_urb(portdata->in_urbs[i]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700607 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900608
609 usb_kill_urb(port->interrupt_in_urb);
610
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700611 port->tty = NULL;
612}
613
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700614static int sierra_startup(struct usb_serial *serial)
615{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700616 struct usb_serial_port *port;
617 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900618 struct urb *urb;
619 int i;
620 int j;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700621
622 dbg("%s", __FUNCTION__);
623
Kevin Lloyd228426e2008-01-10 11:11:04 -0800624 /* Set Device mode to D0 */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700625 sierra_set_power_state(serial->dev, 0x0000);
626
Kevin Lloyd228426e2008-01-10 11:11:04 -0800627 /* Check NMEA and set */
628 if (nmea)
629 sierra_vsc_set_nmea(serial->dev, 1);
630
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700631 /* Now setup per port private data */
632 for (i = 0; i < serial->num_ports; i++) {
633 port = serial->port[i];
634 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
635 if (!portdata) {
636 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
637 __FUNCTION__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900638 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700639 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900640 spin_lock_init(&portdata->lock);
Oliver Neukum4f4f9c52008-03-14 00:53:24 -0700641 for (j = 0; j < N_IN_URB; j++) {
642 portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
643 if (!portdata->in_buffer[j]) {
644 for (--j; j >= 0; j--)
645 kfree(portdata->in_buffer[j]);
646 kfree(portdata);
647 return -ENOMEM;
648 }
649 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700650
651 usb_set_serial_port_data(port, portdata);
652
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900653 /* initialize the in urbs */
654 for (j = 0; j < N_IN_URB; ++j) {
655 urb = usb_alloc_urb(0, GFP_KERNEL);
656 if (urb == NULL) {
657 dbg("%s: alloc for in port failed.",
658 __FUNCTION__);
659 continue;
660 }
661 /* Fill URB using supplied data. */
662 usb_fill_bulk_urb(urb, serial->dev,
663 usb_rcvbulkpipe(serial->dev,
664 port->bulk_in_endpointAddress),
665 portdata->in_buffer[j], IN_BUFLEN,
666 sierra_indat_callback, port);
667 portdata->in_urbs[j] = urb;
668 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700669 }
670
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900671 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700672}
673
674static void sierra_shutdown(struct usb_serial *serial)
675{
676 int i, j;
677 struct usb_serial_port *port;
678 struct sierra_port_private *portdata;
679
680 dbg("%s", __FUNCTION__);
681
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700682 for (i = 0; i < serial->num_ports; ++i) {
683 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700684 if (!port)
685 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700686 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700687 if (!portdata)
688 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700689
690 for (j = 0; j < N_IN_URB; j++) {
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900691 usb_kill_urb(portdata->in_urbs[j]);
692 usb_free_urb(portdata->in_urbs[j]);
Oliver Neukum4f4f9c52008-03-14 00:53:24 -0700693 kfree(portdata->in_buffer[j]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700694 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900695 kfree(portdata);
696 usb_set_serial_port_data(port, NULL);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700697 }
698}
699
Kevin Lloyd228426e2008-01-10 11:11:04 -0800700static struct usb_serial_driver sierra_device = {
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700701 .driver = {
702 .owner = THIS_MODULE,
703 .name = "sierra1",
704 },
Kevin Lloyd228426e2008-01-10 11:11:04 -0800705 .description = "Sierra USB modem",
706 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100707 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700708 .num_interrupt_in = NUM_DONT_CARE,
Kevin Lloyd228426e2008-01-10 11:11:04 -0800709 .num_bulk_in = NUM_DONT_CARE,
710 .num_bulk_out = NUM_DONT_CARE,
711 .calc_num_ports = sierra_calc_num_ports,
712 .probe = sierra_probe,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700713 .open = sierra_open,
714 .close = sierra_close,
715 .write = sierra_write,
716 .write_room = sierra_write_room,
717 .chars_in_buffer = sierra_chars_in_buffer,
718 .throttle = sierra_rx_throttle,
719 .unthrottle = sierra_rx_unthrottle,
720 .ioctl = sierra_ioctl,
721 .set_termios = sierra_set_termios,
722 .break_ctl = sierra_break_ctl,
723 .tiocmget = sierra_tiocmget,
724 .tiocmset = sierra_tiocmset,
725 .attach = sierra_startup,
726 .shutdown = sierra_shutdown,
727 .read_int_callback = sierra_instat_callback,
728};
729
730/* Functions used by new usb-serial code. */
731static int __init sierra_init(void)
732{
733 int retval;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800734 retval = usb_serial_register(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700735 if (retval)
Kevin Lloyd228426e2008-01-10 11:11:04 -0800736 goto failed_device_register;
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700737
738
739 retval = usb_register(&sierra_driver);
740 if (retval)
741 goto failed_driver_register;
742
743 info(DRIVER_DESC ": " DRIVER_VERSION);
744
745 return 0;
746
747failed_driver_register:
Kevin Lloyd228426e2008-01-10 11:11:04 -0800748 usb_serial_deregister(&sierra_device);
749failed_device_register:
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700750 return retval;
751}
752
753static void __exit sierra_exit(void)
754{
755 usb_deregister (&sierra_driver);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800756 usb_serial_deregister(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700757}
758
759module_init(sierra_init);
760module_exit(sierra_exit);
761
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700762MODULE_AUTHOR(DRIVER_AUTHOR);
763MODULE_DESCRIPTION(DRIVER_DESC);
764MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700765MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700766
Kevin Lloyd228426e2008-01-10 11:11:04 -0800767module_param(truinstall, bool, 0);
768MODULE_PARM_DESC(truinstall, "TRU-Install support");
769
770module_param(nmea, bool, 0);
771MODULE_PARM_DESC(nmea, "NMEA streaming");
772
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700773#ifdef CONFIG_USB_DEBUG
774module_param(debug, bool, S_IRUGO | S_IWUSR);
775MODULE_PARM_DESC(debug, "Debug messages");
776#endif
777