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, should
45 *****************************************************************************/
47 #include <linux/sched.h>
48 #include <linux/slab.h>
49 #include <linux/init.h>
50 #include <linux/delay.h>
51 #include <linux/netdevice.h>
52 #include <linux/module.h>
53 #include <linux/ethtool.h>
54 #include <linux/usb.h>
55 #include <linux/timer.h>
56 #include <linux/tty.h>
57 #include <linux/tty_driver.h>
58 #include <linux/tty_flip.h>
59 #include <linux/kmod.h>
60 #include <linux/rfkill.h>
62 #include <linux/uaccess.h>
63 #include <linux/usb/cdc.h>
65 #include <asm/byteorder.h>
68 #define DRIVER_VERSION "1.2"
69 #define MOD_AUTHOR "Option Wireless"
70 #define MOD_DESCRIPTION "USB High Speed Option driver"
71 #define MOD_LICENSE "GPL"
73 #define HSO_MAX_NET_DEVICES 10
74 #define HSO__MAX_MTU 2048
75 #define DEFAULT_MTU 1500
76 #define DEFAULT_MRU 1500
78 #define CTRL_URB_RX_SIZE 1024
79 #define CTRL_URB_TX_SIZE 64
81 #define BULK_URB_RX_SIZE 4096
82 #define BULK_URB_TX_SIZE 8192
84 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
85 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
86 #define MUX_BULK_RX_BUF_COUNT 4
87 #define USB_TYPE_OPTION_VENDOR 0x20
89 /* These definitions are used with the struct hso_net flags element */
90 /* - use *_bit operations on it. (bit indices not values.) */
91 #define HSO_NET_RUNNING 0
93 #define HSO_NET_TX_TIMEOUT (HZ*10)
95 /* Serial port defines and structs. */
96 #define HSO_SERIAL_FLAG_RX_SENT 0
98 #define HSO_SERIAL_MAGIC 0x48534f31
100 /* Number of ttys to handle */
101 #define HSO_SERIAL_TTY_MINORS 256
103 #define MAX_RX_URBS 2
105 #define get_serial_by_tty(x) \
106 (x ? (struct hso_serial *)x->driver_data : NULL)
108 /*****************************************************************************/
109 /* Debugging functions */
110 /*****************************************************************************/
111 #define D__(lvl_, fmt, arg...) \
113 printk(lvl_ "[%d:%s]: " fmt "\n", \
114 __LINE__, __func__, ## arg); \
117 #define D_(lvl, args...) \
120 D__(KERN_INFO, args); \
123 #define D1(args...) D_(0x01, ##args)
124 #define D2(args...) D_(0x02, ##args)
125 #define D3(args...) D_(0x04, ##args)
126 #define D4(args...) D_(0x08, ##args)
127 #define D5(args...) D_(0x10, ##args)
129 /*****************************************************************************/
131 /*****************************************************************************/
132 enum pkt_parse_state
{
138 /*****************************************************************************/
140 /*****************************************************************************/
142 struct hso_shared_int
{
143 struct usb_endpoint_descriptor
*intr_endp
;
144 void *shared_intr_buf
;
145 struct urb
*shared_intr_urb
;
146 struct usb_device
*usb
;
149 struct mutex shared_int_lock
;
153 struct hso_device
*parent
;
154 struct net_device
*net
;
155 struct rfkill
*rfkill
;
157 struct usb_endpoint_descriptor
*in_endp
;
158 struct usb_endpoint_descriptor
*out_endp
;
160 struct urb
*mux_bulk_rx_urb_pool
[MUX_BULK_RX_BUF_COUNT
];
161 struct urb
*mux_bulk_tx_urb
;
162 void *mux_bulk_rx_buf_pool
[MUX_BULK_RX_BUF_COUNT
];
163 void *mux_bulk_tx_buf
;
165 struct sk_buff
*skb_rx_buf
;
166 struct sk_buff
*skb_tx_buf
;
168 enum pkt_parse_state rx_parse_state
;
171 unsigned short rx_buf_size
;
172 unsigned short rx_buf_missing
;
173 struct iphdr rx_ip_hdr
;
179 struct hso_device
*parent
;
183 struct hso_shared_int
*shared_int
;
185 /* rx/tx urb could be either a bulk urb or a control urb depending
186 on which serial port it is used on. */
187 struct urb
*rx_urb
[MAX_RX_URBS
];
189 u8
*rx_data
[MAX_RX_URBS
];
190 u16 rx_data_length
; /* should contain allocated length */
195 u16 tx_data_length
; /* should contain allocated length */
198 struct usb_ctrlrequest ctrl_req_tx
;
199 struct usb_ctrlrequest ctrl_req_rx
;
201 struct usb_endpoint_descriptor
*in_endp
;
202 struct usb_endpoint_descriptor
*out_endp
;
207 unsigned tx_urb_used
:1;
209 /* from usb_serial_port */
210 struct tty_struct
*tty
;
212 spinlock_t serial_lock
;
214 int (*write_data
) (struct hso_serial
*serial
);
219 struct hso_serial
*dev_serial
;
220 struct hso_net
*dev_net
;
227 struct work_struct async_get_intf
;
228 struct work_struct async_put_intf
;
230 struct usb_device
*usb
;
231 struct usb_interface
*interface
;
238 /* Type of interface */
239 #define HSO_INTF_MASK 0xFF00
240 #define HSO_INTF_MUX 0x0100
241 #define HSO_INTF_BULK 0x0200
244 #define HSO_PORT_MASK 0xFF
245 #define HSO_PORT_NO_PORT 0x0
246 #define HSO_PORT_CONTROL 0x1
247 #define HSO_PORT_APP 0x2
248 #define HSO_PORT_GPS 0x3
249 #define HSO_PORT_PCSC 0x4
250 #define HSO_PORT_APP2 0x5
251 #define HSO_PORT_GPS_CONTROL 0x6
252 #define HSO_PORT_MSD 0x7
253 #define HSO_PORT_VOICE 0x8
254 #define HSO_PORT_DIAG2 0x9
255 #define HSO_PORT_DIAG 0x10
256 #define HSO_PORT_MODEM 0x11
257 #define HSO_PORT_NETWORK 0x12
259 /* Additional device info */
260 #define HSO_INFO_MASK 0xFF000000
261 #define HSO_INFO_CRC_BUG 0x01000000
263 /*****************************************************************************/
265 /*****************************************************************************/
266 /* Serial driver functions */
267 static int hso_serial_tiocmset(struct tty_struct
*tty
, struct file
*file
,
268 unsigned int set
, unsigned int clear
);
269 static void ctrl_callback(struct urb
*urb
);
270 static void put_rxbuf_data(struct urb
*urb
, struct hso_serial
*serial
);
271 static void hso_kick_transmit(struct hso_serial
*serial
);
272 /* Helper functions */
273 static int hso_mux_submit_intr_urb(struct hso_shared_int
*mux_int
,
274 struct usb_device
*usb
, gfp_t gfp
);
275 static void log_usb_status(int status
, const char *function
);
276 static struct usb_endpoint_descriptor
*hso_get_ep(struct usb_interface
*intf
,
278 static int hso_get_mux_ports(struct usb_interface
*intf
, unsigned char *ports
);
279 static void hso_free_interface(struct usb_interface
*intf
);
280 static int hso_start_serial_device(struct hso_device
*hso_dev
, gfp_t flags
);
281 static int hso_stop_serial_device(struct hso_device
*hso_dev
);
282 static int hso_start_net_device(struct hso_device
*hso_dev
);
283 static void hso_free_shared_int(struct hso_shared_int
*shared_int
);
284 static int hso_stop_net_device(struct hso_device
*hso_dev
);
285 static void hso_serial_ref_free(struct kref
*ref
);
286 static void async_get_intf(struct work_struct
*data
);
287 static void async_put_intf(struct work_struct
*data
);
288 static int hso_put_activity(struct hso_device
*hso_dev
);
289 static int hso_get_activity(struct hso_device
*hso_dev
);
291 /*****************************************************************************/
292 /* Helping functions */
293 /*****************************************************************************/
297 #define dev2net(x) (x->port_data.dev_net)
298 #define dev2ser(x) (x->port_data.dev_serial)
300 /* Debugging functions */
302 static void dbg_dump(int line_count
, const char *func_name
, unsigned char *buf
,
307 printk(KERN_DEBUG
"[%d:%s]: len %d", line_count
, func_name
, len
);
309 for (i
= 0; i
< len
; i
++) {
311 printk("\n 0x%03x: ", i
);
312 printk("%02x ", (unsigned char)buf
[i
]);
317 #define DUMP(buf_, len_) \
318 dbg_dump(__LINE__, __func__, buf_, len_)
320 #define DUMP1(buf_, len_) \
326 #define DUMP(buf_, len_)
327 #define DUMP1(buf_, len_)
330 /* module parameters */
332 static int tty_major
;
333 static int disable_net
;
336 static const char driver_name
[] = "hso";
337 static const char tty_filename
[] = "ttyHS";
338 static const char *version
= __FILE__
": " DRIVER_VERSION
" " MOD_AUTHOR
;
339 /* the usb driver itself (registered in hso_init) */
340 static struct usb_driver hso_driver
;
341 /* serial structures */
342 static struct tty_driver
*tty_drv
;
343 static struct hso_device
*serial_table
[HSO_SERIAL_TTY_MINORS
];
344 static struct hso_device
*network_table
[HSO_MAX_NET_DEVICES
];
345 static spinlock_t serial_table_lock
;
346 static struct ktermios
*hso_serial_termios
[HSO_SERIAL_TTY_MINORS
];
347 static struct ktermios
*hso_serial_termios_locked
[HSO_SERIAL_TTY_MINORS
];
349 static const s32 default_port_spec
[] = {
350 HSO_INTF_MUX
| HSO_PORT_NETWORK
,
351 HSO_INTF_BULK
| HSO_PORT_DIAG
,
352 HSO_INTF_BULK
| HSO_PORT_MODEM
,
356 static const s32 icon321_port_spec
[] = {
357 HSO_INTF_MUX
| HSO_PORT_NETWORK
,
358 HSO_INTF_BULK
| HSO_PORT_DIAG2
,
359 HSO_INTF_BULK
| HSO_PORT_MODEM
,
360 HSO_INTF_BULK
| HSO_PORT_DIAG
,
364 #define default_port_device(vendor, product) \
365 USB_DEVICE(vendor, product), \
366 .driver_info = (kernel_ulong_t)default_port_spec
368 #define icon321_port_device(vendor, product) \
369 USB_DEVICE(vendor, product), \
370 .driver_info = (kernel_ulong_t)icon321_port_spec
372 /* list of devices we support */
373 static const struct usb_device_id hso_ids
[] = {
374 {default_port_device(0x0af0, 0x6711)},
375 {default_port_device(0x0af0, 0x6731)},
376 {default_port_device(0x0af0, 0x6751)},
377 {default_port_device(0x0af0, 0x6771)},
378 {default_port_device(0x0af0, 0x6791)},
379 {default_port_device(0x0af0, 0x6811)},
380 {default_port_device(0x0af0, 0x6911)},
381 {default_port_device(0x0af0, 0x6951)},
382 {default_port_device(0x0af0, 0x6971)},
383 {default_port_device(0x0af0, 0x7011)},
384 {default_port_device(0x0af0, 0x7031)},
385 {default_port_device(0x0af0, 0x7051)},
386 {default_port_device(0x0af0, 0x7071)},
387 {default_port_device(0x0af0, 0x7111)},
388 {default_port_device(0x0af0, 0x7211)},
389 {default_port_device(0x0af0, 0x7251)},
390 {default_port_device(0x0af0, 0x7271)},
391 {default_port_device(0x0af0, 0x7311)},
392 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
393 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
394 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
395 {default_port_device(0x0af0, 0xd033)}, /* Icon-322 */
396 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
397 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
398 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
399 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
400 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
403 MODULE_DEVICE_TABLE(usb
, hso_ids
);
405 /* Sysfs attribute */
406 static ssize_t
hso_sysfs_show_porttype(struct device
*dev
,
407 struct device_attribute
*attr
,
410 struct hso_device
*hso_dev
= dev
->driver_data
;
416 switch (hso_dev
->port_spec
& HSO_PORT_MASK
) {
417 case HSO_PORT_CONTROL
:
418 port_name
= "Control";
421 port_name
= "Application";
424 port_name
= "Application2";
429 case HSO_PORT_GPS_CONTROL
:
430 port_name
= "GPS Control";
436 port_name
= "Diagnostic";
439 port_name
= "Diagnostic2";
444 case HSO_PORT_NETWORK
:
445 port_name
= "Network";
448 port_name
= "Unknown";
452 return sprintf(buf
, "%s\n", port_name
);
454 static DEVICE_ATTR(hsotype
, S_IRUGO
, hso_sysfs_show_porttype
, NULL
);
456 /* converts mux value to a port spec value */
457 static u32
hso_mux_to_port(int mux
)
463 result
= HSO_PORT_CONTROL
;
466 result
= HSO_PORT_APP
;
469 result
= HSO_PORT_PCSC
;
472 result
= HSO_PORT_GPS
;
475 result
= HSO_PORT_APP2
;
478 result
= HSO_PORT_NO_PORT
;
483 /* converts port spec value to a mux value */
484 static u32
hso_port_to_mux(int port
)
488 switch (port
& HSO_PORT_MASK
) {
489 case HSO_PORT_CONTROL
:
510 static struct hso_serial
*get_serial_by_shared_int_and_type(
511 struct hso_shared_int
*shared_int
,
516 port
= hso_mux_to_port(mux
);
518 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
520 && (dev2ser(serial_table
[i
])->shared_int
== shared_int
)
521 && ((serial_table
[i
]->port_spec
& HSO_PORT_MASK
) == port
)) {
522 return dev2ser(serial_table
[i
]);
529 static struct hso_serial
*get_serial_by_index(unsigned index
)
531 struct hso_serial
*serial
;
534 if (!serial_table
[index
])
536 spin_lock_irqsave(&serial_table_lock
, flags
);
537 serial
= dev2ser(serial_table
[index
]);
538 spin_unlock_irqrestore(&serial_table_lock
, flags
);
543 static int get_free_serial_index(void)
548 spin_lock_irqsave(&serial_table_lock
, flags
);
549 for (index
= 0; index
< HSO_SERIAL_TTY_MINORS
; index
++) {
550 if (serial_table
[index
] == NULL
) {
551 spin_unlock_irqrestore(&serial_table_lock
, flags
);
555 spin_unlock_irqrestore(&serial_table_lock
, flags
);
557 printk(KERN_ERR
"%s: no free serial devices in table\n", __func__
);
561 static void set_serial_by_index(unsigned index
, struct hso_serial
*serial
)
564 spin_lock_irqsave(&serial_table_lock
, flags
);
566 serial_table
[index
] = serial
->parent
;
568 serial_table
[index
] = NULL
;
569 spin_unlock_irqrestore(&serial_table_lock
, flags
);
572 /* log a meaningfull explanation of an USB status */
573 static void log_usb_status(int status
, const char *function
)
579 explanation
= "no device";
582 explanation
= "endpoint not enabled";
585 explanation
= "endpoint stalled";
588 explanation
= "not enough bandwidth";
591 explanation
= "device disabled";
594 explanation
= "device suspended";
600 explanation
= "internal error";
603 explanation
= "unknown status";
606 D1("%s: received USB status - %s (%d)", function
, explanation
, status
);
609 /* Network interface functions */
611 /* called when net interface is brought up by ifconfig */
612 static int hso_net_open(struct net_device
*net
)
614 struct hso_net
*odev
= netdev_priv(net
);
615 unsigned long flags
= 0;
618 dev_err(&net
->dev
, "No net device !\n");
622 odev
->skb_tx_buf
= NULL
;
624 /* setup environment */
625 spin_lock_irqsave(&odev
->net_lock
, flags
);
626 odev
->rx_parse_state
= WAIT_IP
;
627 odev
->rx_buf_size
= 0;
628 odev
->rx_buf_missing
= sizeof(struct iphdr
);
629 spin_unlock_irqrestore(&odev
->net_lock
, flags
);
631 hso_start_net_device(odev
->parent
);
633 /* We are up and running. */
634 set_bit(HSO_NET_RUNNING
, &odev
->flags
);
636 /* Tell the kernel we are ready to start receiving from it */
637 netif_start_queue(net
);
642 /* called when interface is brought down by ifconfig */
643 static int hso_net_close(struct net_device
*net
)
645 struct hso_net
*odev
= netdev_priv(net
);
647 /* we don't need the queue anymore */
648 netif_stop_queue(net
);
649 /* no longer running */
650 clear_bit(HSO_NET_RUNNING
, &odev
->flags
);
652 hso_stop_net_device(odev
->parent
);
658 /* USB tells is xmit done, we should start the netqueue again */
659 static void write_bulk_callback(struct urb
*urb
)
661 struct hso_net
*odev
= urb
->context
;
662 int status
= urb
->status
;
665 if (!odev
|| !test_bit(HSO_NET_RUNNING
, &odev
->flags
)) {
666 dev_err(&urb
->dev
->dev
, "%s: device not running\n", __func__
);
670 /* Do we still have a valid kernel network device? */
671 if (!netif_device_present(odev
->net
)) {
672 dev_err(&urb
->dev
->dev
, "%s: net device not present\n",
677 /* log status, but don't act on it, we don't need to resubmit anything
680 log_usb_status(status
, __func__
);
682 hso_put_activity(odev
->parent
);
684 /* Tell the network interface we are ready for another frame */
685 netif_wake_queue(odev
->net
);
688 /* called by kernel when we need to transmit a packet */
689 static int hso_net_start_xmit(struct sk_buff
*skb
, struct net_device
*net
)
691 struct hso_net
*odev
= netdev_priv(net
);
694 /* Tell the kernel, "No more frames 'til we are done with this one." */
695 netif_stop_queue(net
);
696 if (hso_get_activity(odev
->parent
) == -EAGAIN
) {
697 odev
->skb_tx_buf
= skb
;
702 DUMP1(skb
->data
, skb
->len
);
703 /* Copy it from kernel memory to OUR memory */
704 memcpy(odev
->mux_bulk_tx_buf
, skb
->data
, skb
->len
);
705 D1("len: %d/%d", skb
->len
, MUX_BULK_TX_BUF_SIZE
);
707 /* Fill in the URB for shipping it out. */
708 usb_fill_bulk_urb(odev
->mux_bulk_tx_urb
,
710 usb_sndbulkpipe(odev
->parent
->usb
,
712 bEndpointAddress
& 0x7F),
713 odev
->mux_bulk_tx_buf
, skb
->len
, write_bulk_callback
,
716 /* Deal with the Zero Length packet problem, I hope */
717 odev
->mux_bulk_tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
719 /* Send the URB on its merry way. */
720 result
= usb_submit_urb(odev
->mux_bulk_tx_urb
, GFP_ATOMIC
);
722 dev_warn(&odev
->parent
->interface
->dev
,
723 "failed mux_bulk_tx_urb %d", result
);
724 net
->stats
.tx_errors
++;
725 netif_start_queue(net
);
727 net
->stats
.tx_packets
++;
728 net
->stats
.tx_bytes
+= skb
->len
;
729 /* And tell the kernel when the last transmit started. */
730 net
->trans_start
= jiffies
;
737 static void hso_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*info
)
739 struct hso_net
*odev
= netdev_priv(net
);
741 strncpy(info
->driver
, driver_name
, ETHTOOL_BUSINFO_LEN
);
742 strncpy(info
->version
, DRIVER_VERSION
, ETHTOOL_BUSINFO_LEN
);
743 usb_make_path(odev
->parent
->usb
, info
->bus_info
, sizeof info
->bus_info
);
746 static struct ethtool_ops ops
= {
747 .get_drvinfo
= hso_get_drvinfo
,
748 .get_link
= ethtool_op_get_link
751 /* called when a packet did not ack after watchdogtimeout */
752 static void hso_net_tx_timeout(struct net_device
*net
)
754 struct hso_net
*odev
= netdev_priv(net
);
759 /* Tell syslog we are hosed. */
760 dev_warn(&net
->dev
, "Tx timed out.\n");
762 /* Tear the waiting frame off the list */
763 if (odev
->mux_bulk_tx_urb
764 && (odev
->mux_bulk_tx_urb
->status
== -EINPROGRESS
))
765 usb_unlink_urb(odev
->mux_bulk_tx_urb
);
767 /* Update statistics */
768 net
->stats
.tx_errors
++;
771 /* make a real packet from the received USB buffer */
772 static void packetizeRx(struct hso_net
*odev
, unsigned char *ip_pkt
,
773 unsigned int count
, unsigned char is_eop
)
775 unsigned short temp_bytes
;
776 unsigned short buffer_offset
= 0;
777 unsigned short frame_len
;
778 unsigned char *tmp_rx_buf
;
781 D1("Rx %d bytes", count
);
782 DUMP(ip_pkt
, min(128, (int)count
));
785 switch (odev
->rx_parse_state
) {
787 /* waiting for IP header. */
788 /* wanted bytes - size of ip header */
791 odev
->rx_buf_missing
) ? count
: odev
->
794 memcpy(((unsigned char *)(&odev
->rx_ip_hdr
)) +
795 odev
->rx_buf_size
, ip_pkt
+ buffer_offset
,
798 odev
->rx_buf_size
+= temp_bytes
;
799 buffer_offset
+= temp_bytes
;
800 odev
->rx_buf_missing
-= temp_bytes
;
803 if (!odev
->rx_buf_missing
) {
804 /* header is complete allocate an sk_buffer and
805 * continue to WAIT_DATA */
806 frame_len
= ntohs(odev
->rx_ip_hdr
.tot_len
);
808 if ((frame_len
> DEFAULT_MRU
) ||
809 (frame_len
< sizeof(struct iphdr
))) {
810 dev_err(&odev
->net
->dev
,
811 "Invalid frame (%d) length\n",
813 odev
->rx_parse_state
= WAIT_SYNC
;
816 /* Allocate an sk_buff */
817 odev
->skb_rx_buf
= dev_alloc_skb(frame_len
);
818 if (!odev
->skb_rx_buf
) {
819 /* We got no receive buffer. */
820 D1("could not allocate memory");
821 odev
->rx_parse_state
= WAIT_SYNC
;
824 /* Here's where it came from */
825 odev
->skb_rx_buf
->dev
= odev
->net
;
827 /* Copy what we got so far. make room for iphdr
830 skb_put(odev
->skb_rx_buf
,
831 sizeof(struct iphdr
));
832 memcpy(tmp_rx_buf
, (char *)&(odev
->rx_ip_hdr
),
833 sizeof(struct iphdr
));
836 odev
->rx_buf_size
= sizeof(struct iphdr
);
838 /* Filip actually use .tot_len */
839 odev
->rx_buf_missing
=
840 frame_len
- sizeof(struct iphdr
);
841 odev
->rx_parse_state
= WAIT_DATA
;
846 temp_bytes
= (count
< odev
->rx_buf_missing
)
847 ? count
: odev
->rx_buf_missing
;
849 /* Copy the rest of the bytes that are left in the
850 * buffer into the waiting sk_buf. */
851 /* Make room for temp_bytes after tail. */
852 tmp_rx_buf
= skb_put(odev
->skb_rx_buf
, temp_bytes
);
853 memcpy(tmp_rx_buf
, ip_pkt
+ buffer_offset
, temp_bytes
);
855 odev
->rx_buf_missing
-= temp_bytes
;
857 buffer_offset
+= temp_bytes
;
858 odev
->rx_buf_size
+= temp_bytes
;
859 if (!odev
->rx_buf_missing
) {
860 /* Packet is complete. Inject into stack. */
861 /* We have IP packet here */
862 odev
->skb_rx_buf
->protocol
=
863 __constant_htons(ETH_P_IP
);
865 odev
->skb_rx_buf
->ip_summed
=
866 CHECKSUM_UNNECESSARY
;
868 skb_reset_mac_header(odev
->skb_rx_buf
);
870 /* Ship it off to the kernel */
871 netif_rx(odev
->skb_rx_buf
);
872 /* No longer our buffer. */
873 odev
->skb_rx_buf
= NULL
;
875 /* update out statistics */
876 odev
->net
->stats
.rx_packets
++;
878 odev
->net
->stats
.rx_bytes
+= odev
->rx_buf_size
;
880 odev
->rx_buf_size
= 0;
881 odev
->rx_buf_missing
= sizeof(struct iphdr
);
882 odev
->rx_parse_state
= WAIT_IP
;
897 /* Recovery mechanism for WAIT_SYNC state. */
899 if (odev
->rx_parse_state
== WAIT_SYNC
) {
900 odev
->rx_parse_state
= WAIT_IP
;
901 odev
->rx_buf_size
= 0;
902 odev
->rx_buf_missing
= sizeof(struct iphdr
);
907 /* Moving data from usb to kernel (in interrupt state) */
908 static void read_bulk_callback(struct urb
*urb
)
910 struct hso_net
*odev
= urb
->context
;
911 struct net_device
*net
;
913 int status
= urb
->status
;
915 /* is al ok? (Filip: Who's Al ?) */
917 log_usb_status(status
, __func__
);
922 if (!odev
|| !test_bit(HSO_NET_RUNNING
, &odev
->flags
)) {
923 D1("BULK IN callback but driver is not active!");
926 usb_mark_last_busy(urb
->dev
);
930 if (!netif_device_present(net
)) {
931 /* Somebody killed our network interface... */
935 if (odev
->parent
->port_spec
& HSO_INFO_CRC_BUG
) {
937 u8 crc_check
[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
938 rest
= urb
->actual_length
% odev
->in_endp
->wMaxPacketSize
;
939 if (((rest
== 5) || (rest
== 6))
940 && !memcmp(((u8
*) urb
->transfer_buffer
) +
941 urb
->actual_length
- 4, crc_check
, 4)) {
942 urb
->actual_length
-= 4;
946 /* do we even have a packet? */
947 if (urb
->actual_length
) {
948 /* Handle the IP stream, add header and push it onto network
949 * stack if the packet is complete. */
950 spin_lock(&odev
->net_lock
);
951 packetizeRx(odev
, urb
->transfer_buffer
, urb
->actual_length
,
952 (urb
->transfer_buffer_length
>
953 urb
->actual_length
) ? 1 : 0);
954 spin_unlock(&odev
->net_lock
);
957 /* We are done with this URB, resubmit it. Prep the USB to wait for
958 * another frame. Reuse same as received. */
959 usb_fill_bulk_urb(urb
,
961 usb_rcvbulkpipe(odev
->parent
->usb
,
963 bEndpointAddress
& 0x7F),
964 urb
->transfer_buffer
, MUX_BULK_RX_BUF_SIZE
,
965 read_bulk_callback
, odev
);
967 /* Give this to the USB subsystem so it can tell us when more data
969 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
971 dev_warn(&odev
->parent
->interface
->dev
,
972 "%s failed submit mux_bulk_rx_urb %d", __func__
,
976 /* Serial driver functions */
978 static void _hso_serial_set_termios(struct tty_struct
*tty
,
979 struct ktermios
*old
)
981 struct hso_serial
*serial
= get_serial_by_tty(tty
);
982 struct ktermios
*termios
;
984 if ((!tty
) || (!tty
->termios
) || (!serial
)) {
985 printk(KERN_ERR
"%s: no tty structures", __func__
);
989 D4("port %d", serial
->minor
);
992 * The default requirements for this device are:
994 termios
= tty
->termios
;
996 ~(IGNBRK
/* disable ignore break */
997 | BRKINT
/* disable break causes interrupt */
998 | PARMRK
/* disable mark parity errors */
999 | ISTRIP
/* disable clear high bit of input characters */
1000 | INLCR
/* disable translate NL to CR */
1001 | IGNCR
/* disable ignore CR */
1002 | ICRNL
/* disable translate CR to NL */
1003 | IXON
); /* disable enable XON/XOFF flow control */
1005 /* disable postprocess output characters */
1006 termios
->c_oflag
&= ~OPOST
;
1009 ~(ECHO
/* disable echo input characters */
1010 | ECHONL
/* disable echo new line */
1011 | ICANON
/* disable erase, kill, werase, and rprnt
1012 special characters */
1013 | ISIG
/* disable interrupt, quit, and suspend special
1015 | IEXTEN
); /* disable non-POSIX special characters */
1018 ~(CSIZE
/* no size */
1019 | PARENB
/* disable parity bit */
1020 | CBAUD
/* clear current baud rate */
1021 | CBAUDEX
); /* clear current buad rate */
1023 termios
->c_cflag
|= CS8
; /* character size 8 bits */
1025 /* baud rate 115200 */
1026 tty_encode_baud_rate(serial
->tty
, 115200, 115200);
1029 * Force low_latency on; otherwise the pushes are scheduled;
1030 * this is bad as it opens up the possibility of dropping bytes
1031 * on the floor. We don't want to drop bytes on the floor. :)
1033 serial
->tty
->low_latency
= 1;
1037 /* open the requested serial port */
1038 static int hso_serial_open(struct tty_struct
*tty
, struct file
*filp
)
1040 struct hso_serial
*serial
= get_serial_by_index(tty
->index
);
1044 if (serial
== NULL
|| serial
->magic
!= HSO_SERIAL_MAGIC
) {
1045 tty
->driver_data
= NULL
;
1046 D1("Failed to open port");
1050 mutex_lock(&serial
->parent
->mutex
);
1051 result
= usb_autopm_get_interface(serial
->parent
->interface
);
1055 D1("Opening %d", serial
->minor
);
1056 kref_get(&serial
->parent
->ref
);
1059 tty
->driver_data
= serial
;
1062 /* check for port allready opened, if not set the termios */
1063 serial
->open_count
++;
1064 if (serial
->open_count
== 1) {
1065 tty
->low_latency
= 1;
1067 /* Force default termio settings */
1068 _hso_serial_set_termios(tty
, NULL
);
1069 result
= hso_start_serial_device(serial
->parent
, GFP_KERNEL
);
1071 hso_stop_serial_device(serial
->parent
);
1072 serial
->open_count
--;
1073 kref_put(&serial
->parent
->ref
, hso_serial_ref_free
);
1076 D1("Port was already open");
1079 usb_autopm_put_interface(serial
->parent
->interface
);
1083 hso_serial_tiocmset(tty
, NULL
, TIOCM_RTS
| TIOCM_DTR
, 0);
1085 mutex_unlock(&serial
->parent
->mutex
);
1089 /* close the requested serial port */
1090 static void hso_serial_close(struct tty_struct
*tty
, struct file
*filp
)
1092 struct hso_serial
*serial
= tty
->driver_data
;
1095 D1("Closing serial port");
1097 mutex_lock(&serial
->parent
->mutex
);
1098 usb_gone
= serial
->parent
->usb_gone
;
1101 usb_autopm_get_interface(serial
->parent
->interface
);
1103 /* reset the rts and dtr */
1104 /* do the actual close */
1105 serial
->open_count
--;
1106 kref_put(&serial
->parent
->ref
, hso_serial_ref_free
);
1107 if (serial
->open_count
<= 0) {
1108 serial
->open_count
= 0;
1110 serial
->tty
->driver_data
= NULL
;
1114 hso_stop_serial_device(serial
->parent
);
1117 usb_autopm_put_interface(serial
->parent
->interface
);
1118 mutex_unlock(&serial
->parent
->mutex
);
1121 /* close the requested serial port */
1122 static int hso_serial_write(struct tty_struct
*tty
, const unsigned char *buf
,
1125 struct hso_serial
*serial
= get_serial_by_tty(tty
);
1126 int space
, tx_bytes
;
1127 unsigned long flags
;
1130 if (serial
== NULL
) {
1131 printk(KERN_ERR
"%s: serial is NULL\n", __func__
);
1135 spin_lock_irqsave(&serial
->serial_lock
, flags
);
1137 space
= serial
->tx_data_length
- serial
->tx_buffer_count
;
1138 tx_bytes
= (count
< space
) ? count
: space
;
1143 memcpy(serial
->tx_buffer
+ serial
->tx_buffer_count
, buf
, tx_bytes
);
1144 serial
->tx_buffer_count
+= tx_bytes
;
1147 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
1149 hso_kick_transmit(serial
);
1154 /* how much room is there for writing */
1155 static int hso_serial_write_room(struct tty_struct
*tty
)
1157 struct hso_serial
*serial
= get_serial_by_tty(tty
);
1159 unsigned long flags
;
1161 spin_lock_irqsave(&serial
->serial_lock
, flags
);
1162 room
= serial
->tx_data_length
- serial
->tx_buffer_count
;
1163 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
1165 /* return free room */
1169 /* setup the term */
1170 static void hso_serial_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1172 struct hso_serial
*serial
= get_serial_by_tty(tty
);
1173 unsigned long flags
;
1176 D5("Termios called with: cflags new[%d] - old[%d]",
1177 tty
->termios
->c_cflag
, old
->c_cflag
);
1179 /* the actual setup */
1180 spin_lock_irqsave(&serial
->serial_lock
, flags
);
1181 if (serial
->open_count
)
1182 _hso_serial_set_termios(tty
, old
);
1185 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
1191 /* how many characters in the buffer */
1192 static int hso_serial_chars_in_buffer(struct tty_struct
*tty
)
1194 struct hso_serial
*serial
= get_serial_by_tty(tty
);
1196 unsigned long flags
;
1202 spin_lock_irqsave(&serial
->serial_lock
, flags
);
1203 chars
= serial
->tx_buffer_count
;
1204 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
1209 static int hso_serial_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1212 struct hso_serial
*serial
= get_serial_by_tty(tty
);
1213 unsigned long flags
;
1217 D1("no tty structures");
1221 spin_lock_irqsave(&serial
->serial_lock
, flags
);
1222 value
= ((serial
->rts_state
) ? TIOCM_RTS
: 0) |
1223 ((serial
->dtr_state
) ? TIOCM_DTR
: 0);
1224 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
1229 static int hso_serial_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1230 unsigned int set
, unsigned int clear
)
1233 unsigned long flags
;
1235 struct hso_serial
*serial
= get_serial_by_tty(tty
);
1239 D1("no tty structures");
1242 if_num
= serial
->parent
->interface
->altsetting
->desc
.bInterfaceNumber
;
1244 spin_lock_irqsave(&serial
->serial_lock
, flags
);
1245 if (set
& TIOCM_RTS
)
1246 serial
->rts_state
= 1;
1247 if (set
& TIOCM_DTR
)
1248 serial
->dtr_state
= 1;
1250 if (clear
& TIOCM_RTS
)
1251 serial
->rts_state
= 0;
1252 if (clear
& TIOCM_DTR
)
1253 serial
->dtr_state
= 0;
1255 if (serial
->dtr_state
)
1257 if (serial
->rts_state
)
1260 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
1262 return usb_control_msg(serial
->parent
->usb
,
1263 usb_rcvctrlpipe(serial
->parent
->usb
, 0), 0x22,
1264 0x21, val
, if_num
, NULL
, 0,
1265 USB_CTRL_SET_TIMEOUT
);
1268 /* starts a transmit */
1269 static void hso_kick_transmit(struct hso_serial
*serial
)
1272 unsigned long flags
;
1275 spin_lock_irqsave(&serial
->serial_lock
, flags
);
1276 if (!serial
->tx_buffer_count
)
1279 if (serial
->tx_urb_used
)
1282 /* Wakeup USB interface if necessary */
1283 if (hso_get_activity(serial
->parent
) == -EAGAIN
)
1286 /* Switch pointers around to avoid memcpy */
1287 temp
= serial
->tx_buffer
;
1288 serial
->tx_buffer
= serial
->tx_data
;
1289 serial
->tx_data
= temp
;
1290 serial
->tx_data_count
= serial
->tx_buffer_count
;
1291 serial
->tx_buffer_count
= 0;
1293 /* If temp is set, it means we switched buffers */
1294 if (temp
&& serial
->write_data
) {
1295 res
= serial
->write_data(serial
);
1297 serial
->tx_urb_used
= 1;
1300 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
1303 /* make a request (for reading and writing data to muxed serial port) */
1304 static int mux_device_request(struct hso_serial
*serial
, u8 type
, u16 port
,
1305 struct urb
*ctrl_urb
,
1306 struct usb_ctrlrequest
*ctrl_req
,
1307 u8
*ctrl_urb_data
, u32 size
)
1313 if (!serial
|| !ctrl_urb
|| !ctrl_req
) {
1314 printk(KERN_ERR
"%s: Wrong arguments\n", __func__
);
1319 ctrl_req
->wValue
= 0;
1320 ctrl_req
->wIndex
= hso_port_to_mux(port
);
1321 ctrl_req
->wLength
= size
;
1323 if (type
== USB_CDC_GET_ENCAPSULATED_RESPONSE
) {
1324 /* Reading command */
1325 ctrl_req
->bRequestType
= USB_DIR_IN
|
1326 USB_TYPE_OPTION_VENDOR
|
1327 USB_RECIP_INTERFACE
;
1328 ctrl_req
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
1329 pipe
= usb_rcvctrlpipe(serial
->parent
->usb
, 0);
1331 /* Writing command */
1332 ctrl_req
->bRequestType
= USB_DIR_OUT
|
1333 USB_TYPE_OPTION_VENDOR
|
1334 USB_RECIP_INTERFACE
;
1335 ctrl_req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
1336 pipe
= usb_sndctrlpipe(serial
->parent
->usb
, 0);
1339 D2("%s command (%02x) len: %d, port: %d",
1340 type
== USB_CDC_GET_ENCAPSULATED_RESPONSE
? "Read" : "Write",
1341 ctrl_req
->bRequestType
, ctrl_req
->wLength
, port
);
1344 ctrl_urb
->transfer_flags
= 0;
1345 usb_fill_control_urb(ctrl_urb
,
1346 serial
->parent
->usb
,
1349 ctrl_urb_data
, size
, ctrl_callback
, serial
);
1350 /* Send it on merry way */
1351 result
= usb_submit_urb(ctrl_urb
, GFP_ATOMIC
);
1353 dev_err(&ctrl_urb
->dev
->dev
,
1354 "%s failed submit ctrl_urb %d type %d", __func__
,
1363 /* called by intr_callback when read occurs */
1364 static int hso_mux_serial_read(struct hso_serial
*serial
)
1370 memset(serial
->rx_data
[0], 0, CTRL_URB_RX_SIZE
);
1371 /* make the request */
1373 if (serial
->num_rx_urbs
!= 1) {
1374 dev_err(&serial
->parent
->interface
->dev
,
1375 "ERROR: mux'd reads with multiple buffers "
1379 return mux_device_request(serial
,
1380 USB_CDC_GET_ENCAPSULATED_RESPONSE
,
1381 serial
->parent
->port_spec
& HSO_PORT_MASK
,
1383 &serial
->ctrl_req_rx
,
1384 serial
->rx_data
[0], serial
->rx_data_length
);
1387 /* used for muxed serial port callback (muxed serial read) */
1388 static void intr_callback(struct urb
*urb
)
1390 struct hso_shared_int
*shared_int
= urb
->context
;
1391 struct hso_serial
*serial
;
1392 unsigned char *port_req
;
1393 int status
= urb
->status
;
1396 usb_mark_last_busy(urb
->dev
);
1404 log_usb_status(status
, __func__
);
1407 D4("\n--- Got intr callback 0x%02X ---", status
);
1410 port_req
= urb
->transfer_buffer
;
1411 D4(" port_req = 0x%.2X\n", *port_req
);
1412 /* loop over all muxed ports to find the one sending this */
1413 for (i
= 0; i
< 8; i
++) {
1414 /* max 8 channels on MUX */
1415 if (*port_req
& (1 << i
)) {
1416 serial
= get_serial_by_shared_int_and_type(shared_int
,
1418 if (serial
!= NULL
) {
1419 D1("Pending read interrupt on port %d\n", i
);
1420 if (!test_and_set_bit(HSO_SERIAL_FLAG_RX_SENT
,
1422 /* Setup and send a ctrl req read on
1424 hso_mux_serial_read(serial
);
1426 D1("Already pending a read on "
1432 /* Resubmit interrupt urb */
1433 hso_mux_submit_intr_urb(shared_int
, urb
->dev
, GFP_ATOMIC
);
1436 /* called for writing to muxed serial port */
1437 static int hso_mux_serial_write_data(struct hso_serial
*serial
)
1442 return mux_device_request(serial
,
1443 USB_CDC_SEND_ENCAPSULATED_COMMAND
,
1444 serial
->parent
->port_spec
& HSO_PORT_MASK
,
1446 &serial
->ctrl_req_tx
,
1447 serial
->tx_data
, serial
->tx_data_count
);
1450 /* write callback for Diag and CS port */
1451 static void hso_std_serial_write_bulk_callback(struct urb
*urb
)
1453 struct hso_serial
*serial
= urb
->context
;
1454 int status
= urb
->status
;
1458 D1("serial == NULL");
1462 spin_lock(&serial
->serial_lock
);
1463 serial
->tx_urb_used
= 0;
1464 spin_unlock(&serial
->serial_lock
);
1466 log_usb_status(status
, __func__
);
1469 hso_put_activity(serial
->parent
);
1471 tty_wakeup(serial
->tty
);
1472 hso_kick_transmit(serial
);
1478 /* called for writing diag or CS serial port */
1479 static int hso_std_serial_write_data(struct hso_serial
*serial
)
1481 int count
= serial
->tx_data_count
;
1484 usb_fill_bulk_urb(serial
->tx_urb
,
1485 serial
->parent
->usb
,
1486 usb_sndbulkpipe(serial
->parent
->usb
,
1488 bEndpointAddress
& 0x7F),
1489 serial
->tx_data
, serial
->tx_data_count
,
1490 hso_std_serial_write_bulk_callback
, serial
);
1492 result
= usb_submit_urb(serial
->tx_urb
, GFP_ATOMIC
);
1494 dev_warn(&serial
->parent
->usb
->dev
,
1495 "Failed to submit urb - res %d\n", result
);
1502 /* callback after read or write on muxed serial port */
1503 static void ctrl_callback(struct urb
*urb
)
1505 struct hso_serial
*serial
= urb
->context
;
1506 struct usb_ctrlrequest
*req
;
1507 int status
= urb
->status
;
1513 spin_lock(&serial
->serial_lock
);
1514 serial
->tx_urb_used
= 0;
1515 spin_unlock(&serial
->serial_lock
);
1517 log_usb_status(status
, __func__
);
1522 req
= (struct usb_ctrlrequest
*)(urb
->setup_packet
);
1523 D4("\n--- Got muxed ctrl callback 0x%02X ---", status
);
1524 D4("Actual length of urb = %d\n", urb
->actual_length
);
1525 DUMP1(urb
->transfer_buffer
, urb
->actual_length
);
1527 if (req
->bRequestType
==
1528 (USB_DIR_IN
| USB_TYPE_OPTION_VENDOR
| USB_RECIP_INTERFACE
)) {
1529 /* response to a read command */
1530 if (serial
->open_count
> 0) {
1531 /* handle RX data the normal way */
1532 put_rxbuf_data(urb
, serial
);
1535 /* Re issue a read as long as we receive data. */
1536 if (urb
->actual_length
!= 0)
1537 hso_mux_serial_read(serial
);
1539 clear_bit(HSO_SERIAL_FLAG_RX_SENT
, &serial
->flags
);
1541 hso_put_activity(serial
->parent
);
1543 tty_wakeup(serial
->tty
);
1544 /* response to a write command */
1545 hso_kick_transmit(serial
);
1549 /* handle RX data for serial port */
1550 static void put_rxbuf_data(struct urb
*urb
, struct hso_serial
*serial
)
1552 struct tty_struct
*tty
= serial
->tty
;
1555 if (urb
== NULL
|| serial
== NULL
) {
1556 D1("serial = NULL");
1560 /* Push data to tty */
1561 if (tty
&& urb
->actual_length
) {
1562 D1("data to push to tty");
1563 tty_insert_flip_string(tty
, urb
->transfer_buffer
,
1564 urb
->actual_length
);
1565 tty_flip_buffer_push(tty
);
1569 /* read callback for Diag and CS port */
1570 static void hso_std_serial_read_bulk_callback(struct urb
*urb
)
1572 struct hso_serial
*serial
= urb
->context
;
1574 int status
= urb
->status
;
1578 D1("serial == NULL");
1580 } else if (status
) {
1581 log_usb_status(status
, __func__
);
1585 D4("\n--- Got serial_read_bulk callback %02x ---", status
);
1586 D1("Actual length = %d\n", urb
->actual_length
);
1587 DUMP1(urb
->transfer_buffer
, urb
->actual_length
);
1589 /* Anyone listening? */
1590 if (serial
->open_count
== 0)
1594 if (serial
->parent
->port_spec
& HSO_INFO_CRC_BUG
) {
1596 u8 crc_check
[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1598 urb
->actual_length
%
1599 serial
->in_endp
->wMaxPacketSize
;
1600 if (((rest
== 5) || (rest
== 6))
1601 && !memcmp(((u8
*) urb
->transfer_buffer
) +
1602 urb
->actual_length
- 4, crc_check
, 4)) {
1603 urb
->actual_length
-= 4;
1606 /* Valid data, handle RX data */
1607 put_rxbuf_data(urb
, serial
);
1608 } else if (status
== -ENOENT
|| status
== -ECONNRESET
) {
1609 /* Unlinked - check for throttled port. */
1610 D2("Port %d, successfully unlinked urb", serial
->minor
);
1612 D2("Port %d, status = %d for read urb", serial
->minor
, status
);
1616 usb_mark_last_busy(urb
->dev
);
1618 /* We are done with this URB, resubmit it. Prep the USB to wait for
1620 usb_fill_bulk_urb(urb
, serial
->parent
->usb
,
1621 usb_rcvbulkpipe(serial
->parent
->usb
,
1623 bEndpointAddress
& 0x7F),
1624 urb
->transfer_buffer
, serial
->rx_data_length
,
1625 hso_std_serial_read_bulk_callback
, serial
);
1626 /* Give this to the USB subsystem so it can tell us when more data
1628 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
1630 dev_err(&urb
->dev
->dev
, "%s failed submit serial rx_urb %d",
1635 /* Base driver functions */
1637 static void hso_log_port(struct hso_device
*hso_dev
)
1642 switch (hso_dev
->port_spec
& HSO_PORT_MASK
) {
1643 case HSO_PORT_CONTROL
:
1644 port_type
= "Control";
1647 port_type
= "Application";
1652 case HSO_PORT_GPS_CONTROL
:
1653 port_type
= "GPS control";
1656 port_type
= "Application2";
1662 port_type
= "Diagnostic";
1664 case HSO_PORT_DIAG2
:
1665 port_type
= "Diagnostic2";
1667 case HSO_PORT_MODEM
:
1668 port_type
= "Modem";
1670 case HSO_PORT_NETWORK
:
1671 port_type
= "Network";
1674 port_type
= "Unknown";
1677 if ((hso_dev
->port_spec
& HSO_PORT_MASK
) == HSO_PORT_NETWORK
) {
1678 sprintf(port_dev
, "%s", dev2net(hso_dev
)->net
->name
);
1680 sprintf(port_dev
, "/dev/%s%d", tty_filename
,
1681 dev2ser(hso_dev
)->minor
);
1683 dev_dbg(&hso_dev
->interface
->dev
, "HSO: Found %s port %s\n",
1684 port_type
, port_dev
);
1687 static int hso_start_net_device(struct hso_device
*hso_dev
)
1690 struct hso_net
*hso_net
= dev2net(hso_dev
);
1695 /* send URBs for all read buffers */
1696 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1698 /* Prep a receive URB */
1699 usb_fill_bulk_urb(hso_net
->mux_bulk_rx_urb_pool
[i
],
1701 usb_rcvbulkpipe(hso_dev
->usb
,
1703 bEndpointAddress
& 0x7F),
1704 hso_net
->mux_bulk_rx_buf_pool
[i
],
1705 MUX_BULK_RX_BUF_SIZE
, read_bulk_callback
,
1708 /* Put it out there so the device can send us stuff */
1709 result
= usb_submit_urb(hso_net
->mux_bulk_rx_urb_pool
[i
],
1712 dev_warn(&hso_dev
->usb
->dev
,
1713 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__
,
1720 static int hso_stop_net_device(struct hso_device
*hso_dev
)
1723 struct hso_net
*hso_net
= dev2net(hso_dev
);
1728 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1729 if (hso_net
->mux_bulk_rx_urb_pool
[i
])
1730 usb_kill_urb(hso_net
->mux_bulk_rx_urb_pool
[i
]);
1733 if (hso_net
->mux_bulk_tx_urb
)
1734 usb_kill_urb(hso_net
->mux_bulk_tx_urb
);
1739 static int hso_start_serial_device(struct hso_device
*hso_dev
, gfp_t flags
)
1742 struct hso_serial
*serial
= dev2ser(hso_dev
);
1747 /* If it is not the MUX port fill in and submit a bulk urb (already
1748 * allocated in hso_serial_start) */
1749 if (!(serial
->parent
->port_spec
& HSO_INTF_MUX
)) {
1750 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1751 usb_fill_bulk_urb(serial
->rx_urb
[i
],
1752 serial
->parent
->usb
,
1753 usb_rcvbulkpipe(serial
->parent
->usb
,
1758 serial
->rx_data_length
,
1759 hso_std_serial_read_bulk_callback
,
1761 result
= usb_submit_urb(serial
->rx_urb
[i
], flags
);
1763 dev_warn(&serial
->parent
->usb
->dev
,
1764 "Failed to submit urb - res %d\n",
1770 mutex_lock(&serial
->shared_int
->shared_int_lock
);
1771 if (!serial
->shared_int
->use_count
) {
1773 hso_mux_submit_intr_urb(serial
->shared_int
,
1774 hso_dev
->usb
, flags
);
1776 serial
->shared_int
->use_count
++;
1777 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
1783 static int hso_stop_serial_device(struct hso_device
*hso_dev
)
1786 struct hso_serial
*serial
= dev2ser(hso_dev
);
1791 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1792 if (serial
->rx_urb
[i
])
1793 usb_kill_urb(serial
->rx_urb
[i
]);
1797 usb_kill_urb(serial
->tx_urb
);
1799 if (serial
->shared_int
) {
1800 mutex_lock(&serial
->shared_int
->shared_int_lock
);
1801 if (serial
->shared_int
->use_count
&&
1802 (--serial
->shared_int
->use_count
== 0)) {
1805 urb
= serial
->shared_int
->shared_intr_urb
;
1809 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
1815 static void hso_serial_common_free(struct hso_serial
*serial
)
1819 if (serial
->parent
->dev
)
1820 device_remove_file(serial
->parent
->dev
, &dev_attr_hsotype
);
1822 tty_unregister_device(tty_drv
, serial
->minor
);
1824 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1825 /* unlink and free RX URB */
1826 usb_free_urb(serial
->rx_urb
[i
]);
1827 /* free the RX buffer */
1828 kfree(serial
->rx_data
[i
]);
1831 /* unlink and free TX URB */
1832 usb_free_urb(serial
->tx_urb
);
1833 kfree(serial
->tx_data
);
1836 static int hso_serial_common_create(struct hso_serial
*serial
, int num_urbs
,
1837 int rx_size
, int tx_size
)
1843 minor
= get_free_serial_index();
1847 /* register our minor number */
1848 serial
->parent
->dev
= tty_register_device(tty_drv
, minor
,
1849 &serial
->parent
->interface
->dev
);
1850 dev
= serial
->parent
->dev
;
1851 dev
->driver_data
= serial
->parent
;
1852 i
= device_create_file(dev
, &dev_attr_hsotype
);
1854 /* fill in specific data for later use */
1855 serial
->minor
= minor
;
1856 serial
->magic
= HSO_SERIAL_MAGIC
;
1857 spin_lock_init(&serial
->serial_lock
);
1858 serial
->num_rx_urbs
= num_urbs
;
1860 /* RX, allocate urb and initialize */
1862 /* prepare our RX buffer */
1863 serial
->rx_data_length
= rx_size
;
1864 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1865 serial
->rx_urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
1866 if (!serial
->rx_urb
[i
]) {
1867 dev_err(dev
, "Could not allocate urb?\n");
1870 serial
->rx_urb
[i
]->transfer_buffer
= NULL
;
1871 serial
->rx_urb
[i
]->transfer_buffer_length
= 0;
1872 serial
->rx_data
[i
] = kzalloc(serial
->rx_data_length
,
1874 if (!serial
->rx_data
[i
]) {
1875 dev_err(dev
, "%s - Out of memory\n", __func__
);
1880 /* TX, allocate urb and initialize */
1881 serial
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1882 if (!serial
->tx_urb
) {
1883 dev_err(dev
, "Could not allocate urb?\n");
1886 serial
->tx_urb
->transfer_buffer
= NULL
;
1887 serial
->tx_urb
->transfer_buffer_length
= 0;
1888 /* prepare our TX buffer */
1889 serial
->tx_data_count
= 0;
1890 serial
->tx_buffer_count
= 0;
1891 serial
->tx_data_length
= tx_size
;
1892 serial
->tx_data
= kzalloc(serial
->tx_data_length
, GFP_KERNEL
);
1893 if (!serial
->tx_data
) {
1894 dev_err(dev
, "%s - Out of memory", __func__
);
1897 serial
->tx_buffer
= kzalloc(serial
->tx_data_length
, GFP_KERNEL
);
1898 if (!serial
->tx_buffer
) {
1899 dev_err(dev
, "%s - Out of memory", __func__
);
1905 hso_serial_common_free(serial
);
1909 /* Frees a general hso device */
1910 static void hso_free_device(struct hso_device
*hso_dev
)
1915 /* Creates a general hso device */
1916 static struct hso_device
*hso_create_device(struct usb_interface
*intf
,
1919 struct hso_device
*hso_dev
;
1921 hso_dev
= kzalloc(sizeof(*hso_dev
), GFP_ATOMIC
);
1925 hso_dev
->port_spec
= port_spec
;
1926 hso_dev
->usb
= interface_to_usbdev(intf
);
1927 hso_dev
->interface
= intf
;
1928 kref_init(&hso_dev
->ref
);
1929 mutex_init(&hso_dev
->mutex
);
1931 INIT_WORK(&hso_dev
->async_get_intf
, async_get_intf
);
1932 INIT_WORK(&hso_dev
->async_put_intf
, async_put_intf
);
1937 /* Removes a network device in the network device table */
1938 static int remove_net_device(struct hso_device
*hso_dev
)
1942 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
1943 if (network_table
[i
] == hso_dev
) {
1944 network_table
[i
] = NULL
;
1948 if (i
== HSO_MAX_NET_DEVICES
)
1953 /* Frees our network device */
1954 static void hso_free_net_device(struct hso_device
*hso_dev
)
1957 struct hso_net
*hso_net
= dev2net(hso_dev
);
1963 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1964 usb_free_urb(hso_net
->mux_bulk_rx_urb_pool
[i
]);
1965 kfree(hso_net
->mux_bulk_rx_buf_pool
[i
]);
1967 usb_free_urb(hso_net
->mux_bulk_tx_urb
);
1968 kfree(hso_net
->mux_bulk_tx_buf
);
1970 remove_net_device(hso_net
->parent
);
1973 unregister_netdev(hso_net
->net
);
1974 free_netdev(hso_net
->net
);
1977 hso_free_device(hso_dev
);
1980 /* initialize the network interface */
1981 static void hso_net_init(struct net_device
*net
)
1983 struct hso_net
*hso_net
= netdev_priv(net
);
1985 D1("sizeof hso_net is %d", (int)sizeof(*hso_net
));
1987 /* fill in the other fields */
1988 net
->open
= hso_net_open
;
1989 net
->stop
= hso_net_close
;
1990 net
->hard_start_xmit
= hso_net_start_xmit
;
1991 net
->tx_timeout
= hso_net_tx_timeout
;
1992 net
->watchdog_timeo
= HSO_NET_TX_TIMEOUT
;
1993 net
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
1994 net
->type
= ARPHRD_NONE
;
1995 net
->mtu
= DEFAULT_MTU
- 14;
1996 net
->tx_queue_len
= 10;
1997 SET_ETHTOOL_OPS(net
, &ops
);
1999 /* and initialize the semaphore */
2000 spin_lock_init(&hso_net
->net_lock
);
2003 /* Adds a network device in the network device table */
2004 static int add_net_device(struct hso_device
*hso_dev
)
2008 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2009 if (network_table
[i
] == NULL
) {
2010 network_table
[i
] = hso_dev
;
2014 if (i
== HSO_MAX_NET_DEVICES
)
2019 static int hso_radio_toggle(void *data
, enum rfkill_state state
)
2021 struct hso_device
*hso_dev
= data
;
2022 int enabled
= (state
== RFKILL_STATE_ON
);
2025 mutex_lock(&hso_dev
->mutex
);
2026 if (hso_dev
->usb_gone
)
2029 rv
= usb_control_msg(hso_dev
->usb
, usb_rcvctrlpipe(hso_dev
->usb
, 0),
2030 enabled
? 0x82 : 0x81, 0x40, 0, 0, NULL
, 0,
2031 USB_CTRL_SET_TIMEOUT
);
2032 mutex_unlock(&hso_dev
->mutex
);
2036 /* Creates and sets up everything for rfkill */
2037 static void hso_create_rfkill(struct hso_device
*hso_dev
,
2038 struct usb_interface
*interface
)
2040 struct hso_net
*hso_net
= dev2net(hso_dev
);
2041 struct device
*dev
= hso_dev
->dev
;
2044 hso_net
->rfkill
= rfkill_allocate(&interface_to_usbdev(interface
)->dev
,
2046 if (!hso_net
->rfkill
) {
2047 dev_err(dev
, "%s - Out of memory", __func__
);
2050 rfkn
= kzalloc(20, GFP_KERNEL
);
2052 rfkill_free(hso_net
->rfkill
);
2053 dev_err(dev
, "%s - Out of memory", __func__
);
2056 snprintf(rfkn
, 20, "hso-%d",
2057 interface
->altsetting
->desc
.bInterfaceNumber
);
2058 hso_net
->rfkill
->name
= rfkn
;
2059 hso_net
->rfkill
->state
= RFKILL_STATE_ON
;
2060 hso_net
->rfkill
->data
= hso_dev
;
2061 hso_net
->rfkill
->toggle_radio
= hso_radio_toggle
;
2062 if (rfkill_register(hso_net
->rfkill
) < 0) {
2064 hso_net
->rfkill
->name
= NULL
;
2065 rfkill_free(hso_net
->rfkill
);
2066 dev_err(dev
, "%s - Failed to register rfkill", __func__
);
2071 /* Creates our network device */
2072 static struct hso_device
*hso_create_net_device(struct usb_interface
*interface
)
2075 struct net_device
*net
;
2076 struct hso_net
*hso_net
;
2077 struct hso_device
*hso_dev
;
2079 hso_dev
= hso_create_device(interface
, HSO_INTF_MUX
| HSO_PORT_NETWORK
);
2083 /* allocate our network device, then we can put in our private data */
2084 /* call hso_net_init to do the basic initialization */
2085 net
= alloc_netdev(sizeof(struct hso_net
), "hso%d", hso_net_init
);
2087 dev_err(&interface
->dev
, "Unable to create ethernet device\n");
2091 hso_net
= netdev_priv(net
);
2093 hso_dev
->port_data
.dev_net
= hso_net
;
2095 hso_net
->parent
= hso_dev
;
2097 hso_net
->in_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
,
2099 if (!hso_net
->in_endp
) {
2100 dev_err(&interface
->dev
, "Can't find BULK IN endpoint\n");
2103 hso_net
->out_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
,
2105 if (!hso_net
->out_endp
) {
2106 dev_err(&interface
->dev
, "Can't find BULK OUT endpoint\n");
2109 SET_NETDEV_DEV(net
, &interface
->dev
);
2111 /* registering our net device */
2112 result
= register_netdev(net
);
2114 dev_err(&interface
->dev
, "Failed to register device\n");
2118 /* start allocating */
2119 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
2120 hso_net
->mux_bulk_rx_urb_pool
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
2121 if (!hso_net
->mux_bulk_rx_urb_pool
[i
]) {
2122 dev_err(&interface
->dev
, "Could not allocate rx urb\n");
2125 hso_net
->mux_bulk_rx_buf_pool
[i
] = kzalloc(MUX_BULK_RX_BUF_SIZE
,
2127 if (!hso_net
->mux_bulk_rx_buf_pool
[i
]) {
2128 dev_err(&interface
->dev
, "Could not allocate rx buf\n");
2132 hso_net
->mux_bulk_tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2133 if (!hso_net
->mux_bulk_tx_urb
) {
2134 dev_err(&interface
->dev
, "Could not allocate tx urb\n");
2137 hso_net
->mux_bulk_tx_buf
= kzalloc(MUX_BULK_TX_BUF_SIZE
, GFP_KERNEL
);
2138 if (!hso_net
->mux_bulk_tx_buf
) {
2139 dev_err(&interface
->dev
, "Could not allocate tx buf\n");
2143 add_net_device(hso_dev
);
2145 hso_log_port(hso_dev
);
2147 hso_create_rfkill(hso_dev
, interface
);
2151 hso_free_net_device(hso_dev
);
2155 /* Frees an AT channel ( goes for both mux and non-mux ) */
2156 static void hso_free_serial_device(struct hso_device
*hso_dev
)
2158 struct hso_serial
*serial
= dev2ser(hso_dev
);
2162 set_serial_by_index(serial
->minor
, NULL
);
2164 hso_serial_common_free(serial
);
2166 if (serial
->shared_int
) {
2167 mutex_lock(&serial
->shared_int
->shared_int_lock
);
2168 if (--serial
->shared_int
->ref_count
== 0)
2169 hso_free_shared_int(serial
->shared_int
);
2171 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
2174 hso_free_device(hso_dev
);
2177 /* Creates a bulk AT channel */
2178 static struct hso_device
*hso_create_bulk_serial_device(
2179 struct usb_interface
*interface
, int port
)
2181 struct hso_device
*hso_dev
;
2182 struct hso_serial
*serial
;
2185 hso_dev
= hso_create_device(interface
, port
);
2189 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
2193 serial
->parent
= hso_dev
;
2194 hso_dev
->port_data
.dev_serial
= serial
;
2196 if (port
& HSO_PORT_MODEM
)
2201 if (hso_serial_common_create(serial
, num_urbs
, BULK_URB_RX_SIZE
,
2205 serial
->in_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
,
2207 if (!serial
->in_endp
) {
2208 dev_err(&interface
->dev
, "Failed to find BULK IN ep\n");
2214 hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
))) {
2215 dev_err(&interface
->dev
, "Failed to find BULK IN ep\n");
2219 serial
->write_data
= hso_std_serial_write_data
;
2221 /* and record this serial */
2222 set_serial_by_index(serial
->minor
, serial
);
2224 /* setup the proc dirs and files if needed */
2225 hso_log_port(hso_dev
);
2227 /* done, return it */
2230 if (hso_dev
&& serial
)
2231 hso_serial_common_free(serial
);
2233 hso_free_device(hso_dev
);
2237 /* Creates a multiplexed AT channel */
2239 struct hso_device
*hso_create_mux_serial_device(struct usb_interface
*interface
,
2241 struct hso_shared_int
*mux
)
2243 struct hso_device
*hso_dev
;
2244 struct hso_serial
*serial
;
2247 port_spec
= HSO_INTF_MUX
;
2248 port_spec
&= ~HSO_PORT_MASK
;
2250 port_spec
|= hso_mux_to_port(port
);
2251 if ((port_spec
& HSO_PORT_MASK
) == HSO_PORT_NO_PORT
)
2254 hso_dev
= hso_create_device(interface
, port_spec
);
2258 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
2262 hso_dev
->port_data
.dev_serial
= serial
;
2263 serial
->parent
= hso_dev
;
2265 if (hso_serial_common_create
2266 (serial
, 1, CTRL_URB_RX_SIZE
, CTRL_URB_TX_SIZE
))
2269 serial
->tx_data_length
--;
2270 serial
->write_data
= hso_mux_serial_write_data
;
2272 serial
->shared_int
= mux
;
2273 mutex_lock(&serial
->shared_int
->shared_int_lock
);
2274 serial
->shared_int
->ref_count
++;
2275 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
2277 /* and record this serial */
2278 set_serial_by_index(serial
->minor
, serial
);
2280 /* setup the proc dirs and files if needed */
2281 hso_log_port(hso_dev
);
2283 /* done, return it */
2288 tty_unregister_device(tty_drv
, serial
->minor
);
2292 hso_free_device(hso_dev
);
2297 static void hso_free_shared_int(struct hso_shared_int
*mux
)
2299 usb_free_urb(mux
->shared_intr_urb
);
2300 kfree(mux
->shared_intr_buf
);
2301 mutex_unlock(&mux
->shared_int_lock
);
2306 struct hso_shared_int
*hso_create_shared_int(struct usb_interface
*interface
)
2308 struct hso_shared_int
*mux
= kzalloc(sizeof(*mux
), GFP_KERNEL
);
2313 mux
->intr_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_INT
,
2315 if (!mux
->intr_endp
) {
2316 dev_err(&interface
->dev
, "Can't find INT IN endpoint\n");
2320 mux
->shared_intr_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2321 if (!mux
->shared_intr_urb
) {
2322 dev_err(&interface
->dev
, "Could not allocate intr urb?");
2325 mux
->shared_intr_buf
= kzalloc(mux
->intr_endp
->wMaxPacketSize
,
2327 if (!mux
->shared_intr_buf
) {
2328 dev_err(&interface
->dev
, "Could not allocate intr buf?");
2332 mutex_init(&mux
->shared_int_lock
);
2337 kfree(mux
->shared_intr_buf
);
2338 usb_free_urb(mux
->shared_intr_urb
);
2343 /* Gets the port spec for a certain interface */
2344 static int hso_get_config_data(struct usb_interface
*interface
)
2346 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
2348 u32 if_num
= interface
->altsetting
->desc
.bInterfaceNumber
;
2351 if (usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
2352 0x86, 0xC0, 0, 0, config_data
, 17,
2353 USB_CTRL_SET_TIMEOUT
) != 0x11) {
2357 switch (config_data
[if_num
]) {
2362 result
= HSO_PORT_DIAG
;
2365 result
= HSO_PORT_GPS
;
2368 result
= HSO_PORT_GPS_CONTROL
;
2371 result
= HSO_PORT_APP
;
2374 result
= HSO_PORT_APP2
;
2377 result
= HSO_PORT_CONTROL
;
2380 result
= HSO_PORT_NETWORK
;
2383 result
= HSO_PORT_MODEM
;
2386 result
= HSO_PORT_MSD
;
2389 result
= HSO_PORT_PCSC
;
2392 result
= HSO_PORT_VOICE
;
2399 result
|= HSO_INTF_BULK
;
2401 if (config_data
[16] & 0x1)
2402 result
|= HSO_INFO_CRC_BUG
;
2407 /* called once for each interface upon device insertion */
2408 static int hso_probe(struct usb_interface
*interface
,
2409 const struct usb_device_id
*id
)
2411 int mux
, i
, if_num
, port_spec
;
2412 unsigned char port_mask
;
2413 struct hso_device
*hso_dev
= NULL
;
2414 struct hso_shared_int
*shared_int
;
2415 struct hso_device
*tmp_dev
= NULL
;
2417 if_num
= interface
->altsetting
->desc
.bInterfaceNumber
;
2419 /* Get the interface/port specification from either driver_info or from
2420 * the device itself */
2421 if (id
->driver_info
)
2422 port_spec
= ((u32
*)(id
->driver_info
))[if_num
];
2424 port_spec
= hso_get_config_data(interface
);
2426 if (interface
->cur_altsetting
->desc
.bInterfaceClass
!= 0xFF) {
2427 dev_err(&interface
->dev
, "Not our interface\n");
2430 /* Check if we need to switch to alt interfaces prior to port
2432 if (interface
->num_altsetting
> 1)
2433 usb_set_interface(interface_to_usbdev(interface
), if_num
, 1);
2434 interface
->needs_remote_wakeup
= 1;
2436 /* Allocate new hso device(s) */
2437 switch (port_spec
& HSO_INTF_MASK
) {
2439 if ((port_spec
& HSO_PORT_MASK
) == HSO_PORT_NETWORK
) {
2440 /* Create the network device */
2442 hso_dev
= hso_create_net_device(interface
);
2449 if (hso_get_mux_ports(interface
, &port_mask
))
2450 /* TODO: de-allocate everything */
2453 shared_int
= hso_create_shared_int(interface
);
2457 for (i
= 1, mux
= 0; i
< 0x100; i
= i
<< 1, mux
++) {
2458 if (port_mask
& i
) {
2459 hso_dev
= hso_create_mux_serial_device(
2460 interface
, i
, shared_int
);
2471 /* It's a regular bulk interface */
2472 if (((port_spec
& HSO_PORT_MASK
) == HSO_PORT_NETWORK
)
2474 hso_dev
= hso_create_net_device(interface
);
2477 hso_create_bulk_serial_device(interface
, port_spec
);
2485 usb_driver_claim_interface(&hso_driver
, interface
, hso_dev
);
2487 /* save our data pointer in this device */
2488 usb_set_intfdata(interface
, hso_dev
);
2493 hso_free_interface(interface
);
2497 /* device removed, cleaning up */
2498 static void hso_disconnect(struct usb_interface
*interface
)
2500 hso_free_interface(interface
);
2502 /* remove reference of our private data */
2503 usb_set_intfdata(interface
, NULL
);
2505 usb_driver_release_interface(&hso_driver
, interface
);
2508 static void async_get_intf(struct work_struct
*data
)
2510 struct hso_device
*hso_dev
=
2511 container_of(data
, struct hso_device
, async_get_intf
);
2512 usb_autopm_get_interface(hso_dev
->interface
);
2515 static void async_put_intf(struct work_struct
*data
)
2517 struct hso_device
*hso_dev
=
2518 container_of(data
, struct hso_device
, async_put_intf
);
2519 usb_autopm_put_interface(hso_dev
->interface
);
2522 static int hso_get_activity(struct hso_device
*hso_dev
)
2524 if (hso_dev
->usb
->state
== USB_STATE_SUSPENDED
) {
2525 if (!hso_dev
->is_active
) {
2526 hso_dev
->is_active
= 1;
2527 schedule_work(&hso_dev
->async_get_intf
);
2531 if (hso_dev
->usb
->state
!= USB_STATE_CONFIGURED
)
2534 usb_mark_last_busy(hso_dev
->usb
);
2539 static int hso_put_activity(struct hso_device
*hso_dev
)
2541 if (hso_dev
->usb
->state
!= USB_STATE_SUSPENDED
) {
2542 if (hso_dev
->is_active
) {
2543 hso_dev
->is_active
= 0;
2544 schedule_work(&hso_dev
->async_put_intf
);
2548 hso_dev
->is_active
= 0;
2552 /* called by kernel when we need to suspend device */
2553 static int hso_suspend(struct usb_interface
*iface
, pm_message_t message
)
2557 /* Stop all serial ports */
2558 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2559 if (serial_table
[i
] && (serial_table
[i
]->interface
== iface
)) {
2560 result
= hso_stop_serial_device(serial_table
[i
]);
2566 /* Stop all network ports */
2567 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2568 if (network_table
[i
] &&
2569 (network_table
[i
]->interface
== iface
)) {
2570 result
= hso_stop_net_device(network_table
[i
]);
2580 /* called by kernel when we need to resume device */
2581 static int hso_resume(struct usb_interface
*iface
)
2584 struct hso_net
*hso_net
;
2586 /* Start all serial ports */
2587 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2588 if (serial_table
[i
] && (serial_table
[i
]->interface
== iface
)) {
2589 if (dev2ser(serial_table
[i
])->open_count
) {
2591 hso_start_serial_device(serial_table
[i
], GFP_NOIO
);
2592 hso_kick_transmit(dev2ser(serial_table
[i
]));
2599 /* Start all network ports */
2600 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2601 if (network_table
[i
] &&
2602 (network_table
[i
]->interface
== iface
)) {
2603 hso_net
= dev2net(network_table
[i
]);
2604 /* First transmit any lingering data, then restart the
2606 if (hso_net
->skb_tx_buf
) {
2607 dev_dbg(&iface
->dev
,
2608 "Transmitting lingering data\n");
2609 hso_net_start_xmit(hso_net
->skb_tx_buf
,
2612 result
= hso_start_net_device(network_table
[i
]);
2622 static void hso_serial_ref_free(struct kref
*ref
)
2624 struct hso_device
*hso_dev
= container_of(ref
, struct hso_device
, ref
);
2626 hso_free_serial_device(hso_dev
);
2629 static void hso_free_interface(struct usb_interface
*interface
)
2631 struct hso_serial
*hso_dev
;
2634 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2636 && (serial_table
[i
]->interface
== interface
)) {
2637 hso_dev
= dev2ser(serial_table
[i
]);
2639 tty_hangup(hso_dev
->tty
);
2640 mutex_lock(&hso_dev
->parent
->mutex
);
2641 hso_dev
->parent
->usb_gone
= 1;
2642 mutex_unlock(&hso_dev
->parent
->mutex
);
2643 kref_put(&serial_table
[i
]->ref
, hso_serial_ref_free
);
2647 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2648 if (network_table
[i
]
2649 && (network_table
[i
]->interface
== interface
)) {
2650 struct rfkill
*rfk
= dev2net(network_table
[i
])->rfkill
;
2651 /* hso_stop_net_device doesn't stop the net queue since
2652 * traffic needs to start it again when suspended */
2653 netif_stop_queue(dev2net(network_table
[i
])->net
);
2654 hso_stop_net_device(network_table
[i
]);
2655 cancel_work_sync(&network_table
[i
]->async_put_intf
);
2656 cancel_work_sync(&network_table
[i
]->async_get_intf
);
2658 rfkill_unregister(rfk
);
2659 hso_free_net_device(network_table
[i
]);
2664 /* Helper functions */
2666 /* Get the endpoint ! */
2667 static struct usb_endpoint_descriptor
*hso_get_ep(struct usb_interface
*intf
,
2671 struct usb_host_interface
*iface
= intf
->cur_altsetting
;
2672 struct usb_endpoint_descriptor
*endp
;
2674 for (i
= 0; i
< iface
->desc
.bNumEndpoints
; i
++) {
2675 endp
= &iface
->endpoint
[i
].desc
;
2676 if (((endp
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == dir
) &&
2677 ((endp
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == type
))
2684 /* Get the byte that describes which ports are enabled */
2685 static int hso_get_mux_ports(struct usb_interface
*intf
, unsigned char *ports
)
2688 struct usb_host_interface
*iface
= intf
->cur_altsetting
;
2690 if (iface
->extralen
== 3) {
2691 *ports
= iface
->extra
[2];
2695 for (i
= 0; i
< iface
->desc
.bNumEndpoints
; i
++) {
2696 if (iface
->endpoint
[i
].extralen
== 3) {
2697 *ports
= iface
->endpoint
[i
].extra
[2];
2705 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2706 static int hso_mux_submit_intr_urb(struct hso_shared_int
*shared_int
,
2707 struct usb_device
*usb
, gfp_t gfp
)
2711 usb_fill_int_urb(shared_int
->shared_intr_urb
, usb
,
2713 shared_int
->intr_endp
->bEndpointAddress
& 0x7F),
2714 shared_int
->shared_intr_buf
,
2715 shared_int
->intr_endp
->wMaxPacketSize
,
2716 intr_callback
, shared_int
,
2717 shared_int
->intr_endp
->bInterval
);
2719 result
= usb_submit_urb(shared_int
->shared_intr_urb
, gfp
);
2721 dev_warn(&usb
->dev
, "%s failed mux_intr_urb %d", __func__
,
2727 /* operations setup of the serial interface */
2728 static const struct tty_operations hso_serial_ops
= {
2729 .open
= hso_serial_open
,
2730 .close
= hso_serial_close
,
2731 .write
= hso_serial_write
,
2732 .write_room
= hso_serial_write_room
,
2733 .set_termios
= hso_serial_set_termios
,
2734 .chars_in_buffer
= hso_serial_chars_in_buffer
,
2735 .tiocmget
= hso_serial_tiocmget
,
2736 .tiocmset
= hso_serial_tiocmset
,
2739 static struct usb_driver hso_driver
= {
2740 .name
= driver_name
,
2742 .disconnect
= hso_disconnect
,
2743 .id_table
= hso_ids
,
2744 .suspend
= hso_suspend
,
2745 .resume
= hso_resume
,
2746 .supports_autosuspend
= 1,
2749 static int __init
hso_init(void)
2754 /* put it in the log */
2755 printk(KERN_INFO
"hso: %s\n", version
);
2757 /* Initialise the serial table semaphore and table */
2758 spin_lock_init(&serial_table_lock
);
2759 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++)
2760 serial_table
[i
] = NULL
;
2762 /* allocate our driver using the proper amount of supported minors */
2763 tty_drv
= alloc_tty_driver(HSO_SERIAL_TTY_MINORS
);
2767 /* fill in all needed values */
2768 tty_drv
->magic
= TTY_DRIVER_MAGIC
;
2769 tty_drv
->owner
= THIS_MODULE
;
2770 tty_drv
->driver_name
= driver_name
;
2771 tty_drv
->name
= tty_filename
;
2773 /* if major number is provided as parameter, use that one */
2775 tty_drv
->major
= tty_major
;
2777 tty_drv
->minor_start
= 0;
2778 tty_drv
->num
= HSO_SERIAL_TTY_MINORS
;
2779 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
2780 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
2781 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2782 tty_drv
->init_termios
= tty_std_termios
;
2783 tty_drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2784 tty_drv
->termios
= hso_serial_termios
;
2785 tty_drv
->termios_locked
= hso_serial_termios_locked
;
2786 tty_set_operations(tty_drv
, &hso_serial_ops
);
2788 /* register the tty driver */
2789 result
= tty_register_driver(tty_drv
);
2791 printk(KERN_ERR
"%s - tty_register_driver failed(%d)\n",
2796 /* register this module as an usb driver */
2797 result
= usb_register(&hso_driver
);
2799 printk(KERN_ERR
"Could not register hso driver? error: %d\n",
2801 /* cleanup serial interface */
2802 tty_unregister_driver(tty_drv
);
2810 static void __exit
hso_exit(void)
2812 printk(KERN_INFO
"hso: unloaded\n");
2814 tty_unregister_driver(tty_drv
);
2815 /* deregister the usb driver */
2816 usb_deregister(&hso_driver
);
2819 /* Module definitions */
2820 module_init(hso_init
);
2821 module_exit(hso_exit
);
2823 MODULE_AUTHOR(MOD_AUTHOR
);
2824 MODULE_DESCRIPTION(MOD_DESCRIPTION
);
2825 MODULE_LICENSE(MOD_LICENSE
);
2826 MODULE_INFO(Version
, DRIVER_VERSION
);
2828 /* change the debug level (eg: insmod hso.ko debug=0x04) */
2829 MODULE_PARM_DESC(debug
, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2830 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
2832 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
2833 MODULE_PARM_DESC(tty_major
, "Set the major tty number");
2834 module_param(tty_major
, int, S_IRUGO
| S_IWUSR
);
2836 /* disable network interface (eg: insmod hso.ko disable_net=1) */
2837 MODULE_PARM_DESC(disable_net
, "Disable the network interface");
2838 module_param(disable_net
, int, S_IRUGO
| S_IWUSR
);