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