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