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;
366 static const s32 default_port_spec[] = {
367 HSO_INTF_MUX | HSO_PORT_NETWORK,
368 HSO_INTF_BULK | HSO_PORT_DIAG,
369 HSO_INTF_BULK | HSO_PORT_MODEM,
373 static const s32 icon321_port_spec[] = {
374 HSO_INTF_MUX | HSO_PORT_NETWORK,
375 HSO_INTF_BULK | HSO_PORT_DIAG2,
376 HSO_INTF_BULK | HSO_PORT_MODEM,
377 HSO_INTF_BULK | HSO_PORT_DIAG,
381 #define default_port_device(vendor, product) \
382 USB_DEVICE(vendor, product), \
383 .driver_info = (kernel_ulong_t)default_port_spec
385 #define icon321_port_device(vendor, product) \
386 USB_DEVICE(vendor, product), \
387 .driver_info = (kernel_ulong_t)icon321_port_spec
389 /* list of devices we support */
390 static const struct usb_device_id hso_ids[] = {
391 {default_port_device(0x0af0, 0x6711)},
392 {default_port_device(0x0af0, 0x6731)},
393 {default_port_device(0x0af0, 0x6751)},
394 {default_port_device(0x0af0, 0x6771)},
395 {default_port_device(0x0af0, 0x6791)},
396 {default_port_device(0x0af0, 0x6811)},
397 {default_port_device(0x0af0, 0x6911)},
398 {default_port_device(0x0af0, 0x6951)},
399 {default_port_device(0x0af0, 0x6971)},
400 {default_port_device(0x0af0, 0x7011)},
401 {default_port_device(0x0af0, 0x7031)},
402 {default_port_device(0x0af0, 0x7051)},
403 {default_port_device(0x0af0, 0x7071)},
404 {default_port_device(0x0af0, 0x7111)},
405 {default_port_device(0x0af0, 0x7211)},
406 {default_port_device(0x0af0, 0x7251)},
407 {default_port_device(0x0af0, 0x7271)},
408 {default_port_device(0x0af0, 0x7311)},
409 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
410 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
411 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
412 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
413 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
414 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
415 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
416 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
417 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
418 {USB_DEVICE(0x0af0, 0x7701)},
419 {USB_DEVICE(0x0af0, 0x7801)},
420 {USB_DEVICE(0x0af0, 0x7901)},
421 {USB_DEVICE(0x0af0, 0x7361)},
422 {icon321_port_device(0x0af0, 0xd051)},
425 MODULE_DEVICE_TABLE(usb, hso_ids);
427 /* Sysfs attribute */
428 static ssize_t hso_sysfs_show_porttype(struct device *dev,
429 struct device_attribute *attr,
432 struct hso_device *hso_dev = dev->driver_data;
438 switch (hso_dev->port_spec & HSO_PORT_MASK) {
439 case HSO_PORT_CONTROL:
440 port_name = "Control";
443 port_name = "Application";
446 port_name = "Application2";
451 case HSO_PORT_GPS_CONTROL:
452 port_name = "GPS Control";
458 port_name = "Diagnostic";
461 port_name = "Diagnostic2";
466 case HSO_PORT_NETWORK:
467 port_name = "Network";
470 port_name = "Unknown";
474 return sprintf(buf, "%s\n", port_name);
476 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
478 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
482 for (idx = 0; idx < serial->num_rx_urbs; idx++)
483 if (serial->rx_urb[idx] == urb)
485 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
489 /* converts mux value to a port spec value */
490 static u32 hso_mux_to_port(int mux)
496 result = HSO_PORT_CONTROL;
499 result = HSO_PORT_APP;
502 result = HSO_PORT_PCSC;
505 result = HSO_PORT_GPS;
508 result = HSO_PORT_APP2;
511 result = HSO_PORT_NO_PORT;
516 /* converts port spec value to a mux value */
517 static u32 hso_port_to_mux(int port)
521 switch (port & HSO_PORT_MASK) {
522 case HSO_PORT_CONTROL:
543 static struct hso_serial *get_serial_by_shared_int_and_type(
544 struct hso_shared_int *shared_int,
549 port = hso_mux_to_port(mux);
551 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
553 && (dev2ser(serial_table[i])->shared_int == shared_int)
554 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
555 return dev2ser(serial_table[i]);
562 static struct hso_serial *get_serial_by_index(unsigned index)
564 struct hso_serial *serial = NULL;
567 spin_lock_irqsave(&serial_table_lock, flags);
568 if (serial_table[index])
569 serial = dev2ser(serial_table[index]);
570 spin_unlock_irqrestore(&serial_table_lock, flags);
575 static int get_free_serial_index(void)
580 spin_lock_irqsave(&serial_table_lock, flags);
581 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
582 if (serial_table[index] == NULL) {
583 spin_unlock_irqrestore(&serial_table_lock, flags);
587 spin_unlock_irqrestore(&serial_table_lock, flags);
589 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
593 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
597 spin_lock_irqsave(&serial_table_lock, flags);
599 serial_table[index] = serial->parent;
601 serial_table[index] = NULL;
602 spin_unlock_irqrestore(&serial_table_lock, flags);
605 /* log a meaningful explanation of an USB status */
606 static void log_usb_status(int status, const char *function)
612 explanation = "no device";
615 explanation = "endpoint not enabled";
618 explanation = "endpoint stalled";
621 explanation = "not enough bandwidth";
624 explanation = "device disabled";
627 explanation = "device suspended";
633 explanation = "internal error";
636 explanation = "unknown status";
639 D1("%s: received USB status - %s (%d)", function, explanation, status);
642 /* Network interface functions */
644 /* called when net interface is brought up by ifconfig */
645 static int hso_net_open(struct net_device *net)
647 struct hso_net *odev = netdev_priv(net);
648 unsigned long flags = 0;
651 dev_err(&net->dev, "No net device !\n");
655 odev->skb_tx_buf = NULL;
657 /* setup environment */
658 spin_lock_irqsave(&odev->net_lock, flags);
659 odev->rx_parse_state = WAIT_IP;
660 odev->rx_buf_size = 0;
661 odev->rx_buf_missing = sizeof(struct iphdr);
662 spin_unlock_irqrestore(&odev->net_lock, flags);
664 /* We are up and running. */
665 set_bit(HSO_NET_RUNNING, &odev->flags);
666 hso_start_net_device(odev->parent);
668 /* Tell the kernel we are ready to start receiving from it */
669 netif_start_queue(net);
674 /* called when interface is brought down by ifconfig */
675 static int hso_net_close(struct net_device *net)
677 struct hso_net *odev = netdev_priv(net);
679 /* we don't need the queue anymore */
680 netif_stop_queue(net);
681 /* no longer running */
682 clear_bit(HSO_NET_RUNNING, &odev->flags);
684 hso_stop_net_device(odev->parent);
690 /* USB tells is xmit done, we should start the netqueue again */
691 static void write_bulk_callback(struct urb *urb)
693 struct hso_net *odev = urb->context;
694 int status = urb->status;
697 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
698 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
702 /* Do we still have a valid kernel network device? */
703 if (!netif_device_present(odev->net)) {
704 dev_err(&urb->dev->dev, "%s: net device not present\n",
709 /* log status, but don't act on it, we don't need to resubmit anything
712 log_usb_status(status, __func__);
714 hso_put_activity(odev->parent);
716 /* Tell the network interface we are ready for another frame */
717 netif_wake_queue(odev->net);
720 /* called by kernel when we need to transmit a packet */
721 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
723 struct hso_net *odev = netdev_priv(net);
726 /* Tell the kernel, "No more frames 'til we are done with this one." */
727 netif_stop_queue(net);
728 if (hso_get_activity(odev->parent) == -EAGAIN) {
729 odev->skb_tx_buf = skb;
734 DUMP1(skb->data, skb->len);
735 /* Copy it from kernel memory to OUR memory */
736 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
737 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
739 /* Fill in the URB for shipping it out. */
740 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
742 usb_sndbulkpipe(odev->parent->usb,
744 bEndpointAddress & 0x7F),
745 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
748 /* Deal with the Zero Length packet problem, I hope */
749 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
751 /* Send the URB on its merry way. */
752 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
754 dev_warn(&odev->parent->interface->dev,
755 "failed mux_bulk_tx_urb %d", result);
756 net->stats.tx_errors++;
757 netif_start_queue(net);
759 net->stats.tx_packets++;
760 net->stats.tx_bytes += skb->len;
761 /* And tell the kernel when the last transmit started. */
762 net->trans_start = jiffies;
769 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
771 struct hso_net *odev = netdev_priv(net);
773 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
774 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
775 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
778 static struct ethtool_ops ops = {
779 .get_drvinfo = hso_get_drvinfo,
780 .get_link = ethtool_op_get_link
783 /* called when a packet did not ack after watchdogtimeout */
784 static void hso_net_tx_timeout(struct net_device *net)
786 struct hso_net *odev = netdev_priv(net);
791 /* Tell syslog we are hosed. */
792 dev_warn(&net->dev, "Tx timed out.\n");
794 /* Tear the waiting frame off the list */
795 if (odev->mux_bulk_tx_urb
796 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
797 usb_unlink_urb(odev->mux_bulk_tx_urb);
799 /* Update statistics */
800 net->stats.tx_errors++;
803 /* make a real packet from the received USB buffer */
804 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
805 unsigned int count, unsigned char is_eop)
807 unsigned short temp_bytes;
808 unsigned short buffer_offset = 0;
809 unsigned short frame_len;
810 unsigned char *tmp_rx_buf;
813 D1("Rx %d bytes", count);
814 DUMP(ip_pkt, min(128, (int)count));
817 switch (odev->rx_parse_state) {
819 /* waiting for IP header. */
820 /* wanted bytes - size of ip header */
823 odev->rx_buf_missing) ? count : odev->
826 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
827 odev->rx_buf_size, ip_pkt + buffer_offset,
830 odev->rx_buf_size += temp_bytes;
831 buffer_offset += temp_bytes;
832 odev->rx_buf_missing -= temp_bytes;
835 if (!odev->rx_buf_missing) {
836 /* header is complete allocate an sk_buffer and
837 * continue to WAIT_DATA */
838 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
840 if ((frame_len > DEFAULT_MRU) ||
841 (frame_len < sizeof(struct iphdr))) {
842 dev_err(&odev->net->dev,
843 "Invalid frame (%d) length\n",
845 odev->rx_parse_state = WAIT_SYNC;
848 /* Allocate an sk_buff */
849 odev->skb_rx_buf = dev_alloc_skb(frame_len);
850 if (!odev->skb_rx_buf) {
851 /* We got no receive buffer. */
852 D1("could not allocate memory");
853 odev->rx_parse_state = WAIT_SYNC;
856 /* Here's where it came from */
857 odev->skb_rx_buf->dev = odev->net;
859 /* Copy what we got so far. make room for iphdr
862 skb_put(odev->skb_rx_buf,
863 sizeof(struct iphdr));
864 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
865 sizeof(struct iphdr));
868 odev->rx_buf_size = sizeof(struct iphdr);
870 /* Filip actually use .tot_len */
871 odev->rx_buf_missing =
872 frame_len - sizeof(struct iphdr);
873 odev->rx_parse_state = WAIT_DATA;
878 temp_bytes = (count < odev->rx_buf_missing)
879 ? count : odev->rx_buf_missing;
881 /* Copy the rest of the bytes that are left in the
882 * buffer into the waiting sk_buf. */
883 /* Make room for temp_bytes after tail. */
884 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
885 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
887 odev->rx_buf_missing -= temp_bytes;
889 buffer_offset += temp_bytes;
890 odev->rx_buf_size += temp_bytes;
891 if (!odev->rx_buf_missing) {
892 /* Packet is complete. Inject into stack. */
893 /* We have IP packet here */
894 odev->skb_rx_buf->protocol =
895 __constant_htons(ETH_P_IP);
897 odev->skb_rx_buf->ip_summed =
898 CHECKSUM_UNNECESSARY;
900 skb_reset_mac_header(odev->skb_rx_buf);
902 /* Ship it off to the kernel */
903 netif_rx(odev->skb_rx_buf);
904 /* No longer our buffer. */
905 odev->skb_rx_buf = NULL;
907 /* update out statistics */
908 odev->net->stats.rx_packets++;
910 odev->net->stats.rx_bytes += odev->rx_buf_size;
912 odev->rx_buf_size = 0;
913 odev->rx_buf_missing = sizeof(struct iphdr);
914 odev->rx_parse_state = WAIT_IP;
929 /* Recovery mechanism for WAIT_SYNC state. */
931 if (odev->rx_parse_state == WAIT_SYNC) {
932 odev->rx_parse_state = WAIT_IP;
933 odev->rx_buf_size = 0;
934 odev->rx_buf_missing = sizeof(struct iphdr);
939 /* Moving data from usb to kernel (in interrupt state) */
940 static void read_bulk_callback(struct urb *urb)
942 struct hso_net *odev = urb->context;
943 struct net_device *net;
945 int status = urb->status;
947 /* is al ok? (Filip: Who's Al ?) */
949 log_usb_status(status, __func__);
954 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
955 D1("BULK IN callback but driver is not active!");
958 usb_mark_last_busy(urb->dev);
962 if (!netif_device_present(net)) {
963 /* Somebody killed our network interface... */
967 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
969 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
970 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
971 if (((rest == 5) || (rest == 6))
972 && !memcmp(((u8 *) urb->transfer_buffer) +
973 urb->actual_length - 4, crc_check, 4)) {
974 urb->actual_length -= 4;
978 /* do we even have a packet? */
979 if (urb->actual_length) {
980 /* Handle the IP stream, add header and push it onto network
981 * stack if the packet is complete. */
982 spin_lock(&odev->net_lock);
983 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
984 (urb->transfer_buffer_length >
985 urb->actual_length) ? 1 : 0);
986 spin_unlock(&odev->net_lock);
989 /* We are done with this URB, resubmit it. Prep the USB to wait for
990 * another frame. Reuse same as received. */
991 usb_fill_bulk_urb(urb,
993 usb_rcvbulkpipe(odev->parent->usb,
995 bEndpointAddress & 0x7F),
996 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
997 read_bulk_callback, odev);
999 /* Give this to the USB subsystem so it can tell us when more data
1001 result = usb_submit_urb(urb, GFP_ATOMIC);
1003 dev_warn(&odev->parent->interface->dev,
1004 "%s failed submit mux_bulk_rx_urb %d", __func__,
1008 /* Serial driver functions */
1010 static void hso_init_termios(struct ktermios *termios)
1013 * The default requirements for this device are:
1016 ~(IGNBRK /* disable ignore break */
1017 | BRKINT /* disable break causes interrupt */
1018 | PARMRK /* disable mark parity errors */
1019 | ISTRIP /* disable clear high bit of input characters */
1020 | INLCR /* disable translate NL to CR */
1021 | IGNCR /* disable ignore CR */
1022 | ICRNL /* disable translate CR to NL */
1023 | IXON); /* disable enable XON/XOFF flow control */
1025 /* disable postprocess output characters */
1026 termios->c_oflag &= ~OPOST;
1029 ~(ECHO /* disable echo input characters */
1030 | ECHONL /* disable echo new line */
1031 | ICANON /* disable erase, kill, werase, and rprnt
1032 special characters */
1033 | ISIG /* disable interrupt, quit, and suspend special
1035 | IEXTEN); /* disable non-POSIX special characters */
1038 ~(CSIZE /* no size */
1039 | PARENB /* disable parity bit */
1040 | CBAUD /* clear current baud rate */
1041 | CBAUDEX); /* clear current buad rate */
1043 termios->c_cflag |= CS8; /* character size 8 bits */
1045 /* baud rate 115200 */
1046 tty_termios_encode_baud_rate(termios, 115200, 115200);
1049 static void _hso_serial_set_termios(struct tty_struct *tty,
1050 struct ktermios *old)
1052 struct hso_serial *serial = get_serial_by_tty(tty);
1053 struct ktermios *termios;
1056 printk(KERN_ERR "%s: no tty structures", __func__);
1060 D4("port %d", serial->minor);
1063 * Fix up unsupported bits
1065 termios = tty->termios;
1066 termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1069 ~(CSIZE /* no size */
1070 | PARENB /* disable parity bit */
1071 | CBAUD /* clear current baud rate */
1072 | CBAUDEX); /* clear current buad rate */
1074 termios->c_cflag |= CS8; /* character size 8 bits */
1076 /* baud rate 115200 */
1077 tty_encode_baud_rate(tty, 115200, 115200);
1080 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1083 #ifdef CONFIG_HSO_AUTOPM
1084 usb_mark_last_busy(urb->dev);
1086 /* We are done with this URB, resubmit it. Prep the USB to wait for
1088 usb_fill_bulk_urb(urb, serial->parent->usb,
1089 usb_rcvbulkpipe(serial->parent->usb,
1091 bEndpointAddress & 0x7F),
1092 urb->transfer_buffer, serial->rx_data_length,
1093 hso_std_serial_read_bulk_callback, serial);
1094 /* Give this to the USB subsystem so it can tell us when more data
1096 result = usb_submit_urb(urb, GFP_ATOMIC);
1098 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1106 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1109 struct urb *curr_urb;
1111 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1112 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1113 count = put_rxbuf_data(curr_urb, serial);
1117 serial->curr_rx_urb_idx++;
1118 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1119 serial->curr_rx_urb_idx = 0;
1120 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1125 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1130 urb = serial->rx_urb[0];
1131 if (serial->open_count > 0) {
1132 count = put_rxbuf_data(urb, serial);
1136 /* Re issue a read as long as we receive data. */
1138 if (count == 0 && ((urb->actual_length != 0) ||
1139 (serial->rx_state == RX_PENDING))) {
1140 serial->rx_state = RX_SENT;
1141 hso_mux_serial_read(serial);
1143 serial->rx_state = RX_IDLE;
1147 /* read callback for Diag and CS port */
1148 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1150 struct hso_serial *serial = urb->context;
1151 int status = urb->status;
1155 D1("serial == NULL");
1157 } else if (status) {
1158 log_usb_status(status, __func__);
1162 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1163 D1("Actual length = %d\n", urb->actual_length);
1164 DUMP1(urb->transfer_buffer, urb->actual_length);
1166 /* Anyone listening? */
1167 if (serial->open_count == 0)
1171 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1173 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1175 urb->actual_length %
1176 serial->in_endp->wMaxPacketSize;
1177 if (((rest == 5) || (rest == 6))
1178 && !memcmp(((u8 *) urb->transfer_buffer) +
1179 urb->actual_length - 4, crc_check, 4)) {
1180 urb->actual_length -= 4;
1183 /* Valid data, handle RX data */
1184 spin_lock(&serial->serial_lock);
1185 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1186 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1187 spin_unlock(&serial->serial_lock);
1188 } else if (status == -ENOENT || status == -ECONNRESET) {
1189 /* Unlinked - check for throttled port. */
1190 D2("Port %d, successfully unlinked urb", serial->minor);
1191 spin_lock(&serial->serial_lock);
1192 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1193 hso_resubmit_rx_bulk_urb(serial, urb);
1194 spin_unlock(&serial->serial_lock);
1196 D2("Port %d, status = %d for read urb", serial->minor, status);
1202 * This needs to be a tasklet otherwise we will
1203 * end up recursively calling this function.
1205 void hso_unthrottle_tasklet(struct hso_serial *serial)
1207 unsigned long flags;
1209 spin_lock_irqsave(&serial->serial_lock, flags);
1210 if ((serial->parent->port_spec & HSO_INTF_MUX))
1211 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1213 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1214 spin_unlock_irqrestore(&serial->serial_lock, flags);
1217 static void hso_unthrottle(struct tty_struct *tty)
1219 struct hso_serial *serial = get_serial_by_tty(tty);
1221 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1224 void hso_unthrottle_workfunc(struct work_struct *work)
1226 struct hso_serial *serial =
1227 container_of(work, struct hso_serial,
1228 retry_unthrottle_workqueue);
1229 hso_unthrottle_tasklet(serial);
1232 /* open the requested serial port */
1233 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1235 struct hso_serial *serial = get_serial_by_index(tty->index);
1239 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1241 tty->driver_data = NULL;
1242 D1("Failed to open port");
1246 mutex_lock(&serial->parent->mutex);
1247 result = usb_autopm_get_interface(serial->parent->interface);
1251 D1("Opening %d", serial->minor);
1252 kref_get(&serial->parent->ref);
1255 spin_lock_irq(&serial->serial_lock);
1256 tty->driver_data = serial;
1257 serial->tty = tty_kref_get(tty);
1258 spin_unlock_irq(&serial->serial_lock);
1260 /* check for port already opened, if not set the termios */
1261 serial->open_count++;
1262 if (serial->open_count == 1) {
1263 tty->low_latency = 1;
1264 serial->rx_state = RX_IDLE;
1265 /* Force default termio settings */
1266 _hso_serial_set_termios(tty, NULL);
1267 tasklet_init(&serial->unthrottle_tasklet,
1268 (void (*)(unsigned long))hso_unthrottle_tasklet,
1269 (unsigned long)serial);
1270 INIT_WORK(&serial->retry_unthrottle_workqueue,
1271 hso_unthrottle_workfunc);
1272 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1274 hso_stop_serial_device(serial->parent);
1275 serial->open_count--;
1276 kref_put(&serial->parent->ref, hso_serial_ref_free);
1279 D1("Port was already open");
1282 usb_autopm_put_interface(serial->parent->interface);
1286 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1288 mutex_unlock(&serial->parent->mutex);
1292 /* close the requested serial port */
1293 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1295 struct hso_serial *serial = tty->driver_data;
1298 D1("Closing serial port");
1300 /* Open failed, no close cleanup required */
1304 mutex_lock(&serial->parent->mutex);
1305 usb_gone = serial->parent->usb_gone;
1308 usb_autopm_get_interface(serial->parent->interface);
1310 /* reset the rts and dtr */
1311 /* do the actual close */
1312 serial->open_count--;
1313 kref_put(&serial->parent->ref, hso_serial_ref_free);
1314 if (serial->open_count <= 0) {
1315 serial->open_count = 0;
1316 spin_lock_irq(&serial->serial_lock);
1317 if (serial->tty == tty) {
1318 serial->tty->driver_data = NULL;
1322 spin_unlock_irq(&serial->serial_lock);
1324 hso_stop_serial_device(serial->parent);
1325 tasklet_kill(&serial->unthrottle_tasklet);
1326 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1330 usb_autopm_put_interface(serial->parent->interface);
1332 mutex_unlock(&serial->parent->mutex);
1335 /* close the requested serial port */
1336 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1339 struct hso_serial *serial = get_serial_by_tty(tty);
1340 int space, tx_bytes;
1341 unsigned long flags;
1344 if (serial == NULL) {
1345 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1349 spin_lock_irqsave(&serial->serial_lock, flags);
1351 space = serial->tx_data_length - serial->tx_buffer_count;
1352 tx_bytes = (count < space) ? count : space;
1357 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1358 serial->tx_buffer_count += tx_bytes;
1361 spin_unlock_irqrestore(&serial->serial_lock, flags);
1363 hso_kick_transmit(serial);
1368 /* how much room is there for writing */
1369 static int hso_serial_write_room(struct tty_struct *tty)
1371 struct hso_serial *serial = get_serial_by_tty(tty);
1373 unsigned long flags;
1375 spin_lock_irqsave(&serial->serial_lock, flags);
1376 room = serial->tx_data_length - serial->tx_buffer_count;
1377 spin_unlock_irqrestore(&serial->serial_lock, flags);
1379 /* return free room */
1383 /* setup the term */
1384 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1386 struct hso_serial *serial = get_serial_by_tty(tty);
1387 unsigned long flags;
1390 D5("Termios called with: cflags new[%d] - old[%d]",
1391 tty->termios->c_cflag, old->c_cflag);
1393 /* the actual setup */
1394 spin_lock_irqsave(&serial->serial_lock, flags);
1395 if (serial->open_count)
1396 _hso_serial_set_termios(tty, old);
1399 spin_unlock_irqrestore(&serial->serial_lock, flags);
1405 /* how many characters in the buffer */
1406 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1408 struct hso_serial *serial = get_serial_by_tty(tty);
1410 unsigned long flags;
1416 spin_lock_irqsave(&serial->serial_lock, flags);
1417 chars = serial->tx_buffer_count;
1418 spin_unlock_irqrestore(&serial->serial_lock, flags);
1423 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1426 struct hso_serial *serial = get_serial_by_tty(tty);
1427 unsigned long flags;
1431 D1("no tty structures");
1435 spin_lock_irqsave(&serial->serial_lock, flags);
1436 value = ((serial->rts_state) ? TIOCM_RTS : 0) |
1437 ((serial->dtr_state) ? TIOCM_DTR : 0);
1438 spin_unlock_irqrestore(&serial->serial_lock, flags);
1443 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1444 unsigned int set, unsigned int clear)
1447 unsigned long flags;
1449 struct hso_serial *serial = get_serial_by_tty(tty);
1453 D1("no tty structures");
1456 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1458 spin_lock_irqsave(&serial->serial_lock, flags);
1459 if (set & TIOCM_RTS)
1460 serial->rts_state = 1;
1461 if (set & TIOCM_DTR)
1462 serial->dtr_state = 1;
1464 if (clear & TIOCM_RTS)
1465 serial->rts_state = 0;
1466 if (clear & TIOCM_DTR)
1467 serial->dtr_state = 0;
1469 if (serial->dtr_state)
1471 if (serial->rts_state)
1474 spin_unlock_irqrestore(&serial->serial_lock, flags);
1476 return usb_control_msg(serial->parent->usb,
1477 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1478 0x21, val, if_num, NULL, 0,
1479 USB_CTRL_SET_TIMEOUT);
1482 /* starts a transmit */
1483 static void hso_kick_transmit(struct hso_serial *serial)
1486 unsigned long flags;
1489 spin_lock_irqsave(&serial->serial_lock, flags);
1490 if (!serial->tx_buffer_count)
1493 if (serial->tx_urb_used)
1496 /* Wakeup USB interface if necessary */
1497 if (hso_get_activity(serial->parent) == -EAGAIN)
1500 /* Switch pointers around to avoid memcpy */
1501 temp = serial->tx_buffer;
1502 serial->tx_buffer = serial->tx_data;
1503 serial->tx_data = temp;
1504 serial->tx_data_count = serial->tx_buffer_count;
1505 serial->tx_buffer_count = 0;
1507 /* If temp is set, it means we switched buffers */
1508 if (temp && serial->write_data) {
1509 res = serial->write_data(serial);
1511 serial->tx_urb_used = 1;
1514 spin_unlock_irqrestore(&serial->serial_lock, flags);
1517 /* make a request (for reading and writing data to muxed serial port) */
1518 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1519 struct urb *ctrl_urb,
1520 struct usb_ctrlrequest *ctrl_req,
1521 u8 *ctrl_urb_data, u32 size)
1527 if (!serial || !ctrl_urb || !ctrl_req) {
1528 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1533 ctrl_req->wValue = 0;
1534 ctrl_req->wIndex = hso_port_to_mux(port);
1535 ctrl_req->wLength = size;
1537 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1538 /* Reading command */
1539 ctrl_req->bRequestType = USB_DIR_IN |
1540 USB_TYPE_OPTION_VENDOR |
1541 USB_RECIP_INTERFACE;
1542 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1543 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1545 /* Writing command */
1546 ctrl_req->bRequestType = USB_DIR_OUT |
1547 USB_TYPE_OPTION_VENDOR |
1548 USB_RECIP_INTERFACE;
1549 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1550 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1553 D2("%s command (%02x) len: %d, port: %d",
1554 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1555 ctrl_req->bRequestType, ctrl_req->wLength, port);
1558 ctrl_urb->transfer_flags = 0;
1559 usb_fill_control_urb(ctrl_urb,
1560 serial->parent->usb,
1563 ctrl_urb_data, size, ctrl_callback, serial);
1564 /* Send it on merry way */
1565 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1567 dev_err(&ctrl_urb->dev->dev,
1568 "%s failed submit ctrl_urb %d type %d", __func__,
1577 /* called by intr_callback when read occurs */
1578 static int hso_mux_serial_read(struct hso_serial *serial)
1584 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1585 /* make the request */
1587 if (serial->num_rx_urbs != 1) {
1588 dev_err(&serial->parent->interface->dev,
1589 "ERROR: mux'd reads with multiple buffers "
1593 return mux_device_request(serial,
1594 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1595 serial->parent->port_spec & HSO_PORT_MASK,
1597 &serial->ctrl_req_rx,
1598 serial->rx_data[0], serial->rx_data_length);
1601 /* used for muxed serial port callback (muxed serial read) */
1602 static void intr_callback(struct urb *urb)
1604 struct hso_shared_int *shared_int = urb->context;
1605 struct hso_serial *serial;
1606 unsigned char *port_req;
1607 int status = urb->status;
1610 usb_mark_last_busy(urb->dev);
1618 log_usb_status(status, __func__);
1621 D4("\n--- Got intr callback 0x%02X ---", status);
1624 port_req = urb->transfer_buffer;
1625 D4(" port_req = 0x%.2X\n", *port_req);
1626 /* loop over all muxed ports to find the one sending this */
1627 for (i = 0; i < 8; i++) {
1628 /* max 8 channels on MUX */
1629 if (*port_req & (1 << i)) {
1630 serial = get_serial_by_shared_int_and_type(shared_int,
1632 if (serial != NULL) {
1633 D1("Pending read interrupt on port %d\n", i);
1634 spin_lock(&serial->serial_lock);
1635 if (serial->rx_state == RX_IDLE) {
1636 /* Setup and send a ctrl req read on
1638 if (!serial->rx_urb_filled[0]) {
1639 serial->rx_state = RX_SENT;
1640 hso_mux_serial_read(serial);
1642 serial->rx_state = RX_PENDING;
1645 D1("Already pending a read on "
1648 spin_unlock(&serial->serial_lock);
1652 /* Resubmit interrupt urb */
1653 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1656 /* called for writing to muxed serial port */
1657 static int hso_mux_serial_write_data(struct hso_serial *serial)
1662 return mux_device_request(serial,
1663 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1664 serial->parent->port_spec & HSO_PORT_MASK,
1666 &serial->ctrl_req_tx,
1667 serial->tx_data, serial->tx_data_count);
1670 /* write callback for Diag and CS port */
1671 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1673 struct hso_serial *serial = urb->context;
1674 int status = urb->status;
1675 struct tty_struct *tty;
1679 D1("serial == NULL");
1683 spin_lock(&serial->serial_lock);
1684 serial->tx_urb_used = 0;
1685 tty = tty_kref_get(serial->tty);
1686 spin_unlock(&serial->serial_lock);
1688 log_usb_status(status, __func__);
1692 hso_put_activity(serial->parent);
1697 hso_kick_transmit(serial);
1703 /* called for writing diag or CS serial port */
1704 static int hso_std_serial_write_data(struct hso_serial *serial)
1706 int count = serial->tx_data_count;
1709 usb_fill_bulk_urb(serial->tx_urb,
1710 serial->parent->usb,
1711 usb_sndbulkpipe(serial->parent->usb,
1713 bEndpointAddress & 0x7F),
1714 serial->tx_data, serial->tx_data_count,
1715 hso_std_serial_write_bulk_callback, serial);
1717 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1719 dev_warn(&serial->parent->usb->dev,
1720 "Failed to submit urb - res %d\n", result);
1727 /* callback after read or write on muxed serial port */
1728 static void ctrl_callback(struct urb *urb)
1730 struct hso_serial *serial = urb->context;
1731 struct usb_ctrlrequest *req;
1732 int status = urb->status;
1733 struct tty_struct *tty;
1739 spin_lock(&serial->serial_lock);
1740 serial->tx_urb_used = 0;
1741 tty = tty_kref_get(serial->tty);
1742 spin_unlock(&serial->serial_lock);
1744 log_usb_status(status, __func__);
1750 req = (struct usb_ctrlrequest *)(urb->setup_packet);
1751 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1752 D4("Actual length of urb = %d\n", urb->actual_length);
1753 DUMP1(urb->transfer_buffer, urb->actual_length);
1755 if (req->bRequestType ==
1756 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1757 /* response to a read command */
1758 serial->rx_urb_filled[0] = 1;
1759 spin_lock(&serial->serial_lock);
1760 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1761 spin_unlock(&serial->serial_lock);
1763 hso_put_activity(serial->parent);
1766 /* response to a write command */
1767 hso_kick_transmit(serial);
1772 /* handle RX data for serial port */
1773 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
1775 struct tty_struct *tty;
1776 int write_length_remaining = 0;
1780 if (urb == NULL || serial == NULL) {
1781 D1("serial = NULL");
1785 spin_lock(&serial->serial_lock);
1786 tty = tty_kref_get(serial->tty);
1787 spin_unlock(&serial->serial_lock);
1789 /* Push data to tty */
1791 write_length_remaining = urb->actual_length -
1792 serial->curr_rx_urb_offset;
1793 D1("data to push to tty");
1794 while (write_length_remaining) {
1795 if (test_bit(TTY_THROTTLED, &tty->flags))
1797 curr_write_len = tty_insert_flip_string
1798 (tty, urb->transfer_buffer +
1799 serial->curr_rx_urb_offset,
1800 write_length_remaining);
1801 serial->curr_rx_urb_offset += curr_write_len;
1802 write_length_remaining -= curr_write_len;
1803 tty_flip_buffer_push(tty);
1806 if (write_length_remaining == 0) {
1807 serial->curr_rx_urb_offset = 0;
1808 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1811 return write_length_remaining;
1815 /* Base driver functions */
1817 static void hso_log_port(struct hso_device *hso_dev)
1822 switch (hso_dev->port_spec & HSO_PORT_MASK) {
1823 case HSO_PORT_CONTROL:
1824 port_type = "Control";
1827 port_type = "Application";
1832 case HSO_PORT_GPS_CONTROL:
1833 port_type = "GPS control";
1836 port_type = "Application2";
1842 port_type = "Diagnostic";
1844 case HSO_PORT_DIAG2:
1845 port_type = "Diagnostic2";
1847 case HSO_PORT_MODEM:
1848 port_type = "Modem";
1850 case HSO_PORT_NETWORK:
1851 port_type = "Network";
1854 port_type = "Unknown";
1857 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
1858 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
1860 sprintf(port_dev, "/dev/%s%d", tty_filename,
1861 dev2ser(hso_dev)->minor);
1863 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
1864 port_type, port_dev);
1867 static int hso_start_net_device(struct hso_device *hso_dev)
1870 struct hso_net *hso_net = dev2net(hso_dev);
1875 /* send URBs for all read buffers */
1876 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1878 /* Prep a receive URB */
1879 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
1881 usb_rcvbulkpipe(hso_dev->usb,
1883 bEndpointAddress & 0x7F),
1884 hso_net->mux_bulk_rx_buf_pool[i],
1885 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
1888 /* Put it out there so the device can send us stuff */
1889 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
1892 dev_warn(&hso_dev->usb->dev,
1893 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
1900 static int hso_stop_net_device(struct hso_device *hso_dev)
1903 struct hso_net *hso_net = dev2net(hso_dev);
1908 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1909 if (hso_net->mux_bulk_rx_urb_pool[i])
1910 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1913 if (hso_net->mux_bulk_tx_urb)
1914 usb_kill_urb(hso_net->mux_bulk_tx_urb);
1919 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
1922 struct hso_serial *serial = dev2ser(hso_dev);
1927 /* If it is not the MUX port fill in and submit a bulk urb (already
1928 * allocated in hso_serial_start) */
1929 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
1930 for (i = 0; i < serial->num_rx_urbs; i++) {
1931 usb_fill_bulk_urb(serial->rx_urb[i],
1932 serial->parent->usb,
1933 usb_rcvbulkpipe(serial->parent->usb,
1938 serial->rx_data_length,
1939 hso_std_serial_read_bulk_callback,
1941 result = usb_submit_urb(serial->rx_urb[i], flags);
1943 dev_warn(&serial->parent->usb->dev,
1944 "Failed to submit urb - res %d\n",
1950 mutex_lock(&serial->shared_int->shared_int_lock);
1951 if (!serial->shared_int->use_count) {
1953 hso_mux_submit_intr_urb(serial->shared_int,
1954 hso_dev->usb, flags);
1956 serial->shared_int->use_count++;
1957 mutex_unlock(&serial->shared_int->shared_int_lock);
1963 static int hso_stop_serial_device(struct hso_device *hso_dev)
1966 struct hso_serial *serial = dev2ser(hso_dev);
1971 for (i = 0; i < serial->num_rx_urbs; i++) {
1972 if (serial->rx_urb[i]) {
1973 usb_kill_urb(serial->rx_urb[i]);
1974 serial->rx_urb_filled[i] = 0;
1977 serial->curr_rx_urb_idx = 0;
1978 serial->curr_rx_urb_offset = 0;
1981 usb_kill_urb(serial->tx_urb);
1983 if (serial->shared_int) {
1984 mutex_lock(&serial->shared_int->shared_int_lock);
1985 if (serial->shared_int->use_count &&
1986 (--serial->shared_int->use_count == 0)) {
1989 urb = serial->shared_int->shared_intr_urb;
1993 mutex_unlock(&serial->shared_int->shared_int_lock);
1999 static void hso_serial_common_free(struct hso_serial *serial)
2003 if (serial->parent->dev)
2004 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2006 tty_unregister_device(tty_drv, serial->minor);
2008 for (i = 0; i < serial->num_rx_urbs; i++) {
2009 /* unlink and free RX URB */
2010 usb_free_urb(serial->rx_urb[i]);
2011 /* free the RX buffer */
2012 kfree(serial->rx_data[i]);
2015 /* unlink and free TX URB */
2016 usb_free_urb(serial->tx_urb);
2017 kfree(serial->tx_data);
2020 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2021 int rx_size, int tx_size)
2027 minor = get_free_serial_index();
2031 /* register our minor number */
2032 serial->parent->dev = tty_register_device(tty_drv, minor,
2033 &serial->parent->interface->dev);
2034 dev = serial->parent->dev;
2035 dev->driver_data = serial->parent;
2036 i = device_create_file(dev, &dev_attr_hsotype);
2038 /* fill in specific data for later use */
2039 serial->minor = minor;
2040 serial->magic = HSO_SERIAL_MAGIC;
2041 spin_lock_init(&serial->serial_lock);
2042 serial->num_rx_urbs = num_urbs;
2044 /* RX, allocate urb and initialize */
2046 /* prepare our RX buffer */
2047 serial->rx_data_length = rx_size;
2048 for (i = 0; i < serial->num_rx_urbs; i++) {
2049 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2050 if (!serial->rx_urb[i]) {
2051 dev_err(dev, "Could not allocate urb?\n");
2054 serial->rx_urb[i]->transfer_buffer = NULL;
2055 serial->rx_urb[i]->transfer_buffer_length = 0;
2056 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2058 if (!serial->rx_data[i]) {
2059 dev_err(dev, "%s - Out of memory\n", __func__);
2064 /* TX, allocate urb and initialize */
2065 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2066 if (!serial->tx_urb) {
2067 dev_err(dev, "Could not allocate urb?\n");
2070 serial->tx_urb->transfer_buffer = NULL;
2071 serial->tx_urb->transfer_buffer_length = 0;
2072 /* prepare our TX buffer */
2073 serial->tx_data_count = 0;
2074 serial->tx_buffer_count = 0;
2075 serial->tx_data_length = tx_size;
2076 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2077 if (!serial->tx_data) {
2078 dev_err(dev, "%s - Out of memory", __func__);
2081 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2082 if (!serial->tx_buffer) {
2083 dev_err(dev, "%s - Out of memory", __func__);
2089 hso_serial_common_free(serial);
2093 /* Frees a general hso device */
2094 static void hso_free_device(struct hso_device *hso_dev)
2099 /* Creates a general hso device */
2100 static struct hso_device *hso_create_device(struct usb_interface *intf,
2103 struct hso_device *hso_dev;
2105 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2109 hso_dev->port_spec = port_spec;
2110 hso_dev->usb = interface_to_usbdev(intf);
2111 hso_dev->interface = intf;
2112 kref_init(&hso_dev->ref);
2113 mutex_init(&hso_dev->mutex);
2115 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2116 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2121 /* Removes a network device in the network device table */
2122 static int remove_net_device(struct hso_device *hso_dev)
2126 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2127 if (network_table[i] == hso_dev) {
2128 network_table[i] = NULL;
2132 if (i == HSO_MAX_NET_DEVICES)
2137 /* Frees our network device */
2138 static void hso_free_net_device(struct hso_device *hso_dev)
2141 struct hso_net *hso_net = dev2net(hso_dev);
2147 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2148 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2149 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2151 usb_free_urb(hso_net->mux_bulk_tx_urb);
2152 kfree(hso_net->mux_bulk_tx_buf);
2154 remove_net_device(hso_net->parent);
2157 unregister_netdev(hso_net->net);
2158 free_netdev(hso_net->net);
2161 hso_free_device(hso_dev);
2164 /* initialize the network interface */
2165 static void hso_net_init(struct net_device *net)
2167 struct hso_net *hso_net = netdev_priv(net);
2169 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2171 /* fill in the other fields */
2172 net->open = hso_net_open;
2173 net->stop = hso_net_close;
2174 net->hard_start_xmit = hso_net_start_xmit;
2175 net->tx_timeout = hso_net_tx_timeout;
2176 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2177 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2178 net->type = ARPHRD_NONE;
2179 net->mtu = DEFAULT_MTU - 14;
2180 net->tx_queue_len = 10;
2181 SET_ETHTOOL_OPS(net, &ops);
2183 /* and initialize the semaphore */
2184 spin_lock_init(&hso_net->net_lock);
2187 /* Adds a network device in the network device table */
2188 static int add_net_device(struct hso_device *hso_dev)
2192 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2193 if (network_table[i] == NULL) {
2194 network_table[i] = hso_dev;
2198 if (i == HSO_MAX_NET_DEVICES)
2203 static int hso_radio_toggle(void *data, enum rfkill_state state)
2205 struct hso_device *hso_dev = data;
2206 int enabled = (state == RFKILL_STATE_ON);
2209 mutex_lock(&hso_dev->mutex);
2210 if (hso_dev->usb_gone)
2213 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2214 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2215 USB_CTRL_SET_TIMEOUT);
2216 mutex_unlock(&hso_dev->mutex);
2220 /* Creates and sets up everything for rfkill */
2221 static void hso_create_rfkill(struct hso_device *hso_dev,
2222 struct usb_interface *interface)
2224 struct hso_net *hso_net = dev2net(hso_dev);
2225 struct device *dev = &hso_net->net->dev;
2228 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2230 if (!hso_net->rfkill) {
2231 dev_err(dev, "%s - Out of memory\n", __func__);
2234 rfkn = kzalloc(20, GFP_KERNEL);
2236 rfkill_free(hso_net->rfkill);
2237 hso_net->rfkill = NULL;
2238 dev_err(dev, "%s - Out of memory\n", __func__);
2241 snprintf(rfkn, 20, "hso-%d",
2242 interface->altsetting->desc.bInterfaceNumber);
2243 hso_net->rfkill->name = rfkn;
2244 hso_net->rfkill->state = RFKILL_STATE_ON;
2245 hso_net->rfkill->data = hso_dev;
2246 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2247 if (rfkill_register(hso_net->rfkill) < 0) {
2249 hso_net->rfkill->name = NULL;
2250 rfkill_free(hso_net->rfkill);
2251 hso_net->rfkill = NULL;
2252 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2257 /* Creates our network device */
2258 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2261 struct net_device *net;
2262 struct hso_net *hso_net;
2263 struct hso_device *hso_dev;
2265 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2269 /* allocate our network device, then we can put in our private data */
2270 /* call hso_net_init to do the basic initialization */
2271 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2273 dev_err(&interface->dev, "Unable to create ethernet device\n");
2277 hso_net = netdev_priv(net);
2279 hso_dev->port_data.dev_net = hso_net;
2281 hso_net->parent = hso_dev;
2283 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2285 if (!hso_net->in_endp) {
2286 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2289 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2291 if (!hso_net->out_endp) {
2292 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2295 SET_NETDEV_DEV(net, &interface->dev);
2297 /* registering our net device */
2298 result = register_netdev(net);
2300 dev_err(&interface->dev, "Failed to register device\n");
2304 /* start allocating */
2305 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2306 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2307 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2308 dev_err(&interface->dev, "Could not allocate rx urb\n");
2311 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2313 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2314 dev_err(&interface->dev, "Could not allocate rx buf\n");
2318 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2319 if (!hso_net->mux_bulk_tx_urb) {
2320 dev_err(&interface->dev, "Could not allocate tx urb\n");
2323 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2324 if (!hso_net->mux_bulk_tx_buf) {
2325 dev_err(&interface->dev, "Could not allocate tx buf\n");
2329 add_net_device(hso_dev);
2331 hso_log_port(hso_dev);
2333 hso_create_rfkill(hso_dev, interface);
2337 hso_free_net_device(hso_dev);
2341 /* Frees an AT channel ( goes for both mux and non-mux ) */
2342 static void hso_free_serial_device(struct hso_device *hso_dev)
2344 struct hso_serial *serial = dev2ser(hso_dev);
2348 set_serial_by_index(serial->minor, NULL);
2350 hso_serial_common_free(serial);
2352 if (serial->shared_int) {
2353 mutex_lock(&serial->shared_int->shared_int_lock);
2354 if (--serial->shared_int->ref_count == 0)
2355 hso_free_shared_int(serial->shared_int);
2357 mutex_unlock(&serial->shared_int->shared_int_lock);
2360 hso_free_device(hso_dev);
2363 /* Creates a bulk AT channel */
2364 static struct hso_device *hso_create_bulk_serial_device(
2365 struct usb_interface *interface, int port)
2367 struct hso_device *hso_dev;
2368 struct hso_serial *serial;
2371 hso_dev = hso_create_device(interface, port);
2375 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2379 serial->parent = hso_dev;
2380 hso_dev->port_data.dev_serial = serial;
2382 if (port & HSO_PORT_MODEM)
2387 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2391 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2393 if (!serial->in_endp) {
2394 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2400 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2401 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2405 serial->write_data = hso_std_serial_write_data;
2407 /* and record this serial */
2408 set_serial_by_index(serial->minor, serial);
2410 /* setup the proc dirs and files if needed */
2411 hso_log_port(hso_dev);
2413 /* done, return it */
2417 hso_serial_common_free(serial);
2420 hso_free_device(hso_dev);
2424 /* Creates a multiplexed AT channel */
2426 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2428 struct hso_shared_int *mux)
2430 struct hso_device *hso_dev;
2431 struct hso_serial *serial;
2434 port_spec = HSO_INTF_MUX;
2435 port_spec &= ~HSO_PORT_MASK;
2437 port_spec |= hso_mux_to_port(port);
2438 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2441 hso_dev = hso_create_device(interface, port_spec);
2445 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2449 hso_dev->port_data.dev_serial = serial;
2450 serial->parent = hso_dev;
2452 if (hso_serial_common_create
2453 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2456 serial->tx_data_length--;
2457 serial->write_data = hso_mux_serial_write_data;
2459 serial->shared_int = mux;
2460 mutex_lock(&serial->shared_int->shared_int_lock);
2461 serial->shared_int->ref_count++;
2462 mutex_unlock(&serial->shared_int->shared_int_lock);
2464 /* and record this serial */
2465 set_serial_by_index(serial->minor, serial);
2467 /* setup the proc dirs and files if needed */
2468 hso_log_port(hso_dev);
2470 /* done, return it */
2475 tty_unregister_device(tty_drv, serial->minor);
2479 hso_free_device(hso_dev);
2484 static void hso_free_shared_int(struct hso_shared_int *mux)
2486 usb_free_urb(mux->shared_intr_urb);
2487 kfree(mux->shared_intr_buf);
2488 mutex_unlock(&mux->shared_int_lock);
2493 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2495 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2500 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2502 if (!mux->intr_endp) {
2503 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2507 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2508 if (!mux->shared_intr_urb) {
2509 dev_err(&interface->dev, "Could not allocate intr urb?");
2512 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2514 if (!mux->shared_intr_buf) {
2515 dev_err(&interface->dev, "Could not allocate intr buf?");
2519 mutex_init(&mux->shared_int_lock);
2524 kfree(mux->shared_intr_buf);
2525 usb_free_urb(mux->shared_intr_urb);
2530 /* Gets the port spec for a certain interface */
2531 static int hso_get_config_data(struct usb_interface *interface)
2533 struct usb_device *usbdev = interface_to_usbdev(interface);
2535 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2538 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2539 0x86, 0xC0, 0, 0, config_data, 17,
2540 USB_CTRL_SET_TIMEOUT) != 0x11) {
2544 switch (config_data[if_num]) {
2549 result = HSO_PORT_DIAG;
2552 result = HSO_PORT_GPS;
2555 result = HSO_PORT_GPS_CONTROL;
2558 result = HSO_PORT_APP;
2561 result = HSO_PORT_APP2;
2564 result = HSO_PORT_CONTROL;
2567 result = HSO_PORT_NETWORK;
2570 result = HSO_PORT_MODEM;
2573 result = HSO_PORT_MSD;
2576 result = HSO_PORT_PCSC;
2579 result = HSO_PORT_VOICE;
2586 result |= HSO_INTF_BULK;
2588 if (config_data[16] & 0x1)
2589 result |= HSO_INFO_CRC_BUG;
2594 /* called once for each interface upon device insertion */
2595 static int hso_probe(struct usb_interface *interface,
2596 const struct usb_device_id *id)
2598 int mux, i, if_num, port_spec;
2599 unsigned char port_mask;
2600 struct hso_device *hso_dev = NULL;
2601 struct hso_shared_int *shared_int;
2602 struct hso_device *tmp_dev = NULL;
2604 if_num = interface->altsetting->desc.bInterfaceNumber;
2606 /* Get the interface/port specification from either driver_info or from
2607 * the device itself */
2608 if (id->driver_info)
2609 port_spec = ((u32 *)(id->driver_info))[if_num];
2611 port_spec = hso_get_config_data(interface);
2613 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2614 dev_err(&interface->dev, "Not our interface\n");
2617 /* Check if we need to switch to alt interfaces prior to port
2619 if (interface->num_altsetting > 1)
2620 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2621 interface->needs_remote_wakeup = 1;
2623 /* Allocate new hso device(s) */
2624 switch (port_spec & HSO_INTF_MASK) {
2626 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2627 /* Create the network device */
2629 hso_dev = hso_create_net_device(interface);
2636 if (hso_get_mux_ports(interface, &port_mask))
2637 /* TODO: de-allocate everything */
2640 shared_int = hso_create_shared_int(interface);
2644 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2645 if (port_mask & i) {
2646 hso_dev = hso_create_mux_serial_device(
2647 interface, i, shared_int);
2658 /* It's a regular bulk interface */
2659 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2661 hso_dev = hso_create_net_device(interface);
2664 hso_create_bulk_serial_device(interface, port_spec);
2672 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2674 /* save our data pointer in this device */
2675 usb_set_intfdata(interface, hso_dev);
2680 hso_free_interface(interface);
2684 /* device removed, cleaning up */
2685 static void hso_disconnect(struct usb_interface *interface)
2687 hso_free_interface(interface);
2689 /* remove reference of our private data */
2690 usb_set_intfdata(interface, NULL);
2692 usb_driver_release_interface(&hso_driver, interface);
2695 static void async_get_intf(struct work_struct *data)
2697 struct hso_device *hso_dev =
2698 container_of(data, struct hso_device, async_get_intf);
2699 usb_autopm_get_interface(hso_dev->interface);
2702 static void async_put_intf(struct work_struct *data)
2704 struct hso_device *hso_dev =
2705 container_of(data, struct hso_device, async_put_intf);
2706 usb_autopm_put_interface(hso_dev->interface);
2709 static int hso_get_activity(struct hso_device *hso_dev)
2711 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2712 if (!hso_dev->is_active) {
2713 hso_dev->is_active = 1;
2714 schedule_work(&hso_dev->async_get_intf);
2718 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2721 usb_mark_last_busy(hso_dev->usb);
2726 static int hso_put_activity(struct hso_device *hso_dev)
2728 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2729 if (hso_dev->is_active) {
2730 hso_dev->is_active = 0;
2731 schedule_work(&hso_dev->async_put_intf);
2735 hso_dev->is_active = 0;
2739 /* called by kernel when we need to suspend device */
2740 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2744 /* Stop all serial ports */
2745 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2746 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2747 result = hso_stop_serial_device(serial_table[i]);
2753 /* Stop all network ports */
2754 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2755 if (network_table[i] &&
2756 (network_table[i]->interface == iface)) {
2757 result = hso_stop_net_device(network_table[i]);
2767 /* called by kernel when we need to resume device */
2768 static int hso_resume(struct usb_interface *iface)
2771 struct hso_net *hso_net;
2773 /* Start all serial ports */
2774 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2775 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2776 if (dev2ser(serial_table[i])->open_count) {
2778 hso_start_serial_device(serial_table[i], GFP_NOIO);
2779 hso_kick_transmit(dev2ser(serial_table[i]));
2786 /* Start all network ports */
2787 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2788 if (network_table[i] &&
2789 (network_table[i]->interface == iface)) {
2790 hso_net = dev2net(network_table[i]);
2791 if (hso_net->flags & IFF_UP) {
2792 /* First transmit any lingering data,
2793 then restart the device. */
2794 if (hso_net->skb_tx_buf) {
2795 dev_dbg(&iface->dev,
2797 " lingering data\n");
2798 hso_net_start_xmit(hso_net->skb_tx_buf,
2800 hso_net->skb_tx_buf = NULL;
2802 result = hso_start_net_device(network_table[i]);
2813 static void hso_serial_ref_free(struct kref *ref)
2815 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2817 hso_free_serial_device(hso_dev);
2820 static void hso_free_interface(struct usb_interface *interface)
2822 struct hso_serial *hso_dev;
2823 struct tty_struct *tty;
2826 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2828 && (serial_table[i]->interface == interface)) {
2829 hso_dev = dev2ser(serial_table[i]);
2830 spin_lock_irq(&hso_dev->serial_lock);
2831 tty = tty_kref_get(hso_dev->tty);
2832 spin_unlock_irq(&hso_dev->serial_lock);
2835 mutex_lock(&hso_dev->parent->mutex);
2837 hso_dev->parent->usb_gone = 1;
2838 mutex_unlock(&hso_dev->parent->mutex);
2839 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2843 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2844 if (network_table[i]
2845 && (network_table[i]->interface == interface)) {
2846 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2847 /* hso_stop_net_device doesn't stop the net queue since
2848 * traffic needs to start it again when suspended */
2849 netif_stop_queue(dev2net(network_table[i])->net);
2850 hso_stop_net_device(network_table[i]);
2851 cancel_work_sync(&network_table[i]->async_put_intf);
2852 cancel_work_sync(&network_table[i]->async_get_intf);
2854 rfkill_unregister(rfk);
2855 hso_free_net_device(network_table[i]);
2860 /* Helper functions */
2862 /* Get the endpoint ! */
2863 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2867 struct usb_host_interface *iface = intf->cur_altsetting;
2868 struct usb_endpoint_descriptor *endp;
2870 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2871 endp = &iface->endpoint[i].desc;
2872 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2873 (usb_endpoint_type(endp) == type))
2880 /* Get the byte that describes which ports are enabled */
2881 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2884 struct usb_host_interface *iface = intf->cur_altsetting;
2886 if (iface->extralen == 3) {
2887 *ports = iface->extra[2];
2891 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2892 if (iface->endpoint[i].extralen == 3) {
2893 *ports = iface->endpoint[i].extra[2];
2901 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2902 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2903 struct usb_device *usb, gfp_t gfp)
2907 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2909 shared_int->intr_endp->bEndpointAddress & 0x7F),
2910 shared_int->shared_intr_buf,
2911 shared_int->intr_endp->wMaxPacketSize,
2912 intr_callback, shared_int,
2913 shared_int->intr_endp->bInterval);
2915 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2917 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2923 /* operations setup of the serial interface */
2924 static const struct tty_operations hso_serial_ops = {
2925 .open = hso_serial_open,
2926 .close = hso_serial_close,
2927 .write = hso_serial_write,
2928 .write_room = hso_serial_write_room,
2929 .set_termios = hso_serial_set_termios,
2930 .chars_in_buffer = hso_serial_chars_in_buffer,
2931 .tiocmget = hso_serial_tiocmget,
2932 .tiocmset = hso_serial_tiocmset,
2933 .unthrottle = hso_unthrottle
2936 static struct usb_driver hso_driver = {
2937 .name = driver_name,
2939 .disconnect = hso_disconnect,
2940 .id_table = hso_ids,
2941 .suspend = hso_suspend,
2942 .resume = hso_resume,
2943 .reset_resume = hso_resume,
2944 .supports_autosuspend = 1,
2947 static int __init hso_init(void)
2952 /* put it in the log */
2953 printk(KERN_INFO "hso: %s\n", version);
2955 /* Initialise the serial table semaphore and table */
2956 spin_lock_init(&serial_table_lock);
2957 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2958 serial_table[i] = NULL;
2960 /* allocate our driver using the proper amount of supported minors */
2961 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2965 /* fill in all needed values */
2966 tty_drv->magic = TTY_DRIVER_MAGIC;
2967 tty_drv->owner = THIS_MODULE;
2968 tty_drv->driver_name = driver_name;
2969 tty_drv->name = tty_filename;
2971 /* if major number is provided as parameter, use that one */
2973 tty_drv->major = tty_major;
2975 tty_drv->minor_start = 0;
2976 tty_drv->num = HSO_SERIAL_TTY_MINORS;
2977 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2978 tty_drv->subtype = SERIAL_TYPE_NORMAL;
2979 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2980 tty_drv->init_termios = tty_std_termios;
2981 hso_init_termios(&tty_drv->init_termios);
2982 tty_set_operations(tty_drv, &hso_serial_ops);
2984 /* register the tty driver */
2985 result = tty_register_driver(tty_drv);
2987 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2992 /* register this module as an usb driver */
2993 result = usb_register(&hso_driver);
2995 printk(KERN_ERR "Could not register hso driver? error: %d\n",
2997 /* cleanup serial interface */
2998 tty_unregister_driver(tty_drv);
3006 static void __exit hso_exit(void)
3008 printk(KERN_INFO "hso: unloaded\n");
3010 tty_unregister_driver(tty_drv);
3011 /* deregister the usb driver */
3012 usb_deregister(&hso_driver);
3015 /* Module definitions */
3016 module_init(hso_init);
3017 module_exit(hso_exit);
3019 MODULE_AUTHOR(MOD_AUTHOR);
3020 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3021 MODULE_LICENSE(MOD_LICENSE);
3022 MODULE_INFO(Version, DRIVER_VERSION);
3024 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3025 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3026 module_param(debug, int, S_IRUGO | S_IWUSR);
3028 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3029 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3030 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3032 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3033 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3034 module_param(disable_net, int, S_IRUGO | S_IWUSR);