blob: a4272ed62da865170cd9de7622e7c48875ed9f5c [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
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -070076#define MOD_AUTHOR "Option Wireless"
77#define MOD_DESCRIPTION "USB High Speed Option driver"
78#define MOD_LICENSE "GPL"
79
80#define HSO_MAX_NET_DEVICES 10
81#define HSO__MAX_MTU 2048
82#define DEFAULT_MTU 1500
83#define DEFAULT_MRU 1500
84
85#define CTRL_URB_RX_SIZE 1024
86#define CTRL_URB_TX_SIZE 64
87
88#define BULK_URB_RX_SIZE 4096
89#define BULK_URB_TX_SIZE 8192
90
91#define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
92#define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
93#define MUX_BULK_RX_BUF_COUNT 4
94#define USB_TYPE_OPTION_VENDOR 0x20
95
96/* These definitions are used with the struct hso_net flags element */
97/* - use *_bit operations on it. (bit indices not values.) */
98#define HSO_NET_RUNNING 0
99
100#define HSO_NET_TX_TIMEOUT (HZ*10)
101
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700102#define HSO_SERIAL_MAGIC 0x48534f31
103
104/* Number of ttys to handle */
105#define HSO_SERIAL_TTY_MINORS 256
106
107#define MAX_RX_URBS 2
108
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700109/*****************************************************************************/
110/* Debugging functions */
111/*****************************************************************************/
112#define D__(lvl_, fmt, arg...) \
113 do { \
114 printk(lvl_ "[%d:%s]: " fmt "\n", \
115 __LINE__, __func__, ## arg); \
116 } while (0)
117
118#define D_(lvl, args...) \
119 do { \
120 if (lvl & debug) \
121 D__(KERN_INFO, args); \
122 } while (0)
123
124#define D1(args...) D_(0x01, ##args)
125#define D2(args...) D_(0x02, ##args)
126#define D3(args...) D_(0x04, ##args)
127#define D4(args...) D_(0x08, ##args)
128#define D5(args...) D_(0x10, ##args)
129
130/*****************************************************************************/
131/* Enumerators */
132/*****************************************************************************/
133enum pkt_parse_state {
134 WAIT_IP,
135 WAIT_DATA,
136 WAIT_SYNC
137};
138
139/*****************************************************************************/
140/* Structs */
141/*****************************************************************************/
142
143struct hso_shared_int {
144 struct usb_endpoint_descriptor *intr_endp;
145 void *shared_intr_buf;
146 struct urb *shared_intr_urb;
147 struct usb_device *usb;
148 int use_count;
149 int ref_count;
150 struct mutex shared_int_lock;
151};
152
153struct hso_net {
154 struct hso_device *parent;
155 struct net_device *net;
156 struct rfkill *rfkill;
157
158 struct usb_endpoint_descriptor *in_endp;
159 struct usb_endpoint_descriptor *out_endp;
160
161 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
162 struct urb *mux_bulk_tx_urb;
163 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
164 void *mux_bulk_tx_buf;
165
166 struct sk_buff *skb_rx_buf;
167 struct sk_buff *skb_tx_buf;
168
169 enum pkt_parse_state rx_parse_state;
170 spinlock_t net_lock;
171
172 unsigned short rx_buf_size;
173 unsigned short rx_buf_missing;
174 struct iphdr rx_ip_hdr;
175
176 unsigned long flags;
177};
178
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200179enum rx_ctrl_state{
180 RX_IDLE,
181 RX_SENT,
182 RX_PENDING
183};
184
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000185#define BM_REQUEST_TYPE (0xa1)
186#define B_NOTIFICATION (0x20)
187#define W_VALUE (0x0)
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000188#define W_LENGTH (0x2)
189
190#define B_OVERRUN (0x1<<6)
191#define B_PARITY (0x1<<5)
192#define B_FRAMING (0x1<<4)
193#define B_RING_SIGNAL (0x1<<3)
194#define B_BREAK (0x1<<2)
195#define B_TX_CARRIER (0x1<<1)
196#define B_RX_CARRIER (0x1<<0)
197
198struct hso_serial_state_notification {
199 u8 bmRequestType;
200 u8 bNotification;
201 u16 wValue;
202 u16 wIndex;
203 u16 wLength;
204 u16 UART_state_bitmap;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000205} __packed;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000206
207struct hso_tiocmget {
208 struct mutex mutex;
209 wait_queue_head_t waitq;
210 int intr_completed;
211 struct usb_endpoint_descriptor *endp;
212 struct urb *urb;
213 struct hso_serial_state_notification serial_state_notification;
214 u16 prev_UART_state_bitmap;
215 struct uart_icount icount;
216};
217
218
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700219struct hso_serial {
220 struct hso_device *parent;
221 int magic;
222 u8 minor;
223
224 struct hso_shared_int *shared_int;
225
226 /* rx/tx urb could be either a bulk urb or a control urb depending
227 on which serial port it is used on. */
228 struct urb *rx_urb[MAX_RX_URBS];
229 u8 num_rx_urbs;
230 u8 *rx_data[MAX_RX_URBS];
231 u16 rx_data_length; /* should contain allocated length */
232
233 struct urb *tx_urb;
234 u8 *tx_data;
235 u8 *tx_buffer;
236 u16 tx_data_length; /* should contain allocated length */
237 u16 tx_data_count;
238 u16 tx_buffer_count;
239 struct usb_ctrlrequest ctrl_req_tx;
240 struct usb_ctrlrequest ctrl_req_rx;
241
242 struct usb_endpoint_descriptor *in_endp;
243 struct usb_endpoint_descriptor *out_endp;
244
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200245 enum rx_ctrl_state rx_state;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700246 u8 rts_state;
247 u8 dtr_state;
248 unsigned tx_urb_used:1;
249
Jiri Slaby5ce76e72012-04-02 13:54:05 +0200250 struct tty_port port;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700251 /* from usb_serial_port */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700252 spinlock_t serial_lock;
253
254 int (*write_data) (struct hso_serial *serial);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000255 struct hso_tiocmget *tiocmget;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200256 /* Hacks required to get flow control
257 * working on the serial receive buffers
258 * so as not to drop characters on the floor.
259 */
260 int curr_rx_urb_idx;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200261 u8 rx_urb_filled[MAX_RX_URBS];
262 struct tasklet_struct unthrottle_tasklet;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700263};
264
265struct hso_device {
266 union {
267 struct hso_serial *dev_serial;
268 struct hso_net *dev_net;
269 } port_data;
270
271 u32 port_spec;
272
273 u8 is_active;
274 u8 usb_gone;
275 struct work_struct async_get_intf;
276 struct work_struct async_put_intf;
Jan Dumon68a351c2010-01-05 04:52:13 +0000277 struct work_struct reset_device;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700278
279 struct usb_device *usb;
280 struct usb_interface *interface;
281
282 struct device *dev;
283 struct kref ref;
David S. Millerab153d82008-11-25 03:52:46 -0800284 struct mutex mutex;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700285};
286
287/* Type of interface */
288#define HSO_INTF_MASK 0xFF00
289#define HSO_INTF_MUX 0x0100
290#define HSO_INTF_BULK 0x0200
291
292/* Type of port */
293#define HSO_PORT_MASK 0xFF
294#define HSO_PORT_NO_PORT 0x0
295#define HSO_PORT_CONTROL 0x1
296#define HSO_PORT_APP 0x2
297#define HSO_PORT_GPS 0x3
298#define HSO_PORT_PCSC 0x4
299#define HSO_PORT_APP2 0x5
300#define HSO_PORT_GPS_CONTROL 0x6
301#define HSO_PORT_MSD 0x7
302#define HSO_PORT_VOICE 0x8
303#define HSO_PORT_DIAG2 0x9
304#define HSO_PORT_DIAG 0x10
305#define HSO_PORT_MODEM 0x11
306#define HSO_PORT_NETWORK 0x12
307
308/* Additional device info */
309#define HSO_INFO_MASK 0xFF000000
310#define HSO_INFO_CRC_BUG 0x01000000
311
312/*****************************************************************************/
313/* Prototypes */
314/*****************************************************************************/
315/* Serial driver functions */
Alan Cox20b9d172011-02-14 16:26:50 +0000316static int hso_serial_tiocmset(struct tty_struct *tty,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700317 unsigned int set, unsigned int clear);
318static void ctrl_callback(struct urb *urb);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200319static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700320static void hso_kick_transmit(struct hso_serial *serial);
321/* Helper functions */
322static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
323 struct usb_device *usb, gfp_t gfp);
Jan Dumon68a351c2010-01-05 04:52:13 +0000324static void handle_usb_error(int status, const char *function,
325 struct hso_device *hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700326static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
327 int type, int dir);
328static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
329static void hso_free_interface(struct usb_interface *intf);
330static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
331static int hso_stop_serial_device(struct hso_device *hso_dev);
332static int hso_start_net_device(struct hso_device *hso_dev);
333static void hso_free_shared_int(struct hso_shared_int *shared_int);
334static int hso_stop_net_device(struct hso_device *hso_dev);
335static void hso_serial_ref_free(struct kref *ref);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200336static void hso_std_serial_read_bulk_callback(struct urb *urb);
337static int hso_mux_serial_read(struct hso_serial *serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700338static void async_get_intf(struct work_struct *data);
339static void async_put_intf(struct work_struct *data);
340static int hso_put_activity(struct hso_device *hso_dev);
341static int hso_get_activity(struct hso_device *hso_dev);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000342static void tiocmget_intr_callback(struct urb *urb);
Jan Dumon68a351c2010-01-05 04:52:13 +0000343static void reset_device(struct work_struct *data);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700344/*****************************************************************************/
345/* Helping functions */
346/*****************************************************************************/
347
348/* #define DEBUG */
349
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700350static inline struct hso_net *dev2net(struct hso_device *hso_dev)
351{
352 return hso_dev->port_data.dev_net;
353}
354
355static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
356{
357 return hso_dev->port_data.dev_serial;
358}
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700359
360/* Debugging functions */
361#ifdef DEBUG
362static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
363 unsigned int len)
364{
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700365 static char name[255];
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700366
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700367 sprintf(name, "hso[%d:%s]", line_count, func_name);
368 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700369}
370
371#define DUMP(buf_, len_) \
Antti Kaijanmäki9ce673d2009-11-23 10:54:24 -0800372 dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700373
374#define DUMP1(buf_, len_) \
375 do { \
376 if (0x01 & debug) \
377 DUMP(buf_, len_); \
378 } while (0)
379#else
380#define DUMP(buf_, len_)
381#define DUMP1(buf_, len_)
382#endif
383
384/* module parameters */
385static int debug;
386static int tty_major;
387static int disable_net;
388
389/* driver info */
390static const char driver_name[] = "hso";
391static const char tty_filename[] = "ttyHS";
Filip Aben242647b2010-07-12 21:21:27 -0700392static const char *version = __FILE__ ": " MOD_AUTHOR;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700393/* the usb driver itself (registered in hso_init) */
394static struct usb_driver hso_driver;
395/* serial structures */
396static struct tty_driver *tty_drv;
397static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
398static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
399static spinlock_t serial_table_lock;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700400
401static const s32 default_port_spec[] = {
402 HSO_INTF_MUX | HSO_PORT_NETWORK,
403 HSO_INTF_BULK | HSO_PORT_DIAG,
404 HSO_INTF_BULK | HSO_PORT_MODEM,
405 0
406};
407
408static const s32 icon321_port_spec[] = {
409 HSO_INTF_MUX | HSO_PORT_NETWORK,
410 HSO_INTF_BULK | HSO_PORT_DIAG2,
411 HSO_INTF_BULK | HSO_PORT_MODEM,
412 HSO_INTF_BULK | HSO_PORT_DIAG,
413 0
414};
415
416#define default_port_device(vendor, product) \
417 USB_DEVICE(vendor, product), \
418 .driver_info = (kernel_ulong_t)default_port_spec
419
420#define icon321_port_device(vendor, product) \
421 USB_DEVICE(vendor, product), \
422 .driver_info = (kernel_ulong_t)icon321_port_spec
423
424/* list of devices we support */
425static const struct usb_device_id hso_ids[] = {
426 {default_port_device(0x0af0, 0x6711)},
427 {default_port_device(0x0af0, 0x6731)},
428 {default_port_device(0x0af0, 0x6751)},
429 {default_port_device(0x0af0, 0x6771)},
430 {default_port_device(0x0af0, 0x6791)},
431 {default_port_device(0x0af0, 0x6811)},
432 {default_port_device(0x0af0, 0x6911)},
433 {default_port_device(0x0af0, 0x6951)},
434 {default_port_device(0x0af0, 0x6971)},
435 {default_port_device(0x0af0, 0x7011)},
436 {default_port_device(0x0af0, 0x7031)},
437 {default_port_device(0x0af0, 0x7051)},
438 {default_port_device(0x0af0, 0x7071)},
439 {default_port_device(0x0af0, 0x7111)},
440 {default_port_device(0x0af0, 0x7211)},
441 {default_port_device(0x0af0, 0x7251)},
442 {default_port_device(0x0af0, 0x7271)},
443 {default_port_device(0x0af0, 0x7311)},
444 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
445 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
446 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
Denis Joseph Barrow95eacee2008-08-19 18:07:52 -0700447 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700448 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
449 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
Filip Aben67dd8242009-02-03 15:13:26 -0800450 {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700451 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
452 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
453 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
Denis Joseph Barrowbab04c32008-11-25 00:26:12 -0800454 {USB_DEVICE(0x0af0, 0x7701)},
Jan Dumonec157932010-01-05 04:50:31 +0000455 {USB_DEVICE(0x0af0, 0x7706)},
Denis Joseph Barrowbab04c32008-11-25 00:26:12 -0800456 {USB_DEVICE(0x0af0, 0x7801)},
457 {USB_DEVICE(0x0af0, 0x7901)},
Jan Dumonec157932010-01-05 04:50:31 +0000458 {USB_DEVICE(0x0af0, 0x7A01)},
459 {USB_DEVICE(0x0af0, 0x7A05)},
Jan Dumon9961d8422009-04-02 01:16:44 -0700460 {USB_DEVICE(0x0af0, 0x8200)},
461 {USB_DEVICE(0x0af0, 0x8201)},
Jan Dumonec157932010-01-05 04:50:31 +0000462 {USB_DEVICE(0x0af0, 0x8300)},
463 {USB_DEVICE(0x0af0, 0x8302)},
464 {USB_DEVICE(0x0af0, 0x8304)},
465 {USB_DEVICE(0x0af0, 0x8400)},
Filip Abendd7496f2010-05-25 16:09:23 -0700466 {USB_DEVICE(0x0af0, 0x8600)},
467 {USB_DEVICE(0x0af0, 0x8800)},
468 {USB_DEVICE(0x0af0, 0x8900)},
Filip Aben5c7bf2f2010-08-03 05:36:41 +0000469 {USB_DEVICE(0x0af0, 0x9000)},
Jan Dumon9961d8422009-04-02 01:16:44 -0700470 {USB_DEVICE(0x0af0, 0xd035)},
Filip Aben67dd8242009-02-03 15:13:26 -0800471 {USB_DEVICE(0x0af0, 0xd055)},
Jan Dumon9961d8422009-04-02 01:16:44 -0700472 {USB_DEVICE(0x0af0, 0xd155)},
473 {USB_DEVICE(0x0af0, 0xd255)},
474 {USB_DEVICE(0x0af0, 0xd057)},
475 {USB_DEVICE(0x0af0, 0xd157)},
476 {USB_DEVICE(0x0af0, 0xd257)},
477 {USB_DEVICE(0x0af0, 0xd357)},
Jan Dumonec157932010-01-05 04:50:31 +0000478 {USB_DEVICE(0x0af0, 0xd058)},
479 {USB_DEVICE(0x0af0, 0xc100)},
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700480 {}
481};
482MODULE_DEVICE_TABLE(usb, hso_ids);
483
484/* Sysfs attribute */
485static ssize_t hso_sysfs_show_porttype(struct device *dev,
486 struct device_attribute *attr,
487 char *buf)
488{
Greg Kroah-Hartman1aec5bd2009-04-30 12:19:31 +0000489 struct hso_device *hso_dev = dev_get_drvdata(dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700490 char *port_name;
491
492 if (!hso_dev)
493 return 0;
494
495 switch (hso_dev->port_spec & HSO_PORT_MASK) {
496 case HSO_PORT_CONTROL:
497 port_name = "Control";
498 break;
499 case HSO_PORT_APP:
500 port_name = "Application";
501 break;
502 case HSO_PORT_APP2:
503 port_name = "Application2";
504 break;
505 case HSO_PORT_GPS:
506 port_name = "GPS";
507 break;
508 case HSO_PORT_GPS_CONTROL:
509 port_name = "GPS Control";
510 break;
511 case HSO_PORT_PCSC:
512 port_name = "PCSC";
513 break;
514 case HSO_PORT_DIAG:
515 port_name = "Diagnostic";
516 break;
517 case HSO_PORT_DIAG2:
518 port_name = "Diagnostic2";
519 break;
520 case HSO_PORT_MODEM:
521 port_name = "Modem";
522 break;
523 case HSO_PORT_NETWORK:
524 port_name = "Network";
525 break;
526 default:
527 port_name = "Unknown";
528 break;
529 }
530
531 return sprintf(buf, "%s\n", port_name);
532}
533static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
534
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200535static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
536{
537 int idx;
538
539 for (idx = 0; idx < serial->num_rx_urbs; idx++)
540 if (serial->rx_urb[idx] == urb)
541 return idx;
542 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
543 return -1;
544}
545
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700546/* converts mux value to a port spec value */
547static u32 hso_mux_to_port(int mux)
548{
549 u32 result;
550
551 switch (mux) {
552 case 0x1:
553 result = HSO_PORT_CONTROL;
554 break;
555 case 0x2:
556 result = HSO_PORT_APP;
557 break;
558 case 0x4:
559 result = HSO_PORT_PCSC;
560 break;
561 case 0x8:
562 result = HSO_PORT_GPS;
563 break;
564 case 0x10:
565 result = HSO_PORT_APP2;
566 break;
567 default:
568 result = HSO_PORT_NO_PORT;
569 }
570 return result;
571}
572
573/* converts port spec value to a mux value */
574static u32 hso_port_to_mux(int port)
575{
576 u32 result;
577
578 switch (port & HSO_PORT_MASK) {
579 case HSO_PORT_CONTROL:
580 result = 0x0;
581 break;
582 case HSO_PORT_APP:
583 result = 0x1;
584 break;
585 case HSO_PORT_PCSC:
586 result = 0x2;
587 break;
588 case HSO_PORT_GPS:
589 result = 0x3;
590 break;
591 case HSO_PORT_APP2:
592 result = 0x4;
593 break;
594 default:
595 result = 0x0;
596 }
597 return result;
598}
599
600static struct hso_serial *get_serial_by_shared_int_and_type(
601 struct hso_shared_int *shared_int,
602 int mux)
603{
604 int i, port;
605
606 port = hso_mux_to_port(mux);
607
608 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
Joe Perches8e95a202009-12-03 07:58:21 +0000609 if (serial_table[i] &&
610 (dev2ser(serial_table[i])->shared_int == shared_int) &&
611 ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700612 return dev2ser(serial_table[i]);
613 }
614 }
615
616 return NULL;
617}
618
619static struct hso_serial *get_serial_by_index(unsigned index)
620{
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700621 struct hso_serial *serial = NULL;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700622 unsigned long flags;
623
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700624 spin_lock_irqsave(&serial_table_lock, flags);
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700625 if (serial_table[index])
626 serial = dev2ser(serial_table[index]);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700627 spin_unlock_irqrestore(&serial_table_lock, flags);
628
629 return serial;
630}
631
632static int get_free_serial_index(void)
633{
634 int index;
635 unsigned long flags;
636
637 spin_lock_irqsave(&serial_table_lock, flags);
638 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
639 if (serial_table[index] == NULL) {
640 spin_unlock_irqrestore(&serial_table_lock, flags);
641 return index;
642 }
643 }
644 spin_unlock_irqrestore(&serial_table_lock, flags);
645
646 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
647 return -1;
648}
649
650static void set_serial_by_index(unsigned index, struct hso_serial *serial)
651{
652 unsigned long flags;
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700653
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700654 spin_lock_irqsave(&serial_table_lock, flags);
655 if (serial)
656 serial_table[index] = serial->parent;
657 else
658 serial_table[index] = NULL;
659 spin_unlock_irqrestore(&serial_table_lock, flags);
660}
661
Jan Dumon68a351c2010-01-05 04:52:13 +0000662static void handle_usb_error(int status, const char *function,
663 struct hso_device *hso_dev)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700664{
665 char *explanation;
666
667 switch (status) {
668 case -ENODEV:
669 explanation = "no device";
670 break;
671 case -ENOENT:
672 explanation = "endpoint not enabled";
673 break;
674 case -EPIPE:
675 explanation = "endpoint stalled";
676 break;
677 case -ENOSPC:
678 explanation = "not enough bandwidth";
679 break;
680 case -ESHUTDOWN:
681 explanation = "device disabled";
682 break;
683 case -EHOSTUNREACH:
684 explanation = "device suspended";
685 break;
686 case -EINVAL:
687 case -EAGAIN:
688 case -EFBIG:
689 case -EMSGSIZE:
690 explanation = "internal error";
691 break;
Jan Dumon68a351c2010-01-05 04:52:13 +0000692 case -EILSEQ:
693 case -EPROTO:
694 case -ETIME:
695 case -ETIMEDOUT:
696 explanation = "protocol error";
697 if (hso_dev)
698 schedule_work(&hso_dev->reset_device);
699 break;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700700 default:
701 explanation = "unknown status";
702 break;
703 }
Jan Dumon68a351c2010-01-05 04:52:13 +0000704
705 /* log a meaningful explanation of an USB status */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700706 D1("%s: received USB status - %s (%d)", function, explanation, status);
707}
708
709/* Network interface functions */
710
711/* called when net interface is brought up by ifconfig */
712static int hso_net_open(struct net_device *net)
713{
714 struct hso_net *odev = netdev_priv(net);
715 unsigned long flags = 0;
716
717 if (!odev) {
718 dev_err(&net->dev, "No net device !\n");
719 return -ENODEV;
720 }
721
722 odev->skb_tx_buf = NULL;
723
724 /* setup environment */
725 spin_lock_irqsave(&odev->net_lock, flags);
726 odev->rx_parse_state = WAIT_IP;
727 odev->rx_buf_size = 0;
728 odev->rx_buf_missing = sizeof(struct iphdr);
729 spin_unlock_irqrestore(&odev->net_lock, flags);
730
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700731 /* We are up and running. */
732 set_bit(HSO_NET_RUNNING, &odev->flags);
Oliver Neukum889bd9b2008-12-18 03:57:35 +0000733 hso_start_net_device(odev->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700734
735 /* Tell the kernel we are ready to start receiving from it */
736 netif_start_queue(net);
737
738 return 0;
739}
740
741/* called when interface is brought down by ifconfig */
742static int hso_net_close(struct net_device *net)
743{
744 struct hso_net *odev = netdev_priv(net);
745
746 /* we don't need the queue anymore */
747 netif_stop_queue(net);
748 /* no longer running */
749 clear_bit(HSO_NET_RUNNING, &odev->flags);
750
751 hso_stop_net_device(odev->parent);
752
753 /* done */
754 return 0;
755}
756
757/* USB tells is xmit done, we should start the netqueue again */
758static void write_bulk_callback(struct urb *urb)
759{
760 struct hso_net *odev = urb->context;
761 int status = urb->status;
762
763 /* Sanity check */
764 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
765 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
766 return;
767 }
768
769 /* Do we still have a valid kernel network device? */
770 if (!netif_device_present(odev->net)) {
771 dev_err(&urb->dev->dev, "%s: net device not present\n",
772 __func__);
773 return;
774 }
775
776 /* log status, but don't act on it, we don't need to resubmit anything
777 * anyhow */
778 if (status)
Jan Dumon68a351c2010-01-05 04:52:13 +0000779 handle_usb_error(status, __func__, odev->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700780
781 hso_put_activity(odev->parent);
782
783 /* Tell the network interface we are ready for another frame */
784 netif_wake_queue(odev->net);
785}
786
787/* called by kernel when we need to transmit a packet */
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000788static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
789 struct net_device *net)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700790{
791 struct hso_net *odev = netdev_priv(net);
792 int result;
793
794 /* Tell the kernel, "No more frames 'til we are done with this one." */
795 netif_stop_queue(net);
796 if (hso_get_activity(odev->parent) == -EAGAIN) {
797 odev->skb_tx_buf = skb;
Patrick McHardy6ed10652009-06-23 06:03:08 +0000798 return NETDEV_TX_OK;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700799 }
800
801 /* log if asked */
802 DUMP1(skb->data, skb->len);
803 /* Copy it from kernel memory to OUR memory */
804 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
805 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
806
807 /* Fill in the URB for shipping it out. */
808 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
809 odev->parent->usb,
810 usb_sndbulkpipe(odev->parent->usb,
811 odev->out_endp->
812 bEndpointAddress & 0x7F),
813 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
814 odev);
815
816 /* Deal with the Zero Length packet problem, I hope */
817 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
818
819 /* Send the URB on its merry way. */
820 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
821 if (result) {
822 dev_warn(&odev->parent->interface->dev,
Jan Dumon8a5c9c42010-01-05 04:53:00 +0000823 "failed mux_bulk_tx_urb %d\n", result);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700824 net->stats.tx_errors++;
825 netif_start_queue(net);
826 } else {
827 net->stats.tx_packets++;
828 net->stats.tx_bytes += skb->len;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700829 }
830 dev_kfree_skb(skb);
831 /* we're done */
Patrick McHardy5b2c4b92009-06-12 06:14:36 +0000832 return NETDEV_TX_OK;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700833}
834
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700835static const struct ethtool_ops ops = {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700836 .get_link = ethtool_op_get_link
837};
838
839/* called when a packet did not ack after watchdogtimeout */
840static void hso_net_tx_timeout(struct net_device *net)
841{
842 struct hso_net *odev = netdev_priv(net);
843
844 if (!odev)
845 return;
846
847 /* Tell syslog we are hosed. */
848 dev_warn(&net->dev, "Tx timed out.\n");
849
850 /* Tear the waiting frame off the list */
Joe Perches8e95a202009-12-03 07:58:21 +0000851 if (odev->mux_bulk_tx_urb &&
852 (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700853 usb_unlink_urb(odev->mux_bulk_tx_urb);
854
855 /* Update statistics */
856 net->stats.tx_errors++;
857}
858
859/* make a real packet from the received USB buffer */
860static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
861 unsigned int count, unsigned char is_eop)
862{
863 unsigned short temp_bytes;
864 unsigned short buffer_offset = 0;
865 unsigned short frame_len;
866 unsigned char *tmp_rx_buf;
867
868 /* log if needed */
869 D1("Rx %d bytes", count);
870 DUMP(ip_pkt, min(128, (int)count));
871
872 while (count) {
873 switch (odev->rx_parse_state) {
874 case WAIT_IP:
875 /* waiting for IP header. */
876 /* wanted bytes - size of ip header */
877 temp_bytes =
878 (count <
879 odev->rx_buf_missing) ? count : odev->
880 rx_buf_missing;
881
882 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
883 odev->rx_buf_size, ip_pkt + buffer_offset,
884 temp_bytes);
885
886 odev->rx_buf_size += temp_bytes;
887 buffer_offset += temp_bytes;
888 odev->rx_buf_missing -= temp_bytes;
889 count -= temp_bytes;
890
891 if (!odev->rx_buf_missing) {
892 /* header is complete allocate an sk_buffer and
893 * continue to WAIT_DATA */
894 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
895
896 if ((frame_len > DEFAULT_MRU) ||
897 (frame_len < sizeof(struct iphdr))) {
898 dev_err(&odev->net->dev,
899 "Invalid frame (%d) length\n",
900 frame_len);
901 odev->rx_parse_state = WAIT_SYNC;
902 continue;
903 }
904 /* Allocate an sk_buff */
Paulius Zaleckasd65a68a2009-06-04 05:50:29 +0000905 odev->skb_rx_buf = netdev_alloc_skb(odev->net,
906 frame_len);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700907 if (!odev->skb_rx_buf) {
908 /* We got no receive buffer. */
909 D1("could not allocate memory");
910 odev->rx_parse_state = WAIT_SYNC;
911 return;
912 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700913
914 /* Copy what we got so far. make room for iphdr
915 * after tail. */
916 tmp_rx_buf =
917 skb_put(odev->skb_rx_buf,
918 sizeof(struct iphdr));
919 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
920 sizeof(struct iphdr));
921
922 /* ETH_HLEN */
923 odev->rx_buf_size = sizeof(struct iphdr);
924
925 /* Filip actually use .tot_len */
926 odev->rx_buf_missing =
927 frame_len - sizeof(struct iphdr);
928 odev->rx_parse_state = WAIT_DATA;
929 }
930 break;
931
932 case WAIT_DATA:
933 temp_bytes = (count < odev->rx_buf_missing)
934 ? count : odev->rx_buf_missing;
935
936 /* Copy the rest of the bytes that are left in the
937 * buffer into the waiting sk_buf. */
938 /* Make room for temp_bytes after tail. */
939 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
940 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
941
942 odev->rx_buf_missing -= temp_bytes;
943 count -= temp_bytes;
944 buffer_offset += temp_bytes;
945 odev->rx_buf_size += temp_bytes;
946 if (!odev->rx_buf_missing) {
947 /* Packet is complete. Inject into stack. */
948 /* We have IP packet here */
Harvey Harrison09640e62009-02-01 00:45:17 -0800949 odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700950 skb_reset_mac_header(odev->skb_rx_buf);
951
952 /* Ship it off to the kernel */
953 netif_rx(odev->skb_rx_buf);
954 /* No longer our buffer. */
955 odev->skb_rx_buf = NULL;
956
957 /* update out statistics */
958 odev->net->stats.rx_packets++;
959
960 odev->net->stats.rx_bytes += odev->rx_buf_size;
961
962 odev->rx_buf_size = 0;
963 odev->rx_buf_missing = sizeof(struct iphdr);
964 odev->rx_parse_state = WAIT_IP;
965 }
966 break;
967
968 case WAIT_SYNC:
969 D1(" W_S");
970 count = 0;
971 break;
972 default:
973 D1(" ");
974 count--;
975 break;
976 }
977 }
978
979 /* Recovery mechanism for WAIT_SYNC state. */
980 if (is_eop) {
981 if (odev->rx_parse_state == WAIT_SYNC) {
982 odev->rx_parse_state = WAIT_IP;
983 odev->rx_buf_size = 0;
984 odev->rx_buf_missing = sizeof(struct iphdr);
985 }
986 }
987}
988
Joe Perches5591c752010-12-21 02:16:09 -0800989static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
990{
991 static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
992 u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
993
994 if (((rest == 5) || (rest == 6)) &&
995 !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
996 crc_check, 4)) {
997 urb->actual_length -= 4;
998 }
999}
1000
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001001/* Moving data from usb to kernel (in interrupt state) */
1002static void read_bulk_callback(struct urb *urb)
1003{
1004 struct hso_net *odev = urb->context;
1005 struct net_device *net;
1006 int result;
1007 int status = urb->status;
1008
1009 /* is al ok? (Filip: Who's Al ?) */
1010 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001011 handle_usb_error(status, __func__, odev->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001012 return;
1013 }
1014
1015 /* Sanity check */
1016 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
1017 D1("BULK IN callback but driver is not active!");
1018 return;
1019 }
1020 usb_mark_last_busy(urb->dev);
1021
1022 net = odev->net;
1023
1024 if (!netif_device_present(net)) {
1025 /* Somebody killed our network interface... */
1026 return;
1027 }
1028
Joe Perches5591c752010-12-21 02:16:09 -08001029 if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
1030 fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001031
1032 /* do we even have a packet? */
1033 if (urb->actual_length) {
1034 /* Handle the IP stream, add header and push it onto network
1035 * stack if the packet is complete. */
1036 spin_lock(&odev->net_lock);
1037 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1038 (urb->transfer_buffer_length >
1039 urb->actual_length) ? 1 : 0);
1040 spin_unlock(&odev->net_lock);
1041 }
1042
1043 /* We are done with this URB, resubmit it. Prep the USB to wait for
1044 * another frame. Reuse same as received. */
1045 usb_fill_bulk_urb(urb,
1046 odev->parent->usb,
1047 usb_rcvbulkpipe(odev->parent->usb,
1048 odev->in_endp->
1049 bEndpointAddress & 0x7F),
1050 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1051 read_bulk_callback, odev);
1052
1053 /* Give this to the USB subsystem so it can tell us when more data
1054 * arrives. */
1055 result = usb_submit_urb(urb, GFP_ATOMIC);
1056 if (result)
1057 dev_warn(&odev->parent->interface->dev,
Jan Dumon8a5c9c42010-01-05 04:53:00 +00001058 "%s failed submit mux_bulk_rx_urb %d\n", __func__,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001059 result);
1060}
1061
1062/* Serial driver functions */
1063
Alan Coxac9720c2009-01-02 13:47:45 +00001064static void hso_init_termios(struct ktermios *termios)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001065{
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001066 /*
1067 * The default requirements for this device are:
1068 */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001069 termios->c_iflag &=
1070 ~(IGNBRK /* disable ignore break */
1071 | BRKINT /* disable break causes interrupt */
1072 | PARMRK /* disable mark parity errors */
1073 | ISTRIP /* disable clear high bit of input characters */
1074 | INLCR /* disable translate NL to CR */
1075 | IGNCR /* disable ignore CR */
1076 | ICRNL /* disable translate CR to NL */
1077 | IXON); /* disable enable XON/XOFF flow control */
1078
1079 /* disable postprocess output characters */
1080 termios->c_oflag &= ~OPOST;
1081
1082 termios->c_lflag &=
1083 ~(ECHO /* disable echo input characters */
1084 | ECHONL /* disable echo new line */
1085 | ICANON /* disable erase, kill, werase, and rprnt
1086 special characters */
1087 | ISIG /* disable interrupt, quit, and suspend special
1088 characters */
1089 | IEXTEN); /* disable non-POSIX special characters */
1090
1091 termios->c_cflag &=
1092 ~(CSIZE /* no size */
1093 | PARENB /* disable parity bit */
1094 | CBAUD /* clear current baud rate */
1095 | CBAUDEX); /* clear current buad rate */
1096
1097 termios->c_cflag |= CS8; /* character size 8 bits */
1098
1099 /* baud rate 115200 */
Alan Coxac9720c2009-01-02 13:47:45 +00001100 tty_termios_encode_baud_rate(termios, 115200, 115200);
1101}
1102
1103static void _hso_serial_set_termios(struct tty_struct *tty,
1104 struct ktermios *old)
1105{
Jiri Slaby30409422012-04-02 13:54:06 +02001106 struct hso_serial *serial = tty->driver_data;
Alan Coxac9720c2009-01-02 13:47:45 +00001107
1108 if (!serial) {
1109 printk(KERN_ERR "%s: no tty structures", __func__);
1110 return;
1111 }
1112
1113 D4("port %d", serial->minor);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001114
1115 /*
Alan Coxac9720c2009-01-02 13:47:45 +00001116 * Fix up unsupported bits
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001117 */
Alan Coxadc8d742012-07-14 15:31:47 +01001118 tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
Alan Coxac9720c2009-01-02 13:47:45 +00001119
Alan Coxadc8d742012-07-14 15:31:47 +01001120 tty->termios.c_cflag &=
Alan Coxac9720c2009-01-02 13:47:45 +00001121 ~(CSIZE /* no size */
1122 | PARENB /* disable parity bit */
1123 | CBAUD /* clear current baud rate */
1124 | CBAUDEX); /* clear current buad rate */
1125
Alan Coxadc8d742012-07-14 15:31:47 +01001126 tty->termios.c_cflag |= CS8; /* character size 8 bits */
Alan Coxac9720c2009-01-02 13:47:45 +00001127
1128 /* baud rate 115200 */
1129 tty_encode_baud_rate(tty, 115200, 115200);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001130}
1131
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001132static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1133{
1134 int result;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001135 /* We are done with this URB, resubmit it. Prep the USB to wait for
1136 * another frame */
1137 usb_fill_bulk_urb(urb, serial->parent->usb,
1138 usb_rcvbulkpipe(serial->parent->usb,
1139 serial->in_endp->
1140 bEndpointAddress & 0x7F),
1141 urb->transfer_buffer, serial->rx_data_length,
1142 hso_std_serial_read_bulk_callback, serial);
1143 /* Give this to the USB subsystem so it can tell us when more data
1144 * arrives. */
1145 result = usb_submit_urb(urb, GFP_ATOMIC);
1146 if (result) {
1147 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1148 __func__, result);
1149 }
1150}
1151
1152
1153
1154
1155static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1156{
1157 int count;
1158 struct urb *curr_urb;
1159
1160 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1161 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1162 count = put_rxbuf_data(curr_urb, serial);
1163 if (count == -1)
1164 return;
1165 if (count == 0) {
1166 serial->curr_rx_urb_idx++;
1167 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1168 serial->curr_rx_urb_idx = 0;
1169 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1170 }
1171 }
1172}
1173
1174static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1175{
1176 int count = 0;
1177 struct urb *urb;
1178
1179 urb = serial->rx_urb[0];
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001180 if (serial->port.count > 0) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001181 count = put_rxbuf_data(urb, serial);
1182 if (count == -1)
1183 return;
1184 }
1185 /* Re issue a read as long as we receive data. */
1186
1187 if (count == 0 && ((urb->actual_length != 0) ||
1188 (serial->rx_state == RX_PENDING))) {
1189 serial->rx_state = RX_SENT;
1190 hso_mux_serial_read(serial);
1191 } else
1192 serial->rx_state = RX_IDLE;
1193}
1194
1195
1196/* read callback for Diag and CS port */
1197static void hso_std_serial_read_bulk_callback(struct urb *urb)
1198{
1199 struct hso_serial *serial = urb->context;
1200 int status = urb->status;
1201
Dan Carpenter4ccd0bb2014-02-06 15:53:19 +03001202 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1203
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001204 /* sanity check */
1205 if (!serial) {
1206 D1("serial == NULL");
1207 return;
Dan Carpenter4ccd0bb2014-02-06 15:53:19 +03001208 }
1209 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001210 handle_usb_error(status, __func__, serial->parent);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001211 return;
1212 }
1213
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001214 D1("Actual length = %d\n", urb->actual_length);
1215 DUMP1(urb->transfer_buffer, urb->actual_length);
1216
1217 /* Anyone listening? */
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001218 if (serial->port.count == 0)
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001219 return;
1220
Dan Carpenter4ccd0bb2014-02-06 15:53:19 +03001221 if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
1222 fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
1223 /* Valid data, handle RX data */
1224 spin_lock(&serial->serial_lock);
1225 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1226 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1227 spin_unlock(&serial->serial_lock);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001228}
1229
1230/*
1231 * This needs to be a tasklet otherwise we will
1232 * end up recursively calling this function.
1233 */
Hannes Eder0227abc2009-02-14 11:47:47 +00001234static void hso_unthrottle_tasklet(struct hso_serial *serial)
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001235{
1236 unsigned long flags;
1237
1238 spin_lock_irqsave(&serial->serial_lock, flags);
1239 if ((serial->parent->port_spec & HSO_INTF_MUX))
1240 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1241 else
1242 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1243 spin_unlock_irqrestore(&serial->serial_lock, flags);
1244}
1245
1246static void hso_unthrottle(struct tty_struct *tty)
1247{
Jiri Slaby30409422012-04-02 13:54:06 +02001248 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001249
1250 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1251}
1252
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001253/* open the requested serial port */
1254static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1255{
1256 struct hso_serial *serial = get_serial_by_index(tty->index);
David S. Millerab153d82008-11-25 03:52:46 -08001257 int result;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001258
1259 /* sanity check */
1260 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
Alan Coxe136e302009-01-02 13:47:39 +00001261 WARN_ON(1);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001262 tty->driver_data = NULL;
1263 D1("Failed to open port");
1264 return -ENODEV;
1265 }
1266
David S. Millerab153d82008-11-25 03:52:46 -08001267 mutex_lock(&serial->parent->mutex);
David S. Millerab153d82008-11-25 03:52:46 -08001268 result = usb_autopm_get_interface(serial->parent->interface);
1269 if (result < 0)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001270 goto err_out;
1271
1272 D1("Opening %d", serial->minor);
1273 kref_get(&serial->parent->ref);
1274
1275 /* setup */
1276 tty->driver_data = serial;
Jiri Slaby9f8c0b02012-04-02 13:54:07 +02001277 tty_port_tty_set(&serial->port, tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001278
David S. Miller2f9889a2008-11-25 03:53:09 -08001279 /* check for port already opened, if not set the termios */
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001280 serial->port.count++;
1281 if (serial->port.count == 1) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001282 serial->rx_state = RX_IDLE;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001283 /* Force default termio settings */
1284 _hso_serial_set_termios(tty, NULL);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001285 tasklet_init(&serial->unthrottle_tasklet,
1286 (void (*)(unsigned long))hso_unthrottle_tasklet,
1287 (unsigned long)serial);
David S. Millerab153d82008-11-25 03:52:46 -08001288 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1289 if (result) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001290 hso_stop_serial_device(serial->parent);
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001291 serial->port.count--;
David S. Millerab153d82008-11-25 03:52:46 -08001292 kref_put(&serial->parent->ref, hso_serial_ref_free);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001293 }
1294 } else {
1295 D1("Port was already open");
1296 }
1297
1298 usb_autopm_put_interface(serial->parent->interface);
1299
1300 /* done */
David S. Millerab153d82008-11-25 03:52:46 -08001301 if (result)
Alan Cox20b9d172011-02-14 16:26:50 +00001302 hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001303err_out:
David S. Millerab153d82008-11-25 03:52:46 -08001304 mutex_unlock(&serial->parent->mutex);
1305 return result;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001306}
1307
1308/* close the requested serial port */
1309static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1310{
1311 struct hso_serial *serial = tty->driver_data;
1312 u8 usb_gone;
1313
1314 D1("Closing serial port");
1315
Alan Coxe136e302009-01-02 13:47:39 +00001316 /* Open failed, no close cleanup required */
1317 if (serial == NULL)
1318 return;
1319
David S. Millerab153d82008-11-25 03:52:46 -08001320 mutex_lock(&serial->parent->mutex);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001321 usb_gone = serial->parent->usb_gone;
1322
1323 if (!usb_gone)
1324 usb_autopm_get_interface(serial->parent->interface);
1325
1326 /* reset the rts and dtr */
1327 /* do the actual close */
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001328 serial->port.count--;
Antti Kaijanmäkidcfcb252009-11-23 10:54:47 -08001329
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001330 if (serial->port.count <= 0) {
1331 serial->port.count = 0;
Jiri Slaby9f8c0b02012-04-02 13:54:07 +02001332 tty_port_tty_set(&serial->port, NULL);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001333 if (!usb_gone)
1334 hso_stop_serial_device(serial->parent);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001335 tasklet_kill(&serial->unthrottle_tasklet);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001336 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001337
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001338 if (!usb_gone)
1339 usb_autopm_put_interface(serial->parent->interface);
David S. Millerab153d82008-11-25 03:52:46 -08001340
1341 mutex_unlock(&serial->parent->mutex);
Antti Kaijanmäkidcfcb252009-11-23 10:54:47 -08001342
1343 kref_put(&serial->parent->ref, hso_serial_ref_free);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001344}
1345
1346/* close the requested serial port */
1347static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1348 int count)
1349{
Jiri Slaby30409422012-04-02 13:54:06 +02001350 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001351 int space, tx_bytes;
1352 unsigned long flags;
1353
1354 /* sanity check */
1355 if (serial == NULL) {
1356 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1357 return -ENODEV;
1358 }
1359
1360 spin_lock_irqsave(&serial->serial_lock, flags);
1361
1362 space = serial->tx_data_length - serial->tx_buffer_count;
1363 tx_bytes = (count < space) ? count : space;
1364
1365 if (!tx_bytes)
1366 goto out;
1367
1368 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1369 serial->tx_buffer_count += tx_bytes;
1370
1371out:
1372 spin_unlock_irqrestore(&serial->serial_lock, flags);
1373
1374 hso_kick_transmit(serial);
1375 /* done */
1376 return tx_bytes;
1377}
1378
1379/* how much room is there for writing */
1380static int hso_serial_write_room(struct tty_struct *tty)
1381{
Jiri Slaby30409422012-04-02 13:54:06 +02001382 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001383 int room;
1384 unsigned long flags;
1385
1386 spin_lock_irqsave(&serial->serial_lock, flags);
1387 room = serial->tx_data_length - serial->tx_buffer_count;
1388 spin_unlock_irqrestore(&serial->serial_lock, flags);
1389
1390 /* return free room */
1391 return room;
1392}
1393
1394/* setup the term */
1395static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1396{
Jiri Slaby30409422012-04-02 13:54:06 +02001397 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001398 unsigned long flags;
1399
1400 if (old)
1401 D5("Termios called with: cflags new[%d] - old[%d]",
Alan Coxadc8d742012-07-14 15:31:47 +01001402 tty->termios.c_cflag, old->c_cflag);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001403
1404 /* the actual setup */
1405 spin_lock_irqsave(&serial->serial_lock, flags);
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001406 if (serial->port.count)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001407 _hso_serial_set_termios(tty, old);
1408 else
Alan Coxadc8d742012-07-14 15:31:47 +01001409 tty->termios = *old;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001410 spin_unlock_irqrestore(&serial->serial_lock, flags);
1411
1412 /* done */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001413}
1414
1415/* how many characters in the buffer */
1416static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1417{
Jiri Slaby30409422012-04-02 13:54:06 +02001418 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001419 int chars;
1420 unsigned long flags;
1421
1422 /* sanity check */
1423 if (serial == NULL)
1424 return 0;
1425
1426 spin_lock_irqsave(&serial->serial_lock, flags);
1427 chars = serial->tx_buffer_count;
1428 spin_unlock_irqrestore(&serial->serial_lock, flags);
1429
1430 return chars;
1431}
Hannes Eder0227abc2009-02-14 11:47:47 +00001432static int tiocmget_submit_urb(struct hso_serial *serial,
1433 struct hso_tiocmget *tiocmget,
1434 struct usb_device *usb)
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001435{
1436 int result;
1437
1438 if (serial->parent->usb_gone)
1439 return -ENODEV;
1440 usb_fill_int_urb(tiocmget->urb, usb,
1441 usb_rcvintpipe(usb,
1442 tiocmget->endp->
1443 bEndpointAddress & 0x7F),
1444 &tiocmget->serial_state_notification,
1445 sizeof(struct hso_serial_state_notification),
1446 tiocmget_intr_callback, serial,
1447 tiocmget->endp->bInterval);
1448 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1449 if (result) {
1450 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1451 result);
1452 }
1453 return result;
1454
1455}
1456
1457static void tiocmget_intr_callback(struct urb *urb)
1458{
1459 struct hso_serial *serial = urb->context;
1460 struct hso_tiocmget *tiocmget;
1461 int status = urb->status;
1462 u16 UART_state_bitmap, prev_UART_state_bitmap;
1463 struct uart_icount *icount;
1464 struct hso_serial_state_notification *serial_state_notification;
1465 struct usb_device *usb;
Dan Williamse5e97ee2014-01-06 10:07:29 -06001466 int if_num;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001467
1468 /* Sanity checks */
1469 if (!serial)
1470 return;
1471 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001472 handle_usb_error(status, __func__, serial->parent);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001473 return;
1474 }
Dan Williamse5e97ee2014-01-06 10:07:29 -06001475
1476 /* tiocmget is only supported on HSO_PORT_MODEM */
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001477 tiocmget = serial->tiocmget;
1478 if (!tiocmget)
1479 return;
Dan Williamse5e97ee2014-01-06 10:07:29 -06001480 BUG_ON((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM);
1481
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001482 usb = serial->parent->usb;
Dan Williamse5e97ee2014-01-06 10:07:29 -06001483 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1484
1485 /* wIndex should be the USB interface number of the port to which the
1486 * notification applies, which should always be the Modem port.
1487 */
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001488 serial_state_notification = &tiocmget->serial_state_notification;
1489 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1490 serial_state_notification->bNotification != B_NOTIFICATION ||
1491 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
Dan Williamse5e97ee2014-01-06 10:07:29 -06001492 le16_to_cpu(serial_state_notification->wIndex) != if_num ||
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001493 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1494 dev_warn(&usb->dev,
1495 "hso received invalid serial state notification\n");
1496 DUMP(serial_state_notification,
Antti Kaijanmäki9ce673d2009-11-23 10:54:24 -08001497 sizeof(struct hso_serial_state_notification));
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001498 } else {
1499
1500 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1501 UART_state_bitmap);
1502 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1503 icount = &tiocmget->icount;
1504 spin_lock(&serial->serial_lock);
1505 if ((UART_state_bitmap & B_OVERRUN) !=
1506 (prev_UART_state_bitmap & B_OVERRUN))
1507 icount->parity++;
1508 if ((UART_state_bitmap & B_PARITY) !=
1509 (prev_UART_state_bitmap & B_PARITY))
1510 icount->parity++;
1511 if ((UART_state_bitmap & B_FRAMING) !=
1512 (prev_UART_state_bitmap & B_FRAMING))
1513 icount->frame++;
1514 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1515 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1516 icount->rng++;
1517 if ((UART_state_bitmap & B_BREAK) !=
1518 (prev_UART_state_bitmap & B_BREAK))
1519 icount->brk++;
1520 if ((UART_state_bitmap & B_TX_CARRIER) !=
1521 (prev_UART_state_bitmap & B_TX_CARRIER))
1522 icount->dsr++;
1523 if ((UART_state_bitmap & B_RX_CARRIER) !=
1524 (prev_UART_state_bitmap & B_RX_CARRIER))
1525 icount->dcd++;
1526 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1527 spin_unlock(&serial->serial_lock);
1528 tiocmget->intr_completed = 1;
1529 wake_up_interruptible(&tiocmget->waitq);
1530 }
1531 memset(serial_state_notification, 0,
1532 sizeof(struct hso_serial_state_notification));
1533 tiocmget_submit_urb(serial,
1534 tiocmget,
1535 serial->parent->usb);
1536}
1537
1538/*
1539 * next few functions largely stolen from drivers/serial/serial_core.c
1540 */
1541/* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1542 * - mask passed in arg for lines of interest
1543 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1544 * Caller should use TIOCGICOUNT to see which one it was
1545 */
1546static int
1547hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1548{
1549 DECLARE_WAITQUEUE(wait, current);
1550 struct uart_icount cprev, cnow;
1551 struct hso_tiocmget *tiocmget;
1552 int ret;
1553
1554 tiocmget = serial->tiocmget;
1555 if (!tiocmget)
1556 return -ENOENT;
1557 /*
1558 * note the counters on entry
1559 */
1560 spin_lock_irq(&serial->serial_lock);
1561 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1562 spin_unlock_irq(&serial->serial_lock);
1563 add_wait_queue(&tiocmget->waitq, &wait);
1564 for (;;) {
1565 spin_lock_irq(&serial->serial_lock);
1566 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1567 spin_unlock_irq(&serial->serial_lock);
1568 set_current_state(TASK_INTERRUPTIBLE);
1569 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1570 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1571 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1572 ret = 0;
1573 break;
1574 }
1575 schedule();
1576 /* see if a signal did it */
1577 if (signal_pending(current)) {
1578 ret = -ERESTARTSYS;
1579 break;
1580 }
1581 cprev = cnow;
1582 }
1583 current->state = TASK_RUNNING;
1584 remove_wait_queue(&tiocmget->waitq, &wait);
1585
1586 return ret;
1587}
1588
1589/*
1590 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1591 * Return: write counters to the user passed counter struct
1592 * NB: both 1->0 and 0->1 transitions are counted except for
1593 * RI where only 0->1 is counted.
1594 */
Alan Cox0bca1b92010-09-16 18:21:40 +01001595static int hso_get_count(struct tty_struct *tty,
1596 struct serial_icounter_struct *icount)
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001597{
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001598 struct uart_icount cnow;
Jiri Slaby30409422012-04-02 13:54:06 +02001599 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001600 struct hso_tiocmget *tiocmget = serial->tiocmget;
1601
Dan Carpenter22ad7492012-02-21 21:30:25 +00001602 memset(icount, 0, sizeof(struct serial_icounter_struct));
Dan Rosenberg7011e662010-09-15 11:43:28 +00001603
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001604 if (!tiocmget)
1605 return -ENOENT;
1606 spin_lock_irq(&serial->serial_lock);
1607 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1608 spin_unlock_irq(&serial->serial_lock);
1609
Alan Cox0bca1b92010-09-16 18:21:40 +01001610 icount->cts = cnow.cts;
1611 icount->dsr = cnow.dsr;
1612 icount->rng = cnow.rng;
1613 icount->dcd = cnow.dcd;
1614 icount->rx = cnow.rx;
1615 icount->tx = cnow.tx;
1616 icount->frame = cnow.frame;
1617 icount->overrun = cnow.overrun;
1618 icount->parity = cnow.parity;
1619 icount->brk = cnow.brk;
1620 icount->buf_overrun = cnow.buf_overrun;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001621
Alan Cox0bca1b92010-09-16 18:21:40 +01001622 return 0;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001623}
1624
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001625
Alan Cox60b33c12011-02-14 16:26:14 +00001626static int hso_serial_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001627{
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001628 int retval;
Jiri Slaby30409422012-04-02 13:54:06 +02001629 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001630 struct hso_tiocmget *tiocmget;
1631 u16 UART_state_bitmap;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001632
1633 /* sanity check */
1634 if (!serial) {
1635 D1("no tty structures");
1636 return -EINVAL;
1637 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001638 spin_lock_irq(&serial->serial_lock);
1639 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
David S. Millercd90ee12008-11-25 03:52:17 -08001640 ((serial->dtr_state) ? TIOCM_DTR : 0);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001641 tiocmget = serial->tiocmget;
1642 if (tiocmget) {
David S. Millercd90ee12008-11-25 03:52:17 -08001643
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001644 UART_state_bitmap = le16_to_cpu(
1645 tiocmget->prev_UART_state_bitmap);
1646 if (UART_state_bitmap & B_RING_SIGNAL)
1647 retval |= TIOCM_RNG;
1648 if (UART_state_bitmap & B_RX_CARRIER)
1649 retval |= TIOCM_CD;
1650 if (UART_state_bitmap & B_TX_CARRIER)
1651 retval |= TIOCM_DSR;
1652 }
1653 spin_unlock_irq(&serial->serial_lock);
1654 return retval;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001655}
1656
Alan Cox20b9d172011-02-14 16:26:50 +00001657static int hso_serial_tiocmset(struct tty_struct *tty,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001658 unsigned int set, unsigned int clear)
1659{
1660 int val = 0;
1661 unsigned long flags;
1662 int if_num;
Jiri Slaby30409422012-04-02 13:54:06 +02001663 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001664
1665 /* sanity check */
1666 if (!serial) {
1667 D1("no tty structures");
1668 return -EINVAL;
1669 }
Jan Dumon0e0367e2010-01-05 04:52:42 +00001670
1671 if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
1672 return -EINVAL;
1673
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001674 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1675
1676 spin_lock_irqsave(&serial->serial_lock, flags);
1677 if (set & TIOCM_RTS)
1678 serial->rts_state = 1;
1679 if (set & TIOCM_DTR)
1680 serial->dtr_state = 1;
1681
1682 if (clear & TIOCM_RTS)
1683 serial->rts_state = 0;
1684 if (clear & TIOCM_DTR)
1685 serial->dtr_state = 0;
1686
1687 if (serial->dtr_state)
1688 val |= 0x01;
1689 if (serial->rts_state)
1690 val |= 0x02;
1691
1692 spin_unlock_irqrestore(&serial->serial_lock, flags);
1693
1694 return usb_control_msg(serial->parent->usb,
1695 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1696 0x21, val, if_num, NULL, 0,
1697 USB_CTRL_SET_TIMEOUT);
1698}
1699
Alan Cox6caa76b2011-02-14 16:27:22 +00001700static int hso_serial_ioctl(struct tty_struct *tty,
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001701 unsigned int cmd, unsigned long arg)
1702{
Jiri Slaby30409422012-04-02 13:54:06 +02001703 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001704 int ret = 0;
1705 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1706
1707 if (!serial)
1708 return -ENODEV;
1709 switch (cmd) {
1710 case TIOCMIWAIT:
1711 ret = hso_wait_modem_status(serial, arg);
1712 break;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001713 default:
1714 ret = -ENOIOCTLCMD;
1715 break;
1716 }
1717 return ret;
1718}
1719
1720
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001721/* starts a transmit */
1722static void hso_kick_transmit(struct hso_serial *serial)
1723{
1724 u8 *temp;
1725 unsigned long flags;
1726 int res;
1727
1728 spin_lock_irqsave(&serial->serial_lock, flags);
1729 if (!serial->tx_buffer_count)
1730 goto out;
1731
1732 if (serial->tx_urb_used)
1733 goto out;
1734
1735 /* Wakeup USB interface if necessary */
1736 if (hso_get_activity(serial->parent) == -EAGAIN)
1737 goto out;
1738
1739 /* Switch pointers around to avoid memcpy */
1740 temp = serial->tx_buffer;
1741 serial->tx_buffer = serial->tx_data;
1742 serial->tx_data = temp;
1743 serial->tx_data_count = serial->tx_buffer_count;
1744 serial->tx_buffer_count = 0;
1745
1746 /* If temp is set, it means we switched buffers */
1747 if (temp && serial->write_data) {
1748 res = serial->write_data(serial);
1749 if (res >= 0)
1750 serial->tx_urb_used = 1;
1751 }
1752out:
1753 spin_unlock_irqrestore(&serial->serial_lock, flags);
1754}
1755
1756/* make a request (for reading and writing data to muxed serial port) */
1757static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1758 struct urb *ctrl_urb,
1759 struct usb_ctrlrequest *ctrl_req,
1760 u8 *ctrl_urb_data, u32 size)
1761{
1762 int result;
1763 int pipe;
1764
1765 /* Sanity check */
1766 if (!serial || !ctrl_urb || !ctrl_req) {
1767 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1768 return -EINVAL;
1769 }
1770
1771 /* initialize */
1772 ctrl_req->wValue = 0;
Denis Joseph Barrowb74f62c2009-01-12 21:56:49 -08001773 ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
1774 ctrl_req->wLength = cpu_to_le16(size);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001775
1776 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1777 /* Reading command */
1778 ctrl_req->bRequestType = USB_DIR_IN |
1779 USB_TYPE_OPTION_VENDOR |
1780 USB_RECIP_INTERFACE;
1781 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1782 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1783 } else {
1784 /* Writing command */
1785 ctrl_req->bRequestType = USB_DIR_OUT |
1786 USB_TYPE_OPTION_VENDOR |
1787 USB_RECIP_INTERFACE;
1788 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1789 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1790 }
1791 /* syslog */
1792 D2("%s command (%02x) len: %d, port: %d",
1793 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1794 ctrl_req->bRequestType, ctrl_req->wLength, port);
1795
1796 /* Load ctrl urb */
1797 ctrl_urb->transfer_flags = 0;
1798 usb_fill_control_urb(ctrl_urb,
1799 serial->parent->usb,
1800 pipe,
1801 (u8 *) ctrl_req,
1802 ctrl_urb_data, size, ctrl_callback, serial);
1803 /* Send it on merry way */
1804 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1805 if (result) {
1806 dev_err(&ctrl_urb->dev->dev,
Jan Dumon8a5c9c42010-01-05 04:53:00 +00001807 "%s failed submit ctrl_urb %d type %d\n", __func__,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001808 result, type);
1809 return result;
1810 }
1811
1812 /* done */
1813 return size;
1814}
1815
1816/* called by intr_callback when read occurs */
1817static int hso_mux_serial_read(struct hso_serial *serial)
1818{
1819 if (!serial)
1820 return -EINVAL;
1821
1822 /* clean data */
1823 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1824 /* make the request */
1825
1826 if (serial->num_rx_urbs != 1) {
1827 dev_err(&serial->parent->interface->dev,
1828 "ERROR: mux'd reads with multiple buffers "
1829 "not possible\n");
1830 return 0;
1831 }
1832 return mux_device_request(serial,
1833 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1834 serial->parent->port_spec & HSO_PORT_MASK,
1835 serial->rx_urb[0],
1836 &serial->ctrl_req_rx,
1837 serial->rx_data[0], serial->rx_data_length);
1838}
1839
1840/* used for muxed serial port callback (muxed serial read) */
1841static void intr_callback(struct urb *urb)
1842{
1843 struct hso_shared_int *shared_int = urb->context;
1844 struct hso_serial *serial;
1845 unsigned char *port_req;
1846 int status = urb->status;
1847 int i;
1848
1849 usb_mark_last_busy(urb->dev);
1850
1851 /* sanity check */
1852 if (!shared_int)
1853 return;
1854
1855 /* status check */
1856 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001857 handle_usb_error(status, __func__, NULL);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001858 return;
1859 }
1860 D4("\n--- Got intr callback 0x%02X ---", status);
1861
1862 /* what request? */
1863 port_req = urb->transfer_buffer;
1864 D4(" port_req = 0x%.2X\n", *port_req);
1865 /* loop over all muxed ports to find the one sending this */
1866 for (i = 0; i < 8; i++) {
1867 /* max 8 channels on MUX */
1868 if (*port_req & (1 << i)) {
1869 serial = get_serial_by_shared_int_and_type(shared_int,
1870 (1 << i));
1871 if (serial != NULL) {
1872 D1("Pending read interrupt on port %d\n", i);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001873 spin_lock(&serial->serial_lock);
Jan Dumonf4763e92010-01-05 04:51:28 +00001874 if (serial->rx_state == RX_IDLE &&
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001875 serial->port.count > 0) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001876 /* Setup and send a ctrl req read on
1877 * port i */
Jan Dumonf4763e92010-01-05 04:51:28 +00001878 if (!serial->rx_urb_filled[0]) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001879 serial->rx_state = RX_SENT;
1880 hso_mux_serial_read(serial);
1881 } else
1882 serial->rx_state = RX_PENDING;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001883 } else {
Jan Dumonf4763e92010-01-05 04:51:28 +00001884 D1("Already a read pending on "
1885 "port %d or port not open\n", i);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001886 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001887 spin_unlock(&serial->serial_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001888 }
1889 }
1890 }
1891 /* Resubmit interrupt urb */
1892 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1893}
1894
1895/* called for writing to muxed serial port */
1896static int hso_mux_serial_write_data(struct hso_serial *serial)
1897{
1898 if (NULL == serial)
1899 return -EINVAL;
1900
1901 return mux_device_request(serial,
1902 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1903 serial->parent->port_spec & HSO_PORT_MASK,
1904 serial->tx_urb,
1905 &serial->ctrl_req_tx,
1906 serial->tx_data, serial->tx_data_count);
1907}
1908
1909/* write callback for Diag and CS port */
1910static void hso_std_serial_write_bulk_callback(struct urb *urb)
1911{
1912 struct hso_serial *serial = urb->context;
1913 int status = urb->status;
1914
1915 /* sanity check */
1916 if (!serial) {
1917 D1("serial == NULL");
1918 return;
1919 }
1920
1921 spin_lock(&serial->serial_lock);
1922 serial->tx_urb_used = 0;
1923 spin_unlock(&serial->serial_lock);
1924 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001925 handle_usb_error(status, __func__, serial->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001926 return;
1927 }
1928 hso_put_activity(serial->parent);
Jiri Slaby6aad04f2013-03-07 13:12:29 +01001929 tty_port_tty_wakeup(&serial->port);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001930 hso_kick_transmit(serial);
1931
1932 D1(" ");
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001933}
1934
1935/* called for writing diag or CS serial port */
1936static int hso_std_serial_write_data(struct hso_serial *serial)
1937{
1938 int count = serial->tx_data_count;
1939 int result;
1940
1941 usb_fill_bulk_urb(serial->tx_urb,
1942 serial->parent->usb,
1943 usb_sndbulkpipe(serial->parent->usb,
1944 serial->out_endp->
1945 bEndpointAddress & 0x7F),
1946 serial->tx_data, serial->tx_data_count,
1947 hso_std_serial_write_bulk_callback, serial);
1948
1949 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1950 if (result) {
1951 dev_warn(&serial->parent->usb->dev,
1952 "Failed to submit urb - res %d\n", result);
1953 return result;
1954 }
1955
1956 return count;
1957}
1958
1959/* callback after read or write on muxed serial port */
1960static void ctrl_callback(struct urb *urb)
1961{
1962 struct hso_serial *serial = urb->context;
1963 struct usb_ctrlrequest *req;
1964 int status = urb->status;
1965
1966 /* sanity check */
1967 if (!serial)
1968 return;
1969
1970 spin_lock(&serial->serial_lock);
1971 serial->tx_urb_used = 0;
1972 spin_unlock(&serial->serial_lock);
1973 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001974 handle_usb_error(status, __func__, serial->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001975 return;
1976 }
1977
1978 /* what request? */
1979 req = (struct usb_ctrlrequest *)(urb->setup_packet);
1980 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
1981 D4("Actual length of urb = %d\n", urb->actual_length);
1982 DUMP1(urb->transfer_buffer, urb->actual_length);
1983
1984 if (req->bRequestType ==
1985 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
1986 /* response to a read command */
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001987 serial->rx_urb_filled[0] = 1;
1988 spin_lock(&serial->serial_lock);
1989 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1990 spin_unlock(&serial->serial_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001991 } else {
1992 hso_put_activity(serial->parent);
Jiri Slaby6aad04f2013-03-07 13:12:29 +01001993 tty_port_tty_wakeup(&serial->port);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001994 /* response to a write command */
1995 hso_kick_transmit(serial);
1996 }
1997}
1998
1999/* handle RX data for serial port */
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002000static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002001{
Alan Coxe136e302009-01-02 13:47:39 +00002002 struct tty_struct *tty;
Olivier Sobrie8f9818af2014-07-14 12:08:50 +02002003 int count;
Alan Coxe136e302009-01-02 13:47:39 +00002004
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002005 /* Sanity check */
2006 if (urb == NULL || serial == NULL) {
2007 D1("serial = NULL");
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002008 return -2;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002009 }
2010
Jiri Slaby9f8c0b02012-04-02 13:54:07 +02002011 tty = tty_port_tty_get(&serial->port);
Alan Coxe136e302009-01-02 13:47:39 +00002012
Olivier Sobrie8f9818af2014-07-14 12:08:50 +02002013 if (tty && test_bit(TTY_THROTTLED, &tty->flags)) {
2014 tty_kref_put(tty);
2015 return -1;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002016 }
Olivier Sobrie8f9818af2014-07-14 12:08:50 +02002017
2018 /* Push data to tty */
2019 D1("data to push to tty");
2020 count = tty_buffer_request_room(&serial->port, urb->actual_length);
2021 if (count >= urb->actual_length) {
2022 tty_insert_flip_string(&serial->port, urb->transfer_buffer,
2023 urb->actual_length);
2024 tty_flip_buffer_push(&serial->port);
2025 } else {
2026 dev_warn(&serial->parent->usb->dev,
2027 "dropping data, %d bytes lost\n", urb->actual_length);
2028 }
2029
Jiri Slaby2e124b42013-01-03 15:53:06 +01002030 tty_kref_put(tty);
2031
Olivier Sobrie8f9818af2014-07-14 12:08:50 +02002032 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2033
2034 return 0;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002035}
2036
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002037
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002038/* Base driver functions */
2039
2040static void hso_log_port(struct hso_device *hso_dev)
2041{
2042 char *port_type;
2043 char port_dev[20];
2044
2045 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2046 case HSO_PORT_CONTROL:
2047 port_type = "Control";
2048 break;
2049 case HSO_PORT_APP:
2050 port_type = "Application";
2051 break;
2052 case HSO_PORT_GPS:
2053 port_type = "GPS";
2054 break;
2055 case HSO_PORT_GPS_CONTROL:
2056 port_type = "GPS control";
2057 break;
2058 case HSO_PORT_APP2:
2059 port_type = "Application2";
2060 break;
2061 case HSO_PORT_PCSC:
2062 port_type = "PCSC";
2063 break;
2064 case HSO_PORT_DIAG:
2065 port_type = "Diagnostic";
2066 break;
2067 case HSO_PORT_DIAG2:
2068 port_type = "Diagnostic2";
2069 break;
2070 case HSO_PORT_MODEM:
2071 port_type = "Modem";
2072 break;
2073 case HSO_PORT_NETWORK:
2074 port_type = "Network";
2075 break;
2076 default:
2077 port_type = "Unknown";
2078 break;
2079 }
2080 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2081 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2082 } else
2083 sprintf(port_dev, "/dev/%s%d", tty_filename,
2084 dev2ser(hso_dev)->minor);
2085
2086 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2087 port_type, port_dev);
2088}
2089
2090static int hso_start_net_device(struct hso_device *hso_dev)
2091{
2092 int i, result = 0;
2093 struct hso_net *hso_net = dev2net(hso_dev);
2094
2095 if (!hso_net)
2096 return -ENODEV;
2097
2098 /* send URBs for all read buffers */
2099 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2100
2101 /* Prep a receive URB */
2102 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2103 hso_dev->usb,
2104 usb_rcvbulkpipe(hso_dev->usb,
2105 hso_net->in_endp->
2106 bEndpointAddress & 0x7F),
2107 hso_net->mux_bulk_rx_buf_pool[i],
2108 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2109 hso_net);
2110
2111 /* Put it out there so the device can send us stuff */
2112 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2113 GFP_NOIO);
2114 if (result)
2115 dev_warn(&hso_dev->usb->dev,
2116 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2117 i, result);
2118 }
2119
2120 return result;
2121}
2122
2123static int hso_stop_net_device(struct hso_device *hso_dev)
2124{
2125 int i;
2126 struct hso_net *hso_net = dev2net(hso_dev);
2127
2128 if (!hso_net)
2129 return -ENODEV;
2130
2131 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2132 if (hso_net->mux_bulk_rx_urb_pool[i])
2133 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2134
2135 }
2136 if (hso_net->mux_bulk_tx_urb)
2137 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2138
2139 return 0;
2140}
2141
2142static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2143{
2144 int i, result = 0;
2145 struct hso_serial *serial = dev2ser(hso_dev);
2146
2147 if (!serial)
2148 return -ENODEV;
2149
2150 /* If it is not the MUX port fill in and submit a bulk urb (already
2151 * allocated in hso_serial_start) */
2152 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2153 for (i = 0; i < serial->num_rx_urbs; i++) {
2154 usb_fill_bulk_urb(serial->rx_urb[i],
2155 serial->parent->usb,
2156 usb_rcvbulkpipe(serial->parent->usb,
2157 serial->in_endp->
2158 bEndpointAddress &
2159 0x7F),
2160 serial->rx_data[i],
2161 serial->rx_data_length,
2162 hso_std_serial_read_bulk_callback,
2163 serial);
2164 result = usb_submit_urb(serial->rx_urb[i], flags);
2165 if (result) {
2166 dev_warn(&serial->parent->usb->dev,
2167 "Failed to submit urb - res %d\n",
2168 result);
2169 break;
2170 }
2171 }
2172 } else {
2173 mutex_lock(&serial->shared_int->shared_int_lock);
2174 if (!serial->shared_int->use_count) {
2175 result =
2176 hso_mux_submit_intr_urb(serial->shared_int,
2177 hso_dev->usb, flags);
2178 }
2179 serial->shared_int->use_count++;
2180 mutex_unlock(&serial->shared_int->shared_int_lock);
2181 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002182 if (serial->tiocmget)
2183 tiocmget_submit_urb(serial,
2184 serial->tiocmget,
2185 serial->parent->usb);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002186 return result;
2187}
2188
2189static int hso_stop_serial_device(struct hso_device *hso_dev)
2190{
2191 int i;
2192 struct hso_serial *serial = dev2ser(hso_dev);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002193 struct hso_tiocmget *tiocmget;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002194
2195 if (!serial)
2196 return -ENODEV;
2197
2198 for (i = 0; i < serial->num_rx_urbs; i++) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002199 if (serial->rx_urb[i]) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002200 usb_kill_urb(serial->rx_urb[i]);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002201 serial->rx_urb_filled[i] = 0;
2202 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002203 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002204 serial->curr_rx_urb_idx = 0;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002205
2206 if (serial->tx_urb)
2207 usb_kill_urb(serial->tx_urb);
2208
2209 if (serial->shared_int) {
2210 mutex_lock(&serial->shared_int->shared_int_lock);
2211 if (serial->shared_int->use_count &&
2212 (--serial->shared_int->use_count == 0)) {
2213 struct urb *urb;
2214
2215 urb = serial->shared_int->shared_intr_urb;
2216 if (urb)
2217 usb_kill_urb(urb);
2218 }
2219 mutex_unlock(&serial->shared_int->shared_int_lock);
2220 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002221 tiocmget = serial->tiocmget;
2222 if (tiocmget) {
2223 wake_up_interruptible(&tiocmget->waitq);
2224 usb_kill_urb(tiocmget->urb);
2225 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002226
2227 return 0;
2228}
2229
2230static void hso_serial_common_free(struct hso_serial *serial)
2231{
2232 int i;
2233
2234 if (serial->parent->dev)
2235 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2236
2237 tty_unregister_device(tty_drv, serial->minor);
2238
2239 for (i = 0; i < serial->num_rx_urbs; i++) {
2240 /* unlink and free RX URB */
2241 usb_free_urb(serial->rx_urb[i]);
2242 /* free the RX buffer */
2243 kfree(serial->rx_data[i]);
2244 }
2245
2246 /* unlink and free TX URB */
2247 usb_free_urb(serial->tx_urb);
2248 kfree(serial->tx_data);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002249 tty_port_destroy(&serial->port);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002250}
2251
2252static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2253 int rx_size, int tx_size)
2254{
2255 struct device *dev;
2256 int minor;
2257 int i;
2258
Jiri Slaby191c5f12012-11-15 09:49:56 +01002259 tty_port_init(&serial->port);
2260
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002261 minor = get_free_serial_index();
2262 if (minor < 0)
2263 goto exit;
2264
2265 /* register our minor number */
Jiri Slaby734cc172012-08-07 21:47:47 +02002266 serial->parent->dev = tty_port_register_device(&serial->port, tty_drv,
2267 minor, &serial->parent->interface->dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002268 dev = serial->parent->dev;
Greg Kroah-Hartman1aec5bd2009-04-30 12:19:31 +00002269 dev_set_drvdata(dev, serial->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002270 i = device_create_file(dev, &dev_attr_hsotype);
2271
2272 /* fill in specific data for later use */
2273 serial->minor = minor;
2274 serial->magic = HSO_SERIAL_MAGIC;
2275 spin_lock_init(&serial->serial_lock);
2276 serial->num_rx_urbs = num_urbs;
2277
2278 /* RX, allocate urb and initialize */
2279
2280 /* prepare our RX buffer */
2281 serial->rx_data_length = rx_size;
2282 for (i = 0; i < serial->num_rx_urbs; i++) {
2283 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2284 if (!serial->rx_urb[i]) {
2285 dev_err(dev, "Could not allocate urb?\n");
2286 goto exit;
2287 }
2288 serial->rx_urb[i]->transfer_buffer = NULL;
2289 serial->rx_urb[i]->transfer_buffer_length = 0;
2290 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2291 GFP_KERNEL);
Joe Perches38673c82013-02-03 17:28:11 +00002292 if (!serial->rx_data[i])
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002293 goto exit;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002294 }
2295
2296 /* TX, allocate urb and initialize */
2297 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2298 if (!serial->tx_urb) {
2299 dev_err(dev, "Could not allocate urb?\n");
2300 goto exit;
2301 }
2302 serial->tx_urb->transfer_buffer = NULL;
2303 serial->tx_urb->transfer_buffer_length = 0;
2304 /* prepare our TX buffer */
2305 serial->tx_data_count = 0;
2306 serial->tx_buffer_count = 0;
2307 serial->tx_data_length = tx_size;
2308 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
Joe Perches38673c82013-02-03 17:28:11 +00002309 if (!serial->tx_data)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002310 goto exit;
Joe Perches38673c82013-02-03 17:28:11 +00002311
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002312 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
Joe Perches38673c82013-02-03 17:28:11 +00002313 if (!serial->tx_buffer)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002314 goto exit;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002315
2316 return 0;
2317exit:
2318 hso_serial_common_free(serial);
2319 return -1;
2320}
2321
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002322/* Creates a general hso device */
2323static struct hso_device *hso_create_device(struct usb_interface *intf,
2324 int port_spec)
2325{
2326 struct hso_device *hso_dev;
2327
2328 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2329 if (!hso_dev)
2330 return NULL;
2331
2332 hso_dev->port_spec = port_spec;
2333 hso_dev->usb = interface_to_usbdev(intf);
2334 hso_dev->interface = intf;
2335 kref_init(&hso_dev->ref);
David S. Millerab153d82008-11-25 03:52:46 -08002336 mutex_init(&hso_dev->mutex);
2337
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002338 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2339 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
Jan Dumon68a351c2010-01-05 04:52:13 +00002340 INIT_WORK(&hso_dev->reset_device, reset_device);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002341
2342 return hso_dev;
2343}
2344
2345/* Removes a network device in the network device table */
2346static int remove_net_device(struct hso_device *hso_dev)
2347{
2348 int i;
2349
2350 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2351 if (network_table[i] == hso_dev) {
2352 network_table[i] = NULL;
2353 break;
2354 }
2355 }
2356 if (i == HSO_MAX_NET_DEVICES)
2357 return -1;
2358 return 0;
2359}
2360
2361/* Frees our network device */
2362static void hso_free_net_device(struct hso_device *hso_dev)
2363{
2364 int i;
2365 struct hso_net *hso_net = dev2net(hso_dev);
2366
2367 if (!hso_net)
2368 return;
2369
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002370 remove_net_device(hso_net->parent);
2371
Greg KH5e2cd082011-07-08 03:45:25 +00002372 if (hso_net->net)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002373 unregister_netdev(hso_net->net);
David S. Millerab153d82008-11-25 03:52:46 -08002374
Jan Dumon3b7d2b32009-04-01 22:57:20 +00002375 /* start freeing */
2376 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2377 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2378 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2379 hso_net->mux_bulk_rx_buf_pool[i] = NULL;
2380 }
2381 usb_free_urb(hso_net->mux_bulk_tx_urb);
2382 kfree(hso_net->mux_bulk_tx_buf);
2383 hso_net->mux_bulk_tx_buf = NULL;
2384
Greg KH5e2cd082011-07-08 03:45:25 +00002385 if (hso_net->net)
2386 free_netdev(hso_net->net);
2387
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002388 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002389}
2390
Stephen Hemmingerc266cb42009-03-20 19:35:52 +00002391static const struct net_device_ops hso_netdev_ops = {
2392 .ndo_open = hso_net_open,
2393 .ndo_stop = hso_net_close,
2394 .ndo_start_xmit = hso_net_start_xmit,
2395 .ndo_tx_timeout = hso_net_tx_timeout,
2396};
2397
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002398/* initialize the network interface */
2399static void hso_net_init(struct net_device *net)
2400{
2401 struct hso_net *hso_net = netdev_priv(net);
2402
2403 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2404
2405 /* fill in the other fields */
Stephen Hemmingerc266cb42009-03-20 19:35:52 +00002406 net->netdev_ops = &hso_netdev_ops;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002407 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2408 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2409 net->type = ARPHRD_NONE;
2410 net->mtu = DEFAULT_MTU - 14;
2411 net->tx_queue_len = 10;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002412 net->ethtool_ops = &ops;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002413
2414 /* and initialize the semaphore */
2415 spin_lock_init(&hso_net->net_lock);
2416}
2417
2418/* Adds a network device in the network device table */
2419static int add_net_device(struct hso_device *hso_dev)
2420{
2421 int i;
2422
2423 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2424 if (network_table[i] == NULL) {
2425 network_table[i] = hso_dev;
2426 break;
2427 }
2428 }
2429 if (i == HSO_MAX_NET_DEVICES)
2430 return -1;
2431 return 0;
2432}
2433
Johannes Berg19d337d2009-06-02 13:01:37 +02002434static int hso_rfkill_set_block(void *data, bool blocked)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002435{
2436 struct hso_device *hso_dev = data;
Johannes Berg19d337d2009-06-02 13:01:37 +02002437 int enabled = !blocked;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002438 int rv;
2439
David S. Millerab153d82008-11-25 03:52:46 -08002440 mutex_lock(&hso_dev->mutex);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002441 if (hso_dev->usb_gone)
2442 rv = 0;
2443 else
2444 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2445 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2446 USB_CTRL_SET_TIMEOUT);
David S. Millerab153d82008-11-25 03:52:46 -08002447 mutex_unlock(&hso_dev->mutex);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002448 return rv;
2449}
2450
Johannes Berg19d337d2009-06-02 13:01:37 +02002451static const struct rfkill_ops hso_rfkill_ops = {
2452 .set_block = hso_rfkill_set_block,
2453};
2454
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002455/* Creates and sets up everything for rfkill */
2456static void hso_create_rfkill(struct hso_device *hso_dev,
2457 struct usb_interface *interface)
2458{
2459 struct hso_net *hso_net = dev2net(hso_dev);
Jonathan McDowell939a9512008-11-04 07:51:38 +00002460 struct device *dev = &hso_net->net->dev;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002461 char *rfkn;
2462
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002463 rfkn = kzalloc(20, GFP_KERNEL);
Johannes Berg19d337d2009-06-02 13:01:37 +02002464 if (!rfkn)
Jonathan McDowell939a9512008-11-04 07:51:38 +00002465 dev_err(dev, "%s - Out of memory\n", __func__);
Johannes Berg19d337d2009-06-02 13:01:37 +02002466
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002467 snprintf(rfkn, 20, "hso-%d",
2468 interface->altsetting->desc.bInterfaceNumber);
Johannes Berg19d337d2009-06-02 13:01:37 +02002469
2470 hso_net->rfkill = rfkill_alloc(rfkn,
2471 &interface_to_usbdev(interface)->dev,
2472 RFKILL_TYPE_WWAN,
2473 &hso_rfkill_ops, hso_dev);
2474 if (!hso_net->rfkill) {
2475 dev_err(dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002476 kfree(rfkn);
Johannes Berg19d337d2009-06-02 13:01:37 +02002477 return;
2478 }
2479 if (rfkill_register(hso_net->rfkill) < 0) {
2480 rfkill_destroy(hso_net->rfkill);
2481 kfree(rfkn);
Jonathan McDowell939a9512008-11-04 07:51:38 +00002482 hso_net->rfkill = NULL;
2483 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002484 return;
2485 }
2486}
2487
Marcel Holtmann384912e2009-08-31 21:08:19 +00002488static struct device_type hso_type = {
2489 .name = "wwan",
2490};
2491
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002492/* Creates our network device */
Jan Dumon0de8ca52009-04-01 22:59:07 +00002493static struct hso_device *hso_create_net_device(struct usb_interface *interface,
2494 int port_spec)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002495{
2496 int result, i;
2497 struct net_device *net;
2498 struct hso_net *hso_net;
2499 struct hso_device *hso_dev;
2500
Jan Dumon0de8ca52009-04-01 22:59:07 +00002501 hso_dev = hso_create_device(interface, port_spec);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002502 if (!hso_dev)
2503 return NULL;
2504
2505 /* allocate our network device, then we can put in our private data */
2506 /* call hso_net_init to do the basic initialization */
2507 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2508 if (!net) {
2509 dev_err(&interface->dev, "Unable to create ethernet device\n");
2510 goto exit;
2511 }
2512
2513 hso_net = netdev_priv(net);
2514
2515 hso_dev->port_data.dev_net = hso_net;
2516 hso_net->net = net;
2517 hso_net->parent = hso_dev;
2518
2519 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2520 USB_DIR_IN);
2521 if (!hso_net->in_endp) {
2522 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2523 goto exit;
2524 }
2525 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2526 USB_DIR_OUT);
2527 if (!hso_net->out_endp) {
2528 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2529 goto exit;
2530 }
2531 SET_NETDEV_DEV(net, &interface->dev);
Marcel Holtmann384912e2009-08-31 21:08:19 +00002532 SET_NETDEV_DEVTYPE(net, &hso_type);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002533
2534 /* registering our net device */
2535 result = register_netdev(net);
2536 if (result) {
2537 dev_err(&interface->dev, "Failed to register device\n");
2538 goto exit;
2539 }
2540
2541 /* start allocating */
2542 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2543 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2544 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2545 dev_err(&interface->dev, "Could not allocate rx urb\n");
2546 goto exit;
2547 }
2548 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2549 GFP_KERNEL);
Joe Perches38673c82013-02-03 17:28:11 +00002550 if (!hso_net->mux_bulk_rx_buf_pool[i])
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002551 goto exit;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002552 }
2553 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2554 if (!hso_net->mux_bulk_tx_urb) {
2555 dev_err(&interface->dev, "Could not allocate tx urb\n");
2556 goto exit;
2557 }
2558 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
Joe Perches38673c82013-02-03 17:28:11 +00002559 if (!hso_net->mux_bulk_tx_buf)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002560 goto exit;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002561
2562 add_net_device(hso_dev);
2563
2564 hso_log_port(hso_dev);
2565
2566 hso_create_rfkill(hso_dev, interface);
2567
2568 return hso_dev;
2569exit:
2570 hso_free_net_device(hso_dev);
2571 return NULL;
2572}
2573
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002574static void hso_free_tiomget(struct hso_serial *serial)
2575{
Jesper Juhl5b89db0e2011-02-13 11:15:35 +00002576 struct hso_tiocmget *tiocmget;
2577 if (!serial)
2578 return;
2579 tiocmget = serial->tiocmget;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002580 if (tiocmget) {
Jesper Juhl5b89db0e2011-02-13 11:15:35 +00002581 usb_free_urb(tiocmget->urb);
2582 tiocmget->urb = NULL;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002583 serial->tiocmget = NULL;
Jan Dumon3b7d2b32009-04-01 22:57:20 +00002584 kfree(tiocmget);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002585 }
2586}
2587
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002588/* Frees an AT channel ( goes for both mux and non-mux ) */
2589static void hso_free_serial_device(struct hso_device *hso_dev)
2590{
2591 struct hso_serial *serial = dev2ser(hso_dev);
2592
2593 if (!serial)
2594 return;
2595 set_serial_by_index(serial->minor, NULL);
2596
2597 hso_serial_common_free(serial);
2598
2599 if (serial->shared_int) {
2600 mutex_lock(&serial->shared_int->shared_int_lock);
2601 if (--serial->shared_int->ref_count == 0)
2602 hso_free_shared_int(serial->shared_int);
2603 else
2604 mutex_unlock(&serial->shared_int->shared_int_lock);
2605 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002606 hso_free_tiomget(serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002607 kfree(serial);
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002608 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002609}
2610
2611/* Creates a bulk AT channel */
2612static struct hso_device *hso_create_bulk_serial_device(
2613 struct usb_interface *interface, int port)
2614{
2615 struct hso_device *hso_dev;
2616 struct hso_serial *serial;
2617 int num_urbs;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002618 struct hso_tiocmget *tiocmget;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002619
2620 hso_dev = hso_create_device(interface, port);
2621 if (!hso_dev)
2622 return NULL;
2623
2624 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2625 if (!serial)
2626 goto exit;
2627
2628 serial->parent = hso_dev;
2629 hso_dev->port_data.dev_serial = serial;
2630
Denis Joseph Barrow58eb17f2009-01-02 13:50:29 +00002631 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002632 num_urbs = 2;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002633 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2634 GFP_KERNEL);
2635 /* it isn't going to break our heart if serial->tiocmget
2636 * allocation fails don't bother checking this.
2637 */
2638 if (serial->tiocmget) {
2639 tiocmget = serial->tiocmget;
2640 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2641 if (tiocmget->urb) {
2642 mutex_init(&tiocmget->mutex);
2643 init_waitqueue_head(&tiocmget->waitq);
2644 tiocmget->endp = hso_get_ep(
2645 interface,
2646 USB_ENDPOINT_XFER_INT,
2647 USB_DIR_IN);
2648 } else
2649 hso_free_tiomget(serial);
2650 }
2651 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002652 else
2653 num_urbs = 1;
2654
2655 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2656 BULK_URB_TX_SIZE))
2657 goto exit;
2658
2659 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2660 USB_DIR_IN);
2661 if (!serial->in_endp) {
2662 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
Adrian Bunke57b6412008-08-28 01:02:37 +03002663 goto exit2;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002664 }
2665
2666 if (!
2667 (serial->out_endp =
2668 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2669 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
Adrian Bunke57b6412008-08-28 01:02:37 +03002670 goto exit2;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002671 }
2672
2673 serial->write_data = hso_std_serial_write_data;
2674
2675 /* and record this serial */
2676 set_serial_by_index(serial->minor, serial);
2677
2678 /* setup the proc dirs and files if needed */
2679 hso_log_port(hso_dev);
2680
2681 /* done, return it */
2682 return hso_dev;
Adrian Bunke57b6412008-08-28 01:02:37 +03002683
2684exit2:
2685 hso_serial_common_free(serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002686exit:
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002687 hso_free_tiomget(serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002688 kfree(serial);
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002689 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002690 return NULL;
2691}
2692
2693/* Creates a multiplexed AT channel */
2694static
2695struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2696 int port,
2697 struct hso_shared_int *mux)
2698{
2699 struct hso_device *hso_dev;
2700 struct hso_serial *serial;
2701 int port_spec;
2702
2703 port_spec = HSO_INTF_MUX;
2704 port_spec &= ~HSO_PORT_MASK;
2705
2706 port_spec |= hso_mux_to_port(port);
2707 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2708 return NULL;
2709
2710 hso_dev = hso_create_device(interface, port_spec);
2711 if (!hso_dev)
2712 return NULL;
2713
2714 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2715 if (!serial)
2716 goto exit;
2717
2718 hso_dev->port_data.dev_serial = serial;
2719 serial->parent = hso_dev;
2720
2721 if (hso_serial_common_create
2722 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2723 goto exit;
2724
2725 serial->tx_data_length--;
2726 serial->write_data = hso_mux_serial_write_data;
2727
2728 serial->shared_int = mux;
2729 mutex_lock(&serial->shared_int->shared_int_lock);
2730 serial->shared_int->ref_count++;
2731 mutex_unlock(&serial->shared_int->shared_int_lock);
2732
2733 /* and record this serial */
2734 set_serial_by_index(serial->minor, serial);
2735
2736 /* setup the proc dirs and files if needed */
2737 hso_log_port(hso_dev);
2738
2739 /* done, return it */
2740 return hso_dev;
2741
2742exit:
2743 if (serial) {
2744 tty_unregister_device(tty_drv, serial->minor);
2745 kfree(serial);
2746 }
2747 if (hso_dev)
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002748 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002749 return NULL;
2750
2751}
2752
2753static void hso_free_shared_int(struct hso_shared_int *mux)
2754{
2755 usb_free_urb(mux->shared_intr_urb);
2756 kfree(mux->shared_intr_buf);
2757 mutex_unlock(&mux->shared_int_lock);
2758 kfree(mux);
2759}
2760
2761static
2762struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2763{
2764 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2765
2766 if (!mux)
2767 return NULL;
2768
2769 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2770 USB_DIR_IN);
2771 if (!mux->intr_endp) {
2772 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2773 goto exit;
2774 }
2775
2776 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2777 if (!mux->shared_intr_urb) {
Jan Dumon8a5c9c42010-01-05 04:53:00 +00002778 dev_err(&interface->dev, "Could not allocate intr urb?\n");
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002779 goto exit;
2780 }
Jan Dumond9ced802010-01-05 04:51:02 +00002781 mux->shared_intr_buf =
2782 kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
2783 GFP_KERNEL);
Joe Perches38673c82013-02-03 17:28:11 +00002784 if (!mux->shared_intr_buf)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002785 goto exit;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002786
2787 mutex_init(&mux->shared_int_lock);
2788
2789 return mux;
2790
2791exit:
2792 kfree(mux->shared_intr_buf);
2793 usb_free_urb(mux->shared_intr_urb);
2794 kfree(mux);
2795 return NULL;
2796}
2797
2798/* Gets the port spec for a certain interface */
2799static int hso_get_config_data(struct usb_interface *interface)
2800{
2801 struct usb_device *usbdev = interface_to_usbdev(interface);
Daniel Gimpeleviche75dc672013-08-21 01:43:19 -07002802 u8 *config_data = kmalloc(17, GFP_KERNEL);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002803 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2804 s32 result;
2805
Daniel Gimpeleviche75dc672013-08-21 01:43:19 -07002806 if (!config_data)
2807 return -ENOMEM;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002808 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2809 0x86, 0xC0, 0, 0, config_data, 17,
2810 USB_CTRL_SET_TIMEOUT) != 0x11) {
Daniel Gimpeleviche75dc672013-08-21 01:43:19 -07002811 kfree(config_data);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002812 return -EIO;
2813 }
2814
2815 switch (config_data[if_num]) {
2816 case 0x0:
2817 result = 0;
2818 break;
2819 case 0x1:
2820 result = HSO_PORT_DIAG;
2821 break;
2822 case 0x2:
2823 result = HSO_PORT_GPS;
2824 break;
2825 case 0x3:
2826 result = HSO_PORT_GPS_CONTROL;
2827 break;
2828 case 0x4:
2829 result = HSO_PORT_APP;
2830 break;
2831 case 0x5:
2832 result = HSO_PORT_APP2;
2833 break;
2834 case 0x6:
2835 result = HSO_PORT_CONTROL;
2836 break;
2837 case 0x7:
2838 result = HSO_PORT_NETWORK;
2839 break;
2840 case 0x8:
2841 result = HSO_PORT_MODEM;
2842 break;
2843 case 0x9:
2844 result = HSO_PORT_MSD;
2845 break;
2846 case 0xa:
2847 result = HSO_PORT_PCSC;
2848 break;
2849 case 0xb:
2850 result = HSO_PORT_VOICE;
2851 break;
2852 default:
2853 result = 0;
2854 }
2855
2856 if (result)
2857 result |= HSO_INTF_BULK;
2858
2859 if (config_data[16] & 0x1)
2860 result |= HSO_INFO_CRC_BUG;
2861
Daniel Gimpeleviche75dc672013-08-21 01:43:19 -07002862 kfree(config_data);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002863 return result;
2864}
2865
2866/* called once for each interface upon device insertion */
2867static int hso_probe(struct usb_interface *interface,
2868 const struct usb_device_id *id)
2869{
2870 int mux, i, if_num, port_spec;
2871 unsigned char port_mask;
2872 struct hso_device *hso_dev = NULL;
2873 struct hso_shared_int *shared_int;
2874 struct hso_device *tmp_dev = NULL;
2875
Daniel Gimpelevich35e57e12013-08-21 01:43:07 -07002876 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2877 dev_err(&interface->dev, "Not our interface\n");
2878 return -ENODEV;
2879 }
2880
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002881 if_num = interface->altsetting->desc.bInterfaceNumber;
2882
2883 /* Get the interface/port specification from either driver_info or from
2884 * the device itself */
2885 if (id->driver_info)
2886 port_spec = ((u32 *)(id->driver_info))[if_num];
2887 else
2888 port_spec = hso_get_config_data(interface);
2889
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002890 /* Check if we need to switch to alt interfaces prior to port
2891 * configuration */
2892 if (interface->num_altsetting > 1)
2893 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2894 interface->needs_remote_wakeup = 1;
2895
2896 /* Allocate new hso device(s) */
2897 switch (port_spec & HSO_INTF_MASK) {
2898 case HSO_INTF_MUX:
2899 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2900 /* Create the network device */
2901 if (!disable_net) {
Jan Dumon0de8ca52009-04-01 22:59:07 +00002902 hso_dev = hso_create_net_device(interface,
2903 port_spec);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002904 if (!hso_dev)
2905 goto exit;
2906 tmp_dev = hso_dev;
2907 }
2908 }
2909
2910 if (hso_get_mux_ports(interface, &port_mask))
2911 /* TODO: de-allocate everything */
2912 goto exit;
2913
2914 shared_int = hso_create_shared_int(interface);
2915 if (!shared_int)
2916 goto exit;
2917
2918 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2919 if (port_mask & i) {
2920 hso_dev = hso_create_mux_serial_device(
2921 interface, i, shared_int);
2922 if (!hso_dev)
2923 goto exit;
2924 }
2925 }
2926
2927 if (tmp_dev)
2928 hso_dev = tmp_dev;
2929 break;
2930
2931 case HSO_INTF_BULK:
2932 /* It's a regular bulk interface */
Filip Aben8e65c0e2010-11-25 03:40:50 +00002933 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2934 if (!disable_net)
2935 hso_dev =
2936 hso_create_net_device(interface, port_spec);
2937 } else {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002938 hso_dev =
2939 hso_create_bulk_serial_device(interface, port_spec);
Filip Aben8e65c0e2010-11-25 03:40:50 +00002940 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002941 if (!hso_dev)
2942 goto exit;
2943 break;
2944 default:
2945 goto exit;
2946 }
2947
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002948 /* save our data pointer in this device */
2949 usb_set_intfdata(interface, hso_dev);
2950
2951 /* done */
2952 return 0;
2953exit:
2954 hso_free_interface(interface);
2955 return -ENODEV;
2956}
2957
2958/* device removed, cleaning up */
2959static void hso_disconnect(struct usb_interface *interface)
2960{
2961 hso_free_interface(interface);
2962
2963 /* remove reference of our private data */
2964 usb_set_intfdata(interface, NULL);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002965}
2966
2967static void async_get_intf(struct work_struct *data)
2968{
2969 struct hso_device *hso_dev =
2970 container_of(data, struct hso_device, async_get_intf);
2971 usb_autopm_get_interface(hso_dev->interface);
2972}
2973
2974static void async_put_intf(struct work_struct *data)
2975{
2976 struct hso_device *hso_dev =
2977 container_of(data, struct hso_device, async_put_intf);
2978 usb_autopm_put_interface(hso_dev->interface);
2979}
2980
2981static int hso_get_activity(struct hso_device *hso_dev)
2982{
2983 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
2984 if (!hso_dev->is_active) {
2985 hso_dev->is_active = 1;
2986 schedule_work(&hso_dev->async_get_intf);
2987 }
2988 }
2989
2990 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
2991 return -EAGAIN;
2992
2993 usb_mark_last_busy(hso_dev->usb);
2994
2995 return 0;
2996}
2997
2998static int hso_put_activity(struct hso_device *hso_dev)
2999{
3000 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3001 if (hso_dev->is_active) {
3002 hso_dev->is_active = 0;
3003 schedule_work(&hso_dev->async_put_intf);
3004 return -EAGAIN;
3005 }
3006 }
3007 hso_dev->is_active = 0;
3008 return 0;
3009}
3010
3011/* called by kernel when we need to suspend device */
3012static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3013{
3014 int i, result;
3015
3016 /* Stop all serial ports */
3017 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3018 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3019 result = hso_stop_serial_device(serial_table[i]);
3020 if (result)
3021 goto out;
3022 }
3023 }
3024
3025 /* Stop all network ports */
3026 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3027 if (network_table[i] &&
3028 (network_table[i]->interface == iface)) {
3029 result = hso_stop_net_device(network_table[i]);
3030 if (result)
3031 goto out;
3032 }
3033 }
3034
3035out:
3036 return 0;
3037}
3038
3039/* called by kernel when we need to resume device */
3040static int hso_resume(struct usb_interface *iface)
3041{
3042 int i, result = 0;
3043 struct hso_net *hso_net;
3044
3045 /* Start all serial ports */
3046 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3047 if (serial_table[i] && (serial_table[i]->interface == iface)) {
Jiri Slaby5ce76e72012-04-02 13:54:05 +02003048 if (dev2ser(serial_table[i])->port.count) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003049 result =
3050 hso_start_serial_device(serial_table[i], GFP_NOIO);
3051 hso_kick_transmit(dev2ser(serial_table[i]));
3052 if (result)
3053 goto out;
3054 }
3055 }
3056 }
3057
3058 /* Start all network ports */
3059 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3060 if (network_table[i] &&
3061 (network_table[i]->interface == iface)) {
3062 hso_net = dev2net(network_table[i]);
Denis Joseph Barrow89930b72008-11-25 00:30:48 -08003063 if (hso_net->flags & IFF_UP) {
3064 /* First transmit any lingering data,
3065 then restart the device. */
3066 if (hso_net->skb_tx_buf) {
3067 dev_dbg(&iface->dev,
3068 "Transmitting"
3069 " lingering data\n");
3070 hso_net_start_xmit(hso_net->skb_tx_buf,
3071 hso_net->net);
3072 hso_net->skb_tx_buf = NULL;
3073 }
3074 result = hso_start_net_device(network_table[i]);
3075 if (result)
3076 goto out;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003077 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003078 }
3079 }
3080
3081out:
3082 return result;
3083}
3084
Jan Dumon68a351c2010-01-05 04:52:13 +00003085static void reset_device(struct work_struct *data)
3086{
3087 struct hso_device *hso_dev =
3088 container_of(data, struct hso_device, reset_device);
3089 struct usb_device *usb = hso_dev->usb;
3090 int result;
3091
3092 if (hso_dev->usb_gone) {
3093 D1("No reset during disconnect\n");
3094 } else {
3095 result = usb_lock_device_for_reset(usb, hso_dev->interface);
3096 if (result < 0)
3097 D1("unable to lock device for reset: %d\n", result);
3098 else {
3099 usb_reset_device(usb);
3100 usb_unlock_device(usb);
3101 }
3102 }
3103}
3104
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003105static void hso_serial_ref_free(struct kref *ref)
3106{
3107 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3108
3109 hso_free_serial_device(hso_dev);
3110}
3111
3112static void hso_free_interface(struct usb_interface *interface)
3113{
3114 struct hso_serial *hso_dev;
3115 int i;
3116
3117 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
Joe Perches8e95a202009-12-03 07:58:21 +00003118 if (serial_table[i] &&
3119 (serial_table[i]->interface == interface)) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003120 hso_dev = dev2ser(serial_table[i]);
Jiri Slabyaa27a092013-03-07 13:12:30 +01003121 tty_port_tty_hangup(&hso_dev->port, false);
David S. Millerab153d82008-11-25 03:52:46 -08003122 mutex_lock(&hso_dev->parent->mutex);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003123 hso_dev->parent->usb_gone = 1;
David S. Millerab153d82008-11-25 03:52:46 -08003124 mutex_unlock(&hso_dev->parent->mutex);
3125 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003126 }
3127 }
3128
3129 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
Joe Perches8e95a202009-12-03 07:58:21 +00003130 if (network_table[i] &&
3131 (network_table[i]->interface == interface)) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003132 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3133 /* hso_stop_net_device doesn't stop the net queue since
3134 * traffic needs to start it again when suspended */
3135 netif_stop_queue(dev2net(network_table[i])->net);
3136 hso_stop_net_device(network_table[i]);
3137 cancel_work_sync(&network_table[i]->async_put_intf);
3138 cancel_work_sync(&network_table[i]->async_get_intf);
Johannes Berg19d337d2009-06-02 13:01:37 +02003139 if (rfk) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003140 rfkill_unregister(rfk);
Johannes Berg19d337d2009-06-02 13:01:37 +02003141 rfkill_destroy(rfk);
3142 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003143 hso_free_net_device(network_table[i]);
3144 }
3145 }
3146}
3147
3148/* Helper functions */
3149
3150/* Get the endpoint ! */
3151static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3152 int type, int dir)
3153{
3154 int i;
3155 struct usb_host_interface *iface = intf->cur_altsetting;
3156 struct usb_endpoint_descriptor *endp;
3157
3158 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3159 endp = &iface->endpoint[i].desc;
3160 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
Julia Lawallf201a8a2008-12-29 00:21:07 +00003161 (usb_endpoint_type(endp) == type))
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003162 return endp;
3163 }
3164
3165 return NULL;
3166}
3167
3168/* Get the byte that describes which ports are enabled */
3169static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3170{
3171 int i;
3172 struct usb_host_interface *iface = intf->cur_altsetting;
3173
3174 if (iface->extralen == 3) {
3175 *ports = iface->extra[2];
3176 return 0;
3177 }
3178
3179 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3180 if (iface->endpoint[i].extralen == 3) {
3181 *ports = iface->endpoint[i].extra[2];
3182 return 0;
3183 }
3184 }
3185
3186 return -1;
3187}
3188
3189/* interrupt urb needs to be submitted, used for serial read of muxed port */
3190static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3191 struct usb_device *usb, gfp_t gfp)
3192{
3193 int result;
3194
3195 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3196 usb_rcvintpipe(usb,
3197 shared_int->intr_endp->bEndpointAddress & 0x7F),
3198 shared_int->shared_intr_buf,
Jan Dumond9ced802010-01-05 04:51:02 +00003199 1,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003200 intr_callback, shared_int,
3201 shared_int->intr_endp->bInterval);
3202
3203 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3204 if (result)
Jan Dumon8a5c9c42010-01-05 04:53:00 +00003205 dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003206 result);
3207
3208 return result;
3209}
3210
3211/* operations setup of the serial interface */
Greg Kroah-Hartman6c59f562008-08-08 12:02:14 -07003212static const struct tty_operations hso_serial_ops = {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003213 .open = hso_serial_open,
3214 .close = hso_serial_close,
3215 .write = hso_serial_write,
3216 .write_room = hso_serial_write_room,
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00003217 .ioctl = hso_serial_ioctl,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003218 .set_termios = hso_serial_set_termios,
3219 .chars_in_buffer = hso_serial_chars_in_buffer,
3220 .tiocmget = hso_serial_tiocmget,
3221 .tiocmset = hso_serial_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01003222 .get_icount = hso_get_count,
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02003223 .unthrottle = hso_unthrottle
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003224};
3225
3226static struct usb_driver hso_driver = {
3227 .name = driver_name,
3228 .probe = hso_probe,
3229 .disconnect = hso_disconnect,
3230 .id_table = hso_ids,
3231 .suspend = hso_suspend,
3232 .resume = hso_resume,
Denis Joseph Barrow9c8f92a2008-11-25 00:36:10 -08003233 .reset_resume = hso_resume,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003234 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003235 .disable_hub_initiated_lpm = 1,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003236};
3237
3238static int __init hso_init(void)
3239{
3240 int i;
3241 int result;
3242
3243 /* put it in the log */
3244 printk(KERN_INFO "hso: %s\n", version);
3245
3246 /* Initialise the serial table semaphore and table */
3247 spin_lock_init(&serial_table_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003248 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3249 serial_table[i] = NULL;
3250
3251 /* allocate our driver using the proper amount of supported minors */
3252 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3253 if (!tty_drv)
3254 return -ENOMEM;
3255
3256 /* fill in all needed values */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003257 tty_drv->driver_name = driver_name;
3258 tty_drv->name = tty_filename;
3259
3260 /* if major number is provided as parameter, use that one */
3261 if (tty_major)
3262 tty_drv->major = tty_major;
3263
3264 tty_drv->minor_start = 0;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003265 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3266 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3267 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3268 tty_drv->init_termios = tty_std_termios;
Alan Coxac9720c2009-01-02 13:47:45 +00003269 hso_init_termios(&tty_drv->init_termios);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003270 tty_set_operations(tty_drv, &hso_serial_ops);
3271
3272 /* register the tty driver */
3273 result = tty_register_driver(tty_drv);
3274 if (result) {
3275 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3276 __func__, result);
Jiri Slabyd2307882012-04-02 13:54:04 +02003277 goto err_free_tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003278 }
3279
3280 /* register this module as an usb driver */
3281 result = usb_register(&hso_driver);
3282 if (result) {
3283 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3284 result);
Jiri Slabyd2307882012-04-02 13:54:04 +02003285 goto err_unreg_tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003286 }
3287
3288 /* done */
3289 return 0;
Jiri Slabyd2307882012-04-02 13:54:04 +02003290err_unreg_tty:
3291 tty_unregister_driver(tty_drv);
3292err_free_tty:
3293 put_tty_driver(tty_drv);
3294 return result;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003295}
3296
3297static void __exit hso_exit(void)
3298{
3299 printk(KERN_INFO "hso: unloaded\n");
3300
3301 tty_unregister_driver(tty_drv);
Jiri Slabyd2307882012-04-02 13:54:04 +02003302 put_tty_driver(tty_drv);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003303 /* deregister the usb driver */
3304 usb_deregister(&hso_driver);
3305}
3306
3307/* Module definitions */
3308module_init(hso_init);
3309module_exit(hso_exit);
3310
3311MODULE_AUTHOR(MOD_AUTHOR);
3312MODULE_DESCRIPTION(MOD_DESCRIPTION);
3313MODULE_LICENSE(MOD_LICENSE);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003314
3315/* change the debug level (eg: insmod hso.ko debug=0x04) */
3316MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3317module_param(debug, int, S_IRUGO | S_IWUSR);
3318
3319/* set the major tty number (eg: insmod hso.ko tty_major=245) */
3320MODULE_PARM_DESC(tty_major, "Set the major tty number");
3321module_param(tty_major, int, S_IRUGO | S_IWUSR);
3322
3323/* disable network interface (eg: insmod hso.ko disable_net=1) */
3324MODULE_PARM_DESC(disable_net, "Disable the network interface");
3325module_param(disable_net, int, S_IRUGO | S_IWUSR);