1 /******************************************************************************
3 * Driver for Option High Speed Mobile Devices.
5 * Copyright (C) 2008 Option International
6 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
7 * <ajb@spheresystems.co.uk>
8 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
9 * Copyright (C) 2008 Novell, Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 *****************************************************************************/
28 /******************************************************************************
30 * Description of the device:
32 * Interface 0: Contains the IP network interface on the bulk end points.
33 * The multiplexed serial ports are using the interrupt and
35 * Interrupt contains a bitmap telling which multiplexed
36 * serialport needs servicing.
38 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
39 * port is opened, as this have a huge impact on the network port
42 * Interface 2: Standard modem interface - circuit switched interface, this
43 * can be used to make a standard ppp connection however it
44 * should not be used in conjunction with the IP network interface
45 * enabled for USB performance reasons i.e. if using this set
46 * ideally disable_net=1.
48 *****************************************************************************/
50 #include <linux/sched.h>
51 #include <linux/slab.h>
52 #include <linux/init.h>
53 #include <linux/delay.h>
54 #include <linux/netdevice.h>
55 #include <linux/module.h>
56 #include <linux/ethtool.h>
57 #include <linux/usb.h>
58 #include <linux/timer.h>
59 #include <linux/tty.h>
60 #include <linux/tty_driver.h>
61 #include <linux/tty_flip.h>
62 #include <linux/kmod.h>
63 #include <linux/rfkill.h>
65 #include <linux/uaccess.h>
66 #include <linux/usb/cdc.h>
68 #include <asm/byteorder.h>
69 #include <linux/serial_core.h>
70 #include <linux/serial.h>
73 #define DRIVER_VERSION "1.2"
74 #define MOD_AUTHOR "Option Wireless"
75 #define MOD_DESCRIPTION "USB High Speed Option driver"
76 #define MOD_LICENSE "GPL"
78 #define HSO_MAX_NET_DEVICES 10
79 #define HSO__MAX_MTU 2048
80 #define DEFAULT_MTU 1500
81 #define DEFAULT_MRU 1500
83 #define CTRL_URB_RX_SIZE 1024
84 #define CTRL_URB_TX_SIZE 64
86 #define BULK_URB_RX_SIZE 4096
87 #define BULK_URB_TX_SIZE 8192
89 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
90 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
91 #define MUX_BULK_RX_BUF_COUNT 4
92 #define USB_TYPE_OPTION_VENDOR 0x20
94 /* These definitions are used with the struct hso_net flags element */
95 /* - use *_bit operations on it. (bit indices not values.) */
96 #define HSO_NET_RUNNING 0
98 #define HSO_NET_TX_TIMEOUT (HZ*10)
100 #define HSO_SERIAL_MAGIC 0x48534f31
102 /* Number of ttys to handle */
103 #define HSO_SERIAL_TTY_MINORS 256
105 #define MAX_RX_URBS 2
107 static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
110 return tty->driver_data;
114 /*****************************************************************************/
115 /* Debugging functions */
116 /*****************************************************************************/
117 #define D__(lvl_, fmt, arg...) \
119 printk(lvl_ "[%d:%s]: " fmt "\n", \
120 __LINE__, __func__, ## arg); \
123 #define D_(lvl, args...) \
126 D__(KERN_INFO, args); \
129 #define D1(args...) D_(0x01, ##args)
130 #define D2(args...) D_(0x02, ##args)
131 #define D3(args...) D_(0x04, ##args)
132 #define D4(args...) D_(0x08, ##args)
133 #define D5(args...) D_(0x10, ##args)
135 /*****************************************************************************/
137 /*****************************************************************************/
138 enum pkt_parse_state {
144 /*****************************************************************************/
146 /*****************************************************************************/
148 struct hso_shared_int {
149 struct usb_endpoint_descriptor *intr_endp;
150 void *shared_intr_buf;
151 struct urb *shared_intr_urb;
152 struct usb_device *usb;
155 struct mutex shared_int_lock;
159 struct hso_device *parent;
160 struct net_device *net;
161 struct rfkill *rfkill;
163 struct usb_endpoint_descriptor *in_endp;
164 struct usb_endpoint_descriptor *out_endp;
166 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
167 struct urb *mux_bulk_tx_urb;
168 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
169 void *mux_bulk_tx_buf;
171 struct sk_buff *skb_rx_buf;
172 struct sk_buff *skb_tx_buf;
174 enum pkt_parse_state rx_parse_state;
177 unsigned short rx_buf_size;
178 unsigned short rx_buf_missing;
179 struct iphdr rx_ip_hdr;
190 #define BM_REQUEST_TYPE (0xa1)
191 #define B_NOTIFICATION (0x20)
192 #define W_VALUE (0x0)
193 #define W_INDEX (0x2)
194 #define W_LENGTH (0x2)
196 #define B_OVERRUN (0x1<<6)
197 #define B_PARITY (0x1<<5)
198 #define B_FRAMING (0x1<<4)
199 #define B_RING_SIGNAL (0x1<<3)
200 #define B_BREAK (0x1<<2)
201 #define B_TX_CARRIER (0x1<<1)
202 #define B_RX_CARRIER (0x1<<0)
204 struct hso_serial_state_notification {
210 u16 UART_state_bitmap;
211 } __attribute__((packed));
213 struct hso_tiocmget {
215 wait_queue_head_t waitq;
217 struct usb_endpoint_descriptor *endp;
219 struct hso_serial_state_notification serial_state_notification;
220 u16 prev_UART_state_bitmap;
221 struct uart_icount icount;
226 struct hso_device *parent;
230 struct hso_shared_int *shared_int;
232 /* rx/tx urb could be either a bulk urb or a control urb depending
233 on which serial port it is used on. */
234 struct urb *rx_urb[MAX_RX_URBS];
236 u8 *rx_data[MAX_RX_URBS];
237 u16 rx_data_length; /* should contain allocated length */
242 u16 tx_data_length; /* should contain allocated length */
245 struct usb_ctrlrequest ctrl_req_tx;
246 struct usb_ctrlrequest ctrl_req_rx;
248 struct usb_endpoint_descriptor *in_endp;
249 struct usb_endpoint_descriptor *out_endp;
251 enum rx_ctrl_state rx_state;
254 unsigned tx_urb_used:1;
256 /* from usb_serial_port */
257 struct tty_struct *tty;
259 spinlock_t serial_lock;
261 int (*write_data) (struct hso_serial *serial);
262 struct hso_tiocmget *tiocmget;
263 /* Hacks required to get flow control
264 * working on the serial receive buffers
265 * so as not to drop characters on the floor.
268 u16 curr_rx_urb_offset;
269 u8 rx_urb_filled[MAX_RX_URBS];
270 struct tasklet_struct unthrottle_tasklet;
271 struct work_struct retry_unthrottle_workqueue;
281 struct hso_serial *dev_serial;
282 struct hso_net *dev_net;
289 struct work_struct async_get_intf;
290 struct work_struct async_put_intf;
292 struct usb_device *usb;
293 struct usb_interface *interface;
297 struct hso_mutex_t *mutex;
300 /* Type of interface */
301 #define HSO_INTF_MASK 0xFF00
302 #define HSO_INTF_MUX 0x0100
303 #define HSO_INTF_BULK 0x0200
306 #define HSO_PORT_MASK 0xFF
307 #define HSO_PORT_NO_PORT 0x0
308 #define HSO_PORT_CONTROL 0x1
309 #define HSO_PORT_APP 0x2
310 #define HSO_PORT_GPS 0x3
311 #define HSO_PORT_PCSC 0x4
312 #define HSO_PORT_APP2 0x5
313 #define HSO_PORT_GPS_CONTROL 0x6
314 #define HSO_PORT_MSD 0x7
315 #define HSO_PORT_VOICE 0x8
316 #define HSO_PORT_DIAG2 0x9
317 #define HSO_PORT_DIAG 0x10
318 #define HSO_PORT_MODEM 0x11
319 #define HSO_PORT_NETWORK 0x12
321 /* Additional device info */
322 #define HSO_INFO_MASK 0xFF000000
323 #define HSO_INFO_CRC_BUG 0x01000000
325 /*****************************************************************************/
327 /*****************************************************************************/
328 /* Serial driver functions */
329 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
330 unsigned int set, unsigned int clear);
331 static void ctrl_callback(struct urb *urb);
332 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
333 static void hso_kick_transmit(struct hso_serial *serial);
334 /* Helper functions */
335 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
336 struct usb_device *usb, gfp_t gfp);
337 static void log_usb_status(int status, const char *function);
338 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
340 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
341 static void hso_free_interface(struct usb_interface *intf);
342 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
343 static int hso_stop_serial_device(struct hso_device *hso_dev);
344 static int hso_start_net_device(struct hso_device *hso_dev);
345 static void hso_free_shared_int(struct hso_shared_int *shared_int);
346 static int hso_stop_net_device(struct hso_device *hso_dev);
347 static void hso_serial_ref_free(struct kref *ref);
348 static void hso_std_serial_read_bulk_callback(struct urb *urb);
349 static int hso_mux_serial_read(struct hso_serial *serial);
350 static void async_get_intf(struct work_struct *data);
351 static void async_put_intf(struct work_struct *data);
352 static int hso_put_activity(struct hso_device *hso_dev);
353 static int hso_get_activity(struct hso_device *hso_dev);
354 static void tiocmget_intr_callback(struct urb *urb);
355 /*****************************************************************************/
356 /* Helping functions */
357 /*****************************************************************************/
361 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
363 return hso_dev->port_data.dev_net;
366 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
368 return hso_dev->port_data.dev_serial;
371 /* Debugging functions */
373 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
376 static char name[255];
378 sprintf(name, "hso[%d:%s]", line_count, func_name);
379 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
382 #define DUMP(buf_, len_) \
383 dbg_dump(__LINE__, __func__, buf_, len_)
385 #define DUMP1(buf_, len_) \
391 #define DUMP(buf_, len_)
392 #define DUMP1(buf_, len_)
395 /* module parameters */
397 static int tty_major;
398 static int disable_net;
401 static const char driver_name[] = "hso";
402 static const char tty_filename[] = "ttyHS";
403 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
404 /* the usb driver itself (registered in hso_init) */
405 static struct usb_driver hso_driver;
406 /* serial structures */
407 static struct tty_driver *tty_drv;
408 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
409 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
410 static spinlock_t serial_table_lock;
411 static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
412 static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
413 /* hso_mutex_table has to be declared statically as hso_device
414 * is freed in hso_serial_open & hso_serial_close while
415 * the mutex is still in use.
417 #define HSO_NUM_MUTEXES (HSO_SERIAL_TTY_MINORS+HSO_MAX_NET_DEVICES)
418 static struct hso_mutex_t hso_mutex_table[HSO_NUM_MUTEXES];
419 static spinlock_t hso_mutex_lock;
421 static const s32 default_port_spec[] = {
422 HSO_INTF_MUX | HSO_PORT_NETWORK,
423 HSO_INTF_BULK | HSO_PORT_DIAG,
424 HSO_INTF_BULK | HSO_PORT_MODEM,
428 static const s32 icon321_port_spec[] = {
429 HSO_INTF_MUX | HSO_PORT_NETWORK,
430 HSO_INTF_BULK | HSO_PORT_DIAG2,
431 HSO_INTF_BULK | HSO_PORT_MODEM,
432 HSO_INTF_BULK | HSO_PORT_DIAG,
436 #define default_port_device(vendor, product) \
437 USB_DEVICE(vendor, product), \
438 .driver_info = (kernel_ulong_t)default_port_spec
440 #define icon321_port_device(vendor, product) \
441 USB_DEVICE(vendor, product), \
442 .driver_info = (kernel_ulong_t)icon321_port_spec
444 /* list of devices we support */
445 static const struct usb_device_id hso_ids[] = {
446 {default_port_device(0x0af0, 0x6711)},
447 {default_port_device(0x0af0, 0x6731)},
448 {default_port_device(0x0af0, 0x6751)},
449 {default_port_device(0x0af0, 0x6771)},
450 {default_port_device(0x0af0, 0x6791)},
451 {default_port_device(0x0af0, 0x6811)},
452 {default_port_device(0x0af0, 0x6911)},
453 {default_port_device(0x0af0, 0x6951)},
454 {default_port_device(0x0af0, 0x6971)},
455 {default_port_device(0x0af0, 0x7011)},
456 {default_port_device(0x0af0, 0x7031)},
457 {default_port_device(0x0af0, 0x7051)},
458 {default_port_device(0x0af0, 0x7071)},
459 {default_port_device(0x0af0, 0x7111)},
460 {default_port_device(0x0af0, 0x7211)},
461 {default_port_device(0x0af0, 0x7251)},
462 {default_port_device(0x0af0, 0x7271)},
463 {default_port_device(0x0af0, 0x7311)},
464 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
465 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
466 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
467 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
468 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
469 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
470 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
471 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
472 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
473 {USB_DEVICE(0x0af0, 0x7701)},
474 {USB_DEVICE(0x0af0, 0x7801)},
475 {USB_DEVICE(0x0af0, 0x7901)},
476 {USB_DEVICE(0x0af0, 0x7361)},
477 {icon321_port_device(0x0af0, 0xd051)},
480 MODULE_DEVICE_TABLE(usb, hso_ids);
482 /* Sysfs attribute */
483 static ssize_t hso_sysfs_show_porttype(struct device *dev,
484 struct device_attribute *attr,
487 struct hso_device *hso_dev = dev->driver_data;
493 switch (hso_dev->port_spec & HSO_PORT_MASK) {
494 case HSO_PORT_CONTROL:
495 port_name = "Control";
498 port_name = "Application";
501 port_name = "Application2";
506 case HSO_PORT_GPS_CONTROL:
507 port_name = "GPS Control";
513 port_name = "Diagnostic";
516 port_name = "Diagnostic2";
521 case HSO_PORT_NETWORK:
522 port_name = "Network";
525 port_name = "Unknown";
529 return sprintf(buf, "%s\n", port_name);
531 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
533 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
537 for (idx = 0; idx < serial->num_rx_urbs; idx++)
538 if (serial->rx_urb[idx] == urb)
540 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
544 /* converts mux value to a port spec value */
545 static u32 hso_mux_to_port(int mux)
551 result = HSO_PORT_CONTROL;
554 result = HSO_PORT_APP;
557 result = HSO_PORT_PCSC;
560 result = HSO_PORT_GPS;
563 result = HSO_PORT_APP2;
566 result = HSO_PORT_NO_PORT;
571 /* converts port spec value to a mux value */
572 static u32 hso_port_to_mux(int port)
576 switch (port & HSO_PORT_MASK) {
577 case HSO_PORT_CONTROL:
598 static struct hso_serial *get_serial_by_shared_int_and_type(
599 struct hso_shared_int *shared_int,
604 port = hso_mux_to_port(mux);
606 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
608 && (dev2ser(serial_table[i])->shared_int == shared_int)
609 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
610 return dev2ser(serial_table[i]);
617 static struct hso_serial *get_serial_by_index(unsigned index)
619 struct hso_serial *serial = NULL;
622 spin_lock_irqsave(&serial_table_lock, flags);
623 if (serial_table[index])
624 serial = dev2ser(serial_table[index]);
625 spin_unlock_irqrestore(&serial_table_lock, flags);
630 static int get_free_serial_index(void)
635 spin_lock_irqsave(&serial_table_lock, flags);
636 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
637 if (serial_table[index] == NULL) {
638 spin_unlock_irqrestore(&serial_table_lock, flags);
642 spin_unlock_irqrestore(&serial_table_lock, flags);
644 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
648 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
652 spin_lock_irqsave(&serial_table_lock, flags);
654 serial_table[index] = serial->parent;
656 serial_table[index] = NULL;
657 spin_unlock_irqrestore(&serial_table_lock, flags);
661 static struct hso_mutex_t *hso_get_free_mutex(void)
664 struct hso_mutex_t *curr_hso_mutex;
666 spin_lock(&hso_mutex_lock);
667 for (index = 0; index < HSO_NUM_MUTEXES; index++) {
668 curr_hso_mutex = &hso_mutex_table[index];
669 if (!curr_hso_mutex->allocated) {
670 curr_hso_mutex->allocated = 1;
671 spin_unlock(&hso_mutex_lock);
672 return curr_hso_mutex;
675 printk(KERN_ERR "BUG %s: no free hso_mutexs devices in table\n"
677 spin_unlock(&hso_mutex_lock);
681 static void hso_free_mutex(struct hso_mutex_t *mutex)
683 spin_lock(&hso_mutex_lock);
684 mutex->allocated = 0;
685 spin_unlock(&hso_mutex_lock);
688 /* log a meaningful explanation of an USB status */
689 static void log_usb_status(int status, const char *function)
695 explanation = "no device";
698 explanation = "endpoint not enabled";
701 explanation = "endpoint stalled";
704 explanation = "not enough bandwidth";
707 explanation = "device disabled";
710 explanation = "device suspended";
716 explanation = "internal error";
719 explanation = "unknown status";
722 D1("%s: received USB status - %s (%d)", function, explanation, status);
725 /* Network interface functions */
727 /* called when net interface is brought up by ifconfig */
728 static int hso_net_open(struct net_device *net)
730 struct hso_net *odev = netdev_priv(net);
731 unsigned long flags = 0;
734 dev_err(&net->dev, "No net device !\n");
738 odev->skb_tx_buf = NULL;
740 /* setup environment */
741 spin_lock_irqsave(&odev->net_lock, flags);
742 odev->rx_parse_state = WAIT_IP;
743 odev->rx_buf_size = 0;
744 odev->rx_buf_missing = sizeof(struct iphdr);
745 spin_unlock_irqrestore(&odev->net_lock, flags);
747 hso_start_net_device(odev->parent);
749 /* We are up and running. */
750 set_bit(HSO_NET_RUNNING, &odev->flags);
752 /* Tell the kernel we are ready to start receiving from it */
753 netif_start_queue(net);
758 /* called when interface is brought down by ifconfig */
759 static int hso_net_close(struct net_device *net)
761 struct hso_net *odev = netdev_priv(net);
763 /* we don't need the queue anymore */
764 netif_stop_queue(net);
765 /* no longer running */
766 clear_bit(HSO_NET_RUNNING, &odev->flags);
768 hso_stop_net_device(odev->parent);
774 /* USB tells is xmit done, we should start the netqueue again */
775 static void write_bulk_callback(struct urb *urb)
777 struct hso_net *odev = urb->context;
778 int status = urb->status;
781 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
782 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
786 /* Do we still have a valid kernel network device? */
787 if (!netif_device_present(odev->net)) {
788 dev_err(&urb->dev->dev, "%s: net device not present\n",
793 /* log status, but don't act on it, we don't need to resubmit anything
796 log_usb_status(status, __func__);
798 hso_put_activity(odev->parent);
800 /* Tell the network interface we are ready for another frame */
801 netif_wake_queue(odev->net);
804 /* called by kernel when we need to transmit a packet */
805 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
807 struct hso_net *odev = netdev_priv(net);
810 /* Tell the kernel, "No more frames 'til we are done with this one." */
811 netif_stop_queue(net);
812 if (hso_get_activity(odev->parent) == -EAGAIN) {
813 odev->skb_tx_buf = skb;
818 DUMP1(skb->data, skb->len);
819 /* Copy it from kernel memory to OUR memory */
820 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
821 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
823 /* Fill in the URB for shipping it out. */
824 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
826 usb_sndbulkpipe(odev->parent->usb,
828 bEndpointAddress & 0x7F),
829 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
832 /* Deal with the Zero Length packet problem, I hope */
833 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
835 /* Send the URB on its merry way. */
836 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
838 dev_warn(&odev->parent->interface->dev,
839 "failed mux_bulk_tx_urb %d", result);
840 net->stats.tx_errors++;
841 netif_start_queue(net);
843 net->stats.tx_packets++;
844 net->stats.tx_bytes += skb->len;
845 /* And tell the kernel when the last transmit started. */
846 net->trans_start = jiffies;
853 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
855 struct hso_net *odev = netdev_priv(net);
857 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
858 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
859 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
862 static struct ethtool_ops ops = {
863 .get_drvinfo = hso_get_drvinfo,
864 .get_link = ethtool_op_get_link
867 /* called when a packet did not ack after watchdogtimeout */
868 static void hso_net_tx_timeout(struct net_device *net)
870 struct hso_net *odev = netdev_priv(net);
875 /* Tell syslog we are hosed. */
876 dev_warn(&net->dev, "Tx timed out.\n");
878 /* Tear the waiting frame off the list */
879 if (odev->mux_bulk_tx_urb
880 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
881 usb_unlink_urb(odev->mux_bulk_tx_urb);
883 /* Update statistics */
884 net->stats.tx_errors++;
887 /* make a real packet from the received USB buffer */
888 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
889 unsigned int count, unsigned char is_eop)
891 unsigned short temp_bytes;
892 unsigned short buffer_offset = 0;
893 unsigned short frame_len;
894 unsigned char *tmp_rx_buf;
897 D1("Rx %d bytes", count);
898 DUMP(ip_pkt, min(128, (int)count));
901 switch (odev->rx_parse_state) {
903 /* waiting for IP header. */
904 /* wanted bytes - size of ip header */
907 odev->rx_buf_missing) ? count : odev->
910 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
911 odev->rx_buf_size, ip_pkt + buffer_offset,
914 odev->rx_buf_size += temp_bytes;
915 buffer_offset += temp_bytes;
916 odev->rx_buf_missing -= temp_bytes;
919 if (!odev->rx_buf_missing) {
920 /* header is complete allocate an sk_buffer and
921 * continue to WAIT_DATA */
922 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
924 if ((frame_len > DEFAULT_MRU) ||
925 (frame_len < sizeof(struct iphdr))) {
926 dev_err(&odev->net->dev,
927 "Invalid frame (%d) length\n",
929 odev->rx_parse_state = WAIT_SYNC;
932 /* Allocate an sk_buff */
933 odev->skb_rx_buf = dev_alloc_skb(frame_len);
934 if (!odev->skb_rx_buf) {
935 /* We got no receive buffer. */
936 D1("could not allocate memory");
937 odev->rx_parse_state = WAIT_SYNC;
940 /* Here's where it came from */
941 odev->skb_rx_buf->dev = odev->net;
943 /* Copy what we got so far. make room for iphdr
946 skb_put(odev->skb_rx_buf,
947 sizeof(struct iphdr));
948 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
949 sizeof(struct iphdr));
952 odev->rx_buf_size = sizeof(struct iphdr);
954 /* Filip actually use .tot_len */
955 odev->rx_buf_missing =
956 frame_len - sizeof(struct iphdr);
957 odev->rx_parse_state = WAIT_DATA;
962 temp_bytes = (count < odev->rx_buf_missing)
963 ? count : odev->rx_buf_missing;
965 /* Copy the rest of the bytes that are left in the
966 * buffer into the waiting sk_buf. */
967 /* Make room for temp_bytes after tail. */
968 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
969 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
971 odev->rx_buf_missing -= temp_bytes;
973 buffer_offset += temp_bytes;
974 odev->rx_buf_size += temp_bytes;
975 if (!odev->rx_buf_missing) {
976 /* Packet is complete. Inject into stack. */
977 /* We have IP packet here */
978 odev->skb_rx_buf->protocol =
979 __constant_htons(ETH_P_IP);
981 odev->skb_rx_buf->ip_summed =
982 CHECKSUM_UNNECESSARY;
984 skb_reset_mac_header(odev->skb_rx_buf);
986 /* Ship it off to the kernel */
987 netif_rx(odev->skb_rx_buf);
988 /* No longer our buffer. */
989 odev->skb_rx_buf = NULL;
991 /* update out statistics */
992 odev->net->stats.rx_packets++;
994 odev->net->stats.rx_bytes += odev->rx_buf_size;
996 odev->rx_buf_size = 0;
997 odev->rx_buf_missing = sizeof(struct iphdr);
998 odev->rx_parse_state = WAIT_IP;
1013 /* Recovery mechanism for WAIT_SYNC state. */
1015 if (odev->rx_parse_state == WAIT_SYNC) {
1016 odev->rx_parse_state = WAIT_IP;
1017 odev->rx_buf_size = 0;
1018 odev->rx_buf_missing = sizeof(struct iphdr);
1023 /* Moving data from usb to kernel (in interrupt state) */
1024 static void read_bulk_callback(struct urb *urb)
1026 struct hso_net *odev = urb->context;
1027 struct net_device *net;
1029 int status = urb->status;
1031 /* is al ok? (Filip: Who's Al ?) */
1033 log_usb_status(status, __func__);
1038 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
1039 D1("BULK IN callback but driver is not active!");
1042 usb_mark_last_busy(urb->dev);
1046 if (!netif_device_present(net)) {
1047 /* Somebody killed our network interface... */
1051 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
1053 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1054 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
1055 if (((rest == 5) || (rest == 6))
1056 && !memcmp(((u8 *) urb->transfer_buffer) +
1057 urb->actual_length - 4, crc_check, 4)) {
1058 urb->actual_length -= 4;
1062 /* do we even have a packet? */
1063 if (urb->actual_length) {
1064 /* Handle the IP stream, add header and push it onto network
1065 * stack if the packet is complete. */
1066 spin_lock(&odev->net_lock);
1067 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1068 (urb->transfer_buffer_length >
1069 urb->actual_length) ? 1 : 0);
1070 spin_unlock(&odev->net_lock);
1073 /* We are done with this URB, resubmit it. Prep the USB to wait for
1074 * another frame. Reuse same as received. */
1075 usb_fill_bulk_urb(urb,
1077 usb_rcvbulkpipe(odev->parent->usb,
1079 bEndpointAddress & 0x7F),
1080 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1081 read_bulk_callback, odev);
1083 /* Give this to the USB subsystem so it can tell us when more data
1085 result = usb_submit_urb(urb, GFP_ATOMIC);
1087 dev_warn(&odev->parent->interface->dev,
1088 "%s failed submit mux_bulk_rx_urb %d", __func__,
1092 /* Serial driver functions */
1094 static void _hso_serial_set_termios(struct tty_struct *tty,
1095 struct ktermios *old)
1097 struct hso_serial *serial = get_serial_by_tty(tty);
1098 struct ktermios *termios;
1100 if ((!tty) || (!tty->termios) || (!serial)) {
1101 printk(KERN_ERR "%s: no tty structures", __func__);
1105 D4("port %d", serial->minor);
1108 * The default requirements for this device are:
1110 termios = tty->termios;
1112 ~(IGNBRK /* disable ignore break */
1113 | BRKINT /* disable break causes interrupt */
1114 | PARMRK /* disable mark parity errors */
1115 | ISTRIP /* disable clear high bit of input characters */
1116 | INLCR /* disable translate NL to CR */
1117 | IGNCR /* disable ignore CR */
1118 | ICRNL /* disable translate CR to NL */
1119 | IXON); /* disable enable XON/XOFF flow control */
1121 /* disable postprocess output characters */
1122 termios->c_oflag &= ~OPOST;
1125 ~(ECHO /* disable echo input characters */
1126 | ECHONL /* disable echo new line */
1127 | ICANON /* disable erase, kill, werase, and rprnt
1128 special characters */
1129 | ISIG /* disable interrupt, quit, and suspend special
1131 | IEXTEN); /* disable non-POSIX special characters */
1134 ~(CSIZE /* no size */
1135 | PARENB /* disable parity bit */
1136 | CBAUD /* clear current baud rate */
1137 | CBAUDEX); /* clear current buad rate */
1139 termios->c_cflag |= CS8; /* character size 8 bits */
1141 /* baud rate 115200 */
1142 tty_encode_baud_rate(serial->tty, 115200, 115200);
1145 * Force low_latency on; otherwise the pushes are scheduled;
1146 * this is bad as it opens up the possibility of dropping bytes
1147 * on the floor. We don't want to drop bytes on the floor. :)
1149 serial->tty->low_latency = 1;
1153 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1156 #ifdef CONFIG_HSO_AUTOPM
1157 usb_mark_last_busy(urb->dev);
1159 /* We are done with this URB, resubmit it. Prep the USB to wait for
1161 usb_fill_bulk_urb(urb, serial->parent->usb,
1162 usb_rcvbulkpipe(serial->parent->usb,
1164 bEndpointAddress & 0x7F),
1165 urb->transfer_buffer, serial->rx_data_length,
1166 hso_std_serial_read_bulk_callback, serial);
1167 /* Give this to the USB subsystem so it can tell us when more data
1169 result = usb_submit_urb(urb, GFP_ATOMIC);
1171 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1179 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1182 struct urb *curr_urb;
1184 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1185 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1186 count = put_rxbuf_data(curr_urb, serial);
1190 serial->curr_rx_urb_idx++;
1191 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1192 serial->curr_rx_urb_idx = 0;
1193 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1198 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1203 urb = serial->rx_urb[0];
1204 if (serial->open_count > 0) {
1205 count = put_rxbuf_data(urb, serial);
1209 /* Re issue a read as long as we receive data. */
1211 if (count == 0 && ((urb->actual_length != 0) ||
1212 (serial->rx_state == RX_PENDING))) {
1213 serial->rx_state = RX_SENT;
1214 hso_mux_serial_read(serial);
1216 serial->rx_state = RX_IDLE;
1220 /* read callback for Diag and CS port */
1221 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1223 struct hso_serial *serial = urb->context;
1224 int status = urb->status;
1228 D1("serial == NULL");
1230 } else if (status) {
1231 log_usb_status(status, __func__);
1235 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1236 D1("Actual length = %d\n", urb->actual_length);
1237 DUMP1(urb->transfer_buffer, urb->actual_length);
1239 /* Anyone listening? */
1240 if (serial->open_count == 0)
1244 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1246 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1248 urb->actual_length %
1249 serial->in_endp->wMaxPacketSize;
1250 if (((rest == 5) || (rest == 6))
1251 && !memcmp(((u8 *) urb->transfer_buffer) +
1252 urb->actual_length - 4, crc_check, 4)) {
1253 urb->actual_length -= 4;
1256 /* Valid data, handle RX data */
1257 spin_lock(&serial->serial_lock);
1258 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1259 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1260 spin_unlock(&serial->serial_lock);
1261 } else if (status == -ENOENT || status == -ECONNRESET) {
1262 /* Unlinked - check for throttled port. */
1263 D2("Port %d, successfully unlinked urb", serial->minor);
1264 spin_lock(&serial->serial_lock);
1265 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1266 hso_resubmit_rx_bulk_urb(serial, urb);
1267 spin_unlock(&serial->serial_lock);
1269 D2("Port %d, status = %d for read urb", serial->minor, status);
1275 * This needs to be a tasklet otherwise we will
1276 * end up recursively calling this function.
1278 void hso_unthrottle_tasklet(struct hso_serial *serial)
1280 unsigned long flags;
1282 spin_lock_irqsave(&serial->serial_lock, flags);
1283 if ((serial->parent->port_spec & HSO_INTF_MUX))
1284 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1286 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1287 spin_unlock_irqrestore(&serial->serial_lock, flags);
1290 static void hso_unthrottle(struct tty_struct *tty)
1292 struct hso_serial *serial = get_serial_by_tty(tty);
1294 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1297 void hso_unthrottle_workfunc(struct work_struct *work)
1299 struct hso_serial *serial =
1300 container_of(work, struct hso_serial,
1301 retry_unthrottle_workqueue);
1302 hso_unthrottle_tasklet(serial);
1305 /* open the requested serial port */
1306 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1308 struct hso_serial *serial = get_serial_by_index(tty->index);
1309 int result1 = 0, result2 = 0;
1310 struct mutex *hso_mutex = NULL;
1314 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1315 tty->driver_data = NULL;
1316 D1("Failed to open port");
1320 hso_mutex = &serial->parent->mutex->mutex;
1321 mutex_lock(hso_mutex);
1322 /* check for port already opened, if not set the termios */
1323 /* The serial->open count needs to be here as hso_serial_close
1324 * will be called even if hso_serial_open returns -ENODEV.
1326 serial->open_count++;
1327 result1 = usb_autopm_get_interface(serial->parent->interface);
1331 D1("Opening %d", serial->minor);
1332 kref_get(&serial->parent->ref);
1335 tty->driver_data = serial;
1338 if (serial->open_count == 1) {
1339 tty->low_latency = 1;
1340 serial->rx_state = RX_IDLE;
1341 /* Force default termio settings */
1342 _hso_serial_set_termios(tty, NULL);
1343 tasklet_init(&serial->unthrottle_tasklet,
1344 (void (*)(unsigned long))hso_unthrottle_tasklet,
1345 (unsigned long)serial);
1346 INIT_WORK(&serial->retry_unthrottle_workqueue,
1347 hso_unthrottle_workfunc);
1348 result2 = hso_start_serial_device(serial->parent, GFP_KERNEL);
1350 hso_stop_serial_device(serial->parent);
1351 serial->open_count--;
1354 D1("Port was already open");
1357 usb_autopm_put_interface(serial->parent->interface);
1361 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1364 refcnt = kref_put(&serial->parent->ref, hso_serial_ref_free);
1365 mutex_unlock(hso_mutex);
1367 hso_free_mutex(container_of(hso_mutex,
1368 struct hso_mutex_t, mutex));
1369 return result1 == 0 ? result2 : result1;
1372 /* close the requested serial port */
1373 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1375 struct hso_serial *serial = tty->driver_data;
1377 struct mutex *hso_mutex;
1380 D1("Closing serial port");
1381 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1382 D1("invalid serial structure bailing out.\n");
1386 usb_gone = serial->parent->usb_gone;
1387 hso_mutex = &serial->parent->mutex->mutex;
1388 mutex_lock(hso_mutex);
1391 usb_autopm_get_interface(serial->parent->interface);
1393 /* reset the rts and dtr */
1394 /* do the actual close */
1395 serial->open_count--;
1396 if (serial->open_count <= 0) {
1397 serial->open_count = 0;
1399 serial->tty->driver_data = NULL;
1403 hso_stop_serial_device(serial->parent);
1404 tasklet_kill(&serial->unthrottle_tasklet);
1405 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1409 usb_autopm_put_interface(serial->parent->interface);
1410 refcnt = kref_put(&serial->parent->ref, hso_serial_ref_free);
1411 mutex_unlock(hso_mutex);
1413 hso_free_mutex(container_of(hso_mutex,
1414 struct hso_mutex_t, mutex));
1417 /* close the requested serial port */
1418 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1421 struct hso_serial *serial = get_serial_by_tty(tty);
1422 int space, tx_bytes;
1423 unsigned long flags;
1426 if (serial == NULL) {
1427 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1431 spin_lock_irqsave(&serial->serial_lock, flags);
1433 space = serial->tx_data_length - serial->tx_buffer_count;
1434 tx_bytes = (count < space) ? count : space;
1439 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1440 serial->tx_buffer_count += tx_bytes;
1443 spin_unlock_irqrestore(&serial->serial_lock, flags);
1445 hso_kick_transmit(serial);
1450 /* how much room is there for writing */
1451 static int hso_serial_write_room(struct tty_struct *tty)
1453 struct hso_serial *serial = get_serial_by_tty(tty);
1455 unsigned long flags;
1457 spin_lock_irqsave(&serial->serial_lock, flags);
1458 room = serial->tx_data_length - serial->tx_buffer_count;
1459 spin_unlock_irqrestore(&serial->serial_lock, flags);
1461 /* return free room */
1465 /* setup the term */
1466 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1468 struct hso_serial *serial = get_serial_by_tty(tty);
1469 unsigned long flags;
1472 D5("Termios called with: cflags new[%d] - old[%d]",
1473 tty->termios->c_cflag, old->c_cflag);
1475 /* the actual setup */
1476 spin_lock_irqsave(&serial->serial_lock, flags);
1477 if (serial->open_count)
1478 _hso_serial_set_termios(tty, old);
1481 spin_unlock_irqrestore(&serial->serial_lock, flags);
1487 /* how many characters in the buffer */
1488 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1490 struct hso_serial *serial = get_serial_by_tty(tty);
1492 unsigned long flags;
1498 spin_lock_irqsave(&serial->serial_lock, flags);
1499 chars = serial->tx_buffer_count;
1500 spin_unlock_irqrestore(&serial->serial_lock, flags);
1504 int tiocmget_submit_urb(struct hso_serial *serial,
1505 struct hso_tiocmget *tiocmget,
1506 struct usb_device *usb)
1510 if (serial->parent->usb_gone)
1512 usb_fill_int_urb(tiocmget->urb, usb,
1515 bEndpointAddress & 0x7F),
1516 &tiocmget->serial_state_notification,
1517 sizeof(struct hso_serial_state_notification),
1518 tiocmget_intr_callback, serial,
1519 tiocmget->endp->bInterval);
1520 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1522 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1529 static void tiocmget_intr_callback(struct urb *urb)
1531 struct hso_serial *serial = urb->context;
1532 struct hso_tiocmget *tiocmget;
1533 int status = urb->status;
1534 u16 UART_state_bitmap, prev_UART_state_bitmap;
1535 struct uart_icount *icount;
1536 struct hso_serial_state_notification *serial_state_notification;
1537 struct usb_device *usb;
1543 log_usb_status(status, __func__);
1546 tiocmget = serial->tiocmget;
1549 usb = serial->parent->usb;
1550 serial_state_notification = &tiocmget->serial_state_notification;
1551 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1552 serial_state_notification->bNotification != B_NOTIFICATION ||
1553 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1554 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
1555 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1557 "hso received invalid serial state notification\n");
1558 DUMP(serial_state_notification,
1559 sizeof(hso_serial_state_notifation))
1562 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1564 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1565 icount = &tiocmget->icount;
1566 spin_lock(&serial->serial_lock);
1567 if ((UART_state_bitmap & B_OVERRUN) !=
1568 (prev_UART_state_bitmap & B_OVERRUN))
1570 if ((UART_state_bitmap & B_PARITY) !=
1571 (prev_UART_state_bitmap & B_PARITY))
1573 if ((UART_state_bitmap & B_FRAMING) !=
1574 (prev_UART_state_bitmap & B_FRAMING))
1576 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1577 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1579 if ((UART_state_bitmap & B_BREAK) !=
1580 (prev_UART_state_bitmap & B_BREAK))
1582 if ((UART_state_bitmap & B_TX_CARRIER) !=
1583 (prev_UART_state_bitmap & B_TX_CARRIER))
1585 if ((UART_state_bitmap & B_RX_CARRIER) !=
1586 (prev_UART_state_bitmap & B_RX_CARRIER))
1588 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1589 spin_unlock(&serial->serial_lock);
1590 tiocmget->intr_completed = 1;
1591 wake_up_interruptible(&tiocmget->waitq);
1593 memset(serial_state_notification, 0,
1594 sizeof(struct hso_serial_state_notification));
1595 tiocmget_submit_urb(serial,
1597 serial->parent->usb);
1601 * next few functions largely stolen from drivers/serial/serial_core.c
1603 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1604 * - mask passed in arg for lines of interest
1605 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1606 * Caller should use TIOCGICOUNT to see which one it was
1609 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1611 DECLARE_WAITQUEUE(wait, current);
1612 struct uart_icount cprev, cnow;
1613 struct hso_tiocmget *tiocmget;
1616 tiocmget = serial->tiocmget;
1620 * note the counters on entry
1622 spin_lock_irq(&serial->serial_lock);
1623 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1624 spin_unlock_irq(&serial->serial_lock);
1625 add_wait_queue(&tiocmget->waitq, &wait);
1627 spin_lock_irq(&serial->serial_lock);
1628 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1629 spin_unlock_irq(&serial->serial_lock);
1630 set_current_state(TASK_INTERRUPTIBLE);
1631 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1632 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1633 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1638 /* see if a signal did it */
1639 if (signal_pending(current)) {
1645 current->state = TASK_RUNNING;
1646 remove_wait_queue(&tiocmget->waitq, &wait);
1652 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1653 * Return: write counters to the user passed counter struct
1654 * NB: both 1->0 and 0->1 transitions are counted except for
1655 * RI where only 0->1 is counted.
1657 static int hso_get_count(struct hso_serial *serial,
1658 struct serial_icounter_struct __user *icnt)
1660 struct serial_icounter_struct icount;
1661 struct uart_icount cnow;
1662 struct hso_tiocmget *tiocmget = serial->tiocmget;
1666 spin_lock_irq(&serial->serial_lock);
1667 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1668 spin_unlock_irq(&serial->serial_lock);
1670 icount.cts = cnow.cts;
1671 icount.dsr = cnow.dsr;
1672 icount.rng = cnow.rng;
1673 icount.dcd = cnow.dcd;
1674 icount.rx = cnow.rx;
1675 icount.tx = cnow.tx;
1676 icount.frame = cnow.frame;
1677 icount.overrun = cnow.overrun;
1678 icount.parity = cnow.parity;
1679 icount.brk = cnow.brk;
1680 icount.buf_overrun = cnow.buf_overrun;
1682 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1686 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1689 struct hso_serial *serial = get_serial_by_tty(tty);
1690 struct hso_tiocmget *tiocmget;
1691 u16 UART_state_bitmap;
1695 D1("no tty structures");
1698 spin_lock_irq(&serial->serial_lock);
1699 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
1700 ((serial->dtr_state) ? TIOCM_DTR : 0);
1701 tiocmget = serial->tiocmget;
1704 UART_state_bitmap = le16_to_cpu(
1705 tiocmget->prev_UART_state_bitmap);
1706 if (UART_state_bitmap & B_RING_SIGNAL)
1707 retval |= TIOCM_RNG;
1708 if (UART_state_bitmap & B_RX_CARRIER)
1710 if (UART_state_bitmap & B_TX_CARRIER)
1711 retval |= TIOCM_DSR;
1713 spin_unlock_irq(&serial->serial_lock);
1717 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1718 unsigned int set, unsigned int clear)
1721 unsigned long flags;
1723 struct hso_serial *serial = get_serial_by_tty(tty);
1727 D1("no tty structures");
1730 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1732 spin_lock_irqsave(&serial->serial_lock, flags);
1733 if (set & TIOCM_RTS)
1734 serial->rts_state = 1;
1735 if (set & TIOCM_DTR)
1736 serial->dtr_state = 1;
1738 if (clear & TIOCM_RTS)
1739 serial->rts_state = 0;
1740 if (clear & TIOCM_DTR)
1741 serial->dtr_state = 0;
1743 if (serial->dtr_state)
1745 if (serial->rts_state)
1748 spin_unlock_irqrestore(&serial->serial_lock, flags);
1750 return usb_control_msg(serial->parent->usb,
1751 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1752 0x21, val, if_num, NULL, 0,
1753 USB_CTRL_SET_TIMEOUT);
1756 static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
1757 unsigned int cmd, unsigned long arg)
1759 struct hso_serial *serial = get_serial_by_tty(tty);
1760 void __user *uarg = (void __user *)arg;
1762 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1768 ret = hso_wait_modem_status(serial, arg);
1772 ret = hso_get_count(serial, uarg);
1782 /* starts a transmit */
1783 static void hso_kick_transmit(struct hso_serial *serial)
1786 unsigned long flags;
1789 spin_lock_irqsave(&serial->serial_lock, flags);
1790 if (!serial->tx_buffer_count)
1793 if (serial->tx_urb_used)
1796 /* Wakeup USB interface if necessary */
1797 if (hso_get_activity(serial->parent) == -EAGAIN)
1800 /* Switch pointers around to avoid memcpy */
1801 temp = serial->tx_buffer;
1802 serial->tx_buffer = serial->tx_data;
1803 serial->tx_data = temp;
1804 serial->tx_data_count = serial->tx_buffer_count;
1805 serial->tx_buffer_count = 0;
1807 /* If temp is set, it means we switched buffers */
1808 if (temp && serial->write_data) {
1809 res = serial->write_data(serial);
1811 serial->tx_urb_used = 1;
1814 spin_unlock_irqrestore(&serial->serial_lock, flags);
1817 /* make a request (for reading and writing data to muxed serial port) */
1818 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1819 struct urb *ctrl_urb,
1820 struct usb_ctrlrequest *ctrl_req,
1821 u8 *ctrl_urb_data, u32 size)
1827 if (!serial || !ctrl_urb || !ctrl_req) {
1828 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1833 ctrl_req->wValue = 0;
1834 ctrl_req->wIndex = hso_port_to_mux(port);
1835 ctrl_req->wLength = size;
1837 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1838 /* Reading command */
1839 ctrl_req->bRequestType = USB_DIR_IN |
1840 USB_TYPE_OPTION_VENDOR |
1841 USB_RECIP_INTERFACE;
1842 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1843 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1845 /* Writing command */
1846 ctrl_req->bRequestType = USB_DIR_OUT |
1847 USB_TYPE_OPTION_VENDOR |
1848 USB_RECIP_INTERFACE;
1849 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1850 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1853 D2("%s command (%02x) len: %d, port: %d",
1854 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1855 ctrl_req->bRequestType, ctrl_req->wLength, port);
1858 ctrl_urb->transfer_flags = 0;
1859 usb_fill_control_urb(ctrl_urb,
1860 serial->parent->usb,
1863 ctrl_urb_data, size, ctrl_callback, serial);
1864 /* Send it on merry way */
1865 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1867 dev_err(&ctrl_urb->dev->dev,
1868 "%s failed submit ctrl_urb %d type %d", __func__,
1877 /* called by intr_callback when read occurs */
1878 static int hso_mux_serial_read(struct hso_serial *serial)
1884 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1885 /* make the request */
1887 if (serial->num_rx_urbs != 1) {
1888 dev_err(&serial->parent->interface->dev,
1889 "ERROR: mux'd reads with multiple buffers "
1893 return mux_device_request(serial,
1894 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1895 serial->parent->port_spec & HSO_PORT_MASK,
1897 &serial->ctrl_req_rx,
1898 serial->rx_data[0], serial->rx_data_length);
1901 /* used for muxed serial port callback (muxed serial read) */
1902 static void intr_callback(struct urb *urb)
1904 struct hso_shared_int *shared_int = urb->context;
1905 struct hso_serial *serial;
1906 unsigned char *port_req;
1907 int status = urb->status;
1910 usb_mark_last_busy(urb->dev);
1918 log_usb_status(status, __func__);
1921 D4("\n--- Got intr callback 0x%02X ---", status);
1924 port_req = urb->transfer_buffer;
1925 D4(" port_req = 0x%.2X\n", *port_req);
1926 /* loop over all muxed ports to find the one sending this */
1927 for (i = 0; i < 8; i++) {
1928 /* max 8 channels on MUX */
1929 if (*port_req & (1 << i)) {
1930 serial = get_serial_by_shared_int_and_type(shared_int,
1932 if (serial != NULL) {
1933 D1("Pending read interrupt on port %d\n", i);
1934 spin_lock(&serial->serial_lock);
1935 if (serial->rx_state == RX_IDLE) {
1936 /* Setup and send a ctrl req read on
1938 if (!serial->rx_urb_filled[0]) {
1939 serial->rx_state = RX_SENT;
1940 hso_mux_serial_read(serial);
1942 serial->rx_state = RX_PENDING;
1945 D1("Already pending a read on "
1948 spin_unlock(&serial->serial_lock);
1952 /* Resubmit interrupt urb */
1953 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1956 /* called for writing to muxed serial port */
1957 static int hso_mux_serial_write_data(struct hso_serial *serial)
1962 return mux_device_request(serial,
1963 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1964 serial->parent->port_spec & HSO_PORT_MASK,
1966 &serial->ctrl_req_tx,
1967 serial->tx_data, serial->tx_data_count);
1970 /* write callback for Diag and CS port */
1971 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1973 struct hso_serial *serial = urb->context;
1974 int status = urb->status;
1978 D1("serial == NULL");
1982 spin_lock(&serial->serial_lock);
1983 serial->tx_urb_used = 0;
1984 spin_unlock(&serial->serial_lock);
1986 log_usb_status(status, __func__);
1989 hso_put_activity(serial->parent);
1991 tty_wakeup(serial->tty);
1992 hso_kick_transmit(serial);
1998 /* called for writing diag or CS serial port */
1999 static int hso_std_serial_write_data(struct hso_serial *serial)
2001 int count = serial->tx_data_count;
2004 usb_fill_bulk_urb(serial->tx_urb,
2005 serial->parent->usb,
2006 usb_sndbulkpipe(serial->parent->usb,
2008 bEndpointAddress & 0x7F),
2009 serial->tx_data, serial->tx_data_count,
2010 hso_std_serial_write_bulk_callback, serial);
2012 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
2014 dev_warn(&serial->parent->usb->dev,
2015 "Failed to submit urb - res %d\n", result);
2022 /* callback after read or write on muxed serial port */
2023 static void ctrl_callback(struct urb *urb)
2025 struct hso_serial *serial = urb->context;
2026 struct usb_ctrlrequest *req;
2027 int status = urb->status;
2033 spin_lock(&serial->serial_lock);
2034 serial->tx_urb_used = 0;
2035 spin_unlock(&serial->serial_lock);
2037 log_usb_status(status, __func__);
2042 req = (struct usb_ctrlrequest *)(urb->setup_packet);
2043 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2044 D4("Actual length of urb = %d\n", urb->actual_length);
2045 DUMP1(urb->transfer_buffer, urb->actual_length);
2047 if (req->bRequestType ==
2048 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2049 /* response to a read command */
2050 serial->rx_urb_filled[0] = 1;
2051 spin_lock(&serial->serial_lock);
2052 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2053 spin_unlock(&serial->serial_lock);
2055 hso_put_activity(serial->parent);
2057 tty_wakeup(serial->tty);
2058 /* response to a write command */
2059 hso_kick_transmit(serial);
2063 /* handle RX data for serial port */
2064 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
2066 struct tty_struct *tty = serial->tty;
2067 int write_length_remaining = 0;
2070 if (urb == NULL || serial == NULL) {
2071 D1("serial = NULL");
2075 /* Push data to tty */
2077 write_length_remaining = urb->actual_length -
2078 serial->curr_rx_urb_offset;
2079 D1("data to push to tty");
2080 while (write_length_remaining) {
2081 if (test_bit(TTY_THROTTLED, &tty->flags))
2083 curr_write_len = tty_insert_flip_string
2084 (tty, urb->transfer_buffer +
2085 serial->curr_rx_urb_offset,
2086 write_length_remaining);
2087 serial->curr_rx_urb_offset += curr_write_len;
2088 write_length_remaining -= curr_write_len;
2089 tty_flip_buffer_push(tty);
2092 if (write_length_remaining == 0) {
2093 serial->curr_rx_urb_offset = 0;
2094 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2096 return write_length_remaining;
2100 /* Base driver functions */
2102 static void hso_log_port(struct hso_device *hso_dev)
2107 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2108 case HSO_PORT_CONTROL:
2109 port_type = "Control";
2112 port_type = "Application";
2117 case HSO_PORT_GPS_CONTROL:
2118 port_type = "GPS control";
2121 port_type = "Application2";
2127 port_type = "Diagnostic";
2129 case HSO_PORT_DIAG2:
2130 port_type = "Diagnostic2";
2132 case HSO_PORT_MODEM:
2133 port_type = "Modem";
2135 case HSO_PORT_NETWORK:
2136 port_type = "Network";
2139 port_type = "Unknown";
2142 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2143 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2145 sprintf(port_dev, "/dev/%s%d", tty_filename,
2146 dev2ser(hso_dev)->minor);
2148 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2149 port_type, port_dev);
2152 static int hso_start_net_device(struct hso_device *hso_dev)
2155 struct hso_net *hso_net = dev2net(hso_dev);
2160 /* send URBs for all read buffers */
2161 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2163 /* Prep a receive URB */
2164 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2166 usb_rcvbulkpipe(hso_dev->usb,
2168 bEndpointAddress & 0x7F),
2169 hso_net->mux_bulk_rx_buf_pool[i],
2170 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2173 /* Put it out there so the device can send us stuff */
2174 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2177 dev_warn(&hso_dev->usb->dev,
2178 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2185 static int hso_stop_net_device(struct hso_device *hso_dev)
2188 struct hso_net *hso_net = dev2net(hso_dev);
2193 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2194 if (hso_net->mux_bulk_rx_urb_pool[i])
2195 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2198 if (hso_net->mux_bulk_tx_urb)
2199 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2204 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2207 struct hso_serial *serial = dev2ser(hso_dev);
2212 /* If it is not the MUX port fill in and submit a bulk urb (already
2213 * allocated in hso_serial_start) */
2214 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2215 for (i = 0; i < serial->num_rx_urbs; i++) {
2216 usb_fill_bulk_urb(serial->rx_urb[i],
2217 serial->parent->usb,
2218 usb_rcvbulkpipe(serial->parent->usb,
2223 serial->rx_data_length,
2224 hso_std_serial_read_bulk_callback,
2226 result = usb_submit_urb(serial->rx_urb[i], flags);
2228 dev_warn(&serial->parent->usb->dev,
2229 "Failed to submit urb - res %d\n",
2235 mutex_lock(&serial->shared_int->shared_int_lock);
2236 if (!serial->shared_int->use_count) {
2238 hso_mux_submit_intr_urb(serial->shared_int,
2239 hso_dev->usb, flags);
2241 serial->shared_int->use_count++;
2242 mutex_unlock(&serial->shared_int->shared_int_lock);
2244 if (serial->tiocmget)
2245 tiocmget_submit_urb(serial,
2247 serial->parent->usb);
2251 static int hso_stop_serial_device(struct hso_device *hso_dev)
2254 struct hso_serial *serial = dev2ser(hso_dev);
2255 struct hso_tiocmget *tiocmget;
2260 for (i = 0; i < serial->num_rx_urbs; i++) {
2261 if (serial->rx_urb[i]) {
2262 usb_kill_urb(serial->rx_urb[i]);
2263 serial->rx_urb_filled[i] = 0;
2266 serial->curr_rx_urb_idx = 0;
2267 serial->curr_rx_urb_offset = 0;
2270 usb_kill_urb(serial->tx_urb);
2272 if (serial->shared_int) {
2273 mutex_lock(&serial->shared_int->shared_int_lock);
2274 if (serial->shared_int->use_count &&
2275 (--serial->shared_int->use_count == 0)) {
2278 urb = serial->shared_int->shared_intr_urb;
2282 mutex_unlock(&serial->shared_int->shared_int_lock);
2284 tiocmget = serial->tiocmget;
2286 wake_up_interruptible(&tiocmget->waitq);
2287 usb_kill_urb(tiocmget->urb);
2293 static void hso_serial_common_free(struct hso_serial *serial)
2297 if (serial->parent->dev)
2298 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2300 tty_unregister_device(tty_drv, serial->minor);
2302 for (i = 0; i < serial->num_rx_urbs; i++) {
2303 /* unlink and free RX URB */
2304 usb_free_urb(serial->rx_urb[i]);
2305 /* free the RX buffer */
2306 kfree(serial->rx_data[i]);
2309 /* unlink and free TX URB */
2310 usb_free_urb(serial->tx_urb);
2311 kfree(serial->tx_data);
2314 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2315 int rx_size, int tx_size)
2321 minor = get_free_serial_index();
2325 /* register our minor number */
2326 serial->parent->dev = tty_register_device(tty_drv, minor,
2327 &serial->parent->interface->dev);
2328 dev = serial->parent->dev;
2329 dev->driver_data = serial->parent;
2330 i = device_create_file(dev, &dev_attr_hsotype);
2332 /* fill in specific data for later use */
2333 serial->minor = minor;
2334 serial->magic = HSO_SERIAL_MAGIC;
2335 spin_lock_init(&serial->serial_lock);
2336 serial->num_rx_urbs = num_urbs;
2338 /* RX, allocate urb and initialize */
2340 /* prepare our RX buffer */
2341 serial->rx_data_length = rx_size;
2342 for (i = 0; i < serial->num_rx_urbs; i++) {
2343 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2344 if (!serial->rx_urb[i]) {
2345 dev_err(dev, "Could not allocate urb?\n");
2348 serial->rx_urb[i]->transfer_buffer = NULL;
2349 serial->rx_urb[i]->transfer_buffer_length = 0;
2350 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2352 if (!serial->rx_data[i]) {
2353 dev_err(dev, "%s - Out of memory\n", __func__);
2358 /* TX, allocate urb and initialize */
2359 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2360 if (!serial->tx_urb) {
2361 dev_err(dev, "Could not allocate urb?\n");
2364 serial->tx_urb->transfer_buffer = NULL;
2365 serial->tx_urb->transfer_buffer_length = 0;
2366 /* prepare our TX buffer */
2367 serial->tx_data_count = 0;
2368 serial->tx_buffer_count = 0;
2369 serial->tx_data_length = tx_size;
2370 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2371 if (!serial->tx_data) {
2372 dev_err(dev, "%s - Out of memory", __func__);
2375 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2376 if (!serial->tx_buffer) {
2377 dev_err(dev, "%s - Out of memory", __func__);
2383 hso_serial_common_free(serial);
2387 /* Frees a general hso device */
2388 static void hso_free_device(struct hso_device *hso_dev)
2393 /* Creates a general hso device */
2394 static struct hso_device *hso_create_device(struct usb_interface *intf,
2397 struct hso_device *hso_dev;
2399 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2403 hso_dev->port_spec = port_spec;
2404 hso_dev->usb = interface_to_usbdev(intf);
2405 hso_dev->interface = intf;
2406 kref_init(&hso_dev->ref);
2407 hso_dev->mutex = hso_get_free_mutex();
2408 if (!hso_dev->mutex) {
2412 mutex_init(&hso_dev->mutex->mutex);
2413 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2414 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2419 /* Removes a network device in the network device table */
2420 static int remove_net_device(struct hso_device *hso_dev)
2424 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2425 if (network_table[i] == hso_dev) {
2426 network_table[i] = NULL;
2430 if (i == HSO_MAX_NET_DEVICES)
2435 /* Frees our network device */
2436 static void hso_free_net_device(struct hso_device *hso_dev)
2439 struct hso_net *hso_net = dev2net(hso_dev);
2445 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2446 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2447 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2449 usb_free_urb(hso_net->mux_bulk_tx_urb);
2450 kfree(hso_net->mux_bulk_tx_buf);
2452 remove_net_device(hso_net->parent);
2455 unregister_netdev(hso_net->net);
2456 free_netdev(hso_net->net);
2458 hso_free_mutex(hso_dev->mutex);
2459 hso_free_device(hso_dev);
2462 /* initialize the network interface */
2463 static void hso_net_init(struct net_device *net)
2465 struct hso_net *hso_net = netdev_priv(net);
2467 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2469 /* fill in the other fields */
2470 net->open = hso_net_open;
2471 net->stop = hso_net_close;
2472 net->hard_start_xmit = hso_net_start_xmit;
2473 net->tx_timeout = hso_net_tx_timeout;
2474 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2475 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2476 net->type = ARPHRD_NONE;
2477 net->mtu = DEFAULT_MTU - 14;
2478 net->tx_queue_len = 10;
2479 SET_ETHTOOL_OPS(net, &ops);
2481 /* and initialize the semaphore */
2482 spin_lock_init(&hso_net->net_lock);
2485 /* Adds a network device in the network device table */
2486 static int add_net_device(struct hso_device *hso_dev)
2490 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2491 if (network_table[i] == NULL) {
2492 network_table[i] = hso_dev;
2496 if (i == HSO_MAX_NET_DEVICES)
2501 static int hso_radio_toggle(void *data, enum rfkill_state state)
2503 struct hso_device *hso_dev = data;
2504 int enabled = (state == RFKILL_STATE_ON);
2507 mutex_lock(&hso_dev->mutex->mutex);
2508 if (hso_dev->usb_gone)
2511 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2512 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2513 USB_CTRL_SET_TIMEOUT);
2514 mutex_unlock(&hso_dev->mutex->mutex);
2518 /* Creates and sets up everything for rfkill */
2519 static void hso_create_rfkill(struct hso_device *hso_dev,
2520 struct usb_interface *interface)
2522 struct hso_net *hso_net = dev2net(hso_dev);
2523 struct device *dev = &hso_net->net->dev;
2526 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2528 if (!hso_net->rfkill) {
2529 dev_err(dev, "%s - Out of memory\n", __func__);
2532 rfkn = kzalloc(20, GFP_KERNEL);
2534 rfkill_free(hso_net->rfkill);
2535 hso_net->rfkill = NULL;
2536 dev_err(dev, "%s - Out of memory\n", __func__);
2539 snprintf(rfkn, 20, "hso-%d",
2540 interface->altsetting->desc.bInterfaceNumber);
2541 hso_net->rfkill->name = rfkn;
2542 hso_net->rfkill->state = RFKILL_STATE_ON;
2543 hso_net->rfkill->data = hso_dev;
2544 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2545 if (rfkill_register(hso_net->rfkill) < 0) {
2547 hso_net->rfkill->name = NULL;
2548 rfkill_free(hso_net->rfkill);
2549 hso_net->rfkill = NULL;
2550 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2555 /* Creates our network device */
2556 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2559 struct net_device *net;
2560 struct hso_net *hso_net;
2561 struct hso_device *hso_dev;
2563 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2567 /* allocate our network device, then we can put in our private data */
2568 /* call hso_net_init to do the basic initialization */
2569 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2571 dev_err(&interface->dev, "Unable to create ethernet device\n");
2575 hso_net = netdev_priv(net);
2577 hso_dev->port_data.dev_net = hso_net;
2579 hso_net->parent = hso_dev;
2581 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2583 if (!hso_net->in_endp) {
2584 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2587 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2589 if (!hso_net->out_endp) {
2590 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2593 SET_NETDEV_DEV(net, &interface->dev);
2595 /* registering our net device */
2596 result = register_netdev(net);
2598 dev_err(&interface->dev, "Failed to register device\n");
2602 /* start allocating */
2603 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2604 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2605 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2606 dev_err(&interface->dev, "Could not allocate rx urb\n");
2609 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2611 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2612 dev_err(&interface->dev, "Could not allocate rx buf\n");
2616 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2617 if (!hso_net->mux_bulk_tx_urb) {
2618 dev_err(&interface->dev, "Could not allocate tx urb\n");
2621 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2622 if (!hso_net->mux_bulk_tx_buf) {
2623 dev_err(&interface->dev, "Could not allocate tx buf\n");
2627 add_net_device(hso_dev);
2629 hso_log_port(hso_dev);
2631 hso_create_rfkill(hso_dev, interface);
2635 hso_free_net_device(hso_dev);
2639 static void hso_free_tiomget(struct hso_serial *serial)
2641 struct hso_tiocmget *tiocmget = serial->tiocmget;
2644 if (tiocmget->urb) {
2645 usb_free_urb(tiocmget->urb);
2646 tiocmget->urb = NULL;
2648 serial->tiocmget = NULL;
2653 /* Frees an AT channel ( goes for both mux and non-mux ) */
2654 static void hso_free_serial_device(struct hso_device *hso_dev)
2656 struct hso_serial *serial = dev2ser(hso_dev);
2660 set_serial_by_index(serial->minor, NULL);
2662 hso_serial_common_free(serial);
2664 if (serial->shared_int) {
2665 mutex_lock(&serial->shared_int->shared_int_lock);
2666 if (--serial->shared_int->ref_count == 0)
2667 hso_free_shared_int(serial->shared_int);
2669 mutex_unlock(&serial->shared_int->shared_int_lock);
2671 hso_free_tiomget(serial);
2673 hso_free_device(hso_dev);
2676 /* Creates a bulk AT channel */
2677 static struct hso_device *hso_create_bulk_serial_device(
2678 struct usb_interface *interface, int port)
2680 struct hso_device *hso_dev;
2681 struct hso_serial *serial;
2683 struct hso_tiocmget *tiocmget;
2685 hso_dev = hso_create_device(interface, port);
2689 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2693 serial->parent = hso_dev;
2694 hso_dev->port_data.dev_serial = serial;
2696 if (port & HSO_PORT_MODEM) {
2698 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2700 /* it isn't going to break our heart if serial->tiocmget
2701 * allocation fails don't bother checking this.
2703 if (serial->tiocmget) {
2704 tiocmget = serial->tiocmget;
2705 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2706 if (tiocmget->urb) {
2707 mutex_init(&tiocmget->mutex);
2708 init_waitqueue_head(&tiocmget->waitq);
2709 tiocmget->endp = hso_get_ep(
2711 USB_ENDPOINT_XFER_INT,
2714 hso_free_tiomget(serial);
2720 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2724 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2726 if (!serial->in_endp) {
2727 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2733 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2734 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2738 serial->write_data = hso_std_serial_write_data;
2740 /* and record this serial */
2741 set_serial_by_index(serial->minor, serial);
2743 /* setup the proc dirs and files if needed */
2744 hso_log_port(hso_dev);
2746 /* done, return it */
2750 hso_serial_common_free(serial);
2752 hso_free_tiomget(serial);
2754 hso_free_device(hso_dev);
2758 /* Creates a multiplexed AT channel */
2760 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2762 struct hso_shared_int *mux)
2764 struct hso_device *hso_dev;
2765 struct hso_serial *serial;
2768 port_spec = HSO_INTF_MUX;
2769 port_spec &= ~HSO_PORT_MASK;
2771 port_spec |= hso_mux_to_port(port);
2772 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2775 hso_dev = hso_create_device(interface, port_spec);
2779 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2783 hso_dev->port_data.dev_serial = serial;
2784 serial->parent = hso_dev;
2786 if (hso_serial_common_create
2787 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2790 serial->tx_data_length--;
2791 serial->write_data = hso_mux_serial_write_data;
2793 serial->shared_int = mux;
2794 mutex_lock(&serial->shared_int->shared_int_lock);
2795 serial->shared_int->ref_count++;
2796 mutex_unlock(&serial->shared_int->shared_int_lock);
2798 /* and record this serial */
2799 set_serial_by_index(serial->minor, serial);
2801 /* setup the proc dirs and files if needed */
2802 hso_log_port(hso_dev);
2804 /* done, return it */
2809 tty_unregister_device(tty_drv, serial->minor);
2813 hso_free_device(hso_dev);
2818 static void hso_free_shared_int(struct hso_shared_int *mux)
2820 usb_free_urb(mux->shared_intr_urb);
2821 kfree(mux->shared_intr_buf);
2822 mutex_unlock(&mux->shared_int_lock);
2827 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2829 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2834 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2836 if (!mux->intr_endp) {
2837 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2841 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2842 if (!mux->shared_intr_urb) {
2843 dev_err(&interface->dev, "Could not allocate intr urb?");
2846 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2848 if (!mux->shared_intr_buf) {
2849 dev_err(&interface->dev, "Could not allocate intr buf?");
2853 mutex_init(&mux->shared_int_lock);
2858 kfree(mux->shared_intr_buf);
2859 usb_free_urb(mux->shared_intr_urb);
2864 /* Gets the port spec for a certain interface */
2865 static int hso_get_config_data(struct usb_interface *interface)
2867 struct usb_device *usbdev = interface_to_usbdev(interface);
2869 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2872 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2873 0x86, 0xC0, 0, 0, config_data, 17,
2874 USB_CTRL_SET_TIMEOUT) != 0x11) {
2878 switch (config_data[if_num]) {
2883 result = HSO_PORT_DIAG;
2886 result = HSO_PORT_GPS;
2889 result = HSO_PORT_GPS_CONTROL;
2892 result = HSO_PORT_APP;
2895 result = HSO_PORT_APP2;
2898 result = HSO_PORT_CONTROL;
2901 result = HSO_PORT_NETWORK;
2904 result = HSO_PORT_MODEM;
2907 result = HSO_PORT_MSD;
2910 result = HSO_PORT_PCSC;
2913 result = HSO_PORT_VOICE;
2920 result |= HSO_INTF_BULK;
2922 if (config_data[16] & 0x1)
2923 result |= HSO_INFO_CRC_BUG;
2928 /* called once for each interface upon device insertion */
2929 static int hso_probe(struct usb_interface *interface,
2930 const struct usb_device_id *id)
2932 int mux, i, if_num, port_spec;
2933 unsigned char port_mask;
2934 struct hso_device *hso_dev = NULL;
2935 struct hso_shared_int *shared_int;
2936 struct hso_device *tmp_dev = NULL;
2938 if_num = interface->altsetting->desc.bInterfaceNumber;
2940 /* Get the interface/port specification from either driver_info or from
2941 * the device itself */
2942 if (id->driver_info)
2943 port_spec = ((u32 *)(id->driver_info))[if_num];
2945 port_spec = hso_get_config_data(interface);
2947 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2948 dev_err(&interface->dev, "Not our interface\n");
2951 /* Check if we need to switch to alt interfaces prior to port
2953 if (interface->num_altsetting > 1)
2954 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2955 interface->needs_remote_wakeup = 1;
2957 /* Allocate new hso device(s) */
2958 switch (port_spec & HSO_INTF_MASK) {
2960 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2961 /* Create the network device */
2963 hso_dev = hso_create_net_device(interface);
2970 if (hso_get_mux_ports(interface, &port_mask))
2971 /* TODO: de-allocate everything */
2974 shared_int = hso_create_shared_int(interface);
2978 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2979 if (port_mask & i) {
2980 hso_dev = hso_create_mux_serial_device(
2981 interface, i, shared_int);
2992 /* It's a regular bulk interface */
2993 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2995 hso_dev = hso_create_net_device(interface);
2998 hso_create_bulk_serial_device(interface, port_spec);
3006 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
3008 /* save our data pointer in this device */
3009 usb_set_intfdata(interface, hso_dev);
3014 hso_free_interface(interface);
3018 /* device removed, cleaning up */
3019 static void hso_disconnect(struct usb_interface *interface)
3021 hso_free_interface(interface);
3023 /* remove reference of our private data */
3024 usb_set_intfdata(interface, NULL);
3026 usb_driver_release_interface(&hso_driver, interface);
3029 static void async_get_intf(struct work_struct *data)
3031 struct hso_device *hso_dev =
3032 container_of(data, struct hso_device, async_get_intf);
3033 usb_autopm_get_interface(hso_dev->interface);
3036 static void async_put_intf(struct work_struct *data)
3038 struct hso_device *hso_dev =
3039 container_of(data, struct hso_device, async_put_intf);
3040 usb_autopm_put_interface(hso_dev->interface);
3043 static int hso_get_activity(struct hso_device *hso_dev)
3045 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3046 if (!hso_dev->is_active) {
3047 hso_dev->is_active = 1;
3048 schedule_work(&hso_dev->async_get_intf);
3052 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3055 usb_mark_last_busy(hso_dev->usb);
3060 static int hso_put_activity(struct hso_device *hso_dev)
3062 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3063 if (hso_dev->is_active) {
3064 hso_dev->is_active = 0;
3065 schedule_work(&hso_dev->async_put_intf);
3069 hso_dev->is_active = 0;
3073 /* called by kernel when we need to suspend device */
3074 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3078 /* Stop all serial ports */
3079 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3080 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3081 result = hso_stop_serial_device(serial_table[i]);
3087 /* Stop all network ports */
3088 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3089 if (network_table[i] &&
3090 (network_table[i]->interface == iface)) {
3091 result = hso_stop_net_device(network_table[i]);
3101 /* called by kernel when we need to resume device */
3102 static int hso_resume(struct usb_interface *iface)
3105 struct hso_net *hso_net;
3107 /* Start all serial ports */
3108 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3109 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3110 if (dev2ser(serial_table[i])->open_count) {
3112 hso_start_serial_device(serial_table[i], GFP_NOIO);
3113 hso_kick_transmit(dev2ser(serial_table[i]));
3120 /* Start all network ports */
3121 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3122 if (network_table[i] &&
3123 (network_table[i]->interface == iface)) {
3124 hso_net = dev2net(network_table[i]);
3125 if (hso_net->flags & IFF_UP) {
3126 /* First transmit any lingering data,
3127 then restart the device. */
3128 if (hso_net->skb_tx_buf) {
3129 dev_dbg(&iface->dev,
3131 " lingering data\n");
3132 hso_net_start_xmit(hso_net->skb_tx_buf,
3134 hso_net->skb_tx_buf = NULL;
3136 result = hso_start_net_device(network_table[i]);
3147 static void hso_serial_ref_free(struct kref *ref)
3149 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3151 hso_free_serial_device(hso_dev);
3154 static void hso_free_interface(struct usb_interface *interface)
3156 struct hso_serial *hso_dev;
3158 struct mutex *hso_mutex = NULL;
3161 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3163 && (serial_table[i]->interface == interface)) {
3164 hso_dev = dev2ser(serial_table[i]);
3166 tty_hangup(hso_dev->tty);
3167 hso_mutex = &hso_dev->parent->mutex->mutex;
3168 mutex_lock(hso_mutex);
3169 hso_dev->parent->usb_gone = 1;
3170 refcnt = kref_put(&serial_table[i]->ref,
3171 hso_serial_ref_free);
3172 mutex_unlock(hso_mutex);
3176 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3177 if (network_table[i]
3178 && (network_table[i]->interface == interface)) {
3179 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3180 /* hso_stop_net_device doesn't stop the net queue since
3181 * traffic needs to start it again when suspended */
3182 netif_stop_queue(dev2net(network_table[i])->net);
3183 hso_stop_net_device(network_table[i]);
3184 cancel_work_sync(&network_table[i]->async_put_intf);
3185 cancel_work_sync(&network_table[i]->async_get_intf);
3187 rfkill_unregister(rfk);
3188 hso_free_net_device(network_table[i]);
3192 hso_free_mutex(container_of(hso_mutex,
3193 struct hso_mutex_t, mutex));
3196 /* Helper functions */
3198 /* Get the endpoint ! */
3199 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3203 struct usb_host_interface *iface = intf->cur_altsetting;
3204 struct usb_endpoint_descriptor *endp;
3206 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3207 endp = &iface->endpoint[i].desc;
3208 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3209 ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
3216 /* Get the byte that describes which ports are enabled */
3217 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3220 struct usb_host_interface *iface = intf->cur_altsetting;
3222 if (iface->extralen == 3) {
3223 *ports = iface->extra[2];
3227 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3228 if (iface->endpoint[i].extralen == 3) {
3229 *ports = iface->endpoint[i].extra[2];
3237 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3238 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3239 struct usb_device *usb, gfp_t gfp)
3243 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3245 shared_int->intr_endp->bEndpointAddress & 0x7F),
3246 shared_int->shared_intr_buf,
3247 shared_int->intr_endp->wMaxPacketSize,
3248 intr_callback, shared_int,
3249 shared_int->intr_endp->bInterval);
3251 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3253 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
3259 /* operations setup of the serial interface */
3260 static const struct tty_operations hso_serial_ops = {
3261 .open = hso_serial_open,
3262 .close = hso_serial_close,
3263 .write = hso_serial_write,
3264 .write_room = hso_serial_write_room,
3265 .ioctl = hso_serial_ioctl,
3266 .set_termios = hso_serial_set_termios,
3267 .chars_in_buffer = hso_serial_chars_in_buffer,
3268 .tiocmget = hso_serial_tiocmget,
3269 .tiocmset = hso_serial_tiocmset,
3270 .unthrottle = hso_unthrottle
3273 static struct usb_driver hso_driver = {
3274 .name = driver_name,
3276 .disconnect = hso_disconnect,
3277 .id_table = hso_ids,
3278 .suspend = hso_suspend,
3279 .resume = hso_resume,
3280 .supports_autosuspend = 1,
3283 static int __init hso_init(void)
3288 /* put it in the log */
3289 printk(KERN_INFO "hso: %s\n", version);
3291 /* Initialise the serial table semaphore and table */
3292 spin_lock_init(&serial_table_lock);
3293 spin_lock_init(&hso_mutex_lock);
3294 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3295 serial_table[i] = NULL;
3297 /* allocate our driver using the proper amount of supported minors */
3298 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3302 /* fill in all needed values */
3303 tty_drv->magic = TTY_DRIVER_MAGIC;
3304 tty_drv->owner = THIS_MODULE;
3305 tty_drv->driver_name = driver_name;
3306 tty_drv->name = tty_filename;
3308 /* if major number is provided as parameter, use that one */
3310 tty_drv->major = tty_major;
3312 tty_drv->minor_start = 0;
3313 tty_drv->num = HSO_SERIAL_TTY_MINORS;
3314 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3315 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3316 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3317 tty_drv->init_termios = tty_std_termios;
3318 tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3319 tty_drv->termios = hso_serial_termios;
3320 tty_drv->termios_locked = hso_serial_termios_locked;
3321 tty_set_operations(tty_drv, &hso_serial_ops);
3323 /* register the tty driver */
3324 result = tty_register_driver(tty_drv);
3326 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3331 /* register this module as an usb driver */
3332 result = usb_register(&hso_driver);
3334 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3336 /* cleanup serial interface */
3337 tty_unregister_driver(tty_drv);
3345 static void __exit hso_exit(void)
3347 printk(KERN_INFO "hso: unloaded\n");
3349 tty_unregister_driver(tty_drv);
3350 /* deregister the usb driver */
3351 usb_deregister(&hso_driver);
3354 /* Module definitions */
3355 module_init(hso_init);
3356 module_exit(hso_exit);
3358 MODULE_AUTHOR(MOD_AUTHOR);
3359 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3360 MODULE_LICENSE(MOD_LICENSE);
3361 MODULE_INFO(Version, DRIVER_VERSION);
3363 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3364 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3365 module_param(debug, int, S_IRUGO | S_IWUSR);
3367 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3368 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3369 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3371 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3372 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3373 module_param(disable_net, int, S_IRUGO | S_IWUSR);