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