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