blob: b4d7ac948533eeadfb7bf42bfcaf8230f5260b35 [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 Lloyd112225b2007-07-16 13:49:27 -07004 Copyright (C) 2006, 2007 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
Kevin Lloyd9454c462007-07-16 13:49:29 -070017#define DRIVER_VERSION "v.1.2.5b"
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 Lloyd69de51f2006-06-30 11:17:55 -070029
Kevin Lloyd112225b2007-07-16 13:49:27 -070030#define SWIMS_USB_REQUEST_SetMode 0x0B
31#define SWIMS_USB_REQUEST_TYPE_SetMode 0x40
32#define SWIMS_USB_INDEX_SetMode 0x0000
33#define SWIMS_SET_MODE_Modem 0x0001
34
35/* per port private data */
36#define N_IN_URB 4
37#define N_OUT_URB 4
38#define IN_BUFLEN 4096
39
40static int debug;
41
42enum devicetype {
43 DEVICE_3_PORT = 0,
44 DEVICE_1_PORT = 1,
45 DEVICE_INSTALLER = 2,
46};
47
Adrian Bunk82a24e62007-07-29 16:59:02 +020048static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
Kevin Lloyd112225b2007-07-16 13:49:27 -070049{
50 int result;
Joe Perches898eb712007-10-18 03:06:30 -070051 dev_dbg(&udev->dev, "%s", "SET POWER STATE\n");
Kevin Lloyd112225b2007-07-16 13:49:27 -070052 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
53 0x00, /* __u8 request */
54 0x40, /* __u8 request type */
55 swiState, /* __u16 value */
56 0, /* __u16 index */
57 NULL, /* void *data */
58 0, /* __u16 size */
59 USB_CTRL_SET_TIMEOUT); /* int timeout */
60 return result;
61}
62
Adrian Bunk82a24e62007-07-29 16:59:02 +020063static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSocMode)
Kevin Lloyd112225b2007-07-16 13:49:27 -070064{
65 int result;
Joe Perches898eb712007-10-18 03:06:30 -070066 dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH\n");
Kevin Lloyd112225b2007-07-16 13:49:27 -070067 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
68 SWIMS_USB_REQUEST_SetMode, /* __u8 request */
69 SWIMS_USB_REQUEST_TYPE_SetMode, /* __u8 request type */
70 eSocMode, /* __u16 value */
71 SWIMS_USB_INDEX_SetMode, /* __u16 index */
72 NULL, /* void *data */
73 0, /* __u16 size */
74 USB_CTRL_SET_TIMEOUT); /* int timeout */
75 return result;
76}
77
Adrian Bunk82a24e62007-07-29 16:59:02 +020078static int sierra_probe(struct usb_interface *iface,
79 const struct usb_device_id *id)
Kevin Lloyd112225b2007-07-16 13:49:27 -070080{
81 int result;
82 struct usb_device *udev;
83
84 udev = usb_get_dev(interface_to_usbdev(iface));
85
86 /* Check if in installer mode */
87 if (id->driver_info == DEVICE_INSTALLER) {
88 dev_dbg(&udev->dev, "%s", "FOUND DEVICE(SW)\n");
89 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
90 /*We do not want to bind to the device when in installer mode*/
91 return -EIO;
92 }
93
94 return usb_serial_probe(iface, id);
95}
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070096
Kevin Lloyd69de51f2006-06-30 11:17:55 -070097static struct usb_device_id id_table [] = {
Kevin Lloyd69de51f2006-06-30 11:17:55 -070098 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080099 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700100 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700101 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800102 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.comb9e13ac2007-12-04 11:37:12 -0700103 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800104 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
105 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700106 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800107 { USB_DEVICE(0x1199, 0x0023) }, /* Sierra Wireless AirCard */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700108
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700109 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800110 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700111 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700112 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin R Pageed0ccdb2007-12-13 01:10:48 +0000113 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700114 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700115 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
116 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
117 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
118 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
119 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
120 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800121 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600122 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700123
Kevin Lloyd6835b322008-01-10 11:11:04 -0800124 { USB_DEVICE(0x1199, 0x6468) }, /* Sierra Wireless MP3G - EVDO */
125 { USB_DEVICE(0x1199, 0x6469) }, /* Sierra Wireless MP3G - UMTS/HSPA */
126
Kevin Lloyd112225b2007-07-16 13:49:27 -0700127 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
128 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
Bruno Redondie3e73c92008-01-11 22:00:59 +0100129 { USB_DEVICE(0x05C6, 0x6613), .driver_info = DEVICE_1_PORT }, /* Onda H600/ZTE MF330 */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700130
131 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700132 { }
133};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700134MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700135
136static struct usb_device_id id_table_1port [] = {
137 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
138 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
Bruno Redondie3e73c92008-01-11 22:00:59 +0100139 { USB_DEVICE(0x05C6, 0x6613) }, /* Onda H600/ZTE MF330 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700140 { }
141};
142
143static struct usb_device_id id_table_3port [] = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700144 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800145 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700146 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700147 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800148 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.com5fdcd032007-11-20 13:39:03 -0700149 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800150 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
151 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700152 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U*/
Kevin Lloyd6835b322008-01-10 11:11:04 -0800153 { USB_DEVICE(0x1199, 0x0023) }, /* Sierra Wireless AirCard */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700154
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700155 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800156 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700157 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700158 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin R Pageed0ccdb2007-12-13 01:10:48 +0000159 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700160 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700161 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
162 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
163 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
164 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
165 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
166 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800167 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600168 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881U */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800169 { USB_DEVICE(0x1199, 0x6468) }, /* Sierra Wireless MP3G - EVDO */
170 { USB_DEVICE(0x1199, 0x6469) }, /* Sierra Wireless MP3G - UMTS/HSPA */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700171 { }
172};
173
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700174static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700175 .name = "sierra",
Kevin Lloyd112225b2007-07-16 13:49:27 -0700176 .probe = sierra_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700177 .disconnect = usb_serial_disconnect,
178 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700179 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700180};
181
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700182
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700183struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900184 spinlock_t lock; /* lock the structure */
185 int outstanding_urbs; /* number of out urbs in flight */
186
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700187 /* Input endpoints and buffer for this port */
188 struct urb *in_urbs[N_IN_URB];
189 char in_buffer[N_IN_URB][IN_BUFLEN];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700190
191 /* Settings for the port */
192 int rts_state; /* Handshaking pins (outputs) */
193 int dtr_state;
194 int cts_state; /* Handshaking pins (inputs) */
195 int dsr_state;
196 int dcd_state;
197 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700198};
199
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700200static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700201{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700202 struct usb_serial *serial = port->serial;
203 struct sierra_port_private *portdata;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700204
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700205 dbg("%s", __FUNCTION__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700206
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700207 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700208
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700209 if (port->tty) {
210 int val = 0;
211 if (portdata->dtr_state)
212 val |= 0x01;
213 if (portdata->rts_state)
214 val |= 0x02;
215
216 return usb_control_msg(serial->dev,
217 usb_rcvctrlpipe(serial->dev, 0),
218 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
219 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700220
221 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700222}
223
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700224static void sierra_rx_throttle(struct usb_serial_port *port)
225{
226 dbg("%s", __FUNCTION__);
227}
228
229static void sierra_rx_unthrottle(struct usb_serial_port *port)
230{
231 dbg("%s", __FUNCTION__);
232}
233
234static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
235{
236 /* Unfortunately, I don't know how to send a break */
237 dbg("%s", __FUNCTION__);
238}
239
240static void sierra_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800241 struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700242{
243 dbg("%s", __FUNCTION__);
Alan Coxed1f12e2007-10-18 01:24:22 -0700244 tty_termios_copy_hw(port->tty->termios, old_termios);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700245 sierra_send_setup(port);
246}
247
248static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
249{
250 unsigned int value;
251 struct sierra_port_private *portdata;
252
253 portdata = usb_get_serial_port_data(port);
254
255 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
256 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
257 ((portdata->cts_state) ? TIOCM_CTS : 0) |
258 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
259 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
260 ((portdata->ri_state) ? TIOCM_RNG : 0);
261
262 return value;
263}
264
265static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
266 unsigned int set, unsigned int clear)
267{
268 struct sierra_port_private *portdata;
269
270 portdata = usb_get_serial_port_data(port);
271
272 if (set & TIOCM_RTS)
273 portdata->rts_state = 1;
274 if (set & TIOCM_DTR)
275 portdata->dtr_state = 1;
276
277 if (clear & TIOCM_RTS)
278 portdata->rts_state = 0;
279 if (clear & TIOCM_DTR)
280 portdata->dtr_state = 0;
281 return sierra_send_setup(port);
282}
283
284static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
285 unsigned int cmd, unsigned long arg)
286{
287 return -ENOIOCTLCMD;
288}
289
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900290static void sierra_outdat_callback(struct urb *urb)
291{
292 struct usb_serial_port *port = urb->context;
293 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
294 int status = urb->status;
295 unsigned long flags;
296
297 dbg("%s - port %d", __FUNCTION__, port->number);
298
299 /* free up the transfer buffer, as usb_free_urb() does not do this */
300 kfree(urb->transfer_buffer);
301
302 if (status)
303 dbg("%s - nonzero write bulk status received: %d",
304 __FUNCTION__, status);
305
306 spin_lock_irqsave(&portdata->lock, flags);
307 --portdata->outstanding_urbs;
308 spin_unlock_irqrestore(&portdata->lock, flags);
309
310 usb_serial_port_softint(port);
311}
312
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700313/* Write */
314static int sierra_write(struct usb_serial_port *port,
315 const unsigned char *buf, int count)
316{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900317 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
318 struct usb_serial *serial = port->serial;
319 unsigned long flags;
320 unsigned char *buffer;
321 struct urb *urb;
322 int status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700323
324 portdata = usb_get_serial_port_data(port);
325
326 dbg("%s: write (%d chars)", __FUNCTION__, count);
327
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900328 spin_lock_irqsave(&portdata->lock, flags);
329 if (portdata->outstanding_urbs > N_OUT_URB) {
330 spin_unlock_irqrestore(&portdata->lock, flags);
331 dbg("%s - write limit hit\n", __FUNCTION__);
332 return 0;
333 }
334 portdata->outstanding_urbs++;
335 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700336
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900337 buffer = kmalloc(count, GFP_ATOMIC);
338 if (!buffer) {
339 dev_err(&port->dev, "out of memory\n");
340 count = -ENOMEM;
341 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700342 }
343
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900344 urb = usb_alloc_urb(0, GFP_ATOMIC);
345 if (!urb) {
346 dev_err(&port->dev, "no more free urbs\n");
347 count = -ENOMEM;
348 goto error_no_urb;
349 }
350
351 memcpy(buffer, buf, count);
352
353 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
354
355 usb_fill_bulk_urb(urb, serial->dev,
356 usb_sndbulkpipe(serial->dev,
357 port->bulk_out_endpointAddress),
358 buffer, count, sierra_outdat_callback, port);
359
360 /* send it down the pipe */
361 status = usb_submit_urb(urb, GFP_ATOMIC);
362 if (status) {
363 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
364 "with status = %d\n", __FUNCTION__, status);
365 count = status;
366 goto error;
367 }
368
369 /* we are done with this urb, so let the host driver
370 * really free it when it is finished with it */
371 usb_free_urb(urb);
372
373 return count;
374error:
375 usb_free_urb(urb);
376error_no_urb:
377 kfree(buffer);
378error_no_buffer:
379 spin_lock_irqsave(&portdata->lock, flags);
380 --portdata->outstanding_urbs;
381 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700382 return count;
383}
384
385static void sierra_indat_callback(struct urb *urb)
386{
387 int err;
388 int endpoint;
389 struct usb_serial_port *port;
390 struct tty_struct *tty;
391 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700392 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700393
394 dbg("%s: %p", __FUNCTION__, urb);
395
396 endpoint = usb_pipeendpoint(urb->pipe);
397 port = (struct usb_serial_port *) urb->context;
398
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700399 if (status) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700400 dbg("%s: nonzero status: %d on endpoint %02x.",
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700401 __FUNCTION__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700402 } else {
403 tty = port->tty;
404 if (urb->actual_length) {
405 tty_buffer_request_room(tty, urb->actual_length);
406 tty_insert_flip_string(tty, data, urb->actual_length);
407 tty_flip_buffer_push(tty);
408 } else {
409 dbg("%s: empty read urb received", __FUNCTION__);
410 }
411
412 /* Resubmit urb so we continue receiving */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700413 if (port->open_count && status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700414 err = usb_submit_urb(urb, GFP_ATOMIC);
415 if (err)
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900416 dev_err(&port->dev, "resubmit read urb failed."
Joe Perches898eb712007-10-18 03:06:30 -0700417 "(%d)\n", err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700418 }
419 }
420 return;
421}
422
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700423static void sierra_instat_callback(struct urb *urb)
424{
425 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700426 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700427 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
428 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
429 struct usb_serial *serial = port->serial;
430
431 dbg("%s", __FUNCTION__);
432 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
433
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700434 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700435 struct usb_ctrlrequest *req_pkt =
436 (struct usb_ctrlrequest *)urb->transfer_buffer;
437
438 if (!req_pkt) {
439 dbg("%s: NULL req_pkt\n", __FUNCTION__);
440 return;
441 }
442 if ((req_pkt->bRequestType == 0xA1) &&
443 (req_pkt->bRequest == 0x20)) {
444 int old_dcd_state;
445 unsigned char signals = *((unsigned char *)
446 urb->transfer_buffer +
447 sizeof(struct usb_ctrlrequest));
448
449 dbg("%s: signal x%x", __FUNCTION__, signals);
450
451 old_dcd_state = portdata->dcd_state;
452 portdata->cts_state = 1;
453 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
454 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
455 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
456
457 if (port->tty && !C_CLOCAL(port->tty) &&
458 old_dcd_state && !portdata->dcd_state)
459 tty_hangup(port->tty);
460 } else {
461 dbg("%s: type %x req %x", __FUNCTION__,
462 req_pkt->bRequestType,req_pkt->bRequest);
463 }
464 } else
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700465 dbg("%s: error %d", __FUNCTION__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700466
467 /* Resubmit urb so we continue receiving IRQ data */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700468 if (status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700469 urb->dev = serial->dev;
470 err = usb_submit_urb(urb, GFP_ATOMIC);
471 if (err)
472 dbg("%s: resubmit intr urb failed. (%d)",
473 __FUNCTION__, err);
474 }
475}
476
477static int sierra_write_room(struct usb_serial_port *port)
478{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900479 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
480 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700481
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900482 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700483
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900484 /* try to give a good number back based on if we have any free urbs at
485 * this point in time */
486 spin_lock_irqsave(&portdata->lock, flags);
487 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
488 spin_unlock_irqrestore(&portdata->lock, flags);
489 dbg("%s - write limit hit\n", __FUNCTION__);
490 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700491 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900492 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700493
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900494 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700495}
496
497static int sierra_chars_in_buffer(struct usb_serial_port *port)
498{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900499 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700500
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900501 /*
502 * We can't really account for how much data we
503 * have sent out, but hasn't made it through to the
504 * device as we can't see the backend here, so just
505 * tell the tty layer that everything is flushed.
506 */
507 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700508}
509
510static int sierra_open(struct usb_serial_port *port, struct file *filp)
511{
512 struct sierra_port_private *portdata;
513 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900514 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700515 struct urb *urb;
Kevin Lloyde43062d2007-01-17 16:04:18 -0800516 int result;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700517
518 portdata = usb_get_serial_port_data(port);
519
520 dbg("%s", __FUNCTION__);
521
522 /* Set some sane defaults */
523 portdata->rts_state = 1;
524 portdata->dtr_state = 1;
525
526 /* Reset low level data toggle and start reading from endpoints */
527 for (i = 0; i < N_IN_URB; i++) {
528 urb = portdata->in_urbs[i];
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900529 if (!urb)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700530 continue;
531 if (urb->dev != serial->dev) {
532 dbg("%s: dev %p != %p", __FUNCTION__,
533 urb->dev, serial->dev);
534 continue;
535 }
536
537 /*
538 * make sure endpoint data toggle is synchronized with the
539 * device
540 */
541 usb_clear_halt(urb->dev, urb->pipe);
542
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900543 result = usb_submit_urb(urb, GFP_KERNEL);
544 if (result) {
Joe Perches898eb712007-10-18 03:06:30 -0700545 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900546 i, result, urb->transfer_buffer_length);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700547 }
548 }
549
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700550 port->tty->low_latency = 1;
551
552 sierra_send_setup(port);
553
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900554 /* start up the interrupt endpoint if we have one */
555 if (port->interrupt_in_urb) {
556 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
557 if (result)
Joe Perches898eb712007-10-18 03:06:30 -0700558 dev_err(&port->dev, "submit irq_in urb failed %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900559 result);
560 }
561 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700562}
563
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700564static void sierra_close(struct usb_serial_port *port, struct file *filp)
565{
566 int i;
567 struct usb_serial *serial = port->serial;
568 struct sierra_port_private *portdata;
569
570 dbg("%s", __FUNCTION__);
571 portdata = usb_get_serial_port_data(port);
572
573 portdata->rts_state = 0;
574 portdata->dtr_state = 0;
575
576 if (serial->dev) {
577 sierra_send_setup(port);
578
579 /* Stop reading/writing urbs */
580 for (i = 0; i < N_IN_URB; i++)
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900581 usb_kill_urb(portdata->in_urbs[i]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700582 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900583
584 usb_kill_urb(port->interrupt_in_urb);
585
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700586 port->tty = NULL;
587}
588
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700589static int sierra_startup(struct usb_serial *serial)
590{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700591 struct usb_serial_port *port;
592 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900593 struct urb *urb;
594 int i;
595 int j;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700596
597 dbg("%s", __FUNCTION__);
598
Kevin Lloyd112225b2007-07-16 13:49:27 -0700599 /*Set Device mode to D0 */
600 sierra_set_power_state(serial->dev, 0x0000);
601
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700602 /* Now setup per port private data */
603 for (i = 0; i < serial->num_ports; i++) {
604 port = serial->port[i];
605 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
606 if (!portdata) {
607 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
608 __FUNCTION__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900609 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700610 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900611 spin_lock_init(&portdata->lock);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700612
613 usb_set_serial_port_data(port, portdata);
614
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900615 /* initialize the in urbs */
616 for (j = 0; j < N_IN_URB; ++j) {
617 urb = usb_alloc_urb(0, GFP_KERNEL);
618 if (urb == NULL) {
619 dbg("%s: alloc for in port failed.",
620 __FUNCTION__);
621 continue;
622 }
623 /* Fill URB using supplied data. */
624 usb_fill_bulk_urb(urb, serial->dev,
625 usb_rcvbulkpipe(serial->dev,
626 port->bulk_in_endpointAddress),
627 portdata->in_buffer[j], IN_BUFLEN,
628 sierra_indat_callback, port);
629 portdata->in_urbs[j] = urb;
630 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700631 }
632
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900633 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700634}
635
636static void sierra_shutdown(struct usb_serial *serial)
637{
638 int i, j;
639 struct usb_serial_port *port;
640 struct sierra_port_private *portdata;
641
642 dbg("%s", __FUNCTION__);
643
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700644 for (i = 0; i < serial->num_ports; ++i) {
645 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700646 if (!port)
647 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700648 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700649 if (!portdata)
650 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700651
652 for (j = 0; j < N_IN_URB; j++) {
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900653 usb_kill_urb(portdata->in_urbs[j]);
654 usb_free_urb(portdata->in_urbs[j]);
655 portdata->in_urbs[j] = NULL;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700656 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900657 kfree(portdata);
658 usb_set_serial_port_data(port, NULL);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700659 }
660}
661
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700662static struct usb_serial_driver sierra_1port_device = {
663 .driver = {
664 .owner = THIS_MODULE,
665 .name = "sierra1",
666 },
667 .description = "Sierra USB modem (1 port)",
668 .id_table = id_table_1port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100669 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700670 .num_interrupt_in = NUM_DONT_CARE,
671 .num_bulk_in = 1,
672 .num_bulk_out = 1,
673 .num_ports = 1,
674 .open = sierra_open,
675 .close = sierra_close,
676 .write = sierra_write,
677 .write_room = sierra_write_room,
678 .chars_in_buffer = sierra_chars_in_buffer,
679 .throttle = sierra_rx_throttle,
680 .unthrottle = sierra_rx_unthrottle,
681 .ioctl = sierra_ioctl,
682 .set_termios = sierra_set_termios,
683 .break_ctl = sierra_break_ctl,
684 .tiocmget = sierra_tiocmget,
685 .tiocmset = sierra_tiocmset,
686 .attach = sierra_startup,
687 .shutdown = sierra_shutdown,
688 .read_int_callback = sierra_instat_callback,
689};
690
691static struct usb_serial_driver sierra_3port_device = {
692 .driver = {
693 .owner = THIS_MODULE,
694 .name = "sierra3",
695 },
696 .description = "Sierra USB modem (3 port)",
697 .id_table = id_table_3port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100698 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700699 .num_interrupt_in = NUM_DONT_CARE,
700 .num_bulk_in = 3,
701 .num_bulk_out = 3,
702 .num_ports = 3,
703 .open = sierra_open,
704 .close = sierra_close,
705 .write = sierra_write,
706 .write_room = sierra_write_room,
707 .chars_in_buffer = sierra_chars_in_buffer,
708 .throttle = sierra_rx_throttle,
709 .unthrottle = sierra_rx_unthrottle,
710 .ioctl = sierra_ioctl,
711 .set_termios = sierra_set_termios,
712 .break_ctl = sierra_break_ctl,
713 .tiocmget = sierra_tiocmget,
714 .tiocmset = sierra_tiocmset,
715 .attach = sierra_startup,
716 .shutdown = sierra_shutdown,
717 .read_int_callback = sierra_instat_callback,
718};
719
720/* Functions used by new usb-serial code. */
721static int __init sierra_init(void)
722{
723 int retval;
724 retval = usb_serial_register(&sierra_1port_device);
725 if (retval)
726 goto failed_1port_device_register;
727 retval = usb_serial_register(&sierra_3port_device);
728 if (retval)
729 goto failed_3port_device_register;
730
731
732 retval = usb_register(&sierra_driver);
733 if (retval)
734 goto failed_driver_register;
735
736 info(DRIVER_DESC ": " DRIVER_VERSION);
737
738 return 0;
739
740failed_driver_register:
741 usb_serial_deregister(&sierra_3port_device);
742failed_3port_device_register:
743 usb_serial_deregister(&sierra_1port_device);
744failed_1port_device_register:
745 return retval;
746}
747
748static void __exit sierra_exit(void)
749{
750 usb_deregister (&sierra_driver);
751 usb_serial_deregister(&sierra_1port_device);
752 usb_serial_deregister(&sierra_3port_device);
753}
754
755module_init(sierra_init);
756module_exit(sierra_exit);
757
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700758MODULE_AUTHOR(DRIVER_AUTHOR);
759MODULE_DESCRIPTION(DRIVER_DESC);
760MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700761MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700762
763#ifdef CONFIG_USB_DEBUG
764module_param(debug, bool, S_IRUGO | S_IWUSR);
765MODULE_PARM_DESC(debug, "Debug messages");
766#endif
767