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