2 * Driver for Option High Speed Mobile Devices.
4 * Copyright (C) 2007 Option International
5 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd) ajb@spheresystems.co.uk
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * 0.1 Option International
22 * 0.2 Andrew Bird (Sphere Systems Ltd)
23 * Initial port to 2.6, target 2.6.21/22
24 * 0.3 Andrew Bird (Sphere Systems Ltd)
25 * Multiple device support, startup & shutdown cleanups
26 * 0.4 Andrew Bird (Sphere Systems Ltd)
27 * Initial support for Circuit Switched interface - no H/W handshaking
28 * 0.5 Filip Aben ( Option )
29 * - Removed internal project names from comments
30 * - Removed dependency on Modem port which is not always there
31 * - Added USB id's for other compatible Option devices
32 * 0.6 Marco Himmer ( Option )
33 * - Send REZERO UNIT command to switch from UMS to 3G device
34 * - Move packed out of struct to avoid compiler warning
36 * - Added 2.6.12+ compatability
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/init.h>
43 #include <linux/delay.h>
44 #include <linux/netdevice.h>
45 #include <linux/inetdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/module.h>
48 #include <linux/ethtool.h>
49 #include <asm/uaccess.h>
51 #include <linux/usb.h>
52 #include <linux/timer.h>
54 #include <linux/tty.h>
55 #include <linux/tty_driver.h>
56 #include <linux/tty_flip.h>
57 #include <linux/kmod.h>
59 #include <asm/byteorder.h>
60 #include <linux/version.h>
66 #include <linux/proc_fs.h>
73 Description of the device:
75 Interface 0: Contains the IP network interface on the bulk end points.
76 The multiplexed serial ports are using the interrupt and control endpoints.
77 Interrupt contains a bitmap telling which multiplexed serialport needs servicing.
79 Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the port is opened, as
80 this have a huge impact on the network port throughput.
82 Interface 2: Standard modem interface - circuit switched interface, should not be used.
85 #define DRIVER_VERSION "0.5"
86 #define MOD_AUTHOR "Option Wireless"
88 /* From CDC Spec Table 24 */
89 #define CS_INTERFACE_VAL 0x24
91 #define HSO__MAX_MTU 1984 /*1536 */
92 #define DEFAULT_MTU 1500
93 #define DEFAULT_MRU 1500
95 #define CTRL_URB_RX_SIZE 1024
96 #define CTRL_URB_TX_SIZE 64
98 #define BULK_URB_RX_SIZE 4096
99 #define BULK_URB_TX_SIZE 8192
101 #define MUX_INTR_BUF_SIZE 16
102 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
103 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
104 #define MUX_BULK_RX_BUF_COUNT 4
105 #define USB_TYPE_OPTION_VENDOR 0x20
107 /* These definitions are used with the struct hso_priv flags element
108 - use *_bit operations on it. (bit indices not values.)*/
109 #define HSO_NET_RUNNING 0
110 #define HSO_NET_TX_BUSY 1
111 #define HSO_CARD_UNPLUG 2
113 #define HSO_NET_TX_TIMEOUT (HZ*10)
115 #define SEND_ENCAPSULATED_COMMAND 0x00
116 #define GET_ENCAPSULATED_RESPONSE 0x01
118 /* Serial port defines and structs. */
119 #define HSO_THRESHOLD_THROTTLE (7*1024)
120 #define HSO_THRESHOLD_UNTHROTTLE (2*1024)
122 /* These definitions used in the Ethernet Packet Filtering requests */
123 /* See CDC Spec Table 62 */
124 #define MODE_FLAG_PROMISCUOUS (1<<0)
125 #define MODE_FLAG_ALL_MULTICAST (1<<1)
126 #define MODE_FLAG_DIRECTED (1<<2)
127 #define MODE_FLAG_BROADCAST (1<<3)
128 #define MODE_FLAG_MULTICAST (1<<4)
130 /* CDC Spec class requests - CDC Spec Table 46 */
131 #define SET_ETHERNET_MULTICAST_FILTER 0x40
132 #define SET_ETHERNET_PACKET_FILTER 0x43
134 #define HSO_SERIAL_FLAG_THROTTLED 0
135 #define HSO_SERIAL_FLAG_TX_SENT 1
136 #define HSO_SERIAL_FLAG_RX_SENT 2
138 /* #define HSO_SERIAL_TTY_MAJOR 245 */ /* Experimental Major. */
139 #define HSO_SERIAL_MAGIC 0x48534f31
141 /* Variables and such */
142 #define HSO_SERIAL_TTY_MINORS 256 /* Number of ttys to handle */
144 #define D__(lvl_, fmt, arg...) do { \
145 printk(lvl_ "[%d:%s]: " fmt "\n", __LINE__, __FUNCTION__, ## arg); } while(0)
147 #define NFO(args...) D__( KERN_INFO, ##args)
148 #define ERR(args...) D__( KERN_ERR, ##args)
149 #define WARN(args...) D__( KERN_WARNING, ##args)
151 #define D_(lvl, args...) do { if(lvl & debug) NFO( args ); } while(0)
153 #define D1(args...) D_(0x01, ##args)
154 #define D2(args...) D_(0x02, ##args)
155 #define D3(args...) D_(0x04, ##args)
156 #define D4(args...) D_(0x08, ##args)
157 #define D5(args...) D_(0x10, ##args)
160 #define QFO(fmt, args...) do { \
161 printk( KERN_INFO "hso: " fmt "\n", ##args); \
165 #define DUMP(buf_, len_) \
169 for(i_=0;i_<len_;i_++) { \
170 count_ += snprintf(info_+count_, sizeof(info_)-count_, "%02x ", ((u8*)buf_)[i_]); \
171 if (i_!= 0 && (i_ % 35) == 0) { count_ += snprintf(info_+count_, sizeof(info_)-count_,"\n");} \
175 NFO("%d: dump[%s]", len_, info_); \
181 static void dbg_dump(int line_count
, const char *func_name
, unsigned char *buf
,
186 printk("[%d:%s]: len %d", line_count
, func_name
, len
);
188 for (i
= 0; i
< len
; i
++) {
190 printk("\n 0x%03x: ", i
);
191 printk("%02x ", (unsigned char)buf
[i
]);
195 #define DUMP(buf_, len_) dbg_dump(__LINE__, __FUNCTION__, buf_, len_)
196 #define DUMP1(buf_, len_) do{ if(0x01 & debug) DUMP(buf_, len_);}while(0)
205 enum pkt_parse_state
{
211 struct option_descriptor
{
215 } __attribute__ ((packed
)) ;
218 struct net_device_stats stats
;
219 struct net_device
*net
;
220 struct usb_device
*usb
;
221 struct option_descriptor option_info
;
225 struct proc_dir_entry
*ourproc
;
228 int mux_ep_intr_size
;
230 struct urb
*mux_intr_urb
;
234 int mux_ep_bulk_out_size
;
235 int mux_ep_bulk_in_size
;
237 struct urb
*mux_bulk_rx_urb_pool
[MUX_BULK_RX_BUF_COUNT
];
238 struct urb
*mux_bulk_tx_urb
;
239 void *mux_bulk_rx_buf_pool
[MUX_BULK_RX_BUF_COUNT
];
240 void *mux_bulk_tx_buf
;
243 struct sk_buff
*skb_rx_buf
;
244 enum pkt_parse_state rx_parse_state
;
245 unsigned short rx_buf_size
, rx_buf_missing
;
246 struct iphdr rx_ip_hdr
;
247 struct ethhdr dummy_eth_head
;
250 __u16 wMaxSegmentSize
;
251 __u16 wNumberMCFilters
;
254 /* struct usb_ctrlrequest ctrl_req; */
262 struct hso_priv
*odev
;
264 /* rx/tx urb could be either a bulk urb or a control urb depending
265 on which serial port it is used on. */
268 u16 rx_data_length
; /* should contain allocated length */
272 u16 tx_data_length
; /* should contain allocated length */
274 struct usb_ctrlrequest ctrl_req_tx
;
275 struct usb_ctrlrequest ctrl_req_rx
;
277 int ep_bulk_in
; /* for standard ports, not muxed ones */
279 int ep_bulk_out_size
;
284 int (*write_data
) (struct hso_serial
*serial
);
286 /* from usb_serial_port */
287 struct tty_struct
*tty
;
289 struct semaphore sem
;
296 static int debug
= 0x00;
298 static const char driver_name
[] = "hso";
299 static const char *version
= __FILE__
": " DRIVER_VERSION
" " MOD_AUTHOR
;
301 static struct tty_driver
*tty_drv
;
303 static struct hso_serial
*serial_table
[HSO_SERIAL_TTY_MINORS
];
304 static spinlock_t serial_table_lock
;
306 static struct proc_dir_entry
*hso_proc_dir
;
307 static struct proc_dir_entry
*hso_proc_dir_devices
;
309 static struct usb_driver hso_driver
;
311 static struct usb_device_id hso_ids
[] = {
313 {.match_flags
= USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_PRODUCT
| USB_DEVICE_ID_MATCH_INT_CLASS
| USB_DEVICE_ID_MATCH_INT_SUBCLASS
| USB_DEVICE_ID_MATCH_INT_PROTOCOL
, .idVendor
= 0x0af0, .idProduct
= 0x1000, .bInterfaceClass
= 8, .bInterfaceSubClass
= 6, .bInterfaceProtocol
= 80},
314 {USB_DEVICE(0x0af0, 0x6711)},
315 {USB_DEVICE(0x0af0, 0x6731)},
316 {USB_DEVICE(0x0af0, 0x6751)},
317 {USB_DEVICE(0x0af0, 0x6771)},
318 {USB_DEVICE(0x0af0, 0x6791)},
319 {USB_DEVICE(0x0af0, 0x6811)},
320 {USB_DEVICE(0x0af0, 0x6911)},
321 {USB_DEVICE(0x0af0, 0x6951)},
322 {USB_DEVICE(0x0af0, 0x6971)},
323 {USB_DEVICE(0x0af0, 0x7011)},
324 {USB_DEVICE(0x0af0, 0x7031)},
325 {USB_DEVICE(0x0af0, 0x7051)},
326 {USB_DEVICE(0x0af0, 0x7071)},
327 {USB_DEVICE(0x0af0, 0x7111)},
328 {USB_DEVICE(0x0af0, 0x7211)},
329 {USB_DEVICE(0x0af0, 0x7251)},
330 {USB_DEVICE(0x0af0, 0x7271)},
331 {USB_DEVICE(0x0af0, 0x7311)},
335 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
336 static struct termios
*hso_serial_termios
[HSO_SERIAL_TTY_MINORS
];
337 static struct termios
*hso_serial_termios_locked
[HSO_SERIAL_TTY_MINORS
];
339 static struct ktermios
*hso_serial_termios
[HSO_SERIAL_TTY_MINORS
];
340 static struct ktermios
*hso_serial_termios_locked
[HSO_SERIAL_TTY_MINORS
];
346 static struct hso_serial
*hso_serial_start(struct hso_priv
*odev
, u8 minor
,
347 enum type_intf type
, u8 num
);
349 static void hso_serial_stop(struct usb_device
*dev
, u8 minor
, struct hso_priv
*odev
);
351 static void put_rxbuf_data(struct urb
*urb
, struct hso_serial
*serial
);
353 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
354 static void _hso_serial_set_termios(struct tty_struct
*tty
,
355 struct termios
*old
);
357 static void _hso_serial_set_termios(struct tty_struct
*tty
,
358 struct ktermios
*old
);
361 static void hso_serial_disconnect(struct usb_device
*usb
, struct hso_priv
*odev
);
363 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
364 static int hso_mux_submit_intr_urb(struct hso_priv
*odev
,int gfp
);
366 static int hso_mux_submit_intr_urb(struct hso_priv
*odev
,gfp_t gfp
);
369 static int hso_mux_serial_read(struct hso_priv
*odev
, struct hso_serial
*serial
);
372 * Function declarations
375 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
377 #define kzalloc(x,y) kcalloc(x,1,y)
379 /* Following functions are copied straight from the 2.6.20 kernel to maintain compatability */
381 static inline int usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor
*epd
)
383 return ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
384 USB_ENDPOINT_XFER_BULK
);
387 static inline int usb_endpoint_dir_in(const struct usb_endpoint_descriptor
*epd
)
389 return ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
);
392 static inline int usb_endpoint_dir_out(const struct usb_endpoint_descriptor
*epd
)
394 return ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
);
397 static inline int usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor
*epd
)
399 return (usb_endpoint_xfer_bulk(epd
) && usb_endpoint_dir_in(epd
));
402 static inline int usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor
*epd
)
404 return (usb_endpoint_xfer_bulk(epd
) && usb_endpoint_dir_out(epd
));
411 static int get_free_serial_index(void)
415 spin_lock(&serial_table_lock
);
417 for(index
= 0;index
< HSO_SERIAL_TTY_MINORS
;index
++) {
418 if( serial_table
[index
] == NULL
) {
419 spin_unlock(&serial_table_lock
);
424 spin_unlock(&serial_table_lock
);
426 ERR("no free serial devices in table");
431 static void set_serial_by_index(unsigned index
,struct hso_serial
*serial
)
433 spin_lock(&serial_table_lock
);
434 serial_table
[index
] = serial
;
435 spin_unlock(&serial_table_lock
);
438 static struct hso_serial
*get_serial_by_index(unsigned index
)
440 struct hso_serial
*serial
;
442 spin_lock(&serial_table_lock
);
443 serial
= serial_table
[index
];
444 spin_unlock(&serial_table_lock
);
449 static struct hso_serial
*get_serial_by_odev_and_mux(struct hso_priv
*odev
,
452 struct hso_serial
*serial
;
455 spin_lock(&serial_table_lock
);
457 for(i
= 0;i
< HSO_SERIAL_TTY_MINORS
;i
++) {
458 if((serial
= serial_table
[i
]) == NULL
)
461 if(serial
->odev
== odev
&& serial
->mux
== mux
) {
462 spin_unlock(&serial_table_lock
);
467 spin_unlock(&serial_table_lock
);
472 /* returns serial struct and checks for validity of tty struct. */
473 static inline struct hso_serial
*get_serial_by_tty(struct tty_struct
*tty
)
476 if (tty
== NULL
|| tty
->driver_data
== NULL
) {
480 return (struct hso_serial
*) tty
->driver_data
;
483 /* global driver data */
484 static int hso_proc_options(char *buf
, char **start
, off_t offset
,
485 int count
, int *eof
, void *data
)
489 D1("count: %d", count
);
491 len
+= snprintf(buf
+ len
, count
- len
, "debug: %02x\n", debug
);
496 /* per device instance data */
497 static int hso_proc_device_ttys(char *buf
, char **start
, off_t offset
,
498 int count
, int *eof
, void *data
)
502 struct hso_serial
*serial
;
504 D1("count: %d", count
);
506 spin_lock(&serial_table_lock
);
508 for(i
= 0;i
< HSO_SERIAL_TTY_MINORS
;i
++) {
509 if((serial
= serial_table
[i
]) == NULL
)
512 if(serial
->odev
== data
) {
513 len
+= snprintf(buf
+ len
, count
- len
, "/dev/%s%d\n",
514 tty_drv
->name
, serial
->minor
);
518 spin_unlock(&serial_table_lock
);
523 /* TODO: make this a device variable */
524 const unsigned char dummy_mac
[6] = { 0, 1, 2, 3, 4, 5 };
526 static void packetizeRx(struct hso_priv
*odev
, unsigned char *ip_pkt
,
529 unsigned short temp_bytes
= 0, buffer_offset
= 0, frame_len
;
530 unsigned char *tmp_rx_buf
;
531 struct ethhdr
*eth_head
;
534 D("Rx %d bytes", count
);
535 DUMP(ip_pkt
, min(128, count
));
539 switch (odev
->rx_parse_state
) {
540 case WAIT_IP
: /* waiting for IP header. */
541 /* wanted bytes - size of ip header */
543 (count
< odev
->rx_buf_missing
) ? count
: odev
->rx_buf_missing
;
545 memcpy(((unsigned char *)(&odev
->rx_ip_hdr
)) +
546 odev
->rx_buf_size
, ip_pkt
+ buffer_offset
,
549 odev
->rx_buf_size
+= temp_bytes
;
550 buffer_offset
+= temp_bytes
;
551 odev
->rx_buf_missing
-= temp_bytes
;
554 if (!odev
->rx_buf_missing
) { /* header is complete allocate
555 an sk_buffer and continue to WAIT_DATA */
556 frame_len
= ntohs(odev
->rx_ip_hdr
.tot_len
);
558 if (frame_len
> DEFAULT_MRU
) {
559 odev
->rx_parse_state
= WAIT_SYNC
;
562 /* Allocate an sk_buff */
563 if (!(odev
->skb_rx_buf
=
564 dev_alloc_skb(frame_len
+ 2 +
565 odev
->net
->hard_header_len
))) {
566 /* We got no receive buffer. */
567 D("could not allocate memory");
568 odev
->rx_parse_state
= WAIT_SYNC
;
571 /* Here's where it came from */
572 odev
->skb_rx_buf
->dev
= odev
->net
;
574 /* Make some headroom: standard alignment + the ethernet header. */
575 skb_reserve(odev
->skb_rx_buf
,
576 2 + odev
->net
->hard_header_len
);
578 /* Copy what we got so far.
579 make room for iphdr after tail. */
580 tmp_rx_buf
= skb_put(odev
->skb_rx_buf
, sizeof(struct iphdr
));
581 memcpy(tmp_rx_buf
, (char *)&(odev
->rx_ip_hdr
),
582 sizeof(struct iphdr
));
585 odev
->rx_buf_size
= odev
->net
->hard_header_len
+ sizeof(struct iphdr
);
587 /* Filip actually use .tot_len */
588 odev
->rx_buf_missing
= frame_len
- sizeof(struct iphdr
);
589 odev
->rx_parse_state
= WAIT_DATA
;
596 odev
->rx_buf_missing
) ? count
: odev
->rx_buf_missing
;
598 /* copy the rest of the bytes that are left in the buffer
599 into the waiting sk_buf. Make room for temp_bytes after tail. */
600 tmp_rx_buf
= skb_put(odev
->skb_rx_buf
, temp_bytes
);
601 memcpy(tmp_rx_buf
, ip_pkt
+ buffer_offset
, temp_bytes
);
603 odev
->rx_buf_missing
-= temp_bytes
;
605 buffer_offset
+= temp_bytes
;
606 odev
->rx_buf_size
+= temp_bytes
;
607 if (!odev
->rx_buf_missing
) {
608 /* Packet is complete. Inject into stack. */
610 /* Add fake ethernet header. */
611 eth_head
= (struct ethhdr
*)skb_push(odev
->skb_rx_buf
,
612 odev
->net
->hard_header_len
); /* decrease headroom */
613 memcpy(eth_head
, &odev
->dummy_eth_head
,
614 sizeof(struct ethhdr
));
616 memcpy(eth_head
->h_dest
, odev
->net
->dev_addr
, ETH_ALEN
); /* driver MAC */
617 memcpy(eth_head
->h_source
, dummy_mac
, ETH_ALEN
); /* from dummy device */
618 eth_head
->h_proto
= htons(ETH_P_IP
);
621 /* Not sure here either */
622 odev
->skb_rx_buf
->protocol
= eth_type_trans(odev
->skb_rx_buf
, odev
->net
);
623 odev
->skb_rx_buf
->ip_summed
= CHECKSUM_UNNECESSARY
; /* don't check it */
626 DUMP(ip_pkt
, min(128, count
));
629 /* Ship it off to the kernel */
630 netif_rx(odev
->skb_rx_buf
);
631 odev
->skb_rx_buf
= NULL
; /* No longer our buffer. */
633 /* update out statistics */
634 odev
->stats
.rx_packets
++;
636 /* Hmmm, wonder if we have received the IP len or the ETH len. */
637 odev
->stats
.rx_bytes
+= odev
->rx_buf_size
;
639 odev
->rx_buf_size
= 0;
640 odev
->rx_buf_missing
= sizeof(struct iphdr
);
641 odev
->rx_parse_state
= WAIT_IP
;
657 /* Moving data from usb to kernel (in interrupt state) */
659 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
660 static void read_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
662 static void read_bulk_callback(struct urb
*urb
)
665 struct hso_priv
*odev
= urb
->context
;
666 struct net_device
*net
;
669 int status
= urb
->status
; /* preparation for removal of
670 status from struct urb */
673 if(status
!= -ESHUTDOWN
) {
674 D1("nonzero bulk status received: %d", status
);
680 if (!odev
|| !test_bit(HSO_NET_RUNNING
, &odev
->flags
)) {
681 D1("BULK IN callback but driver is not active!");
686 if (!netif_device_present(net
)) {
687 /* Somebody killed our network interface... */
691 if (urb
->actual_length
) {
692 /* Handle the IP stream, add header and push it
693 onto network stack if the packet is complete. */
694 spin_lock_irqsave(&odev
->net_lock
,flags
);
695 packetizeRx(odev
, urb
->transfer_buffer
, urb
->actual_length
);
696 spin_unlock_irqrestore(&odev
->net_lock
,flags
);
699 /* We are done with this URB, resubmit it.
700 Prep the USB to wait for another frame. Reuse same as received.
705 usb_rcvbulkpipe(odev
->usb
, odev
->mux_ep_bulk_in
),
706 urb
->transfer_buffer
,
707 MUX_BULK_RX_BUF_SIZE
,
711 /* Give this to the USB subsystem so it can tell us
712 when more data arrives.
714 if ((res
= usb_submit_urb(urb
, GFP_ATOMIC
))) {
715 WARN("%s failed submit mux_bulk_rx_urb %d", __FUNCTION__
, res
);
719 /* This will tell kernel to send us packets again */
720 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
721 static void write_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
723 static void write_bulk_callback(struct urb
*urb
)
726 struct hso_priv
*odev
= urb
->context
;
727 int status
= urb
->status
;
729 D1("-----------------------------------");
732 if (!odev
|| !test_bit(HSO_NET_RUNNING
, &odev
->flags
)) {
733 ERR("write_bulk_callback: device not running");
737 /* Do we still have a valid kernel network device? */
738 if (!netif_device_present(odev
->net
)) {
739 ERR("write_bulk_callback: net device not present");
744 D1("%s: TX status %d", odev
->net
->name
, status
);
747 /* Tell the network interface we are
748 ready for another frame
750 netif_wake_queue(odev
->net
);
753 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
754 static void ctrl_callback(struct urb
*urb
, struct pt_regs
*regs
)
756 static void ctrl_callback(struct urb
*urb
)
759 struct hso_serial
*serial
= urb
->context
;
760 struct usb_ctrlrequest
*req
= NULL
;
761 int status
= urb
->status
;
767 req
= (struct usb_ctrlrequest
*)(urb
->setup_packet
);
769 D4("\n-------------------- got ctrl callback %02x ---------------------", status
);
771 /* This is old values */
773 D4("bRequestType: 0x%02x", req
->bRequestType
);
774 D4("bRequest: 0x%02x", req
->bRequest
);
775 D4("wValue: 0x%04x", req
->wValue
);
776 D4("wIndex: 0x%04x", req
->wIndex
);
777 D4("wLength: 0x%04x (%d)", req
->wLength
, req
->wLength
);
782 if(status
!= -ESHUTDOWN
) {
783 D1("nonzero ctrl status received: %d", status
);
788 D4("actual length = %d\n", urb
->actual_length
);
791 DUMP1(urb
->transfer_buffer
, urb
->actual_length
);
794 if (req
->bRequestType
==
795 (USB_DIR_IN
| USB_TYPE_OPTION_VENDOR
| USB_RECIP_INTERFACE
)) {
796 if (serial
->open_count
> 0)
797 put_rxbuf_data(urb
, serial
);
799 D1("urb->actual_length == %d", urb
->actual_length
);
801 /* Re issue a read as long as we receive data. */
802 if (urb
->actual_length
!= 0) {
803 hso_mux_serial_read(serial
->odev
, serial
);
805 clear_bit(HSO_SERIAL_FLAG_RX_SENT
, &serial
->flags
);
806 hso_mux_submit_intr_urb(serial
->odev
,GFP_ATOMIC
);
809 } else { /* write request. */
810 if (urb
->actual_length
!= 0) {
811 clear_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
);
819 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
820 static void hso_std_serial_read_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
822 static void hso_std_serial_read_bulk_callback(struct urb
*urb
)
825 struct hso_serial
*serial
= urb
->context
;
827 int status
= urb
->status
;
828 /* struct usb_ctrlrequest *req = NULL; */
834 D4("\n-------------------- got serial_read_bulk callback %02x ---------------------", status
);
837 if(status
!= -ESHUTDOWN
) {
838 D1("nonzero read bulk status received: %d", status
);
843 D1("actual length = %d\n", urb
->actual_length
);
845 DUMP1(urb
->transfer_buffer
, urb
->actual_length
);
848 if (serial
->open_count
== 0) /* No one is listening, dont resubmit it.. */
851 if (status
== 0) { /* Valid data */
852 put_rxbuf_data(urb
, serial
);
853 } else if (status
== -ENOENT
|| status
== -ECONNRESET
) {
854 /* Unlinked - check for throttled port. */
855 D2(" port %d, successfully unlinked urb", serial
->minor
);
857 D2(" port %d, status = %d for read urb", serial
->minor
, status
);
861 /* We are done with this URB, resubmit it.
862 Prep the USB to wait for another frame */
863 usb_fill_bulk_urb(serial
->rx_urb
,
865 usb_rcvbulkpipe(serial
->odev
->usb
,
868 serial
->rx_data_length
,
869 hso_std_serial_read_bulk_callback
,
872 /* Give this to the USB subsystem so it can tell us
873 when more data arrives.
875 if ((res
= usb_submit_urb(serial
->rx_urb
, GFP_ATOMIC
))) {
876 ERR("%s failed submit serial rx_urb %d", __FUNCTION__
, res
);
882 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
883 static void hso_std_serial_write_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
885 static void hso_std_serial_write_bulk_callback(struct urb
*urb
)
888 struct hso_serial
*serial
= urb
->context
;
889 int status
= urb
->status
;
896 clear_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
);
899 if(status
!= -ESHUTDOWN
) {
900 D1("nonzero write bulk status received: %d", status
);
908 static int mux_device_request(struct hso_serial
*serial
, u8 type
, u16 port
,
909 struct urb
*ctrl_urb
,
910 struct usb_ctrlrequest
*ctrl_req
, u8
*ctrl_urb_data
,
917 if (!serial
|| !ctrl_urb
|| !ctrl_req
) {
918 ERR("Wrong arguments ");
922 ctrl_req
->wValue
= 0;
923 ctrl_req
->wIndex
= port
; /* port to send to */
924 ctrl_req
->wLength
= size
; /* Is this supposed to be this??? */
926 if (type
== GET_ENCAPSULATED_RESPONSE
) { /* Reading command */
927 ctrl_req
->bRequestType
=
928 USB_DIR_IN
| USB_TYPE_OPTION_VENDOR
| USB_RECIP_INTERFACE
;
929 ctrl_req
->bRequest
= GET_ENCAPSULATED_RESPONSE
;
930 pipe
= usb_rcvctrlpipe(serial
->odev
->usb
, 0);
931 } else { /* Writing command */
932 ctrl_req
->bRequestType
=
933 USB_DIR_OUT
| USB_TYPE_OPTION_VENDOR
| USB_RECIP_INTERFACE
;
934 ctrl_req
->bRequest
= SEND_ENCAPSULATED_COMMAND
;
935 pipe
= usb_sndctrlpipe(serial
->odev
->usb
, 0);
938 DUMP(ctrl_urb_data
, size
);
942 D2("%s command (%02x) len: %d, port: %d",
943 type
== GET_ENCAPSULATED_RESPONSE
? "Read" : "Write",
944 ctrl_req
->bRequestType
, ctrl_req
->wLength
, port
);
947 ctrl_urb
->transfer_flags
= 0;
949 usb_fill_control_urb(ctrl_urb
,
958 /* Send it on merry way */
959 if ((res
= usb_submit_urb(ctrl_urb
, GFP_ATOMIC
))) {
960 ERR("%s failed submit ctrl_urb %d type %d", __FUNCTION__
, res
, type
);
967 static int hso_mux_serial_read(struct hso_priv
*odev
, struct hso_serial
*serial
)
971 memset(serial
->rx_data
, 0, CTRL_URB_RX_SIZE
); /* rx_data_length */
973 rc
= mux_device_request(serial
,
974 GET_ENCAPSULATED_RESPONSE
,
977 &serial
->ctrl_req_rx
,
978 serial
->rx_data
, serial
->rx_data_length
);
983 We we have a pending response, here we know what port to send the reponse to
985 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
986 static void intr_callback(struct urb
*urb
, struct pt_regs
*regs
)
988 static void intr_callback(struct urb
*urb
)
991 struct hso_priv
*odev
= urb
->context
;
992 struct hso_serial
*serial
;
993 unsigned char *port_req
;
994 int status
= urb
->status
;
1001 D4("\n---------------------- got intr callback %02x ---------------------", status
);
1004 if(status
!= -ESHUTDOWN
) {
1005 D1("%s intr status %d", odev
->net
->name
, status
);
1010 port_req
= urb
->transfer_buffer
;
1012 D4(" port_req = 0x%.2X\n", *port_req
);
1014 for (i
= 0; i
< 8; i
++) { /* max 8 channels on MUX */
1015 serial
= get_serial_by_odev_and_mux(odev
,i
);
1016 if ((*port_req
& (1 << i
)) && serial
!= NULL
) {
1017 D1("Pending read interrupt on port %d\n", i
);
1018 if (!test_and_set_bit(HSO_SERIAL_FLAG_RX_SENT
,
1020 /* Setup and send a ctrl req read on port i */
1021 hso_mux_serial_read(odev
,serial
);
1023 D1("Already pending a read on port %d\n", i
);
1028 /* D1("issue ctrl read "); */
1029 /* debug_read(odev, serial_table[0]); */
1032 static inline int enable_net_traffic(struct hso_priv
*odev
)
1034 D1("Does not do anything yet.");
1038 static inline void disable_net_traffic(struct hso_priv
*odev
)
1040 D1("Does not do anything yet");
1044 Callback routines for kernel Ethernet Device
1047 static void hso_net_tx_timeout(struct net_device
*net
)
1049 struct hso_priv
*odev
= netdev_priv(net
);
1051 D1("-----------------------------------");
1057 /* Tell syslog we are hosed. */
1058 WARN("%s: Tx timed out.", net
->name
);
1060 /* Tear the waiting frame off the list */
1061 usb_unlink_urb(odev
->mux_bulk_tx_urb
);
1063 /* Update statistics */
1064 odev
->stats
.tx_errors
++;
1067 /* Moving data from kernel to usb
1069 static int hso_net_start_xmit(struct sk_buff
*skb
, struct net_device
*net
)
1071 struct hso_priv
*odev
= netdev_priv(net
);
1074 /* Tell the kernel, "No more frames 'til we are done
1077 netif_stop_queue(net
);
1079 skb_pull(skb
, ETH_HLEN
);
1082 DUMP1(skb
->data
, skb
->len
);
1085 /* Copy it from kernel memory to OUR memory */
1086 memcpy(odev
->mux_bulk_tx_buf
, skb
->data
, skb
->len
);
1088 /* Fill in the URB for shipping it out. */
1089 usb_fill_bulk_urb(odev
->mux_bulk_tx_urb
, odev
->usb
,
1090 usb_sndbulkpipe(odev
->usb
, odev
->mux_ep_bulk_out
),
1091 odev
->mux_bulk_tx_buf
,
1093 write_bulk_callback
, odev
);
1095 D1("len: %d/%d", skb
->len
, MUX_BULK_TX_BUF_SIZE
);
1097 /* Deal with the Zero Length packet problem, I hope */
1098 odev
->mux_bulk_tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
1100 /* Send the URB on its merry way. */
1101 if ((res
= usb_submit_urb(odev
->mux_bulk_tx_urb
, GFP_ATOMIC
))) {
1102 WARN("failed mux_bulk_tx_urb %d", res
);
1103 odev
->stats
.tx_errors
++;
1104 netif_start_queue(net
);
1106 odev
->stats
.tx_packets
++;
1107 odev
->stats
.tx_bytes
+= skb
->len
;
1108 net
->trans_start
= jiffies
; /* And tell the kernel when
1109 the last transmit started. */
1117 static struct net_device_stats
*hso_net_get_stats(struct net_device
*net
)
1119 return &((struct hso_priv
*) netdev_priv(net
))->stats
;
1122 /* AJB, called when net I/F is brought up by ifconfig */
1123 static int hso_net_open(struct net_device
*net
)
1125 struct hso_priv
*odev
= netdev_priv(net
);
1128 unsigned long flags
;
1130 /* Turn on the USB and let the packets flow!!! */
1131 if ((res
= enable_net_traffic(odev
))) {
1132 ERR("%s can't enable_net_traffic() - %d", __FUNCTION__
, res
);
1135 /* STTH test code. reset counter. */
1136 odev
->stats
.rx_packets
= 0;
1138 spin_lock_irqsave(&odev
->net_lock
,flags
);
1139 odev
->rx_parse_state
= WAIT_IP
;
1140 odev
->rx_buf_size
= 0;
1141 odev
->rx_buf_missing
= sizeof(struct iphdr
);
1142 spin_unlock_irqrestore(&odev
->net_lock
,flags
);
1144 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1145 /* Prep a receive URB */
1146 usb_fill_bulk_urb(odev
->mux_bulk_rx_urb_pool
[i
],
1148 usb_rcvbulkpipe(odev
->usb
,
1149 odev
->mux_ep_bulk_in
),
1150 odev
->mux_bulk_rx_buf_pool
[i
],
1151 MUX_BULK_RX_BUF_SIZE
,
1152 read_bulk_callback
, odev
);
1154 /* Put it out there so the device can send us stuff */
1155 if ((res
= usb_submit_urb(odev
->mux_bulk_rx_urb_pool
[i
],
1157 WARN("%s failed mux_bulk_rx_urb %d", __FUNCTION__
, res
);
1160 /* Tell the kernel we are ready to start receiving from it */
1161 netif_start_queue(net
);
1163 /* We are up and running. */
1164 set_bit(HSO_NET_RUNNING
, &odev
->flags
);
1169 /* AJB, called when net I/F is brought down by ifconfig */
1170 static int hso_net_close(struct net_device
*net
)
1172 struct hso_priv
*odev
= netdev_priv(net
);
1175 clear_bit(HSO_NET_RUNNING
, &odev
->flags
);
1177 netif_stop_queue(net
);
1179 /* If we are not already unplugged, turn off USB traffic */
1180 if (!test_bit(HSO_CARD_UNPLUG
, &odev
->flags
)) {
1181 disable_net_traffic(odev
);
1184 /* We don't need the URBs anymore. */
1185 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1186 usb_unlink_urb(odev
->mux_bulk_rx_urb_pool
[i
]);
1188 usb_unlink_urb(odev
->mux_bulk_tx_urb
);
1190 /* interrupt urb is still needed for the serial to work.... */
1195 static int netdev_ethtool_ioctl(struct net_device
*net
, struct ifreq
*rq
)
1197 struct hso_priv
*odev
= netdev_priv(net
);
1201 if (copy_from_user(&cmd
, rq
->ifr_data
,sizeof(cmd
)))
1205 /* get driver info */
1206 case ETHTOOL_GDRVINFO
: {
1207 struct ethtool_drvinfo info
= { ETHTOOL_GDRVINFO
};
1208 strncpy(info
.driver
, driver_name
, ETHTOOL_BUSINFO_LEN
);
1209 strncpy(info
.version
, DRIVER_VERSION
,
1210 ETHTOOL_BUSINFO_LEN
);
1211 sprintf(tmp
, "usb%d:%d", odev
->usb
->bus
->busnum
,
1213 strncpy(info
.bus_info
, tmp
, ETHTOOL_BUSINFO_LEN
);
1214 sprintf(tmp
, "%s %x.%x", driver_name
,
1215 ((odev
->bcdCDC
& 0xff00) >> 8),
1216 (odev
->bcdCDC
& 0x00ff));
1217 strncpy(info
.fw_version
, tmp
, ETHTOOL_BUSINFO_LEN
);
1219 if (copy_to_user(rq
->ifr_data
, &info
, sizeof(info
)))
1224 /* get link status */
1225 case ETHTOOL_GLINK
: {
1226 struct ethtool_value edata
= { ETHTOOL_GLINK
};
1227 edata
.data
= netif_carrier_ok(net
);
1229 if (copy_to_user(rq
->ifr_data
, &edata
, sizeof(edata
)))
1235 D1("Got unsupported ioctl: %x", cmd
);
1236 return -EOPNOTSUPP
; /* the ethtool user space tool relies on this */
1239 static int hso_net_ioctl(struct net_device
*net
, struct ifreq
*rq
, int cmd
)
1241 struct hso_priv
*odev
= netdev_priv(net
);
1245 case SIOCDEVPRIVATE
+ 1:/* Chose this one because SIOCDEVPRIVATE used somewhere else in this code */
1246 D5("Transmitted: %lu", odev
->stats
.tx_bytes
);
1247 rq
->ifr_ifru
.ifru_ivalue
= odev
->stats
.tx_bytes
;
1251 return netdev_ethtool_ioctl(net
, rq
);
1254 return -ENOTTY
; /* per ioctl man page */
1258 /* FIXME AJB, work out what we need to do with multicast */
1259 static void hso_net_set_multicast(struct net_device
*net
)
1261 struct hso_priv
*odev
= netdev_priv(net
);
1265 /* Tell the kernel to stop sending us frames while we get this all set up. */
1266 netif_stop_queue(net
);
1268 /* Note: do not reorder, GCC is clever about common statements. */
1269 if (net
->flags
& IFF_PROMISC
) {
1270 /* Unconditionally log net taps. */
1271 D1("%s: Promiscuous mode enabled", net
->name
);
1273 MODE_FLAG_ALL_MULTICAST
| MODE_FLAG_DIRECTED
|
1274 MODE_FLAG_BROADCAST
| MODE_FLAG_MULTICAST
|
1275 MODE_FLAG_PROMISCUOUS
;
1276 } else if (net
->mc_count
> odev
->wNumberMCFilters
) {
1277 /* Too many to filter perfectly -- accept all multicasts. */
1278 D1("%s: too many MC filters for hardware, using allmulti",
1281 MODE_FLAG_ALL_MULTICAST
| MODE_FLAG_DIRECTED
|
1282 MODE_FLAG_BROADCAST
| MODE_FLAG_MULTICAST
;
1283 } else if (net
->flags
& IFF_ALLMULTI
) {
1284 /* Filter in software */
1285 D1("%s: using allmulti", net
->name
);
1287 MODE_FLAG_ALL_MULTICAST
| MODE_FLAG_DIRECTED
|
1288 MODE_FLAG_BROADCAST
| MODE_FLAG_MULTICAST
;
1290 /* do multicast filtering in hardware */
1291 struct dev_mc_list
*mclist
;
1292 D1("%s: set multicast filters", net
->name
);
1294 MODE_FLAG_ALL_MULTICAST
| MODE_FLAG_DIRECTED
|
1295 MODE_FLAG_BROADCAST
| MODE_FLAG_MULTICAST
;
1296 if (!(buf
= kmalloc(6 * net
->mc_count
, GFP_ATOMIC
))) {
1297 ERR("No memory to allocate?");
1300 for (i
= 0, mclist
= net
->mc_list
;
1301 mclist
&& i
< net
->mc_count
; i
++, mclist
= mclist
->next
) {
1302 memcpy(&mclist
->dmi_addr
, &buf
[i
* 6], 6);
1305 usb_control_msg(odev
->usb
, usb_sndctrlpipe(odev
->usb
, 0), SET_ETHERNET_MULTICAST_FILTER
, /* request */
1306 USB_TYPE_CLASS
| USB_DIR_OUT
| USB_RECIP_INTERFACE
, /* request type */
1307 cpu_to_le16(net
->mc_count
), /* value */
1308 cpu_to_le16((u16
) odev
->mux_interface
), /* index */
1309 buf
, (6 * net
->mc_count
), /* size */
1316 /* Tell the kernel to start giving frames to us again. */
1317 netif_wake_queue(net
);
1320 static int parse_option_information(unsigned char *data
, int length
,
1321 struct hso_priv
*odev
)
1326 ERR("Extralen don't contain expected data! length:%d", length
);
1331 odev
->option_info
.length
= data
[i
++];
1334 odev
->option_info
.descriptor_type
= data
[i
++];
1336 if (odev
->option_info
.descriptor_type
!= CS_INTERFACE_VAL
) {
1337 ERR("Expected CS_INTERFACE_VAL, got: %02x",
1338 odev
->option_info
.descriptor_type
);
1342 /* Enabled modem ports */
1343 odev
->option_info
.enabled_ports
= data
[i
++];
1345 D1("length: %02x, type: %02x, subtype: %02x",
1346 odev
->option_info
.length
,
1347 odev
->option_info
.descriptor_type
,
1348 odev
->option_info
.enabled_ports
);
1353 static int get_mux_endpoints(struct usb_interface
*intf
, struct hso_priv
*odev
)
1356 struct usb_endpoint_descriptor
*endp
= NULL
;
1357 struct usb_host_interface
*iface_desc
= intf
->cur_altsetting
;
1359 /* Start out assuming we won't find anything we can use */
1360 odev
->mux_ep_bulk_out
= odev
->mux_ep_bulk_in
= odev
->mux_ep_intr
= 0;
1362 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
1363 endp
= &iface_desc
->endpoint
[i
].desc
;
1365 if (!usb_endpoint_xfer_bulk(endp
)) {
1366 if (!iface_desc
->endpoint
[i
].extralen
||
1367 parse_option_information(
1368 iface_desc
->endpoint
[i
].extra
,
1369 iface_desc
->endpoint
[i
].extralen
,
1371 ERR("Could not get option specific data, not our device then");
1376 endp
->bEndpointAddress
& 0x7F;
1377 odev
->mux_bInterval
= endp
->bInterval
;
1378 odev
->mux_ep_intr_size
= endp
->wMaxPacketSize
;
1380 if (usb_endpoint_is_bulk_in(endp
)) {
1381 odev
->mux_ep_bulk_in
=
1382 endp
->bEndpointAddress
& 0x7F;
1383 odev
->mux_ep_bulk_in_size
=
1384 endp
->wMaxPacketSize
;
1386 odev
->mux_ep_bulk_out
= endp
->bEndpointAddress
;
1387 odev
->mux_ep_bulk_out_size
=
1388 endp
->wMaxPacketSize
;
1393 /* Now make sure we got both an IN and an OUT */
1394 if (odev
->mux_ep_bulk_in
&& odev
->mux_ep_bulk_out
&& odev
->mux_ep_intr
) {
1395 D1("detected BULK IN/OUT/interrupt packets of [size: %d/%d/%d, addr: %02x/%02x/%02x]",
1396 odev
->mux_ep_bulk_in_size
,
1397 odev
->mux_ep_bulk_out_size
,
1398 odev
->mux_ep_intr_size
,
1399 odev
->mux_ep_bulk_in
,
1400 odev
->mux_ep_bulk_out
,
1407 static int get_std_serial_endpoints(struct usb_interface
*intf
, struct hso_serial
*serial
)
1410 struct usb_endpoint_descriptor
*endp
= NULL
;
1411 struct usb_host_interface
*iface_desc
= intf
->cur_altsetting
;
1413 /* Start out assuming we won't find anything we can use */
1414 serial
->ep_bulk_out
= serial
->ep_bulk_in
= 0;
1416 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
1417 endp
= &iface_desc
->endpoint
[i
].desc
;
1419 if (!usb_endpoint_xfer_bulk(endp
)) {
1423 /* Check the first endpoint to see if it is IN or OUT */
1424 if (usb_endpoint_is_bulk_in(endp
)) {
1425 serial
->ep_bulk_in
= endp
->bEndpointAddress
& 0x7F;
1426 serial
->ep_bulk_in_size
= endp
->wMaxPacketSize
;
1428 serial
->ep_bulk_out
= endp
->bEndpointAddress
;
1429 serial
->ep_bulk_out_size
= endp
->wMaxPacketSize
;
1433 /* FIXME AJB, need to handle the interrupt endpoint also
1434 for Circuit Switched port */
1437 /* Now make sure we got both an IN and an OUT */
1438 if (serial
->ep_bulk_in
&& serial
->ep_bulk_out
) {
1439 D1("detected BULK IN/OUT packets of [size: %d/%d, addr: %02x/%02x]",
1440 serial
->ep_bulk_in_size
,
1441 serial
->ep_bulk_out_size
,
1443 serial
->ep_bulk_out
);
1450 #define UMS_SWITCH_CMD_LEN 31
1453 static int check_ums_and_send_rezero(struct usb_interface
*intf
, struct usb_device
*usb
)
1455 struct usb_endpoint_descriptor
*endp
= NULL
;
1456 struct usb_host_interface
*iface_desc
= intf
->cur_altsetting
;
1463 0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12,
1464 0x01, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x01,
1465 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1466 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1470 /* Check UMS interface */
1471 if (iface_desc
->desc
.bNumEndpoints
== 2 &&
1472 iface_desc
->desc
.bInterfaceClass
== 0x08 &&
1473 iface_desc
->desc
.bInterfaceSubClass
== 0x06 &&
1474 iface_desc
->desc
.bInterfaceProtocol
== 0x50) {
1475 D1("UMS interface found");
1477 if (usb_endpoint_is_bulk_out(&iface_desc
->endpoint
[0].desc
)) {
1478 endp
= &iface_desc
->endpoint
[0].desc
;
1480 endp
= &iface_desc
->endpoint
[1].desc
;
1483 if ( 0 != usb_bulk_msg (usb
,
1484 usb_sndbulkpipe(usb
, endp
->bEndpointAddress
),
1485 (void*)UmsSwitchCmd
,
1489 WARN("send UMS switch failed");
1491 D1("UMS switch done");
1500 static int check_and_claim_interfaces(struct usb_device
*usb
,
1501 struct hso_priv
*odev
)
1503 struct usb_host_config
*conf
= usb
->actconfig
;
1508 0. Multiplexed interface - hsdpa net + muxed serial ports.
1509 1. QXDM port - only used for debug
1510 2. Modem port - only used for Circuit switched connections.
1513 /* AJB, only check 1 and 2, since 0 is claimed for us prior to probe */
1514 for (i
= 1; i
< conf
->desc
.bNumInterfaces
; i
++) {
1515 if (usb_interface_claimed(usb_ifnum_to_if(usb
,i
))) {
1516 D1("Interface %d already claimed", i
);
1523 D1("No one has claimed our auxiliary interfaces, good");
1525 /* Check if we found an Option device */
1526 if (get_mux_endpoints(usb_ifnum_to_if(usb
,0), odev
)) {
1527 D1("Mux interface endpoints did not suit us");
1531 /* MUX interface is claimed for us already */
1533 /* Claim QXDM interface */
1534 usb_driver_claim_interface(&hso_driver
,usb_ifnum_to_if(usb
,1),odev
);
1536 /* Claim C/S interface */
1537 if(conf
->desc
.bNumInterfaces
> 2) {
1538 usb_driver_claim_interface(&hso_driver
,usb_ifnum_to_if(usb
,2),odev
);
1541 return conf
->desc
.bNumInterfaces
;
1547 static inline unsigned char hex2dec(unsigned char digit
)
1550 if ((digit
>= '0') && (digit
<= '9')) {
1551 return (digit
- '0');
1553 /* Map all characters to 0-15 */
1554 if ((digit
>= 'a') && (digit
<= 'z')) {
1555 return (digit
- 'a' + 10) % 16;
1557 if ((digit
>= 'A') && (digit
<= 'Z')) {
1558 return (digit
- 'A' + 10) % 16;
1565 Use the serial number to generate the MAC address
1566 FIXME AJB, unfortunately the devices I have, have this set to 'Serial Number'
1567 and so generate the same MAC address :-(
1569 static void set_ethernet_addr(struct hso_priv
*odev
)
1571 unsigned char mac_addr
[6];
1574 unsigned char buffer
[13];
1576 /* Let's assume we don't get anything */
1584 if (0 > (len
= usb_string(odev
->usb
,
1585 odev
->usb
->descriptor
.iSerialNumber
, buffer
, 13))) {
1586 ERR("Attempting to get MAC address failed: %d", -1 * len
);
1592 /* You gotta love failing sanity checks */
1593 ERR("Attempting to get MAC address returned %d bytes", len
);
1597 /* Fill in the mac_addr */
1598 for (i
= 0; i
< 6; i
++) {
1599 if ((16 == buffer
[2 * i
]) || (16 == buffer
[2 * i
+ 1])) {
1600 ERR("Bad value in MAC address i:%d", i
);
1603 (hex2dec(buffer
[2 * i
]) << 4) +
1604 hex2dec(buffer
[2 * i
+ 1]);
1611 /* Now copy it over to our network device structure */
1612 memcpy(odev
->net
->dev_addr
, mac_addr
, sizeof(mac_addr
));
1614 /* Create the default fake ethernet header. */
1615 memcpy(odev
->dummy_eth_head
.h_dest
, mac_addr
, ETH_ALEN
);
1616 memcpy(odev
->dummy_eth_head
.h_source
, dummy_mac
, ETH_ALEN
);
1617 odev
->dummy_eth_head
.h_proto
= htons(ETH_P_IP
);
1620 static void get_string(u8
* buf
, int buf_len
, int string_num
, struct hso_priv
*odev
)
1632 /* Put it into its buffer */
1633 len
= usb_string(odev
->usb
, string_num
, buf
, buf_len
);
1634 /* Just to be safe */
1639 static void log_device_info(struct hso_priv
*odev
)
1641 unsigned char manu
[256];
1642 unsigned char prod
[256];
1643 unsigned char sern
[256];
1644 unsigned char *mac_addr
= odev
->net
->dev_addr
;
1646 /* Try to get the device Manufacturer */
1647 get_string(manu
, sizeof(manu
), odev
->usb
->descriptor
.iManufacturer
, odev
);
1649 /* Try to get the device Product Name */
1650 get_string(prod
, sizeof(prod
), odev
->usb
->descriptor
.iProduct
, odev
);
1652 /* Try to get the device Serial Number */
1653 get_string(sern
, sizeof(sern
), odev
->usb
->descriptor
.iSerialNumber
, odev
);
1655 /* Now send everything we found to the syslog */
1656 QFO("%s: %s %s %s", odev
->net
->name
, manu
, prod
, sern
);
1657 QFO("%s: %02X:%02X:%02X:%02X:%02X:%02X",
1659 mac_addr
[0], mac_addr
[1], mac_addr
[2],
1660 mac_addr
[3], mac_addr
[4], mac_addr
[5]);
1663 static void hso_free_memory(struct hso_priv
*odev
)
1670 if (odev
->mux_intr_urb
) {
1671 usb_free_urb(odev
->mux_intr_urb
);
1673 if (odev
->mux_intr_buf
) {
1674 kfree(odev
->mux_intr_buf
);
1677 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1678 if (odev
->mux_bulk_rx_urb_pool
[i
]) {
1679 usb_free_urb(odev
->mux_bulk_rx_urb_pool
[i
]);
1681 if (odev
->mux_bulk_rx_buf_pool
[i
]) {
1682 kfree(odev
->mux_bulk_rx_buf_pool
[i
]);
1685 if (odev
->mux_bulk_tx_urb
) {
1686 usb_free_urb(odev
->mux_bulk_tx_urb
);
1688 if (odev
->mux_bulk_tx_buf
) {
1689 kfree(odev
->mux_bulk_tx_buf
);
1693 static int hso_alloc_memory(struct hso_priv
*odev
) /* FIXME AJB, need to make cleanup error path */
1700 if (!(odev
->mux_intr_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
1701 ERR("Could not allocate urb?");
1704 if (!(odev
->mux_intr_buf
= kzalloc(MUX_INTR_BUF_SIZE
, GFP_KERNEL
))) {
1705 ERR("Could not allocate buf?");
1709 for (i
= 0; i
< MUX_BULK_RX_BUF_COUNT
; i
++) {
1710 if (!(odev
->mux_bulk_rx_urb_pool
[i
] = usb_alloc_urb(0, GFP_KERNEL
))) {
1711 ERR("Could not allocate urb?");
1714 if (!(odev
->mux_bulk_rx_buf_pool
[i
] = kzalloc(MUX_BULK_RX_BUF_SIZE
, GFP_KERNEL
))) {
1715 ERR("Could not allocate buf?");
1720 if (!(odev
->mux_bulk_tx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
1721 ERR("Could not allocate urb?");
1724 if (!(odev
->mux_bulk_tx_buf
= kzalloc(MUX_BULK_TX_BUF_SIZE
, GFP_KERNEL
))) {
1725 ERR("Could not allocate buf?");
1732 /* Forward declaration */
1734 static int hso_serial_init(void);
1735 static void hso_serial_exit(void);
1737 static void hso_net_init(struct net_device
*net
)
1739 struct hso_priv
*odev
= netdev_priv(net
);
1741 memset(odev
,0,sizeof(*odev
));
1743 D("sizeof hso_priv is %d",sizeof(*odev
));
1747 SET_MODULE_OWNER(net
);
1748 net
->open
= hso_net_open
;
1749 net
->stop
= hso_net_close
;
1751 net
->hard_start_xmit
= hso_net_start_xmit
;
1752 net
->do_ioctl
= hso_net_ioctl
;
1753 net
->get_stats
= hso_net_get_stats
;
1754 /* rebuild header */
1755 net
->tx_timeout
= hso_net_tx_timeout
;
1756 net
->watchdog_timeo
= HSO_NET_TX_TIMEOUT
;
1758 net
->flags
|= IFF_NOARP
;
1760 /* hard_header_cache */
1761 net
->set_multicast_list
= hso_net_set_multicast
;
1762 net
->mtu
= DEFAULT_MTU
- 14;
1763 net
->tx_queue_len
= 10;
1765 /* more ethernet defaults */
1766 odev
->skb_rx_buf
= NULL
;
1767 odev
->rx_parse_state
= WAIT_IP
;
1768 odev
->wMaxSegmentSize
= DEFAULT_MTU
;
1769 odev
->wNumberMCFilters
= 0; /* disable hardware filtering */
1771 spin_lock_init(&odev
->net_lock
);
1774 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
1775 static int hso_mux_submit_intr_urb(struct hso_priv
*odev
,int gfp
)
1777 static int hso_mux_submit_intr_urb(struct hso_priv
*odev
,gfp_t gfp
)
1782 if (test_bit(HSO_CARD_UNPLUG
, &odev
->flags
)) {
1783 WARN("%s closing so not rearming mux_intr_urb", __FUNCTION__
);
1787 D5("Arm and submit the interrupt URB");
1789 usb_fill_int_urb(odev
->mux_intr_urb
,
1791 usb_rcvintpipe(odev
->usb
, odev
->mux_ep_intr
),
1796 (odev
->usb
->speed
== USB_SPEED_HIGH
) ? (1 << odev
->mux_bInterval
) : odev
->mux_bInterval
);
1798 if ((res
= usb_submit_urb(odev
->mux_intr_urb
, gfp
))) {
1799 WARN("%s failed mux_intr_urb %d", __FUNCTION__
, res
);
1806 /* AJB, called once for each unclaimed interface */
1807 static int hso_probe(struct usb_interface
*interface
,
1808 const struct usb_device_id
*id
)
1810 struct hso_priv
*odev
;
1813 struct net_device
*net
;
1814 struct usb_device
*usb
= interface_to_usbdev(interface
);
1817 printk("++++++++++++++ being probed ++++++++++++++++=\n");
1818 D1("** Start probing **");
1820 D1("probe called with interface num %d",
1821 interface
->altsetting
->desc
.bInterfaceNumber
);
1823 if (interface
->altsetting
->desc
.bInterfaceNumber
!= 0) {
1827 /* Check if we found an Option 3G UMS device */
1828 D1("Check interface num 0");
1829 if (check_ums_and_send_rezero(usb_ifnum_to_if(usb
,0), usb
)) {
1835 * If we get a kernel Ethernet interface, we can get our
1836 * private data storage allocated using it
1838 net
= alloc_netdev(sizeof(struct hso_priv
), "hso%d", hso_net_init
);
1840 ERR("Unable to create ethernet device");
1843 odev
= netdev_priv(net
);
1845 res
= register_netdev(net
);
1847 ERR("Unable to register ethernet device");
1851 if ((num_if
= check_and_claim_interfaces(usb
, odev
)) < 0 ) {
1852 D1("Could not find valid interface configuration");
1853 goto exit_unregister
;
1856 if ((res
= hso_alloc_memory(odev
)) ){
1857 goto exit_release_interfaces
;
1860 usb_set_intfdata(interface
, odev
); /* save our data pointer in this device */
1861 /* FIXME - should do usb_set_intfdata(interface, odev); for 1 also? */
1862 /* FIXME - should do usb_set_intfdata(interface, odev); for 2 also? */
1864 /* We'll keep track of this information for later... */
1868 /* and don't forget the MAC address. */
1869 set_ethernet_addr(odev
);
1871 /* Send a message to syslog about what we are handling */
1872 log_device_info(odev
);
1875 for (i
= 1,mux
= 0; i
< 0x100; i
= i
<< 1,mux
++) {
1876 if (odev
->option_info
.enabled_ports
& i
) {
1877 if((minor
= get_free_serial_index()) < 0)
1879 hso_serial_start(odev
, minor
, MUX_INTERFACE
, mux
);
1883 /* Diagnostics port */
1884 if((minor
= get_free_serial_index()) >= 0) {
1885 hso_serial_start(odev
, minor
, QXDM_INTERFACE
, 1); /* interface num */
1889 if( ( num_if
> 2 ) && ((minor
= get_free_serial_index()) >= 0) ) {
1890 hso_serial_start(odev
, minor
, CS_INTERFACE
, 2); /* interface num */
1895 /* Arm and submit the interrupt URB */
1896 if(hso_mux_submit_intr_urb(odev
,GFP_KERNEL
) != 0) {
1897 goto exit_release_interfaces
;
1900 odev
->ourproc
= proc_mkdir(odev
->net
->name
, hso_proc_dir_devices
);
1901 create_proc_read_entry("ttys", 0, odev
->ourproc
, hso_proc_device_ttys
, odev
);
1903 D1("** probing done **");
1907 exit_release_interfaces
:
1908 /* FIXME AJB, release the usb interfaces */
1911 unregister_netdev(net
);
1920 * Module's disconnect routine
1921 * Called when the driver is unloaded or the device is unplugged
1922 * (Whichever happens first assuming the driver suceeded at its probe)
1924 static void hso_disconnect(struct usb_interface
*interface
)
1926 struct usb_device
*usb
= interface_to_usbdev(interface
);
1927 struct hso_priv
*odev
;
1929 D1("called with interface num %d", interface
->altsetting
->desc
.bInterfaceNumber
);
1931 if (interface
->altsetting
->desc
.bInterfaceNumber
== 0) {
1933 odev
= usb_get_intfdata(interface
);
1934 if (!odev
|| !odev
->usb
|| !odev
->net
) {
1935 WARN("odev, or odev->usb, or odev->net null");
1939 /* let's cleanup our per device info */
1940 remove_proc_entry("ttys", odev
->ourproc
);
1942 remove_proc_entry(odev
->net
->name
, hso_proc_dir_devices
);
1944 usb_set_intfdata(interface
,NULL
);
1946 /* stop the interrupts */
1947 usb_kill_urb(odev
->mux_intr_urb
);
1950 It is possible that this function is called before
1951 the "close" function. This tells the close function
1952 we are already disconnected
1954 set_bit(HSO_CARD_UNPLUG
, &odev
->flags
);
1958 hso_serial_disconnect(usb
, odev
);
1960 /* release the device memory under odev */
1961 hso_free_memory(odev
);
1963 /* We don't need the network device any more */
1964 unregister_netdev(odev
->net
);
1966 /* Free network device including odev memeory */
1967 free_netdev(odev
->net
);
1971 usb_driver_release_interface(&hso_driver
, interface
);
1974 static struct usb_driver hso_driver
= {
1975 .name
= driver_name
,
1977 .disconnect
= hso_disconnect
,
1978 .id_table
= hso_ids
,
1981 int __init
hso_init(void)
1988 hso_proc_dir
= proc_mkdir(driver_name
, proc_root_driver
);
1989 hso_proc_dir_devices
= proc_mkdir("devices", hso_proc_dir
);
1991 /* Initialise our global data */
1992 spin_lock_init(&serial_table_lock
);
1993 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
1994 serial_table
[i
] = NULL
;
1999 if ((rc
= usb_register(&hso_driver
))) {
2000 ERR("Could not register hso driver? error: %d", rc
);
2005 create_proc_read_entry("options", 0, hso_proc_dir
, hso_proc_options
,NULL
);
2010 /* usb_deregister calls disconnect of registered drivers. */
2011 void __exit
hso_exit(void)
2015 usb_deregister(&hso_driver
);
2018 remove_proc_entry("options", hso_proc_dir
);
2019 remove_proc_entry("devices", hso_proc_dir
);
2020 remove_proc_entry(driver_name
, proc_root_driver
);
2025 TTY Layer Type definitions
2027 static int hso_serial_open(struct tty_struct
*tty
, struct file
*filp
);
2028 static void hso_serial_close(struct tty_struct
*tty
, struct file
*filp
);
2029 static void hso_serial_hangup(struct tty_struct
*tty
);
2031 static int hso_serial_write(struct tty_struct
*tty
, const unsigned char *buf
,
2034 static int hso_serial_write_room(struct tty_struct
*tty
);
2035 static int hso_serial_chars_in_buffer(struct tty_struct
*tty
);
2036 static void hso_serial_throttle(struct tty_struct
*tty
);
2037 static void hso_serial_unthrottle(struct tty_struct
*tty
);
2038 static int hso_serial_ioctl(struct tty_struct
*tty
, struct file
*file
,
2039 unsigned int cmd
, unsigned long arg
);
2041 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
2042 static void hso_serial_set_termios(struct tty_struct
*tty
, struct termios
*old
);
2044 static void hso_serial_set_termios(struct tty_struct
*tty
,
2045 struct ktermios
*old
);
2048 static void hso_serial_break(struct tty_struct
*tty
, int break_state
);
2049 static int hso_serial_read_proc(char *page
, char **start
, off_t off
, int count
,
2050 int *eof
, void *data
);
2052 static int hso_serial_open(struct tty_struct
*tty
, struct file
*filp
)
2054 struct hso_serial
*serial
= get_serial_by_index(tty
->index
);
2055 struct hso_priv
*odev
;
2057 if (serial
== NULL
|| serial
->magic
!= HSO_SERIAL_MAGIC
) {
2058 tty
->driver_data
= NULL
;
2059 ERR("Failed to open port");
2065 D1("Opening %d", serial
->minor
);
2067 tty
->driver_data
= serial
;
2071 odev
= serial
->odev
;
2073 serial
->open_count
++;
2074 if (serial
->open_count
== 1) {
2075 tty
->low_latency
= 1;
2079 /* Force default termio settings */
2080 _hso_serial_set_termios(tty
, NULL
);
2083 D1("Port was already open");
2086 /* If it is not the MUX port fill in and submit a bulk urb.
2087 (already allocated in hso_serial_start) */
2088 if (serial
->type
!= MUX_INTERFACE
) {
2090 usb_fill_bulk_urb(serial
->rx_urb
, serial
->odev
->usb
,
2091 usb_rcvbulkpipe(serial
->odev
->usb
,
2092 serial
->ep_bulk_in
),
2093 serial
->rx_data
, serial
->rx_data_length
,
2094 hso_std_serial_read_bulk_callback
, serial
);
2096 if ((res
= usb_submit_urb(serial
->rx_urb
, GFP_KERNEL
)))
2097 D("Failed to submit urb - res %d", res
);
2105 static void __hso_serial_close(struct hso_serial
*serial
)
2109 if (!serial
->open_count
) {
2110 err("%s - port %d: not open", __FUNCTION__
, serial
->minor
);
2114 serial
->open_count
--;
2116 D1("serial->open_count == %d",serial
->open_count
);
2118 if (serial
->open_count
<= 0) {
2119 if (serial
->type
!= MUX_INTERFACE
) {
2120 D1("Unlink any pending bulk URBS");
2121 /* Stop pending urbs. Do not deallocate them,
2122 it will be done in hso_serial_stop. */
2123 /* Is this in interrupt state? */
2124 usb_unlink_urb(serial
->rx_urb
);
2125 usb_unlink_urb(serial
->tx_urb
);
2128 serial
->open_count
= 0;
2130 serial
->tty
->driver_data
= NULL
;
2137 static void hso_serial_close(struct tty_struct
*tty
, struct file
*filp
)
2139 struct hso_serial
*serial
= NULL
;
2142 if (tty
== NULL
|| tty
->driver_data
== NULL
) {
2143 D1("(tty == NULL || tty->driver_data == NULL)");
2147 serial
= tty
->driver_data
;
2149 if (tty
->driver_data
) {
2150 __hso_serial_close(serial
);
2155 static void hso_serial_hangup(struct tty_struct
*tty
)
2160 /* Called once per device shutdown */
2162 static void hso_serial_disconnect(struct usb_device
*usb
, struct hso_priv
*odev
)
2164 struct hso_serial
*serial
;
2167 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2168 serial
= get_serial_by_index(i
);
2169 if (serial
!= NULL
&& serial
->odev
== odev
) { /* only close the
2170 ports belonging to odev */
2172 while (serial
->open_count
> 0) {
2173 __hso_serial_close(serial
);
2175 hso_serial_stop(usb
, i
, odev
);
2182 static int hso_serial_write(struct tty_struct
*tty
, const unsigned char *buf
,
2185 struct hso_serial
*serial
= get_serial_by_tty(tty
);
2186 int retval
= -EINVAL
;
2188 if (serial
== NULL
) {
2189 ERR("%s(%d) tty or tty->driver_data is NULL", __FUNCTION__
,
2196 while (test_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
)) {
2197 /* wait for previous write to complete
2198 * how else to ensure we are always able
2199 * to write at least one byte for putchar()? */
2200 D2("HSO_SERIAL_FLAG_TX_SENT says already in progress");
2204 set_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
);
2206 D2(" hso_port = %d count %d", serial
->minor
, count
);
2207 count
= min((u16
) count
, serial
->tx_data_length
);
2209 memcpy(serial
->tx_data
, buf
, count
);
2211 serial
->tx_data_count
= count
;
2213 if (serial
->write_data
) {
2214 retval
= serial
->write_data(serial
);
2216 clear_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
);
2224 static int hso_mux_serial_write_data(struct hso_serial
*serial
)
2226 return mux_device_request(serial
,
2227 SEND_ENCAPSULATED_COMMAND
,
2230 &serial
->ctrl_req_tx
,
2231 serial
->tx_data
, serial
->tx_data_count
);
2234 static int hso_std_serial_write_data(struct hso_serial
*serial
)
2236 int count
= serial
->tx_data_count
;
2239 usb_fill_bulk_urb(serial
->tx_urb
,
2241 usb_sndbulkpipe(serial
->odev
->usb
,
2242 serial
->ep_bulk_out
),
2244 serial
->tx_data_count
,
2245 hso_std_serial_write_bulk_callback
,
2248 if ((res
= usb_submit_urb(serial
->tx_urb
, GFP_KERNEL
)))
2250 D("Failed to submit urb - res %d ", res
);
2251 clear_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
);
2258 static int hso_serial_write_room(struct tty_struct
*tty
)
2260 struct hso_serial
*serial
= get_serial_by_tty(tty
);
2268 if(!test_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
))
2269 room
= serial
->tx_data_length
;
2273 D4("room = %d ", room
);
2278 static int hso_serial_chars_in_buffer(struct tty_struct
*tty
)
2280 struct hso_serial
*serial
= get_serial_by_tty(tty
);
2283 if (serial
== NULL
) {
2289 if(test_bit(HSO_SERIAL_FLAG_TX_SENT
, &serial
->flags
))
2290 chars
= serial
->tx_urb
->transfer_buffer_length
;
2294 D4("chars = %d ", chars
);
2299 static void put_rxbuf_data(struct urb
*urb
, struct hso_serial
*serial
)
2301 struct tty_struct
*tty
= serial
->tty
;
2303 if (urb
== NULL
|| serial
== NULL
) {
2304 D1("serial = NULL");
2308 /* Push data to tty */
2309 if (tty
&& urb
->actual_length
) {
2312 D("data to push to tty");
2314 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16) )
2317 unsigned char *data
= urb
->transfer_buffer
;
2319 for (i
= 0; i
< urb
->actual_length
; ++i
) {
2320 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
2321 tty_flip_buffer_push(tty
);
2322 tty_insert_flip_char(tty
, data
[i
], 0);
2324 tty_flip_buffer_push(tty
);
2327 tty_buffer_request_room(tty
, urb
->actual_length
);
2328 tty_insert_flip_string(tty
, urb
->transfer_buffer
, urb
->actual_length
);
2329 tty_flip_buffer_push(tty
);
2334 /* FIXME - AJB, maybe we need to throttle? */
2336 if (rx_fifo
->cnt
>= HSO_THRESHOLD_THROTTLE
2337 && !test_and_set_bit(HSO_SERIAL_FLAG_THROTTLED
, &serial
->flags
)) {
2338 D1("Throttle: rx_fifo->cnt[%d] >= HSO_THRESHOLD_THROTTLE[%d]",
2339 rx_fifo
->cnt
, HSO_THRESHOLD_THROTTLE
);
2340 /* hss_throttle(port); */
2346 static void hso_serial_throttle(struct tty_struct
*tty
)
2351 static void hso_serial_unthrottle(struct tty_struct
*tty
)
2356 static int hso_serial_ioctl(struct tty_struct
*tty
, struct file
*file
,
2357 unsigned int cmd
, unsigned long arg
)
2360 return -ENOIOCTLCMD
;
2363 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
2364 static void _hso_serial_set_termios(struct tty_struct
*tty
,
2365 struct termios
*old
)
2367 static void _hso_serial_set_termios(struct tty_struct
*tty
,
2368 struct ktermios
*old
)
2371 struct hso_serial
*serial
= get_serial_by_tty(tty
);
2373 if ((!tty
) || (!tty
->termios
) || (!serial
)) {
2374 ERR("no tty structures");
2378 D4("port %d", serial
->minor
);
2381 * The default requirements for this device are:
2383 serial
->tty
->termios
->c_iflag
&= ~(IGNBRK
/* disable ignore break */
2384 | BRKINT
/* disable break causes interrupt */
2385 | PARMRK
/* disable mark parity errors */
2386 | ISTRIP
/* disable clear high bit of input characters */
2387 | INLCR
/* disable translate NL to CR */
2388 | IGNCR
/* disable ignore CR */
2389 | ICRNL
/* disable translate CR to NL */
2390 | IXON
); /* disable enable XON/XOFF flow control */
2392 serial
->tty
->termios
->c_oflag
&= ~OPOST
; /* disable postprocess output characters */
2394 serial
->tty
->termios
->c_lflag
&= ~(ECHO
/* disable echo input characters */
2395 | ECHONL
/* disable echo new line */
2396 | ICANON
/* disable erase, kill, werase, and rprnt special characters */
2397 | ISIG
/* disable interrupt, quit, and suspend special characters */
2398 | IEXTEN
); /* disable non-POSIX special characters */
2400 serial
->tty
->termios
->c_cflag
&= ~(CSIZE
/* no size */
2401 | PARENB
/* disable parity bit */
2402 | CBAUD
); /* clear current baud rate */
2404 serial
->tty
->termios
->c_cflag
|= (CS8
/* character size 8 bits */
2405 | B115200
); /* baud rate 115200 */
2408 * Force low_latency on; otherwise the pushes are scheduled;
2409 * this is bad as it opens up the possibility of dropping bytes
2410 * on the floor. We don't want to drop bytes on the floor. :)
2412 serial
->tty
->low_latency
= 1;
2414 /* Notify the tty driver that the termios have changed. */
2415 serial
->tty
->ldisc
.set_termios(serial
->tty
, NULL
);
2420 * TODO - need to be down/up protected, but currently it is also used in serial_open
2421 * also locking it...
2424 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
2425 static void hso_serial_set_termios(struct tty_struct
*tty
, struct termios
*old
)
2427 static void hso_serial_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
2430 struct hso_serial
*serial
= get_serial_by_tty(tty
);
2432 if ((!tty
) || (!tty
->termios
) || (!serial
)) {
2433 D1("no tty structures");
2437 D1("port %d", serial
->minor
);
2440 if (serial
->open_count
)
2441 _hso_serial_set_termios(tty
, old
);
2447 static void hso_serial_break(struct tty_struct
*tty
, int break_state
)
2452 static int hso_serial_read_proc(char *page
, char **start
, off_t off
, int count
,
2453 int *eof
, void *data
)
2459 static void port_softint(void *private)
2461 struct hso_serial
*serial
= (struct hso_serial
*) private;
2462 struct tty_struct
*tty
;
2464 D1(" - port %d", serial
->minor
);
2473 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
))
2474 && tty
->ldisc
.write_wakeup
) {
2475 D1(" - write wakeup call.");
2476 (tty
->ldisc
.write_wakeup
) (tty
);
2479 wake_up_interruptible(&tty
->write_wait
);
2483 static struct hso_serial
*hso_serial_start(struct hso_priv
*odev
, u8 minor
, enum type_intf type
, u8 num
)
2485 struct hso_serial
*serial
= NULL
;
2487 if (!(serial
= kmalloc(sizeof(*serial
), GFP_KERNEL
))) {
2488 ERR("%s - Out of memory", __FUNCTION__
);
2492 D1("tty_register_device %d", minor
);
2493 tty_register_device(tty_drv
, minor
, NULL
);
2495 serial
->odev
= odev
;
2496 serial
->minor
= minor
;
2497 serial
->type
= type
;
2498 serial
->mux
= (type
== MUX_INTERFACE
) ? num
: 0;
2499 serial
->magic
= HSO_SERIAL_MAGIC
;
2501 serial
->rx_urb
= NULL
;
2502 serial
->rx_data
= NULL
;
2503 serial
->rx_data_length
= 0;
2509 QFO("Multiplexed Control channel present");
2512 QFO("Multiplexed Application channel present");
2515 QFO("Multiplexed PC/SC channel present");
2518 QFO("Multiplexed GPS channel present");
2521 QFO("Multiplexed Application 2 channel present");
2526 QFO("Multiplexed Reserved(%d) channel present",num
);
2530 case QXDM_INTERFACE
:
2531 QFO("QXDM port present");
2534 QFO("Circuit Switched port present");
2538 if (type
!= MUX_INTERFACE
) {
2539 if (get_std_serial_endpoints(usb_ifnum_to_if(odev
->usb
,num
), serial
)) {
2540 D1("Interface endpoints did not suit us");
2545 if (!(serial
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
2546 ERR("Could not allocate urb?");
2549 serial
->rx_urb
->transfer_buffer
= NULL
;
2550 serial
->rx_urb
->transfer_buffer_length
= 0;
2552 serial
->rx_data_length
=
2553 (type
== MUX_INTERFACE
) ? CTRL_URB_RX_SIZE
: BULK_URB_RX_SIZE
;
2555 if (!(serial
->rx_data
= kzalloc(serial
->rx_data_length
, GFP_KERNEL
))) {
2556 ERR("%s - Out of memory", __FUNCTION__
);
2560 serial
->tx_data_length
= 0;
2561 serial
->tx_data_count
= 0;
2562 serial
->tx_data
= NULL
;
2563 serial
->tx_urb
= NULL
;
2565 if (!(serial
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
2566 ERR("Could not allocate urb?");
2569 serial
->tx_urb
->transfer_buffer
= NULL
;
2570 serial
->tx_urb
->transfer_buffer_length
= 0;
2572 serial
->tx_data_length
=
2573 (type
== MUX_INTERFACE
) ? CTRL_URB_TX_SIZE
: BULK_URB_TX_SIZE
;
2575 if (!(serial
->tx_data
= kzalloc(serial
->tx_data_length
, GFP_KERNEL
))) {
2576 ERR("%s - Out of memory", __FUNCTION__
);
2580 /* The muxed ports can not use full 64 bytes and the diagnostic must use
2581 * the full 64 bytes if sending larger packets. */
2582 if (type
== MUX_INTERFACE
)
2583 serial
->tx_data_length
--;
2585 if (type
== MUX_INTERFACE
) {
2586 serial
->write_data
= hso_mux_serial_write_data
;
2588 serial
->write_data
= hso_std_serial_write_data
;
2591 serial
->open_count
= 0;
2592 serial
->private = NULL
;
2593 init_MUTEX(&serial
->sem
);
2595 set_serial_by_index(minor
,serial
);
2600 if (serial
->rx_urb
) {
2601 usb_free_urb(serial
->rx_urb
);
2603 if (serial
->rx_data
) {
2604 kfree(serial
->rx_data
);
2606 if (serial
->tx_data
) {
2607 kfree(serial
->tx_data
);
2609 if (serial
->tx_urb
) {
2610 usb_free_urb(serial
->tx_urb
);
2619 /* release the serial port */
2620 static void hso_serial_stop(struct usb_device
*dev
, u8 minor
,
2621 struct hso_priv
*odev
)
2623 struct hso_serial
*serial
;
2625 D1("Deregistering serial %d", minor
);
2627 serial
= get_serial_by_index(minor
);
2628 if (serial
== NULL
|| serial
->magic
!= HSO_SERIAL_MAGIC
) {
2629 ERR("Trying to deregister an unused serial port");
2633 if (serial
->odev
!= odev
) {
2634 ERR("Trying to deregister a serial port belonging to a different device");
2638 serial
->odev
= NULL
;
2643 serial
->write_data
= NULL
;
2645 if (serial
->rx_urb
!= NULL
) {
2646 usb_unlink_urb(serial
->rx_urb
);
2647 usb_free_urb(serial
->rx_urb
);
2649 serial
->rx_urb
= NULL
;
2651 if (serial
->rx_data
!= NULL
)
2652 kfree(serial
->rx_data
);
2653 serial
->rx_data
= NULL
;
2655 if (serial
->tx_urb
!= NULL
) {
2656 usb_unlink_urb(serial
->tx_urb
);
2657 usb_free_urb(serial
->tx_urb
);
2659 serial
->tx_urb
= NULL
;
2661 if (serial
->tx_data
!= NULL
)
2662 kfree(serial
->tx_data
);
2663 serial
->tx_data
= NULL
;
2668 tty_unregister_device(tty_drv
,minor
);
2670 set_serial_by_index(minor
,NULL
);
2673 static struct tty_operations hso_serial_ops
= {
2674 .open
= hso_serial_open
,
2675 .close
= hso_serial_close
,
2676 .write
= hso_serial_write
,
2677 .write_room
= hso_serial_write_room
,
2678 .ioctl
= hso_serial_ioctl
,
2679 .set_termios
= hso_serial_set_termios
,
2680 .throttle
= hso_serial_throttle
,
2681 .unthrottle
= hso_serial_unthrottle
,
2682 .break_ctl
= hso_serial_break
,
2683 .chars_in_buffer
= hso_serial_chars_in_buffer
,
2684 .read_proc
= hso_serial_read_proc
,
2685 /* .tiocmget = serial_tiocmget, */
2686 /* .tiocmset = serial_tiocmset, */
2687 .hangup
= hso_serial_hangup
,
2691 /* AJB, called by hso_init() once per load */
2692 static int hso_serial_init(void)
2695 D1("Starting hso_serial");
2697 tty_drv
= alloc_tty_driver(HSO_SERIAL_TTY_MINORS
);
2701 /* register the tty driver */
2702 tty_drv
->magic
= TTY_DRIVER_MAGIC
;
2703 tty_drv
->owner
= THIS_MODULE
;
2704 tty_drv
->driver_name
= "hso";
2705 tty_drv
->name
= "ttyHS";
2707 #ifdef HSO_SERIAL_TTY_MAJOR
2708 tty_drv
->major
= HSO_SERIAL_TTY_MAJOR
;
2710 tty_drv
->minor_start
= 0;
2711 tty_drv
->num
= HSO_SERIAL_TTY_MINORS
; /* FIXME - can we get away without this here? */
2713 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
2714 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
2715 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
2716 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
;
2718 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2721 tty_drv
->init_termios
= tty_std_termios
;
2722 tty_drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2724 tty_drv
->termios
= hso_serial_termios
;
2725 tty_drv
->termios_locked
= hso_serial_termios_locked
;
2727 tty_set_operations(tty_drv
, &hso_serial_ops
);
2729 result
= tty_register_driver(tty_drv
);
2731 err("%s - tty_register_driver failed(%d)", __FUNCTION__
,result
);
2735 D1("end hso_serial");
2740 /* AJB, called by hso_exit() once per unload */
2741 static void hso_serial_exit(void)
2743 struct hso_serial
*serial
= NULL
;
2747 D1("Stopping hso_serial");
2749 for (i
= 0; i
< HSO_SERIAL_TTY_MINORS
; i
++) {
2750 serial
= get_serial_by_index(i
);
2751 if (serial
!= NULL
) {
2753 while (serial
->open_count
> 0) {
2754 __hso_serial_close(serial
);
2756 hso_serial_stop(serial
->odev
->usb
, i
, serial
->odev
);
2761 result
= tty_unregister_driver(tty_drv
);
2763 err("%s - tty_unregister_driver failed(%d)", __FUNCTION__
,result
);
2768 * Module definitions
2770 module_init(hso_init
);
2771 module_exit(hso_exit
);
2773 MODULE_AUTHOR(MOD_AUTHOR
);
2774 MODULE_DESCRIPTION("USB High Speed Option driver");
2775 MODULE_LICENSE("GPL");
2777 MODULE_DEVICE_TABLE(usb
, hso_ids
);
2779 MODULE_PARM_DESC(debug
, "Level of debug [0x01 | 0x02 | 0x04 | 0x08]");
2780 module_param(debug
, int, S_IRUGO
| S_IWUSR
);