xref: /openbmc/linux/drivers/net/usb/hso.c (revision 930beb5a)
1 /******************************************************************************
2  *
3  * Driver for Option High Speed Mobile Devices.
4  *
5  *  Copyright (C) 2008 Option International
6  *                     Filip Aben <f.aben@option.com>
7  *                     Denis Joseph Barrow <d.barow@option.com>
8  *                     Jan Dumon <j.dumon@option.com>
9  *  Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
10  *  			<ajb@spheresystems.co.uk>
11  *  Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
12  *  Copyright (C) 2008 Novell, Inc.
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License version 2 as
16  *  published by the Free Software Foundation.
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software
25  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
26  *  USA
27  *
28  *
29  *****************************************************************************/
30 
31 /******************************************************************************
32  *
33  * Description of the device:
34  *
35  * Interface 0:	Contains the IP network interface on the bulk end points.
36  *		The multiplexed serial ports are using the interrupt and
37  *		control endpoints.
38  *		Interrupt contains a bitmap telling which multiplexed
39  *		serialport needs servicing.
40  *
41  * Interface 1:	Diagnostics port, uses bulk only, do not submit urbs until the
42  *		port is opened, as this have a huge impact on the network port
43  *		throughput.
44  *
45  * Interface 2:	Standard modem interface - circuit switched interface, this
46  *		can be used to make a standard ppp connection however it
47  *              should not be used in conjunction with the IP network interface
48  *              enabled for USB performance reasons i.e. if using this set
49  *              ideally disable_net=1.
50  *
51  *****************************************************************************/
52 
53 #include <linux/sched.h>
54 #include <linux/slab.h>
55 #include <linux/init.h>
56 #include <linux/delay.h>
57 #include <linux/netdevice.h>
58 #include <linux/module.h>
59 #include <linux/ethtool.h>
60 #include <linux/usb.h>
61 #include <linux/timer.h>
62 #include <linux/tty.h>
63 #include <linux/tty_driver.h>
64 #include <linux/tty_flip.h>
65 #include <linux/kmod.h>
66 #include <linux/rfkill.h>
67 #include <linux/ip.h>
68 #include <linux/uaccess.h>
69 #include <linux/usb/cdc.h>
70 #include <net/arp.h>
71 #include <asm/byteorder.h>
72 #include <linux/serial_core.h>
73 #include <linux/serial.h>
74 
75 
76 #define MOD_AUTHOR			"Option Wireless"
77 #define MOD_DESCRIPTION			"USB High Speed Option driver"
78 #define MOD_LICENSE			"GPL"
79 
80 #define HSO_MAX_NET_DEVICES		10
81 #define HSO__MAX_MTU			2048
82 #define DEFAULT_MTU			1500
83 #define DEFAULT_MRU			1500
84 
85 #define CTRL_URB_RX_SIZE		1024
86 #define CTRL_URB_TX_SIZE		64
87 
88 #define BULK_URB_RX_SIZE		4096
89 #define BULK_URB_TX_SIZE		8192
90 
91 #define MUX_BULK_RX_BUF_SIZE		HSO__MAX_MTU
92 #define MUX_BULK_TX_BUF_SIZE		HSO__MAX_MTU
93 #define MUX_BULK_RX_BUF_COUNT		4
94 #define USB_TYPE_OPTION_VENDOR		0x20
95 
96 /* These definitions are used with the struct hso_net flags element */
97 /* - use *_bit operations on it. (bit indices not values.) */
98 #define HSO_NET_RUNNING			0
99 
100 #define	HSO_NET_TX_TIMEOUT		(HZ*10)
101 
102 #define HSO_SERIAL_MAGIC		0x48534f31
103 
104 /* Number of ttys to handle */
105 #define HSO_SERIAL_TTY_MINORS		256
106 
107 #define MAX_RX_URBS			2
108 
109 /*****************************************************************************/
110 /* Debugging functions                                                       */
111 /*****************************************************************************/
112 #define D__(lvl_, fmt, arg...)				\
113 	do {						\
114 		printk(lvl_ "[%d:%s]: " fmt "\n",	\
115 		       __LINE__, __func__, ## arg);	\
116 	} while (0)
117 
118 #define D_(lvl, args...)				\
119 	do {						\
120 		if (lvl & debug)			\
121 			D__(KERN_INFO, args);		\
122 	} while (0)
123 
124 #define D1(args...)	D_(0x01, ##args)
125 #define D2(args...)	D_(0x02, ##args)
126 #define D3(args...)	D_(0x04, ##args)
127 #define D4(args...)	D_(0x08, ##args)
128 #define D5(args...)	D_(0x10, ##args)
129 
130 /*****************************************************************************/
131 /* Enumerators                                                               */
132 /*****************************************************************************/
133 enum pkt_parse_state {
134 	WAIT_IP,
135 	WAIT_DATA,
136 	WAIT_SYNC
137 };
138 
139 /*****************************************************************************/
140 /* Structs                                                                   */
141 /*****************************************************************************/
142 
143 struct hso_shared_int {
144 	struct usb_endpoint_descriptor *intr_endp;
145 	void *shared_intr_buf;
146 	struct urb *shared_intr_urb;
147 	struct usb_device *usb;
148 	int use_count;
149 	int ref_count;
150 	struct mutex shared_int_lock;
151 };
152 
153 struct hso_net {
154 	struct hso_device *parent;
155 	struct net_device *net;
156 	struct rfkill *rfkill;
157 
158 	struct usb_endpoint_descriptor *in_endp;
159 	struct usb_endpoint_descriptor *out_endp;
160 
161 	struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
162 	struct urb *mux_bulk_tx_urb;
163 	void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
164 	void *mux_bulk_tx_buf;
165 
166 	struct sk_buff *skb_rx_buf;
167 	struct sk_buff *skb_tx_buf;
168 
169 	enum pkt_parse_state rx_parse_state;
170 	spinlock_t net_lock;
171 
172 	unsigned short rx_buf_size;
173 	unsigned short rx_buf_missing;
174 	struct iphdr rx_ip_hdr;
175 
176 	unsigned long flags;
177 };
178 
179 enum rx_ctrl_state{
180 	RX_IDLE,
181 	RX_SENT,
182 	RX_PENDING
183 };
184 
185 #define BM_REQUEST_TYPE (0xa1)
186 #define B_NOTIFICATION  (0x20)
187 #define W_VALUE         (0x0)
188 #define W_LENGTH        (0x2)
189 
190 #define B_OVERRUN       (0x1<<6)
191 #define B_PARITY        (0x1<<5)
192 #define B_FRAMING       (0x1<<4)
193 #define B_RING_SIGNAL   (0x1<<3)
194 #define B_BREAK         (0x1<<2)
195 #define B_TX_CARRIER    (0x1<<1)
196 #define B_RX_CARRIER    (0x1<<0)
197 
198 struct hso_serial_state_notification {
199 	u8 bmRequestType;
200 	u8 bNotification;
201 	u16 wValue;
202 	u16 wIndex;
203 	u16 wLength;
204 	u16 UART_state_bitmap;
205 } __packed;
206 
207 struct hso_tiocmget {
208 	struct mutex mutex;
209 	wait_queue_head_t waitq;
210 	int    intr_completed;
211 	struct usb_endpoint_descriptor *endp;
212 	struct urb *urb;
213 	struct hso_serial_state_notification serial_state_notification;
214 	u16    prev_UART_state_bitmap;
215 	struct uart_icount icount;
216 };
217 
218 
219 struct hso_serial {
220 	struct hso_device *parent;
221 	int magic;
222 	u8 minor;
223 
224 	struct hso_shared_int *shared_int;
225 
226 	/* rx/tx urb could be either a bulk urb or a control urb depending
227 	   on which serial port it is used on. */
228 	struct urb *rx_urb[MAX_RX_URBS];
229 	u8 num_rx_urbs;
230 	u8 *rx_data[MAX_RX_URBS];
231 	u16 rx_data_length;	/* should contain allocated length */
232 
233 	struct urb *tx_urb;
234 	u8 *tx_data;
235 	u8 *tx_buffer;
236 	u16 tx_data_length;	/* should contain allocated length */
237 	u16 tx_data_count;
238 	u16 tx_buffer_count;
239 	struct usb_ctrlrequest ctrl_req_tx;
240 	struct usb_ctrlrequest ctrl_req_rx;
241 
242 	struct usb_endpoint_descriptor *in_endp;
243 	struct usb_endpoint_descriptor *out_endp;
244 
245 	enum rx_ctrl_state rx_state;
246 	u8 rts_state;
247 	u8 dtr_state;
248 	unsigned tx_urb_used:1;
249 
250 	struct tty_port port;
251 	/* from usb_serial_port */
252 	spinlock_t serial_lock;
253 
254 	int (*write_data) (struct hso_serial *serial);
255 	struct hso_tiocmget  *tiocmget;
256 	/* Hacks required to get flow control
257 	 * working on the serial receive buffers
258 	 * so as not to drop characters on the floor.
259 	 */
260 	int  curr_rx_urb_idx;
261 	u16  curr_rx_urb_offset;
262 	u8   rx_urb_filled[MAX_RX_URBS];
263 	struct tasklet_struct unthrottle_tasklet;
264 	struct work_struct    retry_unthrottle_workqueue;
265 };
266 
267 struct hso_device {
268 	union {
269 		struct hso_serial *dev_serial;
270 		struct hso_net *dev_net;
271 	} port_data;
272 
273 	u32 port_spec;
274 
275 	u8 is_active;
276 	u8 usb_gone;
277 	struct work_struct async_get_intf;
278 	struct work_struct async_put_intf;
279 	struct work_struct reset_device;
280 
281 	struct usb_device *usb;
282 	struct usb_interface *interface;
283 
284 	struct device *dev;
285 	struct kref ref;
286 	struct mutex mutex;
287 };
288 
289 /* Type of interface */
290 #define HSO_INTF_MASK		0xFF00
291 #define	HSO_INTF_MUX		0x0100
292 #define	HSO_INTF_BULK   	0x0200
293 
294 /* Type of port */
295 #define HSO_PORT_MASK		0xFF
296 #define HSO_PORT_NO_PORT	0x0
297 #define	HSO_PORT_CONTROL	0x1
298 #define	HSO_PORT_APP		0x2
299 #define	HSO_PORT_GPS		0x3
300 #define	HSO_PORT_PCSC		0x4
301 #define	HSO_PORT_APP2		0x5
302 #define HSO_PORT_GPS_CONTROL	0x6
303 #define HSO_PORT_MSD		0x7
304 #define HSO_PORT_VOICE		0x8
305 #define HSO_PORT_DIAG2		0x9
306 #define	HSO_PORT_DIAG		0x10
307 #define	HSO_PORT_MODEM		0x11
308 #define	HSO_PORT_NETWORK	0x12
309 
310 /* Additional device info */
311 #define HSO_INFO_MASK		0xFF000000
312 #define HSO_INFO_CRC_BUG	0x01000000
313 
314 /*****************************************************************************/
315 /* Prototypes                                                                */
316 /*****************************************************************************/
317 /* Serial driver functions */
318 static int hso_serial_tiocmset(struct tty_struct *tty,
319 			       unsigned int set, unsigned int clear);
320 static void ctrl_callback(struct urb *urb);
321 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
322 static void hso_kick_transmit(struct hso_serial *serial);
323 /* Helper functions */
324 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
325 				   struct usb_device *usb, gfp_t gfp);
326 static void handle_usb_error(int status, const char *function,
327 			     struct hso_device *hso_dev);
328 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
329 						  int type, int dir);
330 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
331 static void hso_free_interface(struct usb_interface *intf);
332 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
333 static int hso_stop_serial_device(struct hso_device *hso_dev);
334 static int hso_start_net_device(struct hso_device *hso_dev);
335 static void hso_free_shared_int(struct hso_shared_int *shared_int);
336 static int hso_stop_net_device(struct hso_device *hso_dev);
337 static void hso_serial_ref_free(struct kref *ref);
338 static void hso_std_serial_read_bulk_callback(struct urb *urb);
339 static int hso_mux_serial_read(struct hso_serial *serial);
340 static void async_get_intf(struct work_struct *data);
341 static void async_put_intf(struct work_struct *data);
342 static int hso_put_activity(struct hso_device *hso_dev);
343 static int hso_get_activity(struct hso_device *hso_dev);
344 static void tiocmget_intr_callback(struct urb *urb);
345 static void reset_device(struct work_struct *data);
346 /*****************************************************************************/
347 /* Helping functions                                                         */
348 /*****************************************************************************/
349 
350 /* #define DEBUG */
351 
352 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
353 {
354 	return hso_dev->port_data.dev_net;
355 }
356 
357 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
358 {
359 	return hso_dev->port_data.dev_serial;
360 }
361 
362 /* Debugging functions */
363 #ifdef DEBUG
364 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
365 		     unsigned int len)
366 {
367 	static char name[255];
368 
369 	sprintf(name, "hso[%d:%s]", line_count, func_name);
370 	print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
371 }
372 
373 #define DUMP(buf_, len_)	\
374 	dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
375 
376 #define DUMP1(buf_, len_)			\
377 	do {					\
378 		if (0x01 & debug)		\
379 			DUMP(buf_, len_);	\
380 	} while (0)
381 #else
382 #define DUMP(buf_, len_)
383 #define DUMP1(buf_, len_)
384 #endif
385 
386 /* module parameters */
387 static int debug;
388 static int tty_major;
389 static int disable_net;
390 
391 /* driver info */
392 static const char driver_name[] = "hso";
393 static const char tty_filename[] = "ttyHS";
394 static const char *version = __FILE__ ": " MOD_AUTHOR;
395 /* the usb driver itself (registered in hso_init) */
396 static struct usb_driver hso_driver;
397 /* serial structures */
398 static struct tty_driver *tty_drv;
399 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
400 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
401 static spinlock_t serial_table_lock;
402 
403 static const s32 default_port_spec[] = {
404 	HSO_INTF_MUX | HSO_PORT_NETWORK,
405 	HSO_INTF_BULK | HSO_PORT_DIAG,
406 	HSO_INTF_BULK | HSO_PORT_MODEM,
407 	0
408 };
409 
410 static const s32 icon321_port_spec[] = {
411 	HSO_INTF_MUX | HSO_PORT_NETWORK,
412 	HSO_INTF_BULK | HSO_PORT_DIAG2,
413 	HSO_INTF_BULK | HSO_PORT_MODEM,
414 	HSO_INTF_BULK | HSO_PORT_DIAG,
415 	0
416 };
417 
418 #define default_port_device(vendor, product)	\
419 	USB_DEVICE(vendor, product),	\
420 		.driver_info = (kernel_ulong_t)default_port_spec
421 
422 #define icon321_port_device(vendor, product)	\
423 	USB_DEVICE(vendor, product),	\
424 		.driver_info = (kernel_ulong_t)icon321_port_spec
425 
426 /* list of devices we support */
427 static const struct usb_device_id hso_ids[] = {
428 	{default_port_device(0x0af0, 0x6711)},
429 	{default_port_device(0x0af0, 0x6731)},
430 	{default_port_device(0x0af0, 0x6751)},
431 	{default_port_device(0x0af0, 0x6771)},
432 	{default_port_device(0x0af0, 0x6791)},
433 	{default_port_device(0x0af0, 0x6811)},
434 	{default_port_device(0x0af0, 0x6911)},
435 	{default_port_device(0x0af0, 0x6951)},
436 	{default_port_device(0x0af0, 0x6971)},
437 	{default_port_device(0x0af0, 0x7011)},
438 	{default_port_device(0x0af0, 0x7031)},
439 	{default_port_device(0x0af0, 0x7051)},
440 	{default_port_device(0x0af0, 0x7071)},
441 	{default_port_device(0x0af0, 0x7111)},
442 	{default_port_device(0x0af0, 0x7211)},
443 	{default_port_device(0x0af0, 0x7251)},
444 	{default_port_device(0x0af0, 0x7271)},
445 	{default_port_device(0x0af0, 0x7311)},
446 	{default_port_device(0x0af0, 0xc031)},	/* Icon-Edge */
447 	{icon321_port_device(0x0af0, 0xd013)},	/* Module HSxPA */
448 	{icon321_port_device(0x0af0, 0xd031)},	/* Icon-321 */
449 	{icon321_port_device(0x0af0, 0xd033)},	/* Icon-322 */
450 	{USB_DEVICE(0x0af0, 0x7301)},		/* GE40x */
451 	{USB_DEVICE(0x0af0, 0x7361)},		/* GE40x */
452 	{USB_DEVICE(0x0af0, 0x7381)},		/* GE40x */
453 	{USB_DEVICE(0x0af0, 0x7401)},		/* GI 0401 */
454 	{USB_DEVICE(0x0af0, 0x7501)},		/* GTM 382 */
455 	{USB_DEVICE(0x0af0, 0x7601)},		/* GE40x */
456 	{USB_DEVICE(0x0af0, 0x7701)},
457 	{USB_DEVICE(0x0af0, 0x7706)},
458 	{USB_DEVICE(0x0af0, 0x7801)},
459 	{USB_DEVICE(0x0af0, 0x7901)},
460 	{USB_DEVICE(0x0af0, 0x7A01)},
461 	{USB_DEVICE(0x0af0, 0x7A05)},
462 	{USB_DEVICE(0x0af0, 0x8200)},
463 	{USB_DEVICE(0x0af0, 0x8201)},
464 	{USB_DEVICE(0x0af0, 0x8300)},
465 	{USB_DEVICE(0x0af0, 0x8302)},
466 	{USB_DEVICE(0x0af0, 0x8304)},
467 	{USB_DEVICE(0x0af0, 0x8400)},
468 	{USB_DEVICE(0x0af0, 0x8600)},
469 	{USB_DEVICE(0x0af0, 0x8800)},
470 	{USB_DEVICE(0x0af0, 0x8900)},
471 	{USB_DEVICE(0x0af0, 0x9000)},
472 	{USB_DEVICE(0x0af0, 0xd035)},
473 	{USB_DEVICE(0x0af0, 0xd055)},
474 	{USB_DEVICE(0x0af0, 0xd155)},
475 	{USB_DEVICE(0x0af0, 0xd255)},
476 	{USB_DEVICE(0x0af0, 0xd057)},
477 	{USB_DEVICE(0x0af0, 0xd157)},
478 	{USB_DEVICE(0x0af0, 0xd257)},
479 	{USB_DEVICE(0x0af0, 0xd357)},
480 	{USB_DEVICE(0x0af0, 0xd058)},
481 	{USB_DEVICE(0x0af0, 0xc100)},
482 	{}
483 };
484 MODULE_DEVICE_TABLE(usb, hso_ids);
485 
486 /* Sysfs attribute */
487 static ssize_t hso_sysfs_show_porttype(struct device *dev,
488 				       struct device_attribute *attr,
489 				       char *buf)
490 {
491 	struct hso_device *hso_dev = dev_get_drvdata(dev);
492 	char *port_name;
493 
494 	if (!hso_dev)
495 		return 0;
496 
497 	switch (hso_dev->port_spec & HSO_PORT_MASK) {
498 	case HSO_PORT_CONTROL:
499 		port_name = "Control";
500 		break;
501 	case HSO_PORT_APP:
502 		port_name = "Application";
503 		break;
504 	case HSO_PORT_APP2:
505 		port_name = "Application2";
506 		break;
507 	case HSO_PORT_GPS:
508 		port_name = "GPS";
509 		break;
510 	case HSO_PORT_GPS_CONTROL:
511 		port_name = "GPS Control";
512 		break;
513 	case HSO_PORT_PCSC:
514 		port_name = "PCSC";
515 		break;
516 	case HSO_PORT_DIAG:
517 		port_name = "Diagnostic";
518 		break;
519 	case HSO_PORT_DIAG2:
520 		port_name = "Diagnostic2";
521 		break;
522 	case HSO_PORT_MODEM:
523 		port_name = "Modem";
524 		break;
525 	case HSO_PORT_NETWORK:
526 		port_name = "Network";
527 		break;
528 	default:
529 		port_name = "Unknown";
530 		break;
531 	}
532 
533 	return sprintf(buf, "%s\n", port_name);
534 }
535 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
536 
537 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
538 {
539 	int idx;
540 
541 	for (idx = 0; idx < serial->num_rx_urbs; idx++)
542 		if (serial->rx_urb[idx] == urb)
543 			return idx;
544 	dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
545 	return -1;
546 }
547 
548 /* converts mux value to a port spec value */
549 static u32 hso_mux_to_port(int mux)
550 {
551 	u32 result;
552 
553 	switch (mux) {
554 	case 0x1:
555 		result = HSO_PORT_CONTROL;
556 		break;
557 	case 0x2:
558 		result = HSO_PORT_APP;
559 		break;
560 	case 0x4:
561 		result = HSO_PORT_PCSC;
562 		break;
563 	case 0x8:
564 		result = HSO_PORT_GPS;
565 		break;
566 	case 0x10:
567 		result = HSO_PORT_APP2;
568 		break;
569 	default:
570 		result = HSO_PORT_NO_PORT;
571 	}
572 	return result;
573 }
574 
575 /* converts port spec value to a mux value */
576 static u32 hso_port_to_mux(int port)
577 {
578 	u32 result;
579 
580 	switch (port & HSO_PORT_MASK) {
581 	case HSO_PORT_CONTROL:
582 		result = 0x0;
583 		break;
584 	case HSO_PORT_APP:
585 		result = 0x1;
586 		break;
587 	case HSO_PORT_PCSC:
588 		result = 0x2;
589 		break;
590 	case HSO_PORT_GPS:
591 		result = 0x3;
592 		break;
593 	case HSO_PORT_APP2:
594 		result = 0x4;
595 		break;
596 	default:
597 		result = 0x0;
598 	}
599 	return result;
600 }
601 
602 static struct hso_serial *get_serial_by_shared_int_and_type(
603 					struct hso_shared_int *shared_int,
604 					int mux)
605 {
606 	int i, port;
607 
608 	port = hso_mux_to_port(mux);
609 
610 	for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
611 		if (serial_table[i] &&
612 		    (dev2ser(serial_table[i])->shared_int == shared_int) &&
613 		    ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
614 			return dev2ser(serial_table[i]);
615 		}
616 	}
617 
618 	return NULL;
619 }
620 
621 static struct hso_serial *get_serial_by_index(unsigned index)
622 {
623 	struct hso_serial *serial = NULL;
624 	unsigned long flags;
625 
626 	spin_lock_irqsave(&serial_table_lock, flags);
627 	if (serial_table[index])
628 		serial = dev2ser(serial_table[index]);
629 	spin_unlock_irqrestore(&serial_table_lock, flags);
630 
631 	return serial;
632 }
633 
634 static int get_free_serial_index(void)
635 {
636 	int index;
637 	unsigned long flags;
638 
639 	spin_lock_irqsave(&serial_table_lock, flags);
640 	for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
641 		if (serial_table[index] == NULL) {
642 			spin_unlock_irqrestore(&serial_table_lock, flags);
643 			return index;
644 		}
645 	}
646 	spin_unlock_irqrestore(&serial_table_lock, flags);
647 
648 	printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
649 	return -1;
650 }
651 
652 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
653 {
654 	unsigned long flags;
655 
656 	spin_lock_irqsave(&serial_table_lock, flags);
657 	if (serial)
658 		serial_table[index] = serial->parent;
659 	else
660 		serial_table[index] = NULL;
661 	spin_unlock_irqrestore(&serial_table_lock, flags);
662 }
663 
664 static void handle_usb_error(int status, const char *function,
665 			     struct hso_device *hso_dev)
666 {
667 	char *explanation;
668 
669 	switch (status) {
670 	case -ENODEV:
671 		explanation = "no device";
672 		break;
673 	case -ENOENT:
674 		explanation = "endpoint not enabled";
675 		break;
676 	case -EPIPE:
677 		explanation = "endpoint stalled";
678 		break;
679 	case -ENOSPC:
680 		explanation = "not enough bandwidth";
681 		break;
682 	case -ESHUTDOWN:
683 		explanation = "device disabled";
684 		break;
685 	case -EHOSTUNREACH:
686 		explanation = "device suspended";
687 		break;
688 	case -EINVAL:
689 	case -EAGAIN:
690 	case -EFBIG:
691 	case -EMSGSIZE:
692 		explanation = "internal error";
693 		break;
694 	case -EILSEQ:
695 	case -EPROTO:
696 	case -ETIME:
697 	case -ETIMEDOUT:
698 		explanation = "protocol error";
699 		if (hso_dev)
700 			schedule_work(&hso_dev->reset_device);
701 		break;
702 	default:
703 		explanation = "unknown status";
704 		break;
705 	}
706 
707 	/* log a meaningful explanation of an USB status */
708 	D1("%s: received USB status - %s (%d)", function, explanation, status);
709 }
710 
711 /* Network interface functions */
712 
713 /* called when net interface is brought up by ifconfig */
714 static int hso_net_open(struct net_device *net)
715 {
716 	struct hso_net *odev = netdev_priv(net);
717 	unsigned long flags = 0;
718 
719 	if (!odev) {
720 		dev_err(&net->dev, "No net device !\n");
721 		return -ENODEV;
722 	}
723 
724 	odev->skb_tx_buf = NULL;
725 
726 	/* setup environment */
727 	spin_lock_irqsave(&odev->net_lock, flags);
728 	odev->rx_parse_state = WAIT_IP;
729 	odev->rx_buf_size = 0;
730 	odev->rx_buf_missing = sizeof(struct iphdr);
731 	spin_unlock_irqrestore(&odev->net_lock, flags);
732 
733 	/* We are up and running. */
734 	set_bit(HSO_NET_RUNNING, &odev->flags);
735 	hso_start_net_device(odev->parent);
736 
737 	/* Tell the kernel we are ready to start receiving from it */
738 	netif_start_queue(net);
739 
740 	return 0;
741 }
742 
743 /* called when interface is brought down by ifconfig */
744 static int hso_net_close(struct net_device *net)
745 {
746 	struct hso_net *odev = netdev_priv(net);
747 
748 	/* we don't need the queue anymore */
749 	netif_stop_queue(net);
750 	/* no longer running */
751 	clear_bit(HSO_NET_RUNNING, &odev->flags);
752 
753 	hso_stop_net_device(odev->parent);
754 
755 	/* done */
756 	return 0;
757 }
758 
759 /* USB tells is xmit done, we should start the netqueue again */
760 static void write_bulk_callback(struct urb *urb)
761 {
762 	struct hso_net *odev = urb->context;
763 	int status = urb->status;
764 
765 	/* Sanity check */
766 	if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
767 		dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
768 		return;
769 	}
770 
771 	/* Do we still have a valid kernel network device? */
772 	if (!netif_device_present(odev->net)) {
773 		dev_err(&urb->dev->dev, "%s: net device not present\n",
774 			__func__);
775 		return;
776 	}
777 
778 	/* log status, but don't act on it, we don't need to resubmit anything
779 	 * anyhow */
780 	if (status)
781 		handle_usb_error(status, __func__, odev->parent);
782 
783 	hso_put_activity(odev->parent);
784 
785 	/* Tell the network interface we are ready for another frame */
786 	netif_wake_queue(odev->net);
787 }
788 
789 /* called by kernel when we need to transmit a packet */
790 static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
791 					    struct net_device *net)
792 {
793 	struct hso_net *odev = netdev_priv(net);
794 	int result;
795 
796 	/* Tell the kernel, "No more frames 'til we are done with this one." */
797 	netif_stop_queue(net);
798 	if (hso_get_activity(odev->parent) == -EAGAIN) {
799 		odev->skb_tx_buf = skb;
800 		return NETDEV_TX_OK;
801 	}
802 
803 	/* log if asked */
804 	DUMP1(skb->data, skb->len);
805 	/* Copy it from kernel memory to OUR memory */
806 	memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
807 	D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
808 
809 	/* Fill in the URB for shipping it out. */
810 	usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
811 			  odev->parent->usb,
812 			  usb_sndbulkpipe(odev->parent->usb,
813 					  odev->out_endp->
814 					  bEndpointAddress & 0x7F),
815 			  odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
816 			  odev);
817 
818 	/* Deal with the Zero Length packet problem, I hope */
819 	odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
820 
821 	/* Send the URB on its merry way. */
822 	result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
823 	if (result) {
824 		dev_warn(&odev->parent->interface->dev,
825 			"failed mux_bulk_tx_urb %d\n", result);
826 		net->stats.tx_errors++;
827 		netif_start_queue(net);
828 	} else {
829 		net->stats.tx_packets++;
830 		net->stats.tx_bytes += skb->len;
831 	}
832 	dev_kfree_skb(skb);
833 	/* we're done */
834 	return NETDEV_TX_OK;
835 }
836 
837 static const struct ethtool_ops ops = {
838 	.get_link = ethtool_op_get_link
839 };
840 
841 /* called when a packet did not ack after watchdogtimeout */
842 static void hso_net_tx_timeout(struct net_device *net)
843 {
844 	struct hso_net *odev = netdev_priv(net);
845 
846 	if (!odev)
847 		return;
848 
849 	/* Tell syslog we are hosed. */
850 	dev_warn(&net->dev, "Tx timed out.\n");
851 
852 	/* Tear the waiting frame off the list */
853 	if (odev->mux_bulk_tx_urb &&
854 	    (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
855 		usb_unlink_urb(odev->mux_bulk_tx_urb);
856 
857 	/* Update statistics */
858 	net->stats.tx_errors++;
859 }
860 
861 /* make a real packet from the received USB buffer */
862 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
863 			unsigned int count, unsigned char is_eop)
864 {
865 	unsigned short temp_bytes;
866 	unsigned short buffer_offset = 0;
867 	unsigned short frame_len;
868 	unsigned char *tmp_rx_buf;
869 
870 	/* log if needed */
871 	D1("Rx %d bytes", count);
872 	DUMP(ip_pkt, min(128, (int)count));
873 
874 	while (count) {
875 		switch (odev->rx_parse_state) {
876 		case WAIT_IP:
877 			/* waiting for IP header. */
878 			/* wanted bytes - size of ip header */
879 			temp_bytes =
880 			    (count <
881 			     odev->rx_buf_missing) ? count : odev->
882 			    rx_buf_missing;
883 
884 			memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
885 			       odev->rx_buf_size, ip_pkt + buffer_offset,
886 			       temp_bytes);
887 
888 			odev->rx_buf_size += temp_bytes;
889 			buffer_offset += temp_bytes;
890 			odev->rx_buf_missing -= temp_bytes;
891 			count -= temp_bytes;
892 
893 			if (!odev->rx_buf_missing) {
894 				/* header is complete allocate an sk_buffer and
895 				 * continue to WAIT_DATA */
896 				frame_len = ntohs(odev->rx_ip_hdr.tot_len);
897 
898 				if ((frame_len > DEFAULT_MRU) ||
899 				    (frame_len < sizeof(struct iphdr))) {
900 					dev_err(&odev->net->dev,
901 						"Invalid frame (%d) length\n",
902 						frame_len);
903 					odev->rx_parse_state = WAIT_SYNC;
904 					continue;
905 				}
906 				/* Allocate an sk_buff */
907 				odev->skb_rx_buf = netdev_alloc_skb(odev->net,
908 								    frame_len);
909 				if (!odev->skb_rx_buf) {
910 					/* We got no receive buffer. */
911 					D1("could not allocate memory");
912 					odev->rx_parse_state = WAIT_SYNC;
913 					return;
914 				}
915 
916 				/* Copy what we got so far. make room for iphdr
917 				 * after tail. */
918 				tmp_rx_buf =
919 				    skb_put(odev->skb_rx_buf,
920 					    sizeof(struct iphdr));
921 				memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
922 				       sizeof(struct iphdr));
923 
924 				/* ETH_HLEN */
925 				odev->rx_buf_size = sizeof(struct iphdr);
926 
927 				/* Filip actually use .tot_len */
928 				odev->rx_buf_missing =
929 				    frame_len - sizeof(struct iphdr);
930 				odev->rx_parse_state = WAIT_DATA;
931 			}
932 			break;
933 
934 		case WAIT_DATA:
935 			temp_bytes = (count < odev->rx_buf_missing)
936 					? count : odev->rx_buf_missing;
937 
938 			/* Copy the rest of the bytes that are left in the
939 			 * buffer into the waiting sk_buf. */
940 			/* Make room for temp_bytes after tail. */
941 			tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
942 			memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
943 
944 			odev->rx_buf_missing -= temp_bytes;
945 			count -= temp_bytes;
946 			buffer_offset += temp_bytes;
947 			odev->rx_buf_size += temp_bytes;
948 			if (!odev->rx_buf_missing) {
949 				/* Packet is complete. Inject into stack. */
950 				/* We have IP packet here */
951 				odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
952 				skb_reset_mac_header(odev->skb_rx_buf);
953 
954 				/* Ship it off to the kernel */
955 				netif_rx(odev->skb_rx_buf);
956 				/* No longer our buffer. */
957 				odev->skb_rx_buf = NULL;
958 
959 				/* update out statistics */
960 				odev->net->stats.rx_packets++;
961 
962 				odev->net->stats.rx_bytes += odev->rx_buf_size;
963 
964 				odev->rx_buf_size = 0;
965 				odev->rx_buf_missing = sizeof(struct iphdr);
966 				odev->rx_parse_state = WAIT_IP;
967 			}
968 			break;
969 
970 		case WAIT_SYNC:
971 			D1(" W_S");
972 			count = 0;
973 			break;
974 		default:
975 			D1(" ");
976 			count--;
977 			break;
978 		}
979 	}
980 
981 	/* Recovery mechanism for WAIT_SYNC state. */
982 	if (is_eop) {
983 		if (odev->rx_parse_state == WAIT_SYNC) {
984 			odev->rx_parse_state = WAIT_IP;
985 			odev->rx_buf_size = 0;
986 			odev->rx_buf_missing = sizeof(struct iphdr);
987 		}
988 	}
989 }
990 
991 static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
992 {
993 	static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
994 	u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
995 
996 	if (((rest == 5) || (rest == 6)) &&
997 	    !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
998 		    crc_check, 4)) {
999 		urb->actual_length -= 4;
1000 	}
1001 }
1002 
1003 /* Moving data from usb to kernel (in interrupt state) */
1004 static void read_bulk_callback(struct urb *urb)
1005 {
1006 	struct hso_net *odev = urb->context;
1007 	struct net_device *net;
1008 	int result;
1009 	int status = urb->status;
1010 
1011 	/* is al ok?  (Filip: Who's Al ?) */
1012 	if (status) {
1013 		handle_usb_error(status, __func__, odev->parent);
1014 		return;
1015 	}
1016 
1017 	/* Sanity check */
1018 	if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
1019 		D1("BULK IN callback but driver is not active!");
1020 		return;
1021 	}
1022 	usb_mark_last_busy(urb->dev);
1023 
1024 	net = odev->net;
1025 
1026 	if (!netif_device_present(net)) {
1027 		/* Somebody killed our network interface... */
1028 		return;
1029 	}
1030 
1031 	if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
1032 		fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
1033 
1034 	/* do we even have a packet? */
1035 	if (urb->actual_length) {
1036 		/* Handle the IP stream, add header and push it onto network
1037 		 * stack if the packet is complete. */
1038 		spin_lock(&odev->net_lock);
1039 		packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1040 			    (urb->transfer_buffer_length >
1041 			     urb->actual_length) ? 1 : 0);
1042 		spin_unlock(&odev->net_lock);
1043 	}
1044 
1045 	/* We are done with this URB, resubmit it. Prep the USB to wait for
1046 	 * another frame. Reuse same as received. */
1047 	usb_fill_bulk_urb(urb,
1048 			  odev->parent->usb,
1049 			  usb_rcvbulkpipe(odev->parent->usb,
1050 					  odev->in_endp->
1051 					  bEndpointAddress & 0x7F),
1052 			  urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1053 			  read_bulk_callback, odev);
1054 
1055 	/* Give this to the USB subsystem so it can tell us when more data
1056 	 * arrives. */
1057 	result = usb_submit_urb(urb, GFP_ATOMIC);
1058 	if (result)
1059 		dev_warn(&odev->parent->interface->dev,
1060 			 "%s failed submit mux_bulk_rx_urb %d\n", __func__,
1061 			 result);
1062 }
1063 
1064 /* Serial driver functions */
1065 
1066 static void hso_init_termios(struct ktermios *termios)
1067 {
1068 	/*
1069 	 * The default requirements for this device are:
1070 	 */
1071 	termios->c_iflag &=
1072 		~(IGNBRK	/* disable ignore break */
1073 		| BRKINT	/* disable break causes interrupt */
1074 		| PARMRK	/* disable mark parity errors */
1075 		| ISTRIP	/* disable clear high bit of input characters */
1076 		| INLCR		/* disable translate NL to CR */
1077 		| IGNCR		/* disable ignore CR */
1078 		| ICRNL		/* disable translate CR to NL */
1079 		| IXON);	/* disable enable XON/XOFF flow control */
1080 
1081 	/* disable postprocess output characters */
1082 	termios->c_oflag &= ~OPOST;
1083 
1084 	termios->c_lflag &=
1085 		~(ECHO		/* disable echo input characters */
1086 		| ECHONL	/* disable echo new line */
1087 		| ICANON	/* disable erase, kill, werase, and rprnt
1088 				   special characters */
1089 		| ISIG		/* disable interrupt, quit, and suspend special
1090 				   characters */
1091 		| IEXTEN);	/* disable non-POSIX special characters */
1092 
1093 	termios->c_cflag &=
1094 		~(CSIZE		/* no size */
1095 		| PARENB	/* disable parity bit */
1096 		| CBAUD		/* clear current baud rate */
1097 		| CBAUDEX);	/* clear current buad rate */
1098 
1099 	termios->c_cflag |= CS8;	/* character size 8 bits */
1100 
1101 	/* baud rate 115200 */
1102 	tty_termios_encode_baud_rate(termios, 115200, 115200);
1103 }
1104 
1105 static void _hso_serial_set_termios(struct tty_struct *tty,
1106 				    struct ktermios *old)
1107 {
1108 	struct hso_serial *serial = tty->driver_data;
1109 
1110 	if (!serial) {
1111 		printk(KERN_ERR "%s: no tty structures", __func__);
1112 		return;
1113 	}
1114 
1115 	D4("port %d", serial->minor);
1116 
1117 	/*
1118 	 *	Fix up unsupported bits
1119 	 */
1120 	tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1121 
1122 	tty->termios.c_cflag &=
1123 		~(CSIZE		/* no size */
1124 		| PARENB	/* disable parity bit */
1125 		| CBAUD		/* clear current baud rate */
1126 		| CBAUDEX);	/* clear current buad rate */
1127 
1128 	tty->termios.c_cflag |= CS8;	/* character size 8 bits */
1129 
1130 	/* baud rate 115200 */
1131 	tty_encode_baud_rate(tty, 115200, 115200);
1132 }
1133 
1134 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1135 {
1136 	int result;
1137 	/* We are done with this URB, resubmit it. Prep the USB to wait for
1138 	 * another frame */
1139 	usb_fill_bulk_urb(urb, serial->parent->usb,
1140 			  usb_rcvbulkpipe(serial->parent->usb,
1141 					  serial->in_endp->
1142 					  bEndpointAddress & 0x7F),
1143 			  urb->transfer_buffer, serial->rx_data_length,
1144 			  hso_std_serial_read_bulk_callback, serial);
1145 	/* Give this to the USB subsystem so it can tell us when more data
1146 	 * arrives. */
1147 	result = usb_submit_urb(urb, GFP_ATOMIC);
1148 	if (result) {
1149 		dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1150 			__func__, result);
1151 	}
1152 }
1153 
1154 
1155 
1156 
1157 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1158 {
1159 	int count;
1160 	struct urb *curr_urb;
1161 
1162 	while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1163 		curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1164 		count = put_rxbuf_data(curr_urb, serial);
1165 		if (count == -1)
1166 			return;
1167 		if (count == 0) {
1168 			serial->curr_rx_urb_idx++;
1169 			if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1170 				serial->curr_rx_urb_idx = 0;
1171 			hso_resubmit_rx_bulk_urb(serial, curr_urb);
1172 		}
1173 	}
1174 }
1175 
1176 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1177 {
1178 	int count = 0;
1179 	struct urb *urb;
1180 
1181 	urb = serial->rx_urb[0];
1182 	if (serial->port.count > 0) {
1183 		count = put_rxbuf_data(urb, serial);
1184 		if (count == -1)
1185 			return;
1186 	}
1187 	/* Re issue a read as long as we receive data. */
1188 
1189 	if (count == 0 && ((urb->actual_length != 0) ||
1190 			   (serial->rx_state == RX_PENDING))) {
1191 		serial->rx_state = RX_SENT;
1192 		hso_mux_serial_read(serial);
1193 	} else
1194 		serial->rx_state = RX_IDLE;
1195 }
1196 
1197 
1198 /* read callback for Diag and CS port */
1199 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1200 {
1201 	struct hso_serial *serial = urb->context;
1202 	int status = urb->status;
1203 
1204 	/* sanity check */
1205 	if (!serial) {
1206 		D1("serial == NULL");
1207 		return;
1208 	} else if (status) {
1209 		handle_usb_error(status, __func__, serial->parent);
1210 		return;
1211 	}
1212 
1213 	D4("\n--- Got serial_read_bulk callback %02x ---", status);
1214 	D1("Actual length = %d\n", urb->actual_length);
1215 	DUMP1(urb->transfer_buffer, urb->actual_length);
1216 
1217 	/* Anyone listening? */
1218 	if (serial->port.count == 0)
1219 		return;
1220 
1221 	if (status == 0) {
1222 		if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
1223 			fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
1224 		/* Valid data, handle RX data */
1225 		spin_lock(&serial->serial_lock);
1226 		serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1227 		put_rxbuf_data_and_resubmit_bulk_urb(serial);
1228 		spin_unlock(&serial->serial_lock);
1229 	} else if (status == -ENOENT || status == -ECONNRESET) {
1230 		/* Unlinked - check for throttled port. */
1231 		D2("Port %d, successfully unlinked urb", serial->minor);
1232 		spin_lock(&serial->serial_lock);
1233 		serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1234 		hso_resubmit_rx_bulk_urb(serial, urb);
1235 		spin_unlock(&serial->serial_lock);
1236 	} else {
1237 		D2("Port %d, status = %d for read urb", serial->minor, status);
1238 		return;
1239 	}
1240 }
1241 
1242 /*
1243  * This needs to be a tasklet otherwise we will
1244  * end up recursively calling this function.
1245  */
1246 static void hso_unthrottle_tasklet(struct hso_serial *serial)
1247 {
1248 	unsigned long flags;
1249 
1250 	spin_lock_irqsave(&serial->serial_lock, flags);
1251 	if ((serial->parent->port_spec & HSO_INTF_MUX))
1252 		put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1253 	else
1254 		put_rxbuf_data_and_resubmit_bulk_urb(serial);
1255 	spin_unlock_irqrestore(&serial->serial_lock, flags);
1256 }
1257 
1258 static	void hso_unthrottle(struct tty_struct *tty)
1259 {
1260 	struct hso_serial *serial = tty->driver_data;
1261 
1262 	tasklet_hi_schedule(&serial->unthrottle_tasklet);
1263 }
1264 
1265 static void hso_unthrottle_workfunc(struct work_struct *work)
1266 {
1267 	struct hso_serial *serial =
1268 	    container_of(work, struct hso_serial,
1269 			 retry_unthrottle_workqueue);
1270 	hso_unthrottle_tasklet(serial);
1271 }
1272 
1273 /* open the requested serial port */
1274 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1275 {
1276 	struct hso_serial *serial = get_serial_by_index(tty->index);
1277 	int result;
1278 
1279 	/* sanity check */
1280 	if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1281 		WARN_ON(1);
1282 		tty->driver_data = NULL;
1283 		D1("Failed to open port");
1284 		return -ENODEV;
1285 	}
1286 
1287 	mutex_lock(&serial->parent->mutex);
1288 	result = usb_autopm_get_interface(serial->parent->interface);
1289 	if (result < 0)
1290 		goto err_out;
1291 
1292 	D1("Opening %d", serial->minor);
1293 	kref_get(&serial->parent->ref);
1294 
1295 	/* setup */
1296 	tty->driver_data = serial;
1297 	tty_port_tty_set(&serial->port, tty);
1298 
1299 	/* check for port already opened, if not set the termios */
1300 	serial->port.count++;
1301 	if (serial->port.count == 1) {
1302 		serial->rx_state = RX_IDLE;
1303 		/* Force default termio settings */
1304 		_hso_serial_set_termios(tty, NULL);
1305 		tasklet_init(&serial->unthrottle_tasklet,
1306 			     (void (*)(unsigned long))hso_unthrottle_tasklet,
1307 			     (unsigned long)serial);
1308 		INIT_WORK(&serial->retry_unthrottle_workqueue,
1309 			  hso_unthrottle_workfunc);
1310 		result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1311 		if (result) {
1312 			hso_stop_serial_device(serial->parent);
1313 			serial->port.count--;
1314 			kref_put(&serial->parent->ref, hso_serial_ref_free);
1315 		}
1316 	} else {
1317 		D1("Port was already open");
1318 	}
1319 
1320 	usb_autopm_put_interface(serial->parent->interface);
1321 
1322 	/* done */
1323 	if (result)
1324 		hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
1325 err_out:
1326 	mutex_unlock(&serial->parent->mutex);
1327 	return result;
1328 }
1329 
1330 /* close the requested serial port */
1331 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1332 {
1333 	struct hso_serial *serial = tty->driver_data;
1334 	u8 usb_gone;
1335 
1336 	D1("Closing serial port");
1337 
1338 	/* Open failed, no close cleanup required */
1339 	if (serial == NULL)
1340 		return;
1341 
1342 	mutex_lock(&serial->parent->mutex);
1343 	usb_gone = serial->parent->usb_gone;
1344 
1345 	if (!usb_gone)
1346 		usb_autopm_get_interface(serial->parent->interface);
1347 
1348 	/* reset the rts and dtr */
1349 	/* do the actual close */
1350 	serial->port.count--;
1351 
1352 	if (serial->port.count <= 0) {
1353 		serial->port.count = 0;
1354 		tty_port_tty_set(&serial->port, NULL);
1355 		if (!usb_gone)
1356 			hso_stop_serial_device(serial->parent);
1357 		tasklet_kill(&serial->unthrottle_tasklet);
1358 		cancel_work_sync(&serial->retry_unthrottle_workqueue);
1359 	}
1360 
1361 	if (!usb_gone)
1362 		usb_autopm_put_interface(serial->parent->interface);
1363 
1364 	mutex_unlock(&serial->parent->mutex);
1365 
1366 	kref_put(&serial->parent->ref, hso_serial_ref_free);
1367 }
1368 
1369 /* close the requested serial port */
1370 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1371 			    int count)
1372 {
1373 	struct hso_serial *serial = tty->driver_data;
1374 	int space, tx_bytes;
1375 	unsigned long flags;
1376 
1377 	/* sanity check */
1378 	if (serial == NULL) {
1379 		printk(KERN_ERR "%s: serial is NULL\n", __func__);
1380 		return -ENODEV;
1381 	}
1382 
1383 	spin_lock_irqsave(&serial->serial_lock, flags);
1384 
1385 	space = serial->tx_data_length - serial->tx_buffer_count;
1386 	tx_bytes = (count < space) ? count : space;
1387 
1388 	if (!tx_bytes)
1389 		goto out;
1390 
1391 	memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1392 	serial->tx_buffer_count += tx_bytes;
1393 
1394 out:
1395 	spin_unlock_irqrestore(&serial->serial_lock, flags);
1396 
1397 	hso_kick_transmit(serial);
1398 	/* done */
1399 	return tx_bytes;
1400 }
1401 
1402 /* how much room is there for writing */
1403 static int hso_serial_write_room(struct tty_struct *tty)
1404 {
1405 	struct hso_serial *serial = tty->driver_data;
1406 	int room;
1407 	unsigned long flags;
1408 
1409 	spin_lock_irqsave(&serial->serial_lock, flags);
1410 	room = serial->tx_data_length - serial->tx_buffer_count;
1411 	spin_unlock_irqrestore(&serial->serial_lock, flags);
1412 
1413 	/* return free room */
1414 	return room;
1415 }
1416 
1417 /* setup the term */
1418 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1419 {
1420 	struct hso_serial *serial = tty->driver_data;
1421 	unsigned long flags;
1422 
1423 	if (old)
1424 		D5("Termios called with: cflags new[%d] - old[%d]",
1425 		   tty->termios.c_cflag, old->c_cflag);
1426 
1427 	/* the actual setup */
1428 	spin_lock_irqsave(&serial->serial_lock, flags);
1429 	if (serial->port.count)
1430 		_hso_serial_set_termios(tty, old);
1431 	else
1432 		tty->termios = *old;
1433 	spin_unlock_irqrestore(&serial->serial_lock, flags);
1434 
1435 	/* done */
1436 }
1437 
1438 /* how many characters in the buffer */
1439 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1440 {
1441 	struct hso_serial *serial = tty->driver_data;
1442 	int chars;
1443 	unsigned long flags;
1444 
1445 	/* sanity check */
1446 	if (serial == NULL)
1447 		return 0;
1448 
1449 	spin_lock_irqsave(&serial->serial_lock, flags);
1450 	chars = serial->tx_buffer_count;
1451 	spin_unlock_irqrestore(&serial->serial_lock, flags);
1452 
1453 	return chars;
1454 }
1455 static int tiocmget_submit_urb(struct hso_serial *serial,
1456 			       struct hso_tiocmget *tiocmget,
1457 			       struct usb_device *usb)
1458 {
1459 	int result;
1460 
1461 	if (serial->parent->usb_gone)
1462 		return -ENODEV;
1463 	usb_fill_int_urb(tiocmget->urb, usb,
1464 			 usb_rcvintpipe(usb,
1465 					tiocmget->endp->
1466 					bEndpointAddress & 0x7F),
1467 			 &tiocmget->serial_state_notification,
1468 			 sizeof(struct hso_serial_state_notification),
1469 			 tiocmget_intr_callback, serial,
1470 			 tiocmget->endp->bInterval);
1471 	result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1472 	if (result) {
1473 		dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1474 			 result);
1475 	}
1476 	return result;
1477 
1478 }
1479 
1480 static void tiocmget_intr_callback(struct urb *urb)
1481 {
1482 	struct hso_serial *serial = urb->context;
1483 	struct hso_tiocmget *tiocmget;
1484 	int status = urb->status;
1485 	u16 UART_state_bitmap, prev_UART_state_bitmap;
1486 	struct uart_icount *icount;
1487 	struct hso_serial_state_notification *serial_state_notification;
1488 	struct usb_device *usb;
1489 	int if_num;
1490 
1491 	/* Sanity checks */
1492 	if (!serial)
1493 		return;
1494 	if (status) {
1495 		handle_usb_error(status, __func__, serial->parent);
1496 		return;
1497 	}
1498 
1499 	/* tiocmget is only supported on HSO_PORT_MODEM */
1500 	tiocmget = serial->tiocmget;
1501 	if (!tiocmget)
1502 		return;
1503 	BUG_ON((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM);
1504 
1505 	usb = serial->parent->usb;
1506 	if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1507 
1508 	/* wIndex should be the USB interface number of the port to which the
1509 	 * notification applies, which should always be the Modem port.
1510 	 */
1511 	serial_state_notification = &tiocmget->serial_state_notification;
1512 	if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1513 	    serial_state_notification->bNotification != B_NOTIFICATION ||
1514 	    le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1515 	    le16_to_cpu(serial_state_notification->wIndex) != if_num ||
1516 	    le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1517 		dev_warn(&usb->dev,
1518 			 "hso received invalid serial state notification\n");
1519 		DUMP(serial_state_notification,
1520 		     sizeof(struct hso_serial_state_notification));
1521 	} else {
1522 
1523 		UART_state_bitmap = le16_to_cpu(serial_state_notification->
1524 						UART_state_bitmap);
1525 		prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1526 		icount = &tiocmget->icount;
1527 		spin_lock(&serial->serial_lock);
1528 		if ((UART_state_bitmap & B_OVERRUN) !=
1529 		   (prev_UART_state_bitmap & B_OVERRUN))
1530 			icount->parity++;
1531 		if ((UART_state_bitmap & B_PARITY) !=
1532 		   (prev_UART_state_bitmap & B_PARITY))
1533 			icount->parity++;
1534 		if ((UART_state_bitmap & B_FRAMING) !=
1535 		   (prev_UART_state_bitmap & B_FRAMING))
1536 			icount->frame++;
1537 		if ((UART_state_bitmap & B_RING_SIGNAL) &&
1538 		   !(prev_UART_state_bitmap & B_RING_SIGNAL))
1539 			icount->rng++;
1540 		if ((UART_state_bitmap & B_BREAK) !=
1541 		   (prev_UART_state_bitmap & B_BREAK))
1542 			icount->brk++;
1543 		if ((UART_state_bitmap & B_TX_CARRIER) !=
1544 		   (prev_UART_state_bitmap & B_TX_CARRIER))
1545 			icount->dsr++;
1546 		if ((UART_state_bitmap & B_RX_CARRIER) !=
1547 		   (prev_UART_state_bitmap & B_RX_CARRIER))
1548 			icount->dcd++;
1549 		tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1550 		spin_unlock(&serial->serial_lock);
1551 		tiocmget->intr_completed = 1;
1552 		wake_up_interruptible(&tiocmget->waitq);
1553 	}
1554 	memset(serial_state_notification, 0,
1555 	       sizeof(struct hso_serial_state_notification));
1556 	tiocmget_submit_urb(serial,
1557 			    tiocmget,
1558 			    serial->parent->usb);
1559 }
1560 
1561 /*
1562  * next few functions largely stolen from drivers/serial/serial_core.c
1563  */
1564 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1565  * - mask passed in arg for lines of interest
1566  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1567  * Caller should use TIOCGICOUNT to see which one it was
1568  */
1569 static int
1570 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1571 {
1572 	DECLARE_WAITQUEUE(wait, current);
1573 	struct uart_icount cprev, cnow;
1574 	struct hso_tiocmget  *tiocmget;
1575 	int ret;
1576 
1577 	tiocmget = serial->tiocmget;
1578 	if (!tiocmget)
1579 		return -ENOENT;
1580 	/*
1581 	 * note the counters on entry
1582 	 */
1583 	spin_lock_irq(&serial->serial_lock);
1584 	memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1585 	spin_unlock_irq(&serial->serial_lock);
1586 	add_wait_queue(&tiocmget->waitq, &wait);
1587 	for (;;) {
1588 		spin_lock_irq(&serial->serial_lock);
1589 		memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1590 		spin_unlock_irq(&serial->serial_lock);
1591 		set_current_state(TASK_INTERRUPTIBLE);
1592 		if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1593 		    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1594 		    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd))) {
1595 			ret = 0;
1596 			break;
1597 		}
1598 		schedule();
1599 		/* see if a signal did it */
1600 		if (signal_pending(current)) {
1601 			ret = -ERESTARTSYS;
1602 			break;
1603 		}
1604 		cprev = cnow;
1605 	}
1606 	current->state = TASK_RUNNING;
1607 	remove_wait_queue(&tiocmget->waitq, &wait);
1608 
1609 	return ret;
1610 }
1611 
1612 /*
1613  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1614  * Return: write counters to the user passed counter struct
1615  * NB: both 1->0 and 0->1 transitions are counted except for
1616  *     RI where only 0->1 is counted.
1617  */
1618 static int hso_get_count(struct tty_struct *tty,
1619 		  struct serial_icounter_struct *icount)
1620 {
1621 	struct uart_icount cnow;
1622 	struct hso_serial *serial = tty->driver_data;
1623 	struct hso_tiocmget  *tiocmget = serial->tiocmget;
1624 
1625 	memset(icount, 0, sizeof(struct serial_icounter_struct));
1626 
1627 	if (!tiocmget)
1628 		 return -ENOENT;
1629 	spin_lock_irq(&serial->serial_lock);
1630 	memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1631 	spin_unlock_irq(&serial->serial_lock);
1632 
1633 	icount->cts         = cnow.cts;
1634 	icount->dsr         = cnow.dsr;
1635 	icount->rng         = cnow.rng;
1636 	icount->dcd         = cnow.dcd;
1637 	icount->rx          = cnow.rx;
1638 	icount->tx          = cnow.tx;
1639 	icount->frame       = cnow.frame;
1640 	icount->overrun     = cnow.overrun;
1641 	icount->parity      = cnow.parity;
1642 	icount->brk         = cnow.brk;
1643 	icount->buf_overrun = cnow.buf_overrun;
1644 
1645 	return 0;
1646 }
1647 
1648 
1649 static int hso_serial_tiocmget(struct tty_struct *tty)
1650 {
1651 	int retval;
1652 	struct hso_serial *serial = tty->driver_data;
1653 	struct hso_tiocmget  *tiocmget;
1654 	u16 UART_state_bitmap;
1655 
1656 	/* sanity check */
1657 	if (!serial) {
1658 		D1("no tty structures");
1659 		return -EINVAL;
1660 	}
1661 	spin_lock_irq(&serial->serial_lock);
1662 	retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
1663 	    ((serial->dtr_state) ? TIOCM_DTR : 0);
1664 	tiocmget = serial->tiocmget;
1665 	if (tiocmget) {
1666 
1667 		UART_state_bitmap = le16_to_cpu(
1668 			tiocmget->prev_UART_state_bitmap);
1669 		if (UART_state_bitmap & B_RING_SIGNAL)
1670 			retval |=  TIOCM_RNG;
1671 		if (UART_state_bitmap & B_RX_CARRIER)
1672 			retval |=  TIOCM_CD;
1673 		if (UART_state_bitmap & B_TX_CARRIER)
1674 			retval |=  TIOCM_DSR;
1675 	}
1676 	spin_unlock_irq(&serial->serial_lock);
1677 	return retval;
1678 }
1679 
1680 static int hso_serial_tiocmset(struct tty_struct *tty,
1681 			       unsigned int set, unsigned int clear)
1682 {
1683 	int val = 0;
1684 	unsigned long flags;
1685 	int if_num;
1686 	struct hso_serial *serial = tty->driver_data;
1687 
1688 	/* sanity check */
1689 	if (!serial) {
1690 		D1("no tty structures");
1691 		return -EINVAL;
1692 	}
1693 
1694 	if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
1695 		return -EINVAL;
1696 
1697 	if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1698 
1699 	spin_lock_irqsave(&serial->serial_lock, flags);
1700 	if (set & TIOCM_RTS)
1701 		serial->rts_state = 1;
1702 	if (set & TIOCM_DTR)
1703 		serial->dtr_state = 1;
1704 
1705 	if (clear & TIOCM_RTS)
1706 		serial->rts_state = 0;
1707 	if (clear & TIOCM_DTR)
1708 		serial->dtr_state = 0;
1709 
1710 	if (serial->dtr_state)
1711 		val |= 0x01;
1712 	if (serial->rts_state)
1713 		val |= 0x02;
1714 
1715 	spin_unlock_irqrestore(&serial->serial_lock, flags);
1716 
1717 	return usb_control_msg(serial->parent->usb,
1718 			       usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1719 			       0x21, val, if_num, NULL, 0,
1720 			       USB_CTRL_SET_TIMEOUT);
1721 }
1722 
1723 static int hso_serial_ioctl(struct tty_struct *tty,
1724 			    unsigned int cmd, unsigned long arg)
1725 {
1726 	struct hso_serial *serial = tty->driver_data;
1727 	int ret = 0;
1728 	D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1729 
1730 	if (!serial)
1731 		return -ENODEV;
1732 	switch (cmd) {
1733 	case TIOCMIWAIT:
1734 		ret = hso_wait_modem_status(serial, arg);
1735 		break;
1736 	default:
1737 		ret = -ENOIOCTLCMD;
1738 		break;
1739 	}
1740 	return ret;
1741 }
1742 
1743 
1744 /* starts a transmit */
1745 static void hso_kick_transmit(struct hso_serial *serial)
1746 {
1747 	u8 *temp;
1748 	unsigned long flags;
1749 	int res;
1750 
1751 	spin_lock_irqsave(&serial->serial_lock, flags);
1752 	if (!serial->tx_buffer_count)
1753 		goto out;
1754 
1755 	if (serial->tx_urb_used)
1756 		goto out;
1757 
1758 	/* Wakeup USB interface if necessary */
1759 	if (hso_get_activity(serial->parent) == -EAGAIN)
1760 		goto out;
1761 
1762 	/* Switch pointers around to avoid memcpy */
1763 	temp = serial->tx_buffer;
1764 	serial->tx_buffer = serial->tx_data;
1765 	serial->tx_data = temp;
1766 	serial->tx_data_count = serial->tx_buffer_count;
1767 	serial->tx_buffer_count = 0;
1768 
1769 	/* If temp is set, it means we switched buffers */
1770 	if (temp && serial->write_data) {
1771 		res = serial->write_data(serial);
1772 		if (res >= 0)
1773 			serial->tx_urb_used = 1;
1774 	}
1775 out:
1776 	spin_unlock_irqrestore(&serial->serial_lock, flags);
1777 }
1778 
1779 /* make a request (for reading and writing data to muxed serial port) */
1780 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1781 			      struct urb *ctrl_urb,
1782 			      struct usb_ctrlrequest *ctrl_req,
1783 			      u8 *ctrl_urb_data, u32 size)
1784 {
1785 	int result;
1786 	int pipe;
1787 
1788 	/* Sanity check */
1789 	if (!serial || !ctrl_urb || !ctrl_req) {
1790 		printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1791 		return -EINVAL;
1792 	}
1793 
1794 	/* initialize */
1795 	ctrl_req->wValue = 0;
1796 	ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
1797 	ctrl_req->wLength = cpu_to_le16(size);
1798 
1799 	if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1800 		/* Reading command */
1801 		ctrl_req->bRequestType = USB_DIR_IN |
1802 					 USB_TYPE_OPTION_VENDOR |
1803 					 USB_RECIP_INTERFACE;
1804 		ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1805 		pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1806 	} else {
1807 		/* Writing command */
1808 		ctrl_req->bRequestType = USB_DIR_OUT |
1809 					 USB_TYPE_OPTION_VENDOR |
1810 					 USB_RECIP_INTERFACE;
1811 		ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1812 		pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1813 	}
1814 	/* syslog */
1815 	D2("%s command (%02x) len: %d, port: %d",
1816 	   type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1817 	   ctrl_req->bRequestType, ctrl_req->wLength, port);
1818 
1819 	/* Load ctrl urb */
1820 	ctrl_urb->transfer_flags = 0;
1821 	usb_fill_control_urb(ctrl_urb,
1822 			     serial->parent->usb,
1823 			     pipe,
1824 			     (u8 *) ctrl_req,
1825 			     ctrl_urb_data, size, ctrl_callback, serial);
1826 	/* Send it on merry way */
1827 	result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1828 	if (result) {
1829 		dev_err(&ctrl_urb->dev->dev,
1830 			"%s failed submit ctrl_urb %d type %d\n", __func__,
1831 			result, type);
1832 		return result;
1833 	}
1834 
1835 	/* done */
1836 	return size;
1837 }
1838 
1839 /* called by intr_callback when read occurs */
1840 static int hso_mux_serial_read(struct hso_serial *serial)
1841 {
1842 	if (!serial)
1843 		return -EINVAL;
1844 
1845 	/* clean data */
1846 	memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1847 	/* make the request */
1848 
1849 	if (serial->num_rx_urbs != 1) {
1850 		dev_err(&serial->parent->interface->dev,
1851 			"ERROR: mux'd reads with multiple buffers "
1852 			"not possible\n");
1853 		return 0;
1854 	}
1855 	return mux_device_request(serial,
1856 				  USB_CDC_GET_ENCAPSULATED_RESPONSE,
1857 				  serial->parent->port_spec & HSO_PORT_MASK,
1858 				  serial->rx_urb[0],
1859 				  &serial->ctrl_req_rx,
1860 				  serial->rx_data[0], serial->rx_data_length);
1861 }
1862 
1863 /* used for muxed serial port callback (muxed serial read) */
1864 static void intr_callback(struct urb *urb)
1865 {
1866 	struct hso_shared_int *shared_int = urb->context;
1867 	struct hso_serial *serial;
1868 	unsigned char *port_req;
1869 	int status = urb->status;
1870 	int i;
1871 
1872 	usb_mark_last_busy(urb->dev);
1873 
1874 	/* sanity check */
1875 	if (!shared_int)
1876 		return;
1877 
1878 	/* status check */
1879 	if (status) {
1880 		handle_usb_error(status, __func__, NULL);
1881 		return;
1882 	}
1883 	D4("\n--- Got intr callback 0x%02X ---", status);
1884 
1885 	/* what request? */
1886 	port_req = urb->transfer_buffer;
1887 	D4(" port_req = 0x%.2X\n", *port_req);
1888 	/* loop over all muxed ports to find the one sending this */
1889 	for (i = 0; i < 8; i++) {
1890 		/* max 8 channels on MUX */
1891 		if (*port_req & (1 << i)) {
1892 			serial = get_serial_by_shared_int_and_type(shared_int,
1893 								   (1 << i));
1894 			if (serial != NULL) {
1895 				D1("Pending read interrupt on port %d\n", i);
1896 				spin_lock(&serial->serial_lock);
1897 				if (serial->rx_state == RX_IDLE &&
1898 					serial->port.count > 0) {
1899 					/* Setup and send a ctrl req read on
1900 					 * port i */
1901 					if (!serial->rx_urb_filled[0]) {
1902 						serial->rx_state = RX_SENT;
1903 						hso_mux_serial_read(serial);
1904 					} else
1905 						serial->rx_state = RX_PENDING;
1906 				} else {
1907 					D1("Already a read pending on "
1908 					   "port %d or port not open\n", i);
1909 				}
1910 				spin_unlock(&serial->serial_lock);
1911 			}
1912 		}
1913 	}
1914 	/* Resubmit interrupt urb */
1915 	hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1916 }
1917 
1918 /* called for writing to muxed serial port */
1919 static int hso_mux_serial_write_data(struct hso_serial *serial)
1920 {
1921 	if (NULL == serial)
1922 		return -EINVAL;
1923 
1924 	return mux_device_request(serial,
1925 				  USB_CDC_SEND_ENCAPSULATED_COMMAND,
1926 				  serial->parent->port_spec & HSO_PORT_MASK,
1927 				  serial->tx_urb,
1928 				  &serial->ctrl_req_tx,
1929 				  serial->tx_data, serial->tx_data_count);
1930 }
1931 
1932 /* write callback for Diag and CS port */
1933 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1934 {
1935 	struct hso_serial *serial = urb->context;
1936 	int status = urb->status;
1937 
1938 	/* sanity check */
1939 	if (!serial) {
1940 		D1("serial == NULL");
1941 		return;
1942 	}
1943 
1944 	spin_lock(&serial->serial_lock);
1945 	serial->tx_urb_used = 0;
1946 	spin_unlock(&serial->serial_lock);
1947 	if (status) {
1948 		handle_usb_error(status, __func__, serial->parent);
1949 		return;
1950 	}
1951 	hso_put_activity(serial->parent);
1952 	tty_port_tty_wakeup(&serial->port);
1953 	hso_kick_transmit(serial);
1954 
1955 	D1(" ");
1956 }
1957 
1958 /* called for writing diag or CS serial port */
1959 static int hso_std_serial_write_data(struct hso_serial *serial)
1960 {
1961 	int count = serial->tx_data_count;
1962 	int result;
1963 
1964 	usb_fill_bulk_urb(serial->tx_urb,
1965 			  serial->parent->usb,
1966 			  usb_sndbulkpipe(serial->parent->usb,
1967 					  serial->out_endp->
1968 					  bEndpointAddress & 0x7F),
1969 			  serial->tx_data, serial->tx_data_count,
1970 			  hso_std_serial_write_bulk_callback, serial);
1971 
1972 	result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1973 	if (result) {
1974 		dev_warn(&serial->parent->usb->dev,
1975 			 "Failed to submit urb - res %d\n", result);
1976 		return result;
1977 	}
1978 
1979 	return count;
1980 }
1981 
1982 /* callback after read or write on muxed serial port */
1983 static void ctrl_callback(struct urb *urb)
1984 {
1985 	struct hso_serial *serial = urb->context;
1986 	struct usb_ctrlrequest *req;
1987 	int status = urb->status;
1988 
1989 	/* sanity check */
1990 	if (!serial)
1991 		return;
1992 
1993 	spin_lock(&serial->serial_lock);
1994 	serial->tx_urb_used = 0;
1995 	spin_unlock(&serial->serial_lock);
1996 	if (status) {
1997 		handle_usb_error(status, __func__, serial->parent);
1998 		return;
1999 	}
2000 
2001 	/* what request? */
2002 	req = (struct usb_ctrlrequest *)(urb->setup_packet);
2003 	D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2004 	D4("Actual length of urb = %d\n", urb->actual_length);
2005 	DUMP1(urb->transfer_buffer, urb->actual_length);
2006 
2007 	if (req->bRequestType ==
2008 	    (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2009 		/* response to a read command */
2010 		serial->rx_urb_filled[0] = 1;
2011 		spin_lock(&serial->serial_lock);
2012 		put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2013 		spin_unlock(&serial->serial_lock);
2014 	} else {
2015 		hso_put_activity(serial->parent);
2016 		tty_port_tty_wakeup(&serial->port);
2017 		/* response to a write command */
2018 		hso_kick_transmit(serial);
2019 	}
2020 }
2021 
2022 /* handle RX data for serial port */
2023 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
2024 {
2025 	struct tty_struct *tty;
2026 	int write_length_remaining = 0;
2027 	int curr_write_len;
2028 
2029 	/* Sanity check */
2030 	if (urb == NULL || serial == NULL) {
2031 		D1("serial = NULL");
2032 		return -2;
2033 	}
2034 
2035 	tty = tty_port_tty_get(&serial->port);
2036 
2037 	/* Push data to tty */
2038 	write_length_remaining = urb->actual_length -
2039 		serial->curr_rx_urb_offset;
2040 	D1("data to push to tty");
2041 	while (write_length_remaining) {
2042 		if (tty && test_bit(TTY_THROTTLED, &tty->flags)) {
2043 			tty_kref_put(tty);
2044 			return -1;
2045 		}
2046 		curr_write_len = tty_insert_flip_string(&serial->port,
2047 			urb->transfer_buffer + serial->curr_rx_urb_offset,
2048 			write_length_remaining);
2049 		serial->curr_rx_urb_offset += curr_write_len;
2050 		write_length_remaining -= curr_write_len;
2051 		tty_flip_buffer_push(&serial->port);
2052 	}
2053 	tty_kref_put(tty);
2054 
2055 	if (write_length_remaining == 0) {
2056 		serial->curr_rx_urb_offset = 0;
2057 		serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2058 	}
2059 	return write_length_remaining;
2060 }
2061 
2062 
2063 /* Base driver functions */
2064 
2065 static void hso_log_port(struct hso_device *hso_dev)
2066 {
2067 	char *port_type;
2068 	char port_dev[20];
2069 
2070 	switch (hso_dev->port_spec & HSO_PORT_MASK) {
2071 	case HSO_PORT_CONTROL:
2072 		port_type = "Control";
2073 		break;
2074 	case HSO_PORT_APP:
2075 		port_type = "Application";
2076 		break;
2077 	case HSO_PORT_GPS:
2078 		port_type = "GPS";
2079 		break;
2080 	case HSO_PORT_GPS_CONTROL:
2081 		port_type = "GPS control";
2082 		break;
2083 	case HSO_PORT_APP2:
2084 		port_type = "Application2";
2085 		break;
2086 	case HSO_PORT_PCSC:
2087 		port_type = "PCSC";
2088 		break;
2089 	case HSO_PORT_DIAG:
2090 		port_type = "Diagnostic";
2091 		break;
2092 	case HSO_PORT_DIAG2:
2093 		port_type = "Diagnostic2";
2094 		break;
2095 	case HSO_PORT_MODEM:
2096 		port_type = "Modem";
2097 		break;
2098 	case HSO_PORT_NETWORK:
2099 		port_type = "Network";
2100 		break;
2101 	default:
2102 		port_type = "Unknown";
2103 		break;
2104 	}
2105 	if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2106 		sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2107 	} else
2108 		sprintf(port_dev, "/dev/%s%d", tty_filename,
2109 			dev2ser(hso_dev)->minor);
2110 
2111 	dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2112 		port_type, port_dev);
2113 }
2114 
2115 static int hso_start_net_device(struct hso_device *hso_dev)
2116 {
2117 	int i, result = 0;
2118 	struct hso_net *hso_net = dev2net(hso_dev);
2119 
2120 	if (!hso_net)
2121 		return -ENODEV;
2122 
2123 	/* send URBs for all read buffers */
2124 	for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2125 
2126 		/* Prep a receive URB */
2127 		usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2128 				  hso_dev->usb,
2129 				  usb_rcvbulkpipe(hso_dev->usb,
2130 						  hso_net->in_endp->
2131 						  bEndpointAddress & 0x7F),
2132 				  hso_net->mux_bulk_rx_buf_pool[i],
2133 				  MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2134 				  hso_net);
2135 
2136 		/* Put it out there so the device can send us stuff */
2137 		result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2138 					GFP_NOIO);
2139 		if (result)
2140 			dev_warn(&hso_dev->usb->dev,
2141 				"%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2142 				i, result);
2143 	}
2144 
2145 	return result;
2146 }
2147 
2148 static int hso_stop_net_device(struct hso_device *hso_dev)
2149 {
2150 	int i;
2151 	struct hso_net *hso_net = dev2net(hso_dev);
2152 
2153 	if (!hso_net)
2154 		return -ENODEV;
2155 
2156 	for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2157 		if (hso_net->mux_bulk_rx_urb_pool[i])
2158 			usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2159 
2160 	}
2161 	if (hso_net->mux_bulk_tx_urb)
2162 		usb_kill_urb(hso_net->mux_bulk_tx_urb);
2163 
2164 	return 0;
2165 }
2166 
2167 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2168 {
2169 	int i, result = 0;
2170 	struct hso_serial *serial = dev2ser(hso_dev);
2171 
2172 	if (!serial)
2173 		return -ENODEV;
2174 
2175 	/* If it is not the MUX port fill in and submit a bulk urb (already
2176 	 * allocated in hso_serial_start) */
2177 	if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2178 		for (i = 0; i < serial->num_rx_urbs; i++) {
2179 			usb_fill_bulk_urb(serial->rx_urb[i],
2180 					  serial->parent->usb,
2181 					  usb_rcvbulkpipe(serial->parent->usb,
2182 							  serial->in_endp->
2183 							  bEndpointAddress &
2184 							  0x7F),
2185 					  serial->rx_data[i],
2186 					  serial->rx_data_length,
2187 					  hso_std_serial_read_bulk_callback,
2188 					  serial);
2189 			result = usb_submit_urb(serial->rx_urb[i], flags);
2190 			if (result) {
2191 				dev_warn(&serial->parent->usb->dev,
2192 					 "Failed to submit urb - res %d\n",
2193 					 result);
2194 				break;
2195 			}
2196 		}
2197 	} else {
2198 		mutex_lock(&serial->shared_int->shared_int_lock);
2199 		if (!serial->shared_int->use_count) {
2200 			result =
2201 			    hso_mux_submit_intr_urb(serial->shared_int,
2202 						    hso_dev->usb, flags);
2203 		}
2204 		serial->shared_int->use_count++;
2205 		mutex_unlock(&serial->shared_int->shared_int_lock);
2206 	}
2207 	if (serial->tiocmget)
2208 		tiocmget_submit_urb(serial,
2209 				    serial->tiocmget,
2210 				    serial->parent->usb);
2211 	return result;
2212 }
2213 
2214 static int hso_stop_serial_device(struct hso_device *hso_dev)
2215 {
2216 	int i;
2217 	struct hso_serial *serial = dev2ser(hso_dev);
2218 	struct hso_tiocmget  *tiocmget;
2219 
2220 	if (!serial)
2221 		return -ENODEV;
2222 
2223 	for (i = 0; i < serial->num_rx_urbs; i++) {
2224 		if (serial->rx_urb[i]) {
2225 				usb_kill_urb(serial->rx_urb[i]);
2226 				serial->rx_urb_filled[i] = 0;
2227 		}
2228 	}
2229 	serial->curr_rx_urb_idx = 0;
2230 	serial->curr_rx_urb_offset = 0;
2231 
2232 	if (serial->tx_urb)
2233 		usb_kill_urb(serial->tx_urb);
2234 
2235 	if (serial->shared_int) {
2236 		mutex_lock(&serial->shared_int->shared_int_lock);
2237 		if (serial->shared_int->use_count &&
2238 		    (--serial->shared_int->use_count == 0)) {
2239 			struct urb *urb;
2240 
2241 			urb = serial->shared_int->shared_intr_urb;
2242 			if (urb)
2243 				usb_kill_urb(urb);
2244 		}
2245 		mutex_unlock(&serial->shared_int->shared_int_lock);
2246 	}
2247 	tiocmget = serial->tiocmget;
2248 	if (tiocmget) {
2249 		wake_up_interruptible(&tiocmget->waitq);
2250 		usb_kill_urb(tiocmget->urb);
2251 	}
2252 
2253 	return 0;
2254 }
2255 
2256 static void hso_serial_common_free(struct hso_serial *serial)
2257 {
2258 	int i;
2259 
2260 	if (serial->parent->dev)
2261 		device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2262 
2263 	tty_unregister_device(tty_drv, serial->minor);
2264 
2265 	for (i = 0; i < serial->num_rx_urbs; i++) {
2266 		/* unlink and free RX URB */
2267 		usb_free_urb(serial->rx_urb[i]);
2268 		/* free the RX buffer */
2269 		kfree(serial->rx_data[i]);
2270 	}
2271 
2272 	/* unlink and free TX URB */
2273 	usb_free_urb(serial->tx_urb);
2274 	kfree(serial->tx_data);
2275 	tty_port_destroy(&serial->port);
2276 }
2277 
2278 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2279 				    int rx_size, int tx_size)
2280 {
2281 	struct device *dev;
2282 	int minor;
2283 	int i;
2284 
2285 	tty_port_init(&serial->port);
2286 
2287 	minor = get_free_serial_index();
2288 	if (minor < 0)
2289 		goto exit;
2290 
2291 	/* register our minor number */
2292 	serial->parent->dev = tty_port_register_device(&serial->port, tty_drv,
2293 			minor, &serial->parent->interface->dev);
2294 	dev = serial->parent->dev;
2295 	dev_set_drvdata(dev, serial->parent);
2296 	i = device_create_file(dev, &dev_attr_hsotype);
2297 
2298 	/* fill in specific data for later use */
2299 	serial->minor = minor;
2300 	serial->magic = HSO_SERIAL_MAGIC;
2301 	spin_lock_init(&serial->serial_lock);
2302 	serial->num_rx_urbs = num_urbs;
2303 
2304 	/* RX, allocate urb and initialize */
2305 
2306 	/* prepare our RX buffer */
2307 	serial->rx_data_length = rx_size;
2308 	for (i = 0; i < serial->num_rx_urbs; i++) {
2309 		serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2310 		if (!serial->rx_urb[i]) {
2311 			dev_err(dev, "Could not allocate urb?\n");
2312 			goto exit;
2313 		}
2314 		serial->rx_urb[i]->transfer_buffer = NULL;
2315 		serial->rx_urb[i]->transfer_buffer_length = 0;
2316 		serial->rx_data[i] = kzalloc(serial->rx_data_length,
2317 					     GFP_KERNEL);
2318 		if (!serial->rx_data[i])
2319 			goto exit;
2320 	}
2321 
2322 	/* TX, allocate urb and initialize */
2323 	serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2324 	if (!serial->tx_urb) {
2325 		dev_err(dev, "Could not allocate urb?\n");
2326 		goto exit;
2327 	}
2328 	serial->tx_urb->transfer_buffer = NULL;
2329 	serial->tx_urb->transfer_buffer_length = 0;
2330 	/* prepare our TX buffer */
2331 	serial->tx_data_count = 0;
2332 	serial->tx_buffer_count = 0;
2333 	serial->tx_data_length = tx_size;
2334 	serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2335 	if (!serial->tx_data)
2336 		goto exit;
2337 
2338 	serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2339 	if (!serial->tx_buffer)
2340 		goto exit;
2341 
2342 	return 0;
2343 exit:
2344 	hso_serial_common_free(serial);
2345 	return -1;
2346 }
2347 
2348 /* Creates a general hso device */
2349 static struct hso_device *hso_create_device(struct usb_interface *intf,
2350 					    int port_spec)
2351 {
2352 	struct hso_device *hso_dev;
2353 
2354 	hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2355 	if (!hso_dev)
2356 		return NULL;
2357 
2358 	hso_dev->port_spec = port_spec;
2359 	hso_dev->usb = interface_to_usbdev(intf);
2360 	hso_dev->interface = intf;
2361 	kref_init(&hso_dev->ref);
2362 	mutex_init(&hso_dev->mutex);
2363 
2364 	INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2365 	INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2366 	INIT_WORK(&hso_dev->reset_device, reset_device);
2367 
2368 	return hso_dev;
2369 }
2370 
2371 /* Removes a network device in the network device table */
2372 static int remove_net_device(struct hso_device *hso_dev)
2373 {
2374 	int i;
2375 
2376 	for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2377 		if (network_table[i] == hso_dev) {
2378 			network_table[i] = NULL;
2379 			break;
2380 		}
2381 	}
2382 	if (i == HSO_MAX_NET_DEVICES)
2383 		return -1;
2384 	return 0;
2385 }
2386 
2387 /* Frees our network device */
2388 static void hso_free_net_device(struct hso_device *hso_dev)
2389 {
2390 	int i;
2391 	struct hso_net *hso_net = dev2net(hso_dev);
2392 
2393 	if (!hso_net)
2394 		return;
2395 
2396 	remove_net_device(hso_net->parent);
2397 
2398 	if (hso_net->net)
2399 		unregister_netdev(hso_net->net);
2400 
2401 	/* start freeing */
2402 	for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2403 		usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2404 		kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2405 		hso_net->mux_bulk_rx_buf_pool[i] = NULL;
2406 	}
2407 	usb_free_urb(hso_net->mux_bulk_tx_urb);
2408 	kfree(hso_net->mux_bulk_tx_buf);
2409 	hso_net->mux_bulk_tx_buf = NULL;
2410 
2411 	if (hso_net->net)
2412 		free_netdev(hso_net->net);
2413 
2414 	kfree(hso_dev);
2415 }
2416 
2417 static const struct net_device_ops hso_netdev_ops = {
2418 	.ndo_open	= hso_net_open,
2419 	.ndo_stop	= hso_net_close,
2420 	.ndo_start_xmit = hso_net_start_xmit,
2421 	.ndo_tx_timeout = hso_net_tx_timeout,
2422 };
2423 
2424 /* initialize the network interface */
2425 static void hso_net_init(struct net_device *net)
2426 {
2427 	struct hso_net *hso_net = netdev_priv(net);
2428 
2429 	D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2430 
2431 	/* fill in the other fields */
2432 	net->netdev_ops = &hso_netdev_ops;
2433 	net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2434 	net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2435 	net->type = ARPHRD_NONE;
2436 	net->mtu = DEFAULT_MTU - 14;
2437 	net->tx_queue_len = 10;
2438 	SET_ETHTOOL_OPS(net, &ops);
2439 
2440 	/* and initialize the semaphore */
2441 	spin_lock_init(&hso_net->net_lock);
2442 }
2443 
2444 /* Adds a network device in the network device table */
2445 static int add_net_device(struct hso_device *hso_dev)
2446 {
2447 	int i;
2448 
2449 	for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2450 		if (network_table[i] == NULL) {
2451 			network_table[i] = hso_dev;
2452 			break;
2453 		}
2454 	}
2455 	if (i == HSO_MAX_NET_DEVICES)
2456 		return -1;
2457 	return 0;
2458 }
2459 
2460 static int hso_rfkill_set_block(void *data, bool blocked)
2461 {
2462 	struct hso_device *hso_dev = data;
2463 	int enabled = !blocked;
2464 	int rv;
2465 
2466 	mutex_lock(&hso_dev->mutex);
2467 	if (hso_dev->usb_gone)
2468 		rv = 0;
2469 	else
2470 		rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2471 				       enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2472 				       USB_CTRL_SET_TIMEOUT);
2473 	mutex_unlock(&hso_dev->mutex);
2474 	return rv;
2475 }
2476 
2477 static const struct rfkill_ops hso_rfkill_ops = {
2478 	.set_block = hso_rfkill_set_block,
2479 };
2480 
2481 /* Creates and sets up everything for rfkill */
2482 static void hso_create_rfkill(struct hso_device *hso_dev,
2483 			     struct usb_interface *interface)
2484 {
2485 	struct hso_net *hso_net = dev2net(hso_dev);
2486 	struct device *dev = &hso_net->net->dev;
2487 	char *rfkn;
2488 
2489 	rfkn = kzalloc(20, GFP_KERNEL);
2490 	if (!rfkn)
2491 		dev_err(dev, "%s - Out of memory\n", __func__);
2492 
2493 	snprintf(rfkn, 20, "hso-%d",
2494 		 interface->altsetting->desc.bInterfaceNumber);
2495 
2496 	hso_net->rfkill = rfkill_alloc(rfkn,
2497 				       &interface_to_usbdev(interface)->dev,
2498 				       RFKILL_TYPE_WWAN,
2499 				       &hso_rfkill_ops, hso_dev);
2500 	if (!hso_net->rfkill) {
2501 		dev_err(dev, "%s - Out of memory\n", __func__);
2502 		kfree(rfkn);
2503 		return;
2504 	}
2505 	if (rfkill_register(hso_net->rfkill) < 0) {
2506 		rfkill_destroy(hso_net->rfkill);
2507 		kfree(rfkn);
2508 		hso_net->rfkill = NULL;
2509 		dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2510 		return;
2511 	}
2512 }
2513 
2514 static struct device_type hso_type = {
2515 	.name	= "wwan",
2516 };
2517 
2518 /* Creates our network device */
2519 static struct hso_device *hso_create_net_device(struct usb_interface *interface,
2520 						int port_spec)
2521 {
2522 	int result, i;
2523 	struct net_device *net;
2524 	struct hso_net *hso_net;
2525 	struct hso_device *hso_dev;
2526 
2527 	hso_dev = hso_create_device(interface, port_spec);
2528 	if (!hso_dev)
2529 		return NULL;
2530 
2531 	/* allocate our network device, then we can put in our private data */
2532 	/* call hso_net_init to do the basic initialization */
2533 	net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2534 	if (!net) {
2535 		dev_err(&interface->dev, "Unable to create ethernet device\n");
2536 		goto exit;
2537 	}
2538 
2539 	hso_net = netdev_priv(net);
2540 
2541 	hso_dev->port_data.dev_net = hso_net;
2542 	hso_net->net = net;
2543 	hso_net->parent = hso_dev;
2544 
2545 	hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2546 				      USB_DIR_IN);
2547 	if (!hso_net->in_endp) {
2548 		dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2549 		goto exit;
2550 	}
2551 	hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2552 				       USB_DIR_OUT);
2553 	if (!hso_net->out_endp) {
2554 		dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2555 		goto exit;
2556 	}
2557 	SET_NETDEV_DEV(net, &interface->dev);
2558 	SET_NETDEV_DEVTYPE(net, &hso_type);
2559 
2560 	/* registering our net device */
2561 	result = register_netdev(net);
2562 	if (result) {
2563 		dev_err(&interface->dev, "Failed to register device\n");
2564 		goto exit;
2565 	}
2566 
2567 	/* start allocating */
2568 	for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2569 		hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2570 		if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2571 			dev_err(&interface->dev, "Could not allocate rx urb\n");
2572 			goto exit;
2573 		}
2574 		hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2575 							   GFP_KERNEL);
2576 		if (!hso_net->mux_bulk_rx_buf_pool[i])
2577 			goto exit;
2578 	}
2579 	hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2580 	if (!hso_net->mux_bulk_tx_urb) {
2581 		dev_err(&interface->dev, "Could not allocate tx urb\n");
2582 		goto exit;
2583 	}
2584 	hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2585 	if (!hso_net->mux_bulk_tx_buf)
2586 		goto exit;
2587 
2588 	add_net_device(hso_dev);
2589 
2590 	hso_log_port(hso_dev);
2591 
2592 	hso_create_rfkill(hso_dev, interface);
2593 
2594 	return hso_dev;
2595 exit:
2596 	hso_free_net_device(hso_dev);
2597 	return NULL;
2598 }
2599 
2600 static void hso_free_tiomget(struct hso_serial *serial)
2601 {
2602 	struct hso_tiocmget *tiocmget;
2603 	if (!serial)
2604 		return;
2605 	tiocmget = serial->tiocmget;
2606 	if (tiocmget) {
2607 		usb_free_urb(tiocmget->urb);
2608 		tiocmget->urb = NULL;
2609 		serial->tiocmget = NULL;
2610 		kfree(tiocmget);
2611 	}
2612 }
2613 
2614 /* Frees an AT channel ( goes for both mux and non-mux ) */
2615 static void hso_free_serial_device(struct hso_device *hso_dev)
2616 {
2617 	struct hso_serial *serial = dev2ser(hso_dev);
2618 
2619 	if (!serial)
2620 		return;
2621 	set_serial_by_index(serial->minor, NULL);
2622 
2623 	hso_serial_common_free(serial);
2624 
2625 	if (serial->shared_int) {
2626 		mutex_lock(&serial->shared_int->shared_int_lock);
2627 		if (--serial->shared_int->ref_count == 0)
2628 			hso_free_shared_int(serial->shared_int);
2629 		else
2630 			mutex_unlock(&serial->shared_int->shared_int_lock);
2631 	}
2632 	hso_free_tiomget(serial);
2633 	kfree(serial);
2634 	kfree(hso_dev);
2635 }
2636 
2637 /* Creates a bulk AT channel */
2638 static struct hso_device *hso_create_bulk_serial_device(
2639 			struct usb_interface *interface, int port)
2640 {
2641 	struct hso_device *hso_dev;
2642 	struct hso_serial *serial;
2643 	int num_urbs;
2644 	struct hso_tiocmget *tiocmget;
2645 
2646 	hso_dev = hso_create_device(interface, port);
2647 	if (!hso_dev)
2648 		return NULL;
2649 
2650 	serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2651 	if (!serial)
2652 		goto exit;
2653 
2654 	serial->parent = hso_dev;
2655 	hso_dev->port_data.dev_serial = serial;
2656 
2657 	if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
2658 		num_urbs = 2;
2659 		serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2660 					   GFP_KERNEL);
2661 		/* it isn't going to break our heart if serial->tiocmget
2662 		 *  allocation fails don't bother checking this.
2663 		 */
2664 		if (serial->tiocmget) {
2665 			tiocmget = serial->tiocmget;
2666 			tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2667 			if (tiocmget->urb) {
2668 				mutex_init(&tiocmget->mutex);
2669 				init_waitqueue_head(&tiocmget->waitq);
2670 				tiocmget->endp = hso_get_ep(
2671 					interface,
2672 					USB_ENDPOINT_XFER_INT,
2673 					USB_DIR_IN);
2674 			} else
2675 				hso_free_tiomget(serial);
2676 		}
2677 	}
2678 	else
2679 		num_urbs = 1;
2680 
2681 	if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2682 				     BULK_URB_TX_SIZE))
2683 		goto exit;
2684 
2685 	serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2686 				     USB_DIR_IN);
2687 	if (!serial->in_endp) {
2688 		dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2689 		goto exit2;
2690 	}
2691 
2692 	if (!
2693 	    (serial->out_endp =
2694 	     hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2695 		dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2696 		goto exit2;
2697 	}
2698 
2699 	serial->write_data = hso_std_serial_write_data;
2700 
2701 	/* and record this serial */
2702 	set_serial_by_index(serial->minor, serial);
2703 
2704 	/* setup the proc dirs and files if needed */
2705 	hso_log_port(hso_dev);
2706 
2707 	/* done, return it */
2708 	return hso_dev;
2709 
2710 exit2:
2711 	hso_serial_common_free(serial);
2712 exit:
2713 	hso_free_tiomget(serial);
2714 	kfree(serial);
2715 	kfree(hso_dev);
2716 	return NULL;
2717 }
2718 
2719 /* Creates a multiplexed AT channel */
2720 static
2721 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2722 						int port,
2723 						struct hso_shared_int *mux)
2724 {
2725 	struct hso_device *hso_dev;
2726 	struct hso_serial *serial;
2727 	int port_spec;
2728 
2729 	port_spec = HSO_INTF_MUX;
2730 	port_spec &= ~HSO_PORT_MASK;
2731 
2732 	port_spec |= hso_mux_to_port(port);
2733 	if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2734 		return NULL;
2735 
2736 	hso_dev = hso_create_device(interface, port_spec);
2737 	if (!hso_dev)
2738 		return NULL;
2739 
2740 	serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2741 	if (!serial)
2742 		goto exit;
2743 
2744 	hso_dev->port_data.dev_serial = serial;
2745 	serial->parent = hso_dev;
2746 
2747 	if (hso_serial_common_create
2748 	    (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2749 		goto exit;
2750 
2751 	serial->tx_data_length--;
2752 	serial->write_data = hso_mux_serial_write_data;
2753 
2754 	serial->shared_int = mux;
2755 	mutex_lock(&serial->shared_int->shared_int_lock);
2756 	serial->shared_int->ref_count++;
2757 	mutex_unlock(&serial->shared_int->shared_int_lock);
2758 
2759 	/* and record this serial */
2760 	set_serial_by_index(serial->minor, serial);
2761 
2762 	/* setup the proc dirs and files if needed */
2763 	hso_log_port(hso_dev);
2764 
2765 	/* done, return it */
2766 	return hso_dev;
2767 
2768 exit:
2769 	if (serial) {
2770 		tty_unregister_device(tty_drv, serial->minor);
2771 		kfree(serial);
2772 	}
2773 	if (hso_dev)
2774 		kfree(hso_dev);
2775 	return NULL;
2776 
2777 }
2778 
2779 static void hso_free_shared_int(struct hso_shared_int *mux)
2780 {
2781 	usb_free_urb(mux->shared_intr_urb);
2782 	kfree(mux->shared_intr_buf);
2783 	mutex_unlock(&mux->shared_int_lock);
2784 	kfree(mux);
2785 }
2786 
2787 static
2788 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2789 {
2790 	struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2791 
2792 	if (!mux)
2793 		return NULL;
2794 
2795 	mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2796 				    USB_DIR_IN);
2797 	if (!mux->intr_endp) {
2798 		dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2799 		goto exit;
2800 	}
2801 
2802 	mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2803 	if (!mux->shared_intr_urb) {
2804 		dev_err(&interface->dev, "Could not allocate intr urb?\n");
2805 		goto exit;
2806 	}
2807 	mux->shared_intr_buf =
2808 		kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
2809 			GFP_KERNEL);
2810 	if (!mux->shared_intr_buf)
2811 		goto exit;
2812 
2813 	mutex_init(&mux->shared_int_lock);
2814 
2815 	return mux;
2816 
2817 exit:
2818 	kfree(mux->shared_intr_buf);
2819 	usb_free_urb(mux->shared_intr_urb);
2820 	kfree(mux);
2821 	return NULL;
2822 }
2823 
2824 /* Gets the port spec for a certain interface */
2825 static int hso_get_config_data(struct usb_interface *interface)
2826 {
2827 	struct usb_device *usbdev = interface_to_usbdev(interface);
2828 	u8 *config_data = kmalloc(17, GFP_KERNEL);
2829 	u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2830 	s32 result;
2831 
2832 	if (!config_data)
2833 		return -ENOMEM;
2834 	if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2835 			    0x86, 0xC0, 0, 0, config_data, 17,
2836 			    USB_CTRL_SET_TIMEOUT) != 0x11) {
2837 		kfree(config_data);
2838 		return -EIO;
2839 	}
2840 
2841 	switch (config_data[if_num]) {
2842 	case 0x0:
2843 		result = 0;
2844 		break;
2845 	case 0x1:
2846 		result = HSO_PORT_DIAG;
2847 		break;
2848 	case 0x2:
2849 		result = HSO_PORT_GPS;
2850 		break;
2851 	case 0x3:
2852 		result = HSO_PORT_GPS_CONTROL;
2853 		break;
2854 	case 0x4:
2855 		result = HSO_PORT_APP;
2856 		break;
2857 	case 0x5:
2858 		result = HSO_PORT_APP2;
2859 		break;
2860 	case 0x6:
2861 		result = HSO_PORT_CONTROL;
2862 		break;
2863 	case 0x7:
2864 		result = HSO_PORT_NETWORK;
2865 		break;
2866 	case 0x8:
2867 		result = HSO_PORT_MODEM;
2868 		break;
2869 	case 0x9:
2870 		result = HSO_PORT_MSD;
2871 		break;
2872 	case 0xa:
2873 		result = HSO_PORT_PCSC;
2874 		break;
2875 	case 0xb:
2876 		result = HSO_PORT_VOICE;
2877 		break;
2878 	default:
2879 		result = 0;
2880 	}
2881 
2882 	if (result)
2883 		result |= HSO_INTF_BULK;
2884 
2885 	if (config_data[16] & 0x1)
2886 		result |= HSO_INFO_CRC_BUG;
2887 
2888 	kfree(config_data);
2889 	return result;
2890 }
2891 
2892 /* called once for each interface upon device insertion */
2893 static int hso_probe(struct usb_interface *interface,
2894 		     const struct usb_device_id *id)
2895 {
2896 	int mux, i, if_num, port_spec;
2897 	unsigned char port_mask;
2898 	struct hso_device *hso_dev = NULL;
2899 	struct hso_shared_int *shared_int;
2900 	struct hso_device *tmp_dev = NULL;
2901 
2902 	if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2903 		dev_err(&interface->dev, "Not our interface\n");
2904 		return -ENODEV;
2905 	}
2906 
2907 	if_num = interface->altsetting->desc.bInterfaceNumber;
2908 
2909 	/* Get the interface/port specification from either driver_info or from
2910 	 * the device itself */
2911 	if (id->driver_info)
2912 		port_spec = ((u32 *)(id->driver_info))[if_num];
2913 	else
2914 		port_spec = hso_get_config_data(interface);
2915 
2916 	/* Check if we need to switch to alt interfaces prior to port
2917 	 * configuration */
2918 	if (interface->num_altsetting > 1)
2919 		usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2920 	interface->needs_remote_wakeup = 1;
2921 
2922 	/* Allocate new hso device(s) */
2923 	switch (port_spec & HSO_INTF_MASK) {
2924 	case HSO_INTF_MUX:
2925 		if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2926 			/* Create the network device */
2927 			if (!disable_net) {
2928 				hso_dev = hso_create_net_device(interface,
2929 								port_spec);
2930 				if (!hso_dev)
2931 					goto exit;
2932 				tmp_dev = hso_dev;
2933 			}
2934 		}
2935 
2936 		if (hso_get_mux_ports(interface, &port_mask))
2937 			/* TODO: de-allocate everything */
2938 			goto exit;
2939 
2940 		shared_int = hso_create_shared_int(interface);
2941 		if (!shared_int)
2942 			goto exit;
2943 
2944 		for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2945 			if (port_mask & i) {
2946 				hso_dev = hso_create_mux_serial_device(
2947 						interface, i, shared_int);
2948 				if (!hso_dev)
2949 					goto exit;
2950 			}
2951 		}
2952 
2953 		if (tmp_dev)
2954 			hso_dev = tmp_dev;
2955 		break;
2956 
2957 	case HSO_INTF_BULK:
2958 		/* It's a regular bulk interface */
2959 		if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2960 			if (!disable_net)
2961 				hso_dev =
2962 				    hso_create_net_device(interface, port_spec);
2963 		} else {
2964 			hso_dev =
2965 			    hso_create_bulk_serial_device(interface, port_spec);
2966 		}
2967 		if (!hso_dev)
2968 			goto exit;
2969 		break;
2970 	default:
2971 		goto exit;
2972 	}
2973 
2974 	/* save our data pointer in this device */
2975 	usb_set_intfdata(interface, hso_dev);
2976 
2977 	/* done */
2978 	return 0;
2979 exit:
2980 	hso_free_interface(interface);
2981 	return -ENODEV;
2982 }
2983 
2984 /* device removed, cleaning up */
2985 static void hso_disconnect(struct usb_interface *interface)
2986 {
2987 	hso_free_interface(interface);
2988 
2989 	/* remove reference of our private data */
2990 	usb_set_intfdata(interface, NULL);
2991 }
2992 
2993 static void async_get_intf(struct work_struct *data)
2994 {
2995 	struct hso_device *hso_dev =
2996 	    container_of(data, struct hso_device, async_get_intf);
2997 	usb_autopm_get_interface(hso_dev->interface);
2998 }
2999 
3000 static void async_put_intf(struct work_struct *data)
3001 {
3002 	struct hso_device *hso_dev =
3003 	    container_of(data, struct hso_device, async_put_intf);
3004 	usb_autopm_put_interface(hso_dev->interface);
3005 }
3006 
3007 static int hso_get_activity(struct hso_device *hso_dev)
3008 {
3009 	if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3010 		if (!hso_dev->is_active) {
3011 			hso_dev->is_active = 1;
3012 			schedule_work(&hso_dev->async_get_intf);
3013 		}
3014 	}
3015 
3016 	if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3017 		return -EAGAIN;
3018 
3019 	usb_mark_last_busy(hso_dev->usb);
3020 
3021 	return 0;
3022 }
3023 
3024 static int hso_put_activity(struct hso_device *hso_dev)
3025 {
3026 	if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3027 		if (hso_dev->is_active) {
3028 			hso_dev->is_active = 0;
3029 			schedule_work(&hso_dev->async_put_intf);
3030 			return -EAGAIN;
3031 		}
3032 	}
3033 	hso_dev->is_active = 0;
3034 	return 0;
3035 }
3036 
3037 /* called by kernel when we need to suspend device */
3038 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3039 {
3040 	int i, result;
3041 
3042 	/* Stop all serial ports */
3043 	for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3044 		if (serial_table[i] && (serial_table[i]->interface == iface)) {
3045 			result = hso_stop_serial_device(serial_table[i]);
3046 			if (result)
3047 				goto out;
3048 		}
3049 	}
3050 
3051 	/* Stop all network ports */
3052 	for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3053 		if (network_table[i] &&
3054 		    (network_table[i]->interface == iface)) {
3055 			result = hso_stop_net_device(network_table[i]);
3056 			if (result)
3057 				goto out;
3058 		}
3059 	}
3060 
3061 out:
3062 	return 0;
3063 }
3064 
3065 /* called by kernel when we need to resume device */
3066 static int hso_resume(struct usb_interface *iface)
3067 {
3068 	int i, result = 0;
3069 	struct hso_net *hso_net;
3070 
3071 	/* Start all serial ports */
3072 	for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3073 		if (serial_table[i] && (serial_table[i]->interface == iface)) {
3074 			if (dev2ser(serial_table[i])->port.count) {
3075 				result =
3076 				    hso_start_serial_device(serial_table[i], GFP_NOIO);
3077 				hso_kick_transmit(dev2ser(serial_table[i]));
3078 				if (result)
3079 					goto out;
3080 			}
3081 		}
3082 	}
3083 
3084 	/* Start all network ports */
3085 	for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3086 		if (network_table[i] &&
3087 		    (network_table[i]->interface == iface)) {
3088 			hso_net = dev2net(network_table[i]);
3089 			if (hso_net->flags & IFF_UP) {
3090 				/* First transmit any lingering data,
3091 				   then restart the device. */
3092 				if (hso_net->skb_tx_buf) {
3093 					dev_dbg(&iface->dev,
3094 						"Transmitting"
3095 						" lingering data\n");
3096 					hso_net_start_xmit(hso_net->skb_tx_buf,
3097 							   hso_net->net);
3098 					hso_net->skb_tx_buf = NULL;
3099 				}
3100 				result = hso_start_net_device(network_table[i]);
3101 				if (result)
3102 					goto out;
3103 			}
3104 		}
3105 	}
3106 
3107 out:
3108 	return result;
3109 }
3110 
3111 static void reset_device(struct work_struct *data)
3112 {
3113 	struct hso_device *hso_dev =
3114 	    container_of(data, struct hso_device, reset_device);
3115 	struct usb_device *usb = hso_dev->usb;
3116 	int result;
3117 
3118 	if (hso_dev->usb_gone) {
3119 		D1("No reset during disconnect\n");
3120 	} else {
3121 		result = usb_lock_device_for_reset(usb, hso_dev->interface);
3122 		if (result < 0)
3123 			D1("unable to lock device for reset: %d\n", result);
3124 		else {
3125 			usb_reset_device(usb);
3126 			usb_unlock_device(usb);
3127 		}
3128 	}
3129 }
3130 
3131 static void hso_serial_ref_free(struct kref *ref)
3132 {
3133 	struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3134 
3135 	hso_free_serial_device(hso_dev);
3136 }
3137 
3138 static void hso_free_interface(struct usb_interface *interface)
3139 {
3140 	struct hso_serial *hso_dev;
3141 	int i;
3142 
3143 	for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3144 		if (serial_table[i] &&
3145 		    (serial_table[i]->interface == interface)) {
3146 			hso_dev = dev2ser(serial_table[i]);
3147 			tty_port_tty_hangup(&hso_dev->port, false);
3148 			mutex_lock(&hso_dev->parent->mutex);
3149 			hso_dev->parent->usb_gone = 1;
3150 			mutex_unlock(&hso_dev->parent->mutex);
3151 			kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3152 		}
3153 	}
3154 
3155 	for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3156 		if (network_table[i] &&
3157 		    (network_table[i]->interface == interface)) {
3158 			struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3159 			/* hso_stop_net_device doesn't stop the net queue since
3160 			 * traffic needs to start it again when suspended */
3161 			netif_stop_queue(dev2net(network_table[i])->net);
3162 			hso_stop_net_device(network_table[i]);
3163 			cancel_work_sync(&network_table[i]->async_put_intf);
3164 			cancel_work_sync(&network_table[i]->async_get_intf);
3165 			if (rfk) {
3166 				rfkill_unregister(rfk);
3167 				rfkill_destroy(rfk);
3168 			}
3169 			hso_free_net_device(network_table[i]);
3170 		}
3171 	}
3172 }
3173 
3174 /* Helper functions */
3175 
3176 /* Get the endpoint ! */
3177 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3178 						  int type, int dir)
3179 {
3180 	int i;
3181 	struct usb_host_interface *iface = intf->cur_altsetting;
3182 	struct usb_endpoint_descriptor *endp;
3183 
3184 	for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3185 		endp = &iface->endpoint[i].desc;
3186 		if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3187 		    (usb_endpoint_type(endp) == type))
3188 			return endp;
3189 	}
3190 
3191 	return NULL;
3192 }
3193 
3194 /* Get the byte that describes which ports are enabled */
3195 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3196 {
3197 	int i;
3198 	struct usb_host_interface *iface = intf->cur_altsetting;
3199 
3200 	if (iface->extralen == 3) {
3201 		*ports = iface->extra[2];
3202 		return 0;
3203 	}
3204 
3205 	for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3206 		if (iface->endpoint[i].extralen == 3) {
3207 			*ports = iface->endpoint[i].extra[2];
3208 			return 0;
3209 		}
3210 	}
3211 
3212 	return -1;
3213 }
3214 
3215 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3216 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3217 				   struct usb_device *usb, gfp_t gfp)
3218 {
3219 	int result;
3220 
3221 	usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3222 			 usb_rcvintpipe(usb,
3223 				shared_int->intr_endp->bEndpointAddress & 0x7F),
3224 			 shared_int->shared_intr_buf,
3225 			 1,
3226 			 intr_callback, shared_int,
3227 			 shared_int->intr_endp->bInterval);
3228 
3229 	result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3230 	if (result)
3231 		dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
3232 			result);
3233 
3234 	return result;
3235 }
3236 
3237 /* operations setup of the serial interface */
3238 static const struct tty_operations hso_serial_ops = {
3239 	.open = hso_serial_open,
3240 	.close = hso_serial_close,
3241 	.write = hso_serial_write,
3242 	.write_room = hso_serial_write_room,
3243 	.ioctl = hso_serial_ioctl,
3244 	.set_termios = hso_serial_set_termios,
3245 	.chars_in_buffer = hso_serial_chars_in_buffer,
3246 	.tiocmget = hso_serial_tiocmget,
3247 	.tiocmset = hso_serial_tiocmset,
3248 	.get_icount = hso_get_count,
3249 	.unthrottle = hso_unthrottle
3250 };
3251 
3252 static struct usb_driver hso_driver = {
3253 	.name = driver_name,
3254 	.probe = hso_probe,
3255 	.disconnect = hso_disconnect,
3256 	.id_table = hso_ids,
3257 	.suspend = hso_suspend,
3258 	.resume = hso_resume,
3259 	.reset_resume = hso_resume,
3260 	.supports_autosuspend = 1,
3261 	.disable_hub_initiated_lpm = 1,
3262 };
3263 
3264 static int __init hso_init(void)
3265 {
3266 	int i;
3267 	int result;
3268 
3269 	/* put it in the log */
3270 	printk(KERN_INFO "hso: %s\n", version);
3271 
3272 	/* Initialise the serial table semaphore and table */
3273 	spin_lock_init(&serial_table_lock);
3274 	for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3275 		serial_table[i] = NULL;
3276 
3277 	/* allocate our driver using the proper amount of supported minors */
3278 	tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3279 	if (!tty_drv)
3280 		return -ENOMEM;
3281 
3282 	/* fill in all needed values */
3283 	tty_drv->driver_name = driver_name;
3284 	tty_drv->name = tty_filename;
3285 
3286 	/* if major number is provided as parameter, use that one */
3287 	if (tty_major)
3288 		tty_drv->major = tty_major;
3289 
3290 	tty_drv->minor_start = 0;
3291 	tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3292 	tty_drv->subtype = SERIAL_TYPE_NORMAL;
3293 	tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3294 	tty_drv->init_termios = tty_std_termios;
3295 	hso_init_termios(&tty_drv->init_termios);
3296 	tty_set_operations(tty_drv, &hso_serial_ops);
3297 
3298 	/* register the tty driver */
3299 	result = tty_register_driver(tty_drv);
3300 	if (result) {
3301 		printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3302 			__func__, result);
3303 		goto err_free_tty;
3304 	}
3305 
3306 	/* register this module as an usb driver */
3307 	result = usb_register(&hso_driver);
3308 	if (result) {
3309 		printk(KERN_ERR "Could not register hso driver? error: %d\n",
3310 			result);
3311 		goto err_unreg_tty;
3312 	}
3313 
3314 	/* done */
3315 	return 0;
3316 err_unreg_tty:
3317 	tty_unregister_driver(tty_drv);
3318 err_free_tty:
3319 	put_tty_driver(tty_drv);
3320 	return result;
3321 }
3322 
3323 static void __exit hso_exit(void)
3324 {
3325 	printk(KERN_INFO "hso: unloaded\n");
3326 
3327 	tty_unregister_driver(tty_drv);
3328 	put_tty_driver(tty_drv);
3329 	/* deregister the usb driver */
3330 	usb_deregister(&hso_driver);
3331 }
3332 
3333 /* Module definitions */
3334 module_init(hso_init);
3335 module_exit(hso_exit);
3336 
3337 MODULE_AUTHOR(MOD_AUTHOR);
3338 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3339 MODULE_LICENSE(MOD_LICENSE);
3340 
3341 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3342 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3343 module_param(debug, int, S_IRUGO | S_IWUSR);
3344 
3345 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3346 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3347 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3348 
3349 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3350 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3351 module_param(disable_net, int, S_IRUGO | S_IWUSR);
3352