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