blob: 7aea2de4161eda36e167bf9b5ead84517c1a36f6 [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 R Pageed0ccdb2007-12-13 01:10:48 +0000112 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700113 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700114 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
115 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
116 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
117 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
118 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
119 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600120 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700121
Kevin Lloyd112225b2007-07-16 13:49:27 -0700122 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
123 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
124
125 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700126 { }
127};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700128MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700129
130static struct usb_device_id id_table_1port [] = {
131 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
132 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
133 { }
134};
135
136static struct usb_device_id id_table_3port [] = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700137 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800138 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700139 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700140 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800141 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.com5fdcd032007-11-20 13:39:03 -0700142 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800143 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
144 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700145 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U*/
Kevin Lloyd112225b2007-07-16 13:49:27 -0700146
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700147 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800148 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700149 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700150 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin R Pageed0ccdb2007-12-13 01:10:48 +0000151 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700152 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700153 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
154 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
155 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
156 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
157 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
158 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600159 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881U */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700160 { }
161};
162
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700163static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700164 .name = "sierra",
Kevin Lloyd112225b2007-07-16 13:49:27 -0700165 .probe = sierra_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700166 .disconnect = usb_serial_disconnect,
167 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700168 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700169};
170
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700171
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700172struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900173 spinlock_t lock; /* lock the structure */
174 int outstanding_urbs; /* number of out urbs in flight */
175
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700176 /* Input endpoints and buffer for this port */
177 struct urb *in_urbs[N_IN_URB];
178 char in_buffer[N_IN_URB][IN_BUFLEN];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700179
180 /* Settings for the port */
181 int rts_state; /* Handshaking pins (outputs) */
182 int dtr_state;
183 int cts_state; /* Handshaking pins (inputs) */
184 int dsr_state;
185 int dcd_state;
186 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700187};
188
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700189static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700190{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700191 struct usb_serial *serial = port->serial;
192 struct sierra_port_private *portdata;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700193
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700194 dbg("%s", __FUNCTION__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700195
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700196 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700197
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700198 if (port->tty) {
199 int val = 0;
200 if (portdata->dtr_state)
201 val |= 0x01;
202 if (portdata->rts_state)
203 val |= 0x02;
204
205 return usb_control_msg(serial->dev,
206 usb_rcvctrlpipe(serial->dev, 0),
207 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
208 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700209
210 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700211}
212
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700213static void sierra_rx_throttle(struct usb_serial_port *port)
214{
215 dbg("%s", __FUNCTION__);
216}
217
218static void sierra_rx_unthrottle(struct usb_serial_port *port)
219{
220 dbg("%s", __FUNCTION__);
221}
222
223static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
224{
225 /* Unfortunately, I don't know how to send a break */
226 dbg("%s", __FUNCTION__);
227}
228
229static void sierra_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800230 struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700231{
232 dbg("%s", __FUNCTION__);
Alan Coxed1f12e2007-10-18 01:24:22 -0700233 tty_termios_copy_hw(port->tty->termios, old_termios);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700234 sierra_send_setup(port);
235}
236
237static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
238{
239 unsigned int value;
240 struct sierra_port_private *portdata;
241
242 portdata = usb_get_serial_port_data(port);
243
244 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
245 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
246 ((portdata->cts_state) ? TIOCM_CTS : 0) |
247 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
248 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
249 ((portdata->ri_state) ? TIOCM_RNG : 0);
250
251 return value;
252}
253
254static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
255 unsigned int set, unsigned int clear)
256{
257 struct sierra_port_private *portdata;
258
259 portdata = usb_get_serial_port_data(port);
260
261 if (set & TIOCM_RTS)
262 portdata->rts_state = 1;
263 if (set & TIOCM_DTR)
264 portdata->dtr_state = 1;
265
266 if (clear & TIOCM_RTS)
267 portdata->rts_state = 0;
268 if (clear & TIOCM_DTR)
269 portdata->dtr_state = 0;
270 return sierra_send_setup(port);
271}
272
273static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
274 unsigned int cmd, unsigned long arg)
275{
276 return -ENOIOCTLCMD;
277}
278
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900279static void sierra_outdat_callback(struct urb *urb)
280{
281 struct usb_serial_port *port = urb->context;
282 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
283 int status = urb->status;
284 unsigned long flags;
285
286 dbg("%s - port %d", __FUNCTION__, port->number);
287
288 /* free up the transfer buffer, as usb_free_urb() does not do this */
289 kfree(urb->transfer_buffer);
290
291 if (status)
292 dbg("%s - nonzero write bulk status received: %d",
293 __FUNCTION__, status);
294
295 spin_lock_irqsave(&portdata->lock, flags);
296 --portdata->outstanding_urbs;
297 spin_unlock_irqrestore(&portdata->lock, flags);
298
299 usb_serial_port_softint(port);
300}
301
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700302/* Write */
303static int sierra_write(struct usb_serial_port *port,
304 const unsigned char *buf, int count)
305{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900306 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
307 struct usb_serial *serial = port->serial;
308 unsigned long flags;
309 unsigned char *buffer;
310 struct urb *urb;
311 int status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700312
313 portdata = usb_get_serial_port_data(port);
314
315 dbg("%s: write (%d chars)", __FUNCTION__, count);
316
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900317 spin_lock_irqsave(&portdata->lock, flags);
318 if (portdata->outstanding_urbs > N_OUT_URB) {
319 spin_unlock_irqrestore(&portdata->lock, flags);
320 dbg("%s - write limit hit\n", __FUNCTION__);
321 return 0;
322 }
323 portdata->outstanding_urbs++;
324 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700325
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900326 buffer = kmalloc(count, GFP_ATOMIC);
327 if (!buffer) {
328 dev_err(&port->dev, "out of memory\n");
329 count = -ENOMEM;
330 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700331 }
332
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900333 urb = usb_alloc_urb(0, GFP_ATOMIC);
334 if (!urb) {
335 dev_err(&port->dev, "no more free urbs\n");
336 count = -ENOMEM;
337 goto error_no_urb;
338 }
339
340 memcpy(buffer, buf, count);
341
342 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
343
344 usb_fill_bulk_urb(urb, serial->dev,
345 usb_sndbulkpipe(serial->dev,
346 port->bulk_out_endpointAddress),
347 buffer, count, sierra_outdat_callback, port);
348
349 /* send it down the pipe */
350 status = usb_submit_urb(urb, GFP_ATOMIC);
351 if (status) {
352 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
353 "with status = %d\n", __FUNCTION__, status);
354 count = status;
355 goto error;
356 }
357
358 /* we are done with this urb, so let the host driver
359 * really free it when it is finished with it */
360 usb_free_urb(urb);
361
362 return count;
363error:
364 usb_free_urb(urb);
365error_no_urb:
366 kfree(buffer);
367error_no_buffer:
368 spin_lock_irqsave(&portdata->lock, flags);
369 --portdata->outstanding_urbs;
370 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700371 return count;
372}
373
374static void sierra_indat_callback(struct urb *urb)
375{
376 int err;
377 int endpoint;
378 struct usb_serial_port *port;
379 struct tty_struct *tty;
380 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700381 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700382
383 dbg("%s: %p", __FUNCTION__, urb);
384
385 endpoint = usb_pipeendpoint(urb->pipe);
386 port = (struct usb_serial_port *) urb->context;
387
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700388 if (status) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700389 dbg("%s: nonzero status: %d on endpoint %02x.",
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700390 __FUNCTION__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700391 } else {
392 tty = port->tty;
393 if (urb->actual_length) {
394 tty_buffer_request_room(tty, urb->actual_length);
395 tty_insert_flip_string(tty, data, urb->actual_length);
396 tty_flip_buffer_push(tty);
397 } else {
398 dbg("%s: empty read urb received", __FUNCTION__);
399 }
400
401 /* Resubmit urb so we continue receiving */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700402 if (port->open_count && status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700403 err = usb_submit_urb(urb, GFP_ATOMIC);
404 if (err)
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900405 dev_err(&port->dev, "resubmit read urb failed."
Joe Perches898eb712007-10-18 03:06:30 -0700406 "(%d)\n", err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700407 }
408 }
409 return;
410}
411
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700412static void sierra_instat_callback(struct urb *urb)
413{
414 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700415 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700416 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
417 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
418 struct usb_serial *serial = port->serial;
419
420 dbg("%s", __FUNCTION__);
421 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
422
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700423 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700424 struct usb_ctrlrequest *req_pkt =
425 (struct usb_ctrlrequest *)urb->transfer_buffer;
426
427 if (!req_pkt) {
428 dbg("%s: NULL req_pkt\n", __FUNCTION__);
429 return;
430 }
431 if ((req_pkt->bRequestType == 0xA1) &&
432 (req_pkt->bRequest == 0x20)) {
433 int old_dcd_state;
434 unsigned char signals = *((unsigned char *)
435 urb->transfer_buffer +
436 sizeof(struct usb_ctrlrequest));
437
438 dbg("%s: signal x%x", __FUNCTION__, signals);
439
440 old_dcd_state = portdata->dcd_state;
441 portdata->cts_state = 1;
442 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
443 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
444 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
445
446 if (port->tty && !C_CLOCAL(port->tty) &&
447 old_dcd_state && !portdata->dcd_state)
448 tty_hangup(port->tty);
449 } else {
450 dbg("%s: type %x req %x", __FUNCTION__,
451 req_pkt->bRequestType,req_pkt->bRequest);
452 }
453 } else
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700454 dbg("%s: error %d", __FUNCTION__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700455
456 /* Resubmit urb so we continue receiving IRQ data */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700457 if (status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700458 urb->dev = serial->dev;
459 err = usb_submit_urb(urb, GFP_ATOMIC);
460 if (err)
461 dbg("%s: resubmit intr urb failed. (%d)",
462 __FUNCTION__, err);
463 }
464}
465
466static int sierra_write_room(struct usb_serial_port *port)
467{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900468 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
469 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700470
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900471 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700472
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900473 /* try to give a good number back based on if we have any free urbs at
474 * this point in time */
475 spin_lock_irqsave(&portdata->lock, flags);
476 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
477 spin_unlock_irqrestore(&portdata->lock, flags);
478 dbg("%s - write limit hit\n", __FUNCTION__);
479 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700480 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900481 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700482
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900483 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700484}
485
486static int sierra_chars_in_buffer(struct usb_serial_port *port)
487{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900488 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700489
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900490 /*
491 * We can't really account for how much data we
492 * have sent out, but hasn't made it through to the
493 * device as we can't see the backend here, so just
494 * tell the tty layer that everything is flushed.
495 */
496 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700497}
498
499static int sierra_open(struct usb_serial_port *port, struct file *filp)
500{
501 struct sierra_port_private *portdata;
502 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900503 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700504 struct urb *urb;
Kevin Lloyde43062d2007-01-17 16:04:18 -0800505 int result;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700506
507 portdata = usb_get_serial_port_data(port);
508
509 dbg("%s", __FUNCTION__);
510
511 /* Set some sane defaults */
512 portdata->rts_state = 1;
513 portdata->dtr_state = 1;
514
515 /* Reset low level data toggle and start reading from endpoints */
516 for (i = 0; i < N_IN_URB; i++) {
517 urb = portdata->in_urbs[i];
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900518 if (!urb)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700519 continue;
520 if (urb->dev != serial->dev) {
521 dbg("%s: dev %p != %p", __FUNCTION__,
522 urb->dev, serial->dev);
523 continue;
524 }
525
526 /*
527 * make sure endpoint data toggle is synchronized with the
528 * device
529 */
530 usb_clear_halt(urb->dev, urb->pipe);
531
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900532 result = usb_submit_urb(urb, GFP_KERNEL);
533 if (result) {
Joe Perches898eb712007-10-18 03:06:30 -0700534 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900535 i, result, urb->transfer_buffer_length);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700536 }
537 }
538
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700539 port->tty->low_latency = 1;
540
541 sierra_send_setup(port);
542
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900543 /* start up the interrupt endpoint if we have one */
544 if (port->interrupt_in_urb) {
545 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
546 if (result)
Joe Perches898eb712007-10-18 03:06:30 -0700547 dev_err(&port->dev, "submit irq_in urb failed %d\n",
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900548 result);
549 }
550 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700551}
552
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700553static void sierra_close(struct usb_serial_port *port, struct file *filp)
554{
555 int i;
556 struct usb_serial *serial = port->serial;
557 struct sierra_port_private *portdata;
558
559 dbg("%s", __FUNCTION__);
560 portdata = usb_get_serial_port_data(port);
561
562 portdata->rts_state = 0;
563 portdata->dtr_state = 0;
564
565 if (serial->dev) {
566 sierra_send_setup(port);
567
568 /* Stop reading/writing urbs */
569 for (i = 0; i < N_IN_URB; i++)
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900570 usb_kill_urb(portdata->in_urbs[i]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700571 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900572
573 usb_kill_urb(port->interrupt_in_urb);
574
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700575 port->tty = NULL;
576}
577
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700578static int sierra_startup(struct usb_serial *serial)
579{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700580 struct usb_serial_port *port;
581 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900582 struct urb *urb;
583 int i;
584 int j;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700585
586 dbg("%s", __FUNCTION__);
587
Kevin Lloyd112225b2007-07-16 13:49:27 -0700588 /*Set Device mode to D0 */
589 sierra_set_power_state(serial->dev, 0x0000);
590
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700591 /* Now setup per port private data */
592 for (i = 0; i < serial->num_ports; i++) {
593 port = serial->port[i];
594 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
595 if (!portdata) {
596 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
597 __FUNCTION__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900598 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700599 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900600 spin_lock_init(&portdata->lock);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700601
602 usb_set_serial_port_data(port, portdata);
603
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900604 /* initialize the in urbs */
605 for (j = 0; j < N_IN_URB; ++j) {
606 urb = usb_alloc_urb(0, GFP_KERNEL);
607 if (urb == NULL) {
608 dbg("%s: alloc for in port failed.",
609 __FUNCTION__);
610 continue;
611 }
612 /* Fill URB using supplied data. */
613 usb_fill_bulk_urb(urb, serial->dev,
614 usb_rcvbulkpipe(serial->dev,
615 port->bulk_in_endpointAddress),
616 portdata->in_buffer[j], IN_BUFLEN,
617 sierra_indat_callback, port);
618 portdata->in_urbs[j] = urb;
619 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700620 }
621
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900622 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700623}
624
625static void sierra_shutdown(struct usb_serial *serial)
626{
627 int i, j;
628 struct usb_serial_port *port;
629 struct sierra_port_private *portdata;
630
631 dbg("%s", __FUNCTION__);
632
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700633 for (i = 0; i < serial->num_ports; ++i) {
634 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700635 if (!port)
636 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700637 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700638 if (!portdata)
639 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700640
641 for (j = 0; j < N_IN_URB; j++) {
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900642 usb_kill_urb(portdata->in_urbs[j]);
643 usb_free_urb(portdata->in_urbs[j]);
644 portdata->in_urbs[j] = NULL;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700645 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900646 kfree(portdata);
647 usb_set_serial_port_data(port, NULL);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700648 }
649}
650
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700651static struct usb_serial_driver sierra_1port_device = {
652 .driver = {
653 .owner = THIS_MODULE,
654 .name = "sierra1",
655 },
656 .description = "Sierra USB modem (1 port)",
657 .id_table = id_table_1port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100658 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700659 .num_interrupt_in = NUM_DONT_CARE,
660 .num_bulk_in = 1,
661 .num_bulk_out = 1,
662 .num_ports = 1,
663 .open = sierra_open,
664 .close = sierra_close,
665 .write = sierra_write,
666 .write_room = sierra_write_room,
667 .chars_in_buffer = sierra_chars_in_buffer,
668 .throttle = sierra_rx_throttle,
669 .unthrottle = sierra_rx_unthrottle,
670 .ioctl = sierra_ioctl,
671 .set_termios = sierra_set_termios,
672 .break_ctl = sierra_break_ctl,
673 .tiocmget = sierra_tiocmget,
674 .tiocmset = sierra_tiocmset,
675 .attach = sierra_startup,
676 .shutdown = sierra_shutdown,
677 .read_int_callback = sierra_instat_callback,
678};
679
680static struct usb_serial_driver sierra_3port_device = {
681 .driver = {
682 .owner = THIS_MODULE,
683 .name = "sierra3",
684 },
685 .description = "Sierra USB modem (3 port)",
686 .id_table = id_table_3port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100687 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700688 .num_interrupt_in = NUM_DONT_CARE,
689 .num_bulk_in = 3,
690 .num_bulk_out = 3,
691 .num_ports = 3,
692 .open = sierra_open,
693 .close = sierra_close,
694 .write = sierra_write,
695 .write_room = sierra_write_room,
696 .chars_in_buffer = sierra_chars_in_buffer,
697 .throttle = sierra_rx_throttle,
698 .unthrottle = sierra_rx_unthrottle,
699 .ioctl = sierra_ioctl,
700 .set_termios = sierra_set_termios,
701 .break_ctl = sierra_break_ctl,
702 .tiocmget = sierra_tiocmget,
703 .tiocmset = sierra_tiocmset,
704 .attach = sierra_startup,
705 .shutdown = sierra_shutdown,
706 .read_int_callback = sierra_instat_callback,
707};
708
709/* Functions used by new usb-serial code. */
710static int __init sierra_init(void)
711{
712 int retval;
713 retval = usb_serial_register(&sierra_1port_device);
714 if (retval)
715 goto failed_1port_device_register;
716 retval = usb_serial_register(&sierra_3port_device);
717 if (retval)
718 goto failed_3port_device_register;
719
720
721 retval = usb_register(&sierra_driver);
722 if (retval)
723 goto failed_driver_register;
724
725 info(DRIVER_DESC ": " DRIVER_VERSION);
726
727 return 0;
728
729failed_driver_register:
730 usb_serial_deregister(&sierra_3port_device);
731failed_3port_device_register:
732 usb_serial_deregister(&sierra_1port_device);
733failed_1port_device_register:
734 return retval;
735}
736
737static void __exit sierra_exit(void)
738{
739 usb_deregister (&sierra_driver);
740 usb_serial_deregister(&sierra_1port_device);
741 usb_serial_deregister(&sierra_3port_device);
742}
743
744module_init(sierra_init);
745module_exit(sierra_exit);
746
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700747MODULE_AUTHOR(DRIVER_AUTHOR);
748MODULE_DESCRIPTION(DRIVER_DESC);
749MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700750MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700751
752#ifdef CONFIG_USB_DEBUG
753module_param(debug, bool, S_IRUGO | S_IWUSR);
754MODULE_PARM_DESC(debug, "Debug messages");
755#endif
756