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