USB: HSO: make tty_operations const
[linux-2.6/mini2440.git] / drivers / net / usb / hso.c
blob35a7772cf1a6cb85ae022668895f392c52e77cd6
1 /******************************************************************************
3 * Driver for Option High Speed Mobile Devices.
5 * Copyright (C) 2008 Option International
6 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
7 * <ajb@spheresystems.co.uk>
8 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
9 * Copyright (C) 2008 Novell, Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 * USA
26 *****************************************************************************/
28 /******************************************************************************
30 * Description of the device:
32 * Interface 0: Contains the IP network interface on the bulk end points.
33 * The multiplexed serial ports are using the interrupt and
34 * control endpoints.
35 * Interrupt contains a bitmap telling which multiplexed
36 * serialport needs servicing.
38 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
39 * port is opened, as this have a huge impact on the network port
40 * throughput.
42 * Interface 2: Standard modem interface - circuit switched interface, should
43 * not be used.
45 *****************************************************************************/
47 #include <linux/sched.h>
48 #include <linux/slab.h>
49 #include <linux/init.h>
50 #include <linux/delay.h>
51 #include <linux/netdevice.h>
52 #include <linux/module.h>
53 #include <linux/ethtool.h>
54 #include <linux/usb.h>
55 #include <linux/timer.h>
56 #include <linux/tty.h>
57 #include <linux/tty_driver.h>
58 #include <linux/tty_flip.h>
59 #include <linux/kmod.h>
60 #include <linux/rfkill.h>
61 #include <linux/ip.h>
62 #include <linux/uaccess.h>
63 #include <linux/usb/cdc.h>
64 #include <net/arp.h>
65 #include <asm/byteorder.h>
68 #define DRIVER_VERSION "1.2"
69 #define MOD_AUTHOR "Option Wireless"
70 #define MOD_DESCRIPTION "USB High Speed Option driver"
71 #define MOD_LICENSE "GPL"
73 #define HSO_MAX_NET_DEVICES 10
74 #define HSO__MAX_MTU 2048
75 #define DEFAULT_MTU 1500
76 #define DEFAULT_MRU 1500
78 #define CTRL_URB_RX_SIZE 1024
79 #define CTRL_URB_TX_SIZE 64
81 #define BULK_URB_RX_SIZE 4096
82 #define BULK_URB_TX_SIZE 8192
84 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
85 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
86 #define MUX_BULK_RX_BUF_COUNT 4
87 #define USB_TYPE_OPTION_VENDOR 0x20
89 /* These definitions are used with the struct hso_net flags element */
90 /* - use *_bit operations on it. (bit indices not values.) */
91 #define HSO_NET_RUNNING 0
93 #define HSO_NET_TX_TIMEOUT (HZ*10)
95 /* Serial port defines and structs. */
96 #define HSO_SERIAL_FLAG_RX_SENT 0
98 #define HSO_SERIAL_MAGIC 0x48534f31
100 /* Number of ttys to handle */
101 #define HSO_SERIAL_TTY_MINORS 256
103 #define MAX_RX_URBS 2
105 #define get_serial_by_tty(x) \
106 (x ? (struct hso_serial *)x->driver_data : NULL)
108 /*****************************************************************************/
109 /* Debugging functions */
110 /*****************************************************************************/
111 #define D__(lvl_, fmt, arg...) \
112 do { \
113 printk(lvl_ "[%d:%s]: " fmt "\n", \
114 __LINE__, __func__, ## arg); \
115 } while (0)
117 #define D_(lvl, args...) \
118 do { \
119 if (lvl & debug) \
120 D__(KERN_INFO, args); \
121 } while (0)
123 #define D1(args...) D_(0x01, ##args)
124 #define D2(args...) D_(0x02, ##args)
125 #define D3(args...) D_(0x04, ##args)
126 #define D4(args...) D_(0x08, ##args)
127 #define D5(args...) D_(0x10, ##args)
129 /*****************************************************************************/
130 /* Enumerators */
131 /*****************************************************************************/
132 enum pkt_parse_state {
133 WAIT_IP,
134 WAIT_DATA,
135 WAIT_SYNC
138 /*****************************************************************************/
139 /* Structs */
140 /*****************************************************************************/
142 struct hso_shared_int {
143 struct usb_endpoint_descriptor *intr_endp;
144 void *shared_intr_buf;
145 struct urb *shared_intr_urb;
146 struct usb_device *usb;
147 int use_count;
148 int ref_count;
149 struct mutex shared_int_lock;
152 struct hso_net {
153 struct hso_device *parent;
154 struct net_device *net;
155 struct rfkill *rfkill;
157 struct usb_endpoint_descriptor *in_endp;
158 struct usb_endpoint_descriptor *out_endp;
160 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
161 struct urb *mux_bulk_tx_urb;
162 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
163 void *mux_bulk_tx_buf;
165 struct sk_buff *skb_rx_buf;
166 struct sk_buff *skb_tx_buf;
168 enum pkt_parse_state rx_parse_state;
169 spinlock_t net_lock;
171 unsigned short rx_buf_size;
172 unsigned short rx_buf_missing;
173 struct iphdr rx_ip_hdr;
175 unsigned long flags;
178 struct hso_serial {
179 struct hso_device *parent;
180 int magic;
181 u8 minor;
183 struct hso_shared_int *shared_int;
185 /* rx/tx urb could be either a bulk urb or a control urb depending
186 on which serial port it is used on. */
187 struct urb *rx_urb[MAX_RX_URBS];
188 u8 num_rx_urbs;
189 u8 *rx_data[MAX_RX_URBS];
190 u16 rx_data_length; /* should contain allocated length */
192 struct urb *tx_urb;
193 u8 *tx_data;
194 u8 *tx_buffer;
195 u16 tx_data_length; /* should contain allocated length */
196 u16 tx_data_count;
197 u16 tx_buffer_count;
198 struct usb_ctrlrequest ctrl_req_tx;
199 struct usb_ctrlrequest ctrl_req_rx;
201 struct usb_endpoint_descriptor *in_endp;
202 struct usb_endpoint_descriptor *out_endp;
204 unsigned long flags;
205 u8 rts_state;
206 u8 dtr_state;
207 unsigned tx_urb_used:1;
209 /* from usb_serial_port */
210 struct tty_struct *tty;
211 int open_count;
212 spinlock_t serial_lock;
214 int (*write_data) (struct hso_serial *serial);
217 struct hso_device {
218 union {
219 struct hso_serial *dev_serial;
220 struct hso_net *dev_net;
221 } port_data;
223 u32 port_spec;
225 u8 is_active;
226 u8 usb_gone;
227 struct work_struct async_get_intf;
228 struct work_struct async_put_intf;
230 struct usb_device *usb;
231 struct usb_interface *interface;
233 struct device *dev;
234 struct kref ref;
235 struct mutex mutex;
238 /* Type of interface */
239 #define HSO_INTF_MASK 0xFF00
240 #define HSO_INTF_MUX 0x0100
241 #define HSO_INTF_BULK 0x0200
243 /* Type of port */
244 #define HSO_PORT_MASK 0xFF
245 #define HSO_PORT_NO_PORT 0x0
246 #define HSO_PORT_CONTROL 0x1
247 #define HSO_PORT_APP 0x2
248 #define HSO_PORT_GPS 0x3
249 #define HSO_PORT_PCSC 0x4
250 #define HSO_PORT_APP2 0x5
251 #define HSO_PORT_GPS_CONTROL 0x6
252 #define HSO_PORT_MSD 0x7
253 #define HSO_PORT_VOICE 0x8
254 #define HSO_PORT_DIAG2 0x9
255 #define HSO_PORT_DIAG 0x10
256 #define HSO_PORT_MODEM 0x11
257 #define HSO_PORT_NETWORK 0x12
259 /* Additional device info */
260 #define HSO_INFO_MASK 0xFF000000
261 #define HSO_INFO_CRC_BUG 0x01000000
263 /*****************************************************************************/
264 /* Prototypes */
265 /*****************************************************************************/
266 /* Serial driver functions */
267 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
268 unsigned int set, unsigned int clear);
269 static void ctrl_callback(struct urb *urb);
270 static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
271 static void hso_kick_transmit(struct hso_serial *serial);
272 /* Helper functions */
273 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
274 struct usb_device *usb, gfp_t gfp);
275 static void log_usb_status(int status, const char *function);
276 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
277 int type, int dir);
278 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
279 static void hso_free_interface(struct usb_interface *intf);
280 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
281 static int hso_stop_serial_device(struct hso_device *hso_dev);
282 static int hso_start_net_device(struct hso_device *hso_dev);
283 static void hso_free_shared_int(struct hso_shared_int *shared_int);
284 static int hso_stop_net_device(struct hso_device *hso_dev);
285 static void hso_serial_ref_free(struct kref *ref);
286 static void async_get_intf(struct work_struct *data);
287 static void async_put_intf(struct work_struct *data);
288 static int hso_put_activity(struct hso_device *hso_dev);
289 static int hso_get_activity(struct hso_device *hso_dev);
291 /*****************************************************************************/
292 /* Helping functions */
293 /*****************************************************************************/
295 /* #define DEBUG */
297 #define dev2net(x) (x->port_data.dev_net)
298 #define dev2ser(x) (x->port_data.dev_serial)
300 /* Debugging functions */
301 #ifdef DEBUG
302 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
303 unsigned int len)
305 u8 i = 0;
307 printk(KERN_DEBUG "[%d:%s]: len %d", line_count, func_name, len);
309 for (i = 0; i < len; i++) {
310 if (!(i % 16))
311 printk("\n 0x%03x: ", i);
312 printk("%02x ", (unsigned char)buf[i]);
314 printk("\n");
317 #define DUMP(buf_, len_) \
318 dbg_dump(__LINE__, __func__, buf_, len_)
320 #define DUMP1(buf_, len_) \
321 do { \
322 if (0x01 & debug) \
323 DUMP(buf_, len_); \
324 } while (0)
325 #else
326 #define DUMP(buf_, len_)
327 #define DUMP1(buf_, len_)
328 #endif
330 /* module parameters */
331 static int debug;
332 static int tty_major;
333 static int disable_net;
335 /* driver info */
336 static const char driver_name[] = "hso";
337 static const char tty_filename[] = "ttyHS";
338 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
339 /* the usb driver itself (registered in hso_init) */
340 static struct usb_driver hso_driver;
341 /* serial structures */
342 static struct tty_driver *tty_drv;
343 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
344 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
345 static spinlock_t serial_table_lock;
346 static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
347 static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
349 static const s32 default_port_spec[] = {
350 HSO_INTF_MUX | HSO_PORT_NETWORK,
351 HSO_INTF_BULK | HSO_PORT_DIAG,
352 HSO_INTF_BULK | HSO_PORT_MODEM,
356 static const s32 icon321_port_spec[] = {
357 HSO_INTF_MUX | HSO_PORT_NETWORK,
358 HSO_INTF_BULK | HSO_PORT_DIAG2,
359 HSO_INTF_BULK | HSO_PORT_MODEM,
360 HSO_INTF_BULK | HSO_PORT_DIAG,
364 #define default_port_device(vendor, product) \
365 USB_DEVICE(vendor, product), \
366 .driver_info = (kernel_ulong_t)default_port_spec
368 #define icon321_port_device(vendor, product) \
369 USB_DEVICE(vendor, product), \
370 .driver_info = (kernel_ulong_t)icon321_port_spec
372 /* list of devices we support */
373 static const struct usb_device_id hso_ids[] = {
374 {default_port_device(0x0af0, 0x6711)},
375 {default_port_device(0x0af0, 0x6731)},
376 {default_port_device(0x0af0, 0x6751)},
377 {default_port_device(0x0af0, 0x6771)},
378 {default_port_device(0x0af0, 0x6791)},
379 {default_port_device(0x0af0, 0x6811)},
380 {default_port_device(0x0af0, 0x6911)},
381 {default_port_device(0x0af0, 0x6951)},
382 {default_port_device(0x0af0, 0x6971)},
383 {default_port_device(0x0af0, 0x7011)},
384 {default_port_device(0x0af0, 0x7031)},
385 {default_port_device(0x0af0, 0x7051)},
386 {default_port_device(0x0af0, 0x7071)},
387 {default_port_device(0x0af0, 0x7111)},
388 {default_port_device(0x0af0, 0x7211)},
389 {default_port_device(0x0af0, 0x7251)},
390 {default_port_device(0x0af0, 0x7271)},
391 {default_port_device(0x0af0, 0x7311)},
392 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
393 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
394 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
395 {default_port_device(0x0af0, 0xd033)}, /* Icon-322 */
396 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
397 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
398 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
399 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
400 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
403 MODULE_DEVICE_TABLE(usb, hso_ids);
405 /* Sysfs attribute */
406 static ssize_t hso_sysfs_show_porttype(struct device *dev,
407 struct device_attribute *attr,
408 char *buf)
410 struct hso_device *hso_dev = dev->driver_data;
411 char *port_name;
413 if (!hso_dev)
414 return 0;
416 switch (hso_dev->port_spec & HSO_PORT_MASK) {
417 case HSO_PORT_CONTROL:
418 port_name = "Control";
419 break;
420 case HSO_PORT_APP:
421 port_name = "Application";
422 break;
423 case HSO_PORT_APP2:
424 port_name = "Application2";
425 break;
426 case HSO_PORT_GPS:
427 port_name = "GPS";
428 break;
429 case HSO_PORT_GPS_CONTROL:
430 port_name = "GPS Control";
431 break;
432 case HSO_PORT_PCSC:
433 port_name = "PCSC";
434 break;
435 case HSO_PORT_DIAG:
436 port_name = "Diagnostic";
437 break;
438 case HSO_PORT_DIAG2:
439 port_name = "Diagnostic2";
440 break;
441 case HSO_PORT_MODEM:
442 port_name = "Modem";
443 break;
444 case HSO_PORT_NETWORK:
445 port_name = "Network";
446 break;
447 default:
448 port_name = "Unknown";
449 break;
452 return sprintf(buf, "%s\n", port_name);
454 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
456 /* converts mux value to a port spec value */
457 static u32 hso_mux_to_port(int mux)
459 u32 result;
461 switch (mux) {
462 case 0x1:
463 result = HSO_PORT_CONTROL;
464 break;
465 case 0x2:
466 result = HSO_PORT_APP;
467 break;
468 case 0x4:
469 result = HSO_PORT_PCSC;
470 break;
471 case 0x8:
472 result = HSO_PORT_GPS;
473 break;
474 case 0x10:
475 result = HSO_PORT_APP2;
476 break;
477 default:
478 result = HSO_PORT_NO_PORT;
480 return result;
483 /* converts port spec value to a mux value */
484 static u32 hso_port_to_mux(int port)
486 u32 result;
488 switch (port & HSO_PORT_MASK) {
489 case HSO_PORT_CONTROL:
490 result = 0x0;
491 break;
492 case HSO_PORT_APP:
493 result = 0x1;
494 break;
495 case HSO_PORT_PCSC:
496 result = 0x2;
497 break;
498 case HSO_PORT_GPS:
499 result = 0x3;
500 break;
501 case HSO_PORT_APP2:
502 result = 0x4;
503 break;
504 default:
505 result = 0x0;
507 return result;
510 static struct hso_serial *get_serial_by_shared_int_and_type(
511 struct hso_shared_int *shared_int,
512 int mux)
514 int i, port;
516 port = hso_mux_to_port(mux);
518 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
519 if (serial_table[i]
520 && (dev2ser(serial_table[i])->shared_int == shared_int)
521 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
522 return dev2ser(serial_table[i]);
526 return NULL;
529 static struct hso_serial *get_serial_by_index(unsigned index)
531 struct hso_serial *serial;
532 unsigned long flags;
534 if (!serial_table[index])
535 return NULL;
536 spin_lock_irqsave(&serial_table_lock, flags);
537 serial = dev2ser(serial_table[index]);
538 spin_unlock_irqrestore(&serial_table_lock, flags);
540 return serial;
543 static int get_free_serial_index(void)
545 int index;
546 unsigned long flags;
548 spin_lock_irqsave(&serial_table_lock, flags);
549 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
550 if (serial_table[index] == NULL) {
551 spin_unlock_irqrestore(&serial_table_lock, flags);
552 return index;
555 spin_unlock_irqrestore(&serial_table_lock, flags);
557 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
558 return -1;
561 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
563 unsigned long flags;
564 spin_lock_irqsave(&serial_table_lock, flags);
565 if (serial)
566 serial_table[index] = serial->parent;
567 else
568 serial_table[index] = NULL;
569 spin_unlock_irqrestore(&serial_table_lock, flags);
572 /* log a meaningfull explanation of an USB status */
573 static void log_usb_status(int status, const char *function)
575 char *explanation;
577 switch (status) {
578 case -ENODEV:
579 explanation = "no device";
580 break;
581 case -ENOENT:
582 explanation = "endpoint not enabled";
583 break;
584 case -EPIPE:
585 explanation = "endpoint stalled";
586 break;
587 case -ENOSPC:
588 explanation = "not enough bandwidth";
589 break;
590 case -ESHUTDOWN:
591 explanation = "device disabled";
592 break;
593 case -EHOSTUNREACH:
594 explanation = "device suspended";
595 break;
596 case -EINVAL:
597 case -EAGAIN:
598 case -EFBIG:
599 case -EMSGSIZE:
600 explanation = "internal error";
601 break;
602 default:
603 explanation = "unknown status";
604 break;
606 D1("%s: received USB status - %s (%d)", function, explanation, status);
609 /* Network interface functions */
611 /* called when net interface is brought up by ifconfig */
612 static int hso_net_open(struct net_device *net)
614 struct hso_net *odev = netdev_priv(net);
615 unsigned long flags = 0;
617 if (!odev) {
618 dev_err(&net->dev, "No net device !\n");
619 return -ENODEV;
622 odev->skb_tx_buf = NULL;
624 /* setup environment */
625 spin_lock_irqsave(&odev->net_lock, flags);
626 odev->rx_parse_state = WAIT_IP;
627 odev->rx_buf_size = 0;
628 odev->rx_buf_missing = sizeof(struct iphdr);
629 spin_unlock_irqrestore(&odev->net_lock, flags);
631 hso_start_net_device(odev->parent);
633 /* We are up and running. */
634 set_bit(HSO_NET_RUNNING, &odev->flags);
636 /* Tell the kernel we are ready to start receiving from it */
637 netif_start_queue(net);
639 return 0;
642 /* called when interface is brought down by ifconfig */
643 static int hso_net_close(struct net_device *net)
645 struct hso_net *odev = netdev_priv(net);
647 /* we don't need the queue anymore */
648 netif_stop_queue(net);
649 /* no longer running */
650 clear_bit(HSO_NET_RUNNING, &odev->flags);
652 hso_stop_net_device(odev->parent);
654 /* done */
655 return 0;
658 /* USB tells is xmit done, we should start the netqueue again */
659 static void write_bulk_callback(struct urb *urb)
661 struct hso_net *odev = urb->context;
662 int status = urb->status;
664 /* Sanity check */
665 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
666 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
667 return;
670 /* Do we still have a valid kernel network device? */
671 if (!netif_device_present(odev->net)) {
672 dev_err(&urb->dev->dev, "%s: net device not present\n",
673 __func__);
674 return;
677 /* log status, but don't act on it, we don't need to resubmit anything
678 * anyhow */
679 if (status)
680 log_usb_status(status, __func__);
682 hso_put_activity(odev->parent);
684 /* Tell the network interface we are ready for another frame */
685 netif_wake_queue(odev->net);
688 /* called by kernel when we need to transmit a packet */
689 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
691 struct hso_net *odev = netdev_priv(net);
692 int result;
694 /* Tell the kernel, "No more frames 'til we are done with this one." */
695 netif_stop_queue(net);
696 if (hso_get_activity(odev->parent) == -EAGAIN) {
697 odev->skb_tx_buf = skb;
698 return 0;
701 /* log if asked */
702 DUMP1(skb->data, skb->len);
703 /* Copy it from kernel memory to OUR memory */
704 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
705 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
707 /* Fill in the URB for shipping it out. */
708 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
709 odev->parent->usb,
710 usb_sndbulkpipe(odev->parent->usb,
711 odev->out_endp->
712 bEndpointAddress & 0x7F),
713 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
714 odev);
716 /* Deal with the Zero Length packet problem, I hope */
717 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
719 /* Send the URB on its merry way. */
720 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
721 if (result) {
722 dev_warn(&odev->parent->interface->dev,
723 "failed mux_bulk_tx_urb %d", result);
724 net->stats.tx_errors++;
725 netif_start_queue(net);
726 } else {
727 net->stats.tx_packets++;
728 net->stats.tx_bytes += skb->len;
729 /* And tell the kernel when the last transmit started. */
730 net->trans_start = jiffies;
732 dev_kfree_skb(skb);
733 /* we're done */
734 return result;
737 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
739 struct hso_net *odev = netdev_priv(net);
741 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
742 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
743 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
746 static struct ethtool_ops ops = {
747 .get_drvinfo = hso_get_drvinfo,
748 .get_link = ethtool_op_get_link
751 /* called when a packet did not ack after watchdogtimeout */
752 static void hso_net_tx_timeout(struct net_device *net)
754 struct hso_net *odev = netdev_priv(net);
756 if (!odev)
757 return;
759 /* Tell syslog we are hosed. */
760 dev_warn(&net->dev, "Tx timed out.\n");
762 /* Tear the waiting frame off the list */
763 if (odev->mux_bulk_tx_urb
764 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
765 usb_unlink_urb(odev->mux_bulk_tx_urb);
767 /* Update statistics */
768 net->stats.tx_errors++;
771 /* make a real packet from the received USB buffer */
772 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
773 unsigned int count, unsigned char is_eop)
775 unsigned short temp_bytes;
776 unsigned short buffer_offset = 0;
777 unsigned short frame_len;
778 unsigned char *tmp_rx_buf;
780 /* log if needed */
781 D1("Rx %d bytes", count);
782 DUMP(ip_pkt, min(128, (int)count));
784 while (count) {
785 switch (odev->rx_parse_state) {
786 case WAIT_IP:
787 /* waiting for IP header. */
788 /* wanted bytes - size of ip header */
789 temp_bytes =
790 (count <
791 odev->rx_buf_missing) ? count : odev->
792 rx_buf_missing;
794 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
795 odev->rx_buf_size, ip_pkt + buffer_offset,
796 temp_bytes);
798 odev->rx_buf_size += temp_bytes;
799 buffer_offset += temp_bytes;
800 odev->rx_buf_missing -= temp_bytes;
801 count -= temp_bytes;
803 if (!odev->rx_buf_missing) {
804 /* header is complete allocate an sk_buffer and
805 * continue to WAIT_DATA */
806 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
808 if ((frame_len > DEFAULT_MRU) ||
809 (frame_len < sizeof(struct iphdr))) {
810 dev_err(&odev->net->dev,
811 "Invalid frame (%d) length\n",
812 frame_len);
813 odev->rx_parse_state = WAIT_SYNC;
814 continue;
816 /* Allocate an sk_buff */
817 odev->skb_rx_buf = dev_alloc_skb(frame_len);
818 if (!odev->skb_rx_buf) {
819 /* We got no receive buffer. */
820 D1("could not allocate memory");
821 odev->rx_parse_state = WAIT_SYNC;
822 return;
824 /* Here's where it came from */
825 odev->skb_rx_buf->dev = odev->net;
827 /* Copy what we got so far. make room for iphdr
828 * after tail. */
829 tmp_rx_buf =
830 skb_put(odev->skb_rx_buf,
831 sizeof(struct iphdr));
832 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
833 sizeof(struct iphdr));
835 /* ETH_HLEN */
836 odev->rx_buf_size = sizeof(struct iphdr);
838 /* Filip actually use .tot_len */
839 odev->rx_buf_missing =
840 frame_len - sizeof(struct iphdr);
841 odev->rx_parse_state = WAIT_DATA;
843 break;
845 case WAIT_DATA:
846 temp_bytes = (count < odev->rx_buf_missing)
847 ? count : odev->rx_buf_missing;
849 /* Copy the rest of the bytes that are left in the
850 * buffer into the waiting sk_buf. */
851 /* Make room for temp_bytes after tail. */
852 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
853 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
855 odev->rx_buf_missing -= temp_bytes;
856 count -= temp_bytes;
857 buffer_offset += temp_bytes;
858 odev->rx_buf_size += temp_bytes;
859 if (!odev->rx_buf_missing) {
860 /* Packet is complete. Inject into stack. */
861 /* We have IP packet here */
862 odev->skb_rx_buf->protocol =
863 __constant_htons(ETH_P_IP);
864 /* don't check it */
865 odev->skb_rx_buf->ip_summed =
866 CHECKSUM_UNNECESSARY;
868 skb_reset_mac_header(odev->skb_rx_buf);
870 /* Ship it off to the kernel */
871 netif_rx(odev->skb_rx_buf);
872 /* No longer our buffer. */
873 odev->skb_rx_buf = NULL;
875 /* update out statistics */
876 odev->net->stats.rx_packets++;
878 odev->net->stats.rx_bytes += odev->rx_buf_size;
880 odev->rx_buf_size = 0;
881 odev->rx_buf_missing = sizeof(struct iphdr);
882 odev->rx_parse_state = WAIT_IP;
884 break;
886 case WAIT_SYNC:
887 D1(" W_S");
888 count = 0;
889 break;
890 default:
891 D1(" ");
892 count--;
893 break;
897 /* Recovery mechanism for WAIT_SYNC state. */
898 if (is_eop) {
899 if (odev->rx_parse_state == WAIT_SYNC) {
900 odev->rx_parse_state = WAIT_IP;
901 odev->rx_buf_size = 0;
902 odev->rx_buf_missing = sizeof(struct iphdr);
907 /* Moving data from usb to kernel (in interrupt state) */
908 static void read_bulk_callback(struct urb *urb)
910 struct hso_net *odev = urb->context;
911 struct net_device *net;
912 int result;
913 int status = urb->status;
915 /* is al ok? (Filip: Who's Al ?) */
916 if (status) {
917 log_usb_status(status, __func__);
918 return;
921 /* Sanity check */
922 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
923 D1("BULK IN callback but driver is not active!");
924 return;
926 usb_mark_last_busy(urb->dev);
928 net = odev->net;
930 if (!netif_device_present(net)) {
931 /* Somebody killed our network interface... */
932 return;
935 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
936 u32 rest;
937 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
938 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
939 if (((rest == 5) || (rest == 6))
940 && !memcmp(((u8 *) urb->transfer_buffer) +
941 urb->actual_length - 4, crc_check, 4)) {
942 urb->actual_length -= 4;
946 /* do we even have a packet? */
947 if (urb->actual_length) {
948 /* Handle the IP stream, add header and push it onto network
949 * stack if the packet is complete. */
950 spin_lock(&odev->net_lock);
951 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
952 (urb->transfer_buffer_length >
953 urb->actual_length) ? 1 : 0);
954 spin_unlock(&odev->net_lock);
957 /* We are done with this URB, resubmit it. Prep the USB to wait for
958 * another frame. Reuse same as received. */
959 usb_fill_bulk_urb(urb,
960 odev->parent->usb,
961 usb_rcvbulkpipe(odev->parent->usb,
962 odev->in_endp->
963 bEndpointAddress & 0x7F),
964 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
965 read_bulk_callback, odev);
967 /* Give this to the USB subsystem so it can tell us when more data
968 * arrives. */
969 result = usb_submit_urb(urb, GFP_ATOMIC);
970 if (result)
971 dev_warn(&odev->parent->interface->dev,
972 "%s failed submit mux_bulk_rx_urb %d", __func__,
973 result);
976 /* Serial driver functions */
978 static void _hso_serial_set_termios(struct tty_struct *tty,
979 struct ktermios *old)
981 struct hso_serial *serial = get_serial_by_tty(tty);
982 struct ktermios *termios;
984 if ((!tty) || (!tty->termios) || (!serial)) {
985 printk(KERN_ERR "%s: no tty structures", __func__);
986 return;
989 D4("port %d", serial->minor);
992 * The default requirements for this device are:
994 termios = tty->termios;
995 termios->c_iflag &=
996 ~(IGNBRK /* disable ignore break */
997 | BRKINT /* disable break causes interrupt */
998 | PARMRK /* disable mark parity errors */
999 | ISTRIP /* disable clear high bit of input characters */
1000 | INLCR /* disable translate NL to CR */
1001 | IGNCR /* disable ignore CR */
1002 | ICRNL /* disable translate CR to NL */
1003 | IXON); /* disable enable XON/XOFF flow control */
1005 /* disable postprocess output characters */
1006 termios->c_oflag &= ~OPOST;
1008 termios->c_lflag &=
1009 ~(ECHO /* disable echo input characters */
1010 | ECHONL /* disable echo new line */
1011 | ICANON /* disable erase, kill, werase, and rprnt
1012 special characters */
1013 | ISIG /* disable interrupt, quit, and suspend special
1014 characters */
1015 | IEXTEN); /* disable non-POSIX special characters */
1017 termios->c_cflag &=
1018 ~(CSIZE /* no size */
1019 | PARENB /* disable parity bit */
1020 | CBAUD /* clear current baud rate */
1021 | CBAUDEX); /* clear current buad rate */
1023 termios->c_cflag |= CS8; /* character size 8 bits */
1025 /* baud rate 115200 */
1026 tty_encode_baud_rate(serial->tty, 115200, 115200);
1029 * Force low_latency on; otherwise the pushes are scheduled;
1030 * this is bad as it opens up the possibility of dropping bytes
1031 * on the floor. We don't want to drop bytes on the floor. :)
1033 serial->tty->low_latency = 1;
1034 return;
1037 /* open the requested serial port */
1038 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1040 struct hso_serial *serial = get_serial_by_index(tty->index);
1041 int result;
1043 /* sanity check */
1044 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1045 tty->driver_data = NULL;
1046 D1("Failed to open port");
1047 return -ENODEV;
1050 mutex_lock(&serial->parent->mutex);
1051 result = usb_autopm_get_interface(serial->parent->interface);
1052 if (result < 0)
1053 goto err_out;
1055 D1("Opening %d", serial->minor);
1056 kref_get(&serial->parent->ref);
1058 /* setup */
1059 tty->driver_data = serial;
1060 serial->tty = tty;
1062 /* check for port allready opened, if not set the termios */
1063 serial->open_count++;
1064 if (serial->open_count == 1) {
1065 tty->low_latency = 1;
1066 serial->flags = 0;
1067 /* Force default termio settings */
1068 _hso_serial_set_termios(tty, NULL);
1069 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1070 if (result) {
1071 hso_stop_serial_device(serial->parent);
1072 serial->open_count--;
1073 kref_put(&serial->parent->ref, hso_serial_ref_free);
1075 } else {
1076 D1("Port was already open");
1079 usb_autopm_put_interface(serial->parent->interface);
1081 /* done */
1082 if (result)
1083 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1084 err_out:
1085 mutex_unlock(&serial->parent->mutex);
1086 return result;
1089 /* close the requested serial port */
1090 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1092 struct hso_serial *serial = tty->driver_data;
1093 u8 usb_gone;
1095 D1("Closing serial port");
1097 mutex_lock(&serial->parent->mutex);
1098 usb_gone = serial->parent->usb_gone;
1100 if (!usb_gone)
1101 usb_autopm_get_interface(serial->parent->interface);
1103 /* reset the rts and dtr */
1104 /* do the actual close */
1105 serial->open_count--;
1106 kref_put(&serial->parent->ref, hso_serial_ref_free);
1107 if (serial->open_count <= 0) {
1108 serial->open_count = 0;
1109 if (serial->tty) {
1110 serial->tty->driver_data = NULL;
1111 serial->tty = NULL;
1113 if (!usb_gone)
1114 hso_stop_serial_device(serial->parent);
1116 if (!usb_gone)
1117 usb_autopm_put_interface(serial->parent->interface);
1118 mutex_unlock(&serial->parent->mutex);
1121 /* close the requested serial port */
1122 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1123 int count)
1125 struct hso_serial *serial = get_serial_by_tty(tty);
1126 int space, tx_bytes;
1127 unsigned long flags;
1129 /* sanity check */
1130 if (serial == NULL) {
1131 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1132 return -ENODEV;
1135 spin_lock_irqsave(&serial->serial_lock, flags);
1137 space = serial->tx_data_length - serial->tx_buffer_count;
1138 tx_bytes = (count < space) ? count : space;
1140 if (!tx_bytes)
1141 goto out;
1143 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1144 serial->tx_buffer_count += tx_bytes;
1146 out:
1147 spin_unlock_irqrestore(&serial->serial_lock, flags);
1149 hso_kick_transmit(serial);
1150 /* done */
1151 return tx_bytes;
1154 /* how much room is there for writing */
1155 static int hso_serial_write_room(struct tty_struct *tty)
1157 struct hso_serial *serial = get_serial_by_tty(tty);
1158 int room;
1159 unsigned long flags;
1161 spin_lock_irqsave(&serial->serial_lock, flags);
1162 room = serial->tx_data_length - serial->tx_buffer_count;
1163 spin_unlock_irqrestore(&serial->serial_lock, flags);
1165 /* return free room */
1166 return room;
1169 /* setup the term */
1170 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1172 struct hso_serial *serial = get_serial_by_tty(tty);
1173 unsigned long flags;
1175 if (old)
1176 D5("Termios called with: cflags new[%d] - old[%d]",
1177 tty->termios->c_cflag, old->c_cflag);
1179 /* the actual setup */
1180 spin_lock_irqsave(&serial->serial_lock, flags);
1181 if (serial->open_count)
1182 _hso_serial_set_termios(tty, old);
1183 else
1184 tty->termios = old;
1185 spin_unlock_irqrestore(&serial->serial_lock, flags);
1187 /* done */
1188 return;
1191 /* how many characters in the buffer */
1192 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1194 struct hso_serial *serial = get_serial_by_tty(tty);
1195 int chars;
1196 unsigned long flags;
1198 /* sanity check */
1199 if (serial == NULL)
1200 return 0;
1202 spin_lock_irqsave(&serial->serial_lock, flags);
1203 chars = serial->tx_buffer_count;
1204 spin_unlock_irqrestore(&serial->serial_lock, flags);
1206 return chars;
1209 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1211 unsigned int value;
1212 struct hso_serial *serial = get_serial_by_tty(tty);
1213 unsigned long flags;
1215 /* sanity check */
1216 if (!serial) {
1217 D1("no tty structures");
1218 return -EINVAL;
1221 spin_lock_irqsave(&serial->serial_lock, flags);
1222 value = ((serial->rts_state) ? TIOCM_RTS : 0) |
1223 ((serial->dtr_state) ? TIOCM_DTR : 0);
1224 spin_unlock_irqrestore(&serial->serial_lock, flags);
1226 return value;
1229 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1230 unsigned int set, unsigned int clear)
1232 int val = 0;
1233 unsigned long flags;
1234 int if_num;
1235 struct hso_serial *serial = get_serial_by_tty(tty);
1237 /* sanity check */
1238 if (!serial) {
1239 D1("no tty structures");
1240 return -EINVAL;
1242 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1244 spin_lock_irqsave(&serial->serial_lock, flags);
1245 if (set & TIOCM_RTS)
1246 serial->rts_state = 1;
1247 if (set & TIOCM_DTR)
1248 serial->dtr_state = 1;
1250 if (clear & TIOCM_RTS)
1251 serial->rts_state = 0;
1252 if (clear & TIOCM_DTR)
1253 serial->dtr_state = 0;
1255 if (serial->dtr_state)
1256 val |= 0x01;
1257 if (serial->rts_state)
1258 val |= 0x02;
1260 spin_unlock_irqrestore(&serial->serial_lock, flags);
1262 return usb_control_msg(serial->parent->usb,
1263 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1264 0x21, val, if_num, NULL, 0,
1265 USB_CTRL_SET_TIMEOUT);
1268 /* starts a transmit */
1269 static void hso_kick_transmit(struct hso_serial *serial)
1271 u8 *temp;
1272 unsigned long flags;
1273 int res;
1275 spin_lock_irqsave(&serial->serial_lock, flags);
1276 if (!serial->tx_buffer_count)
1277 goto out;
1279 if (serial->tx_urb_used)
1280 goto out;
1282 /* Wakeup USB interface if necessary */
1283 if (hso_get_activity(serial->parent) == -EAGAIN)
1284 goto out;
1286 /* Switch pointers around to avoid memcpy */
1287 temp = serial->tx_buffer;
1288 serial->tx_buffer = serial->tx_data;
1289 serial->tx_data = temp;
1290 serial->tx_data_count = serial->tx_buffer_count;
1291 serial->tx_buffer_count = 0;
1293 /* If temp is set, it means we switched buffers */
1294 if (temp && serial->write_data) {
1295 res = serial->write_data(serial);
1296 if (res >= 0)
1297 serial->tx_urb_used = 1;
1299 out:
1300 spin_unlock_irqrestore(&serial->serial_lock, flags);
1303 /* make a request (for reading and writing data to muxed serial port) */
1304 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1305 struct urb *ctrl_urb,
1306 struct usb_ctrlrequest *ctrl_req,
1307 u8 *ctrl_urb_data, u32 size)
1309 int result;
1310 int pipe;
1312 /* Sanity check */
1313 if (!serial || !ctrl_urb || !ctrl_req) {
1314 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1315 return -EINVAL;
1318 /* initialize */
1319 ctrl_req->wValue = 0;
1320 ctrl_req->wIndex = hso_port_to_mux(port);
1321 ctrl_req->wLength = size;
1323 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1324 /* Reading command */
1325 ctrl_req->bRequestType = USB_DIR_IN |
1326 USB_TYPE_OPTION_VENDOR |
1327 USB_RECIP_INTERFACE;
1328 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1329 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1330 } else {
1331 /* Writing command */
1332 ctrl_req->bRequestType = USB_DIR_OUT |
1333 USB_TYPE_OPTION_VENDOR |
1334 USB_RECIP_INTERFACE;
1335 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1336 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1338 /* syslog */
1339 D2("%s command (%02x) len: %d, port: %d",
1340 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1341 ctrl_req->bRequestType, ctrl_req->wLength, port);
1343 /* Load ctrl urb */
1344 ctrl_urb->transfer_flags = 0;
1345 usb_fill_control_urb(ctrl_urb,
1346 serial->parent->usb,
1347 pipe,
1348 (u8 *) ctrl_req,
1349 ctrl_urb_data, size, ctrl_callback, serial);
1350 /* Send it on merry way */
1351 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1352 if (result) {
1353 dev_err(&ctrl_urb->dev->dev,
1354 "%s failed submit ctrl_urb %d type %d", __func__,
1355 result, type);
1356 return result;
1359 /* done */
1360 return size;
1363 /* called by intr_callback when read occurs */
1364 static int hso_mux_serial_read(struct hso_serial *serial)
1366 if (!serial)
1367 return -EINVAL;
1369 /* clean data */
1370 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1371 /* make the request */
1373 if (serial->num_rx_urbs != 1) {
1374 dev_err(&serial->parent->interface->dev,
1375 "ERROR: mux'd reads with multiple buffers "
1376 "not possible\n");
1377 return 0;
1379 return mux_device_request(serial,
1380 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1381 serial->parent->port_spec & HSO_PORT_MASK,
1382 serial->rx_urb[0],
1383 &serial->ctrl_req_rx,
1384 serial->rx_data[0], serial->rx_data_length);
1387 /* used for muxed serial port callback (muxed serial read) */
1388 static void intr_callback(struct urb *urb)
1390 struct hso_shared_int *shared_int = urb->context;
1391 struct hso_serial *serial;
1392 unsigned char *port_req;
1393 int status = urb->status;
1394 int i;
1396 usb_mark_last_busy(urb->dev);
1398 /* sanity check */
1399 if (!shared_int)
1400 return;
1402 /* status check */
1403 if (status) {
1404 log_usb_status(status, __func__);
1405 return;
1407 D4("\n--- Got intr callback 0x%02X ---", status);
1409 /* what request? */
1410 port_req = urb->transfer_buffer;
1411 D4(" port_req = 0x%.2X\n", *port_req);
1412 /* loop over all muxed ports to find the one sending this */
1413 for (i = 0; i < 8; i++) {
1414 /* max 8 channels on MUX */
1415 if (*port_req & (1 << i)) {
1416 serial = get_serial_by_shared_int_and_type(shared_int,
1417 (1 << i));
1418 if (serial != NULL) {
1419 D1("Pending read interrupt on port %d\n", i);
1420 if (!test_and_set_bit(HSO_SERIAL_FLAG_RX_SENT,
1421 &serial->flags)) {
1422 /* Setup and send a ctrl req read on
1423 * port i */
1424 hso_mux_serial_read(serial);
1425 } else {
1426 D1("Already pending a read on "
1427 "port %d\n", i);
1432 /* Resubmit interrupt urb */
1433 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1436 /* called for writing to muxed serial port */
1437 static int hso_mux_serial_write_data(struct hso_serial *serial)
1439 if (NULL == serial)
1440 return -EINVAL;
1442 return mux_device_request(serial,
1443 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1444 serial->parent->port_spec & HSO_PORT_MASK,
1445 serial->tx_urb,
1446 &serial->ctrl_req_tx,
1447 serial->tx_data, serial->tx_data_count);
1450 /* write callback for Diag and CS port */
1451 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1453 struct hso_serial *serial = urb->context;
1454 int status = urb->status;
1456 /* sanity check */
1457 if (!serial) {
1458 D1("serial == NULL");
1459 return;
1462 spin_lock(&serial->serial_lock);
1463 serial->tx_urb_used = 0;
1464 spin_unlock(&serial->serial_lock);
1465 if (status) {
1466 log_usb_status(status, __func__);
1467 return;
1469 hso_put_activity(serial->parent);
1470 if (serial->tty)
1471 tty_wakeup(serial->tty);
1472 hso_kick_transmit(serial);
1474 D1(" ");
1475 return;
1478 /* called for writing diag or CS serial port */
1479 static int hso_std_serial_write_data(struct hso_serial *serial)
1481 int count = serial->tx_data_count;
1482 int result;
1484 usb_fill_bulk_urb(serial->tx_urb,
1485 serial->parent->usb,
1486 usb_sndbulkpipe(serial->parent->usb,
1487 serial->out_endp->
1488 bEndpointAddress & 0x7F),
1489 serial->tx_data, serial->tx_data_count,
1490 hso_std_serial_write_bulk_callback, serial);
1492 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1493 if (result) {
1494 dev_warn(&serial->parent->usb->dev,
1495 "Failed to submit urb - res %d\n", result);
1496 return result;
1499 return count;
1502 /* callback after read or write on muxed serial port */
1503 static void ctrl_callback(struct urb *urb)
1505 struct hso_serial *serial = urb->context;
1506 struct usb_ctrlrequest *req;
1507 int status = urb->status;
1509 /* sanity check */
1510 if (!serial)
1511 return;
1513 spin_lock(&serial->serial_lock);
1514 serial->tx_urb_used = 0;
1515 spin_unlock(&serial->serial_lock);
1516 if (status) {
1517 log_usb_status(status, __func__);
1518 return;
1521 /* what request? */
1522 req = (struct usb_ctrlrequest *)(urb->setup_packet);
1523 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1524 D4("Actual length of urb = %d\n", urb->actual_length);
1525 DUMP1(urb->transfer_buffer, urb->actual_length);
1527 if (req->bRequestType ==
1528 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1529 /* response to a read command */
1530 if (serial->open_count > 0) {
1531 /* handle RX data the normal way */
1532 put_rxbuf_data(urb, serial);
1535 /* Re issue a read as long as we receive data. */
1536 if (urb->actual_length != 0)
1537 hso_mux_serial_read(serial);
1538 else
1539 clear_bit(HSO_SERIAL_FLAG_RX_SENT, &serial->flags);
1540 } else {
1541 hso_put_activity(serial->parent);
1542 if (serial->tty)
1543 tty_wakeup(serial->tty);
1544 /* response to a write command */
1545 hso_kick_transmit(serial);
1549 /* handle RX data for serial port */
1550 static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
1552 struct tty_struct *tty = serial->tty;
1554 /* Sanity check */
1555 if (urb == NULL || serial == NULL) {
1556 D1("serial = NULL");
1557 return;
1560 /* Push data to tty */
1561 if (tty && urb->actual_length) {
1562 D1("data to push to tty");
1563 tty_insert_flip_string(tty, urb->transfer_buffer,
1564 urb->actual_length);
1565 tty_flip_buffer_push(tty);
1569 /* read callback for Diag and CS port */
1570 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1572 struct hso_serial *serial = urb->context;
1573 int result;
1574 int status = urb->status;
1576 /* sanity check */
1577 if (!serial) {
1578 D1("serial == NULL");
1579 return;
1580 } else if (status) {
1581 log_usb_status(status, __func__);
1582 return;
1585 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1586 D1("Actual length = %d\n", urb->actual_length);
1587 DUMP1(urb->transfer_buffer, urb->actual_length);
1589 /* Anyone listening? */
1590 if (serial->open_count == 0)
1591 return;
1593 if (status == 0) {
1594 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1595 u32 rest;
1596 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1597 rest =
1598 urb->actual_length %
1599 serial->in_endp->wMaxPacketSize;
1600 if (((rest == 5) || (rest == 6))
1601 && !memcmp(((u8 *) urb->transfer_buffer) +
1602 urb->actual_length - 4, crc_check, 4)) {
1603 urb->actual_length -= 4;
1606 /* Valid data, handle RX data */
1607 put_rxbuf_data(urb, serial);
1608 } else if (status == -ENOENT || status == -ECONNRESET) {
1609 /* Unlinked - check for throttled port. */
1610 D2("Port %d, successfully unlinked urb", serial->minor);
1611 } else {
1612 D2("Port %d, status = %d for read urb", serial->minor, status);
1613 return;
1616 usb_mark_last_busy(urb->dev);
1618 /* We are done with this URB, resubmit it. Prep the USB to wait for
1619 * another frame */
1620 usb_fill_bulk_urb(urb, serial->parent->usb,
1621 usb_rcvbulkpipe(serial->parent->usb,
1622 serial->in_endp->
1623 bEndpointAddress & 0x7F),
1624 urb->transfer_buffer, serial->rx_data_length,
1625 hso_std_serial_read_bulk_callback, serial);
1626 /* Give this to the USB subsystem so it can tell us when more data
1627 * arrives. */
1628 result = usb_submit_urb(urb, GFP_ATOMIC);
1629 if (result) {
1630 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d",
1631 __func__, result);
1635 /* Base driver functions */
1637 static void hso_log_port(struct hso_device *hso_dev)
1639 char *port_type;
1640 char port_dev[20];
1642 switch (hso_dev->port_spec & HSO_PORT_MASK) {
1643 case HSO_PORT_CONTROL:
1644 port_type = "Control";
1645 break;
1646 case HSO_PORT_APP:
1647 port_type = "Application";
1648 break;
1649 case HSO_PORT_GPS:
1650 port_type = "GPS";
1651 break;
1652 case HSO_PORT_GPS_CONTROL:
1653 port_type = "GPS control";
1654 break;
1655 case HSO_PORT_APP2:
1656 port_type = "Application2";
1657 break;
1658 case HSO_PORT_PCSC:
1659 port_type = "PCSC";
1660 break;
1661 case HSO_PORT_DIAG:
1662 port_type = "Diagnostic";
1663 break;
1664 case HSO_PORT_DIAG2:
1665 port_type = "Diagnostic2";
1666 break;
1667 case HSO_PORT_MODEM:
1668 port_type = "Modem";
1669 break;
1670 case HSO_PORT_NETWORK:
1671 port_type = "Network";
1672 break;
1673 default:
1674 port_type = "Unknown";
1675 break;
1677 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
1678 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
1679 } else
1680 sprintf(port_dev, "/dev/%s%d", tty_filename,
1681 dev2ser(hso_dev)->minor);
1683 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
1684 port_type, port_dev);
1687 static int hso_start_net_device(struct hso_device *hso_dev)
1689 int i, result = 0;
1690 struct hso_net *hso_net = dev2net(hso_dev);
1692 if (!hso_net)
1693 return -ENODEV;
1695 /* send URBs for all read buffers */
1696 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1698 /* Prep a receive URB */
1699 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
1700 hso_dev->usb,
1701 usb_rcvbulkpipe(hso_dev->usb,
1702 hso_net->in_endp->
1703 bEndpointAddress & 0x7F),
1704 hso_net->mux_bulk_rx_buf_pool[i],
1705 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
1706 hso_net);
1708 /* Put it out there so the device can send us stuff */
1709 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
1710 GFP_NOIO);
1711 if (result)
1712 dev_warn(&hso_dev->usb->dev,
1713 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
1714 i, result);
1717 return result;
1720 static int hso_stop_net_device(struct hso_device *hso_dev)
1722 int i;
1723 struct hso_net *hso_net = dev2net(hso_dev);
1725 if (!hso_net)
1726 return -ENODEV;
1728 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1729 if (hso_net->mux_bulk_rx_urb_pool[i])
1730 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1733 if (hso_net->mux_bulk_tx_urb)
1734 usb_kill_urb(hso_net->mux_bulk_tx_urb);
1736 return 0;
1739 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
1741 int i, result = 0;
1742 struct hso_serial *serial = dev2ser(hso_dev);
1744 if (!serial)
1745 return -ENODEV;
1747 /* If it is not the MUX port fill in and submit a bulk urb (already
1748 * allocated in hso_serial_start) */
1749 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
1750 for (i = 0; i < serial->num_rx_urbs; i++) {
1751 usb_fill_bulk_urb(serial->rx_urb[i],
1752 serial->parent->usb,
1753 usb_rcvbulkpipe(serial->parent->usb,
1754 serial->in_endp->
1755 bEndpointAddress &
1756 0x7F),
1757 serial->rx_data[i],
1758 serial->rx_data_length,
1759 hso_std_serial_read_bulk_callback,
1760 serial);
1761 result = usb_submit_urb(serial->rx_urb[i], flags);
1762 if (result) {
1763 dev_warn(&serial->parent->usb->dev,
1764 "Failed to submit urb - res %d\n",
1765 result);
1766 break;
1769 } else {
1770 mutex_lock(&serial->shared_int->shared_int_lock);
1771 if (!serial->shared_int->use_count) {
1772 result =
1773 hso_mux_submit_intr_urb(serial->shared_int,
1774 hso_dev->usb, flags);
1776 serial->shared_int->use_count++;
1777 mutex_unlock(&serial->shared_int->shared_int_lock);
1780 return result;
1783 static int hso_stop_serial_device(struct hso_device *hso_dev)
1785 int i;
1786 struct hso_serial *serial = dev2ser(hso_dev);
1788 if (!serial)
1789 return -ENODEV;
1791 for (i = 0; i < serial->num_rx_urbs; i++) {
1792 if (serial->rx_urb[i])
1793 usb_kill_urb(serial->rx_urb[i]);
1796 if (serial->tx_urb)
1797 usb_kill_urb(serial->tx_urb);
1799 if (serial->shared_int) {
1800 mutex_lock(&serial->shared_int->shared_int_lock);
1801 if (serial->shared_int->use_count &&
1802 (--serial->shared_int->use_count == 0)) {
1803 struct urb *urb;
1805 urb = serial->shared_int->shared_intr_urb;
1806 if (urb)
1807 usb_kill_urb(urb);
1809 mutex_unlock(&serial->shared_int->shared_int_lock);
1812 return 0;
1815 static void hso_serial_common_free(struct hso_serial *serial)
1817 int i;
1819 if (serial->parent->dev)
1820 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
1822 tty_unregister_device(tty_drv, serial->minor);
1824 for (i = 0; i < serial->num_rx_urbs; i++) {
1825 /* unlink and free RX URB */
1826 usb_free_urb(serial->rx_urb[i]);
1827 /* free the RX buffer */
1828 kfree(serial->rx_data[i]);
1831 /* unlink and free TX URB */
1832 usb_free_urb(serial->tx_urb);
1833 kfree(serial->tx_data);
1836 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
1837 int rx_size, int tx_size)
1839 struct device *dev;
1840 int minor;
1841 int i;
1843 minor = get_free_serial_index();
1844 if (minor < 0)
1845 goto exit;
1847 /* register our minor number */
1848 serial->parent->dev = tty_register_device(tty_drv, minor,
1849 &serial->parent->interface->dev);
1850 dev = serial->parent->dev;
1851 dev->driver_data = serial->parent;
1852 i = device_create_file(dev, &dev_attr_hsotype);
1854 /* fill in specific data for later use */
1855 serial->minor = minor;
1856 serial->magic = HSO_SERIAL_MAGIC;
1857 spin_lock_init(&serial->serial_lock);
1858 serial->num_rx_urbs = num_urbs;
1860 /* RX, allocate urb and initialize */
1862 /* prepare our RX buffer */
1863 serial->rx_data_length = rx_size;
1864 for (i = 0; i < serial->num_rx_urbs; i++) {
1865 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1866 if (!serial->rx_urb[i]) {
1867 dev_err(dev, "Could not allocate urb?\n");
1868 goto exit;
1870 serial->rx_urb[i]->transfer_buffer = NULL;
1871 serial->rx_urb[i]->transfer_buffer_length = 0;
1872 serial->rx_data[i] = kzalloc(serial->rx_data_length,
1873 GFP_KERNEL);
1874 if (!serial->rx_data[i]) {
1875 dev_err(dev, "%s - Out of memory\n", __func__);
1876 goto exit;
1880 /* TX, allocate urb and initialize */
1881 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1882 if (!serial->tx_urb) {
1883 dev_err(dev, "Could not allocate urb?\n");
1884 goto exit;
1886 serial->tx_urb->transfer_buffer = NULL;
1887 serial->tx_urb->transfer_buffer_length = 0;
1888 /* prepare our TX buffer */
1889 serial->tx_data_count = 0;
1890 serial->tx_buffer_count = 0;
1891 serial->tx_data_length = tx_size;
1892 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
1893 if (!serial->tx_data) {
1894 dev_err(dev, "%s - Out of memory", __func__);
1895 goto exit;
1897 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
1898 if (!serial->tx_buffer) {
1899 dev_err(dev, "%s - Out of memory", __func__);
1900 goto exit;
1903 return 0;
1904 exit:
1905 hso_serial_common_free(serial);
1906 return -1;
1909 /* Frees a general hso device */
1910 static void hso_free_device(struct hso_device *hso_dev)
1912 kfree(hso_dev);
1915 /* Creates a general hso device */
1916 static struct hso_device *hso_create_device(struct usb_interface *intf,
1917 int port_spec)
1919 struct hso_device *hso_dev;
1921 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
1922 if (!hso_dev)
1923 return NULL;
1925 hso_dev->port_spec = port_spec;
1926 hso_dev->usb = interface_to_usbdev(intf);
1927 hso_dev->interface = intf;
1928 kref_init(&hso_dev->ref);
1929 mutex_init(&hso_dev->mutex);
1931 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
1932 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
1934 return hso_dev;
1937 /* Removes a network device in the network device table */
1938 static int remove_net_device(struct hso_device *hso_dev)
1940 int i;
1942 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
1943 if (network_table[i] == hso_dev) {
1944 network_table[i] = NULL;
1945 break;
1948 if (i == HSO_MAX_NET_DEVICES)
1949 return -1;
1950 return 0;
1953 /* Frees our network device */
1954 static void hso_free_net_device(struct hso_device *hso_dev)
1956 int i;
1957 struct hso_net *hso_net = dev2net(hso_dev);
1959 if (!hso_net)
1960 return;
1962 /* start freeing */
1963 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1964 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1965 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
1967 usb_free_urb(hso_net->mux_bulk_tx_urb);
1968 kfree(hso_net->mux_bulk_tx_buf);
1970 remove_net_device(hso_net->parent);
1972 if (hso_net->net) {
1973 unregister_netdev(hso_net->net);
1974 free_netdev(hso_net->net);
1977 hso_free_device(hso_dev);
1980 /* initialize the network interface */
1981 static void hso_net_init(struct net_device *net)
1983 struct hso_net *hso_net = netdev_priv(net);
1985 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
1987 /* fill in the other fields */
1988 net->open = hso_net_open;
1989 net->stop = hso_net_close;
1990 net->hard_start_xmit = hso_net_start_xmit;
1991 net->tx_timeout = hso_net_tx_timeout;
1992 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
1993 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1994 net->type = ARPHRD_NONE;
1995 net->mtu = DEFAULT_MTU - 14;
1996 net->tx_queue_len = 10;
1997 SET_ETHTOOL_OPS(net, &ops);
1999 /* and initialize the semaphore */
2000 spin_lock_init(&hso_net->net_lock);
2003 /* Adds a network device in the network device table */
2004 static int add_net_device(struct hso_device *hso_dev)
2006 int i;
2008 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2009 if (network_table[i] == NULL) {
2010 network_table[i] = hso_dev;
2011 break;
2014 if (i == HSO_MAX_NET_DEVICES)
2015 return -1;
2016 return 0;
2019 static int hso_radio_toggle(void *data, enum rfkill_state state)
2021 struct hso_device *hso_dev = data;
2022 int enabled = (state == RFKILL_STATE_ON);
2023 int rv;
2025 mutex_lock(&hso_dev->mutex);
2026 if (hso_dev->usb_gone)
2027 rv = 0;
2028 else
2029 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2030 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2031 USB_CTRL_SET_TIMEOUT);
2032 mutex_unlock(&hso_dev->mutex);
2033 return rv;
2036 /* Creates and sets up everything for rfkill */
2037 static void hso_create_rfkill(struct hso_device *hso_dev,
2038 struct usb_interface *interface)
2040 struct hso_net *hso_net = dev2net(hso_dev);
2041 struct device *dev = hso_dev->dev;
2042 char *rfkn;
2044 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2045 RFKILL_TYPE_WLAN);
2046 if (!hso_net->rfkill) {
2047 dev_err(dev, "%s - Out of memory", __func__);
2048 return;
2050 rfkn = kzalloc(20, GFP_KERNEL);
2051 if (!rfkn) {
2052 rfkill_free(hso_net->rfkill);
2053 dev_err(dev, "%s - Out of memory", __func__);
2054 return;
2056 snprintf(rfkn, 20, "hso-%d",
2057 interface->altsetting->desc.bInterfaceNumber);
2058 hso_net->rfkill->name = rfkn;
2059 hso_net->rfkill->state = RFKILL_STATE_ON;
2060 hso_net->rfkill->data = hso_dev;
2061 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2062 if (rfkill_register(hso_net->rfkill) < 0) {
2063 kfree(rfkn);
2064 hso_net->rfkill->name = NULL;
2065 rfkill_free(hso_net->rfkill);
2066 dev_err(dev, "%s - Failed to register rfkill", __func__);
2067 return;
2071 /* Creates our network device */
2072 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2074 int result, i;
2075 struct net_device *net;
2076 struct hso_net *hso_net;
2077 struct hso_device *hso_dev;
2079 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2080 if (!hso_dev)
2081 return NULL;
2083 /* allocate our network device, then we can put in our private data */
2084 /* call hso_net_init to do the basic initialization */
2085 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2086 if (!net) {
2087 dev_err(&interface->dev, "Unable to create ethernet device\n");
2088 goto exit;
2091 hso_net = netdev_priv(net);
2093 hso_dev->port_data.dev_net = hso_net;
2094 hso_net->net = net;
2095 hso_net->parent = hso_dev;
2097 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2098 USB_DIR_IN);
2099 if (!hso_net->in_endp) {
2100 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2101 goto exit;
2103 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2104 USB_DIR_OUT);
2105 if (!hso_net->out_endp) {
2106 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2107 goto exit;
2109 SET_NETDEV_DEV(net, &interface->dev);
2111 /* registering our net device */
2112 result = register_netdev(net);
2113 if (result) {
2114 dev_err(&interface->dev, "Failed to register device\n");
2115 goto exit;
2118 /* start allocating */
2119 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2120 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2121 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2122 dev_err(&interface->dev, "Could not allocate rx urb\n");
2123 goto exit;
2125 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2126 GFP_KERNEL);
2127 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2128 dev_err(&interface->dev, "Could not allocate rx buf\n");
2129 goto exit;
2132 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2133 if (!hso_net->mux_bulk_tx_urb) {
2134 dev_err(&interface->dev, "Could not allocate tx urb\n");
2135 goto exit;
2137 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2138 if (!hso_net->mux_bulk_tx_buf) {
2139 dev_err(&interface->dev, "Could not allocate tx buf\n");
2140 goto exit;
2143 add_net_device(hso_dev);
2145 hso_log_port(hso_dev);
2147 hso_create_rfkill(hso_dev, interface);
2149 return hso_dev;
2150 exit:
2151 hso_free_net_device(hso_dev);
2152 return NULL;
2155 /* Frees an AT channel ( goes for both mux and non-mux ) */
2156 static void hso_free_serial_device(struct hso_device *hso_dev)
2158 struct hso_serial *serial = dev2ser(hso_dev);
2160 if (!serial)
2161 return;
2162 set_serial_by_index(serial->minor, NULL);
2164 hso_serial_common_free(serial);
2166 if (serial->shared_int) {
2167 mutex_lock(&serial->shared_int->shared_int_lock);
2168 if (--serial->shared_int->ref_count == 0)
2169 hso_free_shared_int(serial->shared_int);
2170 else
2171 mutex_unlock(&serial->shared_int->shared_int_lock);
2173 kfree(serial);
2174 hso_free_device(hso_dev);
2177 /* Creates a bulk AT channel */
2178 static struct hso_device *hso_create_bulk_serial_device(
2179 struct usb_interface *interface, int port)
2181 struct hso_device *hso_dev;
2182 struct hso_serial *serial;
2183 int num_urbs;
2185 hso_dev = hso_create_device(interface, port);
2186 if (!hso_dev)
2187 return NULL;
2189 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2190 if (!serial)
2191 goto exit;
2193 serial->parent = hso_dev;
2194 hso_dev->port_data.dev_serial = serial;
2196 if (port & HSO_PORT_MODEM)
2197 num_urbs = 2;
2198 else
2199 num_urbs = 1;
2201 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2202 BULK_URB_TX_SIZE))
2203 goto exit;
2205 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2206 USB_DIR_IN);
2207 if (!serial->in_endp) {
2208 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2209 goto exit;
2212 if (!
2213 (serial->out_endp =
2214 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2215 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2216 goto exit;
2219 serial->write_data = hso_std_serial_write_data;
2221 /* and record this serial */
2222 set_serial_by_index(serial->minor, serial);
2224 /* setup the proc dirs and files if needed */
2225 hso_log_port(hso_dev);
2227 /* done, return it */
2228 return hso_dev;
2229 exit:
2230 if (hso_dev && serial)
2231 hso_serial_common_free(serial);
2232 kfree(serial);
2233 hso_free_device(hso_dev);
2234 return NULL;
2237 /* Creates a multiplexed AT channel */
2238 static
2239 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2240 int port,
2241 struct hso_shared_int *mux)
2243 struct hso_device *hso_dev;
2244 struct hso_serial *serial;
2245 int port_spec;
2247 port_spec = HSO_INTF_MUX;
2248 port_spec &= ~HSO_PORT_MASK;
2250 port_spec |= hso_mux_to_port(port);
2251 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2252 return NULL;
2254 hso_dev = hso_create_device(interface, port_spec);
2255 if (!hso_dev)
2256 return NULL;
2258 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2259 if (!serial)
2260 goto exit;
2262 hso_dev->port_data.dev_serial = serial;
2263 serial->parent = hso_dev;
2265 if (hso_serial_common_create
2266 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2267 goto exit;
2269 serial->tx_data_length--;
2270 serial->write_data = hso_mux_serial_write_data;
2272 serial->shared_int = mux;
2273 mutex_lock(&serial->shared_int->shared_int_lock);
2274 serial->shared_int->ref_count++;
2275 mutex_unlock(&serial->shared_int->shared_int_lock);
2277 /* and record this serial */
2278 set_serial_by_index(serial->minor, serial);
2280 /* setup the proc dirs and files if needed */
2281 hso_log_port(hso_dev);
2283 /* done, return it */
2284 return hso_dev;
2286 exit:
2287 if (serial) {
2288 tty_unregister_device(tty_drv, serial->minor);
2289 kfree(serial);
2291 if (hso_dev)
2292 hso_free_device(hso_dev);
2293 return NULL;
2297 static void hso_free_shared_int(struct hso_shared_int *mux)
2299 usb_free_urb(mux->shared_intr_urb);
2300 kfree(mux->shared_intr_buf);
2301 mutex_unlock(&mux->shared_int_lock);
2302 kfree(mux);
2305 static
2306 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2308 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2310 if (!mux)
2311 return NULL;
2313 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2314 USB_DIR_IN);
2315 if (!mux->intr_endp) {
2316 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2317 goto exit;
2320 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2321 if (!mux->shared_intr_urb) {
2322 dev_err(&interface->dev, "Could not allocate intr urb?");
2323 goto exit;
2325 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2326 GFP_KERNEL);
2327 if (!mux->shared_intr_buf) {
2328 dev_err(&interface->dev, "Could not allocate intr buf?");
2329 goto exit;
2332 mutex_init(&mux->shared_int_lock);
2334 return mux;
2336 exit:
2337 kfree(mux->shared_intr_buf);
2338 usb_free_urb(mux->shared_intr_urb);
2339 kfree(mux);
2340 return NULL;
2343 /* Gets the port spec for a certain interface */
2344 static int hso_get_config_data(struct usb_interface *interface)
2346 struct usb_device *usbdev = interface_to_usbdev(interface);
2347 u8 config_data[17];
2348 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2349 s32 result;
2351 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2352 0x86, 0xC0, 0, 0, config_data, 17,
2353 USB_CTRL_SET_TIMEOUT) != 0x11) {
2354 return -EIO;
2357 switch (config_data[if_num]) {
2358 case 0x0:
2359 result = 0;
2360 break;
2361 case 0x1:
2362 result = HSO_PORT_DIAG;
2363 break;
2364 case 0x2:
2365 result = HSO_PORT_GPS;
2366 break;
2367 case 0x3:
2368 result = HSO_PORT_GPS_CONTROL;
2369 break;
2370 case 0x4:
2371 result = HSO_PORT_APP;
2372 break;
2373 case 0x5:
2374 result = HSO_PORT_APP2;
2375 break;
2376 case 0x6:
2377 result = HSO_PORT_CONTROL;
2378 break;
2379 case 0x7:
2380 result = HSO_PORT_NETWORK;
2381 break;
2382 case 0x8:
2383 result = HSO_PORT_MODEM;
2384 break;
2385 case 0x9:
2386 result = HSO_PORT_MSD;
2387 break;
2388 case 0xa:
2389 result = HSO_PORT_PCSC;
2390 break;
2391 case 0xb:
2392 result = HSO_PORT_VOICE;
2393 break;
2394 default:
2395 result = 0;
2398 if (result)
2399 result |= HSO_INTF_BULK;
2401 if (config_data[16] & 0x1)
2402 result |= HSO_INFO_CRC_BUG;
2404 return result;
2407 /* called once for each interface upon device insertion */
2408 static int hso_probe(struct usb_interface *interface,
2409 const struct usb_device_id *id)
2411 int mux, i, if_num, port_spec;
2412 unsigned char port_mask;
2413 struct hso_device *hso_dev = NULL;
2414 struct hso_shared_int *shared_int;
2415 struct hso_device *tmp_dev = NULL;
2417 if_num = interface->altsetting->desc.bInterfaceNumber;
2419 /* Get the interface/port specification from either driver_info or from
2420 * the device itself */
2421 if (id->driver_info)
2422 port_spec = ((u32 *)(id->driver_info))[if_num];
2423 else
2424 port_spec = hso_get_config_data(interface);
2426 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2427 dev_err(&interface->dev, "Not our interface\n");
2428 return -ENODEV;
2430 /* Check if we need to switch to alt interfaces prior to port
2431 * configuration */
2432 if (interface->num_altsetting > 1)
2433 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2434 interface->needs_remote_wakeup = 1;
2436 /* Allocate new hso device(s) */
2437 switch (port_spec & HSO_INTF_MASK) {
2438 case HSO_INTF_MUX:
2439 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2440 /* Create the network device */
2441 if (!disable_net) {
2442 hso_dev = hso_create_net_device(interface);
2443 if (!hso_dev)
2444 goto exit;
2445 tmp_dev = hso_dev;
2449 if (hso_get_mux_ports(interface, &port_mask))
2450 /* TODO: de-allocate everything */
2451 goto exit;
2453 shared_int = hso_create_shared_int(interface);
2454 if (!shared_int)
2455 goto exit;
2457 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2458 if (port_mask & i) {
2459 hso_dev = hso_create_mux_serial_device(
2460 interface, i, shared_int);
2461 if (!hso_dev)
2462 goto exit;
2466 if (tmp_dev)
2467 hso_dev = tmp_dev;
2468 break;
2470 case HSO_INTF_BULK:
2471 /* It's a regular bulk interface */
2472 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2473 && !disable_net)
2474 hso_dev = hso_create_net_device(interface);
2475 else
2476 hso_dev =
2477 hso_create_bulk_serial_device(interface, port_spec);
2478 if (!hso_dev)
2479 goto exit;
2480 break;
2481 default:
2482 goto exit;
2485 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2487 /* save our data pointer in this device */
2488 usb_set_intfdata(interface, hso_dev);
2490 /* done */
2491 return 0;
2492 exit:
2493 hso_free_interface(interface);
2494 return -ENODEV;
2497 /* device removed, cleaning up */
2498 static void hso_disconnect(struct usb_interface *interface)
2500 hso_free_interface(interface);
2502 /* remove reference of our private data */
2503 usb_set_intfdata(interface, NULL);
2505 usb_driver_release_interface(&hso_driver, interface);
2508 static void async_get_intf(struct work_struct *data)
2510 struct hso_device *hso_dev =
2511 container_of(data, struct hso_device, async_get_intf);
2512 usb_autopm_get_interface(hso_dev->interface);
2515 static void async_put_intf(struct work_struct *data)
2517 struct hso_device *hso_dev =
2518 container_of(data, struct hso_device, async_put_intf);
2519 usb_autopm_put_interface(hso_dev->interface);
2522 static int hso_get_activity(struct hso_device *hso_dev)
2524 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2525 if (!hso_dev->is_active) {
2526 hso_dev->is_active = 1;
2527 schedule_work(&hso_dev->async_get_intf);
2531 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2532 return -EAGAIN;
2534 usb_mark_last_busy(hso_dev->usb);
2536 return 0;
2539 static int hso_put_activity(struct hso_device *hso_dev)
2541 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2542 if (hso_dev->is_active) {
2543 hso_dev->is_active = 0;
2544 schedule_work(&hso_dev->async_put_intf);
2545 return -EAGAIN;
2548 hso_dev->is_active = 0;
2549 return 0;
2552 /* called by kernel when we need to suspend device */
2553 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2555 int i, result;
2557 /* Stop all serial ports */
2558 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2559 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2560 result = hso_stop_serial_device(serial_table[i]);
2561 if (result)
2562 goto out;
2566 /* Stop all network ports */
2567 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2568 if (network_table[i] &&
2569 (network_table[i]->interface == iface)) {
2570 result = hso_stop_net_device(network_table[i]);
2571 if (result)
2572 goto out;
2576 out:
2577 return 0;
2580 /* called by kernel when we need to resume device */
2581 static int hso_resume(struct usb_interface *iface)
2583 int i, result = 0;
2584 struct hso_net *hso_net;
2586 /* Start all serial ports */
2587 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2588 if (serial_table[i] && (serial_table[i]->interface == iface)) {
2589 if (dev2ser(serial_table[i])->open_count) {
2590 result =
2591 hso_start_serial_device(serial_table[i], GFP_NOIO);
2592 hso_kick_transmit(dev2ser(serial_table[i]));
2593 if (result)
2594 goto out;
2599 /* Start all network ports */
2600 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2601 if (network_table[i] &&
2602 (network_table[i]->interface == iface)) {
2603 hso_net = dev2net(network_table[i]);
2604 /* First transmit any lingering data, then restart the
2605 * device. */
2606 if (hso_net->skb_tx_buf) {
2607 dev_dbg(&iface->dev,
2608 "Transmitting lingering data\n");
2609 hso_net_start_xmit(hso_net->skb_tx_buf,
2610 hso_net->net);
2612 result = hso_start_net_device(network_table[i]);
2613 if (result)
2614 goto out;
2618 out:
2619 return result;
2622 static void hso_serial_ref_free(struct kref *ref)
2624 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2626 hso_free_serial_device(hso_dev);
2629 static void hso_free_interface(struct usb_interface *interface)
2631 struct hso_serial *hso_dev;
2632 int i;
2634 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2635 if (serial_table[i]
2636 && (serial_table[i]->interface == interface)) {
2637 hso_dev = dev2ser(serial_table[i]);
2638 if (hso_dev->tty)
2639 tty_hangup(hso_dev->tty);
2640 mutex_lock(&hso_dev->parent->mutex);
2641 hso_dev->parent->usb_gone = 1;
2642 mutex_unlock(&hso_dev->parent->mutex);
2643 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2647 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2648 if (network_table[i]
2649 && (network_table[i]->interface == interface)) {
2650 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2651 /* hso_stop_net_device doesn't stop the net queue since
2652 * traffic needs to start it again when suspended */
2653 netif_stop_queue(dev2net(network_table[i])->net);
2654 hso_stop_net_device(network_table[i]);
2655 cancel_work_sync(&network_table[i]->async_put_intf);
2656 cancel_work_sync(&network_table[i]->async_get_intf);
2657 if(rfk)
2658 rfkill_unregister(rfk);
2659 hso_free_net_device(network_table[i]);
2664 /* Helper functions */
2666 /* Get the endpoint ! */
2667 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2668 int type, int dir)
2670 int i;
2671 struct usb_host_interface *iface = intf->cur_altsetting;
2672 struct usb_endpoint_descriptor *endp;
2674 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2675 endp = &iface->endpoint[i].desc;
2676 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2677 ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
2678 return endp;
2681 return NULL;
2684 /* Get the byte that describes which ports are enabled */
2685 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2687 int i;
2688 struct usb_host_interface *iface = intf->cur_altsetting;
2690 if (iface->extralen == 3) {
2691 *ports = iface->extra[2];
2692 return 0;
2695 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2696 if (iface->endpoint[i].extralen == 3) {
2697 *ports = iface->endpoint[i].extra[2];
2698 return 0;
2702 return -1;
2705 /* interrupt urb needs to be submitted, used for serial read of muxed port */
2706 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2707 struct usb_device *usb, gfp_t gfp)
2709 int result;
2711 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2712 usb_rcvintpipe(usb,
2713 shared_int->intr_endp->bEndpointAddress & 0x7F),
2714 shared_int->shared_intr_buf,
2715 shared_int->intr_endp->wMaxPacketSize,
2716 intr_callback, shared_int,
2717 shared_int->intr_endp->bInterval);
2719 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2720 if (result)
2721 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2722 result);
2724 return result;
2727 /* operations setup of the serial interface */
2728 static const struct tty_operations hso_serial_ops = {
2729 .open = hso_serial_open,
2730 .close = hso_serial_close,
2731 .write = hso_serial_write,
2732 .write_room = hso_serial_write_room,
2733 .set_termios = hso_serial_set_termios,
2734 .chars_in_buffer = hso_serial_chars_in_buffer,
2735 .tiocmget = hso_serial_tiocmget,
2736 .tiocmset = hso_serial_tiocmset,
2739 static struct usb_driver hso_driver = {
2740 .name = driver_name,
2741 .probe = hso_probe,
2742 .disconnect = hso_disconnect,
2743 .id_table = hso_ids,
2744 .suspend = hso_suspend,
2745 .resume = hso_resume,
2746 .supports_autosuspend = 1,
2749 static int __init hso_init(void)
2751 int i;
2752 int result;
2754 /* put it in the log */
2755 printk(KERN_INFO "hso: %s\n", version);
2757 /* Initialise the serial table semaphore and table */
2758 spin_lock_init(&serial_table_lock);
2759 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2760 serial_table[i] = NULL;
2762 /* allocate our driver using the proper amount of supported minors */
2763 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2764 if (!tty_drv)
2765 return -ENOMEM;
2767 /* fill in all needed values */
2768 tty_drv->magic = TTY_DRIVER_MAGIC;
2769 tty_drv->owner = THIS_MODULE;
2770 tty_drv->driver_name = driver_name;
2771 tty_drv->name = tty_filename;
2773 /* if major number is provided as parameter, use that one */
2774 if (tty_major)
2775 tty_drv->major = tty_major;
2777 tty_drv->minor_start = 0;
2778 tty_drv->num = HSO_SERIAL_TTY_MINORS;
2779 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2780 tty_drv->subtype = SERIAL_TYPE_NORMAL;
2781 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2782 tty_drv->init_termios = tty_std_termios;
2783 tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2784 tty_drv->termios = hso_serial_termios;
2785 tty_drv->termios_locked = hso_serial_termios_locked;
2786 tty_set_operations(tty_drv, &hso_serial_ops);
2788 /* register the tty driver */
2789 result = tty_register_driver(tty_drv);
2790 if (result) {
2791 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2792 __func__, result);
2793 return result;
2796 /* register this module as an usb driver */
2797 result = usb_register(&hso_driver);
2798 if (result) {
2799 printk(KERN_ERR "Could not register hso driver? error: %d\n",
2800 result);
2801 /* cleanup serial interface */
2802 tty_unregister_driver(tty_drv);
2803 return result;
2806 /* done */
2807 return 0;
2810 static void __exit hso_exit(void)
2812 printk(KERN_INFO "hso: unloaded\n");
2814 tty_unregister_driver(tty_drv);
2815 /* deregister the usb driver */
2816 usb_deregister(&hso_driver);
2819 /* Module definitions */
2820 module_init(hso_init);
2821 module_exit(hso_exit);
2823 MODULE_AUTHOR(MOD_AUTHOR);
2824 MODULE_DESCRIPTION(MOD_DESCRIPTION);
2825 MODULE_LICENSE(MOD_LICENSE);
2826 MODULE_INFO(Version, DRIVER_VERSION);
2828 /* change the debug level (eg: insmod hso.ko debug=0x04) */
2829 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2830 module_param(debug, int, S_IRUGO | S_IWUSR);
2832 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
2833 MODULE_PARM_DESC(tty_major, "Set the major tty number");
2834 module_param(tty_major, int, S_IRUGO | S_IWUSR);
2836 /* disable network interface (eg: insmod hso.ko disable_net=1) */
2837 MODULE_PARM_DESC(disable_net, "Disable the network interface");
2838 module_param(disable_net, int, S_IRUGO | S_IWUSR);