1 /******************************************************************************
3 * Driver for Option High Speed Mobile Devices.
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.
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.
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.
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,
26 *****************************************************************************/
28 /******************************************************************************
30 * Description of the device:
32 * Interface 0: Contains the IP network interface on the bulk end points.
33 * The multiplexed serial ports are using the interrupt and
35 * Interrupt contains a bitmap telling which multiplexed
36 * serialport needs servicing.
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
42 * Interface 2: Standard modem interface - circuit switched interface, should
45 *****************************************************************************/
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>
62 #include <linux/uaccess.h>
63 #include <linux/usb/cdc.h>
65 #include <asm/byteorder.h>
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"
73 #define HSO_MAX_NET_DEVICES 10
74 #define HSO__MAX_MTU 2048
75 #define DEFAULT_MTU 1500
76 #define DEFAULT_MRU 1500
78 #define CTRL_URB_RX_SIZE 1024
79 #define CTRL_URB_TX_SIZE 64
81 #define BULK_URB_RX_SIZE 4096
82 #define BULK_URB_TX_SIZE 8192
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
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
93 #define HSO_NET_TX_TIMEOUT (HZ*10)
95 /* Serial port defines and structs. */
96 #define HSO_SERIAL_FLAG_RX_SENT 0
98 #define HSO_SERIAL_MAGIC 0x48534f31
100 /* Number of ttys to handle */
101 #define HSO_SERIAL_TTY_MINORS 256
103 #define MAX_RX_URBS 2
105 static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
108 return tty->driver_data;
112 /*****************************************************************************/
113 /* Debugging functions */
114 /*****************************************************************************/
115 #define D__(lvl_, fmt, arg...) \
117 printk(lvl_ "[%d:%s]: " fmt "\n", \
118 __LINE__, __func__, ## arg); \
121 #define D_(lvl, args...) \
124 D__(KERN_INFO, args); \
127 #define D1(args...) D_(0x01, ##args)
128 #define D2(args...) D_(0x02, ##args)
129 #define D3(args...) D_(0x04, ##args)
130 #define D4(args...) D_(0x08, ##args)
131 #define D5(args...) D_(0x10, ##args)
133 /*****************************************************************************/
135 /*****************************************************************************/
136 enum pkt_parse_state {
142 /*****************************************************************************/
144 /*****************************************************************************/
146 struct hso_shared_int {
147 struct usb_endpoint_descriptor *intr_endp;
148 void *shared_intr_buf;
149 struct urb *shared_intr_urb;
150 struct usb_device *usb;
153 struct mutex shared_int_lock;
157 struct hso_device *parent;
158 struct net_device *net;
159 struct rfkill *rfkill;
161 struct usb_endpoint_descriptor *in_endp;
162 struct usb_endpoint_descriptor *out_endp;
164 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
165 struct urb *mux_bulk_tx_urb;
166 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
167 void *mux_bulk_tx_buf;
169 struct sk_buff *skb_rx_buf;
170 struct sk_buff *skb_tx_buf;
172 enum pkt_parse_state rx_parse_state;
175 unsigned short rx_buf_size;
176 unsigned short rx_buf_missing;
177 struct iphdr rx_ip_hdr;
183 struct hso_device *parent;
187 struct hso_shared_int *shared_int;
189 /* rx/tx urb could be either a bulk urb or a control urb depending
190 on which serial port it is used on. */
191 struct urb *rx_urb[MAX_RX_URBS];
193 u8 *rx_data[MAX_RX_URBS];
194 u16 rx_data_length; /* should contain allocated length */
199 u16 tx_data_length; /* should contain allocated length */
202 struct usb_ctrlrequest ctrl_req_tx;
203 struct usb_ctrlrequest ctrl_req_rx;
205 struct usb_endpoint_descriptor *in_endp;
206 struct usb_endpoint_descriptor *out_endp;
211 unsigned tx_urb_used:1;
213 /* from usb_serial_port */
214 struct tty_struct *tty;
216 spinlock_t serial_lock;
218 int (*write_data) (struct hso_serial *serial);
223 struct hso_serial *dev_serial;
224 struct hso_net *dev_net;
231 struct work_struct async_get_intf;
232 struct work_struct async_put_intf;
234 struct usb_device *usb;
235 struct usb_interface *interface;
242 /* Type of interface */
243 #define HSO_INTF_MASK 0xFF00
244 #define HSO_INTF_MUX 0x0100
245 #define HSO_INTF_BULK 0x0200
248 #define HSO_PORT_MASK 0xFF
249 #define HSO_PORT_NO_PORT 0x0
250 #define HSO_PORT_CONTROL 0x1
251 #define HSO_PORT_APP 0x2
252 #define HSO_PORT_GPS 0x3
253 #define HSO_PORT_PCSC 0x4
254 #define HSO_PORT_APP2 0x5
255 #define HSO_PORT_GPS_CONTROL 0x6
256 #define HSO_PORT_MSD 0x7
257 #define HSO_PORT_VOICE 0x8
258 #define HSO_PORT_DIAG2 0x9
259 #define HSO_PORT_DIAG 0x10
260 #define HSO_PORT_MODEM 0x11
261 #define HSO_PORT_NETWORK 0x12
263 /* Additional device info */
264 #define HSO_INFO_MASK 0xFF000000
265 #define HSO_INFO_CRC_BUG 0x01000000
267 /*****************************************************************************/
269 /*****************************************************************************/
270 /* Serial driver functions */
271 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
272 unsigned int set, unsigned int clear);
273 static void ctrl_callback(struct urb *urb);
274 static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
275 static void hso_kick_transmit(struct hso_serial *serial);
276 /* Helper functions */
277 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
278 struct usb_device *usb, gfp_t gfp);
279 static void log_usb_status(int status, const char *function);
280 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
282 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
283 static void hso_free_interface(struct usb_interface *intf);
284 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
285 static int hso_stop_serial_device(struct hso_device *hso_dev);
286 static int hso_start_net_device(struct hso_device *hso_dev);
287 static void hso_free_shared_int(struct hso_shared_int *shared_int);
288 static int hso_stop_net_device(struct hso_device *hso_dev);
289 static void hso_serial_ref_free(struct kref *ref);
290 static void async_get_intf(struct work_struct *data);
291 static void async_put_intf(struct work_struct *data);
292 static int hso_put_activity(struct hso_device *hso_dev);
293 static int hso_get_activity(struct hso_device *hso_dev);
295 /*****************************************************************************/
296 /* Helping functions */
297 /*****************************************************************************/
301 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
303 return hso_dev->port_data.dev_net;
306 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
308 return hso_dev->port_data.dev_serial;
311 /* Debugging functions */
313 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
316 static char name[255];
318 sprintf(name, "hso[%d:%s]", line_count, func_name);
319 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
322 #define DUMP(buf_, len_) \
323 dbg_dump(__LINE__, __func__, buf_, len_)
325 #define DUMP1(buf_, len_) \
331 #define DUMP(buf_, len_)
332 #define DUMP1(buf_, len_)
335 /* module parameters */
337 static int tty_major;
338 static int disable_net;
341 static const char driver_name[] = "hso";
342 static const char tty_filename[] = "ttyHS";
343 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
344 /* the usb driver itself (registered in hso_init) */
345 static struct usb_driver hso_driver;
346 /* serial structures */
347 static struct tty_driver *tty_drv;
348 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
349 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
350 static spinlock_t serial_table_lock;
351 static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
352 static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
354 static const s32 default_port_spec[] = {
355 HSO_INTF_MUX | HSO_PORT_NETWORK,
356 HSO_INTF_BULK | HSO_PORT_DIAG,
357 HSO_INTF_BULK | HSO_PORT_MODEM,
361 static const s32 icon321_port_spec[] = {
362 HSO_INTF_MUX | HSO_PORT_NETWORK,
363 HSO_INTF_BULK | HSO_PORT_DIAG2,
364 HSO_INTF_BULK | HSO_PORT_MODEM,
365 HSO_INTF_BULK | HSO_PORT_DIAG,
369 #define default_port_device(vendor, product) \
370 USB_DEVICE(vendor, product), \
371 .driver_info = (kernel_ulong_t)default_port_spec
373 #define icon321_port_device(vendor, product) \
374 USB_DEVICE(vendor, product), \
375 .driver_info = (kernel_ulong_t)icon321_port_spec
377 /* list of devices we support */
378 static const struct usb_device_id hso_ids[] = {
379 {default_port_device(0x0af0, 0x6711)},
380 {default_port_device(0x0af0, 0x6731)},
381 {default_port_device(0x0af0, 0x6751)},
382 {default_port_device(0x0af0, 0x6771)},
383 {default_port_device(0x0af0, 0x6791)},
384 {default_port_device(0x0af0, 0x6811)},
385 {default_port_device(0x0af0, 0x6911)},
386 {default_port_device(0x0af0, 0x6951)},
387 {default_port_device(0x0af0, 0x6971)},
388 {default_port_device(0x0af0, 0x7011)},
389 {default_port_device(0x0af0, 0x7031)},
390 {default_port_device(0x0af0, 0x7051)},
391 {default_port_device(0x0af0, 0x7071)},
392 {default_port_device(0x0af0, 0x7111)},
393 {default_port_device(0x0af0, 0x7211)},
394 {default_port_device(0x0af0, 0x7251)},
395 {default_port_device(0x0af0, 0x7271)},
396 {default_port_device(0x0af0, 0x7311)},
397 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
398 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
399 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
400 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
401 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
402 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
403 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
404 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
405 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
408 MODULE_DEVICE_TABLE(usb, hso_ids);
410 /* Sysfs attribute */
411 static ssize_t hso_sysfs_show_porttype(struct device *dev,
412 struct device_attribute *attr,
415 struct hso_device *hso_dev = dev->driver_data;
421 switch (hso_dev->port_spec & HSO_PORT_MASK) {
422 case HSO_PORT_CONTROL:
423 port_name = "Control";
426 port_name = "Application";
429 port_name = "Application2";
434 case HSO_PORT_GPS_CONTROL:
435 port_name = "GPS Control";
441 port_name = "Diagnostic";
444 port_name = "Diagnostic2";
449 case HSO_PORT_NETWORK:
450 port_name = "Network";
453 port_name = "Unknown";
457 return sprintf(buf, "%s\n", port_name);
459 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
461 /* converts mux value to a port spec value */
462 static u32 hso_mux_to_port(int mux)
468 result = HSO_PORT_CONTROL;
471 result = HSO_PORT_APP;
474 result = HSO_PORT_PCSC;
477 result = HSO_PORT_GPS;
480 result = HSO_PORT_APP2;
483 result = HSO_PORT_NO_PORT;
488 /* converts port spec value to a mux value */
489 static u32 hso_port_to_mux(int port)
493 switch (port & HSO_PORT_MASK) {
494 case HSO_PORT_CONTROL:
515 static struct hso_serial *get_serial_by_shared_int_and_type(
516 struct hso_shared_int *shared_int,
521 port = hso_mux_to_port(mux);
523 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
525 && (dev2ser(serial_table[i])->shared_int == shared_int)
526 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
527 return dev2ser(serial_table[i]);
534 static struct hso_serial *get_serial_by_index(unsigned index)
536 struct hso_serial *serial = NULL;
539 spin_lock_irqsave(&serial_table_lock, flags);
540 if (serial_table[index])
541 serial = dev2ser(serial_table[index]);
542 spin_unlock_irqrestore(&serial_table_lock, flags);
547 static int get_free_serial_index(void)
552 spin_lock_irqsave(&serial_table_lock, flags);
553 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
554 if (serial_table[index] == NULL) {
555 spin_unlock_irqrestore(&serial_table_lock, flags);
559 spin_unlock_irqrestore(&serial_table_lock, flags);
561 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
565 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
569 spin_lock_irqsave(&serial_table_lock, flags);
571 serial_table[index] = serial->parent;
573 serial_table[index] = NULL;
574 spin_unlock_irqrestore(&serial_table_lock, flags);
577 /* log a meaningful explanation of an USB status */
578 static void log_usb_status(int status, const char *function)
584 explanation = "no device";
587 explanation = "endpoint not enabled";
590 explanation = "endpoint stalled";
593 explanation = "not enough bandwidth";
596 explanation = "device disabled";
599 explanation = "device suspended";
605 explanation = "internal error";
608 explanation = "unknown status";
611 D1("%s: received USB status - %s (%d)", function, explanation, status);
614 /* Network interface functions */
616 /* called when net interface is brought up by ifconfig */
617 static int hso_net_open(struct net_device *net)
619 struct hso_net *odev = netdev_priv(net);
620 unsigned long flags = 0;
623 dev_err(&net->dev, "No net device !\n");
627 odev->skb_tx_buf = NULL;
629 /* setup environment */
630 spin_lock_irqsave(&odev->net_lock, flags);
631 odev->rx_parse_state = WAIT_IP;
632 odev->rx_buf_size = 0;
633 odev->rx_buf_missing = sizeof(struct iphdr);
634 spin_unlock_irqrestore(&odev->net_lock, flags);
636 hso_start_net_device(odev->parent);
638 /* We are up and running. */
639 set_bit(HSO_NET_RUNNING, &odev->flags);
641 /* Tell the kernel we are ready to start receiving from it */
642 netif_start_queue(net);
647 /* called when interface is brought down by ifconfig */
648 static int hso_net_close(struct net_device *net)
650 struct hso_net *odev = netdev_priv(net);
652 /* we don't need the queue anymore */
653 netif_stop_queue(net);
654 /* no longer running */
655 clear_bit(HSO_NET_RUNNING, &odev->flags);
657 hso_stop_net_device(odev->parent);
663 /* USB tells is xmit done, we should start the netqueue again */
664 static void write_bulk_callback(struct urb *urb)
666 struct hso_net *odev = urb->context;
667 int status = urb->status;
670 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
671 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
675 /* Do we still have a valid kernel network device? */
676 if (!netif_device_present(odev->net)) {
677 dev_err(&urb->dev->dev, "%s: net device not present\n",
682 /* log status, but don't act on it, we don't need to resubmit anything
685 log_usb_status(status, __func__);
687 hso_put_activity(odev->parent);
689 /* Tell the network interface we are ready for another frame */
690 netif_wake_queue(odev->net);
693 /* called by kernel when we need to transmit a packet */
694 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
696 struct hso_net *odev = netdev_priv(net);
699 /* Tell the kernel, "No more frames 'til we are done with this one." */
700 netif_stop_queue(net);
701 if (hso_get_activity(odev->parent) == -EAGAIN) {
702 odev->skb_tx_buf = skb;
707 DUMP1(skb->data, skb->len);
708 /* Copy it from kernel memory to OUR memory */
709 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
710 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
712 /* Fill in the URB for shipping it out. */
713 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
715 usb_sndbulkpipe(odev->parent->usb,
717 bEndpointAddress & 0x7F),
718 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
721 /* Deal with the Zero Length packet problem, I hope */
722 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
724 /* Send the URB on its merry way. */
725 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
727 dev_warn(&odev->parent->interface->dev,
728 "failed mux_bulk_tx_urb %d", result);
729 net->stats.tx_errors++;
730 netif_start_queue(net);
732 net->stats.tx_packets++;
733 net->stats.tx_bytes += skb->len;
734 /* And tell the kernel when the last transmit started. */
735 net->trans_start = jiffies;
742 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
744 struct hso_net *odev = netdev_priv(net);
746 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
747 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
748 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
751 static struct ethtool_ops ops = {
752 .get_drvinfo = hso_get_drvinfo,
753 .get_link = ethtool_op_get_link
756 /* called when a packet did not ack after watchdogtimeout */
757 static void hso_net_tx_timeout(struct net_device *net)
759 struct hso_net *odev = netdev_priv(net);
764 /* Tell syslog we are hosed. */
765 dev_warn(&net->dev, "Tx timed out.\n");
767 /* Tear the waiting frame off the list */
768 if (odev->mux_bulk_tx_urb
769 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
770 usb_unlink_urb(odev->mux_bulk_tx_urb);
772 /* Update statistics */
773 net->stats.tx_errors++;
776 /* make a real packet from the received USB buffer */
777 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
778 unsigned int count, unsigned char is_eop)
780 unsigned short temp_bytes;
781 unsigned short buffer_offset = 0;
782 unsigned short frame_len;
783 unsigned char *tmp_rx_buf;
786 D1("Rx %d bytes", count);
787 DUMP(ip_pkt, min(128, (int)count));
790 switch (odev->rx_parse_state) {
792 /* waiting for IP header. */
793 /* wanted bytes - size of ip header */
796 odev->rx_buf_missing) ? count : odev->
799 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
800 odev->rx_buf_size, ip_pkt + buffer_offset,
803 odev->rx_buf_size += temp_bytes;
804 buffer_offset += temp_bytes;
805 odev->rx_buf_missing -= temp_bytes;
808 if (!odev->rx_buf_missing) {
809 /* header is complete allocate an sk_buffer and
810 * continue to WAIT_DATA */
811 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
813 if ((frame_len > DEFAULT_MRU) ||
814 (frame_len < sizeof(struct iphdr))) {
815 dev_err(&odev->net->dev,
816 "Invalid frame (%d) length\n",
818 odev->rx_parse_state = WAIT_SYNC;
821 /* Allocate an sk_buff */
822 odev->skb_rx_buf = dev_alloc_skb(frame_len);
823 if (!odev->skb_rx_buf) {
824 /* We got no receive buffer. */
825 D1("could not allocate memory");
826 odev->rx_parse_state = WAIT_SYNC;
829 /* Here's where it came from */
830 odev->skb_rx_buf->dev = odev->net;
832 /* Copy what we got so far. make room for iphdr
835 skb_put(odev->skb_rx_buf,
836 sizeof(struct iphdr));
837 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
838 sizeof(struct iphdr));
841 odev->rx_buf_size = sizeof(struct iphdr);
843 /* Filip actually use .tot_len */
844 odev->rx_buf_missing =
845 frame_len - sizeof(struct iphdr);
846 odev->rx_parse_state = WAIT_DATA;
851 temp_bytes = (count < odev->rx_buf_missing)
852 ? count : odev->rx_buf_missing;
854 /* Copy the rest of the bytes that are left in the
855 * buffer into the waiting sk_buf. */
856 /* Make room for temp_bytes after tail. */
857 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
858 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
860 odev->rx_buf_missing -= temp_bytes;
862 buffer_offset += temp_bytes;
863 odev->rx_buf_size += temp_bytes;
864 if (!odev->rx_buf_missing) {
865 /* Packet is complete. Inject into stack. */
866 /* We have IP packet here */
867 odev->skb_rx_buf->protocol =
868 __constant_htons(ETH_P_IP);
870 odev->skb_rx_buf->ip_summed =
871 CHECKSUM_UNNECESSARY;
873 skb_reset_mac_header(odev->skb_rx_buf);
875 /* Ship it off to the kernel */
876 netif_rx(odev->skb_rx_buf);
877 /* No longer our buffer. */
878 odev->skb_rx_buf = NULL;
880 /* update out statistics */
881 odev->net->stats.rx_packets++;
883 odev->net->stats.rx_bytes += odev->rx_buf_size;
885 odev->rx_buf_size = 0;
886 odev->rx_buf_missing = sizeof(struct iphdr);
887 odev->rx_parse_state = WAIT_IP;
902 /* Recovery mechanism for WAIT_SYNC state. */
904 if (odev->rx_parse_state == WAIT_SYNC) {
905 odev->rx_parse_state = WAIT_IP;
906 odev->rx_buf_size = 0;
907 odev->rx_buf_missing = sizeof(struct iphdr);
912 /* Moving data from usb to kernel (in interrupt state) */
913 static void read_bulk_callback(struct urb *urb)
915 struct hso_net *odev = urb->context;
916 struct net_device *net;
918 int status = urb->status;
920 /* is al ok? (Filip: Who's Al ?) */
922 log_usb_status(status, __func__);
927 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
928 D1("BULK IN callback but driver is not active!");
931 usb_mark_last_busy(urb->dev);
935 if (!netif_device_present(net)) {
936 /* Somebody killed our network interface... */
940 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
942 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
943 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
944 if (((rest == 5) || (rest == 6))
945 && !memcmp(((u8 *) urb->transfer_buffer) +
946 urb->actual_length - 4, crc_check, 4)) {
947 urb->actual_length -= 4;
951 /* do we even have a packet? */
952 if (urb->actual_length) {
953 /* Handle the IP stream, add header and push it onto network
954 * stack if the packet is complete. */
955 spin_lock(&odev->net_lock);
956 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
957 (urb->transfer_buffer_length >
958 urb->actual_length) ? 1 : 0);
959 spin_unlock(&odev->net_lock);
962 /* We are done with this URB, resubmit it. Prep the USB to wait for
963 * another frame. Reuse same as received. */
964 usb_fill_bulk_urb(urb,
966 usb_rcvbulkpipe(odev->parent->usb,
968 bEndpointAddress & 0x7F),
969 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
970 read_bulk_callback, odev);
972 /* Give this to the USB subsystem so it can tell us when more data
974 result = usb_submit_urb(urb, GFP_ATOMIC);
976 dev_warn(&odev->parent->interface->dev,
977 "%s failed submit mux_bulk_rx_urb %d", __func__,
981 /* Serial driver functions */
983 static void _hso_serial_set_termios(struct tty_struct *tty,
984 struct ktermios *old)
986 struct hso_serial *serial = get_serial_by_tty(tty);
987 struct ktermios *termios;
989 if ((!tty) || (!tty->termios) || (!serial)) {
990 printk(KERN_ERR "%s: no tty structures", __func__);
994 D4("port %d", serial->minor);
997 * The default requirements for this device are:
999 termios = tty->termios;
1001 ~(IGNBRK /* disable ignore break */
1002 | BRKINT /* disable break causes interrupt */
1003 | PARMRK /* disable mark parity errors */
1004 | ISTRIP /* disable clear high bit of input characters */
1005 | INLCR /* disable translate NL to CR */
1006 | IGNCR /* disable ignore CR */
1007 | ICRNL /* disable translate CR to NL */
1008 | IXON); /* disable enable XON/XOFF flow control */
1010 /* disable postprocess output characters */
1011 termios->c_oflag &= ~OPOST;
1014 ~(ECHO /* disable echo input characters */
1015 | ECHONL /* disable echo new line */
1016 | ICANON /* disable erase, kill, werase, and rprnt
1017 special characters */
1018 | ISIG /* disable interrupt, quit, and suspend special
1020 | IEXTEN); /* disable non-POSIX special characters */
1023 ~(CSIZE /* no size */
1024 | PARENB /* disable parity bit */
1025 | CBAUD /* clear current baud rate */
1026 | CBAUDEX); /* clear current buad rate */
1028 termios->c_cflag |= CS8; /* character size 8 bits */
1030 /* baud rate 115200 */
1031 tty_encode_baud_rate(serial->tty, 115200, 115200);
1034 * Force low_latency on; otherwise the pushes are scheduled;
1035 * this is bad as it opens up the possibility of dropping bytes
1036 * on the floor. We don't want to drop bytes on the floor. :)
1038 serial->tty->low_latency = 1;
1042 /* open the requested serial port */
1043 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1045 struct hso_serial *serial = get_serial_by_index(tty->index);
1049 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1050 tty->driver_data = NULL;
1051 D1("Failed to open port");
1055 mutex_lock(&serial->parent->mutex);
1056 result = usb_autopm_get_interface(serial->parent->interface);
1060 D1("Opening %d", serial->minor);
1061 kref_get(&serial->parent->ref);
1064 tty->driver_data = serial;
1067 /* check for port allready opened, if not set the termios */
1068 serial->open_count++;
1069 if (serial->open_count == 1) {
1070 tty->low_latency = 1;
1072 /* Force default termio settings */
1073 _hso_serial_set_termios(tty, NULL);
1074 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1076 hso_stop_serial_device(serial->parent);
1077 serial->open_count--;
1078 kref_put(&serial->parent->ref, hso_serial_ref_free);
1081 D1("Port was already open");
1084 usb_autopm_put_interface(serial->parent->interface);
1088 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1090 mutex_unlock(&serial->parent->mutex);
1094 /* close the requested serial port */
1095 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1097 struct hso_serial *serial = tty->driver_data;
1100 D1("Closing serial port");
1102 mutex_lock(&serial->parent->mutex);
1103 usb_gone = serial->parent->usb_gone;
1106 usb_autopm_get_interface(serial->parent->interface);
1108 /* reset the rts and dtr */
1109 /* do the actual close */
1110 serial->open_count--;
1111 kref_put(&serial->parent->ref, hso_serial_ref_free);
1112 if (serial->open_count <= 0) {
1113 serial->open_count = 0;
1115 serial->tty->driver_data = NULL;
1119 hso_stop_serial_device(serial->parent);
1122 usb_autopm_put_interface(serial->parent->interface);
1123 mutex_unlock(&serial->parent->mutex);
1126 /* close the requested serial port */
1127 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1130 struct hso_serial *serial = get_serial_by_tty(tty);
1131 int space, tx_bytes;
1132 unsigned long flags;
1135 if (serial == NULL) {
1136 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1140 spin_lock_irqsave(&serial->serial_lock, flags);
1142 space = serial->tx_data_length - serial->tx_buffer_count;
1143 tx_bytes = (count < space) ? count : space;
1148 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1149 serial->tx_buffer_count += tx_bytes;
1152 spin_unlock_irqrestore(&serial->serial_lock, flags);
1154 hso_kick_transmit(serial);
1159 /* how much room is there for writing */
1160 static int hso_serial_write_room(struct tty_struct *tty)
1162 struct hso_serial *serial = get_serial_by_tty(tty);
1164 unsigned long flags;
1166 spin_lock_irqsave(&serial->serial_lock, flags);
1167 room = serial->tx_data_length - serial->tx_buffer_count;
1168 spin_unlock_irqrestore(&serial->serial_lock, flags);
1170 /* return free room */
1174 /* setup the term */
1175 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1177 struct hso_serial *serial = get_serial_by_tty(tty);
1178 unsigned long flags;
1181 D5("Termios called with: cflags new[%d] - old[%d]",
1182 tty->termios->c_cflag, old->c_cflag);
1184 /* the actual setup */
1185 spin_lock_irqsave(&serial->serial_lock, flags);
1186 if (serial->open_count)
1187 _hso_serial_set_termios(tty, old);
1190 spin_unlock_irqrestore(&serial->serial_lock, flags);
1196 /* how many characters in the buffer */
1197 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1199 struct hso_serial *serial = get_serial_by_tty(tty);
1201 unsigned long flags;
1207 spin_lock_irqsave(&serial->serial_lock, flags);
1208 chars = serial->tx_buffer_count;
1209 spin_unlock_irqrestore(&serial->serial_lock, flags);
1214 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1217 struct hso_serial *serial = get_serial_by_tty(tty);
1218 unsigned long flags;
1222 D1("no tty structures");
1226 spin_lock_irqsave(&serial->serial_lock, flags);
1227 value = ((serial->rts_state) ? TIOCM_RTS : 0) |
1228 ((serial->dtr_state) ? TIOCM_DTR : 0);
1229 spin_unlock_irqrestore(&serial->serial_lock, flags);
1234 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1235 unsigned int set, unsigned int clear)
1238 unsigned long flags;
1240 struct hso_serial *serial = get_serial_by_tty(tty);
1244 D1("no tty structures");
1247 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1249 spin_lock_irqsave(&serial->serial_lock, flags);
1250 if (set & TIOCM_RTS)
1251 serial->rts_state = 1;
1252 if (set & TIOCM_DTR)
1253 serial->dtr_state = 1;
1255 if (clear & TIOCM_RTS)
1256 serial->rts_state = 0;
1257 if (clear & TIOCM_DTR)
1258 serial->dtr_state = 0;
1260 if (serial->dtr_state)
1262 if (serial->rts_state)
1265 spin_unlock_irqrestore(&serial->serial_lock, flags);
1267 return usb_control_msg(serial->parent->usb,
1268 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1269 0x21, val, if_num, NULL, 0,
1270 USB_CTRL_SET_TIMEOUT);
1273 /* starts a transmit */
1274 static void hso_kick_transmit(struct hso_serial *serial)
1277 unsigned long flags;
1280 spin_lock_irqsave(&serial->serial_lock, flags);
1281 if (!serial->tx_buffer_count)
1284 if (serial->tx_urb_used)
1287 /* Wakeup USB interface if necessary */
1288 if (hso_get_activity(serial->parent) == -EAGAIN)
1291 /* Switch pointers around to avoid memcpy */
1292 temp = serial->tx_buffer;
1293 serial->tx_buffer = serial->tx_data;
1294 serial->tx_data = temp;
1295 serial->tx_data_count = serial->tx_buffer_count;
1296 serial->tx_buffer_count = 0;
1298 /* If temp is set, it means we switched buffers */
1299 if (temp && serial->write_data) {
1300 res = serial->write_data(serial);
1302 serial->tx_urb_used = 1;
1305 spin_unlock_irqrestore(&serial->serial_lock, flags);
1308 /* make a request (for reading and writing data to muxed serial port) */
1309 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1310 struct urb *ctrl_urb,
1311 struct usb_ctrlrequest *ctrl_req,
1312 u8 *ctrl_urb_data, u32 size)
1318 if (!serial || !ctrl_urb || !ctrl_req) {
1319 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1324 ctrl_req->wValue = 0;
1325 ctrl_req->wIndex = hso_port_to_mux(port);
1326 ctrl_req->wLength = size;
1328 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1329 /* Reading command */
1330 ctrl_req->bRequestType = USB_DIR_IN |
1331 USB_TYPE_OPTION_VENDOR |
1332 USB_RECIP_INTERFACE;
1333 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1334 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1336 /* Writing command */
1337 ctrl_req->bRequestType = USB_DIR_OUT |
1338 USB_TYPE_OPTION_VENDOR |
1339 USB_RECIP_INTERFACE;
1340 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1341 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1344 D2("%s command (%02x) len: %d, port: %d",
1345 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1346 ctrl_req->bRequestType, ctrl_req->wLength, port);
1349 ctrl_urb->transfer_flags = 0;
1350 usb_fill_control_urb(ctrl_urb,
1351 serial->parent->usb,
1354 ctrl_urb_data, size, ctrl_callback, serial);
1355 /* Send it on merry way */
1356 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1358 dev_err(&ctrl_urb->dev->dev,
1359 "%s failed submit ctrl_urb %d type %d", __func__,
1368 /* called by intr_callback when read occurs */
1369 static int hso_mux_serial_read(struct hso_serial *serial)
1375 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1376 /* make the request */
1378 if (serial->num_rx_urbs != 1) {
1379 dev_err(&serial->parent->interface->dev,
1380 "ERROR: mux'd reads with multiple buffers "
1384 return mux_device_request(serial,
1385 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1386 serial->parent->port_spec & HSO_PORT_MASK,
1388 &serial->ctrl_req_rx,
1389 serial->rx_data[0], serial->rx_data_length);
1392 /* used for muxed serial port callback (muxed serial read) */
1393 static void intr_callback(struct urb *urb)
1395 struct hso_shared_int *shared_int = urb->context;
1396 struct hso_serial *serial;
1397 unsigned char *port_req;
1398 int status = urb->status;
1401 usb_mark_last_busy(urb->dev);
1409 log_usb_status(status, __func__);
1412 D4("\n--- Got intr callback 0x%02X ---", status);
1415 port_req = urb->transfer_buffer;
1416 D4(" port_req = 0x%.2X\n", *port_req);
1417 /* loop over all muxed ports to find the one sending this */
1418 for (i = 0; i < 8; i++) {
1419 /* max 8 channels on MUX */
1420 if (*port_req & (1 << i)) {
1421 serial = get_serial_by_shared_int_and_type(shared_int,
1423 if (serial != NULL) {
1424 D1("Pending read interrupt on port %d\n", i);
1425 if (!test_and_set_bit(HSO_SERIAL_FLAG_RX_SENT,
1427 /* Setup and send a ctrl req read on
1429 hso_mux_serial_read(serial);
1431 D1("Already pending a read on "
1437 /* Resubmit interrupt urb */
1438 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1441 /* called for writing to muxed serial port */
1442 static int hso_mux_serial_write_data(struct hso_serial *serial)
1447 return mux_device_request(serial,
1448 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1449 serial->parent->port_spec & HSO_PORT_MASK,
1451 &serial->ctrl_req_tx,
1452 serial->tx_data, serial->tx_data_count);
1455 /* write callback for Diag and CS port */
1456 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1458 struct hso_serial *serial = urb->context;
1459 int status = urb->status;
1463 D1("serial == NULL");
1467 spin_lock(&serial->serial_lock);
1468 serial->tx_urb_used = 0;
1469 spin_unlock(&serial->serial_lock);
1471 log_usb_status(status, __func__);
1474 hso_put_activity(serial->parent);
1476 tty_wakeup(serial->tty);
1477 hso_kick_transmit(serial);
1483 /* called for writing diag or CS serial port */
1484 static int hso_std_serial_write_data(struct hso_serial *serial)
1486 int count = serial->tx_data_count;
1489 usb_fill_bulk_urb(serial->tx_urb,
1490 serial->parent->usb,
1491 usb_sndbulkpipe(serial->parent->usb,
1493 bEndpointAddress & 0x7F),
1494 serial->tx_data, serial->tx_data_count,
1495 hso_std_serial_write_bulk_callback, serial);
1497 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1499 dev_warn(&serial->parent->usb->dev,
1500 "Failed to submit urb - res %d\n", result);
1507 /* callback after read or write on muxed serial port */
1508 static void ctrl_callback(struct urb *urb)
1510 struct hso_serial *serial = urb->context;
1511 struct usb_ctrlrequest *req;
1512 int status = urb->status;
1518 spin_lock(&serial->serial_lock);
1519 serial->tx_urb_used = 0;
1520 spin_unlock(&serial->serial_lock);
1522 log_usb_status(status, __func__);
1527 req = (struct usb_ctrlrequest *)(urb->setup_packet);
1528 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1529 D4("Actual length of urb = %d\n", urb->actual_length);
1530 DUMP1(urb->transfer_buffer, urb->actual_length);
1532 if (req->bRequestType ==
1533 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1534 /* response to a read command */
1535 if (serial->open_count > 0) {
1536 /* handle RX data the normal way */
1537 put_rxbuf_data(urb, serial);
1540 /* Re issue a read as long as we receive data. */
1541 if (urb->actual_length != 0)
1542 hso_mux_serial_read(serial);
1544 clear_bit(HSO_SERIAL_FLAG_RX_SENT, &serial->flags);
1546 hso_put_activity(serial->parent);
1548 tty_wakeup(serial->tty);
1549 /* response to a write command */
1550 hso_kick_transmit(serial);
1554 /* handle RX data for serial port */
1555 static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
1557 struct tty_struct *tty = serial->tty;
1560 if (urb == NULL || serial == NULL) {
1561 D1("serial = NULL");
1565 /* Push data to tty */
1566 if (tty && urb->actual_length) {
1567 D1("data to push to tty");
1568 tty_insert_flip_string(tty, urb->transfer_buffer,
1569 urb->actual_length);
1570 tty_flip_buffer_push(tty);
1574 /* read callback for Diag and CS port */
1575 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1577 struct hso_serial *serial = urb->context;
1579 int status = urb->status;
1583 D1("serial == NULL");
1585 } else if (status) {
1586 log_usb_status(status, __func__);
1590 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1591 D1("Actual length = %d\n", urb->actual_length);
1592 DUMP1(urb->transfer_buffer, urb->actual_length);
1594 /* Anyone listening? */
1595 if (serial->open_count == 0)
1599 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1601 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1603 urb->actual_length %
1604 serial->in_endp->wMaxPacketSize;
1605 if (((rest == 5) || (rest == 6))
1606 && !memcmp(((u8 *) urb->transfer_buffer) +
1607 urb->actual_length - 4, crc_check, 4)) {
1608 urb->actual_length -= 4;
1611 /* Valid data, handle RX data */
1612 put_rxbuf_data(urb, serial);
1613 } else if (status == -ENOENT || status == -ECONNRESET) {
1614 /* Unlinked - check for throttled port. */
1615 D2("Port %d, successfully unlinked urb", serial->minor);
1617 D2("Port %d, status = %d for read urb", serial->minor, status);
1621 usb_mark_last_busy(urb->dev);
1623 /* We are done with this URB, resubmit it. Prep the USB to wait for
1625 usb_fill_bulk_urb(urb, serial->parent->usb,
1626 usb_rcvbulkpipe(serial->parent->usb,
1628 bEndpointAddress & 0x7F),
1629 urb->transfer_buffer, serial->rx_data_length,
1630 hso_std_serial_read_bulk_callback, serial);
1631 /* Give this to the USB subsystem so it can tell us when more data
1633 result = usb_submit_urb(urb, GFP_ATOMIC);
1635 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d",
1640 /* Base driver functions */
1642 static void hso_log_port(struct hso_device *hso_dev)
1647 switch (hso_dev->port_spec & HSO_PORT_MASK) {
1648 case HSO_PORT_CONTROL:
1649 port_type = "Control";
1652 port_type = "Application";
1657 case HSO_PORT_GPS_CONTROL:
1658 port_type = "GPS control";
1661 port_type = "Application2";
1667 port_type = "Diagnostic";
1669 case HSO_PORT_DIAG2:
1670 port_type = "Diagnostic2";
1672 case HSO_PORT_MODEM:
1673 port_type = "Modem";
1675 case HSO_PORT_NETWORK:
1676 port_type = "Network";
1679 port_type = "Unknown";
1682 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
1683 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
1685 sprintf(port_dev, "/dev/%s%d", tty_filename,
1686 dev2ser(hso_dev)->minor);
1688 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
1689 port_type, port_dev);
1692 static int hso_start_net_device(struct hso_device *hso_dev)
1695 struct hso_net *hso_net = dev2net(hso_dev);
1700 /* send URBs for all read buffers */
1701 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1703 /* Prep a receive URB */
1704 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
1706 usb_rcvbulkpipe(hso_dev->usb,
1708 bEndpointAddress & 0x7F),
1709 hso_net->mux_bulk_rx_buf_pool[i],
1710 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
1713 /* Put it out there so the device can send us stuff */
1714 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
1717 dev_warn(&hso_dev->usb->dev,
1718 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
1725 static int hso_stop_net_device(struct hso_device *hso_dev)
1728 struct hso_net *hso_net = dev2net(hso_dev);
1733 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1734 if (hso_net->mux_bulk_rx_urb_pool[i])
1735 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1738 if (hso_net->mux_bulk_tx_urb)
1739 usb_kill_urb(hso_net->mux_bulk_tx_urb);
1744 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
1747 struct hso_serial *serial = dev2ser(hso_dev);
1752 /* If it is not the MUX port fill in and submit a bulk urb (already
1753 * allocated in hso_serial_start) */
1754 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
1755 for (i = 0; i < serial->num_rx_urbs; i++) {
1756 usb_fill_bulk_urb(serial->rx_urb[i],
1757 serial->parent->usb,
1758 usb_rcvbulkpipe(serial->parent->usb,
1763 serial->rx_data_length,
1764 hso_std_serial_read_bulk_callback,
1766 result = usb_submit_urb(serial->rx_urb[i], flags);
1768 dev_warn(&serial->parent->usb->dev,
1769 "Failed to submit urb - res %d\n",
1775 mutex_lock(&serial->shared_int->shared_int_lock);
1776 if (!serial->shared_int->use_count) {
1778 hso_mux_submit_intr_urb(serial->shared_int,
1779 hso_dev->usb, flags);
1781 serial->shared_int->use_count++;
1782 mutex_unlock(&serial->shared_int->shared_int_lock);
1788 static int hso_stop_serial_device(struct hso_device *hso_dev)
1791 struct hso_serial *serial = dev2ser(hso_dev);
1796 for (i = 0; i < serial->num_rx_urbs; i++) {
1797 if (serial->rx_urb[i])
1798 usb_kill_urb(serial->rx_urb[i]);
1802 usb_kill_urb(serial->tx_urb);
1804 if (serial->shared_int) {
1805 mutex_lock(&serial->shared_int->shared_int_lock);
1806 if (serial->shared_int->use_count &&
1807 (--serial->shared_int->use_count == 0)) {
1810 urb = serial->shared_int->shared_intr_urb;
1814 mutex_unlock(&serial->shared_int->shared_int_lock);
1820 static void hso_serial_common_free(struct hso_serial *serial)
1824 if (serial->parent->dev)
1825 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
1827 tty_unregister_device(tty_drv, serial->minor);
1829 for (i = 0; i < serial->num_rx_urbs; i++) {
1830 /* unlink and free RX URB */
1831 usb_free_urb(serial->rx_urb[i]);
1832 /* free the RX buffer */
1833 kfree(serial->rx_data[i]);
1836 /* unlink and free TX URB */
1837 usb_free_urb(serial->tx_urb);
1838 kfree(serial->tx_data);
1841 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
1842 int rx_size, int tx_size)
1848 minor = get_free_serial_index();
1852 /* register our minor number */
1853 serial->parent->dev = tty_register_device(tty_drv, minor,
1854 &serial->parent->interface->dev);
1855 dev = serial->parent->dev;
1856 dev->driver_data = serial->parent;
1857 i = device_create_file(dev, &dev_attr_hsotype);
1859 /* fill in specific data for later use */
1860 serial->minor = minor;
1861 serial->magic = HSO_SERIAL_MAGIC;
1862 spin_lock_init(&serial->serial_lock);
1863 serial->num_rx_urbs = num_urbs;
1865 /* RX, allocate urb and initialize */
1867 /* prepare our RX buffer */
1868 serial->rx_data_length = rx_size;
1869 for (i = 0; i < serial->num_rx_urbs; i++) {
1870 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1871 if (!serial->rx_urb[i]) {
1872 dev_err(dev, "Could not allocate urb?\n");
1875 serial->rx_urb[i]->transfer_buffer = NULL;
1876 serial->rx_urb[i]->transfer_buffer_length = 0;
1877 serial->rx_data[i] = kzalloc(serial->rx_data_length,
1879 if (!serial->rx_data[i]) {
1880 dev_err(dev, "%s - Out of memory\n", __func__);
1885 /* TX, allocate urb and initialize */
1886 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1887 if (!serial->tx_urb) {
1888 dev_err(dev, "Could not allocate urb?\n");
1891 serial->tx_urb->transfer_buffer = NULL;
1892 serial->tx_urb->transfer_buffer_length = 0;
1893 /* prepare our TX buffer */
1894 serial->tx_data_count = 0;
1895 serial->tx_buffer_count = 0;
1896 serial->tx_data_length = tx_size;
1897 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
1898 if (!serial->tx_data) {
1899 dev_err(dev, "%s - Out of memory", __func__);
1902 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
1903 if (!serial->tx_buffer) {
1904 dev_err(dev, "%s - Out of memory", __func__);
1910 hso_serial_common_free(serial);
1914 /* Frees a general hso device */
1915 static void hso_free_device(struct hso_device *hso_dev)
1920 /* Creates a general hso device */
1921 static struct hso_device *hso_create_device(struct usb_interface *intf,
1924 struct hso_device *hso_dev;
1926 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
1930 hso_dev->port_spec = port_spec;
1931 hso_dev->usb = interface_to_usbdev(intf);
1932 hso_dev->interface = intf;
1933 kref_init(&hso_dev->ref);
1934 mutex_init(&hso_dev->mutex);
1936 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
1937 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
1942 /* Removes a network device in the network device table */
1943 static int remove_net_device(struct hso_device *hso_dev)
1947 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
1948 if (network_table[i] == hso_dev) {
1949 network_table[i] = NULL;
1953 if (i == HSO_MAX_NET_DEVICES)
1958 /* Frees our network device */
1959 static void hso_free_net_device(struct hso_device *hso_dev)
1962 struct hso_net *hso_net = dev2net(hso_dev);
1968 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1969 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1970 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
1972 usb_free_urb(hso_net->mux_bulk_tx_urb);
1973 kfree(hso_net->mux_bulk_tx_buf);
1975 remove_net_device(hso_net->parent);
1978 unregister_netdev(hso_net->net);
1979 free_netdev(hso_net->net);
1982 hso_free_device(hso_dev);
1985 /* initialize the network interface */
1986 static void hso_net_init(struct net_device *net)
1988 struct hso_net *hso_net = netdev_priv(net);
1990 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
1992 /* fill in the other fields */
1993 net->open = hso_net_open;
1994 net->stop = hso_net_close;
1995 net->hard_start_xmit = hso_net_start_xmit;
1996 net->tx_timeout = hso_net_tx_timeout;
1997 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
1998 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1999 net->type = ARPHRD_NONE;
2000 net->mtu = DEFAULT_MTU - 14;
2001 net->tx_queue_len = 10;
2002 SET_ETHTOOL_OPS(net, &ops);
2004 /* and initialize the semaphore */
2005 spin_lock_init(&hso_net->net_lock);
2008 /* Adds a network device in the network device table */
2009 static int add_net_device(struct hso_device *hso_dev)
2013 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2014 if (network_table[i] == NULL) {
2015 network_table[i] = hso_dev;
2019 if (i == HSO_MAX_NET_DEVICES)
2024 static int hso_radio_toggle(void *data, enum rfkill_state state)
2026 struct hso_device *hso_dev = data;
2027 int enabled = (state == RFKILL_STATE_ON);
2030 mutex_lock(&hso_dev->mutex);
2031 if (hso_dev->usb_gone)
2034 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2035 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2036 USB_CTRL_SET_TIMEOUT);
2037 mutex_unlock(&hso_dev->mutex);
2041 /* Creates and sets up everything for rfkill */
2042 static void hso_create_rfkill(struct hso_device *hso_dev,
2043 struct usb_interface *interface)
2045 struct hso_net *hso_net = dev2net(hso_dev);
2046 struct device *dev = hso_dev->dev;
2049 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2051 if (!hso_net->rfkill) {
2052 dev_err(dev, "%s - Out of memory", __func__);
2055 rfkn = kzalloc(20, GFP_KERNEL);
2057 rfkill_free(hso_net->rfkill);
2058 dev_err(dev, "%s - Out of memory", __func__);
2061 snprintf(rfkn, 20, "hso-%d",
2062 interface->altsetting->desc.bInterfaceNumber);
2063 hso_net->rfkill->name = rfkn;
2064 hso_net->rfkill->state = RFKILL_STATE_ON;
2065 hso_net->rfkill->data = hso_dev;
2066 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2067 if (rfkill_register(hso_net->rfkill) < 0) {
2069 hso_net->rfkill->name = NULL;
2070 rfkill_free(hso_net->rfkill);
2071 dev_err(dev, "%s - Failed to register rfkill", __func__);
2076 /* Creates our network device */
2077 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2080 struct net_device *net;
2081 struct hso_net *hso_net;
2082 struct hso_device *hso_dev;
2084 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2088 /* allocate our network device, then we can put in our private data */
2089 /* call hso_net_init to do the basic initialization */
2090 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2092 dev_err(&interface->dev, "Unable to create ethernet device\n");
2096 hso_net = netdev_priv(net);
2098 hso_dev->port_data.dev_net = hso_net;
2100 hso_net->parent = hso_dev;
2102 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2104 if (!hso_net->in_endp) {
2105 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2108 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2110 if (!hso_net->out_endp) {
2111 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2114 SET_NETDEV_DEV(net, &interface->dev);
2116 /* registering our net device */
2117 result = register_netdev(net);
2119 dev_err(&interface->dev, "Failed to register device\n");
2123 /* start allocating */
2124 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2125 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2126 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2127 dev_err(&interface->dev, "Could not allocate rx urb\n");
2130 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2132 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2133 dev_err(&interface->dev, "Could not allocate rx buf\n");
2137 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2138 if (!hso_net->mux_bulk_tx_urb) {
2139 dev_err(&interface->dev, "Could not allocate tx urb\n");
2142 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2143 if (!hso_net->mux_bulk_tx_buf) {
2144 dev_err(&interface->dev, "Could not allocate tx buf\n");
2148 add_net_device(hso_dev);
2150 hso_log_port(hso_dev);
2152 hso_create_rfkill(hso_dev, interface);
2156 hso_free_net_device(hso_dev);
2160 /* Frees an AT channel ( goes for both mux and non-mux ) */
2161 static void hso_free_serial_device(struct hso_device *hso_dev)
2163 struct hso_serial *serial = dev2ser(hso_dev);
2167 set_serial_by_index(serial->minor, NULL);
2169 hso_serial_common_free(serial);
2171 if (serial->shared_int) {
2172 mutex_lock(&serial->shared_int->shared_int_lock);
2173 if (--serial->shared_int->ref_count == 0)
2174 hso_free_shared_int(serial->shared_int);
2176 mutex_unlock(&serial->shared_int->shared_int_lock);
2179 hso_free_device(hso_dev);
2182 /* Creates a bulk AT channel */
2183 static struct hso_device *hso_create_bulk_serial_device(
2184 struct usb_interface *interface, int port)
2186 struct hso_device *hso_dev;
2187 struct hso_serial *serial;
2190 hso_dev = hso_create_device(interface, port);
2194 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2198 serial->parent = hso_dev;
2199 hso_dev->port_data.dev_serial = serial;
2201 if (port & HSO_PORT_MODEM)
2206 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2210 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2212 if (!serial->in_endp) {
2213 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2219 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2220 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2224 serial->write_data = hso_std_serial_write_data;
2226 /* and record this serial */
2227 set_serial_by_index(serial->minor, serial);
2229 /* setup the proc dirs and files if needed */
2230 hso_log_port(hso_dev);
2232 /* done, return it */
2235 if (hso_dev && serial)
2236 hso_serial_common_free(serial);
2238 hso_free_device(hso_dev);
2242 /* Creates a multiplexed AT channel */
2244 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2246 struct hso_shared_int *mux)
2248 struct hso_device *hso_dev;
2249 struct hso_serial *serial;
2252 port_spec = HSO_INTF_MUX;
2253 port_spec &= ~HSO_PORT_MASK;
2255 port_spec |= hso_mux_to_port(port);
2256 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2259 hso_dev = hso_create_device(interface, port_spec);
2263 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2267 hso_dev->port_data.dev_serial = serial;
2268 serial->parent = hso_dev;
2270 if (hso_serial_common_create
2271 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2274 serial->tx_data_length--;
2275 serial->write_data = hso_mux_serial_write_data;
2277 serial->shared_int = mux;
2278 mutex_lock(&serial->shared_int->shared_int_lock);
2279 serial->shared_int->ref_count++;
2280 mutex_unlock(&serial->shared_int->shared_int_lock);
2282 /* and record this serial */
2283 set_serial_by_index(serial->minor, serial);
2285 /* setup the proc dirs and files if needed */
2286 hso_log_port(hso_dev);
2288 /* done, return it */
2293 tty_unregister_device(tty_drv, serial->minor);
2297 hso_free_device(hso_dev);
2302 static void hso_free_shared_int(struct hso_shared_int *mux)
2304 usb_free_urb(mux->shared_intr_urb);
2305 kfree(mux->shared_intr_buf);
2306 mutex_unlock(&mux->shared_int_lock);
2311 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2313 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2318 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2320 if (!mux->intr_endp) {
2321 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2325 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2326 if (!mux->shared_intr_urb) {
2327 dev_err(&interface->dev, "Could not allocate intr urb?");
2330 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2332 if (!mux->shared_intr_buf) {
2333 dev_err(&interface->dev, "Could not allocate intr buf?");
2337 mutex_init(&mux->shared_int_lock);
2342 kfree(mux->shared_intr_buf);
2343 usb_free_urb(mux->shared_intr_urb);
2348 /* Gets the port spec for a certain interface */
2349 static int hso_get_config_data(struct usb_interface *interface)
2351 struct usb_device *usbdev = interface_to_usbdev(interface);
2353 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2356 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2357 0x86, 0xC0, 0, 0, config_data, 17,
2358 USB_CTRL_SET_TIMEOUT) != 0x11) {
2362 switch (config_data[if_num]) {
2367 result = HSO_PORT_DIAG;
2370 result = HSO_PORT_GPS;
2373 result = HSO_PORT_GPS_CONTROL;
2376 result = HSO_PORT_APP;
2379 result = HSO_PORT_APP2;
2382 result = HSO_PORT_CONTROL;
2385 result = HSO_PORT_NETWORK;
2388 result = HSO_PORT_MODEM;
2391 result = HSO_PORT_MSD;
2394 result = HSO_PORT_PCSC;
2397 result = HSO_PORT_VOICE;
2404 result |= HSO_INTF_BULK;
2406 if (config_data[16] & 0x1)
2407 result |= HSO_INFO_CRC_BUG;
2412 /* called once for each interface upon device insertion */
2413 static int hso_probe(struct usb_interface *interface,
2414 const struct usb_device_id *id)
2416 int mux, i, if_num, port_spec;
2417 unsigned char port_mask;
2418 struct hso_device *hso_dev = NULL;
2419 struct hso_shared_int *shared_int;
2420 struct hso_device *tmp_dev = NULL;
2422 if_num = interface->altsetting->desc.bInterfaceNumber;
2424 /* Get the interface/port specification from either driver_info or from
2425 * the device itself */
2426 if (id->driver_info)
2427 port_spec = ((u32 *)(id->driver_info))[if_num];
2429 port_spec = hso_get_config_data(interface);
2431 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2432 dev_err(&interface->dev, "Not our interface\n");
2435 /* Check if we need to switch to alt interfaces prior to port
2437 if (interface->num_altsetting > 1)
2438 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2439 interface->needs_remote_wakeup = 1;
2441 /* Allocate new hso device(s) */
2442 switch (port_spec & HSO_INTF_MASK) {
2444 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2445 /* Create the network device */
2447 hso_dev = hso_create_net_device(interface);
2454 if (hso_get_mux_ports(interface, &port_mask))
2455 /* TODO: de-allocate everything */
2458 shared_int = hso_create_shared_int(interface);
2462 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2463 if (port_mask & i) {
2464 hso_dev = hso_create_mux_serial_device(
2465 interface, i, shared_int);
2476 /* It's a regular bulk interface */
2477 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2479 hso_dev = hso_create_net_device(interface);
2482 hso_create_bulk_serial_device(interface, port_spec);
2490 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2492 /* save our data pointer in this device */
2493 usb_set_intfdata(interface, hso_dev);
2498 hso_free_interface(interface);
2502 /* device removed, cleaning up */
2503 static void hso_disconnect(struct usb_interface *interface)
2505 hso_free_interface(interface);
2507 /* remove reference of our private data */
2508 usb_set_intfdata(interface, NULL);
2510 usb_driver_release_interface(&hso_driver, interface);
2513 static void async_get_intf(struct work_struct *data)
2515 struct hso_device *hso_dev =
2516 container_of(data, struct hso_device, async_get_intf);
2517 usb_autopm_get_interface(hso_dev->interface);
2520 static void async_put_intf(struct work_struct *data)
2522 struct hso_device *hso_dev =
2523 container_of(data, struct hso_device, async_put_intf);
2524 usb_autopm_put_interface(hso_dev->interface);
2527 static int hso_get_activity(struct hso_device *hso_dev)
2529 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2530 if (!hso_dev->is_active) {
2531 hso_dev->is_active = 1;
2532 schedule_work(&hso_dev->async_get_intf);
2536 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2539 usb_mark_last_busy(hso_dev->usb);
2544 static int hso_put_activity(struct hso_device *hso_dev)
2546 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2547 if (hso_dev->is_active) {
2548 hso_dev->is_active = 0;
2549 schedule_work(&hso_dev->async_put_intf);
2553 hso_dev->is_active = 0;
2557 /* called by kernel when we need to suspend device */
2558 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2562 /* Stop all serial ports */
2563 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2564 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2565 result = hso_stop_serial_device(serial_table[i]);
2571 /* Stop all network ports */
2572 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2573 if (network_table[i] &&
2574 (network_table[i]->interface == iface)) {
2575 result = hso_stop_net_device(network_table[i]);
2585 /* called by kernel when we need to resume device */
2586 static int hso_resume(struct usb_interface *iface)
2589 struct hso_net *hso_net;
2591 /* Start all serial ports */
2592 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2593 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2594 if (dev2ser(serial_table[i])->open_count) {
2596 hso_start_serial_device(serial_table[i], GFP_NOIO);
2597 hso_kick_transmit(dev2ser(serial_table[i]));
2604 /* Start all network ports */
2605 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2606 if (network_table[i] &&
2607 (network_table[i]->interface == iface)) {
2608 hso_net = dev2net(network_table[i]);
2609 /* First transmit any lingering data, then restart the
2611 if (hso_net->skb_tx_buf) {
2612 dev_dbg(&iface->dev,
2613 "Transmitting lingering data\n");
2614 hso_net_start_xmit(hso_net->skb_tx_buf,
2616 hso_net->skb_tx_buf = NULL;
2618 result = hso_start_net_device(network_table[i]);
2628 static void hso_serial_ref_free(struct kref *ref)
2630 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2632 hso_free_serial_device(hso_dev);
2635 static void hso_free_interface(struct usb_interface *interface)
2637 struct hso_serial *hso_dev;
2640 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2642 && (serial_table[i]->interface == interface)) {
2643 hso_dev = dev2ser(serial_table[i]);
2645 tty_hangup(hso_dev->tty);
2646 mutex_lock(&hso_dev->parent->mutex);
2647 hso_dev->parent->usb_gone = 1;
2648 mutex_unlock(&hso_dev->parent->mutex);
2649 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2653 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2654 if (network_table[i]
2655 && (network_table[i]->interface == interface)) {
2656 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2657 /* hso_stop_net_device doesn't stop the net queue since
2658 * traffic needs to start it again when suspended */
2659 netif_stop_queue(dev2net(network_table[i])->net);
2660 hso_stop_net_device(network_table[i]);
2661 cancel_work_sync(&network_table[i]->async_put_intf);
2662 cancel_work_sync(&network_table[i]->async_get_intf);
2664 rfkill_unregister(rfk);
2665 hso_free_net_device(network_table[i]);
2670 /* Helper functions */
2672 /* Get the endpoint ! */
2673 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2677 struct usb_host_interface *iface = intf->cur_altsetting;
2678 struct usb_endpoint_descriptor *endp;
2680 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2681 endp = &iface->endpoint[i].desc;
2682 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2683 ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
2690 /* Get the byte that describes which ports are enabled */
2691 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2694 struct usb_host_interface *iface = intf->cur_altsetting;
2696 if (iface->extralen == 3) {
2697 *ports = iface->extra[2];
2701 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2702 if (iface->endpoint[i].extralen == 3) {
2703 *ports = iface->endpoint[i].extra[2];
2711 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2712 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2713 struct usb_device *usb, gfp_t gfp)
2717 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2719 shared_int->intr_endp->bEndpointAddress & 0x7F),
2720 shared_int->shared_intr_buf,
2721 shared_int->intr_endp->wMaxPacketSize,
2722 intr_callback, shared_int,
2723 shared_int->intr_endp->bInterval);
2725 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2727 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2733 /* operations setup of the serial interface */
2734 static const struct tty_operations hso_serial_ops = {
2735 .open = hso_serial_open,
2736 .close = hso_serial_close,
2737 .write = hso_serial_write,
2738 .write_room = hso_serial_write_room,
2739 .set_termios = hso_serial_set_termios,
2740 .chars_in_buffer = hso_serial_chars_in_buffer,
2741 .tiocmget = hso_serial_tiocmget,
2742 .tiocmset = hso_serial_tiocmset,
2745 static struct usb_driver hso_driver = {
2746 .name = driver_name,
2748 .disconnect = hso_disconnect,
2749 .id_table = hso_ids,
2750 .suspend = hso_suspend,
2751 .resume = hso_resume,
2752 .supports_autosuspend = 1,
2755 static int __init hso_init(void)
2760 /* put it in the log */
2761 printk(KERN_INFO "hso: %s\n", version);
2763 /* Initialise the serial table semaphore and table */
2764 spin_lock_init(&serial_table_lock);
2765 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2766 serial_table[i] = NULL;
2768 /* allocate our driver using the proper amount of supported minors */
2769 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2773 /* fill in all needed values */
2774 tty_drv->magic = TTY_DRIVER_MAGIC;
2775 tty_drv->owner = THIS_MODULE;
2776 tty_drv->driver_name = driver_name;
2777 tty_drv->name = tty_filename;
2779 /* if major number is provided as parameter, use that one */
2781 tty_drv->major = tty_major;
2783 tty_drv->minor_start = 0;
2784 tty_drv->num = HSO_SERIAL_TTY_MINORS;
2785 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2786 tty_drv->subtype = SERIAL_TYPE_NORMAL;
2787 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2788 tty_drv->init_termios = tty_std_termios;
2789 tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2790 tty_drv->termios = hso_serial_termios;
2791 tty_drv->termios_locked = hso_serial_termios_locked;
2792 tty_set_operations(tty_drv, &hso_serial_ops);
2794 /* register the tty driver */
2795 result = tty_register_driver(tty_drv);
2797 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2802 /* register this module as an usb driver */
2803 result = usb_register(&hso_driver);
2805 printk(KERN_ERR "Could not register hso driver? error: %d\n",
2807 /* cleanup serial interface */
2808 tty_unregister_driver(tty_drv);
2816 static void __exit hso_exit(void)
2818 printk(KERN_INFO "hso: unloaded\n");
2820 tty_unregister_driver(tty_drv);
2821 /* deregister the usb driver */
2822 usb_deregister(&hso_driver);
2825 /* Module definitions */
2826 module_init(hso_init);
2827 module_exit(hso_exit);
2829 MODULE_AUTHOR(MOD_AUTHOR);
2830 MODULE_DESCRIPTION(MOD_DESCRIPTION);
2831 MODULE_LICENSE(MOD_LICENSE);
2832 MODULE_INFO(Version, DRIVER_VERSION);
2834 /* change the debug level (eg: insmod hso.ko debug=0x04) */
2835 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2836 module_param(debug, int, S_IRUGO | S_IWUSR);
2838 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
2839 MODULE_PARM_DESC(tty_major, "Set the major tty number");
2840 module_param(tty_major, int, S_IRUGO | S_IWUSR);
2842 /* disable network interface (eg: insmod hso.ko disable_net=1) */
2843 MODULE_PARM_DESC(disable_net, "Disable the network interface");
2844 module_param(disable_net, int, S_IRUGO | S_IWUSR);