blob: c137c8962b9f8f0bfbe6797fa5cddafb3e72c801 [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 Lloyd112225b2007-07-16 13:49:27 -070017#define DRIVER_VERSION "v.1.2.4b"
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
48int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
49{
50 int result;
51 dev_dbg(&udev->dev, "%s", "SET POWER STATE");
52 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
63int sierra_set_ms_mode(struct usb_device *udev, __u16 eSocMode)
64{
65 int result;
66 dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH");
67 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
78int sierra_probe(struct usb_interface *iface, const struct usb_device_id *id)
79{
80 int result;
81 struct usb_device *udev;
82
83 udev = usb_get_dev(interface_to_usbdev(iface));
84
85 /* Check if in installer mode */
86 if (id->driver_info == DEVICE_INSTALLER) {
87 dev_dbg(&udev->dev, "%s", "FOUND DEVICE(SW)\n");
88 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
89 /*We do not want to bind to the device when in installer mode*/
90 return -EIO;
91 }
92
93 return usb_serial_probe(iface, id);
94}
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070095
Kevin Lloyd69de51f2006-06-30 11:17:55 -070096static struct usb_device_id id_table [] = {
Kevin Lloyd69de51f2006-06-30 11:17:55 -070097 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080098 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -070099 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800100 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
101 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
Danny Budik5b7da8f2007-05-04 16:09:37 -0400102 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800103 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700104 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800105 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700106 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
107 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
108 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700109
Kevin Lloyd112225b2007-07-16 13:49:27 -0700110 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
111 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
112
113 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700114 { }
115};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700116MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700117
118static struct usb_device_id id_table_1port [] = {
119 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
120 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
121 { }
122};
123
124static struct usb_device_id id_table_3port [] = {
Kevin Lloyd112225b2007-07-16 13:49:27 -0700125 /* CDMA Devices */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700126 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800127 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700128 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800129 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
130 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
Danny Budik5b7da8f2007-05-04 16:09:37 -0400131 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800132 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700133
134 /* GSM/UMTS Devices */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700135 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800136 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700137 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
138 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
139 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
140 { }
141};
142
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700143static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700144 .name = "sierra",
Kevin Lloyd112225b2007-07-16 13:49:27 -0700145 .probe = sierra_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700146 .disconnect = usb_serial_disconnect,
147 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700148 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700149};
150
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700151
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700152struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900153 spinlock_t lock; /* lock the structure */
154 int outstanding_urbs; /* number of out urbs in flight */
155
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700156 /* Input endpoints and buffer for this port */
157 struct urb *in_urbs[N_IN_URB];
158 char in_buffer[N_IN_URB][IN_BUFLEN];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700159
160 /* Settings for the port */
161 int rts_state; /* Handshaking pins (outputs) */
162 int dtr_state;
163 int cts_state; /* Handshaking pins (inputs) */
164 int dsr_state;
165 int dcd_state;
166 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700167};
168
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700169static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700170{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700171 struct usb_serial *serial = port->serial;
172 struct sierra_port_private *portdata;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700173
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700174 dbg("%s", __FUNCTION__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700175
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700176 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700177
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700178 if (port->tty) {
179 int val = 0;
180 if (portdata->dtr_state)
181 val |= 0x01;
182 if (portdata->rts_state)
183 val |= 0x02;
184
185 return usb_control_msg(serial->dev,
186 usb_rcvctrlpipe(serial->dev, 0),
187 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
188 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700189
190 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700191}
192
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700193static void sierra_rx_throttle(struct usb_serial_port *port)
194{
195 dbg("%s", __FUNCTION__);
196}
197
198static void sierra_rx_unthrottle(struct usb_serial_port *port)
199{
200 dbg("%s", __FUNCTION__);
201}
202
203static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
204{
205 /* Unfortunately, I don't know how to send a break */
206 dbg("%s", __FUNCTION__);
207}
208
209static void sierra_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800210 struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700211{
212 dbg("%s", __FUNCTION__);
213
214 sierra_send_setup(port);
215}
216
217static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
218{
219 unsigned int value;
220 struct sierra_port_private *portdata;
221
222 portdata = usb_get_serial_port_data(port);
223
224 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
225 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
226 ((portdata->cts_state) ? TIOCM_CTS : 0) |
227 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
228 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
229 ((portdata->ri_state) ? TIOCM_RNG : 0);
230
231 return value;
232}
233
234static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
235 unsigned int set, unsigned int clear)
236{
237 struct sierra_port_private *portdata;
238
239 portdata = usb_get_serial_port_data(port);
240
241 if (set & TIOCM_RTS)
242 portdata->rts_state = 1;
243 if (set & TIOCM_DTR)
244 portdata->dtr_state = 1;
245
246 if (clear & TIOCM_RTS)
247 portdata->rts_state = 0;
248 if (clear & TIOCM_DTR)
249 portdata->dtr_state = 0;
250 return sierra_send_setup(port);
251}
252
253static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
254 unsigned int cmd, unsigned long arg)
255{
256 return -ENOIOCTLCMD;
257}
258
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900259static void sierra_outdat_callback(struct urb *urb)
260{
261 struct usb_serial_port *port = urb->context;
262 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
263 int status = urb->status;
264 unsigned long flags;
265
266 dbg("%s - port %d", __FUNCTION__, port->number);
267
268 /* free up the transfer buffer, as usb_free_urb() does not do this */
269 kfree(urb->transfer_buffer);
270
271 if (status)
272 dbg("%s - nonzero write bulk status received: %d",
273 __FUNCTION__, status);
274
275 spin_lock_irqsave(&portdata->lock, flags);
276 --portdata->outstanding_urbs;
277 spin_unlock_irqrestore(&portdata->lock, flags);
278
279 usb_serial_port_softint(port);
280}
281
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700282/* Write */
283static int sierra_write(struct usb_serial_port *port,
284 const unsigned char *buf, int count)
285{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900286 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
287 struct usb_serial *serial = port->serial;
288 unsigned long flags;
289 unsigned char *buffer;
290 struct urb *urb;
291 int status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700292
293 portdata = usb_get_serial_port_data(port);
294
295 dbg("%s: write (%d chars)", __FUNCTION__, count);
296
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900297 spin_lock_irqsave(&portdata->lock, flags);
298 if (portdata->outstanding_urbs > N_OUT_URB) {
299 spin_unlock_irqrestore(&portdata->lock, flags);
300 dbg("%s - write limit hit\n", __FUNCTION__);
301 return 0;
302 }
303 portdata->outstanding_urbs++;
304 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700305
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900306 buffer = kmalloc(count, GFP_ATOMIC);
307 if (!buffer) {
308 dev_err(&port->dev, "out of memory\n");
309 count = -ENOMEM;
310 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700311 }
312
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900313 urb = usb_alloc_urb(0, GFP_ATOMIC);
314 if (!urb) {
315 dev_err(&port->dev, "no more free urbs\n");
316 count = -ENOMEM;
317 goto error_no_urb;
318 }
319
320 memcpy(buffer, buf, count);
321
322 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
323
324 usb_fill_bulk_urb(urb, serial->dev,
325 usb_sndbulkpipe(serial->dev,
326 port->bulk_out_endpointAddress),
327 buffer, count, sierra_outdat_callback, port);
328
329 /* send it down the pipe */
330 status = usb_submit_urb(urb, GFP_ATOMIC);
331 if (status) {
332 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
333 "with status = %d\n", __FUNCTION__, status);
334 count = status;
335 goto error;
336 }
337
338 /* we are done with this urb, so let the host driver
339 * really free it when it is finished with it */
340 usb_free_urb(urb);
341
342 return count;
343error:
344 usb_free_urb(urb);
345error_no_urb:
346 kfree(buffer);
347error_no_buffer:
348 spin_lock_irqsave(&portdata->lock, flags);
349 --portdata->outstanding_urbs;
350 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700351 return count;
352}
353
354static void sierra_indat_callback(struct urb *urb)
355{
356 int err;
357 int endpoint;
358 struct usb_serial_port *port;
359 struct tty_struct *tty;
360 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700361 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700362
363 dbg("%s: %p", __FUNCTION__, urb);
364
365 endpoint = usb_pipeendpoint(urb->pipe);
366 port = (struct usb_serial_port *) urb->context;
367
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700368 if (status) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700369 dbg("%s: nonzero status: %d on endpoint %02x.",
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700370 __FUNCTION__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700371 } else {
372 tty = port->tty;
373 if (urb->actual_length) {
374 tty_buffer_request_room(tty, urb->actual_length);
375 tty_insert_flip_string(tty, data, urb->actual_length);
376 tty_flip_buffer_push(tty);
377 } else {
378 dbg("%s: empty read urb received", __FUNCTION__);
379 }
380
381 /* Resubmit urb so we continue receiving */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700382 if (port->open_count && status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700383 err = usb_submit_urb(urb, GFP_ATOMIC);
384 if (err)
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900385 dev_err(&port->dev, "resubmit read urb failed."
386 "(%d)", err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700387 }
388 }
389 return;
390}
391
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700392static void sierra_instat_callback(struct urb *urb)
393{
394 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700395 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700396 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
397 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
398 struct usb_serial *serial = port->serial;
399
400 dbg("%s", __FUNCTION__);
401 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
402
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700403 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700404 struct usb_ctrlrequest *req_pkt =
405 (struct usb_ctrlrequest *)urb->transfer_buffer;
406
407 if (!req_pkt) {
408 dbg("%s: NULL req_pkt\n", __FUNCTION__);
409 return;
410 }
411 if ((req_pkt->bRequestType == 0xA1) &&
412 (req_pkt->bRequest == 0x20)) {
413 int old_dcd_state;
414 unsigned char signals = *((unsigned char *)
415 urb->transfer_buffer +
416 sizeof(struct usb_ctrlrequest));
417
418 dbg("%s: signal x%x", __FUNCTION__, signals);
419
420 old_dcd_state = portdata->dcd_state;
421 portdata->cts_state = 1;
422 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
423 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
424 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
425
426 if (port->tty && !C_CLOCAL(port->tty) &&
427 old_dcd_state && !portdata->dcd_state)
428 tty_hangup(port->tty);
429 } else {
430 dbg("%s: type %x req %x", __FUNCTION__,
431 req_pkt->bRequestType,req_pkt->bRequest);
432 }
433 } else
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700434 dbg("%s: error %d", __FUNCTION__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700435
436 /* Resubmit urb so we continue receiving IRQ data */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700437 if (status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700438 urb->dev = serial->dev;
439 err = usb_submit_urb(urb, GFP_ATOMIC);
440 if (err)
441 dbg("%s: resubmit intr urb failed. (%d)",
442 __FUNCTION__, err);
443 }
444}
445
446static int sierra_write_room(struct usb_serial_port *port)
447{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900448 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
449 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700450
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900451 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700452
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900453 /* try to give a good number back based on if we have any free urbs at
454 * this point in time */
455 spin_lock_irqsave(&portdata->lock, flags);
456 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
457 spin_unlock_irqrestore(&portdata->lock, flags);
458 dbg("%s - write limit hit\n", __FUNCTION__);
459 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700460 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900461 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700462
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900463 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700464}
465
466static int sierra_chars_in_buffer(struct usb_serial_port *port)
467{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900468 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700469
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900470 /*
471 * We can't really account for how much data we
472 * have sent out, but hasn't made it through to the
473 * device as we can't see the backend here, so just
474 * tell the tty layer that everything is flushed.
475 */
476 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700477}
478
479static int sierra_open(struct usb_serial_port *port, struct file *filp)
480{
481 struct sierra_port_private *portdata;
482 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900483 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700484 struct urb *urb;
Kevin Lloyde43062d2007-01-17 16:04:18 -0800485 int result;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700486
487 portdata = usb_get_serial_port_data(port);
488
489 dbg("%s", __FUNCTION__);
490
491 /* Set some sane defaults */
492 portdata->rts_state = 1;
493 portdata->dtr_state = 1;
494
495 /* Reset low level data toggle and start reading from endpoints */
496 for (i = 0; i < N_IN_URB; i++) {
497 urb = portdata->in_urbs[i];
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900498 if (!urb)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700499 continue;
500 if (urb->dev != serial->dev) {
501 dbg("%s: dev %p != %p", __FUNCTION__,
502 urb->dev, serial->dev);
503 continue;
504 }
505
506 /*
507 * make sure endpoint data toggle is synchronized with the
508 * device
509 */
510 usb_clear_halt(urb->dev, urb->pipe);
511
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900512 result = usb_submit_urb(urb, GFP_KERNEL);
513 if (result) {
514 dev_err(&port->dev, "submit urb %d failed (%d) %d",
515 i, result, urb->transfer_buffer_length);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700516 }
517 }
518
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700519 port->tty->low_latency = 1;
520
521 sierra_send_setup(port);
522
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900523 /* start up the interrupt endpoint if we have one */
524 if (port->interrupt_in_urb) {
525 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
526 if (result)
527 dev_err(&port->dev, "submit irq_in urb failed %d",
528 result);
529 }
530 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700531}
532
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700533static void sierra_close(struct usb_serial_port *port, struct file *filp)
534{
535 int i;
536 struct usb_serial *serial = port->serial;
537 struct sierra_port_private *portdata;
538
539 dbg("%s", __FUNCTION__);
540 portdata = usb_get_serial_port_data(port);
541
542 portdata->rts_state = 0;
543 portdata->dtr_state = 0;
544
545 if (serial->dev) {
546 sierra_send_setup(port);
547
548 /* Stop reading/writing urbs */
549 for (i = 0; i < N_IN_URB; i++)
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900550 usb_kill_urb(portdata->in_urbs[i]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700551 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900552
553 usb_kill_urb(port->interrupt_in_urb);
554
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700555 port->tty = NULL;
556}
557
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700558static int sierra_startup(struct usb_serial *serial)
559{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700560 struct usb_serial_port *port;
561 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900562 struct urb *urb;
563 int i;
564 int j;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700565
566 dbg("%s", __FUNCTION__);
567
Kevin Lloyd112225b2007-07-16 13:49:27 -0700568 /*Set Device mode to D0 */
569 sierra_set_power_state(serial->dev, 0x0000);
570
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700571 /* Now setup per port private data */
572 for (i = 0; i < serial->num_ports; i++) {
573 port = serial->port[i];
574 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
575 if (!portdata) {
576 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
577 __FUNCTION__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900578 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700579 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900580 spin_lock_init(&portdata->lock);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700581
582 usb_set_serial_port_data(port, portdata);
583
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900584 /* initialize the in urbs */
585 for (j = 0; j < N_IN_URB; ++j) {
586 urb = usb_alloc_urb(0, GFP_KERNEL);
587 if (urb == NULL) {
588 dbg("%s: alloc for in port failed.",
589 __FUNCTION__);
590 continue;
591 }
592 /* Fill URB using supplied data. */
593 usb_fill_bulk_urb(urb, serial->dev,
594 usb_rcvbulkpipe(serial->dev,
595 port->bulk_in_endpointAddress),
596 portdata->in_buffer[j], IN_BUFLEN,
597 sierra_indat_callback, port);
598 portdata->in_urbs[j] = urb;
599 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700600 }
601
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900602 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700603}
604
605static void sierra_shutdown(struct usb_serial *serial)
606{
607 int i, j;
608 struct usb_serial_port *port;
609 struct sierra_port_private *portdata;
610
611 dbg("%s", __FUNCTION__);
612
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700613 for (i = 0; i < serial->num_ports; ++i) {
614 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700615 if (!port)
616 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700617 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700618 if (!portdata)
619 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700620
621 for (j = 0; j < N_IN_URB; j++) {
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900622 usb_kill_urb(portdata->in_urbs[j]);
623 usb_free_urb(portdata->in_urbs[j]);
624 portdata->in_urbs[j] = NULL;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700625 }
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900626 kfree(portdata);
627 usb_set_serial_port_data(port, NULL);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700628 }
629}
630
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700631static struct usb_serial_driver sierra_1port_device = {
632 .driver = {
633 .owner = THIS_MODULE,
634 .name = "sierra1",
635 },
636 .description = "Sierra USB modem (1 port)",
637 .id_table = id_table_1port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100638 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700639 .num_interrupt_in = NUM_DONT_CARE,
640 .num_bulk_in = 1,
641 .num_bulk_out = 1,
642 .num_ports = 1,
643 .open = sierra_open,
644 .close = sierra_close,
645 .write = sierra_write,
646 .write_room = sierra_write_room,
647 .chars_in_buffer = sierra_chars_in_buffer,
648 .throttle = sierra_rx_throttle,
649 .unthrottle = sierra_rx_unthrottle,
650 .ioctl = sierra_ioctl,
651 .set_termios = sierra_set_termios,
652 .break_ctl = sierra_break_ctl,
653 .tiocmget = sierra_tiocmget,
654 .tiocmset = sierra_tiocmset,
655 .attach = sierra_startup,
656 .shutdown = sierra_shutdown,
657 .read_int_callback = sierra_instat_callback,
658};
659
660static struct usb_serial_driver sierra_3port_device = {
661 .driver = {
662 .owner = THIS_MODULE,
663 .name = "sierra3",
664 },
665 .description = "Sierra USB modem (3 port)",
666 .id_table = id_table_3port,
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 = 3,
670 .num_bulk_out = 3,
671 .num_ports = 3,
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
689/* Functions used by new usb-serial code. */
690static int __init sierra_init(void)
691{
692 int retval;
693 retval = usb_serial_register(&sierra_1port_device);
694 if (retval)
695 goto failed_1port_device_register;
696 retval = usb_serial_register(&sierra_3port_device);
697 if (retval)
698 goto failed_3port_device_register;
699
700
701 retval = usb_register(&sierra_driver);
702 if (retval)
703 goto failed_driver_register;
704
705 info(DRIVER_DESC ": " DRIVER_VERSION);
706
707 return 0;
708
709failed_driver_register:
710 usb_serial_deregister(&sierra_3port_device);
711failed_3port_device_register:
712 usb_serial_deregister(&sierra_1port_device);
713failed_1port_device_register:
714 return retval;
715}
716
717static void __exit sierra_exit(void)
718{
719 usb_deregister (&sierra_driver);
720 usb_serial_deregister(&sierra_1port_device);
721 usb_serial_deregister(&sierra_3port_device);
722}
723
724module_init(sierra_init);
725module_exit(sierra_exit);
726
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700727MODULE_AUTHOR(DRIVER_AUTHOR);
728MODULE_DESCRIPTION(DRIVER_DESC);
729MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700730MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700731
732#ifdef CONFIG_USB_DEBUG
733module_param(debug, bool, S_IRUGO | S_IWUSR);
734MODULE_PARM_DESC(debug, "Debug messages");
735#endif
736