blob: 47bce35fd484441c1903b858f216e156b17b3df7 [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 */
129
130 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700131 { }
132};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700133MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700134
135static struct usb_device_id id_table_1port [] = {
136 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
137 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
138 { }
139};
140
141static struct usb_device_id id_table_3port [] = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700142 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800143 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700144 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700145 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800146 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.com5fdcd032007-11-20 13:39:03 -0700147 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800148 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
149 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700150 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U*/
Kevin Lloyd6835b322008-01-10 11:11:04 -0800151 { USB_DEVICE(0x1199, 0x0023) }, /* Sierra Wireless AirCard */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700152
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700153 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800154 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700155 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700156 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin R Pageed0ccdb2007-12-13 01:10:48 +0000157 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700158 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700159 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
160 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
161 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
162 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
163 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
164 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800165 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600166 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881U */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800167 { USB_DEVICE(0x1199, 0x6468) }, /* Sierra Wireless MP3G - EVDO */
168 { USB_DEVICE(0x1199, 0x6469) }, /* Sierra Wireless MP3G - UMTS/HSPA */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700169 { }
170};
171
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700172static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700173 .name = "sierra",
Kevin Lloyd112225b2007-07-16 13:49:27 -0700174 .probe = sierra_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700175 .disconnect = usb_serial_disconnect,
176 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700177 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700178};
179
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700180
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700181struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900182 spinlock_t lock; /* lock the structure */
183 int outstanding_urbs; /* number of out urbs in flight */
184
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700185 /* Input endpoints and buffer for this port */
186 struct urb *in_urbs[N_IN_URB];
187 char in_buffer[N_IN_URB][IN_BUFLEN];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700188
189 /* Settings for the port */
190 int rts_state; /* Handshaking pins (outputs) */
191 int dtr_state;
192 int cts_state; /* Handshaking pins (inputs) */
193 int dsr_state;
194 int dcd_state;
195 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700196};
197
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700198static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700199{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700200 struct usb_serial *serial = port->serial;
201 struct sierra_port_private *portdata;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700202
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700203 dbg("%s", __FUNCTION__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700204
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700205 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700206
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700207 if (port->tty) {
208 int val = 0;
209 if (portdata->dtr_state)
210 val |= 0x01;
211 if (portdata->rts_state)
212 val |= 0x02;
213
214 return usb_control_msg(serial->dev,
215 usb_rcvctrlpipe(serial->dev, 0),
216 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
217 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700218
219 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700220}
221
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700222static void sierra_rx_throttle(struct usb_serial_port *port)
223{
224 dbg("%s", __FUNCTION__);
225}
226
227static void sierra_rx_unthrottle(struct usb_serial_port *port)
228{
229 dbg("%s", __FUNCTION__);
230}
231
232static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
233{
234 /* Unfortunately, I don't know how to send a break */
235 dbg("%s", __FUNCTION__);
236}
237
238static void sierra_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800239 struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700240{
241 dbg("%s", __FUNCTION__);
Alan Coxed1f12e2007-10-18 01:24:22 -0700242 tty_termios_copy_hw(port->tty->termios, old_termios);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700243 sierra_send_setup(port);
244}
245
246static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
247{
248 unsigned int value;
249 struct sierra_port_private *portdata;
250
251 portdata = usb_get_serial_port_data(port);
252
253 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
254 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
255 ((portdata->cts_state) ? TIOCM_CTS : 0) |
256 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
257 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
258 ((portdata->ri_state) ? TIOCM_RNG : 0);
259
260 return value;
261}
262
263static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
264 unsigned int set, unsigned int clear)
265{
266 struct sierra_port_private *portdata;
267
268 portdata = usb_get_serial_port_data(port);
269
270 if (set & TIOCM_RTS)
271 portdata->rts_state = 1;
272 if (set & TIOCM_DTR)
273 portdata->dtr_state = 1;
274
275 if (clear & TIOCM_RTS)
276 portdata->rts_state = 0;
277 if (clear & TIOCM_DTR)
278 portdata->dtr_state = 0;
279 return sierra_send_setup(port);
280}
281
282static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
283 unsigned int cmd, unsigned long arg)
284{
285 return -ENOIOCTLCMD;
286}
287
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900288static void sierra_outdat_callback(struct urb *urb)
289{
290 struct usb_serial_port *port = urb->context;
291 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
292 int status = urb->status;
293 unsigned long flags;
294
295 dbg("%s - port %d", __FUNCTION__, port->number);
296
297 /* free up the transfer buffer, as usb_free_urb() does not do this */
298 kfree(urb->transfer_buffer);
299
300 if (status)
301 dbg("%s - nonzero write bulk status received: %d",
302 __FUNCTION__, status);
303
304 spin_lock_irqsave(&portdata->lock, flags);
305 --portdata->outstanding_urbs;
306 spin_unlock_irqrestore(&portdata->lock, flags);
307
308 usb_serial_port_softint(port);
309}
310
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700311/* Write */
312static int sierra_write(struct usb_serial_port *port,
313 const unsigned char *buf, int count)
314{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900315 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
316 struct usb_serial *serial = port->serial;
317 unsigned long flags;
318 unsigned char *buffer;
319 struct urb *urb;
320 int status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700321
322 portdata = usb_get_serial_port_data(port);
323
324 dbg("%s: write (%d chars)", __FUNCTION__, count);
325
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900326 spin_lock_irqsave(&portdata->lock, flags);
327 if (portdata->outstanding_urbs > N_OUT_URB) {
328 spin_unlock_irqrestore(&portdata->lock, flags);
329 dbg("%s - write limit hit\n", __FUNCTION__);
330 return 0;
331 }
332 portdata->outstanding_urbs++;
333 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700334
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900335 buffer = kmalloc(count, GFP_ATOMIC);
336 if (!buffer) {
337 dev_err(&port->dev, "out of memory\n");
338 count = -ENOMEM;
339 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700340 }
341
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900342 urb = usb_alloc_urb(0, GFP_ATOMIC);
343 if (!urb) {
344 dev_err(&port->dev, "no more free urbs\n");
345 count = -ENOMEM;
346 goto error_no_urb;
347 }
348
349 memcpy(buffer, buf, count);
350
351 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
352
353 usb_fill_bulk_urb(urb, serial->dev,
354 usb_sndbulkpipe(serial->dev,
355 port->bulk_out_endpointAddress),
356 buffer, count, sierra_outdat_callback, port);
357
358 /* send it down the pipe */
359 status = usb_submit_urb(urb, GFP_ATOMIC);
360 if (status) {
361 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
362 "with status = %d\n", __FUNCTION__, status);
363 count = status;
364 goto error;
365 }
366
367 /* we are done with this urb, so let the host driver
368 * really free it when it is finished with it */
369 usb_free_urb(urb);
370
371 return count;
372error:
373 usb_free_urb(urb);
374error_no_urb:
375 kfree(buffer);
376error_no_buffer:
377 spin_lock_irqsave(&portdata->lock, flags);
378 --portdata->outstanding_urbs;
379 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700380 return count;
381}
382
383static void sierra_indat_callback(struct urb *urb)
384{
385 int err;
386 int endpoint;
387 struct usb_serial_port *port;
388 struct tty_struct *tty;
389 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700390 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700391
392 dbg("%s: %p", __FUNCTION__, urb);
393
394 endpoint = usb_pipeendpoint(urb->pipe);
395 port = (struct usb_serial_port *) urb->context;
396
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700397 if (status) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700398 dbg("%s: nonzero status: %d on endpoint %02x.",
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700399 __FUNCTION__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700400 } else {
401 tty = port->tty;
402 if (urb->actual_length) {
403 tty_buffer_request_room(tty, urb->actual_length);
404 tty_insert_flip_string(tty, data, urb->actual_length);
405 tty_flip_buffer_push(tty);
406 } else {
407 dbg("%s: empty read urb received", __FUNCTION__);
408 }
409
410 /* Resubmit urb so we continue receiving */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700411 if (port->open_count && status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700412 err = usb_submit_urb(urb, GFP_ATOMIC);
413 if (err)
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900414 dev_err(&port->dev, "resubmit read urb failed."
Joe Perches898eb712007-10-18 03:06:30 -0700415 "(%d)\n", err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700416 }
417 }
418 return;
419}
420
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700421static void sierra_instat_callback(struct urb *urb)
422{
423 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700424 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700425 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
426 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
427 struct usb_serial *serial = port->serial;
428
429 dbg("%s", __FUNCTION__);
430 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
431
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700432 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700433 struct usb_ctrlrequest *req_pkt =
434 (struct usb_ctrlrequest *)urb->transfer_buffer;
435
436 if (!req_pkt) {
437 dbg("%s: NULL req_pkt\n", __FUNCTION__);
438 return;
439 }
440 if ((req_pkt->bRequestType == 0xA1) &&
441 (req_pkt->bRequest == 0x20)) {
442 int old_dcd_state;
443 unsigned char signals = *((unsigned char *)
444 urb->transfer_buffer +
445 sizeof(struct usb_ctrlrequest));
446
447 dbg("%s: signal x%x", __FUNCTION__, signals);
448
449 old_dcd_state = portdata->dcd_state;
450 portdata->cts_state = 1;
451 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
452 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
453 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
454
455 if (port->tty && !C_CLOCAL(port->tty) &&
456 old_dcd_state && !portdata->dcd_state)
457 tty_hangup(port->tty);
458 } else {
459 dbg("%s: type %x req %x", __FUNCTION__,
460 req_pkt->bRequestType,req_pkt->bRequest);
461 }
462 } else
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700463 dbg("%s: error %d", __FUNCTION__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700464
465 /* Resubmit urb so we continue receiving IRQ data */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700466 if (status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700467 urb->dev = serial->dev;
468 err = usb_submit_urb(urb, GFP_ATOMIC);
469 if (err)
470 dbg("%s: resubmit intr urb failed. (%d)",
471 __FUNCTION__, err);
472 }
473}
474
475static int sierra_write_room(struct usb_serial_port *port)
476{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900477 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
478 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700479
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900480 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700481
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900482 /* try to give a good number back based on if we have any free urbs at
483 * this point in time */
484 spin_lock_irqsave(&portdata->lock, flags);
485 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
486 spin_unlock_irqrestore(&portdata->lock, flags);
487 dbg("%s - write limit hit\n", __FUNCTION__);
488 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700489 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900490 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700491
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900492 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700493}
494
495static int sierra_chars_in_buffer(struct usb_serial_port *port)
496{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900497 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700498
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900499 /*
500 * We can't really account for how much data we
501 * have sent out, but hasn't made it through to the
502 * device as we can't see the backend here, so just
503 * tell the tty layer that everything is flushed.
504 */
505 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700506}
507
508static int sierra_open(struct usb_serial_port *port, struct file *filp)
509{
510 struct sierra_port_private *portdata;
511 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900512 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700513 struct urb *urb;
Kevin Lloyde43062d2007-01-17 16:04:18 -0800514 int result;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700515
516 portdata = usb_get_serial_port_data(port);
517
518 dbg("%s", __FUNCTION__);
519
520 /* Set some sane defaults */
521 portdata->rts_state = 1;
522 portdata->dtr_state = 1;
523
524 /* Reset low level data toggle and start reading from endpoints */
525 for (i = 0; i < N_IN_URB; i++) {
526 urb = portdata->in_urbs[i];
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900527 if (!urb)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700528 continue;
529 if (urb->dev != serial->dev) {
530 dbg("%s: dev %p != %p", __FUNCTION__,
531 urb->dev, serial->dev);
532 continue;
533 }
534
535 /*
536 * make sure endpoint data toggle is synchronized with the
537 * device
538 */
539 usb_clear_halt(urb->dev, urb->pipe);
540
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900541 result = usb_submit_urb(urb, GFP_KERNEL);
542 if (result) {
Joe Perches898eb712007-10-18 03:06:30 -0700543 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900544 i, result, urb->transfer_buffer_length);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700545 }
546 }
547
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700548 port->tty->low_latency = 1;
549
550 sierra_send_setup(port);
551
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900552 /* start up the interrupt endpoint if we have one */
553 if (port->interrupt_in_urb) {
554 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
555 if (result)
Joe Perches898eb712007-10-18 03:06:30 -0700556 dev_err(&port->dev, "submit irq_in urb failed %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900557 result);
558 }
559 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700560}
561
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700562static void sierra_close(struct usb_serial_port *port, struct file *filp)
563{
564 int i;
565 struct usb_serial *serial = port->serial;
566 struct sierra_port_private *portdata;
567
568 dbg("%s", __FUNCTION__);
569 portdata = usb_get_serial_port_data(port);
570
571 portdata->rts_state = 0;
572 portdata->dtr_state = 0;
573
574 if (serial->dev) {
575 sierra_send_setup(port);
576
577 /* Stop reading/writing urbs */
578 for (i = 0; i < N_IN_URB; i++)
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900579 usb_kill_urb(portdata->in_urbs[i]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700580 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900581
582 usb_kill_urb(port->interrupt_in_urb);
583
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700584 port->tty = NULL;
585}
586
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700587static int sierra_startup(struct usb_serial *serial)
588{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700589 struct usb_serial_port *port;
590 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900591 struct urb *urb;
592 int i;
593 int j;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700594
595 dbg("%s", __FUNCTION__);
596
Kevin Lloyd112225b2007-07-16 13:49:27 -0700597 /*Set Device mode to D0 */
598 sierra_set_power_state(serial->dev, 0x0000);
599
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700600 /* Now setup per port private data */
601 for (i = 0; i < serial->num_ports; i++) {
602 port = serial->port[i];
603 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
604 if (!portdata) {
605 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
606 __FUNCTION__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900607 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700608 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900609 spin_lock_init(&portdata->lock);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700610
611 usb_set_serial_port_data(port, portdata);
612
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900613 /* initialize the in urbs */
614 for (j = 0; j < N_IN_URB; ++j) {
615 urb = usb_alloc_urb(0, GFP_KERNEL);
616 if (urb == NULL) {
617 dbg("%s: alloc for in port failed.",
618 __FUNCTION__);
619 continue;
620 }
621 /* Fill URB using supplied data. */
622 usb_fill_bulk_urb(urb, serial->dev,
623 usb_rcvbulkpipe(serial->dev,
624 port->bulk_in_endpointAddress),
625 portdata->in_buffer[j], IN_BUFLEN,
626 sierra_indat_callback, port);
627 portdata->in_urbs[j] = urb;
628 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700629 }
630
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900631 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700632}
633
634static void sierra_shutdown(struct usb_serial *serial)
635{
636 int i, j;
637 struct usb_serial_port *port;
638 struct sierra_port_private *portdata;
639
640 dbg("%s", __FUNCTION__);
641
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700642 for (i = 0; i < serial->num_ports; ++i) {
643 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700644 if (!port)
645 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700646 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700647 if (!portdata)
648 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700649
650 for (j = 0; j < N_IN_URB; j++) {
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900651 usb_kill_urb(portdata->in_urbs[j]);
652 usb_free_urb(portdata->in_urbs[j]);
653 portdata->in_urbs[j] = NULL;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700654 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900655 kfree(portdata);
656 usb_set_serial_port_data(port, NULL);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700657 }
658}
659
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700660static struct usb_serial_driver sierra_1port_device = {
661 .driver = {
662 .owner = THIS_MODULE,
663 .name = "sierra1",
664 },
665 .description = "Sierra USB modem (1 port)",
666 .id_table = id_table_1port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100667 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700668 .num_interrupt_in = NUM_DONT_CARE,
669 .num_bulk_in = 1,
670 .num_bulk_out = 1,
671 .num_ports = 1,
672 .open = sierra_open,
673 .close = sierra_close,
674 .write = sierra_write,
675 .write_room = sierra_write_room,
676 .chars_in_buffer = sierra_chars_in_buffer,
677 .throttle = sierra_rx_throttle,
678 .unthrottle = sierra_rx_unthrottle,
679 .ioctl = sierra_ioctl,
680 .set_termios = sierra_set_termios,
681 .break_ctl = sierra_break_ctl,
682 .tiocmget = sierra_tiocmget,
683 .tiocmset = sierra_tiocmset,
684 .attach = sierra_startup,
685 .shutdown = sierra_shutdown,
686 .read_int_callback = sierra_instat_callback,
687};
688
689static struct usb_serial_driver sierra_3port_device = {
690 .driver = {
691 .owner = THIS_MODULE,
692 .name = "sierra3",
693 },
694 .description = "Sierra USB modem (3 port)",
695 .id_table = id_table_3port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100696 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700697 .num_interrupt_in = NUM_DONT_CARE,
698 .num_bulk_in = 3,
699 .num_bulk_out = 3,
700 .num_ports = 3,
701 .open = sierra_open,
702 .close = sierra_close,
703 .write = sierra_write,
704 .write_room = sierra_write_room,
705 .chars_in_buffer = sierra_chars_in_buffer,
706 .throttle = sierra_rx_throttle,
707 .unthrottle = sierra_rx_unthrottle,
708 .ioctl = sierra_ioctl,
709 .set_termios = sierra_set_termios,
710 .break_ctl = sierra_break_ctl,
711 .tiocmget = sierra_tiocmget,
712 .tiocmset = sierra_tiocmset,
713 .attach = sierra_startup,
714 .shutdown = sierra_shutdown,
715 .read_int_callback = sierra_instat_callback,
716};
717
718/* Functions used by new usb-serial code. */
719static int __init sierra_init(void)
720{
721 int retval;
722 retval = usb_serial_register(&sierra_1port_device);
723 if (retval)
724 goto failed_1port_device_register;
725 retval = usb_serial_register(&sierra_3port_device);
726 if (retval)
727 goto failed_3port_device_register;
728
729
730 retval = usb_register(&sierra_driver);
731 if (retval)
732 goto failed_driver_register;
733
734 info(DRIVER_DESC ": " DRIVER_VERSION);
735
736 return 0;
737
738failed_driver_register:
739 usb_serial_deregister(&sierra_3port_device);
740failed_3port_device_register:
741 usb_serial_deregister(&sierra_1port_device);
742failed_1port_device_register:
743 return retval;
744}
745
746static void __exit sierra_exit(void)
747{
748 usb_deregister (&sierra_driver);
749 usb_serial_deregister(&sierra_1port_device);
750 usb_serial_deregister(&sierra_3port_device);
751}
752
753module_init(sierra_init);
754module_exit(sierra_exit);
755
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700756MODULE_AUTHOR(DRIVER_AUTHOR);
757MODULE_DESCRIPTION(DRIVER_DESC);
758MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700759MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700760
761#ifdef CONFIG_USB_DEBUG
762module_param(debug, bool, S_IRUGO | S_IWUSR);
763MODULE_PARM_DESC(debug, "Debug messages");
764#endif
765