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 if (serial
->open_count
<= 0) {
1107 kref_put(&serial
->parent
->ref
, hso_serial_ref_free
);
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
);
1470 tty_wakeup(serial
->tty
);
1471 hso_kick_transmit(serial
);
1477 /* called for writing diag or CS serial port */
1478 static int hso_std_serial_write_data(struct hso_serial
*serial
)
1480 int count
= serial
->tx_data_count
;
1483 usb_fill_bulk_urb(serial
->tx_urb
,
1484 serial
->parent
->usb
,
1485 usb_sndbulkpipe(serial
->parent
->usb
,
1487 bEndpointAddress
& 0x7F),
1488 serial
->tx_data
, serial
->tx_data_count
,
1489 hso_std_serial_write_bulk_callback
, serial
);
1491 result
= usb_submit_urb(serial
->tx_urb
, GFP_ATOMIC
);
1493 dev_warn(&serial
->parent
->usb
->dev
,
1494 "Failed to submit urb - res %d\n", result
);
1501 /* callback after read or write on muxed serial port */
1502 static void ctrl_callback(struct urb
*urb
)
1504 struct hso_serial
*serial
= urb
->context
;
1505 struct usb_ctrlrequest
*req
;
1506 int status
= urb
->status
;
1512 spin_lock(&serial
->serial_lock
);
1513 serial
->tx_urb_used
= 0;
1514 spin_unlock(&serial
->serial_lock
);
1516 log_usb_status(status
, __func__
);
1521 req
= (struct usb_ctrlrequest
*)(urb
->setup_packet
);
1522 D4("\n--- Got muxed ctrl callback 0x%02X ---", status
);
1523 D4("Actual length of urb = %d\n", urb
->actual_length
);
1524 DUMP1(urb
->transfer_buffer
, urb
->actual_length
);
1526 if (req
->bRequestType
==
1527 (USB_DIR_IN
| USB_TYPE_OPTION_VENDOR
| USB_RECIP_INTERFACE
)) {
1528 /* response to a read command */
1529 if (serial
->open_count
> 0) {
1530 /* handle RX data the normal way */
1531 put_rxbuf_data(urb
, serial
);
1534 /* Re issue a read as long as we receive data. */
1535 if (urb
->actual_length
!= 0)
1536 hso_mux_serial_read(serial
);
1538 clear_bit(HSO_SERIAL_FLAG_RX_SENT
, &serial
->flags
);
1540 hso_put_activity(serial
->parent
);
1541 tty_wakeup(serial
->tty
);
1542 /* response to a write command */
1543 hso_kick_transmit(serial
);
1547 /* handle RX data for serial port */
1548 static void put_rxbuf_data(struct urb
*urb
, struct hso_serial
*serial
)
1550 struct tty_struct
*tty
= serial
->tty
;
1553 if (urb
== NULL
|| serial
== NULL
) {
1554 D1("serial = NULL");
1558 /* Push data to tty */
1559 if (tty
&& urb
->actual_length
) {
1560 D1("data to push to tty");
1561 tty_insert_flip_string(tty
, urb
->transfer_buffer
,
1562 urb
->actual_length
);
1563 tty_flip_buffer_push(tty
);
1567 /* read callback for Diag and CS port */
1568 static void hso_std_serial_read_bulk_callback(struct urb
*urb
)
1570 struct hso_serial
*serial
= urb
->context
;
1572 int status
= urb
->status
;
1576 D1("serial == NULL");
1578 } else if (status
) {
1579 log_usb_status(status
, __func__
);
1583 D4("\n--- Got serial_read_bulk callback %02x ---", status
);
1584 D1("Actual length = %d\n", urb
->actual_length
);
1585 DUMP1(urb
->transfer_buffer
, urb
->actual_length
);
1587 /* Anyone listening? */
1588 if (serial
->open_count
== 0)
1592 if (serial
->parent
->port_spec
& HSO_INFO_CRC_BUG
) {
1594 u8 crc_check
[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1596 urb
->actual_length
%
1597 serial
->in_endp
->wMaxPacketSize
;
1598 if (((rest
== 5) || (rest
== 6))
1599 && !memcmp(((u8
*) urb
->transfer_buffer
) +
1600 urb
->actual_length
- 4, crc_check
, 4)) {
1601 urb
->actual_length
-= 4;
1604 /* Valid data, handle RX data */
1605 put_rxbuf_data(urb
, serial
);
1606 } else if (status
== -ENOENT
|| status
== -ECONNRESET
) {
1607 /* Unlinked - check for throttled port. */
1608 D2("Port %d, successfully unlinked urb", serial
->minor
);
1610 D2("Port %d, status = %d for read urb", serial
->minor
, status
);
1614 usb_mark_last_busy(urb
->dev
);
1616 /* We are done with this URB, resubmit it. Prep the USB to wait for
1618 usb_fill_bulk_urb(urb
, serial
->parent
->usb
,
1619 usb_rcvbulkpipe(serial
->parent
->usb
,
1621 bEndpointAddress
& 0x7F),
1622 urb
->transfer_buffer
, serial
->rx_data_length
,
1623 hso_std_serial_read_bulk_callback
, serial
);
1624 /* Give this to the USB subsystem so it can tell us when more data
1626 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
1628 dev_err(&urb
->dev
->dev
, "%s failed submit serial rx_urb %d",
1633 /* Base driver functions */
1635 static void hso_log_port(struct hso_device
*hso_dev
)
1640 switch (hso_dev
->port_spec
& HSO_PORT_MASK
) {
1641 case HSO_PORT_CONTROL
:
1642 port_type
= "Control";
1645 port_type
= "Application";
1650 case HSO_PORT_GPS_CONTROL
:
1651 port_type
= "GPS control";
1654 port_type
= "Application2";
1660 port_type
= "Diagnostic";
1662 case HSO_PORT_DIAG2
:
1663 port_type
= "Diagnostic2";
1665 case HSO_PORT_MODEM
:
1666 port_type
= "Modem";
1668 case HSO_PORT_NETWORK
:
1669 port_type
= "Network";
1672 port_type
= "Unknown";
1675 if ((hso_dev
->port_spec
& HSO_PORT_MASK
) == HSO_PORT_NETWORK
) {
1676 sprintf(port_dev
, "%s", dev2net(hso_dev
)->net
->name
);
1678 sprintf(port_dev
, "/dev/%s%d", tty_filename
,
1679 dev2ser(hso_dev
)->minor
);
1681 dev_dbg(&hso_dev
->interface
->dev
, "HSO: Found %s port %s\n",
1682 port_type
, port_dev
);
1685 static int hso_start_net_device(struct hso_device
*hso_dev
)
1688 struct hso_net
*hso_net
= dev2net(hso_dev
);
1693 /* send URBs for all read buffers */
1694 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1696 /* Prep a receive URB */
1697 usb_fill_bulk_urb(hso_net
->mux_bulk_rx_urb_pool
[i
],
1699 usb_rcvbulkpipe(hso_dev
->usb
,
1701 bEndpointAddress
& 0x7F),
1702 hso_net
->mux_bulk_rx_buf_pool
[i
],
1703 MUX_BULK_RX_BUF_SIZE
, read_bulk_callback
,
1706 /* Put it out there so the device can send us stuff */
1707 result
= usb_submit_urb(hso_net
->mux_bulk_rx_urb_pool
[i
],
1710 dev_warn(&hso_dev
->usb
->dev
,
1711 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__
,
1718 static int hso_stop_net_device(struct hso_device
*hso_dev
)
1721 struct hso_net
*hso_net
= dev2net(hso_dev
);
1726 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1727 if (hso_net
->mux_bulk_rx_urb_pool
[i
])
1728 usb_kill_urb(hso_net
->mux_bulk_rx_urb_pool
[i
]);
1731 if (hso_net
->mux_bulk_tx_urb
)
1732 usb_kill_urb(hso_net
->mux_bulk_tx_urb
);
1737 static int hso_start_serial_device(struct hso_device
*hso_dev
, gfp_t flags
)
1740 struct hso_serial
*serial
= dev2ser(hso_dev
);
1745 /* If it is not the MUX port fill in and submit a bulk urb (already
1746 * allocated in hso_serial_start) */
1747 if (!(serial
->parent
->port_spec
& HSO_INTF_MUX
)) {
1748 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1749 usb_fill_bulk_urb(serial
->rx_urb
[i
],
1750 serial
->parent
->usb
,
1751 usb_rcvbulkpipe(serial
->parent
->usb
,
1756 serial
->rx_data_length
,
1757 hso_std_serial_read_bulk_callback
,
1759 result
= usb_submit_urb(serial
->rx_urb
[i
], flags
);
1761 dev_warn(&serial
->parent
->usb
->dev
,
1762 "Failed to submit urb - res %d\n",
1768 mutex_lock(&serial
->shared_int
->shared_int_lock
);
1769 if (!serial
->shared_int
->use_count
) {
1771 hso_mux_submit_intr_urb(serial
->shared_int
,
1772 hso_dev
->usb
, flags
);
1774 serial
->shared_int
->use_count
++;
1775 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
1781 static int hso_stop_serial_device(struct hso_device
*hso_dev
)
1784 struct hso_serial
*serial
= dev2ser(hso_dev
);
1789 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1790 if (serial
->rx_urb
[i
])
1791 usb_kill_urb(serial
->rx_urb
[i
]);
1795 usb_kill_urb(serial
->tx_urb
);
1797 if (serial
->shared_int
) {
1798 mutex_lock(&serial
->shared_int
->shared_int_lock
);
1799 if (serial
->shared_int
->use_count
&&
1800 (--serial
->shared_int
->use_count
== 0)) {
1803 urb
= serial
->shared_int
->shared_intr_urb
;
1807 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
1813 static void hso_serial_common_free(struct hso_serial
*serial
)
1817 if (serial
->parent
->dev
)
1818 device_remove_file(serial
->parent
->dev
, &dev_attr_hsotype
);
1820 tty_unregister_device(tty_drv
, serial
->minor
);
1822 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1823 /* unlink and free RX URB */
1824 usb_free_urb(serial
->rx_urb
[i
]);
1825 /* free the RX buffer */
1826 kfree(serial
->rx_data
[i
]);
1829 /* unlink and free TX URB */
1830 usb_free_urb(serial
->tx_urb
);
1831 kfree(serial
->tx_data
);
1834 static int hso_serial_common_create(struct hso_serial
*serial
, int num_urbs
,
1835 int rx_size
, int tx_size
)
1841 minor
= get_free_serial_index();
1845 /* register our minor number */
1846 serial
->parent
->dev
= tty_register_device(tty_drv
, minor
,
1847 &serial
->parent
->interface
->dev
);
1848 dev
= serial
->parent
->dev
;
1849 dev
->driver_data
= serial
->parent
;
1850 i
= device_create_file(dev
, &dev_attr_hsotype
);
1852 /* fill in specific data for later use */
1853 serial
->minor
= minor
;
1854 serial
->magic
= HSO_SERIAL_MAGIC
;
1855 spin_lock_init(&serial
->serial_lock
);
1856 serial
->num_rx_urbs
= num_urbs
;
1858 /* RX, allocate urb and initialize */
1860 /* prepare our RX buffer */
1861 serial
->rx_data_length
= rx_size
;
1862 for (i
= 0; i
< serial
->num_rx_urbs
; i
++) {
1863 serial
->rx_urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
1864 if (!serial
->rx_urb
[i
]) {
1865 dev_err(dev
, "Could not allocate urb?\n");
1868 serial
->rx_urb
[i
]->transfer_buffer
= NULL
;
1869 serial
->rx_urb
[i
]->transfer_buffer_length
= 0;
1870 serial
->rx_data
[i
] = kzalloc(serial
->rx_data_length
,
1872 if (!serial
->rx_data
[i
]) {
1873 dev_err(dev
, "%s - Out of memory\n", __func__
);
1878 /* TX, allocate urb and initialize */
1879 serial
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1880 if (!serial
->tx_urb
) {
1881 dev_err(dev
, "Could not allocate urb?\n");
1884 serial
->tx_urb
->transfer_buffer
= NULL
;
1885 serial
->tx_urb
->transfer_buffer_length
= 0;
1886 /* prepare our TX buffer */
1887 serial
->tx_data_count
= 0;
1888 serial
->tx_buffer_count
= 0;
1889 serial
->tx_data_length
= tx_size
;
1890 serial
->tx_data
= kzalloc(serial
->tx_data_length
, GFP_KERNEL
);
1891 if (!serial
->tx_data
) {
1892 dev_err(dev
, "%s - Out of memory", __func__
);
1895 serial
->tx_buffer
= kzalloc(serial
->tx_data_length
, GFP_KERNEL
);
1896 if (!serial
->tx_buffer
) {
1897 dev_err(dev
, "%s - Out of memory", __func__
);
1903 hso_serial_common_free(serial
);
1907 /* Frees a general hso device */
1908 static void hso_free_device(struct hso_device
*hso_dev
)
1913 /* Creates a general hso device */
1914 static struct hso_device
*hso_create_device(struct usb_interface
*intf
,
1917 struct hso_device
*hso_dev
;
1919 hso_dev
= kzalloc(sizeof(*hso_dev
), GFP_ATOMIC
);
1923 hso_dev
->port_spec
= port_spec
;
1924 hso_dev
->usb
= interface_to_usbdev(intf
);
1925 hso_dev
->interface
= intf
;
1926 kref_init(&hso_dev
->ref
);
1927 mutex_init(&hso_dev
->mutex
);
1929 INIT_WORK(&hso_dev
->async_get_intf
, async_get_intf
);
1930 INIT_WORK(&hso_dev
->async_put_intf
, async_put_intf
);
1935 /* Removes a network device in the network device table */
1936 static int remove_net_device(struct hso_device
*hso_dev
)
1940 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
1941 if (network_table
[i
] == hso_dev
) {
1942 network_table
[i
] = NULL
;
1946 if (i
== HSO_MAX_NET_DEVICES
)
1951 /* Frees our network device */
1952 static void hso_free_net_device(struct hso_device
*hso_dev
)
1955 struct hso_net
*hso_net
= dev2net(hso_dev
);
1961 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1962 usb_free_urb(hso_net
->mux_bulk_rx_urb_pool
[i
]);
1963 kfree(hso_net
->mux_bulk_rx_buf_pool
[i
]);
1965 usb_free_urb(hso_net
->mux_bulk_tx_urb
);
1966 kfree(hso_net
->mux_bulk_tx_buf
);
1968 remove_net_device(hso_net
->parent
);
1971 unregister_netdev(hso_net
->net
);
1972 free_netdev(hso_net
->net
);
1975 hso_free_device(hso_dev
);
1978 /* initialize the network interface */
1979 static void hso_net_init(struct net_device
*net
)
1981 struct hso_net
*hso_net
= netdev_priv(net
);
1983 D1("sizeof hso_net is %d", (int)sizeof(*hso_net
));
1985 /* fill in the other fields */
1986 net
->open
= hso_net_open
;
1987 net
->stop
= hso_net_close
;
1988 net
->hard_start_xmit
= hso_net_start_xmit
;
1989 net
->tx_timeout
= hso_net_tx_timeout
;
1990 net
->watchdog_timeo
= HSO_NET_TX_TIMEOUT
;
1991 net
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
1992 net
->type
= ARPHRD_NONE
;
1993 net
->mtu
= DEFAULT_MTU
- 14;
1994 net
->tx_queue_len
= 10;
1995 SET_ETHTOOL_OPS(net
, &ops
);
1997 /* and initialize the semaphore */
1998 spin_lock_init(&hso_net
->net_lock
);
2001 /* Adds a network device in the network device table */
2002 static int add_net_device(struct hso_device
*hso_dev
)
2006 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2007 if (network_table
[i
] == NULL
) {
2008 network_table
[i
] = hso_dev
;
2012 if (i
== HSO_MAX_NET_DEVICES
)
2017 static int hso_radio_toggle(void *data
, enum rfkill_state state
)
2019 struct hso_device
*hso_dev
= data
;
2020 int enabled
= (state
== RFKILL_STATE_ON
);
2023 mutex_lock(&hso_dev
->mutex
);
2024 if (hso_dev
->usb_gone
)
2027 rv
= usb_control_msg(hso_dev
->usb
, usb_rcvctrlpipe(hso_dev
->usb
, 0),
2028 enabled
? 0x82 : 0x81, 0x40, 0, 0, NULL
, 0,
2029 USB_CTRL_SET_TIMEOUT
);
2030 mutex_unlock(&hso_dev
->mutex
);
2034 /* Creates and sets up everything for rfkill */
2035 static void hso_create_rfkill(struct hso_device
*hso_dev
,
2036 struct usb_interface
*interface
)
2038 struct hso_net
*hso_net
= dev2net(hso_dev
);
2039 struct device
*dev
= hso_dev
->dev
;
2042 hso_net
->rfkill
= rfkill_allocate(&interface_to_usbdev(interface
)->dev
,
2044 if (!hso_net
->rfkill
) {
2045 dev_err(dev
, "%s - Out of memory", __func__
);
2048 rfkn
= kzalloc(20, GFP_KERNEL
);
2050 rfkill_free(hso_net
->rfkill
);
2051 dev_err(dev
, "%s - Out of memory", __func__
);
2054 snprintf(rfkn
, 20, "hso-%d",
2055 interface
->altsetting
->desc
.bInterfaceNumber
);
2056 hso_net
->rfkill
->name
= rfkn
;
2057 hso_net
->rfkill
->state
= RFKILL_STATE_ON
;
2058 hso_net
->rfkill
->data
= hso_dev
;
2059 hso_net
->rfkill
->toggle_radio
= hso_radio_toggle
;
2060 if (rfkill_register(hso_net
->rfkill
) < 0) {
2062 hso_net
->rfkill
->name
= NULL
;
2063 rfkill_free(hso_net
->rfkill
);
2064 dev_err(dev
, "%s - Failed to register rfkill", __func__
);
2069 /* Creates our network device */
2070 static struct hso_device
*hso_create_net_device(struct usb_interface
*interface
)
2073 struct net_device
*net
;
2074 struct hso_net
*hso_net
;
2075 struct hso_device
*hso_dev
;
2077 hso_dev
= hso_create_device(interface
, HSO_INTF_MUX
| HSO_PORT_NETWORK
);
2081 /* allocate our network device, then we can put in our private data */
2082 /* call hso_net_init to do the basic initialization */
2083 net
= alloc_netdev(sizeof(struct hso_net
), "hso%d", hso_net_init
);
2085 dev_err(&interface
->dev
, "Unable to create ethernet device\n");
2089 hso_net
= netdev_priv(net
);
2091 hso_dev
->port_data
.dev_net
= hso_net
;
2093 hso_net
->parent
= hso_dev
;
2095 hso_net
->in_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
,
2097 if (!hso_net
->in_endp
) {
2098 dev_err(&interface
->dev
, "Can't find BULK IN endpoint\n");
2101 hso_net
->out_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
,
2103 if (!hso_net
->out_endp
) {
2104 dev_err(&interface
->dev
, "Can't find BULK OUT endpoint\n");
2107 SET_NETDEV_DEV(net
, &interface
->dev
);
2109 /* registering our net device */
2110 result
= register_netdev(net
);
2112 dev_err(&interface
->dev
, "Failed to register device\n");
2116 /* start allocating */
2117 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
2118 hso_net
->mux_bulk_rx_urb_pool
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
2119 if (!hso_net
->mux_bulk_rx_urb_pool
[i
]) {
2120 dev_err(&interface
->dev
, "Could not allocate rx urb\n");
2123 hso_net
->mux_bulk_rx_buf_pool
[i
] = kzalloc(MUX_BULK_RX_BUF_SIZE
,
2125 if (!hso_net
->mux_bulk_rx_buf_pool
[i
]) {
2126 dev_err(&interface
->dev
, "Could not allocate rx buf\n");
2130 hso_net
->mux_bulk_tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2131 if (!hso_net
->mux_bulk_tx_urb
) {
2132 dev_err(&interface
->dev
, "Could not allocate tx urb\n");
2135 hso_net
->mux_bulk_tx_buf
= kzalloc(MUX_BULK_TX_BUF_SIZE
, GFP_KERNEL
);
2136 if (!hso_net
->mux_bulk_tx_buf
) {
2137 dev_err(&interface
->dev
, "Could not allocate tx buf\n");
2141 add_net_device(hso_dev
);
2143 hso_log_port(hso_dev
);
2145 hso_create_rfkill(hso_dev
, interface
);
2149 hso_free_net_device(hso_dev
);
2153 /* Frees an AT channel ( goes for both mux and non-mux ) */
2154 static void hso_free_serial_device(struct hso_device
*hso_dev
)
2156 struct hso_serial
*serial
= dev2ser(hso_dev
);
2160 set_serial_by_index(serial
->minor
, NULL
);
2162 hso_serial_common_free(serial
);
2164 if (serial
->shared_int
) {
2165 mutex_lock(&serial
->shared_int
->shared_int_lock
);
2166 if (--serial
->shared_int
->ref_count
== 0)
2167 hso_free_shared_int(serial
->shared_int
);
2169 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
2172 hso_free_device(hso_dev
);
2175 /* Creates a bulk AT channel */
2176 static struct hso_device
*hso_create_bulk_serial_device(
2177 struct usb_interface
*interface
, int port
)
2179 struct hso_device
*hso_dev
;
2180 struct hso_serial
*serial
;
2183 hso_dev
= hso_create_device(interface
, port
);
2187 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
2191 serial
->parent
= hso_dev
;
2192 hso_dev
->port_data
.dev_serial
= serial
;
2194 if (port
& HSO_PORT_MODEM
)
2199 if (hso_serial_common_create(serial
, num_urbs
, BULK_URB_RX_SIZE
,
2203 serial
->in_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
,
2205 if (!serial
->in_endp
) {
2206 dev_err(&interface
->dev
, "Failed to find BULK IN ep\n");
2212 hso_get_ep(interface
, USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
))) {
2213 dev_err(&interface
->dev
, "Failed to find BULK IN ep\n");
2217 serial
->write_data
= hso_std_serial_write_data
;
2219 /* and record this serial */
2220 set_serial_by_index(serial
->minor
, serial
);
2222 /* setup the proc dirs and files if needed */
2223 hso_log_port(hso_dev
);
2225 /* done, return it */
2228 if (hso_dev
&& serial
)
2229 hso_serial_common_free(serial
);
2231 hso_free_device(hso_dev
);
2235 /* Creates a multiplexed AT channel */
2237 struct hso_device
*hso_create_mux_serial_device(struct usb_interface
*interface
,
2239 struct hso_shared_int
*mux
)
2241 struct hso_device
*hso_dev
;
2242 struct hso_serial
*serial
;
2245 port_spec
= HSO_INTF_MUX
;
2246 port_spec
&= ~HSO_PORT_MASK
;
2248 port_spec
|= hso_mux_to_port(port
);
2249 if ((port_spec
& HSO_PORT_MASK
) == HSO_PORT_NO_PORT
)
2252 hso_dev
= hso_create_device(interface
, port_spec
);
2256 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
2260 hso_dev
->port_data
.dev_serial
= serial
;
2261 serial
->parent
= hso_dev
;
2263 if (hso_serial_common_create
2264 (serial
, 1, CTRL_URB_RX_SIZE
, CTRL_URB_TX_SIZE
))
2267 serial
->tx_data_length
--;
2268 serial
->write_data
= hso_mux_serial_write_data
;
2270 serial
->shared_int
= mux
;
2271 mutex_lock(&serial
->shared_int
->shared_int_lock
);
2272 serial
->shared_int
->ref_count
++;
2273 mutex_unlock(&serial
->shared_int
->shared_int_lock
);
2275 /* and record this serial */
2276 set_serial_by_index(serial
->minor
, serial
);
2278 /* setup the proc dirs and files if needed */
2279 hso_log_port(hso_dev
);
2281 /* done, return it */
2286 tty_unregister_device(tty_drv
, serial
->minor
);
2290 hso_free_device(hso_dev
);
2295 static void hso_free_shared_int(struct hso_shared_int
*mux
)
2297 usb_free_urb(mux
->shared_intr_urb
);
2298 kfree(mux
->shared_intr_buf
);
2299 mutex_unlock(&mux
->shared_int_lock
);
2304 struct hso_shared_int
*hso_create_shared_int(struct usb_interface
*interface
)
2306 struct hso_shared_int
*mux
= kzalloc(sizeof(*mux
), GFP_KERNEL
);
2311 mux
->intr_endp
= hso_get_ep(interface
, USB_ENDPOINT_XFER_INT
,
2313 if (!mux
->intr_endp
) {
2314 dev_err(&interface
->dev
, "Can't find INT IN endpoint\n");
2318 mux
->shared_intr_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2319 if (!mux
->shared_intr_urb
) {
2320 dev_err(&interface
->dev
, "Could not allocate intr urb?");
2323 mux
->shared_intr_buf
= kzalloc(mux
->intr_endp
->wMaxPacketSize
,
2325 if (!mux
->shared_intr_buf
) {
2326 dev_err(&interface
->dev
, "Could not allocate intr buf?");
2330 mutex_init(&mux
->shared_int_lock
);
2335 kfree(mux
->shared_intr_buf
);
2336 usb_free_urb(mux
->shared_intr_urb
);
2341 /* Gets the port spec for a certain interface */
2342 static int hso_get_config_data(struct usb_interface
*interface
)
2344 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
2346 u32 if_num
= interface
->altsetting
->desc
.bInterfaceNumber
;
2349 if (usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
2350 0x86, 0xC0, 0, 0, config_data
, 17,
2351 USB_CTRL_SET_TIMEOUT
) != 0x11) {
2355 switch (config_data
[if_num
]) {
2360 result
= HSO_PORT_DIAG
;
2363 result
= HSO_PORT_GPS
;
2366 result
= HSO_PORT_GPS_CONTROL
;
2369 result
= HSO_PORT_APP
;
2372 result
= HSO_PORT_APP2
;
2375 result
= HSO_PORT_CONTROL
;
2378 result
= HSO_PORT_NETWORK
;
2381 result
= HSO_PORT_MODEM
;
2384 result
= HSO_PORT_MSD
;
2387 result
= HSO_PORT_PCSC
;
2390 result
= HSO_PORT_VOICE
;
2397 result
|= HSO_INTF_BULK
;
2399 if (config_data
[16] & 0x1)
2400 result
|= HSO_INFO_CRC_BUG
;
2405 /* called once for each interface upon device insertion */
2406 static int hso_probe(struct usb_interface
*interface
,
2407 const struct usb_device_id
*id
)
2409 int mux
, i
, if_num
, port_spec
;
2410 unsigned char port_mask
;
2411 struct hso_device
*hso_dev
= NULL
;
2412 struct hso_shared_int
*shared_int
;
2413 struct hso_device
*tmp_dev
= NULL
;
2415 if_num
= interface
->altsetting
->desc
.bInterfaceNumber
;
2417 /* Get the interface/port specification from either driver_info or from
2418 * the device itself */
2419 if (id
->driver_info
)
2420 port_spec
= ((u32
*)(id
->driver_info
))[if_num
];
2422 port_spec
= hso_get_config_data(interface
);
2424 if (interface
->cur_altsetting
->desc
.bInterfaceClass
!= 0xFF) {
2425 dev_err(&interface
->dev
, "Not our interface\n");
2428 /* Check if we need to switch to alt interfaces prior to port
2430 if (interface
->num_altsetting
> 1)
2431 usb_set_interface(interface_to_usbdev(interface
), if_num
, 1);
2432 interface
->needs_remote_wakeup
= 1;
2434 /* Allocate new hso device(s) */
2435 switch (port_spec
& HSO_INTF_MASK
) {
2437 if ((port_spec
& HSO_PORT_MASK
) == HSO_PORT_NETWORK
) {
2438 /* Create the network device */
2440 hso_dev
= hso_create_net_device(interface
);
2447 if (hso_get_mux_ports(interface
, &port_mask
))
2448 /* TODO: de-allocate everything */
2451 shared_int
= hso_create_shared_int(interface
);
2455 for (i
= 1, mux
= 0; i
< 0x100; i
= i
<< 1, mux
++) {
2456 if (port_mask
& i
) {
2457 hso_dev
= hso_create_mux_serial_device(
2458 interface
, i
, shared_int
);
2469 /* It's a regular bulk interface */
2470 if (((port_spec
& HSO_PORT_MASK
) == HSO_PORT_NETWORK
)
2472 hso_dev
= hso_create_net_device(interface
);
2475 hso_create_bulk_serial_device(interface
, port_spec
);
2483 usb_driver_claim_interface(&hso_driver
, interface
, hso_dev
);
2485 /* save our data pointer in this device */
2486 usb_set_intfdata(interface
, hso_dev
);
2491 hso_free_interface(interface
);
2495 /* device removed, cleaning up */
2496 static void hso_disconnect(struct usb_interface
*interface
)
2498 hso_free_interface(interface
);
2500 /* remove reference of our private data */
2501 usb_set_intfdata(interface
, NULL
);
2503 usb_driver_release_interface(&hso_driver
, interface
);
2506 static void async_get_intf(struct work_struct
*data
)
2508 struct hso_device
*hso_dev
=
2509 container_of(data
, struct hso_device
, async_get_intf
);
2510 usb_autopm_get_interface(hso_dev
->interface
);
2513 static void async_put_intf(struct work_struct
*data
)
2515 struct hso_device
*hso_dev
=
2516 container_of(data
, struct hso_device
, async_put_intf
);
2517 usb_autopm_put_interface(hso_dev
->interface
);
2520 static int hso_get_activity(struct hso_device
*hso_dev
)
2522 if (hso_dev
->usb
->state
== USB_STATE_SUSPENDED
) {
2523 if (!hso_dev
->is_active
) {
2524 hso_dev
->is_active
= 1;
2525 schedule_work(&hso_dev
->async_get_intf
);
2529 if (hso_dev
->usb
->state
!= USB_STATE_CONFIGURED
)
2532 usb_mark_last_busy(hso_dev
->usb
);
2537 static int hso_put_activity(struct hso_device
*hso_dev
)
2539 if (hso_dev
->usb
->state
!= USB_STATE_SUSPENDED
) {
2540 if (hso_dev
->is_active
) {
2541 hso_dev
->is_active
= 0;
2542 schedule_work(&hso_dev
->async_put_intf
);
2546 hso_dev
->is_active
= 0;
2550 /* called by kernel when we need to suspend device */
2551 static int hso_suspend(struct usb_interface
*iface
, pm_message_t message
)
2555 /* Stop all serial ports */
2556 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2557 if (serial_table
[i
] && (serial_table
[i
]->interface
== iface
)) {
2558 result
= hso_stop_serial_device(serial_table
[i
]);
2564 /* Stop all network ports */
2565 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2566 if (network_table
[i
] &&
2567 (network_table
[i
]->interface
== iface
)) {
2568 result
= hso_stop_net_device(network_table
[i
]);
2578 /* called by kernel when we need to resume device */
2579 static int hso_resume(struct usb_interface
*iface
)
2582 struct hso_net
*hso_net
;
2584 /* Start all serial ports */
2585 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2586 if (serial_table
[i
] && (serial_table
[i
]->interface
== iface
)) {
2587 if (dev2ser(serial_table
[i
])->open_count
) {
2589 hso_start_serial_device(serial_table
[i
], GFP_NOIO
);
2590 hso_kick_transmit(dev2ser(serial_table
[i
]));
2597 /* Start all network ports */
2598 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2599 if (network_table
[i
] &&
2600 (network_table
[i
]->interface
== iface
)) {
2601 hso_net
= dev2net(network_table
[i
]);
2602 /* First transmit any lingering data, then restart the
2604 if (hso_net
->skb_tx_buf
) {
2605 dev_dbg(&iface
->dev
,
2606 "Transmitting lingering data\n");
2607 hso_net_start_xmit(hso_net
->skb_tx_buf
,
2610 result
= hso_start_net_device(network_table
[i
]);
2620 static void hso_serial_ref_free(struct kref
*ref
)
2622 struct hso_device
*hso_dev
= container_of(ref
, struct hso_device
, ref
);
2624 hso_free_serial_device(hso_dev
);
2627 static void hso_free_interface(struct usb_interface
*interface
)
2629 struct hso_serial
*hso_dev
;
2632 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2634 && (serial_table
[i
]->interface
== interface
)) {
2635 hso_dev
= dev2ser(serial_table
[i
]);
2637 tty_hangup(hso_dev
->tty
);
2638 mutex_lock(&hso_dev
->parent
->mutex
);
2639 hso_dev
->parent
->usb_gone
= 1;
2640 mutex_unlock(&hso_dev
->parent
->mutex
);
2641 kref_put(&serial_table
[i
]->ref
, hso_serial_ref_free
);
2645 for (i
= 0; i
< HSO_MAX_NET_DEVICES
; i
++) {
2646 if (network_table
[i
]
2647 && (network_table
[i
]->interface
== interface
)) {
2648 struct rfkill
*rfk
= dev2net(network_table
[i
])->rfkill
;
2649 /* hso_stop_net_device doesn't stop the net queue since
2650 * traffic needs to start it again when suspended */
2651 netif_stop_queue(dev2net(network_table
[i
])->net
);
2652 hso_stop_net_device(network_table
[i
]);
2653 cancel_work_sync(&network_table
[i
]->async_put_intf
);
2654 cancel_work_sync(&network_table
[i
]->async_get_intf
);
2656 rfkill_unregister(rfk
);
2657 hso_free_net_device(network_table
[i
]);
2662 /* Helper functions */
2664 /* Get the endpoint ! */
2665 static struct usb_endpoint_descriptor
*hso_get_ep(struct usb_interface
*intf
,
2669 struct usb_host_interface
*iface
= intf
->cur_altsetting
;
2670 struct usb_endpoint_descriptor
*endp
;
2672 for (i
= 0; i
< iface
->desc
.bNumEndpoints
; i
++) {
2673 endp
= &iface
->endpoint
[i
].desc
;
2674 if (((endp
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == dir
) &&
2675 ((endp
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == type
))
2682 /* Get the byte that describes which ports are enabled */
2683 static int hso_get_mux_ports(struct usb_interface
*intf
, unsigned char *ports
)
2686 struct usb_host_interface
*iface
= intf
->cur_altsetting
;
2688 if (iface
->extralen
== 3) {
2689 *ports
= iface
->extra
[2];
2693 for (i
= 0; i
< iface
->desc
.bNumEndpoints
; i
++) {
2694 if (iface
->endpoint
[i
].extralen
== 3) {
2695 *ports
= iface
->endpoint
[i
].extra
[2];
2703 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2704 static int hso_mux_submit_intr_urb(struct hso_shared_int
*shared_int
,
2705 struct usb_device
*usb
, gfp_t gfp
)
2709 usb_fill_int_urb(shared_int
->shared_intr_urb
, usb
,
2711 shared_int
->intr_endp
->bEndpointAddress
& 0x7F),
2712 shared_int
->shared_intr_buf
,
2713 shared_int
->intr_endp
->wMaxPacketSize
,
2714 intr_callback
, shared_int
,
2715 shared_int
->intr_endp
->bInterval
);
2717 result
= usb_submit_urb(shared_int
->shared_intr_urb
, gfp
);
2719 dev_warn(&usb
->dev
, "%s failed mux_intr_urb %d", __func__
,
2725 /* operations setup of the serial interface */
2726 static struct tty_operations hso_serial_ops
= {
2727 .open
= hso_serial_open
,
2728 .close
= hso_serial_close
,
2729 .write
= hso_serial_write
,
2730 .write_room
= hso_serial_write_room
,
2731 .set_termios
= hso_serial_set_termios
,
2732 .chars_in_buffer
= hso_serial_chars_in_buffer
,
2733 .tiocmget
= hso_serial_tiocmget
,
2734 .tiocmset
= hso_serial_tiocmset
,
2737 static struct usb_driver hso_driver
= {
2738 .name
= driver_name
,
2740 .disconnect
= hso_disconnect
,
2741 .id_table
= hso_ids
,
2742 .suspend
= hso_suspend
,
2743 .resume
= hso_resume
,
2744 .supports_autosuspend
= 1,
2747 static int __init
hso_init(void)
2752 /* put it in the log */
2753 printk(KERN_INFO
"hso: %s\n", version
);
2755 /* Initialise the serial table semaphore and table */
2756 spin_lock_init(&serial_table_lock
);
2757 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++)
2758 serial_table
[i
] = NULL
;
2760 /* allocate our driver using the proper amount of supported minors */
2761 tty_drv
= alloc_tty_driver(HSO_SERIAL_TTY_MINORS
);
2765 /* fill in all needed values */
2766 tty_drv
->magic
= TTY_DRIVER_MAGIC
;
2767 tty_drv
->owner
= THIS_MODULE
;
2768 tty_drv
->driver_name
= driver_name
;
2769 tty_drv
->name
= tty_filename
;
2771 /* if major number is provided as parameter, use that one */
2773 tty_drv
->major
= tty_major
;
2775 tty_drv
->minor_start
= 0;
2776 tty_drv
->num
= HSO_SERIAL_TTY_MINORS
;
2777 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
2778 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
2779 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2780 tty_drv
->init_termios
= tty_std_termios
;
2781 tty_drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2782 tty_drv
->termios
= hso_serial_termios
;
2783 tty_drv
->termios_locked
= hso_serial_termios_locked
;
2784 tty_set_operations(tty_drv
, &hso_serial_ops
);
2786 /* register the tty driver */
2787 result
= tty_register_driver(tty_drv
);
2789 printk(KERN_ERR
"%s - tty_register_driver failed(%d)\n",
2794 /* register this module as an usb driver */
2795 result
= usb_register(&hso_driver
);
2797 printk(KERN_ERR
"Could not register hso driver? error: %d\n",
2799 /* cleanup serial interface */
2800 tty_unregister_driver(tty_drv
);
2808 static void __exit
hso_exit(void)
2810 printk(KERN_INFO
"hso: unloaded\n");
2812 tty_unregister_driver(tty_drv
);
2813 /* deregister the usb driver */
2814 usb_deregister(&hso_driver
);
2817 /* Module definitions */
2818 module_init(hso_init
);
2819 module_exit(hso_exit
);
2821 MODULE_AUTHOR(MOD_AUTHOR
);
2822 MODULE_DESCRIPTION(MOD_DESCRIPTION
);
2823 MODULE_LICENSE(MOD_LICENSE
);
2824 MODULE_INFO(Version
, DRIVER_VERSION
);
2826 /* change the debug level (eg: insmod hso.ko debug=0x04) */
2827 MODULE_PARM_DESC(debug
, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2828 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
2830 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
2831 MODULE_PARM_DESC(tty_major
, "Set the major tty number");
2832 module_param(tty_major
, int, S_IRUGO
| S_IWUSR
);
2834 /* disable network interface (eg: insmod hso.ko disable_net=1) */
2835 MODULE_PARM_DESC(disable_net
, "Disable the network interface");
2836 module_param(disable_net
, int, S_IRUGO
| S_IWUSR
);