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