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