1 From: Greg Kroah-Hartman <gregkh@suse.de>
2 Date: Wed, 14 May 2008 04:57:12 +0000 (-0700)
3 Subject: HSO: add option hso driver
4 X-Git-Url: http://git.kernel.org/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git;a=commitdiff_plain;h=72dc1c096c7051a48ab1dbb12f71976656b55eb5;hp=44f74c046961bd1f21b7d35baaa9165572d1e975
6 HSO: add option hso driver
8 This driver is for a number of different Option devices. Originally
9 written by Option and Andrew Bird, but cleaned up massivly for
10 acceptance into mainline by me and others.
12 Many thanks to the following for their help in cleaning up the driver by
13 providing feedback and patches to it:
14 - Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
15 - Oliver Neukum <oliver@neukum.org>
16 - Alan Cox <alan@lxorguk.ukuu.org.uk>
17 - Javier Marcet <javier@krausbeck.org>
19 Cc: Andrew Bird <ajb@spheresystems.co.uk>
20 Cc: Javier Marcet <javier@krausbeck.org>
21 Cc: Filip Aben <f.aben@option.com>
22 Cc: Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
23 Cc: Oliver Neukum <oliver@neukum.org>
24 Acked-by: Alan Cox <alan@lxorguk.ukuu.org.uk>
25 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
26 Signed-off-by: Jeff Garzik <jgarzik@redhat.com>
29 --- a/drivers/net/Makefile
30 +++ b/drivers/net/Makefile
31 @@ -237,6 +237,7 @@ obj-$(CONFIG_USB_CATC) += usb/
32 obj-$(CONFIG_USB_KAWETH) += usb/
33 obj-$(CONFIG_USB_PEGASUS) += usb/
34 obj-$(CONFIG_USB_RTL8150) += usb/
35 +obj-$(CONFIG_USB_HSO) += usb/
36 obj-$(CONFIG_USB_USBNET) += usb/
37 obj-$(CONFIG_USB_ZD1201) += usb/
39 --- a/drivers/net/usb/Kconfig
40 +++ b/drivers/net/usb/Kconfig
41 @@ -154,6 +154,16 @@ config USB_NET_AX8817X
42 This driver creates an interface named "ethX", where X depends on
43 what other networking devices you have in use.
46 + tristate "Option USB High Speed Mobile Devices"
47 + depends on USB && RFKILL
50 + Choose this option if you have an Option HSDPA/HSUPA card.
51 + These cards support downlink speeds of 7.2Mbps or greater.
53 + To compile this driver as a module, choose M here: the
54 + module will be called hso.
56 config USB_NET_CDCETHER
57 tristate "CDC Ethernet support (smart devices such as cable modems)"
58 --- a/drivers/net/usb/Makefile
59 +++ b/drivers/net/usb/Makefile
60 @@ -6,6 +6,7 @@ obj-$(CONFIG_USB_CATC) += catc.o
61 obj-$(CONFIG_USB_KAWETH) += kaweth.o
62 obj-$(CONFIG_USB_PEGASUS) += pegasus.o
63 obj-$(CONFIG_USB_RTL8150) += rtl8150.o
64 +obj-$(CONFIG_USB_HSO) += hso.o
65 obj-$(CONFIG_USB_NET_AX8817X) += asix.o
66 obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o
67 obj-$(CONFIG_USB_NET_DM9601) += dm9601.o
69 +++ b/drivers/net/usb/hso.c
71 +/******************************************************************************
73 + * Driver for Option High Speed Mobile Devices.
75 + * Copyright (C) 2008 Option International
76 + * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
77 + * <ajb@spheresystems.co.uk>
78 + * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
79 + * Copyright (C) 2008 Novell, Inc.
81 + * This program is free software; you can redistribute it and/or modify
82 + * it under the terms of the GNU General Public License version 2 as
83 + * published by the Free Software Foundation.
85 + * This program is distributed in the hope that it will be useful,
86 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
87 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
88 + * GNU General Public License for more details.
90 + * You should have received a copy of the GNU General Public License
91 + * along with this program; if not, write to the Free Software
92 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
96 + *****************************************************************************/
98 +/******************************************************************************
100 + * Description of the device:
102 + * Interface 0: Contains the IP network interface on the bulk end points.
103 + * The multiplexed serial ports are using the interrupt and
104 + * control endpoints.
105 + * Interrupt contains a bitmap telling which multiplexed
106 + * serialport needs servicing.
108 + * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
109 + * port is opened, as this have a huge impact on the network port
112 + * Interface 2: Standard modem interface - circuit switched interface, should
115 + *****************************************************************************/
117 +#include <linux/sched.h>
118 +#include <linux/slab.h>
119 +#include <linux/init.h>
120 +#include <linux/delay.h>
121 +#include <linux/netdevice.h>
122 +#include <linux/module.h>
123 +#include <linux/ethtool.h>
124 +#include <linux/usb.h>
125 +#include <linux/timer.h>
126 +#include <linux/tty.h>
127 +#include <linux/tty_driver.h>
128 +#include <linux/tty_flip.h>
129 +#include <linux/kmod.h>
130 +#include <linux/rfkill.h>
131 +#include <linux/ip.h>
132 +#include <linux/uaccess.h>
133 +#include <linux/usb/cdc.h>
134 +#include <net/arp.h>
135 +#include <asm/byteorder.h>
138 +#define DRIVER_VERSION "1.2"
139 +#define MOD_AUTHOR "Option Wireless"
140 +#define MOD_DESCRIPTION "USB High Speed Option driver"
141 +#define MOD_LICENSE "GPL"
143 +#define HSO_MAX_NET_DEVICES 10
144 +#define HSO__MAX_MTU 2048
145 +#define DEFAULT_MTU 1500
146 +#define DEFAULT_MRU 1500
148 +#define CTRL_URB_RX_SIZE 1024
149 +#define CTRL_URB_TX_SIZE 64
151 +#define BULK_URB_RX_SIZE 4096
152 +#define BULK_URB_TX_SIZE 8192
154 +#define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
155 +#define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
156 +#define MUX_BULK_RX_BUF_COUNT 4
157 +#define USB_TYPE_OPTION_VENDOR 0x20
159 +/* These definitions are used with the struct hso_net flags element */
160 +/* - use *_bit operations on it. (bit indices not values.) */
161 +#define HSO_NET_RUNNING 0
163 +#define HSO_NET_TX_TIMEOUT (HZ*10)
165 +/* Serial port defines and structs. */
166 +#define HSO_SERIAL_FLAG_RX_SENT 0
168 +#define HSO_SERIAL_MAGIC 0x48534f31
170 +/* Number of ttys to handle */
171 +#define HSO_SERIAL_TTY_MINORS 256
173 +#define MAX_RX_URBS 2
175 +#define get_serial_by_tty(x) \
176 + (x ? (struct hso_serial *)x->driver_data : NULL)
178 +/*****************************************************************************/
179 +/* Debugging functions */
180 +/*****************************************************************************/
181 +#define D__(lvl_, fmt, arg...) \
183 + printk(lvl_ "[%d:%s]: " fmt "\n", \
184 + __LINE__, __func__, ## arg); \
187 +#define D_(lvl, args...) \
190 + D__(KERN_INFO, args); \
193 +#define D1(args...) D_(0x01, ##args)
194 +#define D2(args...) D_(0x02, ##args)
195 +#define D3(args...) D_(0x04, ##args)
196 +#define D4(args...) D_(0x08, ##args)
197 +#define D5(args...) D_(0x10, ##args)
199 +/*****************************************************************************/
201 +/*****************************************************************************/
202 +enum pkt_parse_state {
208 +/*****************************************************************************/
210 +/*****************************************************************************/
212 +struct hso_shared_int {
213 + struct usb_endpoint_descriptor *intr_endp;
214 + void *shared_intr_buf;
215 + struct urb *shared_intr_urb;
216 + struct usb_device *usb;
219 + struct mutex shared_int_lock;
223 + struct hso_device *parent;
224 + struct net_device *net;
225 + struct rfkill *rfkill;
227 + struct usb_endpoint_descriptor *in_endp;
228 + struct usb_endpoint_descriptor *out_endp;
230 + struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
231 + struct urb *mux_bulk_tx_urb;
232 + void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
233 + void *mux_bulk_tx_buf;
235 + struct sk_buff *skb_rx_buf;
236 + struct sk_buff *skb_tx_buf;
238 + enum pkt_parse_state rx_parse_state;
239 + spinlock_t net_lock;
241 + unsigned short rx_buf_size;
242 + unsigned short rx_buf_missing;
243 + struct iphdr rx_ip_hdr;
245 + unsigned long flags;
249 + struct hso_device *parent;
253 + struct hso_shared_int *shared_int;
255 + /* rx/tx urb could be either a bulk urb or a control urb depending
256 + on which serial port it is used on. */
257 + struct urb *rx_urb[MAX_RX_URBS];
259 + u8 *rx_data[MAX_RX_URBS];
260 + u16 rx_data_length; /* should contain allocated length */
262 + struct urb *tx_urb;
265 + u16 tx_data_length; /* should contain allocated length */
267 + u16 tx_buffer_count;
268 + struct usb_ctrlrequest ctrl_req_tx;
269 + struct usb_ctrlrequest ctrl_req_rx;
271 + struct usb_endpoint_descriptor *in_endp;
272 + struct usb_endpoint_descriptor *out_endp;
274 + unsigned long flags;
277 + unsigned tx_urb_used:1;
279 + /* from usb_serial_port */
280 + struct tty_struct *tty;
282 + spinlock_t serial_lock;
284 + int (*write_data) (struct hso_serial *serial);
289 + struct hso_serial *dev_serial;
290 + struct hso_net *dev_net;
297 + struct work_struct async_get_intf;
298 + struct work_struct async_put_intf;
300 + struct usb_device *usb;
301 + struct usb_interface *interface;
303 + struct device *dev;
305 + struct mutex mutex;
308 +/* Type of interface */
309 +#define HSO_INTF_MASK 0xFF00
310 +#define HSO_INTF_MUX 0x0100
311 +#define HSO_INTF_BULK 0x0200
314 +#define HSO_PORT_MASK 0xFF
315 +#define HSO_PORT_NO_PORT 0x0
316 +#define HSO_PORT_CONTROL 0x1
317 +#define HSO_PORT_APP 0x2
318 +#define HSO_PORT_GPS 0x3
319 +#define HSO_PORT_PCSC 0x4
320 +#define HSO_PORT_APP2 0x5
321 +#define HSO_PORT_GPS_CONTROL 0x6
322 +#define HSO_PORT_MSD 0x7
323 +#define HSO_PORT_VOICE 0x8
324 +#define HSO_PORT_DIAG2 0x9
325 +#define HSO_PORT_DIAG 0x10
326 +#define HSO_PORT_MODEM 0x11
327 +#define HSO_PORT_NETWORK 0x12
329 +/* Additional device info */
330 +#define HSO_INFO_MASK 0xFF000000
331 +#define HSO_INFO_CRC_BUG 0x01000000
333 +/*****************************************************************************/
335 +/*****************************************************************************/
336 +/* Serial driver functions */
337 +static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
338 + unsigned int set, unsigned int clear);
339 +static void ctrl_callback(struct urb *urb);
340 +static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
341 +static void hso_kick_transmit(struct hso_serial *serial);
342 +/* Helper functions */
343 +static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
344 + struct usb_device *usb, gfp_t gfp);
345 +static void log_usb_status(int status, const char *function);
346 +static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
347 + int type, int dir);
348 +static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
349 +static void hso_free_interface(struct usb_interface *intf);
350 +static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
351 +static int hso_stop_serial_device(struct hso_device *hso_dev);
352 +static int hso_start_net_device(struct hso_device *hso_dev);
353 +static void hso_free_shared_int(struct hso_shared_int *shared_int);
354 +static int hso_stop_net_device(struct hso_device *hso_dev);
355 +static void hso_serial_ref_free(struct kref *ref);
356 +static void async_get_intf(struct work_struct *data);
357 +static void async_put_intf(struct work_struct *data);
358 +static int hso_put_activity(struct hso_device *hso_dev);
359 +static int hso_get_activity(struct hso_device *hso_dev);
361 +/*****************************************************************************/
362 +/* Helping functions */
363 +/*****************************************************************************/
367 +#define dev2net(x) (x->port_data.dev_net)
368 +#define dev2ser(x) (x->port_data.dev_serial)
370 +/* Debugging functions */
372 +static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
377 + printk(KERN_DEBUG "[%d:%s]: len %d", line_count, func_name, len);
379 + for (i = 0; i < len; i++) {
381 + printk("\n 0x%03x: ", i);
382 + printk("%02x ", (unsigned char)buf[i]);
387 +#define DUMP(buf_, len_) \
388 + dbg_dump(__LINE__, __func__, buf_, len_)
390 +#define DUMP1(buf_, len_) \
392 + if (0x01 & debug) \
393 + DUMP(buf_, len_); \
396 +#define DUMP(buf_, len_)
397 +#define DUMP1(buf_, len_)
400 +/* module parameters */
402 +static int tty_major;
403 +static int disable_net;
406 +static const char driver_name[] = "hso";
407 +static const char tty_filename[] = "ttyHS";
408 +static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
409 +/* the usb driver itself (registered in hso_init) */
410 +static struct usb_driver hso_driver;
411 +/* serial structures */
412 +static struct tty_driver *tty_drv;
413 +static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
414 +static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
415 +static spinlock_t serial_table_lock;
416 +static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
417 +static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
419 +static const s32 default_port_spec[] = {
420 + HSO_INTF_MUX | HSO_PORT_NETWORK,
421 + HSO_INTF_BULK | HSO_PORT_DIAG,
422 + HSO_INTF_BULK | HSO_PORT_MODEM,
426 +static const s32 icon321_port_spec[] = {
427 + HSO_INTF_MUX | HSO_PORT_NETWORK,
428 + HSO_INTF_BULK | HSO_PORT_DIAG2,
429 + HSO_INTF_BULK | HSO_PORT_MODEM,
430 + HSO_INTF_BULK | HSO_PORT_DIAG,
434 +#define default_port_device(vendor, product) \
435 + USB_DEVICE(vendor, product), \
436 + .driver_info = (kernel_ulong_t)default_port_spec
438 +#define icon321_port_device(vendor, product) \
439 + USB_DEVICE(vendor, product), \
440 + .driver_info = (kernel_ulong_t)icon321_port_spec
442 +/* list of devices we support */
443 +static const struct usb_device_id hso_ids[] = {
444 + {default_port_device(0x0af0, 0x6711)},
445 + {default_port_device(0x0af0, 0x6731)},
446 + {default_port_device(0x0af0, 0x6751)},
447 + {default_port_device(0x0af0, 0x6771)},
448 + {default_port_device(0x0af0, 0x6791)},
449 + {default_port_device(0x0af0, 0x6811)},
450 + {default_port_device(0x0af0, 0x6911)},
451 + {default_port_device(0x0af0, 0x6951)},
452 + {default_port_device(0x0af0, 0x6971)},
453 + {default_port_device(0x0af0, 0x7011)},
454 + {default_port_device(0x0af0, 0x7031)},
455 + {default_port_device(0x0af0, 0x7051)},
456 + {default_port_device(0x0af0, 0x7071)},
457 + {default_port_device(0x0af0, 0x7111)},
458 + {default_port_device(0x0af0, 0x7211)},
459 + {default_port_device(0x0af0, 0x7251)},
460 + {default_port_device(0x0af0, 0x7271)},
461 + {default_port_device(0x0af0, 0x7311)},
462 + {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
463 + {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
464 + {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
465 + {default_port_device(0x0af0, 0xd033)}, /* Icon-322 */
466 + {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
467 + {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
468 + {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
469 + {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
470 + {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
473 +MODULE_DEVICE_TABLE(usb, hso_ids);
475 +/* Sysfs attribute */
476 +static ssize_t hso_sysfs_show_porttype(struct device *dev,
477 + struct device_attribute *attr,
480 + struct hso_device *hso_dev = dev->driver_data;
486 + switch (hso_dev->port_spec & HSO_PORT_MASK) {
487 + case HSO_PORT_CONTROL:
488 + port_name = "Control";
491 + port_name = "Application";
493 + case HSO_PORT_APP2:
494 + port_name = "Application2";
499 + case HSO_PORT_GPS_CONTROL:
500 + port_name = "GPS Control";
502 + case HSO_PORT_PCSC:
503 + port_name = "PCSC";
505 + case HSO_PORT_DIAG:
506 + port_name = "Diagnostic";
508 + case HSO_PORT_DIAG2:
509 + port_name = "Diagnostic2";
511 + case HSO_PORT_MODEM:
512 + port_name = "Modem";
514 + case HSO_PORT_NETWORK:
515 + port_name = "Network";
518 + port_name = "Unknown";
522 + return sprintf(buf, "%s\n", port_name);
524 +static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
526 +/* converts mux value to a port spec value */
527 +static u32 hso_mux_to_port(int mux)
533 + result = HSO_PORT_CONTROL;
536 + result = HSO_PORT_APP;
539 + result = HSO_PORT_PCSC;
542 + result = HSO_PORT_GPS;
545 + result = HSO_PORT_APP2;
548 + result = HSO_PORT_NO_PORT;
553 +/* converts port spec value to a mux value */
554 +static u32 hso_port_to_mux(int port)
558 + switch (port & HSO_PORT_MASK) {
559 + case HSO_PORT_CONTROL:
565 + case HSO_PORT_PCSC:
571 + case HSO_PORT_APP2:
580 +static struct hso_serial *get_serial_by_shared_int_and_type(
581 + struct hso_shared_int *shared_int,
586 + port = hso_mux_to_port(mux);
588 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
589 + if (serial_table[i]
590 + && (dev2ser(serial_table[i])->shared_int == shared_int)
591 + && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
592 + return dev2ser(serial_table[i]);
599 +static struct hso_serial *get_serial_by_index(unsigned index)
601 + struct hso_serial *serial;
602 + unsigned long flags;
604 + if (!serial_table[index])
606 + spin_lock_irqsave(&serial_table_lock, flags);
607 + serial = dev2ser(serial_table[index]);
608 + spin_unlock_irqrestore(&serial_table_lock, flags);
613 +static int get_free_serial_index(void)
616 + unsigned long flags;
618 + spin_lock_irqsave(&serial_table_lock, flags);
619 + for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
620 + if (serial_table[index] == NULL) {
621 + spin_unlock_irqrestore(&serial_table_lock, flags);
625 + spin_unlock_irqrestore(&serial_table_lock, flags);
627 + printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
631 +static void set_serial_by_index(unsigned index, struct hso_serial *serial)
633 + unsigned long flags;
634 + spin_lock_irqsave(&serial_table_lock, flags);
636 + serial_table[index] = serial->parent;
638 + serial_table[index] = NULL;
639 + spin_unlock_irqrestore(&serial_table_lock, flags);
642 +/* log a meaningfull explanation of an USB status */
643 +static void log_usb_status(int status, const char *function)
649 + explanation = "no device";
652 + explanation = "endpoint not enabled";
655 + explanation = "endpoint stalled";
658 + explanation = "not enough bandwidth";
661 + explanation = "device disabled";
663 + case -EHOSTUNREACH:
664 + explanation = "device suspended";
670 + explanation = "internal error";
673 + explanation = "unknown status";
676 + D1("%s: received USB status - %s (%d)", function, explanation, status);
679 +/* Network interface functions */
681 +/* called when net interface is brought up by ifconfig */
682 +static int hso_net_open(struct net_device *net)
684 + struct hso_net *odev = netdev_priv(net);
685 + unsigned long flags = 0;
688 + dev_err(&net->dev, "No net device !\n");
692 + odev->skb_tx_buf = NULL;
694 + /* setup environment */
695 + spin_lock_irqsave(&odev->net_lock, flags);
696 + odev->rx_parse_state = WAIT_IP;
697 + odev->rx_buf_size = 0;
698 + odev->rx_buf_missing = sizeof(struct iphdr);
699 + spin_unlock_irqrestore(&odev->net_lock, flags);
701 + hso_start_net_device(odev->parent);
703 + /* We are up and running. */
704 + set_bit(HSO_NET_RUNNING, &odev->flags);
706 + /* Tell the kernel we are ready to start receiving from it */
707 + netif_start_queue(net);
712 +/* called when interface is brought down by ifconfig */
713 +static int hso_net_close(struct net_device *net)
715 + struct hso_net *odev = netdev_priv(net);
717 + /* we don't need the queue anymore */
718 + netif_stop_queue(net);
719 + /* no longer running */
720 + clear_bit(HSO_NET_RUNNING, &odev->flags);
722 + hso_stop_net_device(odev->parent);
728 +/* USB tells is xmit done, we should start the netqueue again */
729 +static void write_bulk_callback(struct urb *urb)
731 + struct hso_net *odev = urb->context;
732 + int status = urb->status;
735 + if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
736 + dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
740 + /* Do we still have a valid kernel network device? */
741 + if (!netif_device_present(odev->net)) {
742 + dev_err(&urb->dev->dev, "%s: net device not present\n",
747 + /* log status, but don't act on it, we don't need to resubmit anything
750 + log_usb_status(status, __func__);
752 + hso_put_activity(odev->parent);
754 + /* Tell the network interface we are ready for another frame */
755 + netif_wake_queue(odev->net);
758 +/* called by kernel when we need to transmit a packet */
759 +static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
761 + struct hso_net *odev = netdev_priv(net);
764 + /* Tell the kernel, "No more frames 'til we are done with this one." */
765 + netif_stop_queue(net);
766 + if (hso_get_activity(odev->parent) == -EAGAIN) {
767 + odev->skb_tx_buf = skb;
772 + DUMP1(skb->data, skb->len);
773 + /* Copy it from kernel memory to OUR memory */
774 + memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
775 + D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
777 + /* Fill in the URB for shipping it out. */
778 + usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
780 + usb_sndbulkpipe(odev->parent->usb,
782 + bEndpointAddress & 0x7F),
783 + odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
786 + /* Deal with the Zero Length packet problem, I hope */
787 + odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
789 + /* Send the URB on its merry way. */
790 + result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
792 + dev_warn(&odev->parent->interface->dev,
793 + "failed mux_bulk_tx_urb %d", result);
794 + net->stats.tx_errors++;
795 + netif_start_queue(net);
797 + net->stats.tx_packets++;
798 + net->stats.tx_bytes += skb->len;
799 + /* And tell the kernel when the last transmit started. */
800 + net->trans_start = jiffies;
802 + dev_kfree_skb(skb);
807 +static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
809 + struct hso_net *odev = netdev_priv(net);
811 + strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
812 + strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
813 + usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
816 +static struct ethtool_ops ops = {
817 + .get_drvinfo = hso_get_drvinfo,
818 + .get_link = ethtool_op_get_link
821 +/* called when a packet did not ack after watchdogtimeout */
822 +static void hso_net_tx_timeout(struct net_device *net)
824 + struct hso_net *odev = netdev_priv(net);
829 + /* Tell syslog we are hosed. */
830 + dev_warn(&net->dev, "Tx timed out.\n");
832 + /* Tear the waiting frame off the list */
833 + if (odev->mux_bulk_tx_urb
834 + && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
835 + usb_unlink_urb(odev->mux_bulk_tx_urb);
837 + /* Update statistics */
838 + net->stats.tx_errors++;
841 +/* make a real packet from the received USB buffer */
842 +static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
843 + unsigned int count, unsigned char is_eop)
845 + unsigned short temp_bytes;
846 + unsigned short buffer_offset = 0;
847 + unsigned short frame_len;
848 + unsigned char *tmp_rx_buf;
850 + /* log if needed */
851 + D1("Rx %d bytes", count);
852 + DUMP(ip_pkt, min(128, (int)count));
855 + switch (odev->rx_parse_state) {
857 + /* waiting for IP header. */
858 + /* wanted bytes - size of ip header */
861 + odev->rx_buf_missing) ? count : odev->
864 + memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
865 + odev->rx_buf_size, ip_pkt + buffer_offset,
868 + odev->rx_buf_size += temp_bytes;
869 + buffer_offset += temp_bytes;
870 + odev->rx_buf_missing -= temp_bytes;
871 + count -= temp_bytes;
873 + if (!odev->rx_buf_missing) {
874 + /* header is complete allocate an sk_buffer and
875 + * continue to WAIT_DATA */
876 + frame_len = ntohs(odev->rx_ip_hdr.tot_len);
878 + if ((frame_len > DEFAULT_MRU) ||
879 + (frame_len < sizeof(struct iphdr))) {
880 + dev_err(&odev->net->dev,
881 + "Invalid frame (%d) length\n",
883 + odev->rx_parse_state = WAIT_SYNC;
886 + /* Allocate an sk_buff */
887 + odev->skb_rx_buf = dev_alloc_skb(frame_len);
888 + if (!odev->skb_rx_buf) {
889 + /* We got no receive buffer. */
890 + D1("could not allocate memory");
891 + odev->rx_parse_state = WAIT_SYNC;
894 + /* Here's where it came from */
895 + odev->skb_rx_buf->dev = odev->net;
897 + /* Copy what we got so far. make room for iphdr
900 + skb_put(odev->skb_rx_buf,
901 + sizeof(struct iphdr));
902 + memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
903 + sizeof(struct iphdr));
906 + odev->rx_buf_size = sizeof(struct iphdr);
908 + /* Filip actually use .tot_len */
909 + odev->rx_buf_missing =
910 + frame_len - sizeof(struct iphdr);
911 + odev->rx_parse_state = WAIT_DATA;
916 + temp_bytes = (count < odev->rx_buf_missing)
917 + ? count : odev->rx_buf_missing;
919 + /* Copy the rest of the bytes that are left in the
920 + * buffer into the waiting sk_buf. */
921 + /* Make room for temp_bytes after tail. */
922 + tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
923 + memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
925 + odev->rx_buf_missing -= temp_bytes;
926 + count -= temp_bytes;
927 + buffer_offset += temp_bytes;
928 + odev->rx_buf_size += temp_bytes;
929 + if (!odev->rx_buf_missing) {
930 + /* Packet is complete. Inject into stack. */
931 + /* We have IP packet here */
932 + odev->skb_rx_buf->protocol =
933 + __constant_htons(ETH_P_IP);
934 + /* don't check it */
935 + odev->skb_rx_buf->ip_summed =
936 + CHECKSUM_UNNECESSARY;
938 + skb_reset_mac_header(odev->skb_rx_buf);
940 + /* Ship it off to the kernel */
941 + netif_rx(odev->skb_rx_buf);
942 + /* No longer our buffer. */
943 + odev->skb_rx_buf = NULL;
945 + /* update out statistics */
946 + odev->net->stats.rx_packets++;
948 + odev->net->stats.rx_bytes += odev->rx_buf_size;
950 + odev->rx_buf_size = 0;
951 + odev->rx_buf_missing = sizeof(struct iphdr);
952 + odev->rx_parse_state = WAIT_IP;
967 + /* Recovery mechanism for WAIT_SYNC state. */
969 + if (odev->rx_parse_state == WAIT_SYNC) {
970 + odev->rx_parse_state = WAIT_IP;
971 + odev->rx_buf_size = 0;
972 + odev->rx_buf_missing = sizeof(struct iphdr);
977 +/* Moving data from usb to kernel (in interrupt state) */
978 +static void read_bulk_callback(struct urb *urb)
980 + struct hso_net *odev = urb->context;
981 + struct net_device *net;
983 + int status = urb->status;
985 + /* is al ok? (Filip: Who's Al ?) */
987 + log_usb_status(status, __func__);
992 + if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
993 + D1("BULK IN callback but driver is not active!");
996 + usb_mark_last_busy(urb->dev);
1000 + if (!netif_device_present(net)) {
1001 + /* Somebody killed our network interface... */
1005 + if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
1007 + u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1008 + rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
1009 + if (((rest == 5) || (rest == 6))
1010 + && !memcmp(((u8 *) urb->transfer_buffer) +
1011 + urb->actual_length - 4, crc_check, 4)) {
1012 + urb->actual_length -= 4;
1016 + /* do we even have a packet? */
1017 + if (urb->actual_length) {
1018 + /* Handle the IP stream, add header and push it onto network
1019 + * stack if the packet is complete. */
1020 + spin_lock(&odev->net_lock);
1021 + packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1022 + (urb->transfer_buffer_length >
1023 + urb->actual_length) ? 1 : 0);
1024 + spin_unlock(&odev->net_lock);
1027 + /* We are done with this URB, resubmit it. Prep the USB to wait for
1028 + * another frame. Reuse same as received. */
1029 + usb_fill_bulk_urb(urb,
1030 + odev->parent->usb,
1031 + usb_rcvbulkpipe(odev->parent->usb,
1033 + bEndpointAddress & 0x7F),
1034 + urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1035 + read_bulk_callback, odev);
1037 + /* Give this to the USB subsystem so it can tell us when more data
1039 + result = usb_submit_urb(urb, GFP_ATOMIC);
1041 + dev_warn(&odev->parent->interface->dev,
1042 + "%s failed submit mux_bulk_rx_urb %d", __func__,
1046 +/* Serial driver functions */
1048 +static void _hso_serial_set_termios(struct tty_struct *tty,
1049 + struct ktermios *old)
1051 + struct hso_serial *serial = get_serial_by_tty(tty);
1052 + struct ktermios *termios;
1054 + if ((!tty) || (!tty->termios) || (!serial)) {
1055 + printk(KERN_ERR "%s: no tty structures", __func__);
1059 + D4("port %d", serial->minor);
1062 + * The default requirements for this device are:
1064 + termios = tty->termios;
1065 + termios->c_iflag &=
1066 + ~(IGNBRK /* disable ignore break */
1067 + | BRKINT /* disable break causes interrupt */
1068 + | PARMRK /* disable mark parity errors */
1069 + | ISTRIP /* disable clear high bit of input characters */
1070 + | INLCR /* disable translate NL to CR */
1071 + | IGNCR /* disable ignore CR */
1072 + | ICRNL /* disable translate CR to NL */
1073 + | IXON); /* disable enable XON/XOFF flow control */
1075 + /* disable postprocess output characters */
1076 + termios->c_oflag &= ~OPOST;
1078 + termios->c_lflag &=
1079 + ~(ECHO /* disable echo input characters */
1080 + | ECHONL /* disable echo new line */
1081 + | ICANON /* disable erase, kill, werase, and rprnt
1082 + special characters */
1083 + | ISIG /* disable interrupt, quit, and suspend special
1085 + | IEXTEN); /* disable non-POSIX special characters */
1087 + termios->c_cflag &=
1088 + ~(CSIZE /* no size */
1089 + | PARENB /* disable parity bit */
1090 + | CBAUD /* clear current baud rate */
1091 + | CBAUDEX); /* clear current buad rate */
1093 + termios->c_cflag |= CS8; /* character size 8 bits */
1095 + /* baud rate 115200 */
1096 + tty_encode_baud_rate(serial->tty, 115200, 115200);
1099 + * Force low_latency on; otherwise the pushes are scheduled;
1100 + * this is bad as it opens up the possibility of dropping bytes
1101 + * on the floor. We don't want to drop bytes on the floor. :)
1103 + serial->tty->low_latency = 1;
1107 +/* open the requested serial port */
1108 +static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1110 + struct hso_serial *serial = get_serial_by_index(tty->index);
1113 + /* sanity check */
1114 + if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1115 + tty->driver_data = NULL;
1116 + D1("Failed to open port");
1120 + mutex_lock(&serial->parent->mutex);
1121 + result = usb_autopm_get_interface(serial->parent->interface);
1125 + D1("Opening %d", serial->minor);
1126 + kref_get(&serial->parent->ref);
1129 + tty->driver_data = serial;
1130 + serial->tty = tty;
1132 + /* check for port allready opened, if not set the termios */
1133 + serial->open_count++;
1134 + if (serial->open_count == 1) {
1135 + tty->low_latency = 1;
1136 + serial->flags = 0;
1137 + /* Force default termio settings */
1138 + _hso_serial_set_termios(tty, NULL);
1139 + result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1141 + hso_stop_serial_device(serial->parent);
1142 + serial->open_count--;
1143 + kref_put(&serial->parent->ref, hso_serial_ref_free);
1146 + D1("Port was already open");
1149 + usb_autopm_put_interface(serial->parent->interface);
1153 + hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1155 + mutex_unlock(&serial->parent->mutex);
1159 +/* close the requested serial port */
1160 +static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1162 + struct hso_serial *serial = tty->driver_data;
1165 + D1("Closing serial port");
1167 + mutex_lock(&serial->parent->mutex);
1168 + usb_gone = serial->parent->usb_gone;
1171 + usb_autopm_get_interface(serial->parent->interface);
1173 + /* reset the rts and dtr */
1174 + /* do the actual close */
1175 + serial->open_count--;
1176 + if (serial->open_count <= 0) {
1177 + kref_put(&serial->parent->ref, hso_serial_ref_free);
1178 + serial->open_count = 0;
1179 + if (serial->tty) {
1180 + serial->tty->driver_data = NULL;
1181 + serial->tty = NULL;
1184 + hso_stop_serial_device(serial->parent);
1187 + usb_autopm_put_interface(serial->parent->interface);
1188 + mutex_unlock(&serial->parent->mutex);
1191 +/* close the requested serial port */
1192 +static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1195 + struct hso_serial *serial = get_serial_by_tty(tty);
1196 + int space, tx_bytes;
1197 + unsigned long flags;
1199 + /* sanity check */
1200 + if (serial == NULL) {
1201 + printk(KERN_ERR "%s: serial is NULL\n", __func__);
1205 + spin_lock_irqsave(&serial->serial_lock, flags);
1207 + space = serial->tx_data_length - serial->tx_buffer_count;
1208 + tx_bytes = (count < space) ? count : space;
1213 + memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1214 + serial->tx_buffer_count += tx_bytes;
1217 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1219 + hso_kick_transmit(serial);
1224 +/* how much room is there for writing */
1225 +static int hso_serial_write_room(struct tty_struct *tty)
1227 + struct hso_serial *serial = get_serial_by_tty(tty);
1229 + unsigned long flags;
1231 + spin_lock_irqsave(&serial->serial_lock, flags);
1232 + room = serial->tx_data_length - serial->tx_buffer_count;
1233 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1235 + /* return free room */
1239 +/* setup the term */
1240 +static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1242 + struct hso_serial *serial = get_serial_by_tty(tty);
1243 + unsigned long flags;
1246 + D5("Termios called with: cflags new[%d] - old[%d]",
1247 + tty->termios->c_cflag, old->c_cflag);
1249 + /* the actual setup */
1250 + spin_lock_irqsave(&serial->serial_lock, flags);
1251 + if (serial->open_count)
1252 + _hso_serial_set_termios(tty, old);
1254 + tty->termios = old;
1255 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1261 +/* how many characters in the buffer */
1262 +static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1264 + struct hso_serial *serial = get_serial_by_tty(tty);
1266 + unsigned long flags;
1268 + /* sanity check */
1269 + if (serial == NULL)
1272 + spin_lock_irqsave(&serial->serial_lock, flags);
1273 + chars = serial->tx_buffer_count;
1274 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1279 +static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1281 + unsigned int value;
1282 + struct hso_serial *serial = get_serial_by_tty(tty);
1283 + unsigned long flags;
1285 + /* sanity check */
1287 + D1("no tty structures");
1291 + spin_lock_irqsave(&serial->serial_lock, flags);
1292 + value = ((serial->rts_state) ? TIOCM_RTS : 0) |
1293 + ((serial->dtr_state) ? TIOCM_DTR : 0);
1294 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1299 +static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1300 + unsigned int set, unsigned int clear)
1303 + unsigned long flags;
1305 + struct hso_serial *serial = get_serial_by_tty(tty);
1307 + /* sanity check */
1309 + D1("no tty structures");
1312 + if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1314 + spin_lock_irqsave(&serial->serial_lock, flags);
1315 + if (set & TIOCM_RTS)
1316 + serial->rts_state = 1;
1317 + if (set & TIOCM_DTR)
1318 + serial->dtr_state = 1;
1320 + if (clear & TIOCM_RTS)
1321 + serial->rts_state = 0;
1322 + if (clear & TIOCM_DTR)
1323 + serial->dtr_state = 0;
1325 + if (serial->dtr_state)
1327 + if (serial->rts_state)
1330 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1332 + return usb_control_msg(serial->parent->usb,
1333 + usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1334 + 0x21, val, if_num, NULL, 0,
1335 + USB_CTRL_SET_TIMEOUT);
1338 +/* starts a transmit */
1339 +static void hso_kick_transmit(struct hso_serial *serial)
1342 + unsigned long flags;
1345 + spin_lock_irqsave(&serial->serial_lock, flags);
1346 + if (!serial->tx_buffer_count)
1349 + if (serial->tx_urb_used)
1352 + /* Wakeup USB interface if necessary */
1353 + if (hso_get_activity(serial->parent) == -EAGAIN)
1356 + /* Switch pointers around to avoid memcpy */
1357 + temp = serial->tx_buffer;
1358 + serial->tx_buffer = serial->tx_data;
1359 + serial->tx_data = temp;
1360 + serial->tx_data_count = serial->tx_buffer_count;
1361 + serial->tx_buffer_count = 0;
1363 + /* If temp is set, it means we switched buffers */
1364 + if (temp && serial->write_data) {
1365 + res = serial->write_data(serial);
1367 + serial->tx_urb_used = 1;
1370 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1373 +/* make a request (for reading and writing data to muxed serial port) */
1374 +static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1375 + struct urb *ctrl_urb,
1376 + struct usb_ctrlrequest *ctrl_req,
1377 + u8 *ctrl_urb_data, u32 size)
1382 + /* Sanity check */
1383 + if (!serial || !ctrl_urb || !ctrl_req) {
1384 + printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1389 + ctrl_req->wValue = 0;
1390 + ctrl_req->wIndex = hso_port_to_mux(port);
1391 + ctrl_req->wLength = size;
1393 + if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1394 + /* Reading command */
1395 + ctrl_req->bRequestType = USB_DIR_IN |
1396 + USB_TYPE_OPTION_VENDOR |
1397 + USB_RECIP_INTERFACE;
1398 + ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1399 + pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1401 + /* Writing command */
1402 + ctrl_req->bRequestType = USB_DIR_OUT |
1403 + USB_TYPE_OPTION_VENDOR |
1404 + USB_RECIP_INTERFACE;
1405 + ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1406 + pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1409 + D2("%s command (%02x) len: %d, port: %d",
1410 + type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1411 + ctrl_req->bRequestType, ctrl_req->wLength, port);
1413 + /* Load ctrl urb */
1414 + ctrl_urb->transfer_flags = 0;
1415 + usb_fill_control_urb(ctrl_urb,
1416 + serial->parent->usb,
1419 + ctrl_urb_data, size, ctrl_callback, serial);
1420 + /* Send it on merry way */
1421 + result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1423 + dev_err(&ctrl_urb->dev->dev,
1424 + "%s failed submit ctrl_urb %d type %d", __func__,
1433 +/* called by intr_callback when read occurs */
1434 +static int hso_mux_serial_read(struct hso_serial *serial)
1440 + memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1441 + /* make the request */
1443 + if (serial->num_rx_urbs != 1) {
1444 + dev_err(&serial->parent->interface->dev,
1445 + "ERROR: mux'd reads with multiple buffers "
1446 + "not possible\n");
1449 + return mux_device_request(serial,
1450 + USB_CDC_GET_ENCAPSULATED_RESPONSE,
1451 + serial->parent->port_spec & HSO_PORT_MASK,
1452 + serial->rx_urb[0],
1453 + &serial->ctrl_req_rx,
1454 + serial->rx_data[0], serial->rx_data_length);
1457 +/* used for muxed serial port callback (muxed serial read) */
1458 +static void intr_callback(struct urb *urb)
1460 + struct hso_shared_int *shared_int = urb->context;
1461 + struct hso_serial *serial;
1462 + unsigned char *port_req;
1463 + int status = urb->status;
1466 + usb_mark_last_busy(urb->dev);
1468 + /* sanity check */
1472 + /* status check */
1474 + log_usb_status(status, __func__);
1477 + D4("\n--- Got intr callback 0x%02X ---", status);
1479 + /* what request? */
1480 + port_req = urb->transfer_buffer;
1481 + D4(" port_req = 0x%.2X\n", *port_req);
1482 + /* loop over all muxed ports to find the one sending this */
1483 + for (i = 0; i < 8; i++) {
1484 + /* max 8 channels on MUX */
1485 + if (*port_req & (1 << i)) {
1486 + serial = get_serial_by_shared_int_and_type(shared_int,
1488 + if (serial != NULL) {
1489 + D1("Pending read interrupt on port %d\n", i);
1490 + if (!test_and_set_bit(HSO_SERIAL_FLAG_RX_SENT,
1491 + &serial->flags)) {
1492 + /* Setup and send a ctrl req read on
1494 + hso_mux_serial_read(serial);
1496 + D1("Already pending a read on "
1502 + /* Resubmit interrupt urb */
1503 + hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1506 +/* called for writing to muxed serial port */
1507 +static int hso_mux_serial_write_data(struct hso_serial *serial)
1509 + if (NULL == serial)
1512 + return mux_device_request(serial,
1513 + USB_CDC_SEND_ENCAPSULATED_COMMAND,
1514 + serial->parent->port_spec & HSO_PORT_MASK,
1516 + &serial->ctrl_req_tx,
1517 + serial->tx_data, serial->tx_data_count);
1520 +/* write callback for Diag and CS port */
1521 +static void hso_std_serial_write_bulk_callback(struct urb *urb)
1523 + struct hso_serial *serial = urb->context;
1524 + int status = urb->status;
1526 + /* sanity check */
1528 + D1("serial == NULL");
1532 + spin_lock(&serial->serial_lock);
1533 + serial->tx_urb_used = 0;
1534 + spin_unlock(&serial->serial_lock);
1536 + log_usb_status(status, __func__);
1539 + hso_put_activity(serial->parent);
1540 + tty_wakeup(serial->tty);
1541 + hso_kick_transmit(serial);
1547 +/* called for writing diag or CS serial port */
1548 +static int hso_std_serial_write_data(struct hso_serial *serial)
1550 + int count = serial->tx_data_count;
1553 + usb_fill_bulk_urb(serial->tx_urb,
1554 + serial->parent->usb,
1555 + usb_sndbulkpipe(serial->parent->usb,
1556 + serial->out_endp->
1557 + bEndpointAddress & 0x7F),
1558 + serial->tx_data, serial->tx_data_count,
1559 + hso_std_serial_write_bulk_callback, serial);
1561 + result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1563 + dev_warn(&serial->parent->usb->dev,
1564 + "Failed to submit urb - res %d\n", result);
1571 +/* callback after read or write on muxed serial port */
1572 +static void ctrl_callback(struct urb *urb)
1574 + struct hso_serial *serial = urb->context;
1575 + struct usb_ctrlrequest *req;
1576 + int status = urb->status;
1578 + /* sanity check */
1582 + spin_lock(&serial->serial_lock);
1583 + serial->tx_urb_used = 0;
1584 + spin_unlock(&serial->serial_lock);
1586 + log_usb_status(status, __func__);
1590 + /* what request? */
1591 + req = (struct usb_ctrlrequest *)(urb->setup_packet);
1592 + D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1593 + D4("Actual length of urb = %d\n", urb->actual_length);
1594 + DUMP1(urb->transfer_buffer, urb->actual_length);
1596 + if (req->bRequestType ==
1597 + (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1598 + /* response to a read command */
1599 + if (serial->open_count > 0) {
1600 + /* handle RX data the normal way */
1601 + put_rxbuf_data(urb, serial);
1604 + /* Re issue a read as long as we receive data. */
1605 + if (urb->actual_length != 0)
1606 + hso_mux_serial_read(serial);
1608 + clear_bit(HSO_SERIAL_FLAG_RX_SENT, &serial->flags);
1610 + hso_put_activity(serial->parent);
1611 + tty_wakeup(serial->tty);
1612 + /* response to a write command */
1613 + hso_kick_transmit(serial);
1617 +/* handle RX data for serial port */
1618 +static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
1620 + struct tty_struct *tty = serial->tty;
1622 + /* Sanity check */
1623 + if (urb == NULL || serial == NULL) {
1624 + D1("serial = NULL");
1628 + /* Push data to tty */
1629 + if (tty && urb->actual_length) {
1630 + D1("data to push to tty");
1631 + tty_insert_flip_string(tty, urb->transfer_buffer,
1632 + urb->actual_length);
1633 + tty_flip_buffer_push(tty);
1637 +/* read callback for Diag and CS port */
1638 +static void hso_std_serial_read_bulk_callback(struct urb *urb)
1640 + struct hso_serial *serial = urb->context;
1642 + int status = urb->status;
1644 + /* sanity check */
1646 + D1("serial == NULL");
1648 + } else if (status) {
1649 + log_usb_status(status, __func__);
1653 + D4("\n--- Got serial_read_bulk callback %02x ---", status);
1654 + D1("Actual length = %d\n", urb->actual_length);
1655 + DUMP1(urb->transfer_buffer, urb->actual_length);
1657 + /* Anyone listening? */
1658 + if (serial->open_count == 0)
1661 + if (status == 0) {
1662 + if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1664 + u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1666 + urb->actual_length %
1667 + serial->in_endp->wMaxPacketSize;
1668 + if (((rest == 5) || (rest == 6))
1669 + && !memcmp(((u8 *) urb->transfer_buffer) +
1670 + urb->actual_length - 4, crc_check, 4)) {
1671 + urb->actual_length -= 4;
1674 + /* Valid data, handle RX data */
1675 + put_rxbuf_data(urb, serial);
1676 + } else if (status == -ENOENT || status == -ECONNRESET) {
1677 + /* Unlinked - check for throttled port. */
1678 + D2("Port %d, successfully unlinked urb", serial->minor);
1680 + D2("Port %d, status = %d for read urb", serial->minor, status);
1684 + usb_mark_last_busy(urb->dev);
1686 + /* We are done with this URB, resubmit it. Prep the USB to wait for
1687 + * another frame */
1688 + usb_fill_bulk_urb(urb, serial->parent->usb,
1689 + usb_rcvbulkpipe(serial->parent->usb,
1691 + bEndpointAddress & 0x7F),
1692 + urb->transfer_buffer, serial->rx_data_length,
1693 + hso_std_serial_read_bulk_callback, serial);
1694 + /* Give this to the USB subsystem so it can tell us when more data
1696 + result = usb_submit_urb(urb, GFP_ATOMIC);
1698 + dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d",
1699 + __func__, result);
1703 +/* Base driver functions */
1705 +static void hso_log_port(struct hso_device *hso_dev)
1708 + char port_dev[20];
1710 + switch (hso_dev->port_spec & HSO_PORT_MASK) {
1711 + case HSO_PORT_CONTROL:
1712 + port_type = "Control";
1714 + case HSO_PORT_APP:
1715 + port_type = "Application";
1717 + case HSO_PORT_GPS:
1718 + port_type = "GPS";
1720 + case HSO_PORT_GPS_CONTROL:
1721 + port_type = "GPS control";
1723 + case HSO_PORT_APP2:
1724 + port_type = "Application2";
1726 + case HSO_PORT_PCSC:
1727 + port_type = "PCSC";
1729 + case HSO_PORT_DIAG:
1730 + port_type = "Diagnostic";
1732 + case HSO_PORT_DIAG2:
1733 + port_type = "Diagnostic2";
1735 + case HSO_PORT_MODEM:
1736 + port_type = "Modem";
1738 + case HSO_PORT_NETWORK:
1739 + port_type = "Network";
1742 + port_type = "Unknown";
1745 + if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
1746 + sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
1748 + sprintf(port_dev, "/dev/%s%d", tty_filename,
1749 + dev2ser(hso_dev)->minor);
1751 + dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
1752 + port_type, port_dev);
1755 +static int hso_start_net_device(struct hso_device *hso_dev)
1757 + int i, result = 0;
1758 + struct hso_net *hso_net = dev2net(hso_dev);
1763 + /* send URBs for all read buffers */
1764 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1766 + /* Prep a receive URB */
1767 + usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
1769 + usb_rcvbulkpipe(hso_dev->usb,
1770 + hso_net->in_endp->
1771 + bEndpointAddress & 0x7F),
1772 + hso_net->mux_bulk_rx_buf_pool[i],
1773 + MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
1776 + /* Put it out there so the device can send us stuff */
1777 + result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
1780 + dev_warn(&hso_dev->usb->dev,
1781 + "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
1788 +static int hso_stop_net_device(struct hso_device *hso_dev)
1791 + struct hso_net *hso_net = dev2net(hso_dev);
1796 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1797 + if (hso_net->mux_bulk_rx_urb_pool[i])
1798 + usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1801 + if (hso_net->mux_bulk_tx_urb)
1802 + usb_kill_urb(hso_net->mux_bulk_tx_urb);
1807 +static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
1809 + int i, result = 0;
1810 + struct hso_serial *serial = dev2ser(hso_dev);
1815 + /* If it is not the MUX port fill in and submit a bulk urb (already
1816 + * allocated in hso_serial_start) */
1817 + if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
1818 + for (i = 0; i < serial->num_rx_urbs; i++) {
1819 + usb_fill_bulk_urb(serial->rx_urb[i],
1820 + serial->parent->usb,
1821 + usb_rcvbulkpipe(serial->parent->usb,
1823 + bEndpointAddress &
1825 + serial->rx_data[i],
1826 + serial->rx_data_length,
1827 + hso_std_serial_read_bulk_callback,
1829 + result = usb_submit_urb(serial->rx_urb[i], flags);
1831 + dev_warn(&serial->parent->usb->dev,
1832 + "Failed to submit urb - res %d\n",
1838 + mutex_lock(&serial->shared_int->shared_int_lock);
1839 + if (!serial->shared_int->use_count) {
1841 + hso_mux_submit_intr_urb(serial->shared_int,
1842 + hso_dev->usb, flags);
1844 + serial->shared_int->use_count++;
1845 + mutex_unlock(&serial->shared_int->shared_int_lock);
1851 +static int hso_stop_serial_device(struct hso_device *hso_dev)
1854 + struct hso_serial *serial = dev2ser(hso_dev);
1859 + for (i = 0; i < serial->num_rx_urbs; i++) {
1860 + if (serial->rx_urb[i])
1861 + usb_kill_urb(serial->rx_urb[i]);
1864 + if (serial->tx_urb)
1865 + usb_kill_urb(serial->tx_urb);
1867 + if (serial->shared_int) {
1868 + mutex_lock(&serial->shared_int->shared_int_lock);
1869 + if (serial->shared_int->use_count &&
1870 + (--serial->shared_int->use_count == 0)) {
1873 + urb = serial->shared_int->shared_intr_urb;
1875 + usb_kill_urb(urb);
1877 + mutex_unlock(&serial->shared_int->shared_int_lock);
1883 +static void hso_serial_common_free(struct hso_serial *serial)
1887 + if (serial->parent->dev)
1888 + device_remove_file(serial->parent->dev, &dev_attr_hsotype);
1890 + tty_unregister_device(tty_drv, serial->minor);
1892 + for (i = 0; i < serial->num_rx_urbs; i++) {
1893 + /* unlink and free RX URB */
1894 + usb_free_urb(serial->rx_urb[i]);
1895 + /* free the RX buffer */
1896 + kfree(serial->rx_data[i]);
1899 + /* unlink and free TX URB */
1900 + usb_free_urb(serial->tx_urb);
1901 + kfree(serial->tx_data);
1904 +static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
1905 + int rx_size, int tx_size)
1907 + struct device *dev;
1911 + minor = get_free_serial_index();
1915 + /* register our minor number */
1916 + serial->parent->dev = tty_register_device(tty_drv, minor,
1917 + &serial->parent->interface->dev);
1918 + dev = serial->parent->dev;
1919 + dev->driver_data = serial->parent;
1920 + i = device_create_file(dev, &dev_attr_hsotype);
1922 + /* fill in specific data for later use */
1923 + serial->minor = minor;
1924 + serial->magic = HSO_SERIAL_MAGIC;
1925 + spin_lock_init(&serial->serial_lock);
1926 + serial->num_rx_urbs = num_urbs;
1928 + /* RX, allocate urb and initialize */
1930 + /* prepare our RX buffer */
1931 + serial->rx_data_length = rx_size;
1932 + for (i = 0; i < serial->num_rx_urbs; i++) {
1933 + serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1934 + if (!serial->rx_urb[i]) {
1935 + dev_err(dev, "Could not allocate urb?\n");
1938 + serial->rx_urb[i]->transfer_buffer = NULL;
1939 + serial->rx_urb[i]->transfer_buffer_length = 0;
1940 + serial->rx_data[i] = kzalloc(serial->rx_data_length,
1942 + if (!serial->rx_data[i]) {
1943 + dev_err(dev, "%s - Out of memory\n", __func__);
1948 + /* TX, allocate urb and initialize */
1949 + serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1950 + if (!serial->tx_urb) {
1951 + dev_err(dev, "Could not allocate urb?\n");
1954 + serial->tx_urb->transfer_buffer = NULL;
1955 + serial->tx_urb->transfer_buffer_length = 0;
1956 + /* prepare our TX buffer */
1957 + serial->tx_data_count = 0;
1958 + serial->tx_buffer_count = 0;
1959 + serial->tx_data_length = tx_size;
1960 + serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
1961 + if (!serial->tx_data) {
1962 + dev_err(dev, "%s - Out of memory", __func__);
1965 + serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
1966 + if (!serial->tx_buffer) {
1967 + dev_err(dev, "%s - Out of memory", __func__);
1973 + hso_serial_common_free(serial);
1977 +/* Frees a general hso device */
1978 +static void hso_free_device(struct hso_device *hso_dev)
1983 +/* Creates a general hso device */
1984 +static struct hso_device *hso_create_device(struct usb_interface *intf,
1987 + struct hso_device *hso_dev;
1989 + hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
1993 + hso_dev->port_spec = port_spec;
1994 + hso_dev->usb = interface_to_usbdev(intf);
1995 + hso_dev->interface = intf;
1996 + kref_init(&hso_dev->ref);
1997 + mutex_init(&hso_dev->mutex);
1999 + INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2000 + INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2005 +/* Removes a network device in the network device table */
2006 +static int remove_net_device(struct hso_device *hso_dev)
2010 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2011 + if (network_table[i] == hso_dev) {
2012 + network_table[i] = NULL;
2016 + if (i == HSO_MAX_NET_DEVICES)
2021 +/* Frees our network device */
2022 +static void hso_free_net_device(struct hso_device *hso_dev)
2025 + struct hso_net *hso_net = dev2net(hso_dev);
2030 + /* start freeing */
2031 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2032 + usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2033 + kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2035 + usb_free_urb(hso_net->mux_bulk_tx_urb);
2036 + kfree(hso_net->mux_bulk_tx_buf);
2038 + remove_net_device(hso_net->parent);
2040 + if (hso_net->net) {
2041 + unregister_netdev(hso_net->net);
2042 + free_netdev(hso_net->net);
2045 + hso_free_device(hso_dev);
2048 +/* initialize the network interface */
2049 +static void hso_net_init(struct net_device *net)
2051 + struct hso_net *hso_net = netdev_priv(net);
2053 + D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2055 + /* fill in the other fields */
2056 + net->open = hso_net_open;
2057 + net->stop = hso_net_close;
2058 + net->hard_start_xmit = hso_net_start_xmit;
2059 + net->tx_timeout = hso_net_tx_timeout;
2060 + net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2061 + net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2062 + net->type = ARPHRD_NONE;
2063 + net->mtu = DEFAULT_MTU - 14;
2064 + net->tx_queue_len = 10;
2065 + SET_ETHTOOL_OPS(net, &ops);
2067 + /* and initialize the semaphore */
2068 + spin_lock_init(&hso_net->net_lock);
2071 +/* Adds a network device in the network device table */
2072 +static int add_net_device(struct hso_device *hso_dev)
2076 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2077 + if (network_table[i] == NULL) {
2078 + network_table[i] = hso_dev;
2082 + if (i == HSO_MAX_NET_DEVICES)
2087 +static int hso_radio_toggle(void *data, enum rfkill_state state)
2089 + struct hso_device *hso_dev = data;
2090 + int enabled = (state == RFKILL_STATE_ON);
2093 + mutex_lock(&hso_dev->mutex);
2094 + if (hso_dev->usb_gone)
2097 + rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2098 + enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2099 + USB_CTRL_SET_TIMEOUT);
2100 + mutex_unlock(&hso_dev->mutex);
2104 +/* Creates and sets up everything for rfkill */
2105 +static void hso_create_rfkill(struct hso_device *hso_dev,
2106 + struct usb_interface *interface)
2108 + struct hso_net *hso_net = dev2net(hso_dev);
2109 + struct device *dev = hso_dev->dev;
2112 + hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2113 + RFKILL_TYPE_WLAN);
2114 + if (!hso_net->rfkill) {
2115 + dev_err(dev, "%s - Out of memory", __func__);
2118 + rfkn = kzalloc(20, GFP_KERNEL);
2120 + rfkill_free(hso_net->rfkill);
2121 + dev_err(dev, "%s - Out of memory", __func__);
2124 + snprintf(rfkn, 20, "hso-%d",
2125 + interface->altsetting->desc.bInterfaceNumber);
2126 + hso_net->rfkill->name = rfkn;
2127 + hso_net->rfkill->state = RFKILL_STATE_ON;
2128 + hso_net->rfkill->data = hso_dev;
2129 + hso_net->rfkill->toggle_radio = hso_radio_toggle;
2130 + if (rfkill_register(hso_net->rfkill) < 0) {
2132 + hso_net->rfkill->name = NULL;
2133 + rfkill_free(hso_net->rfkill);
2134 + dev_err(dev, "%s - Failed to register rfkill", __func__);
2139 +/* Creates our network device */
2140 +static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2143 + struct net_device *net;
2144 + struct hso_net *hso_net;
2145 + struct hso_device *hso_dev;
2147 + hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2151 + /* allocate our network device, then we can put in our private data */
2152 + /* call hso_net_init to do the basic initialization */
2153 + net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2155 + dev_err(&interface->dev, "Unable to create ethernet device\n");
2159 + hso_net = netdev_priv(net);
2161 + hso_dev->port_data.dev_net = hso_net;
2162 + hso_net->net = net;
2163 + hso_net->parent = hso_dev;
2165 + hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2167 + if (!hso_net->in_endp) {
2168 + dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2171 + hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2173 + if (!hso_net->out_endp) {
2174 + dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2177 + SET_NETDEV_DEV(net, &interface->dev);
2179 + /* registering our net device */
2180 + result = register_netdev(net);
2182 + dev_err(&interface->dev, "Failed to register device\n");
2186 + /* start allocating */
2187 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2188 + hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2189 + if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2190 + dev_err(&interface->dev, "Could not allocate rx urb\n");
2193 + hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2195 + if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2196 + dev_err(&interface->dev, "Could not allocate rx buf\n");
2200 + hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2201 + if (!hso_net->mux_bulk_tx_urb) {
2202 + dev_err(&interface->dev, "Could not allocate tx urb\n");
2205 + hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2206 + if (!hso_net->mux_bulk_tx_buf) {
2207 + dev_err(&interface->dev, "Could not allocate tx buf\n");
2211 + add_net_device(hso_dev);
2213 + hso_log_port(hso_dev);
2215 + hso_create_rfkill(hso_dev, interface);
2219 + hso_free_net_device(hso_dev);
2223 +/* Frees an AT channel ( goes for both mux and non-mux ) */
2224 +static void hso_free_serial_device(struct hso_device *hso_dev)
2226 + struct hso_serial *serial = dev2ser(hso_dev);
2230 + set_serial_by_index(serial->minor, NULL);
2232 + hso_serial_common_free(serial);
2234 + if (serial->shared_int) {
2235 + mutex_lock(&serial->shared_int->shared_int_lock);
2236 + if (--serial->shared_int->ref_count == 0)
2237 + hso_free_shared_int(serial->shared_int);
2239 + mutex_unlock(&serial->shared_int->shared_int_lock);
2242 + hso_free_device(hso_dev);
2245 +/* Creates a bulk AT channel */
2246 +static struct hso_device *hso_create_bulk_serial_device(
2247 + struct usb_interface *interface, int port)
2249 + struct hso_device *hso_dev;
2250 + struct hso_serial *serial;
2253 + hso_dev = hso_create_device(interface, port);
2257 + serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2261 + serial->parent = hso_dev;
2262 + hso_dev->port_data.dev_serial = serial;
2264 + if (port & HSO_PORT_MODEM)
2269 + if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2270 + BULK_URB_TX_SIZE))
2273 + serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2275 + if (!serial->in_endp) {
2276 + dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2281 + (serial->out_endp =
2282 + hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2283 + dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2287 + serial->write_data = hso_std_serial_write_data;
2289 + /* and record this serial */
2290 + set_serial_by_index(serial->minor, serial);
2292 + /* setup the proc dirs and files if needed */
2293 + hso_log_port(hso_dev);
2295 + /* done, return it */
2298 + if (hso_dev && serial)
2299 + hso_serial_common_free(serial);
2301 + hso_free_device(hso_dev);
2305 +/* Creates a multiplexed AT channel */
2307 +struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2309 + struct hso_shared_int *mux)
2311 + struct hso_device *hso_dev;
2312 + struct hso_serial *serial;
2315 + port_spec = HSO_INTF_MUX;
2316 + port_spec &= ~HSO_PORT_MASK;
2318 + port_spec |= hso_mux_to_port(port);
2319 + if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2322 + hso_dev = hso_create_device(interface, port_spec);
2326 + serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2330 + hso_dev->port_data.dev_serial = serial;
2331 + serial->parent = hso_dev;
2333 + if (hso_serial_common_create
2334 + (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2337 + serial->tx_data_length--;
2338 + serial->write_data = hso_mux_serial_write_data;
2340 + serial->shared_int = mux;
2341 + mutex_lock(&serial->shared_int->shared_int_lock);
2342 + serial->shared_int->ref_count++;
2343 + mutex_unlock(&serial->shared_int->shared_int_lock);
2345 + /* and record this serial */
2346 + set_serial_by_index(serial->minor, serial);
2348 + /* setup the proc dirs and files if needed */
2349 + hso_log_port(hso_dev);
2351 + /* done, return it */
2356 + tty_unregister_device(tty_drv, serial->minor);
2360 + hso_free_device(hso_dev);
2365 +static void hso_free_shared_int(struct hso_shared_int *mux)
2367 + usb_free_urb(mux->shared_intr_urb);
2368 + kfree(mux->shared_intr_buf);
2369 + mutex_unlock(&mux->shared_int_lock);
2374 +struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2376 + struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2381 + mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2383 + if (!mux->intr_endp) {
2384 + dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2388 + mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2389 + if (!mux->shared_intr_urb) {
2390 + dev_err(&interface->dev, "Could not allocate intr urb?");
2393 + mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2395 + if (!mux->shared_intr_buf) {
2396 + dev_err(&interface->dev, "Could not allocate intr buf?");
2400 + mutex_init(&mux->shared_int_lock);
2405 + kfree(mux->shared_intr_buf);
2406 + usb_free_urb(mux->shared_intr_urb);
2411 +/* Gets the port spec for a certain interface */
2412 +static int hso_get_config_data(struct usb_interface *interface)
2414 + struct usb_device *usbdev = interface_to_usbdev(interface);
2415 + u8 config_data[17];
2416 + u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2419 + if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2420 + 0x86, 0xC0, 0, 0, config_data, 17,
2421 + USB_CTRL_SET_TIMEOUT) != 0x11) {
2425 + switch (config_data[if_num]) {
2430 + result = HSO_PORT_DIAG;
2433 + result = HSO_PORT_GPS;
2436 + result = HSO_PORT_GPS_CONTROL;
2439 + result = HSO_PORT_APP;
2442 + result = HSO_PORT_APP2;
2445 + result = HSO_PORT_CONTROL;
2448 + result = HSO_PORT_NETWORK;
2451 + result = HSO_PORT_MODEM;
2454 + result = HSO_PORT_MSD;
2457 + result = HSO_PORT_PCSC;
2460 + result = HSO_PORT_VOICE;
2467 + result |= HSO_INTF_BULK;
2469 + if (config_data[16] & 0x1)
2470 + result |= HSO_INFO_CRC_BUG;
2475 +/* called once for each interface upon device insertion */
2476 +static int hso_probe(struct usb_interface *interface,
2477 + const struct usb_device_id *id)
2479 + int mux, i, if_num, port_spec;
2480 + unsigned char port_mask;
2481 + struct hso_device *hso_dev = NULL;
2482 + struct hso_shared_int *shared_int;
2483 + struct hso_device *tmp_dev = NULL;
2485 + if_num = interface->altsetting->desc.bInterfaceNumber;
2487 + /* Get the interface/port specification from either driver_info or from
2488 + * the device itself */
2489 + if (id->driver_info)
2490 + port_spec = ((u32 *)(id->driver_info))[if_num];
2492 + port_spec = hso_get_config_data(interface);
2494 + if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2495 + dev_err(&interface->dev, "Not our interface\n");
2498 + /* Check if we need to switch to alt interfaces prior to port
2499 + * configuration */
2500 + if (interface->num_altsetting > 1)
2501 + usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2502 + interface->needs_remote_wakeup = 1;
2504 + /* Allocate new hso device(s) */
2505 + switch (port_spec & HSO_INTF_MASK) {
2506 + case HSO_INTF_MUX:
2507 + if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2508 + /* Create the network device */
2509 + if (!disable_net) {
2510 + hso_dev = hso_create_net_device(interface);
2513 + tmp_dev = hso_dev;
2517 + if (hso_get_mux_ports(interface, &port_mask))
2518 + /* TODO: de-allocate everything */
2521 + shared_int = hso_create_shared_int(interface);
2525 + for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2526 + if (port_mask & i) {
2527 + hso_dev = hso_create_mux_serial_device(
2528 + interface, i, shared_int);
2535 + hso_dev = tmp_dev;
2538 + case HSO_INTF_BULK:
2539 + /* It's a regular bulk interface */
2540 + if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2542 + hso_dev = hso_create_net_device(interface);
2545 + hso_create_bulk_serial_device(interface, port_spec);
2553 + usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2555 + /* save our data pointer in this device */
2556 + usb_set_intfdata(interface, hso_dev);
2561 + hso_free_interface(interface);
2565 +/* device removed, cleaning up */
2566 +static void hso_disconnect(struct usb_interface *interface)
2568 + hso_free_interface(interface);
2570 + /* remove reference of our private data */
2571 + usb_set_intfdata(interface, NULL);
2573 + usb_driver_release_interface(&hso_driver, interface);
2576 +static void async_get_intf(struct work_struct *data)
2578 + struct hso_device *hso_dev =
2579 + container_of(data, struct hso_device, async_get_intf);
2580 + usb_autopm_get_interface(hso_dev->interface);
2583 +static void async_put_intf(struct work_struct *data)
2585 + struct hso_device *hso_dev =
2586 + container_of(data, struct hso_device, async_put_intf);
2587 + usb_autopm_put_interface(hso_dev->interface);
2590 +static int hso_get_activity(struct hso_device *hso_dev)
2592 + if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2593 + if (!hso_dev->is_active) {
2594 + hso_dev->is_active = 1;
2595 + schedule_work(&hso_dev->async_get_intf);
2599 + if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2602 + usb_mark_last_busy(hso_dev->usb);
2607 +static int hso_put_activity(struct hso_device *hso_dev)
2609 + if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2610 + if (hso_dev->is_active) {
2611 + hso_dev->is_active = 0;
2612 + schedule_work(&hso_dev->async_put_intf);
2616 + hso_dev->is_active = 0;
2620 +/* called by kernel when we need to suspend device */
2621 +static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2625 + /* Stop all serial ports */
2626 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2627 + if (serial_table[i] && (serial_table[i]->interface == iface)) {
2628 + result = hso_stop_serial_device(serial_table[i]);
2634 + /* Stop all network ports */
2635 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2636 + if (network_table[i] &&
2637 + (network_table[i]->interface == iface)) {
2638 + result = hso_stop_net_device(network_table[i]);
2648 +/* called by kernel when we need to resume device */
2649 +static int hso_resume(struct usb_interface *iface)
2651 + int i, result = 0;
2652 + struct hso_net *hso_net;
2654 + /* Start all serial ports */
2655 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2656 + if (serial_table[i] && (serial_table[i]->interface == iface)) {
2657 + if (dev2ser(serial_table[i])->open_count) {
2659 + hso_start_serial_device(serial_table[i], GFP_NOIO);
2660 + hso_kick_transmit(dev2ser(serial_table[i]));
2667 + /* Start all network ports */
2668 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2669 + if (network_table[i] &&
2670 + (network_table[i]->interface == iface)) {
2671 + hso_net = dev2net(network_table[i]);
2672 + /* First transmit any lingering data, then restart the
2674 + if (hso_net->skb_tx_buf) {
2675 + dev_dbg(&iface->dev,
2676 + "Transmitting lingering data\n");
2677 + hso_net_start_xmit(hso_net->skb_tx_buf,
2680 + result = hso_start_net_device(network_table[i]);
2690 +static void hso_serial_ref_free(struct kref *ref)
2692 + struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2694 + hso_free_serial_device(hso_dev);
2697 +static void hso_free_interface(struct usb_interface *interface)
2699 + struct hso_serial *hso_dev;
2702 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2703 + if (serial_table[i]
2704 + && (serial_table[i]->interface == interface)) {
2705 + hso_dev = dev2ser(serial_table[i]);
2707 + tty_hangup(hso_dev->tty);
2708 + mutex_lock(&hso_dev->parent->mutex);
2709 + hso_dev->parent->usb_gone = 1;
2710 + mutex_unlock(&hso_dev->parent->mutex);
2711 + kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2715 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2716 + if (network_table[i]
2717 + && (network_table[i]->interface == interface)) {
2718 + struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2719 + /* hso_stop_net_device doesn't stop the net queue since
2720 + * traffic needs to start it again when suspended */
2721 + netif_stop_queue(dev2net(network_table[i])->net);
2722 + hso_stop_net_device(network_table[i]);
2723 + cancel_work_sync(&network_table[i]->async_put_intf);
2724 + cancel_work_sync(&network_table[i]->async_get_intf);
2726 + rfkill_unregister(rfk);
2727 + hso_free_net_device(network_table[i]);
2732 +/* Helper functions */
2734 +/* Get the endpoint ! */
2735 +static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2736 + int type, int dir)
2739 + struct usb_host_interface *iface = intf->cur_altsetting;
2740 + struct usb_endpoint_descriptor *endp;
2742 + for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2743 + endp = &iface->endpoint[i].desc;
2744 + if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2745 + ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
2752 +/* Get the byte that describes which ports are enabled */
2753 +static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2756 + struct usb_host_interface *iface = intf->cur_altsetting;
2758 + if (iface->extralen == 3) {
2759 + *ports = iface->extra[2];
2763 + for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2764 + if (iface->endpoint[i].extralen == 3) {
2765 + *ports = iface->endpoint[i].extra[2];
2773 +/* interrupt urb needs to be submitted, used for serial read of muxed port */
2774 +static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2775 + struct usb_device *usb, gfp_t gfp)
2779 + usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2780 + usb_rcvintpipe(usb,
2781 + shared_int->intr_endp->bEndpointAddress & 0x7F),
2782 + shared_int->shared_intr_buf,
2783 + shared_int->intr_endp->wMaxPacketSize,
2784 + intr_callback, shared_int,
2785 + shared_int->intr_endp->bInterval);
2787 + result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2789 + dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2795 +/* operations setup of the serial interface */
2796 +static struct tty_operations hso_serial_ops = {
2797 + .open = hso_serial_open,
2798 + .close = hso_serial_close,
2799 + .write = hso_serial_write,
2800 + .write_room = hso_serial_write_room,
2801 + .set_termios = hso_serial_set_termios,
2802 + .chars_in_buffer = hso_serial_chars_in_buffer,
2803 + .tiocmget = hso_serial_tiocmget,
2804 + .tiocmset = hso_serial_tiocmset,
2807 +static struct usb_driver hso_driver = {
2808 + .name = driver_name,
2809 + .probe = hso_probe,
2810 + .disconnect = hso_disconnect,
2811 + .id_table = hso_ids,
2812 + .suspend = hso_suspend,
2813 + .resume = hso_resume,
2814 + .supports_autosuspend = 1,
2817 +static int __init hso_init(void)
2822 + /* put it in the log */
2823 + printk(KERN_INFO "hso: %s\n", version);
2825 + /* Initialise the serial table semaphore and table */
2826 + spin_lock_init(&serial_table_lock);
2827 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2828 + serial_table[i] = NULL;
2830 + /* allocate our driver using the proper amount of supported minors */
2831 + tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2835 + /* fill in all needed values */
2836 + tty_drv->magic = TTY_DRIVER_MAGIC;
2837 + tty_drv->owner = THIS_MODULE;
2838 + tty_drv->driver_name = driver_name;
2839 + tty_drv->name = tty_filename;
2841 + /* if major number is provided as parameter, use that one */
2843 + tty_drv->major = tty_major;
2845 + tty_drv->minor_start = 0;
2846 + tty_drv->num = HSO_SERIAL_TTY_MINORS;
2847 + tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2848 + tty_drv->subtype = SERIAL_TYPE_NORMAL;
2849 + tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2850 + tty_drv->init_termios = tty_std_termios;
2851 + tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2852 + tty_drv->termios = hso_serial_termios;
2853 + tty_drv->termios_locked = hso_serial_termios_locked;
2854 + tty_set_operations(tty_drv, &hso_serial_ops);
2856 + /* register the tty driver */
2857 + result = tty_register_driver(tty_drv);
2859 + printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2860 + __func__, result);
2864 + /* register this module as an usb driver */
2865 + result = usb_register(&hso_driver);
2867 + printk(KERN_ERR "Could not register hso driver? error: %d\n",
2869 + /* cleanup serial interface */
2870 + tty_unregister_driver(tty_drv);
2878 +static void __exit hso_exit(void)
2880 + printk(KERN_INFO "hso: unloaded\n");
2882 + tty_unregister_driver(tty_drv);
2883 + /* deregister the usb driver */
2884 + usb_deregister(&hso_driver);
2887 +/* Module definitions */
2888 +module_init(hso_init);
2889 +module_exit(hso_exit);
2891 +MODULE_AUTHOR(MOD_AUTHOR);
2892 +MODULE_DESCRIPTION(MOD_DESCRIPTION);
2893 +MODULE_LICENSE(MOD_LICENSE);
2894 +MODULE_INFO(Version, DRIVER_VERSION);
2896 +/* change the debug level (eg: insmod hso.ko debug=0x04) */
2897 +MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2898 +module_param(debug, int, S_IRUGO | S_IWUSR);
2900 +/* set the major tty number (eg: insmod hso.ko tty_major=245) */
2901 +MODULE_PARM_DESC(tty_major, "Set the major tty number");
2902 +module_param(tty_major, int, S_IRUGO | S_IWUSR);
2904 +/* disable network interface (eg: insmod hso.ko disable_net=1) */
2905 +MODULE_PARM_DESC(disable_net, "Disable the network interface");
2906 +module_param(disable_net, int, S_IRUGO | S_IWUSR);