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