MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / hso.c
blob59b54f7093c7a2940a1dab8a80fca91933cf46df
1 /*
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
21 * 2.4 driver
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
35 * 0.7 Filip Aben
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>
62 /* #define DEBUG */
63 #define HSO_PROC
65 #ifdef HSO_PROC
66 #include <linux/proc_fs.h>
67 #endif
69 #include <net/arp.h>
70 #include <linux/ip.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)
158 #define D D1
160 #define QFO(fmt, args...) do { \
161 printk( KERN_INFO "hso: " fmt "\n", ##args); \
162 } while(0)
164 #if 0
165 #define DUMP(buf_, len_) \
166 do { \
167 char info_[256]; \
168 u8 i_, count_=0; \
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");} \
174 if (len_) { \
175 NFO("%d: dump[%s]", len_, info_); \
177 } while (0)
178 #endif
180 #ifdef DEBUG
181 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
182 unsigned int len)
184 u8 i = 0;
186 printk("[%d:%s]: len %d", line_count, func_name, len);
188 for (i = 0; i < len; i++) {
189 if (!(i % 16))
190 printk("\n 0x%03x: ", i);
191 printk("%02x ", (unsigned char)buf[i]);
193 printk("\n");
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)
197 #endif
199 enum type_intf {
200 MUX_INTERFACE,
201 QXDM_INTERFACE,
202 CS_INTERFACE
205 enum pkt_parse_state {
206 WAIT_IP,
207 WAIT_DATA,
208 WAIT_SYNC
211 struct option_descriptor {
212 u8 length;
213 u8 descriptor_type;
214 u8 enabled_ports;
215 } __attribute__ ((packed)) ;
217 struct hso_priv {
218 struct net_device_stats stats;
219 struct net_device *net;
220 struct usb_device *usb;
221 struct option_descriptor option_info;
222 unsigned long flags;
223 u32 properties;
225 struct proc_dir_entry *ourproc;
227 int mux_ep_intr;
228 int mux_ep_intr_size;
229 void *mux_intr_buf;
230 struct urb *mux_intr_urb;
232 int mux_ep_bulk_in;
233 int mux_ep_bulk_out;
234 int mux_ep_bulk_out_size;
235 int mux_ep_bulk_in_size;
236 int mux_bInterval;
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;
242 spinlock_t net_lock;
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;
249 __u16 bcdCDC;
250 __u16 wMaxSegmentSize;
251 __u16 wNumberMCFilters;
252 __u16 mode_flags;
254 /* struct usb_ctrlrequest ctrl_req; */
257 struct hso_serial {
258 int magic;
259 u8 minor;
260 u8 mux;
261 enum type_intf type;
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. */
266 struct urb *rx_urb;
267 u8 *rx_data;
268 u16 rx_data_length; /* should contain allocated length */
270 struct urb *tx_urb;
271 u8 *tx_data;
272 u16 tx_data_length; /* should contain allocated length */
273 u16 tx_data_count;
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 */
278 int ep_bulk_out;
279 int ep_bulk_out_size;
280 int ep_bulk_in_size;
282 unsigned long flags;
284 int (*write_data) (struct hso_serial *serial);
286 /* from usb_serial_port */
287 struct tty_struct *tty;
288 int open_count;
289 struct semaphore sem;
290 void *private;
294 Globals
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];
338 #else
339 static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
340 static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
341 #endif
344 * Prototypes
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);
356 #else
357 static void _hso_serial_set_termios(struct tty_struct *tty,
358 struct ktermios *old);
359 #endif
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);
365 #else
366 static int hso_mux_submit_intr_urb(struct hso_priv *odev,gfp_t gfp);
367 #endif
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));
408 #endif
411 static int get_free_serial_index(void)
413 int index;
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);
420 return index;
424 spin_unlock(&serial_table_lock);
426 ERR("no free serial devices in table");
428 return -1;
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);
446 return serial;
449 static struct hso_serial *get_serial_by_odev_and_mux(struct hso_priv *odev,
450 unsigned mux)
452 struct hso_serial *serial;
453 int i;
455 spin_lock(&serial_table_lock);
457 for(i = 0;i < HSO_SERIAL_TTY_MINORS;i++) {
458 if((serial = serial_table[i]) == NULL)
459 continue;
461 if(serial->odev == odev && serial->mux == mux) {
462 spin_unlock(&serial_table_lock);
463 return serial;
467 spin_unlock(&serial_table_lock);
469 return NULL;
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) {
477 return 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)
487 int len = 0;
489 D1("count: %d", count);
491 len += snprintf(buf + len, count - len, "debug: %02x\n", debug);
493 return len;
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)
500 int len = 0;
501 int i;
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)
510 continue;
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);
520 return len;
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,
527 unsigned int count)
529 unsigned short temp_bytes = 0, buffer_offset = 0, frame_len;
530 unsigned char *tmp_rx_buf;
531 struct ethhdr *eth_head;
533 #ifdef DEBUG
534 D("Rx %d bytes", count);
535 DUMP(ip_pkt, min(128, count));
536 #endif
538 while (count) {
539 switch (odev->rx_parse_state) {
540 case WAIT_IP: /* waiting for IP header. */
541 /* wanted bytes - size of ip header */
542 temp_bytes =
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,
547 temp_bytes);
549 odev->rx_buf_size += temp_bytes;
550 buffer_offset += temp_bytes;
551 odev->rx_buf_missing -= temp_bytes;
552 count -= 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;
560 continue;
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;
569 return;
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));
584 /* ETH_HLEN */
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;
591 break;
593 case WAIT_DATA:
594 temp_bytes =
595 (count <
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;
604 count -= 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));
615 #if 0
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);
619 #endif
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 */
625 #ifdef DEBUG
626 DUMP(ip_pkt, min(128, count));
627 #endif
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;
643 break;
645 case WAIT_SYNC:
646 D(" W_S");
647 count = 0;
648 break;
649 default:
650 D(" ");
651 count--;
652 break;
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)
661 #else
662 static void read_bulk_callback(struct urb *urb)
663 #endif
665 struct hso_priv *odev = urb->context;
666 struct net_device *net;
667 int res;
668 unsigned long flags;
669 int status = urb->status; /* preparation for removal of
670 status from struct urb */
672 if (status) {
673 if(status != -ESHUTDOWN) {
674 D1("nonzero bulk status received: %d", status);
676 return;
679 /* Sanity check */
680 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
681 D1("BULK IN callback but driver is not active!");
682 return;
685 net = odev->net;
686 if (!netif_device_present(net)) {
687 /* Somebody killed our network interface... */
688 return;
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.
702 usb_fill_bulk_urb(
703 urb,
704 odev->usb,
705 usb_rcvbulkpipe(odev->usb, odev->mux_ep_bulk_in),
706 urb->transfer_buffer,
707 MUX_BULK_RX_BUF_SIZE,
708 read_bulk_callback,
709 odev);
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)
722 #else
723 static void write_bulk_callback(struct urb *urb)
724 #endif
726 struct hso_priv *odev = urb->context;
727 int status = urb->status;
729 D1("-----------------------------------");
731 /* Sanity check */
732 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
733 ERR("write_bulk_callback: device not running");
734 return;
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");
740 return;
743 if (status) {
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)
755 #else
756 static void ctrl_callback(struct urb *urb)
757 #endif
759 struct hso_serial *serial = urb->context;
760 struct usb_ctrlrequest *req = NULL;
761 int status = urb->status;
763 if (!serial) {
764 return;
767 req = (struct usb_ctrlrequest *)(urb->setup_packet);
769 D4("\n-------------------- got ctrl callback %02x ---------------------", status);
770 #if 0
771 /* This is old values */
772 if (req) {
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);
779 #endif
781 if (status) {
782 if(status != -ESHUTDOWN) {
783 D1("nonzero ctrl status received: %d", status);
785 return;
788 D4("actual length = %d\n", urb->actual_length);
790 #ifdef DEBUG
791 DUMP1(urb->transfer_buffer, urb->actual_length);
792 #endif
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);
804 } else {
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);
816 /* Diag/CS port only
817 Interrupt state
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)
821 #else
822 static void hso_std_serial_read_bulk_callback(struct urb *urb)
823 #endif
825 struct hso_serial *serial = urb->context;
826 int res;
827 int status = urb->status;
828 /* struct usb_ctrlrequest *req = NULL; */
830 if (!serial) {
831 return;
834 D4("\n-------------------- got serial_read_bulk callback %02x ---------------------", status);
836 if (status) {
837 if(status != -ESHUTDOWN) {
838 D1("nonzero read bulk status received: %d", status);
840 return;
843 D1("actual length = %d\n", urb->actual_length);
844 #ifdef DEBUG
845 DUMP1(urb->transfer_buffer, urb->actual_length);
846 #endif
848 if (serial->open_count == 0) /* No one is listening, dont resubmit it.. */
849 return;
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);
856 } else {
857 D2(" port %d, status = %d for read urb", serial->minor, status);
858 return;
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,
864 serial->odev->usb,
865 usb_rcvbulkpipe(serial->odev->usb,
866 serial->ep_bulk_in),
867 serial->rx_data,
868 serial->rx_data_length,
869 hso_std_serial_read_bulk_callback,
870 serial);
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);
880 /* Diag/CS port only
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)
884 #else
885 static void hso_std_serial_write_bulk_callback(struct urb *urb)
886 #endif
888 struct hso_serial *serial = urb->context;
889 int status = urb->status;
891 if (!serial)
892 return;
894 D1(" ");
896 clear_bit(HSO_SERIAL_FLAG_TX_SENT, &serial->flags);
898 if (status) {
899 if(status != -ESHUTDOWN) {
900 D1("nonzero write bulk status received: %d", status);
902 return;
905 return;
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,
911 u32 size)
913 int res = 0;
914 int pipe = -1;
916 /* Sanity check */
917 if (!serial || !ctrl_urb || !ctrl_req) {
918 ERR("Wrong arguments ");
919 return -EINVAL;
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);
937 #ifdef DEBUG
938 DUMP(ctrl_urb_data, size);
939 #endif
942 D2("%s command (%02x) len: %d, port: %d",
943 type == GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
944 ctrl_req->bRequestType, ctrl_req->wLength, port);
946 /* Load ctrl urb */
947 ctrl_urb->transfer_flags = 0;
949 usb_fill_control_urb(ctrl_urb,
950 serial->odev->usb,
951 pipe,
952 (u8 *) ctrl_req,
953 ctrl_urb_data,
954 size,
955 ctrl_callback,
956 serial);
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);
961 return res;
964 return size;
967 static int hso_mux_serial_read(struct hso_priv *odev, struct hso_serial *serial)
969 int rc = 0;
971 memset(serial->rx_data, 0, CTRL_URB_RX_SIZE); /* rx_data_length */
973 rc = mux_device_request(serial,
974 GET_ENCAPSULATED_RESPONSE,
975 serial->mux,
976 serial->rx_urb,
977 &serial->ctrl_req_rx,
978 serial->rx_data, serial->rx_data_length);
979 return rc;
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)
987 #else
988 static void intr_callback(struct urb *urb)
989 #endif
991 struct hso_priv *odev = urb->context;
992 struct hso_serial *serial;
993 unsigned char *port_req;
994 int status = urb->status;
995 int i;
997 if (!odev) {
998 return;
1001 D4("\n---------------------- got intr callback %02x ---------------------", status);
1003 if (status) {
1004 if(status != -ESHUTDOWN) {
1005 D1("%s intr status %d", odev->net->name, status);
1007 return;
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,
1019 &serial->flags)) {
1020 /* Setup and send a ctrl req read on port i */
1021 hso_mux_serial_read(odev,serial);
1022 } else {
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.");
1035 return 0;
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("-----------------------------------");
1053 if (!odev) {
1054 return;
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);
1072 int res;
1074 /* Tell the kernel, "No more frames 'til we are done
1075 with this one.'
1077 netif_stop_queue(net);
1079 skb_pull(skb, ETH_HLEN);
1081 #ifdef DEBUG
1082 DUMP1(skb->data, skb->len);
1083 #endif
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,
1092 skb->len,
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);
1105 } else {
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. */
1112 dev_kfree_skb(skb);
1114 return res;
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);
1126 int res;
1127 int i;
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);
1133 return -EIO;
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],
1147 odev->usb,
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],
1156 GFP_KERNEL))) {
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);
1166 return 0;
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);
1173 int i;
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.... */
1192 return 0;
1195 static int netdev_ethtool_ioctl(struct net_device *net, struct ifreq *rq)
1197 struct hso_priv *odev = netdev_priv(net);
1198 u32 cmd;
1199 char tmp[40];
1201 if (copy_from_user(&cmd, rq->ifr_data,sizeof(cmd)))
1202 return -EFAULT;
1204 switch (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,
1212 odev->usb->devnum);
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)))
1220 return -EFAULT;
1222 return 0;
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)))
1230 return -EFAULT;
1232 return 0;
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);
1243 switch (cmd) {
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;
1248 return 0;
1250 case SIOCETHTOOL:
1251 return netdev_ethtool_ioctl(net, rq);
1253 default:
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);
1262 int i;
1263 __u8 *buf;
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);
1272 odev->mode_flags =
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",
1279 net->name);
1280 odev->mode_flags =
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);
1286 odev->mode_flags =
1287 MODE_FLAG_ALL_MULTICAST | MODE_FLAG_DIRECTED |
1288 MODE_FLAG_BROADCAST | MODE_FLAG_MULTICAST;
1289 } else {
1290 /* do multicast filtering in hardware */
1291 struct dev_mc_list *mclist;
1292 D1("%s: set multicast filters", net->name);
1293 odev->mode_flags =
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?");
1298 goto exit;
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);
1304 #if 0
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 */
1310 HZ); /* timeout */
1311 #endif
1312 kfree(buf);
1315 exit:
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)
1323 int i = 0;
1325 if (length != 3) {
1326 ERR("Extralen don't contain expected data! length:%d", length);
1327 return -1;
1330 /* Length */
1331 odev->option_info.length = data[i++];
1333 /* Type */
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);
1339 return -1;
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);
1350 return 0;
1353 static int get_mux_endpoints(struct usb_interface *intf, struct hso_priv *odev)
1355 int i;
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,
1370 odev)) {
1371 ERR("Could not get option specific data, not our device then");
1372 return -1;
1375 odev->mux_ep_intr =
1376 endp->bEndpointAddress & 0x7F;
1377 odev->mux_bInterval = endp->bInterval;
1378 odev->mux_ep_intr_size = endp->wMaxPacketSize;
1379 } else {
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;
1385 } else {
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,
1401 odev->mux_ep_intr);
1402 return 0;
1404 return -1;
1407 static int get_std_serial_endpoints(struct usb_interface *intf, struct hso_serial *serial)
1409 int i;
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)) {
1420 continue;
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;
1427 } else {
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,
1442 serial->ep_bulk_in,
1443 serial->ep_bulk_out);
1444 return 0;
1446 return -1;
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;
1457 int retval = 0;
1458 int actual_length;
1461 u8 UmsSwitchCmd[] =
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;
1479 } else {
1480 endp = &iface_desc->endpoint[1].desc;
1483 if ( 0 != usb_bulk_msg (usb,
1484 usb_sndbulkpipe(usb, endp->bEndpointAddress),
1485 (void*)UmsSwitchCmd,
1486 UMS_SWITCH_CMD_LEN,
1487 &actual_length,
1488 0)) {
1489 WARN("send UMS switch failed");
1490 } else {
1491 D1("UMS switch done");
1492 retval = 1;
1496 return retval;
1500 static int check_and_claim_interfaces(struct usb_device *usb,
1501 struct hso_priv *odev)
1503 struct usb_host_config *conf = usb->actconfig;
1504 int i;
1505 int result = 0;
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);
1517 result = -1;
1520 if (result)
1521 return result;
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");
1528 goto exit;
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;
1543 exit:
1544 return -EIO;
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;
1561 return 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];
1572 int i;
1573 int len;
1574 unsigned char buffer[13];
1576 /* Let's assume we don't get anything */
1577 mac_addr[0] = 0x00;
1578 mac_addr[1] = 0x00;
1579 mac_addr[2] = 0x00;
1580 mac_addr[3] = 0x00;
1581 mac_addr[4] = 0x00;
1582 mac_addr[5] = 0x00;
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);
1587 return;
1590 /* Sanity check */
1591 if (len != 12) {
1592 /* You gotta love failing sanity checks */
1593 ERR("Attempting to get MAC address returned %d bytes", len);
1594 return;
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);
1601 } else {
1602 mac_addr[i] =
1603 (hex2dec(buffer[2 * i]) << 4) +
1604 hex2dec(buffer[2 * i + 1]);
1608 mac_addr[0] = 0x00;
1609 mac_addr[1] = 0x03;
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)
1622 int len;
1624 if (!buf) {
1625 ERR("No buffer?");
1626 return;
1629 buf[0] = 0x00;
1631 if (string_num) {
1632 /* Put it into its buffer */
1633 len = usb_string(odev->usb, string_num, buf, buf_len);
1634 /* Just to be safe */
1635 buf[len] = 0x00;
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",
1658 odev->net->name,
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)
1665 int i;
1667 if (!odev)
1668 return;
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 */
1695 int i;
1697 if (!odev)
1698 return -1;
1700 if (!(odev->mux_intr_urb = usb_alloc_urb(0, GFP_KERNEL))) {
1701 ERR("Could not allocate urb?");
1702 return -1;
1704 if (!(odev->mux_intr_buf = kzalloc(MUX_INTR_BUF_SIZE, GFP_KERNEL))) {
1705 ERR("Could not allocate buf?");
1706 return -1;
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?");
1712 return -1;
1714 if (!(odev->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE, GFP_KERNEL))) {
1715 ERR("Could not allocate buf?");
1716 return -1;
1720 if (!(odev->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
1721 ERR("Could not allocate urb?");
1722 return -1;
1724 if (!(odev->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL))) {
1725 ERR("Could not allocate buf?");
1726 return -1;
1729 return 0;
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));
1745 ether_setup(net);
1747 SET_MODULE_OWNER(net);
1748 net->open = hso_net_open;
1749 net->stop = hso_net_close;
1750 /* set config */
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;
1759 /* features */
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)
1776 #else
1777 static int hso_mux_submit_intr_urb(struct hso_priv *odev,gfp_t gfp)
1778 #endif
1780 int res = 0;
1782 if (test_bit(HSO_CARD_UNPLUG, &odev->flags)) {
1783 WARN("%s closing so not rearming mux_intr_urb", __FUNCTION__);
1784 return 0;
1787 D5("Arm and submit the interrupt URB");
1789 usb_fill_int_urb(odev->mux_intr_urb,
1790 odev->usb,
1791 usb_rcvintpipe(odev->usb, odev->mux_ep_intr),
1792 odev->mux_intr_buf,
1793 MUX_INTR_BUF_SIZE,
1794 intr_callback,
1795 odev,
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);
1800 goto exit;
1802 exit:
1803 return 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;
1811 int mux = 0;
1812 int i, res;
1813 struct net_device *net;
1814 struct usb_device *usb = interface_to_usbdev(interface);
1815 int minor, num_if;
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) {
1824 return 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)) {
1830 return 0;
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);
1839 if (!net) {
1840 ERR("Unable to create ethernet device");
1841 return -ENOMEM;
1843 odev = netdev_priv(net);
1845 res = register_netdev(net);
1846 if (res) {
1847 ERR("Unable to register ethernet device");
1848 goto exit;
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... */
1865 odev->usb = usb;
1866 odev->net = net;
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);
1874 /* Muxed ports */
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)
1878 break;
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 */
1888 /* C/S port */
1889 if( ( num_if > 2 ) && ((minor = get_free_serial_index()) >= 0) ) {
1890 hso_serial_start(odev, minor, CS_INTERFACE, 2); /* interface num */
1893 usb_get_dev(usb);
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 **");
1905 return 0;
1907 exit_release_interfaces:
1908 /* FIXME AJB, release the usb interfaces */
1910 exit_unregister:
1911 unregister_netdev(net);
1913 exit:
1914 free_netdev(net);
1916 return -EIO;
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");
1936 return;
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);
1956 usb_put_dev(usb);
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,
1976 .probe = hso_probe,
1977 .disconnect = hso_disconnect,
1978 .id_table = hso_ids,
1981 int __init hso_init(void)
1983 int i,rc = 0;
1985 D1("%s", version);
1987 #ifdef HSO_PROC
1988 hso_proc_dir = proc_mkdir(driver_name, proc_root_driver);
1989 hso_proc_dir_devices = proc_mkdir("devices", hso_proc_dir);
1990 #endif
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;
1997 hso_serial_init();
1999 if ((rc = usb_register(&hso_driver))) {
2000 ERR("Could not register hso driver? error: %d", rc);
2001 return rc;
2004 #ifdef HSO_PROC
2005 create_proc_read_entry("options", 0, hso_proc_dir, hso_proc_options,NULL);
2006 #endif
2007 return 0;
2010 /* usb_deregister calls disconnect of registered drivers. */
2011 void __exit hso_exit(void)
2013 hso_serial_exit();
2015 usb_deregister(&hso_driver);
2017 #ifdef HSO_PROC
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);
2021 #endif
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,
2032 int count);
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);
2043 #else
2044 static void hso_serial_set_termios(struct tty_struct *tty,
2045 struct ktermios *old);
2046 #endif
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");
2060 return -ENODEV;
2063 down(&serial->sem);
2065 D1("Opening %d", serial->minor);
2067 tty->driver_data = serial;
2069 serial->tty = tty;
2071 odev = serial->odev;
2073 serial->open_count++;
2074 if (serial->open_count == 1) {
2075 tty->low_latency = 1;
2077 serial->flags = 0;
2079 /* Force default termio settings */
2080 _hso_serial_set_termios(tty, NULL);
2082 } else {
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) {
2089 int res;
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);
2100 up(&serial->sem);
2102 return 0;
2105 static void __hso_serial_close(struct hso_serial *serial)
2107 D1(" ");
2109 if (!serial->open_count) {
2110 err("%s - port %d: not open", __FUNCTION__, serial->minor);
2111 return;
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);
2127 /*close ? */
2128 serial->open_count = 0;
2129 if (serial->tty) {
2130 serial->tty->driver_data = NULL;
2131 serial->tty = NULL;
2133 return;
2137 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
2139 struct hso_serial *serial = NULL;
2140 D1(" ");
2142 if (tty == NULL || tty->driver_data == NULL) {
2143 D1("(tty == NULL || tty->driver_data == NULL)");
2144 return;
2147 serial = tty->driver_data;
2148 down(&serial->sem);
2149 if (tty->driver_data) {
2150 __hso_serial_close(serial);
2152 up(&serial->sem);
2155 static void hso_serial_hangup(struct tty_struct *tty)
2157 D1("hang up");
2160 /* Called once per device shutdown */
2161 /* usb unused */
2162 static void hso_serial_disconnect(struct usb_device *usb, struct hso_priv *odev)
2164 struct hso_serial *serial;
2165 int i;
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 */
2171 down(&serial->sem);
2172 while (serial->open_count > 0) {
2173 __hso_serial_close(serial);
2175 hso_serial_stop(usb, i, odev);
2176 up(&serial->sem);
2182 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
2183 int count)
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__,
2190 __LINE__);
2191 return -ENODEV;
2194 down(&serial->sem);
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");
2201 udelay(500);
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);
2215 } else {
2216 clear_bit(HSO_SERIAL_FLAG_TX_SENT, &serial->flags);
2217 retval = -ENODEV;
2220 up(&serial->sem);
2221 return retval;
2224 static int hso_mux_serial_write_data(struct hso_serial *serial)
2226 return mux_device_request(serial,
2227 SEND_ENCAPSULATED_COMMAND,
2228 serial->mux,
2229 serial->tx_urb,
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;
2237 int res;
2239 usb_fill_bulk_urb(serial->tx_urb,
2240 serial->odev->usb,
2241 usb_sndbulkpipe(serial->odev->usb,
2242 serial->ep_bulk_out),
2243 serial->tx_data,
2244 serial->tx_data_count,
2245 hso_std_serial_write_bulk_callback,
2246 serial);
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);
2252 return res;
2255 return count;
2258 static int hso_serial_write_room(struct tty_struct *tty)
2260 struct hso_serial *serial = get_serial_by_tty(tty);
2261 int room = 0;
2263 if (serial == NULL)
2264 return 0;
2266 down(&serial->sem);
2268 if(!test_bit(HSO_SERIAL_FLAG_TX_SENT, &serial->flags))
2269 room = serial->tx_data_length;
2271 up(&serial->sem);
2273 D4("room = %d ", room);
2275 return room;
2278 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
2280 struct hso_serial *serial = get_serial_by_tty(tty);
2281 int chars = 0;
2283 if (serial == NULL) {
2284 return 0;
2287 down(&serial->sem);
2289 if(test_bit(HSO_SERIAL_FLAG_TX_SENT, &serial->flags))
2290 chars = serial->tx_urb->transfer_buffer_length;
2292 up(&serial->sem);
2294 D4("chars = %d ", chars);
2296 return 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");
2305 return;
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) )
2316 int i;
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);
2326 #else
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);
2330 #endif
2333 #if 0
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); */
2342 #endif
2346 static void hso_serial_throttle(struct tty_struct *tty)
2348 D1(" ");
2351 static void hso_serial_unthrottle(struct tty_struct *tty)
2353 D1(" ");
2356 static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
2357 unsigned int cmd, unsigned long arg)
2359 D4(" ");
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)
2366 #else
2367 static void _hso_serial_set_termios(struct tty_struct *tty,
2368 struct ktermios *old)
2369 #endif
2371 struct hso_serial *serial = get_serial_by_tty(tty);
2373 if ((!tty) || (!tty->termios) || (!serial)) {
2374 ERR("no tty structures");
2375 return;
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);
2416 return;
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)
2426 #else
2427 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
2428 #endif
2430 struct hso_serial *serial = get_serial_by_tty(tty);
2432 if ((!tty) || (!tty->termios) || (!serial)) {
2433 D1("no tty structures");
2434 return;
2437 D1("port %d", serial->minor);
2439 down(&serial->sem);
2440 if (serial->open_count)
2441 _hso_serial_set_termios(tty, old);
2442 up(&serial->sem);
2444 return;
2447 static void hso_serial_break(struct tty_struct *tty, int break_state)
2449 D1(" ");
2452 static int hso_serial_read_proc(char *page, char **start, off_t off, int count,
2453 int *eof, void *data)
2455 return 0;
2458 #if 0
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);
2466 if (!serial)
2467 return;
2469 tty = serial->tty;
2470 if (!tty)
2471 return;
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);
2481 #endif
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__);
2489 return NULL;
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;
2505 switch(type) {
2506 case MUX_INTERFACE:
2507 switch(num) {
2508 case 0:
2509 QFO("Multiplexed Control channel present");
2510 break;
2511 case 1:
2512 QFO("Multiplexed Application channel present");
2513 break;
2514 case 2:
2515 QFO("Multiplexed PC/SC channel present");
2516 break;
2517 case 3:
2518 QFO("Multiplexed GPS channel present");
2519 break;
2520 case 4:
2521 QFO("Multiplexed Application 2 channel present");
2522 break;
2523 case 5:
2524 case 6:
2525 case 7:
2526 QFO("Multiplexed Reserved(%d) channel present",num);
2527 break;
2529 break;
2530 case QXDM_INTERFACE:
2531 QFO("QXDM port present");
2532 break;
2533 case CS_INTERFACE:
2534 QFO("Circuit Switched port present");
2535 break;
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");
2541 goto exit;
2545 if (!(serial->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
2546 ERR("Could not allocate urb?");
2547 goto exit;
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__);
2557 goto exit;
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?");
2567 goto exit;
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__);
2577 goto exit;
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;
2587 } else {
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);
2597 return serial;
2599 exit:
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);
2613 if (serial) {
2614 kfree(serial);
2616 return NULL;
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");
2630 return;
2633 if (serial->odev != odev) {
2634 ERR("Trying to deregister a serial port belonging to a different device");
2635 return;
2638 serial->odev = NULL;
2639 serial->minor = 0;
2640 serial->mux = 0;
2641 serial->magic = 0;
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;
2665 kfree(serial);
2666 serial = 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)
2694 int result;
2695 D1("Starting hso_serial");
2697 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2698 if (!tty_drv)
2699 return -ENOMEM;
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;
2709 #endif
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;
2717 #else
2718 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2719 #endif
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);
2730 if (result) {
2731 err("%s - tty_register_driver failed(%d)", __FUNCTION__,result);
2732 return result;
2735 D1("end hso_serial");
2737 return 0;
2740 /* AJB, called by hso_exit() once per unload */
2741 static void hso_serial_exit(void)
2743 struct hso_serial *serial = NULL;
2744 int i;
2745 int result;
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) {
2752 down(&serial->sem);
2753 while (serial->open_count > 0) {
2754 __hso_serial_close(serial);
2756 hso_serial_stop(serial->odev->usb, i, serial->odev);
2757 up(&serial->sem);
2761 result = tty_unregister_driver(tty_drv);
2762 if (result) {
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);