1 /******************************************************************************
3 * Driver for Option High Speed Mobile Devices.
5 * Copyright (C) 2008 Option International
6 * Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com>
8 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
9 * <ajb@spheresystems.co.uk>
10 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
11 * Copyright (C) 2008 Novell, Inc.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 *****************************************************************************/
30 /******************************************************************************
32 * Description of the device:
34 * Interface 0: Contains the IP network interface on the bulk end points.
35 * The multiplexed serial ports are using the interrupt and
37 * Interrupt contains a bitmap telling which multiplexed
38 * serialport needs servicing.
40 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
41 * port is opened, as this have a huge impact on the network port
44 * Interface 2: Standard modem interface - circuit switched interface, this
45 * can be used to make a standard ppp connection however it
46 * should not be used in conjunction with the IP network interface
47 * enabled for USB performance reasons i.e. if using this set
48 * ideally disable_net=1.
50 *****************************************************************************/
52 #include <linux/sched.h>
53 #include <linux/slab.h>
54 #include <linux/init.h>
55 #include <linux/delay.h>
56 #include <linux/netdevice.h>
57 #include <linux/module.h>
58 #include <linux/ethtool.h>
59 #include <linux/usb.h>
60 #include <linux/timer.h>
61 #include <linux/tty.h>
62 #include <linux/tty_driver.h>
63 #include <linux/tty_flip.h>
64 #include <linux/kmod.h>
65 #include <linux/rfkill.h>
67 #include <linux/uaccess.h>
68 #include <linux/usb/cdc.h>
70 #include <asm/byteorder.h>
71 #include <linux/serial_core.h>
72 #include <linux/serial.h>
75 #define DRIVER_VERSION "1.2"
76 #define MOD_AUTHOR "Option Wireless"
77 #define MOD_DESCRIPTION "USB High Speed Option driver"
78 #define MOD_LICENSE "GPL"
80 #define HSO_MAX_NET_DEVICES 10
81 #define HSO__MAX_MTU 2048
82 #define DEFAULT_MTU 1500
83 #define DEFAULT_MRU 1500
85 #define CTRL_URB_RX_SIZE 1024
86 #define CTRL_URB_TX_SIZE 64
88 #define BULK_URB_RX_SIZE 4096
89 #define BULK_URB_TX_SIZE 8192
91 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
92 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
93 #define MUX_BULK_RX_BUF_COUNT 4
94 #define USB_TYPE_OPTION_VENDOR 0x20
96 /* These definitions are used with the struct hso_net flags element */
97 /* - use *_bit operations on it. (bit indices not values.) */
98 #define HSO_NET_RUNNING 0
100 #define HSO_NET_TX_TIMEOUT (HZ*10)
102 #define HSO_SERIAL_MAGIC 0x48534f31
104 /* Number of ttys to handle */
105 #define HSO_SERIAL_TTY_MINORS 256
107 #define MAX_RX_URBS 2
109 static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
112 return tty->driver_data;
116 /*****************************************************************************/
117 /* Debugging functions */
118 /*****************************************************************************/
119 #define D__(lvl_, fmt, arg...) \
121 printk(lvl_ "[%d:%s]: " fmt "\n", \
122 __LINE__, __func__, ## arg); \
125 #define D_(lvl, args...) \
128 D__(KERN_INFO, args); \
131 #define D1(args...) D_(0x01, ##args)
132 #define D2(args...) D_(0x02, ##args)
133 #define D3(args...) D_(0x04, ##args)
134 #define D4(args...) D_(0x08, ##args)
135 #define D5(args...) D_(0x10, ##args)
137 /*****************************************************************************/
139 /*****************************************************************************/
140 enum pkt_parse_state {
146 /*****************************************************************************/
148 /*****************************************************************************/
150 struct hso_shared_int {
151 struct usb_endpoint_descriptor *intr_endp;
152 void *shared_intr_buf;
153 struct urb *shared_intr_urb;
154 struct usb_device *usb;
157 struct mutex shared_int_lock;
161 struct hso_device *parent;
162 struct net_device *net;
163 struct rfkill *rfkill;
165 struct usb_endpoint_descriptor *in_endp;
166 struct usb_endpoint_descriptor *out_endp;
168 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
169 struct urb *mux_bulk_tx_urb;
170 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
171 void *mux_bulk_tx_buf;
173 struct sk_buff *skb_rx_buf;
174 struct sk_buff *skb_tx_buf;
176 enum pkt_parse_state rx_parse_state;
179 unsigned short rx_buf_size;
180 unsigned short rx_buf_missing;
181 struct iphdr rx_ip_hdr;
192 #define BM_REQUEST_TYPE (0xa1)
193 #define B_NOTIFICATION (0x20)
194 #define W_VALUE (0x0)
195 #define W_INDEX (0x2)
196 #define W_LENGTH (0x2)
198 #define B_OVERRUN (0x1<<6)
199 #define B_PARITY (0x1<<5)
200 #define B_FRAMING (0x1<<4)
201 #define B_RING_SIGNAL (0x1<<3)
202 #define B_BREAK (0x1<<2)
203 #define B_TX_CARRIER (0x1<<1)
204 #define B_RX_CARRIER (0x1<<0)
206 struct hso_serial_state_notification {
212 u16 UART_state_bitmap;
213 } __attribute__((packed));
215 struct hso_tiocmget {
217 wait_queue_head_t waitq;
219 struct usb_endpoint_descriptor *endp;
221 struct hso_serial_state_notification serial_state_notification;
222 u16 prev_UART_state_bitmap;
223 struct uart_icount icount;
228 struct hso_device *parent;
232 struct hso_shared_int *shared_int;
234 /* rx/tx urb could be either a bulk urb or a control urb depending
235 on which serial port it is used on. */
236 struct urb *rx_urb[MAX_RX_URBS];
238 u8 *rx_data[MAX_RX_URBS];
239 u16 rx_data_length; /* should contain allocated length */
244 u16 tx_data_length; /* should contain allocated length */
247 struct usb_ctrlrequest ctrl_req_tx;
248 struct usb_ctrlrequest ctrl_req_rx;
250 struct usb_endpoint_descriptor *in_endp;
251 struct usb_endpoint_descriptor *out_endp;
253 enum rx_ctrl_state rx_state;
256 unsigned tx_urb_used:1;
258 /* from usb_serial_port */
259 struct tty_struct *tty;
261 spinlock_t serial_lock;
263 int (*write_data) (struct hso_serial *serial);
264 struct hso_tiocmget *tiocmget;
265 /* Hacks required to get flow control
266 * working on the serial receive buffers
267 * so as not to drop characters on the floor.
270 u16 curr_rx_urb_offset;
271 u8 rx_urb_filled[MAX_RX_URBS];
272 struct tasklet_struct unthrottle_tasklet;
273 struct work_struct retry_unthrottle_workqueue;
278 struct hso_serial *dev_serial;
279 struct hso_net *dev_net;
286 struct work_struct async_get_intf;
287 struct work_struct async_put_intf;
289 struct usb_device *usb;
290 struct usb_interface *interface;
297 /* Type of interface */
298 #define HSO_INTF_MASK 0xFF00
299 #define HSO_INTF_MUX 0x0100
300 #define HSO_INTF_BULK 0x0200
303 #define HSO_PORT_MASK 0xFF
304 #define HSO_PORT_NO_PORT 0x0
305 #define HSO_PORT_CONTROL 0x1
306 #define HSO_PORT_APP 0x2
307 #define HSO_PORT_GPS 0x3
308 #define HSO_PORT_PCSC 0x4
309 #define HSO_PORT_APP2 0x5
310 #define HSO_PORT_GPS_CONTROL 0x6
311 #define HSO_PORT_MSD 0x7
312 #define HSO_PORT_VOICE 0x8
313 #define HSO_PORT_DIAG2 0x9
314 #define HSO_PORT_DIAG 0x10
315 #define HSO_PORT_MODEM 0x11
316 #define HSO_PORT_NETWORK 0x12
318 /* Additional device info */
319 #define HSO_INFO_MASK 0xFF000000
320 #define HSO_INFO_CRC_BUG 0x01000000
322 /*****************************************************************************/
324 /*****************************************************************************/
325 /* Serial driver functions */
326 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
327 unsigned int set, unsigned int clear);
328 static void ctrl_callback(struct urb *urb);
329 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
330 static void hso_kick_transmit(struct hso_serial *serial);
331 /* Helper functions */
332 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
333 struct usb_device *usb, gfp_t gfp);
334 static void log_usb_status(int status, const char *function);
335 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
337 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
338 static void hso_free_interface(struct usb_interface *intf);
339 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
340 static int hso_stop_serial_device(struct hso_device *hso_dev);
341 static int hso_start_net_device(struct hso_device *hso_dev);
342 static void hso_free_shared_int(struct hso_shared_int *shared_int);
343 static int hso_stop_net_device(struct hso_device *hso_dev);
344 static void hso_serial_ref_free(struct kref *ref);
345 static void hso_std_serial_read_bulk_callback(struct urb *urb);
346 static int hso_mux_serial_read(struct hso_serial *serial);
347 static void async_get_intf(struct work_struct *data);
348 static void async_put_intf(struct work_struct *data);
349 static int hso_put_activity(struct hso_device *hso_dev);
350 static int hso_get_activity(struct hso_device *hso_dev);
351 static void tiocmget_intr_callback(struct urb *urb);
352 /*****************************************************************************/
353 /* Helping functions */
354 /*****************************************************************************/
358 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
360 return hso_dev->port_data.dev_net;
363 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
365 return hso_dev->port_data.dev_serial;
368 /* Debugging functions */
370 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
373 static char name[255];
375 sprintf(name, "hso[%d:%s]", line_count, func_name);
376 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
379 #define DUMP(buf_, len_) \
380 dbg_dump(__LINE__, __func__, buf_, len_)
382 #define DUMP1(buf_, len_) \
388 #define DUMP(buf_, len_)
389 #define DUMP1(buf_, len_)
392 /* module parameters */
394 static int tty_major;
395 static int disable_net;
398 static const char driver_name[] = "hso";
399 static const char tty_filename[] = "ttyHS";
400 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
401 /* the usb driver itself (registered in hso_init) */
402 static struct usb_driver hso_driver;
403 /* serial structures */
404 static struct tty_driver *tty_drv;
405 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
406 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
407 static spinlock_t serial_table_lock;
409 static const s32 default_port_spec[] = {
410 HSO_INTF_MUX | HSO_PORT_NETWORK,
411 HSO_INTF_BULK | HSO_PORT_DIAG,
412 HSO_INTF_BULK | HSO_PORT_MODEM,
416 static const s32 icon321_port_spec[] = {
417 HSO_INTF_MUX | HSO_PORT_NETWORK,
418 HSO_INTF_BULK | HSO_PORT_DIAG2,
419 HSO_INTF_BULK | HSO_PORT_MODEM,
420 HSO_INTF_BULK | HSO_PORT_DIAG,
424 #define default_port_device(vendor, product) \
425 USB_DEVICE(vendor, product), \
426 .driver_info = (kernel_ulong_t)default_port_spec
428 #define icon321_port_device(vendor, product) \
429 USB_DEVICE(vendor, product), \
430 .driver_info = (kernel_ulong_t)icon321_port_spec
432 /* list of devices we support */
433 static const struct usb_device_id hso_ids[] = {
434 {default_port_device(0x0af0, 0x6711)},
435 {default_port_device(0x0af0, 0x6731)},
436 {default_port_device(0x0af0, 0x6751)},
437 {default_port_device(0x0af0, 0x6771)},
438 {default_port_device(0x0af0, 0x6791)},
439 {default_port_device(0x0af0, 0x6811)},
440 {default_port_device(0x0af0, 0x6911)},
441 {default_port_device(0x0af0, 0x6951)},
442 {default_port_device(0x0af0, 0x6971)},
443 {default_port_device(0x0af0, 0x7011)},
444 {default_port_device(0x0af0, 0x7031)},
445 {default_port_device(0x0af0, 0x7051)},
446 {default_port_device(0x0af0, 0x7071)},
447 {default_port_device(0x0af0, 0x7111)},
448 {default_port_device(0x0af0, 0x7211)},
449 {default_port_device(0x0af0, 0x7251)},
450 {default_port_device(0x0af0, 0x7271)},
451 {default_port_device(0x0af0, 0x7311)},
452 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
453 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
454 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
455 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
456 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
457 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
458 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
459 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
460 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
461 {USB_DEVICE(0x0af0, 0x7701)},
462 {USB_DEVICE(0x0af0, 0x7801)},
463 {USB_DEVICE(0x0af0, 0x7901)},
464 {USB_DEVICE(0x0af0, 0x7361)},
465 {icon321_port_device(0x0af0, 0xd051)},
468 MODULE_DEVICE_TABLE(usb, hso_ids);
470 /* Sysfs attribute */
471 static ssize_t hso_sysfs_show_porttype(struct device *dev,
472 struct device_attribute *attr,
475 struct hso_device *hso_dev = dev->driver_data;
481 switch (hso_dev->port_spec & HSO_PORT_MASK) {
482 case HSO_PORT_CONTROL:
483 port_name = "Control";
486 port_name = "Application";
489 port_name = "Application2";
494 case HSO_PORT_GPS_CONTROL:
495 port_name = "GPS Control";
501 port_name = "Diagnostic";
504 port_name = "Diagnostic2";
509 case HSO_PORT_NETWORK:
510 port_name = "Network";
513 port_name = "Unknown";
517 return sprintf(buf, "%s\n", port_name);
519 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
521 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
525 for (idx = 0; idx < serial->num_rx_urbs; idx++)
526 if (serial->rx_urb[idx] == urb)
528 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
532 /* converts mux value to a port spec value */
533 static u32 hso_mux_to_port(int mux)
539 result = HSO_PORT_CONTROL;
542 result = HSO_PORT_APP;
545 result = HSO_PORT_PCSC;
548 result = HSO_PORT_GPS;
551 result = HSO_PORT_APP2;
554 result = HSO_PORT_NO_PORT;
559 /* converts port spec value to a mux value */
560 static u32 hso_port_to_mux(int port)
564 switch (port & HSO_PORT_MASK) {
565 case HSO_PORT_CONTROL:
586 static struct hso_serial *get_serial_by_shared_int_and_type(
587 struct hso_shared_int *shared_int,
592 port = hso_mux_to_port(mux);
594 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
596 && (dev2ser(serial_table[i])->shared_int == shared_int)
597 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
598 return dev2ser(serial_table[i]);
605 static struct hso_serial *get_serial_by_index(unsigned index)
607 struct hso_serial *serial = NULL;
610 spin_lock_irqsave(&serial_table_lock, flags);
611 if (serial_table[index])
612 serial = dev2ser(serial_table[index]);
613 spin_unlock_irqrestore(&serial_table_lock, flags);
618 static int get_free_serial_index(void)
623 spin_lock_irqsave(&serial_table_lock, flags);
624 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
625 if (serial_table[index] == NULL) {
626 spin_unlock_irqrestore(&serial_table_lock, flags);
630 spin_unlock_irqrestore(&serial_table_lock, flags);
632 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
636 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
640 spin_lock_irqsave(&serial_table_lock, flags);
642 serial_table[index] = serial->parent;
644 serial_table[index] = NULL;
645 spin_unlock_irqrestore(&serial_table_lock, flags);
648 /* log a meaningful explanation of an USB status */
649 static void log_usb_status(int status, const char *function)
655 explanation = "no device";
658 explanation = "endpoint not enabled";
661 explanation = "endpoint stalled";
664 explanation = "not enough bandwidth";
667 explanation = "device disabled";
670 explanation = "device suspended";
676 explanation = "internal error";
679 explanation = "unknown status";
682 D1("%s: received USB status - %s (%d)", function, explanation, status);
685 /* Network interface functions */
687 /* called when net interface is brought up by ifconfig */
688 static int hso_net_open(struct net_device *net)
690 struct hso_net *odev = netdev_priv(net);
691 unsigned long flags = 0;
694 dev_err(&net->dev, "No net device !\n");
698 odev->skb_tx_buf = NULL;
700 /* setup environment */
701 spin_lock_irqsave(&odev->net_lock, flags);
702 odev->rx_parse_state = WAIT_IP;
703 odev->rx_buf_size = 0;
704 odev->rx_buf_missing = sizeof(struct iphdr);
705 spin_unlock_irqrestore(&odev->net_lock, flags);
707 /* We are up and running. */
708 set_bit(HSO_NET_RUNNING, &odev->flags);
709 hso_start_net_device(odev->parent);
711 /* Tell the kernel we are ready to start receiving from it */
712 netif_start_queue(net);
717 /* called when interface is brought down by ifconfig */
718 static int hso_net_close(struct net_device *net)
720 struct hso_net *odev = netdev_priv(net);
722 /* we don't need the queue anymore */
723 netif_stop_queue(net);
724 /* no longer running */
725 clear_bit(HSO_NET_RUNNING, &odev->flags);
727 hso_stop_net_device(odev->parent);
733 /* USB tells is xmit done, we should start the netqueue again */
734 static void write_bulk_callback(struct urb *urb)
736 struct hso_net *odev = urb->context;
737 int status = urb->status;
740 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
741 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
745 /* Do we still have a valid kernel network device? */
746 if (!netif_device_present(odev->net)) {
747 dev_err(&urb->dev->dev, "%s: net device not present\n",
752 /* log status, but don't act on it, we don't need to resubmit anything
755 log_usb_status(status, __func__);
757 hso_put_activity(odev->parent);
759 /* Tell the network interface we are ready for another frame */
760 netif_wake_queue(odev->net);
763 /* called by kernel when we need to transmit a packet */
764 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
766 struct hso_net *odev = netdev_priv(net);
769 /* Tell the kernel, "No more frames 'til we are done with this one." */
770 netif_stop_queue(net);
771 if (hso_get_activity(odev->parent) == -EAGAIN) {
772 odev->skb_tx_buf = skb;
777 DUMP1(skb->data, skb->len);
778 /* Copy it from kernel memory to OUR memory */
779 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
780 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
782 /* Fill in the URB for shipping it out. */
783 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
785 usb_sndbulkpipe(odev->parent->usb,
787 bEndpointAddress & 0x7F),
788 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
791 /* Deal with the Zero Length packet problem, I hope */
792 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
794 /* Send the URB on its merry way. */
795 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
797 dev_warn(&odev->parent->interface->dev,
798 "failed mux_bulk_tx_urb %d", result);
799 net->stats.tx_errors++;
800 netif_start_queue(net);
802 net->stats.tx_packets++;
803 net->stats.tx_bytes += skb->len;
804 /* And tell the kernel when the last transmit started. */
805 net->trans_start = jiffies;
812 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
814 struct hso_net *odev = netdev_priv(net);
816 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
817 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
818 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
821 static struct ethtool_ops ops = {
822 .get_drvinfo = hso_get_drvinfo,
823 .get_link = ethtool_op_get_link
826 /* called when a packet did not ack after watchdogtimeout */
827 static void hso_net_tx_timeout(struct net_device *net)
829 struct hso_net *odev = netdev_priv(net);
834 /* Tell syslog we are hosed. */
835 dev_warn(&net->dev, "Tx timed out.\n");
837 /* Tear the waiting frame off the list */
838 if (odev->mux_bulk_tx_urb
839 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
840 usb_unlink_urb(odev->mux_bulk_tx_urb);
842 /* Update statistics */
843 net->stats.tx_errors++;
846 /* make a real packet from the received USB buffer */
847 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
848 unsigned int count, unsigned char is_eop)
850 unsigned short temp_bytes;
851 unsigned short buffer_offset = 0;
852 unsigned short frame_len;
853 unsigned char *tmp_rx_buf;
856 D1("Rx %d bytes", count);
857 DUMP(ip_pkt, min(128, (int)count));
860 switch (odev->rx_parse_state) {
862 /* waiting for IP header. */
863 /* wanted bytes - size of ip header */
866 odev->rx_buf_missing) ? count : odev->
869 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
870 odev->rx_buf_size, ip_pkt + buffer_offset,
873 odev->rx_buf_size += temp_bytes;
874 buffer_offset += temp_bytes;
875 odev->rx_buf_missing -= temp_bytes;
878 if (!odev->rx_buf_missing) {
879 /* header is complete allocate an sk_buffer and
880 * continue to WAIT_DATA */
881 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
883 if ((frame_len > DEFAULT_MRU) ||
884 (frame_len < sizeof(struct iphdr))) {
885 dev_err(&odev->net->dev,
886 "Invalid frame (%d) length\n",
888 odev->rx_parse_state = WAIT_SYNC;
891 /* Allocate an sk_buff */
892 odev->skb_rx_buf = dev_alloc_skb(frame_len);
893 if (!odev->skb_rx_buf) {
894 /* We got no receive buffer. */
895 D1("could not allocate memory");
896 odev->rx_parse_state = WAIT_SYNC;
899 /* Here's where it came from */
900 odev->skb_rx_buf->dev = odev->net;
902 /* Copy what we got so far. make room for iphdr
905 skb_put(odev->skb_rx_buf,
906 sizeof(struct iphdr));
907 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
908 sizeof(struct iphdr));
911 odev->rx_buf_size = sizeof(struct iphdr);
913 /* Filip actually use .tot_len */
914 odev->rx_buf_missing =
915 frame_len - sizeof(struct iphdr);
916 odev->rx_parse_state = WAIT_DATA;
921 temp_bytes = (count < odev->rx_buf_missing)
922 ? count : odev->rx_buf_missing;
924 /* Copy the rest of the bytes that are left in the
925 * buffer into the waiting sk_buf. */
926 /* Make room for temp_bytes after tail. */
927 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
928 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
930 odev->rx_buf_missing -= temp_bytes;
932 buffer_offset += temp_bytes;
933 odev->rx_buf_size += temp_bytes;
934 if (!odev->rx_buf_missing) {
935 /* Packet is complete. Inject into stack. */
936 /* We have IP packet here */
937 odev->skb_rx_buf->protocol =
938 __constant_htons(ETH_P_IP);
940 odev->skb_rx_buf->ip_summed =
941 CHECKSUM_UNNECESSARY;
943 skb_reset_mac_header(odev->skb_rx_buf);
945 /* Ship it off to the kernel */
946 netif_rx(odev->skb_rx_buf);
947 /* No longer our buffer. */
948 odev->skb_rx_buf = NULL;
950 /* update out statistics */
951 odev->net->stats.rx_packets++;
953 odev->net->stats.rx_bytes += odev->rx_buf_size;
955 odev->rx_buf_size = 0;
956 odev->rx_buf_missing = sizeof(struct iphdr);
957 odev->rx_parse_state = WAIT_IP;
972 /* Recovery mechanism for WAIT_SYNC state. */
974 if (odev->rx_parse_state == WAIT_SYNC) {
975 odev->rx_parse_state = WAIT_IP;
976 odev->rx_buf_size = 0;
977 odev->rx_buf_missing = sizeof(struct iphdr);
982 /* Moving data from usb to kernel (in interrupt state) */
983 static void read_bulk_callback(struct urb *urb)
985 struct hso_net *odev = urb->context;
986 struct net_device *net;
988 int status = urb->status;
990 /* is al ok? (Filip: Who's Al ?) */
992 log_usb_status(status, __func__);
997 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
998 D1("BULK IN callback but driver is not active!");
1001 usb_mark_last_busy(urb->dev);
1005 if (!netif_device_present(net)) {
1006 /* Somebody killed our network interface... */
1010 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
1012 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1013 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
1014 if (((rest == 5) || (rest == 6))
1015 && !memcmp(((u8 *) urb->transfer_buffer) +
1016 urb->actual_length - 4, crc_check, 4)) {
1017 urb->actual_length -= 4;
1021 /* do we even have a packet? */
1022 if (urb->actual_length) {
1023 /* Handle the IP stream, add header and push it onto network
1024 * stack if the packet is complete. */
1025 spin_lock(&odev->net_lock);
1026 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1027 (urb->transfer_buffer_length >
1028 urb->actual_length) ? 1 : 0);
1029 spin_unlock(&odev->net_lock);
1032 /* We are done with this URB, resubmit it. Prep the USB to wait for
1033 * another frame. Reuse same as received. */
1034 usb_fill_bulk_urb(urb,
1036 usb_rcvbulkpipe(odev->parent->usb,
1038 bEndpointAddress & 0x7F),
1039 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1040 read_bulk_callback, odev);
1042 /* Give this to the USB subsystem so it can tell us when more data
1044 result = usb_submit_urb(urb, GFP_ATOMIC);
1046 dev_warn(&odev->parent->interface->dev,
1047 "%s failed submit mux_bulk_rx_urb %d", __func__,
1051 /* Serial driver functions */
1053 static void hso_init_termios(struct ktermios *termios)
1056 * The default requirements for this device are:
1059 ~(IGNBRK /* disable ignore break */
1060 | BRKINT /* disable break causes interrupt */
1061 | PARMRK /* disable mark parity errors */
1062 | ISTRIP /* disable clear high bit of input characters */
1063 | INLCR /* disable translate NL to CR */
1064 | IGNCR /* disable ignore CR */
1065 | ICRNL /* disable translate CR to NL */
1066 | IXON); /* disable enable XON/XOFF flow control */
1068 /* disable postprocess output characters */
1069 termios->c_oflag &= ~OPOST;
1072 ~(ECHO /* disable echo input characters */
1073 | ECHONL /* disable echo new line */
1074 | ICANON /* disable erase, kill, werase, and rprnt
1075 special characters */
1076 | ISIG /* disable interrupt, quit, and suspend special
1078 | IEXTEN); /* disable non-POSIX special characters */
1081 ~(CSIZE /* no size */
1082 | PARENB /* disable parity bit */
1083 | CBAUD /* clear current baud rate */
1084 | CBAUDEX); /* clear current buad rate */
1086 termios->c_cflag |= CS8; /* character size 8 bits */
1088 /* baud rate 115200 */
1089 tty_termios_encode_baud_rate(termios, 115200, 115200);
1092 static void _hso_serial_set_termios(struct tty_struct *tty,
1093 struct ktermios *old)
1095 struct hso_serial *serial = get_serial_by_tty(tty);
1096 struct ktermios *termios;
1099 printk(KERN_ERR "%s: no tty structures", __func__);
1103 D4("port %d", serial->minor);
1106 * Fix up unsupported bits
1108 termios = tty->termios;
1109 termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1112 ~(CSIZE /* no size */
1113 | PARENB /* disable parity bit */
1114 | CBAUD /* clear current baud rate */
1115 | CBAUDEX); /* clear current buad rate */
1117 termios->c_cflag |= CS8; /* character size 8 bits */
1119 /* baud rate 115200 */
1120 tty_encode_baud_rate(tty, 115200, 115200);
1123 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1126 #ifdef CONFIG_HSO_AUTOPM
1127 usb_mark_last_busy(urb->dev);
1129 /* We are done with this URB, resubmit it. Prep the USB to wait for
1131 usb_fill_bulk_urb(urb, serial->parent->usb,
1132 usb_rcvbulkpipe(serial->parent->usb,
1134 bEndpointAddress & 0x7F),
1135 urb->transfer_buffer, serial->rx_data_length,
1136 hso_std_serial_read_bulk_callback, serial);
1137 /* Give this to the USB subsystem so it can tell us when more data
1139 result = usb_submit_urb(urb, GFP_ATOMIC);
1141 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1149 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1152 struct urb *curr_urb;
1154 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1155 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1156 count = put_rxbuf_data(curr_urb, serial);
1160 serial->curr_rx_urb_idx++;
1161 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1162 serial->curr_rx_urb_idx = 0;
1163 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1168 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1173 urb = serial->rx_urb[0];
1174 if (serial->open_count > 0) {
1175 count = put_rxbuf_data(urb, serial);
1179 /* Re issue a read as long as we receive data. */
1181 if (count == 0 && ((urb->actual_length != 0) ||
1182 (serial->rx_state == RX_PENDING))) {
1183 serial->rx_state = RX_SENT;
1184 hso_mux_serial_read(serial);
1186 serial->rx_state = RX_IDLE;
1190 /* read callback for Diag and CS port */
1191 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1193 struct hso_serial *serial = urb->context;
1194 int status = urb->status;
1198 D1("serial == NULL");
1200 } else if (status) {
1201 log_usb_status(status, __func__);
1205 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1206 D1("Actual length = %d\n", urb->actual_length);
1207 DUMP1(urb->transfer_buffer, urb->actual_length);
1209 /* Anyone listening? */
1210 if (serial->open_count == 0)
1214 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1216 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1218 urb->actual_length %
1219 serial->in_endp->wMaxPacketSize;
1220 if (((rest == 5) || (rest == 6))
1221 && !memcmp(((u8 *) urb->transfer_buffer) +
1222 urb->actual_length - 4, crc_check, 4)) {
1223 urb->actual_length -= 4;
1226 /* Valid data, handle RX data */
1227 spin_lock(&serial->serial_lock);
1228 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1229 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1230 spin_unlock(&serial->serial_lock);
1231 } else if (status == -ENOENT || status == -ECONNRESET) {
1232 /* Unlinked - check for throttled port. */
1233 D2("Port %d, successfully unlinked urb", serial->minor);
1234 spin_lock(&serial->serial_lock);
1235 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1236 hso_resubmit_rx_bulk_urb(serial, urb);
1237 spin_unlock(&serial->serial_lock);
1239 D2("Port %d, status = %d for read urb", serial->minor, status);
1245 * This needs to be a tasklet otherwise we will
1246 * end up recursively calling this function.
1248 void hso_unthrottle_tasklet(struct hso_serial *serial)
1250 unsigned long flags;
1252 spin_lock_irqsave(&serial->serial_lock, flags);
1253 if ((serial->parent->port_spec & HSO_INTF_MUX))
1254 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1256 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1257 spin_unlock_irqrestore(&serial->serial_lock, flags);
1260 static void hso_unthrottle(struct tty_struct *tty)
1262 struct hso_serial *serial = get_serial_by_tty(tty);
1264 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1267 void hso_unthrottle_workfunc(struct work_struct *work)
1269 struct hso_serial *serial =
1270 container_of(work, struct hso_serial,
1271 retry_unthrottle_workqueue);
1272 hso_unthrottle_tasklet(serial);
1275 /* open the requested serial port */
1276 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1278 struct hso_serial *serial = get_serial_by_index(tty->index);
1282 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1284 tty->driver_data = NULL;
1285 D1("Failed to open port");
1289 mutex_lock(&serial->parent->mutex);
1290 result = usb_autopm_get_interface(serial->parent->interface);
1294 D1("Opening %d", serial->minor);
1295 kref_get(&serial->parent->ref);
1298 spin_lock_irq(&serial->serial_lock);
1299 tty->driver_data = serial;
1300 serial->tty = tty_kref_get(tty);
1301 spin_unlock_irq(&serial->serial_lock);
1303 /* check for port already opened, if not set the termios */
1304 serial->open_count++;
1305 if (serial->open_count == 1) {
1306 tty->low_latency = 1;
1307 serial->rx_state = RX_IDLE;
1308 /* Force default termio settings */
1309 _hso_serial_set_termios(tty, NULL);
1310 tasklet_init(&serial->unthrottle_tasklet,
1311 (void (*)(unsigned long))hso_unthrottle_tasklet,
1312 (unsigned long)serial);
1313 INIT_WORK(&serial->retry_unthrottle_workqueue,
1314 hso_unthrottle_workfunc);
1315 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1317 hso_stop_serial_device(serial->parent);
1318 serial->open_count--;
1319 kref_put(&serial->parent->ref, hso_serial_ref_free);
1322 D1("Port was already open");
1325 usb_autopm_put_interface(serial->parent->interface);
1329 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1331 mutex_unlock(&serial->parent->mutex);
1335 /* close the requested serial port */
1336 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1338 struct hso_serial *serial = tty->driver_data;
1341 D1("Closing serial port");
1343 /* Open failed, no close cleanup required */
1347 mutex_lock(&serial->parent->mutex);
1348 usb_gone = serial->parent->usb_gone;
1351 usb_autopm_get_interface(serial->parent->interface);
1353 /* reset the rts and dtr */
1354 /* do the actual close */
1355 serial->open_count--;
1356 kref_put(&serial->parent->ref, hso_serial_ref_free);
1357 if (serial->open_count <= 0) {
1358 serial->open_count = 0;
1359 spin_lock_irq(&serial->serial_lock);
1360 if (serial->tty == tty) {
1361 serial->tty->driver_data = NULL;
1365 spin_unlock_irq(&serial->serial_lock);
1367 hso_stop_serial_device(serial->parent);
1368 tasklet_kill(&serial->unthrottle_tasklet);
1369 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1373 usb_autopm_put_interface(serial->parent->interface);
1375 mutex_unlock(&serial->parent->mutex);
1378 /* close the requested serial port */
1379 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1382 struct hso_serial *serial = get_serial_by_tty(tty);
1383 int space, tx_bytes;
1384 unsigned long flags;
1387 if (serial == NULL) {
1388 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1392 spin_lock_irqsave(&serial->serial_lock, flags);
1394 space = serial->tx_data_length - serial->tx_buffer_count;
1395 tx_bytes = (count < space) ? count : space;
1400 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1401 serial->tx_buffer_count += tx_bytes;
1404 spin_unlock_irqrestore(&serial->serial_lock, flags);
1406 hso_kick_transmit(serial);
1411 /* how much room is there for writing */
1412 static int hso_serial_write_room(struct tty_struct *tty)
1414 struct hso_serial *serial = get_serial_by_tty(tty);
1416 unsigned long flags;
1418 spin_lock_irqsave(&serial->serial_lock, flags);
1419 room = serial->tx_data_length - serial->tx_buffer_count;
1420 spin_unlock_irqrestore(&serial->serial_lock, flags);
1422 /* return free room */
1426 /* setup the term */
1427 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1429 struct hso_serial *serial = get_serial_by_tty(tty);
1430 unsigned long flags;
1433 D5("Termios called with: cflags new[%d] - old[%d]",
1434 tty->termios->c_cflag, old->c_cflag);
1436 /* the actual setup */
1437 spin_lock_irqsave(&serial->serial_lock, flags);
1438 if (serial->open_count)
1439 _hso_serial_set_termios(tty, old);
1442 spin_unlock_irqrestore(&serial->serial_lock, flags);
1448 /* how many characters in the buffer */
1449 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1451 struct hso_serial *serial = get_serial_by_tty(tty);
1453 unsigned long flags;
1459 spin_lock_irqsave(&serial->serial_lock, flags);
1460 chars = serial->tx_buffer_count;
1461 spin_unlock_irqrestore(&serial->serial_lock, flags);
1465 int tiocmget_submit_urb(struct hso_serial *serial,
1466 struct hso_tiocmget *tiocmget,
1467 struct usb_device *usb)
1471 if (serial->parent->usb_gone)
1473 usb_fill_int_urb(tiocmget->urb, usb,
1476 bEndpointAddress & 0x7F),
1477 &tiocmget->serial_state_notification,
1478 sizeof(struct hso_serial_state_notification),
1479 tiocmget_intr_callback, serial,
1480 tiocmget->endp->bInterval);
1481 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1483 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1490 static void tiocmget_intr_callback(struct urb *urb)
1492 struct hso_serial *serial = urb->context;
1493 struct hso_tiocmget *tiocmget;
1494 int status = urb->status;
1495 u16 UART_state_bitmap, prev_UART_state_bitmap;
1496 struct uart_icount *icount;
1497 struct hso_serial_state_notification *serial_state_notification;
1498 struct usb_device *usb;
1504 log_usb_status(status, __func__);
1507 tiocmget = serial->tiocmget;
1510 usb = serial->parent->usb;
1511 serial_state_notification = &tiocmget->serial_state_notification;
1512 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1513 serial_state_notification->bNotification != B_NOTIFICATION ||
1514 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1515 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
1516 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1518 "hso received invalid serial state notification\n");
1519 DUMP(serial_state_notification,
1520 sizeof(hso_serial_state_notifation))
1523 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1525 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1526 icount = &tiocmget->icount;
1527 spin_lock(&serial->serial_lock);
1528 if ((UART_state_bitmap & B_OVERRUN) !=
1529 (prev_UART_state_bitmap & B_OVERRUN))
1531 if ((UART_state_bitmap & B_PARITY) !=
1532 (prev_UART_state_bitmap & B_PARITY))
1534 if ((UART_state_bitmap & B_FRAMING) !=
1535 (prev_UART_state_bitmap & B_FRAMING))
1537 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1538 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1540 if ((UART_state_bitmap & B_BREAK) !=
1541 (prev_UART_state_bitmap & B_BREAK))
1543 if ((UART_state_bitmap & B_TX_CARRIER) !=
1544 (prev_UART_state_bitmap & B_TX_CARRIER))
1546 if ((UART_state_bitmap & B_RX_CARRIER) !=
1547 (prev_UART_state_bitmap & B_RX_CARRIER))
1549 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1550 spin_unlock(&serial->serial_lock);
1551 tiocmget->intr_completed = 1;
1552 wake_up_interruptible(&tiocmget->waitq);
1554 memset(serial_state_notification, 0,
1555 sizeof(struct hso_serial_state_notification));
1556 tiocmget_submit_urb(serial,
1558 serial->parent->usb);
1562 * next few functions largely stolen from drivers/serial/serial_core.c
1564 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1565 * - mask passed in arg for lines of interest
1566 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1567 * Caller should use TIOCGICOUNT to see which one it was
1570 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1572 DECLARE_WAITQUEUE(wait, current);
1573 struct uart_icount cprev, cnow;
1574 struct hso_tiocmget *tiocmget;
1577 tiocmget = serial->tiocmget;
1581 * note the counters on entry
1583 spin_lock_irq(&serial->serial_lock);
1584 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1585 spin_unlock_irq(&serial->serial_lock);
1586 add_wait_queue(&tiocmget->waitq, &wait);
1588 spin_lock_irq(&serial->serial_lock);
1589 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1590 spin_unlock_irq(&serial->serial_lock);
1591 set_current_state(TASK_INTERRUPTIBLE);
1592 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1593 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1594 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1599 /* see if a signal did it */
1600 if (signal_pending(current)) {
1606 current->state = TASK_RUNNING;
1607 remove_wait_queue(&tiocmget->waitq, &wait);
1613 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1614 * Return: write counters to the user passed counter struct
1615 * NB: both 1->0 and 0->1 transitions are counted except for
1616 * RI where only 0->1 is counted.
1618 static int hso_get_count(struct hso_serial *serial,
1619 struct serial_icounter_struct __user *icnt)
1621 struct serial_icounter_struct icount;
1622 struct uart_icount cnow;
1623 struct hso_tiocmget *tiocmget = serial->tiocmget;
1627 spin_lock_irq(&serial->serial_lock);
1628 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1629 spin_unlock_irq(&serial->serial_lock);
1631 icount.cts = cnow.cts;
1632 icount.dsr = cnow.dsr;
1633 icount.rng = cnow.rng;
1634 icount.dcd = cnow.dcd;
1635 icount.rx = cnow.rx;
1636 icount.tx = cnow.tx;
1637 icount.frame = cnow.frame;
1638 icount.overrun = cnow.overrun;
1639 icount.parity = cnow.parity;
1640 icount.brk = cnow.brk;
1641 icount.buf_overrun = cnow.buf_overrun;
1643 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1647 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1650 struct hso_serial *serial = get_serial_by_tty(tty);
1651 struct hso_tiocmget *tiocmget;
1652 u16 UART_state_bitmap;
1656 D1("no tty structures");
1659 spin_lock_irq(&serial->serial_lock);
1660 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
1661 ((serial->dtr_state) ? TIOCM_DTR : 0);
1662 tiocmget = serial->tiocmget;
1665 UART_state_bitmap = le16_to_cpu(
1666 tiocmget->prev_UART_state_bitmap);
1667 if (UART_state_bitmap & B_RING_SIGNAL)
1668 retval |= TIOCM_RNG;
1669 if (UART_state_bitmap & B_RX_CARRIER)
1671 if (UART_state_bitmap & B_TX_CARRIER)
1672 retval |= TIOCM_DSR;
1674 spin_unlock_irq(&serial->serial_lock);
1678 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1679 unsigned int set, unsigned int clear)
1682 unsigned long flags;
1684 struct hso_serial *serial = get_serial_by_tty(tty);
1688 D1("no tty structures");
1691 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1693 spin_lock_irqsave(&serial->serial_lock, flags);
1694 if (set & TIOCM_RTS)
1695 serial->rts_state = 1;
1696 if (set & TIOCM_DTR)
1697 serial->dtr_state = 1;
1699 if (clear & TIOCM_RTS)
1700 serial->rts_state = 0;
1701 if (clear & TIOCM_DTR)
1702 serial->dtr_state = 0;
1704 if (serial->dtr_state)
1706 if (serial->rts_state)
1709 spin_unlock_irqrestore(&serial->serial_lock, flags);
1711 return usb_control_msg(serial->parent->usb,
1712 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1713 0x21, val, if_num, NULL, 0,
1714 USB_CTRL_SET_TIMEOUT);
1717 static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
1718 unsigned int cmd, unsigned long arg)
1720 struct hso_serial *serial = get_serial_by_tty(tty);
1721 void __user *uarg = (void __user *)arg;
1723 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1729 ret = hso_wait_modem_status(serial, arg);
1733 ret = hso_get_count(serial, uarg);
1743 /* starts a transmit */
1744 static void hso_kick_transmit(struct hso_serial *serial)
1747 unsigned long flags;
1750 spin_lock_irqsave(&serial->serial_lock, flags);
1751 if (!serial->tx_buffer_count)
1754 if (serial->tx_urb_used)
1757 /* Wakeup USB interface if necessary */
1758 if (hso_get_activity(serial->parent) == -EAGAIN)
1761 /* Switch pointers around to avoid memcpy */
1762 temp = serial->tx_buffer;
1763 serial->tx_buffer = serial->tx_data;
1764 serial->tx_data = temp;
1765 serial->tx_data_count = serial->tx_buffer_count;
1766 serial->tx_buffer_count = 0;
1768 /* If temp is set, it means we switched buffers */
1769 if (temp && serial->write_data) {
1770 res = serial->write_data(serial);
1772 serial->tx_urb_used = 1;
1775 spin_unlock_irqrestore(&serial->serial_lock, flags);
1778 /* make a request (for reading and writing data to muxed serial port) */
1779 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1780 struct urb *ctrl_urb,
1781 struct usb_ctrlrequest *ctrl_req,
1782 u8 *ctrl_urb_data, u32 size)
1788 if (!serial || !ctrl_urb || !ctrl_req) {
1789 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1794 ctrl_req->wValue = 0;
1795 ctrl_req->wIndex = hso_port_to_mux(port);
1796 ctrl_req->wLength = size;
1798 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1799 /* Reading command */
1800 ctrl_req->bRequestType = USB_DIR_IN |
1801 USB_TYPE_OPTION_VENDOR |
1802 USB_RECIP_INTERFACE;
1803 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1804 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1806 /* Writing command */
1807 ctrl_req->bRequestType = USB_DIR_OUT |
1808 USB_TYPE_OPTION_VENDOR |
1809 USB_RECIP_INTERFACE;
1810 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1811 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1814 D2("%s command (%02x) len: %d, port: %d",
1815 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1816 ctrl_req->bRequestType, ctrl_req->wLength, port);
1819 ctrl_urb->transfer_flags = 0;
1820 usb_fill_control_urb(ctrl_urb,
1821 serial->parent->usb,
1824 ctrl_urb_data, size, ctrl_callback, serial);
1825 /* Send it on merry way */
1826 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1828 dev_err(&ctrl_urb->dev->dev,
1829 "%s failed submit ctrl_urb %d type %d", __func__,
1838 /* called by intr_callback when read occurs */
1839 static int hso_mux_serial_read(struct hso_serial *serial)
1845 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1846 /* make the request */
1848 if (serial->num_rx_urbs != 1) {
1849 dev_err(&serial->parent->interface->dev,
1850 "ERROR: mux'd reads with multiple buffers "
1854 return mux_device_request(serial,
1855 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1856 serial->parent->port_spec & HSO_PORT_MASK,
1858 &serial->ctrl_req_rx,
1859 serial->rx_data[0], serial->rx_data_length);
1862 /* used for muxed serial port callback (muxed serial read) */
1863 static void intr_callback(struct urb *urb)
1865 struct hso_shared_int *shared_int = urb->context;
1866 struct hso_serial *serial;
1867 unsigned char *port_req;
1868 int status = urb->status;
1871 usb_mark_last_busy(urb->dev);
1879 log_usb_status(status, __func__);
1882 D4("\n--- Got intr callback 0x%02X ---", status);
1885 port_req = urb->transfer_buffer;
1886 D4(" port_req = 0x%.2X\n", *port_req);
1887 /* loop over all muxed ports to find the one sending this */
1888 for (i = 0; i < 8; i++) {
1889 /* max 8 channels on MUX */
1890 if (*port_req & (1 << i)) {
1891 serial = get_serial_by_shared_int_and_type(shared_int,
1893 if (serial != NULL) {
1894 D1("Pending read interrupt on port %d\n", i);
1895 spin_lock(&serial->serial_lock);
1896 if (serial->rx_state == RX_IDLE) {
1897 /* Setup and send a ctrl req read on
1899 if (!serial->rx_urb_filled[0]) {
1900 serial->rx_state = RX_SENT;
1901 hso_mux_serial_read(serial);
1903 serial->rx_state = RX_PENDING;
1906 D1("Already pending a read on "
1909 spin_unlock(&serial->serial_lock);
1913 /* Resubmit interrupt urb */
1914 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1917 /* called for writing to muxed serial port */
1918 static int hso_mux_serial_write_data(struct hso_serial *serial)
1923 return mux_device_request(serial,
1924 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1925 serial->parent->port_spec & HSO_PORT_MASK,
1927 &serial->ctrl_req_tx,
1928 serial->tx_data, serial->tx_data_count);
1931 /* write callback for Diag and CS port */
1932 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1934 struct hso_serial *serial = urb->context;
1935 int status = urb->status;
1936 struct tty_struct *tty;
1940 D1("serial == NULL");
1944 spin_lock(&serial->serial_lock);
1945 serial->tx_urb_used = 0;
1946 tty = tty_kref_get(serial->tty);
1947 spin_unlock(&serial->serial_lock);
1949 log_usb_status(status, __func__);
1953 hso_put_activity(serial->parent);
1958 hso_kick_transmit(serial);
1964 /* called for writing diag or CS serial port */
1965 static int hso_std_serial_write_data(struct hso_serial *serial)
1967 int count = serial->tx_data_count;
1970 usb_fill_bulk_urb(serial->tx_urb,
1971 serial->parent->usb,
1972 usb_sndbulkpipe(serial->parent->usb,
1974 bEndpointAddress & 0x7F),
1975 serial->tx_data, serial->tx_data_count,
1976 hso_std_serial_write_bulk_callback, serial);
1978 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1980 dev_warn(&serial->parent->usb->dev,
1981 "Failed to submit urb - res %d\n", result);
1988 /* callback after read or write on muxed serial port */
1989 static void ctrl_callback(struct urb *urb)
1991 struct hso_serial *serial = urb->context;
1992 struct usb_ctrlrequest *req;
1993 int status = urb->status;
1994 struct tty_struct *tty;
2000 spin_lock(&serial->serial_lock);
2001 serial->tx_urb_used = 0;
2002 tty = tty_kref_get(serial->tty);
2003 spin_unlock(&serial->serial_lock);
2005 log_usb_status(status, __func__);
2011 req = (struct usb_ctrlrequest *)(urb->setup_packet);
2012 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2013 D4("Actual length of urb = %d\n", urb->actual_length);
2014 DUMP1(urb->transfer_buffer, urb->actual_length);
2016 if (req->bRequestType ==
2017 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2018 /* response to a read command */
2019 serial->rx_urb_filled[0] = 1;
2020 spin_lock(&serial->serial_lock);
2021 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2022 spin_unlock(&serial->serial_lock);
2024 hso_put_activity(serial->parent);
2027 /* response to a write command */
2028 hso_kick_transmit(serial);
2033 /* handle RX data for serial port */
2034 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
2036 struct tty_struct *tty;
2037 int write_length_remaining = 0;
2041 if (urb == NULL || serial == NULL) {
2042 D1("serial = NULL");
2046 spin_lock(&serial->serial_lock);
2047 tty = tty_kref_get(serial->tty);
2048 spin_unlock(&serial->serial_lock);
2050 /* Push data to tty */
2052 write_length_remaining = urb->actual_length -
2053 serial->curr_rx_urb_offset;
2054 D1("data to push to tty");
2055 while (write_length_remaining) {
2056 if (test_bit(TTY_THROTTLED, &tty->flags))
2058 curr_write_len = tty_insert_flip_string
2059 (tty, urb->transfer_buffer +
2060 serial->curr_rx_urb_offset,
2061 write_length_remaining);
2062 serial->curr_rx_urb_offset += curr_write_len;
2063 write_length_remaining -= curr_write_len;
2064 tty_flip_buffer_push(tty);
2067 if (write_length_remaining == 0) {
2068 serial->curr_rx_urb_offset = 0;
2069 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2072 return write_length_remaining;
2076 /* Base driver functions */
2078 static void hso_log_port(struct hso_device *hso_dev)
2083 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2084 case HSO_PORT_CONTROL:
2085 port_type = "Control";
2088 port_type = "Application";
2093 case HSO_PORT_GPS_CONTROL:
2094 port_type = "GPS control";
2097 port_type = "Application2";
2103 port_type = "Diagnostic";
2105 case HSO_PORT_DIAG2:
2106 port_type = "Diagnostic2";
2108 case HSO_PORT_MODEM:
2109 port_type = "Modem";
2111 case HSO_PORT_NETWORK:
2112 port_type = "Network";
2115 port_type = "Unknown";
2118 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2119 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2121 sprintf(port_dev, "/dev/%s%d", tty_filename,
2122 dev2ser(hso_dev)->minor);
2124 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2125 port_type, port_dev);
2128 static int hso_start_net_device(struct hso_device *hso_dev)
2131 struct hso_net *hso_net = dev2net(hso_dev);
2136 /* send URBs for all read buffers */
2137 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2139 /* Prep a receive URB */
2140 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2142 usb_rcvbulkpipe(hso_dev->usb,
2144 bEndpointAddress & 0x7F),
2145 hso_net->mux_bulk_rx_buf_pool[i],
2146 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2149 /* Put it out there so the device can send us stuff */
2150 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2153 dev_warn(&hso_dev->usb->dev,
2154 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2161 static int hso_stop_net_device(struct hso_device *hso_dev)
2164 struct hso_net *hso_net = dev2net(hso_dev);
2169 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2170 if (hso_net->mux_bulk_rx_urb_pool[i])
2171 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2174 if (hso_net->mux_bulk_tx_urb)
2175 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2180 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2183 struct hso_serial *serial = dev2ser(hso_dev);
2188 /* If it is not the MUX port fill in and submit a bulk urb (already
2189 * allocated in hso_serial_start) */
2190 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2191 for (i = 0; i < serial->num_rx_urbs; i++) {
2192 usb_fill_bulk_urb(serial->rx_urb[i],
2193 serial->parent->usb,
2194 usb_rcvbulkpipe(serial->parent->usb,
2199 serial->rx_data_length,
2200 hso_std_serial_read_bulk_callback,
2202 result = usb_submit_urb(serial->rx_urb[i], flags);
2204 dev_warn(&serial->parent->usb->dev,
2205 "Failed to submit urb - res %d\n",
2211 mutex_lock(&serial->shared_int->shared_int_lock);
2212 if (!serial->shared_int->use_count) {
2214 hso_mux_submit_intr_urb(serial->shared_int,
2215 hso_dev->usb, flags);
2217 serial->shared_int->use_count++;
2218 mutex_unlock(&serial->shared_int->shared_int_lock);
2220 if (serial->tiocmget)
2221 tiocmget_submit_urb(serial,
2223 serial->parent->usb);
2227 static int hso_stop_serial_device(struct hso_device *hso_dev)
2230 struct hso_serial *serial = dev2ser(hso_dev);
2231 struct hso_tiocmget *tiocmget;
2236 for (i = 0; i < serial->num_rx_urbs; i++) {
2237 if (serial->rx_urb[i]) {
2238 usb_kill_urb(serial->rx_urb[i]);
2239 serial->rx_urb_filled[i] = 0;
2242 serial->curr_rx_urb_idx = 0;
2243 serial->curr_rx_urb_offset = 0;
2246 usb_kill_urb(serial->tx_urb);
2248 if (serial->shared_int) {
2249 mutex_lock(&serial->shared_int->shared_int_lock);
2250 if (serial->shared_int->use_count &&
2251 (--serial->shared_int->use_count == 0)) {
2254 urb = serial->shared_int->shared_intr_urb;
2258 mutex_unlock(&serial->shared_int->shared_int_lock);
2260 tiocmget = serial->tiocmget;
2262 wake_up_interruptible(&tiocmget->waitq);
2263 usb_kill_urb(tiocmget->urb);
2269 static void hso_serial_common_free(struct hso_serial *serial)
2273 if (serial->parent->dev)
2274 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2276 tty_unregister_device(tty_drv, serial->minor);
2278 for (i = 0; i < serial->num_rx_urbs; i++) {
2279 /* unlink and free RX URB */
2280 usb_free_urb(serial->rx_urb[i]);
2281 /* free the RX buffer */
2282 kfree(serial->rx_data[i]);
2285 /* unlink and free TX URB */
2286 usb_free_urb(serial->tx_urb);
2287 kfree(serial->tx_data);
2290 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2291 int rx_size, int tx_size)
2297 minor = get_free_serial_index();
2301 /* register our minor number */
2302 serial->parent->dev = tty_register_device(tty_drv, minor,
2303 &serial->parent->interface->dev);
2304 dev = serial->parent->dev;
2305 dev->driver_data = serial->parent;
2306 i = device_create_file(dev, &dev_attr_hsotype);
2308 /* fill in specific data for later use */
2309 serial->minor = minor;
2310 serial->magic = HSO_SERIAL_MAGIC;
2311 spin_lock_init(&serial->serial_lock);
2312 serial->num_rx_urbs = num_urbs;
2314 /* RX, allocate urb and initialize */
2316 /* prepare our RX buffer */
2317 serial->rx_data_length = rx_size;
2318 for (i = 0; i < serial->num_rx_urbs; i++) {
2319 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2320 if (!serial->rx_urb[i]) {
2321 dev_err(dev, "Could not allocate urb?\n");
2324 serial->rx_urb[i]->transfer_buffer = NULL;
2325 serial->rx_urb[i]->transfer_buffer_length = 0;
2326 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2328 if (!serial->rx_data[i]) {
2329 dev_err(dev, "%s - Out of memory\n", __func__);
2334 /* TX, allocate urb and initialize */
2335 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2336 if (!serial->tx_urb) {
2337 dev_err(dev, "Could not allocate urb?\n");
2340 serial->tx_urb->transfer_buffer = NULL;
2341 serial->tx_urb->transfer_buffer_length = 0;
2342 /* prepare our TX buffer */
2343 serial->tx_data_count = 0;
2344 serial->tx_buffer_count = 0;
2345 serial->tx_data_length = tx_size;
2346 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2347 if (!serial->tx_data) {
2348 dev_err(dev, "%s - Out of memory", __func__);
2351 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2352 if (!serial->tx_buffer) {
2353 dev_err(dev, "%s - Out of memory", __func__);
2359 hso_serial_common_free(serial);
2363 /* Frees a general hso device */
2364 static void hso_free_device(struct hso_device *hso_dev)
2369 /* Creates a general hso device */
2370 static struct hso_device *hso_create_device(struct usb_interface *intf,
2373 struct hso_device *hso_dev;
2375 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2379 hso_dev->port_spec = port_spec;
2380 hso_dev->usb = interface_to_usbdev(intf);
2381 hso_dev->interface = intf;
2382 kref_init(&hso_dev->ref);
2383 mutex_init(&hso_dev->mutex);
2385 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2386 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2391 /* Removes a network device in the network device table */
2392 static int remove_net_device(struct hso_device *hso_dev)
2396 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2397 if (network_table[i] == hso_dev) {
2398 network_table[i] = NULL;
2402 if (i == HSO_MAX_NET_DEVICES)
2407 /* Frees our network device */
2408 static void hso_free_net_device(struct hso_device *hso_dev)
2411 struct hso_net *hso_net = dev2net(hso_dev);
2417 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2418 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2419 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2421 usb_free_urb(hso_net->mux_bulk_tx_urb);
2422 kfree(hso_net->mux_bulk_tx_buf);
2424 remove_net_device(hso_net->parent);
2427 unregister_netdev(hso_net->net);
2428 free_netdev(hso_net->net);
2431 hso_free_device(hso_dev);
2434 /* initialize the network interface */
2435 static void hso_net_init(struct net_device *net)
2437 struct hso_net *hso_net = netdev_priv(net);
2439 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2441 /* fill in the other fields */
2442 net->open = hso_net_open;
2443 net->stop = hso_net_close;
2444 net->hard_start_xmit = hso_net_start_xmit;
2445 net->tx_timeout = hso_net_tx_timeout;
2446 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2447 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2448 net->type = ARPHRD_NONE;
2449 net->mtu = DEFAULT_MTU - 14;
2450 net->tx_queue_len = 10;
2451 SET_ETHTOOL_OPS(net, &ops);
2453 /* and initialize the semaphore */
2454 spin_lock_init(&hso_net->net_lock);
2457 /* Adds a network device in the network device table */
2458 static int add_net_device(struct hso_device *hso_dev)
2462 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2463 if (network_table[i] == NULL) {
2464 network_table[i] = hso_dev;
2468 if (i == HSO_MAX_NET_DEVICES)
2473 static int hso_radio_toggle(void *data, enum rfkill_state state)
2475 struct hso_device *hso_dev = data;
2476 int enabled = (state == RFKILL_STATE_ON);
2479 mutex_lock(&hso_dev->mutex);
2480 if (hso_dev->usb_gone)
2483 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2484 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2485 USB_CTRL_SET_TIMEOUT);
2486 mutex_unlock(&hso_dev->mutex);
2490 /* Creates and sets up everything for rfkill */
2491 static void hso_create_rfkill(struct hso_device *hso_dev,
2492 struct usb_interface *interface)
2494 struct hso_net *hso_net = dev2net(hso_dev);
2495 struct device *dev = &hso_net->net->dev;
2498 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2500 if (!hso_net->rfkill) {
2501 dev_err(dev, "%s - Out of memory\n", __func__);
2504 rfkn = kzalloc(20, GFP_KERNEL);
2506 rfkill_free(hso_net->rfkill);
2507 hso_net->rfkill = NULL;
2508 dev_err(dev, "%s - Out of memory\n", __func__);
2511 snprintf(rfkn, 20, "hso-%d",
2512 interface->altsetting->desc.bInterfaceNumber);
2513 hso_net->rfkill->name = rfkn;
2514 hso_net->rfkill->state = RFKILL_STATE_ON;
2515 hso_net->rfkill->data = hso_dev;
2516 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2517 if (rfkill_register(hso_net->rfkill) < 0) {
2519 hso_net->rfkill->name = NULL;
2520 rfkill_free(hso_net->rfkill);
2521 hso_net->rfkill = NULL;
2522 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2527 /* Creates our network device */
2528 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2531 struct net_device *net;
2532 struct hso_net *hso_net;
2533 struct hso_device *hso_dev;
2535 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2539 /* allocate our network device, then we can put in our private data */
2540 /* call hso_net_init to do the basic initialization */
2541 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2543 dev_err(&interface->dev, "Unable to create ethernet device\n");
2547 hso_net = netdev_priv(net);
2549 hso_dev->port_data.dev_net = hso_net;
2551 hso_net->parent = hso_dev;
2553 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2555 if (!hso_net->in_endp) {
2556 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2559 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2561 if (!hso_net->out_endp) {
2562 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2565 SET_NETDEV_DEV(net, &interface->dev);
2567 /* registering our net device */
2568 result = register_netdev(net);
2570 dev_err(&interface->dev, "Failed to register device\n");
2574 /* start allocating */
2575 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2576 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2577 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2578 dev_err(&interface->dev, "Could not allocate rx urb\n");
2581 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2583 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2584 dev_err(&interface->dev, "Could not allocate rx buf\n");
2588 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2589 if (!hso_net->mux_bulk_tx_urb) {
2590 dev_err(&interface->dev, "Could not allocate tx urb\n");
2593 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2594 if (!hso_net->mux_bulk_tx_buf) {
2595 dev_err(&interface->dev, "Could not allocate tx buf\n");
2599 add_net_device(hso_dev);
2601 hso_log_port(hso_dev);
2603 hso_create_rfkill(hso_dev, interface);
2607 hso_free_net_device(hso_dev);
2611 static void hso_free_tiomget(struct hso_serial *serial)
2613 struct hso_tiocmget *tiocmget = serial->tiocmget;
2616 if (tiocmget->urb) {
2617 usb_free_urb(tiocmget->urb);
2618 tiocmget->urb = NULL;
2620 serial->tiocmget = NULL;
2625 /* Frees an AT channel ( goes for both mux and non-mux ) */
2626 static void hso_free_serial_device(struct hso_device *hso_dev)
2628 struct hso_serial *serial = dev2ser(hso_dev);
2632 set_serial_by_index(serial->minor, NULL);
2634 hso_serial_common_free(serial);
2636 if (serial->shared_int) {
2637 mutex_lock(&serial->shared_int->shared_int_lock);
2638 if (--serial->shared_int->ref_count == 0)
2639 hso_free_shared_int(serial->shared_int);
2641 mutex_unlock(&serial->shared_int->shared_int_lock);
2643 hso_free_tiomget(serial);
2645 hso_free_device(hso_dev);
2648 /* Creates a bulk AT channel */
2649 static struct hso_device *hso_create_bulk_serial_device(
2650 struct usb_interface *interface, int port)
2652 struct hso_device *hso_dev;
2653 struct hso_serial *serial;
2655 struct hso_tiocmget *tiocmget;
2657 hso_dev = hso_create_device(interface, port);
2661 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2665 serial->parent = hso_dev;
2666 hso_dev->port_data.dev_serial = serial;
2668 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
2670 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2672 /* it isn't going to break our heart if serial->tiocmget
2673 * allocation fails don't bother checking this.
2675 if (serial->tiocmget) {
2676 tiocmget = serial->tiocmget;
2677 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2678 if (tiocmget->urb) {
2679 mutex_init(&tiocmget->mutex);
2680 init_waitqueue_head(&tiocmget->waitq);
2681 tiocmget->endp = hso_get_ep(
2683 USB_ENDPOINT_XFER_INT,
2686 hso_free_tiomget(serial);
2692 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2696 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2698 if (!serial->in_endp) {
2699 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2705 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2706 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2710 serial->write_data = hso_std_serial_write_data;
2712 /* and record this serial */
2713 set_serial_by_index(serial->minor, serial);
2715 /* setup the proc dirs and files if needed */
2716 hso_log_port(hso_dev);
2718 /* done, return it */
2722 hso_serial_common_free(serial);
2724 hso_free_tiomget(serial);
2726 hso_free_device(hso_dev);
2730 /* Creates a multiplexed AT channel */
2732 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2734 struct hso_shared_int *mux)
2736 struct hso_device *hso_dev;
2737 struct hso_serial *serial;
2740 port_spec = HSO_INTF_MUX;
2741 port_spec &= ~HSO_PORT_MASK;
2743 port_spec |= hso_mux_to_port(port);
2744 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2747 hso_dev = hso_create_device(interface, port_spec);
2751 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2755 hso_dev->port_data.dev_serial = serial;
2756 serial->parent = hso_dev;
2758 if (hso_serial_common_create
2759 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2762 serial->tx_data_length--;
2763 serial->write_data = hso_mux_serial_write_data;
2765 serial->shared_int = mux;
2766 mutex_lock(&serial->shared_int->shared_int_lock);
2767 serial->shared_int->ref_count++;
2768 mutex_unlock(&serial->shared_int->shared_int_lock);
2770 /* and record this serial */
2771 set_serial_by_index(serial->minor, serial);
2773 /* setup the proc dirs and files if needed */
2774 hso_log_port(hso_dev);
2776 /* done, return it */
2781 tty_unregister_device(tty_drv, serial->minor);
2785 hso_free_device(hso_dev);
2790 static void hso_free_shared_int(struct hso_shared_int *mux)
2792 usb_free_urb(mux->shared_intr_urb);
2793 kfree(mux->shared_intr_buf);
2794 mutex_unlock(&mux->shared_int_lock);
2799 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2801 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2806 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2808 if (!mux->intr_endp) {
2809 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2813 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2814 if (!mux->shared_intr_urb) {
2815 dev_err(&interface->dev, "Could not allocate intr urb?");
2818 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2820 if (!mux->shared_intr_buf) {
2821 dev_err(&interface->dev, "Could not allocate intr buf?");
2825 mutex_init(&mux->shared_int_lock);
2830 kfree(mux->shared_intr_buf);
2831 usb_free_urb(mux->shared_intr_urb);
2836 /* Gets the port spec for a certain interface */
2837 static int hso_get_config_data(struct usb_interface *interface)
2839 struct usb_device *usbdev = interface_to_usbdev(interface);
2841 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2844 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2845 0x86, 0xC0, 0, 0, config_data, 17,
2846 USB_CTRL_SET_TIMEOUT) != 0x11) {
2850 switch (config_data[if_num]) {
2855 result = HSO_PORT_DIAG;
2858 result = HSO_PORT_GPS;
2861 result = HSO_PORT_GPS_CONTROL;
2864 result = HSO_PORT_APP;
2867 result = HSO_PORT_APP2;
2870 result = HSO_PORT_CONTROL;
2873 result = HSO_PORT_NETWORK;
2876 result = HSO_PORT_MODEM;
2879 result = HSO_PORT_MSD;
2882 result = HSO_PORT_PCSC;
2885 result = HSO_PORT_VOICE;
2892 result |= HSO_INTF_BULK;
2894 if (config_data[16] & 0x1)
2895 result |= HSO_INFO_CRC_BUG;
2900 /* called once for each interface upon device insertion */
2901 static int hso_probe(struct usb_interface *interface,
2902 const struct usb_device_id *id)
2904 int mux, i, if_num, port_spec;
2905 unsigned char port_mask;
2906 struct hso_device *hso_dev = NULL;
2907 struct hso_shared_int *shared_int;
2908 struct hso_device *tmp_dev = NULL;
2910 if_num = interface->altsetting->desc.bInterfaceNumber;
2912 /* Get the interface/port specification from either driver_info or from
2913 * the device itself */
2914 if (id->driver_info)
2915 port_spec = ((u32 *)(id->driver_info))[if_num];
2917 port_spec = hso_get_config_data(interface);
2919 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2920 dev_err(&interface->dev, "Not our interface\n");
2923 /* Check if we need to switch to alt interfaces prior to port
2925 if (interface->num_altsetting > 1)
2926 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2927 interface->needs_remote_wakeup = 1;
2929 /* Allocate new hso device(s) */
2930 switch (port_spec & HSO_INTF_MASK) {
2932 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2933 /* Create the network device */
2935 hso_dev = hso_create_net_device(interface);
2942 if (hso_get_mux_ports(interface, &port_mask))
2943 /* TODO: de-allocate everything */
2946 shared_int = hso_create_shared_int(interface);
2950 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2951 if (port_mask & i) {
2952 hso_dev = hso_create_mux_serial_device(
2953 interface, i, shared_int);
2964 /* It's a regular bulk interface */
2965 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2967 hso_dev = hso_create_net_device(interface);
2970 hso_create_bulk_serial_device(interface, port_spec);
2978 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2980 /* save our data pointer in this device */
2981 usb_set_intfdata(interface, hso_dev);
2986 hso_free_interface(interface);
2990 /* device removed, cleaning up */
2991 static void hso_disconnect(struct usb_interface *interface)
2993 hso_free_interface(interface);
2995 /* remove reference of our private data */
2996 usb_set_intfdata(interface, NULL);
2998 usb_driver_release_interface(&hso_driver, interface);
3001 static void async_get_intf(struct work_struct *data)
3003 struct hso_device *hso_dev =
3004 container_of(data, struct hso_device, async_get_intf);
3005 usb_autopm_get_interface(hso_dev->interface);
3008 static void async_put_intf(struct work_struct *data)
3010 struct hso_device *hso_dev =
3011 container_of(data, struct hso_device, async_put_intf);
3012 usb_autopm_put_interface(hso_dev->interface);
3015 static int hso_get_activity(struct hso_device *hso_dev)
3017 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3018 if (!hso_dev->is_active) {
3019 hso_dev->is_active = 1;
3020 schedule_work(&hso_dev->async_get_intf);
3024 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3027 usb_mark_last_busy(hso_dev->usb);
3032 static int hso_put_activity(struct hso_device *hso_dev)
3034 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3035 if (hso_dev->is_active) {
3036 hso_dev->is_active = 0;
3037 schedule_work(&hso_dev->async_put_intf);
3041 hso_dev->is_active = 0;
3045 /* called by kernel when we need to suspend device */
3046 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3050 /* Stop all serial ports */
3051 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3052 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3053 result = hso_stop_serial_device(serial_table[i]);
3059 /* Stop all network ports */
3060 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3061 if (network_table[i] &&
3062 (network_table[i]->interface == iface)) {
3063 result = hso_stop_net_device(network_table[i]);
3073 /* called by kernel when we need to resume device */
3074 static int hso_resume(struct usb_interface *iface)
3077 struct hso_net *hso_net;
3079 /* Start all serial ports */
3080 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3081 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3082 if (dev2ser(serial_table[i])->open_count) {
3084 hso_start_serial_device(serial_table[i], GFP_NOIO);
3085 hso_kick_transmit(dev2ser(serial_table[i]));
3092 /* Start all network ports */
3093 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3094 if (network_table[i] &&
3095 (network_table[i]->interface == iface)) {
3096 hso_net = dev2net(network_table[i]);
3097 if (hso_net->flags & IFF_UP) {
3098 /* First transmit any lingering data,
3099 then restart the device. */
3100 if (hso_net->skb_tx_buf) {
3101 dev_dbg(&iface->dev,
3103 " lingering data\n");
3104 hso_net_start_xmit(hso_net->skb_tx_buf,
3106 hso_net->skb_tx_buf = NULL;
3108 result = hso_start_net_device(network_table[i]);
3119 static void hso_serial_ref_free(struct kref *ref)
3121 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3123 hso_free_serial_device(hso_dev);
3126 static void hso_free_interface(struct usb_interface *interface)
3128 struct hso_serial *hso_dev;
3129 struct tty_struct *tty;
3132 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3134 && (serial_table[i]->interface == interface)) {
3135 hso_dev = dev2ser(serial_table[i]);
3136 spin_lock_irq(&hso_dev->serial_lock);
3137 tty = tty_kref_get(hso_dev->tty);
3138 spin_unlock_irq(&hso_dev->serial_lock);
3141 mutex_lock(&hso_dev->parent->mutex);
3143 hso_dev->parent->usb_gone = 1;
3144 mutex_unlock(&hso_dev->parent->mutex);
3145 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3149 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3150 if (network_table[i]
3151 && (network_table[i]->interface == interface)) {
3152 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3153 /* hso_stop_net_device doesn't stop the net queue since
3154 * traffic needs to start it again when suspended */
3155 netif_stop_queue(dev2net(network_table[i])->net);
3156 hso_stop_net_device(network_table[i]);
3157 cancel_work_sync(&network_table[i]->async_put_intf);
3158 cancel_work_sync(&network_table[i]->async_get_intf);
3160 rfkill_unregister(rfk);
3161 hso_free_net_device(network_table[i]);
3166 /* Helper functions */
3168 /* Get the endpoint ! */
3169 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3173 struct usb_host_interface *iface = intf->cur_altsetting;
3174 struct usb_endpoint_descriptor *endp;
3176 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3177 endp = &iface->endpoint[i].desc;
3178 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3179 (usb_endpoint_type(endp) == type))
3186 /* Get the byte that describes which ports are enabled */
3187 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3190 struct usb_host_interface *iface = intf->cur_altsetting;
3192 if (iface->extralen == 3) {
3193 *ports = iface->extra[2];
3197 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3198 if (iface->endpoint[i].extralen == 3) {
3199 *ports = iface->endpoint[i].extra[2];
3207 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3208 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3209 struct usb_device *usb, gfp_t gfp)
3213 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3215 shared_int->intr_endp->bEndpointAddress & 0x7F),
3216 shared_int->shared_intr_buf,
3217 shared_int->intr_endp->wMaxPacketSize,
3218 intr_callback, shared_int,
3219 shared_int->intr_endp->bInterval);
3221 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3223 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
3229 /* operations setup of the serial interface */
3230 static const struct tty_operations hso_serial_ops = {
3231 .open = hso_serial_open,
3232 .close = hso_serial_close,
3233 .write = hso_serial_write,
3234 .write_room = hso_serial_write_room,
3235 .ioctl = hso_serial_ioctl,
3236 .set_termios = hso_serial_set_termios,
3237 .chars_in_buffer = hso_serial_chars_in_buffer,
3238 .tiocmget = hso_serial_tiocmget,
3239 .tiocmset = hso_serial_tiocmset,
3240 .unthrottle = hso_unthrottle
3243 static struct usb_driver hso_driver = {
3244 .name = driver_name,
3246 .disconnect = hso_disconnect,
3247 .id_table = hso_ids,
3248 .suspend = hso_suspend,
3249 .resume = hso_resume,
3250 .reset_resume = hso_resume,
3251 .supports_autosuspend = 1,
3254 static int __init hso_init(void)
3259 /* put it in the log */
3260 printk(KERN_INFO "hso: %s\n", version);
3262 /* Initialise the serial table semaphore and table */
3263 spin_lock_init(&serial_table_lock);
3264 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3265 serial_table[i] = NULL;
3267 /* allocate our driver using the proper amount of supported minors */
3268 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3272 /* fill in all needed values */
3273 tty_drv->magic = TTY_DRIVER_MAGIC;
3274 tty_drv->owner = THIS_MODULE;
3275 tty_drv->driver_name = driver_name;
3276 tty_drv->name = tty_filename;
3278 /* if major number is provided as parameter, use that one */
3280 tty_drv->major = tty_major;
3282 tty_drv->minor_start = 0;
3283 tty_drv->num = HSO_SERIAL_TTY_MINORS;
3284 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3285 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3286 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3287 tty_drv->init_termios = tty_std_termios;
3288 hso_init_termios(&tty_drv->init_termios);
3289 tty_set_operations(tty_drv, &hso_serial_ops);
3291 /* register the tty driver */
3292 result = tty_register_driver(tty_drv);
3294 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3299 /* register this module as an usb driver */
3300 result = usb_register(&hso_driver);
3302 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3304 /* cleanup serial interface */
3305 tty_unregister_driver(tty_drv);
3313 static void __exit hso_exit(void)
3315 printk(KERN_INFO "hso: unloaded\n");
3317 tty_unregister_driver(tty_drv);
3318 /* deregister the usb driver */
3319 usb_deregister(&hso_driver);
3322 /* Module definitions */
3323 module_init(hso_init);
3324 module_exit(hso_exit);
3326 MODULE_AUTHOR(MOD_AUTHOR);
3327 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3328 MODULE_LICENSE(MOD_LICENSE);
3329 MODULE_INFO(Version, DRIVER_VERSION);
3331 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3332 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3333 module_param(debug, int, S_IRUGO | S_IWUSR);
3335 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3336 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3337 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3339 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3340 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3341 module_param(disable_net, int, S_IRUGO | S_IWUSR);