blob: e5c274044a5fdd9accf3d2a4225e089565d56d57 [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 */
107
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700108 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800109 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700110 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700111 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700112 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700113 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
114 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
115 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
116 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
117 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
118 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700119
Kevin Lloyd112225b2007-07-16 13:49:27 -0700120 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
121 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
122
123 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700124 { }
125};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700126MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700127
128static struct usb_device_id id_table_1port [] = {
129 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
130 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
131 { }
132};
133
134static struct usb_device_id id_table_3port [] = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700135 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800136 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700137 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700138 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800139 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.com5fdcd032007-11-20 13:39:03 -0700140 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800141 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
142 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700143 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U*/
Kevin Lloyd112225b2007-07-16 13:49:27 -0700144
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700145 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800146 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700147 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700148 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700149 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700150 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
151 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
152 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
153 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
154 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
155 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700156 { }
157};
158
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700159static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700160 .name = "sierra",
Kevin Lloyd112225b2007-07-16 13:49:27 -0700161 .probe = sierra_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700162 .disconnect = usb_serial_disconnect,
163 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700164 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700165};
166
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700167
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700168struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900169 spinlock_t lock; /* lock the structure */
170 int outstanding_urbs; /* number of out urbs in flight */
171
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700172 /* Input endpoints and buffer for this port */
173 struct urb *in_urbs[N_IN_URB];
174 char in_buffer[N_IN_URB][IN_BUFLEN];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700175
176 /* Settings for the port */
177 int rts_state; /* Handshaking pins (outputs) */
178 int dtr_state;
179 int cts_state; /* Handshaking pins (inputs) */
180 int dsr_state;
181 int dcd_state;
182 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700183};
184
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700185static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700186{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700187 struct usb_serial *serial = port->serial;
188 struct sierra_port_private *portdata;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700189
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700190 dbg("%s", __FUNCTION__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700191
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700192 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700193
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700194 if (port->tty) {
195 int val = 0;
196 if (portdata->dtr_state)
197 val |= 0x01;
198 if (portdata->rts_state)
199 val |= 0x02;
200
201 return usb_control_msg(serial->dev,
202 usb_rcvctrlpipe(serial->dev, 0),
203 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
204 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700205
206 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700207}
208
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700209static void sierra_rx_throttle(struct usb_serial_port *port)
210{
211 dbg("%s", __FUNCTION__);
212}
213
214static void sierra_rx_unthrottle(struct usb_serial_port *port)
215{
216 dbg("%s", __FUNCTION__);
217}
218
219static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
220{
221 /* Unfortunately, I don't know how to send a break */
222 dbg("%s", __FUNCTION__);
223}
224
225static void sierra_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800226 struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700227{
228 dbg("%s", __FUNCTION__);
Alan Coxed1f12e2007-10-18 01:24:22 -0700229 tty_termios_copy_hw(port->tty->termios, old_termios);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700230 sierra_send_setup(port);
231}
232
233static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
234{
235 unsigned int value;
236 struct sierra_port_private *portdata;
237
238 portdata = usb_get_serial_port_data(port);
239
240 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
241 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
242 ((portdata->cts_state) ? TIOCM_CTS : 0) |
243 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
244 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
245 ((portdata->ri_state) ? TIOCM_RNG : 0);
246
247 return value;
248}
249
250static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
251 unsigned int set, unsigned int clear)
252{
253 struct sierra_port_private *portdata;
254
255 portdata = usb_get_serial_port_data(port);
256
257 if (set & TIOCM_RTS)
258 portdata->rts_state = 1;
259 if (set & TIOCM_DTR)
260 portdata->dtr_state = 1;
261
262 if (clear & TIOCM_RTS)
263 portdata->rts_state = 0;
264 if (clear & TIOCM_DTR)
265 portdata->dtr_state = 0;
266 return sierra_send_setup(port);
267}
268
269static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
270 unsigned int cmd, unsigned long arg)
271{
272 return -ENOIOCTLCMD;
273}
274
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900275static void sierra_outdat_callback(struct urb *urb)
276{
277 struct usb_serial_port *port = urb->context;
278 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
279 int status = urb->status;
280 unsigned long flags;
281
282 dbg("%s - port %d", __FUNCTION__, port->number);
283
284 /* free up the transfer buffer, as usb_free_urb() does not do this */
285 kfree(urb->transfer_buffer);
286
287 if (status)
288 dbg("%s - nonzero write bulk status received: %d",
289 __FUNCTION__, status);
290
291 spin_lock_irqsave(&portdata->lock, flags);
292 --portdata->outstanding_urbs;
293 spin_unlock_irqrestore(&portdata->lock, flags);
294
295 usb_serial_port_softint(port);
296}
297
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700298/* Write */
299static int sierra_write(struct usb_serial_port *port,
300 const unsigned char *buf, int count)
301{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900302 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
303 struct usb_serial *serial = port->serial;
304 unsigned long flags;
305 unsigned char *buffer;
306 struct urb *urb;
307 int status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700308
309 portdata = usb_get_serial_port_data(port);
310
311 dbg("%s: write (%d chars)", __FUNCTION__, count);
312
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900313 spin_lock_irqsave(&portdata->lock, flags);
314 if (portdata->outstanding_urbs > N_OUT_URB) {
315 spin_unlock_irqrestore(&portdata->lock, flags);
316 dbg("%s - write limit hit\n", __FUNCTION__);
317 return 0;
318 }
319 portdata->outstanding_urbs++;
320 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700321
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900322 buffer = kmalloc(count, GFP_ATOMIC);
323 if (!buffer) {
324 dev_err(&port->dev, "out of memory\n");
325 count = -ENOMEM;
326 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700327 }
328
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900329 urb = usb_alloc_urb(0, GFP_ATOMIC);
330 if (!urb) {
331 dev_err(&port->dev, "no more free urbs\n");
332 count = -ENOMEM;
333 goto error_no_urb;
334 }
335
336 memcpy(buffer, buf, count);
337
338 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
339
340 usb_fill_bulk_urb(urb, serial->dev,
341 usb_sndbulkpipe(serial->dev,
342 port->bulk_out_endpointAddress),
343 buffer, count, sierra_outdat_callback, port);
344
345 /* send it down the pipe */
346 status = usb_submit_urb(urb, GFP_ATOMIC);
347 if (status) {
348 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
349 "with status = %d\n", __FUNCTION__, status);
350 count = status;
351 goto error;
352 }
353
354 /* we are done with this urb, so let the host driver
355 * really free it when it is finished with it */
356 usb_free_urb(urb);
357
358 return count;
359error:
360 usb_free_urb(urb);
361error_no_urb:
362 kfree(buffer);
363error_no_buffer:
364 spin_lock_irqsave(&portdata->lock, flags);
365 --portdata->outstanding_urbs;
366 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700367 return count;
368}
369
370static void sierra_indat_callback(struct urb *urb)
371{
372 int err;
373 int endpoint;
374 struct usb_serial_port *port;
375 struct tty_struct *tty;
376 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700377 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700378
379 dbg("%s: %p", __FUNCTION__, urb);
380
381 endpoint = usb_pipeendpoint(urb->pipe);
382 port = (struct usb_serial_port *) urb->context;
383
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700384 if (status) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700385 dbg("%s: nonzero status: %d on endpoint %02x.",
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700386 __FUNCTION__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700387 } else {
388 tty = port->tty;
389 if (urb->actual_length) {
390 tty_buffer_request_room(tty, urb->actual_length);
391 tty_insert_flip_string(tty, data, urb->actual_length);
392 tty_flip_buffer_push(tty);
393 } else {
394 dbg("%s: empty read urb received", __FUNCTION__);
395 }
396
397 /* Resubmit urb so we continue receiving */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700398 if (port->open_count && status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700399 err = usb_submit_urb(urb, GFP_ATOMIC);
400 if (err)
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900401 dev_err(&port->dev, "resubmit read urb failed."
Joe Perches898eb712007-10-18 03:06:30 -0700402 "(%d)\n", err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700403 }
404 }
405 return;
406}
407
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700408static void sierra_instat_callback(struct urb *urb)
409{
410 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700411 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700412 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
413 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
414 struct usb_serial *serial = port->serial;
415
416 dbg("%s", __FUNCTION__);
417 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
418
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700419 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700420 struct usb_ctrlrequest *req_pkt =
421 (struct usb_ctrlrequest *)urb->transfer_buffer;
422
423 if (!req_pkt) {
424 dbg("%s: NULL req_pkt\n", __FUNCTION__);
425 return;
426 }
427 if ((req_pkt->bRequestType == 0xA1) &&
428 (req_pkt->bRequest == 0x20)) {
429 int old_dcd_state;
430 unsigned char signals = *((unsigned char *)
431 urb->transfer_buffer +
432 sizeof(struct usb_ctrlrequest));
433
434 dbg("%s: signal x%x", __FUNCTION__, signals);
435
436 old_dcd_state = portdata->dcd_state;
437 portdata->cts_state = 1;
438 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
439 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
440 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
441
442 if (port->tty && !C_CLOCAL(port->tty) &&
443 old_dcd_state && !portdata->dcd_state)
444 tty_hangup(port->tty);
445 } else {
446 dbg("%s: type %x req %x", __FUNCTION__,
447 req_pkt->bRequestType,req_pkt->bRequest);
448 }
449 } else
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700450 dbg("%s: error %d", __FUNCTION__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700451
452 /* Resubmit urb so we continue receiving IRQ data */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700453 if (status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700454 urb->dev = serial->dev;
455 err = usb_submit_urb(urb, GFP_ATOMIC);
456 if (err)
457 dbg("%s: resubmit intr urb failed. (%d)",
458 __FUNCTION__, err);
459 }
460}
461
462static int sierra_write_room(struct usb_serial_port *port)
463{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900464 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
465 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700466
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900467 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700468
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900469 /* try to give a good number back based on if we have any free urbs at
470 * this point in time */
471 spin_lock_irqsave(&portdata->lock, flags);
472 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
473 spin_unlock_irqrestore(&portdata->lock, flags);
474 dbg("%s - write limit hit\n", __FUNCTION__);
475 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700476 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900477 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700478
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900479 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700480}
481
482static int sierra_chars_in_buffer(struct usb_serial_port *port)
483{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900484 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700485
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900486 /*
487 * We can't really account for how much data we
488 * have sent out, but hasn't made it through to the
489 * device as we can't see the backend here, so just
490 * tell the tty layer that everything is flushed.
491 */
492 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700493}
494
495static int sierra_open(struct usb_serial_port *port, struct file *filp)
496{
497 struct sierra_port_private *portdata;
498 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900499 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700500 struct urb *urb;
Kevin Lloyde43062d2007-01-17 16:04:18 -0800501 int result;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700502
503 portdata = usb_get_serial_port_data(port);
504
505 dbg("%s", __FUNCTION__);
506
507 /* Set some sane defaults */
508 portdata->rts_state = 1;
509 portdata->dtr_state = 1;
510
511 /* Reset low level data toggle and start reading from endpoints */
512 for (i = 0; i < N_IN_URB; i++) {
513 urb = portdata->in_urbs[i];
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900514 if (!urb)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700515 continue;
516 if (urb->dev != serial->dev) {
517 dbg("%s: dev %p != %p", __FUNCTION__,
518 urb->dev, serial->dev);
519 continue;
520 }
521
522 /*
523 * make sure endpoint data toggle is synchronized with the
524 * device
525 */
526 usb_clear_halt(urb->dev, urb->pipe);
527
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900528 result = usb_submit_urb(urb, GFP_KERNEL);
529 if (result) {
Joe Perches898eb712007-10-18 03:06:30 -0700530 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900531 i, result, urb->transfer_buffer_length);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700532 }
533 }
534
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700535 port->tty->low_latency = 1;
536
537 sierra_send_setup(port);
538
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900539 /* start up the interrupt endpoint if we have one */
540 if (port->interrupt_in_urb) {
541 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
542 if (result)
Joe Perches898eb712007-10-18 03:06:30 -0700543 dev_err(&port->dev, "submit irq_in urb failed %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900544 result);
545 }
546 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700547}
548
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700549static void sierra_close(struct usb_serial_port *port, struct file *filp)
550{
551 int i;
552 struct usb_serial *serial = port->serial;
553 struct sierra_port_private *portdata;
554
555 dbg("%s", __FUNCTION__);
556 portdata = usb_get_serial_port_data(port);
557
558 portdata->rts_state = 0;
559 portdata->dtr_state = 0;
560
561 if (serial->dev) {
562 sierra_send_setup(port);
563
564 /* Stop reading/writing urbs */
565 for (i = 0; i < N_IN_URB; i++)
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900566 usb_kill_urb(portdata->in_urbs[i]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700567 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900568
569 usb_kill_urb(port->interrupt_in_urb);
570
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700571 port->tty = NULL;
572}
573
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700574static int sierra_startup(struct usb_serial *serial)
575{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700576 struct usb_serial_port *port;
577 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900578 struct urb *urb;
579 int i;
580 int j;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700581
582 dbg("%s", __FUNCTION__);
583
Kevin Lloyd112225b2007-07-16 13:49:27 -0700584 /*Set Device mode to D0 */
585 sierra_set_power_state(serial->dev, 0x0000);
586
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700587 /* Now setup per port private data */
588 for (i = 0; i < serial->num_ports; i++) {
589 port = serial->port[i];
590 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
591 if (!portdata) {
592 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
593 __FUNCTION__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900594 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700595 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900596 spin_lock_init(&portdata->lock);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700597
598 usb_set_serial_port_data(port, portdata);
599
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900600 /* initialize the in urbs */
601 for (j = 0; j < N_IN_URB; ++j) {
602 urb = usb_alloc_urb(0, GFP_KERNEL);
603 if (urb == NULL) {
604 dbg("%s: alloc for in port failed.",
605 __FUNCTION__);
606 continue;
607 }
608 /* Fill URB using supplied data. */
609 usb_fill_bulk_urb(urb, serial->dev,
610 usb_rcvbulkpipe(serial->dev,
611 port->bulk_in_endpointAddress),
612 portdata->in_buffer[j], IN_BUFLEN,
613 sierra_indat_callback, port);
614 portdata->in_urbs[j] = urb;
615 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700616 }
617
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900618 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700619}
620
621static void sierra_shutdown(struct usb_serial *serial)
622{
623 int i, j;
624 struct usb_serial_port *port;
625 struct sierra_port_private *portdata;
626
627 dbg("%s", __FUNCTION__);
628
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700629 for (i = 0; i < serial->num_ports; ++i) {
630 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700631 if (!port)
632 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700633 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700634 if (!portdata)
635 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700636
637 for (j = 0; j < N_IN_URB; j++) {
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900638 usb_kill_urb(portdata->in_urbs[j]);
639 usb_free_urb(portdata->in_urbs[j]);
640 portdata->in_urbs[j] = NULL;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700641 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900642 kfree(portdata);
643 usb_set_serial_port_data(port, NULL);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700644 }
645}
646
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700647static struct usb_serial_driver sierra_1port_device = {
648 .driver = {
649 .owner = THIS_MODULE,
650 .name = "sierra1",
651 },
652 .description = "Sierra USB modem (1 port)",
653 .id_table = id_table_1port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100654 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700655 .num_interrupt_in = NUM_DONT_CARE,
656 .num_bulk_in = 1,
657 .num_bulk_out = 1,
658 .num_ports = 1,
659 .open = sierra_open,
660 .close = sierra_close,
661 .write = sierra_write,
662 .write_room = sierra_write_room,
663 .chars_in_buffer = sierra_chars_in_buffer,
664 .throttle = sierra_rx_throttle,
665 .unthrottle = sierra_rx_unthrottle,
666 .ioctl = sierra_ioctl,
667 .set_termios = sierra_set_termios,
668 .break_ctl = sierra_break_ctl,
669 .tiocmget = sierra_tiocmget,
670 .tiocmset = sierra_tiocmset,
671 .attach = sierra_startup,
672 .shutdown = sierra_shutdown,
673 .read_int_callback = sierra_instat_callback,
674};
675
676static struct usb_serial_driver sierra_3port_device = {
677 .driver = {
678 .owner = THIS_MODULE,
679 .name = "sierra3",
680 },
681 .description = "Sierra USB modem (3 port)",
682 .id_table = id_table_3port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100683 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700684 .num_interrupt_in = NUM_DONT_CARE,
685 .num_bulk_in = 3,
686 .num_bulk_out = 3,
687 .num_ports = 3,
688 .open = sierra_open,
689 .close = sierra_close,
690 .write = sierra_write,
691 .write_room = sierra_write_room,
692 .chars_in_buffer = sierra_chars_in_buffer,
693 .throttle = sierra_rx_throttle,
694 .unthrottle = sierra_rx_unthrottle,
695 .ioctl = sierra_ioctl,
696 .set_termios = sierra_set_termios,
697 .break_ctl = sierra_break_ctl,
698 .tiocmget = sierra_tiocmget,
699 .tiocmset = sierra_tiocmset,
700 .attach = sierra_startup,
701 .shutdown = sierra_shutdown,
702 .read_int_callback = sierra_instat_callback,
703};
704
705/* Functions used by new usb-serial code. */
706static int __init sierra_init(void)
707{
708 int retval;
709 retval = usb_serial_register(&sierra_1port_device);
710 if (retval)
711 goto failed_1port_device_register;
712 retval = usb_serial_register(&sierra_3port_device);
713 if (retval)
714 goto failed_3port_device_register;
715
716
717 retval = usb_register(&sierra_driver);
718 if (retval)
719 goto failed_driver_register;
720
721 info(DRIVER_DESC ": " DRIVER_VERSION);
722
723 return 0;
724
725failed_driver_register:
726 usb_serial_deregister(&sierra_3port_device);
727failed_3port_device_register:
728 usb_serial_deregister(&sierra_1port_device);
729failed_1port_device_register:
730 return retval;
731}
732
733static void __exit sierra_exit(void)
734{
735 usb_deregister (&sierra_driver);
736 usb_serial_deregister(&sierra_1port_device);
737 usb_serial_deregister(&sierra_3port_device);
738}
739
740module_init(sierra_init);
741module_exit(sierra_exit);
742
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700743MODULE_AUTHOR(DRIVER_AUTHOR);
744MODULE_DESCRIPTION(DRIVER_DESC);
745MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700746MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700747
748#ifdef CONFIG_USB_DEBUG
749module_param(debug, bool, S_IRUGO | S_IWUSR);
750MODULE_PARM_DESC(debug, "Debug messages");
751#endif
752