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