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 */
420 {USB_DEVICE(0x0af0, 0x7701)},
421 {USB_DEVICE(0x0af0, 0x7801)},
422 {USB_DEVICE(0x0af0, 0x7901)},
423 {USB_DEVICE(0x0af0, 0x7361)},
424 {icon321_port_device(0x0af0, 0xd051)},
427 MODULE_DEVICE_TABLE(usb, hso_ids);
429 /* Sysfs attribute */
430 static ssize_t hso_sysfs_show_porttype(struct device *dev,
431 struct device_attribute *attr,
434 struct hso_device *hso_dev = dev->driver_data;
440 switch (hso_dev->port_spec & HSO_PORT_MASK) {
441 case HSO_PORT_CONTROL:
442 port_name = "Control";
445 port_name = "Application";
448 port_name = "Application2";
453 case HSO_PORT_GPS_CONTROL:
454 port_name = "GPS Control";
460 port_name = "Diagnostic";
463 port_name = "Diagnostic2";
468 case HSO_PORT_NETWORK:
469 port_name = "Network";
472 port_name = "Unknown";
476 return sprintf(buf, "%s\n", port_name);
478 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
480 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
484 for (idx = 0; idx < serial->num_rx_urbs; idx++)
485 if (serial->rx_urb[idx] == urb)
487 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
491 /* converts mux value to a port spec value */
492 static u32 hso_mux_to_port(int mux)
498 result = HSO_PORT_CONTROL;
501 result = HSO_PORT_APP;
504 result = HSO_PORT_PCSC;
507 result = HSO_PORT_GPS;
510 result = HSO_PORT_APP2;
513 result = HSO_PORT_NO_PORT;
518 /* converts port spec value to a mux value */
519 static u32 hso_port_to_mux(int port)
523 switch (port & HSO_PORT_MASK) {
524 case HSO_PORT_CONTROL:
545 static struct hso_serial *get_serial_by_shared_int_and_type(
546 struct hso_shared_int *shared_int,
551 port = hso_mux_to_port(mux);
553 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
555 && (dev2ser(serial_table[i])->shared_int == shared_int)
556 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
557 return dev2ser(serial_table[i]);
564 static struct hso_serial *get_serial_by_index(unsigned index)
566 struct hso_serial *serial = NULL;
569 spin_lock_irqsave(&serial_table_lock, flags);
570 if (serial_table[index])
571 serial = dev2ser(serial_table[index]);
572 spin_unlock_irqrestore(&serial_table_lock, flags);
577 static int get_free_serial_index(void)
582 spin_lock_irqsave(&serial_table_lock, flags);
583 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
584 if (serial_table[index] == NULL) {
585 spin_unlock_irqrestore(&serial_table_lock, flags);
589 spin_unlock_irqrestore(&serial_table_lock, flags);
591 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
595 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
599 spin_lock_irqsave(&serial_table_lock, flags);
601 serial_table[index] = serial->parent;
603 serial_table[index] = NULL;
604 spin_unlock_irqrestore(&serial_table_lock, flags);
607 /* log a meaningful explanation of an USB status */
608 static void log_usb_status(int status, const char *function)
614 explanation = "no device";
617 explanation = "endpoint not enabled";
620 explanation = "endpoint stalled";
623 explanation = "not enough bandwidth";
626 explanation = "device disabled";
629 explanation = "device suspended";
635 explanation = "internal error";
638 explanation = "unknown status";
641 D1("%s: received USB status - %s (%d)", function, explanation, status);
644 /* Network interface functions */
646 /* called when net interface is brought up by ifconfig */
647 static int hso_net_open(struct net_device *net)
649 struct hso_net *odev = netdev_priv(net);
650 unsigned long flags = 0;
653 dev_err(&net->dev, "No net device !\n");
657 odev->skb_tx_buf = NULL;
659 /* setup environment */
660 spin_lock_irqsave(&odev->net_lock, flags);
661 odev->rx_parse_state = WAIT_IP;
662 odev->rx_buf_size = 0;
663 odev->rx_buf_missing = sizeof(struct iphdr);
664 spin_unlock_irqrestore(&odev->net_lock, flags);
666 /* We are up and running. */
667 set_bit(HSO_NET_RUNNING, &odev->flags);
668 hso_start_net_device(odev->parent);
670 /* Tell the kernel we are ready to start receiving from it */
671 netif_start_queue(net);
676 /* called when interface is brought down by ifconfig */
677 static int hso_net_close(struct net_device *net)
679 struct hso_net *odev = netdev_priv(net);
681 /* we don't need the queue anymore */
682 netif_stop_queue(net);
683 /* no longer running */
684 clear_bit(HSO_NET_RUNNING, &odev->flags);
686 hso_stop_net_device(odev->parent);
692 /* USB tells is xmit done, we should start the netqueue again */
693 static void write_bulk_callback(struct urb *urb)
695 struct hso_net *odev = urb->context;
696 int status = urb->status;
699 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
700 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
704 /* Do we still have a valid kernel network device? */
705 if (!netif_device_present(odev->net)) {
706 dev_err(&urb->dev->dev, "%s: net device not present\n",
711 /* log status, but don't act on it, we don't need to resubmit anything
714 log_usb_status(status, __func__);
716 hso_put_activity(odev->parent);
718 /* Tell the network interface we are ready for another frame */
719 netif_wake_queue(odev->net);
722 /* called by kernel when we need to transmit a packet */
723 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
725 struct hso_net *odev = netdev_priv(net);
728 /* Tell the kernel, "No more frames 'til we are done with this one." */
729 netif_stop_queue(net);
730 if (hso_get_activity(odev->parent) == -EAGAIN) {
731 odev->skb_tx_buf = skb;
736 DUMP1(skb->data, skb->len);
737 /* Copy it from kernel memory to OUR memory */
738 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
739 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
741 /* Fill in the URB for shipping it out. */
742 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
744 usb_sndbulkpipe(odev->parent->usb,
746 bEndpointAddress & 0x7F),
747 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
750 /* Deal with the Zero Length packet problem, I hope */
751 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
753 /* Send the URB on its merry way. */
754 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
756 dev_warn(&odev->parent->interface->dev,
757 "failed mux_bulk_tx_urb %d", result);
758 net->stats.tx_errors++;
759 netif_start_queue(net);
761 net->stats.tx_packets++;
762 net->stats.tx_bytes += skb->len;
763 /* And tell the kernel when the last transmit started. */
764 net->trans_start = jiffies;
771 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
773 struct hso_net *odev = netdev_priv(net);
775 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
776 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
777 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
780 static struct ethtool_ops ops = {
781 .get_drvinfo = hso_get_drvinfo,
782 .get_link = ethtool_op_get_link
785 /* called when a packet did not ack after watchdogtimeout */
786 static void hso_net_tx_timeout(struct net_device *net)
788 struct hso_net *odev = netdev_priv(net);
793 /* Tell syslog we are hosed. */
794 dev_warn(&net->dev, "Tx timed out.\n");
796 /* Tear the waiting frame off the list */
797 if (odev->mux_bulk_tx_urb
798 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
799 usb_unlink_urb(odev->mux_bulk_tx_urb);
801 /* Update statistics */
802 net->stats.tx_errors++;
805 /* make a real packet from the received USB buffer */
806 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
807 unsigned int count, unsigned char is_eop)
809 unsigned short temp_bytes;
810 unsigned short buffer_offset = 0;
811 unsigned short frame_len;
812 unsigned char *tmp_rx_buf;
815 D1("Rx %d bytes", count);
816 DUMP(ip_pkt, min(128, (int)count));
819 switch (odev->rx_parse_state) {
821 /* waiting for IP header. */
822 /* wanted bytes - size of ip header */
825 odev->rx_buf_missing) ? count : odev->
828 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
829 odev->rx_buf_size, ip_pkt + buffer_offset,
832 odev->rx_buf_size += temp_bytes;
833 buffer_offset += temp_bytes;
834 odev->rx_buf_missing -= temp_bytes;
837 if (!odev->rx_buf_missing) {
838 /* header is complete allocate an sk_buffer and
839 * continue to WAIT_DATA */
840 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
842 if ((frame_len > DEFAULT_MRU) ||
843 (frame_len < sizeof(struct iphdr))) {
844 dev_err(&odev->net->dev,
845 "Invalid frame (%d) length\n",
847 odev->rx_parse_state = WAIT_SYNC;
850 /* Allocate an sk_buff */
851 odev->skb_rx_buf = dev_alloc_skb(frame_len);
852 if (!odev->skb_rx_buf) {
853 /* We got no receive buffer. */
854 D1("could not allocate memory");
855 odev->rx_parse_state = WAIT_SYNC;
858 /* Here's where it came from */
859 odev->skb_rx_buf->dev = odev->net;
861 /* Copy what we got so far. make room for iphdr
864 skb_put(odev->skb_rx_buf,
865 sizeof(struct iphdr));
866 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
867 sizeof(struct iphdr));
870 odev->rx_buf_size = sizeof(struct iphdr);
872 /* Filip actually use .tot_len */
873 odev->rx_buf_missing =
874 frame_len - sizeof(struct iphdr);
875 odev->rx_parse_state = WAIT_DATA;
880 temp_bytes = (count < odev->rx_buf_missing)
881 ? count : odev->rx_buf_missing;
883 /* Copy the rest of the bytes that are left in the
884 * buffer into the waiting sk_buf. */
885 /* Make room for temp_bytes after tail. */
886 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
887 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
889 odev->rx_buf_missing -= temp_bytes;
891 buffer_offset += temp_bytes;
892 odev->rx_buf_size += temp_bytes;
893 if (!odev->rx_buf_missing) {
894 /* Packet is complete. Inject into stack. */
895 /* We have IP packet here */
896 odev->skb_rx_buf->protocol =
897 __constant_htons(ETH_P_IP);
899 odev->skb_rx_buf->ip_summed =
900 CHECKSUM_UNNECESSARY;
902 skb_reset_mac_header(odev->skb_rx_buf);
904 /* Ship it off to the kernel */
905 netif_rx(odev->skb_rx_buf);
906 /* No longer our buffer. */
907 odev->skb_rx_buf = NULL;
909 /* update out statistics */
910 odev->net->stats.rx_packets++;
912 odev->net->stats.rx_bytes += odev->rx_buf_size;
914 odev->rx_buf_size = 0;
915 odev->rx_buf_missing = sizeof(struct iphdr);
916 odev->rx_parse_state = WAIT_IP;
931 /* Recovery mechanism for WAIT_SYNC state. */
933 if (odev->rx_parse_state == WAIT_SYNC) {
934 odev->rx_parse_state = WAIT_IP;
935 odev->rx_buf_size = 0;
936 odev->rx_buf_missing = sizeof(struct iphdr);
941 /* Moving data from usb to kernel (in interrupt state) */
942 static void read_bulk_callback(struct urb *urb)
944 struct hso_net *odev = urb->context;
945 struct net_device *net;
947 int status = urb->status;
949 /* is al ok? (Filip: Who's Al ?) */
951 log_usb_status(status, __func__);
956 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
957 D1("BULK IN callback but driver is not active!");
960 usb_mark_last_busy(urb->dev);
964 if (!netif_device_present(net)) {
965 /* Somebody killed our network interface... */
969 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
971 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
972 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
973 if (((rest == 5) || (rest == 6))
974 && !memcmp(((u8 *) urb->transfer_buffer) +
975 urb->actual_length - 4, crc_check, 4)) {
976 urb->actual_length -= 4;
980 /* do we even have a packet? */
981 if (urb->actual_length) {
982 /* Handle the IP stream, add header and push it onto network
983 * stack if the packet is complete. */
984 spin_lock(&odev->net_lock);
985 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
986 (urb->transfer_buffer_length >
987 urb->actual_length) ? 1 : 0);
988 spin_unlock(&odev->net_lock);
991 /* We are done with this URB, resubmit it. Prep the USB to wait for
992 * another frame. Reuse same as received. */
993 usb_fill_bulk_urb(urb,
995 usb_rcvbulkpipe(odev->parent->usb,
997 bEndpointAddress & 0x7F),
998 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
999 read_bulk_callback, odev);
1001 /* Give this to the USB subsystem so it can tell us when more data
1003 result = usb_submit_urb(urb, GFP_ATOMIC);
1005 dev_warn(&odev->parent->interface->dev,
1006 "%s failed submit mux_bulk_rx_urb %d", __func__,
1010 /* Serial driver functions */
1012 static void _hso_serial_set_termios(struct tty_struct *tty,
1013 struct ktermios *old)
1015 struct hso_serial *serial = get_serial_by_tty(tty);
1016 struct ktermios *termios;
1019 printk(KERN_ERR "%s: no tty structures", __func__);
1023 D4("port %d", serial->minor);
1026 * The default requirements for this device are:
1028 termios = tty->termios;
1030 ~(IGNBRK /* disable ignore break */
1031 | BRKINT /* disable break causes interrupt */
1032 | PARMRK /* disable mark parity errors */
1033 | ISTRIP /* disable clear high bit of input characters */
1034 | INLCR /* disable translate NL to CR */
1035 | IGNCR /* disable ignore CR */
1036 | ICRNL /* disable translate CR to NL */
1037 | IXON); /* disable enable XON/XOFF flow control */
1039 /* disable postprocess output characters */
1040 termios->c_oflag &= ~OPOST;
1043 ~(ECHO /* disable echo input characters */
1044 | ECHONL /* disable echo new line */
1045 | ICANON /* disable erase, kill, werase, and rprnt
1046 special characters */
1047 | ISIG /* disable interrupt, quit, and suspend special
1049 | IEXTEN); /* disable non-POSIX special characters */
1052 ~(CSIZE /* no size */
1053 | PARENB /* disable parity bit */
1054 | CBAUD /* clear current baud rate */
1055 | CBAUDEX); /* clear current buad rate */
1057 termios->c_cflag |= CS8; /* character size 8 bits */
1059 /* baud rate 115200 */
1060 tty_encode_baud_rate(tty, 115200, 115200);
1063 * Force low_latency on; otherwise the pushes are scheduled;
1064 * this is bad as it opens up the possibility of dropping bytes
1065 * on the floor. We don't want to drop bytes on the floor. :)
1067 tty->low_latency = 1;
1071 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1074 #ifdef CONFIG_HSO_AUTOPM
1075 usb_mark_last_busy(urb->dev);
1077 /* We are done with this URB, resubmit it. Prep the USB to wait for
1079 usb_fill_bulk_urb(urb, serial->parent->usb,
1080 usb_rcvbulkpipe(serial->parent->usb,
1082 bEndpointAddress & 0x7F),
1083 urb->transfer_buffer, serial->rx_data_length,
1084 hso_std_serial_read_bulk_callback, serial);
1085 /* Give this to the USB subsystem so it can tell us when more data
1087 result = usb_submit_urb(urb, GFP_ATOMIC);
1089 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1097 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1100 struct urb *curr_urb;
1102 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1103 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1104 count = put_rxbuf_data(curr_urb, serial);
1108 serial->curr_rx_urb_idx++;
1109 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1110 serial->curr_rx_urb_idx = 0;
1111 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1116 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1121 urb = serial->rx_urb[0];
1122 if (serial->open_count > 0) {
1123 count = put_rxbuf_data(urb, serial);
1127 /* Re issue a read as long as we receive data. */
1129 if (count == 0 && ((urb->actual_length != 0) ||
1130 (serial->rx_state == RX_PENDING))) {
1131 serial->rx_state = RX_SENT;
1132 hso_mux_serial_read(serial);
1134 serial->rx_state = RX_IDLE;
1138 /* read callback for Diag and CS port */
1139 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1141 struct hso_serial *serial = urb->context;
1142 int status = urb->status;
1146 D1("serial == NULL");
1148 } else if (status) {
1149 log_usb_status(status, __func__);
1153 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1154 D1("Actual length = %d\n", urb->actual_length);
1155 DUMP1(urb->transfer_buffer, urb->actual_length);
1157 /* Anyone listening? */
1158 if (serial->open_count == 0)
1162 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1164 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1166 urb->actual_length %
1167 serial->in_endp->wMaxPacketSize;
1168 if (((rest == 5) || (rest == 6))
1169 && !memcmp(((u8 *) urb->transfer_buffer) +
1170 urb->actual_length - 4, crc_check, 4)) {
1171 urb->actual_length -= 4;
1174 /* Valid data, handle RX data */
1175 spin_lock(&serial->serial_lock);
1176 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1177 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1178 spin_unlock(&serial->serial_lock);
1179 } else if (status == -ENOENT || status == -ECONNRESET) {
1180 /* Unlinked - check for throttled port. */
1181 D2("Port %d, successfully unlinked urb", serial->minor);
1182 spin_lock(&serial->serial_lock);
1183 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1184 hso_resubmit_rx_bulk_urb(serial, urb);
1185 spin_unlock(&serial->serial_lock);
1187 D2("Port %d, status = %d for read urb", serial->minor, status);
1193 * This needs to be a tasklet otherwise we will
1194 * end up recursively calling this function.
1196 void hso_unthrottle_tasklet(struct hso_serial *serial)
1198 unsigned long flags;
1200 spin_lock_irqsave(&serial->serial_lock, flags);
1201 if ((serial->parent->port_spec & HSO_INTF_MUX))
1202 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1204 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1205 spin_unlock_irqrestore(&serial->serial_lock, flags);
1208 static void hso_unthrottle(struct tty_struct *tty)
1210 struct hso_serial *serial = get_serial_by_tty(tty);
1212 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1215 void hso_unthrottle_workfunc(struct work_struct *work)
1217 struct hso_serial *serial =
1218 container_of(work, struct hso_serial,
1219 retry_unthrottle_workqueue);
1220 hso_unthrottle_tasklet(serial);
1223 /* open the requested serial port */
1224 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1226 struct hso_serial *serial = get_serial_by_index(tty->index);
1230 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1232 tty->driver_data = NULL;
1233 D1("Failed to open port");
1237 mutex_lock(&serial->parent->mutex);
1238 result = usb_autopm_get_interface(serial->parent->interface);
1242 D1("Opening %d", serial->minor);
1243 kref_get(&serial->parent->ref);
1246 spin_lock_irq(&serial->serial_lock);
1247 tty->driver_data = serial;
1248 serial->tty = tty_kref_get(tty);
1249 spin_unlock_irq(&serial->serial_lock);
1251 /* check for port already opened, if not set the termios */
1252 serial->open_count++;
1253 if (serial->open_count == 1) {
1254 tty->low_latency = 1;
1255 serial->rx_state = RX_IDLE;
1256 /* Force default termio settings */
1257 _hso_serial_set_termios(tty, NULL);
1258 tasklet_init(&serial->unthrottle_tasklet,
1259 (void (*)(unsigned long))hso_unthrottle_tasklet,
1260 (unsigned long)serial);
1261 INIT_WORK(&serial->retry_unthrottle_workqueue,
1262 hso_unthrottle_workfunc);
1263 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1265 hso_stop_serial_device(serial->parent);
1266 serial->open_count--;
1267 kref_put(&serial->parent->ref, hso_serial_ref_free);
1270 D1("Port was already open");
1273 usb_autopm_put_interface(serial->parent->interface);
1277 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1279 mutex_unlock(&serial->parent->mutex);
1283 /* close the requested serial port */
1284 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1286 struct hso_serial *serial = tty->driver_data;
1289 D1("Closing serial port");
1291 /* Open failed, no close cleanup required */
1295 mutex_lock(&serial->parent->mutex);
1296 usb_gone = serial->parent->usb_gone;
1299 usb_autopm_get_interface(serial->parent->interface);
1301 /* reset the rts and dtr */
1302 /* do the actual close */
1303 serial->open_count--;
1304 kref_put(&serial->parent->ref, hso_serial_ref_free);
1305 if (serial->open_count <= 0) {
1306 serial->open_count = 0;
1307 spin_lock_irq(&serial->serial_lock);
1308 if (serial->tty == tty) {
1309 serial->tty->driver_data = NULL;
1313 spin_unlock_irq(&serial->serial_lock);
1315 hso_stop_serial_device(serial->parent);
1316 tasklet_kill(&serial->unthrottle_tasklet);
1317 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1321 usb_autopm_put_interface(serial->parent->interface);
1323 mutex_unlock(&serial->parent->mutex);
1326 /* close the requested serial port */
1327 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1330 struct hso_serial *serial = get_serial_by_tty(tty);
1331 int space, tx_bytes;
1332 unsigned long flags;
1335 if (serial == NULL) {
1336 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1340 spin_lock_irqsave(&serial->serial_lock, flags);
1342 space = serial->tx_data_length - serial->tx_buffer_count;
1343 tx_bytes = (count < space) ? count : space;
1348 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1349 serial->tx_buffer_count += tx_bytes;
1352 spin_unlock_irqrestore(&serial->serial_lock, flags);
1354 hso_kick_transmit(serial);
1359 /* how much room is there for writing */
1360 static int hso_serial_write_room(struct tty_struct *tty)
1362 struct hso_serial *serial = get_serial_by_tty(tty);
1364 unsigned long flags;
1366 spin_lock_irqsave(&serial->serial_lock, flags);
1367 room = serial->tx_data_length - serial->tx_buffer_count;
1368 spin_unlock_irqrestore(&serial->serial_lock, flags);
1370 /* return free room */
1374 /* setup the term */
1375 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1377 struct hso_serial *serial = get_serial_by_tty(tty);
1378 unsigned long flags;
1381 D5("Termios called with: cflags new[%d] - old[%d]",
1382 tty->termios->c_cflag, old->c_cflag);
1384 /* the actual setup */
1385 spin_lock_irqsave(&serial->serial_lock, flags);
1386 if (serial->open_count)
1387 _hso_serial_set_termios(tty, old);
1390 spin_unlock_irqrestore(&serial->serial_lock, flags);
1396 /* how many characters in the buffer */
1397 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1399 struct hso_serial *serial = get_serial_by_tty(tty);
1401 unsigned long flags;
1407 spin_lock_irqsave(&serial->serial_lock, flags);
1408 chars = serial->tx_buffer_count;
1409 spin_unlock_irqrestore(&serial->serial_lock, flags);
1414 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1417 struct hso_serial *serial = get_serial_by_tty(tty);
1418 unsigned long flags;
1422 D1("no tty structures");
1426 spin_lock_irqsave(&serial->serial_lock, flags);
1427 value = ((serial->rts_state) ? TIOCM_RTS : 0) |
1428 ((serial->dtr_state) ? TIOCM_DTR : 0);
1429 spin_unlock_irqrestore(&serial->serial_lock, flags);
1434 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1435 unsigned int set, unsigned int clear)
1438 unsigned long flags;
1440 struct hso_serial *serial = get_serial_by_tty(tty);
1444 D1("no tty structures");
1447 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1449 spin_lock_irqsave(&serial->serial_lock, flags);
1450 if (set & TIOCM_RTS)
1451 serial->rts_state = 1;
1452 if (set & TIOCM_DTR)
1453 serial->dtr_state = 1;
1455 if (clear & TIOCM_RTS)
1456 serial->rts_state = 0;
1457 if (clear & TIOCM_DTR)
1458 serial->dtr_state = 0;
1460 if (serial->dtr_state)
1462 if (serial->rts_state)
1465 spin_unlock_irqrestore(&serial->serial_lock, flags);
1467 return usb_control_msg(serial->parent->usb,
1468 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1469 0x21, val, if_num, NULL, 0,
1470 USB_CTRL_SET_TIMEOUT);
1473 /* starts a transmit */
1474 static void hso_kick_transmit(struct hso_serial *serial)
1477 unsigned long flags;
1480 spin_lock_irqsave(&serial->serial_lock, flags);
1481 if (!serial->tx_buffer_count)
1484 if (serial->tx_urb_used)
1487 /* Wakeup USB interface if necessary */
1488 if (hso_get_activity(serial->parent) == -EAGAIN)
1491 /* Switch pointers around to avoid memcpy */
1492 temp = serial->tx_buffer;
1493 serial->tx_buffer = serial->tx_data;
1494 serial->tx_data = temp;
1495 serial->tx_data_count = serial->tx_buffer_count;
1496 serial->tx_buffer_count = 0;
1498 /* If temp is set, it means we switched buffers */
1499 if (temp && serial->write_data) {
1500 res = serial->write_data(serial);
1502 serial->tx_urb_used = 1;
1505 spin_unlock_irqrestore(&serial->serial_lock, flags);
1508 /* make a request (for reading and writing data to muxed serial port) */
1509 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1510 struct urb *ctrl_urb,
1511 struct usb_ctrlrequest *ctrl_req,
1512 u8 *ctrl_urb_data, u32 size)
1518 if (!serial || !ctrl_urb || !ctrl_req) {
1519 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1524 ctrl_req->wValue = 0;
1525 ctrl_req->wIndex = hso_port_to_mux(port);
1526 ctrl_req->wLength = size;
1528 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1529 /* Reading command */
1530 ctrl_req->bRequestType = USB_DIR_IN |
1531 USB_TYPE_OPTION_VENDOR |
1532 USB_RECIP_INTERFACE;
1533 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1534 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1536 /* Writing command */
1537 ctrl_req->bRequestType = USB_DIR_OUT |
1538 USB_TYPE_OPTION_VENDOR |
1539 USB_RECIP_INTERFACE;
1540 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1541 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1544 D2("%s command (%02x) len: %d, port: %d",
1545 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1546 ctrl_req->bRequestType, ctrl_req->wLength, port);
1549 ctrl_urb->transfer_flags = 0;
1550 usb_fill_control_urb(ctrl_urb,
1551 serial->parent->usb,
1554 ctrl_urb_data, size, ctrl_callback, serial);
1555 /* Send it on merry way */
1556 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1558 dev_err(&ctrl_urb->dev->dev,
1559 "%s failed submit ctrl_urb %d type %d", __func__,
1568 /* called by intr_callback when read occurs */
1569 static int hso_mux_serial_read(struct hso_serial *serial)
1575 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1576 /* make the request */
1578 if (serial->num_rx_urbs != 1) {
1579 dev_err(&serial->parent->interface->dev,
1580 "ERROR: mux'd reads with multiple buffers "
1584 return mux_device_request(serial,
1585 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1586 serial->parent->port_spec & HSO_PORT_MASK,
1588 &serial->ctrl_req_rx,
1589 serial->rx_data[0], serial->rx_data_length);
1592 /* used for muxed serial port callback (muxed serial read) */
1593 static void intr_callback(struct urb *urb)
1595 struct hso_shared_int *shared_int = urb->context;
1596 struct hso_serial *serial;
1597 unsigned char *port_req;
1598 int status = urb->status;
1601 usb_mark_last_busy(urb->dev);
1609 log_usb_status(status, __func__);
1612 D4("\n--- Got intr callback 0x%02X ---", status);
1615 port_req = urb->transfer_buffer;
1616 D4(" port_req = 0x%.2X\n", *port_req);
1617 /* loop over all muxed ports to find the one sending this */
1618 for (i = 0; i < 8; i++) {
1619 /* max 8 channels on MUX */
1620 if (*port_req & (1 << i)) {
1621 serial = get_serial_by_shared_int_and_type(shared_int,
1623 if (serial != NULL) {
1624 D1("Pending read interrupt on port %d\n", i);
1625 spin_lock(&serial->serial_lock);
1626 if (serial->rx_state == RX_IDLE) {
1627 /* Setup and send a ctrl req read on
1629 if (!serial->rx_urb_filled[0]) {
1630 serial->rx_state = RX_SENT;
1631 hso_mux_serial_read(serial);
1633 serial->rx_state = RX_PENDING;
1636 D1("Already pending a read on "
1639 spin_unlock(&serial->serial_lock);
1643 /* Resubmit interrupt urb */
1644 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1647 /* called for writing to muxed serial port */
1648 static int hso_mux_serial_write_data(struct hso_serial *serial)
1653 return mux_device_request(serial,
1654 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1655 serial->parent->port_spec & HSO_PORT_MASK,
1657 &serial->ctrl_req_tx,
1658 serial->tx_data, serial->tx_data_count);
1661 /* write callback for Diag and CS port */
1662 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1664 struct hso_serial *serial = urb->context;
1665 int status = urb->status;
1666 struct tty_struct *tty;
1670 D1("serial == NULL");
1674 spin_lock(&serial->serial_lock);
1675 serial->tx_urb_used = 0;
1676 tty = tty_kref_get(serial->tty);
1677 spin_unlock(&serial->serial_lock);
1679 log_usb_status(status, __func__);
1683 hso_put_activity(serial->parent);
1688 hso_kick_transmit(serial);
1694 /* called for writing diag or CS serial port */
1695 static int hso_std_serial_write_data(struct hso_serial *serial)
1697 int count = serial->tx_data_count;
1700 usb_fill_bulk_urb(serial->tx_urb,
1701 serial->parent->usb,
1702 usb_sndbulkpipe(serial->parent->usb,
1704 bEndpointAddress & 0x7F),
1705 serial->tx_data, serial->tx_data_count,
1706 hso_std_serial_write_bulk_callback, serial);
1708 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1710 dev_warn(&serial->parent->usb->dev,
1711 "Failed to submit urb - res %d\n", result);
1718 /* callback after read or write on muxed serial port */
1719 static void ctrl_callback(struct urb *urb)
1721 struct hso_serial *serial = urb->context;
1722 struct usb_ctrlrequest *req;
1723 int status = urb->status;
1724 struct tty_struct *tty;
1730 spin_lock(&serial->serial_lock);
1731 serial->tx_urb_used = 0;
1732 tty = tty_kref_get(serial->tty);
1733 spin_unlock(&serial->serial_lock);
1735 log_usb_status(status, __func__);
1741 req = (struct usb_ctrlrequest *)(urb->setup_packet);
1742 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1743 D4("Actual length of urb = %d\n", urb->actual_length);
1744 DUMP1(urb->transfer_buffer, urb->actual_length);
1746 if (req->bRequestType ==
1747 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1748 /* response to a read command */
1749 serial->rx_urb_filled[0] = 1;
1750 spin_lock(&serial->serial_lock);
1751 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1752 spin_unlock(&serial->serial_lock);
1754 hso_put_activity(serial->parent);
1757 /* response to a write command */
1758 hso_kick_transmit(serial);
1763 /* handle RX data for serial port */
1764 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
1766 struct tty_struct *tty;
1767 int write_length_remaining = 0;
1771 if (urb == NULL || serial == NULL) {
1772 D1("serial = NULL");
1776 spin_lock(&serial->serial_lock);
1777 tty = tty_kref_get(serial->tty);
1778 spin_unlock(&serial->serial_lock);
1780 /* Push data to tty */
1782 write_length_remaining = urb->actual_length -
1783 serial->curr_rx_urb_offset;
1784 D1("data to push to tty");
1785 while (write_length_remaining) {
1786 if (test_bit(TTY_THROTTLED, &tty->flags))
1788 curr_write_len = tty_insert_flip_string
1789 (tty, urb->transfer_buffer +
1790 serial->curr_rx_urb_offset,
1791 write_length_remaining);
1792 serial->curr_rx_urb_offset += curr_write_len;
1793 write_length_remaining -= curr_write_len;
1794 tty_flip_buffer_push(tty);
1797 if (write_length_remaining == 0) {
1798 serial->curr_rx_urb_offset = 0;
1799 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1802 return write_length_remaining;
1806 /* Base driver functions */
1808 static void hso_log_port(struct hso_device *hso_dev)
1813 switch (hso_dev->port_spec & HSO_PORT_MASK) {
1814 case HSO_PORT_CONTROL:
1815 port_type = "Control";
1818 port_type = "Application";
1823 case HSO_PORT_GPS_CONTROL:
1824 port_type = "GPS control";
1827 port_type = "Application2";
1833 port_type = "Diagnostic";
1835 case HSO_PORT_DIAG2:
1836 port_type = "Diagnostic2";
1838 case HSO_PORT_MODEM:
1839 port_type = "Modem";
1841 case HSO_PORT_NETWORK:
1842 port_type = "Network";
1845 port_type = "Unknown";
1848 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
1849 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
1851 sprintf(port_dev, "/dev/%s%d", tty_filename,
1852 dev2ser(hso_dev)->minor);
1854 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
1855 port_type, port_dev);
1858 static int hso_start_net_device(struct hso_device *hso_dev)
1861 struct hso_net *hso_net = dev2net(hso_dev);
1866 /* send URBs for all read buffers */
1867 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1869 /* Prep a receive URB */
1870 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
1872 usb_rcvbulkpipe(hso_dev->usb,
1874 bEndpointAddress & 0x7F),
1875 hso_net->mux_bulk_rx_buf_pool[i],
1876 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
1879 /* Put it out there so the device can send us stuff */
1880 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
1883 dev_warn(&hso_dev->usb->dev,
1884 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
1891 static int hso_stop_net_device(struct hso_device *hso_dev)
1894 struct hso_net *hso_net = dev2net(hso_dev);
1899 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1900 if (hso_net->mux_bulk_rx_urb_pool[i])
1901 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1904 if (hso_net->mux_bulk_tx_urb)
1905 usb_kill_urb(hso_net->mux_bulk_tx_urb);
1910 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
1913 struct hso_serial *serial = dev2ser(hso_dev);
1918 /* If it is not the MUX port fill in and submit a bulk urb (already
1919 * allocated in hso_serial_start) */
1920 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
1921 for (i = 0; i < serial->num_rx_urbs; i++) {
1922 usb_fill_bulk_urb(serial->rx_urb[i],
1923 serial->parent->usb,
1924 usb_rcvbulkpipe(serial->parent->usb,
1929 serial->rx_data_length,
1930 hso_std_serial_read_bulk_callback,
1932 result = usb_submit_urb(serial->rx_urb[i], flags);
1934 dev_warn(&serial->parent->usb->dev,
1935 "Failed to submit urb - res %d\n",
1941 mutex_lock(&serial->shared_int->shared_int_lock);
1942 if (!serial->shared_int->use_count) {
1944 hso_mux_submit_intr_urb(serial->shared_int,
1945 hso_dev->usb, flags);
1947 serial->shared_int->use_count++;
1948 mutex_unlock(&serial->shared_int->shared_int_lock);
1954 static int hso_stop_serial_device(struct hso_device *hso_dev)
1957 struct hso_serial *serial = dev2ser(hso_dev);
1962 for (i = 0; i < serial->num_rx_urbs; i++) {
1963 if (serial->rx_urb[i]) {
1964 usb_kill_urb(serial->rx_urb[i]);
1965 serial->rx_urb_filled[i] = 0;
1968 serial->curr_rx_urb_idx = 0;
1969 serial->curr_rx_urb_offset = 0;
1972 usb_kill_urb(serial->tx_urb);
1974 if (serial->shared_int) {
1975 mutex_lock(&serial->shared_int->shared_int_lock);
1976 if (serial->shared_int->use_count &&
1977 (--serial->shared_int->use_count == 0)) {
1980 urb = serial->shared_int->shared_intr_urb;
1984 mutex_unlock(&serial->shared_int->shared_int_lock);
1990 static void hso_serial_common_free(struct hso_serial *serial)
1994 if (serial->parent->dev)
1995 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
1997 tty_unregister_device(tty_drv, serial->minor);
1999 for (i = 0; i < serial->num_rx_urbs; i++) {
2000 /* unlink and free RX URB */
2001 usb_free_urb(serial->rx_urb[i]);
2002 /* free the RX buffer */
2003 kfree(serial->rx_data[i]);
2006 /* unlink and free TX URB */
2007 usb_free_urb(serial->tx_urb);
2008 kfree(serial->tx_data);
2011 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2012 int rx_size, int tx_size)
2018 minor = get_free_serial_index();
2022 /* register our minor number */
2023 serial->parent->dev = tty_register_device(tty_drv, minor,
2024 &serial->parent->interface->dev);
2025 dev = serial->parent->dev;
2026 dev->driver_data = serial->parent;
2027 i = device_create_file(dev, &dev_attr_hsotype);
2029 /* fill in specific data for later use */
2030 serial->minor = minor;
2031 serial->magic = HSO_SERIAL_MAGIC;
2032 spin_lock_init(&serial->serial_lock);
2033 serial->num_rx_urbs = num_urbs;
2035 /* RX, allocate urb and initialize */
2037 /* prepare our RX buffer */
2038 serial->rx_data_length = rx_size;
2039 for (i = 0; i < serial->num_rx_urbs; i++) {
2040 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2041 if (!serial->rx_urb[i]) {
2042 dev_err(dev, "Could not allocate urb?\n");
2045 serial->rx_urb[i]->transfer_buffer = NULL;
2046 serial->rx_urb[i]->transfer_buffer_length = 0;
2047 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2049 if (!serial->rx_data[i]) {
2050 dev_err(dev, "%s - Out of memory\n", __func__);
2055 /* TX, allocate urb and initialize */
2056 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2057 if (!serial->tx_urb) {
2058 dev_err(dev, "Could not allocate urb?\n");
2061 serial->tx_urb->transfer_buffer = NULL;
2062 serial->tx_urb->transfer_buffer_length = 0;
2063 /* prepare our TX buffer */
2064 serial->tx_data_count = 0;
2065 serial->tx_buffer_count = 0;
2066 serial->tx_data_length = tx_size;
2067 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2068 if (!serial->tx_data) {
2069 dev_err(dev, "%s - Out of memory", __func__);
2072 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2073 if (!serial->tx_buffer) {
2074 dev_err(dev, "%s - Out of memory", __func__);
2080 hso_serial_common_free(serial);
2084 /* Frees a general hso device */
2085 static void hso_free_device(struct hso_device *hso_dev)
2090 /* Creates a general hso device */
2091 static struct hso_device *hso_create_device(struct usb_interface *intf,
2094 struct hso_device *hso_dev;
2096 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2100 hso_dev->port_spec = port_spec;
2101 hso_dev->usb = interface_to_usbdev(intf);
2102 hso_dev->interface = intf;
2103 kref_init(&hso_dev->ref);
2104 mutex_init(&hso_dev->mutex);
2106 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2107 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2112 /* Removes a network device in the network device table */
2113 static int remove_net_device(struct hso_device *hso_dev)
2117 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2118 if (network_table[i] == hso_dev) {
2119 network_table[i] = NULL;
2123 if (i == HSO_MAX_NET_DEVICES)
2128 /* Frees our network device */
2129 static void hso_free_net_device(struct hso_device *hso_dev)
2132 struct hso_net *hso_net = dev2net(hso_dev);
2138 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2139 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2140 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2142 usb_free_urb(hso_net->mux_bulk_tx_urb);
2143 kfree(hso_net->mux_bulk_tx_buf);
2145 remove_net_device(hso_net->parent);
2148 unregister_netdev(hso_net->net);
2149 free_netdev(hso_net->net);
2152 hso_free_device(hso_dev);
2155 /* initialize the network interface */
2156 static void hso_net_init(struct net_device *net)
2158 struct hso_net *hso_net = netdev_priv(net);
2160 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2162 /* fill in the other fields */
2163 net->open = hso_net_open;
2164 net->stop = hso_net_close;
2165 net->hard_start_xmit = hso_net_start_xmit;
2166 net->tx_timeout = hso_net_tx_timeout;
2167 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2168 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2169 net->type = ARPHRD_NONE;
2170 net->mtu = DEFAULT_MTU - 14;
2171 net->tx_queue_len = 10;
2172 SET_ETHTOOL_OPS(net, &ops);
2174 /* and initialize the semaphore */
2175 spin_lock_init(&hso_net->net_lock);
2178 /* Adds a network device in the network device table */
2179 static int add_net_device(struct hso_device *hso_dev)
2183 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2184 if (network_table[i] == NULL) {
2185 network_table[i] = hso_dev;
2189 if (i == HSO_MAX_NET_DEVICES)
2194 static int hso_radio_toggle(void *data, enum rfkill_state state)
2196 struct hso_device *hso_dev = data;
2197 int enabled = (state == RFKILL_STATE_ON);
2200 mutex_lock(&hso_dev->mutex);
2201 if (hso_dev->usb_gone)
2204 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2205 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2206 USB_CTRL_SET_TIMEOUT);
2207 mutex_unlock(&hso_dev->mutex);
2211 /* Creates and sets up everything for rfkill */
2212 static void hso_create_rfkill(struct hso_device *hso_dev,
2213 struct usb_interface *interface)
2215 struct hso_net *hso_net = dev2net(hso_dev);
2216 struct device *dev = &hso_net->net->dev;
2219 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2221 if (!hso_net->rfkill) {
2222 dev_err(dev, "%s - Out of memory\n", __func__);
2225 rfkn = kzalloc(20, GFP_KERNEL);
2227 rfkill_free(hso_net->rfkill);
2228 hso_net->rfkill = NULL;
2229 dev_err(dev, "%s - Out of memory\n", __func__);
2232 snprintf(rfkn, 20, "hso-%d",
2233 interface->altsetting->desc.bInterfaceNumber);
2234 hso_net->rfkill->name = rfkn;
2235 hso_net->rfkill->state = RFKILL_STATE_ON;
2236 hso_net->rfkill->data = hso_dev;
2237 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2238 if (rfkill_register(hso_net->rfkill) < 0) {
2240 hso_net->rfkill->name = NULL;
2241 rfkill_free(hso_net->rfkill);
2242 hso_net->rfkill = NULL;
2243 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2248 /* Creates our network device */
2249 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2252 struct net_device *net;
2253 struct hso_net *hso_net;
2254 struct hso_device *hso_dev;
2256 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2260 /* allocate our network device, then we can put in our private data */
2261 /* call hso_net_init to do the basic initialization */
2262 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2264 dev_err(&interface->dev, "Unable to create ethernet device\n");
2268 hso_net = netdev_priv(net);
2270 hso_dev->port_data.dev_net = hso_net;
2272 hso_net->parent = hso_dev;
2274 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2276 if (!hso_net->in_endp) {
2277 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2280 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2282 if (!hso_net->out_endp) {
2283 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2286 SET_NETDEV_DEV(net, &interface->dev);
2288 /* registering our net device */
2289 result = register_netdev(net);
2291 dev_err(&interface->dev, "Failed to register device\n");
2295 /* start allocating */
2296 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2297 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2298 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2299 dev_err(&interface->dev, "Could not allocate rx urb\n");
2302 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2304 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2305 dev_err(&interface->dev, "Could not allocate rx buf\n");
2309 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2310 if (!hso_net->mux_bulk_tx_urb) {
2311 dev_err(&interface->dev, "Could not allocate tx urb\n");
2314 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2315 if (!hso_net->mux_bulk_tx_buf) {
2316 dev_err(&interface->dev, "Could not allocate tx buf\n");
2320 add_net_device(hso_dev);
2322 hso_log_port(hso_dev);
2324 hso_create_rfkill(hso_dev, interface);
2328 hso_free_net_device(hso_dev);
2332 /* Frees an AT channel ( goes for both mux and non-mux ) */
2333 static void hso_free_serial_device(struct hso_device *hso_dev)
2335 struct hso_serial *serial = dev2ser(hso_dev);
2339 set_serial_by_index(serial->minor, NULL);
2341 hso_serial_common_free(serial);
2343 if (serial->shared_int) {
2344 mutex_lock(&serial->shared_int->shared_int_lock);
2345 if (--serial->shared_int->ref_count == 0)
2346 hso_free_shared_int(serial->shared_int);
2348 mutex_unlock(&serial->shared_int->shared_int_lock);
2351 hso_free_device(hso_dev);
2354 /* Creates a bulk AT channel */
2355 static struct hso_device *hso_create_bulk_serial_device(
2356 struct usb_interface *interface, int port)
2358 struct hso_device *hso_dev;
2359 struct hso_serial *serial;
2362 hso_dev = hso_create_device(interface, port);
2366 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2370 serial->parent = hso_dev;
2371 hso_dev->port_data.dev_serial = serial;
2373 if (port & HSO_PORT_MODEM)
2378 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2382 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2384 if (!serial->in_endp) {
2385 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2391 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2392 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2396 serial->write_data = hso_std_serial_write_data;
2398 /* and record this serial */
2399 set_serial_by_index(serial->minor, serial);
2401 /* setup the proc dirs and files if needed */
2402 hso_log_port(hso_dev);
2404 /* done, return it */
2408 hso_serial_common_free(serial);
2411 hso_free_device(hso_dev);
2415 /* Creates a multiplexed AT channel */
2417 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2419 struct hso_shared_int *mux)
2421 struct hso_device *hso_dev;
2422 struct hso_serial *serial;
2425 port_spec = HSO_INTF_MUX;
2426 port_spec &= ~HSO_PORT_MASK;
2428 port_spec |= hso_mux_to_port(port);
2429 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2432 hso_dev = hso_create_device(interface, port_spec);
2436 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2440 hso_dev->port_data.dev_serial = serial;
2441 serial->parent = hso_dev;
2443 if (hso_serial_common_create
2444 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2447 serial->tx_data_length--;
2448 serial->write_data = hso_mux_serial_write_data;
2450 serial->shared_int = mux;
2451 mutex_lock(&serial->shared_int->shared_int_lock);
2452 serial->shared_int->ref_count++;
2453 mutex_unlock(&serial->shared_int->shared_int_lock);
2455 /* and record this serial */
2456 set_serial_by_index(serial->minor, serial);
2458 /* setup the proc dirs and files if needed */
2459 hso_log_port(hso_dev);
2461 /* done, return it */
2466 tty_unregister_device(tty_drv, serial->minor);
2470 hso_free_device(hso_dev);
2475 static void hso_free_shared_int(struct hso_shared_int *mux)
2477 usb_free_urb(mux->shared_intr_urb);
2478 kfree(mux->shared_intr_buf);
2479 mutex_unlock(&mux->shared_int_lock);
2484 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2486 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2491 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2493 if (!mux->intr_endp) {
2494 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2498 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2499 if (!mux->shared_intr_urb) {
2500 dev_err(&interface->dev, "Could not allocate intr urb?");
2503 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2505 if (!mux->shared_intr_buf) {
2506 dev_err(&interface->dev, "Could not allocate intr buf?");
2510 mutex_init(&mux->shared_int_lock);
2515 kfree(mux->shared_intr_buf);
2516 usb_free_urb(mux->shared_intr_urb);
2521 /* Gets the port spec for a certain interface */
2522 static int hso_get_config_data(struct usb_interface *interface)
2524 struct usb_device *usbdev = interface_to_usbdev(interface);
2526 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2529 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2530 0x86, 0xC0, 0, 0, config_data, 17,
2531 USB_CTRL_SET_TIMEOUT) != 0x11) {
2535 switch (config_data[if_num]) {
2540 result = HSO_PORT_DIAG;
2543 result = HSO_PORT_GPS;
2546 result = HSO_PORT_GPS_CONTROL;
2549 result = HSO_PORT_APP;
2552 result = HSO_PORT_APP2;
2555 result = HSO_PORT_CONTROL;
2558 result = HSO_PORT_NETWORK;
2561 result = HSO_PORT_MODEM;
2564 result = HSO_PORT_MSD;
2567 result = HSO_PORT_PCSC;
2570 result = HSO_PORT_VOICE;
2577 result |= HSO_INTF_BULK;
2579 if (config_data[16] & 0x1)
2580 result |= HSO_INFO_CRC_BUG;
2585 /* called once for each interface upon device insertion */
2586 static int hso_probe(struct usb_interface *interface,
2587 const struct usb_device_id *id)
2589 int mux, i, if_num, port_spec;
2590 unsigned char port_mask;
2591 struct hso_device *hso_dev = NULL;
2592 struct hso_shared_int *shared_int;
2593 struct hso_device *tmp_dev = NULL;
2595 if_num = interface->altsetting->desc.bInterfaceNumber;
2597 /* Get the interface/port specification from either driver_info or from
2598 * the device itself */
2599 if (id->driver_info)
2600 port_spec = ((u32 *)(id->driver_info))[if_num];
2602 port_spec = hso_get_config_data(interface);
2604 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2605 dev_err(&interface->dev, "Not our interface\n");
2608 /* Check if we need to switch to alt interfaces prior to port
2610 if (interface->num_altsetting > 1)
2611 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2612 interface->needs_remote_wakeup = 1;
2614 /* Allocate new hso device(s) */
2615 switch (port_spec & HSO_INTF_MASK) {
2617 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2618 /* Create the network device */
2620 hso_dev = hso_create_net_device(interface);
2627 if (hso_get_mux_ports(interface, &port_mask))
2628 /* TODO: de-allocate everything */
2631 shared_int = hso_create_shared_int(interface);
2635 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2636 if (port_mask & i) {
2637 hso_dev = hso_create_mux_serial_device(
2638 interface, i, shared_int);
2649 /* It's a regular bulk interface */
2650 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2652 hso_dev = hso_create_net_device(interface);
2655 hso_create_bulk_serial_device(interface, port_spec);
2663 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2665 /* save our data pointer in this device */
2666 usb_set_intfdata(interface, hso_dev);
2671 hso_free_interface(interface);
2675 /* device removed, cleaning up */
2676 static void hso_disconnect(struct usb_interface *interface)
2678 hso_free_interface(interface);
2680 /* remove reference of our private data */
2681 usb_set_intfdata(interface, NULL);
2683 usb_driver_release_interface(&hso_driver, interface);
2686 static void async_get_intf(struct work_struct *data)
2688 struct hso_device *hso_dev =
2689 container_of(data, struct hso_device, async_get_intf);
2690 usb_autopm_get_interface(hso_dev->interface);
2693 static void async_put_intf(struct work_struct *data)
2695 struct hso_device *hso_dev =
2696 container_of(data, struct hso_device, async_put_intf);
2697 usb_autopm_put_interface(hso_dev->interface);
2700 static int hso_get_activity(struct hso_device *hso_dev)
2702 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2703 if (!hso_dev->is_active) {
2704 hso_dev->is_active = 1;
2705 schedule_work(&hso_dev->async_get_intf);
2709 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2712 usb_mark_last_busy(hso_dev->usb);
2717 static int hso_put_activity(struct hso_device *hso_dev)
2719 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2720 if (hso_dev->is_active) {
2721 hso_dev->is_active = 0;
2722 schedule_work(&hso_dev->async_put_intf);
2726 hso_dev->is_active = 0;
2730 /* called by kernel when we need to suspend device */
2731 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2735 /* Stop all serial ports */
2736 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2737 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2738 result = hso_stop_serial_device(serial_table[i]);
2744 /* Stop all network ports */
2745 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2746 if (network_table[i] &&
2747 (network_table[i]->interface == iface)) {
2748 result = hso_stop_net_device(network_table[i]);
2758 /* called by kernel when we need to resume device */
2759 static int hso_resume(struct usb_interface *iface)
2762 struct hso_net *hso_net;
2764 /* Start all serial ports */
2765 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2766 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2767 if (dev2ser(serial_table[i])->open_count) {
2769 hso_start_serial_device(serial_table[i], GFP_NOIO);
2770 hso_kick_transmit(dev2ser(serial_table[i]));
2777 /* Start all network ports */
2778 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2779 if (network_table[i] &&
2780 (network_table[i]->interface == iface)) {
2781 hso_net = dev2net(network_table[i]);
2782 if (hso_net->flags & IFF_UP) {
2783 /* First transmit any lingering data,
2784 then restart the device. */
2785 if (hso_net->skb_tx_buf) {
2786 dev_dbg(&iface->dev,
2788 " lingering data\n");
2789 hso_net_start_xmit(hso_net->skb_tx_buf,
2791 hso_net->skb_tx_buf = NULL;
2793 result = hso_start_net_device(network_table[i]);
2804 static void hso_serial_ref_free(struct kref *ref)
2806 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2808 hso_free_serial_device(hso_dev);
2811 static void hso_free_interface(struct usb_interface *interface)
2813 struct hso_serial *hso_dev;
2814 struct tty_struct *tty;
2817 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2819 && (serial_table[i]->interface == interface)) {
2820 hso_dev = dev2ser(serial_table[i]);
2821 spin_lock_irq(&hso_dev->serial_lock);
2822 tty = tty_kref_get(hso_dev->tty);
2823 spin_unlock_irq(&hso_dev->serial_lock);
2826 mutex_lock(&hso_dev->parent->mutex);
2828 hso_dev->parent->usb_gone = 1;
2829 mutex_unlock(&hso_dev->parent->mutex);
2830 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2834 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2835 if (network_table[i]
2836 && (network_table[i]->interface == interface)) {
2837 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2838 /* hso_stop_net_device doesn't stop the net queue since
2839 * traffic needs to start it again when suspended */
2840 netif_stop_queue(dev2net(network_table[i])->net);
2841 hso_stop_net_device(network_table[i]);
2842 cancel_work_sync(&network_table[i]->async_put_intf);
2843 cancel_work_sync(&network_table[i]->async_get_intf);
2845 rfkill_unregister(rfk);
2846 hso_free_net_device(network_table[i]);
2851 /* Helper functions */
2853 /* Get the endpoint ! */
2854 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2858 struct usb_host_interface *iface = intf->cur_altsetting;
2859 struct usb_endpoint_descriptor *endp;
2861 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2862 endp = &iface->endpoint[i].desc;
2863 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2864 (usb_endpoint_type(endp) == type))
2871 /* Get the byte that describes which ports are enabled */
2872 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2875 struct usb_host_interface *iface = intf->cur_altsetting;
2877 if (iface->extralen == 3) {
2878 *ports = iface->extra[2];
2882 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2883 if (iface->endpoint[i].extralen == 3) {
2884 *ports = iface->endpoint[i].extra[2];
2892 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2893 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2894 struct usb_device *usb, gfp_t gfp)
2898 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2900 shared_int->intr_endp->bEndpointAddress & 0x7F),
2901 shared_int->shared_intr_buf,
2902 shared_int->intr_endp->wMaxPacketSize,
2903 intr_callback, shared_int,
2904 shared_int->intr_endp->bInterval);
2906 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2908 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2914 /* operations setup of the serial interface */
2915 static const struct tty_operations hso_serial_ops = {
2916 .open = hso_serial_open,
2917 .close = hso_serial_close,
2918 .write = hso_serial_write,
2919 .write_room = hso_serial_write_room,
2920 .set_termios = hso_serial_set_termios,
2921 .chars_in_buffer = hso_serial_chars_in_buffer,
2922 .tiocmget = hso_serial_tiocmget,
2923 .tiocmset = hso_serial_tiocmset,
2924 .unthrottle = hso_unthrottle
2927 static struct usb_driver hso_driver = {
2928 .name = driver_name,
2930 .disconnect = hso_disconnect,
2931 .id_table = hso_ids,
2932 .suspend = hso_suspend,
2933 .resume = hso_resume,
2934 .reset_resume = hso_resume,
2935 .supports_autosuspend = 1,
2938 static int __init hso_init(void)
2943 /* put it in the log */
2944 printk(KERN_INFO "hso: %s\n", version);
2946 /* Initialise the serial table semaphore and table */
2947 spin_lock_init(&serial_table_lock);
2948 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2949 serial_table[i] = NULL;
2951 /* allocate our driver using the proper amount of supported minors */
2952 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2956 /* fill in all needed values */
2957 tty_drv->magic = TTY_DRIVER_MAGIC;
2958 tty_drv->owner = THIS_MODULE;
2959 tty_drv->driver_name = driver_name;
2960 tty_drv->name = tty_filename;
2962 /* if major number is provided as parameter, use that one */
2964 tty_drv->major = tty_major;
2966 tty_drv->minor_start = 0;
2967 tty_drv->num = HSO_SERIAL_TTY_MINORS;
2968 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2969 tty_drv->subtype = SERIAL_TYPE_NORMAL;
2970 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2971 tty_drv->init_termios = tty_std_termios;
2972 tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2973 tty_drv->termios = hso_serial_termios;
2974 tty_drv->termios_locked = hso_serial_termios_locked;
2975 tty_set_operations(tty_drv, &hso_serial_ops);
2977 /* register the tty driver */
2978 result = tty_register_driver(tty_drv);
2980 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2985 /* register this module as an usb driver */
2986 result = usb_register(&hso_driver);
2988 printk(KERN_ERR "Could not register hso driver? error: %d\n",
2990 /* cleanup serial interface */
2991 tty_unregister_driver(tty_drv);
2999 static void __exit hso_exit(void)
3001 printk(KERN_INFO "hso: unloaded\n");
3003 tty_unregister_driver(tty_drv);
3004 /* deregister the usb driver */
3005 usb_deregister(&hso_driver);
3008 /* Module definitions */
3009 module_init(hso_init);
3010 module_exit(hso_exit);
3012 MODULE_AUTHOR(MOD_AUTHOR);
3013 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3014 MODULE_LICENSE(MOD_LICENSE);
3015 MODULE_INFO(Version, DRIVER_VERSION);
3017 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3018 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3019 module_param(debug, int, S_IRUGO | S_IWUSR);
3021 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3022 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3023 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3025 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3026 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3027 module_param(disable_net, int, S_IRUGO | S_IWUSR);