blob: 551c6ce89f669de69f3fc54a75d9789272981f16 [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
4 Copyright (C) 2006 Kevin Lloyd <linux@sierrawireless.com>
5
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>
15
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070016*/
17
Kevin Lloyde43062d2007-01-17 16:04:18 -080018#define DRIVER_VERSION "v.1.0.6"
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070019#define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>"
20#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
Kevin Lloyd69de51f2006-06-30 11:17:55 -070021
22#include <linux/kernel.h>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070023#include <linux/jiffies.h>
24#include <linux/errno.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070025#include <linux/tty.h>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070026#include <linux/tty_flip.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070027#include <linux/module.h>
28#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070029#include <linux/usb/serial.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070030
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070031
Kevin Lloyd69de51f2006-06-30 11:17:55 -070032static struct usb_device_id id_table [] = {
Kevin Lloyd69de51f2006-06-30 11:17:55 -070033 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080034 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -070035 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080036 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
37 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
Danny Budik5b7da8f2007-05-04 16:09:37 -040038 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */
Kevin Lloyde43062d2007-01-17 16:04:18 -080039 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd69de51f2006-06-30 11:17:55 -070040 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080041 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -070042 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
43 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
44 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070045
46 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
47 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -070048 { }
49};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -070050MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070051
52static struct usb_device_id id_table_1port [] = {
53 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
54 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
55 { }
56};
57
58static struct usb_device_id id_table_3port [] = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070059 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080060 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -070061 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080062 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
63 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
Danny Budik5b7da8f2007-05-04 16:09:37 -040064 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */
Kevin Lloyde43062d2007-01-17 16:04:18 -080065 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070066 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -080067 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070068 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
69 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
70 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
71 { }
72};
73
Kevin Lloyd69de51f2006-06-30 11:17:55 -070074static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070075 .name = "sierra",
76 .probe = usb_serial_probe,
77 .disconnect = usb_serial_disconnect,
78 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -070079 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -070080};
81
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070082
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070083static int debug;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070084
85/* per port private data */
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -070086#define N_IN_URB 4
Kevin Lloyde43062d2007-01-17 16:04:18 -080087#define N_OUT_URB 4
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -070088#define IN_BUFLEN 4096
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070089
90struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +090091 spinlock_t lock; /* lock the structure */
92 int outstanding_urbs; /* number of out urbs in flight */
93
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070094 /* Input endpoints and buffer for this port */
95 struct urb *in_urbs[N_IN_URB];
96 char in_buffer[N_IN_URB][IN_BUFLEN];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070097
98 /* Settings for the port */
99 int rts_state; /* Handshaking pins (outputs) */
100 int dtr_state;
101 int cts_state; /* Handshaking pins (inputs) */
102 int dsr_state;
103 int dcd_state;
104 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700105};
106
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700107static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700108{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700109 struct usb_serial *serial = port->serial;
110 struct sierra_port_private *portdata;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700111
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700112 dbg("%s", __FUNCTION__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700113
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700114 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700115
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700116 if (port->tty) {
117 int val = 0;
118 if (portdata->dtr_state)
119 val |= 0x01;
120 if (portdata->rts_state)
121 val |= 0x02;
122
123 return usb_control_msg(serial->dev,
124 usb_rcvctrlpipe(serial->dev, 0),
125 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
126 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700127
128 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700129}
130
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700131static void sierra_rx_throttle(struct usb_serial_port *port)
132{
133 dbg("%s", __FUNCTION__);
134}
135
136static void sierra_rx_unthrottle(struct usb_serial_port *port)
137{
138 dbg("%s", __FUNCTION__);
139}
140
141static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
142{
143 /* Unfortunately, I don't know how to send a break */
144 dbg("%s", __FUNCTION__);
145}
146
147static void sierra_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800148 struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700149{
150 dbg("%s", __FUNCTION__);
151
152 sierra_send_setup(port);
153}
154
155static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
156{
157 unsigned int value;
158 struct sierra_port_private *portdata;
159
160 portdata = usb_get_serial_port_data(port);
161
162 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
163 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
164 ((portdata->cts_state) ? TIOCM_CTS : 0) |
165 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
166 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
167 ((portdata->ri_state) ? TIOCM_RNG : 0);
168
169 return value;
170}
171
172static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
173 unsigned int set, unsigned int clear)
174{
175 struct sierra_port_private *portdata;
176
177 portdata = usb_get_serial_port_data(port);
178
179 if (set & TIOCM_RTS)
180 portdata->rts_state = 1;
181 if (set & TIOCM_DTR)
182 portdata->dtr_state = 1;
183
184 if (clear & TIOCM_RTS)
185 portdata->rts_state = 0;
186 if (clear & TIOCM_DTR)
187 portdata->dtr_state = 0;
188 return sierra_send_setup(port);
189}
190
191static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
192 unsigned int cmd, unsigned long arg)
193{
194 return -ENOIOCTLCMD;
195}
196
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900197static void sierra_outdat_callback(struct urb *urb)
198{
199 struct usb_serial_port *port = urb->context;
200 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
201 int status = urb->status;
202 unsigned long flags;
203
204 dbg("%s - port %d", __FUNCTION__, port->number);
205
206 /* free up the transfer buffer, as usb_free_urb() does not do this */
207 kfree(urb->transfer_buffer);
208
209 if (status)
210 dbg("%s - nonzero write bulk status received: %d",
211 __FUNCTION__, status);
212
213 spin_lock_irqsave(&portdata->lock, flags);
214 --portdata->outstanding_urbs;
215 spin_unlock_irqrestore(&portdata->lock, flags);
216
217 usb_serial_port_softint(port);
218}
219
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700220/* Write */
221static int sierra_write(struct usb_serial_port *port,
222 const unsigned char *buf, int count)
223{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900224 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
225 struct usb_serial *serial = port->serial;
226 unsigned long flags;
227 unsigned char *buffer;
228 struct urb *urb;
229 int status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700230
231 portdata = usb_get_serial_port_data(port);
232
233 dbg("%s: write (%d chars)", __FUNCTION__, count);
234
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900235 spin_lock_irqsave(&portdata->lock, flags);
236 if (portdata->outstanding_urbs > N_OUT_URB) {
237 spin_unlock_irqrestore(&portdata->lock, flags);
238 dbg("%s - write limit hit\n", __FUNCTION__);
239 return 0;
240 }
241 portdata->outstanding_urbs++;
242 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700243
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900244 buffer = kmalloc(count, GFP_ATOMIC);
245 if (!buffer) {
246 dev_err(&port->dev, "out of memory\n");
247 count = -ENOMEM;
248 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700249 }
250
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900251 urb = usb_alloc_urb(0, GFP_ATOMIC);
252 if (!urb) {
253 dev_err(&port->dev, "no more free urbs\n");
254 count = -ENOMEM;
255 goto error_no_urb;
256 }
257
258 memcpy(buffer, buf, count);
259
260 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
261
262 usb_fill_bulk_urb(urb, serial->dev,
263 usb_sndbulkpipe(serial->dev,
264 port->bulk_out_endpointAddress),
265 buffer, count, sierra_outdat_callback, port);
266
267 /* send it down the pipe */
268 status = usb_submit_urb(urb, GFP_ATOMIC);
269 if (status) {
270 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
271 "with status = %d\n", __FUNCTION__, status);
272 count = status;
273 goto error;
274 }
275
276 /* we are done with this urb, so let the host driver
277 * really free it when it is finished with it */
278 usb_free_urb(urb);
279
280 return count;
281error:
282 usb_free_urb(urb);
283error_no_urb:
284 kfree(buffer);
285error_no_buffer:
286 spin_lock_irqsave(&portdata->lock, flags);
287 --portdata->outstanding_urbs;
288 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700289 return count;
290}
291
292static void sierra_indat_callback(struct urb *urb)
293{
294 int err;
295 int endpoint;
296 struct usb_serial_port *port;
297 struct tty_struct *tty;
298 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700299 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700300
301 dbg("%s: %p", __FUNCTION__, urb);
302
303 endpoint = usb_pipeendpoint(urb->pipe);
304 port = (struct usb_serial_port *) urb->context;
305
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700306 if (status) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700307 dbg("%s: nonzero status: %d on endpoint %02x.",
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700308 __FUNCTION__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700309 } else {
310 tty = port->tty;
311 if (urb->actual_length) {
312 tty_buffer_request_room(tty, urb->actual_length);
313 tty_insert_flip_string(tty, data, urb->actual_length);
314 tty_flip_buffer_push(tty);
315 } else {
316 dbg("%s: empty read urb received", __FUNCTION__);
317 }
318
319 /* Resubmit urb so we continue receiving */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700320 if (port->open_count && status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700321 err = usb_submit_urb(urb, GFP_ATOMIC);
322 if (err)
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900323 dev_err(&port->dev, "resubmit read urb failed."
324 "(%d)", err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700325 }
326 }
327 return;
328}
329
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700330static void sierra_instat_callback(struct urb *urb)
331{
332 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700333 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700334 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
335 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
336 struct usb_serial *serial = port->serial;
337
338 dbg("%s", __FUNCTION__);
339 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
340
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700341 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700342 struct usb_ctrlrequest *req_pkt =
343 (struct usb_ctrlrequest *)urb->transfer_buffer;
344
345 if (!req_pkt) {
346 dbg("%s: NULL req_pkt\n", __FUNCTION__);
347 return;
348 }
349 if ((req_pkt->bRequestType == 0xA1) &&
350 (req_pkt->bRequest == 0x20)) {
351 int old_dcd_state;
352 unsigned char signals = *((unsigned char *)
353 urb->transfer_buffer +
354 sizeof(struct usb_ctrlrequest));
355
356 dbg("%s: signal x%x", __FUNCTION__, signals);
357
358 old_dcd_state = portdata->dcd_state;
359 portdata->cts_state = 1;
360 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
361 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
362 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
363
364 if (port->tty && !C_CLOCAL(port->tty) &&
365 old_dcd_state && !portdata->dcd_state)
366 tty_hangup(port->tty);
367 } else {
368 dbg("%s: type %x req %x", __FUNCTION__,
369 req_pkt->bRequestType,req_pkt->bRequest);
370 }
371 } else
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700372 dbg("%s: error %d", __FUNCTION__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700373
374 /* Resubmit urb so we continue receiving IRQ data */
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700375 if (status != -ESHUTDOWN) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700376 urb->dev = serial->dev;
377 err = usb_submit_urb(urb, GFP_ATOMIC);
378 if (err)
379 dbg("%s: resubmit intr urb failed. (%d)",
380 __FUNCTION__, err);
381 }
382}
383
384static int sierra_write_room(struct usb_serial_port *port)
385{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900386 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
387 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700388
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900389 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700390
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900391 /* try to give a good number back based on if we have any free urbs at
392 * this point in time */
393 spin_lock_irqsave(&portdata->lock, flags);
394 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
395 spin_unlock_irqrestore(&portdata->lock, flags);
396 dbg("%s - write limit hit\n", __FUNCTION__);
397 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700398 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900399 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700400
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900401 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700402}
403
404static int sierra_chars_in_buffer(struct usb_serial_port *port)
405{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900406 dbg("%s - port %d", __FUNCTION__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700407
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900408 /*
409 * We can't really account for how much data we
410 * have sent out, but hasn't made it through to the
411 * device as we can't see the backend here, so just
412 * tell the tty layer that everything is flushed.
413 */
414 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700415}
416
417static int sierra_open(struct usb_serial_port *port, struct file *filp)
418{
419 struct sierra_port_private *portdata;
420 struct usb_serial *serial = port->serial;
421 int i, err;
422 struct urb *urb;
Kevin Lloyde43062d2007-01-17 16:04:18 -0800423 int result;
424 __u16 set_mode_dzero = 0x0000;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700425
426 portdata = usb_get_serial_port_data(port);
427
428 dbg("%s", __FUNCTION__);
429
430 /* Set some sane defaults */
431 portdata->rts_state = 1;
432 portdata->dtr_state = 1;
433
434 /* Reset low level data toggle and start reading from endpoints */
435 for (i = 0; i < N_IN_URB; i++) {
436 urb = portdata->in_urbs[i];
437 if (! urb)
438 continue;
439 if (urb->dev != serial->dev) {
440 dbg("%s: dev %p != %p", __FUNCTION__,
441 urb->dev, serial->dev);
442 continue;
443 }
444
445 /*
446 * make sure endpoint data toggle is synchronized with the
447 * device
448 */
449 usb_clear_halt(urb->dev, urb->pipe);
450
451 err = usb_submit_urb(urb, GFP_KERNEL);
452 if (err) {
453 dbg("%s: submit urb %d failed (%d) %d",
454 __FUNCTION__, i, err,
455 urb->transfer_buffer_length);
456 }
457 }
458
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700459 port->tty->low_latency = 1;
460
Kevin Lloyde43062d2007-01-17 16:04:18 -0800461 /* set mode to D0 */
462 result = usb_control_msg(serial->dev,
463 usb_rcvctrlpipe(serial->dev, 0),
464 0x00, 0x40, set_mode_dzero, 0, NULL,
465 0, USB_CTRL_SET_TIMEOUT);
466
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700467 sierra_send_setup(port);
468
469 return (0);
470}
471
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700472static void sierra_close(struct usb_serial_port *port, struct file *filp)
473{
474 int i;
475 struct usb_serial *serial = port->serial;
476 struct sierra_port_private *portdata;
477
478 dbg("%s", __FUNCTION__);
479 portdata = usb_get_serial_port_data(port);
480
481 portdata->rts_state = 0;
482 portdata->dtr_state = 0;
483
484 if (serial->dev) {
485 sierra_send_setup(port);
486
487 /* Stop reading/writing urbs */
488 for (i = 0; i < N_IN_URB; i++)
Oliver Neukumebcf3ed2007-03-20 13:54:05 +0100489 usb_unlink_urb(portdata->in_urbs[i]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700490 }
491 port->tty = NULL;
492}
493
494/* Helper functions used by sierra_setup_urbs */
495static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700496 int dir, void *ctx, char *buf, int len,
497 usb_complete_t callback)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700498{
499 struct urb *urb;
500
501 if (endpoint == -1)
502 return NULL; /* endpoint not needed */
503
504 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
505 if (urb == NULL) {
506 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
507 return NULL;
508 }
509
510 /* Fill URB using supplied data. */
511 usb_fill_bulk_urb(urb, serial->dev,
512 usb_sndbulkpipe(serial->dev, endpoint) | dir,
513 buf, len, callback, ctx);
514
515 return urb;
516}
517
518/* Setup urbs */
519static void sierra_setup_urbs(struct usb_serial *serial)
520{
521 int i,j;
522 struct usb_serial_port *port;
523 struct sierra_port_private *portdata;
524
525 dbg("%s", __FUNCTION__);
526
527 for (i = 0; i < serial->num_ports; i++) {
528 port = serial->port[i];
529 portdata = usb_get_serial_port_data(port);
530
531 /* Do indat endpoints first */
532 for (j = 0; j < N_IN_URB; ++j) {
533 portdata->in_urbs[j] = sierra_setup_urb (serial,
534 port->bulk_in_endpointAddress, USB_DIR_IN, port,
535 portdata->in_buffer[j], IN_BUFLEN, sierra_indat_callback);
536 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700537 }
538}
539
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700540static int sierra_startup(struct usb_serial *serial)
541{
542 int i, err;
543 struct usb_serial_port *port;
544 struct sierra_port_private *portdata;
545
546 dbg("%s", __FUNCTION__);
547
548 /* Now setup per port private data */
549 for (i = 0; i < serial->num_ports; i++) {
550 port = serial->port[i];
551 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
552 if (!portdata) {
553 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
554 __FUNCTION__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900555 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700556 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900557 spin_lock_init(&portdata->lock);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700558
559 usb_set_serial_port_data(port, portdata);
560
561 if (! port->interrupt_in_urb)
562 continue;
563 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
564 if (err)
565 dbg("%s: submit irq_in urb failed %d",
566 __FUNCTION__, err);
567 }
568
569 sierra_setup_urbs(serial);
570
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900571 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700572}
573
574static void sierra_shutdown(struct usb_serial *serial)
575{
576 int i, j;
577 struct usb_serial_port *port;
578 struct sierra_port_private *portdata;
579
580 dbg("%s", __FUNCTION__);
581
582 /* Stop reading/writing urbs */
583 for (i = 0; i < serial->num_ports; ++i) {
584 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700585 if (!port)
586 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700587 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700588 if (!portdata)
589 continue;
590
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700591 for (j = 0; j < N_IN_URB; j++)
Oliver Neukumebcf3ed2007-03-20 13:54:05 +0100592 usb_unlink_urb(portdata->in_urbs[j]);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700593 }
594
595 /* Now free them */
596 for (i = 0; i < serial->num_ports; ++i) {
597 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700598 if (!port)
599 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700600 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700601 if (!portdata)
602 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700603
604 for (j = 0; j < N_IN_URB; j++) {
605 if (portdata->in_urbs[j]) {
606 usb_free_urb(portdata->in_urbs[j]);
607 portdata->in_urbs[j] = NULL;
608 }
609 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700610 }
611
612 /* Now free per port private data */
613 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 kfree(usb_get_serial_port_data(port));
618 }
619}
620
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700621static struct usb_serial_driver sierra_1port_device = {
622 .driver = {
623 .owner = THIS_MODULE,
624 .name = "sierra1",
625 },
626 .description = "Sierra USB modem (1 port)",
627 .id_table = id_table_1port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100628 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700629 .num_interrupt_in = NUM_DONT_CARE,
630 .num_bulk_in = 1,
631 .num_bulk_out = 1,
632 .num_ports = 1,
633 .open = sierra_open,
634 .close = sierra_close,
635 .write = sierra_write,
636 .write_room = sierra_write_room,
637 .chars_in_buffer = sierra_chars_in_buffer,
638 .throttle = sierra_rx_throttle,
639 .unthrottle = sierra_rx_unthrottle,
640 .ioctl = sierra_ioctl,
641 .set_termios = sierra_set_termios,
642 .break_ctl = sierra_break_ctl,
643 .tiocmget = sierra_tiocmget,
644 .tiocmset = sierra_tiocmset,
645 .attach = sierra_startup,
646 .shutdown = sierra_shutdown,
647 .read_int_callback = sierra_instat_callback,
648};
649
650static struct usb_serial_driver sierra_3port_device = {
651 .driver = {
652 .owner = THIS_MODULE,
653 .name = "sierra3",
654 },
655 .description = "Sierra USB modem (3 port)",
656 .id_table = id_table_3port,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100657 .usb_driver = &sierra_driver,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700658 .num_interrupt_in = NUM_DONT_CARE,
659 .num_bulk_in = 3,
660 .num_bulk_out = 3,
661 .num_ports = 3,
662 .open = sierra_open,
663 .close = sierra_close,
664 .write = sierra_write,
665 .write_room = sierra_write_room,
666 .chars_in_buffer = sierra_chars_in_buffer,
667 .throttle = sierra_rx_throttle,
668 .unthrottle = sierra_rx_unthrottle,
669 .ioctl = sierra_ioctl,
670 .set_termios = sierra_set_termios,
671 .break_ctl = sierra_break_ctl,
672 .tiocmget = sierra_tiocmget,
673 .tiocmset = sierra_tiocmset,
674 .attach = sierra_startup,
675 .shutdown = sierra_shutdown,
676 .read_int_callback = sierra_instat_callback,
677};
678
679/* Functions used by new usb-serial code. */
680static int __init sierra_init(void)
681{
682 int retval;
683 retval = usb_serial_register(&sierra_1port_device);
684 if (retval)
685 goto failed_1port_device_register;
686 retval = usb_serial_register(&sierra_3port_device);
687 if (retval)
688 goto failed_3port_device_register;
689
690
691 retval = usb_register(&sierra_driver);
692 if (retval)
693 goto failed_driver_register;
694
695 info(DRIVER_DESC ": " DRIVER_VERSION);
696
697 return 0;
698
699failed_driver_register:
700 usb_serial_deregister(&sierra_3port_device);
701failed_3port_device_register:
702 usb_serial_deregister(&sierra_1port_device);
703failed_1port_device_register:
704 return retval;
705}
706
707static void __exit sierra_exit(void)
708{
709 usb_deregister (&sierra_driver);
710 usb_serial_deregister(&sierra_1port_device);
711 usb_serial_deregister(&sierra_3port_device);
712}
713
714module_init(sierra_init);
715module_exit(sierra_exit);
716
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700717MODULE_AUTHOR(DRIVER_AUTHOR);
718MODULE_DESCRIPTION(DRIVER_DESC);
719MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700720MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700721
722#ifdef CONFIG_USB_DEBUG
723module_param(debug, bool, S_IRUGO | S_IWUSR);
724MODULE_PARM_DESC(debug, "Debug messages");
725#endif
726