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