xref: /linux/drivers/usb/serial/sierra.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3   USB Driver for Sierra Wireless
4 
5   Copyright (C) 2006, 2007, 2008  Kevin Lloyd <klloyd@sierrawireless.com>,
6 
7   Copyright (C) 2008, 2009  Elina Pasheva, Matthew Safar, Rory Filer
8 			<linux@sierrawireless.com>
9 
10   IMPORTANT DISCLAIMER: This driver is not commercially supported by
11   Sierra Wireless. Use at your own risk.
12 
13   This driver is free software; you can redistribute it and/or modify
14   it under the terms of Version 2 of the GNU General Public License as
15   published by the Free Software Foundation.
16 
17   Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
18   Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
19 */
20 /* Uncomment to log function calls */
21 /* #define DEBUG */
22 
23 #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
24 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
25 
26 #include <linux/kernel.h>
27 #include <linux/jiffies.h>
28 #include <linux/errno.h>
29 #include <linux/tty.h>
30 #include <linux/slab.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 
36 #define SWIMS_USB_REQUEST_SetPower	0x00
37 #define SWIMS_USB_REQUEST_SetNmea	0x07
38 
39 #define N_IN_URB_HM	8
40 #define N_OUT_URB_HM	64
41 #define N_IN_URB	4
42 #define N_OUT_URB	4
43 #define IN_BUFLEN	4096
44 
45 #define MAX_TRANSFER		(PAGE_SIZE - 512)
46 /* MAX_TRANSFER is chosen so that the VM is not stressed by
47    allocations > PAGE_SIZE and the number of packets in a page
48    is an integer 512 is the largest possible packet on EHCI */
49 
50 static bool nmea;
51 
52 /* Used in interface blacklisting */
53 struct sierra_iface_info {
54 	const u32 infolen;	/* number of interface numbers on blacklist */
55 	const u8  *ifaceinfo;	/* pointer to the array holding the numbers */
56 };
57 
58 struct sierra_intf_private {
59 	spinlock_t susp_lock;
60 	unsigned int suspended:1;
61 	int in_flight;
62 	unsigned int open_ports;
63 };
64 
65 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
66 {
67 	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
68 			SWIMS_USB_REQUEST_SetPower,	/* __u8 request      */
69 			USB_TYPE_VENDOR,		/* __u8 request type */
70 			swiState,			/* __u16 value       */
71 			0,				/* __u16 index       */
72 			NULL,				/* void *data        */
73 			0,				/* __u16 size 	     */
74 			USB_CTRL_SET_TIMEOUT);		/* int timeout 	     */
75 }
76 
77 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
78 {
79 	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
80 			SWIMS_USB_REQUEST_SetNmea,	/* __u8 request      */
81 			USB_TYPE_VENDOR,		/* __u8 request type */
82 			enable,				/* __u16 value       */
83 			0x0000,				/* __u16 index       */
84 			NULL,				/* void *data        */
85 			0,				/* __u16 size 	     */
86 			USB_CTRL_SET_TIMEOUT);		/* int timeout       */
87 }
88 
89 static int sierra_calc_num_ports(struct usb_serial *serial,
90 					struct usb_serial_endpoints *epds)
91 {
92 	int num_ports = 0;
93 	u8 ifnum, numendpoints;
94 
95 	ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
96 	numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
97 
98 	/* Dummy interface present on some SKUs should be ignored */
99 	if (ifnum == 0x99)
100 		num_ports = 0;
101 	else if (numendpoints <= 3)
102 		num_ports = 1;
103 	else
104 		num_ports = (numendpoints-1)/2;
105 	return num_ports;
106 }
107 
108 static int is_blacklisted(const u8 ifnum,
109 				const struct sierra_iface_info *blacklist)
110 {
111 	const u8  *info;
112 	int i;
113 
114 	if (blacklist) {
115 		info = blacklist->ifaceinfo;
116 
117 		for (i = 0; i < blacklist->infolen; i++) {
118 			if (info[i] == ifnum)
119 				return 1;
120 		}
121 	}
122 	return 0;
123 }
124 
125 static int is_himemory(const u8 ifnum,
126 				const struct sierra_iface_info *himemorylist)
127 {
128 	const u8  *info;
129 	int i;
130 
131 	if (himemorylist) {
132 		info = himemorylist->ifaceinfo;
133 
134 		for (i=0; i < himemorylist->infolen; i++) {
135 			if (info[i] == ifnum)
136 				return 1;
137 		}
138 	}
139 	return 0;
140 }
141 
142 static u8 sierra_interface_num(struct usb_serial *serial)
143 {
144 	return serial->interface->cur_altsetting->desc.bInterfaceNumber;
145 }
146 
147 static int sierra_probe(struct usb_serial *serial,
148 			const struct usb_device_id *id)
149 {
150 	int result = 0;
151 	struct usb_device *udev;
152 	u8 ifnum;
153 
154 	udev = serial->dev;
155 	ifnum = sierra_interface_num(serial);
156 
157 	/*
158 	 * If this interface supports more than 1 alternate
159 	 * select the 2nd one
160 	 */
161 	if (serial->interface->num_altsetting == 2) {
162 		dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
163 			ifnum);
164 		/* We know the alternate setting is 1 for the MC8785 */
165 		usb_set_interface(udev, ifnum, 1);
166 	}
167 
168 	if (is_blacklisted(ifnum,
169 				(struct sierra_iface_info *)id->driver_info)) {
170 		dev_dbg(&serial->dev->dev,
171 			"Ignoring blacklisted interface #%d\n", ifnum);
172 		return -ENODEV;
173 	}
174 
175 	return result;
176 }
177 
178 /* interfaces with higher memory requirements */
179 static const u8 hi_memory_typeA_ifaces[] = { 0, 2 };
180 static const struct sierra_iface_info typeA_interface_list = {
181 	.infolen = ARRAY_SIZE(hi_memory_typeA_ifaces),
182 	.ifaceinfo = hi_memory_typeA_ifaces,
183 };
184 
185 static const u8 hi_memory_typeB_ifaces[] = { 3, 4, 5, 6 };
186 static const struct sierra_iface_info typeB_interface_list = {
187 	.infolen = ARRAY_SIZE(hi_memory_typeB_ifaces),
188 	.ifaceinfo = hi_memory_typeB_ifaces,
189 };
190 
191 /* 'blacklist' of interfaces not served by this driver */
192 static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11, 19, 20 };
193 static const struct sierra_iface_info direct_ip_interface_blacklist = {
194 	.infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
195 	.ifaceinfo = direct_ip_non_serial_ifaces,
196 };
197 
198 static const struct usb_device_id id_table[] = {
199 	{ USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
200 	{ USB_DEVICE(0x03F0, 0x1B1D) },	/* HP ev2200 a.k.a MC5720 */
201 	{ USB_DEVICE(0x03F0, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
202 	{ USB_DEVICE(0x03F0, 0x1E1D) },	/* HP hs2300 a.k.a MC8775 */
203 
204 	{ USB_DEVICE(0x1199, 0x0017) },	/* Sierra Wireless EM5625 */
205 	{ USB_DEVICE(0x1199, 0x0018) },	/* Sierra Wireless MC5720 */
206 	{ USB_DEVICE(0x1199, 0x0218) },	/* Sierra Wireless MC5720 */
207 	{ USB_DEVICE(0x1199, 0x0020) },	/* Sierra Wireless MC5725 */
208 	{ USB_DEVICE(0x1199, 0x0220) },	/* Sierra Wireless MC5725 */
209 	{ USB_DEVICE(0x1199, 0x0022) },	/* Sierra Wireless EM5725 */
210 	{ USB_DEVICE(0x1199, 0x0024) },	/* Sierra Wireless MC5727 */
211 	{ USB_DEVICE(0x1199, 0x0224) },	/* Sierra Wireless MC5727 */
212 	{ USB_DEVICE(0x1199, 0x0019) },	/* Sierra Wireless AirCard 595 */
213 	{ USB_DEVICE(0x1199, 0x0021) },	/* Sierra Wireless AirCard 597E */
214 	{ USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
215 	{ USB_DEVICE(0x1199, 0x0120) },	/* Sierra Wireless USB Dongle 595U */
216 	{ USB_DEVICE(0x1199, 0x0301) },	/* Sierra Wireless USB Dongle 250U */
217 	/* Sierra Wireless C597 */
218 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
219 	/* Sierra Wireless T598 */
220 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
221 	{ USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
222 	{ USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
223 	{ USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
224 	{ USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
225 
226 	{ USB_DEVICE(0x1199, 0x6802) },	/* Sierra Wireless MC8755 */
227 	{ USB_DEVICE(0x1199, 0x6803) },	/* Sierra Wireless MC8765 */
228 	{ USB_DEVICE(0x1199, 0x6804) },	/* Sierra Wireless MC8755 */
229 	{ USB_DEVICE(0x1199, 0x6805) },	/* Sierra Wireless MC8765 */
230 	{ USB_DEVICE(0x1199, 0x6808) },	/* Sierra Wireless MC8755 */
231 	{ USB_DEVICE(0x1199, 0x6809) },	/* Sierra Wireless MC8765 */
232 	{ USB_DEVICE(0x1199, 0x6812) },	/* Sierra Wireless MC8775 & AC 875U */
233 	{ USB_DEVICE(0x1199, 0x6813) },	/* Sierra Wireless MC8775 */
234 	{ USB_DEVICE(0x1199, 0x6815) },	/* Sierra Wireless MC8775 */
235 	{ USB_DEVICE(0x1199, 0x6816) },	/* Sierra Wireless MC8775 */
236 	{ USB_DEVICE(0x1199, 0x6820) },	/* Sierra Wireless AirCard 875 */
237 	{ USB_DEVICE(0x1199, 0x6821) },	/* Sierra Wireless AirCard 875U */
238 	{ USB_DEVICE(0x1199, 0x6822) },	/* Sierra Wireless AirCard 875E */
239 	{ USB_DEVICE(0x1199, 0x6832) },	/* Sierra Wireless MC8780 */
240 	{ USB_DEVICE(0x1199, 0x6833) },	/* Sierra Wireless MC8781 */
241 	{ USB_DEVICE(0x1199, 0x6834) },	/* Sierra Wireless MC8780 */
242 	{ USB_DEVICE(0x1199, 0x6835) },	/* Sierra Wireless MC8781 */
243 	{ USB_DEVICE(0x1199, 0x6838) },	/* Sierra Wireless MC8780 */
244 	{ USB_DEVICE(0x1199, 0x6839) },	/* Sierra Wireless MC8781 */
245 	{ USB_DEVICE(0x1199, 0x683A) },	/* Sierra Wireless MC8785 */
246 	{ USB_DEVICE(0x1199, 0x683B) },	/* Sierra Wireless MC8785 Composite */
247 	/* Sierra Wireless MC8790, MC8791, MC8792 Composite */
248 	{ USB_DEVICE(0x1199, 0x683C) },
249 	{ USB_DEVICE(0x1199, 0x683D) },	/* Sierra Wireless MC8791 Composite */
250 	/* Sierra Wireless MC8790, MC8791, MC8792 */
251 	{ USB_DEVICE(0x1199, 0x683E) },
252 	{ USB_DEVICE(0x1199, 0x6850) },	/* Sierra Wireless AirCard 880 */
253 	{ USB_DEVICE(0x1199, 0x6851) },	/* Sierra Wireless AirCard 881 */
254 	{ USB_DEVICE(0x1199, 0x6852) },	/* Sierra Wireless AirCard 880 E */
255 	{ USB_DEVICE(0x1199, 0x6853) },	/* Sierra Wireless AirCard 881 E */
256 	{ USB_DEVICE(0x1199, 0x6855) },	/* Sierra Wireless AirCard 880 U */
257 	{ USB_DEVICE(0x1199, 0x6856) },	/* Sierra Wireless AirCard 881 U */
258 	{ USB_DEVICE(0x1199, 0x6859) },	/* Sierra Wireless AirCard 885 E */
259 	{ USB_DEVICE(0x1199, 0x685A) },	/* Sierra Wireless AirCard 885 E */
260 	/* Sierra Wireless C885 */
261 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
262 	/* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
263 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
264 	/* Sierra Wireless C22/C33 */
265 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
266 	/* Sierra Wireless HSPA Non-Composite Device */
267 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
268 	{ USB_DEVICE(0x1199, 0x6893) },	/* Sierra Wireless Device */
269 	/* Sierra Wireless Direct IP modems */
270 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
271 	  .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
272 	},
273 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
274 	  .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
275 	},
276 	{ USB_DEVICE(0x1199, 0x68AB) }, /* Sierra Wireless AR8550 */
277 	/* AT&T Direct IP LTE modems */
278 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
279 	  .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
280 	},
281 	/* Airprime/Sierra Wireless Direct IP modems */
282 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
283 	  .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
284 	},
285 
286 	{ }
287 };
288 MODULE_DEVICE_TABLE(usb, id_table);
289 
290 
291 struct sierra_port_private {
292 	spinlock_t lock;	/* lock the structure */
293 	int outstanding_urbs;	/* number of out urbs in flight */
294 	struct usb_anchor active;
295 	struct usb_anchor delayed;
296 
297 	int num_out_urbs;
298 	int num_in_urbs;
299 	/* Input endpoints and buffers for this port */
300 	struct urb *in_urbs[N_IN_URB_HM];
301 
302 	/* Settings for the port */
303 	int rts_state;	/* Handshaking pins (outputs) */
304 	int dtr_state;
305 	int cts_state;	/* Handshaking pins (inputs) */
306 	int dsr_state;
307 	int dcd_state;
308 	int ri_state;
309 };
310 
311 static int sierra_send_setup(struct usb_serial_port *port)
312 {
313 	struct usb_serial *serial = port->serial;
314 	struct sierra_port_private *portdata;
315 	__u16 interface = 0;
316 	int val = 0;
317 	int do_send = 0;
318 	int retval;
319 
320 	portdata = usb_get_serial_port_data(port);
321 
322 	if (portdata->dtr_state)
323 		val |= 0x01;
324 	if (portdata->rts_state)
325 		val |= 0x02;
326 
327 	/* If composite device then properly report interface */
328 	if (serial->num_ports == 1) {
329 		interface = sierra_interface_num(serial);
330 		/* Control message is sent only to interfaces with
331 		 * interrupt_in endpoints
332 		 */
333 		if (port->interrupt_in_urb) {
334 			/* send control message */
335 			do_send = 1;
336 		}
337 	}
338 
339 	/* Otherwise the need to do non-composite mapping */
340 	else {
341 		if (port->bulk_out_endpointAddress == 2)
342 			interface = 0;
343 		else if (port->bulk_out_endpointAddress == 4)
344 			interface = 1;
345 		else if (port->bulk_out_endpointAddress == 5)
346 			interface = 2;
347 
348 		do_send = 1;
349 	}
350 	if (!do_send)
351 		return 0;
352 
353 	retval = usb_autopm_get_interface(serial->interface);
354 	if (retval < 0)
355 		return retval;
356 
357 	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
358 		0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
359 	usb_autopm_put_interface(serial->interface);
360 
361 	return retval;
362 }
363 
364 static int sierra_tiocmget(struct tty_struct *tty)
365 {
366 	struct usb_serial_port *port = tty->driver_data;
367 	unsigned int value;
368 	struct sierra_port_private *portdata;
369 
370 	portdata = usb_get_serial_port_data(port);
371 
372 	value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
373 		((portdata->dtr_state) ? TIOCM_DTR : 0) |
374 		((portdata->cts_state) ? TIOCM_CTS : 0) |
375 		((portdata->dsr_state) ? TIOCM_DSR : 0) |
376 		((portdata->dcd_state) ? TIOCM_CAR : 0) |
377 		((portdata->ri_state) ? TIOCM_RNG : 0);
378 
379 	return value;
380 }
381 
382 static int sierra_tiocmset(struct tty_struct *tty,
383 			unsigned int set, unsigned int clear)
384 {
385 	struct usb_serial_port *port = tty->driver_data;
386 	struct sierra_port_private *portdata;
387 
388 	portdata = usb_get_serial_port_data(port);
389 
390 	if (set & TIOCM_RTS)
391 		portdata->rts_state = 1;
392 	if (set & TIOCM_DTR)
393 		portdata->dtr_state = 1;
394 
395 	if (clear & TIOCM_RTS)
396 		portdata->rts_state = 0;
397 	if (clear & TIOCM_DTR)
398 		portdata->dtr_state = 0;
399 	return sierra_send_setup(port);
400 }
401 
402 static void sierra_release_urb(struct urb *urb)
403 {
404 	if (urb) {
405 		kfree(urb->transfer_buffer);
406 		usb_free_urb(urb);
407 	}
408 }
409 
410 static void sierra_outdat_callback(struct urb *urb)
411 {
412 	struct usb_serial_port *port = urb->context;
413 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
414 	struct sierra_intf_private *intfdata;
415 	int status = urb->status;
416 
417 	intfdata = usb_get_serial_data(port->serial);
418 
419 	/* free up the transfer buffer, as usb_free_urb() does not do this */
420 	kfree(urb->transfer_buffer);
421 	usb_autopm_put_interface_async(port->serial->interface);
422 	if (status)
423 		dev_dbg(&port->dev, "%s - nonzero write bulk status "
424 		    "received: %d\n", __func__, status);
425 
426 	spin_lock(&portdata->lock);
427 	--portdata->outstanding_urbs;
428 	spin_unlock(&portdata->lock);
429 	spin_lock(&intfdata->susp_lock);
430 	--intfdata->in_flight;
431 	spin_unlock(&intfdata->susp_lock);
432 
433 	usb_serial_port_softint(port);
434 }
435 
436 /* Write */
437 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
438 					const unsigned char *buf, int count)
439 {
440 	struct sierra_port_private *portdata;
441 	struct sierra_intf_private *intfdata;
442 	struct usb_serial *serial = port->serial;
443 	unsigned long flags;
444 	unsigned char *buffer;
445 	struct urb *urb;
446 	size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
447 	int retval = 0;
448 
449 	/* verify that we actually have some data to write */
450 	if (count == 0)
451 		return 0;
452 
453 	portdata = usb_get_serial_port_data(port);
454 	intfdata = usb_get_serial_data(serial);
455 
456 	dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
457 	spin_lock_irqsave(&portdata->lock, flags);
458 	dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
459 		portdata->outstanding_urbs);
460 	if (portdata->outstanding_urbs > portdata->num_out_urbs) {
461 		spin_unlock_irqrestore(&portdata->lock, flags);
462 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
463 		return 0;
464 	}
465 	portdata->outstanding_urbs++;
466 	dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
467 		portdata->outstanding_urbs);
468 	spin_unlock_irqrestore(&portdata->lock, flags);
469 
470 	retval = usb_autopm_get_interface_async(serial->interface);
471 	if (retval < 0) {
472 		spin_lock_irqsave(&portdata->lock, flags);
473 		portdata->outstanding_urbs--;
474 		spin_unlock_irqrestore(&portdata->lock, flags);
475 		goto error_simple;
476 	}
477 
478 	buffer = kmalloc(writesize, GFP_ATOMIC);
479 	if (!buffer) {
480 		retval = -ENOMEM;
481 		goto error_no_buffer;
482 	}
483 
484 	urb = usb_alloc_urb(0, GFP_ATOMIC);
485 	if (!urb) {
486 		retval = -ENOMEM;
487 		goto error_no_urb;
488 	}
489 
490 	memcpy(buffer, buf, writesize);
491 
492 	usb_serial_debug_data(&port->dev, __func__, writesize, buffer);
493 
494 	usb_fill_bulk_urb(urb, serial->dev,
495 			  usb_sndbulkpipe(serial->dev,
496 					  port->bulk_out_endpointAddress),
497 			  buffer, writesize, sierra_outdat_callback, port);
498 
499 	/* Handle the need to send a zero length packet */
500 	urb->transfer_flags |= URB_ZERO_PACKET;
501 
502 	spin_lock_irqsave(&intfdata->susp_lock, flags);
503 
504 	if (intfdata->suspended) {
505 		usb_anchor_urb(urb, &portdata->delayed);
506 		spin_unlock_irqrestore(&intfdata->susp_lock, flags);
507 		goto skip_power;
508 	} else {
509 		usb_anchor_urb(urb, &portdata->active);
510 	}
511 	/* send it down the pipe */
512 	retval = usb_submit_urb(urb, GFP_ATOMIC);
513 	if (retval) {
514 		usb_unanchor_urb(urb);
515 		spin_unlock_irqrestore(&intfdata->susp_lock, flags);
516 		dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
517 			"with status = %d\n", __func__, retval);
518 		goto error;
519 	} else {
520 		intfdata->in_flight++;
521 		spin_unlock_irqrestore(&intfdata->susp_lock, flags);
522 	}
523 
524 skip_power:
525 	/* we are done with this urb, so let the host driver
526 	 * really free it when it is finished with it */
527 	usb_free_urb(urb);
528 
529 	return writesize;
530 error:
531 	usb_free_urb(urb);
532 error_no_urb:
533 	kfree(buffer);
534 error_no_buffer:
535 	spin_lock_irqsave(&portdata->lock, flags);
536 	--portdata->outstanding_urbs;
537 	dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
538 		portdata->outstanding_urbs);
539 	spin_unlock_irqrestore(&portdata->lock, flags);
540 	usb_autopm_put_interface_async(serial->interface);
541 error_simple:
542 	return retval;
543 }
544 
545 static void sierra_indat_callback(struct urb *urb)
546 {
547 	int err;
548 	int endpoint;
549 	struct usb_serial_port *port;
550 	unsigned char *data = urb->transfer_buffer;
551 	int status = urb->status;
552 
553 	endpoint = usb_pipeendpoint(urb->pipe);
554 	port = urb->context;
555 
556 	if (status) {
557 		dev_dbg(&port->dev, "%s: nonzero status: %d on"
558 			" endpoint %02x\n", __func__, status, endpoint);
559 	} else {
560 		if (urb->actual_length) {
561 			tty_insert_flip_string(&port->port, data,
562 				urb->actual_length);
563 			tty_flip_buffer_push(&port->port);
564 
565 			usb_serial_debug_data(&port->dev, __func__,
566 					      urb->actual_length, data);
567 		} else {
568 			dev_dbg(&port->dev, "%s: empty read urb"
569 				" received\n", __func__);
570 		}
571 	}
572 
573 	/* Resubmit urb so we continue receiving */
574 	if (status != -ESHUTDOWN && status != -EPERM) {
575 		usb_mark_last_busy(port->serial->dev);
576 		err = usb_submit_urb(urb, GFP_ATOMIC);
577 		if (err && err != -EPERM)
578 			dev_err(&port->dev, "resubmit read urb failed."
579 				"(%d)\n", err);
580 	}
581 }
582 
583 static void sierra_instat_callback(struct urb *urb)
584 {
585 	int err;
586 	int status = urb->status;
587 	struct usb_serial_port *port =  urb->context;
588 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
589 	struct usb_serial *serial = port->serial;
590 
591 	dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
592 		urb, port, portdata);
593 
594 	if (status == 0) {
595 		struct usb_ctrlrequest *req_pkt =
596 				(struct usb_ctrlrequest *)urb->transfer_buffer;
597 
598 		if (!req_pkt) {
599 			dev_dbg(&port->dev, "%s: NULL req_pkt\n",
600 				__func__);
601 			return;
602 		}
603 		if ((req_pkt->bRequestType == 0xA1) &&
604 				(req_pkt->bRequest == 0x20)) {
605 			int old_dcd_state;
606 			unsigned char signals = *((unsigned char *)
607 					urb->transfer_buffer +
608 					sizeof(struct usb_ctrlrequest));
609 
610 			dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
611 				signals);
612 
613 			old_dcd_state = portdata->dcd_state;
614 			portdata->cts_state = 1;
615 			portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
616 			portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
617 			portdata->ri_state = ((signals & 0x08) ? 1 : 0);
618 
619 			if (old_dcd_state && !portdata->dcd_state)
620 				tty_port_tty_hangup(&port->port, true);
621 		} else {
622 			dev_dbg(&port->dev, "%s: type %x req %x\n",
623 				__func__, req_pkt->bRequestType,
624 				req_pkt->bRequest);
625 		}
626 	} else
627 		dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
628 
629 	/* Resubmit urb so we continue receiving IRQ data */
630 	if (status != -ESHUTDOWN && status != -ENOENT) {
631 		usb_mark_last_busy(serial->dev);
632 		err = usb_submit_urb(urb, GFP_ATOMIC);
633 		if (err && err != -EPERM)
634 			dev_err(&port->dev, "%s: resubmit intr urb "
635 				"failed. (%d)\n", __func__, err);
636 	}
637 }
638 
639 static int sierra_write_room(struct tty_struct *tty)
640 {
641 	struct usb_serial_port *port = tty->driver_data;
642 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
643 	unsigned long flags;
644 
645 	/* try to give a good number back based on if we have any free urbs at
646 	 * this point in time */
647 	spin_lock_irqsave(&portdata->lock, flags);
648 	if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
649 		spin_unlock_irqrestore(&portdata->lock, flags);
650 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
651 		return 0;
652 	}
653 	spin_unlock_irqrestore(&portdata->lock, flags);
654 
655 	return 2048;
656 }
657 
658 static int sierra_chars_in_buffer(struct tty_struct *tty)
659 {
660 	struct usb_serial_port *port = tty->driver_data;
661 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
662 	unsigned long flags;
663 	int chars;
664 
665 	/* NOTE: This overcounts somewhat. */
666 	spin_lock_irqsave(&portdata->lock, flags);
667 	chars = portdata->outstanding_urbs * MAX_TRANSFER;
668 	spin_unlock_irqrestore(&portdata->lock, flags);
669 
670 	dev_dbg(&port->dev, "%s - %d\n", __func__, chars);
671 
672 	return chars;
673 }
674 
675 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
676 {
677 	int i;
678 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
679 
680 	for (i = 0; i < portdata->num_in_urbs; i++)
681 		usb_kill_urb(portdata->in_urbs[i]);
682 
683 	usb_kill_urb(port->interrupt_in_urb);
684 }
685 
686 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
687 {
688 	int ok_cnt;
689 	int err = -EINVAL;
690 	int i;
691 	struct urb *urb;
692 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
693 
694 	ok_cnt = 0;
695 	for (i = 0; i < portdata->num_in_urbs; i++) {
696 		urb = portdata->in_urbs[i];
697 		if (!urb)
698 			continue;
699 		err = usb_submit_urb(urb, mem_flags);
700 		if (err) {
701 			dev_err(&port->dev, "%s: submit urb failed: %d\n",
702 				__func__, err);
703 		} else {
704 			ok_cnt++;
705 		}
706 	}
707 
708 	if (ok_cnt && port->interrupt_in_urb) {
709 		err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
710 		if (err) {
711 			dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
712 				__func__, err);
713 		}
714 	}
715 
716 	if (ok_cnt > 0) /* at least one rx urb submitted */
717 		return 0;
718 	else
719 		return err;
720 }
721 
722 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
723 					int dir, void *ctx, int len,
724 					gfp_t mem_flags,
725 					usb_complete_t callback)
726 {
727 	struct urb	*urb;
728 	u8		*buf;
729 
730 	urb = usb_alloc_urb(0, mem_flags);
731 	if (!urb)
732 		return NULL;
733 
734 	buf = kmalloc(len, mem_flags);
735 	if (buf) {
736 		/* Fill URB using supplied data */
737 		usb_fill_bulk_urb(urb, serial->dev,
738 			usb_sndbulkpipe(serial->dev, endpoint) | dir,
739 			buf, len, callback, ctx);
740 
741 		dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
742 				dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
743 	} else {
744 		sierra_release_urb(urb);
745 		urb = NULL;
746 	}
747 
748 	return urb;
749 }
750 
751 static void sierra_close(struct usb_serial_port *port)
752 {
753 	int i;
754 	struct usb_serial *serial = port->serial;
755 	struct sierra_port_private *portdata;
756 	struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
757 	struct urb *urb;
758 
759 	portdata = usb_get_serial_port_data(port);
760 
761 	/*
762 	 * Need to take susp_lock to make sure port is not already being
763 	 * resumed, but no need to hold it due to initialized
764 	 */
765 	spin_lock_irq(&intfdata->susp_lock);
766 	if (--intfdata->open_ports == 0)
767 		serial->interface->needs_remote_wakeup = 0;
768 	spin_unlock_irq(&intfdata->susp_lock);
769 
770 	for (;;) {
771 		urb = usb_get_from_anchor(&portdata->delayed);
772 		if (!urb)
773 			break;
774 		kfree(urb->transfer_buffer);
775 		usb_free_urb(urb);
776 		usb_autopm_put_interface_async(serial->interface);
777 		spin_lock(&portdata->lock);
778 		portdata->outstanding_urbs--;
779 		spin_unlock(&portdata->lock);
780 	}
781 
782 	sierra_stop_rx_urbs(port);
783 	usb_kill_anchored_urbs(&portdata->active);
784 
785 	for (i = 0; i < portdata->num_in_urbs; i++) {
786 		sierra_release_urb(portdata->in_urbs[i]);
787 		portdata->in_urbs[i] = NULL;
788 	}
789 
790 	usb_autopm_get_interface_no_resume(serial->interface);
791 }
792 
793 static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
794 {
795 	struct sierra_port_private *portdata;
796 	struct usb_serial *serial = port->serial;
797 	struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
798 	int i;
799 	int err;
800 	int endpoint;
801 	struct urb *urb;
802 
803 	portdata = usb_get_serial_port_data(port);
804 
805 	endpoint = port->bulk_in_endpointAddress;
806 	for (i = 0; i < portdata->num_in_urbs; i++) {
807 		urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
808 					IN_BUFLEN, GFP_KERNEL,
809 					sierra_indat_callback);
810 		portdata->in_urbs[i] = urb;
811 	}
812 	/* clear halt condition */
813 	usb_clear_halt(serial->dev,
814 			usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
815 
816 	err = sierra_submit_rx_urbs(port, GFP_KERNEL);
817 	if (err)
818 		goto err_submit;
819 
820 	spin_lock_irq(&intfdata->susp_lock);
821 	if (++intfdata->open_ports == 1)
822 		serial->interface->needs_remote_wakeup = 1;
823 	spin_unlock_irq(&intfdata->susp_lock);
824 	usb_autopm_put_interface(serial->interface);
825 
826 	return 0;
827 
828 err_submit:
829 	sierra_stop_rx_urbs(port);
830 
831 	for (i = 0; i < portdata->num_in_urbs; i++) {
832 		sierra_release_urb(portdata->in_urbs[i]);
833 		portdata->in_urbs[i] = NULL;
834 	}
835 
836 	return err;
837 }
838 
839 
840 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
841 {
842 	struct sierra_port_private *portdata;
843 
844 	portdata = usb_get_serial_port_data(port);
845 	portdata->rts_state = on;
846 	portdata->dtr_state = on;
847 
848 	sierra_send_setup(port);
849 }
850 
851 static int sierra_startup(struct usb_serial *serial)
852 {
853 	struct sierra_intf_private *intfdata;
854 
855 	intfdata = kzalloc(sizeof(*intfdata), GFP_KERNEL);
856 	if (!intfdata)
857 		return -ENOMEM;
858 
859 	spin_lock_init(&intfdata->susp_lock);
860 
861 	usb_set_serial_data(serial, intfdata);
862 
863 	/* Set Device mode to D0 */
864 	sierra_set_power_state(serial->dev, 0x0000);
865 
866 	/* Check NMEA and set */
867 	if (nmea)
868 		sierra_vsc_set_nmea(serial->dev, 1);
869 
870 	return 0;
871 }
872 
873 static void sierra_release(struct usb_serial *serial)
874 {
875 	struct sierra_intf_private *intfdata;
876 
877 	intfdata = usb_get_serial_data(serial);
878 	kfree(intfdata);
879 }
880 
881 static int sierra_port_probe(struct usb_serial_port *port)
882 {
883 	struct usb_serial *serial = port->serial;
884 	struct sierra_port_private *portdata;
885 	const struct sierra_iface_info *himemoryp;
886 	u8 ifnum;
887 
888 	portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
889 	if (!portdata)
890 		return -ENOMEM;
891 
892 	spin_lock_init(&portdata->lock);
893 	init_usb_anchor(&portdata->active);
894 	init_usb_anchor(&portdata->delayed);
895 
896 	/* Assume low memory requirements */
897 	portdata->num_out_urbs = N_OUT_URB;
898 	portdata->num_in_urbs  = N_IN_URB;
899 
900 	/* Determine actual memory requirements */
901 	if (serial->num_ports == 1) {
902 		/* Get interface number for composite device */
903 		ifnum = sierra_interface_num(serial);
904 		himemoryp = &typeB_interface_list;
905 	} else {
906 		/* This is really the usb-serial port number of the interface
907 		 * rather than the interface number.
908 		 */
909 		ifnum = port->port_number;
910 		himemoryp = &typeA_interface_list;
911 	}
912 
913 	if (is_himemory(ifnum, himemoryp)) {
914 		portdata->num_out_urbs = N_OUT_URB_HM;
915 		portdata->num_in_urbs  = N_IN_URB_HM;
916 	}
917 
918 	dev_dbg(&port->dev,
919 			"Memory usage (urbs) interface #%d, in=%d, out=%d\n",
920 			ifnum, portdata->num_in_urbs, portdata->num_out_urbs);
921 
922 	usb_set_serial_port_data(port, portdata);
923 
924 	return 0;
925 }
926 
927 static int sierra_port_remove(struct usb_serial_port *port)
928 {
929 	struct sierra_port_private *portdata;
930 
931 	portdata = usb_get_serial_port_data(port);
932 	usb_set_serial_port_data(port, NULL);
933 	kfree(portdata);
934 
935 	return 0;
936 }
937 
938 #ifdef CONFIG_PM
939 static void stop_read_write_urbs(struct usb_serial *serial)
940 {
941 	int i;
942 	struct usb_serial_port *port;
943 	struct sierra_port_private *portdata;
944 
945 	/* Stop reading/writing urbs */
946 	for (i = 0; i < serial->num_ports; ++i) {
947 		port = serial->port[i];
948 		portdata = usb_get_serial_port_data(port);
949 		if (!portdata)
950 			continue;
951 		sierra_stop_rx_urbs(port);
952 		usb_kill_anchored_urbs(&portdata->active);
953 	}
954 }
955 
956 static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
957 {
958 	struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
959 
960 	spin_lock_irq(&intfdata->susp_lock);
961 	if (PMSG_IS_AUTO(message)) {
962 		if (intfdata->in_flight) {
963 			spin_unlock_irq(&intfdata->susp_lock);
964 			return -EBUSY;
965 		}
966 	}
967 	intfdata->suspended = 1;
968 	spin_unlock_irq(&intfdata->susp_lock);
969 
970 	stop_read_write_urbs(serial);
971 
972 	return 0;
973 }
974 
975 /* Caller must hold susp_lock. */
976 static int sierra_submit_delayed_urbs(struct usb_serial_port *port)
977 {
978 	struct sierra_port_private *portdata = usb_get_serial_port_data(port);
979 	struct sierra_intf_private *intfdata;
980 	struct urb *urb;
981 	int ec = 0;
982 	int err;
983 
984 	intfdata = usb_get_serial_data(port->serial);
985 
986 	for (;;) {
987 		urb = usb_get_from_anchor(&portdata->delayed);
988 		if (!urb)
989 			break;
990 
991 		usb_anchor_urb(urb, &portdata->active);
992 		intfdata->in_flight++;
993 		err = usb_submit_urb(urb, GFP_ATOMIC);
994 		if (err) {
995 			dev_err(&port->dev, "%s - submit urb failed: %d",
996 					__func__, err);
997 			ec++;
998 			intfdata->in_flight--;
999 			usb_unanchor_urb(urb);
1000 			kfree(urb->transfer_buffer);
1001 			usb_free_urb(urb);
1002 
1003 			spin_lock(&portdata->lock);
1004 			portdata->outstanding_urbs--;
1005 			spin_unlock(&portdata->lock);
1006 		}
1007 	}
1008 
1009 	if (ec)
1010 		return -EIO;
1011 
1012 	return 0;
1013 }
1014 
1015 static int sierra_resume(struct usb_serial *serial)
1016 {
1017 	struct usb_serial_port *port;
1018 	struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
1019 	int ec = 0;
1020 	int i, err;
1021 
1022 	spin_lock_irq(&intfdata->susp_lock);
1023 	for (i = 0; i < serial->num_ports; i++) {
1024 		port = serial->port[i];
1025 
1026 		if (!tty_port_initialized(&port->port))
1027 			continue;
1028 
1029 		err = sierra_submit_delayed_urbs(port);
1030 		if (err)
1031 			ec++;
1032 
1033 		err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
1034 		if (err)
1035 			ec++;
1036 	}
1037 	intfdata->suspended = 0;
1038 	spin_unlock_irq(&intfdata->susp_lock);
1039 
1040 	return ec ? -EIO : 0;
1041 }
1042 
1043 #else
1044 #define sierra_suspend NULL
1045 #define sierra_resume NULL
1046 #endif
1047 
1048 static struct usb_serial_driver sierra_device = {
1049 	.driver = {
1050 		.owner =	THIS_MODULE,
1051 		.name =		"sierra",
1052 	},
1053 	.description       = "Sierra USB modem",
1054 	.id_table          = id_table,
1055 	.calc_num_ports	   = sierra_calc_num_ports,
1056 	.probe		   = sierra_probe,
1057 	.open              = sierra_open,
1058 	.close             = sierra_close,
1059 	.dtr_rts	   = sierra_dtr_rts,
1060 	.write             = sierra_write,
1061 	.write_room        = sierra_write_room,
1062 	.chars_in_buffer   = sierra_chars_in_buffer,
1063 	.tiocmget          = sierra_tiocmget,
1064 	.tiocmset          = sierra_tiocmset,
1065 	.attach            = sierra_startup,
1066 	.release           = sierra_release,
1067 	.port_probe        = sierra_port_probe,
1068 	.port_remove       = sierra_port_remove,
1069 	.suspend	   = sierra_suspend,
1070 	.resume		   = sierra_resume,
1071 	.read_int_callback = sierra_instat_callback,
1072 };
1073 
1074 static struct usb_serial_driver * const serial_drivers[] = {
1075 	&sierra_device, NULL
1076 };
1077 
1078 module_usb_serial_driver(serial_drivers, id_table);
1079 
1080 MODULE_AUTHOR(DRIVER_AUTHOR);
1081 MODULE_DESCRIPTION(DRIVER_DESC);
1082 MODULE_LICENSE("GPL");
1083 
1084 module_param(nmea, bool, S_IRUGO | S_IWUSR);
1085 MODULE_PARM_DESC(nmea, "NMEA streaming");
1086