1 /******************************************************************************
3 * Driver for Option High Speed Mobile Devices.
5 * Copyright (C) 2008 Option International
6 * Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com>
8 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
9 * <ajb@spheresystems.co.uk>
10 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
11 * Copyright (C) 2008 Novell, Inc.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 *****************************************************************************/
30 /******************************************************************************
32 * Description of the device:
34 * Interface 0: Contains the IP network interface on the bulk end points.
35 * The multiplexed serial ports are using the interrupt and
37 * Interrupt contains a bitmap telling which multiplexed
38 * serialport needs servicing.
40 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
41 * port is opened, as this have a huge impact on the network port
44 * Interface 2: Standard modem interface - circuit switched interface, this
45 * can be used to make a standard ppp connection however it
46 * should not be used in conjunction with the IP network interface
47 * enabled for USB performance reasons i.e. if using this set
48 * ideally disable_net=1.
50 *****************************************************************************/
52 #include <linux/sched.h>
53 #include <linux/slab.h>
54 #include <linux/init.h>
55 #include <linux/delay.h>
56 #include <linux/netdevice.h>
57 #include <linux/module.h>
58 #include <linux/ethtool.h>
59 #include <linux/usb.h>
60 #include <linux/timer.h>
61 #include <linux/tty.h>
62 #include <linux/tty_driver.h>
63 #include <linux/tty_flip.h>
64 #include <linux/kmod.h>
65 #include <linux/rfkill.h>
67 #include <linux/uaccess.h>
68 #include <linux/usb/cdc.h>
70 #include <asm/byteorder.h>
71 #include <linux/serial_core.h>
72 #include <linux/serial.h>
75 #define DRIVER_VERSION "1.2"
76 #define MOD_AUTHOR "Option Wireless"
77 #define MOD_DESCRIPTION "USB High Speed Option driver"
78 #define MOD_LICENSE "GPL"
80 #define HSO_MAX_NET_DEVICES 10
81 #define HSO__MAX_MTU 2048
82 #define DEFAULT_MTU 1500
83 #define DEFAULT_MRU 1500
85 #define CTRL_URB_RX_SIZE 1024
86 #define CTRL_URB_TX_SIZE 64
88 #define BULK_URB_RX_SIZE 4096
89 #define BULK_URB_TX_SIZE 8192
91 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
92 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
93 #define MUX_BULK_RX_BUF_COUNT 4
94 #define USB_TYPE_OPTION_VENDOR 0x20
96 /* These definitions are used with the struct hso_net flags element */
97 /* - use *_bit operations on it. (bit indices not values.) */
98 #define HSO_NET_RUNNING 0
100 #define HSO_NET_TX_TIMEOUT (HZ*10)
102 #define HSO_SERIAL_MAGIC 0x48534f31
104 /* Number of ttys to handle */
105 #define HSO_SERIAL_TTY_MINORS 256
107 #define MAX_RX_URBS 2
109 static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
112 return tty->driver_data;
116 /*****************************************************************************/
117 /* Debugging functions */
118 /*****************************************************************************/
119 #define D__(lvl_, fmt, arg...) \
121 printk(lvl_ "[%d:%s]: " fmt "\n", \
122 __LINE__, __func__, ## arg); \
125 #define D_(lvl, args...) \
128 D__(KERN_INFO, args); \
131 #define D1(args...) D_(0x01, ##args)
132 #define D2(args...) D_(0x02, ##args)
133 #define D3(args...) D_(0x04, ##args)
134 #define D4(args...) D_(0x08, ##args)
135 #define D5(args...) D_(0x10, ##args)
137 /*****************************************************************************/
139 /*****************************************************************************/
140 enum pkt_parse_state {
146 /*****************************************************************************/
148 /*****************************************************************************/
150 struct hso_shared_int {
151 struct usb_endpoint_descriptor *intr_endp;
152 void *shared_intr_buf;
153 struct urb *shared_intr_urb;
154 struct usb_device *usb;
157 struct mutex shared_int_lock;
161 struct hso_device *parent;
162 struct net_device *net;
163 struct rfkill *rfkill;
165 struct usb_endpoint_descriptor *in_endp;
166 struct usb_endpoint_descriptor *out_endp;
168 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
169 struct urb *mux_bulk_tx_urb;
170 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
171 void *mux_bulk_tx_buf;
173 struct sk_buff *skb_rx_buf;
174 struct sk_buff *skb_tx_buf;
176 enum pkt_parse_state rx_parse_state;
179 unsigned short rx_buf_size;
180 unsigned short rx_buf_missing;
181 struct iphdr rx_ip_hdr;
192 #define BM_REQUEST_TYPE (0xa1)
193 #define B_NOTIFICATION (0x20)
194 #define W_VALUE (0x0)
195 #define W_INDEX (0x2)
196 #define W_LENGTH (0x2)
198 #define B_OVERRUN (0x1<<6)
199 #define B_PARITY (0x1<<5)
200 #define B_FRAMING (0x1<<4)
201 #define B_RING_SIGNAL (0x1<<3)
202 #define B_BREAK (0x1<<2)
203 #define B_TX_CARRIER (0x1<<1)
204 #define B_RX_CARRIER (0x1<<0)
206 struct hso_serial_state_notification {
212 u16 UART_state_bitmap;
213 } __attribute__((packed));
215 struct hso_tiocmget {
217 wait_queue_head_t waitq;
219 struct usb_endpoint_descriptor *endp;
221 struct hso_serial_state_notification serial_state_notification;
222 u16 prev_UART_state_bitmap;
223 struct uart_icount icount;
228 struct hso_device *parent;
232 struct hso_shared_int *shared_int;
234 /* rx/tx urb could be either a bulk urb or a control urb depending
235 on which serial port it is used on. */
236 struct urb *rx_urb[MAX_RX_URBS];
238 u8 *rx_data[MAX_RX_URBS];
239 u16 rx_data_length; /* should contain allocated length */
244 u16 tx_data_length; /* should contain allocated length */
247 struct usb_ctrlrequest ctrl_req_tx;
248 struct usb_ctrlrequest ctrl_req_rx;
250 struct usb_endpoint_descriptor *in_endp;
251 struct usb_endpoint_descriptor *out_endp;
253 enum rx_ctrl_state rx_state;
256 unsigned tx_urb_used:1;
258 /* from usb_serial_port */
259 struct tty_struct *tty;
261 spinlock_t serial_lock;
263 int (*write_data) (struct hso_serial *serial);
264 struct hso_tiocmget *tiocmget;
265 /* Hacks required to get flow control
266 * working on the serial receive buffers
267 * so as not to drop characters on the floor.
270 u16 curr_rx_urb_offset;
271 u8 rx_urb_filled[MAX_RX_URBS];
272 struct tasklet_struct unthrottle_tasklet;
273 struct work_struct retry_unthrottle_workqueue;
278 struct hso_serial *dev_serial;
279 struct hso_net *dev_net;
286 struct work_struct async_get_intf;
287 struct work_struct async_put_intf;
289 struct usb_device *usb;
290 struct usb_interface *interface;
297 /* Type of interface */
298 #define HSO_INTF_MASK 0xFF00
299 #define HSO_INTF_MUX 0x0100
300 #define HSO_INTF_BULK 0x0200
303 #define HSO_PORT_MASK 0xFF
304 #define HSO_PORT_NO_PORT 0x0
305 #define HSO_PORT_CONTROL 0x1
306 #define HSO_PORT_APP 0x2
307 #define HSO_PORT_GPS 0x3
308 #define HSO_PORT_PCSC 0x4
309 #define HSO_PORT_APP2 0x5
310 #define HSO_PORT_GPS_CONTROL 0x6
311 #define HSO_PORT_MSD 0x7
312 #define HSO_PORT_VOICE 0x8
313 #define HSO_PORT_DIAG2 0x9
314 #define HSO_PORT_DIAG 0x10
315 #define HSO_PORT_MODEM 0x11
316 #define HSO_PORT_NETWORK 0x12
318 /* Additional device info */
319 #define HSO_INFO_MASK 0xFF000000
320 #define HSO_INFO_CRC_BUG 0x01000000
322 /*****************************************************************************/
324 /*****************************************************************************/
325 /* Serial driver functions */
326 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
327 unsigned int set, unsigned int clear);
328 static void ctrl_callback(struct urb *urb);
329 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
330 static void hso_kick_transmit(struct hso_serial *serial);
331 /* Helper functions */
332 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
333 struct usb_device *usb, gfp_t gfp);
334 static void log_usb_status(int status, const char *function);
335 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
337 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
338 static void hso_free_interface(struct usb_interface *intf);
339 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
340 static int hso_stop_serial_device(struct hso_device *hso_dev);
341 static int hso_start_net_device(struct hso_device *hso_dev);
342 static void hso_free_shared_int(struct hso_shared_int *shared_int);
343 static int hso_stop_net_device(struct hso_device *hso_dev);
344 static void hso_serial_ref_free(struct kref *ref);
345 static void hso_std_serial_read_bulk_callback(struct urb *urb);
346 static int hso_mux_serial_read(struct hso_serial *serial);
347 static void async_get_intf(struct work_struct *data);
348 static void async_put_intf(struct work_struct *data);
349 static int hso_put_activity(struct hso_device *hso_dev);
350 static int hso_get_activity(struct hso_device *hso_dev);
351 static void tiocmget_intr_callback(struct urb *urb);
352 /*****************************************************************************/
353 /* Helping functions */
354 /*****************************************************************************/
358 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
360 return hso_dev->port_data.dev_net;
363 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
365 return hso_dev->port_data.dev_serial;
368 /* Debugging functions */
370 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
373 static char name[255];
375 sprintf(name, "hso[%d:%s]", line_count, func_name);
376 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
379 #define DUMP(buf_, len_) \
380 dbg_dump(__LINE__, __func__, buf_, len_)
382 #define DUMP1(buf_, len_) \
388 #define DUMP(buf_, len_)
389 #define DUMP1(buf_, len_)
392 /* module parameters */
394 static int tty_major;
395 static int disable_net;
398 static const char driver_name[] = "hso";
399 static const char tty_filename[] = "ttyHS";
400 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
401 /* the usb driver itself (registered in hso_init) */
402 static struct usb_driver hso_driver;
403 /* serial structures */
404 static struct tty_driver *tty_drv;
405 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
406 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
407 static spinlock_t serial_table_lock;
409 static const s32 default_port_spec[] = {
410 HSO_INTF_MUX | HSO_PORT_NETWORK,
411 HSO_INTF_BULK | HSO_PORT_DIAG,
412 HSO_INTF_BULK | HSO_PORT_MODEM,
416 static const s32 icon321_port_spec[] = {
417 HSO_INTF_MUX | HSO_PORT_NETWORK,
418 HSO_INTF_BULK | HSO_PORT_DIAG2,
419 HSO_INTF_BULK | HSO_PORT_MODEM,
420 HSO_INTF_BULK | HSO_PORT_DIAG,
424 #define default_port_device(vendor, product) \
425 USB_DEVICE(vendor, product), \
426 .driver_info = (kernel_ulong_t)default_port_spec
428 #define icon321_port_device(vendor, product) \
429 USB_DEVICE(vendor, product), \
430 .driver_info = (kernel_ulong_t)icon321_port_spec
432 /* list of devices we support */
433 static const struct usb_device_id hso_ids[] = {
434 {default_port_device(0x0af0, 0x6711)},
435 {default_port_device(0x0af0, 0x6731)},
436 {default_port_device(0x0af0, 0x6751)},
437 {default_port_device(0x0af0, 0x6771)},
438 {default_port_device(0x0af0, 0x6791)},
439 {default_port_device(0x0af0, 0x6811)},
440 {default_port_device(0x0af0, 0x6911)},
441 {default_port_device(0x0af0, 0x6951)},
442 {default_port_device(0x0af0, 0x6971)},
443 {default_port_device(0x0af0, 0x7011)},
444 {default_port_device(0x0af0, 0x7031)},
445 {default_port_device(0x0af0, 0x7051)},
446 {default_port_device(0x0af0, 0x7071)},
447 {default_port_device(0x0af0, 0x7111)},
448 {default_port_device(0x0af0, 0x7211)},
449 {default_port_device(0x0af0, 0x7251)},
450 {default_port_device(0x0af0, 0x7271)},
451 {default_port_device(0x0af0, 0x7311)},
452 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
453 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
454 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
455 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
456 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
457 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
458 {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
459 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
460 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
461 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
462 {USB_DEVICE(0x0af0, 0x7701)},
463 {USB_DEVICE(0x0af0, 0x7801)},
464 {USB_DEVICE(0x0af0, 0x7901)},
465 {USB_DEVICE(0x0af0, 0x7361)},
466 {USB_DEVICE(0x0af0, 0xd057)},
467 {USB_DEVICE(0x0af0, 0xd055)},
470 MODULE_DEVICE_TABLE(usb, hso_ids);
472 /* Sysfs attribute */
473 static ssize_t hso_sysfs_show_porttype(struct device *dev,
474 struct device_attribute *attr,
477 struct hso_device *hso_dev = dev->driver_data;
483 switch (hso_dev->port_spec & HSO_PORT_MASK) {
484 case HSO_PORT_CONTROL:
485 port_name = "Control";
488 port_name = "Application";
491 port_name = "Application2";
496 case HSO_PORT_GPS_CONTROL:
497 port_name = "GPS Control";
503 port_name = "Diagnostic";
506 port_name = "Diagnostic2";
511 case HSO_PORT_NETWORK:
512 port_name = "Network";
515 port_name = "Unknown";
519 return sprintf(buf, "%s\n", port_name);
521 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
523 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
527 for (idx = 0; idx < serial->num_rx_urbs; idx++)
528 if (serial->rx_urb[idx] == urb)
530 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
534 /* converts mux value to a port spec value */
535 static u32 hso_mux_to_port(int mux)
541 result = HSO_PORT_CONTROL;
544 result = HSO_PORT_APP;
547 result = HSO_PORT_PCSC;
550 result = HSO_PORT_GPS;
553 result = HSO_PORT_APP2;
556 result = HSO_PORT_NO_PORT;
561 /* converts port spec value to a mux value */
562 static u32 hso_port_to_mux(int port)
566 switch (port & HSO_PORT_MASK) {
567 case HSO_PORT_CONTROL:
588 static struct hso_serial *get_serial_by_shared_int_and_type(
589 struct hso_shared_int *shared_int,
594 port = hso_mux_to_port(mux);
596 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
598 && (dev2ser(serial_table[i])->shared_int == shared_int)
599 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
600 return dev2ser(serial_table[i]);
607 static struct hso_serial *get_serial_by_index(unsigned index)
609 struct hso_serial *serial = NULL;
612 spin_lock_irqsave(&serial_table_lock, flags);
613 if (serial_table[index])
614 serial = dev2ser(serial_table[index]);
615 spin_unlock_irqrestore(&serial_table_lock, flags);
620 static int get_free_serial_index(void)
625 spin_lock_irqsave(&serial_table_lock, flags);
626 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
627 if (serial_table[index] == NULL) {
628 spin_unlock_irqrestore(&serial_table_lock, flags);
632 spin_unlock_irqrestore(&serial_table_lock, flags);
634 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
638 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
642 spin_lock_irqsave(&serial_table_lock, flags);
644 serial_table[index] = serial->parent;
646 serial_table[index] = NULL;
647 spin_unlock_irqrestore(&serial_table_lock, flags);
650 /* log a meaningful explanation of an USB status */
651 static void log_usb_status(int status, const char *function)
657 explanation = "no device";
660 explanation = "endpoint not enabled";
663 explanation = "endpoint stalled";
666 explanation = "not enough bandwidth";
669 explanation = "device disabled";
672 explanation = "device suspended";
678 explanation = "internal error";
681 explanation = "unknown status";
684 D1("%s: received USB status - %s (%d)", function, explanation, status);
687 /* Network interface functions */
689 /* called when net interface is brought up by ifconfig */
690 static int hso_net_open(struct net_device *net)
692 struct hso_net *odev = netdev_priv(net);
693 unsigned long flags = 0;
696 dev_err(&net->dev, "No net device !\n");
700 odev->skb_tx_buf = NULL;
702 /* setup environment */
703 spin_lock_irqsave(&odev->net_lock, flags);
704 odev->rx_parse_state = WAIT_IP;
705 odev->rx_buf_size = 0;
706 odev->rx_buf_missing = sizeof(struct iphdr);
707 spin_unlock_irqrestore(&odev->net_lock, flags);
709 /* We are up and running. */
710 set_bit(HSO_NET_RUNNING, &odev->flags);
711 hso_start_net_device(odev->parent);
713 /* Tell the kernel we are ready to start receiving from it */
714 netif_start_queue(net);
719 /* called when interface is brought down by ifconfig */
720 static int hso_net_close(struct net_device *net)
722 struct hso_net *odev = netdev_priv(net);
724 /* we don't need the queue anymore */
725 netif_stop_queue(net);
726 /* no longer running */
727 clear_bit(HSO_NET_RUNNING, &odev->flags);
729 hso_stop_net_device(odev->parent);
735 /* USB tells is xmit done, we should start the netqueue again */
736 static void write_bulk_callback(struct urb *urb)
738 struct hso_net *odev = urb->context;
739 int status = urb->status;
742 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
743 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
747 /* Do we still have a valid kernel network device? */
748 if (!netif_device_present(odev->net)) {
749 dev_err(&urb->dev->dev, "%s: net device not present\n",
754 /* log status, but don't act on it, we don't need to resubmit anything
757 log_usb_status(status, __func__);
759 hso_put_activity(odev->parent);
761 /* Tell the network interface we are ready for another frame */
762 netif_wake_queue(odev->net);
765 /* called by kernel when we need to transmit a packet */
766 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
768 struct hso_net *odev = netdev_priv(net);
771 /* Tell the kernel, "No more frames 'til we are done with this one." */
772 netif_stop_queue(net);
773 if (hso_get_activity(odev->parent) == -EAGAIN) {
774 odev->skb_tx_buf = skb;
779 DUMP1(skb->data, skb->len);
780 /* Copy it from kernel memory to OUR memory */
781 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
782 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
784 /* Fill in the URB for shipping it out. */
785 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
787 usb_sndbulkpipe(odev->parent->usb,
789 bEndpointAddress & 0x7F),
790 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
793 /* Deal with the Zero Length packet problem, I hope */
794 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
796 /* Send the URB on its merry way. */
797 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
799 dev_warn(&odev->parent->interface->dev,
800 "failed mux_bulk_tx_urb %d", result);
801 net->stats.tx_errors++;
802 netif_start_queue(net);
804 net->stats.tx_packets++;
805 net->stats.tx_bytes += skb->len;
806 /* And tell the kernel when the last transmit started. */
807 net->trans_start = jiffies;
814 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
816 struct hso_net *odev = netdev_priv(net);
818 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
819 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
820 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
823 static struct ethtool_ops ops = {
824 .get_drvinfo = hso_get_drvinfo,
825 .get_link = ethtool_op_get_link
828 /* called when a packet did not ack after watchdogtimeout */
829 static void hso_net_tx_timeout(struct net_device *net)
831 struct hso_net *odev = netdev_priv(net);
836 /* Tell syslog we are hosed. */
837 dev_warn(&net->dev, "Tx timed out.\n");
839 /* Tear the waiting frame off the list */
840 if (odev->mux_bulk_tx_urb
841 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
842 usb_unlink_urb(odev->mux_bulk_tx_urb);
844 /* Update statistics */
845 net->stats.tx_errors++;
848 /* make a real packet from the received USB buffer */
849 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
850 unsigned int count, unsigned char is_eop)
852 unsigned short temp_bytes;
853 unsigned short buffer_offset = 0;
854 unsigned short frame_len;
855 unsigned char *tmp_rx_buf;
858 D1("Rx %d bytes", count);
859 DUMP(ip_pkt, min(128, (int)count));
862 switch (odev->rx_parse_state) {
864 /* waiting for IP header. */
865 /* wanted bytes - size of ip header */
868 odev->rx_buf_missing) ? count : odev->
871 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
872 odev->rx_buf_size, ip_pkt + buffer_offset,
875 odev->rx_buf_size += temp_bytes;
876 buffer_offset += temp_bytes;
877 odev->rx_buf_missing -= temp_bytes;
880 if (!odev->rx_buf_missing) {
881 /* header is complete allocate an sk_buffer and
882 * continue to WAIT_DATA */
883 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
885 if ((frame_len > DEFAULT_MRU) ||
886 (frame_len < sizeof(struct iphdr))) {
887 dev_err(&odev->net->dev,
888 "Invalid frame (%d) length\n",
890 odev->rx_parse_state = WAIT_SYNC;
893 /* Allocate an sk_buff */
894 odev->skb_rx_buf = dev_alloc_skb(frame_len);
895 if (!odev->skb_rx_buf) {
896 /* We got no receive buffer. */
897 D1("could not allocate memory");
898 odev->rx_parse_state = WAIT_SYNC;
901 /* Here's where it came from */
902 odev->skb_rx_buf->dev = odev->net;
904 /* Copy what we got so far. make room for iphdr
907 skb_put(odev->skb_rx_buf,
908 sizeof(struct iphdr));
909 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
910 sizeof(struct iphdr));
913 odev->rx_buf_size = sizeof(struct iphdr);
915 /* Filip actually use .tot_len */
916 odev->rx_buf_missing =
917 frame_len - sizeof(struct iphdr);
918 odev->rx_parse_state = WAIT_DATA;
923 temp_bytes = (count < odev->rx_buf_missing)
924 ? count : odev->rx_buf_missing;
926 /* Copy the rest of the bytes that are left in the
927 * buffer into the waiting sk_buf. */
928 /* Make room for temp_bytes after tail. */
929 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
930 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
932 odev->rx_buf_missing -= temp_bytes;
934 buffer_offset += temp_bytes;
935 odev->rx_buf_size += temp_bytes;
936 if (!odev->rx_buf_missing) {
937 /* Packet is complete. Inject into stack. */
938 /* We have IP packet here */
939 odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
941 odev->skb_rx_buf->ip_summed =
942 CHECKSUM_UNNECESSARY;
944 skb_reset_mac_header(odev->skb_rx_buf);
946 /* Ship it off to the kernel */
947 netif_rx(odev->skb_rx_buf);
948 /* No longer our buffer. */
949 odev->skb_rx_buf = NULL;
951 /* update out statistics */
952 odev->net->stats.rx_packets++;
954 odev->net->stats.rx_bytes += odev->rx_buf_size;
956 odev->rx_buf_size = 0;
957 odev->rx_buf_missing = sizeof(struct iphdr);
958 odev->rx_parse_state = WAIT_IP;
973 /* Recovery mechanism for WAIT_SYNC state. */
975 if (odev->rx_parse_state == WAIT_SYNC) {
976 odev->rx_parse_state = WAIT_IP;
977 odev->rx_buf_size = 0;
978 odev->rx_buf_missing = sizeof(struct iphdr);
983 /* Moving data from usb to kernel (in interrupt state) */
984 static void read_bulk_callback(struct urb *urb)
986 struct hso_net *odev = urb->context;
987 struct net_device *net;
989 int status = urb->status;
991 /* is al ok? (Filip: Who's Al ?) */
993 log_usb_status(status, __func__);
998 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
999 D1("BULK IN callback but driver is not active!");
1002 usb_mark_last_busy(urb->dev);
1006 if (!netif_device_present(net)) {
1007 /* Somebody killed our network interface... */
1011 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
1013 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1014 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
1015 if (((rest == 5) || (rest == 6))
1016 && !memcmp(((u8 *) urb->transfer_buffer) +
1017 urb->actual_length - 4, crc_check, 4)) {
1018 urb->actual_length -= 4;
1022 /* do we even have a packet? */
1023 if (urb->actual_length) {
1024 /* Handle the IP stream, add header and push it onto network
1025 * stack if the packet is complete. */
1026 spin_lock(&odev->net_lock);
1027 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1028 (urb->transfer_buffer_length >
1029 urb->actual_length) ? 1 : 0);
1030 spin_unlock(&odev->net_lock);
1033 /* We are done with this URB, resubmit it. Prep the USB to wait for
1034 * another frame. Reuse same as received. */
1035 usb_fill_bulk_urb(urb,
1037 usb_rcvbulkpipe(odev->parent->usb,
1039 bEndpointAddress & 0x7F),
1040 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1041 read_bulk_callback, odev);
1043 /* Give this to the USB subsystem so it can tell us when more data
1045 result = usb_submit_urb(urb, GFP_ATOMIC);
1047 dev_warn(&odev->parent->interface->dev,
1048 "%s failed submit mux_bulk_rx_urb %d", __func__,
1052 /* Serial driver functions */
1054 static void hso_init_termios(struct ktermios *termios)
1057 * The default requirements for this device are:
1060 ~(IGNBRK /* disable ignore break */
1061 | BRKINT /* disable break causes interrupt */
1062 | PARMRK /* disable mark parity errors */
1063 | ISTRIP /* disable clear high bit of input characters */
1064 | INLCR /* disable translate NL to CR */
1065 | IGNCR /* disable ignore CR */
1066 | ICRNL /* disable translate CR to NL */
1067 | IXON); /* disable enable XON/XOFF flow control */
1069 /* disable postprocess output characters */
1070 termios->c_oflag &= ~OPOST;
1073 ~(ECHO /* disable echo input characters */
1074 | ECHONL /* disable echo new line */
1075 | ICANON /* disable erase, kill, werase, and rprnt
1076 special characters */
1077 | ISIG /* disable interrupt, quit, and suspend special
1079 | IEXTEN); /* disable non-POSIX special characters */
1082 ~(CSIZE /* no size */
1083 | PARENB /* disable parity bit */
1084 | CBAUD /* clear current baud rate */
1085 | CBAUDEX); /* clear current buad rate */
1087 termios->c_cflag |= CS8; /* character size 8 bits */
1089 /* baud rate 115200 */
1090 tty_termios_encode_baud_rate(termios, 115200, 115200);
1093 static void _hso_serial_set_termios(struct tty_struct *tty,
1094 struct ktermios *old)
1096 struct hso_serial *serial = get_serial_by_tty(tty);
1097 struct ktermios *termios;
1100 printk(KERN_ERR "%s: no tty structures", __func__);
1104 D4("port %d", serial->minor);
1107 * Fix up unsupported bits
1109 termios = tty->termios;
1110 termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1113 ~(CSIZE /* no size */
1114 | PARENB /* disable parity bit */
1115 | CBAUD /* clear current baud rate */
1116 | CBAUDEX); /* clear current buad rate */
1118 termios->c_cflag |= CS8; /* character size 8 bits */
1120 /* baud rate 115200 */
1121 tty_encode_baud_rate(tty, 115200, 115200);
1124 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1127 #ifdef CONFIG_HSO_AUTOPM
1128 usb_mark_last_busy(urb->dev);
1130 /* We are done with this URB, resubmit it. Prep the USB to wait for
1132 usb_fill_bulk_urb(urb, serial->parent->usb,
1133 usb_rcvbulkpipe(serial->parent->usb,
1135 bEndpointAddress & 0x7F),
1136 urb->transfer_buffer, serial->rx_data_length,
1137 hso_std_serial_read_bulk_callback, serial);
1138 /* Give this to the USB subsystem so it can tell us when more data
1140 result = usb_submit_urb(urb, GFP_ATOMIC);
1142 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1150 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1153 struct urb *curr_urb;
1155 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1156 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1157 count = put_rxbuf_data(curr_urb, serial);
1161 serial->curr_rx_urb_idx++;
1162 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1163 serial->curr_rx_urb_idx = 0;
1164 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1169 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1174 urb = serial->rx_urb[0];
1175 if (serial->open_count > 0) {
1176 count = put_rxbuf_data(urb, serial);
1180 /* Re issue a read as long as we receive data. */
1182 if (count == 0 && ((urb->actual_length != 0) ||
1183 (serial->rx_state == RX_PENDING))) {
1184 serial->rx_state = RX_SENT;
1185 hso_mux_serial_read(serial);
1187 serial->rx_state = RX_IDLE;
1191 /* read callback for Diag and CS port */
1192 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1194 struct hso_serial *serial = urb->context;
1195 int status = urb->status;
1199 D1("serial == NULL");
1201 } else if (status) {
1202 log_usb_status(status, __func__);
1206 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1207 D1("Actual length = %d\n", urb->actual_length);
1208 DUMP1(urb->transfer_buffer, urb->actual_length);
1210 /* Anyone listening? */
1211 if (serial->open_count == 0)
1215 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1217 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1219 urb->actual_length %
1220 serial->in_endp->wMaxPacketSize;
1221 if (((rest == 5) || (rest == 6))
1222 && !memcmp(((u8 *) urb->transfer_buffer) +
1223 urb->actual_length - 4, crc_check, 4)) {
1224 urb->actual_length -= 4;
1227 /* Valid data, handle RX data */
1228 spin_lock(&serial->serial_lock);
1229 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1230 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1231 spin_unlock(&serial->serial_lock);
1232 } else if (status == -ENOENT || status == -ECONNRESET) {
1233 /* Unlinked - check for throttled port. */
1234 D2("Port %d, successfully unlinked urb", serial->minor);
1235 spin_lock(&serial->serial_lock);
1236 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1237 hso_resubmit_rx_bulk_urb(serial, urb);
1238 spin_unlock(&serial->serial_lock);
1240 D2("Port %d, status = %d for read urb", serial->minor, status);
1246 * This needs to be a tasklet otherwise we will
1247 * end up recursively calling this function.
1249 static void hso_unthrottle_tasklet(struct hso_serial *serial)
1251 unsigned long flags;
1253 spin_lock_irqsave(&serial->serial_lock, flags);
1254 if ((serial->parent->port_spec & HSO_INTF_MUX))
1255 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1257 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1258 spin_unlock_irqrestore(&serial->serial_lock, flags);
1261 static void hso_unthrottle(struct tty_struct *tty)
1263 struct hso_serial *serial = get_serial_by_tty(tty);
1265 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1268 static void hso_unthrottle_workfunc(struct work_struct *work)
1270 struct hso_serial *serial =
1271 container_of(work, struct hso_serial,
1272 retry_unthrottle_workqueue);
1273 hso_unthrottle_tasklet(serial);
1276 /* open the requested serial port */
1277 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1279 struct hso_serial *serial = get_serial_by_index(tty->index);
1283 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1285 tty->driver_data = NULL;
1286 D1("Failed to open port");
1290 mutex_lock(&serial->parent->mutex);
1291 result = usb_autopm_get_interface(serial->parent->interface);
1295 D1("Opening %d", serial->minor);
1296 kref_get(&serial->parent->ref);
1299 spin_lock_irq(&serial->serial_lock);
1300 tty->driver_data = serial;
1301 tty_kref_put(serial->tty);
1302 serial->tty = tty_kref_get(tty);
1303 spin_unlock_irq(&serial->serial_lock);
1305 /* check for port already opened, if not set the termios */
1306 serial->open_count++;
1307 if (serial->open_count == 1) {
1308 tty->low_latency = 1;
1309 serial->rx_state = RX_IDLE;
1310 /* Force default termio settings */
1311 _hso_serial_set_termios(tty, NULL);
1312 tasklet_init(&serial->unthrottle_tasklet,
1313 (void (*)(unsigned long))hso_unthrottle_tasklet,
1314 (unsigned long)serial);
1315 INIT_WORK(&serial->retry_unthrottle_workqueue,
1316 hso_unthrottle_workfunc);
1317 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1319 hso_stop_serial_device(serial->parent);
1320 serial->open_count--;
1321 kref_put(&serial->parent->ref, hso_serial_ref_free);
1324 D1("Port was already open");
1327 usb_autopm_put_interface(serial->parent->interface);
1331 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1333 mutex_unlock(&serial->parent->mutex);
1337 /* close the requested serial port */
1338 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1340 struct hso_serial *serial = tty->driver_data;
1343 D1("Closing serial port");
1345 /* Open failed, no close cleanup required */
1349 mutex_lock(&serial->parent->mutex);
1350 usb_gone = serial->parent->usb_gone;
1353 usb_autopm_get_interface(serial->parent->interface);
1355 /* reset the rts and dtr */
1356 /* do the actual close */
1357 serial->open_count--;
1358 kref_put(&serial->parent->ref, hso_serial_ref_free);
1359 if (serial->open_count <= 0) {
1360 serial->open_count = 0;
1361 spin_lock_irq(&serial->serial_lock);
1362 if (serial->tty == tty) {
1363 serial->tty->driver_data = NULL;
1367 spin_unlock_irq(&serial->serial_lock);
1369 hso_stop_serial_device(serial->parent);
1370 tasklet_kill(&serial->unthrottle_tasklet);
1371 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1375 usb_autopm_put_interface(serial->parent->interface);
1377 mutex_unlock(&serial->parent->mutex);
1380 /* close the requested serial port */
1381 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1384 struct hso_serial *serial = get_serial_by_tty(tty);
1385 int space, tx_bytes;
1386 unsigned long flags;
1389 if (serial == NULL) {
1390 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1394 spin_lock_irqsave(&serial->serial_lock, flags);
1396 space = serial->tx_data_length - serial->tx_buffer_count;
1397 tx_bytes = (count < space) ? count : space;
1402 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1403 serial->tx_buffer_count += tx_bytes;
1406 spin_unlock_irqrestore(&serial->serial_lock, flags);
1408 hso_kick_transmit(serial);
1413 /* how much room is there for writing */
1414 static int hso_serial_write_room(struct tty_struct *tty)
1416 struct hso_serial *serial = get_serial_by_tty(tty);
1418 unsigned long flags;
1420 spin_lock_irqsave(&serial->serial_lock, flags);
1421 room = serial->tx_data_length - serial->tx_buffer_count;
1422 spin_unlock_irqrestore(&serial->serial_lock, flags);
1424 /* return free room */
1428 /* setup the term */
1429 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1431 struct hso_serial *serial = get_serial_by_tty(tty);
1432 unsigned long flags;
1435 D5("Termios called with: cflags new[%d] - old[%d]",
1436 tty->termios->c_cflag, old->c_cflag);
1438 /* the actual setup */
1439 spin_lock_irqsave(&serial->serial_lock, flags);
1440 if (serial->open_count)
1441 _hso_serial_set_termios(tty, old);
1444 spin_unlock_irqrestore(&serial->serial_lock, flags);
1450 /* how many characters in the buffer */
1451 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1453 struct hso_serial *serial = get_serial_by_tty(tty);
1455 unsigned long flags;
1461 spin_lock_irqsave(&serial->serial_lock, flags);
1462 chars = serial->tx_buffer_count;
1463 spin_unlock_irqrestore(&serial->serial_lock, flags);
1467 static int tiocmget_submit_urb(struct hso_serial *serial,
1468 struct hso_tiocmget *tiocmget,
1469 struct usb_device *usb)
1473 if (serial->parent->usb_gone)
1475 usb_fill_int_urb(tiocmget->urb, usb,
1478 bEndpointAddress & 0x7F),
1479 &tiocmget->serial_state_notification,
1480 sizeof(struct hso_serial_state_notification),
1481 tiocmget_intr_callback, serial,
1482 tiocmget->endp->bInterval);
1483 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1485 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1492 static void tiocmget_intr_callback(struct urb *urb)
1494 struct hso_serial *serial = urb->context;
1495 struct hso_tiocmget *tiocmget;
1496 int status = urb->status;
1497 u16 UART_state_bitmap, prev_UART_state_bitmap;
1498 struct uart_icount *icount;
1499 struct hso_serial_state_notification *serial_state_notification;
1500 struct usb_device *usb;
1506 log_usb_status(status, __func__);
1509 tiocmget = serial->tiocmget;
1512 usb = serial->parent->usb;
1513 serial_state_notification = &tiocmget->serial_state_notification;
1514 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1515 serial_state_notification->bNotification != B_NOTIFICATION ||
1516 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1517 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
1518 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1520 "hso received invalid serial state notification\n");
1521 DUMP(serial_state_notification,
1522 sizeof(hso_serial_state_notifation))
1525 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1527 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1528 icount = &tiocmget->icount;
1529 spin_lock(&serial->serial_lock);
1530 if ((UART_state_bitmap & B_OVERRUN) !=
1531 (prev_UART_state_bitmap & B_OVERRUN))
1533 if ((UART_state_bitmap & B_PARITY) !=
1534 (prev_UART_state_bitmap & B_PARITY))
1536 if ((UART_state_bitmap & B_FRAMING) !=
1537 (prev_UART_state_bitmap & B_FRAMING))
1539 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1540 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1542 if ((UART_state_bitmap & B_BREAK) !=
1543 (prev_UART_state_bitmap & B_BREAK))
1545 if ((UART_state_bitmap & B_TX_CARRIER) !=
1546 (prev_UART_state_bitmap & B_TX_CARRIER))
1548 if ((UART_state_bitmap & B_RX_CARRIER) !=
1549 (prev_UART_state_bitmap & B_RX_CARRIER))
1551 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1552 spin_unlock(&serial->serial_lock);
1553 tiocmget->intr_completed = 1;
1554 wake_up_interruptible(&tiocmget->waitq);
1556 memset(serial_state_notification, 0,
1557 sizeof(struct hso_serial_state_notification));
1558 tiocmget_submit_urb(serial,
1560 serial->parent->usb);
1564 * next few functions largely stolen from drivers/serial/serial_core.c
1566 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1567 * - mask passed in arg for lines of interest
1568 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1569 * Caller should use TIOCGICOUNT to see which one it was
1572 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1574 DECLARE_WAITQUEUE(wait, current);
1575 struct uart_icount cprev, cnow;
1576 struct hso_tiocmget *tiocmget;
1579 tiocmget = serial->tiocmget;
1583 * note the counters on entry
1585 spin_lock_irq(&serial->serial_lock);
1586 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1587 spin_unlock_irq(&serial->serial_lock);
1588 add_wait_queue(&tiocmget->waitq, &wait);
1590 spin_lock_irq(&serial->serial_lock);
1591 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1592 spin_unlock_irq(&serial->serial_lock);
1593 set_current_state(TASK_INTERRUPTIBLE);
1594 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1595 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1596 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1601 /* see if a signal did it */
1602 if (signal_pending(current)) {
1608 current->state = TASK_RUNNING;
1609 remove_wait_queue(&tiocmget->waitq, &wait);
1615 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1616 * Return: write counters to the user passed counter struct
1617 * NB: both 1->0 and 0->1 transitions are counted except for
1618 * RI where only 0->1 is counted.
1620 static int hso_get_count(struct hso_serial *serial,
1621 struct serial_icounter_struct __user *icnt)
1623 struct serial_icounter_struct icount;
1624 struct uart_icount cnow;
1625 struct hso_tiocmget *tiocmget = serial->tiocmget;
1629 spin_lock_irq(&serial->serial_lock);
1630 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1631 spin_unlock_irq(&serial->serial_lock);
1633 icount.cts = cnow.cts;
1634 icount.dsr = cnow.dsr;
1635 icount.rng = cnow.rng;
1636 icount.dcd = cnow.dcd;
1637 icount.rx = cnow.rx;
1638 icount.tx = cnow.tx;
1639 icount.frame = cnow.frame;
1640 icount.overrun = cnow.overrun;
1641 icount.parity = cnow.parity;
1642 icount.brk = cnow.brk;
1643 icount.buf_overrun = cnow.buf_overrun;
1645 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1649 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1652 struct hso_serial *serial = get_serial_by_tty(tty);
1653 struct hso_tiocmget *tiocmget;
1654 u16 UART_state_bitmap;
1658 D1("no tty structures");
1661 spin_lock_irq(&serial->serial_lock);
1662 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
1663 ((serial->dtr_state) ? TIOCM_DTR : 0);
1664 tiocmget = serial->tiocmget;
1667 UART_state_bitmap = le16_to_cpu(
1668 tiocmget->prev_UART_state_bitmap);
1669 if (UART_state_bitmap & B_RING_SIGNAL)
1670 retval |= TIOCM_RNG;
1671 if (UART_state_bitmap & B_RX_CARRIER)
1673 if (UART_state_bitmap & B_TX_CARRIER)
1674 retval |= TIOCM_DSR;
1676 spin_unlock_irq(&serial->serial_lock);
1680 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1681 unsigned int set, unsigned int clear)
1684 unsigned long flags;
1686 struct hso_serial *serial = get_serial_by_tty(tty);
1690 D1("no tty structures");
1693 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1695 spin_lock_irqsave(&serial->serial_lock, flags);
1696 if (set & TIOCM_RTS)
1697 serial->rts_state = 1;
1698 if (set & TIOCM_DTR)
1699 serial->dtr_state = 1;
1701 if (clear & TIOCM_RTS)
1702 serial->rts_state = 0;
1703 if (clear & TIOCM_DTR)
1704 serial->dtr_state = 0;
1706 if (serial->dtr_state)
1708 if (serial->rts_state)
1711 spin_unlock_irqrestore(&serial->serial_lock, flags);
1713 return usb_control_msg(serial->parent->usb,
1714 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1715 0x21, val, if_num, NULL, 0,
1716 USB_CTRL_SET_TIMEOUT);
1719 static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
1720 unsigned int cmd, unsigned long arg)
1722 struct hso_serial *serial = get_serial_by_tty(tty);
1723 void __user *uarg = (void __user *)arg;
1725 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1731 ret = hso_wait_modem_status(serial, arg);
1735 ret = hso_get_count(serial, uarg);
1745 /* starts a transmit */
1746 static void hso_kick_transmit(struct hso_serial *serial)
1749 unsigned long flags;
1752 spin_lock_irqsave(&serial->serial_lock, flags);
1753 if (!serial->tx_buffer_count)
1756 if (serial->tx_urb_used)
1759 /* Wakeup USB interface if necessary */
1760 if (hso_get_activity(serial->parent) == -EAGAIN)
1763 /* Switch pointers around to avoid memcpy */
1764 temp = serial->tx_buffer;
1765 serial->tx_buffer = serial->tx_data;
1766 serial->tx_data = temp;
1767 serial->tx_data_count = serial->tx_buffer_count;
1768 serial->tx_buffer_count = 0;
1770 /* If temp is set, it means we switched buffers */
1771 if (temp && serial->write_data) {
1772 res = serial->write_data(serial);
1774 serial->tx_urb_used = 1;
1777 spin_unlock_irqrestore(&serial->serial_lock, flags);
1780 /* make a request (for reading and writing data to muxed serial port) */
1781 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1782 struct urb *ctrl_urb,
1783 struct usb_ctrlrequest *ctrl_req,
1784 u8 *ctrl_urb_data, u32 size)
1790 if (!serial || !ctrl_urb || !ctrl_req) {
1791 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1796 ctrl_req->wValue = 0;
1797 ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
1798 ctrl_req->wLength = cpu_to_le16(size);
1800 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1801 /* Reading command */
1802 ctrl_req->bRequestType = USB_DIR_IN |
1803 USB_TYPE_OPTION_VENDOR |
1804 USB_RECIP_INTERFACE;
1805 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1806 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1808 /* Writing command */
1809 ctrl_req->bRequestType = USB_DIR_OUT |
1810 USB_TYPE_OPTION_VENDOR |
1811 USB_RECIP_INTERFACE;
1812 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1813 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1816 D2("%s command (%02x) len: %d, port: %d",
1817 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1818 ctrl_req->bRequestType, ctrl_req->wLength, port);
1821 ctrl_urb->transfer_flags = 0;
1822 usb_fill_control_urb(ctrl_urb,
1823 serial->parent->usb,
1826 ctrl_urb_data, size, ctrl_callback, serial);
1827 /* Send it on merry way */
1828 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1830 dev_err(&ctrl_urb->dev->dev,
1831 "%s failed submit ctrl_urb %d type %d", __func__,
1840 /* called by intr_callback when read occurs */
1841 static int hso_mux_serial_read(struct hso_serial *serial)
1847 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1848 /* make the request */
1850 if (serial->num_rx_urbs != 1) {
1851 dev_err(&serial->parent->interface->dev,
1852 "ERROR: mux'd reads with multiple buffers "
1856 return mux_device_request(serial,
1857 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1858 serial->parent->port_spec & HSO_PORT_MASK,
1860 &serial->ctrl_req_rx,
1861 serial->rx_data[0], serial->rx_data_length);
1864 /* used for muxed serial port callback (muxed serial read) */
1865 static void intr_callback(struct urb *urb)
1867 struct hso_shared_int *shared_int = urb->context;
1868 struct hso_serial *serial;
1869 unsigned char *port_req;
1870 int status = urb->status;
1873 usb_mark_last_busy(urb->dev);
1881 log_usb_status(status, __func__);
1884 D4("\n--- Got intr callback 0x%02X ---", status);
1887 port_req = urb->transfer_buffer;
1888 D4(" port_req = 0x%.2X\n", *port_req);
1889 /* loop over all muxed ports to find the one sending this */
1890 for (i = 0; i < 8; i++) {
1891 /* max 8 channels on MUX */
1892 if (*port_req & (1 << i)) {
1893 serial = get_serial_by_shared_int_and_type(shared_int,
1895 if (serial != NULL) {
1896 D1("Pending read interrupt on port %d\n", i);
1897 spin_lock(&serial->serial_lock);
1898 if (serial->rx_state == RX_IDLE) {
1899 /* Setup and send a ctrl req read on
1901 if (!serial->rx_urb_filled[0]) {
1902 serial->rx_state = RX_SENT;
1903 hso_mux_serial_read(serial);
1905 serial->rx_state = RX_PENDING;
1908 D1("Already pending a read on "
1911 spin_unlock(&serial->serial_lock);
1915 /* Resubmit interrupt urb */
1916 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1919 /* called for writing to muxed serial port */
1920 static int hso_mux_serial_write_data(struct hso_serial *serial)
1925 return mux_device_request(serial,
1926 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1927 serial->parent->port_spec & HSO_PORT_MASK,
1929 &serial->ctrl_req_tx,
1930 serial->tx_data, serial->tx_data_count);
1933 /* write callback for Diag and CS port */
1934 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1936 struct hso_serial *serial = urb->context;
1937 int status = urb->status;
1938 struct tty_struct *tty;
1942 D1("serial == NULL");
1946 spin_lock(&serial->serial_lock);
1947 serial->tx_urb_used = 0;
1948 tty = tty_kref_get(serial->tty);
1949 spin_unlock(&serial->serial_lock);
1951 log_usb_status(status, __func__);
1955 hso_put_activity(serial->parent);
1960 hso_kick_transmit(serial);
1966 /* called for writing diag or CS serial port */
1967 static int hso_std_serial_write_data(struct hso_serial *serial)
1969 int count = serial->tx_data_count;
1972 usb_fill_bulk_urb(serial->tx_urb,
1973 serial->parent->usb,
1974 usb_sndbulkpipe(serial->parent->usb,
1976 bEndpointAddress & 0x7F),
1977 serial->tx_data, serial->tx_data_count,
1978 hso_std_serial_write_bulk_callback, serial);
1980 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1982 dev_warn(&serial->parent->usb->dev,
1983 "Failed to submit urb - res %d\n", result);
1990 /* callback after read or write on muxed serial port */
1991 static void ctrl_callback(struct urb *urb)
1993 struct hso_serial *serial = urb->context;
1994 struct usb_ctrlrequest *req;
1995 int status = urb->status;
1996 struct tty_struct *tty;
2002 spin_lock(&serial->serial_lock);
2003 serial->tx_urb_used = 0;
2004 tty = tty_kref_get(serial->tty);
2005 spin_unlock(&serial->serial_lock);
2007 log_usb_status(status, __func__);
2013 req = (struct usb_ctrlrequest *)(urb->setup_packet);
2014 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2015 D4("Actual length of urb = %d\n", urb->actual_length);
2016 DUMP1(urb->transfer_buffer, urb->actual_length);
2018 if (req->bRequestType ==
2019 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2020 /* response to a read command */
2021 serial->rx_urb_filled[0] = 1;
2022 spin_lock(&serial->serial_lock);
2023 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2024 spin_unlock(&serial->serial_lock);
2026 hso_put_activity(serial->parent);
2029 /* response to a write command */
2030 hso_kick_transmit(serial);
2035 /* handle RX data for serial port */
2036 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
2038 struct tty_struct *tty;
2039 int write_length_remaining = 0;
2043 if (urb == NULL || serial == NULL) {
2044 D1("serial = NULL");
2048 /* All callers to put_rxbuf_data hold serial_lock */
2049 tty = tty_kref_get(serial->tty);
2051 /* Push data to tty */
2053 write_length_remaining = urb->actual_length -
2054 serial->curr_rx_urb_offset;
2055 D1("data to push to tty");
2056 while (write_length_remaining) {
2057 if (test_bit(TTY_THROTTLED, &tty->flags)) {
2061 curr_write_len = tty_insert_flip_string
2062 (tty, urb->transfer_buffer +
2063 serial->curr_rx_urb_offset,
2064 write_length_remaining);
2065 serial->curr_rx_urb_offset += curr_write_len;
2066 write_length_remaining -= curr_write_len;
2067 tty_flip_buffer_push(tty);
2070 if (write_length_remaining == 0) {
2071 serial->curr_rx_urb_offset = 0;
2072 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2075 return write_length_remaining;
2079 /* Base driver functions */
2081 static void hso_log_port(struct hso_device *hso_dev)
2086 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2087 case HSO_PORT_CONTROL:
2088 port_type = "Control";
2091 port_type = "Application";
2096 case HSO_PORT_GPS_CONTROL:
2097 port_type = "GPS control";
2100 port_type = "Application2";
2106 port_type = "Diagnostic";
2108 case HSO_PORT_DIAG2:
2109 port_type = "Diagnostic2";
2111 case HSO_PORT_MODEM:
2112 port_type = "Modem";
2114 case HSO_PORT_NETWORK:
2115 port_type = "Network";
2118 port_type = "Unknown";
2121 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2122 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2124 sprintf(port_dev, "/dev/%s%d", tty_filename,
2125 dev2ser(hso_dev)->minor);
2127 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2128 port_type, port_dev);
2131 static int hso_start_net_device(struct hso_device *hso_dev)
2134 struct hso_net *hso_net = dev2net(hso_dev);
2139 /* send URBs for all read buffers */
2140 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2142 /* Prep a receive URB */
2143 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2145 usb_rcvbulkpipe(hso_dev->usb,
2147 bEndpointAddress & 0x7F),
2148 hso_net->mux_bulk_rx_buf_pool[i],
2149 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2152 /* Put it out there so the device can send us stuff */
2153 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2156 dev_warn(&hso_dev->usb->dev,
2157 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2164 static int hso_stop_net_device(struct hso_device *hso_dev)
2167 struct hso_net *hso_net = dev2net(hso_dev);
2172 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2173 if (hso_net->mux_bulk_rx_urb_pool[i])
2174 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2177 if (hso_net->mux_bulk_tx_urb)
2178 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2183 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2186 struct hso_serial *serial = dev2ser(hso_dev);
2191 /* If it is not the MUX port fill in and submit a bulk urb (already
2192 * allocated in hso_serial_start) */
2193 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2194 for (i = 0; i < serial->num_rx_urbs; i++) {
2195 usb_fill_bulk_urb(serial->rx_urb[i],
2196 serial->parent->usb,
2197 usb_rcvbulkpipe(serial->parent->usb,
2202 serial->rx_data_length,
2203 hso_std_serial_read_bulk_callback,
2205 result = usb_submit_urb(serial->rx_urb[i], flags);
2207 dev_warn(&serial->parent->usb->dev,
2208 "Failed to submit urb - res %d\n",
2214 mutex_lock(&serial->shared_int->shared_int_lock);
2215 if (!serial->shared_int->use_count) {
2217 hso_mux_submit_intr_urb(serial->shared_int,
2218 hso_dev->usb, flags);
2220 serial->shared_int->use_count++;
2221 mutex_unlock(&serial->shared_int->shared_int_lock);
2223 if (serial->tiocmget)
2224 tiocmget_submit_urb(serial,
2226 serial->parent->usb);
2230 static int hso_stop_serial_device(struct hso_device *hso_dev)
2233 struct hso_serial *serial = dev2ser(hso_dev);
2234 struct hso_tiocmget *tiocmget;
2239 for (i = 0; i < serial->num_rx_urbs; i++) {
2240 if (serial->rx_urb[i]) {
2241 usb_kill_urb(serial->rx_urb[i]);
2242 serial->rx_urb_filled[i] = 0;
2245 serial->curr_rx_urb_idx = 0;
2246 serial->curr_rx_urb_offset = 0;
2249 usb_kill_urb(serial->tx_urb);
2251 if (serial->shared_int) {
2252 mutex_lock(&serial->shared_int->shared_int_lock);
2253 if (serial->shared_int->use_count &&
2254 (--serial->shared_int->use_count == 0)) {
2257 urb = serial->shared_int->shared_intr_urb;
2261 mutex_unlock(&serial->shared_int->shared_int_lock);
2263 tiocmget = serial->tiocmget;
2265 wake_up_interruptible(&tiocmget->waitq);
2266 usb_kill_urb(tiocmget->urb);
2272 static void hso_serial_common_free(struct hso_serial *serial)
2276 if (serial->parent->dev)
2277 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2279 tty_unregister_device(tty_drv, serial->minor);
2281 for (i = 0; i < serial->num_rx_urbs; i++) {
2282 /* unlink and free RX URB */
2283 usb_free_urb(serial->rx_urb[i]);
2284 /* free the RX buffer */
2285 kfree(serial->rx_data[i]);
2288 /* unlink and free TX URB */
2289 usb_free_urb(serial->tx_urb);
2290 kfree(serial->tx_data);
2293 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2294 int rx_size, int tx_size)
2300 minor = get_free_serial_index();
2304 /* register our minor number */
2305 serial->parent->dev = tty_register_device(tty_drv, minor,
2306 &serial->parent->interface->dev);
2307 dev = serial->parent->dev;
2308 dev->driver_data = serial->parent;
2309 i = device_create_file(dev, &dev_attr_hsotype);
2311 /* fill in specific data for later use */
2312 serial->minor = minor;
2313 serial->magic = HSO_SERIAL_MAGIC;
2314 spin_lock_init(&serial->serial_lock);
2315 serial->num_rx_urbs = num_urbs;
2317 /* RX, allocate urb and initialize */
2319 /* prepare our RX buffer */
2320 serial->rx_data_length = rx_size;
2321 for (i = 0; i < serial->num_rx_urbs; i++) {
2322 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2323 if (!serial->rx_urb[i]) {
2324 dev_err(dev, "Could not allocate urb?\n");
2327 serial->rx_urb[i]->transfer_buffer = NULL;
2328 serial->rx_urb[i]->transfer_buffer_length = 0;
2329 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2331 if (!serial->rx_data[i]) {
2332 dev_err(dev, "%s - Out of memory\n", __func__);
2337 /* TX, allocate urb and initialize */
2338 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2339 if (!serial->tx_urb) {
2340 dev_err(dev, "Could not allocate urb?\n");
2343 serial->tx_urb->transfer_buffer = NULL;
2344 serial->tx_urb->transfer_buffer_length = 0;
2345 /* prepare our TX buffer */
2346 serial->tx_data_count = 0;
2347 serial->tx_buffer_count = 0;
2348 serial->tx_data_length = tx_size;
2349 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2350 if (!serial->tx_data) {
2351 dev_err(dev, "%s - Out of memory", __func__);
2354 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2355 if (!serial->tx_buffer) {
2356 dev_err(dev, "%s - Out of memory", __func__);
2362 hso_serial_common_free(serial);
2366 /* Creates a general hso device */
2367 static struct hso_device *hso_create_device(struct usb_interface *intf,
2370 struct hso_device *hso_dev;
2372 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2376 hso_dev->port_spec = port_spec;
2377 hso_dev->usb = interface_to_usbdev(intf);
2378 hso_dev->interface = intf;
2379 kref_init(&hso_dev->ref);
2380 mutex_init(&hso_dev->mutex);
2382 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2383 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2388 /* Removes a network device in the network device table */
2389 static int remove_net_device(struct hso_device *hso_dev)
2393 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2394 if (network_table[i] == hso_dev) {
2395 network_table[i] = NULL;
2399 if (i == HSO_MAX_NET_DEVICES)
2404 /* Frees our network device */
2405 static void hso_free_net_device(struct hso_device *hso_dev)
2408 struct hso_net *hso_net = dev2net(hso_dev);
2414 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2415 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2416 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2418 usb_free_urb(hso_net->mux_bulk_tx_urb);
2419 kfree(hso_net->mux_bulk_tx_buf);
2421 remove_net_device(hso_net->parent);
2424 unregister_netdev(hso_net->net);
2425 free_netdev(hso_net->net);
2431 static const struct net_device_ops hso_netdev_ops = {
2432 .ndo_open = hso_net_open,
2433 .ndo_stop = hso_net_close,
2434 .ndo_start_xmit = hso_net_start_xmit,
2435 .ndo_tx_timeout = hso_net_tx_timeout,
2438 /* initialize the network interface */
2439 static void hso_net_init(struct net_device *net)
2441 struct hso_net *hso_net = netdev_priv(net);
2443 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2445 /* fill in the other fields */
2446 net->netdev_ops = &hso_netdev_ops;
2447 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2448 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2449 net->type = ARPHRD_NONE;
2450 net->mtu = DEFAULT_MTU - 14;
2451 net->tx_queue_len = 10;
2452 SET_ETHTOOL_OPS(net, &ops);
2454 /* and initialize the semaphore */
2455 spin_lock_init(&hso_net->net_lock);
2458 /* Adds a network device in the network device table */
2459 static int add_net_device(struct hso_device *hso_dev)
2463 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2464 if (network_table[i] == NULL) {
2465 network_table[i] = hso_dev;
2469 if (i == HSO_MAX_NET_DEVICES)
2474 static int hso_radio_toggle(void *data, enum rfkill_state state)
2476 struct hso_device *hso_dev = data;
2477 int enabled = (state == RFKILL_STATE_ON);
2480 mutex_lock(&hso_dev->mutex);
2481 if (hso_dev->usb_gone)
2484 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2485 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2486 USB_CTRL_SET_TIMEOUT);
2487 mutex_unlock(&hso_dev->mutex);
2491 /* Creates and sets up everything for rfkill */
2492 static void hso_create_rfkill(struct hso_device *hso_dev,
2493 struct usb_interface *interface)
2495 struct hso_net *hso_net = dev2net(hso_dev);
2496 struct device *dev = &hso_net->net->dev;
2499 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2501 if (!hso_net->rfkill) {
2502 dev_err(dev, "%s - Out of memory\n", __func__);
2505 rfkn = kzalloc(20, GFP_KERNEL);
2507 rfkill_free(hso_net->rfkill);
2508 hso_net->rfkill = NULL;
2509 dev_err(dev, "%s - Out of memory\n", __func__);
2512 snprintf(rfkn, 20, "hso-%d",
2513 interface->altsetting->desc.bInterfaceNumber);
2514 hso_net->rfkill->name = rfkn;
2515 hso_net->rfkill->state = RFKILL_STATE_ON;
2516 hso_net->rfkill->data = hso_dev;
2517 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2518 if (rfkill_register(hso_net->rfkill) < 0) {
2520 hso_net->rfkill->name = NULL;
2521 rfkill_free(hso_net->rfkill);
2522 hso_net->rfkill = NULL;
2523 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2528 /* Creates our network device */
2529 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2532 struct net_device *net;
2533 struct hso_net *hso_net;
2534 struct hso_device *hso_dev;
2536 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2540 /* allocate our network device, then we can put in our private data */
2541 /* call hso_net_init to do the basic initialization */
2542 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2544 dev_err(&interface->dev, "Unable to create ethernet device\n");
2548 hso_net = netdev_priv(net);
2550 hso_dev->port_data.dev_net = hso_net;
2552 hso_net->parent = hso_dev;
2554 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2556 if (!hso_net->in_endp) {
2557 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2560 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2562 if (!hso_net->out_endp) {
2563 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2566 SET_NETDEV_DEV(net, &interface->dev);
2568 /* registering our net device */
2569 result = register_netdev(net);
2571 dev_err(&interface->dev, "Failed to register device\n");
2575 /* start allocating */
2576 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2577 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2578 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2579 dev_err(&interface->dev, "Could not allocate rx urb\n");
2582 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2584 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2585 dev_err(&interface->dev, "Could not allocate rx buf\n");
2589 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2590 if (!hso_net->mux_bulk_tx_urb) {
2591 dev_err(&interface->dev, "Could not allocate tx urb\n");
2594 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2595 if (!hso_net->mux_bulk_tx_buf) {
2596 dev_err(&interface->dev, "Could not allocate tx buf\n");
2600 add_net_device(hso_dev);
2602 hso_log_port(hso_dev);
2604 hso_create_rfkill(hso_dev, interface);
2608 hso_free_net_device(hso_dev);
2612 static void hso_free_tiomget(struct hso_serial *serial)
2614 struct hso_tiocmget *tiocmget = serial->tiocmget;
2617 if (tiocmget->urb) {
2618 usb_free_urb(tiocmget->urb);
2619 tiocmget->urb = NULL;
2621 serial->tiocmget = NULL;
2626 /* Frees an AT channel ( goes for both mux and non-mux ) */
2627 static void hso_free_serial_device(struct hso_device *hso_dev)
2629 struct hso_serial *serial = dev2ser(hso_dev);
2633 set_serial_by_index(serial->minor, NULL);
2635 hso_serial_common_free(serial);
2637 if (serial->shared_int) {
2638 mutex_lock(&serial->shared_int->shared_int_lock);
2639 if (--serial->shared_int->ref_count == 0)
2640 hso_free_shared_int(serial->shared_int);
2642 mutex_unlock(&serial->shared_int->shared_int_lock);
2644 hso_free_tiomget(serial);
2649 /* Creates a bulk AT channel */
2650 static struct hso_device *hso_create_bulk_serial_device(
2651 struct usb_interface *interface, int port)
2653 struct hso_device *hso_dev;
2654 struct hso_serial *serial;
2656 struct hso_tiocmget *tiocmget;
2658 hso_dev = hso_create_device(interface, port);
2662 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2666 serial->parent = hso_dev;
2667 hso_dev->port_data.dev_serial = serial;
2669 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
2671 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2673 /* it isn't going to break our heart if serial->tiocmget
2674 * allocation fails don't bother checking this.
2676 if (serial->tiocmget) {
2677 tiocmget = serial->tiocmget;
2678 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2679 if (tiocmget->urb) {
2680 mutex_init(&tiocmget->mutex);
2681 init_waitqueue_head(&tiocmget->waitq);
2682 tiocmget->endp = hso_get_ep(
2684 USB_ENDPOINT_XFER_INT,
2687 hso_free_tiomget(serial);
2693 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2697 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2699 if (!serial->in_endp) {
2700 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2706 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2707 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2711 serial->write_data = hso_std_serial_write_data;
2713 /* and record this serial */
2714 set_serial_by_index(serial->minor, serial);
2716 /* setup the proc dirs and files if needed */
2717 hso_log_port(hso_dev);
2719 /* done, return it */
2723 hso_serial_common_free(serial);
2725 hso_free_tiomget(serial);
2731 /* Creates a multiplexed AT channel */
2733 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2735 struct hso_shared_int *mux)
2737 struct hso_device *hso_dev;
2738 struct hso_serial *serial;
2741 port_spec = HSO_INTF_MUX;
2742 port_spec &= ~HSO_PORT_MASK;
2744 port_spec |= hso_mux_to_port(port);
2745 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2748 hso_dev = hso_create_device(interface, port_spec);
2752 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2756 hso_dev->port_data.dev_serial = serial;
2757 serial->parent = hso_dev;
2759 if (hso_serial_common_create
2760 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2763 serial->tx_data_length--;
2764 serial->write_data = hso_mux_serial_write_data;
2766 serial->shared_int = mux;
2767 mutex_lock(&serial->shared_int->shared_int_lock);
2768 serial->shared_int->ref_count++;
2769 mutex_unlock(&serial->shared_int->shared_int_lock);
2771 /* and record this serial */
2772 set_serial_by_index(serial->minor, serial);
2774 /* setup the proc dirs and files if needed */
2775 hso_log_port(hso_dev);
2777 /* done, return it */
2782 tty_unregister_device(tty_drv, serial->minor);
2791 static void hso_free_shared_int(struct hso_shared_int *mux)
2793 usb_free_urb(mux->shared_intr_urb);
2794 kfree(mux->shared_intr_buf);
2795 mutex_unlock(&mux->shared_int_lock);
2800 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2802 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2807 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2809 if (!mux->intr_endp) {
2810 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2814 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2815 if (!mux->shared_intr_urb) {
2816 dev_err(&interface->dev, "Could not allocate intr urb?");
2819 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2821 if (!mux->shared_intr_buf) {
2822 dev_err(&interface->dev, "Could not allocate intr buf?");
2826 mutex_init(&mux->shared_int_lock);
2831 kfree(mux->shared_intr_buf);
2832 usb_free_urb(mux->shared_intr_urb);
2837 /* Gets the port spec for a certain interface */
2838 static int hso_get_config_data(struct usb_interface *interface)
2840 struct usb_device *usbdev = interface_to_usbdev(interface);
2842 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2845 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2846 0x86, 0xC0, 0, 0, config_data, 17,
2847 USB_CTRL_SET_TIMEOUT) != 0x11) {
2851 switch (config_data[if_num]) {
2856 result = HSO_PORT_DIAG;
2859 result = HSO_PORT_GPS;
2862 result = HSO_PORT_GPS_CONTROL;
2865 result = HSO_PORT_APP;
2868 result = HSO_PORT_APP2;
2871 result = HSO_PORT_CONTROL;
2874 result = HSO_PORT_NETWORK;
2877 result = HSO_PORT_MODEM;
2880 result = HSO_PORT_MSD;
2883 result = HSO_PORT_PCSC;
2886 result = HSO_PORT_VOICE;
2893 result |= HSO_INTF_BULK;
2895 if (config_data[16] & 0x1)
2896 result |= HSO_INFO_CRC_BUG;
2901 /* called once for each interface upon device insertion */
2902 static int hso_probe(struct usb_interface *interface,
2903 const struct usb_device_id *id)
2905 int mux, i, if_num, port_spec;
2906 unsigned char port_mask;
2907 struct hso_device *hso_dev = NULL;
2908 struct hso_shared_int *shared_int;
2909 struct hso_device *tmp_dev = NULL;
2911 if_num = interface->altsetting->desc.bInterfaceNumber;
2913 /* Get the interface/port specification from either driver_info or from
2914 * the device itself */
2915 if (id->driver_info)
2916 port_spec = ((u32 *)(id->driver_info))[if_num];
2918 port_spec = hso_get_config_data(interface);
2920 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2921 dev_err(&interface->dev, "Not our interface\n");
2924 /* Check if we need to switch to alt interfaces prior to port
2926 if (interface->num_altsetting > 1)
2927 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2928 interface->needs_remote_wakeup = 1;
2930 /* Allocate new hso device(s) */
2931 switch (port_spec & HSO_INTF_MASK) {
2933 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2934 /* Create the network device */
2936 hso_dev = hso_create_net_device(interface);
2943 if (hso_get_mux_ports(interface, &port_mask))
2944 /* TODO: de-allocate everything */
2947 shared_int = hso_create_shared_int(interface);
2951 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2952 if (port_mask & i) {
2953 hso_dev = hso_create_mux_serial_device(
2954 interface, i, shared_int);
2965 /* It's a regular bulk interface */
2966 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2968 hso_dev = hso_create_net_device(interface);
2971 hso_create_bulk_serial_device(interface, port_spec);
2979 /* save our data pointer in this device */
2980 usb_set_intfdata(interface, hso_dev);
2985 hso_free_interface(interface);
2989 /* device removed, cleaning up */
2990 static void hso_disconnect(struct usb_interface *interface)
2992 hso_free_interface(interface);
2994 /* remove reference of our private data */
2995 usb_set_intfdata(interface, NULL);
2998 static void async_get_intf(struct work_struct *data)
3000 struct hso_device *hso_dev =
3001 container_of(data, struct hso_device, async_get_intf);
3002 usb_autopm_get_interface(hso_dev->interface);
3005 static void async_put_intf(struct work_struct *data)
3007 struct hso_device *hso_dev =
3008 container_of(data, struct hso_device, async_put_intf);
3009 usb_autopm_put_interface(hso_dev->interface);
3012 static int hso_get_activity(struct hso_device *hso_dev)
3014 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3015 if (!hso_dev->is_active) {
3016 hso_dev->is_active = 1;
3017 schedule_work(&hso_dev->async_get_intf);
3021 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3024 usb_mark_last_busy(hso_dev->usb);
3029 static int hso_put_activity(struct hso_device *hso_dev)
3031 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3032 if (hso_dev->is_active) {
3033 hso_dev->is_active = 0;
3034 schedule_work(&hso_dev->async_put_intf);
3038 hso_dev->is_active = 0;
3042 /* called by kernel when we need to suspend device */
3043 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3047 /* Stop all serial ports */
3048 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3049 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3050 result = hso_stop_serial_device(serial_table[i]);
3056 /* Stop all network ports */
3057 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3058 if (network_table[i] &&
3059 (network_table[i]->interface == iface)) {
3060 result = hso_stop_net_device(network_table[i]);
3070 /* called by kernel when we need to resume device */
3071 static int hso_resume(struct usb_interface *iface)
3074 struct hso_net *hso_net;
3076 /* Start all serial ports */
3077 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3078 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3079 if (dev2ser(serial_table[i])->open_count) {
3081 hso_start_serial_device(serial_table[i], GFP_NOIO);
3082 hso_kick_transmit(dev2ser(serial_table[i]));
3089 /* Start all network ports */
3090 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3091 if (network_table[i] &&
3092 (network_table[i]->interface == iface)) {
3093 hso_net = dev2net(network_table[i]);
3094 if (hso_net->flags & IFF_UP) {
3095 /* First transmit any lingering data,
3096 then restart the device. */
3097 if (hso_net->skb_tx_buf) {
3098 dev_dbg(&iface->dev,
3100 " lingering data\n");
3101 hso_net_start_xmit(hso_net->skb_tx_buf,
3103 hso_net->skb_tx_buf = NULL;
3105 result = hso_start_net_device(network_table[i]);
3116 static void hso_serial_ref_free(struct kref *ref)
3118 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3120 hso_free_serial_device(hso_dev);
3123 static void hso_free_interface(struct usb_interface *interface)
3125 struct hso_serial *hso_dev;
3126 struct tty_struct *tty;
3129 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3131 && (serial_table[i]->interface == interface)) {
3132 hso_dev = dev2ser(serial_table[i]);
3133 spin_lock_irq(&hso_dev->serial_lock);
3134 tty = tty_kref_get(hso_dev->tty);
3135 spin_unlock_irq(&hso_dev->serial_lock);
3138 mutex_lock(&hso_dev->parent->mutex);
3140 hso_dev->parent->usb_gone = 1;
3141 mutex_unlock(&hso_dev->parent->mutex);
3142 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3146 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3147 if (network_table[i]
3148 && (network_table[i]->interface == interface)) {
3149 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3150 /* hso_stop_net_device doesn't stop the net queue since
3151 * traffic needs to start it again when suspended */
3152 netif_stop_queue(dev2net(network_table[i])->net);
3153 hso_stop_net_device(network_table[i]);
3154 cancel_work_sync(&network_table[i]->async_put_intf);
3155 cancel_work_sync(&network_table[i]->async_get_intf);
3157 rfkill_unregister(rfk);
3158 hso_free_net_device(network_table[i]);
3163 /* Helper functions */
3165 /* Get the endpoint ! */
3166 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3170 struct usb_host_interface *iface = intf->cur_altsetting;
3171 struct usb_endpoint_descriptor *endp;
3173 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3174 endp = &iface->endpoint[i].desc;
3175 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3176 (usb_endpoint_type(endp) == type))
3183 /* Get the byte that describes which ports are enabled */
3184 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3187 struct usb_host_interface *iface = intf->cur_altsetting;
3189 if (iface->extralen == 3) {
3190 *ports = iface->extra[2];
3194 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3195 if (iface->endpoint[i].extralen == 3) {
3196 *ports = iface->endpoint[i].extra[2];
3204 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3205 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3206 struct usb_device *usb, gfp_t gfp)
3210 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3212 shared_int->intr_endp->bEndpointAddress & 0x7F),
3213 shared_int->shared_intr_buf,
3214 shared_int->intr_endp->wMaxPacketSize,
3215 intr_callback, shared_int,
3216 shared_int->intr_endp->bInterval);
3218 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3220 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
3226 /* operations setup of the serial interface */
3227 static const struct tty_operations hso_serial_ops = {
3228 .open = hso_serial_open,
3229 .close = hso_serial_close,
3230 .write = hso_serial_write,
3231 .write_room = hso_serial_write_room,
3232 .ioctl = hso_serial_ioctl,
3233 .set_termios = hso_serial_set_termios,
3234 .chars_in_buffer = hso_serial_chars_in_buffer,
3235 .tiocmget = hso_serial_tiocmget,
3236 .tiocmset = hso_serial_tiocmset,
3237 .unthrottle = hso_unthrottle
3240 static struct usb_driver hso_driver = {
3241 .name = driver_name,
3243 .disconnect = hso_disconnect,
3244 .id_table = hso_ids,
3245 .suspend = hso_suspend,
3246 .resume = hso_resume,
3247 .reset_resume = hso_resume,
3248 .supports_autosuspend = 1,
3251 static int __init hso_init(void)
3256 /* put it in the log */
3257 printk(KERN_INFO "hso: %s\n", version);
3259 /* Initialise the serial table semaphore and table */
3260 spin_lock_init(&serial_table_lock);
3261 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3262 serial_table[i] = NULL;
3264 /* allocate our driver using the proper amount of supported minors */
3265 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3269 /* fill in all needed values */
3270 tty_drv->magic = TTY_DRIVER_MAGIC;
3271 tty_drv->owner = THIS_MODULE;
3272 tty_drv->driver_name = driver_name;
3273 tty_drv->name = tty_filename;
3275 /* if major number is provided as parameter, use that one */
3277 tty_drv->major = tty_major;
3279 tty_drv->minor_start = 0;
3280 tty_drv->num = HSO_SERIAL_TTY_MINORS;
3281 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3282 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3283 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3284 tty_drv->init_termios = tty_std_termios;
3285 hso_init_termios(&tty_drv->init_termios);
3286 tty_set_operations(tty_drv, &hso_serial_ops);
3288 /* register the tty driver */
3289 result = tty_register_driver(tty_drv);
3291 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3296 /* register this module as an usb driver */
3297 result = usb_register(&hso_driver);
3299 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3301 /* cleanup serial interface */
3302 tty_unregister_driver(tty_drv);
3310 static void __exit hso_exit(void)
3312 printk(KERN_INFO "hso: unloaded\n");
3314 tty_unregister_driver(tty_drv);
3315 /* deregister the usb driver */
3316 usb_deregister(&hso_driver);
3319 /* Module definitions */
3320 module_init(hso_init);
3321 module_exit(hso_exit);
3323 MODULE_AUTHOR(MOD_AUTHOR);
3324 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3325 MODULE_LICENSE(MOD_LICENSE);
3326 MODULE_INFO(Version, DRIVER_VERSION);
3328 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3329 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3330 module_param(debug, int, S_IRUGO | S_IWUSR);
3332 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3333 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3334 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3336 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3337 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3338 module_param(disable_net, int, S_IRUGO | S_IWUSR);