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;
276 struct hso_serial *dev_serial;
277 struct hso_net *dev_net;
284 struct work_struct async_get_intf;
285 struct work_struct async_put_intf;
287 struct usb_device *usb;
288 struct usb_interface *interface;
295 /* Type of interface */
296 #define HSO_INTF_MASK 0xFF00
297 #define HSO_INTF_MUX 0x0100
298 #define HSO_INTF_BULK 0x0200
301 #define HSO_PORT_MASK 0xFF
302 #define HSO_PORT_NO_PORT 0x0
303 #define HSO_PORT_CONTROL 0x1
304 #define HSO_PORT_APP 0x2
305 #define HSO_PORT_GPS 0x3
306 #define HSO_PORT_PCSC 0x4
307 #define HSO_PORT_APP2 0x5
308 #define HSO_PORT_GPS_CONTROL 0x6
309 #define HSO_PORT_MSD 0x7
310 #define HSO_PORT_VOICE 0x8
311 #define HSO_PORT_DIAG2 0x9
312 #define HSO_PORT_DIAG 0x10
313 #define HSO_PORT_MODEM 0x11
314 #define HSO_PORT_NETWORK 0x12
316 /* Additional device info */
317 #define HSO_INFO_MASK 0xFF000000
318 #define HSO_INFO_CRC_BUG 0x01000000
320 /*****************************************************************************/
322 /*****************************************************************************/
323 /* Serial driver functions */
324 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
325 unsigned int set, unsigned int clear);
326 static void ctrl_callback(struct urb *urb);
327 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
328 static void hso_kick_transmit(struct hso_serial *serial);
329 /* Helper functions */
330 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
331 struct usb_device *usb, gfp_t gfp);
332 static void log_usb_status(int status, const char *function);
333 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
335 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
336 static void hso_free_interface(struct usb_interface *intf);
337 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
338 static int hso_stop_serial_device(struct hso_device *hso_dev);
339 static int hso_start_net_device(struct hso_device *hso_dev);
340 static void hso_free_shared_int(struct hso_shared_int *shared_int);
341 static int hso_stop_net_device(struct hso_device *hso_dev);
342 static void hso_serial_ref_free(struct kref *ref);
343 static void hso_std_serial_read_bulk_callback(struct urb *urb);
344 static int hso_mux_serial_read(struct hso_serial *serial);
345 static void async_get_intf(struct work_struct *data);
346 static void async_put_intf(struct work_struct *data);
347 static int hso_put_activity(struct hso_device *hso_dev);
348 static int hso_get_activity(struct hso_device *hso_dev);
349 static void tiocmget_intr_callback(struct urb *urb);
350 /*****************************************************************************/
351 /* Helping functions */
352 /*****************************************************************************/
356 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
358 return hso_dev->port_data.dev_net;
361 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
363 return hso_dev->port_data.dev_serial;
366 /* Debugging functions */
368 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
371 static char name[255];
373 sprintf(name, "hso[%d:%s]", line_count, func_name);
374 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
377 #define DUMP(buf_, len_) \
378 dbg_dump(__LINE__, __func__, buf_, len_)
380 #define DUMP1(buf_, len_) \
386 #define DUMP(buf_, len_)
387 #define DUMP1(buf_, len_)
390 /* module parameters */
392 static int tty_major;
393 static int disable_net;
396 static const char driver_name[] = "hso";
397 static const char tty_filename[] = "ttyHS";
398 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
399 /* the usb driver itself (registered in hso_init) */
400 static struct usb_driver hso_driver;
401 /* serial structures */
402 static struct tty_driver *tty_drv;
403 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
404 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
405 static spinlock_t serial_table_lock;
407 static const s32 default_port_spec[] = {
408 HSO_INTF_MUX | HSO_PORT_NETWORK,
409 HSO_INTF_BULK | HSO_PORT_DIAG,
410 HSO_INTF_BULK | HSO_PORT_MODEM,
414 static const s32 icon321_port_spec[] = {
415 HSO_INTF_MUX | HSO_PORT_NETWORK,
416 HSO_INTF_BULK | HSO_PORT_DIAG2,
417 HSO_INTF_BULK | HSO_PORT_MODEM,
418 HSO_INTF_BULK | HSO_PORT_DIAG,
422 #define default_port_device(vendor, product) \
423 USB_DEVICE(vendor, product), \
424 .driver_info = (kernel_ulong_t)default_port_spec
426 #define icon321_port_device(vendor, product) \
427 USB_DEVICE(vendor, product), \
428 .driver_info = (kernel_ulong_t)icon321_port_spec
430 /* list of devices we support */
431 static const struct usb_device_id hso_ids[] = {
432 {default_port_device(0x0af0, 0x6711)},
433 {default_port_device(0x0af0, 0x6731)},
434 {default_port_device(0x0af0, 0x6751)},
435 {default_port_device(0x0af0, 0x6771)},
436 {default_port_device(0x0af0, 0x6791)},
437 {default_port_device(0x0af0, 0x6811)},
438 {default_port_device(0x0af0, 0x6911)},
439 {default_port_device(0x0af0, 0x6951)},
440 {default_port_device(0x0af0, 0x6971)},
441 {default_port_device(0x0af0, 0x7011)},
442 {default_port_device(0x0af0, 0x7031)},
443 {default_port_device(0x0af0, 0x7051)},
444 {default_port_device(0x0af0, 0x7071)},
445 {default_port_device(0x0af0, 0x7111)},
446 {default_port_device(0x0af0, 0x7211)},
447 {default_port_device(0x0af0, 0x7251)},
448 {default_port_device(0x0af0, 0x7271)},
449 {default_port_device(0x0af0, 0x7311)},
450 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
451 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
452 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
453 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
454 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
455 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
456 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
457 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
458 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
459 {USB_DEVICE(0x0af0, 0x7701)},
460 {USB_DEVICE(0x0af0, 0x7801)},
461 {USB_DEVICE(0x0af0, 0x7901)},
462 {USB_DEVICE(0x0af0, 0x7361)},
463 {icon321_port_device(0x0af0, 0xd051)},
466 MODULE_DEVICE_TABLE(usb, hso_ids);
468 /* Sysfs attribute */
469 static ssize_t hso_sysfs_show_porttype(struct device *dev,
470 struct device_attribute *attr,
473 struct hso_device *hso_dev = dev->driver_data;
479 switch (hso_dev->port_spec & HSO_PORT_MASK) {
480 case HSO_PORT_CONTROL:
481 port_name = "Control";
484 port_name = "Application";
487 port_name = "Application2";
492 case HSO_PORT_GPS_CONTROL:
493 port_name = "GPS Control";
499 port_name = "Diagnostic";
502 port_name = "Diagnostic2";
507 case HSO_PORT_NETWORK:
508 port_name = "Network";
511 port_name = "Unknown";
515 return sprintf(buf, "%s\n", port_name);
517 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
519 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
523 for (idx = 0; idx < serial->num_rx_urbs; idx++)
524 if (serial->rx_urb[idx] == urb)
526 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
530 /* converts mux value to a port spec value */
531 static u32 hso_mux_to_port(int mux)
537 result = HSO_PORT_CONTROL;
540 result = HSO_PORT_APP;
543 result = HSO_PORT_PCSC;
546 result = HSO_PORT_GPS;
549 result = HSO_PORT_APP2;
552 result = HSO_PORT_NO_PORT;
557 /* converts port spec value to a mux value */
558 static u32 hso_port_to_mux(int port)
562 switch (port & HSO_PORT_MASK) {
563 case HSO_PORT_CONTROL:
584 static struct hso_serial *get_serial_by_shared_int_and_type(
585 struct hso_shared_int *shared_int,
590 port = hso_mux_to_port(mux);
592 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
594 && (dev2ser(serial_table[i])->shared_int == shared_int)
595 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
596 return dev2ser(serial_table[i]);
603 static struct hso_serial *get_serial_by_index(unsigned index)
605 struct hso_serial *serial = NULL;
608 spin_lock_irqsave(&serial_table_lock, flags);
609 if (serial_table[index])
610 serial = dev2ser(serial_table[index]);
611 spin_unlock_irqrestore(&serial_table_lock, flags);
616 static int get_free_serial_index(void)
621 spin_lock_irqsave(&serial_table_lock, flags);
622 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
623 if (serial_table[index] == NULL) {
624 spin_unlock_irqrestore(&serial_table_lock, flags);
628 spin_unlock_irqrestore(&serial_table_lock, flags);
630 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
634 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
638 spin_lock_irqsave(&serial_table_lock, flags);
640 serial_table[index] = serial->parent;
642 serial_table[index] = NULL;
643 spin_unlock_irqrestore(&serial_table_lock, flags);
646 /* log a meaningful explanation of an USB status */
647 static void log_usb_status(int status, const char *function)
653 explanation = "no device";
656 explanation = "endpoint not enabled";
659 explanation = "endpoint stalled";
662 explanation = "not enough bandwidth";
665 explanation = "device disabled";
668 explanation = "device suspended";
674 explanation = "internal error";
677 explanation = "unknown status";
680 D1("%s: received USB status - %s (%d)", function, explanation, status);
683 /* Network interface functions */
685 /* called when net interface is brought up by ifconfig */
686 static int hso_net_open(struct net_device *net)
688 struct hso_net *odev = netdev_priv(net);
689 unsigned long flags = 0;
692 dev_err(&net->dev, "No net device !\n");
696 odev->skb_tx_buf = NULL;
698 /* setup environment */
699 spin_lock_irqsave(&odev->net_lock, flags);
700 odev->rx_parse_state = WAIT_IP;
701 odev->rx_buf_size = 0;
702 odev->rx_buf_missing = sizeof(struct iphdr);
703 spin_unlock_irqrestore(&odev->net_lock, flags);
705 /* We are up and running. */
706 set_bit(HSO_NET_RUNNING, &odev->flags);
707 hso_start_net_device(odev->parent);
709 /* Tell the kernel we are ready to start receiving from it */
710 netif_start_queue(net);
715 /* called when interface is brought down by ifconfig */
716 static int hso_net_close(struct net_device *net)
718 struct hso_net *odev = netdev_priv(net);
720 /* we don't need the queue anymore */
721 netif_stop_queue(net);
722 /* no longer running */
723 clear_bit(HSO_NET_RUNNING, &odev->flags);
725 hso_stop_net_device(odev->parent);
731 /* USB tells is xmit done, we should start the netqueue again */
732 static void write_bulk_callback(struct urb *urb)
734 struct hso_net *odev = urb->context;
735 int status = urb->status;
738 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
739 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
743 /* Do we still have a valid kernel network device? */
744 if (!netif_device_present(odev->net)) {
745 dev_err(&urb->dev->dev, "%s: net device not present\n",
750 /* log status, but don't act on it, we don't need to resubmit anything
753 log_usb_status(status, __func__);
755 hso_put_activity(odev->parent);
757 /* Tell the network interface we are ready for another frame */
758 netif_wake_queue(odev->net);
761 /* called by kernel when we need to transmit a packet */
762 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
764 struct hso_net *odev = netdev_priv(net);
767 /* Tell the kernel, "No more frames 'til we are done with this one." */
768 netif_stop_queue(net);
769 if (hso_get_activity(odev->parent) == -EAGAIN) {
770 odev->skb_tx_buf = skb;
775 DUMP1(skb->data, skb->len);
776 /* Copy it from kernel memory to OUR memory */
777 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
778 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
780 /* Fill in the URB for shipping it out. */
781 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
783 usb_sndbulkpipe(odev->parent->usb,
785 bEndpointAddress & 0x7F),
786 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
789 /* Deal with the Zero Length packet problem, I hope */
790 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
792 /* Send the URB on its merry way. */
793 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
795 dev_warn(&odev->parent->interface->dev,
796 "failed mux_bulk_tx_urb %d", result);
797 net->stats.tx_errors++;
798 netif_start_queue(net);
800 net->stats.tx_packets++;
801 net->stats.tx_bytes += skb->len;
802 /* And tell the kernel when the last transmit started. */
803 net->trans_start = jiffies;
810 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
812 struct hso_net *odev = netdev_priv(net);
814 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
815 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
816 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
819 static struct ethtool_ops ops = {
820 .get_drvinfo = hso_get_drvinfo,
821 .get_link = ethtool_op_get_link
824 /* called when a packet did not ack after watchdogtimeout */
825 static void hso_net_tx_timeout(struct net_device *net)
827 struct hso_net *odev = netdev_priv(net);
832 /* Tell syslog we are hosed. */
833 dev_warn(&net->dev, "Tx timed out.\n");
835 /* Tear the waiting frame off the list */
836 if (odev->mux_bulk_tx_urb
837 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
838 usb_unlink_urb(odev->mux_bulk_tx_urb);
840 /* Update statistics */
841 net->stats.tx_errors++;
844 /* make a real packet from the received USB buffer */
845 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
846 unsigned int count, unsigned char is_eop)
848 unsigned short temp_bytes;
849 unsigned short buffer_offset = 0;
850 unsigned short frame_len;
851 unsigned char *tmp_rx_buf;
854 D1("Rx %d bytes", count);
855 DUMP(ip_pkt, min(128, (int)count));
858 switch (odev->rx_parse_state) {
860 /* waiting for IP header. */
861 /* wanted bytes - size of ip header */
864 odev->rx_buf_missing) ? count : odev->
867 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
868 odev->rx_buf_size, ip_pkt + buffer_offset,
871 odev->rx_buf_size += temp_bytes;
872 buffer_offset += temp_bytes;
873 odev->rx_buf_missing -= temp_bytes;
876 if (!odev->rx_buf_missing) {
877 /* header is complete allocate an sk_buffer and
878 * continue to WAIT_DATA */
879 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
881 if ((frame_len > DEFAULT_MRU) ||
882 (frame_len < sizeof(struct iphdr))) {
883 dev_err(&odev->net->dev,
884 "Invalid frame (%d) length\n",
886 odev->rx_parse_state = WAIT_SYNC;
889 /* Allocate an sk_buff */
890 odev->skb_rx_buf = dev_alloc_skb(frame_len);
891 if (!odev->skb_rx_buf) {
892 /* We got no receive buffer. */
893 D1("could not allocate memory");
894 odev->rx_parse_state = WAIT_SYNC;
897 /* Here's where it came from */
898 odev->skb_rx_buf->dev = odev->net;
900 /* Copy what we got so far. make room for iphdr
903 skb_put(odev->skb_rx_buf,
904 sizeof(struct iphdr));
905 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
906 sizeof(struct iphdr));
909 odev->rx_buf_size = sizeof(struct iphdr);
911 /* Filip actually use .tot_len */
912 odev->rx_buf_missing =
913 frame_len - sizeof(struct iphdr);
914 odev->rx_parse_state = WAIT_DATA;
919 temp_bytes = (count < odev->rx_buf_missing)
920 ? count : odev->rx_buf_missing;
922 /* Copy the rest of the bytes that are left in the
923 * buffer into the waiting sk_buf. */
924 /* Make room for temp_bytes after tail. */
925 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
926 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
928 odev->rx_buf_missing -= temp_bytes;
930 buffer_offset += temp_bytes;
931 odev->rx_buf_size += temp_bytes;
932 if (!odev->rx_buf_missing) {
933 /* Packet is complete. Inject into stack. */
934 /* We have IP packet here */
935 odev->skb_rx_buf->protocol =
936 __constant_htons(ETH_P_IP);
938 odev->skb_rx_buf->ip_summed =
939 CHECKSUM_UNNECESSARY;
941 skb_reset_mac_header(odev->skb_rx_buf);
943 /* Ship it off to the kernel */
944 netif_rx(odev->skb_rx_buf);
945 /* No longer our buffer. */
946 odev->skb_rx_buf = NULL;
948 /* update out statistics */
949 odev->net->stats.rx_packets++;
951 odev->net->stats.rx_bytes += odev->rx_buf_size;
953 odev->rx_buf_size = 0;
954 odev->rx_buf_missing = sizeof(struct iphdr);
955 odev->rx_parse_state = WAIT_IP;
970 /* Recovery mechanism for WAIT_SYNC state. */
972 if (odev->rx_parse_state == WAIT_SYNC) {
973 odev->rx_parse_state = WAIT_IP;
974 odev->rx_buf_size = 0;
975 odev->rx_buf_missing = sizeof(struct iphdr);
980 /* Moving data from usb to kernel (in interrupt state) */
981 static void read_bulk_callback(struct urb *urb)
983 struct hso_net *odev = urb->context;
984 struct net_device *net;
986 int status = urb->status;
988 /* is al ok? (Filip: Who's Al ?) */
990 log_usb_status(status, __func__);
995 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
996 D1("BULK IN callback but driver is not active!");
999 usb_mark_last_busy(urb->dev);
1003 if (!netif_device_present(net)) {
1004 /* Somebody killed our network interface... */
1008 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
1010 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1011 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
1012 if (((rest == 5) || (rest == 6))
1013 && !memcmp(((u8 *) urb->transfer_buffer) +
1014 urb->actual_length - 4, crc_check, 4)) {
1015 urb->actual_length -= 4;
1019 /* do we even have a packet? */
1020 if (urb->actual_length) {
1021 /* Handle the IP stream, add header and push it onto network
1022 * stack if the packet is complete. */
1023 spin_lock(&odev->net_lock);
1024 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1025 (urb->transfer_buffer_length >
1026 urb->actual_length) ? 1 : 0);
1027 spin_unlock(&odev->net_lock);
1030 /* We are done with this URB, resubmit it. Prep the USB to wait for
1031 * another frame. Reuse same as received. */
1032 usb_fill_bulk_urb(urb,
1034 usb_rcvbulkpipe(odev->parent->usb,
1036 bEndpointAddress & 0x7F),
1037 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1038 read_bulk_callback, odev);
1040 /* Give this to the USB subsystem so it can tell us when more data
1042 result = usb_submit_urb(urb, GFP_ATOMIC);
1044 dev_warn(&odev->parent->interface->dev,
1045 "%s failed submit mux_bulk_rx_urb %d", __func__,
1049 /* Serial driver functions */
1051 static void hso_init_termios(struct ktermios *termios)
1054 * The default requirements for this device are:
1057 ~(IGNBRK /* disable ignore break */
1058 | BRKINT /* disable break causes interrupt */
1059 | PARMRK /* disable mark parity errors */
1060 | ISTRIP /* disable clear high bit of input characters */
1061 | INLCR /* disable translate NL to CR */
1062 | IGNCR /* disable ignore CR */
1063 | ICRNL /* disable translate CR to NL */
1064 | IXON); /* disable enable XON/XOFF flow control */
1066 /* disable postprocess output characters */
1067 termios->c_oflag &= ~OPOST;
1070 ~(ECHO /* disable echo input characters */
1071 | ECHONL /* disable echo new line */
1072 | ICANON /* disable erase, kill, werase, and rprnt
1073 special characters */
1074 | ISIG /* disable interrupt, quit, and suspend special
1076 | IEXTEN); /* disable non-POSIX special characters */
1079 ~(CSIZE /* no size */
1080 | PARENB /* disable parity bit */
1081 | CBAUD /* clear current baud rate */
1082 | CBAUDEX); /* clear current buad rate */
1084 termios->c_cflag |= CS8; /* character size 8 bits */
1086 /* baud rate 115200 */
1087 tty_termios_encode_baud_rate(termios, 115200, 115200);
1090 static void _hso_serial_set_termios(struct tty_struct *tty,
1091 struct ktermios *old)
1093 struct hso_serial *serial = get_serial_by_tty(tty);
1094 struct ktermios *termios;
1097 printk(KERN_ERR "%s: no tty structures", __func__);
1101 D4("port %d", serial->minor);
1104 * Fix up unsupported bits
1106 termios = tty->termios;
1107 termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1110 ~(CSIZE /* no size */
1111 | PARENB /* disable parity bit */
1112 | CBAUD /* clear current baud rate */
1113 | CBAUDEX); /* clear current buad rate */
1115 termios->c_cflag |= CS8; /* character size 8 bits */
1117 /* baud rate 115200 */
1118 tty_encode_baud_rate(tty, 115200, 115200);
1121 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1124 #ifdef CONFIG_HSO_AUTOPM
1125 usb_mark_last_busy(urb->dev);
1127 /* We are done with this URB, resubmit it. Prep the USB to wait for
1129 usb_fill_bulk_urb(urb, serial->parent->usb,
1130 usb_rcvbulkpipe(serial->parent->usb,
1132 bEndpointAddress & 0x7F),
1133 urb->transfer_buffer, serial->rx_data_length,
1134 hso_std_serial_read_bulk_callback, serial);
1135 /* Give this to the USB subsystem so it can tell us when more data
1137 result = usb_submit_urb(urb, GFP_ATOMIC);
1139 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1147 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1150 struct urb *curr_urb;
1152 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1153 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1154 count = put_rxbuf_data(curr_urb, serial);
1158 serial->curr_rx_urb_idx++;
1159 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1160 serial->curr_rx_urb_idx = 0;
1161 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1166 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1171 urb = serial->rx_urb[0];
1172 if (serial->open_count > 0) {
1173 count = put_rxbuf_data(urb, serial);
1177 /* Re issue a read as long as we receive data. */
1179 if (count == 0 && ((urb->actual_length != 0) ||
1180 (serial->rx_state == RX_PENDING))) {
1181 serial->rx_state = RX_SENT;
1182 hso_mux_serial_read(serial);
1184 serial->rx_state = RX_IDLE;
1188 /* read callback for Diag and CS port */
1189 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1191 struct hso_serial *serial = urb->context;
1192 int status = urb->status;
1196 D1("serial == NULL");
1198 } else if (status) {
1199 log_usb_status(status, __func__);
1203 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1204 D1("Actual length = %d\n", urb->actual_length);
1205 DUMP1(urb->transfer_buffer, urb->actual_length);
1207 /* Anyone listening? */
1208 if (serial->open_count == 0)
1212 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1214 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1216 urb->actual_length %
1217 serial->in_endp->wMaxPacketSize;
1218 if (((rest == 5) || (rest == 6))
1219 && !memcmp(((u8 *) urb->transfer_buffer) +
1220 urb->actual_length - 4, crc_check, 4)) {
1221 urb->actual_length -= 4;
1224 /* Valid data, handle RX data */
1225 spin_lock(&serial->serial_lock);
1226 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1227 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1228 spin_unlock(&serial->serial_lock);
1229 } else if (status == -ENOENT || status == -ECONNRESET) {
1230 /* Unlinked - check for throttled port. */
1231 D2("Port %d, successfully unlinked urb", serial->minor);
1232 spin_lock(&serial->serial_lock);
1233 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1234 hso_resubmit_rx_bulk_urb(serial, urb);
1235 spin_unlock(&serial->serial_lock);
1237 D2("Port %d, status = %d for read urb", serial->minor, status);
1243 * This needs to be a tasklet otherwise we will
1244 * end up recursively calling this function.
1246 void hso_unthrottle_tasklet(struct hso_serial *serial)
1248 unsigned long flags;
1250 spin_lock_irqsave(&serial->serial_lock, flags);
1251 if ((serial->parent->port_spec & HSO_INTF_MUX))
1252 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1254 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1255 spin_unlock_irqrestore(&serial->serial_lock, flags);
1258 static void hso_unthrottle(struct tty_struct *tty)
1260 struct hso_serial *serial = get_serial_by_tty(tty);
1262 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1265 void hso_unthrottle_workfunc(struct work_struct *work)
1267 struct hso_serial *serial =
1268 container_of(work, struct hso_serial,
1269 retry_unthrottle_workqueue);
1270 hso_unthrottle_tasklet(serial);
1273 /* open the requested serial port */
1274 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1276 struct hso_serial *serial = get_serial_by_index(tty->index);
1280 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1282 tty->driver_data = NULL;
1283 D1("Failed to open port");
1287 mutex_lock(&serial->parent->mutex);
1288 result = usb_autopm_get_interface(serial->parent->interface);
1292 D1("Opening %d", serial->minor);
1293 kref_get(&serial->parent->ref);
1296 spin_lock_irq(&serial->serial_lock);
1297 tty->driver_data = serial;
1298 serial->tty = tty_kref_get(tty);
1299 spin_unlock_irq(&serial->serial_lock);
1301 /* check for port already opened, if not set the termios */
1302 serial->open_count++;
1303 if (serial->open_count == 1) {
1304 tty->low_latency = 1;
1305 serial->rx_state = RX_IDLE;
1306 /* Force default termio settings */
1307 _hso_serial_set_termios(tty, NULL);
1308 tasklet_init(&serial->unthrottle_tasklet,
1309 (void (*)(unsigned long))hso_unthrottle_tasklet,
1310 (unsigned long)serial);
1311 INIT_WORK(&serial->retry_unthrottle_workqueue,
1312 hso_unthrottle_workfunc);
1313 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1315 hso_stop_serial_device(serial->parent);
1316 serial->open_count--;
1317 kref_put(&serial->parent->ref, hso_serial_ref_free);
1320 D1("Port was already open");
1323 usb_autopm_put_interface(serial->parent->interface);
1327 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1329 mutex_unlock(&serial->parent->mutex);
1333 /* close the requested serial port */
1334 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1336 struct hso_serial *serial = tty->driver_data;
1339 D1("Closing serial port");
1341 /* Open failed, no close cleanup required */
1345 mutex_lock(&serial->parent->mutex);
1346 usb_gone = serial->parent->usb_gone;
1349 usb_autopm_get_interface(serial->parent->interface);
1351 /* reset the rts and dtr */
1352 /* do the actual close */
1353 serial->open_count--;
1354 kref_put(&serial->parent->ref, hso_serial_ref_free);
1355 if (serial->open_count <= 0) {
1356 serial->open_count = 0;
1357 spin_lock_irq(&serial->serial_lock);
1358 if (serial->tty == tty) {
1359 serial->tty->driver_data = NULL;
1363 spin_unlock_irq(&serial->serial_lock);
1365 hso_stop_serial_device(serial->parent);
1366 tasklet_kill(&serial->unthrottle_tasklet);
1367 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1371 usb_autopm_put_interface(serial->parent->interface);
1373 mutex_unlock(&serial->parent->mutex);
1376 /* close the requested serial port */
1377 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1380 struct hso_serial *serial = get_serial_by_tty(tty);
1381 int space, tx_bytes;
1382 unsigned long flags;
1385 if (serial == NULL) {
1386 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1390 spin_lock_irqsave(&serial->serial_lock, flags);
1392 space = serial->tx_data_length - serial->tx_buffer_count;
1393 tx_bytes = (count < space) ? count : space;
1398 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1399 serial->tx_buffer_count += tx_bytes;
1402 spin_unlock_irqrestore(&serial->serial_lock, flags);
1404 hso_kick_transmit(serial);
1409 /* how much room is there for writing */
1410 static int hso_serial_write_room(struct tty_struct *tty)
1412 struct hso_serial *serial = get_serial_by_tty(tty);
1414 unsigned long flags;
1416 spin_lock_irqsave(&serial->serial_lock, flags);
1417 room = serial->tx_data_length - serial->tx_buffer_count;
1418 spin_unlock_irqrestore(&serial->serial_lock, flags);
1420 /* return free room */
1424 /* setup the term */
1425 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1427 struct hso_serial *serial = get_serial_by_tty(tty);
1428 unsigned long flags;
1431 D5("Termios called with: cflags new[%d] - old[%d]",
1432 tty->termios->c_cflag, old->c_cflag);
1434 /* the actual setup */
1435 spin_lock_irqsave(&serial->serial_lock, flags);
1436 if (serial->open_count)
1437 _hso_serial_set_termios(tty, old);
1440 spin_unlock_irqrestore(&serial->serial_lock, flags);
1446 /* how many characters in the buffer */
1447 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1449 struct hso_serial *serial = get_serial_by_tty(tty);
1451 unsigned long flags;
1457 spin_lock_irqsave(&serial->serial_lock, flags);
1458 chars = serial->tx_buffer_count;
1459 spin_unlock_irqrestore(&serial->serial_lock, flags);
1463 int tiocmget_submit_urb(struct hso_serial *serial,
1464 struct hso_tiocmget *tiocmget,
1465 struct usb_device *usb)
1469 if (serial->parent->usb_gone)
1471 usb_fill_int_urb(tiocmget->urb, usb,
1474 bEndpointAddress & 0x7F),
1475 &tiocmget->serial_state_notification,
1476 sizeof(struct hso_serial_state_notification),
1477 tiocmget_intr_callback, serial,
1478 tiocmget->endp->bInterval);
1479 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1481 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1488 static void tiocmget_intr_callback(struct urb *urb)
1490 struct hso_serial *serial = urb->context;
1491 struct hso_tiocmget *tiocmget;
1492 int status = urb->status;
1493 u16 UART_state_bitmap, prev_UART_state_bitmap;
1494 struct uart_icount *icount;
1495 struct hso_serial_state_notification *serial_state_notification;
1496 struct usb_device *usb;
1502 log_usb_status(status, __func__);
1505 tiocmget = serial->tiocmget;
1508 usb = serial->parent->usb;
1509 serial_state_notification = &tiocmget->serial_state_notification;
1510 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1511 serial_state_notification->bNotification != B_NOTIFICATION ||
1512 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1513 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
1514 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1516 "hso received invalid serial state notification\n");
1517 DUMP(serial_state_notification,
1518 sizeof(hso_serial_state_notifation))
1521 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1523 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1524 icount = &tiocmget->icount;
1525 spin_lock(&serial->serial_lock);
1526 if ((UART_state_bitmap & B_OVERRUN) !=
1527 (prev_UART_state_bitmap & B_OVERRUN))
1529 if ((UART_state_bitmap & B_PARITY) !=
1530 (prev_UART_state_bitmap & B_PARITY))
1532 if ((UART_state_bitmap & B_FRAMING) !=
1533 (prev_UART_state_bitmap & B_FRAMING))
1535 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1536 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1538 if ((UART_state_bitmap & B_BREAK) !=
1539 (prev_UART_state_bitmap & B_BREAK))
1541 if ((UART_state_bitmap & B_TX_CARRIER) !=
1542 (prev_UART_state_bitmap & B_TX_CARRIER))
1544 if ((UART_state_bitmap & B_RX_CARRIER) !=
1545 (prev_UART_state_bitmap & B_RX_CARRIER))
1547 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1548 spin_unlock(&serial->serial_lock);
1549 tiocmget->intr_completed = 1;
1550 wake_up_interruptible(&tiocmget->waitq);
1552 memset(serial_state_notification, 0,
1553 sizeof(struct hso_serial_state_notification));
1554 tiocmget_submit_urb(serial,
1556 serial->parent->usb);
1560 * next few functions largely stolen from drivers/serial/serial_core.c
1562 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1563 * - mask passed in arg for lines of interest
1564 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1565 * Caller should use TIOCGICOUNT to see which one it was
1568 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1570 DECLARE_WAITQUEUE(wait, current);
1571 struct uart_icount cprev, cnow;
1572 struct hso_tiocmget *tiocmget;
1575 tiocmget = serial->tiocmget;
1579 * note the counters on entry
1581 spin_lock_irq(&serial->serial_lock);
1582 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1583 spin_unlock_irq(&serial->serial_lock);
1584 add_wait_queue(&tiocmget->waitq, &wait);
1586 spin_lock_irq(&serial->serial_lock);
1587 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1588 spin_unlock_irq(&serial->serial_lock);
1589 set_current_state(TASK_INTERRUPTIBLE);
1590 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1591 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1592 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1597 /* see if a signal did it */
1598 if (signal_pending(current)) {
1604 current->state = TASK_RUNNING;
1605 remove_wait_queue(&tiocmget->waitq, &wait);
1611 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1612 * Return: write counters to the user passed counter struct
1613 * NB: both 1->0 and 0->1 transitions are counted except for
1614 * RI where only 0->1 is counted.
1616 static int hso_get_count(struct hso_serial *serial,
1617 struct serial_icounter_struct __user *icnt)
1619 struct serial_icounter_struct icount;
1620 struct uart_icount cnow;
1621 struct hso_tiocmget *tiocmget = serial->tiocmget;
1625 spin_lock_irq(&serial->serial_lock);
1626 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1627 spin_unlock_irq(&serial->serial_lock);
1629 icount.cts = cnow.cts;
1630 icount.dsr = cnow.dsr;
1631 icount.rng = cnow.rng;
1632 icount.dcd = cnow.dcd;
1633 icount.rx = cnow.rx;
1634 icount.tx = cnow.tx;
1635 icount.frame = cnow.frame;
1636 icount.overrun = cnow.overrun;
1637 icount.parity = cnow.parity;
1638 icount.brk = cnow.brk;
1639 icount.buf_overrun = cnow.buf_overrun;
1641 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1645 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1648 struct hso_serial *serial = get_serial_by_tty(tty);
1649 struct hso_tiocmget *tiocmget;
1650 u16 UART_state_bitmap;
1654 D1("no tty structures");
1657 spin_lock_irq(&serial->serial_lock);
1658 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
1659 ((serial->dtr_state) ? TIOCM_DTR : 0);
1660 tiocmget = serial->tiocmget;
1663 UART_state_bitmap = le16_to_cpu(
1664 tiocmget->prev_UART_state_bitmap);
1665 if (UART_state_bitmap & B_RING_SIGNAL)
1666 retval |= TIOCM_RNG;
1667 if (UART_state_bitmap & B_RX_CARRIER)
1669 if (UART_state_bitmap & B_TX_CARRIER)
1670 retval |= TIOCM_DSR;
1672 spin_unlock_irq(&serial->serial_lock);
1676 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1677 unsigned int set, unsigned int clear)
1680 unsigned long flags;
1682 struct hso_serial *serial = get_serial_by_tty(tty);
1686 D1("no tty structures");
1689 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1691 spin_lock_irqsave(&serial->serial_lock, flags);
1692 if (set & TIOCM_RTS)
1693 serial->rts_state = 1;
1694 if (set & TIOCM_DTR)
1695 serial->dtr_state = 1;
1697 if (clear & TIOCM_RTS)
1698 serial->rts_state = 0;
1699 if (clear & TIOCM_DTR)
1700 serial->dtr_state = 0;
1702 if (serial->dtr_state)
1704 if (serial->rts_state)
1707 spin_unlock_irqrestore(&serial->serial_lock, flags);
1709 return usb_control_msg(serial->parent->usb,
1710 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1711 0x21, val, if_num, NULL, 0,
1712 USB_CTRL_SET_TIMEOUT);
1715 static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
1716 unsigned int cmd, unsigned long arg)
1718 struct hso_serial *serial = get_serial_by_tty(tty);
1719 void __user *uarg = (void __user *)arg;
1721 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1727 ret = hso_wait_modem_status(serial, arg);
1731 ret = hso_get_count(serial, uarg);
1741 /* starts a transmit */
1742 static void hso_kick_transmit(struct hso_serial *serial)
1745 unsigned long flags;
1748 spin_lock_irqsave(&serial->serial_lock, flags);
1749 if (!serial->tx_buffer_count)
1752 if (serial->tx_urb_used)
1755 /* Wakeup USB interface if necessary */
1756 if (hso_get_activity(serial->parent) == -EAGAIN)
1759 /* Switch pointers around to avoid memcpy */
1760 temp = serial->tx_buffer;
1761 serial->tx_buffer = serial->tx_data;
1762 serial->tx_data = temp;
1763 serial->tx_data_count = serial->tx_buffer_count;
1764 serial->tx_buffer_count = 0;
1766 /* If temp is set, it means we switched buffers */
1767 if (temp && serial->write_data) {
1768 res = serial->write_data(serial);
1770 serial->tx_urb_used = 1;
1773 spin_unlock_irqrestore(&serial->serial_lock, flags);
1776 /* make a request (for reading and writing data to muxed serial port) */
1777 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1778 struct urb *ctrl_urb,
1779 struct usb_ctrlrequest *ctrl_req,
1780 u8 *ctrl_urb_data, u32 size)
1786 if (!serial || !ctrl_urb || !ctrl_req) {
1787 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1792 ctrl_req->wValue = 0;
1793 ctrl_req->wIndex = hso_port_to_mux(port);
1794 ctrl_req->wLength = size;
1796 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1797 /* Reading command */
1798 ctrl_req->bRequestType = USB_DIR_IN |
1799 USB_TYPE_OPTION_VENDOR |
1800 USB_RECIP_INTERFACE;
1801 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1802 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1804 /* Writing command */
1805 ctrl_req->bRequestType = USB_DIR_OUT |
1806 USB_TYPE_OPTION_VENDOR |
1807 USB_RECIP_INTERFACE;
1808 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1809 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1812 D2("%s command (%02x) len: %d, port: %d",
1813 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1814 ctrl_req->bRequestType, ctrl_req->wLength, port);
1817 ctrl_urb->transfer_flags = 0;
1818 usb_fill_control_urb(ctrl_urb,
1819 serial->parent->usb,
1822 ctrl_urb_data, size, ctrl_callback, serial);
1823 /* Send it on merry way */
1824 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1826 dev_err(&ctrl_urb->dev->dev,
1827 "%s failed submit ctrl_urb %d type %d", __func__,
1836 /* called by intr_callback when read occurs */
1837 static int hso_mux_serial_read(struct hso_serial *serial)
1843 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1844 /* make the request */
1846 if (serial->num_rx_urbs != 1) {
1847 dev_err(&serial->parent->interface->dev,
1848 "ERROR: mux'd reads with multiple buffers "
1852 return mux_device_request(serial,
1853 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1854 serial->parent->port_spec & HSO_PORT_MASK,
1856 &serial->ctrl_req_rx,
1857 serial->rx_data[0], serial->rx_data_length);
1860 /* used for muxed serial port callback (muxed serial read) */
1861 static void intr_callback(struct urb *urb)
1863 struct hso_shared_int *shared_int = urb->context;
1864 struct hso_serial *serial;
1865 unsigned char *port_req;
1866 int status = urb->status;
1869 usb_mark_last_busy(urb->dev);
1877 log_usb_status(status, __func__);
1880 D4("\n--- Got intr callback 0x%02X ---", status);
1883 port_req = urb->transfer_buffer;
1884 D4(" port_req = 0x%.2X\n", *port_req);
1885 /* loop over all muxed ports to find the one sending this */
1886 for (i = 0; i < 8; i++) {
1887 /* max 8 channels on MUX */
1888 if (*port_req & (1 << i)) {
1889 serial = get_serial_by_shared_int_and_type(shared_int,
1891 if (serial != NULL) {
1892 D1("Pending read interrupt on port %d\n", i);
1893 spin_lock(&serial->serial_lock);
1894 if (serial->rx_state == RX_IDLE) {
1895 /* Setup and send a ctrl req read on
1897 if (!serial->rx_urb_filled[0]) {
1898 serial->rx_state = RX_SENT;
1899 hso_mux_serial_read(serial);
1901 serial->rx_state = RX_PENDING;
1904 D1("Already pending a read on "
1907 spin_unlock(&serial->serial_lock);
1911 /* Resubmit interrupt urb */
1912 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1915 /* called for writing to muxed serial port */
1916 static int hso_mux_serial_write_data(struct hso_serial *serial)
1921 return mux_device_request(serial,
1922 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1923 serial->parent->port_spec & HSO_PORT_MASK,
1925 &serial->ctrl_req_tx,
1926 serial->tx_data, serial->tx_data_count);
1929 /* write callback for Diag and CS port */
1930 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1932 struct hso_serial *serial = urb->context;
1933 int status = urb->status;
1934 struct tty_struct *tty;
1938 D1("serial == NULL");
1942 spin_lock(&serial->serial_lock);
1943 serial->tx_urb_used = 0;
1944 tty = tty_kref_get(serial->tty);
1945 spin_unlock(&serial->serial_lock);
1947 log_usb_status(status, __func__);
1951 hso_put_activity(serial->parent);
1956 hso_kick_transmit(serial);
1962 /* called for writing diag or CS serial port */
1963 static int hso_std_serial_write_data(struct hso_serial *serial)
1965 int count = serial->tx_data_count;
1968 usb_fill_bulk_urb(serial->tx_urb,
1969 serial->parent->usb,
1970 usb_sndbulkpipe(serial->parent->usb,
1972 bEndpointAddress & 0x7F),
1973 serial->tx_data, serial->tx_data_count,
1974 hso_std_serial_write_bulk_callback, serial);
1976 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1978 dev_warn(&serial->parent->usb->dev,
1979 "Failed to submit urb - res %d\n", result);
1986 /* callback after read or write on muxed serial port */
1987 static void ctrl_callback(struct urb *urb)
1989 struct hso_serial *serial = urb->context;
1990 struct usb_ctrlrequest *req;
1991 int status = urb->status;
1992 struct tty_struct *tty;
1998 spin_lock(&serial->serial_lock);
1999 serial->tx_urb_used = 0;
2000 tty = tty_kref_get(serial->tty);
2001 spin_unlock(&serial->serial_lock);
2003 log_usb_status(status, __func__);
2009 req = (struct usb_ctrlrequest *)(urb->setup_packet);
2010 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2011 D4("Actual length of urb = %d\n", urb->actual_length);
2012 DUMP1(urb->transfer_buffer, urb->actual_length);
2014 if (req->bRequestType ==
2015 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2016 /* response to a read command */
2017 serial->rx_urb_filled[0] = 1;
2018 spin_lock(&serial->serial_lock);
2019 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2020 spin_unlock(&serial->serial_lock);
2022 hso_put_activity(serial->parent);
2025 /* response to a write command */
2026 hso_kick_transmit(serial);
2031 /* handle RX data for serial port */
2032 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
2034 struct tty_struct *tty;
2035 int write_length_remaining = 0;
2039 if (urb == NULL || serial == NULL) {
2040 D1("serial = NULL");
2044 spin_lock(&serial->serial_lock);
2045 tty = tty_kref_get(serial->tty);
2046 spin_unlock(&serial->serial_lock);
2048 /* Push data to tty */
2050 write_length_remaining = urb->actual_length -
2051 serial->curr_rx_urb_offset;
2052 D1("data to push to tty");
2053 while (write_length_remaining) {
2054 if (test_bit(TTY_THROTTLED, &tty->flags))
2056 curr_write_len = tty_insert_flip_string
2057 (tty, urb->transfer_buffer +
2058 serial->curr_rx_urb_offset,
2059 write_length_remaining);
2060 serial->curr_rx_urb_offset += curr_write_len;
2061 write_length_remaining -= curr_write_len;
2062 tty_flip_buffer_push(tty);
2065 if (write_length_remaining == 0) {
2066 serial->curr_rx_urb_offset = 0;
2067 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2070 return write_length_remaining;
2074 /* Base driver functions */
2076 static void hso_log_port(struct hso_device *hso_dev)
2081 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2082 case HSO_PORT_CONTROL:
2083 port_type = "Control";
2086 port_type = "Application";
2091 case HSO_PORT_GPS_CONTROL:
2092 port_type = "GPS control";
2095 port_type = "Application2";
2101 port_type = "Diagnostic";
2103 case HSO_PORT_DIAG2:
2104 port_type = "Diagnostic2";
2106 case HSO_PORT_MODEM:
2107 port_type = "Modem";
2109 case HSO_PORT_NETWORK:
2110 port_type = "Network";
2113 port_type = "Unknown";
2116 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2117 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2119 sprintf(port_dev, "/dev/%s%d", tty_filename,
2120 dev2ser(hso_dev)->minor);
2122 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2123 port_type, port_dev);
2126 static int hso_start_net_device(struct hso_device *hso_dev)
2129 struct hso_net *hso_net = dev2net(hso_dev);
2134 /* send URBs for all read buffers */
2135 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2137 /* Prep a receive URB */
2138 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2140 usb_rcvbulkpipe(hso_dev->usb,
2142 bEndpointAddress & 0x7F),
2143 hso_net->mux_bulk_rx_buf_pool[i],
2144 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2147 /* Put it out there so the device can send us stuff */
2148 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2151 dev_warn(&hso_dev->usb->dev,
2152 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2159 static int hso_stop_net_device(struct hso_device *hso_dev)
2162 struct hso_net *hso_net = dev2net(hso_dev);
2167 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2168 if (hso_net->mux_bulk_rx_urb_pool[i])
2169 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2172 if (hso_net->mux_bulk_tx_urb)
2173 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2178 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2181 struct hso_serial *serial = dev2ser(hso_dev);
2186 /* If it is not the MUX port fill in and submit a bulk urb (already
2187 * allocated in hso_serial_start) */
2188 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2189 for (i = 0; i < serial->num_rx_urbs; i++) {
2190 usb_fill_bulk_urb(serial->rx_urb[i],
2191 serial->parent->usb,
2192 usb_rcvbulkpipe(serial->parent->usb,
2197 serial->rx_data_length,
2198 hso_std_serial_read_bulk_callback,
2200 result = usb_submit_urb(serial->rx_urb[i], flags);
2202 dev_warn(&serial->parent->usb->dev,
2203 "Failed to submit urb - res %d\n",
2209 mutex_lock(&serial->shared_int->shared_int_lock);
2210 if (!serial->shared_int->use_count) {
2212 hso_mux_submit_intr_urb(serial->shared_int,
2213 hso_dev->usb, flags);
2215 serial->shared_int->use_count++;
2216 mutex_unlock(&serial->shared_int->shared_int_lock);
2218 if (serial->tiocmget)
2219 tiocmget_submit_urb(serial,
2221 serial->parent->usb);
2225 static int hso_stop_serial_device(struct hso_device *hso_dev)
2228 struct hso_serial *serial = dev2ser(hso_dev);
2229 struct hso_tiocmget *tiocmget;
2234 for (i = 0; i < serial->num_rx_urbs; i++) {
2235 if (serial->rx_urb[i]) {
2236 usb_kill_urb(serial->rx_urb[i]);
2237 serial->rx_urb_filled[i] = 0;
2240 serial->curr_rx_urb_idx = 0;
2241 serial->curr_rx_urb_offset = 0;
2244 usb_kill_urb(serial->tx_urb);
2246 if (serial->shared_int) {
2247 mutex_lock(&serial->shared_int->shared_int_lock);
2248 if (serial->shared_int->use_count &&
2249 (--serial->shared_int->use_count == 0)) {
2252 urb = serial->shared_int->shared_intr_urb;
2256 mutex_unlock(&serial->shared_int->shared_int_lock);
2258 tiocmget = serial->tiocmget;
2260 wake_up_interruptible(&tiocmget->waitq);
2261 usb_kill_urb(tiocmget->urb);
2267 static void hso_serial_common_free(struct hso_serial *serial)
2271 if (serial->parent->dev)
2272 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2274 tty_unregister_device(tty_drv, serial->minor);
2276 for (i = 0; i < serial->num_rx_urbs; i++) {
2277 /* unlink and free RX URB */
2278 usb_free_urb(serial->rx_urb[i]);
2279 /* free the RX buffer */
2280 kfree(serial->rx_data[i]);
2283 /* unlink and free TX URB */
2284 usb_free_urb(serial->tx_urb);
2285 kfree(serial->tx_data);
2288 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2289 int rx_size, int tx_size)
2295 minor = get_free_serial_index();
2299 /* register our minor number */
2300 serial->parent->dev = tty_register_device(tty_drv, minor,
2301 &serial->parent->interface->dev);
2302 dev = serial->parent->dev;
2303 dev->driver_data = serial->parent;
2304 i = device_create_file(dev, &dev_attr_hsotype);
2306 /* fill in specific data for later use */
2307 serial->minor = minor;
2308 serial->magic = HSO_SERIAL_MAGIC;
2309 spin_lock_init(&serial->serial_lock);
2310 serial->num_rx_urbs = num_urbs;
2312 /* RX, allocate urb and initialize */
2314 /* prepare our RX buffer */
2315 serial->rx_data_length = rx_size;
2316 for (i = 0; i < serial->num_rx_urbs; i++) {
2317 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2318 if (!serial->rx_urb[i]) {
2319 dev_err(dev, "Could not allocate urb?\n");
2322 serial->rx_urb[i]->transfer_buffer = NULL;
2323 serial->rx_urb[i]->transfer_buffer_length = 0;
2324 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2326 if (!serial->rx_data[i]) {
2327 dev_err(dev, "%s - Out of memory\n", __func__);
2332 /* TX, allocate urb and initialize */
2333 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2334 if (!serial->tx_urb) {
2335 dev_err(dev, "Could not allocate urb?\n");
2338 serial->tx_urb->transfer_buffer = NULL;
2339 serial->tx_urb->transfer_buffer_length = 0;
2340 /* prepare our TX buffer */
2341 serial->tx_data_count = 0;
2342 serial->tx_buffer_count = 0;
2343 serial->tx_data_length = tx_size;
2344 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2345 if (!serial->tx_data) {
2346 dev_err(dev, "%s - Out of memory", __func__);
2349 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2350 if (!serial->tx_buffer) {
2351 dev_err(dev, "%s - Out of memory", __func__);
2357 hso_serial_common_free(serial);
2361 /* Frees a general hso device */
2362 static void hso_free_device(struct hso_device *hso_dev)
2367 /* Creates a general hso device */
2368 static struct hso_device *hso_create_device(struct usb_interface *intf,
2371 struct hso_device *hso_dev;
2373 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2377 hso_dev->port_spec = port_spec;
2378 hso_dev->usb = interface_to_usbdev(intf);
2379 hso_dev->interface = intf;
2380 kref_init(&hso_dev->ref);
2381 mutex_init(&hso_dev->mutex);
2383 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2384 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2389 /* Removes a network device in the network device table */
2390 static int remove_net_device(struct hso_device *hso_dev)
2394 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2395 if (network_table[i] == hso_dev) {
2396 network_table[i] = NULL;
2400 if (i == HSO_MAX_NET_DEVICES)
2405 /* Frees our network device */
2406 static void hso_free_net_device(struct hso_device *hso_dev)
2409 struct hso_net *hso_net = dev2net(hso_dev);
2415 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2416 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2417 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2419 usb_free_urb(hso_net->mux_bulk_tx_urb);
2420 kfree(hso_net->mux_bulk_tx_buf);
2422 remove_net_device(hso_net->parent);
2425 unregister_netdev(hso_net->net);
2426 free_netdev(hso_net->net);
2429 hso_free_device(hso_dev);
2432 /* initialize the network interface */
2433 static void hso_net_init(struct net_device *net)
2435 struct hso_net *hso_net = netdev_priv(net);
2437 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2439 /* fill in the other fields */
2440 net->open = hso_net_open;
2441 net->stop = hso_net_close;
2442 net->hard_start_xmit = hso_net_start_xmit;
2443 net->tx_timeout = hso_net_tx_timeout;
2444 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2445 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2446 net->type = ARPHRD_NONE;
2447 net->mtu = DEFAULT_MTU - 14;
2448 net->tx_queue_len = 10;
2449 SET_ETHTOOL_OPS(net, &ops);
2451 /* and initialize the semaphore */
2452 spin_lock_init(&hso_net->net_lock);
2455 /* Adds a network device in the network device table */
2456 static int add_net_device(struct hso_device *hso_dev)
2460 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2461 if (network_table[i] == NULL) {
2462 network_table[i] = hso_dev;
2466 if (i == HSO_MAX_NET_DEVICES)
2471 static int hso_radio_toggle(void *data, enum rfkill_state state)
2473 struct hso_device *hso_dev = data;
2474 int enabled = (state == RFKILL_STATE_ON);
2477 mutex_lock(&hso_dev->mutex);
2478 if (hso_dev->usb_gone)
2481 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2482 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2483 USB_CTRL_SET_TIMEOUT);
2484 mutex_unlock(&hso_dev->mutex);
2488 /* Creates and sets up everything for rfkill */
2489 static void hso_create_rfkill(struct hso_device *hso_dev,
2490 struct usb_interface *interface)
2492 struct hso_net *hso_net = dev2net(hso_dev);
2493 struct device *dev = &hso_net->net->dev;
2496 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2498 if (!hso_net->rfkill) {
2499 dev_err(dev, "%s - Out of memory\n", __func__);
2502 rfkn = kzalloc(20, GFP_KERNEL);
2504 rfkill_free(hso_net->rfkill);
2505 hso_net->rfkill = NULL;
2506 dev_err(dev, "%s - Out of memory\n", __func__);
2509 snprintf(rfkn, 20, "hso-%d",
2510 interface->altsetting->desc.bInterfaceNumber);
2511 hso_net->rfkill->name = rfkn;
2512 hso_net->rfkill->state = RFKILL_STATE_ON;
2513 hso_net->rfkill->data = hso_dev;
2514 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2515 if (rfkill_register(hso_net->rfkill) < 0) {
2517 hso_net->rfkill->name = NULL;
2518 rfkill_free(hso_net->rfkill);
2519 hso_net->rfkill = NULL;
2520 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2525 /* Creates our network device */
2526 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2529 struct net_device *net;
2530 struct hso_net *hso_net;
2531 struct hso_device *hso_dev;
2533 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2537 /* allocate our network device, then we can put in our private data */
2538 /* call hso_net_init to do the basic initialization */
2539 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2541 dev_err(&interface->dev, "Unable to create ethernet device\n");
2545 hso_net = netdev_priv(net);
2547 hso_dev->port_data.dev_net = hso_net;
2549 hso_net->parent = hso_dev;
2551 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2553 if (!hso_net->in_endp) {
2554 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2557 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2559 if (!hso_net->out_endp) {
2560 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2563 SET_NETDEV_DEV(net, &interface->dev);
2565 /* registering our net device */
2566 result = register_netdev(net);
2568 dev_err(&interface->dev, "Failed to register device\n");
2572 /* start allocating */
2573 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2574 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2575 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2576 dev_err(&interface->dev, "Could not allocate rx urb\n");
2579 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2581 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2582 dev_err(&interface->dev, "Could not allocate rx buf\n");
2586 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2587 if (!hso_net->mux_bulk_tx_urb) {
2588 dev_err(&interface->dev, "Could not allocate tx urb\n");
2591 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2592 if (!hso_net->mux_bulk_tx_buf) {
2593 dev_err(&interface->dev, "Could not allocate tx buf\n");
2597 add_net_device(hso_dev);
2599 hso_log_port(hso_dev);
2601 hso_create_rfkill(hso_dev, interface);
2605 hso_free_net_device(hso_dev);
2609 static void hso_free_tiomget(struct hso_serial *serial)
2611 struct hso_tiocmget *tiocmget = serial->tiocmget;
2614 if (tiocmget->urb) {
2615 usb_free_urb(tiocmget->urb);
2616 tiocmget->urb = NULL;
2618 serial->tiocmget = NULL;
2623 /* Frees an AT channel ( goes for both mux and non-mux ) */
2624 static void hso_free_serial_device(struct hso_device *hso_dev)
2626 struct hso_serial *serial = dev2ser(hso_dev);
2630 set_serial_by_index(serial->minor, NULL);
2632 hso_serial_common_free(serial);
2634 if (serial->shared_int) {
2635 mutex_lock(&serial->shared_int->shared_int_lock);
2636 if (--serial->shared_int->ref_count == 0)
2637 hso_free_shared_int(serial->shared_int);
2639 mutex_unlock(&serial->shared_int->shared_int_lock);
2641 hso_free_tiomget(serial);
2643 hso_free_device(hso_dev);
2646 /* Creates a bulk AT channel */
2647 static struct hso_device *hso_create_bulk_serial_device(
2648 struct usb_interface *interface, int port)
2650 struct hso_device *hso_dev;
2651 struct hso_serial *serial;
2653 struct hso_tiocmget *tiocmget;
2655 hso_dev = hso_create_device(interface, port);
2659 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2663 serial->parent = hso_dev;
2664 hso_dev->port_data.dev_serial = serial;
2666 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
2668 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2670 /* it isn't going to break our heart if serial->tiocmget
2671 * allocation fails don't bother checking this.
2673 if (serial->tiocmget) {
2674 tiocmget = serial->tiocmget;
2675 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2676 if (tiocmget->urb) {
2677 mutex_init(&tiocmget->mutex);
2678 init_waitqueue_head(&tiocmget->waitq);
2679 tiocmget->endp = hso_get_ep(
2681 USB_ENDPOINT_XFER_INT,
2684 hso_free_tiomget(serial);
2690 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2694 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2696 if (!serial->in_endp) {
2697 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2703 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2704 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2708 serial->write_data = hso_std_serial_write_data;
2710 /* and record this serial */
2711 set_serial_by_index(serial->minor, serial);
2713 /* setup the proc dirs and files if needed */
2714 hso_log_port(hso_dev);
2716 /* done, return it */
2720 hso_serial_common_free(serial);
2722 hso_free_tiomget(serial);
2724 hso_free_device(hso_dev);
2728 /* Creates a multiplexed AT channel */
2730 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2732 struct hso_shared_int *mux)
2734 struct hso_device *hso_dev;
2735 struct hso_serial *serial;
2738 port_spec = HSO_INTF_MUX;
2739 port_spec &= ~HSO_PORT_MASK;
2741 port_spec |= hso_mux_to_port(port);
2742 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2745 hso_dev = hso_create_device(interface, port_spec);
2749 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2753 hso_dev->port_data.dev_serial = serial;
2754 serial->parent = hso_dev;
2756 if (hso_serial_common_create
2757 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2760 serial->tx_data_length--;
2761 serial->write_data = hso_mux_serial_write_data;
2763 serial->shared_int = mux;
2764 mutex_lock(&serial->shared_int->shared_int_lock);
2765 serial->shared_int->ref_count++;
2766 mutex_unlock(&serial->shared_int->shared_int_lock);
2768 /* and record this serial */
2769 set_serial_by_index(serial->minor, serial);
2771 /* setup the proc dirs and files if needed */
2772 hso_log_port(hso_dev);
2774 /* done, return it */
2779 tty_unregister_device(tty_drv, serial->minor);
2783 hso_free_device(hso_dev);
2788 static void hso_free_shared_int(struct hso_shared_int *mux)
2790 usb_free_urb(mux->shared_intr_urb);
2791 kfree(mux->shared_intr_buf);
2792 mutex_unlock(&mux->shared_int_lock);
2797 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2799 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2804 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2806 if (!mux->intr_endp) {
2807 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2811 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2812 if (!mux->shared_intr_urb) {
2813 dev_err(&interface->dev, "Could not allocate intr urb?");
2816 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2818 if (!mux->shared_intr_buf) {
2819 dev_err(&interface->dev, "Could not allocate intr buf?");
2823 mutex_init(&mux->shared_int_lock);
2828 kfree(mux->shared_intr_buf);
2829 usb_free_urb(mux->shared_intr_urb);
2834 /* Gets the port spec for a certain interface */
2835 static int hso_get_config_data(struct usb_interface *interface)
2837 struct usb_device *usbdev = interface_to_usbdev(interface);
2839 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2842 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2843 0x86, 0xC0, 0, 0, config_data, 17,
2844 USB_CTRL_SET_TIMEOUT) != 0x11) {
2848 switch (config_data[if_num]) {
2853 result = HSO_PORT_DIAG;
2856 result = HSO_PORT_GPS;
2859 result = HSO_PORT_GPS_CONTROL;
2862 result = HSO_PORT_APP;
2865 result = HSO_PORT_APP2;
2868 result = HSO_PORT_CONTROL;
2871 result = HSO_PORT_NETWORK;
2874 result = HSO_PORT_MODEM;
2877 result = HSO_PORT_MSD;
2880 result = HSO_PORT_PCSC;
2883 result = HSO_PORT_VOICE;
2890 result |= HSO_INTF_BULK;
2892 if (config_data[16] & 0x1)
2893 result |= HSO_INFO_CRC_BUG;
2898 /* called once for each interface upon device insertion */
2899 static int hso_probe(struct usb_interface *interface,
2900 const struct usb_device_id *id)
2902 int mux, i, if_num, port_spec;
2903 unsigned char port_mask;
2904 struct hso_device *hso_dev = NULL;
2905 struct hso_shared_int *shared_int;
2906 struct hso_device *tmp_dev = NULL;
2908 if_num = interface->altsetting->desc.bInterfaceNumber;
2910 /* Get the interface/port specification from either driver_info or from
2911 * the device itself */
2912 if (id->driver_info)
2913 port_spec = ((u32 *)(id->driver_info))[if_num];
2915 port_spec = hso_get_config_data(interface);
2917 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2918 dev_err(&interface->dev, "Not our interface\n");
2921 /* Check if we need to switch to alt interfaces prior to port
2923 if (interface->num_altsetting > 1)
2924 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2925 interface->needs_remote_wakeup = 1;
2927 /* Allocate new hso device(s) */
2928 switch (port_spec & HSO_INTF_MASK) {
2930 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2931 /* Create the network device */
2933 hso_dev = hso_create_net_device(interface);
2940 if (hso_get_mux_ports(interface, &port_mask))
2941 /* TODO: de-allocate everything */
2944 shared_int = hso_create_shared_int(interface);
2948 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2949 if (port_mask & i) {
2950 hso_dev = hso_create_mux_serial_device(
2951 interface, i, shared_int);
2962 /* It's a regular bulk interface */
2963 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2965 hso_dev = hso_create_net_device(interface);
2968 hso_create_bulk_serial_device(interface, port_spec);
2976 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2978 /* save our data pointer in this device */
2979 usb_set_intfdata(interface, hso_dev);
2984 hso_free_interface(interface);
2988 /* device removed, cleaning up */
2989 static void hso_disconnect(struct usb_interface *interface)
2991 hso_free_interface(interface);
2993 /* remove reference of our private data */
2994 usb_set_intfdata(interface, NULL);
2996 usb_driver_release_interface(&hso_driver, interface);
2999 static void async_get_intf(struct work_struct *data)
3001 struct hso_device *hso_dev =
3002 container_of(data, struct hso_device, async_get_intf);
3003 usb_autopm_get_interface(hso_dev->interface);
3006 static void async_put_intf(struct work_struct *data)
3008 struct hso_device *hso_dev =
3009 container_of(data, struct hso_device, async_put_intf);
3010 usb_autopm_put_interface(hso_dev->interface);
3013 static int hso_get_activity(struct hso_device *hso_dev)
3015 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3016 if (!hso_dev->is_active) {
3017 hso_dev->is_active = 1;
3018 schedule_work(&hso_dev->async_get_intf);
3022 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3025 usb_mark_last_busy(hso_dev->usb);
3030 static int hso_put_activity(struct hso_device *hso_dev)
3032 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3033 if (hso_dev->is_active) {
3034 hso_dev->is_active = 0;
3035 schedule_work(&hso_dev->async_put_intf);
3039 hso_dev->is_active = 0;
3043 /* called by kernel when we need to suspend device */
3044 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3048 /* Stop all serial ports */
3049 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3050 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3051 result = hso_stop_serial_device(serial_table[i]);
3057 /* Stop all network ports */
3058 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3059 if (network_table[i] &&
3060 (network_table[i]->interface == iface)) {
3061 result = hso_stop_net_device(network_table[i]);
3071 /* called by kernel when we need to resume device */
3072 static int hso_resume(struct usb_interface *iface)
3075 struct hso_net *hso_net;
3077 /* Start all serial ports */
3078 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3079 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3080 if (dev2ser(serial_table[i])->open_count) {
3082 hso_start_serial_device(serial_table[i], GFP_NOIO);
3083 hso_kick_transmit(dev2ser(serial_table[i]));
3090 /* Start all network ports */
3091 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3092 if (network_table[i] &&
3093 (network_table[i]->interface == iface)) {
3094 hso_net = dev2net(network_table[i]);
3095 if (hso_net->flags & IFF_UP) {
3096 /* First transmit any lingering data,
3097 then restart the device. */
3098 if (hso_net->skb_tx_buf) {
3099 dev_dbg(&iface->dev,
3101 " lingering data\n");
3102 hso_net_start_xmit(hso_net->skb_tx_buf,
3104 hso_net->skb_tx_buf = NULL;
3106 result = hso_start_net_device(network_table[i]);
3117 static void hso_serial_ref_free(struct kref *ref)
3119 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3121 hso_free_serial_device(hso_dev);
3124 static void hso_free_interface(struct usb_interface *interface)
3126 struct hso_serial *hso_dev;
3127 struct tty_struct *tty;
3130 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3132 && (serial_table[i]->interface == interface)) {
3133 hso_dev = dev2ser(serial_table[i]);
3134 spin_lock_irq(&hso_dev->serial_lock);
3135 tty = tty_kref_get(hso_dev->tty);
3136 spin_unlock_irq(&hso_dev->serial_lock);
3139 mutex_lock(&hso_dev->parent->mutex);
3141 hso_dev->parent->usb_gone = 1;
3142 mutex_unlock(&hso_dev->parent->mutex);
3143 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3147 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3148 if (network_table[i]
3149 && (network_table[i]->interface == interface)) {
3150 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3151 /* hso_stop_net_device doesn't stop the net queue since
3152 * traffic needs to start it again when suspended */
3153 netif_stop_queue(dev2net(network_table[i])->net);
3154 hso_stop_net_device(network_table[i]);
3155 cancel_work_sync(&network_table[i]->async_put_intf);
3156 cancel_work_sync(&network_table[i]->async_get_intf);
3158 rfkill_unregister(rfk);
3159 hso_free_net_device(network_table[i]);
3164 /* Helper functions */
3166 /* Get the endpoint ! */
3167 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3171 struct usb_host_interface *iface = intf->cur_altsetting;
3172 struct usb_endpoint_descriptor *endp;
3174 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3175 endp = &iface->endpoint[i].desc;
3176 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3177 (usb_endpoint_type(endp) == type))
3184 /* Get the byte that describes which ports are enabled */
3185 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3188 struct usb_host_interface *iface = intf->cur_altsetting;
3190 if (iface->extralen == 3) {
3191 *ports = iface->extra[2];
3195 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3196 if (iface->endpoint[i].extralen == 3) {
3197 *ports = iface->endpoint[i].extra[2];
3205 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3206 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3207 struct usb_device *usb, gfp_t gfp)
3211 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3213 shared_int->intr_endp->bEndpointAddress & 0x7F),
3214 shared_int->shared_intr_buf,
3215 shared_int->intr_endp->wMaxPacketSize,
3216 intr_callback, shared_int,
3217 shared_int->intr_endp->bInterval);
3219 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3221 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
3227 /* operations setup of the serial interface */
3228 static const struct tty_operations hso_serial_ops = {
3229 .open = hso_serial_open,
3230 .close = hso_serial_close,
3231 .write = hso_serial_write,
3232 .write_room = hso_serial_write_room,
3233 .ioctl = hso_serial_ioctl,
3234 .set_termios = hso_serial_set_termios,
3235 .chars_in_buffer = hso_serial_chars_in_buffer,
3236 .tiocmget = hso_serial_tiocmget,
3237 .tiocmset = hso_serial_tiocmset,
3238 .unthrottle = hso_unthrottle
3241 static struct usb_driver hso_driver = {
3242 .name = driver_name,
3244 .disconnect = hso_disconnect,
3245 .id_table = hso_ids,
3246 .suspend = hso_suspend,
3247 .resume = hso_resume,
3248 .reset_resume = hso_resume,
3249 .supports_autosuspend = 1,
3252 static int __init hso_init(void)
3257 /* put it in the log */
3258 printk(KERN_INFO "hso: %s\n", version);
3260 /* Initialise the serial table semaphore and table */
3261 spin_lock_init(&serial_table_lock);
3262 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3263 serial_table[i] = NULL;
3265 /* allocate our driver using the proper amount of supported minors */
3266 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3270 /* fill in all needed values */
3271 tty_drv->magic = TTY_DRIVER_MAGIC;
3272 tty_drv->owner = THIS_MODULE;
3273 tty_drv->driver_name = driver_name;
3274 tty_drv->name = tty_filename;
3276 /* if major number is provided as parameter, use that one */
3278 tty_drv->major = tty_major;
3280 tty_drv->minor_start = 0;
3281 tty_drv->num = HSO_SERIAL_TTY_MINORS;
3282 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3283 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3284 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3285 tty_drv->init_termios = tty_std_termios;
3286 hso_init_termios(&tty_drv->init_termios);
3287 tty_set_operations(tty_drv, &hso_serial_ops);
3289 /* register the tty driver */
3290 result = tty_register_driver(tty_drv);
3292 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3297 /* register this module as an usb driver */
3298 result = usb_register(&hso_driver);
3300 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3302 /* cleanup serial interface */
3303 tty_unregister_driver(tty_drv);
3311 static void __exit hso_exit(void)
3313 printk(KERN_INFO "hso: unloaded\n");
3315 tty_unregister_driver(tty_drv);
3316 /* deregister the usb driver */
3317 usb_deregister(&hso_driver);
3320 /* Module definitions */
3321 module_init(hso_init);
3322 module_exit(hso_exit);
3324 MODULE_AUTHOR(MOD_AUTHOR);
3325 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3326 MODULE_LICENSE(MOD_LICENSE);
3327 MODULE_INFO(Version, DRIVER_VERSION);
3329 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3330 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3331 module_param(debug, int, S_IRUGO | S_IWUSR);
3333 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3334 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3335 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3337 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3338 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3339 module_param(disable_net, int, S_IRUGO | S_IWUSR);