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