[netdrvr] usb/hso: Cleanup rfkill error handling
[linux-2.6] / drivers / net / usb / hso.c
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_net->net->dev;
2188         char *rfkn;
2189
2190         hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2191                                  RFKILL_TYPE_WWAN);
2192         if (!hso_net->rfkill) {
2193                 dev_err(dev, "%s - Out of memory\n", __func__);
2194                 return;
2195         }
2196         rfkn = kzalloc(20, GFP_KERNEL);
2197         if (!rfkn) {
2198                 rfkill_free(hso_net->rfkill);
2199                 hso_net->rfkill = NULL;
2200                 dev_err(dev, "%s - Out of memory\n", __func__);
2201                 return;
2202         }
2203         snprintf(rfkn, 20, "hso-%d",
2204                  interface->altsetting->desc.bInterfaceNumber);
2205         hso_net->rfkill->name = rfkn;
2206         hso_net->rfkill->state = RFKILL_STATE_ON;
2207         hso_net->rfkill->data = hso_dev;
2208         hso_net->rfkill->toggle_radio = hso_radio_toggle;
2209         if (rfkill_register(hso_net->rfkill) < 0) {
2210                 kfree(rfkn);
2211                 hso_net->rfkill->name = NULL;
2212                 rfkill_free(hso_net->rfkill);
2213                 hso_net->rfkill = NULL;
2214                 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2215                 return;
2216         }
2217 }
2218
2219 /* Creates our network device */
2220 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2221 {
2222         int result, i;
2223         struct net_device *net;
2224         struct hso_net *hso_net;
2225         struct hso_device *hso_dev;
2226
2227         hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2228         if (!hso_dev)
2229                 return NULL;
2230
2231         /* allocate our network device, then we can put in our private data */
2232         /* call hso_net_init to do the basic initialization */
2233         net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2234         if (!net) {
2235                 dev_err(&interface->dev, "Unable to create ethernet device\n");
2236                 goto exit;
2237         }
2238
2239         hso_net = netdev_priv(net);
2240
2241         hso_dev->port_data.dev_net = hso_net;
2242         hso_net->net = net;
2243         hso_net->parent = hso_dev;
2244
2245         hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2246                                       USB_DIR_IN);
2247         if (!hso_net->in_endp) {
2248                 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2249                 goto exit;
2250         }
2251         hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2252                                        USB_DIR_OUT);
2253         if (!hso_net->out_endp) {
2254                 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2255                 goto exit;
2256         }
2257         SET_NETDEV_DEV(net, &interface->dev);
2258
2259         /* registering our net device */
2260         result = register_netdev(net);
2261         if (result) {
2262                 dev_err(&interface->dev, "Failed to register device\n");
2263                 goto exit;
2264         }
2265
2266         /* start allocating */
2267         for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2268                 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2269                 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2270                         dev_err(&interface->dev, "Could not allocate rx urb\n");
2271                         goto exit;
2272                 }
2273                 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2274                                                            GFP_KERNEL);
2275                 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2276                         dev_err(&interface->dev, "Could not allocate rx buf\n");
2277                         goto exit;
2278                 }
2279         }
2280         hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2281         if (!hso_net->mux_bulk_tx_urb) {
2282                 dev_err(&interface->dev, "Could not allocate tx urb\n");
2283                 goto exit;
2284         }
2285         hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2286         if (!hso_net->mux_bulk_tx_buf) {
2287                 dev_err(&interface->dev, "Could not allocate tx buf\n");
2288                 goto exit;
2289         }
2290
2291         add_net_device(hso_dev);
2292
2293         hso_log_port(hso_dev);
2294
2295         hso_create_rfkill(hso_dev, interface);
2296
2297         return hso_dev;
2298 exit:
2299         hso_free_net_device(hso_dev);
2300         return NULL;
2301 }
2302
2303 /* Frees an AT channel ( goes for both mux and non-mux ) */
2304 static void hso_free_serial_device(struct hso_device *hso_dev)
2305 {
2306         struct hso_serial *serial = dev2ser(hso_dev);
2307
2308         if (!serial)
2309                 return;
2310         set_serial_by_index(serial->minor, NULL);
2311
2312         hso_serial_common_free(serial);
2313
2314         if (serial->shared_int) {
2315                 mutex_lock(&serial->shared_int->shared_int_lock);
2316                 if (--serial->shared_int->ref_count == 0)
2317                         hso_free_shared_int(serial->shared_int);
2318                 else
2319                         mutex_unlock(&serial->shared_int->shared_int_lock);
2320         }
2321         kfree(serial);
2322         hso_free_device(hso_dev);
2323 }
2324
2325 /* Creates a bulk AT channel */
2326 static struct hso_device *hso_create_bulk_serial_device(
2327                         struct usb_interface *interface, int port)
2328 {
2329         struct hso_device *hso_dev;
2330         struct hso_serial *serial;
2331         int num_urbs;
2332
2333         hso_dev = hso_create_device(interface, port);
2334         if (!hso_dev)
2335                 return NULL;
2336
2337         serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2338         if (!serial)
2339                 goto exit;
2340
2341         serial->parent = hso_dev;
2342         hso_dev->port_data.dev_serial = serial;
2343
2344         if (port & HSO_PORT_MODEM)
2345                 num_urbs = 2;
2346         else
2347                 num_urbs = 1;
2348
2349         if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2350                                      BULK_URB_TX_SIZE))
2351                 goto exit;
2352
2353         serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2354                                      USB_DIR_IN);
2355         if (!serial->in_endp) {
2356                 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2357                 goto exit2;
2358         }
2359
2360         if (!
2361             (serial->out_endp =
2362              hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2363                 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2364                 goto exit2;
2365         }
2366
2367         serial->write_data = hso_std_serial_write_data;
2368
2369         /* and record this serial */
2370         set_serial_by_index(serial->minor, serial);
2371
2372         /* setup the proc dirs and files if needed */
2373         hso_log_port(hso_dev);
2374
2375         /* done, return it */
2376         return hso_dev;
2377
2378 exit2:
2379         hso_serial_common_free(serial);
2380 exit:
2381         kfree(serial);
2382         hso_free_device(hso_dev);
2383         return NULL;
2384 }
2385
2386 /* Creates a multiplexed AT channel */
2387 static
2388 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2389                                                 int port,
2390                                                 struct hso_shared_int *mux)
2391 {
2392         struct hso_device *hso_dev;
2393         struct hso_serial *serial;
2394         int port_spec;
2395
2396         port_spec = HSO_INTF_MUX;
2397         port_spec &= ~HSO_PORT_MASK;
2398
2399         port_spec |= hso_mux_to_port(port);
2400         if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2401                 return NULL;
2402
2403         hso_dev = hso_create_device(interface, port_spec);
2404         if (!hso_dev)
2405                 return NULL;
2406
2407         serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2408         if (!serial)
2409                 goto exit;
2410
2411         hso_dev->port_data.dev_serial = serial;
2412         serial->parent = hso_dev;
2413
2414         if (hso_serial_common_create
2415             (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2416                 goto exit;
2417
2418         serial->tx_data_length--;
2419         serial->write_data = hso_mux_serial_write_data;
2420
2421         serial->shared_int = mux;
2422         mutex_lock(&serial->shared_int->shared_int_lock);
2423         serial->shared_int->ref_count++;
2424         mutex_unlock(&serial->shared_int->shared_int_lock);
2425
2426         /* and record this serial */
2427         set_serial_by_index(serial->minor, serial);
2428
2429         /* setup the proc dirs and files if needed */
2430         hso_log_port(hso_dev);
2431
2432         /* done, return it */
2433         return hso_dev;
2434
2435 exit:
2436         if (serial) {
2437                 tty_unregister_device(tty_drv, serial->minor);
2438                 kfree(serial);
2439         }
2440         if (hso_dev)
2441                 hso_free_device(hso_dev);
2442         return NULL;
2443
2444 }
2445
2446 static void hso_free_shared_int(struct hso_shared_int *mux)
2447 {
2448         usb_free_urb(mux->shared_intr_urb);
2449         kfree(mux->shared_intr_buf);
2450         mutex_unlock(&mux->shared_int_lock);
2451         kfree(mux);
2452 }
2453
2454 static
2455 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2456 {
2457         struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2458
2459         if (!mux)
2460                 return NULL;
2461
2462         mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2463                                     USB_DIR_IN);
2464         if (!mux->intr_endp) {
2465                 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2466                 goto exit;
2467         }
2468
2469         mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2470         if (!mux->shared_intr_urb) {
2471                 dev_err(&interface->dev, "Could not allocate intr urb?");
2472                 goto exit;
2473         }
2474         mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2475                                        GFP_KERNEL);
2476         if (!mux->shared_intr_buf) {
2477                 dev_err(&interface->dev, "Could not allocate intr buf?");
2478                 goto exit;
2479         }
2480
2481         mutex_init(&mux->shared_int_lock);
2482
2483         return mux;
2484
2485 exit:
2486         kfree(mux->shared_intr_buf);
2487         usb_free_urb(mux->shared_intr_urb);
2488         kfree(mux);
2489         return NULL;
2490 }
2491
2492 /* Gets the port spec for a certain interface */
2493 static int hso_get_config_data(struct usb_interface *interface)
2494 {
2495         struct usb_device *usbdev = interface_to_usbdev(interface);
2496         u8 config_data[17];
2497         u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2498         s32 result;
2499
2500         if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2501                             0x86, 0xC0, 0, 0, config_data, 17,
2502                             USB_CTRL_SET_TIMEOUT) != 0x11) {
2503                 return -EIO;
2504         }
2505
2506         switch (config_data[if_num]) {
2507         case 0x0:
2508                 result = 0;
2509                 break;
2510         case 0x1:
2511                 result = HSO_PORT_DIAG;
2512                 break;
2513         case 0x2:
2514                 result = HSO_PORT_GPS;
2515                 break;
2516         case 0x3:
2517                 result = HSO_PORT_GPS_CONTROL;
2518                 break;
2519         case 0x4:
2520                 result = HSO_PORT_APP;
2521                 break;
2522         case 0x5:
2523                 result = HSO_PORT_APP2;
2524                 break;
2525         case 0x6:
2526                 result = HSO_PORT_CONTROL;
2527                 break;
2528         case 0x7:
2529                 result = HSO_PORT_NETWORK;
2530                 break;
2531         case 0x8:
2532                 result = HSO_PORT_MODEM;
2533                 break;
2534         case 0x9:
2535                 result = HSO_PORT_MSD;
2536                 break;
2537         case 0xa:
2538                 result = HSO_PORT_PCSC;
2539                 break;
2540         case 0xb:
2541                 result = HSO_PORT_VOICE;
2542                 break;
2543         default:
2544                 result = 0;
2545         }
2546
2547         if (result)
2548                 result |= HSO_INTF_BULK;
2549
2550         if (config_data[16] & 0x1)
2551                 result |= HSO_INFO_CRC_BUG;
2552
2553         return result;
2554 }
2555
2556 /* called once for each interface upon device insertion */
2557 static int hso_probe(struct usb_interface *interface,
2558                      const struct usb_device_id *id)
2559 {
2560         int mux, i, if_num, port_spec;
2561         unsigned char port_mask;
2562         struct hso_device *hso_dev = NULL;
2563         struct hso_shared_int *shared_int;
2564         struct hso_device *tmp_dev = NULL;
2565
2566         if_num = interface->altsetting->desc.bInterfaceNumber;
2567
2568         /* Get the interface/port specification from either driver_info or from
2569          * the device itself */
2570         if (id->driver_info)
2571                 port_spec = ((u32 *)(id->driver_info))[if_num];
2572         else
2573                 port_spec = hso_get_config_data(interface);
2574
2575         if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2576                 dev_err(&interface->dev, "Not our interface\n");
2577                 return -ENODEV;
2578         }
2579         /* Check if we need to switch to alt interfaces prior to port
2580          * configuration */
2581         if (interface->num_altsetting > 1)
2582                 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2583         interface->needs_remote_wakeup = 1;
2584
2585         /* Allocate new hso device(s) */
2586         switch (port_spec & HSO_INTF_MASK) {
2587         case HSO_INTF_MUX:
2588                 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2589                         /* Create the network device */
2590                         if (!disable_net) {
2591                                 hso_dev = hso_create_net_device(interface);
2592                                 if (!hso_dev)
2593                                         goto exit;
2594                                 tmp_dev = hso_dev;
2595                         }
2596                 }
2597
2598                 if (hso_get_mux_ports(interface, &port_mask))
2599                         /* TODO: de-allocate everything */
2600                         goto exit;
2601
2602                 shared_int = hso_create_shared_int(interface);
2603                 if (!shared_int)
2604                         goto exit;
2605
2606                 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2607                         if (port_mask & i) {
2608                                 hso_dev = hso_create_mux_serial_device(
2609                                                 interface, i, shared_int);
2610                                 if (!hso_dev)
2611                                         goto exit;
2612                         }
2613                 }
2614
2615                 if (tmp_dev)
2616                         hso_dev = tmp_dev;
2617                 break;
2618
2619         case HSO_INTF_BULK:
2620                 /* It's a regular bulk interface */
2621                 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2622                     && !disable_net)
2623                         hso_dev = hso_create_net_device(interface);
2624                 else
2625                         hso_dev =
2626                             hso_create_bulk_serial_device(interface, port_spec);
2627                 if (!hso_dev)
2628                         goto exit;
2629                 break;
2630         default:
2631                 goto exit;
2632         }
2633
2634         usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2635
2636         /* save our data pointer in this device */
2637         usb_set_intfdata(interface, hso_dev);
2638
2639         /* done */
2640         return 0;
2641 exit:
2642         hso_free_interface(interface);
2643         return -ENODEV;
2644 }
2645
2646 /* device removed, cleaning up */
2647 static void hso_disconnect(struct usb_interface *interface)
2648 {
2649         hso_free_interface(interface);
2650
2651         /* remove reference of our private data */
2652         usb_set_intfdata(interface, NULL);
2653
2654         usb_driver_release_interface(&hso_driver, interface);
2655 }
2656
2657 static void async_get_intf(struct work_struct *data)
2658 {
2659         struct hso_device *hso_dev =
2660             container_of(data, struct hso_device, async_get_intf);
2661         usb_autopm_get_interface(hso_dev->interface);
2662 }
2663
2664 static void async_put_intf(struct work_struct *data)
2665 {
2666         struct hso_device *hso_dev =
2667             container_of(data, struct hso_device, async_put_intf);
2668         usb_autopm_put_interface(hso_dev->interface);
2669 }
2670
2671 static int hso_get_activity(struct hso_device *hso_dev)
2672 {
2673         if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2674                 if (!hso_dev->is_active) {
2675                         hso_dev->is_active = 1;
2676                         schedule_work(&hso_dev->async_get_intf);
2677                 }
2678         }
2679
2680         if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2681                 return -EAGAIN;
2682
2683         usb_mark_last_busy(hso_dev->usb);
2684
2685         return 0;
2686 }
2687
2688 static int hso_put_activity(struct hso_device *hso_dev)
2689 {
2690         if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2691                 if (hso_dev->is_active) {
2692                         hso_dev->is_active = 0;
2693                         schedule_work(&hso_dev->async_put_intf);
2694                         return -EAGAIN;
2695                 }
2696         }
2697         hso_dev->is_active = 0;
2698         return 0;
2699 }
2700
2701 /* called by kernel when we need to suspend device */
2702 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2703 {
2704         int i, result;
2705
2706         /* Stop all serial ports */
2707         for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2708                 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2709                         result = hso_stop_serial_device(serial_table[i]);
2710                         if (result)
2711                                 goto out;
2712                 }
2713         }
2714
2715         /* Stop all network ports */
2716         for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2717                 if (network_table[i] &&
2718                     (network_table[i]->interface == iface)) {
2719                         result = hso_stop_net_device(network_table[i]);
2720                         if (result)
2721                                 goto out;
2722                 }
2723         }
2724
2725 out:
2726         return 0;
2727 }
2728
2729 /* called by kernel when we need to resume device */
2730 static int hso_resume(struct usb_interface *iface)
2731 {
2732         int i, result = 0;
2733         struct hso_net *hso_net;
2734
2735         /* Start all serial ports */
2736         for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2737                 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2738                         if (dev2ser(serial_table[i])->open_count) {
2739                                 result =
2740                                     hso_start_serial_device(serial_table[i], GFP_NOIO);
2741                                 hso_kick_transmit(dev2ser(serial_table[i]));
2742                                 if (result)
2743                                         goto out;
2744                         }
2745                 }
2746         }
2747
2748         /* Start all network ports */
2749         for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2750                 if (network_table[i] &&
2751                     (network_table[i]->interface == iface)) {
2752                         hso_net = dev2net(network_table[i]);
2753                         /* First transmit any lingering data, then restart the
2754                          * device. */
2755                         if (hso_net->skb_tx_buf) {
2756                                 dev_dbg(&iface->dev,
2757                                         "Transmitting lingering data\n");
2758                                 hso_net_start_xmit(hso_net->skb_tx_buf,
2759                                                    hso_net->net);
2760                                 hso_net->skb_tx_buf = NULL;
2761                         }
2762                         result = hso_start_net_device(network_table[i]);
2763                         if (result)
2764                                 goto out;
2765                 }
2766         }
2767
2768 out:
2769         return result;
2770 }
2771
2772 static void hso_serial_ref_free(struct kref *ref)
2773 {
2774         struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2775
2776         hso_free_serial_device(hso_dev);
2777 }
2778
2779 static void hso_free_interface(struct usb_interface *interface)
2780 {
2781         struct hso_serial *hso_dev;
2782         int i;
2783
2784         for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2785                 if (serial_table[i]
2786                     && (serial_table[i]->interface == interface)) {
2787                         hso_dev = dev2ser(serial_table[i]);
2788                         if (hso_dev->tty)
2789                                 tty_hangup(hso_dev->tty);
2790                         mutex_lock(&hso_dev->parent->mutex);
2791                         hso_dev->parent->usb_gone = 1;
2792                         mutex_unlock(&hso_dev->parent->mutex);
2793                         kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2794                 }
2795         }
2796
2797         for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2798                 if (network_table[i]
2799                     && (network_table[i]->interface == interface)) {
2800                         struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2801                         /* hso_stop_net_device doesn't stop the net queue since
2802                          * traffic needs to start it again when suspended */
2803                         netif_stop_queue(dev2net(network_table[i])->net);
2804                         hso_stop_net_device(network_table[i]);
2805                         cancel_work_sync(&network_table[i]->async_put_intf);
2806                         cancel_work_sync(&network_table[i]->async_get_intf);
2807                         if (rfk)
2808                                 rfkill_unregister(rfk);
2809                         hso_free_net_device(network_table[i]);
2810                 }
2811         }
2812 }
2813
2814 /* Helper functions */
2815
2816 /* Get the endpoint ! */
2817 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2818                                                   int type, int dir)
2819 {
2820         int i;
2821         struct usb_host_interface *iface = intf->cur_altsetting;
2822         struct usb_endpoint_descriptor *endp;
2823
2824         for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2825                 endp = &iface->endpoint[i].desc;
2826                 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2827                     ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
2828                         return endp;
2829         }
2830
2831         return NULL;
2832 }
2833
2834 /* Get the byte that describes which ports are enabled */
2835 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2836 {
2837         int i;
2838         struct usb_host_interface *iface = intf->cur_altsetting;
2839
2840         if (iface->extralen == 3) {
2841                 *ports = iface->extra[2];
2842                 return 0;
2843         }
2844
2845         for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2846                 if (iface->endpoint[i].extralen == 3) {
2847                         *ports = iface->endpoint[i].extra[2];
2848                         return 0;
2849                 }
2850         }
2851
2852         return -1;
2853 }
2854
2855 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2856 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2857                                    struct usb_device *usb, gfp_t gfp)
2858 {
2859         int result;
2860
2861         usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2862                          usb_rcvintpipe(usb,
2863                                 shared_int->intr_endp->bEndpointAddress & 0x7F),
2864                          shared_int->shared_intr_buf,
2865                          shared_int->intr_endp->wMaxPacketSize,
2866                          intr_callback, shared_int,
2867                          shared_int->intr_endp->bInterval);
2868
2869         result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2870         if (result)
2871                 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2872                         result);
2873
2874         return result;
2875 }
2876
2877 /* operations setup of the serial interface */
2878 static const struct tty_operations hso_serial_ops = {
2879         .open = hso_serial_open,
2880         .close = hso_serial_close,
2881         .write = hso_serial_write,
2882         .write_room = hso_serial_write_room,
2883         .set_termios = hso_serial_set_termios,
2884         .chars_in_buffer = hso_serial_chars_in_buffer,
2885         .tiocmget = hso_serial_tiocmget,
2886         .tiocmset = hso_serial_tiocmset,
2887         .unthrottle = hso_unthrottle
2888 };
2889
2890 static struct usb_driver hso_driver = {
2891         .name = driver_name,
2892         .probe = hso_probe,
2893         .disconnect = hso_disconnect,
2894         .id_table = hso_ids,
2895         .suspend = hso_suspend,
2896         .resume = hso_resume,
2897         .supports_autosuspend = 1,
2898 };
2899
2900 static int __init hso_init(void)
2901 {
2902         int i;
2903         int result;
2904
2905         /* put it in the log */
2906         printk(KERN_INFO "hso: %s\n", version);
2907
2908         /* Initialise the serial table semaphore and table */
2909         spin_lock_init(&serial_table_lock);
2910         for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2911                 serial_table[i] = NULL;
2912
2913         /* allocate our driver using the proper amount of supported minors */
2914         tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2915         if (!tty_drv)
2916                 return -ENOMEM;
2917
2918         /* fill in all needed values */
2919         tty_drv->magic = TTY_DRIVER_MAGIC;
2920         tty_drv->owner = THIS_MODULE;
2921         tty_drv->driver_name = driver_name;
2922         tty_drv->name = tty_filename;
2923
2924         /* if major number is provided as parameter, use that one */
2925         if (tty_major)
2926                 tty_drv->major = tty_major;
2927
2928         tty_drv->minor_start = 0;
2929         tty_drv->num = HSO_SERIAL_TTY_MINORS;
2930         tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2931         tty_drv->subtype = SERIAL_TYPE_NORMAL;
2932         tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2933         tty_drv->init_termios = tty_std_termios;
2934         tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2935         tty_drv->termios = hso_serial_termios;
2936         tty_drv->termios_locked = hso_serial_termios_locked;
2937         tty_set_operations(tty_drv, &hso_serial_ops);
2938
2939         /* register the tty driver */
2940         result = tty_register_driver(tty_drv);
2941         if (result) {
2942                 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2943                         __func__, result);
2944                 return result;
2945         }
2946
2947         /* register this module as an usb driver */
2948         result = usb_register(&hso_driver);
2949         if (result) {
2950                 printk(KERN_ERR "Could not register hso driver? error: %d\n",
2951                         result);
2952                 /* cleanup serial interface */
2953                 tty_unregister_driver(tty_drv);
2954                 return result;
2955         }
2956
2957         /* done */
2958         return 0;
2959 }
2960
2961 static void __exit hso_exit(void)
2962 {
2963         printk(KERN_INFO "hso: unloaded\n");
2964
2965         tty_unregister_driver(tty_drv);
2966         /* deregister the usb driver */
2967         usb_deregister(&hso_driver);
2968 }
2969
2970 /* Module definitions */
2971 module_init(hso_init);
2972 module_exit(hso_exit);
2973
2974 MODULE_AUTHOR(MOD_AUTHOR);
2975 MODULE_DESCRIPTION(MOD_DESCRIPTION);
2976 MODULE_LICENSE(MOD_LICENSE);
2977 MODULE_INFO(Version, DRIVER_VERSION);
2978
2979 /* change the debug level (eg: insmod hso.ko debug=0x04) */
2980 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2981 module_param(debug, int, S_IRUGO | S_IWUSR);
2982
2983 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
2984 MODULE_PARM_DESC(tty_major, "Set the major tty number");
2985 module_param(tty_major, int, S_IRUGO | S_IWUSR);
2986
2987 /* disable network interface (eg: insmod hso.ko disable_net=1) */
2988 MODULE_PARM_DESC(disable_net, "Disable the network interface");
2989 module_param(disable_net, int, S_IRUGO | S_IWUSR);