[brcm63xx] some more fixes to the SPI controller driver
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.26 / 915-hso-backport.patch
1 From: Greg Kroah-Hartman <gregkh@suse.de>
2 Date: Wed, 14 May 2008 04:57:12 +0000 (-0700)
3 Subject: HSO: add option hso driver
4 X-Git-Url: http://git.kernel.org/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git;a=commitdiff_plain;h=72dc1c096c7051a48ab1dbb12f71976656b55eb5;hp=44f74c046961bd1f21b7d35baaa9165572d1e975
5
6 HSO: add option hso driver
7
8 This driver is for a number of different Option devices. Originally
9 written by Option and Andrew Bird, but cleaned up massivly for
10 acceptance into mainline by me and others.
11
12 Many thanks to the following for their help in cleaning up the driver by
13 providing feedback and patches to it:
14 - Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
15 - Oliver Neukum <oliver@neukum.org>
16 - Alan Cox <alan@lxorguk.ukuu.org.uk>
17 - Javier Marcet <javier@krausbeck.org>
18
19 Cc: Andrew Bird <ajb@spheresystems.co.uk>
20 Cc: Javier Marcet <javier@krausbeck.org>
21 Cc: Filip Aben <f.aben@option.com>
22 Cc: Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
23 Cc: Oliver Neukum <oliver@neukum.org>
24 Acked-by: Alan Cox <alan@lxorguk.ukuu.org.uk>
25 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
26 Signed-off-by: Jeff Garzik <jgarzik@redhat.com>
27 ---
28
29 --- a/drivers/net/Makefile
30 +++ b/drivers/net/Makefile
31 @@ -237,6 +237,7 @@ obj-$(CONFIG_USB_CATC) += usb/
32 obj-$(CONFIG_USB_KAWETH) += usb/
33 obj-$(CONFIG_USB_PEGASUS) += usb/
34 obj-$(CONFIG_USB_RTL8150) += usb/
35 +obj-$(CONFIG_USB_HSO) += usb/
36 obj-$(CONFIG_USB_USBNET) += usb/
37 obj-$(CONFIG_USB_ZD1201) += usb/
38
39 --- a/drivers/net/usb/Kconfig
40 +++ b/drivers/net/usb/Kconfig
41 @@ -154,6 +154,16 @@ config USB_NET_AX8817X
42 This driver creates an interface named "ethX", where X depends on
43 what other networking devices you have in use.
44
45 +config USB_HSO
46 + tristate "Option USB High Speed Mobile Devices"
47 + depends on USB && RFKILL
48 + default n
49 + help
50 + Choose this option if you have an Option HSDPA/HSUPA card.
51 + These cards support downlink speeds of 7.2Mbps or greater.
52 +
53 + To compile this driver as a module, choose M here: the
54 + module will be called hso.
55
56 config USB_NET_CDCETHER
57 tristate "CDC Ethernet support (smart devices such as cable modems)"
58 --- a/drivers/net/usb/Makefile
59 +++ b/drivers/net/usb/Makefile
60 @@ -6,6 +6,7 @@ obj-$(CONFIG_USB_CATC) += catc.o
61 obj-$(CONFIG_USB_KAWETH) += kaweth.o
62 obj-$(CONFIG_USB_PEGASUS) += pegasus.o
63 obj-$(CONFIG_USB_RTL8150) += rtl8150.o
64 +obj-$(CONFIG_USB_HSO) += hso.o
65 obj-$(CONFIG_USB_NET_AX8817X) += asix.o
66 obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o
67 obj-$(CONFIG_USB_NET_DM9601) += dm9601.o
68 --- /dev/null
69 +++ b/drivers/net/usb/hso.c
70 @@ -0,0 +1,2836 @@
71 +/******************************************************************************
72 + *
73 + * Driver for Option High Speed Mobile Devices.
74 + *
75 + * Copyright (C) 2008 Option International
76 + * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
77 + * <ajb@spheresystems.co.uk>
78 + * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
79 + * Copyright (C) 2008 Novell, Inc.
80 + *
81 + * This program is free software; you can redistribute it and/or modify
82 + * it under the terms of the GNU General Public License version 2 as
83 + * published by the Free Software Foundation.
84 + *
85 + * This program is distributed in the hope that it will be useful,
86 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
87 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
88 + * GNU General Public License for more details.
89 + *
90 + * You should have received a copy of the GNU General Public License
91 + * along with this program; if not, write to the Free Software
92 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
93 + * USA
94 + *
95 + *
96 + *****************************************************************************/
97 +
98 +/******************************************************************************
99 + *
100 + * Description of the device:
101 + *
102 + * Interface 0: Contains the IP network interface on the bulk end points.
103 + * The multiplexed serial ports are using the interrupt and
104 + * control endpoints.
105 + * Interrupt contains a bitmap telling which multiplexed
106 + * serialport needs servicing.
107 + *
108 + * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
109 + * port is opened, as this have a huge impact on the network port
110 + * throughput.
111 + *
112 + * Interface 2: Standard modem interface - circuit switched interface, should
113 + * not be used.
114 + *
115 + *****************************************************************************/
116 +
117 +#include <linux/sched.h>
118 +#include <linux/slab.h>
119 +#include <linux/init.h>
120 +#include <linux/delay.h>
121 +#include <linux/netdevice.h>
122 +#include <linux/module.h>
123 +#include <linux/ethtool.h>
124 +#include <linux/usb.h>
125 +#include <linux/timer.h>
126 +#include <linux/tty.h>
127 +#include <linux/tty_driver.h>
128 +#include <linux/tty_flip.h>
129 +#include <linux/kmod.h>
130 +#include <linux/rfkill.h>
131 +#include <linux/ip.h>
132 +#include <linux/uaccess.h>
133 +#include <linux/usb/cdc.h>
134 +#include <net/arp.h>
135 +#include <asm/byteorder.h>
136 +
137 +
138 +#define DRIVER_VERSION "1.2"
139 +#define MOD_AUTHOR "Option Wireless"
140 +#define MOD_DESCRIPTION "USB High Speed Option driver"
141 +#define MOD_LICENSE "GPL"
142 +
143 +#define HSO_MAX_NET_DEVICES 10
144 +#define HSO__MAX_MTU 2048
145 +#define DEFAULT_MTU 1500
146 +#define DEFAULT_MRU 1500
147 +
148 +#define CTRL_URB_RX_SIZE 1024
149 +#define CTRL_URB_TX_SIZE 64
150 +
151 +#define BULK_URB_RX_SIZE 4096
152 +#define BULK_URB_TX_SIZE 8192
153 +
154 +#define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
155 +#define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
156 +#define MUX_BULK_RX_BUF_COUNT 4
157 +#define USB_TYPE_OPTION_VENDOR 0x20
158 +
159 +/* These definitions are used with the struct hso_net flags element */
160 +/* - use *_bit operations on it. (bit indices not values.) */
161 +#define HSO_NET_RUNNING 0
162 +
163 +#define HSO_NET_TX_TIMEOUT (HZ*10)
164 +
165 +/* Serial port defines and structs. */
166 +#define HSO_SERIAL_FLAG_RX_SENT 0
167 +
168 +#define HSO_SERIAL_MAGIC 0x48534f31
169 +
170 +/* Number of ttys to handle */
171 +#define HSO_SERIAL_TTY_MINORS 256
172 +
173 +#define MAX_RX_URBS 2
174 +
175 +#define get_serial_by_tty(x) \
176 + (x ? (struct hso_serial *)x->driver_data : NULL)
177 +
178 +/*****************************************************************************/
179 +/* Debugging functions */
180 +/*****************************************************************************/
181 +#define D__(lvl_, fmt, arg...) \
182 + do { \
183 + printk(lvl_ "[%d:%s]: " fmt "\n", \
184 + __LINE__, __func__, ## arg); \
185 + } while (0)
186 +
187 +#define D_(lvl, args...) \
188 + do { \
189 + if (lvl & debug) \
190 + D__(KERN_INFO, args); \
191 + } while (0)
192 +
193 +#define D1(args...) D_(0x01, ##args)
194 +#define D2(args...) D_(0x02, ##args)
195 +#define D3(args...) D_(0x04, ##args)
196 +#define D4(args...) D_(0x08, ##args)
197 +#define D5(args...) D_(0x10, ##args)
198 +
199 +/*****************************************************************************/
200 +/* Enumerators */
201 +/*****************************************************************************/
202 +enum pkt_parse_state {
203 + WAIT_IP,
204 + WAIT_DATA,
205 + WAIT_SYNC
206 +};
207 +
208 +/*****************************************************************************/
209 +/* Structs */
210 +/*****************************************************************************/
211 +
212 +struct hso_shared_int {
213 + struct usb_endpoint_descriptor *intr_endp;
214 + void *shared_intr_buf;
215 + struct urb *shared_intr_urb;
216 + struct usb_device *usb;
217 + int use_count;
218 + int ref_count;
219 + struct mutex shared_int_lock;
220 +};
221 +
222 +struct hso_net {
223 + struct hso_device *parent;
224 + struct net_device *net;
225 + struct rfkill *rfkill;
226 +
227 + struct usb_endpoint_descriptor *in_endp;
228 + struct usb_endpoint_descriptor *out_endp;
229 +
230 + struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
231 + struct urb *mux_bulk_tx_urb;
232 + void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
233 + void *mux_bulk_tx_buf;
234 +
235 + struct sk_buff *skb_rx_buf;
236 + struct sk_buff *skb_tx_buf;
237 +
238 + enum pkt_parse_state rx_parse_state;
239 + spinlock_t net_lock;
240 +
241 + unsigned short rx_buf_size;
242 + unsigned short rx_buf_missing;
243 + struct iphdr rx_ip_hdr;
244 +
245 + unsigned long flags;
246 +};
247 +
248 +struct hso_serial {
249 + struct hso_device *parent;
250 + int magic;
251 + u8 minor;
252 +
253 + struct hso_shared_int *shared_int;
254 +
255 + /* rx/tx urb could be either a bulk urb or a control urb depending
256 + on which serial port it is used on. */
257 + struct urb *rx_urb[MAX_RX_URBS];
258 + u8 num_rx_urbs;
259 + u8 *rx_data[MAX_RX_URBS];
260 + u16 rx_data_length; /* should contain allocated length */
261 +
262 + struct urb *tx_urb;
263 + u8 *tx_data;
264 + u8 *tx_buffer;
265 + u16 tx_data_length; /* should contain allocated length */
266 + u16 tx_data_count;
267 + u16 tx_buffer_count;
268 + struct usb_ctrlrequest ctrl_req_tx;
269 + struct usb_ctrlrequest ctrl_req_rx;
270 +
271 + struct usb_endpoint_descriptor *in_endp;
272 + struct usb_endpoint_descriptor *out_endp;
273 +
274 + unsigned long flags;
275 + u8 rts_state;
276 + u8 dtr_state;
277 + unsigned tx_urb_used:1;
278 +
279 + /* from usb_serial_port */
280 + struct tty_struct *tty;
281 + int open_count;
282 + spinlock_t serial_lock;
283 +
284 + int (*write_data) (struct hso_serial *serial);
285 +};
286 +
287 +struct hso_device {
288 + union {
289 + struct hso_serial *dev_serial;
290 + struct hso_net *dev_net;
291 + } port_data;
292 +
293 + u32 port_spec;
294 +
295 + u8 is_active;
296 + u8 usb_gone;
297 + struct work_struct async_get_intf;
298 + struct work_struct async_put_intf;
299 +
300 + struct usb_device *usb;
301 + struct usb_interface *interface;
302 +
303 + struct device *dev;
304 + struct kref ref;
305 + struct mutex mutex;
306 +};
307 +
308 +/* Type of interface */
309 +#define HSO_INTF_MASK 0xFF00
310 +#define HSO_INTF_MUX 0x0100
311 +#define HSO_INTF_BULK 0x0200
312 +
313 +/* Type of port */
314 +#define HSO_PORT_MASK 0xFF
315 +#define HSO_PORT_NO_PORT 0x0
316 +#define HSO_PORT_CONTROL 0x1
317 +#define HSO_PORT_APP 0x2
318 +#define HSO_PORT_GPS 0x3
319 +#define HSO_PORT_PCSC 0x4
320 +#define HSO_PORT_APP2 0x5
321 +#define HSO_PORT_GPS_CONTROL 0x6
322 +#define HSO_PORT_MSD 0x7
323 +#define HSO_PORT_VOICE 0x8
324 +#define HSO_PORT_DIAG2 0x9
325 +#define HSO_PORT_DIAG 0x10
326 +#define HSO_PORT_MODEM 0x11
327 +#define HSO_PORT_NETWORK 0x12
328 +
329 +/* Additional device info */
330 +#define HSO_INFO_MASK 0xFF000000
331 +#define HSO_INFO_CRC_BUG 0x01000000
332 +
333 +/*****************************************************************************/
334 +/* Prototypes */
335 +/*****************************************************************************/
336 +/* Serial driver functions */
337 +static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
338 + unsigned int set, unsigned int clear);
339 +static void ctrl_callback(struct urb *urb);
340 +static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
341 +static void hso_kick_transmit(struct hso_serial *serial);
342 +/* Helper functions */
343 +static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
344 + struct usb_device *usb, gfp_t gfp);
345 +static void log_usb_status(int status, const char *function);
346 +static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
347 + int type, int dir);
348 +static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
349 +static void hso_free_interface(struct usb_interface *intf);
350 +static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
351 +static int hso_stop_serial_device(struct hso_device *hso_dev);
352 +static int hso_start_net_device(struct hso_device *hso_dev);
353 +static void hso_free_shared_int(struct hso_shared_int *shared_int);
354 +static int hso_stop_net_device(struct hso_device *hso_dev);
355 +static void hso_serial_ref_free(struct kref *ref);
356 +static void async_get_intf(struct work_struct *data);
357 +static void async_put_intf(struct work_struct *data);
358 +static int hso_put_activity(struct hso_device *hso_dev);
359 +static int hso_get_activity(struct hso_device *hso_dev);
360 +
361 +/*****************************************************************************/
362 +/* Helping functions */
363 +/*****************************************************************************/
364 +
365 +/* #define DEBUG */
366 +
367 +#define dev2net(x) (x->port_data.dev_net)
368 +#define dev2ser(x) (x->port_data.dev_serial)
369 +
370 +/* Debugging functions */
371 +#ifdef DEBUG
372 +static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
373 + unsigned int len)
374 +{
375 + u8 i = 0;
376 +
377 + printk(KERN_DEBUG "[%d:%s]: len %d", line_count, func_name, len);
378 +
379 + for (i = 0; i < len; i++) {
380 + if (!(i % 16))
381 + printk("\n 0x%03x: ", i);
382 + printk("%02x ", (unsigned char)buf[i]);
383 + }
384 + printk("\n");
385 +}
386 +
387 +#define DUMP(buf_, len_) \
388 + dbg_dump(__LINE__, __func__, buf_, len_)
389 +
390 +#define DUMP1(buf_, len_) \
391 + do { \
392 + if (0x01 & debug) \
393 + DUMP(buf_, len_); \
394 + } while (0)
395 +#else
396 +#define DUMP(buf_, len_)
397 +#define DUMP1(buf_, len_)
398 +#endif
399 +
400 +/* module parameters */
401 +static int debug;
402 +static int tty_major;
403 +static int disable_net;
404 +
405 +/* driver info */
406 +static const char driver_name[] = "hso";
407 +static const char tty_filename[] = "ttyHS";
408 +static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
409 +/* the usb driver itself (registered in hso_init) */
410 +static struct usb_driver hso_driver;
411 +/* serial structures */
412 +static struct tty_driver *tty_drv;
413 +static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
414 +static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
415 +static spinlock_t serial_table_lock;
416 +static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
417 +static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
418 +
419 +static const s32 default_port_spec[] = {
420 + HSO_INTF_MUX | HSO_PORT_NETWORK,
421 + HSO_INTF_BULK | HSO_PORT_DIAG,
422 + HSO_INTF_BULK | HSO_PORT_MODEM,
423 + 0
424 +};
425 +
426 +static const s32 icon321_port_spec[] = {
427 + HSO_INTF_MUX | HSO_PORT_NETWORK,
428 + HSO_INTF_BULK | HSO_PORT_DIAG2,
429 + HSO_INTF_BULK | HSO_PORT_MODEM,
430 + HSO_INTF_BULK | HSO_PORT_DIAG,
431 + 0
432 +};
433 +
434 +#define default_port_device(vendor, product) \
435 + USB_DEVICE(vendor, product), \
436 + .driver_info = (kernel_ulong_t)default_port_spec
437 +
438 +#define icon321_port_device(vendor, product) \
439 + USB_DEVICE(vendor, product), \
440 + .driver_info = (kernel_ulong_t)icon321_port_spec
441 +
442 +/* list of devices we support */
443 +static const struct usb_device_id hso_ids[] = {
444 + {default_port_device(0x0af0, 0x6711)},
445 + {default_port_device(0x0af0, 0x6731)},
446 + {default_port_device(0x0af0, 0x6751)},
447 + {default_port_device(0x0af0, 0x6771)},
448 + {default_port_device(0x0af0, 0x6791)},
449 + {default_port_device(0x0af0, 0x6811)},
450 + {default_port_device(0x0af0, 0x6911)},
451 + {default_port_device(0x0af0, 0x6951)},
452 + {default_port_device(0x0af0, 0x6971)},
453 + {default_port_device(0x0af0, 0x7011)},
454 + {default_port_device(0x0af0, 0x7031)},
455 + {default_port_device(0x0af0, 0x7051)},
456 + {default_port_device(0x0af0, 0x7071)},
457 + {default_port_device(0x0af0, 0x7111)},
458 + {default_port_device(0x0af0, 0x7211)},
459 + {default_port_device(0x0af0, 0x7251)},
460 + {default_port_device(0x0af0, 0x7271)},
461 + {default_port_device(0x0af0, 0x7311)},
462 + {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
463 + {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
464 + {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
465 + {default_port_device(0x0af0, 0xd033)}, /* Icon-322 */
466 + {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
467 + {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
468 + {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
469 + {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
470 + {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
471 + {}
472 +};
473 +MODULE_DEVICE_TABLE(usb, hso_ids);
474 +
475 +/* Sysfs attribute */
476 +static ssize_t hso_sysfs_show_porttype(struct device *dev,
477 + struct device_attribute *attr,
478 + char *buf)
479 +{
480 + struct hso_device *hso_dev = dev->driver_data;
481 + char *port_name;
482 +
483 + if (!hso_dev)
484 + return 0;
485 +
486 + switch (hso_dev->port_spec & HSO_PORT_MASK) {
487 + case HSO_PORT_CONTROL:
488 + port_name = "Control";
489 + break;
490 + case HSO_PORT_APP:
491 + port_name = "Application";
492 + break;
493 + case HSO_PORT_APP2:
494 + port_name = "Application2";
495 + break;
496 + case HSO_PORT_GPS:
497 + port_name = "GPS";
498 + break;
499 + case HSO_PORT_GPS_CONTROL:
500 + port_name = "GPS Control";
501 + break;
502 + case HSO_PORT_PCSC:
503 + port_name = "PCSC";
504 + break;
505 + case HSO_PORT_DIAG:
506 + port_name = "Diagnostic";
507 + break;
508 + case HSO_PORT_DIAG2:
509 + port_name = "Diagnostic2";
510 + break;
511 + case HSO_PORT_MODEM:
512 + port_name = "Modem";
513 + break;
514 + case HSO_PORT_NETWORK:
515 + port_name = "Network";
516 + break;
517 + default:
518 + port_name = "Unknown";
519 + break;
520 + }
521 +
522 + return sprintf(buf, "%s\n", port_name);
523 +}
524 +static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
525 +
526 +/* converts mux value to a port spec value */
527 +static u32 hso_mux_to_port(int mux)
528 +{
529 + u32 result;
530 +
531 + switch (mux) {
532 + case 0x1:
533 + result = HSO_PORT_CONTROL;
534 + break;
535 + case 0x2:
536 + result = HSO_PORT_APP;
537 + break;
538 + case 0x4:
539 + result = HSO_PORT_PCSC;
540 + break;
541 + case 0x8:
542 + result = HSO_PORT_GPS;
543 + break;
544 + case 0x10:
545 + result = HSO_PORT_APP2;
546 + break;
547 + default:
548 + result = HSO_PORT_NO_PORT;
549 + }
550 + return result;
551 +}
552 +
553 +/* converts port spec value to a mux value */
554 +static u32 hso_port_to_mux(int port)
555 +{
556 + u32 result;
557 +
558 + switch (port & HSO_PORT_MASK) {
559 + case HSO_PORT_CONTROL:
560 + result = 0x0;
561 + break;
562 + case HSO_PORT_APP:
563 + result = 0x1;
564 + break;
565 + case HSO_PORT_PCSC:
566 + result = 0x2;
567 + break;
568 + case HSO_PORT_GPS:
569 + result = 0x3;
570 + break;
571 + case HSO_PORT_APP2:
572 + result = 0x4;
573 + break;
574 + default:
575 + result = 0x0;
576 + }
577 + return result;
578 +}
579 +
580 +static struct hso_serial *get_serial_by_shared_int_and_type(
581 + struct hso_shared_int *shared_int,
582 + int mux)
583 +{
584 + int i, port;
585 +
586 + port = hso_mux_to_port(mux);
587 +
588 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
589 + if (serial_table[i]
590 + && (dev2ser(serial_table[i])->shared_int == shared_int)
591 + && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
592 + return dev2ser(serial_table[i]);
593 + }
594 + }
595 +
596 + return NULL;
597 +}
598 +
599 +static struct hso_serial *get_serial_by_index(unsigned index)
600 +{
601 + struct hso_serial *serial;
602 + unsigned long flags;
603 +
604 + if (!serial_table[index])
605 + return NULL;
606 + spin_lock_irqsave(&serial_table_lock, flags);
607 + serial = dev2ser(serial_table[index]);
608 + spin_unlock_irqrestore(&serial_table_lock, flags);
609 +
610 + return serial;
611 +}
612 +
613 +static int get_free_serial_index(void)
614 +{
615 + int index;
616 + unsigned long flags;
617 +
618 + spin_lock_irqsave(&serial_table_lock, flags);
619 + for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
620 + if (serial_table[index] == NULL) {
621 + spin_unlock_irqrestore(&serial_table_lock, flags);
622 + return index;
623 + }
624 + }
625 + spin_unlock_irqrestore(&serial_table_lock, flags);
626 +
627 + printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
628 + return -1;
629 +}
630 +
631 +static void set_serial_by_index(unsigned index, struct hso_serial *serial)
632 +{
633 + unsigned long flags;
634 + spin_lock_irqsave(&serial_table_lock, flags);
635 + if (serial)
636 + serial_table[index] = serial->parent;
637 + else
638 + serial_table[index] = NULL;
639 + spin_unlock_irqrestore(&serial_table_lock, flags);
640 +}
641 +
642 +/* log a meaningfull explanation of an USB status */
643 +static void log_usb_status(int status, const char *function)
644 +{
645 + char *explanation;
646 +
647 + switch (status) {
648 + case -ENODEV:
649 + explanation = "no device";
650 + break;
651 + case -ENOENT:
652 + explanation = "endpoint not enabled";
653 + break;
654 + case -EPIPE:
655 + explanation = "endpoint stalled";
656 + break;
657 + case -ENOSPC:
658 + explanation = "not enough bandwidth";
659 + break;
660 + case -ESHUTDOWN:
661 + explanation = "device disabled";
662 + break;
663 + case -EHOSTUNREACH:
664 + explanation = "device suspended";
665 + break;
666 + case -EINVAL:
667 + case -EAGAIN:
668 + case -EFBIG:
669 + case -EMSGSIZE:
670 + explanation = "internal error";
671 + break;
672 + default:
673 + explanation = "unknown status";
674 + break;
675 + }
676 + D1("%s: received USB status - %s (%d)", function, explanation, status);
677 +}
678 +
679 +/* Network interface functions */
680 +
681 +/* called when net interface is brought up by ifconfig */
682 +static int hso_net_open(struct net_device *net)
683 +{
684 + struct hso_net *odev = netdev_priv(net);
685 + unsigned long flags = 0;
686 +
687 + if (!odev) {
688 + dev_err(&net->dev, "No net device !\n");
689 + return -ENODEV;
690 + }
691 +
692 + odev->skb_tx_buf = NULL;
693 +
694 + /* setup environment */
695 + spin_lock_irqsave(&odev->net_lock, flags);
696 + odev->rx_parse_state = WAIT_IP;
697 + odev->rx_buf_size = 0;
698 + odev->rx_buf_missing = sizeof(struct iphdr);
699 + spin_unlock_irqrestore(&odev->net_lock, flags);
700 +
701 + hso_start_net_device(odev->parent);
702 +
703 + /* We are up and running. */
704 + set_bit(HSO_NET_RUNNING, &odev->flags);
705 +
706 + /* Tell the kernel we are ready to start receiving from it */
707 + netif_start_queue(net);
708 +
709 + return 0;
710 +}
711 +
712 +/* called when interface is brought down by ifconfig */
713 +static int hso_net_close(struct net_device *net)
714 +{
715 + struct hso_net *odev = netdev_priv(net);
716 +
717 + /* we don't need the queue anymore */
718 + netif_stop_queue(net);
719 + /* no longer running */
720 + clear_bit(HSO_NET_RUNNING, &odev->flags);
721 +
722 + hso_stop_net_device(odev->parent);
723 +
724 + /* done */
725 + return 0;
726 +}
727 +
728 +/* USB tells is xmit done, we should start the netqueue again */
729 +static void write_bulk_callback(struct urb *urb)
730 +{
731 + struct hso_net *odev = urb->context;
732 + int status = urb->status;
733 +
734 + /* Sanity check */
735 + if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
736 + dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
737 + return;
738 + }
739 +
740 + /* Do we still have a valid kernel network device? */
741 + if (!netif_device_present(odev->net)) {
742 + dev_err(&urb->dev->dev, "%s: net device not present\n",
743 + __func__);
744 + return;
745 + }
746 +
747 + /* log status, but don't act on it, we don't need to resubmit anything
748 + * anyhow */
749 + if (status)
750 + log_usb_status(status, __func__);
751 +
752 + hso_put_activity(odev->parent);
753 +
754 + /* Tell the network interface we are ready for another frame */
755 + netif_wake_queue(odev->net);
756 +}
757 +
758 +/* called by kernel when we need to transmit a packet */
759 +static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
760 +{
761 + struct hso_net *odev = netdev_priv(net);
762 + int result;
763 +
764 + /* Tell the kernel, "No more frames 'til we are done with this one." */
765 + netif_stop_queue(net);
766 + if (hso_get_activity(odev->parent) == -EAGAIN) {
767 + odev->skb_tx_buf = skb;
768 + return 0;
769 + }
770 +
771 + /* log if asked */
772 + DUMP1(skb->data, skb->len);
773 + /* Copy it from kernel memory to OUR memory */
774 + memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
775 + D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
776 +
777 + /* Fill in the URB for shipping it out. */
778 + usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
779 + odev->parent->usb,
780 + usb_sndbulkpipe(odev->parent->usb,
781 + odev->out_endp->
782 + bEndpointAddress & 0x7F),
783 + odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
784 + odev);
785 +
786 + /* Deal with the Zero Length packet problem, I hope */
787 + odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
788 +
789 + /* Send the URB on its merry way. */
790 + result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
791 + if (result) {
792 + dev_warn(&odev->parent->interface->dev,
793 + "failed mux_bulk_tx_urb %d", result);
794 + net->stats.tx_errors++;
795 + netif_start_queue(net);
796 + } else {
797 + net->stats.tx_packets++;
798 + net->stats.tx_bytes += skb->len;
799 + /* And tell the kernel when the last transmit started. */
800 + net->trans_start = jiffies;
801 + }
802 + dev_kfree_skb(skb);
803 + /* we're done */
804 + return result;
805 +}
806 +
807 +static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
808 +{
809 + struct hso_net *odev = netdev_priv(net);
810 +
811 + strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
812 + strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
813 + usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
814 +}
815 +
816 +static struct ethtool_ops ops = {
817 + .get_drvinfo = hso_get_drvinfo,
818 + .get_link = ethtool_op_get_link
819 +};
820 +
821 +/* called when a packet did not ack after watchdogtimeout */
822 +static void hso_net_tx_timeout(struct net_device *net)
823 +{
824 + struct hso_net *odev = netdev_priv(net);
825 +
826 + if (!odev)
827 + return;
828 +
829 + /* Tell syslog we are hosed. */
830 + dev_warn(&net->dev, "Tx timed out.\n");
831 +
832 + /* Tear the waiting frame off the list */
833 + if (odev->mux_bulk_tx_urb
834 + && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
835 + usb_unlink_urb(odev->mux_bulk_tx_urb);
836 +
837 + /* Update statistics */
838 + net->stats.tx_errors++;
839 +}
840 +
841 +/* make a real packet from the received USB buffer */
842 +static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
843 + unsigned int count, unsigned char is_eop)
844 +{
845 + unsigned short temp_bytes;
846 + unsigned short buffer_offset = 0;
847 + unsigned short frame_len;
848 + unsigned char *tmp_rx_buf;
849 +
850 + /* log if needed */
851 + D1("Rx %d bytes", count);
852 + DUMP(ip_pkt, min(128, (int)count));
853 +
854 + while (count) {
855 + switch (odev->rx_parse_state) {
856 + case WAIT_IP:
857 + /* waiting for IP header. */
858 + /* wanted bytes - size of ip header */
859 + temp_bytes =
860 + (count <
861 + odev->rx_buf_missing) ? count : odev->
862 + rx_buf_missing;
863 +
864 + memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
865 + odev->rx_buf_size, ip_pkt + buffer_offset,
866 + temp_bytes);
867 +
868 + odev->rx_buf_size += temp_bytes;
869 + buffer_offset += temp_bytes;
870 + odev->rx_buf_missing -= temp_bytes;
871 + count -= temp_bytes;
872 +
873 + if (!odev->rx_buf_missing) {
874 + /* header is complete allocate an sk_buffer and
875 + * continue to WAIT_DATA */
876 + frame_len = ntohs(odev->rx_ip_hdr.tot_len);
877 +
878 + if ((frame_len > DEFAULT_MRU) ||
879 + (frame_len < sizeof(struct iphdr))) {
880 + dev_err(&odev->net->dev,
881 + "Invalid frame (%d) length\n",
882 + frame_len);
883 + odev->rx_parse_state = WAIT_SYNC;
884 + continue;
885 + }
886 + /* Allocate an sk_buff */
887 + odev->skb_rx_buf = dev_alloc_skb(frame_len);
888 + if (!odev->skb_rx_buf) {
889 + /* We got no receive buffer. */
890 + D1("could not allocate memory");
891 + odev->rx_parse_state = WAIT_SYNC;
892 + return;
893 + }
894 + /* Here's where it came from */
895 + odev->skb_rx_buf->dev = odev->net;
896 +
897 + /* Copy what we got so far. make room for iphdr
898 + * after tail. */
899 + tmp_rx_buf =
900 + skb_put(odev->skb_rx_buf,
901 + sizeof(struct iphdr));
902 + memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
903 + sizeof(struct iphdr));
904 +
905 + /* ETH_HLEN */
906 + odev->rx_buf_size = sizeof(struct iphdr);
907 +
908 + /* Filip actually use .tot_len */
909 + odev->rx_buf_missing =
910 + frame_len - sizeof(struct iphdr);
911 + odev->rx_parse_state = WAIT_DATA;
912 + }
913 + break;
914 +
915 + case WAIT_DATA:
916 + temp_bytes = (count < odev->rx_buf_missing)
917 + ? count : odev->rx_buf_missing;
918 +
919 + /* Copy the rest of the bytes that are left in the
920 + * buffer into the waiting sk_buf. */
921 + /* Make room for temp_bytes after tail. */
922 + tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
923 + memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
924 +
925 + odev->rx_buf_missing -= temp_bytes;
926 + count -= temp_bytes;
927 + buffer_offset += temp_bytes;
928 + odev->rx_buf_size += temp_bytes;
929 + if (!odev->rx_buf_missing) {
930 + /* Packet is complete. Inject into stack. */
931 + /* We have IP packet here */
932 + odev->skb_rx_buf->protocol =
933 + __constant_htons(ETH_P_IP);
934 + /* don't check it */
935 + odev->skb_rx_buf->ip_summed =
936 + CHECKSUM_UNNECESSARY;
937 +
938 + skb_reset_mac_header(odev->skb_rx_buf);
939 +
940 + /* Ship it off to the kernel */
941 + netif_rx(odev->skb_rx_buf);
942 + /* No longer our buffer. */
943 + odev->skb_rx_buf = NULL;
944 +
945 + /* update out statistics */
946 + odev->net->stats.rx_packets++;
947 +
948 + odev->net->stats.rx_bytes += odev->rx_buf_size;
949 +
950 + odev->rx_buf_size = 0;
951 + odev->rx_buf_missing = sizeof(struct iphdr);
952 + odev->rx_parse_state = WAIT_IP;
953 + }
954 + break;
955 +
956 + case WAIT_SYNC:
957 + D1(" W_S");
958 + count = 0;
959 + break;
960 + default:
961 + D1(" ");
962 + count--;
963 + break;
964 + }
965 + }
966 +
967 + /* Recovery mechanism for WAIT_SYNC state. */
968 + if (is_eop) {
969 + if (odev->rx_parse_state == WAIT_SYNC) {
970 + odev->rx_parse_state = WAIT_IP;
971 + odev->rx_buf_size = 0;
972 + odev->rx_buf_missing = sizeof(struct iphdr);
973 + }
974 + }
975 +}
976 +
977 +/* Moving data from usb to kernel (in interrupt state) */
978 +static void read_bulk_callback(struct urb *urb)
979 +{
980 + struct hso_net *odev = urb->context;
981 + struct net_device *net;
982 + int result;
983 + int status = urb->status;
984 +
985 + /* is al ok? (Filip: Who's Al ?) */
986 + if (status) {
987 + log_usb_status(status, __func__);
988 + return;
989 + }
990 +
991 + /* Sanity check */
992 + if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
993 + D1("BULK IN callback but driver is not active!");
994 + return;
995 + }
996 + usb_mark_last_busy(urb->dev);
997 +
998 + net = odev->net;
999 +
1000 + if (!netif_device_present(net)) {
1001 + /* Somebody killed our network interface... */
1002 + return;
1003 + }
1004 +
1005 + if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
1006 + u32 rest;
1007 + u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1008 + rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
1009 + if (((rest == 5) || (rest == 6))
1010 + && !memcmp(((u8 *) urb->transfer_buffer) +
1011 + urb->actual_length - 4, crc_check, 4)) {
1012 + urb->actual_length -= 4;
1013 + }
1014 + }
1015 +
1016 + /* do we even have a packet? */
1017 + if (urb->actual_length) {
1018 + /* Handle the IP stream, add header and push it onto network
1019 + * stack if the packet is complete. */
1020 + spin_lock(&odev->net_lock);
1021 + packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1022 + (urb->transfer_buffer_length >
1023 + urb->actual_length) ? 1 : 0);
1024 + spin_unlock(&odev->net_lock);
1025 + }
1026 +
1027 + /* We are done with this URB, resubmit it. Prep the USB to wait for
1028 + * another frame. Reuse same as received. */
1029 + usb_fill_bulk_urb(urb,
1030 + odev->parent->usb,
1031 + usb_rcvbulkpipe(odev->parent->usb,
1032 + odev->in_endp->
1033 + bEndpointAddress & 0x7F),
1034 + urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1035 + read_bulk_callback, odev);
1036 +
1037 + /* Give this to the USB subsystem so it can tell us when more data
1038 + * arrives. */
1039 + result = usb_submit_urb(urb, GFP_ATOMIC);
1040 + if (result)
1041 + dev_warn(&odev->parent->interface->dev,
1042 + "%s failed submit mux_bulk_rx_urb %d", __func__,
1043 + result);
1044 +}
1045 +
1046 +/* Serial driver functions */
1047 +
1048 +static void _hso_serial_set_termios(struct tty_struct *tty,
1049 + struct ktermios *old)
1050 +{
1051 + struct hso_serial *serial = get_serial_by_tty(tty);
1052 + struct ktermios *termios;
1053 +
1054 + if ((!tty) || (!tty->termios) || (!serial)) {
1055 + printk(KERN_ERR "%s: no tty structures", __func__);
1056 + return;
1057 + }
1058 +
1059 + D4("port %d", serial->minor);
1060 +
1061 + /*
1062 + * The default requirements for this device are:
1063 + */
1064 + termios = tty->termios;
1065 + termios->c_iflag &=
1066 + ~(IGNBRK /* disable ignore break */
1067 + | BRKINT /* disable break causes interrupt */
1068 + | PARMRK /* disable mark parity errors */
1069 + | ISTRIP /* disable clear high bit of input characters */
1070 + | INLCR /* disable translate NL to CR */
1071 + | IGNCR /* disable ignore CR */
1072 + | ICRNL /* disable translate CR to NL */
1073 + | IXON); /* disable enable XON/XOFF flow control */
1074 +
1075 + /* disable postprocess output characters */
1076 + termios->c_oflag &= ~OPOST;
1077 +
1078 + termios->c_lflag &=
1079 + ~(ECHO /* disable echo input characters */
1080 + | ECHONL /* disable echo new line */
1081 + | ICANON /* disable erase, kill, werase, and rprnt
1082 + special characters */
1083 + | ISIG /* disable interrupt, quit, and suspend special
1084 + characters */
1085 + | IEXTEN); /* disable non-POSIX special characters */
1086 +
1087 + termios->c_cflag &=
1088 + ~(CSIZE /* no size */
1089 + | PARENB /* disable parity bit */
1090 + | CBAUD /* clear current baud rate */
1091 + | CBAUDEX); /* clear current buad rate */
1092 +
1093 + termios->c_cflag |= CS8; /* character size 8 bits */
1094 +
1095 + /* baud rate 115200 */
1096 + tty_encode_baud_rate(serial->tty, 115200, 115200);
1097 +
1098 + /*
1099 + * Force low_latency on; otherwise the pushes are scheduled;
1100 + * this is bad as it opens up the possibility of dropping bytes
1101 + * on the floor. We don't want to drop bytes on the floor. :)
1102 + */
1103 + serial->tty->low_latency = 1;
1104 + return;
1105 +}
1106 +
1107 +/* open the requested serial port */
1108 +static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1109 +{
1110 + struct hso_serial *serial = get_serial_by_index(tty->index);
1111 + int result;
1112 +
1113 + /* sanity check */
1114 + if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1115 + tty->driver_data = NULL;
1116 + D1("Failed to open port");
1117 + return -ENODEV;
1118 + }
1119 +
1120 + mutex_lock(&serial->parent->mutex);
1121 + result = usb_autopm_get_interface(serial->parent->interface);
1122 + if (result < 0)
1123 + goto err_out;
1124 +
1125 + D1("Opening %d", serial->minor);
1126 + kref_get(&serial->parent->ref);
1127 +
1128 + /* setup */
1129 + tty->driver_data = serial;
1130 + serial->tty = tty;
1131 +
1132 + /* check for port allready opened, if not set the termios */
1133 + serial->open_count++;
1134 + if (serial->open_count == 1) {
1135 + tty->low_latency = 1;
1136 + serial->flags = 0;
1137 + /* Force default termio settings */
1138 + _hso_serial_set_termios(tty, NULL);
1139 + result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1140 + if (result) {
1141 + hso_stop_serial_device(serial->parent);
1142 + serial->open_count--;
1143 + kref_put(&serial->parent->ref, hso_serial_ref_free);
1144 + }
1145 + } else {
1146 + D1("Port was already open");
1147 + }
1148 +
1149 + usb_autopm_put_interface(serial->parent->interface);
1150 +
1151 + /* done */
1152 + if (result)
1153 + hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
1154 +err_out:
1155 + mutex_unlock(&serial->parent->mutex);
1156 + return result;
1157 +}
1158 +
1159 +/* close the requested serial port */
1160 +static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1161 +{
1162 + struct hso_serial *serial = tty->driver_data;
1163 + u8 usb_gone;
1164 +
1165 + D1("Closing serial port");
1166 +
1167 + mutex_lock(&serial->parent->mutex);
1168 + usb_gone = serial->parent->usb_gone;
1169 +
1170 + if (!usb_gone)
1171 + usb_autopm_get_interface(serial->parent->interface);
1172 +
1173 + /* reset the rts and dtr */
1174 + /* do the actual close */
1175 + serial->open_count--;
1176 + if (serial->open_count <= 0) {
1177 + kref_put(&serial->parent->ref, hso_serial_ref_free);
1178 + serial->open_count = 0;
1179 + if (serial->tty) {
1180 + serial->tty->driver_data = NULL;
1181 + serial->tty = NULL;
1182 + }
1183 + if (!usb_gone)
1184 + hso_stop_serial_device(serial->parent);
1185 + }
1186 + if (!usb_gone)
1187 + usb_autopm_put_interface(serial->parent->interface);
1188 + mutex_unlock(&serial->parent->mutex);
1189 +}
1190 +
1191 +/* close the requested serial port */
1192 +static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1193 + int count)
1194 +{
1195 + struct hso_serial *serial = get_serial_by_tty(tty);
1196 + int space, tx_bytes;
1197 + unsigned long flags;
1198 +
1199 + /* sanity check */
1200 + if (serial == NULL) {
1201 + printk(KERN_ERR "%s: serial is NULL\n", __func__);
1202 + return -ENODEV;
1203 + }
1204 +
1205 + spin_lock_irqsave(&serial->serial_lock, flags);
1206 +
1207 + space = serial->tx_data_length - serial->tx_buffer_count;
1208 + tx_bytes = (count < space) ? count : space;
1209 +
1210 + if (!tx_bytes)
1211 + goto out;
1212 +
1213 + memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1214 + serial->tx_buffer_count += tx_bytes;
1215 +
1216 +out:
1217 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1218 +
1219 + hso_kick_transmit(serial);
1220 + /* done */
1221 + return tx_bytes;
1222 +}
1223 +
1224 +/* how much room is there for writing */
1225 +static int hso_serial_write_room(struct tty_struct *tty)
1226 +{
1227 + struct hso_serial *serial = get_serial_by_tty(tty);
1228 + int room;
1229 + unsigned long flags;
1230 +
1231 + spin_lock_irqsave(&serial->serial_lock, flags);
1232 + room = serial->tx_data_length - serial->tx_buffer_count;
1233 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1234 +
1235 + /* return free room */
1236 + return room;
1237 +}
1238 +
1239 +/* setup the term */
1240 +static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1241 +{
1242 + struct hso_serial *serial = get_serial_by_tty(tty);
1243 + unsigned long flags;
1244 +
1245 + if (old)
1246 + D5("Termios called with: cflags new[%d] - old[%d]",
1247 + tty->termios->c_cflag, old->c_cflag);
1248 +
1249 + /* the actual setup */
1250 + spin_lock_irqsave(&serial->serial_lock, flags);
1251 + if (serial->open_count)
1252 + _hso_serial_set_termios(tty, old);
1253 + else
1254 + tty->termios = old;
1255 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1256 +
1257 + /* done */
1258 + return;
1259 +}
1260 +
1261 +/* how many characters in the buffer */
1262 +static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1263 +{
1264 + struct hso_serial *serial = get_serial_by_tty(tty);
1265 + int chars;
1266 + unsigned long flags;
1267 +
1268 + /* sanity check */
1269 + if (serial == NULL)
1270 + return 0;
1271 +
1272 + spin_lock_irqsave(&serial->serial_lock, flags);
1273 + chars = serial->tx_buffer_count;
1274 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1275 +
1276 + return chars;
1277 +}
1278 +
1279 +static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
1280 +{
1281 + unsigned int value;
1282 + struct hso_serial *serial = get_serial_by_tty(tty);
1283 + unsigned long flags;
1284 +
1285 + /* sanity check */
1286 + if (!serial) {
1287 + D1("no tty structures");
1288 + return -EINVAL;
1289 + }
1290 +
1291 + spin_lock_irqsave(&serial->serial_lock, flags);
1292 + value = ((serial->rts_state) ? TIOCM_RTS : 0) |
1293 + ((serial->dtr_state) ? TIOCM_DTR : 0);
1294 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1295 +
1296 + return value;
1297 +}
1298 +
1299 +static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
1300 + unsigned int set, unsigned int clear)
1301 +{
1302 + int val = 0;
1303 + unsigned long flags;
1304 + int if_num;
1305 + struct hso_serial *serial = get_serial_by_tty(tty);
1306 +
1307 + /* sanity check */
1308 + if (!serial) {
1309 + D1("no tty structures");
1310 + return -EINVAL;
1311 + }
1312 + if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1313 +
1314 + spin_lock_irqsave(&serial->serial_lock, flags);
1315 + if (set & TIOCM_RTS)
1316 + serial->rts_state = 1;
1317 + if (set & TIOCM_DTR)
1318 + serial->dtr_state = 1;
1319 +
1320 + if (clear & TIOCM_RTS)
1321 + serial->rts_state = 0;
1322 + if (clear & TIOCM_DTR)
1323 + serial->dtr_state = 0;
1324 +
1325 + if (serial->dtr_state)
1326 + val |= 0x01;
1327 + if (serial->rts_state)
1328 + val |= 0x02;
1329 +
1330 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1331 +
1332 + return usb_control_msg(serial->parent->usb,
1333 + usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1334 + 0x21, val, if_num, NULL, 0,
1335 + USB_CTRL_SET_TIMEOUT);
1336 +}
1337 +
1338 +/* starts a transmit */
1339 +static void hso_kick_transmit(struct hso_serial *serial)
1340 +{
1341 + u8 *temp;
1342 + unsigned long flags;
1343 + int res;
1344 +
1345 + spin_lock_irqsave(&serial->serial_lock, flags);
1346 + if (!serial->tx_buffer_count)
1347 + goto out;
1348 +
1349 + if (serial->tx_urb_used)
1350 + goto out;
1351 +
1352 + /* Wakeup USB interface if necessary */
1353 + if (hso_get_activity(serial->parent) == -EAGAIN)
1354 + goto out;
1355 +
1356 + /* Switch pointers around to avoid memcpy */
1357 + temp = serial->tx_buffer;
1358 + serial->tx_buffer = serial->tx_data;
1359 + serial->tx_data = temp;
1360 + serial->tx_data_count = serial->tx_buffer_count;
1361 + serial->tx_buffer_count = 0;
1362 +
1363 + /* If temp is set, it means we switched buffers */
1364 + if (temp && serial->write_data) {
1365 + res = serial->write_data(serial);
1366 + if (res >= 0)
1367 + serial->tx_urb_used = 1;
1368 + }
1369 +out:
1370 + spin_unlock_irqrestore(&serial->serial_lock, flags);
1371 +}
1372 +
1373 +/* make a request (for reading and writing data to muxed serial port) */
1374 +static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1375 + struct urb *ctrl_urb,
1376 + struct usb_ctrlrequest *ctrl_req,
1377 + u8 *ctrl_urb_data, u32 size)
1378 +{
1379 + int result;
1380 + int pipe;
1381 +
1382 + /* Sanity check */
1383 + if (!serial || !ctrl_urb || !ctrl_req) {
1384 + printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1385 + return -EINVAL;
1386 + }
1387 +
1388 + /* initialize */
1389 + ctrl_req->wValue = 0;
1390 + ctrl_req->wIndex = hso_port_to_mux(port);
1391 + ctrl_req->wLength = size;
1392 +
1393 + if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1394 + /* Reading command */
1395 + ctrl_req->bRequestType = USB_DIR_IN |
1396 + USB_TYPE_OPTION_VENDOR |
1397 + USB_RECIP_INTERFACE;
1398 + ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1399 + pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1400 + } else {
1401 + /* Writing command */
1402 + ctrl_req->bRequestType = USB_DIR_OUT |
1403 + USB_TYPE_OPTION_VENDOR |
1404 + USB_RECIP_INTERFACE;
1405 + ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1406 + pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1407 + }
1408 + /* syslog */
1409 + D2("%s command (%02x) len: %d, port: %d",
1410 + type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1411 + ctrl_req->bRequestType, ctrl_req->wLength, port);
1412 +
1413 + /* Load ctrl urb */
1414 + ctrl_urb->transfer_flags = 0;
1415 + usb_fill_control_urb(ctrl_urb,
1416 + serial->parent->usb,
1417 + pipe,
1418 + (u8 *) ctrl_req,
1419 + ctrl_urb_data, size, ctrl_callback, serial);
1420 + /* Send it on merry way */
1421 + result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1422 + if (result) {
1423 + dev_err(&ctrl_urb->dev->dev,
1424 + "%s failed submit ctrl_urb %d type %d", __func__,
1425 + result, type);
1426 + return result;
1427 + }
1428 +
1429 + /* done */
1430 + return size;
1431 +}
1432 +
1433 +/* called by intr_callback when read occurs */
1434 +static int hso_mux_serial_read(struct hso_serial *serial)
1435 +{
1436 + if (!serial)
1437 + return -EINVAL;
1438 +
1439 + /* clean data */
1440 + memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1441 + /* make the request */
1442 +
1443 + if (serial->num_rx_urbs != 1) {
1444 + dev_err(&serial->parent->interface->dev,
1445 + "ERROR: mux'd reads with multiple buffers "
1446 + "not possible\n");
1447 + return 0;
1448 + }
1449 + return mux_device_request(serial,
1450 + USB_CDC_GET_ENCAPSULATED_RESPONSE,
1451 + serial->parent->port_spec & HSO_PORT_MASK,
1452 + serial->rx_urb[0],
1453 + &serial->ctrl_req_rx,
1454 + serial->rx_data[0], serial->rx_data_length);
1455 +}
1456 +
1457 +/* used for muxed serial port callback (muxed serial read) */
1458 +static void intr_callback(struct urb *urb)
1459 +{
1460 + struct hso_shared_int *shared_int = urb->context;
1461 + struct hso_serial *serial;
1462 + unsigned char *port_req;
1463 + int status = urb->status;
1464 + int i;
1465 +
1466 + usb_mark_last_busy(urb->dev);
1467 +
1468 + /* sanity check */
1469 + if (!shared_int)
1470 + return;
1471 +
1472 + /* status check */
1473 + if (status) {
1474 + log_usb_status(status, __func__);
1475 + return;
1476 + }
1477 + D4("\n--- Got intr callback 0x%02X ---", status);
1478 +
1479 + /* what request? */
1480 + port_req = urb->transfer_buffer;
1481 + D4(" port_req = 0x%.2X\n", *port_req);
1482 + /* loop over all muxed ports to find the one sending this */
1483 + for (i = 0; i < 8; i++) {
1484 + /* max 8 channels on MUX */
1485 + if (*port_req & (1 << i)) {
1486 + serial = get_serial_by_shared_int_and_type(shared_int,
1487 + (1 << i));
1488 + if (serial != NULL) {
1489 + D1("Pending read interrupt on port %d\n", i);
1490 + if (!test_and_set_bit(HSO_SERIAL_FLAG_RX_SENT,
1491 + &serial->flags)) {
1492 + /* Setup and send a ctrl req read on
1493 + * port i */
1494 + hso_mux_serial_read(serial);
1495 + } else {
1496 + D1("Already pending a read on "
1497 + "port %d\n", i);
1498 + }
1499 + }
1500 + }
1501 + }
1502 + /* Resubmit interrupt urb */
1503 + hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1504 +}
1505 +
1506 +/* called for writing to muxed serial port */
1507 +static int hso_mux_serial_write_data(struct hso_serial *serial)
1508 +{
1509 + if (NULL == serial)
1510 + return -EINVAL;
1511 +
1512 + return mux_device_request(serial,
1513 + USB_CDC_SEND_ENCAPSULATED_COMMAND,
1514 + serial->parent->port_spec & HSO_PORT_MASK,
1515 + serial->tx_urb,
1516 + &serial->ctrl_req_tx,
1517 + serial->tx_data, serial->tx_data_count);
1518 +}
1519 +
1520 +/* write callback for Diag and CS port */
1521 +static void hso_std_serial_write_bulk_callback(struct urb *urb)
1522 +{
1523 + struct hso_serial *serial = urb->context;
1524 + int status = urb->status;
1525 +
1526 + /* sanity check */
1527 + if (!serial) {
1528 + D1("serial == NULL");
1529 + return;
1530 + }
1531 +
1532 + spin_lock(&serial->serial_lock);
1533 + serial->tx_urb_used = 0;
1534 + spin_unlock(&serial->serial_lock);
1535 + if (status) {
1536 + log_usb_status(status, __func__);
1537 + return;
1538 + }
1539 + hso_put_activity(serial->parent);
1540 + tty_wakeup(serial->tty);
1541 + hso_kick_transmit(serial);
1542 +
1543 + D1(" ");
1544 + return;
1545 +}
1546 +
1547 +/* called for writing diag or CS serial port */
1548 +static int hso_std_serial_write_data(struct hso_serial *serial)
1549 +{
1550 + int count = serial->tx_data_count;
1551 + int result;
1552 +
1553 + usb_fill_bulk_urb(serial->tx_urb,
1554 + serial->parent->usb,
1555 + usb_sndbulkpipe(serial->parent->usb,
1556 + serial->out_endp->
1557 + bEndpointAddress & 0x7F),
1558 + serial->tx_data, serial->tx_data_count,
1559 + hso_std_serial_write_bulk_callback, serial);
1560 +
1561 + result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1562 + if (result) {
1563 + dev_warn(&serial->parent->usb->dev,
1564 + "Failed to submit urb - res %d\n", result);
1565 + return result;
1566 + }
1567 +
1568 + return count;
1569 +}
1570 +
1571 +/* callback after read or write on muxed serial port */
1572 +static void ctrl_callback(struct urb *urb)
1573 +{
1574 + struct hso_serial *serial = urb->context;
1575 + struct usb_ctrlrequest *req;
1576 + int status = urb->status;
1577 +
1578 + /* sanity check */
1579 + if (!serial)
1580 + return;
1581 +
1582 + spin_lock(&serial->serial_lock);
1583 + serial->tx_urb_used = 0;
1584 + spin_unlock(&serial->serial_lock);
1585 + if (status) {
1586 + log_usb_status(status, __func__);
1587 + return;
1588 + }
1589 +
1590 + /* what request? */
1591 + req = (struct usb_ctrlrequest *)(urb->setup_packet);
1592 + D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1593 + D4("Actual length of urb = %d\n", urb->actual_length);
1594 + DUMP1(urb->transfer_buffer, urb->actual_length);
1595 +
1596 + if (req->bRequestType ==
1597 + (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1598 + /* response to a read command */
1599 + if (serial->open_count > 0) {
1600 + /* handle RX data the normal way */
1601 + put_rxbuf_data(urb, serial);
1602 + }
1603 +
1604 + /* Re issue a read as long as we receive data. */
1605 + if (urb->actual_length != 0)
1606 + hso_mux_serial_read(serial);
1607 + else
1608 + clear_bit(HSO_SERIAL_FLAG_RX_SENT, &serial->flags);
1609 + } else {
1610 + hso_put_activity(serial->parent);
1611 + tty_wakeup(serial->tty);
1612 + /* response to a write command */
1613 + hso_kick_transmit(serial);
1614 + }
1615 +}
1616 +
1617 +/* handle RX data for serial port */
1618 +static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
1619 +{
1620 + struct tty_struct *tty = serial->tty;
1621 +
1622 + /* Sanity check */
1623 + if (urb == NULL || serial == NULL) {
1624 + D1("serial = NULL");
1625 + return;
1626 + }
1627 +
1628 + /* Push data to tty */
1629 + if (tty && urb->actual_length) {
1630 + D1("data to push to tty");
1631 + tty_insert_flip_string(tty, urb->transfer_buffer,
1632 + urb->actual_length);
1633 + tty_flip_buffer_push(tty);
1634 + }
1635 +}
1636 +
1637 +/* read callback for Diag and CS port */
1638 +static void hso_std_serial_read_bulk_callback(struct urb *urb)
1639 +{
1640 + struct hso_serial *serial = urb->context;
1641 + int result;
1642 + int status = urb->status;
1643 +
1644 + /* sanity check */
1645 + if (!serial) {
1646 + D1("serial == NULL");
1647 + return;
1648 + } else if (status) {
1649 + log_usb_status(status, __func__);
1650 + return;
1651 + }
1652 +
1653 + D4("\n--- Got serial_read_bulk callback %02x ---", status);
1654 + D1("Actual length = %d\n", urb->actual_length);
1655 + DUMP1(urb->transfer_buffer, urb->actual_length);
1656 +
1657 + /* Anyone listening? */
1658 + if (serial->open_count == 0)
1659 + return;
1660 +
1661 + if (status == 0) {
1662 + if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
1663 + u32 rest;
1664 + u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
1665 + rest =
1666 + urb->actual_length %
1667 + serial->in_endp->wMaxPacketSize;
1668 + if (((rest == 5) || (rest == 6))
1669 + && !memcmp(((u8 *) urb->transfer_buffer) +
1670 + urb->actual_length - 4, crc_check, 4)) {
1671 + urb->actual_length -= 4;
1672 + }
1673 + }
1674 + /* Valid data, handle RX data */
1675 + put_rxbuf_data(urb, serial);
1676 + } else if (status == -ENOENT || status == -ECONNRESET) {
1677 + /* Unlinked - check for throttled port. */
1678 + D2("Port %d, successfully unlinked urb", serial->minor);
1679 + } else {
1680 + D2("Port %d, status = %d for read urb", serial->minor, status);
1681 + return;
1682 + }
1683 +
1684 + usb_mark_last_busy(urb->dev);
1685 +
1686 + /* We are done with this URB, resubmit it. Prep the USB to wait for
1687 + * another frame */
1688 + usb_fill_bulk_urb(urb, serial->parent->usb,
1689 + usb_rcvbulkpipe(serial->parent->usb,
1690 + serial->in_endp->
1691 + bEndpointAddress & 0x7F),
1692 + urb->transfer_buffer, serial->rx_data_length,
1693 + hso_std_serial_read_bulk_callback, serial);
1694 + /* Give this to the USB subsystem so it can tell us when more data
1695 + * arrives. */
1696 + result = usb_submit_urb(urb, GFP_ATOMIC);
1697 + if (result) {
1698 + dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d",
1699 + __func__, result);
1700 + }
1701 +}
1702 +
1703 +/* Base driver functions */
1704 +
1705 +static void hso_log_port(struct hso_device *hso_dev)
1706 +{
1707 + char *port_type;
1708 + char port_dev[20];
1709 +
1710 + switch (hso_dev->port_spec & HSO_PORT_MASK) {
1711 + case HSO_PORT_CONTROL:
1712 + port_type = "Control";
1713 + break;
1714 + case HSO_PORT_APP:
1715 + port_type = "Application";
1716 + break;
1717 + case HSO_PORT_GPS:
1718 + port_type = "GPS";
1719 + break;
1720 + case HSO_PORT_GPS_CONTROL:
1721 + port_type = "GPS control";
1722 + break;
1723 + case HSO_PORT_APP2:
1724 + port_type = "Application2";
1725 + break;
1726 + case HSO_PORT_PCSC:
1727 + port_type = "PCSC";
1728 + break;
1729 + case HSO_PORT_DIAG:
1730 + port_type = "Diagnostic";
1731 + break;
1732 + case HSO_PORT_DIAG2:
1733 + port_type = "Diagnostic2";
1734 + break;
1735 + case HSO_PORT_MODEM:
1736 + port_type = "Modem";
1737 + break;
1738 + case HSO_PORT_NETWORK:
1739 + port_type = "Network";
1740 + break;
1741 + default:
1742 + port_type = "Unknown";
1743 + break;
1744 + }
1745 + if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
1746 + sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
1747 + } else
1748 + sprintf(port_dev, "/dev/%s%d", tty_filename,
1749 + dev2ser(hso_dev)->minor);
1750 +
1751 + dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
1752 + port_type, port_dev);
1753 +}
1754 +
1755 +static int hso_start_net_device(struct hso_device *hso_dev)
1756 +{
1757 + int i, result = 0;
1758 + struct hso_net *hso_net = dev2net(hso_dev);
1759 +
1760 + if (!hso_net)
1761 + return -ENODEV;
1762 +
1763 + /* send URBs for all read buffers */
1764 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1765 +
1766 + /* Prep a receive URB */
1767 + usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
1768 + hso_dev->usb,
1769 + usb_rcvbulkpipe(hso_dev->usb,
1770 + hso_net->in_endp->
1771 + bEndpointAddress & 0x7F),
1772 + hso_net->mux_bulk_rx_buf_pool[i],
1773 + MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
1774 + hso_net);
1775 +
1776 + /* Put it out there so the device can send us stuff */
1777 + result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
1778 + GFP_NOIO);
1779 + if (result)
1780 + dev_warn(&hso_dev->usb->dev,
1781 + "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
1782 + i, result);
1783 + }
1784 +
1785 + return result;
1786 +}
1787 +
1788 +static int hso_stop_net_device(struct hso_device *hso_dev)
1789 +{
1790 + int i;
1791 + struct hso_net *hso_net = dev2net(hso_dev);
1792 +
1793 + if (!hso_net)
1794 + return -ENODEV;
1795 +
1796 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
1797 + if (hso_net->mux_bulk_rx_urb_pool[i])
1798 + usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
1799 +
1800 + }
1801 + if (hso_net->mux_bulk_tx_urb)
1802 + usb_kill_urb(hso_net->mux_bulk_tx_urb);
1803 +
1804 + return 0;
1805 +}
1806 +
1807 +static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
1808 +{
1809 + int i, result = 0;
1810 + struct hso_serial *serial = dev2ser(hso_dev);
1811 +
1812 + if (!serial)
1813 + return -ENODEV;
1814 +
1815 + /* If it is not the MUX port fill in and submit a bulk urb (already
1816 + * allocated in hso_serial_start) */
1817 + if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
1818 + for (i = 0; i < serial->num_rx_urbs; i++) {
1819 + usb_fill_bulk_urb(serial->rx_urb[i],
1820 + serial->parent->usb,
1821 + usb_rcvbulkpipe(serial->parent->usb,
1822 + serial->in_endp->
1823 + bEndpointAddress &
1824 + 0x7F),
1825 + serial->rx_data[i],
1826 + serial->rx_data_length,
1827 + hso_std_serial_read_bulk_callback,
1828 + serial);
1829 + result = usb_submit_urb(serial->rx_urb[i], flags);
1830 + if (result) {
1831 + dev_warn(&serial->parent->usb->dev,
1832 + "Failed to submit urb - res %d\n",
1833 + result);
1834 + break;
1835 + }
1836 + }
1837 + } else {
1838 + mutex_lock(&serial->shared_int->shared_int_lock);
1839 + if (!serial->shared_int->use_count) {
1840 + result =
1841 + hso_mux_submit_intr_urb(serial->shared_int,
1842 + hso_dev->usb, flags);
1843 + }
1844 + serial->shared_int->use_count++;
1845 + mutex_unlock(&serial->shared_int->shared_int_lock);
1846 + }
1847 +
1848 + return result;
1849 +}
1850 +
1851 +static int hso_stop_serial_device(struct hso_device *hso_dev)
1852 +{
1853 + int i;
1854 + struct hso_serial *serial = dev2ser(hso_dev);
1855 +
1856 + if (!serial)
1857 + return -ENODEV;
1858 +
1859 + for (i = 0; i < serial->num_rx_urbs; i++) {
1860 + if (serial->rx_urb[i])
1861 + usb_kill_urb(serial->rx_urb[i]);
1862 + }
1863 +
1864 + if (serial->tx_urb)
1865 + usb_kill_urb(serial->tx_urb);
1866 +
1867 + if (serial->shared_int) {
1868 + mutex_lock(&serial->shared_int->shared_int_lock);
1869 + if (serial->shared_int->use_count &&
1870 + (--serial->shared_int->use_count == 0)) {
1871 + struct urb *urb;
1872 +
1873 + urb = serial->shared_int->shared_intr_urb;
1874 + if (urb)
1875 + usb_kill_urb(urb);
1876 + }
1877 + mutex_unlock(&serial->shared_int->shared_int_lock);
1878 + }
1879 +
1880 + return 0;
1881 +}
1882 +
1883 +static void hso_serial_common_free(struct hso_serial *serial)
1884 +{
1885 + int i;
1886 +
1887 + if (serial->parent->dev)
1888 + device_remove_file(serial->parent->dev, &dev_attr_hsotype);
1889 +
1890 + tty_unregister_device(tty_drv, serial->minor);
1891 +
1892 + for (i = 0; i < serial->num_rx_urbs; i++) {
1893 + /* unlink and free RX URB */
1894 + usb_free_urb(serial->rx_urb[i]);
1895 + /* free the RX buffer */
1896 + kfree(serial->rx_data[i]);
1897 + }
1898 +
1899 + /* unlink and free TX URB */
1900 + usb_free_urb(serial->tx_urb);
1901 + kfree(serial->tx_data);
1902 +}
1903 +
1904 +static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
1905 + int rx_size, int tx_size)
1906 +{
1907 + struct device *dev;
1908 + int minor;
1909 + int i;
1910 +
1911 + minor = get_free_serial_index();
1912 + if (minor < 0)
1913 + goto exit;
1914 +
1915 + /* register our minor number */
1916 + serial->parent->dev = tty_register_device(tty_drv, minor,
1917 + &serial->parent->interface->dev);
1918 + dev = serial->parent->dev;
1919 + dev->driver_data = serial->parent;
1920 + i = device_create_file(dev, &dev_attr_hsotype);
1921 +
1922 + /* fill in specific data for later use */
1923 + serial->minor = minor;
1924 + serial->magic = HSO_SERIAL_MAGIC;
1925 + spin_lock_init(&serial->serial_lock);
1926 + serial->num_rx_urbs = num_urbs;
1927 +
1928 + /* RX, allocate urb and initialize */
1929 +
1930 + /* prepare our RX buffer */
1931 + serial->rx_data_length = rx_size;
1932 + for (i = 0; i < serial->num_rx_urbs; i++) {
1933 + serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1934 + if (!serial->rx_urb[i]) {
1935 + dev_err(dev, "Could not allocate urb?\n");
1936 + goto exit;
1937 + }
1938 + serial->rx_urb[i]->transfer_buffer = NULL;
1939 + serial->rx_urb[i]->transfer_buffer_length = 0;
1940 + serial->rx_data[i] = kzalloc(serial->rx_data_length,
1941 + GFP_KERNEL);
1942 + if (!serial->rx_data[i]) {
1943 + dev_err(dev, "%s - Out of memory\n", __func__);
1944 + goto exit;
1945 + }
1946 + }
1947 +
1948 + /* TX, allocate urb and initialize */
1949 + serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1950 + if (!serial->tx_urb) {
1951 + dev_err(dev, "Could not allocate urb?\n");
1952 + goto exit;
1953 + }
1954 + serial->tx_urb->transfer_buffer = NULL;
1955 + serial->tx_urb->transfer_buffer_length = 0;
1956 + /* prepare our TX buffer */
1957 + serial->tx_data_count = 0;
1958 + serial->tx_buffer_count = 0;
1959 + serial->tx_data_length = tx_size;
1960 + serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
1961 + if (!serial->tx_data) {
1962 + dev_err(dev, "%s - Out of memory", __func__);
1963 + goto exit;
1964 + }
1965 + serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
1966 + if (!serial->tx_buffer) {
1967 + dev_err(dev, "%s - Out of memory", __func__);
1968 + goto exit;
1969 + }
1970 +
1971 + return 0;
1972 +exit:
1973 + hso_serial_common_free(serial);
1974 + return -1;
1975 +}
1976 +
1977 +/* Frees a general hso device */
1978 +static void hso_free_device(struct hso_device *hso_dev)
1979 +{
1980 + kfree(hso_dev);
1981 +}
1982 +
1983 +/* Creates a general hso device */
1984 +static struct hso_device *hso_create_device(struct usb_interface *intf,
1985 + int port_spec)
1986 +{
1987 + struct hso_device *hso_dev;
1988 +
1989 + hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
1990 + if (!hso_dev)
1991 + return NULL;
1992 +
1993 + hso_dev->port_spec = port_spec;
1994 + hso_dev->usb = interface_to_usbdev(intf);
1995 + hso_dev->interface = intf;
1996 + kref_init(&hso_dev->ref);
1997 + mutex_init(&hso_dev->mutex);
1998 +
1999 + INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2000 + INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2001 +
2002 + return hso_dev;
2003 +}
2004 +
2005 +/* Removes a network device in the network device table */
2006 +static int remove_net_device(struct hso_device *hso_dev)
2007 +{
2008 + int i;
2009 +
2010 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2011 + if (network_table[i] == hso_dev) {
2012 + network_table[i] = NULL;
2013 + break;
2014 + }
2015 + }
2016 + if (i == HSO_MAX_NET_DEVICES)
2017 + return -1;
2018 + return 0;
2019 +}
2020 +
2021 +/* Frees our network device */
2022 +static void hso_free_net_device(struct hso_device *hso_dev)
2023 +{
2024 + int i;
2025 + struct hso_net *hso_net = dev2net(hso_dev);
2026 +
2027 + if (!hso_net)
2028 + return;
2029 +
2030 + /* start freeing */
2031 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2032 + usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2033 + kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2034 + }
2035 + usb_free_urb(hso_net->mux_bulk_tx_urb);
2036 + kfree(hso_net->mux_bulk_tx_buf);
2037 +
2038 + remove_net_device(hso_net->parent);
2039 +
2040 + if (hso_net->net) {
2041 + unregister_netdev(hso_net->net);
2042 + free_netdev(hso_net->net);
2043 + }
2044 +
2045 + hso_free_device(hso_dev);
2046 +}
2047 +
2048 +/* initialize the network interface */
2049 +static void hso_net_init(struct net_device *net)
2050 +{
2051 + struct hso_net *hso_net = netdev_priv(net);
2052 +
2053 + D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2054 +
2055 + /* fill in the other fields */
2056 + net->open = hso_net_open;
2057 + net->stop = hso_net_close;
2058 + net->hard_start_xmit = hso_net_start_xmit;
2059 + net->tx_timeout = hso_net_tx_timeout;
2060 + net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2061 + net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2062 + net->type = ARPHRD_NONE;
2063 + net->mtu = DEFAULT_MTU - 14;
2064 + net->tx_queue_len = 10;
2065 + SET_ETHTOOL_OPS(net, &ops);
2066 +
2067 + /* and initialize the semaphore */
2068 + spin_lock_init(&hso_net->net_lock);
2069 +}
2070 +
2071 +/* Adds a network device in the network device table */
2072 +static int add_net_device(struct hso_device *hso_dev)
2073 +{
2074 + int i;
2075 +
2076 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2077 + if (network_table[i] == NULL) {
2078 + network_table[i] = hso_dev;
2079 + break;
2080 + }
2081 + }
2082 + if (i == HSO_MAX_NET_DEVICES)
2083 + return -1;
2084 + return 0;
2085 +}
2086 +
2087 +static int hso_radio_toggle(void *data, enum rfkill_state state)
2088 +{
2089 + struct hso_device *hso_dev = data;
2090 + int enabled = (state == RFKILL_STATE_ON);
2091 + int rv;
2092 +
2093 + mutex_lock(&hso_dev->mutex);
2094 + if (hso_dev->usb_gone)
2095 + rv = 0;
2096 + else
2097 + rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2098 + enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2099 + USB_CTRL_SET_TIMEOUT);
2100 + mutex_unlock(&hso_dev->mutex);
2101 + return rv;
2102 +}
2103 +
2104 +/* Creates and sets up everything for rfkill */
2105 +static void hso_create_rfkill(struct hso_device *hso_dev,
2106 + struct usb_interface *interface)
2107 +{
2108 + struct hso_net *hso_net = dev2net(hso_dev);
2109 + struct device *dev = hso_dev->dev;
2110 + char *rfkn;
2111 +
2112 + hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2113 + RFKILL_TYPE_WLAN);
2114 + if (!hso_net->rfkill) {
2115 + dev_err(dev, "%s - Out of memory", __func__);
2116 + return;
2117 + }
2118 + rfkn = kzalloc(20, GFP_KERNEL);
2119 + if (!rfkn) {
2120 + rfkill_free(hso_net->rfkill);
2121 + dev_err(dev, "%s - Out of memory", __func__);
2122 + return;
2123 + }
2124 + snprintf(rfkn, 20, "hso-%d",
2125 + interface->altsetting->desc.bInterfaceNumber);
2126 + hso_net->rfkill->name = rfkn;
2127 + hso_net->rfkill->state = RFKILL_STATE_ON;
2128 + hso_net->rfkill->data = hso_dev;
2129 + hso_net->rfkill->toggle_radio = hso_radio_toggle;
2130 + if (rfkill_register(hso_net->rfkill) < 0) {
2131 + kfree(rfkn);
2132 + hso_net->rfkill->name = NULL;
2133 + rfkill_free(hso_net->rfkill);
2134 + dev_err(dev, "%s - Failed to register rfkill", __func__);
2135 + return;
2136 + }
2137 +}
2138 +
2139 +/* Creates our network device */
2140 +static struct hso_device *hso_create_net_device(struct usb_interface *interface)
2141 +{
2142 + int result, i;
2143 + struct net_device *net;
2144 + struct hso_net *hso_net;
2145 + struct hso_device *hso_dev;
2146 +
2147 + hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
2148 + if (!hso_dev)
2149 + return NULL;
2150 +
2151 + /* allocate our network device, then we can put in our private data */
2152 + /* call hso_net_init to do the basic initialization */
2153 + net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2154 + if (!net) {
2155 + dev_err(&interface->dev, "Unable to create ethernet device\n");
2156 + goto exit;
2157 + }
2158 +
2159 + hso_net = netdev_priv(net);
2160 +
2161 + hso_dev->port_data.dev_net = hso_net;
2162 + hso_net->net = net;
2163 + hso_net->parent = hso_dev;
2164 +
2165 + hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2166 + USB_DIR_IN);
2167 + if (!hso_net->in_endp) {
2168 + dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2169 + goto exit;
2170 + }
2171 + hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2172 + USB_DIR_OUT);
2173 + if (!hso_net->out_endp) {
2174 + dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2175 + goto exit;
2176 + }
2177 + SET_NETDEV_DEV(net, &interface->dev);
2178 +
2179 + /* registering our net device */
2180 + result = register_netdev(net);
2181 + if (result) {
2182 + dev_err(&interface->dev, "Failed to register device\n");
2183 + goto exit;
2184 + }
2185 +
2186 + /* start allocating */
2187 + for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2188 + hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2189 + if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2190 + dev_err(&interface->dev, "Could not allocate rx urb\n");
2191 + goto exit;
2192 + }
2193 + hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2194 + GFP_KERNEL);
2195 + if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2196 + dev_err(&interface->dev, "Could not allocate rx buf\n");
2197 + goto exit;
2198 + }
2199 + }
2200 + hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2201 + if (!hso_net->mux_bulk_tx_urb) {
2202 + dev_err(&interface->dev, "Could not allocate tx urb\n");
2203 + goto exit;
2204 + }
2205 + hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2206 + if (!hso_net->mux_bulk_tx_buf) {
2207 + dev_err(&interface->dev, "Could not allocate tx buf\n");
2208 + goto exit;
2209 + }
2210 +
2211 + add_net_device(hso_dev);
2212 +
2213 + hso_log_port(hso_dev);
2214 +
2215 + hso_create_rfkill(hso_dev, interface);
2216 +
2217 + return hso_dev;
2218 +exit:
2219 + hso_free_net_device(hso_dev);
2220 + return NULL;
2221 +}
2222 +
2223 +/* Frees an AT channel ( goes for both mux and non-mux ) */
2224 +static void hso_free_serial_device(struct hso_device *hso_dev)
2225 +{
2226 + struct hso_serial *serial = dev2ser(hso_dev);
2227 +
2228 + if (!serial)
2229 + return;
2230 + set_serial_by_index(serial->minor, NULL);
2231 +
2232 + hso_serial_common_free(serial);
2233 +
2234 + if (serial->shared_int) {
2235 + mutex_lock(&serial->shared_int->shared_int_lock);
2236 + if (--serial->shared_int->ref_count == 0)
2237 + hso_free_shared_int(serial->shared_int);
2238 + else
2239 + mutex_unlock(&serial->shared_int->shared_int_lock);
2240 + }
2241 + kfree(serial);
2242 + hso_free_device(hso_dev);
2243 +}
2244 +
2245 +/* Creates a bulk AT channel */
2246 +static struct hso_device *hso_create_bulk_serial_device(
2247 + struct usb_interface *interface, int port)
2248 +{
2249 + struct hso_device *hso_dev;
2250 + struct hso_serial *serial;
2251 + int num_urbs;
2252 +
2253 + hso_dev = hso_create_device(interface, port);
2254 + if (!hso_dev)
2255 + return NULL;
2256 +
2257 + serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2258 + if (!serial)
2259 + goto exit;
2260 +
2261 + serial->parent = hso_dev;
2262 + hso_dev->port_data.dev_serial = serial;
2263 +
2264 + if (port & HSO_PORT_MODEM)
2265 + num_urbs = 2;
2266 + else
2267 + num_urbs = 1;
2268 +
2269 + if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2270 + BULK_URB_TX_SIZE))
2271 + goto exit;
2272 +
2273 + serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2274 + USB_DIR_IN);
2275 + if (!serial->in_endp) {
2276 + dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2277 + goto exit;
2278 + }
2279 +
2280 + if (!
2281 + (serial->out_endp =
2282 + hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2283 + dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2284 + goto exit;
2285 + }
2286 +
2287 + serial->write_data = hso_std_serial_write_data;
2288 +
2289 + /* and record this serial */
2290 + set_serial_by_index(serial->minor, serial);
2291 +
2292 + /* setup the proc dirs and files if needed */
2293 + hso_log_port(hso_dev);
2294 +
2295 + /* done, return it */
2296 + return hso_dev;
2297 +exit:
2298 + if (hso_dev && serial)
2299 + hso_serial_common_free(serial);
2300 + kfree(serial);
2301 + hso_free_device(hso_dev);
2302 + return NULL;
2303 +}
2304 +
2305 +/* Creates a multiplexed AT channel */
2306 +static
2307 +struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2308 + int port,
2309 + struct hso_shared_int *mux)
2310 +{
2311 + struct hso_device *hso_dev;
2312 + struct hso_serial *serial;
2313 + int port_spec;
2314 +
2315 + port_spec = HSO_INTF_MUX;
2316 + port_spec &= ~HSO_PORT_MASK;
2317 +
2318 + port_spec |= hso_mux_to_port(port);
2319 + if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2320 + return NULL;
2321 +
2322 + hso_dev = hso_create_device(interface, port_spec);
2323 + if (!hso_dev)
2324 + return NULL;
2325 +
2326 + serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2327 + if (!serial)
2328 + goto exit;
2329 +
2330 + hso_dev->port_data.dev_serial = serial;
2331 + serial->parent = hso_dev;
2332 +
2333 + if (hso_serial_common_create
2334 + (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2335 + goto exit;
2336 +
2337 + serial->tx_data_length--;
2338 + serial->write_data = hso_mux_serial_write_data;
2339 +
2340 + serial->shared_int = mux;
2341 + mutex_lock(&serial->shared_int->shared_int_lock);
2342 + serial->shared_int->ref_count++;
2343 + mutex_unlock(&serial->shared_int->shared_int_lock);
2344 +
2345 + /* and record this serial */
2346 + set_serial_by_index(serial->minor, serial);
2347 +
2348 + /* setup the proc dirs and files if needed */
2349 + hso_log_port(hso_dev);
2350 +
2351 + /* done, return it */
2352 + return hso_dev;
2353 +
2354 +exit:
2355 + if (serial) {
2356 + tty_unregister_device(tty_drv, serial->minor);
2357 + kfree(serial);
2358 + }
2359 + if (hso_dev)
2360 + hso_free_device(hso_dev);
2361 + return NULL;
2362 +
2363 +}
2364 +
2365 +static void hso_free_shared_int(struct hso_shared_int *mux)
2366 +{
2367 + usb_free_urb(mux->shared_intr_urb);
2368 + kfree(mux->shared_intr_buf);
2369 + mutex_unlock(&mux->shared_int_lock);
2370 + kfree(mux);
2371 +}
2372 +
2373 +static
2374 +struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2375 +{
2376 + struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2377 +
2378 + if (!mux)
2379 + return NULL;
2380 +
2381 + mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2382 + USB_DIR_IN);
2383 + if (!mux->intr_endp) {
2384 + dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2385 + goto exit;
2386 + }
2387 +
2388 + mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2389 + if (!mux->shared_intr_urb) {
2390 + dev_err(&interface->dev, "Could not allocate intr urb?");
2391 + goto exit;
2392 + }
2393 + mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
2394 + GFP_KERNEL);
2395 + if (!mux->shared_intr_buf) {
2396 + dev_err(&interface->dev, "Could not allocate intr buf?");
2397 + goto exit;
2398 + }
2399 +
2400 + mutex_init(&mux->shared_int_lock);
2401 +
2402 + return mux;
2403 +
2404 +exit:
2405 + kfree(mux->shared_intr_buf);
2406 + usb_free_urb(mux->shared_intr_urb);
2407 + kfree(mux);
2408 + return NULL;
2409 +}
2410 +
2411 +/* Gets the port spec for a certain interface */
2412 +static int hso_get_config_data(struct usb_interface *interface)
2413 +{
2414 + struct usb_device *usbdev = interface_to_usbdev(interface);
2415 + u8 config_data[17];
2416 + u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2417 + s32 result;
2418 +
2419 + if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2420 + 0x86, 0xC0, 0, 0, config_data, 17,
2421 + USB_CTRL_SET_TIMEOUT) != 0x11) {
2422 + return -EIO;
2423 + }
2424 +
2425 + switch (config_data[if_num]) {
2426 + case 0x0:
2427 + result = 0;
2428 + break;
2429 + case 0x1:
2430 + result = HSO_PORT_DIAG;
2431 + break;
2432 + case 0x2:
2433 + result = HSO_PORT_GPS;
2434 + break;
2435 + case 0x3:
2436 + result = HSO_PORT_GPS_CONTROL;
2437 + break;
2438 + case 0x4:
2439 + result = HSO_PORT_APP;
2440 + break;
2441 + case 0x5:
2442 + result = HSO_PORT_APP2;
2443 + break;
2444 + case 0x6:
2445 + result = HSO_PORT_CONTROL;
2446 + break;
2447 + case 0x7:
2448 + result = HSO_PORT_NETWORK;
2449 + break;
2450 + case 0x8:
2451 + result = HSO_PORT_MODEM;
2452 + break;
2453 + case 0x9:
2454 + result = HSO_PORT_MSD;
2455 + break;
2456 + case 0xa:
2457 + result = HSO_PORT_PCSC;
2458 + break;
2459 + case 0xb:
2460 + result = HSO_PORT_VOICE;
2461 + break;
2462 + default:
2463 + result = 0;
2464 + }
2465 +
2466 + if (result)
2467 + result |= HSO_INTF_BULK;
2468 +
2469 + if (config_data[16] & 0x1)
2470 + result |= HSO_INFO_CRC_BUG;
2471 +
2472 + return result;
2473 +}
2474 +
2475 +/* called once for each interface upon device insertion */
2476 +static int hso_probe(struct usb_interface *interface,
2477 + const struct usb_device_id *id)
2478 +{
2479 + int mux, i, if_num, port_spec;
2480 + unsigned char port_mask;
2481 + struct hso_device *hso_dev = NULL;
2482 + struct hso_shared_int *shared_int;
2483 + struct hso_device *tmp_dev = NULL;
2484 +
2485 + if_num = interface->altsetting->desc.bInterfaceNumber;
2486 +
2487 + /* Get the interface/port specification from either driver_info or from
2488 + * the device itself */
2489 + if (id->driver_info)
2490 + port_spec = ((u32 *)(id->driver_info))[if_num];
2491 + else
2492 + port_spec = hso_get_config_data(interface);
2493 +
2494 + if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2495 + dev_err(&interface->dev, "Not our interface\n");
2496 + return -ENODEV;
2497 + }
2498 + /* Check if we need to switch to alt interfaces prior to port
2499 + * configuration */
2500 + if (interface->num_altsetting > 1)
2501 + usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2502 + interface->needs_remote_wakeup = 1;
2503 +
2504 + /* Allocate new hso device(s) */
2505 + switch (port_spec & HSO_INTF_MASK) {
2506 + case HSO_INTF_MUX:
2507 + if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2508 + /* Create the network device */
2509 + if (!disable_net) {
2510 + hso_dev = hso_create_net_device(interface);
2511 + if (!hso_dev)
2512 + goto exit;
2513 + tmp_dev = hso_dev;
2514 + }
2515 + }
2516 +
2517 + if (hso_get_mux_ports(interface, &port_mask))
2518 + /* TODO: de-allocate everything */
2519 + goto exit;
2520 +
2521 + shared_int = hso_create_shared_int(interface);
2522 + if (!shared_int)
2523 + goto exit;
2524 +
2525 + for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2526 + if (port_mask & i) {
2527 + hso_dev = hso_create_mux_serial_device(
2528 + interface, i, shared_int);
2529 + if (!hso_dev)
2530 + goto exit;
2531 + }
2532 + }
2533 +
2534 + if (tmp_dev)
2535 + hso_dev = tmp_dev;
2536 + break;
2537 +
2538 + case HSO_INTF_BULK:
2539 + /* It's a regular bulk interface */
2540 + if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
2541 + && !disable_net)
2542 + hso_dev = hso_create_net_device(interface);
2543 + else
2544 + hso_dev =
2545 + hso_create_bulk_serial_device(interface, port_spec);
2546 + if (!hso_dev)
2547 + goto exit;
2548 + break;
2549 + default:
2550 + goto exit;
2551 + }
2552 +
2553 + usb_driver_claim_interface(&hso_driver, interface, hso_dev);
2554 +
2555 + /* save our data pointer in this device */
2556 + usb_set_intfdata(interface, hso_dev);
2557 +
2558 + /* done */
2559 + return 0;
2560 +exit:
2561 + hso_free_interface(interface);
2562 + return -ENODEV;
2563 +}
2564 +
2565 +/* device removed, cleaning up */
2566 +static void hso_disconnect(struct usb_interface *interface)
2567 +{
2568 + hso_free_interface(interface);
2569 +
2570 + /* remove reference of our private data */
2571 + usb_set_intfdata(interface, NULL);
2572 +
2573 + usb_driver_release_interface(&hso_driver, interface);
2574 +}
2575 +
2576 +static void async_get_intf(struct work_struct *data)
2577 +{
2578 + struct hso_device *hso_dev =
2579 + container_of(data, struct hso_device, async_get_intf);
2580 + usb_autopm_get_interface(hso_dev->interface);
2581 +}
2582 +
2583 +static void async_put_intf(struct work_struct *data)
2584 +{
2585 + struct hso_device *hso_dev =
2586 + container_of(data, struct hso_device, async_put_intf);
2587 + usb_autopm_put_interface(hso_dev->interface);
2588 +}
2589 +
2590 +static int hso_get_activity(struct hso_device *hso_dev)
2591 +{
2592 + if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2593 + if (!hso_dev->is_active) {
2594 + hso_dev->is_active = 1;
2595 + schedule_work(&hso_dev->async_get_intf);
2596 + }
2597 + }
2598 +
2599 + if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2600 + return -EAGAIN;
2601 +
2602 + usb_mark_last_busy(hso_dev->usb);
2603 +
2604 + return 0;
2605 +}
2606 +
2607 +static int hso_put_activity(struct hso_device *hso_dev)
2608 +{
2609 + if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
2610 + if (hso_dev->is_active) {
2611 + hso_dev->is_active = 0;
2612 + schedule_work(&hso_dev->async_put_intf);
2613 + return -EAGAIN;
2614 + }
2615 + }
2616 + hso_dev->is_active = 0;
2617 + return 0;
2618 +}
2619 +
2620 +/* called by kernel when we need to suspend device */
2621 +static int hso_suspend(struct usb_interface *iface, pm_message_t message)
2622 +{
2623 + int i, result;
2624 +
2625 + /* Stop all serial ports */
2626 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2627 + if (serial_table[i] && (serial_table[i]->interface == iface)) {
2628 + result = hso_stop_serial_device(serial_table[i]);
2629 + if (result)
2630 + goto out;
2631 + }
2632 + }
2633 +
2634 + /* Stop all network ports */
2635 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2636 + if (network_table[i] &&
2637 + (network_table[i]->interface == iface)) {
2638 + result = hso_stop_net_device(network_table[i]);
2639 + if (result)
2640 + goto out;
2641 + }
2642 + }
2643 +
2644 +out:
2645 + return 0;
2646 +}
2647 +
2648 +/* called by kernel when we need to resume device */
2649 +static int hso_resume(struct usb_interface *iface)
2650 +{
2651 + int i, result = 0;
2652 + struct hso_net *hso_net;
2653 +
2654 + /* Start all serial ports */
2655 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2656 + if (serial_table[i] && (serial_table[i]->interface == iface)) {
2657 + if (dev2ser(serial_table[i])->open_count) {
2658 + result =
2659 + hso_start_serial_device(serial_table[i], GFP_NOIO);
2660 + hso_kick_transmit(dev2ser(serial_table[i]));
2661 + if (result)
2662 + goto out;
2663 + }
2664 + }
2665 + }
2666 +
2667 + /* Start all network ports */
2668 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2669 + if (network_table[i] &&
2670 + (network_table[i]->interface == iface)) {
2671 + hso_net = dev2net(network_table[i]);
2672 + /* First transmit any lingering data, then restart the
2673 + * device. */
2674 + if (hso_net->skb_tx_buf) {
2675 + dev_dbg(&iface->dev,
2676 + "Transmitting lingering data\n");
2677 + hso_net_start_xmit(hso_net->skb_tx_buf,
2678 + hso_net->net);
2679 + }
2680 + result = hso_start_net_device(network_table[i]);
2681 + if (result)
2682 + goto out;
2683 + }
2684 + }
2685 +
2686 +out:
2687 + return result;
2688 +}
2689 +
2690 +static void hso_serial_ref_free(struct kref *ref)
2691 +{
2692 + struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
2693 +
2694 + hso_free_serial_device(hso_dev);
2695 +}
2696 +
2697 +static void hso_free_interface(struct usb_interface *interface)
2698 +{
2699 + struct hso_serial *hso_dev;
2700 + int i;
2701 +
2702 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
2703 + if (serial_table[i]
2704 + && (serial_table[i]->interface == interface)) {
2705 + hso_dev = dev2ser(serial_table[i]);
2706 + if (hso_dev->tty)
2707 + tty_hangup(hso_dev->tty);
2708 + mutex_lock(&hso_dev->parent->mutex);
2709 + hso_dev->parent->usb_gone = 1;
2710 + mutex_unlock(&hso_dev->parent->mutex);
2711 + kref_put(&serial_table[i]->ref, hso_serial_ref_free);
2712 + }
2713 + }
2714 +
2715 + for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2716 + if (network_table[i]
2717 + && (network_table[i]->interface == interface)) {
2718 + struct rfkill *rfk = dev2net(network_table[i])->rfkill;
2719 + /* hso_stop_net_device doesn't stop the net queue since
2720 + * traffic needs to start it again when suspended */
2721 + netif_stop_queue(dev2net(network_table[i])->net);
2722 + hso_stop_net_device(network_table[i]);
2723 + cancel_work_sync(&network_table[i]->async_put_intf);
2724 + cancel_work_sync(&network_table[i]->async_get_intf);
2725 + if(rfk)
2726 + rfkill_unregister(rfk);
2727 + hso_free_net_device(network_table[i]);
2728 + }
2729 + }
2730 +}
2731 +
2732 +/* Helper functions */
2733 +
2734 +/* Get the endpoint ! */
2735 +static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
2736 + int type, int dir)
2737 +{
2738 + int i;
2739 + struct usb_host_interface *iface = intf->cur_altsetting;
2740 + struct usb_endpoint_descriptor *endp;
2741 +
2742 + for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2743 + endp = &iface->endpoint[i].desc;
2744 + if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
2745 + ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
2746 + return endp;
2747 + }
2748 +
2749 + return NULL;
2750 +}
2751 +
2752 +/* Get the byte that describes which ports are enabled */
2753 +static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
2754 +{
2755 + int i;
2756 + struct usb_host_interface *iface = intf->cur_altsetting;
2757 +
2758 + if (iface->extralen == 3) {
2759 + *ports = iface->extra[2];
2760 + return 0;
2761 + }
2762 +
2763 + for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2764 + if (iface->endpoint[i].extralen == 3) {
2765 + *ports = iface->endpoint[i].extra[2];
2766 + return 0;
2767 + }
2768 + }
2769 +
2770 + return -1;
2771 +}
2772 +
2773 +/* interrupt urb needs to be submitted, used for serial read of muxed port */
2774 +static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
2775 + struct usb_device *usb, gfp_t gfp)
2776 +{
2777 + int result;
2778 +
2779 + usb_fill_int_urb(shared_int->shared_intr_urb, usb,
2780 + usb_rcvintpipe(usb,
2781 + shared_int->intr_endp->bEndpointAddress & 0x7F),
2782 + shared_int->shared_intr_buf,
2783 + shared_int->intr_endp->wMaxPacketSize,
2784 + intr_callback, shared_int,
2785 + shared_int->intr_endp->bInterval);
2786 +
2787 + result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
2788 + if (result)
2789 + dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
2790 + result);
2791 +
2792 + return result;
2793 +}
2794 +
2795 +/* operations setup of the serial interface */
2796 +static struct tty_operations hso_serial_ops = {
2797 + .open = hso_serial_open,
2798 + .close = hso_serial_close,
2799 + .write = hso_serial_write,
2800 + .write_room = hso_serial_write_room,
2801 + .set_termios = hso_serial_set_termios,
2802 + .chars_in_buffer = hso_serial_chars_in_buffer,
2803 + .tiocmget = hso_serial_tiocmget,
2804 + .tiocmset = hso_serial_tiocmset,
2805 +};
2806 +
2807 +static struct usb_driver hso_driver = {
2808 + .name = driver_name,
2809 + .probe = hso_probe,
2810 + .disconnect = hso_disconnect,
2811 + .id_table = hso_ids,
2812 + .suspend = hso_suspend,
2813 + .resume = hso_resume,
2814 + .supports_autosuspend = 1,
2815 +};
2816 +
2817 +static int __init hso_init(void)
2818 +{
2819 + int i;
2820 + int result;
2821 +
2822 + /* put it in the log */
2823 + printk(KERN_INFO "hso: %s\n", version);
2824 +
2825 + /* Initialise the serial table semaphore and table */
2826 + spin_lock_init(&serial_table_lock);
2827 + for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
2828 + serial_table[i] = NULL;
2829 +
2830 + /* allocate our driver using the proper amount of supported minors */
2831 + tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
2832 + if (!tty_drv)
2833 + return -ENOMEM;
2834 +
2835 + /* fill in all needed values */
2836 + tty_drv->magic = TTY_DRIVER_MAGIC;
2837 + tty_drv->owner = THIS_MODULE;
2838 + tty_drv->driver_name = driver_name;
2839 + tty_drv->name = tty_filename;
2840 +
2841 + /* if major number is provided as parameter, use that one */
2842 + if (tty_major)
2843 + tty_drv->major = tty_major;
2844 +
2845 + tty_drv->minor_start = 0;
2846 + tty_drv->num = HSO_SERIAL_TTY_MINORS;
2847 + tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2848 + tty_drv->subtype = SERIAL_TYPE_NORMAL;
2849 + tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2850 + tty_drv->init_termios = tty_std_termios;
2851 + tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2852 + tty_drv->termios = hso_serial_termios;
2853 + tty_drv->termios_locked = hso_serial_termios_locked;
2854 + tty_set_operations(tty_drv, &hso_serial_ops);
2855 +
2856 + /* register the tty driver */
2857 + result = tty_register_driver(tty_drv);
2858 + if (result) {
2859 + printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
2860 + __func__, result);
2861 + return result;
2862 + }
2863 +
2864 + /* register this module as an usb driver */
2865 + result = usb_register(&hso_driver);
2866 + if (result) {
2867 + printk(KERN_ERR "Could not register hso driver? error: %d\n",
2868 + result);
2869 + /* cleanup serial interface */
2870 + tty_unregister_driver(tty_drv);
2871 + return result;
2872 + }
2873 +
2874 + /* done */
2875 + return 0;
2876 +}
2877 +
2878 +static void __exit hso_exit(void)
2879 +{
2880 + printk(KERN_INFO "hso: unloaded\n");
2881 +
2882 + tty_unregister_driver(tty_drv);
2883 + /* deregister the usb driver */
2884 + usb_deregister(&hso_driver);
2885 +}
2886 +
2887 +/* Module definitions */
2888 +module_init(hso_init);
2889 +module_exit(hso_exit);
2890 +
2891 +MODULE_AUTHOR(MOD_AUTHOR);
2892 +MODULE_DESCRIPTION(MOD_DESCRIPTION);
2893 +MODULE_LICENSE(MOD_LICENSE);
2894 +MODULE_INFO(Version, DRIVER_VERSION);
2895 +
2896 +/* change the debug level (eg: insmod hso.ko debug=0x04) */
2897 +MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
2898 +module_param(debug, int, S_IRUGO | S_IWUSR);
2899 +
2900 +/* set the major tty number (eg: insmod hso.ko tty_major=245) */
2901 +MODULE_PARM_DESC(tty_major, "Set the major tty number");
2902 +module_param(tty_major, int, S_IRUGO | S_IWUSR);
2903 +
2904 +/* disable network interface (eg: insmod hso.ko disable_net=1) */
2905 +MODULE_PARM_DESC(disable_net, "Disable the network interface");
2906 +module_param(disable_net, int, S_IRUGO | S_IWUSR);
This page took 0.164898 seconds and 5 git commands to generate.