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