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