xref: /openbmc/linux/drivers/usb/serial/sierra.c (revision 384740dc)
1 /*
2   USB Driver for Sierra Wireless
3 
4   Copyright (C) 2006, 2007, 2008  Kevin Lloyd <klloyd@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 */
16 
17 #define DRIVER_VERSION "v.1.3.2"
18 #define DRIVER_AUTHOR "Kevin Lloyd <klloyd@sierrawireless.com>"
19 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
20 
21 #include <linux/kernel.h>
22 #include <linux/jiffies.h>
23 #include <linux/errno.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/usb.h>
28 #include <linux/usb/serial.h>
29 #include <linux/usb/ch9.h>
30 
31 #define SWIMS_USB_REQUEST_SetPower	0x00
32 #define SWIMS_USB_REQUEST_SetNmea	0x07
33 
34 /* per port private data */
35 #define N_IN_URB	4
36 #define N_OUT_URB	4
37 #define IN_BUFLEN	4096
38 
39 static int debug;
40 static int nmea;
41 
42 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
43 {
44 	int result;
45 	dev_dbg(&udev->dev, "%s", __func__);
46 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
47 			SWIMS_USB_REQUEST_SetPower,	/* __u8 request      */
48 			USB_TYPE_VENDOR,		/* __u8 request type */
49 			swiState,			/* __u16 value       */
50 			0,				/* __u16 index       */
51 			NULL,				/* void *data        */
52 			0,				/* __u16 size 	     */
53 			USB_CTRL_SET_TIMEOUT);		/* int timeout 	     */
54 	return result;
55 }
56 
57 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
58 {
59 	int result;
60 	dev_dbg(&udev->dev, "%s", __func__);
61 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
62 			SWIMS_USB_REQUEST_SetNmea,	/* __u8 request      */
63 			USB_TYPE_VENDOR,		/* __u8 request type */
64 			enable,				/* __u16 value       */
65 			0x0000,				/* __u16 index       */
66 			NULL,				/* void *data        */
67 			0,				/* __u16 size 	     */
68 			USB_CTRL_SET_TIMEOUT);		/* int timeout       */
69 	return result;
70 }
71 
72 static int sierra_calc_num_ports(struct usb_serial *serial)
73 {
74 	int result;
75 	int *num_ports = usb_get_serial_data(serial);
76 	dev_dbg(&serial->dev->dev, "%s", __func__);
77 
78 	result = *num_ports;
79 
80 	if (result) {
81 		kfree(num_ports);
82 		usb_set_serial_data(serial, NULL);
83 	}
84 
85 	return result;
86 }
87 
88 static int sierra_calc_interface(struct usb_serial *serial)
89 {
90 	int interface;
91 	struct usb_interface *p_interface;
92 	struct usb_host_interface *p_host_interface;
93 	dev_dbg(&serial->dev->dev, "%s", __func__);
94 
95 	/* Get the interface structure pointer from the serial struct */
96 	p_interface = serial->interface;
97 
98 	/* Get a pointer to the host interface structure */
99 	p_host_interface = p_interface->cur_altsetting;
100 
101 	/* read the interface descriptor for this active altsetting
102 	 * to find out the interface number we are on
103 	*/
104 	interface = p_host_interface->desc.bInterfaceNumber;
105 
106 	return interface;
107 }
108 
109 static int sierra_probe(struct usb_serial *serial,
110 			const struct usb_device_id *id)
111 {
112 	int result = 0;
113 	struct usb_device *udev;
114 	int *num_ports;
115 	u8 ifnum;
116 	u8 numendpoints;
117 
118 	dev_dbg(&serial->dev->dev, "%s", __func__);
119 
120 	num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
121 	if (!num_ports)
122 		return -ENOMEM;
123 
124 	ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
125 	numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
126 	udev = serial->dev;
127 
128 	/* Figure out the interface number from the serial structure */
129 	ifnum = sierra_calc_interface(serial);
130 
131 	/*
132 	 * If this interface supports more than 1 alternate
133 	 * select the 2nd one
134 	 */
135 	if (serial->interface->num_altsetting == 2) {
136 		dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
137 			ifnum);
138 		/* We know the alternate setting is 1 for the MC8785 */
139 		usb_set_interface(udev, ifnum, 1);
140 	}
141 
142 	/* Dummy interface present on some SKUs should be ignored */
143 	if (ifnum == 0x99)
144 		*num_ports = 0;
145 	else if (numendpoints <= 3)
146 		*num_ports = 1;
147 	else
148 		*num_ports = (numendpoints-1)/2;
149 
150 	/*
151 	 * save off our num_ports info so that we can use it in the
152 	 * calc_num_ports callback
153 	 */
154 	usb_set_serial_data(serial, (void *)num_ports);
155 
156 	return result;
157 }
158 
159 static struct usb_device_id id_table [] = {
160 	{ USB_DEVICE(0x1199, 0x0017) },	/* Sierra Wireless EM5625 */
161 	{ USB_DEVICE(0x1199, 0x0018) },	/* Sierra Wireless MC5720 */
162 	{ USB_DEVICE(0x1199, 0x0218) },	/* Sierra Wireless MC5720 */
163 	{ USB_DEVICE(0x03f0, 0x1b1d) }, /* HP ev2200 a.k.a MC5720 */
164 	{ USB_DEVICE(0x1199, 0x0020) },	/* Sierra Wireless MC5725 */
165 	{ USB_DEVICE(0x1199, 0x0024) },	/* Sierra Wireless MC5727 */
166 	{ USB_DEVICE(0x1199, 0x0220) },	/* Sierra Wireless MC5725 */
167 	{ USB_DEVICE(0x1199, 0x0019) },	/* Sierra Wireless AirCard 595 */
168 	{ USB_DEVICE(0x1199, 0x0021) },	/* Sierra Wireless AirCard 597E */
169 	{ USB_DEVICE(0x1199, 0x0120) },	/* Sierra Wireless USB Dongle 595U */
170 	 /* Sierra Wireless C597 */
171 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
172 	 /* Sierra Wireless Device */
173 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
174 	{ USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless Device */
175 	{ USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless Device */
176 	{ USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless Device */
177 
178 	{ USB_DEVICE(0x1199, 0x6802) },	/* Sierra Wireless MC8755 */
179 	{ USB_DEVICE(0x1199, 0x6804) },	/* Sierra Wireless MC8755 */
180 	{ USB_DEVICE(0x1199, 0x6803) },	/* Sierra Wireless MC8765 */
181 	{ USB_DEVICE(0x1199, 0x6812) },	/* Sierra Wireless MC8775 & AC 875U */
182 	{ USB_DEVICE(0x1199, 0x6813) },	/* Sierra Wireless MC8775 (Lenovo) */
183 	{ USB_DEVICE(0x1199, 0x6815) },	/* Sierra Wireless MC8775 */
184 	{ USB_DEVICE(0x03f0, 0x1e1d) },	/* HP hs2300 a.k.a MC8775 */
185 	{ USB_DEVICE(0x1199, 0x6820) },	/* Sierra Wireless AirCard 875 */
186 	{ USB_DEVICE(0x1199, 0x6821) },	/* Sierra Wireless AirCard 875U */
187 	{ USB_DEVICE(0x1199, 0x6832) },	/* Sierra Wireless MC8780 */
188 	{ USB_DEVICE(0x1199, 0x6833) },	/* Sierra Wireless MC8781 */
189 	{ USB_DEVICE(0x1199, 0x683A) },	/* Sierra Wireless MC8785 */
190 	{ USB_DEVICE(0x1199, 0x683B) },	/* Sierra Wireless MC8785 Composite */
191 	{ USB_DEVICE(0x1199, 0x683C) },	/* Sierra Wireless MC8790 */
192 	{ USB_DEVICE(0x1199, 0x683D) },	/* Sierra Wireless MC8790 */
193 	{ USB_DEVICE(0x1199, 0x683E) },	/* Sierra Wireless MC8790 */
194 	{ USB_DEVICE(0x1199, 0x6850) },	/* Sierra Wireless AirCard 880 */
195 	{ USB_DEVICE(0x1199, 0x6851) },	/* Sierra Wireless AirCard 881 */
196 	{ USB_DEVICE(0x1199, 0x6852) },	/* Sierra Wireless AirCard 880 E */
197 	{ USB_DEVICE(0x1199, 0x6853) },	/* Sierra Wireless AirCard 881 E */
198 	{ USB_DEVICE(0x1199, 0x6855) },	/* Sierra Wireless AirCard 880 U */
199 	{ USB_DEVICE(0x1199, 0x6856) },	/* Sierra Wireless AirCard 881 U */
200 	{ USB_DEVICE(0x1199, 0x6859) },	/* Sierra Wireless AirCard 885 E */
201 	{ USB_DEVICE(0x1199, 0x685A) },	/* Sierra Wireless AirCard 885 E */
202 	/* Sierra Wireless C885 */
203 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
204 	/* Sierra Wireless Device */
205 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
206 	/* Sierra Wireless Device */
207 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
208 	/* Sierra Wireless Device */
209 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
210 
211 	{ USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
212 	{ USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
213 
214 	{ }
215 };
216 MODULE_DEVICE_TABLE(usb, id_table);
217 
218 static struct usb_driver sierra_driver = {
219 	.name       = "sierra",
220 	.probe      = usb_serial_probe,
221 	.disconnect = usb_serial_disconnect,
222 	.id_table   = id_table,
223 	.no_dynamic_id = 	1,
224 };
225 
226 struct sierra_port_private {
227 	spinlock_t lock;	/* lock the structure */
228 	int outstanding_urbs;	/* number of out urbs in flight */
229 
230 	/* Input endpoints and buffers for this port */
231 	struct urb *in_urbs[N_IN_URB];
232 	char *in_buffer[N_IN_URB];
233 
234 	/* Settings for the port */
235 	int rts_state;	/* Handshaking pins (outputs) */
236 	int dtr_state;
237 	int cts_state;	/* Handshaking pins (inputs) */
238 	int dsr_state;
239 	int dcd_state;
240 	int ri_state;
241 };
242 
243 static int sierra_send_setup(struct tty_struct *tty,
244 						struct usb_serial_port *port)
245 {
246 	struct usb_serial *serial = port->serial;
247 	struct sierra_port_private *portdata;
248 	__u16 interface = 0;
249 
250 	dbg("%s", __func__);
251 
252 	portdata = usb_get_serial_port_data(port);
253 
254 	if (tty) {
255 		int val = 0;
256 		if (portdata->dtr_state)
257 			val |= 0x01;
258 		if (portdata->rts_state)
259 			val |= 0x02;
260 
261 		/* If composite device then properly report interface */
262 		if (serial->num_ports == 1)
263 			interface = sierra_calc_interface(serial);
264 
265 		/* Otherwise the need to do non-composite mapping */
266 		else {
267 			if (port->bulk_out_endpointAddress == 2)
268 				interface = 0;
269 			else if (port->bulk_out_endpointAddress == 4)
270 				interface = 1;
271 			else if (port->bulk_out_endpointAddress == 5)
272 				interface = 2;
273 		}
274 
275 		return usb_control_msg(serial->dev,
276 				usb_rcvctrlpipe(serial->dev, 0),
277 				0x22, 0x21, val, interface,
278 				NULL, 0, USB_CTRL_SET_TIMEOUT);
279 	}
280 
281 	return 0;
282 }
283 
284 static void sierra_set_termios(struct tty_struct *tty,
285 		struct usb_serial_port *port, struct ktermios *old_termios)
286 {
287 	dbg("%s", __func__);
288 	tty_termios_copy_hw(tty->termios, old_termios);
289 	sierra_send_setup(tty, port);
290 }
291 
292 static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
293 {
294 	struct usb_serial_port *port = tty->driver_data;
295 	unsigned int value;
296 	struct sierra_port_private *portdata;
297 
298 	portdata = usb_get_serial_port_data(port);
299 
300 	value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
301 		((portdata->dtr_state) ? TIOCM_DTR : 0) |
302 		((portdata->cts_state) ? TIOCM_CTS : 0) |
303 		((portdata->dsr_state) ? TIOCM_DSR : 0) |
304 		((portdata->dcd_state) ? TIOCM_CAR : 0) |
305 		((portdata->ri_state) ? TIOCM_RNG : 0);
306 
307 	return value;
308 }
309 
310 static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
311 			unsigned int set, unsigned int clear)
312 {
313 	struct usb_serial_port *port = tty->driver_data;
314 	struct sierra_port_private *portdata;
315 
316 	portdata = usb_get_serial_port_data(port);
317 
318 	if (set & TIOCM_RTS)
319 		portdata->rts_state = 1;
320 	if (set & TIOCM_DTR)
321 		portdata->dtr_state = 1;
322 
323 	if (clear & TIOCM_RTS)
324 		portdata->rts_state = 0;
325 	if (clear & TIOCM_DTR)
326 		portdata->dtr_state = 0;
327 	return sierra_send_setup(tty, port);
328 }
329 
330 static void sierra_outdat_callback(struct urb *urb)
331 {
332 	struct usb_serial_port *port = urb->context;
333 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
334 	int status = urb->status;
335 	unsigned long flags;
336 
337 	dbg("%s - port %d", __func__, port->number);
338 
339 	/* free up the transfer buffer, as usb_free_urb() does not do this */
340 	kfree(urb->transfer_buffer);
341 
342 	if (status)
343 		dbg("%s - nonzero write bulk status received: %d",
344 		    __func__, status);
345 
346 	spin_lock_irqsave(&portdata->lock, flags);
347 	--portdata->outstanding_urbs;
348 	spin_unlock_irqrestore(&portdata->lock, flags);
349 
350 	usb_serial_port_softint(port);
351 }
352 
353 /* Write */
354 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
355 					const unsigned char *buf, int count)
356 {
357 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
358 	struct usb_serial *serial = port->serial;
359 	unsigned long flags;
360 	unsigned char *buffer;
361 	struct urb *urb;
362 	int status;
363 
364 	portdata = usb_get_serial_port_data(port);
365 
366 	dbg("%s: write (%d chars)", __func__, count);
367 
368 	spin_lock_irqsave(&portdata->lock, flags);
369 	if (portdata->outstanding_urbs > N_OUT_URB) {
370 		spin_unlock_irqrestore(&portdata->lock, flags);
371 		dbg("%s - write limit hit\n", __func__);
372 		return 0;
373 	}
374 	portdata->outstanding_urbs++;
375 	spin_unlock_irqrestore(&portdata->lock, flags);
376 
377 	buffer = kmalloc(count, GFP_ATOMIC);
378 	if (!buffer) {
379 		dev_err(&port->dev, "out of memory\n");
380 		count = -ENOMEM;
381 		goto error_no_buffer;
382 	}
383 
384 	urb = usb_alloc_urb(0, GFP_ATOMIC);
385 	if (!urb) {
386 		dev_err(&port->dev, "no more free urbs\n");
387 		count = -ENOMEM;
388 		goto error_no_urb;
389 	}
390 
391 	memcpy(buffer, buf, count);
392 
393 	usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
394 
395 	usb_fill_bulk_urb(urb, serial->dev,
396 			  usb_sndbulkpipe(serial->dev,
397 					  port->bulk_out_endpointAddress),
398 			  buffer, count, sierra_outdat_callback, port);
399 
400 	/* send it down the pipe */
401 	status = usb_submit_urb(urb, GFP_ATOMIC);
402 	if (status) {
403 		dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
404 			"with status = %d\n", __func__, status);
405 		count = status;
406 		goto error;
407 	}
408 
409 	/* we are done with this urb, so let the host driver
410 	 * really free it when it is finished with it */
411 	usb_free_urb(urb);
412 
413 	return count;
414 error:
415 	usb_free_urb(urb);
416 error_no_urb:
417 	kfree(buffer);
418 error_no_buffer:
419 	spin_lock_irqsave(&portdata->lock, flags);
420 	--portdata->outstanding_urbs;
421 	spin_unlock_irqrestore(&portdata->lock, flags);
422 	return count;
423 }
424 
425 static void sierra_indat_callback(struct urb *urb)
426 {
427 	int err;
428 	int endpoint;
429 	struct usb_serial_port *port;
430 	struct tty_struct *tty;
431 	unsigned char *data = urb->transfer_buffer;
432 	int status = urb->status;
433 
434 	dbg("%s: %p", __func__, urb);
435 
436 	endpoint = usb_pipeendpoint(urb->pipe);
437 	port =  urb->context;
438 
439 	if (status) {
440 		dbg("%s: nonzero status: %d on endpoint %02x.",
441 		    __func__, status, endpoint);
442 	} else {
443 		tty = port->port.tty;
444 		if (urb->actual_length) {
445 			tty_buffer_request_room(tty, urb->actual_length);
446 			tty_insert_flip_string(tty, data, urb->actual_length);
447 			tty_flip_buffer_push(tty);
448 		} else {
449 			dbg("%s: empty read urb received", __func__);
450 		}
451 
452 		/* Resubmit urb so we continue receiving */
453 		if (port->port.count && status != -ESHUTDOWN) {
454 			err = usb_submit_urb(urb, GFP_ATOMIC);
455 			if (err)
456 				dev_err(&port->dev, "resubmit read urb failed."
457 					"(%d)\n", err);
458 		}
459 	}
460 	return;
461 }
462 
463 static void sierra_instat_callback(struct urb *urb)
464 {
465 	int err;
466 	int status = urb->status;
467 	struct usb_serial_port *port =  urb->context;
468 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
469 	struct usb_serial *serial = port->serial;
470 
471 	dbg("%s", __func__);
472 	dbg("%s: urb %p port %p has data %p", __func__, urb, port, portdata);
473 
474 	if (status == 0) {
475 		struct usb_ctrlrequest *req_pkt =
476 				(struct usb_ctrlrequest *)urb->transfer_buffer;
477 
478 		if (!req_pkt) {
479 			dbg("%s: NULL req_pkt\n", __func__);
480 			return;
481 		}
482 		if ((req_pkt->bRequestType == 0xA1) &&
483 				(req_pkt->bRequest == 0x20)) {
484 			int old_dcd_state;
485 			unsigned char signals = *((unsigned char *)
486 					urb->transfer_buffer +
487 					sizeof(struct usb_ctrlrequest));
488 
489 			dbg("%s: signal x%x", __func__, signals);
490 
491 			old_dcd_state = portdata->dcd_state;
492 			portdata->cts_state = 1;
493 			portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
494 			portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
495 			portdata->ri_state = ((signals & 0x08) ? 1 : 0);
496 
497 			if (port->port.tty && !C_CLOCAL(port->port.tty) &&
498 					old_dcd_state && !portdata->dcd_state)
499 				tty_hangup(port->port.tty);
500 		} else {
501 			dbg("%s: type %x req %x", __func__,
502 				req_pkt->bRequestType, req_pkt->bRequest);
503 		}
504 	} else
505 		dbg("%s: error %d", __func__, status);
506 
507 	/* Resubmit urb so we continue receiving IRQ data */
508 	if (status != -ESHUTDOWN) {
509 		urb->dev = serial->dev;
510 		err = usb_submit_urb(urb, GFP_ATOMIC);
511 		if (err)
512 			dbg("%s: resubmit intr urb failed. (%d)",
513 				__func__, err);
514 	}
515 }
516 
517 static int sierra_write_room(struct tty_struct *tty)
518 {
519 	struct usb_serial_port *port = tty->driver_data;
520 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
521 	unsigned long flags;
522 
523 	dbg("%s - port %d", __func__, port->number);
524 
525 	/* try to give a good number back based on if we have any free urbs at
526 	 * this point in time */
527 	spin_lock_irqsave(&portdata->lock, flags);
528 	if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
529 		spin_unlock_irqrestore(&portdata->lock, flags);
530 		dbg("%s - write limit hit\n", __func__);
531 		return 0;
532 	}
533 	spin_unlock_irqrestore(&portdata->lock, flags);
534 
535 	return 2048;
536 }
537 
538 static int sierra_open(struct tty_struct *tty,
539 			struct usb_serial_port *port, struct file *filp)
540 {
541 	struct sierra_port_private *portdata;
542 	struct usb_serial *serial = port->serial;
543 	int i;
544 	struct urb *urb;
545 	int result;
546 
547 	portdata = usb_get_serial_port_data(port);
548 
549 	dbg("%s", __func__);
550 
551 	/* Set some sane defaults */
552 	portdata->rts_state = 1;
553 	portdata->dtr_state = 1;
554 
555 	/* Reset low level data toggle and start reading from endpoints */
556 	for (i = 0; i < N_IN_URB; i++) {
557 		urb = portdata->in_urbs[i];
558 		if (!urb)
559 			continue;
560 		if (urb->dev != serial->dev) {
561 			dbg("%s: dev %p != %p", __func__,
562 				urb->dev, serial->dev);
563 			continue;
564 		}
565 
566 		/*
567 		 * make sure endpoint data toggle is synchronized with the
568 		 * device
569 		 */
570 		usb_clear_halt(urb->dev, urb->pipe);
571 
572 		result = usb_submit_urb(urb, GFP_KERNEL);
573 		if (result) {
574 			dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
575 				i, result, urb->transfer_buffer_length);
576 		}
577 	}
578 
579 	if (tty)
580 		tty->low_latency = 1;
581 
582 	sierra_send_setup(tty, port);
583 
584 	/* start up the interrupt endpoint if we have one */
585 	if (port->interrupt_in_urb) {
586 		result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
587 		if (result)
588 			dev_err(&port->dev, "submit irq_in urb failed %d\n",
589 				result);
590 	}
591 	return 0;
592 }
593 
594 static void sierra_close(struct tty_struct *tty,
595 			struct usb_serial_port *port, struct file *filp)
596 {
597 	int i;
598 	struct usb_serial *serial = port->serial;
599 	struct sierra_port_private *portdata;
600 
601 	dbg("%s", __func__);
602 	portdata = usb_get_serial_port_data(port);
603 
604 	portdata->rts_state = 0;
605 	portdata->dtr_state = 0;
606 
607 	if (serial->dev) {
608 		mutex_lock(&serial->disc_mutex);
609 		if (!serial->disconnected)
610 			sierra_send_setup(tty, port);
611 		mutex_unlock(&serial->disc_mutex);
612 
613 		/* Stop reading/writing urbs */
614 		for (i = 0; i < N_IN_URB; i++)
615 			usb_kill_urb(portdata->in_urbs[i]);
616 	}
617 
618 	usb_kill_urb(port->interrupt_in_urb);
619 
620 	port->port.tty = NULL;	/* FIXME */
621 }
622 
623 static int sierra_startup(struct usb_serial *serial)
624 {
625 	struct usb_serial_port *port;
626 	struct sierra_port_private *portdata;
627 	struct urb *urb;
628 	int i;
629 	int j;
630 
631 	dbg("%s", __func__);
632 
633 	/* Set Device mode to D0 */
634 	sierra_set_power_state(serial->dev, 0x0000);
635 
636 	/* Check NMEA and set */
637 	if (nmea)
638 		sierra_vsc_set_nmea(serial->dev, 1);
639 
640 	/* Now setup per port private data */
641 	for (i = 0; i < serial->num_ports; i++) {
642 		port = serial->port[i];
643 		portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
644 		if (!portdata) {
645 			dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
646 					__func__, i);
647 			return -ENOMEM;
648 		}
649 		spin_lock_init(&portdata->lock);
650 		for (j = 0; j < N_IN_URB; j++) {
651 			portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
652 			if (!portdata->in_buffer[j]) {
653 				for (--j; j >= 0; j--)
654 					kfree(portdata->in_buffer[j]);
655 				kfree(portdata);
656 				return -ENOMEM;
657 			}
658 		}
659 
660 		usb_set_serial_port_data(port, portdata);
661 
662 		/* initialize the in urbs */
663 		for (j = 0; j < N_IN_URB; ++j) {
664 			urb = usb_alloc_urb(0, GFP_KERNEL);
665 			if (urb == NULL) {
666 				dbg("%s: alloc for in port failed.",
667 				    __func__);
668 				continue;
669 			}
670 			/* Fill URB using supplied data. */
671 			usb_fill_bulk_urb(urb, serial->dev,
672 					  usb_rcvbulkpipe(serial->dev,
673 						port->bulk_in_endpointAddress),
674 					  portdata->in_buffer[j], IN_BUFLEN,
675 					  sierra_indat_callback, port);
676 			portdata->in_urbs[j] = urb;
677 		}
678 	}
679 
680 	return 0;
681 }
682 
683 static void sierra_shutdown(struct usb_serial *serial)
684 {
685 	int i, j;
686 	struct usb_serial_port *port;
687 	struct sierra_port_private *portdata;
688 
689 	dbg("%s", __func__);
690 
691 	for (i = 0; i < serial->num_ports; ++i) {
692 		port = serial->port[i];
693 		if (!port)
694 			continue;
695 		portdata = usb_get_serial_port_data(port);
696 		if (!portdata)
697 			continue;
698 
699 		for (j = 0; j < N_IN_URB; j++) {
700 			usb_kill_urb(portdata->in_urbs[j]);
701 			usb_free_urb(portdata->in_urbs[j]);
702 			kfree(portdata->in_buffer[j]);
703 		}
704 		kfree(portdata);
705 		usb_set_serial_port_data(port, NULL);
706 	}
707 }
708 
709 static struct usb_serial_driver sierra_device = {
710 	.driver = {
711 		.owner =	THIS_MODULE,
712 		.name =		"sierra",
713 	},
714 	.description       = "Sierra USB modem",
715 	.id_table          = id_table,
716 	.usb_driver        = &sierra_driver,
717 	.calc_num_ports	   = sierra_calc_num_ports,
718 	.probe		   = sierra_probe,
719 	.open              = sierra_open,
720 	.close             = sierra_close,
721 	.write             = sierra_write,
722 	.write_room        = sierra_write_room,
723 	.set_termios       = sierra_set_termios,
724 	.tiocmget          = sierra_tiocmget,
725 	.tiocmset          = sierra_tiocmset,
726 	.attach            = sierra_startup,
727 	.shutdown          = sierra_shutdown,
728 	.read_int_callback = sierra_instat_callback,
729 };
730 
731 /* Functions used by new usb-serial code. */
732 static int __init sierra_init(void)
733 {
734 	int retval;
735 	retval = usb_serial_register(&sierra_device);
736 	if (retval)
737 		goto failed_device_register;
738 
739 
740 	retval = usb_register(&sierra_driver);
741 	if (retval)
742 		goto failed_driver_register;
743 
744 	info(DRIVER_DESC ": " DRIVER_VERSION);
745 
746 	return 0;
747 
748 failed_driver_register:
749 	usb_serial_deregister(&sierra_device);
750 failed_device_register:
751 	return retval;
752 }
753 
754 static void __exit sierra_exit(void)
755 {
756 	usb_deregister(&sierra_driver);
757 	usb_serial_deregister(&sierra_device);
758 }
759 
760 module_init(sierra_init);
761 module_exit(sierra_exit);
762 
763 MODULE_AUTHOR(DRIVER_AUTHOR);
764 MODULE_DESCRIPTION(DRIVER_DESC);
765 MODULE_VERSION(DRIVER_VERSION);
766 MODULE_LICENSE("GPL");
767 
768 module_param(nmea, bool, S_IRUGO | S_IWUSR);
769 MODULE_PARM_DESC(nmea, "NMEA streaming");
770 
771 module_param(debug, bool, S_IRUGO | S_IWUSR);
772 MODULE_PARM_DESC(debug, "Debug messages");
773