hso serial throttled tty kref fix.
[linux-2.6/mini2440.git] / drivers / net / usb / hso.c
blob6478bf63f2897d9444e4138991bd8f811f1ed801
1 /******************************************************************************
3 * Driver for Option High Speed Mobile Devices.
5 * Copyright (C) 2008 Option International
6 * Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com>
8 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
9 * <ajb@spheresystems.co.uk>
10 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
11 * Copyright (C) 2008 Novell, Inc.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 * USA
28 *****************************************************************************/
30 /******************************************************************************
32 * Description of the device:
34 * Interface 0: Contains the IP network interface on the bulk end points.
35 * The multiplexed serial ports are using the interrupt and
36 * control endpoints.
37 * Interrupt contains a bitmap telling which multiplexed
38 * serialport needs servicing.
40 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
41 * port is opened, as this have a huge impact on the network port
42 * throughput.
44 * Interface 2: Standard modem interface - circuit switched interface, this
45 * can be used to make a standard ppp connection however it
46 * should not be used in conjunction with the IP network interface
47 * enabled for USB performance reasons i.e. if using this set
48 * ideally disable_net=1.
50 *****************************************************************************/
52 #include <linux/sched.h>
53 #include <linux/slab.h>
54 #include <linux/init.h>
55 #include <linux/delay.h>
56 #include <linux/netdevice.h>
57 #include <linux/module.h>
58 #include <linux/ethtool.h>
59 #include <linux/usb.h>
60 #include <linux/timer.h>
61 #include <linux/tty.h>
62 #include <linux/tty_driver.h>
63 #include <linux/tty_flip.h>
64 #include <linux/kmod.h>
65 #include <linux/rfkill.h>
66 #include <linux/ip.h>
67 #include <linux/uaccess.h>
68 #include <linux/usb/cdc.h>
69 #include <net/arp.h>
70 #include <asm/byteorder.h>
71 #include <linux/serial_core.h>
72 #include <linux/serial.h>
75 #define DRIVER_VERSION "1.2"
76 #define MOD_AUTHOR "Option Wireless"
77 #define MOD_DESCRIPTION "USB High Speed Option driver"
78 #define MOD_LICENSE "GPL"
80 #define HSO_MAX_NET_DEVICES 10
81 #define HSO__MAX_MTU 2048
82 #define DEFAULT_MTU 1500
83 #define DEFAULT_MRU 1500
85 #define CTRL_URB_RX_SIZE 1024
86 #define CTRL_URB_TX_SIZE 64
88 #define BULK_URB_RX_SIZE 4096
89 #define BULK_URB_TX_SIZE 8192
91 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
92 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
93 #define MUX_BULK_RX_BUF_COUNT 4
94 #define USB_TYPE_OPTION_VENDOR 0x20
96 /* These definitions are used with the struct hso_net flags element */
97 /* - use *_bit operations on it. (bit indices not values.) */
98 #define HSO_NET_RUNNING 0
100 #define HSO_NET_TX_TIMEOUT (HZ*10)
102 #define HSO_SERIAL_MAGIC 0x48534f31
104 /* Number of ttys to handle */
105 #define HSO_SERIAL_TTY_MINORS 256
107 #define MAX_RX_URBS 2
109 static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
111 if (tty)
112 return tty->driver_data;
113 return NULL;
116 /*****************************************************************************/
117 /* Debugging functions */
118 /*****************************************************************************/
119 #define D__(lvl_, fmt, arg...) \
120 do { \
121 printk(lvl_ "[%d:%s]: " fmt "\n", \
122 __LINE__, __func__, ## arg); \
123 } while (0)
125 #define D_(lvl, args...) \
126 do { \
127 if (lvl & debug) \
128 D__(KERN_INFO, args); \
129 } while (0)
131 #define D1(args...) D_(0x01, ##args)
132 #define D2(args...) D_(0x02, ##args)
133 #define D3(args...) D_(0x04, ##args)
134 #define D4(args...) D_(0x08, ##args)
135 #define D5(args...) D_(0x10, ##args)
137 /*****************************************************************************/
138 /* Enumerators */
139 /*****************************************************************************/
140 enum pkt_parse_state {
141 WAIT_IP,
142 WAIT_DATA,
143 WAIT_SYNC
146 /*****************************************************************************/
147 /* Structs */
148 /*****************************************************************************/
150 struct hso_shared_int {
151 struct usb_endpoint_descriptor *intr_endp;
152 void *shared_intr_buf;
153 struct urb *shared_intr_urb;
154 struct usb_device *usb;
155 int use_count;
156 int ref_count;
157 struct mutex shared_int_lock;
160 struct hso_net {
161 struct hso_device *parent;
162 struct net_device *net;
163 struct rfkill *rfkill;
165 struct usb_endpoint_descriptor *in_endp;
166 struct usb_endpoint_descriptor *out_endp;
168 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
169 struct urb *mux_bulk_tx_urb;
170 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
171 void *mux_bulk_tx_buf;
173 struct sk_buff *skb_rx_buf;
174 struct sk_buff *skb_tx_buf;
176 enum pkt_parse_state rx_parse_state;
177 spinlock_t net_lock;
179 unsigned short rx_buf_size;
180 unsigned short rx_buf_missing;
181 struct iphdr rx_ip_hdr;
183 unsigned long flags;
186 enum rx_ctrl_state{
187 RX_IDLE,
188 RX_SENT,
189 RX_PENDING
192 #define BM_REQUEST_TYPE (0xa1)
193 #define B_NOTIFICATION (0x20)
194 #define W_VALUE (0x0)
195 #define W_INDEX (0x2)
196 #define W_LENGTH (0x2)
198 #define B_OVERRUN (0x1<<6)
199 #define B_PARITY (0x1<<5)
200 #define B_FRAMING (0x1<<4)
201 #define B_RING_SIGNAL (0x1<<3)
202 #define B_BREAK (0x1<<2)
203 #define B_TX_CARRIER (0x1<<1)
204 #define B_RX_CARRIER (0x1<<0)
206 struct hso_serial_state_notification {
207 u8 bmRequestType;
208 u8 bNotification;
209 u16 wValue;
210 u16 wIndex;
211 u16 wLength;
212 u16 UART_state_bitmap;
213 } __attribute__((packed));
215 struct hso_tiocmget {
216 struct mutex mutex;
217 wait_queue_head_t waitq;
218 int intr_completed;
219 struct usb_endpoint_descriptor *endp;
220 struct urb *urb;
221 struct hso_serial_state_notification serial_state_notification;
222 u16 prev_UART_state_bitmap;
223 struct uart_icount icount;
227 struct hso_serial {
228 struct hso_device *parent;
229 int magic;
230 u8 minor;
232 struct hso_shared_int *shared_int;
234 /* rx/tx urb could be either a bulk urb or a control urb depending
235 on which serial port it is used on. */
236 struct urb *rx_urb[MAX_RX_URBS];
237 u8 num_rx_urbs;
238 u8 *rx_data[MAX_RX_URBS];
239 u16 rx_data_length; /* should contain allocated length */
241 struct urb *tx_urb;
242 u8 *tx_data;
243 u8 *tx_buffer;
244 u16 tx_data_length; /* should contain allocated length */
245 u16 tx_data_count;
246 u16 tx_buffer_count;
247 struct usb_ctrlrequest ctrl_req_tx;
248 struct usb_ctrlrequest ctrl_req_rx;
250 struct usb_endpoint_descriptor *in_endp;
251 struct usb_endpoint_descriptor *out_endp;
253 enum rx_ctrl_state rx_state;
254 u8 rts_state;
255 u8 dtr_state;
256 unsigned tx_urb_used:1;
258 /* from usb_serial_port */
259 struct tty_struct *tty;
260 int open_count;
261 spinlock_t serial_lock;
263 int (*write_data) (struct hso_serial *serial);
264 struct hso_tiocmget *tiocmget;
265 /* Hacks required to get flow control
266 * working on the serial receive buffers
267 * so as not to drop characters on the floor.
269 int curr_rx_urb_idx;
270 u16 curr_rx_urb_offset;
271 u8 rx_urb_filled[MAX_RX_URBS];
272 struct tasklet_struct unthrottle_tasklet;
273 struct work_struct retry_unthrottle_workqueue;
276 struct hso_device {
277 union {
278 struct hso_serial *dev_serial;
279 struct hso_net *dev_net;
280 } port_data;
282 u32 port_spec;
284 u8 is_active;
285 u8 usb_gone;
286 struct work_struct async_get_intf;
287 struct work_struct async_put_intf;
289 struct usb_device *usb;
290 struct usb_interface *interface;
292 struct device *dev;
293 struct kref ref;
294 struct mutex mutex;
297 /* Type of interface */
298 #define HSO_INTF_MASK 0xFF00
299 #define HSO_INTF_MUX 0x0100
300 #define HSO_INTF_BULK 0x0200
302 /* Type of port */
303 #define HSO_PORT_MASK 0xFF
304 #define HSO_PORT_NO_PORT 0x0
305 #define HSO_PORT_CONTROL 0x1
306 #define HSO_PORT_APP 0x2
307 #define HSO_PORT_GPS 0x3
308 #define HSO_PORT_PCSC 0x4
309 #define HSO_PORT_APP2 0x5
310 #define HSO_PORT_GPS_CONTROL 0x6
311 #define HSO_PORT_MSD 0x7
312 #define HSO_PORT_VOICE 0x8
313 #define HSO_PORT_DIAG2 0x9
314 #define HSO_PORT_DIAG 0x10
315 #define HSO_PORT_MODEM 0x11
316 #define HSO_PORT_NETWORK 0x12
318 /* Additional device info */
319 #define HSO_INFO_MASK 0xFF000000
320 #define HSO_INFO_CRC_BUG 0x01000000
322 /*****************************************************************************/
323 /* Prototypes */
324 /*****************************************************************************/
325 /* Serial driver functions */
326 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
327 unsigned int set, unsigned int clear);
328 static void ctrl_callback(struct urb *urb);
329 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
330 static void hso_kick_transmit(struct hso_serial *serial);
331 /* Helper functions */
332 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
333 struct usb_device *usb, gfp_t gfp);
334 static void log_usb_status(int status, const char *function);
335 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
336 int type, int dir);
337 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
338 static void hso_free_interface(struct usb_interface *intf);
339 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
340 static int hso_stop_serial_device(struct hso_device *hso_dev);
341 static int hso_start_net_device(struct hso_device *hso_dev);
342 static void hso_free_shared_int(struct hso_shared_int *shared_int);
343 static int hso_stop_net_device(struct hso_device *hso_dev);
344 static void hso_serial_ref_free(struct kref *ref);
345 static void hso_std_serial_read_bulk_callback(struct urb *urb);
346 static int hso_mux_serial_read(struct hso_serial *serial);
347 static void async_get_intf(struct work_struct *data);
348 static void async_put_intf(struct work_struct *data);
349 static int hso_put_activity(struct hso_device *hso_dev);
350 static int hso_get_activity(struct hso_device *hso_dev);
351 static void tiocmget_intr_callback(struct urb *urb);
352 /*****************************************************************************/
353 /* Helping functions */
354 /*****************************************************************************/
356 /* #define DEBUG */
358 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
360 return hso_dev->port_data.dev_net;
363 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
365 return hso_dev->port_data.dev_serial;
368 /* Debugging functions */
369 #ifdef DEBUG
370 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
371 unsigned int len)
373 static char name[255];
375 sprintf(name, "hso[%d:%s]", line_count, func_name);
376 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
379 #define DUMP(buf_, len_) \
380 dbg_dump(__LINE__, __func__, buf_, len_)
382 #define DUMP1(buf_, len_) \
383 do { \
384 if (0x01 & debug) \
385 DUMP(buf_, len_); \
386 } while (0)
387 #else
388 #define DUMP(buf_, len_)
389 #define DUMP1(buf_, len_)
390 #endif
392 /* module parameters */
393 static int debug;
394 static int tty_major;
395 static int disable_net;
397 /* driver info */
398 static const char driver_name[] = "hso";
399 static const char tty_filename[] = "ttyHS";
400 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
401 /* the usb driver itself (registered in hso_init) */
402 static struct usb_driver hso_driver;
403 /* serial structures */
404 static struct tty_driver *tty_drv;
405 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
406 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
407 static spinlock_t serial_table_lock;
409 static const s32 default_port_spec[] = {
410 HSO_INTF_MUX | HSO_PORT_NETWORK,
411 HSO_INTF_BULK | HSO_PORT_DIAG,
412 HSO_INTF_BULK | HSO_PORT_MODEM,
416 static const s32 icon321_port_spec[] = {
417 HSO_INTF_MUX | HSO_PORT_NETWORK,
418 HSO_INTF_BULK | HSO_PORT_DIAG2,
419 HSO_INTF_BULK | HSO_PORT_MODEM,
420 HSO_INTF_BULK | HSO_PORT_DIAG,
424 #define default_port_device(vendor, product) \
425 USB_DEVICE(vendor, product), \
426 .driver_info = (kernel_ulong_t)default_port_spec
428 #define icon321_port_device(vendor, product) \
429 USB_DEVICE(vendor, product), \
430 .driver_info = (kernel_ulong_t)icon321_port_spec
432 /* list of devices we support */
433 static const struct usb_device_id hso_ids[] = {
434 {default_port_device(0x0af0, 0x6711)},
435 {default_port_device(0x0af0, 0x6731)},
436 {default_port_device(0x0af0, 0x6751)},
437 {default_port_device(0x0af0, 0x6771)},
438 {default_port_device(0x0af0, 0x6791)},
439 {default_port_device(0x0af0, 0x6811)},
440 {default_port_device(0x0af0, 0x6911)},
441 {default_port_device(0x0af0, 0x6951)},
442 {default_port_device(0x0af0, 0x6971)},
443 {default_port_device(0x0af0, 0x7011)},
444 {default_port_device(0x0af0, 0x7031)},
445 {default_port_device(0x0af0, 0x7051)},
446 {default_port_device(0x0af0, 0x7071)},
447 {default_port_device(0x0af0, 0x7111)},
448 {default_port_device(0x0af0, 0x7211)},
449 {default_port_device(0x0af0, 0x7251)},
450 {default_port_device(0x0af0, 0x7271)},
451 {default_port_device(0x0af0, 0x7311)},
452 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
453 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
454 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
455 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
456 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
457 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
458 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
459 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
460 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
461 {USB_DEVICE(0x0af0, 0x7701)},
462 {USB_DEVICE(0x0af0, 0x7801)},
463 {USB_DEVICE(0x0af0, 0x7901)},
464 {USB_DEVICE(0x0af0, 0x7361)},
465 {icon321_port_device(0x0af0, 0xd051)},
468 MODULE_DEVICE_TABLE(usb, hso_ids);
470 /* Sysfs attribute */
471 static ssize_t hso_sysfs_show_porttype(struct device *dev,
472 struct device_attribute *attr,
473 char *buf)
475 struct hso_device *hso_dev = dev->driver_data;
476 char *port_name;
478 if (!hso_dev)
479 return 0;
481 switch (hso_dev->port_spec & HSO_PORT_MASK) {
482 case HSO_PORT_CONTROL:
483 port_name = "Control";
484 break;
485 case HSO_PORT_APP:
486 port_name = "Application";
487 break;
488 case HSO_PORT_APP2:
489 port_name = "Application2";
490 break;
491 case HSO_PORT_GPS:
492 port_name = "GPS";
493 break;
494 case HSO_PORT_GPS_CONTROL:
495 port_name = "GPS Control";
496 break;
497 case HSO_PORT_PCSC:
498 port_name = "PCSC";
499 break;
500 case HSO_PORT_DIAG:
501 port_name = "Diagnostic";
502 break;
503 case HSO_PORT_DIAG2:
504 port_name = "Diagnostic2";
505 break;
506 case HSO_PORT_MODEM:
507 port_name = "Modem";
508 break;
509 case HSO_PORT_NETWORK:
510 port_name = "Network";
511 break;
512 default:
513 port_name = "Unknown";
514 break;
517 return sprintf(buf, "%s\n", port_name);
519 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
521 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
523 int idx;
525 for (idx = 0; idx < serial->num_rx_urbs; idx++)
526 if (serial->rx_urb[idx] == urb)
527 return idx;
528 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
529 return -1;
532 /* converts mux value to a port spec value */
533 static u32 hso_mux_to_port(int mux)
535 u32 result;
537 switch (mux) {
538 case 0x1:
539 result = HSO_PORT_CONTROL;
540 break;
541 case 0x2:
542 result = HSO_PORT_APP;
543 break;
544 case 0x4:
545 result = HSO_PORT_PCSC;
546 break;
547 case 0x8:
548 result = HSO_PORT_GPS;
549 break;
550 case 0x10:
551 result = HSO_PORT_APP2;
552 break;
553 default:
554 result = HSO_PORT_NO_PORT;
556 return result;
559 /* converts port spec value to a mux value */
560 static u32 hso_port_to_mux(int port)
562 u32 result;
564 switch (port & HSO_PORT_MASK) {
565 case HSO_PORT_CONTROL:
566 result = 0x0;
567 break;
568 case HSO_PORT_APP:
569 result = 0x1;
570 break;
571 case HSO_PORT_PCSC:
572 result = 0x2;
573 break;
574 case HSO_PORT_GPS:
575 result = 0x3;
576 break;
577 case HSO_PORT_APP2:
578 result = 0x4;
579 break;
580 default:
581 result = 0x0;
583 return result;
586 static struct hso_serial *get_serial_by_shared_int_and_type(
587 struct hso_shared_int *shared_int,
588 int mux)
590 int i, port;
592 port = hso_mux_to_port(mux);
594 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
595 if (serial_table[i]
596 && (dev2ser(serial_table[i])->shared_int == shared_int)
597 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
598 return dev2ser(serial_table[i]);
602 return NULL;
605 static struct hso_serial *get_serial_by_index(unsigned index)
607 struct hso_serial *serial = NULL;
608 unsigned long flags;
610 spin_lock_irqsave(&serial_table_lock, flags);
611 if (serial_table[index])
612 serial = dev2ser(serial_table[index]);
613 spin_unlock_irqrestore(&serial_table_lock, flags);
615 return serial;
618 static int get_free_serial_index(void)
620 int index;
621 unsigned long flags;
623 spin_lock_irqsave(&serial_table_lock, flags);
624 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
625 if (serial_table[index] == NULL) {
626 spin_unlock_irqrestore(&serial_table_lock, flags);
627 return index;
630 spin_unlock_irqrestore(&serial_table_lock, flags);
632 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
633 return -1;
636 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
638 unsigned long flags;
640 spin_lock_irqsave(&serial_table_lock, flags);
641 if (serial)
642 serial_table[index] = serial->parent;
643 else
644 serial_table[index] = NULL;
645 spin_unlock_irqrestore(&serial_table_lock, flags);
648 /* log a meaningful explanation of an USB status */
649 static void log_usb_status(int status, const char *function)
651 char *explanation;
653 switch (status) {
654 case -ENODEV:
655 explanation = "no device";
656 break;
657 case -ENOENT:
658 explanation = "endpoint not enabled";
659 break;
660 case -EPIPE:
661 explanation = "endpoint stalled";
662 break;
663 case -ENOSPC:
664 explanation = "not enough bandwidth";
665 break;
666 case -ESHUTDOWN:
667 explanation = "device disabled";
668 break;
669 case -EHOSTUNREACH:
670 explanation = "device suspended";
671 break;
672 case -EINVAL:
673 case -EAGAIN:
674 case -EFBIG:
675 case -EMSGSIZE:
676 explanation = "internal error";
677 break;
678 default:
679 explanation = "unknown status";
680 break;
682 D1("%s: received USB status - %s (%d)", function, explanation, status);
685 /* Network interface functions */
687 /* called when net interface is brought up by ifconfig */
688 static int hso_net_open(struct net_device *net)
690 struct hso_net *odev = netdev_priv(net);
691 unsigned long flags = 0;
693 if (!odev) {
694 dev_err(&net->dev, "No net device !\n");
695 return -ENODEV;
698 odev->skb_tx_buf = NULL;
700 /* setup environment */
701 spin_lock_irqsave(&odev->net_lock, flags);
702 odev->rx_parse_state = WAIT_IP;
703 odev->rx_buf_size = 0;
704 odev->rx_buf_missing = sizeof(struct iphdr);
705 spin_unlock_irqrestore(&odev->net_lock, flags);
707 /* We are up and running. */
708 set_bit(HSO_NET_RUNNING, &odev->flags);
709 hso_start_net_device(odev->parent);
711 /* Tell the kernel we are ready to start receiving from it */
712 netif_start_queue(net);
714 return 0;
717 /* called when interface is brought down by ifconfig */
718 static int hso_net_close(struct net_device *net)
720 struct hso_net *odev = netdev_priv(net);
722 /* we don't need the queue anymore */
723 netif_stop_queue(net);
724 /* no longer running */
725 clear_bit(HSO_NET_RUNNING, &odev->flags);
727 hso_stop_net_device(odev->parent);
729 /* done */
730 return 0;
733 /* USB tells is xmit done, we should start the netqueue again */
734 static void write_bulk_callback(struct urb *urb)
736 struct hso_net *odev = urb->context;
737 int status = urb->status;
739 /* Sanity check */
740 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
741 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
742 return;
745 /* Do we still have a valid kernel network device? */
746 if (!netif_device_present(odev->net)) {
747 dev_err(&urb->dev->dev, "%s: net device not present\n",
748 __func__);
749 return;
752 /* log status, but don't act on it, we don't need to resubmit anything
753 * anyhow */
754 if (status)
755 log_usb_status(status, __func__);
757 hso_put_activity(odev->parent);
759 /* Tell the network interface we are ready for another frame */
760 netif_wake_queue(odev->net);
763 /* called by kernel when we need to transmit a packet */
764 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
766 struct hso_net *odev = netdev_priv(net);
767 int result;
769 /* Tell the kernel, "No more frames 'til we are done with this one." */
770 netif_stop_queue(net);
771 if (hso_get_activity(odev->parent) == -EAGAIN) {
772 odev->skb_tx_buf = skb;
773 return 0;
776 /* log if asked */
777 DUMP1(skb->data, skb->len);
778 /* Copy it from kernel memory to OUR memory */
779 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
780 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
782 /* Fill in the URB for shipping it out. */
783 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
784 odev->parent->usb,
785 usb_sndbulkpipe(odev->parent->usb,
786 odev->out_endp->
787 bEndpointAddress & 0x7F),
788 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
789 odev);
791 /* Deal with the Zero Length packet problem, I hope */
792 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
794 /* Send the URB on its merry way. */
795 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
796 if (result) {
797 dev_warn(&odev->parent->interface->dev,
798 "failed mux_bulk_tx_urb %d", result);
799 net->stats.tx_errors++;
800 netif_start_queue(net);
801 } else {
802 net->stats.tx_packets++;
803 net->stats.tx_bytes += skb->len;
804 /* And tell the kernel when the last transmit started. */
805 net->trans_start = jiffies;
807 dev_kfree_skb(skb);
808 /* we're done */
809 return result;
812 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
814 struct hso_net *odev = netdev_priv(net);
816 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
817 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
818 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
821 static struct ethtool_ops ops = {
822 .get_drvinfo = hso_get_drvinfo,
823 .get_link = ethtool_op_get_link
826 /* called when a packet did not ack after watchdogtimeout */
827 static void hso_net_tx_timeout(struct net_device *net)
829 struct hso_net *odev = netdev_priv(net);
831 if (!odev)
832 return;
834 /* Tell syslog we are hosed. */
835 dev_warn(&net->dev, "Tx timed out.\n");
837 /* Tear the waiting frame off the list */
838 if (odev->mux_bulk_tx_urb
839 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
840 usb_unlink_urb(odev->mux_bulk_tx_urb);
842 /* Update statistics */
843 net->stats.tx_errors++;
846 /* make a real packet from the received USB buffer */
847 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
848 unsigned int count, unsigned char is_eop)
850 unsigned short temp_bytes;
851 unsigned short buffer_offset = 0;
852 unsigned short frame_len;
853 unsigned char *tmp_rx_buf;
855 /* log if needed */
856 D1("Rx %d bytes", count);
857 DUMP(ip_pkt, min(128, (int)count));
859 while (count) {
860 switch (odev->rx_parse_state) {
861 case WAIT_IP:
862 /* waiting for IP header. */
863 /* wanted bytes - size of ip header */
864 temp_bytes =
865 (count <
866 odev->rx_buf_missing) ? count : odev->
867 rx_buf_missing;
869 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
870 odev->rx_buf_size, ip_pkt + buffer_offset,
871 temp_bytes);
873 odev->rx_buf_size += temp_bytes;
874 buffer_offset += temp_bytes;
875 odev->rx_buf_missing -= temp_bytes;
876 count -= temp_bytes;
878 if (!odev->rx_buf_missing) {
879 /* header is complete allocate an sk_buffer and
880 * continue to WAIT_DATA */
881 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
883 if ((frame_len > DEFAULT_MRU) ||
884 (frame_len < sizeof(struct iphdr))) {
885 dev_err(&odev->net->dev,
886 "Invalid frame (%d) length\n",
887 frame_len);
888 odev->rx_parse_state = WAIT_SYNC;
889 continue;
891 /* Allocate an sk_buff */
892 odev->skb_rx_buf = dev_alloc_skb(frame_len);
893 if (!odev->skb_rx_buf) {
894 /* We got no receive buffer. */
895 D1("could not allocate memory");
896 odev->rx_parse_state = WAIT_SYNC;
897 return;
899 /* Here's where it came from */
900 odev->skb_rx_buf->dev = odev->net;
902 /* Copy what we got so far. make room for iphdr
903 * after tail. */
904 tmp_rx_buf =
905 skb_put(odev->skb_rx_buf,
906 sizeof(struct iphdr));
907 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
908 sizeof(struct iphdr));
910 /* ETH_HLEN */
911 odev->rx_buf_size = sizeof(struct iphdr);
913 /* Filip actually use .tot_len */
914 odev->rx_buf_missing =
915 frame_len - sizeof(struct iphdr);
916 odev->rx_parse_state = WAIT_DATA;
918 break;
920 case WAIT_DATA:
921 temp_bytes = (count < odev->rx_buf_missing)
922 ? count : odev->rx_buf_missing;
924 /* Copy the rest of the bytes that are left in the
925 * buffer into the waiting sk_buf. */
926 /* Make room for temp_bytes after tail. */
927 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
928 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
930 odev->rx_buf_missing -= temp_bytes;
931 count -= temp_bytes;
932 buffer_offset += temp_bytes;
933 odev->rx_buf_size += temp_bytes;
934 if (!odev->rx_buf_missing) {
935 /* Packet is complete. Inject into stack. */
936 /* We have IP packet here */
937 odev->skb_rx_buf->protocol =
938 __constant_htons(ETH_P_IP);
939 /* don't check it */
940 odev->skb_rx_buf->ip_summed =
941 CHECKSUM_UNNECESSARY;
943 skb_reset_mac_header(odev->skb_rx_buf);
945 /* Ship it off to the kernel */
946 netif_rx(odev->skb_rx_buf);
947 /* No longer our buffer. */
948 odev->skb_rx_buf = NULL;
950 /* update out statistics */
951 odev->net->stats.rx_packets++;
953 odev->net->stats.rx_bytes += odev->rx_buf_size;
955 odev->rx_buf_size = 0;
956 odev->rx_buf_missing = sizeof(struct iphdr);
957 odev->rx_parse_state = WAIT_IP;
959 break;
961 case WAIT_SYNC:
962 D1(" W_S");
963 count = 0;
964 break;
965 default:
966 D1(" ");
967 count--;
968 break;
972 /* Recovery mechanism for WAIT_SYNC state. */
973 if (is_eop) {
974 if (odev->rx_parse_state == WAIT_SYNC) {
975 odev->rx_parse_state = WAIT_IP;
976 odev->rx_buf_size = 0;
977 odev->rx_buf_missing = sizeof(struct iphdr);
982 /* Moving data from usb to kernel (in interrupt state) */
983 static void read_bulk_callback(struct urb *urb)
985 struct hso_net *odev = urb->context;
986 struct net_device *net;
987 int result;
988 int status = urb->status;
990 /* is al ok? (Filip: Who's Al ?) */
991 if (status) {
992 log_usb_status(status, __func__);
993 return;
996 /* Sanity check */
997 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
998 D1("BULK IN callback but driver is not active!");
999 return;
1001 usb_mark_last_busy(urb->dev);
1003 net = odev->net;
1005 if (!netif_device_present(net)) {
1006 /* Somebody killed our network interface... */
1007 return;
1010 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
1011 u32 rest;
1012 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1013 rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
1014 if (((rest == 5) || (rest == 6))
1015 && !memcmp(((u8 *) urb->transfer_buffer) +
1016 urb->actual_length - 4, crc_check, 4)) {
1017 urb->actual_length -= 4;
1021 /* do we even have a packet? */
1022 if (urb->actual_length) {
1023 /* Handle the IP stream, add header and push it onto network
1024 * stack if the packet is complete. */
1025 spin_lock(&odev->net_lock);
1026 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1027 (urb->transfer_buffer_length >
1028 urb->actual_length) ? 1 : 0);
1029 spin_unlock(&odev->net_lock);
1032 /* We are done with this URB, resubmit it. Prep the USB to wait for
1033 * another frame. Reuse same as received. */
1034 usb_fill_bulk_urb(urb,
1035 odev->parent->usb,
1036 usb_rcvbulkpipe(odev->parent->usb,
1037 odev->in_endp->
1038 bEndpointAddress & 0x7F),
1039 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1040 read_bulk_callback, odev);
1042 /* Give this to the USB subsystem so it can tell us when more data
1043 * arrives. */
1044 result = usb_submit_urb(urb, GFP_ATOMIC);
1045 if (result)
1046 dev_warn(&odev->parent->interface->dev,
1047 "%s failed submit mux_bulk_rx_urb %d", __func__,
1048 result);
1051 /* Serial driver functions */
1053 static void hso_init_termios(struct ktermios *termios)
1056 * The default requirements for this device are:
1058 termios->c_iflag &=
1059 ~(IGNBRK /* disable ignore break */
1060 | BRKINT /* disable break causes interrupt */
1061 | PARMRK /* disable mark parity errors */
1062 | ISTRIP /* disable clear high bit of input characters */
1063 | INLCR /* disable translate NL to CR */
1064 | IGNCR /* disable ignore CR */
1065 | ICRNL /* disable translate CR to NL */
1066 | IXON); /* disable enable XON/XOFF flow control */
1068 /* disable postprocess output characters */
1069 termios->c_oflag &= ~OPOST;
1071 termios->c_lflag &=
1072 ~(ECHO /* disable echo input characters */
1073 | ECHONL /* disable echo new line */
1074 | ICANON /* disable erase, kill, werase, and rprnt
1075 special characters */
1076 | ISIG /* disable interrupt, quit, and suspend special
1077 characters */
1078 | IEXTEN); /* disable non-POSIX special characters */
1080 termios->c_cflag &=
1081 ~(CSIZE /* no size */
1082 | PARENB /* disable parity bit */
1083 | CBAUD /* clear current baud rate */
1084 | CBAUDEX); /* clear current buad rate */
1086 termios->c_cflag |= CS8; /* character size 8 bits */
1088 /* baud rate 115200 */
1089 tty_termios_encode_baud_rate(termios, 115200, 115200);
1092 static void _hso_serial_set_termios(struct tty_struct *tty,
1093 struct ktermios *old)
1095 struct hso_serial *serial = get_serial_by_tty(tty);
1096 struct ktermios *termios;
1098 if (!serial) {
1099 printk(KERN_ERR "%s: no tty structures", __func__);
1100 return;
1103 D4("port %d", serial->minor);
1106 * Fix up unsupported bits
1108 termios = tty->termios;
1109 termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1111 termios->c_cflag &=
1112 ~(CSIZE /* no size */
1113 | PARENB /* disable parity bit */
1114 | CBAUD /* clear current baud rate */
1115 | CBAUDEX); /* clear current buad rate */
1117 termios->c_cflag |= CS8; /* character size 8 bits */
1119 /* baud rate 115200 */
1120 tty_encode_baud_rate(tty, 115200, 115200);
1123 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1125 int result;
1126 #ifdef CONFIG_HSO_AUTOPM
1127 usb_mark_last_busy(urb->dev);
1128 #endif
1129 /* We are done with this URB, resubmit it. Prep the USB to wait for
1130 * another frame */
1131 usb_fill_bulk_urb(urb, serial->parent->usb,
1132 usb_rcvbulkpipe(serial->parent->usb,
1133 serial->in_endp->
1134 bEndpointAddress & 0x7F),
1135 urb->transfer_buffer, serial->rx_data_length,
1136 hso_std_serial_read_bulk_callback, serial);
1137 /* Give this to the USB subsystem so it can tell us when more data
1138 * arrives. */
1139 result = usb_submit_urb(urb, GFP_ATOMIC);
1140 if (result) {
1141 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1142 __func__, result);
1149 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1151 int count;
1152 struct urb *curr_urb;
1154 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1155 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1156 count = put_rxbuf_data(curr_urb, serial);
1157 if (count == -1)
1158 return;
1159 if (count == 0) {
1160 serial->curr_rx_urb_idx++;
1161 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1162 serial->curr_rx_urb_idx = 0;
1163 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1168 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1170 int count = 0;
1171 struct urb *urb;
1173 urb = serial->rx_urb[0];
1174 if (serial->open_count > 0) {
1175 count = put_rxbuf_data(urb, serial);
1176 if (count == -1)
1177 return;
1179 /* Re issue a read as long as we receive data. */
1181 if (count == 0 && ((urb->actual_length != 0) ||
1182 (serial->rx_state == RX_PENDING))) {
1183 serial->rx_state = RX_SENT;
1184 hso_mux_serial_read(serial);
1185 } else
1186 serial->rx_state = RX_IDLE;
1190 /* read callback for Diag and CS port */
1191 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1193 struct hso_serial *serial = urb->context;
1194 int status = urb->status;
1196 /* sanity check */
1197 if (!serial) {
1198 D1("serial == NULL");
1199 return;
1200 } else if (status) {
1201 log_usb_status(status, __func__);
1202 return;
1205 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1206 D1("Actual length = %d\n", urb->actual_length);
1207 DUMP1(urb->transfer_buffer, urb->actual_length);
1209 /* Anyone listening? */
1210 if (serial->open_count == 0)
1211 return;
1213 if (status == 0) {
1214 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1215 u32 rest;
1216 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1217 rest =
1218 urb->actual_length %
1219 serial->in_endp->wMaxPacketSize;
1220 if (((rest == 5) || (rest == 6))
1221 && !memcmp(((u8 *) urb->transfer_buffer) +
1222 urb->actual_length - 4, crc_check, 4)) {
1223 urb->actual_length -= 4;
1226 /* Valid data, handle RX data */
1227 spin_lock(&serial->serial_lock);
1228 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1229 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1230 spin_unlock(&serial->serial_lock);
1231 } else if (status == -ENOENT || status == -ECONNRESET) {
1232 /* Unlinked - check for throttled port. */
1233 D2("Port %d, successfully unlinked urb", serial->minor);
1234 spin_lock(&serial->serial_lock);
1235 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1236 hso_resubmit_rx_bulk_urb(serial, urb);
1237 spin_unlock(&serial->serial_lock);
1238 } else {
1239 D2("Port %d, status = %d for read urb", serial->minor, status);
1240 return;
1245 * This needs to be a tasklet otherwise we will
1246 * end up recursively calling this function.
1248 void hso_unthrottle_tasklet(struct hso_serial *serial)
1250 unsigned long flags;
1252 spin_lock_irqsave(&serial->serial_lock, flags);
1253 if ((serial->parent->port_spec & HSO_INTF_MUX))
1254 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1255 else
1256 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1257 spin_unlock_irqrestore(&serial->serial_lock, flags);
1260 static void hso_unthrottle(struct tty_struct *tty)
1262 struct hso_serial *serial = get_serial_by_tty(tty);
1264 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1267 void hso_unthrottle_workfunc(struct work_struct *work)
1269 struct hso_serial *serial =
1270 container_of(work, struct hso_serial,
1271 retry_unthrottle_workqueue);
1272 hso_unthrottle_tasklet(serial);
1275 /* open the requested serial port */
1276 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1278 struct hso_serial *serial = get_serial_by_index(tty->index);
1279 int result;
1281 /* sanity check */
1282 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1283 WARN_ON(1);
1284 tty->driver_data = NULL;
1285 D1("Failed to open port");
1286 return -ENODEV;
1289 mutex_lock(&serial->parent->mutex);
1290 result = usb_autopm_get_interface(serial->parent->interface);
1291 if (result < 0)
1292 goto err_out;
1294 D1("Opening %d", serial->minor);
1295 kref_get(&serial->parent->ref);
1297 /* setup */
1298 spin_lock_irq(&serial->serial_lock);
1299 tty->driver_data = serial;
1300 tty_kref_put(serial->tty);
1301 serial->tty = tty_kref_get(tty);
1302 spin_unlock_irq(&serial->serial_lock);
1304 /* check for port already opened, if not set the termios */
1305 serial->open_count++;
1306 if (serial->open_count == 1) {
1307 tty->low_latency = 1;
1308 serial->rx_state = RX_IDLE;
1309 /* Force default termio settings */
1310 _hso_serial_set_termios(tty, NULL);
1311 tasklet_init(&serial->unthrottle_tasklet,
1312 (void (*)(unsigned long))hso_unthrottle_tasklet,
1313 (unsigned long)serial);
1314 INIT_WORK(&serial->retry_unthrottle_workqueue,
1315 hso_unthrottle_workfunc);
1316 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1317 if (result) {
1318 hso_stop_serial_device(serial->parent);
1319 serial->open_count--;
1320 kref_put(&serial->parent->ref, hso_serial_ref_free);
1322 } else {
1323 D1("Port was already open");
1326 usb_autopm_put_interface(serial->parent->interface);
1328 /* done */
1329 if (result)
1330 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1331 err_out:
1332 mutex_unlock(&serial->parent->mutex);
1333 return result;
1336 /* close the requested serial port */
1337 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1339 struct hso_serial *serial = tty->driver_data;
1340 u8 usb_gone;
1342 D1("Closing serial port");
1344 /* Open failed, no close cleanup required */
1345 if (serial == NULL)
1346 return;
1348 mutex_lock(&serial->parent->mutex);
1349 usb_gone = serial->parent->usb_gone;
1351 if (!usb_gone)
1352 usb_autopm_get_interface(serial->parent->interface);
1354 /* reset the rts and dtr */
1355 /* do the actual close */
1356 serial->open_count--;
1357 kref_put(&serial->parent->ref, hso_serial_ref_free);
1358 if (serial->open_count <= 0) {
1359 serial->open_count = 0;
1360 spin_lock_irq(&serial->serial_lock);
1361 if (serial->tty == tty) {
1362 serial->tty->driver_data = NULL;
1363 serial->tty = NULL;
1364 tty_kref_put(tty);
1366 spin_unlock_irq(&serial->serial_lock);
1367 if (!usb_gone)
1368 hso_stop_serial_device(serial->parent);
1369 tasklet_kill(&serial->unthrottle_tasklet);
1370 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1373 if (!usb_gone)
1374 usb_autopm_put_interface(serial->parent->interface);
1376 mutex_unlock(&serial->parent->mutex);
1379 /* close the requested serial port */
1380 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1381 int count)
1383 struct hso_serial *serial = get_serial_by_tty(tty);
1384 int space, tx_bytes;
1385 unsigned long flags;
1387 /* sanity check */
1388 if (serial == NULL) {
1389 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1390 return -ENODEV;
1393 spin_lock_irqsave(&serial->serial_lock, flags);
1395 space = serial->tx_data_length - serial->tx_buffer_count;
1396 tx_bytes = (count < space) ? count : space;
1398 if (!tx_bytes)
1399 goto out;
1401 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1402 serial->tx_buffer_count += tx_bytes;
1404 out:
1405 spin_unlock_irqrestore(&serial->serial_lock, flags);
1407 hso_kick_transmit(serial);
1408 /* done */
1409 return tx_bytes;
1412 /* how much room is there for writing */
1413 static int hso_serial_write_room(struct tty_struct *tty)
1415 struct hso_serial *serial = get_serial_by_tty(tty);
1416 int room;
1417 unsigned long flags;
1419 spin_lock_irqsave(&serial->serial_lock, flags);
1420 room = serial->tx_data_length - serial->tx_buffer_count;
1421 spin_unlock_irqrestore(&serial->serial_lock, flags);
1423 /* return free room */
1424 return room;
1427 /* setup the term */
1428 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1430 struct hso_serial *serial = get_serial_by_tty(tty);
1431 unsigned long flags;
1433 if (old)
1434 D5("Termios called with: cflags new[%d] - old[%d]",
1435 tty->termios->c_cflag, old->c_cflag);
1437 /* the actual setup */
1438 spin_lock_irqsave(&serial->serial_lock, flags);
1439 if (serial->open_count)
1440 _hso_serial_set_termios(tty, old);
1441 else
1442 tty->termios = old;
1443 spin_unlock_irqrestore(&serial->serial_lock, flags);
1445 /* done */
1446 return;
1449 /* how many characters in the buffer */
1450 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1452 struct hso_serial *serial = get_serial_by_tty(tty);
1453 int chars;
1454 unsigned long flags;
1456 /* sanity check */
1457 if (serial == NULL)
1458 return 0;
1460 spin_lock_irqsave(&serial->serial_lock, flags);
1461 chars = serial->tx_buffer_count;
1462 spin_unlock_irqrestore(&serial->serial_lock, flags);
1464 return chars;
1466 int tiocmget_submit_urb(struct hso_serial *serial,
1467 struct hso_tiocmget *tiocmget,
1468 struct usb_device *usb)
1470 int result;
1472 if (serial->parent->usb_gone)
1473 return -ENODEV;
1474 usb_fill_int_urb(tiocmget->urb, usb,
1475 usb_rcvintpipe(usb,
1476 tiocmget->endp->
1477 bEndpointAddress & 0x7F),
1478 &tiocmget->serial_state_notification,
1479 sizeof(struct hso_serial_state_notification),
1480 tiocmget_intr_callback, serial,
1481 tiocmget->endp->bInterval);
1482 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1483 if (result) {
1484 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1485 result);
1487 return result;
1491 static void tiocmget_intr_callback(struct urb *urb)
1493 struct hso_serial *serial = urb->context;
1494 struct hso_tiocmget *tiocmget;
1495 int status = urb->status;
1496 u16 UART_state_bitmap, prev_UART_state_bitmap;
1497 struct uart_icount *icount;
1498 struct hso_serial_state_notification *serial_state_notification;
1499 struct usb_device *usb;
1501 /* Sanity checks */
1502 if (!serial)
1503 return;
1504 if (status) {
1505 log_usb_status(status, __func__);
1506 return;
1508 tiocmget = serial->tiocmget;
1509 if (!tiocmget)
1510 return;
1511 usb = serial->parent->usb;
1512 serial_state_notification = &tiocmget->serial_state_notification;
1513 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1514 serial_state_notification->bNotification != B_NOTIFICATION ||
1515 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1516 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
1517 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1518 dev_warn(&usb->dev,
1519 "hso received invalid serial state notification\n");
1520 DUMP(serial_state_notification,
1521 sizeof(hso_serial_state_notifation))
1522 } else {
1524 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1525 UART_state_bitmap);
1526 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1527 icount = &tiocmget->icount;
1528 spin_lock(&serial->serial_lock);
1529 if ((UART_state_bitmap & B_OVERRUN) !=
1530 (prev_UART_state_bitmap & B_OVERRUN))
1531 icount->parity++;
1532 if ((UART_state_bitmap & B_PARITY) !=
1533 (prev_UART_state_bitmap & B_PARITY))
1534 icount->parity++;
1535 if ((UART_state_bitmap & B_FRAMING) !=
1536 (prev_UART_state_bitmap & B_FRAMING))
1537 icount->frame++;
1538 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1539 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1540 icount->rng++;
1541 if ((UART_state_bitmap & B_BREAK) !=
1542 (prev_UART_state_bitmap & B_BREAK))
1543 icount->brk++;
1544 if ((UART_state_bitmap & B_TX_CARRIER) !=
1545 (prev_UART_state_bitmap & B_TX_CARRIER))
1546 icount->dsr++;
1547 if ((UART_state_bitmap & B_RX_CARRIER) !=
1548 (prev_UART_state_bitmap & B_RX_CARRIER))
1549 icount->dcd++;
1550 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1551 spin_unlock(&serial->serial_lock);
1552 tiocmget->intr_completed = 1;
1553 wake_up_interruptible(&tiocmget->waitq);
1555 memset(serial_state_notification, 0,
1556 sizeof(struct hso_serial_state_notification));
1557 tiocmget_submit_urb(serial,
1558 tiocmget,
1559 serial->parent->usb);
1563 * next few functions largely stolen from drivers/serial/serial_core.c
1565 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1566 * - mask passed in arg for lines of interest
1567 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1568 * Caller should use TIOCGICOUNT to see which one it was
1570 static int
1571 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1573 DECLARE_WAITQUEUE(wait, current);
1574 struct uart_icount cprev, cnow;
1575 struct hso_tiocmget *tiocmget;
1576 int ret;
1578 tiocmget = serial->tiocmget;
1579 if (!tiocmget)
1580 return -ENOENT;
1582 * note the counters on entry
1584 spin_lock_irq(&serial->serial_lock);
1585 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1586 spin_unlock_irq(&serial->serial_lock);
1587 add_wait_queue(&tiocmget->waitq, &wait);
1588 for (;;) {
1589 spin_lock_irq(&serial->serial_lock);
1590 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1591 spin_unlock_irq(&serial->serial_lock);
1592 set_current_state(TASK_INTERRUPTIBLE);
1593 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1594 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1595 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1596 ret = 0;
1597 break;
1599 schedule();
1600 /* see if a signal did it */
1601 if (signal_pending(current)) {
1602 ret = -ERESTARTSYS;
1603 break;
1605 cprev = cnow;
1607 current->state = TASK_RUNNING;
1608 remove_wait_queue(&tiocmget->waitq, &wait);
1610 return ret;
1614 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1615 * Return: write counters to the user passed counter struct
1616 * NB: both 1->0 and 0->1 transitions are counted except for
1617 * RI where only 0->1 is counted.
1619 static int hso_get_count(struct hso_serial *serial,
1620 struct serial_icounter_struct __user *icnt)
1622 struct serial_icounter_struct icount;
1623 struct uart_icount cnow;
1624 struct hso_tiocmget *tiocmget = serial->tiocmget;
1626 if (!tiocmget)
1627 return -ENOENT;
1628 spin_lock_irq(&serial->serial_lock);
1629 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1630 spin_unlock_irq(&serial->serial_lock);
1632 icount.cts = cnow.cts;
1633 icount.dsr = cnow.dsr;
1634 icount.rng = cnow.rng;
1635 icount.dcd = cnow.dcd;
1636 icount.rx = cnow.rx;
1637 icount.tx = cnow.tx;
1638 icount.frame = cnow.frame;
1639 icount.overrun = cnow.overrun;
1640 icount.parity = cnow.parity;
1641 icount.brk = cnow.brk;
1642 icount.buf_overrun = cnow.buf_overrun;
1644 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1648 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1650 int retval;
1651 struct hso_serial *serial = get_serial_by_tty(tty);
1652 struct hso_tiocmget *tiocmget;
1653 u16 UART_state_bitmap;
1655 /* sanity check */
1656 if (!serial) {
1657 D1("no tty structures");
1658 return -EINVAL;
1660 spin_lock_irq(&serial->serial_lock);
1661 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
1662 ((serial->dtr_state) ? TIOCM_DTR : 0);
1663 tiocmget = serial->tiocmget;
1664 if (tiocmget) {
1666 UART_state_bitmap = le16_to_cpu(
1667 tiocmget->prev_UART_state_bitmap);
1668 if (UART_state_bitmap & B_RING_SIGNAL)
1669 retval |= TIOCM_RNG;
1670 if (UART_state_bitmap & B_RX_CARRIER)
1671 retval |= TIOCM_CD;
1672 if (UART_state_bitmap & B_TX_CARRIER)
1673 retval |= TIOCM_DSR;
1675 spin_unlock_irq(&serial->serial_lock);
1676 return retval;
1679 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1680 unsigned int set, unsigned int clear)
1682 int val = 0;
1683 unsigned long flags;
1684 int if_num;
1685 struct hso_serial *serial = get_serial_by_tty(tty);
1687 /* sanity check */
1688 if (!serial) {
1689 D1("no tty structures");
1690 return -EINVAL;
1692 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1694 spin_lock_irqsave(&serial->serial_lock, flags);
1695 if (set & TIOCM_RTS)
1696 serial->rts_state = 1;
1697 if (set & TIOCM_DTR)
1698 serial->dtr_state = 1;
1700 if (clear & TIOCM_RTS)
1701 serial->rts_state = 0;
1702 if (clear & TIOCM_DTR)
1703 serial->dtr_state = 0;
1705 if (serial->dtr_state)
1706 val |= 0x01;
1707 if (serial->rts_state)
1708 val |= 0x02;
1710 spin_unlock_irqrestore(&serial->serial_lock, flags);
1712 return usb_control_msg(serial->parent->usb,
1713 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1714 0x21, val, if_num, NULL, 0,
1715 USB_CTRL_SET_TIMEOUT);
1718 static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
1719 unsigned int cmd, unsigned long arg)
1721 struct hso_serial *serial = get_serial_by_tty(tty);
1722 void __user *uarg = (void __user *)arg;
1723 int ret = 0;
1724 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1726 if (!serial)
1727 return -ENODEV;
1728 switch (cmd) {
1729 case TIOCMIWAIT:
1730 ret = hso_wait_modem_status(serial, arg);
1731 break;
1733 case TIOCGICOUNT:
1734 ret = hso_get_count(serial, uarg);
1735 break;
1736 default:
1737 ret = -ENOIOCTLCMD;
1738 break;
1740 return ret;
1744 /* starts a transmit */
1745 static void hso_kick_transmit(struct hso_serial *serial)
1747 u8 *temp;
1748 unsigned long flags;
1749 int res;
1751 spin_lock_irqsave(&serial->serial_lock, flags);
1752 if (!serial->tx_buffer_count)
1753 goto out;
1755 if (serial->tx_urb_used)
1756 goto out;
1758 /* Wakeup USB interface if necessary */
1759 if (hso_get_activity(serial->parent) == -EAGAIN)
1760 goto out;
1762 /* Switch pointers around to avoid memcpy */
1763 temp = serial->tx_buffer;
1764 serial->tx_buffer = serial->tx_data;
1765 serial->tx_data = temp;
1766 serial->tx_data_count = serial->tx_buffer_count;
1767 serial->tx_buffer_count = 0;
1769 /* If temp is set, it means we switched buffers */
1770 if (temp && serial->write_data) {
1771 res = serial->write_data(serial);
1772 if (res >= 0)
1773 serial->tx_urb_used = 1;
1775 out:
1776 spin_unlock_irqrestore(&serial->serial_lock, flags);
1779 /* make a request (for reading and writing data to muxed serial port) */
1780 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1781 struct urb *ctrl_urb,
1782 struct usb_ctrlrequest *ctrl_req,
1783 u8 *ctrl_urb_data, u32 size)
1785 int result;
1786 int pipe;
1788 /* Sanity check */
1789 if (!serial || !ctrl_urb || !ctrl_req) {
1790 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1791 return -EINVAL;
1794 /* initialize */
1795 ctrl_req->wValue = 0;
1796 ctrl_req->wIndex = hso_port_to_mux(port);
1797 ctrl_req->wLength = size;
1799 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1800 /* Reading command */
1801 ctrl_req->bRequestType = USB_DIR_IN |
1802 USB_TYPE_OPTION_VENDOR |
1803 USB_RECIP_INTERFACE;
1804 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1805 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1806 } else {
1807 /* Writing command */
1808 ctrl_req->bRequestType = USB_DIR_OUT |
1809 USB_TYPE_OPTION_VENDOR |
1810 USB_RECIP_INTERFACE;
1811 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1812 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1814 /* syslog */
1815 D2("%s command (%02x) len: %d, port: %d",
1816 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1817 ctrl_req->bRequestType, ctrl_req->wLength, port);
1819 /* Load ctrl urb */
1820 ctrl_urb->transfer_flags = 0;
1821 usb_fill_control_urb(ctrl_urb,
1822 serial->parent->usb,
1823 pipe,
1824 (u8 *) ctrl_req,
1825 ctrl_urb_data, size, ctrl_callback, serial);
1826 /* Send it on merry way */
1827 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1828 if (result) {
1829 dev_err(&ctrl_urb->dev->dev,
1830 "%s failed submit ctrl_urb %d type %d", __func__,
1831 result, type);
1832 return result;
1835 /* done */
1836 return size;
1839 /* called by intr_callback when read occurs */
1840 static int hso_mux_serial_read(struct hso_serial *serial)
1842 if (!serial)
1843 return -EINVAL;
1845 /* clean data */
1846 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1847 /* make the request */
1849 if (serial->num_rx_urbs != 1) {
1850 dev_err(&serial->parent->interface->dev,
1851 "ERROR: mux'd reads with multiple buffers "
1852 "not possible\n");
1853 return 0;
1855 return mux_device_request(serial,
1856 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1857 serial->parent->port_spec & HSO_PORT_MASK,
1858 serial->rx_urb[0],
1859 &serial->ctrl_req_rx,
1860 serial->rx_data[0], serial->rx_data_length);
1863 /* used for muxed serial port callback (muxed serial read) */
1864 static void intr_callback(struct urb *urb)
1866 struct hso_shared_int *shared_int = urb->context;
1867 struct hso_serial *serial;
1868 unsigned char *port_req;
1869 int status = urb->status;
1870 int i;
1872 usb_mark_last_busy(urb->dev);
1874 /* sanity check */
1875 if (!shared_int)
1876 return;
1878 /* status check */
1879 if (status) {
1880 log_usb_status(status, __func__);
1881 return;
1883 D4("\n--- Got intr callback 0x%02X ---", status);
1885 /* what request? */
1886 port_req = urb->transfer_buffer;
1887 D4(" port_req = 0x%.2X\n", *port_req);
1888 /* loop over all muxed ports to find the one sending this */
1889 for (i = 0; i < 8; i++) {
1890 /* max 8 channels on MUX */
1891 if (*port_req & (1 << i)) {
1892 serial = get_serial_by_shared_int_and_type(shared_int,
1893 (1 << i));
1894 if (serial != NULL) {
1895 D1("Pending read interrupt on port %d\n", i);
1896 spin_lock(&serial->serial_lock);
1897 if (serial->rx_state == RX_IDLE) {
1898 /* Setup and send a ctrl req read on
1899 * port i */
1900 if (!serial->rx_urb_filled[0]) {
1901 serial->rx_state = RX_SENT;
1902 hso_mux_serial_read(serial);
1903 } else
1904 serial->rx_state = RX_PENDING;
1906 } else {
1907 D1("Already pending a read on "
1908 "port %d\n", i);
1910 spin_unlock(&serial->serial_lock);
1914 /* Resubmit interrupt urb */
1915 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1918 /* called for writing to muxed serial port */
1919 static int hso_mux_serial_write_data(struct hso_serial *serial)
1921 if (NULL == serial)
1922 return -EINVAL;
1924 return mux_device_request(serial,
1925 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1926 serial->parent->port_spec & HSO_PORT_MASK,
1927 serial->tx_urb,
1928 &serial->ctrl_req_tx,
1929 serial->tx_data, serial->tx_data_count);
1932 /* write callback for Diag and CS port */
1933 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1935 struct hso_serial *serial = urb->context;
1936 int status = urb->status;
1937 struct tty_struct *tty;
1939 /* sanity check */
1940 if (!serial) {
1941 D1("serial == NULL");
1942 return;
1945 spin_lock(&serial->serial_lock);
1946 serial->tx_urb_used = 0;
1947 tty = tty_kref_get(serial->tty);
1948 spin_unlock(&serial->serial_lock);
1949 if (status) {
1950 log_usb_status(status, __func__);
1951 tty_kref_put(tty);
1952 return;
1954 hso_put_activity(serial->parent);
1955 if (tty) {
1956 tty_wakeup(tty);
1957 tty_kref_put(tty);
1959 hso_kick_transmit(serial);
1961 D1(" ");
1962 return;
1965 /* called for writing diag or CS serial port */
1966 static int hso_std_serial_write_data(struct hso_serial *serial)
1968 int count = serial->tx_data_count;
1969 int result;
1971 usb_fill_bulk_urb(serial->tx_urb,
1972 serial->parent->usb,
1973 usb_sndbulkpipe(serial->parent->usb,
1974 serial->out_endp->
1975 bEndpointAddress & 0x7F),
1976 serial->tx_data, serial->tx_data_count,
1977 hso_std_serial_write_bulk_callback, serial);
1979 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1980 if (result) {
1981 dev_warn(&serial->parent->usb->dev,
1982 "Failed to submit urb - res %d\n", result);
1983 return result;
1986 return count;
1989 /* callback after read or write on muxed serial port */
1990 static void ctrl_callback(struct urb *urb)
1992 struct hso_serial *serial = urb->context;
1993 struct usb_ctrlrequest *req;
1994 int status = urb->status;
1995 struct tty_struct *tty;
1997 /* sanity check */
1998 if (!serial)
1999 return;
2001 spin_lock(&serial->serial_lock);
2002 serial->tx_urb_used = 0;
2003 tty = tty_kref_get(serial->tty);
2004 spin_unlock(&serial->serial_lock);
2005 if (status) {
2006 log_usb_status(status, __func__);
2007 tty_kref_put(tty);
2008 return;
2011 /* what request? */
2012 req = (struct usb_ctrlrequest *)(urb->setup_packet);
2013 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2014 D4("Actual length of urb = %d\n", urb->actual_length);
2015 DUMP1(urb->transfer_buffer, urb->actual_length);
2017 if (req->bRequestType ==
2018 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2019 /* response to a read command */
2020 serial->rx_urb_filled[0] = 1;
2021 spin_lock(&serial->serial_lock);
2022 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2023 spin_unlock(&serial->serial_lock);
2024 } else {
2025 hso_put_activity(serial->parent);
2026 if (tty)
2027 tty_wakeup(tty);
2028 /* response to a write command */
2029 hso_kick_transmit(serial);
2031 tty_kref_put(tty);
2034 /* handle RX data for serial port */
2035 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
2037 struct tty_struct *tty;
2038 int write_length_remaining = 0;
2039 int curr_write_len;
2041 /* Sanity check */
2042 if (urb == NULL || serial == NULL) {
2043 D1("serial = NULL");
2044 return -2;
2047 /* All callers to put_rxbuf_data hold serial_lock */
2048 tty = tty_kref_get(serial->tty);
2050 /* Push data to tty */
2051 if (tty) {
2052 write_length_remaining = urb->actual_length -
2053 serial->curr_rx_urb_offset;
2054 D1("data to push to tty");
2055 while (write_length_remaining) {
2056 if (test_bit(TTY_THROTTLED, &tty->flags)) {
2057 tty_kref_put(tty);
2058 return -1;
2060 curr_write_len = tty_insert_flip_string
2061 (tty, urb->transfer_buffer +
2062 serial->curr_rx_urb_offset,
2063 write_length_remaining);
2064 serial->curr_rx_urb_offset += curr_write_len;
2065 write_length_remaining -= curr_write_len;
2066 tty_flip_buffer_push(tty);
2069 if (write_length_remaining == 0) {
2070 serial->curr_rx_urb_offset = 0;
2071 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2073 tty_kref_put(tty);
2074 return write_length_remaining;
2078 /* Base driver functions */
2080 static void hso_log_port(struct hso_device *hso_dev)
2082 char *port_type;
2083 char port_dev[20];
2085 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2086 case HSO_PORT_CONTROL:
2087 port_type = "Control";
2088 break;
2089 case HSO_PORT_APP:
2090 port_type = "Application";
2091 break;
2092 case HSO_PORT_GPS:
2093 port_type = "GPS";
2094 break;
2095 case HSO_PORT_GPS_CONTROL:
2096 port_type = "GPS control";
2097 break;
2098 case HSO_PORT_APP2:
2099 port_type = "Application2";
2100 break;
2101 case HSO_PORT_PCSC:
2102 port_type = "PCSC";
2103 break;
2104 case HSO_PORT_DIAG:
2105 port_type = "Diagnostic";
2106 break;
2107 case HSO_PORT_DIAG2:
2108 port_type = "Diagnostic2";
2109 break;
2110 case HSO_PORT_MODEM:
2111 port_type = "Modem";
2112 break;
2113 case HSO_PORT_NETWORK:
2114 port_type = "Network";
2115 break;
2116 default:
2117 port_type = "Unknown";
2118 break;
2120 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2121 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2122 } else
2123 sprintf(port_dev, "/dev/%s%d", tty_filename,
2124 dev2ser(hso_dev)->minor);
2126 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2127 port_type, port_dev);
2130 static int hso_start_net_device(struct hso_device *hso_dev)
2132 int i, result = 0;
2133 struct hso_net *hso_net = dev2net(hso_dev);
2135 if (!hso_net)
2136 return -ENODEV;
2138 /* send URBs for all read buffers */
2139 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2141 /* Prep a receive URB */
2142 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2143 hso_dev->usb,
2144 usb_rcvbulkpipe(hso_dev->usb,
2145 hso_net->in_endp->
2146 bEndpointAddress & 0x7F),
2147 hso_net->mux_bulk_rx_buf_pool[i],
2148 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2149 hso_net);
2151 /* Put it out there so the device can send us stuff */
2152 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2153 GFP_NOIO);
2154 if (result)
2155 dev_warn(&hso_dev->usb->dev,
2156 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2157 i, result);
2160 return result;
2163 static int hso_stop_net_device(struct hso_device *hso_dev)
2165 int i;
2166 struct hso_net *hso_net = dev2net(hso_dev);
2168 if (!hso_net)
2169 return -ENODEV;
2171 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2172 if (hso_net->mux_bulk_rx_urb_pool[i])
2173 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2176 if (hso_net->mux_bulk_tx_urb)
2177 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2179 return 0;
2182 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2184 int i, result = 0;
2185 struct hso_serial *serial = dev2ser(hso_dev);
2187 if (!serial)
2188 return -ENODEV;
2190 /* If it is not the MUX port fill in and submit a bulk urb (already
2191 * allocated in hso_serial_start) */
2192 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2193 for (i = 0; i < serial->num_rx_urbs; i++) {
2194 usb_fill_bulk_urb(serial->rx_urb[i],
2195 serial->parent->usb,
2196 usb_rcvbulkpipe(serial->parent->usb,
2197 serial->in_endp->
2198 bEndpointAddress &
2199 0x7F),
2200 serial->rx_data[i],
2201 serial->rx_data_length,
2202 hso_std_serial_read_bulk_callback,
2203 serial);
2204 result = usb_submit_urb(serial->rx_urb[i], flags);
2205 if (result) {
2206 dev_warn(&serial->parent->usb->dev,
2207 "Failed to submit urb - res %d\n",
2208 result);
2209 break;
2212 } else {
2213 mutex_lock(&serial->shared_int->shared_int_lock);
2214 if (!serial->shared_int->use_count) {
2215 result =
2216 hso_mux_submit_intr_urb(serial->shared_int,
2217 hso_dev->usb, flags);
2219 serial->shared_int->use_count++;
2220 mutex_unlock(&serial->shared_int->shared_int_lock);
2222 if (serial->tiocmget)
2223 tiocmget_submit_urb(serial,
2224 serial->tiocmget,
2225 serial->parent->usb);
2226 return result;
2229 static int hso_stop_serial_device(struct hso_device *hso_dev)
2231 int i;
2232 struct hso_serial *serial = dev2ser(hso_dev);
2233 struct hso_tiocmget *tiocmget;
2235 if (!serial)
2236 return -ENODEV;
2238 for (i = 0; i < serial->num_rx_urbs; i++) {
2239 if (serial->rx_urb[i]) {
2240 usb_kill_urb(serial->rx_urb[i]);
2241 serial->rx_urb_filled[i] = 0;
2244 serial->curr_rx_urb_idx = 0;
2245 serial->curr_rx_urb_offset = 0;
2247 if (serial->tx_urb)
2248 usb_kill_urb(serial->tx_urb);
2250 if (serial->shared_int) {
2251 mutex_lock(&serial->shared_int->shared_int_lock);
2252 if (serial->shared_int->use_count &&
2253 (--serial->shared_int->use_count == 0)) {
2254 struct urb *urb;
2256 urb = serial->shared_int->shared_intr_urb;
2257 if (urb)
2258 usb_kill_urb(urb);
2260 mutex_unlock(&serial->shared_int->shared_int_lock);
2262 tiocmget = serial->tiocmget;
2263 if (tiocmget) {
2264 wake_up_interruptible(&tiocmget->waitq);
2265 usb_kill_urb(tiocmget->urb);
2268 return 0;
2271 static void hso_serial_common_free(struct hso_serial *serial)
2273 int i;
2275 if (serial->parent->dev)
2276 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2278 tty_unregister_device(tty_drv, serial->minor);
2280 for (i = 0; i < serial->num_rx_urbs; i++) {
2281 /* unlink and free RX URB */
2282 usb_free_urb(serial->rx_urb[i]);
2283 /* free the RX buffer */
2284 kfree(serial->rx_data[i]);
2287 /* unlink and free TX URB */
2288 usb_free_urb(serial->tx_urb);
2289 kfree(serial->tx_data);
2292 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2293 int rx_size, int tx_size)
2295 struct device *dev;
2296 int minor;
2297 int i;
2299 minor = get_free_serial_index();
2300 if (minor < 0)
2301 goto exit;
2303 /* register our minor number */
2304 serial->parent->dev = tty_register_device(tty_drv, minor,
2305 &serial->parent->interface->dev);
2306 dev = serial->parent->dev;
2307 dev->driver_data = serial->parent;
2308 i = device_create_file(dev, &dev_attr_hsotype);
2310 /* fill in specific data for later use */
2311 serial->minor = minor;
2312 serial->magic = HSO_SERIAL_MAGIC;
2313 spin_lock_init(&serial->serial_lock);
2314 serial->num_rx_urbs = num_urbs;
2316 /* RX, allocate urb and initialize */
2318 /* prepare our RX buffer */
2319 serial->rx_data_length = rx_size;
2320 for (i = 0; i < serial->num_rx_urbs; i++) {
2321 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2322 if (!serial->rx_urb[i]) {
2323 dev_err(dev, "Could not allocate urb?\n");
2324 goto exit;
2326 serial->rx_urb[i]->transfer_buffer = NULL;
2327 serial->rx_urb[i]->transfer_buffer_length = 0;
2328 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2329 GFP_KERNEL);
2330 if (!serial->rx_data[i]) {
2331 dev_err(dev, "%s - Out of memory\n", __func__);
2332 goto exit;
2336 /* TX, allocate urb and initialize */
2337 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2338 if (!serial->tx_urb) {
2339 dev_err(dev, "Could not allocate urb?\n");
2340 goto exit;
2342 serial->tx_urb->transfer_buffer = NULL;
2343 serial->tx_urb->transfer_buffer_length = 0;
2344 /* prepare our TX buffer */
2345 serial->tx_data_count = 0;
2346 serial->tx_buffer_count = 0;
2347 serial->tx_data_length = tx_size;
2348 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2349 if (!serial->tx_data) {
2350 dev_err(dev, "%s - Out of memory", __func__);
2351 goto exit;
2353 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2354 if (!serial->tx_buffer) {
2355 dev_err(dev, "%s - Out of memory", __func__);
2356 goto exit;
2359 return 0;
2360 exit:
2361 hso_serial_common_free(serial);
2362 return -1;
2365 /* Frees a general hso device */
2366 static void hso_free_device(struct hso_device *hso_dev)
2368 kfree(hso_dev);
2371 /* Creates a general hso device */
2372 static struct hso_device *hso_create_device(struct usb_interface *intf,
2373 int port_spec)
2375 struct hso_device *hso_dev;
2377 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2378 if (!hso_dev)
2379 return NULL;
2381 hso_dev->port_spec = port_spec;
2382 hso_dev->usb = interface_to_usbdev(intf);
2383 hso_dev->interface = intf;
2384 kref_init(&hso_dev->ref);
2385 mutex_init(&hso_dev->mutex);
2387 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2388 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2390 return hso_dev;
2393 /* Removes a network device in the network device table */
2394 static int remove_net_device(struct hso_device *hso_dev)
2396 int i;
2398 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2399 if (network_table[i] == hso_dev) {
2400 network_table[i] = NULL;
2401 break;
2404 if (i == HSO_MAX_NET_DEVICES)
2405 return -1;
2406 return 0;
2409 /* Frees our network device */
2410 static void hso_free_net_device(struct hso_device *hso_dev)
2412 int i;
2413 struct hso_net *hso_net = dev2net(hso_dev);
2415 if (!hso_net)
2416 return;
2418 /* start freeing */
2419 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2420 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2421 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2423 usb_free_urb(hso_net->mux_bulk_tx_urb);
2424 kfree(hso_net->mux_bulk_tx_buf);
2426 remove_net_device(hso_net->parent);
2428 if (hso_net->net) {
2429 unregister_netdev(hso_net->net);
2430 free_netdev(hso_net->net);
2433 hso_free_device(hso_dev);
2436 /* initialize the network interface */
2437 static void hso_net_init(struct net_device *net)
2439 struct hso_net *hso_net = netdev_priv(net);
2441 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2443 /* fill in the other fields */
2444 net->open = hso_net_open;
2445 net->stop = hso_net_close;
2446 net->hard_start_xmit = hso_net_start_xmit;
2447 net->tx_timeout = hso_net_tx_timeout;
2448 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2449 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2450 net->type = ARPHRD_NONE;
2451 net->mtu = DEFAULT_MTU - 14;
2452 net->tx_queue_len = 10;
2453 SET_ETHTOOL_OPS(net, &ops);
2455 /* and initialize the semaphore */
2456 spin_lock_init(&hso_net->net_lock);
2459 /* Adds a network device in the network device table */
2460 static int add_net_device(struct hso_device *hso_dev)
2462 int i;
2464 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2465 if (network_table[i] == NULL) {
2466 network_table[i] = hso_dev;
2467 break;
2470 if (i == HSO_MAX_NET_DEVICES)
2471 return -1;
2472 return 0;
2475 static int hso_radio_toggle(void *data, enum rfkill_state state)
2477 struct hso_device *hso_dev = data;
2478 int enabled = (state == RFKILL_STATE_ON);
2479 int rv;
2481 mutex_lock(&hso_dev->mutex);
2482 if (hso_dev->usb_gone)
2483 rv = 0;
2484 else
2485 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2486 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2487 USB_CTRL_SET_TIMEOUT);
2488 mutex_unlock(&hso_dev->mutex);
2489 return rv;
2492 /* Creates and sets up everything for rfkill */
2493 static void hso_create_rfkill(struct hso_device *hso_dev,
2494 struct usb_interface *interface)
2496 struct hso_net *hso_net = dev2net(hso_dev);
2497 struct device *dev = &hso_net->net->dev;
2498 char *rfkn;
2500 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2501 RFKILL_TYPE_WWAN);
2502 if (!hso_net->rfkill) {
2503 dev_err(dev, "%s - Out of memory\n", __func__);
2504 return;
2506 rfkn = kzalloc(20, GFP_KERNEL);
2507 if (!rfkn) {
2508 rfkill_free(hso_net->rfkill);
2509 hso_net->rfkill = NULL;
2510 dev_err(dev, "%s - Out of memory\n", __func__);
2511 return;
2513 snprintf(rfkn, 20, "hso-%d",
2514 interface->altsetting->desc.bInterfaceNumber);
2515 hso_net->rfkill->name = rfkn;
2516 hso_net->rfkill->state = RFKILL_STATE_ON;
2517 hso_net->rfkill->data = hso_dev;
2518 hso_net->rfkill->toggle_radio = hso_radio_toggle;
2519 if (rfkill_register(hso_net->rfkill) < 0) {
2520 kfree(rfkn);
2521 hso_net->rfkill->name = NULL;
2522 rfkill_free(hso_net->rfkill);
2523 hso_net->rfkill = NULL;
2524 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2525 return;
2529 /* Creates our network device */
2530 static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2532 int result, i;
2533 struct net_device *net;
2534 struct hso_net *hso_net;
2535 struct hso_device *hso_dev;
2537 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2538 if (!hso_dev)
2539 return NULL;
2541 /* allocate our network device, then we can put in our private data */
2542 /* call hso_net_init to do the basic initialization */
2543 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2544 if (!net) {
2545 dev_err(&interface->dev, "Unable to create ethernet device\n");
2546 goto exit;
2549 hso_net = netdev_priv(net);
2551 hso_dev->port_data.dev_net = hso_net;
2552 hso_net->net = net;
2553 hso_net->parent = hso_dev;
2555 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2556 USB_DIR_IN);
2557 if (!hso_net->in_endp) {
2558 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2559 goto exit;
2561 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2562 USB_DIR_OUT);
2563 if (!hso_net->out_endp) {
2564 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2565 goto exit;
2567 SET_NETDEV_DEV(net, &interface->dev);
2569 /* registering our net device */
2570 result = register_netdev(net);
2571 if (result) {
2572 dev_err(&interface->dev, "Failed to register device\n");
2573 goto exit;
2576 /* start allocating */
2577 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2578 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2579 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2580 dev_err(&interface->dev, "Could not allocate rx urb\n");
2581 goto exit;
2583 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2584 GFP_KERNEL);
2585 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2586 dev_err(&interface->dev, "Could not allocate rx buf\n");
2587 goto exit;
2590 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2591 if (!hso_net->mux_bulk_tx_urb) {
2592 dev_err(&interface->dev, "Could not allocate tx urb\n");
2593 goto exit;
2595 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2596 if (!hso_net->mux_bulk_tx_buf) {
2597 dev_err(&interface->dev, "Could not allocate tx buf\n");
2598 goto exit;
2601 add_net_device(hso_dev);
2603 hso_log_port(hso_dev);
2605 hso_create_rfkill(hso_dev, interface);
2607 return hso_dev;
2608 exit:
2609 hso_free_net_device(hso_dev);
2610 return NULL;
2613 static void hso_free_tiomget(struct hso_serial *serial)
2615 struct hso_tiocmget *tiocmget = serial->tiocmget;
2616 if (tiocmget) {
2617 kfree(tiocmget);
2618 if (tiocmget->urb) {
2619 usb_free_urb(tiocmget->urb);
2620 tiocmget->urb = NULL;
2622 serial->tiocmget = NULL;
2627 /* Frees an AT channel ( goes for both mux and non-mux ) */
2628 static void hso_free_serial_device(struct hso_device *hso_dev)
2630 struct hso_serial *serial = dev2ser(hso_dev);
2632 if (!serial)
2633 return;
2634 set_serial_by_index(serial->minor, NULL);
2636 hso_serial_common_free(serial);
2638 if (serial->shared_int) {
2639 mutex_lock(&serial->shared_int->shared_int_lock);
2640 if (--serial->shared_int->ref_count == 0)
2641 hso_free_shared_int(serial->shared_int);
2642 else
2643 mutex_unlock(&serial->shared_int->shared_int_lock);
2645 hso_free_tiomget(serial);
2646 kfree(serial);
2647 hso_free_device(hso_dev);
2650 /* Creates a bulk AT channel */
2651 static struct hso_device *hso_create_bulk_serial_device(
2652 struct usb_interface *interface, int port)
2654 struct hso_device *hso_dev;
2655 struct hso_serial *serial;
2656 int num_urbs;
2657 struct hso_tiocmget *tiocmget;
2659 hso_dev = hso_create_device(interface, port);
2660 if (!hso_dev)
2661 return NULL;
2663 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2664 if (!serial)
2665 goto exit;
2667 serial->parent = hso_dev;
2668 hso_dev->port_data.dev_serial = serial;
2670 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
2671 num_urbs = 2;
2672 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2673 GFP_KERNEL);
2674 /* it isn't going to break our heart if serial->tiocmget
2675 * allocation fails don't bother checking this.
2677 if (serial->tiocmget) {
2678 tiocmget = serial->tiocmget;
2679 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2680 if (tiocmget->urb) {
2681 mutex_init(&tiocmget->mutex);
2682 init_waitqueue_head(&tiocmget->waitq);
2683 tiocmget->endp = hso_get_ep(
2684 interface,
2685 USB_ENDPOINT_XFER_INT,
2686 USB_DIR_IN);
2687 } else
2688 hso_free_tiomget(serial);
2691 else
2692 num_urbs = 1;
2694 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2695 BULK_URB_TX_SIZE))
2696 goto exit;
2698 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2699 USB_DIR_IN);
2700 if (!serial->in_endp) {
2701 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2702 goto exit2;
2705 if (!
2706 (serial->out_endp =
2707 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2708 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2709 goto exit2;
2712 serial->write_data = hso_std_serial_write_data;
2714 /* and record this serial */
2715 set_serial_by_index(serial->minor, serial);
2717 /* setup the proc dirs and files if needed */
2718 hso_log_port(hso_dev);
2720 /* done, return it */
2721 return hso_dev;
2723 exit2:
2724 hso_serial_common_free(serial);
2725 exit:
2726 hso_free_tiomget(serial);
2727 kfree(serial);
2728 hso_free_device(hso_dev);
2729 return NULL;
2732 /* Creates a multiplexed AT channel */
2733 static
2734 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2735 int port,
2736 struct hso_shared_int *mux)
2738 struct hso_device *hso_dev;
2739 struct hso_serial *serial;
2740 int port_spec;
2742 port_spec = HSO_INTF_MUX;
2743 port_spec &= ~HSO_PORT_MASK;
2745 port_spec |= hso_mux_to_port(port);
2746 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2747 return NULL;
2749 hso_dev = hso_create_device(interface, port_spec);
2750 if (!hso_dev)
2751 return NULL;
2753 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2754 if (!serial)
2755 goto exit;
2757 hso_dev->port_data.dev_serial = serial;
2758 serial->parent = hso_dev;
2760 if (hso_serial_common_create
2761 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2762 goto exit;
2764 serial->tx_data_length--;
2765 serial->write_data = hso_mux_serial_write_data;
2767 serial->shared_int = mux;
2768 mutex_lock(&serial->shared_int->shared_int_lock);
2769 serial->shared_int->ref_count++;
2770 mutex_unlock(&serial->shared_int->shared_int_lock);
2772 /* and record this serial */
2773 set_serial_by_index(serial->minor, serial);
2775 /* setup the proc dirs and files if needed */
2776 hso_log_port(hso_dev);
2778 /* done, return it */
2779 return hso_dev;
2781 exit:
2782 if (serial) {
2783 tty_unregister_device(tty_drv, serial->minor);
2784 kfree(serial);
2786 if (hso_dev)
2787 hso_free_device(hso_dev);
2788 return NULL;
2792 static void hso_free_shared_int(struct hso_shared_int *mux)
2794 usb_free_urb(mux->shared_intr_urb);
2795 kfree(mux->shared_intr_buf);
2796 mutex_unlock(&mux->shared_int_lock);
2797 kfree(mux);
2800 static
2801 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2803 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2805 if (!mux)
2806 return NULL;
2808 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2809 USB_DIR_IN);
2810 if (!mux->intr_endp) {
2811 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2812 goto exit;
2815 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2816 if (!mux->shared_intr_urb) {
2817 dev_err(&interface->dev, "Could not allocate intr urb?");
2818 goto exit;
2820 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2821 GFP_KERNEL);
2822 if (!mux->shared_intr_buf) {
2823 dev_err(&interface->dev, "Could not allocate intr buf?");
2824 goto exit;
2827 mutex_init(&mux->shared_int_lock);
2829 return mux;
2831 exit:
2832 kfree(mux->shared_intr_buf);
2833 usb_free_urb(mux->shared_intr_urb);
2834 kfree(mux);
2835 return NULL;
2838 /* Gets the port spec for a certain interface */
2839 static int hso_get_config_data(struct usb_interface *interface)
2841 struct usb_device *usbdev = interface_to_usbdev(interface);
2842 u8 config_data[17];
2843 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2844 s32 result;
2846 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2847 0x86, 0xC0, 0, 0, config_data, 17,
2848 USB_CTRL_SET_TIMEOUT) != 0x11) {
2849 return -EIO;
2852 switch (config_data[if_num]) {
2853 case 0x0:
2854 result = 0;
2855 break;
2856 case 0x1:
2857 result = HSO_PORT_DIAG;
2858 break;
2859 case 0x2:
2860 result = HSO_PORT_GPS;
2861 break;
2862 case 0x3:
2863 result = HSO_PORT_GPS_CONTROL;
2864 break;
2865 case 0x4:
2866 result = HSO_PORT_APP;
2867 break;
2868 case 0x5:
2869 result = HSO_PORT_APP2;
2870 break;
2871 case 0x6:
2872 result = HSO_PORT_CONTROL;
2873 break;
2874 case 0x7:
2875 result = HSO_PORT_NETWORK;
2876 break;
2877 case 0x8:
2878 result = HSO_PORT_MODEM;
2879 break;
2880 case 0x9:
2881 result = HSO_PORT_MSD;
2882 break;
2883 case 0xa:
2884 result = HSO_PORT_PCSC;
2885 break;
2886 case 0xb:
2887 result = HSO_PORT_VOICE;
2888 break;
2889 default:
2890 result = 0;
2893 if (result)
2894 result |= HSO_INTF_BULK;
2896 if (config_data[16] & 0x1)
2897 result |= HSO_INFO_CRC_BUG;
2899 return result;
2902 /* called once for each interface upon device insertion */
2903 static int hso_probe(struct usb_interface *interface,
2904 const struct usb_device_id *id)
2906 int mux, i, if_num, port_spec;
2907 unsigned char port_mask;
2908 struct hso_device *hso_dev = NULL;
2909 struct hso_shared_int *shared_int;
2910 struct hso_device *tmp_dev = NULL;
2912 if_num = interface->altsetting->desc.bInterfaceNumber;
2914 /* Get the interface/port specification from either driver_info or from
2915 * the device itself */
2916 if (id->driver_info)
2917 port_spec = ((u32 *)(id->driver_info))[if_num];
2918 else
2919 port_spec = hso_get_config_data(interface);
2921 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2922 dev_err(&interface->dev, "Not our interface\n");
2923 return -ENODEV;
2925 /* Check if we need to switch to alt interfaces prior to port
2926 * configuration */
2927 if (interface->num_altsetting > 1)
2928 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2929 interface->needs_remote_wakeup = 1;
2931 /* Allocate new hso device(s) */
2932 switch (port_spec & HSO_INTF_MASK) {
2933 case HSO_INTF_MUX:
2934 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2935 /* Create the network device */
2936 if (!disable_net) {
2937 hso_dev = hso_create_net_device(interface);
2938 if (!hso_dev)
2939 goto exit;
2940 tmp_dev = hso_dev;
2944 if (hso_get_mux_ports(interface, &port_mask))
2945 /* TODO: de-allocate everything */
2946 goto exit;
2948 shared_int = hso_create_shared_int(interface);
2949 if (!shared_int)
2950 goto exit;
2952 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2953 if (port_mask & i) {
2954 hso_dev = hso_create_mux_serial_device(
2955 interface, i, shared_int);
2956 if (!hso_dev)
2957 goto exit;
2961 if (tmp_dev)
2962 hso_dev = tmp_dev;
2963 break;
2965 case HSO_INTF_BULK:
2966 /* It's a regular bulk interface */
2967 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2968 && !disable_net)
2969 hso_dev = hso_create_net_device(interface);
2970 else
2971 hso_dev =
2972 hso_create_bulk_serial_device(interface, port_spec);
2973 if (!hso_dev)
2974 goto exit;
2975 break;
2976 default:
2977 goto exit;
2980 usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2982 /* save our data pointer in this device */
2983 usb_set_intfdata(interface, hso_dev);
2985 /* done */
2986 return 0;
2987 exit:
2988 hso_free_interface(interface);
2989 return -ENODEV;
2992 /* device removed, cleaning up */
2993 static void hso_disconnect(struct usb_interface *interface)
2995 hso_free_interface(interface);
2997 /* remove reference of our private data */
2998 usb_set_intfdata(interface, NULL);
3000 usb_driver_release_interface(&hso_driver, interface);
3003 static void async_get_intf(struct work_struct *data)
3005 struct hso_device *hso_dev =
3006 container_of(data, struct hso_device, async_get_intf);
3007 usb_autopm_get_interface(hso_dev->interface);
3010 static void async_put_intf(struct work_struct *data)
3012 struct hso_device *hso_dev =
3013 container_of(data, struct hso_device, async_put_intf);
3014 usb_autopm_put_interface(hso_dev->interface);
3017 static int hso_get_activity(struct hso_device *hso_dev)
3019 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3020 if (!hso_dev->is_active) {
3021 hso_dev->is_active = 1;
3022 schedule_work(&hso_dev->async_get_intf);
3026 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3027 return -EAGAIN;
3029 usb_mark_last_busy(hso_dev->usb);
3031 return 0;
3034 static int hso_put_activity(struct hso_device *hso_dev)
3036 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3037 if (hso_dev->is_active) {
3038 hso_dev->is_active = 0;
3039 schedule_work(&hso_dev->async_put_intf);
3040 return -EAGAIN;
3043 hso_dev->is_active = 0;
3044 return 0;
3047 /* called by kernel when we need to suspend device */
3048 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3050 int i, result;
3052 /* Stop all serial ports */
3053 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3054 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3055 result = hso_stop_serial_device(serial_table[i]);
3056 if (result)
3057 goto out;
3061 /* Stop all network ports */
3062 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3063 if (network_table[i] &&
3064 (network_table[i]->interface == iface)) {
3065 result = hso_stop_net_device(network_table[i]);
3066 if (result)
3067 goto out;
3071 out:
3072 return 0;
3075 /* called by kernel when we need to resume device */
3076 static int hso_resume(struct usb_interface *iface)
3078 int i, result = 0;
3079 struct hso_net *hso_net;
3081 /* Start all serial ports */
3082 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3083 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3084 if (dev2ser(serial_table[i])->open_count) {
3085 result =
3086 hso_start_serial_device(serial_table[i], GFP_NOIO);
3087 hso_kick_transmit(dev2ser(serial_table[i]));
3088 if (result)
3089 goto out;
3094 /* Start all network ports */
3095 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3096 if (network_table[i] &&
3097 (network_table[i]->interface == iface)) {
3098 hso_net = dev2net(network_table[i]);
3099 if (hso_net->flags & IFF_UP) {
3100 /* First transmit any lingering data,
3101 then restart the device. */
3102 if (hso_net->skb_tx_buf) {
3103 dev_dbg(&iface->dev,
3104 "Transmitting"
3105 " lingering data\n");
3106 hso_net_start_xmit(hso_net->skb_tx_buf,
3107 hso_net->net);
3108 hso_net->skb_tx_buf = NULL;
3110 result = hso_start_net_device(network_table[i]);
3111 if (result)
3112 goto out;
3117 out:
3118 return result;
3121 static void hso_serial_ref_free(struct kref *ref)
3123 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3125 hso_free_serial_device(hso_dev);
3128 static void hso_free_interface(struct usb_interface *interface)
3130 struct hso_serial *hso_dev;
3131 struct tty_struct *tty;
3132 int i;
3134 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3135 if (serial_table[i]
3136 && (serial_table[i]->interface == interface)) {
3137 hso_dev = dev2ser(serial_table[i]);
3138 spin_lock_irq(&hso_dev->serial_lock);
3139 tty = tty_kref_get(hso_dev->tty);
3140 spin_unlock_irq(&hso_dev->serial_lock);
3141 if (tty)
3142 tty_hangup(tty);
3143 mutex_lock(&hso_dev->parent->mutex);
3144 tty_kref_put(tty);
3145 hso_dev->parent->usb_gone = 1;
3146 mutex_unlock(&hso_dev->parent->mutex);
3147 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3151 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3152 if (network_table[i]
3153 && (network_table[i]->interface == interface)) {
3154 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3155 /* hso_stop_net_device doesn't stop the net queue since
3156 * traffic needs to start it again when suspended */
3157 netif_stop_queue(dev2net(network_table[i])->net);
3158 hso_stop_net_device(network_table[i]);
3159 cancel_work_sync(&network_table[i]->async_put_intf);
3160 cancel_work_sync(&network_table[i]->async_get_intf);
3161 if (rfk)
3162 rfkill_unregister(rfk);
3163 hso_free_net_device(network_table[i]);
3168 /* Helper functions */
3170 /* Get the endpoint ! */
3171 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3172 int type, int dir)
3174 int i;
3175 struct usb_host_interface *iface = intf->cur_altsetting;
3176 struct usb_endpoint_descriptor *endp;
3178 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3179 endp = &iface->endpoint[i].desc;
3180 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3181 (usb_endpoint_type(endp) == type))
3182 return endp;
3185 return NULL;
3188 /* Get the byte that describes which ports are enabled */
3189 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3191 int i;
3192 struct usb_host_interface *iface = intf->cur_altsetting;
3194 if (iface->extralen == 3) {
3195 *ports = iface->extra[2];
3196 return 0;
3199 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3200 if (iface->endpoint[i].extralen == 3) {
3201 *ports = iface->endpoint[i].extra[2];
3202 return 0;
3206 return -1;
3209 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3210 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3211 struct usb_device *usb, gfp_t gfp)
3213 int result;
3215 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3216 usb_rcvintpipe(usb,
3217 shared_int->intr_endp->bEndpointAddress & 0x7F),
3218 shared_int->shared_intr_buf,
3219 shared_int->intr_endp->wMaxPacketSize,
3220 intr_callback, shared_int,
3221 shared_int->intr_endp->bInterval);
3223 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3224 if (result)
3225 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
3226 result);
3228 return result;
3231 /* operations setup of the serial interface */
3232 static const struct tty_operations hso_serial_ops = {
3233 .open = hso_serial_open,
3234 .close = hso_serial_close,
3235 .write = hso_serial_write,
3236 .write_room = hso_serial_write_room,
3237 .ioctl = hso_serial_ioctl,
3238 .set_termios = hso_serial_set_termios,
3239 .chars_in_buffer = hso_serial_chars_in_buffer,
3240 .tiocmget = hso_serial_tiocmget,
3241 .tiocmset = hso_serial_tiocmset,
3242 .unthrottle = hso_unthrottle
3245 static struct usb_driver hso_driver = {
3246 .name = driver_name,
3247 .probe = hso_probe,
3248 .disconnect = hso_disconnect,
3249 .id_table = hso_ids,
3250 .suspend = hso_suspend,
3251 .resume = hso_resume,
3252 .reset_resume = hso_resume,
3253 .supports_autosuspend = 1,
3256 static int __init hso_init(void)
3258 int i;
3259 int result;
3261 /* put it in the log */
3262 printk(KERN_INFO "hso: %s\n", version);
3264 /* Initialise the serial table semaphore and table */
3265 spin_lock_init(&serial_table_lock);
3266 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3267 serial_table[i] = NULL;
3269 /* allocate our driver using the proper amount of supported minors */
3270 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3271 if (!tty_drv)
3272 return -ENOMEM;
3274 /* fill in all needed values */
3275 tty_drv->magic = TTY_DRIVER_MAGIC;
3276 tty_drv->owner = THIS_MODULE;
3277 tty_drv->driver_name = driver_name;
3278 tty_drv->name = tty_filename;
3280 /* if major number is provided as parameter, use that one */
3281 if (tty_major)
3282 tty_drv->major = tty_major;
3284 tty_drv->minor_start = 0;
3285 tty_drv->num = HSO_SERIAL_TTY_MINORS;
3286 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3287 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3288 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3289 tty_drv->init_termios = tty_std_termios;
3290 hso_init_termios(&tty_drv->init_termios);
3291 tty_set_operations(tty_drv, &hso_serial_ops);
3293 /* register the tty driver */
3294 result = tty_register_driver(tty_drv);
3295 if (result) {
3296 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3297 __func__, result);
3298 return result;
3301 /* register this module as an usb driver */
3302 result = usb_register(&hso_driver);
3303 if (result) {
3304 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3305 result);
3306 /* cleanup serial interface */
3307 tty_unregister_driver(tty_drv);
3308 return result;
3311 /* done */
3312 return 0;
3315 static void __exit hso_exit(void)
3317 printk(KERN_INFO "hso: unloaded\n");
3319 tty_unregister_driver(tty_drv);
3320 /* deregister the usb driver */
3321 usb_deregister(&hso_driver);
3324 /* Module definitions */
3325 module_init(hso_init);
3326 module_exit(hso_exit);
3328 MODULE_AUTHOR(MOD_AUTHOR);
3329 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3330 MODULE_LICENSE(MOD_LICENSE);
3331 MODULE_INFO(Version, DRIVER_VERSION);
3333 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3334 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3335 module_param(debug, int, S_IRUGO | S_IWUSR);
3337 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3338 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3339 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3341 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3342 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3343 module_param(disable_net, int, S_IRUGO | S_IWUSR);