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 #define HSO_SERIAL_MAGIC 0x48534f31
97 /* Number of ttys to handle */
98 #define HSO_SERIAL_TTY_MINORS 256
100 #define MAX_RX_URBS 2
102 static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
105 return tty->driver_data;
109 /*****************************************************************************/
110 /* Debugging functions */
111 /*****************************************************************************/
112 #define D__(lvl_, fmt, arg...) \
114 printk(lvl_ "[%d:%s]: " fmt "\n", \
115 __LINE__, __func__, ## arg); \
118 #define D_(lvl, args...) \
121 D__(KERN_INFO, args); \
124 #define D1(args...) D_(0x01, ##args)
125 #define D2(args...) D_(0x02, ##args)
126 #define D3(args...) D_(0x04, ##args)
127 #define D4(args...) D_(0x08, ##args)
128 #define D5(args...) D_(0x10, ##args)
130 /*****************************************************************************/
132 /*****************************************************************************/
133 enum pkt_parse_state {
139 /*****************************************************************************/
141 /*****************************************************************************/
143 struct hso_shared_int {
144 struct usb_endpoint_descriptor *intr_endp;
145 void *shared_intr_buf;
146 struct urb *shared_intr_urb;
147 struct usb_device *usb;
150 struct mutex shared_int_lock;
154 struct hso_device *parent;
155 struct net_device *net;
156 struct rfkill *rfkill;
158 struct usb_endpoint_descriptor *in_endp;
159 struct usb_endpoint_descriptor *out_endp;
161 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
162 struct urb *mux_bulk_tx_urb;
163 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
164 void *mux_bulk_tx_buf;
166 struct sk_buff *skb_rx_buf;
167 struct sk_buff *skb_tx_buf;
169 enum pkt_parse_state rx_parse_state;
172 unsigned short rx_buf_size;
173 unsigned short rx_buf_missing;
174 struct iphdr rx_ip_hdr;
186 struct hso_device *parent;
190 struct hso_shared_int *shared_int;
192 /* rx/tx urb could be either a bulk urb or a control urb depending
193 on which serial port it is used on. */
194 struct urb *rx_urb[MAX_RX_URBS];
196 u8 *rx_data[MAX_RX_URBS];
197 u16 rx_data_length; /* should contain allocated length */
202 u16 tx_data_length; /* should contain allocated length */
205 struct usb_ctrlrequest ctrl_req_tx;
206 struct usb_ctrlrequest ctrl_req_rx;
208 struct usb_endpoint_descriptor *in_endp;
209 struct usb_endpoint_descriptor *out_endp;
211 enum rx_ctrl_state rx_state;
214 unsigned tx_urb_used:1;
216 /* from usb_serial_port */
217 struct tty_struct *tty;
219 spinlock_t serial_lock;
221 int (*write_data) (struct hso_serial *serial);
222 /* Hacks required to get flow control
223 * working on the serial receive buffers
224 * so as not to drop characters on the floor.
227 u16 curr_rx_urb_offset;
228 u8 rx_urb_filled[MAX_RX_URBS];
229 struct tasklet_struct unthrottle_tasklet;
230 struct work_struct retry_unthrottle_workqueue;
235 struct hso_serial *dev_serial;
236 struct hso_net *dev_net;
243 struct work_struct async_get_intf;
244 struct work_struct async_put_intf;
246 struct usb_device *usb;
247 struct usb_interface *interface;
254 /* Type of interface */
255 #define HSO_INTF_MASK 0xFF00
256 #define HSO_INTF_MUX 0x0100
257 #define HSO_INTF_BULK 0x0200
260 #define HSO_PORT_MASK 0xFF
261 #define HSO_PORT_NO_PORT 0x0
262 #define HSO_PORT_CONTROL 0x1
263 #define HSO_PORT_APP 0x2
264 #define HSO_PORT_GPS 0x3
265 #define HSO_PORT_PCSC 0x4
266 #define HSO_PORT_APP2 0x5
267 #define HSO_PORT_GPS_CONTROL 0x6
268 #define HSO_PORT_MSD 0x7
269 #define HSO_PORT_VOICE 0x8
270 #define HSO_PORT_DIAG2 0x9
271 #define HSO_PORT_DIAG 0x10
272 #define HSO_PORT_MODEM 0x11
273 #define HSO_PORT_NETWORK 0x12
275 /* Additional device info */
276 #define HSO_INFO_MASK 0xFF000000
277 #define HSO_INFO_CRC_BUG 0x01000000
279 /*****************************************************************************/
281 /*****************************************************************************/
282 /* Serial driver functions */
283 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
284 unsigned int set, unsigned int clear);
285 static void ctrl_callback(struct urb *urb);
286 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
287 static void hso_kick_transmit(struct hso_serial *serial);
288 /* Helper functions */
289 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
290 struct usb_device *usb, gfp_t gfp);
291 static void log_usb_status(int status, const char *function);
292 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
294 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
295 static void hso_free_interface(struct usb_interface *intf);
296 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
297 static int hso_stop_serial_device(struct hso_device *hso_dev);
298 static int hso_start_net_device(struct hso_device *hso_dev);
299 static void hso_free_shared_int(struct hso_shared_int *shared_int);
300 static int hso_stop_net_device(struct hso_device *hso_dev);
301 static void hso_serial_ref_free(struct kref *ref);
302 static void hso_std_serial_read_bulk_callback(struct urb *urb);
303 static int hso_mux_serial_read(struct hso_serial *serial);
304 static void async_get_intf(struct work_struct *data);
305 static void async_put_intf(struct work_struct *data);
306 static int hso_put_activity(struct hso_device *hso_dev);
307 static int hso_get_activity(struct hso_device *hso_dev);
309 /*****************************************************************************/
310 /* Helping functions */
311 /*****************************************************************************/
315 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
317 return hso_dev->port_data.dev_net;
320 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
322 return hso_dev->port_data.dev_serial;
325 /* Debugging functions */
327 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
330 static char name[255];
332 sprintf(name, "hso[%d:%s]", line_count, func_name);
333 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
336 #define DUMP(buf_, len_) \
337 dbg_dump(__LINE__, __func__, buf_, len_)
339 #define DUMP1(buf_, len_) \
345 #define DUMP(buf_, len_)
346 #define DUMP1(buf_, len_)
349 /* module parameters */
351 static int tty_major;
352 static int disable_net;
355 static const char driver_name[] = "hso";
356 static const char tty_filename[] = "ttyHS";
357 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
358 /* the usb driver itself (registered in hso_init) */
359 static struct usb_driver hso_driver;
360 /* serial structures */
361 static struct tty_driver *tty_drv;
362 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
363 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
364 static spinlock_t serial_table_lock;
365 static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
366 static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
368 static const s32 default_port_spec[] = {
369 HSO_INTF_MUX | HSO_PORT_NETWORK,
370 HSO_INTF_BULK | HSO_PORT_DIAG,
371 HSO_INTF_BULK | HSO_PORT_MODEM,
375 static const s32 icon321_port_spec[] = {
376 HSO_INTF_MUX | HSO_PORT_NETWORK,
377 HSO_INTF_BULK | HSO_PORT_DIAG2,
378 HSO_INTF_BULK | HSO_PORT_MODEM,
379 HSO_INTF_BULK | HSO_PORT_DIAG,
383 #define default_port_device(vendor, product) \
384 USB_DEVICE(vendor, product), \
385 .driver_info = (kernel_ulong_t)default_port_spec
387 #define icon321_port_device(vendor, product) \
388 USB_DEVICE(vendor, product), \
389 .driver_info = (kernel_ulong_t)icon321_port_spec
391 /* list of devices we support */
392 static const struct usb_device_id hso_ids[] = {
393 {default_port_device(0x0af0, 0x6711)},
394 {default_port_device(0x0af0, 0x6731)},
395 {default_port_device(0x0af0, 0x6751)},
396 {default_port_device(0x0af0, 0x6771)},
397 {default_port_device(0x0af0, 0x6791)},
398 {default_port_device(0x0af0, 0x6811)},
399 {default_port_device(0x0af0, 0x6911)},
400 {default_port_device(0x0af0, 0x6951)},
401 {default_port_device(0x0af0, 0x6971)},
402 {default_port_device(0x0af0, 0x7011)},
403 {default_port_device(0x0af0, 0x7031)},
404 {default_port_device(0x0af0, 0x7051)},
405 {default_port_device(0x0af0, 0x7071)},
406 {default_port_device(0x0af0, 0x7111)},
407 {default_port_device(0x0af0, 0x7211)},
408 {default_port_device(0x0af0, 0x7251)},
409 {default_port_device(0x0af0, 0x7271)},
410 {default_port_device(0x0af0, 0x7311)},
411 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
412 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
413 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
414 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
415 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
416 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
417 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
418 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
419 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
422 MODULE_DEVICE_TABLE(usb, hso_ids);
424 /* Sysfs attribute */
425 static ssize_t hso_sysfs_show_porttype(struct device *dev,
426 struct device_attribute *attr,
429 struct hso_device *hso_dev = dev->driver_data;
435 switch (hso_dev->port_spec & HSO_PORT_MASK) {
436 case HSO_PORT_CONTROL:
437 port_name = "Control";
440 port_name = "Application";
443 port_name = "Application2";
448 case HSO_PORT_GPS_CONTROL:
449 port_name = "GPS Control";
455 port_name = "Diagnostic";
458 port_name = "Diagnostic2";
463 case HSO_PORT_NETWORK:
464 port_name = "Network";
467 port_name = "Unknown";
471 return sprintf(buf, "%s\n", port_name);
473 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
475 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
479 for (idx = 0; idx < serial->num_rx_urbs; idx++)
480 if (serial->rx_urb[idx] == urb)
482 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
486 /* converts mux value to a port spec value */
487 static u32 hso_mux_to_port(int mux)
493 result = HSO_PORT_CONTROL;
496 result = HSO_PORT_APP;
499 result = HSO_PORT_PCSC;
502 result = HSO_PORT_GPS;
505 result = HSO_PORT_APP2;
508 result = HSO_PORT_NO_PORT;
513 /* converts port spec value to a mux value */
514 static u32 hso_port_to_mux(int port)
518 switch (port & HSO_PORT_MASK) {
519 case HSO_PORT_CONTROL:
540 static struct hso_serial *get_serial_by_shared_int_and_type(
541 struct hso_shared_int *shared_int,
546 port = hso_mux_to_port(mux);
548 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
550 && (dev2ser(serial_table[i])->shared_int == shared_int)
551 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
552 return dev2ser(serial_table[i]);
559 static struct hso_serial *get_serial_by_index(unsigned index)
561 struct hso_serial *serial = NULL;
564 spin_lock_irqsave(&serial_table_lock, flags);
565 if (serial_table[index])
566 serial = dev2ser(serial_table[index]);
567 spin_unlock_irqrestore(&serial_table_lock, flags);
572 static int get_free_serial_index(void)
577 spin_lock_irqsave(&serial_table_lock, flags);
578 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
579 if (serial_table[index] == NULL) {
580 spin_unlock_irqrestore(&serial_table_lock, flags);
584 spin_unlock_irqrestore(&serial_table_lock, flags);
586 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
590 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
594 spin_lock_irqsave(&serial_table_lock, flags);
596 serial_table[index] = serial->parent;
598 serial_table[index] = NULL;
599 spin_unlock_irqrestore(&serial_table_lock, flags);
602 /* log a meaningful explanation of an USB status */
603 static void log_usb_status(int status, const char *function)
609 explanation = "no device";
612 explanation = "endpoint not enabled";
615 explanation = "endpoint stalled";
618 explanation = "not enough bandwidth";
621 explanation = "device disabled";
624 explanation = "device suspended";
630 explanation = "internal error";
633 explanation = "unknown status";
636 D1("%s: received USB status - %s (%d)", function, explanation, status);
639 /* Network interface functions */
641 /* called when net interface is brought up by ifconfig */
642 static int hso_net_open(struct net_device *net)
644 struct hso_net *odev = netdev_priv(net);
645 unsigned long flags = 0;
648 dev_err(&net->dev, "No net device !\n");
652 odev->skb_tx_buf = NULL;
654 /* setup environment */
655 spin_lock_irqsave(&odev->net_lock, flags);
656 odev->rx_parse_state = WAIT_IP;
657 odev->rx_buf_size = 0;
658 odev->rx_buf_missing = sizeof(struct iphdr);
659 spin_unlock_irqrestore(&odev->net_lock, flags);
661 hso_start_net_device(odev->parent);
663 /* We are up and running. */
664 set_bit(HSO_NET_RUNNING, &odev->flags);
666 /* Tell the kernel we are ready to start receiving from it */
667 netif_start_queue(net);
672 /* called when interface is brought down by ifconfig */
673 static int hso_net_close(struct net_device *net)
675 struct hso_net *odev = netdev_priv(net);
677 /* we don't need the queue anymore */
678 netif_stop_queue(net);
679 /* no longer running */
680 clear_bit(HSO_NET_RUNNING, &odev->flags);
682 hso_stop_net_device(odev->parent);
688 /* USB tells is xmit done, we should start the netqueue again */
689 static void write_bulk_callback(struct urb *urb)
691 struct hso_net *odev = urb->context;
692 int status = urb->status;
695 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
696 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
700 /* Do we still have a valid kernel network device? */
701 if (!netif_device_present(odev->net)) {
702 dev_err(&urb->dev->dev, "%s: net device not present\n",
707 /* log status, but don't act on it, we don't need to resubmit anything
710 log_usb_status(status, __func__);
712 hso_put_activity(odev->parent);
714 /* Tell the network interface we are ready for another frame */
715 netif_wake_queue(odev->net);
718 /* called by kernel when we need to transmit a packet */
719 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
721 struct hso_net *odev = netdev_priv(net);
724 /* Tell the kernel, "No more frames 'til we are done with this one." */
725 netif_stop_queue(net);
726 if (hso_get_activity(odev->parent) == -EAGAIN) {
727 odev->skb_tx_buf = skb;
732 DUMP1(skb->data, skb->len);
733 /* Copy it from kernel memory to OUR memory */
734 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
735 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
737 /* Fill in the URB for shipping it out. */
738 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
740 usb_sndbulkpipe(odev->parent->usb,
742 bEndpointAddress & 0x7F),
743 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
746 /* Deal with the Zero Length packet problem, I hope */
747 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
749 /* Send the URB on its merry way. */
750 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
752 dev_warn(&odev->parent->interface->dev,
753 "failed mux_bulk_tx_urb %d", result);
754 net->stats.tx_errors++;
755 netif_start_queue(net);
757 net->stats.tx_packets++;
758 net->stats.tx_bytes += skb->len;
759 /* And tell the kernel when the last transmit started. */
760 net->trans_start = jiffies;
767 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
769 struct hso_net *odev = netdev_priv(net);
771 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
772 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
773 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
776 static struct ethtool_ops ops = {
777 .get_drvinfo = hso_get_drvinfo,
778 .get_link = ethtool_op_get_link
781 /* called when a packet did not ack after watchdogtimeout */
782 static void hso_net_tx_timeout(struct net_device *net)
784 struct hso_net *odev = netdev_priv(net);
789 /* Tell syslog we are hosed. */
790 dev_warn(&net->dev, "Tx timed out.\n");
792 /* Tear the waiting frame off the list */
793 if (odev->mux_bulk_tx_urb
794 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
795 usb_unlink_urb(odev->mux_bulk_tx_urb);
797 /* Update statistics */
798 net->stats.tx_errors++;
801 /* make a real packet from the received USB buffer */
802 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
803 unsigned int count, unsigned char is_eop)
805 unsigned short temp_bytes;
806 unsigned short buffer_offset = 0;
807 unsigned short frame_len;
808 unsigned char *tmp_rx_buf;
811 D1("Rx %d bytes", count);
812 DUMP(ip_pkt, min(128, (int)count));
815 switch (odev->rx_parse_state) {
817 /* waiting for IP header. */
818 /* wanted bytes - size of ip header */
821 odev->rx_buf_missing) ? count : odev->
824 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
825 odev->rx_buf_size, ip_pkt + buffer_offset,
828 odev->rx_buf_size += temp_bytes;
829 buffer_offset += temp_bytes;
830 odev->rx_buf_missing -= temp_bytes;
833 if (!odev->rx_buf_missing) {
834 /* header is complete allocate an sk_buffer and
835 * continue to WAIT_DATA */
836 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
838 if ((frame_len > DEFAULT_MRU) ||
839 (frame_len < sizeof(struct iphdr))) {
840 dev_err(&odev->net->dev,
841 "Invalid frame (%d) length\n",
843 odev->rx_parse_state = WAIT_SYNC;
846 /* Allocate an sk_buff */
847 odev->skb_rx_buf = dev_alloc_skb(frame_len);
848 if (!odev->skb_rx_buf) {
849 /* We got no receive buffer. */
850 D1("could not allocate memory");
851 odev->rx_parse_state = WAIT_SYNC;
854 /* Here's where it came from */
855 odev->skb_rx_buf->dev = odev->net;
857 /* Copy what we got so far. make room for iphdr
860 skb_put(odev->skb_rx_buf,
861 sizeof(struct iphdr));
862 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
863 sizeof(struct iphdr));
866 odev->rx_buf_size = sizeof(struct iphdr);
868 /* Filip actually use .tot_len */
869 odev->rx_buf_missing =
870 frame_len - sizeof(struct iphdr);
871 odev->rx_parse_state = WAIT_DATA;
876 temp_bytes = (count < odev->rx_buf_missing)
877 ? count : odev->rx_buf_missing;
879 /* Copy the rest of the bytes that are left in the
880 * buffer into the waiting sk_buf. */
881 /* Make room for temp_bytes after tail. */
882 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
883 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
885 odev->rx_buf_missing -= temp_bytes;
887 buffer_offset += temp_bytes;
888 odev->rx_buf_size += temp_bytes;
889 if (!odev->rx_buf_missing) {
890 /* Packet is complete. Inject into stack. */
891 /* We have IP packet here */
892 odev->skb_rx_buf->protocol =
893 __constant_htons(ETH_P_IP);
895 odev->skb_rx_buf->ip_summed =
896 CHECKSUM_UNNECESSARY;
898 skb_reset_mac_header(odev->skb_rx_buf);
900 /* Ship it off to the kernel */
901 netif_rx(odev->skb_rx_buf);
902 /* No longer our buffer. */
903 odev->skb_rx_buf = NULL;
905 /* update out statistics */
906 odev->net->stats.rx_packets++;
908 odev->net->stats.rx_bytes += odev->rx_buf_size;
910 odev->rx_buf_size = 0;
911 odev->rx_buf_missing = sizeof(struct iphdr);
912 odev->rx_parse_state = WAIT_IP;
927 /* Recovery mechanism for WAIT_SYNC state. */
929 if (odev->rx_parse_state == WAIT_SYNC) {
930 odev->rx_parse_state = WAIT_IP;
931 odev->rx_buf_size = 0;
932 odev->rx_buf_missing = sizeof(struct iphdr);
937 /* Moving data from usb to kernel (in interrupt state) */
938 static void read_bulk_callback(struct urb *urb)
940 struct hso_net *odev = urb->context;
941 struct net_device *net;
943 int status = urb->status;
945 /* is al ok? (Filip: Who's Al ?) */
947 log_usb_status(status, __func__);
952 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
953 D1("BULK IN callback but driver is not active!");
956 usb_mark_last_busy(urb->dev);
960 if (!netif_device_present(net)) {
961 /* Somebody killed our network interface... */
965 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
967 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
968 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
969 if (((rest == 5) || (rest == 6))
970 && !memcmp(((u8 *) urb->transfer_buffer) +
971 urb->actual_length - 4, crc_check, 4)) {
972 urb->actual_length -= 4;
976 /* do we even have a packet? */
977 if (urb->actual_length) {
978 /* Handle the IP stream, add header and push it onto network
979 * stack if the packet is complete. */
980 spin_lock(&odev->net_lock);
981 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
982 (urb->transfer_buffer_length >
983 urb->actual_length) ? 1 : 0);
984 spin_unlock(&odev->net_lock);
987 /* We are done with this URB, resubmit it. Prep the USB to wait for
988 * another frame. Reuse same as received. */
989 usb_fill_bulk_urb(urb,
991 usb_rcvbulkpipe(odev->parent->usb,
993 bEndpointAddress & 0x7F),
994 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
995 read_bulk_callback, odev);
997 /* Give this to the USB subsystem so it can tell us when more data
999 result = usb_submit_urb(urb, GFP_ATOMIC);
1001 dev_warn(&odev->parent->interface->dev,
1002 "%s failed submit mux_bulk_rx_urb %d", __func__,
1006 /* Serial driver functions */
1008 static void _hso_serial_set_termios(struct tty_struct *tty,
1009 struct ktermios *old)
1011 struct hso_serial *serial = get_serial_by_tty(tty);
1012 struct ktermios *termios;
1014 if ((!tty) || (!tty->termios) || (!serial)) {
1015 printk(KERN_ERR "%s: no tty structures", __func__);
1019 D4("port %d", serial->minor);
1022 * The default requirements for this device are:
1024 termios = tty->termios;
1026 ~(IGNBRK /* disable ignore break */
1027 | BRKINT /* disable break causes interrupt */
1028 | PARMRK /* disable mark parity errors */
1029 | ISTRIP /* disable clear high bit of input characters */
1030 | INLCR /* disable translate NL to CR */
1031 | IGNCR /* disable ignore CR */
1032 | ICRNL /* disable translate CR to NL */
1033 | IXON); /* disable enable XON/XOFF flow control */
1035 /* disable postprocess output characters */
1036 termios->c_oflag &= ~OPOST;
1039 ~(ECHO /* disable echo input characters */
1040 | ECHONL /* disable echo new line */
1041 | ICANON /* disable erase, kill, werase, and rprnt
1042 special characters */
1043 | ISIG /* disable interrupt, quit, and suspend special
1045 | IEXTEN); /* disable non-POSIX special characters */
1048 ~(CSIZE /* no size */
1049 | PARENB /* disable parity bit */
1050 | CBAUD /* clear current baud rate */
1051 | CBAUDEX); /* clear current buad rate */
1053 termios->c_cflag |= CS8; /* character size 8 bits */
1055 /* baud rate 115200 */
1056 tty_encode_baud_rate(serial->tty, 115200, 115200);
1059 * Force low_latency on; otherwise the pushes are scheduled;
1060 * this is bad as it opens up the possibility of dropping bytes
1061 * on the floor. We don't want to drop bytes on the floor. :)
1063 serial->tty->low_latency = 1;
1067 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1070 #ifdef CONFIG_HSO_AUTOPM
1071 usb_mark_last_busy(urb->dev);
1073 /* We are done with this URB, resubmit it. Prep the USB to wait for
1075 usb_fill_bulk_urb(urb, serial->parent->usb,
1076 usb_rcvbulkpipe(serial->parent->usb,
1078 bEndpointAddress & 0x7F),
1079 urb->transfer_buffer, serial->rx_data_length,
1080 hso_std_serial_read_bulk_callback, serial);
1081 /* Give this to the USB subsystem so it can tell us when more data
1083 result = usb_submit_urb(urb, GFP_ATOMIC);
1085 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1093 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1096 struct urb *curr_urb;
1098 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1099 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1100 count = put_rxbuf_data(curr_urb, serial);
1104 serial->curr_rx_urb_idx++;
1105 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1106 serial->curr_rx_urb_idx = 0;
1107 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1112 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1117 urb = serial->rx_urb[0];
1118 if (serial->open_count > 0) {
1119 count = put_rxbuf_data(urb, serial);
1123 /* Re issue a read as long as we receive data. */
1125 if (count == 0 && ((urb->actual_length != 0) ||
1126 (serial->rx_state == RX_PENDING))) {
1127 serial->rx_state = RX_SENT;
1128 hso_mux_serial_read(serial);
1130 serial->rx_state = RX_IDLE;
1134 /* read callback for Diag and CS port */
1135 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1137 struct hso_serial *serial = urb->context;
1138 int status = urb->status;
1142 D1("serial == NULL");
1144 } else if (status) {
1145 log_usb_status(status, __func__);
1149 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1150 D1("Actual length = %d\n", urb->actual_length);
1151 DUMP1(urb->transfer_buffer, urb->actual_length);
1153 /* Anyone listening? */
1154 if (serial->open_count == 0)
1158 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1160 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1162 urb->actual_length %
1163 serial->in_endp->wMaxPacketSize;
1164 if (((rest == 5) || (rest == 6))
1165 && !memcmp(((u8 *) urb->transfer_buffer) +
1166 urb->actual_length - 4, crc_check, 4)) {
1167 urb->actual_length -= 4;
1170 /* Valid data, handle RX data */
1171 spin_lock(&serial->serial_lock);
1172 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1173 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1174 spin_unlock(&serial->serial_lock);
1175 } else if (status == -ENOENT || status == -ECONNRESET) {
1176 /* Unlinked - check for throttled port. */
1177 D2("Port %d, successfully unlinked urb", serial->minor);
1178 spin_lock(&serial->serial_lock);
1179 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1180 hso_resubmit_rx_bulk_urb(serial, urb);
1181 spin_unlock(&serial->serial_lock);
1183 D2("Port %d, status = %d for read urb", serial->minor, status);
1189 * This needs to be a tasklet otherwise we will
1190 * end up recursively calling this function.
1192 void hso_unthrottle_tasklet(struct hso_serial *serial)
1194 unsigned long flags;
1196 spin_lock_irqsave(&serial->serial_lock, flags);
1197 if ((serial->parent->port_spec & HSO_INTF_MUX))
1198 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1200 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1201 spin_unlock_irqrestore(&serial->serial_lock, flags);
1204 static void hso_unthrottle(struct tty_struct *tty)
1206 struct hso_serial *serial = get_serial_by_tty(tty);
1208 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1211 void hso_unthrottle_workfunc(struct work_struct *work)
1213 struct hso_serial *serial =
1214 container_of(work, struct hso_serial,
1215 retry_unthrottle_workqueue);
1216 hso_unthrottle_tasklet(serial);
1219 /* open the requested serial port */
1220 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1222 struct hso_serial *serial = get_serial_by_index(tty->index);
1226 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1227 tty->driver_data = NULL;
1228 D1("Failed to open port");
1232 mutex_lock(&serial->parent->mutex);
1233 result = usb_autopm_get_interface(serial->parent->interface);
1237 D1("Opening %d", serial->minor);
1238 kref_get(&serial->parent->ref);
1241 tty->driver_data = serial;
1244 /* check for port already opened, if not set the termios */
1245 serial->open_count++;
1246 if (serial->open_count == 1) {
1247 tty->low_latency = 1;
1248 serial->rx_state = RX_IDLE;
1249 /* Force default termio settings */
1250 _hso_serial_set_termios(tty, NULL);
1251 tasklet_init(&serial->unthrottle_tasklet,
1252 (void (*)(unsigned long))hso_unthrottle_tasklet,
1253 (unsigned long)serial);
1254 INIT_WORK(&serial->retry_unthrottle_workqueue,
1255 hso_unthrottle_workfunc);
1256 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1258 hso_stop_serial_device(serial->parent);
1259 serial->open_count--;
1260 kref_put(&serial->parent->ref, hso_serial_ref_free);
1263 D1("Port was already open");
1266 usb_autopm_put_interface(serial->parent->interface);
1270 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1272 mutex_unlock(&serial->parent->mutex);
1276 /* close the requested serial port */
1277 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1279 struct hso_serial *serial = tty->driver_data;
1282 D1("Closing serial port");
1284 mutex_lock(&serial->parent->mutex);
1285 usb_gone = serial->parent->usb_gone;
1288 usb_autopm_get_interface(serial->parent->interface);
1290 /* reset the rts and dtr */
1291 /* do the actual close */
1292 serial->open_count--;
1293 kref_put(&serial->parent->ref, hso_serial_ref_free);
1294 if (serial->open_count <= 0) {
1295 serial->open_count = 0;
1297 serial->tty->driver_data = NULL;
1301 hso_stop_serial_device(serial->parent);
1302 tasklet_kill(&serial->unthrottle_tasklet);
1303 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1307 usb_autopm_put_interface(serial->parent->interface);
1309 mutex_unlock(&serial->parent->mutex);
1312 /* close the requested serial port */
1313 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1316 struct hso_serial *serial = get_serial_by_tty(tty);
1317 int space, tx_bytes;
1318 unsigned long flags;
1321 if (serial == NULL) {
1322 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1326 spin_lock_irqsave(&serial->serial_lock, flags);
1328 space = serial->tx_data_length - serial->tx_buffer_count;
1329 tx_bytes = (count < space) ? count : space;
1334 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1335 serial->tx_buffer_count += tx_bytes;
1338 spin_unlock_irqrestore(&serial->serial_lock, flags);
1340 hso_kick_transmit(serial);
1345 /* how much room is there for writing */
1346 static int hso_serial_write_room(struct tty_struct *tty)
1348 struct hso_serial *serial = get_serial_by_tty(tty);
1350 unsigned long flags;
1352 spin_lock_irqsave(&serial->serial_lock, flags);
1353 room = serial->tx_data_length - serial->tx_buffer_count;
1354 spin_unlock_irqrestore(&serial->serial_lock, flags);
1356 /* return free room */
1360 /* setup the term */
1361 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1363 struct hso_serial *serial = get_serial_by_tty(tty);
1364 unsigned long flags;
1367 D5("Termios called with: cflags new[%d] - old[%d]",
1368 tty->termios->c_cflag, old->c_cflag);
1370 /* the actual setup */
1371 spin_lock_irqsave(&serial->serial_lock, flags);
1372 if (serial->open_count)
1373 _hso_serial_set_termios(tty, old);
1376 spin_unlock_irqrestore(&serial->serial_lock, flags);
1382 /* how many characters in the buffer */
1383 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1385 struct hso_serial *serial = get_serial_by_tty(tty);
1387 unsigned long flags;
1393 spin_lock_irqsave(&serial->serial_lock, flags);
1394 chars = serial->tx_buffer_count;
1395 spin_unlock_irqrestore(&serial->serial_lock, flags);
1400 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1403 struct hso_serial *serial = get_serial_by_tty(tty);
1404 unsigned long flags;
1408 D1("no tty structures");
1412 spin_lock_irqsave(&serial->serial_lock, flags);
1413 value = ((serial->rts_state) ? TIOCM_RTS : 0) |
1414 ((serial->dtr_state) ? TIOCM_DTR : 0);
1415 spin_unlock_irqrestore(&serial->serial_lock, flags);
1420 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1421 unsigned int set, unsigned int clear)
1424 unsigned long flags;
1426 struct hso_serial *serial = get_serial_by_tty(tty);
1430 D1("no tty structures");
1433 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1435 spin_lock_irqsave(&serial->serial_lock, flags);
1436 if (set & TIOCM_RTS)
1437 serial->rts_state = 1;
1438 if (set & TIOCM_DTR)
1439 serial->dtr_state = 1;
1441 if (clear & TIOCM_RTS)
1442 serial->rts_state = 0;
1443 if (clear & TIOCM_DTR)
1444 serial->dtr_state = 0;
1446 if (serial->dtr_state)
1448 if (serial->rts_state)
1451 spin_unlock_irqrestore(&serial->serial_lock, flags);
1453 return usb_control_msg(serial->parent->usb,
1454 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1455 0x21, val, if_num, NULL, 0,
1456 USB_CTRL_SET_TIMEOUT);
1459 /* starts a transmit */
1460 static void hso_kick_transmit(struct hso_serial *serial)
1463 unsigned long flags;
1466 spin_lock_irqsave(&serial->serial_lock, flags);
1467 if (!serial->tx_buffer_count)
1470 if (serial->tx_urb_used)
1473 /* Wakeup USB interface if necessary */
1474 if (hso_get_activity(serial->parent) == -EAGAIN)
1477 /* Switch pointers around to avoid memcpy */
1478 temp = serial->tx_buffer;
1479 serial->tx_buffer = serial->tx_data;
1480 serial->tx_data = temp;
1481 serial->tx_data_count = serial->tx_buffer_count;
1482 serial->tx_buffer_count = 0;
1484 /* If temp is set, it means we switched buffers */
1485 if (temp && serial->write_data) {
1486 res = serial->write_data(serial);
1488 serial->tx_urb_used = 1;
1491 spin_unlock_irqrestore(&serial->serial_lock, flags);
1494 /* make a request (for reading and writing data to muxed serial port) */
1495 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1496 struct urb *ctrl_urb,
1497 struct usb_ctrlrequest *ctrl_req,
1498 u8 *ctrl_urb_data, u32 size)
1504 if (!serial || !ctrl_urb || !ctrl_req) {
1505 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1510 ctrl_req->wValue = 0;
1511 ctrl_req->wIndex = hso_port_to_mux(port);
1512 ctrl_req->wLength = size;
1514 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1515 /* Reading command */
1516 ctrl_req->bRequestType = USB_DIR_IN |
1517 USB_TYPE_OPTION_VENDOR |
1518 USB_RECIP_INTERFACE;
1519 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1520 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1522 /* Writing command */
1523 ctrl_req->bRequestType = USB_DIR_OUT |
1524 USB_TYPE_OPTION_VENDOR |
1525 USB_RECIP_INTERFACE;
1526 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1527 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1530 D2("%s command (%02x) len: %d, port: %d",
1531 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1532 ctrl_req->bRequestType, ctrl_req->wLength, port);
1535 ctrl_urb->transfer_flags = 0;
1536 usb_fill_control_urb(ctrl_urb,
1537 serial->parent->usb,
1540 ctrl_urb_data, size, ctrl_callback, serial);
1541 /* Send it on merry way */
1542 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1544 dev_err(&ctrl_urb->dev->dev,
1545 "%s failed submit ctrl_urb %d type %d", __func__,
1554 /* called by intr_callback when read occurs */
1555 static int hso_mux_serial_read(struct hso_serial *serial)
1561 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1562 /* make the request */
1564 if (serial->num_rx_urbs != 1) {
1565 dev_err(&serial->parent->interface->dev,
1566 "ERROR: mux'd reads with multiple buffers "
1570 return mux_device_request(serial,
1571 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1572 serial->parent->port_spec & HSO_PORT_MASK,
1574 &serial->ctrl_req_rx,
1575 serial->rx_data[0], serial->rx_data_length);
1578 /* used for muxed serial port callback (muxed serial read) */
1579 static void intr_callback(struct urb *urb)
1581 struct hso_shared_int *shared_int = urb->context;
1582 struct hso_serial *serial;
1583 unsigned char *port_req;
1584 int status = urb->status;
1587 usb_mark_last_busy(urb->dev);
1595 log_usb_status(status, __func__);
1598 D4("\n--- Got intr callback 0x%02X ---", status);
1601 port_req = urb->transfer_buffer;
1602 D4(" port_req = 0x%.2X\n", *port_req);
1603 /* loop over all muxed ports to find the one sending this */
1604 for (i = 0; i < 8; i++) {
1605 /* max 8 channels on MUX */
1606 if (*port_req & (1 << i)) {
1607 serial = get_serial_by_shared_int_and_type(shared_int,
1609 if (serial != NULL) {
1610 D1("Pending read interrupt on port %d\n", i);
1611 spin_lock(&serial->serial_lock);
1612 if (serial->rx_state == RX_IDLE) {
1613 /* Setup and send a ctrl req read on
1615 if (!serial->rx_urb_filled[0]) {
1616 serial->rx_state = RX_SENT;
1617 hso_mux_serial_read(serial);
1619 serial->rx_state = RX_PENDING;
1622 D1("Already pending a read on "
1625 spin_unlock(&serial->serial_lock);
1629 /* Resubmit interrupt urb */
1630 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1633 /* called for writing to muxed serial port */
1634 static int hso_mux_serial_write_data(struct hso_serial *serial)
1639 return mux_device_request(serial,
1640 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1641 serial->parent->port_spec & HSO_PORT_MASK,
1643 &serial->ctrl_req_tx,
1644 serial->tx_data, serial->tx_data_count);
1647 /* write callback for Diag and CS port */
1648 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1650 struct hso_serial *serial = urb->context;
1651 int status = urb->status;
1655 D1("serial == NULL");
1659 spin_lock(&serial->serial_lock);
1660 serial->tx_urb_used = 0;
1661 spin_unlock(&serial->serial_lock);
1663 log_usb_status(status, __func__);
1666 hso_put_activity(serial->parent);
1668 tty_wakeup(serial->tty);
1669 hso_kick_transmit(serial);
1675 /* called for writing diag or CS serial port */
1676 static int hso_std_serial_write_data(struct hso_serial *serial)
1678 int count = serial->tx_data_count;
1681 usb_fill_bulk_urb(serial->tx_urb,
1682 serial->parent->usb,
1683 usb_sndbulkpipe(serial->parent->usb,
1685 bEndpointAddress & 0x7F),
1686 serial->tx_data, serial->tx_data_count,
1687 hso_std_serial_write_bulk_callback, serial);
1689 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1691 dev_warn(&serial->parent->usb->dev,
1692 "Failed to submit urb - res %d\n", result);
1699 /* callback after read or write on muxed serial port */
1700 static void ctrl_callback(struct urb *urb)
1702 struct hso_serial *serial = urb->context;
1703 struct usb_ctrlrequest *req;
1704 int status = urb->status;
1710 spin_lock(&serial->serial_lock);
1711 serial->tx_urb_used = 0;
1712 spin_unlock(&serial->serial_lock);
1714 log_usb_status(status, __func__);
1719 req = (struct usb_ctrlrequest *)(urb->setup_packet);
1720 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1721 D4("Actual length of urb = %d\n", urb->actual_length);
1722 DUMP1(urb->transfer_buffer, urb->actual_length);
1724 if (req->bRequestType ==
1725 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1726 /* response to a read command */
1727 serial->rx_urb_filled[0] = 1;
1728 spin_lock(&serial->serial_lock);
1729 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1730 spin_unlock(&serial->serial_lock);
1732 hso_put_activity(serial->parent);
1734 tty_wakeup(serial->tty);
1735 /* response to a write command */
1736 hso_kick_transmit(serial);
1740 /* handle RX data for serial port */
1741 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
1743 struct tty_struct *tty = serial->tty;
1744 int write_length_remaining = 0;
1747 if (urb == NULL || serial == NULL) {
1748 D1("serial = NULL");
1752 /* Push data to tty */
1754 write_length_remaining = urb->actual_length -
1755 serial->curr_rx_urb_offset;
1756 D1("data to push to tty");
1757 while (write_length_remaining) {
1758 if (test_bit(TTY_THROTTLED, &tty->flags))
1760 curr_write_len = tty_insert_flip_string
1761 (tty, urb->transfer_buffer +
1762 serial->curr_rx_urb_offset,
1763 write_length_remaining);
1764 serial->curr_rx_urb_offset += curr_write_len;
1765 write_length_remaining -= curr_write_len;
1766 tty_flip_buffer_push(tty);
1769 if (write_length_remaining == 0) {
1770 serial->curr_rx_urb_offset = 0;
1771 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1773 return write_length_remaining;
1777 /* Base driver functions */
1779 static void hso_log_port(struct hso_device *hso_dev)
1784 switch (hso_dev->port_spec & HSO_PORT_MASK) {
1785 case HSO_PORT_CONTROL:
1786 port_type = "Control";
1789 port_type = "Application";
1794 case HSO_PORT_GPS_CONTROL:
1795 port_type = "GPS control";
1798 port_type = "Application2";
1804 port_type = "Diagnostic";
1806 case HSO_PORT_DIAG2:
1807 port_type = "Diagnostic2";
1809 case HSO_PORT_MODEM:
1810 port_type = "Modem";
1812 case HSO_PORT_NETWORK:
1813 port_type = "Network";
1816 port_type = "Unknown";
1819 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
1820 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
1822 sprintf(port_dev, "/dev/%s%d", tty_filename,
1823 dev2ser(hso_dev)->minor);
1825 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
1826 port_type, port_dev);
1829 static int hso_start_net_device(struct hso_device *hso_dev)
1832 struct hso_net *hso_net = dev2net(hso_dev);
1837 /* send URBs for all read buffers */
1838 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1840 /* Prep a receive URB */
1841 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
1843 usb_rcvbulkpipe(hso_dev->usb,
1845 bEndpointAddress & 0x7F),
1846 hso_net->mux_bulk_rx_buf_pool[i],
1847 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
1850 /* Put it out there so the device can send us stuff */
1851 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
1854 dev_warn(&hso_dev->usb->dev,
1855 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
1862 static int hso_stop_net_device(struct hso_device *hso_dev)
1865 struct hso_net *hso_net = dev2net(hso_dev);
1870 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1871 if (hso_net->mux_bulk_rx_urb_pool[i])
1872 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1875 if (hso_net->mux_bulk_tx_urb)
1876 usb_kill_urb(hso_net->mux_bulk_tx_urb);
1881 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
1884 struct hso_serial *serial = dev2ser(hso_dev);
1889 /* If it is not the MUX port fill in and submit a bulk urb (already
1890 * allocated in hso_serial_start) */
1891 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
1892 for (i = 0; i < serial->num_rx_urbs; i++) {
1893 usb_fill_bulk_urb(serial->rx_urb[i],
1894 serial->parent->usb,
1895 usb_rcvbulkpipe(serial->parent->usb,
1900 serial->rx_data_length,
1901 hso_std_serial_read_bulk_callback,
1903 result = usb_submit_urb(serial->rx_urb[i], flags);
1905 dev_warn(&serial->parent->usb->dev,
1906 "Failed to submit urb - res %d\n",
1912 mutex_lock(&serial->shared_int->shared_int_lock);
1913 if (!serial->shared_int->use_count) {
1915 hso_mux_submit_intr_urb(serial->shared_int,
1916 hso_dev->usb, flags);
1918 serial->shared_int->use_count++;
1919 mutex_unlock(&serial->shared_int->shared_int_lock);
1925 static int hso_stop_serial_device(struct hso_device *hso_dev)
1928 struct hso_serial *serial = dev2ser(hso_dev);
1933 for (i = 0; i < serial->num_rx_urbs; i++) {
1934 if (serial->rx_urb[i]) {
1935 usb_kill_urb(serial->rx_urb[i]);
1936 serial->rx_urb_filled[i] = 0;
1939 serial->curr_rx_urb_idx = 0;
1940 serial->curr_rx_urb_offset = 0;
1943 usb_kill_urb(serial->tx_urb);
1945 if (serial->shared_int) {
1946 mutex_lock(&serial->shared_int->shared_int_lock);
1947 if (serial->shared_int->use_count &&
1948 (--serial->shared_int->use_count == 0)) {
1951 urb = serial->shared_int->shared_intr_urb;
1955 mutex_unlock(&serial->shared_int->shared_int_lock);
1961 static void hso_serial_common_free(struct hso_serial *serial)
1965 if (serial->parent->dev)
1966 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
1968 tty_unregister_device(tty_drv, serial->minor);
1970 for (i = 0; i < serial->num_rx_urbs; i++) {
1971 /* unlink and free RX URB */
1972 usb_free_urb(serial->rx_urb[i]);
1973 /* free the RX buffer */
1974 kfree(serial->rx_data[i]);
1977 /* unlink and free TX URB */
1978 usb_free_urb(serial->tx_urb);
1979 kfree(serial->tx_data);
1982 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
1983 int rx_size, int tx_size)
1989 minor = get_free_serial_index();
1993 /* register our minor number */
1994 serial->parent->dev = tty_register_device(tty_drv, minor,
1995 &serial->parent->interface->dev);
1996 dev = serial->parent->dev;
1997 dev->driver_data = serial->parent;
1998 i = device_create_file(dev, &dev_attr_hsotype);
2000 /* fill in specific data for later use */
2001 serial->minor = minor;
2002 serial->magic = HSO_SERIAL_MAGIC;
2003 spin_lock_init(&serial->serial_lock);
2004 serial->num_rx_urbs = num_urbs;
2006 /* RX, allocate urb and initialize */
2008 /* prepare our RX buffer */
2009 serial->rx_data_length = rx_size;
2010 for (i = 0; i < serial->num_rx_urbs; i++) {
2011 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2012 if (!serial->rx_urb[i]) {
2013 dev_err(dev, "Could not allocate urb?\n");
2016 serial->rx_urb[i]->transfer_buffer = NULL;
2017 serial->rx_urb[i]->transfer_buffer_length = 0;
2018 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2020 if (!serial->rx_data[i]) {
2021 dev_err(dev, "%s - Out of memory\n", __func__);
2026 /* TX, allocate urb and initialize */
2027 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2028 if (!serial->tx_urb) {
2029 dev_err(dev, "Could not allocate urb?\n");
2032 serial->tx_urb->transfer_buffer = NULL;
2033 serial->tx_urb->transfer_buffer_length = 0;
2034 /* prepare our TX buffer */
2035 serial->tx_data_count = 0;
2036 serial->tx_buffer_count = 0;
2037 serial->tx_data_length = tx_size;
2038 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2039 if (!serial->tx_data) {
2040 dev_err(dev, "%s - Out of memory", __func__);
2043 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2044 if (!serial->tx_buffer) {
2045 dev_err(dev, "%s - Out of memory", __func__);
2051 hso_serial_common_free(serial);
2055 /* Frees a general hso device */
2056 static void hso_free_device(struct hso_device *hso_dev)
2061 /* Creates a general hso device */
2062 static struct hso_device *hso_create_device(struct usb_interface *intf,
2065 struct hso_device *hso_dev;
2067 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2071 hso_dev->port_spec = port_spec;
2072 hso_dev->usb = interface_to_usbdev(intf);
2073 hso_dev->interface = intf;
2074 kref_init(&hso_dev->ref);
2075 mutex_init(&hso_dev->mutex);
2077 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2078 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2083 /* Removes a network device in the network device table */
2084 static int remove_net_device(struct hso_device *hso_dev)
2088 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2089 if (network_table[i] == hso_dev) {
2090 network_table[i] = NULL;
2094 if (i == HSO_MAX_NET_DEVICES)
2099 /* Frees our network device */
2100 static void hso_free_net_device(struct hso_device *hso_dev)
2103 struct hso_net *hso_net = dev2net(hso_dev);
2109 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2110 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2111 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2113 usb_free_urb(hso_net->mux_bulk_tx_urb);
2114 kfree(hso_net->mux_bulk_tx_buf);
2116 remove_net_device(hso_net->parent);
2119 unregister_netdev(hso_net->net);
2120 free_netdev(hso_net->net);
2123 hso_free_device(hso_dev);
2126 /* initialize the network interface */
2127 static void hso_net_init(struct net_device *net)
2129 struct hso_net *hso_net = netdev_priv(net);
2131 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2133 /* fill in the other fields */
2134 net->open = hso_net_open;
2135 net->stop = hso_net_close;
2136 net->hard_start_xmit = hso_net_start_xmit;
2137 net->tx_timeout = hso_net_tx_timeout;
2138 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2139 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2140 net->type = ARPHRD_NONE;
2141 net->mtu = DEFAULT_MTU - 14;
2142 net->tx_queue_len = 10;
2143 SET_ETHTOOL_OPS(net, &ops);
2145 /* and initialize the semaphore */
2146 spin_lock_init(&hso_net->net_lock);
2149 /* Adds a network device in the network device table */
2150 static int add_net_device(struct hso_device *hso_dev)
2154 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2155 if (network_table[i] == NULL) {
2156 network_table[i] = hso_dev;
2160 if (i == HSO_MAX_NET_DEVICES)
2165 static int hso_radio_toggle(void *data, enum rfkill_state state)
2167 struct hso_device *hso_dev = data;
2168 int enabled = (state == RFKILL_STATE_ON);
2171 mutex_lock(&hso_dev->mutex);
2172 if (hso_dev->usb_gone)
2175 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2176 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2177 USB_CTRL_SET_TIMEOUT);
2178 mutex_unlock(&hso_dev->mutex);
2182 /* Creates and sets up everything for rfkill */
2183 static void hso_create_rfkill(struct hso_device *hso_dev,
2184 struct usb_interface *interface)
2186 struct hso_net *hso_net = dev2net(hso_dev);
2187 struct device *dev = &hso_net->net->dev;
2190 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2192 if (!hso_net->rfkill) {
2193 dev_err(dev, "%s - Out of memory\n", __func__);
2196 rfkn = kzalloc(20, GFP_KERNEL);
2198 rfkill_free(hso_net->rfkill);
2199 hso_net->rfkill = NULL;
2200 dev_err(dev, "%s - Out of memory\n", __func__);
2203 snprintf(rfkn, 20, "hso-%d",
2204 interface->altsetting->desc.bInterfaceNumber);
2205 hso_net->rfkill->name = rfkn;
2206 hso_net->rfkill->state = RFKILL_STATE_ON;
2207 hso_net->rfkill->data = hso_dev;
2208 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2209 if (rfkill_register(hso_net->rfkill) < 0) {
2211 hso_net->rfkill->name = NULL;
2212 rfkill_free(hso_net->rfkill);
2213 hso_net->rfkill = NULL;
2214 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2219 /* Creates our network device */
2220 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2223 struct net_device *net;
2224 struct hso_net *hso_net;
2225 struct hso_device *hso_dev;
2227 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2231 /* allocate our network device, then we can put in our private data */
2232 /* call hso_net_init to do the basic initialization */
2233 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2235 dev_err(&interface->dev, "Unable to create ethernet device\n");
2239 hso_net = netdev_priv(net);
2241 hso_dev->port_data.dev_net = hso_net;
2243 hso_net->parent = hso_dev;
2245 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2247 if (!hso_net->in_endp) {
2248 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2251 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2253 if (!hso_net->out_endp) {
2254 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2257 SET_NETDEV_DEV(net, &interface->dev);
2259 /* registering our net device */
2260 result = register_netdev(net);
2262 dev_err(&interface->dev, "Failed to register device\n");
2266 /* start allocating */
2267 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2268 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2269 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2270 dev_err(&interface->dev, "Could not allocate rx urb\n");
2273 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2275 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2276 dev_err(&interface->dev, "Could not allocate rx buf\n");
2280 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2281 if (!hso_net->mux_bulk_tx_urb) {
2282 dev_err(&interface->dev, "Could not allocate tx urb\n");
2285 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2286 if (!hso_net->mux_bulk_tx_buf) {
2287 dev_err(&interface->dev, "Could not allocate tx buf\n");
2291 add_net_device(hso_dev);
2293 hso_log_port(hso_dev);
2295 hso_create_rfkill(hso_dev, interface);
2299 hso_free_net_device(hso_dev);
2303 /* Frees an AT channel ( goes for both mux and non-mux ) */
2304 static void hso_free_serial_device(struct hso_device *hso_dev)
2306 struct hso_serial *serial = dev2ser(hso_dev);
2310 set_serial_by_index(serial->minor, NULL);
2312 hso_serial_common_free(serial);
2314 if (serial->shared_int) {
2315 mutex_lock(&serial->shared_int->shared_int_lock);
2316 if (--serial->shared_int->ref_count == 0)
2317 hso_free_shared_int(serial->shared_int);
2319 mutex_unlock(&serial->shared_int->shared_int_lock);
2322 hso_free_device(hso_dev);
2325 /* Creates a bulk AT channel */
2326 static struct hso_device *hso_create_bulk_serial_device(
2327 struct usb_interface *interface, int port)
2329 struct hso_device *hso_dev;
2330 struct hso_serial *serial;
2333 hso_dev = hso_create_device(interface, port);
2337 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2341 serial->parent = hso_dev;
2342 hso_dev->port_data.dev_serial = serial;
2344 if (port & HSO_PORT_MODEM)
2349 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2353 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2355 if (!serial->in_endp) {
2356 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2362 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2363 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2367 serial->write_data = hso_std_serial_write_data;
2369 /* and record this serial */
2370 set_serial_by_index(serial->minor, serial);
2372 /* setup the proc dirs and files if needed */
2373 hso_log_port(hso_dev);
2375 /* done, return it */
2379 hso_serial_common_free(serial);
2382 hso_free_device(hso_dev);
2386 /* Creates a multiplexed AT channel */
2388 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2390 struct hso_shared_int *mux)
2392 struct hso_device *hso_dev;
2393 struct hso_serial *serial;
2396 port_spec = HSO_INTF_MUX;
2397 port_spec &= ~HSO_PORT_MASK;
2399 port_spec |= hso_mux_to_port(port);
2400 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2403 hso_dev = hso_create_device(interface, port_spec);
2407 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2411 hso_dev->port_data.dev_serial = serial;
2412 serial->parent = hso_dev;
2414 if (hso_serial_common_create
2415 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2418 serial->tx_data_length--;
2419 serial->write_data = hso_mux_serial_write_data;
2421 serial->shared_int = mux;
2422 mutex_lock(&serial->shared_int->shared_int_lock);
2423 serial->shared_int->ref_count++;
2424 mutex_unlock(&serial->shared_int->shared_int_lock);
2426 /* and record this serial */
2427 set_serial_by_index(serial->minor, serial);
2429 /* setup the proc dirs and files if needed */
2430 hso_log_port(hso_dev);
2432 /* done, return it */
2437 tty_unregister_device(tty_drv, serial->minor);
2441 hso_free_device(hso_dev);
2446 static void hso_free_shared_int(struct hso_shared_int *mux)
2448 usb_free_urb(mux->shared_intr_urb);
2449 kfree(mux->shared_intr_buf);
2450 mutex_unlock(&mux->shared_int_lock);
2455 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2457 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2462 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2464 if (!mux->intr_endp) {
2465 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2469 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2470 if (!mux->shared_intr_urb) {
2471 dev_err(&interface->dev, "Could not allocate intr urb?");
2474 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2476 if (!mux->shared_intr_buf) {
2477 dev_err(&interface->dev, "Could not allocate intr buf?");
2481 mutex_init(&mux->shared_int_lock);
2486 kfree(mux->shared_intr_buf);
2487 usb_free_urb(mux->shared_intr_urb);
2492 /* Gets the port spec for a certain interface */
2493 static int hso_get_config_data(struct usb_interface *interface)
2495 struct usb_device *usbdev = interface_to_usbdev(interface);
2497 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2500 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2501 0x86, 0xC0, 0, 0, config_data, 17,
2502 USB_CTRL_SET_TIMEOUT) != 0x11) {
2506 switch (config_data[if_num]) {
2511 result = HSO_PORT_DIAG;
2514 result = HSO_PORT_GPS;
2517 result = HSO_PORT_GPS_CONTROL;
2520 result = HSO_PORT_APP;
2523 result = HSO_PORT_APP2;
2526 result = HSO_PORT_CONTROL;
2529 result = HSO_PORT_NETWORK;
2532 result = HSO_PORT_MODEM;
2535 result = HSO_PORT_MSD;
2538 result = HSO_PORT_PCSC;
2541 result = HSO_PORT_VOICE;
2548 result |= HSO_INTF_BULK;
2550 if (config_data[16] & 0x1)
2551 result |= HSO_INFO_CRC_BUG;
2556 /* called once for each interface upon device insertion */
2557 static int hso_probe(struct usb_interface *interface,
2558 const struct usb_device_id *id)
2560 int mux, i, if_num, port_spec;
2561 unsigned char port_mask;
2562 struct hso_device *hso_dev = NULL;
2563 struct hso_shared_int *shared_int;
2564 struct hso_device *tmp_dev = NULL;
2566 if_num = interface->altsetting->desc.bInterfaceNumber;
2568 /* Get the interface/port specification from either driver_info or from
2569 * the device itself */
2570 if (id->driver_info)
2571 port_spec = ((u32 *)(id->driver_info))[if_num];
2573 port_spec = hso_get_config_data(interface);
2575 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2576 dev_err(&interface->dev, "Not our interface\n");
2579 /* Check if we need to switch to alt interfaces prior to port
2581 if (interface->num_altsetting > 1)
2582 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2583 interface->needs_remote_wakeup = 1;
2585 /* Allocate new hso device(s) */
2586 switch (port_spec & HSO_INTF_MASK) {
2588 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2589 /* Create the network device */
2591 hso_dev = hso_create_net_device(interface);
2598 if (hso_get_mux_ports(interface, &port_mask))
2599 /* TODO: de-allocate everything */
2602 shared_int = hso_create_shared_int(interface);
2606 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2607 if (port_mask & i) {
2608 hso_dev = hso_create_mux_serial_device(
2609 interface, i, shared_int);
2620 /* It's a regular bulk interface */
2621 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2623 hso_dev = hso_create_net_device(interface);
2626 hso_create_bulk_serial_device(interface, port_spec);
2634 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2636 /* save our data pointer in this device */
2637 usb_set_intfdata(interface, hso_dev);
2642 hso_free_interface(interface);
2646 /* device removed, cleaning up */
2647 static void hso_disconnect(struct usb_interface *interface)
2649 hso_free_interface(interface);
2651 /* remove reference of our private data */
2652 usb_set_intfdata(interface, NULL);
2654 usb_driver_release_interface(&hso_driver, interface);
2657 static void async_get_intf(struct work_struct *data)
2659 struct hso_device *hso_dev =
2660 container_of(data, struct hso_device, async_get_intf);
2661 usb_autopm_get_interface(hso_dev->interface);
2664 static void async_put_intf(struct work_struct *data)
2666 struct hso_device *hso_dev =
2667 container_of(data, struct hso_device, async_put_intf);
2668 usb_autopm_put_interface(hso_dev->interface);
2671 static int hso_get_activity(struct hso_device *hso_dev)
2673 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2674 if (!hso_dev->is_active) {
2675 hso_dev->is_active = 1;
2676 schedule_work(&hso_dev->async_get_intf);
2680 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2683 usb_mark_last_busy(hso_dev->usb);
2688 static int hso_put_activity(struct hso_device *hso_dev)
2690 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2691 if (hso_dev->is_active) {
2692 hso_dev->is_active = 0;
2693 schedule_work(&hso_dev->async_put_intf);
2697 hso_dev->is_active = 0;
2701 /* called by kernel when we need to suspend device */
2702 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2706 /* Stop all serial ports */
2707 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2708 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2709 result = hso_stop_serial_device(serial_table[i]);
2715 /* Stop all network ports */
2716 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2717 if (network_table[i] &&
2718 (network_table[i]->interface == iface)) {
2719 result = hso_stop_net_device(network_table[i]);
2729 /* called by kernel when we need to resume device */
2730 static int hso_resume(struct usb_interface *iface)
2733 struct hso_net *hso_net;
2735 /* Start all serial ports */
2736 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2737 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2738 if (dev2ser(serial_table[i])->open_count) {
2740 hso_start_serial_device(serial_table[i], GFP_NOIO);
2741 hso_kick_transmit(dev2ser(serial_table[i]));
2748 /* Start all network ports */
2749 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2750 if (network_table[i] &&
2751 (network_table[i]->interface == iface)) {
2752 hso_net = dev2net(network_table[i]);
2753 /* First transmit any lingering data, then restart the
2755 if (hso_net->skb_tx_buf) {
2756 dev_dbg(&iface->dev,
2757 "Transmitting lingering data\n");
2758 hso_net_start_xmit(hso_net->skb_tx_buf,
2760 hso_net->skb_tx_buf = NULL;
2762 result = hso_start_net_device(network_table[i]);
2772 static void hso_serial_ref_free(struct kref *ref)
2774 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2776 hso_free_serial_device(hso_dev);
2779 static void hso_free_interface(struct usb_interface *interface)
2781 struct hso_serial *hso_dev;
2784 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2786 && (serial_table[i]->interface == interface)) {
2787 hso_dev = dev2ser(serial_table[i]);
2789 tty_hangup(hso_dev->tty);
2790 mutex_lock(&hso_dev->parent->mutex);
2791 hso_dev->parent->usb_gone = 1;
2792 mutex_unlock(&hso_dev->parent->mutex);
2793 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2797 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2798 if (network_table[i]
2799 && (network_table[i]->interface == interface)) {
2800 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2801 /* hso_stop_net_device doesn't stop the net queue since
2802 * traffic needs to start it again when suspended */
2803 netif_stop_queue(dev2net(network_table[i])->net);
2804 hso_stop_net_device(network_table[i]);
2805 cancel_work_sync(&network_table[i]->async_put_intf);
2806 cancel_work_sync(&network_table[i]->async_get_intf);
2808 rfkill_unregister(rfk);
2809 hso_free_net_device(network_table[i]);
2814 /* Helper functions */
2816 /* Get the endpoint ! */
2817 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2821 struct usb_host_interface *iface = intf->cur_altsetting;
2822 struct usb_endpoint_descriptor *endp;
2824 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2825 endp = &iface->endpoint[i].desc;
2826 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2827 ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
2834 /* Get the byte that describes which ports are enabled */
2835 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2838 struct usb_host_interface *iface = intf->cur_altsetting;
2840 if (iface->extralen == 3) {
2841 *ports = iface->extra[2];
2845 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2846 if (iface->endpoint[i].extralen == 3) {
2847 *ports = iface->endpoint[i].extra[2];
2855 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2856 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2857 struct usb_device *usb, gfp_t gfp)
2861 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2863 shared_int->intr_endp->bEndpointAddress & 0x7F),
2864 shared_int->shared_intr_buf,
2865 shared_int->intr_endp->wMaxPacketSize,
2866 intr_callback, shared_int,
2867 shared_int->intr_endp->bInterval);
2869 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2871 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2877 /* operations setup of the serial interface */
2878 static const struct tty_operations hso_serial_ops = {
2879 .open = hso_serial_open,
2880 .close = hso_serial_close,
2881 .write = hso_serial_write,
2882 .write_room = hso_serial_write_room,
2883 .set_termios = hso_serial_set_termios,
2884 .chars_in_buffer = hso_serial_chars_in_buffer,
2885 .tiocmget = hso_serial_tiocmget,
2886 .tiocmset = hso_serial_tiocmset,
2887 .unthrottle = hso_unthrottle
2890 static struct usb_driver hso_driver = {
2891 .name = driver_name,
2893 .disconnect = hso_disconnect,
2894 .id_table = hso_ids,
2895 .suspend = hso_suspend,
2896 .resume = hso_resume,
2897 .supports_autosuspend = 1,
2900 static int __init hso_init(void)
2905 /* put it in the log */
2906 printk(KERN_INFO "hso: %s\n", version);
2908 /* Initialise the serial table semaphore and table */
2909 spin_lock_init(&serial_table_lock);
2910 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2911 serial_table[i] = NULL;
2913 /* allocate our driver using the proper amount of supported minors */
2914 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2918 /* fill in all needed values */
2919 tty_drv->magic = TTY_DRIVER_MAGIC;
2920 tty_drv->owner = THIS_MODULE;
2921 tty_drv->driver_name = driver_name;
2922 tty_drv->name = tty_filename;
2924 /* if major number is provided as parameter, use that one */
2926 tty_drv->major = tty_major;
2928 tty_drv->minor_start = 0;
2929 tty_drv->num = HSO_SERIAL_TTY_MINORS;
2930 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2931 tty_drv->subtype = SERIAL_TYPE_NORMAL;
2932 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2933 tty_drv->init_termios = tty_std_termios;
2934 tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2935 tty_drv->termios = hso_serial_termios;
2936 tty_drv->termios_locked = hso_serial_termios_locked;
2937 tty_set_operations(tty_drv, &hso_serial_ops);
2939 /* register the tty driver */
2940 result = tty_register_driver(tty_drv);
2942 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2947 /* register this module as an usb driver */
2948 result = usb_register(&hso_driver);
2950 printk(KERN_ERR "Could not register hso driver? error: %d\n",
2952 /* cleanup serial interface */
2953 tty_unregister_driver(tty_drv);
2961 static void __exit hso_exit(void)
2963 printk(KERN_INFO "hso: unloaded\n");
2965 tty_unregister_driver(tty_drv);
2966 /* deregister the usb driver */
2967 usb_deregister(&hso_driver);
2970 /* Module definitions */
2971 module_init(hso_init);
2972 module_exit(hso_exit);
2974 MODULE_AUTHOR(MOD_AUTHOR);
2975 MODULE_DESCRIPTION(MOD_DESCRIPTION);
2976 MODULE_LICENSE(MOD_LICENSE);
2977 MODULE_INFO(Version, DRIVER_VERSION);
2979 /* change the debug level (eg: insmod hso.ko debug=0x04) */
2980 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2981 module_param(debug, int, S_IRUGO | S_IWUSR);
2983 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
2984 MODULE_PARM_DESC(tty_major, "Set the major tty number");
2985 module_param(tty_major, int, S_IRUGO | S_IWUSR);
2987 /* disable network interface (eg: insmod hso.ko disable_net=1) */
2988 MODULE_PARM_DESC(disable_net, "Disable the network interface");
2989 module_param(disable_net, int, S_IRUGO | S_IWUSR);