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 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 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 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 /* Frees a general hso device */
2367 static void hso_free_device(struct hso_device *hso_dev)
2372 /* Creates a general hso device */
2373 static struct hso_device *hso_create_device(struct usb_interface *intf,
2376 struct hso_device *hso_dev;
2378 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2382 hso_dev->port_spec = port_spec;
2383 hso_dev->usb = interface_to_usbdev(intf);
2384 hso_dev->interface = intf;
2385 kref_init(&hso_dev->ref);
2386 mutex_init(&hso_dev->mutex);
2388 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2389 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2394 /* Removes a network device in the network device table */
2395 static int remove_net_device(struct hso_device *hso_dev)
2399 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2400 if (network_table[i] == hso_dev) {
2401 network_table[i] = NULL;
2405 if (i == HSO_MAX_NET_DEVICES)
2410 /* Frees our network device */
2411 static void hso_free_net_device(struct hso_device *hso_dev)
2414 struct hso_net *hso_net = dev2net(hso_dev);
2420 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2421 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2422 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2424 usb_free_urb(hso_net->mux_bulk_tx_urb);
2425 kfree(hso_net->mux_bulk_tx_buf);
2427 remove_net_device(hso_net->parent);
2430 unregister_netdev(hso_net->net);
2431 free_netdev(hso_net->net);
2434 hso_free_device(hso_dev);
2437 /* initialize the network interface */
2438 static void hso_net_init(struct net_device *net)
2440 struct hso_net *hso_net = netdev_priv(net);
2442 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2444 /* fill in the other fields */
2445 net->open = hso_net_open;
2446 net->stop = hso_net_close;
2447 net->hard_start_xmit = hso_net_start_xmit;
2448 net->tx_timeout = hso_net_tx_timeout;
2449 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2450 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2451 net->type = ARPHRD_NONE;
2452 net->mtu = DEFAULT_MTU - 14;
2453 net->tx_queue_len = 10;
2454 SET_ETHTOOL_OPS(net, &ops);
2456 /* and initialize the semaphore */
2457 spin_lock_init(&hso_net->net_lock);
2460 /* Adds a network device in the network device table */
2461 static int add_net_device(struct hso_device *hso_dev)
2465 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2466 if (network_table[i] == NULL) {
2467 network_table[i] = hso_dev;
2471 if (i == HSO_MAX_NET_DEVICES)
2476 static int hso_radio_toggle(void *data, enum rfkill_state state)
2478 struct hso_device *hso_dev = data;
2479 int enabled = (state == RFKILL_STATE_ON);
2482 mutex_lock(&hso_dev->mutex);
2483 if (hso_dev->usb_gone)
2486 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2487 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2488 USB_CTRL_SET_TIMEOUT);
2489 mutex_unlock(&hso_dev->mutex);
2493 /* Creates and sets up everything for rfkill */
2494 static void hso_create_rfkill(struct hso_device *hso_dev,
2495 struct usb_interface *interface)
2497 struct hso_net *hso_net = dev2net(hso_dev);
2498 struct device *dev = &hso_net->net->dev;
2501 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2503 if (!hso_net->rfkill) {
2504 dev_err(dev, "%s - Out of memory\n", __func__);
2507 rfkn = kzalloc(20, GFP_KERNEL);
2509 rfkill_free(hso_net->rfkill);
2510 hso_net->rfkill = NULL;
2511 dev_err(dev, "%s - Out of memory\n", __func__);
2514 snprintf(rfkn, 20, "hso-%d",
2515 interface->altsetting->desc.bInterfaceNumber);
2516 hso_net->rfkill->name = rfkn;
2517 hso_net->rfkill->state = RFKILL_STATE_ON;
2518 hso_net->rfkill->data = hso_dev;
2519 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2520 if (rfkill_register(hso_net->rfkill) < 0) {
2522 hso_net->rfkill->name = NULL;
2523 rfkill_free(hso_net->rfkill);
2524 hso_net->rfkill = NULL;
2525 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2530 /* Creates our network device */
2531 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2534 struct net_device *net;
2535 struct hso_net *hso_net;
2536 struct hso_device *hso_dev;
2538 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2542 /* allocate our network device, then we can put in our private data */
2543 /* call hso_net_init to do the basic initialization */
2544 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2546 dev_err(&interface->dev, "Unable to create ethernet device\n");
2550 hso_net = netdev_priv(net);
2552 hso_dev->port_data.dev_net = hso_net;
2554 hso_net->parent = hso_dev;
2556 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2558 if (!hso_net->in_endp) {
2559 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2562 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2564 if (!hso_net->out_endp) {
2565 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2568 SET_NETDEV_DEV(net, &interface->dev);
2570 /* registering our net device */
2571 result = register_netdev(net);
2573 dev_err(&interface->dev, "Failed to register device\n");
2577 /* start allocating */
2578 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2579 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2580 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2581 dev_err(&interface->dev, "Could not allocate rx urb\n");
2584 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2586 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2587 dev_err(&interface->dev, "Could not allocate rx buf\n");
2591 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2592 if (!hso_net->mux_bulk_tx_urb) {
2593 dev_err(&interface->dev, "Could not allocate tx urb\n");
2596 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2597 if (!hso_net->mux_bulk_tx_buf) {
2598 dev_err(&interface->dev, "Could not allocate tx buf\n");
2602 add_net_device(hso_dev);
2604 hso_log_port(hso_dev);
2606 hso_create_rfkill(hso_dev, interface);
2610 hso_free_net_device(hso_dev);
2614 static void hso_free_tiomget(struct hso_serial *serial)
2616 struct hso_tiocmget *tiocmget = serial->tiocmget;
2619 if (tiocmget->urb) {
2620 usb_free_urb(tiocmget->urb);
2621 tiocmget->urb = NULL;
2623 serial->tiocmget = NULL;
2628 /* Frees an AT channel ( goes for both mux and non-mux ) */
2629 static void hso_free_serial_device(struct hso_device *hso_dev)
2631 struct hso_serial *serial = dev2ser(hso_dev);
2635 set_serial_by_index(serial->minor, NULL);
2637 hso_serial_common_free(serial);
2639 if (serial->shared_int) {
2640 mutex_lock(&serial->shared_int->shared_int_lock);
2641 if (--serial->shared_int->ref_count == 0)
2642 hso_free_shared_int(serial->shared_int);
2644 mutex_unlock(&serial->shared_int->shared_int_lock);
2646 hso_free_tiomget(serial);
2648 hso_free_device(hso_dev);
2651 /* Creates a bulk AT channel */
2652 static struct hso_device *hso_create_bulk_serial_device(
2653 struct usb_interface *interface, int port)
2655 struct hso_device *hso_dev;
2656 struct hso_serial *serial;
2658 struct hso_tiocmget *tiocmget;
2660 hso_dev = hso_create_device(interface, port);
2664 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2668 serial->parent = hso_dev;
2669 hso_dev->port_data.dev_serial = serial;
2671 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
2673 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2675 /* it isn't going to break our heart if serial->tiocmget
2676 * allocation fails don't bother checking this.
2678 if (serial->tiocmget) {
2679 tiocmget = serial->tiocmget;
2680 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2681 if (tiocmget->urb) {
2682 mutex_init(&tiocmget->mutex);
2683 init_waitqueue_head(&tiocmget->waitq);
2684 tiocmget->endp = hso_get_ep(
2686 USB_ENDPOINT_XFER_INT,
2689 hso_free_tiomget(serial);
2695 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2699 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2701 if (!serial->in_endp) {
2702 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2708 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2709 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2713 serial->write_data = hso_std_serial_write_data;
2715 /* and record this serial */
2716 set_serial_by_index(serial->minor, serial);
2718 /* setup the proc dirs and files if needed */
2719 hso_log_port(hso_dev);
2721 /* done, return it */
2725 hso_serial_common_free(serial);
2727 hso_free_tiomget(serial);
2729 hso_free_device(hso_dev);
2733 /* Creates a multiplexed AT channel */
2735 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2737 struct hso_shared_int *mux)
2739 struct hso_device *hso_dev;
2740 struct hso_serial *serial;
2743 port_spec = HSO_INTF_MUX;
2744 port_spec &= ~HSO_PORT_MASK;
2746 port_spec |= hso_mux_to_port(port);
2747 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2750 hso_dev = hso_create_device(interface, port_spec);
2754 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2758 hso_dev->port_data.dev_serial = serial;
2759 serial->parent = hso_dev;
2761 if (hso_serial_common_create
2762 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2765 serial->tx_data_length--;
2766 serial->write_data = hso_mux_serial_write_data;
2768 serial->shared_int = mux;
2769 mutex_lock(&serial->shared_int->shared_int_lock);
2770 serial->shared_int->ref_count++;
2771 mutex_unlock(&serial->shared_int->shared_int_lock);
2773 /* and record this serial */
2774 set_serial_by_index(serial->minor, serial);
2776 /* setup the proc dirs and files if needed */
2777 hso_log_port(hso_dev);
2779 /* done, return it */
2784 tty_unregister_device(tty_drv, serial->minor);
2788 hso_free_device(hso_dev);
2793 static void hso_free_shared_int(struct hso_shared_int *mux)
2795 usb_free_urb(mux->shared_intr_urb);
2796 kfree(mux->shared_intr_buf);
2797 mutex_unlock(&mux->shared_int_lock);
2802 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2804 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2809 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2811 if (!mux->intr_endp) {
2812 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2816 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2817 if (!mux->shared_intr_urb) {
2818 dev_err(&interface->dev, "Could not allocate intr urb?");
2821 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2823 if (!mux->shared_intr_buf) {
2824 dev_err(&interface->dev, "Could not allocate intr buf?");
2828 mutex_init(&mux->shared_int_lock);
2833 kfree(mux->shared_intr_buf);
2834 usb_free_urb(mux->shared_intr_urb);
2839 /* Gets the port spec for a certain interface */
2840 static int hso_get_config_data(struct usb_interface *interface)
2842 struct usb_device *usbdev = interface_to_usbdev(interface);
2844 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2847 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2848 0x86, 0xC0, 0, 0, config_data, 17,
2849 USB_CTRL_SET_TIMEOUT) != 0x11) {
2853 switch (config_data[if_num]) {
2858 result = HSO_PORT_DIAG;
2861 result = HSO_PORT_GPS;
2864 result = HSO_PORT_GPS_CONTROL;
2867 result = HSO_PORT_APP;
2870 result = HSO_PORT_APP2;
2873 result = HSO_PORT_CONTROL;
2876 result = HSO_PORT_NETWORK;
2879 result = HSO_PORT_MODEM;
2882 result = HSO_PORT_MSD;
2885 result = HSO_PORT_PCSC;
2888 result = HSO_PORT_VOICE;
2895 result |= HSO_INTF_BULK;
2897 if (config_data[16] & 0x1)
2898 result |= HSO_INFO_CRC_BUG;
2903 /* called once for each interface upon device insertion */
2904 static int hso_probe(struct usb_interface *interface,
2905 const struct usb_device_id *id)
2907 int mux, i, if_num, port_spec;
2908 unsigned char port_mask;
2909 struct hso_device *hso_dev = NULL;
2910 struct hso_shared_int *shared_int;
2911 struct hso_device *tmp_dev = NULL;
2913 if_num = interface->altsetting->desc.bInterfaceNumber;
2915 /* Get the interface/port specification from either driver_info or from
2916 * the device itself */
2917 if (id->driver_info)
2918 port_spec = ((u32 *)(id->driver_info))[if_num];
2920 port_spec = hso_get_config_data(interface);
2922 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2923 dev_err(&interface->dev, "Not our interface\n");
2926 /* Check if we need to switch to alt interfaces prior to port
2928 if (interface->num_altsetting > 1)
2929 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2930 interface->needs_remote_wakeup = 1;
2932 /* Allocate new hso device(s) */
2933 switch (port_spec & HSO_INTF_MASK) {
2935 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2936 /* Create the network device */
2938 hso_dev = hso_create_net_device(interface);
2945 if (hso_get_mux_ports(interface, &port_mask))
2946 /* TODO: de-allocate everything */
2949 shared_int = hso_create_shared_int(interface);
2953 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2954 if (port_mask & i) {
2955 hso_dev = hso_create_mux_serial_device(
2956 interface, i, shared_int);
2967 /* It's a regular bulk interface */
2968 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2970 hso_dev = hso_create_net_device(interface);
2973 hso_create_bulk_serial_device(interface, port_spec);
2981 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2983 /* save our data pointer in this device */
2984 usb_set_intfdata(interface, hso_dev);
2989 hso_free_interface(interface);
2993 /* device removed, cleaning up */
2994 static void hso_disconnect(struct usb_interface *interface)
2996 hso_free_interface(interface);
2998 /* remove reference of our private data */
2999 usb_set_intfdata(interface, NULL);
3001 usb_driver_release_interface(&hso_driver, interface);
3004 static void async_get_intf(struct work_struct *data)
3006 struct hso_device *hso_dev =
3007 container_of(data, struct hso_device, async_get_intf);
3008 usb_autopm_get_interface(hso_dev->interface);
3011 static void async_put_intf(struct work_struct *data)
3013 struct hso_device *hso_dev =
3014 container_of(data, struct hso_device, async_put_intf);
3015 usb_autopm_put_interface(hso_dev->interface);
3018 static int hso_get_activity(struct hso_device *hso_dev)
3020 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3021 if (!hso_dev->is_active) {
3022 hso_dev->is_active = 1;
3023 schedule_work(&hso_dev->async_get_intf);
3027 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3030 usb_mark_last_busy(hso_dev->usb);
3035 static int hso_put_activity(struct hso_device *hso_dev)
3037 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3038 if (hso_dev->is_active) {
3039 hso_dev->is_active = 0;
3040 schedule_work(&hso_dev->async_put_intf);
3044 hso_dev->is_active = 0;
3048 /* called by kernel when we need to suspend device */
3049 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3053 /* Stop all serial ports */
3054 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3055 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3056 result = hso_stop_serial_device(serial_table[i]);
3062 /* Stop all network ports */
3063 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3064 if (network_table[i] &&
3065 (network_table[i]->interface == iface)) {
3066 result = hso_stop_net_device(network_table[i]);
3076 /* called by kernel when we need to resume device */
3077 static int hso_resume(struct usb_interface *iface)
3080 struct hso_net *hso_net;
3082 /* Start all serial ports */
3083 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3084 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3085 if (dev2ser(serial_table[i])->open_count) {
3087 hso_start_serial_device(serial_table[i], GFP_NOIO);
3088 hso_kick_transmit(dev2ser(serial_table[i]));
3095 /* Start all network ports */
3096 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3097 if (network_table[i] &&
3098 (network_table[i]->interface == iface)) {
3099 hso_net = dev2net(network_table[i]);
3100 if (hso_net->flags & IFF_UP) {
3101 /* First transmit any lingering data,
3102 then restart the device. */
3103 if (hso_net->skb_tx_buf) {
3104 dev_dbg(&iface->dev,
3106 " lingering data\n");
3107 hso_net_start_xmit(hso_net->skb_tx_buf,
3109 hso_net->skb_tx_buf = NULL;
3111 result = hso_start_net_device(network_table[i]);
3122 static void hso_serial_ref_free(struct kref *ref)
3124 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3126 hso_free_serial_device(hso_dev);
3129 static void hso_free_interface(struct usb_interface *interface)
3131 struct hso_serial *hso_dev;
3132 struct tty_struct *tty;
3135 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3137 && (serial_table[i]->interface == interface)) {
3138 hso_dev = dev2ser(serial_table[i]);
3139 spin_lock_irq(&hso_dev->serial_lock);
3140 tty = tty_kref_get(hso_dev->tty);
3141 spin_unlock_irq(&hso_dev->serial_lock);
3144 mutex_lock(&hso_dev->parent->mutex);
3146 hso_dev->parent->usb_gone = 1;
3147 mutex_unlock(&hso_dev->parent->mutex);
3148 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3152 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3153 if (network_table[i]
3154 && (network_table[i]->interface == interface)) {
3155 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3156 /* hso_stop_net_device doesn't stop the net queue since
3157 * traffic needs to start it again when suspended */
3158 netif_stop_queue(dev2net(network_table[i])->net);
3159 hso_stop_net_device(network_table[i]);
3160 cancel_work_sync(&network_table[i]->async_put_intf);
3161 cancel_work_sync(&network_table[i]->async_get_intf);
3163 rfkill_unregister(rfk);
3164 hso_free_net_device(network_table[i]);
3169 /* Helper functions */
3171 /* Get the endpoint ! */
3172 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3176 struct usb_host_interface *iface = intf->cur_altsetting;
3177 struct usb_endpoint_descriptor *endp;
3179 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3180 endp = &iface->endpoint[i].desc;
3181 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3182 (usb_endpoint_type(endp) == type))
3189 /* Get the byte that describes which ports are enabled */
3190 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3193 struct usb_host_interface *iface = intf->cur_altsetting;
3195 if (iface->extralen == 3) {
3196 *ports = iface->extra[2];
3200 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3201 if (iface->endpoint[i].extralen == 3) {
3202 *ports = iface->endpoint[i].extra[2];
3210 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3211 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3212 struct usb_device *usb, gfp_t gfp)
3216 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3218 shared_int->intr_endp->bEndpointAddress & 0x7F),
3219 shared_int->shared_intr_buf,
3220 shared_int->intr_endp->wMaxPacketSize,
3221 intr_callback, shared_int,
3222 shared_int->intr_endp->bInterval);
3224 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3226 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
3232 /* operations setup of the serial interface */
3233 static const struct tty_operations hso_serial_ops = {
3234 .open = hso_serial_open,
3235 .close = hso_serial_close,
3236 .write = hso_serial_write,
3237 .write_room = hso_serial_write_room,
3238 .ioctl = hso_serial_ioctl,
3239 .set_termios = hso_serial_set_termios,
3240 .chars_in_buffer = hso_serial_chars_in_buffer,
3241 .tiocmget = hso_serial_tiocmget,
3242 .tiocmset = hso_serial_tiocmset,
3243 .unthrottle = hso_unthrottle
3246 static struct usb_driver hso_driver = {
3247 .name = driver_name,
3249 .disconnect = hso_disconnect,
3250 .id_table = hso_ids,
3251 .suspend = hso_suspend,
3252 .resume = hso_resume,
3253 .reset_resume = hso_resume,
3254 .supports_autosuspend = 1,
3257 static int __init hso_init(void)
3262 /* put it in the log */
3263 printk(KERN_INFO "hso: %s\n", version);
3265 /* Initialise the serial table semaphore and table */
3266 spin_lock_init(&serial_table_lock);
3267 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3268 serial_table[i] = NULL;
3270 /* allocate our driver using the proper amount of supported minors */
3271 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3275 /* fill in all needed values */
3276 tty_drv->magic = TTY_DRIVER_MAGIC;
3277 tty_drv->owner = THIS_MODULE;
3278 tty_drv->driver_name = driver_name;
3279 tty_drv->name = tty_filename;
3281 /* if major number is provided as parameter, use that one */
3283 tty_drv->major = tty_major;
3285 tty_drv->minor_start = 0;
3286 tty_drv->num = HSO_SERIAL_TTY_MINORS;
3287 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3288 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3289 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3290 tty_drv->init_termios = tty_std_termios;
3291 hso_init_termios(&tty_drv->init_termios);
3292 tty_set_operations(tty_drv, &hso_serial_ops);
3294 /* register the tty driver */
3295 result = tty_register_driver(tty_drv);
3297 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3302 /* register this module as an usb driver */
3303 result = usb_register(&hso_driver);
3305 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3307 /* cleanup serial interface */
3308 tty_unregister_driver(tty_drv);
3316 static void __exit hso_exit(void)
3318 printk(KERN_INFO "hso: unloaded\n");
3320 tty_unregister_driver(tty_drv);
3321 /* deregister the usb driver */
3322 usb_deregister(&hso_driver);
3325 /* Module definitions */
3326 module_init(hso_init);
3327 module_exit(hso_exit);
3329 MODULE_AUTHOR(MOD_AUTHOR);
3330 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3331 MODULE_LICENSE(MOD_LICENSE);
3332 MODULE_INFO(Version, DRIVER_VERSION);
3334 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3335 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3336 module_param(debug, int, S_IRUGO | S_IWUSR);
3338 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3339 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3340 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3342 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3343 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3344 module_param(disable_net, int, S_IRUGO | S_IWUSR);