blob: ec782c7eabd86667a1e6294bcb6e9cc69c3da862 [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)
188#define W_INDEX (0x2)
189#define W_LENGTH (0x2)
190
191#define B_OVERRUN (0x1<<6)
192#define B_PARITY (0x1<<5)
193#define B_FRAMING (0x1<<4)
194#define B_RING_SIGNAL (0x1<<3)
195#define B_BREAK (0x1<<2)
196#define B_TX_CARRIER (0x1<<1)
197#define B_RX_CARRIER (0x1<<0)
198
199struct hso_serial_state_notification {
200 u8 bmRequestType;
201 u8 bNotification;
202 u16 wValue;
203 u16 wIndex;
204 u16 wLength;
205 u16 UART_state_bitmap;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000206} __packed;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000207
208struct hso_tiocmget {
209 struct mutex mutex;
210 wait_queue_head_t waitq;
211 int intr_completed;
212 struct usb_endpoint_descriptor *endp;
213 struct urb *urb;
214 struct hso_serial_state_notification serial_state_notification;
215 u16 prev_UART_state_bitmap;
216 struct uart_icount icount;
217};
218
219
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700220struct hso_serial {
221 struct hso_device *parent;
222 int magic;
223 u8 minor;
224
225 struct hso_shared_int *shared_int;
226
227 /* rx/tx urb could be either a bulk urb or a control urb depending
228 on which serial port it is used on. */
229 struct urb *rx_urb[MAX_RX_URBS];
230 u8 num_rx_urbs;
231 u8 *rx_data[MAX_RX_URBS];
232 u16 rx_data_length; /* should contain allocated length */
233
234 struct urb *tx_urb;
235 u8 *tx_data;
236 u8 *tx_buffer;
237 u16 tx_data_length; /* should contain allocated length */
238 u16 tx_data_count;
239 u16 tx_buffer_count;
240 struct usb_ctrlrequest ctrl_req_tx;
241 struct usb_ctrlrequest ctrl_req_rx;
242
243 struct usb_endpoint_descriptor *in_endp;
244 struct usb_endpoint_descriptor *out_endp;
245
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200246 enum rx_ctrl_state rx_state;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700247 u8 rts_state;
248 u8 dtr_state;
249 unsigned tx_urb_used:1;
250
Jiri Slaby5ce76e72012-04-02 13:54:05 +0200251 struct tty_port port;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700252 /* from usb_serial_port */
253 struct tty_struct *tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700254 spinlock_t serial_lock;
255
256 int (*write_data) (struct hso_serial *serial);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000257 struct hso_tiocmget *tiocmget;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200258 /* Hacks required to get flow control
259 * working on the serial receive buffers
260 * so as not to drop characters on the floor.
261 */
262 int curr_rx_urb_idx;
263 u16 curr_rx_urb_offset;
264 u8 rx_urb_filled[MAX_RX_URBS];
265 struct tasklet_struct unthrottle_tasklet;
266 struct work_struct retry_unthrottle_workqueue;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700267};
268
269struct hso_device {
270 union {
271 struct hso_serial *dev_serial;
272 struct hso_net *dev_net;
273 } port_data;
274
275 u32 port_spec;
276
277 u8 is_active;
278 u8 usb_gone;
279 struct work_struct async_get_intf;
280 struct work_struct async_put_intf;
Jan Dumon68a351c2010-01-05 04:52:13 +0000281 struct work_struct reset_device;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700282
283 struct usb_device *usb;
284 struct usb_interface *interface;
285
286 struct device *dev;
287 struct kref ref;
David S. Millerab153d82008-11-25 03:52:46 -0800288 struct mutex mutex;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700289};
290
291/* Type of interface */
292#define HSO_INTF_MASK 0xFF00
293#define HSO_INTF_MUX 0x0100
294#define HSO_INTF_BULK 0x0200
295
296/* Type of port */
297#define HSO_PORT_MASK 0xFF
298#define HSO_PORT_NO_PORT 0x0
299#define HSO_PORT_CONTROL 0x1
300#define HSO_PORT_APP 0x2
301#define HSO_PORT_GPS 0x3
302#define HSO_PORT_PCSC 0x4
303#define HSO_PORT_APP2 0x5
304#define HSO_PORT_GPS_CONTROL 0x6
305#define HSO_PORT_MSD 0x7
306#define HSO_PORT_VOICE 0x8
307#define HSO_PORT_DIAG2 0x9
308#define HSO_PORT_DIAG 0x10
309#define HSO_PORT_MODEM 0x11
310#define HSO_PORT_NETWORK 0x12
311
312/* Additional device info */
313#define HSO_INFO_MASK 0xFF000000
314#define HSO_INFO_CRC_BUG 0x01000000
315
316/*****************************************************************************/
317/* Prototypes */
318/*****************************************************************************/
319/* Serial driver functions */
Alan Cox20b9d172011-02-14 16:26:50 +0000320static int hso_serial_tiocmset(struct tty_struct *tty,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700321 unsigned int set, unsigned int clear);
322static void ctrl_callback(struct urb *urb);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200323static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700324static void hso_kick_transmit(struct hso_serial *serial);
325/* Helper functions */
326static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
327 struct usb_device *usb, gfp_t gfp);
Jan Dumon68a351c2010-01-05 04:52:13 +0000328static void handle_usb_error(int status, const char *function,
329 struct hso_device *hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700330static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
331 int type, int dir);
332static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
333static void hso_free_interface(struct usb_interface *intf);
334static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
335static int hso_stop_serial_device(struct hso_device *hso_dev);
336static int hso_start_net_device(struct hso_device *hso_dev);
337static void hso_free_shared_int(struct hso_shared_int *shared_int);
338static int hso_stop_net_device(struct hso_device *hso_dev);
339static void hso_serial_ref_free(struct kref *ref);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200340static void hso_std_serial_read_bulk_callback(struct urb *urb);
341static int hso_mux_serial_read(struct hso_serial *serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700342static void async_get_intf(struct work_struct *data);
343static void async_put_intf(struct work_struct *data);
344static int hso_put_activity(struct hso_device *hso_dev);
345static int hso_get_activity(struct hso_device *hso_dev);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +0000346static void tiocmget_intr_callback(struct urb *urb);
Jan Dumon68a351c2010-01-05 04:52:13 +0000347static void reset_device(struct work_struct *data);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700348/*****************************************************************************/
349/* Helping functions */
350/*****************************************************************************/
351
352/* #define DEBUG */
353
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700354static inline struct hso_net *dev2net(struct hso_device *hso_dev)
355{
356 return hso_dev->port_data.dev_net;
357}
358
359static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
360{
361 return hso_dev->port_data.dev_serial;
362}
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700363
364/* Debugging functions */
365#ifdef DEBUG
366static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
367 unsigned int len)
368{
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700369 static char name[255];
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700370
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700371 sprintf(name, "hso[%d:%s]", line_count, func_name);
372 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700373}
374
375#define DUMP(buf_, len_) \
Antti Kaijanmäki9ce673d2009-11-23 10:54:24 -0800376 dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700377
378#define DUMP1(buf_, len_) \
379 do { \
380 if (0x01 & debug) \
381 DUMP(buf_, len_); \
382 } while (0)
383#else
384#define DUMP(buf_, len_)
385#define DUMP1(buf_, len_)
386#endif
387
388/* module parameters */
389static int debug;
390static int tty_major;
391static int disable_net;
392
393/* driver info */
394static const char driver_name[] = "hso";
395static const char tty_filename[] = "ttyHS";
Filip Aben242647b2010-07-12 21:21:27 -0700396static const char *version = __FILE__ ": " MOD_AUTHOR;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700397/* the usb driver itself (registered in hso_init) */
398static struct usb_driver hso_driver;
399/* serial structures */
400static struct tty_driver *tty_drv;
401static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
402static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
403static spinlock_t serial_table_lock;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700404
405static const s32 default_port_spec[] = {
406 HSO_INTF_MUX | HSO_PORT_NETWORK,
407 HSO_INTF_BULK | HSO_PORT_DIAG,
408 HSO_INTF_BULK | HSO_PORT_MODEM,
409 0
410};
411
412static const s32 icon321_port_spec[] = {
413 HSO_INTF_MUX | HSO_PORT_NETWORK,
414 HSO_INTF_BULK | HSO_PORT_DIAG2,
415 HSO_INTF_BULK | HSO_PORT_MODEM,
416 HSO_INTF_BULK | HSO_PORT_DIAG,
417 0
418};
419
420#define default_port_device(vendor, product) \
421 USB_DEVICE(vendor, product), \
422 .driver_info = (kernel_ulong_t)default_port_spec
423
424#define icon321_port_device(vendor, product) \
425 USB_DEVICE(vendor, product), \
426 .driver_info = (kernel_ulong_t)icon321_port_spec
427
428/* list of devices we support */
429static const struct usb_device_id hso_ids[] = {
430 {default_port_device(0x0af0, 0x6711)},
431 {default_port_device(0x0af0, 0x6731)},
432 {default_port_device(0x0af0, 0x6751)},
433 {default_port_device(0x0af0, 0x6771)},
434 {default_port_device(0x0af0, 0x6791)},
435 {default_port_device(0x0af0, 0x6811)},
436 {default_port_device(0x0af0, 0x6911)},
437 {default_port_device(0x0af0, 0x6951)},
438 {default_port_device(0x0af0, 0x6971)},
439 {default_port_device(0x0af0, 0x7011)},
440 {default_port_device(0x0af0, 0x7031)},
441 {default_port_device(0x0af0, 0x7051)},
442 {default_port_device(0x0af0, 0x7071)},
443 {default_port_device(0x0af0, 0x7111)},
444 {default_port_device(0x0af0, 0x7211)},
445 {default_port_device(0x0af0, 0x7251)},
446 {default_port_device(0x0af0, 0x7271)},
447 {default_port_device(0x0af0, 0x7311)},
448 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
449 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
450 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
Denis Joseph Barrow95eacee2008-08-19 18:07:52 -0700451 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700452 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
453 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
Filip Aben67dd8242009-02-03 15:13:26 -0800454 {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700455 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
456 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
457 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
Denis Joseph Barrowbab04c32008-11-25 00:26:12 -0800458 {USB_DEVICE(0x0af0, 0x7701)},
Jan Dumonec157932010-01-05 04:50:31 +0000459 {USB_DEVICE(0x0af0, 0x7706)},
Denis Joseph Barrowbab04c32008-11-25 00:26:12 -0800460 {USB_DEVICE(0x0af0, 0x7801)},
461 {USB_DEVICE(0x0af0, 0x7901)},
Jan Dumonec157932010-01-05 04:50:31 +0000462 {USB_DEVICE(0x0af0, 0x7A01)},
463 {USB_DEVICE(0x0af0, 0x7A05)},
Jan Dumon9961d8422009-04-02 01:16:44 -0700464 {USB_DEVICE(0x0af0, 0x8200)},
465 {USB_DEVICE(0x0af0, 0x8201)},
Jan Dumonec157932010-01-05 04:50:31 +0000466 {USB_DEVICE(0x0af0, 0x8300)},
467 {USB_DEVICE(0x0af0, 0x8302)},
468 {USB_DEVICE(0x0af0, 0x8304)},
469 {USB_DEVICE(0x0af0, 0x8400)},
Filip Abendd7496f2010-05-25 16:09:23 -0700470 {USB_DEVICE(0x0af0, 0x8600)},
471 {USB_DEVICE(0x0af0, 0x8800)},
472 {USB_DEVICE(0x0af0, 0x8900)},
Filip Aben5c7bf2f2010-08-03 05:36:41 +0000473 {USB_DEVICE(0x0af0, 0x9000)},
Jan Dumon9961d8422009-04-02 01:16:44 -0700474 {USB_DEVICE(0x0af0, 0xd035)},
Filip Aben67dd8242009-02-03 15:13:26 -0800475 {USB_DEVICE(0x0af0, 0xd055)},
Jan Dumon9961d8422009-04-02 01:16:44 -0700476 {USB_DEVICE(0x0af0, 0xd155)},
477 {USB_DEVICE(0x0af0, 0xd255)},
478 {USB_DEVICE(0x0af0, 0xd057)},
479 {USB_DEVICE(0x0af0, 0xd157)},
480 {USB_DEVICE(0x0af0, 0xd257)},
481 {USB_DEVICE(0x0af0, 0xd357)},
Jan Dumonec157932010-01-05 04:50:31 +0000482 {USB_DEVICE(0x0af0, 0xd058)},
483 {USB_DEVICE(0x0af0, 0xc100)},
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700484 {}
485};
486MODULE_DEVICE_TABLE(usb, hso_ids);
487
488/* Sysfs attribute */
489static ssize_t hso_sysfs_show_porttype(struct device *dev,
490 struct device_attribute *attr,
491 char *buf)
492{
Greg Kroah-Hartman1aec5bd2009-04-30 12:19:31 +0000493 struct hso_device *hso_dev = dev_get_drvdata(dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700494 char *port_name;
495
496 if (!hso_dev)
497 return 0;
498
499 switch (hso_dev->port_spec & HSO_PORT_MASK) {
500 case HSO_PORT_CONTROL:
501 port_name = "Control";
502 break;
503 case HSO_PORT_APP:
504 port_name = "Application";
505 break;
506 case HSO_PORT_APP2:
507 port_name = "Application2";
508 break;
509 case HSO_PORT_GPS:
510 port_name = "GPS";
511 break;
512 case HSO_PORT_GPS_CONTROL:
513 port_name = "GPS Control";
514 break;
515 case HSO_PORT_PCSC:
516 port_name = "PCSC";
517 break;
518 case HSO_PORT_DIAG:
519 port_name = "Diagnostic";
520 break;
521 case HSO_PORT_DIAG2:
522 port_name = "Diagnostic2";
523 break;
524 case HSO_PORT_MODEM:
525 port_name = "Modem";
526 break;
527 case HSO_PORT_NETWORK:
528 port_name = "Network";
529 break;
530 default:
531 port_name = "Unknown";
532 break;
533 }
534
535 return sprintf(buf, "%s\n", port_name);
536}
537static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
538
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +0200539static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
540{
541 int idx;
542
543 for (idx = 0; idx < serial->num_rx_urbs; idx++)
544 if (serial->rx_urb[idx] == urb)
545 return idx;
546 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
547 return -1;
548}
549
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700550/* converts mux value to a port spec value */
551static u32 hso_mux_to_port(int mux)
552{
553 u32 result;
554
555 switch (mux) {
556 case 0x1:
557 result = HSO_PORT_CONTROL;
558 break;
559 case 0x2:
560 result = HSO_PORT_APP;
561 break;
562 case 0x4:
563 result = HSO_PORT_PCSC;
564 break;
565 case 0x8:
566 result = HSO_PORT_GPS;
567 break;
568 case 0x10:
569 result = HSO_PORT_APP2;
570 break;
571 default:
572 result = HSO_PORT_NO_PORT;
573 }
574 return result;
575}
576
577/* converts port spec value to a mux value */
578static u32 hso_port_to_mux(int port)
579{
580 u32 result;
581
582 switch (port & HSO_PORT_MASK) {
583 case HSO_PORT_CONTROL:
584 result = 0x0;
585 break;
586 case HSO_PORT_APP:
587 result = 0x1;
588 break;
589 case HSO_PORT_PCSC:
590 result = 0x2;
591 break;
592 case HSO_PORT_GPS:
593 result = 0x3;
594 break;
595 case HSO_PORT_APP2:
596 result = 0x4;
597 break;
598 default:
599 result = 0x0;
600 }
601 return result;
602}
603
604static struct hso_serial *get_serial_by_shared_int_and_type(
605 struct hso_shared_int *shared_int,
606 int mux)
607{
608 int i, port;
609
610 port = hso_mux_to_port(mux);
611
612 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
Joe Perches8e95a202009-12-03 07:58:21 +0000613 if (serial_table[i] &&
614 (dev2ser(serial_table[i])->shared_int == shared_int) &&
615 ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700616 return dev2ser(serial_table[i]);
617 }
618 }
619
620 return NULL;
621}
622
623static struct hso_serial *get_serial_by_index(unsigned index)
624{
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700625 struct hso_serial *serial = NULL;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700626 unsigned long flags;
627
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700628 spin_lock_irqsave(&serial_table_lock, flags);
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700629 if (serial_table[index])
630 serial = dev2ser(serial_table[index]);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700631 spin_unlock_irqrestore(&serial_table_lock, flags);
632
633 return serial;
634}
635
636static int get_free_serial_index(void)
637{
638 int index;
639 unsigned long flags;
640
641 spin_lock_irqsave(&serial_table_lock, flags);
642 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
643 if (serial_table[index] == NULL) {
644 spin_unlock_irqrestore(&serial_table_lock, flags);
645 return index;
646 }
647 }
648 spin_unlock_irqrestore(&serial_table_lock, flags);
649
650 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
651 return -1;
652}
653
654static void set_serial_by_index(unsigned index, struct hso_serial *serial)
655{
656 unsigned long flags;
Greg Kroah-Hartman0235f642008-08-08 12:02:57 -0700657
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700658 spin_lock_irqsave(&serial_table_lock, flags);
659 if (serial)
660 serial_table[index] = serial->parent;
661 else
662 serial_table[index] = NULL;
663 spin_unlock_irqrestore(&serial_table_lock, flags);
664}
665
Jan Dumon68a351c2010-01-05 04:52:13 +0000666static void handle_usb_error(int status, const char *function,
667 struct hso_device *hso_dev)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700668{
669 char *explanation;
670
671 switch (status) {
672 case -ENODEV:
673 explanation = "no device";
674 break;
675 case -ENOENT:
676 explanation = "endpoint not enabled";
677 break;
678 case -EPIPE:
679 explanation = "endpoint stalled";
680 break;
681 case -ENOSPC:
682 explanation = "not enough bandwidth";
683 break;
684 case -ESHUTDOWN:
685 explanation = "device disabled";
686 break;
687 case -EHOSTUNREACH:
688 explanation = "device suspended";
689 break;
690 case -EINVAL:
691 case -EAGAIN:
692 case -EFBIG:
693 case -EMSGSIZE:
694 explanation = "internal error";
695 break;
Jan Dumon68a351c2010-01-05 04:52:13 +0000696 case -EILSEQ:
697 case -EPROTO:
698 case -ETIME:
699 case -ETIMEDOUT:
700 explanation = "protocol error";
701 if (hso_dev)
702 schedule_work(&hso_dev->reset_device);
703 break;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700704 default:
705 explanation = "unknown status";
706 break;
707 }
Jan Dumon68a351c2010-01-05 04:52:13 +0000708
709 /* log a meaningful explanation of an USB status */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700710 D1("%s: received USB status - %s (%d)", function, explanation, status);
711}
712
713/* Network interface functions */
714
715/* called when net interface is brought up by ifconfig */
716static int hso_net_open(struct net_device *net)
717{
718 struct hso_net *odev = netdev_priv(net);
719 unsigned long flags = 0;
720
721 if (!odev) {
722 dev_err(&net->dev, "No net device !\n");
723 return -ENODEV;
724 }
725
726 odev->skb_tx_buf = NULL;
727
728 /* setup environment */
729 spin_lock_irqsave(&odev->net_lock, flags);
730 odev->rx_parse_state = WAIT_IP;
731 odev->rx_buf_size = 0;
732 odev->rx_buf_missing = sizeof(struct iphdr);
733 spin_unlock_irqrestore(&odev->net_lock, flags);
734
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700735 /* We are up and running. */
736 set_bit(HSO_NET_RUNNING, &odev->flags);
Oliver Neukum889bd9b2008-12-18 03:57:35 +0000737 hso_start_net_device(odev->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700738
739 /* Tell the kernel we are ready to start receiving from it */
740 netif_start_queue(net);
741
742 return 0;
743}
744
745/* called when interface is brought down by ifconfig */
746static int hso_net_close(struct net_device *net)
747{
748 struct hso_net *odev = netdev_priv(net);
749
750 /* we don't need the queue anymore */
751 netif_stop_queue(net);
752 /* no longer running */
753 clear_bit(HSO_NET_RUNNING, &odev->flags);
754
755 hso_stop_net_device(odev->parent);
756
757 /* done */
758 return 0;
759}
760
761/* USB tells is xmit done, we should start the netqueue again */
762static void write_bulk_callback(struct urb *urb)
763{
764 struct hso_net *odev = urb->context;
765 int status = urb->status;
766
767 /* Sanity check */
768 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
769 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
770 return;
771 }
772
773 /* Do we still have a valid kernel network device? */
774 if (!netif_device_present(odev->net)) {
775 dev_err(&urb->dev->dev, "%s: net device not present\n",
776 __func__);
777 return;
778 }
779
780 /* log status, but don't act on it, we don't need to resubmit anything
781 * anyhow */
782 if (status)
Jan Dumon68a351c2010-01-05 04:52:13 +0000783 handle_usb_error(status, __func__, odev->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700784
785 hso_put_activity(odev->parent);
786
787 /* Tell the network interface we are ready for another frame */
788 netif_wake_queue(odev->net);
789}
790
791/* called by kernel when we need to transmit a packet */
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000792static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
793 struct net_device *net)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700794{
795 struct hso_net *odev = netdev_priv(net);
796 int result;
797
798 /* Tell the kernel, "No more frames 'til we are done with this one." */
799 netif_stop_queue(net);
800 if (hso_get_activity(odev->parent) == -EAGAIN) {
801 odev->skb_tx_buf = skb;
Patrick McHardy6ed10652009-06-23 06:03:08 +0000802 return NETDEV_TX_OK;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700803 }
804
805 /* log if asked */
806 DUMP1(skb->data, skb->len);
807 /* Copy it from kernel memory to OUR memory */
808 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
809 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
810
811 /* Fill in the URB for shipping it out. */
812 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
813 odev->parent->usb,
814 usb_sndbulkpipe(odev->parent->usb,
815 odev->out_endp->
816 bEndpointAddress & 0x7F),
817 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
818 odev);
819
820 /* Deal with the Zero Length packet problem, I hope */
821 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
822
823 /* Send the URB on its merry way. */
824 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
825 if (result) {
826 dev_warn(&odev->parent->interface->dev,
Jan Dumon8a5c9c42010-01-05 04:53:00 +0000827 "failed mux_bulk_tx_urb %d\n", result);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700828 net->stats.tx_errors++;
829 netif_start_queue(net);
830 } else {
831 net->stats.tx_packets++;
832 net->stats.tx_bytes += skb->len;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700833 }
834 dev_kfree_skb(skb);
835 /* we're done */
Patrick McHardy5b2c4b92009-06-12 06:14:36 +0000836 return NETDEV_TX_OK;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700837}
838
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700839static const struct ethtool_ops ops = {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700840 .get_link = ethtool_op_get_link
841};
842
843/* called when a packet did not ack after watchdogtimeout */
844static void hso_net_tx_timeout(struct net_device *net)
845{
846 struct hso_net *odev = netdev_priv(net);
847
848 if (!odev)
849 return;
850
851 /* Tell syslog we are hosed. */
852 dev_warn(&net->dev, "Tx timed out.\n");
853
854 /* Tear the waiting frame off the list */
Joe Perches8e95a202009-12-03 07:58:21 +0000855 if (odev->mux_bulk_tx_urb &&
856 (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700857 usb_unlink_urb(odev->mux_bulk_tx_urb);
858
859 /* Update statistics */
860 net->stats.tx_errors++;
861}
862
863/* make a real packet from the received USB buffer */
864static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
865 unsigned int count, unsigned char is_eop)
866{
867 unsigned short temp_bytes;
868 unsigned short buffer_offset = 0;
869 unsigned short frame_len;
870 unsigned char *tmp_rx_buf;
871
872 /* log if needed */
873 D1("Rx %d bytes", count);
874 DUMP(ip_pkt, min(128, (int)count));
875
876 while (count) {
877 switch (odev->rx_parse_state) {
878 case WAIT_IP:
879 /* waiting for IP header. */
880 /* wanted bytes - size of ip header */
881 temp_bytes =
882 (count <
883 odev->rx_buf_missing) ? count : odev->
884 rx_buf_missing;
885
886 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
887 odev->rx_buf_size, ip_pkt + buffer_offset,
888 temp_bytes);
889
890 odev->rx_buf_size += temp_bytes;
891 buffer_offset += temp_bytes;
892 odev->rx_buf_missing -= temp_bytes;
893 count -= temp_bytes;
894
895 if (!odev->rx_buf_missing) {
896 /* header is complete allocate an sk_buffer and
897 * continue to WAIT_DATA */
898 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
899
900 if ((frame_len > DEFAULT_MRU) ||
901 (frame_len < sizeof(struct iphdr))) {
902 dev_err(&odev->net->dev,
903 "Invalid frame (%d) length\n",
904 frame_len);
905 odev->rx_parse_state = WAIT_SYNC;
906 continue;
907 }
908 /* Allocate an sk_buff */
Paulius Zaleckasd65a68a2009-06-04 05:50:29 +0000909 odev->skb_rx_buf = netdev_alloc_skb(odev->net,
910 frame_len);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700911 if (!odev->skb_rx_buf) {
912 /* We got no receive buffer. */
913 D1("could not allocate memory");
914 odev->rx_parse_state = WAIT_SYNC;
915 return;
916 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700917
918 /* Copy what we got so far. make room for iphdr
919 * after tail. */
920 tmp_rx_buf =
921 skb_put(odev->skb_rx_buf,
922 sizeof(struct iphdr));
923 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
924 sizeof(struct iphdr));
925
926 /* ETH_HLEN */
927 odev->rx_buf_size = sizeof(struct iphdr);
928
929 /* Filip actually use .tot_len */
930 odev->rx_buf_missing =
931 frame_len - sizeof(struct iphdr);
932 odev->rx_parse_state = WAIT_DATA;
933 }
934 break;
935
936 case WAIT_DATA:
937 temp_bytes = (count < odev->rx_buf_missing)
938 ? count : odev->rx_buf_missing;
939
940 /* Copy the rest of the bytes that are left in the
941 * buffer into the waiting sk_buf. */
942 /* Make room for temp_bytes after tail. */
943 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
944 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
945
946 odev->rx_buf_missing -= temp_bytes;
947 count -= temp_bytes;
948 buffer_offset += temp_bytes;
949 odev->rx_buf_size += temp_bytes;
950 if (!odev->rx_buf_missing) {
951 /* Packet is complete. Inject into stack. */
952 /* We have IP packet here */
Harvey Harrison09640e62009-02-01 00:45:17 -0800953 odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -0700954 skb_reset_mac_header(odev->skb_rx_buf);
955
956 /* Ship it off to the kernel */
957 netif_rx(odev->skb_rx_buf);
958 /* No longer our buffer. */
959 odev->skb_rx_buf = NULL;
960
961 /* update out statistics */
962 odev->net->stats.rx_packets++;
963
964 odev->net->stats.rx_bytes += odev->rx_buf_size;
965
966 odev->rx_buf_size = 0;
967 odev->rx_buf_missing = sizeof(struct iphdr);
968 odev->rx_parse_state = WAIT_IP;
969 }
970 break;
971
972 case WAIT_SYNC:
973 D1(" W_S");
974 count = 0;
975 break;
976 default:
977 D1(" ");
978 count--;
979 break;
980 }
981 }
982
983 /* Recovery mechanism for WAIT_SYNC state. */
984 if (is_eop) {
985 if (odev->rx_parse_state == WAIT_SYNC) {
986 odev->rx_parse_state = WAIT_IP;
987 odev->rx_buf_size = 0;
988 odev->rx_buf_missing = sizeof(struct iphdr);
989 }
990 }
991}
992
Joe Perches5591c752010-12-21 02:16:09 -0800993static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
994{
995 static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
996 u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
997
998 if (((rest == 5) || (rest == 6)) &&
999 !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
1000 crc_check, 4)) {
1001 urb->actual_length -= 4;
1002 }
1003}
1004
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001005/* Moving data from usb to kernel (in interrupt state) */
1006static void read_bulk_callback(struct urb *urb)
1007{
1008 struct hso_net *odev = urb->context;
1009 struct net_device *net;
1010 int result;
1011 int status = urb->status;
1012
1013 /* is al ok? (Filip: Who's Al ?) */
1014 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001015 handle_usb_error(status, __func__, odev->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001016 return;
1017 }
1018
1019 /* Sanity check */
1020 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
1021 D1("BULK IN callback but driver is not active!");
1022 return;
1023 }
1024 usb_mark_last_busy(urb->dev);
1025
1026 net = odev->net;
1027
1028 if (!netif_device_present(net)) {
1029 /* Somebody killed our network interface... */
1030 return;
1031 }
1032
Joe Perches5591c752010-12-21 02:16:09 -08001033 if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
1034 fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001035
1036 /* do we even have a packet? */
1037 if (urb->actual_length) {
1038 /* Handle the IP stream, add header and push it onto network
1039 * stack if the packet is complete. */
1040 spin_lock(&odev->net_lock);
1041 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1042 (urb->transfer_buffer_length >
1043 urb->actual_length) ? 1 : 0);
1044 spin_unlock(&odev->net_lock);
1045 }
1046
1047 /* We are done with this URB, resubmit it. Prep the USB to wait for
1048 * another frame. Reuse same as received. */
1049 usb_fill_bulk_urb(urb,
1050 odev->parent->usb,
1051 usb_rcvbulkpipe(odev->parent->usb,
1052 odev->in_endp->
1053 bEndpointAddress & 0x7F),
1054 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1055 read_bulk_callback, odev);
1056
1057 /* Give this to the USB subsystem so it can tell us when more data
1058 * arrives. */
1059 result = usb_submit_urb(urb, GFP_ATOMIC);
1060 if (result)
1061 dev_warn(&odev->parent->interface->dev,
Jan Dumon8a5c9c42010-01-05 04:53:00 +00001062 "%s failed submit mux_bulk_rx_urb %d\n", __func__,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001063 result);
1064}
1065
1066/* Serial driver functions */
1067
Alan Coxac9720c2009-01-02 13:47:45 +00001068static void hso_init_termios(struct ktermios *termios)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001069{
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001070 /*
1071 * The default requirements for this device are:
1072 */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001073 termios->c_iflag &=
1074 ~(IGNBRK /* disable ignore break */
1075 | BRKINT /* disable break causes interrupt */
1076 | PARMRK /* disable mark parity errors */
1077 | ISTRIP /* disable clear high bit of input characters */
1078 | INLCR /* disable translate NL to CR */
1079 | IGNCR /* disable ignore CR */
1080 | ICRNL /* disable translate CR to NL */
1081 | IXON); /* disable enable XON/XOFF flow control */
1082
1083 /* disable postprocess output characters */
1084 termios->c_oflag &= ~OPOST;
1085
1086 termios->c_lflag &=
1087 ~(ECHO /* disable echo input characters */
1088 | ECHONL /* disable echo new line */
1089 | ICANON /* disable erase, kill, werase, and rprnt
1090 special characters */
1091 | ISIG /* disable interrupt, quit, and suspend special
1092 characters */
1093 | IEXTEN); /* disable non-POSIX special characters */
1094
1095 termios->c_cflag &=
1096 ~(CSIZE /* no size */
1097 | PARENB /* disable parity bit */
1098 | CBAUD /* clear current baud rate */
1099 | CBAUDEX); /* clear current buad rate */
1100
1101 termios->c_cflag |= CS8; /* character size 8 bits */
1102
1103 /* baud rate 115200 */
Alan Coxac9720c2009-01-02 13:47:45 +00001104 tty_termios_encode_baud_rate(termios, 115200, 115200);
1105}
1106
1107static void _hso_serial_set_termios(struct tty_struct *tty,
1108 struct ktermios *old)
1109{
Jiri Slaby30409422012-04-02 13:54:06 +02001110 struct hso_serial *serial = tty->driver_data;
Alan Coxac9720c2009-01-02 13:47:45 +00001111 struct ktermios *termios;
1112
1113 if (!serial) {
1114 printk(KERN_ERR "%s: no tty structures", __func__);
1115 return;
1116 }
1117
1118 D4("port %d", serial->minor);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001119
1120 /*
Alan Coxac9720c2009-01-02 13:47:45 +00001121 * Fix up unsupported bits
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001122 */
Alan Coxac9720c2009-01-02 13:47:45 +00001123 termios = tty->termios;
1124 termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1125
1126 termios->c_cflag &=
1127 ~(CSIZE /* no size */
1128 | PARENB /* disable parity bit */
1129 | CBAUD /* clear current baud rate */
1130 | CBAUDEX); /* clear current buad rate */
1131
1132 termios->c_cflag |= CS8; /* character size 8 bits */
1133
1134 /* baud rate 115200 */
1135 tty_encode_baud_rate(tty, 115200, 115200);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001136}
1137
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001138static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1139{
1140 int result;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001141 /* We are done with this URB, resubmit it. Prep the USB to wait for
1142 * another frame */
1143 usb_fill_bulk_urb(urb, serial->parent->usb,
1144 usb_rcvbulkpipe(serial->parent->usb,
1145 serial->in_endp->
1146 bEndpointAddress & 0x7F),
1147 urb->transfer_buffer, serial->rx_data_length,
1148 hso_std_serial_read_bulk_callback, serial);
1149 /* Give this to the USB subsystem so it can tell us when more data
1150 * arrives. */
1151 result = usb_submit_urb(urb, GFP_ATOMIC);
1152 if (result) {
1153 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1154 __func__, result);
1155 }
1156}
1157
1158
1159
1160
1161static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1162{
1163 int count;
1164 struct urb *curr_urb;
1165
1166 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1167 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1168 count = put_rxbuf_data(curr_urb, serial);
1169 if (count == -1)
1170 return;
1171 if (count == 0) {
1172 serial->curr_rx_urb_idx++;
1173 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1174 serial->curr_rx_urb_idx = 0;
1175 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1176 }
1177 }
1178}
1179
1180static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1181{
1182 int count = 0;
1183 struct urb *urb;
1184
1185 urb = serial->rx_urb[0];
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001186 if (serial->port.count > 0) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001187 count = put_rxbuf_data(urb, serial);
1188 if (count == -1)
1189 return;
1190 }
1191 /* Re issue a read as long as we receive data. */
1192
1193 if (count == 0 && ((urb->actual_length != 0) ||
1194 (serial->rx_state == RX_PENDING))) {
1195 serial->rx_state = RX_SENT;
1196 hso_mux_serial_read(serial);
1197 } else
1198 serial->rx_state = RX_IDLE;
1199}
1200
1201
1202/* read callback for Diag and CS port */
1203static void hso_std_serial_read_bulk_callback(struct urb *urb)
1204{
1205 struct hso_serial *serial = urb->context;
1206 int status = urb->status;
1207
1208 /* sanity check */
1209 if (!serial) {
1210 D1("serial == NULL");
1211 return;
1212 } else if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001213 handle_usb_error(status, __func__, serial->parent);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001214 return;
1215 }
1216
1217 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1218 D1("Actual length = %d\n", urb->actual_length);
1219 DUMP1(urb->transfer_buffer, urb->actual_length);
1220
1221 /* Anyone listening? */
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001222 if (serial->port.count == 0)
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001223 return;
1224
1225 if (status == 0) {
Joe Perches5591c752010-12-21 02:16:09 -08001226 if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
1227 fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001228 /* Valid data, handle RX data */
1229 spin_lock(&serial->serial_lock);
1230 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1231 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1232 spin_unlock(&serial->serial_lock);
1233 } else if (status == -ENOENT || status == -ECONNRESET) {
1234 /* Unlinked - check for throttled port. */
1235 D2("Port %d, successfully unlinked urb", serial->minor);
1236 spin_lock(&serial->serial_lock);
1237 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1238 hso_resubmit_rx_bulk_urb(serial, urb);
1239 spin_unlock(&serial->serial_lock);
1240 } else {
1241 D2("Port %d, status = %d for read urb", serial->minor, status);
1242 return;
1243 }
1244}
1245
1246/*
1247 * This needs to be a tasklet otherwise we will
1248 * end up recursively calling this function.
1249 */
Hannes Eder0227abc2009-02-14 11:47:47 +00001250static void hso_unthrottle_tasklet(struct hso_serial *serial)
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001251{
1252 unsigned long flags;
1253
1254 spin_lock_irqsave(&serial->serial_lock, flags);
1255 if ((serial->parent->port_spec & HSO_INTF_MUX))
1256 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1257 else
1258 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1259 spin_unlock_irqrestore(&serial->serial_lock, flags);
1260}
1261
1262static void hso_unthrottle(struct tty_struct *tty)
1263{
Jiri Slaby30409422012-04-02 13:54:06 +02001264 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001265
1266 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1267}
1268
Hannes Eder0227abc2009-02-14 11:47:47 +00001269static void hso_unthrottle_workfunc(struct work_struct *work)
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001270{
1271 struct hso_serial *serial =
1272 container_of(work, struct hso_serial,
1273 retry_unthrottle_workqueue);
1274 hso_unthrottle_tasklet(serial);
1275}
1276
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001277/* open the requested serial port */
1278static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1279{
1280 struct hso_serial *serial = get_serial_by_index(tty->index);
David S. Millerab153d82008-11-25 03:52:46 -08001281 int result;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001282
1283 /* sanity check */
1284 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
Alan Coxe136e302009-01-02 13:47:39 +00001285 WARN_ON(1);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001286 tty->driver_data = NULL;
1287 D1("Failed to open port");
1288 return -ENODEV;
1289 }
1290
David S. Millerab153d82008-11-25 03:52:46 -08001291 mutex_lock(&serial->parent->mutex);
David S. Millerab153d82008-11-25 03:52:46 -08001292 result = usb_autopm_get_interface(serial->parent->interface);
1293 if (result < 0)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001294 goto err_out;
1295
1296 D1("Opening %d", serial->minor);
1297 kref_get(&serial->parent->ref);
1298
1299 /* setup */
Alan Coxe136e302009-01-02 13:47:39 +00001300 spin_lock_irq(&serial->serial_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001301 tty->driver_data = serial;
Alan Coxfe41cbb2009-01-15 13:31:15 +00001302 tty_kref_put(serial->tty);
Alan Coxe136e302009-01-02 13:47:39 +00001303 serial->tty = tty_kref_get(tty);
1304 spin_unlock_irq(&serial->serial_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001305
David S. Miller2f9889a2008-11-25 03:53:09 -08001306 /* check for port already opened, if not set the termios */
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001307 serial->port.count++;
1308 if (serial->port.count == 1) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001309 serial->rx_state = RX_IDLE;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001310 /* Force default termio settings */
1311 _hso_serial_set_termios(tty, NULL);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001312 tasklet_init(&serial->unthrottle_tasklet,
1313 (void (*)(unsigned long))hso_unthrottle_tasklet,
1314 (unsigned long)serial);
1315 INIT_WORK(&serial->retry_unthrottle_workqueue,
1316 hso_unthrottle_workfunc);
David S. Millerab153d82008-11-25 03:52:46 -08001317 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1318 if (result) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001319 hso_stop_serial_device(serial->parent);
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001320 serial->port.count--;
David S. Millerab153d82008-11-25 03:52:46 -08001321 kref_put(&serial->parent->ref, hso_serial_ref_free);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001322 }
1323 } else {
1324 D1("Port was already open");
1325 }
1326
1327 usb_autopm_put_interface(serial->parent->interface);
1328
1329 /* done */
David S. Millerab153d82008-11-25 03:52:46 -08001330 if (result)
Alan Cox20b9d172011-02-14 16:26:50 +00001331 hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001332err_out:
David S. Millerab153d82008-11-25 03:52:46 -08001333 mutex_unlock(&serial->parent->mutex);
1334 return result;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001335}
1336
1337/* close the requested serial port */
1338static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1339{
1340 struct hso_serial *serial = tty->driver_data;
1341 u8 usb_gone;
1342
1343 D1("Closing serial port");
1344
Alan Coxe136e302009-01-02 13:47:39 +00001345 /* Open failed, no close cleanup required */
1346 if (serial == NULL)
1347 return;
1348
David S. Millerab153d82008-11-25 03:52:46 -08001349 mutex_lock(&serial->parent->mutex);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001350 usb_gone = serial->parent->usb_gone;
1351
1352 if (!usb_gone)
1353 usb_autopm_get_interface(serial->parent->interface);
1354
1355 /* reset the rts and dtr */
1356 /* do the actual close */
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001357 serial->port.count--;
Antti Kaijanmäkidcfcb252009-11-23 10:54:47 -08001358
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001359 if (serial->port.count <= 0) {
1360 serial->port.count = 0;
Alan Coxe136e302009-01-02 13:47:39 +00001361 spin_lock_irq(&serial->serial_lock);
1362 if (serial->tty == tty) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001363 serial->tty->driver_data = NULL;
1364 serial->tty = NULL;
Alan Coxe136e302009-01-02 13:47:39 +00001365 tty_kref_put(tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001366 }
Alan Coxe136e302009-01-02 13:47:39 +00001367 spin_unlock_irq(&serial->serial_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001368 if (!usb_gone)
1369 hso_stop_serial_device(serial->parent);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001370 tasklet_kill(&serial->unthrottle_tasklet);
1371 cancel_work_sync(&serial->retry_unthrottle_workqueue);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001372 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001373
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001374 if (!usb_gone)
1375 usb_autopm_put_interface(serial->parent->interface);
David S. Millerab153d82008-11-25 03:52:46 -08001376
1377 mutex_unlock(&serial->parent->mutex);
Antti Kaijanmäkidcfcb252009-11-23 10:54:47 -08001378
1379 kref_put(&serial->parent->ref, hso_serial_ref_free);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001380}
1381
1382/* close the requested serial port */
1383static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1384 int count)
1385{
Jiri Slaby30409422012-04-02 13:54:06 +02001386 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001387 int space, tx_bytes;
1388 unsigned long flags;
1389
1390 /* sanity check */
1391 if (serial == NULL) {
1392 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1393 return -ENODEV;
1394 }
1395
1396 spin_lock_irqsave(&serial->serial_lock, flags);
1397
1398 space = serial->tx_data_length - serial->tx_buffer_count;
1399 tx_bytes = (count < space) ? count : space;
1400
1401 if (!tx_bytes)
1402 goto out;
1403
1404 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1405 serial->tx_buffer_count += tx_bytes;
1406
1407out:
1408 spin_unlock_irqrestore(&serial->serial_lock, flags);
1409
1410 hso_kick_transmit(serial);
1411 /* done */
1412 return tx_bytes;
1413}
1414
1415/* how much room is there for writing */
1416static int hso_serial_write_room(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 room;
1420 unsigned long flags;
1421
1422 spin_lock_irqsave(&serial->serial_lock, flags);
1423 room = serial->tx_data_length - serial->tx_buffer_count;
1424 spin_unlock_irqrestore(&serial->serial_lock, flags);
1425
1426 /* return free room */
1427 return room;
1428}
1429
1430/* setup the term */
1431static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1432{
Jiri Slaby30409422012-04-02 13:54:06 +02001433 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001434 unsigned long flags;
1435
1436 if (old)
1437 D5("Termios called with: cflags new[%d] - old[%d]",
1438 tty->termios->c_cflag, old->c_cflag);
1439
1440 /* the actual setup */
1441 spin_lock_irqsave(&serial->serial_lock, flags);
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001442 if (serial->port.count)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001443 _hso_serial_set_termios(tty, old);
1444 else
1445 tty->termios = old;
1446 spin_unlock_irqrestore(&serial->serial_lock, flags);
1447
1448 /* done */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001449}
1450
1451/* how many characters in the buffer */
1452static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1453{
Jiri Slaby30409422012-04-02 13:54:06 +02001454 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001455 int chars;
1456 unsigned long flags;
1457
1458 /* sanity check */
1459 if (serial == NULL)
1460 return 0;
1461
1462 spin_lock_irqsave(&serial->serial_lock, flags);
1463 chars = serial->tx_buffer_count;
1464 spin_unlock_irqrestore(&serial->serial_lock, flags);
1465
1466 return chars;
1467}
Hannes Eder0227abc2009-02-14 11:47:47 +00001468static int tiocmget_submit_urb(struct hso_serial *serial,
1469 struct hso_tiocmget *tiocmget,
1470 struct usb_device *usb)
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001471{
1472 int result;
1473
1474 if (serial->parent->usb_gone)
1475 return -ENODEV;
1476 usb_fill_int_urb(tiocmget->urb, usb,
1477 usb_rcvintpipe(usb,
1478 tiocmget->endp->
1479 bEndpointAddress & 0x7F),
1480 &tiocmget->serial_state_notification,
1481 sizeof(struct hso_serial_state_notification),
1482 tiocmget_intr_callback, serial,
1483 tiocmget->endp->bInterval);
1484 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1485 if (result) {
1486 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1487 result);
1488 }
1489 return result;
1490
1491}
1492
1493static void tiocmget_intr_callback(struct urb *urb)
1494{
1495 struct hso_serial *serial = urb->context;
1496 struct hso_tiocmget *tiocmget;
1497 int status = urb->status;
1498 u16 UART_state_bitmap, prev_UART_state_bitmap;
1499 struct uart_icount *icount;
1500 struct hso_serial_state_notification *serial_state_notification;
1501 struct usb_device *usb;
1502
1503 /* Sanity checks */
1504 if (!serial)
1505 return;
1506 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001507 handle_usb_error(status, __func__, serial->parent);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001508 return;
1509 }
1510 tiocmget = serial->tiocmget;
1511 if (!tiocmget)
1512 return;
1513 usb = serial->parent->usb;
1514 serial_state_notification = &tiocmget->serial_state_notification;
1515 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1516 serial_state_notification->bNotification != B_NOTIFICATION ||
1517 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1518 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
1519 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1520 dev_warn(&usb->dev,
1521 "hso received invalid serial state notification\n");
1522 DUMP(serial_state_notification,
Antti Kaijanmäki9ce673d2009-11-23 10:54:24 -08001523 sizeof(struct hso_serial_state_notification));
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001524 } else {
1525
1526 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1527 UART_state_bitmap);
1528 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1529 icount = &tiocmget->icount;
1530 spin_lock(&serial->serial_lock);
1531 if ((UART_state_bitmap & B_OVERRUN) !=
1532 (prev_UART_state_bitmap & B_OVERRUN))
1533 icount->parity++;
1534 if ((UART_state_bitmap & B_PARITY) !=
1535 (prev_UART_state_bitmap & B_PARITY))
1536 icount->parity++;
1537 if ((UART_state_bitmap & B_FRAMING) !=
1538 (prev_UART_state_bitmap & B_FRAMING))
1539 icount->frame++;
1540 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1541 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1542 icount->rng++;
1543 if ((UART_state_bitmap & B_BREAK) !=
1544 (prev_UART_state_bitmap & B_BREAK))
1545 icount->brk++;
1546 if ((UART_state_bitmap & B_TX_CARRIER) !=
1547 (prev_UART_state_bitmap & B_TX_CARRIER))
1548 icount->dsr++;
1549 if ((UART_state_bitmap & B_RX_CARRIER) !=
1550 (prev_UART_state_bitmap & B_RX_CARRIER))
1551 icount->dcd++;
1552 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1553 spin_unlock(&serial->serial_lock);
1554 tiocmget->intr_completed = 1;
1555 wake_up_interruptible(&tiocmget->waitq);
1556 }
1557 memset(serial_state_notification, 0,
1558 sizeof(struct hso_serial_state_notification));
1559 tiocmget_submit_urb(serial,
1560 tiocmget,
1561 serial->parent->usb);
1562}
1563
1564/*
1565 * next few functions largely stolen from drivers/serial/serial_core.c
1566 */
1567/* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1568 * - mask passed in arg for lines of interest
1569 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1570 * Caller should use TIOCGICOUNT to see which one it was
1571 */
1572static int
1573hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1574{
1575 DECLARE_WAITQUEUE(wait, current);
1576 struct uart_icount cprev, cnow;
1577 struct hso_tiocmget *tiocmget;
1578 int ret;
1579
1580 tiocmget = serial->tiocmget;
1581 if (!tiocmget)
1582 return -ENOENT;
1583 /*
1584 * note the counters on entry
1585 */
1586 spin_lock_irq(&serial->serial_lock);
1587 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1588 spin_unlock_irq(&serial->serial_lock);
1589 add_wait_queue(&tiocmget->waitq, &wait);
1590 for (;;) {
1591 spin_lock_irq(&serial->serial_lock);
1592 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1593 spin_unlock_irq(&serial->serial_lock);
1594 set_current_state(TASK_INTERRUPTIBLE);
1595 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1596 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1597 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1598 ret = 0;
1599 break;
1600 }
1601 schedule();
1602 /* see if a signal did it */
1603 if (signal_pending(current)) {
1604 ret = -ERESTARTSYS;
1605 break;
1606 }
1607 cprev = cnow;
1608 }
1609 current->state = TASK_RUNNING;
1610 remove_wait_queue(&tiocmget->waitq, &wait);
1611
1612 return ret;
1613}
1614
1615/*
1616 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1617 * Return: write counters to the user passed counter struct
1618 * NB: both 1->0 and 0->1 transitions are counted except for
1619 * RI where only 0->1 is counted.
1620 */
Alan Cox0bca1b92010-09-16 18:21:40 +01001621static int hso_get_count(struct tty_struct *tty,
1622 struct serial_icounter_struct *icount)
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001623{
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001624 struct uart_icount cnow;
Jiri Slaby30409422012-04-02 13:54:06 +02001625 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001626 struct hso_tiocmget *tiocmget = serial->tiocmget;
1627
Dan Carpenter22ad7492012-02-21 21:30:25 +00001628 memset(icount, 0, sizeof(struct serial_icounter_struct));
Dan Rosenberg7011e662010-09-15 11:43:28 +00001629
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001630 if (!tiocmget)
1631 return -ENOENT;
1632 spin_lock_irq(&serial->serial_lock);
1633 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1634 spin_unlock_irq(&serial->serial_lock);
1635
Alan Cox0bca1b92010-09-16 18:21:40 +01001636 icount->cts = cnow.cts;
1637 icount->dsr = cnow.dsr;
1638 icount->rng = cnow.rng;
1639 icount->dcd = cnow.dcd;
1640 icount->rx = cnow.rx;
1641 icount->tx = cnow.tx;
1642 icount->frame = cnow.frame;
1643 icount->overrun = cnow.overrun;
1644 icount->parity = cnow.parity;
1645 icount->brk = cnow.brk;
1646 icount->buf_overrun = cnow.buf_overrun;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001647
Alan Cox0bca1b92010-09-16 18:21:40 +01001648 return 0;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001649}
1650
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001651
Alan Cox60b33c12011-02-14 16:26:14 +00001652static int hso_serial_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001653{
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001654 int retval;
Jiri Slaby30409422012-04-02 13:54:06 +02001655 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001656 struct hso_tiocmget *tiocmget;
1657 u16 UART_state_bitmap;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001658
1659 /* sanity check */
1660 if (!serial) {
1661 D1("no tty structures");
1662 return -EINVAL;
1663 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001664 spin_lock_irq(&serial->serial_lock);
1665 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
David S. Millercd90ee12008-11-25 03:52:17 -08001666 ((serial->dtr_state) ? TIOCM_DTR : 0);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001667 tiocmget = serial->tiocmget;
1668 if (tiocmget) {
David S. Millercd90ee12008-11-25 03:52:17 -08001669
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001670 UART_state_bitmap = le16_to_cpu(
1671 tiocmget->prev_UART_state_bitmap);
1672 if (UART_state_bitmap & B_RING_SIGNAL)
1673 retval |= TIOCM_RNG;
1674 if (UART_state_bitmap & B_RX_CARRIER)
1675 retval |= TIOCM_CD;
1676 if (UART_state_bitmap & B_TX_CARRIER)
1677 retval |= TIOCM_DSR;
1678 }
1679 spin_unlock_irq(&serial->serial_lock);
1680 return retval;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001681}
1682
Alan Cox20b9d172011-02-14 16:26:50 +00001683static int hso_serial_tiocmset(struct tty_struct *tty,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001684 unsigned int set, unsigned int clear)
1685{
1686 int val = 0;
1687 unsigned long flags;
1688 int if_num;
Jiri Slaby30409422012-04-02 13:54:06 +02001689 struct hso_serial *serial = tty->driver_data;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001690
1691 /* sanity check */
1692 if (!serial) {
1693 D1("no tty structures");
1694 return -EINVAL;
1695 }
Jan Dumon0e0367e2010-01-05 04:52:42 +00001696
1697 if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
1698 return -EINVAL;
1699
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001700 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1701
1702 spin_lock_irqsave(&serial->serial_lock, flags);
1703 if (set & TIOCM_RTS)
1704 serial->rts_state = 1;
1705 if (set & TIOCM_DTR)
1706 serial->dtr_state = 1;
1707
1708 if (clear & TIOCM_RTS)
1709 serial->rts_state = 0;
1710 if (clear & TIOCM_DTR)
1711 serial->dtr_state = 0;
1712
1713 if (serial->dtr_state)
1714 val |= 0x01;
1715 if (serial->rts_state)
1716 val |= 0x02;
1717
1718 spin_unlock_irqrestore(&serial->serial_lock, flags);
1719
1720 return usb_control_msg(serial->parent->usb,
1721 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1722 0x21, val, if_num, NULL, 0,
1723 USB_CTRL_SET_TIMEOUT);
1724}
1725
Alan Cox6caa76b2011-02-14 16:27:22 +00001726static int hso_serial_ioctl(struct tty_struct *tty,
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001727 unsigned int cmd, unsigned long arg)
1728{
Jiri Slaby30409422012-04-02 13:54:06 +02001729 struct hso_serial *serial = tty->driver_data;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001730 int ret = 0;
1731 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1732
1733 if (!serial)
1734 return -ENODEV;
1735 switch (cmd) {
1736 case TIOCMIWAIT:
1737 ret = hso_wait_modem_status(serial, arg);
1738 break;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00001739 default:
1740 ret = -ENOIOCTLCMD;
1741 break;
1742 }
1743 return ret;
1744}
1745
1746
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001747/* starts a transmit */
1748static void hso_kick_transmit(struct hso_serial *serial)
1749{
1750 u8 *temp;
1751 unsigned long flags;
1752 int res;
1753
1754 spin_lock_irqsave(&serial->serial_lock, flags);
1755 if (!serial->tx_buffer_count)
1756 goto out;
1757
1758 if (serial->tx_urb_used)
1759 goto out;
1760
1761 /* Wakeup USB interface if necessary */
1762 if (hso_get_activity(serial->parent) == -EAGAIN)
1763 goto out;
1764
1765 /* Switch pointers around to avoid memcpy */
1766 temp = serial->tx_buffer;
1767 serial->tx_buffer = serial->tx_data;
1768 serial->tx_data = temp;
1769 serial->tx_data_count = serial->tx_buffer_count;
1770 serial->tx_buffer_count = 0;
1771
1772 /* If temp is set, it means we switched buffers */
1773 if (temp && serial->write_data) {
1774 res = serial->write_data(serial);
1775 if (res >= 0)
1776 serial->tx_urb_used = 1;
1777 }
1778out:
1779 spin_unlock_irqrestore(&serial->serial_lock, flags);
1780}
1781
1782/* make a request (for reading and writing data to muxed serial port) */
1783static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1784 struct urb *ctrl_urb,
1785 struct usb_ctrlrequest *ctrl_req,
1786 u8 *ctrl_urb_data, u32 size)
1787{
1788 int result;
1789 int pipe;
1790
1791 /* Sanity check */
1792 if (!serial || !ctrl_urb || !ctrl_req) {
1793 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1794 return -EINVAL;
1795 }
1796
1797 /* initialize */
1798 ctrl_req->wValue = 0;
Denis Joseph Barrowb74f62c2009-01-12 21:56:49 -08001799 ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
1800 ctrl_req->wLength = cpu_to_le16(size);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001801
1802 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1803 /* Reading command */
1804 ctrl_req->bRequestType = USB_DIR_IN |
1805 USB_TYPE_OPTION_VENDOR |
1806 USB_RECIP_INTERFACE;
1807 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1808 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1809 } else {
1810 /* Writing command */
1811 ctrl_req->bRequestType = USB_DIR_OUT |
1812 USB_TYPE_OPTION_VENDOR |
1813 USB_RECIP_INTERFACE;
1814 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1815 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1816 }
1817 /* syslog */
1818 D2("%s command (%02x) len: %d, port: %d",
1819 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1820 ctrl_req->bRequestType, ctrl_req->wLength, port);
1821
1822 /* Load ctrl urb */
1823 ctrl_urb->transfer_flags = 0;
1824 usb_fill_control_urb(ctrl_urb,
1825 serial->parent->usb,
1826 pipe,
1827 (u8 *) ctrl_req,
1828 ctrl_urb_data, size, ctrl_callback, serial);
1829 /* Send it on merry way */
1830 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1831 if (result) {
1832 dev_err(&ctrl_urb->dev->dev,
Jan Dumon8a5c9c42010-01-05 04:53:00 +00001833 "%s failed submit ctrl_urb %d type %d\n", __func__,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001834 result, type);
1835 return result;
1836 }
1837
1838 /* done */
1839 return size;
1840}
1841
1842/* called by intr_callback when read occurs */
1843static int hso_mux_serial_read(struct hso_serial *serial)
1844{
1845 if (!serial)
1846 return -EINVAL;
1847
1848 /* clean data */
1849 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1850 /* make the request */
1851
1852 if (serial->num_rx_urbs != 1) {
1853 dev_err(&serial->parent->interface->dev,
1854 "ERROR: mux'd reads with multiple buffers "
1855 "not possible\n");
1856 return 0;
1857 }
1858 return mux_device_request(serial,
1859 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1860 serial->parent->port_spec & HSO_PORT_MASK,
1861 serial->rx_urb[0],
1862 &serial->ctrl_req_rx,
1863 serial->rx_data[0], serial->rx_data_length);
1864}
1865
1866/* used for muxed serial port callback (muxed serial read) */
1867static void intr_callback(struct urb *urb)
1868{
1869 struct hso_shared_int *shared_int = urb->context;
1870 struct hso_serial *serial;
1871 unsigned char *port_req;
1872 int status = urb->status;
1873 int i;
1874
1875 usb_mark_last_busy(urb->dev);
1876
1877 /* sanity check */
1878 if (!shared_int)
1879 return;
1880
1881 /* status check */
1882 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001883 handle_usb_error(status, __func__, NULL);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001884 return;
1885 }
1886 D4("\n--- Got intr callback 0x%02X ---", status);
1887
1888 /* what request? */
1889 port_req = urb->transfer_buffer;
1890 D4(" port_req = 0x%.2X\n", *port_req);
1891 /* loop over all muxed ports to find the one sending this */
1892 for (i = 0; i < 8; i++) {
1893 /* max 8 channels on MUX */
1894 if (*port_req & (1 << i)) {
1895 serial = get_serial_by_shared_int_and_type(shared_int,
1896 (1 << i));
1897 if (serial != NULL) {
1898 D1("Pending read interrupt on port %d\n", i);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001899 spin_lock(&serial->serial_lock);
Jan Dumonf4763e92010-01-05 04:51:28 +00001900 if (serial->rx_state == RX_IDLE &&
Jiri Slaby5ce76e72012-04-02 13:54:05 +02001901 serial->port.count > 0) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001902 /* Setup and send a ctrl req read on
1903 * port i */
Jan Dumonf4763e92010-01-05 04:51:28 +00001904 if (!serial->rx_urb_filled[0]) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001905 serial->rx_state = RX_SENT;
1906 hso_mux_serial_read(serial);
1907 } else
1908 serial->rx_state = RX_PENDING;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001909 } else {
Jan Dumonf4763e92010-01-05 04:51:28 +00001910 D1("Already a read pending on "
1911 "port %d or port not open\n", i);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001912 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02001913 spin_unlock(&serial->serial_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001914 }
1915 }
1916 }
1917 /* Resubmit interrupt urb */
1918 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1919}
1920
1921/* called for writing to muxed serial port */
1922static int hso_mux_serial_write_data(struct hso_serial *serial)
1923{
1924 if (NULL == serial)
1925 return -EINVAL;
1926
1927 return mux_device_request(serial,
1928 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1929 serial->parent->port_spec & HSO_PORT_MASK,
1930 serial->tx_urb,
1931 &serial->ctrl_req_tx,
1932 serial->tx_data, serial->tx_data_count);
1933}
1934
1935/* write callback for Diag and CS port */
1936static void hso_std_serial_write_bulk_callback(struct urb *urb)
1937{
1938 struct hso_serial *serial = urb->context;
1939 int status = urb->status;
Alan Coxe136e302009-01-02 13:47:39 +00001940 struct tty_struct *tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001941
1942 /* sanity check */
1943 if (!serial) {
1944 D1("serial == NULL");
1945 return;
1946 }
1947
1948 spin_lock(&serial->serial_lock);
1949 serial->tx_urb_used = 0;
Alan Coxe136e302009-01-02 13:47:39 +00001950 tty = tty_kref_get(serial->tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001951 spin_unlock(&serial->serial_lock);
1952 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00001953 handle_usb_error(status, __func__, serial->parent);
Alan Coxe136e302009-01-02 13:47:39 +00001954 tty_kref_put(tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001955 return;
1956 }
1957 hso_put_activity(serial->parent);
Alan Coxe136e302009-01-02 13:47:39 +00001958 if (tty) {
1959 tty_wakeup(tty);
1960 tty_kref_put(tty);
1961 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001962 hso_kick_transmit(serial);
1963
1964 D1(" ");
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001965}
1966
1967/* called for writing diag or CS serial port */
1968static int hso_std_serial_write_data(struct hso_serial *serial)
1969{
1970 int count = serial->tx_data_count;
1971 int result;
1972
1973 usb_fill_bulk_urb(serial->tx_urb,
1974 serial->parent->usb,
1975 usb_sndbulkpipe(serial->parent->usb,
1976 serial->out_endp->
1977 bEndpointAddress & 0x7F),
1978 serial->tx_data, serial->tx_data_count,
1979 hso_std_serial_write_bulk_callback, serial);
1980
1981 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1982 if (result) {
1983 dev_warn(&serial->parent->usb->dev,
1984 "Failed to submit urb - res %d\n", result);
1985 return result;
1986 }
1987
1988 return count;
1989}
1990
1991/* callback after read or write on muxed serial port */
1992static void ctrl_callback(struct urb *urb)
1993{
1994 struct hso_serial *serial = urb->context;
1995 struct usb_ctrlrequest *req;
1996 int status = urb->status;
Alan Coxe136e302009-01-02 13:47:39 +00001997 struct tty_struct *tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07001998
1999 /* sanity check */
2000 if (!serial)
2001 return;
2002
2003 spin_lock(&serial->serial_lock);
2004 serial->tx_urb_used = 0;
Alan Coxe136e302009-01-02 13:47:39 +00002005 tty = tty_kref_get(serial->tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002006 spin_unlock(&serial->serial_lock);
2007 if (status) {
Jan Dumon68a351c2010-01-05 04:52:13 +00002008 handle_usb_error(status, __func__, serial->parent);
Alan Coxe136e302009-01-02 13:47:39 +00002009 tty_kref_put(tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002010 return;
2011 }
2012
2013 /* what request? */
2014 req = (struct usb_ctrlrequest *)(urb->setup_packet);
2015 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2016 D4("Actual length of urb = %d\n", urb->actual_length);
2017 DUMP1(urb->transfer_buffer, urb->actual_length);
2018
2019 if (req->bRequestType ==
2020 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2021 /* response to a read command */
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002022 serial->rx_urb_filled[0] = 1;
2023 spin_lock(&serial->serial_lock);
2024 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2025 spin_unlock(&serial->serial_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002026 } else {
2027 hso_put_activity(serial->parent);
Alan Coxe136e302009-01-02 13:47:39 +00002028 if (tty)
2029 tty_wakeup(tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002030 /* response to a write command */
2031 hso_kick_transmit(serial);
2032 }
Alan Coxe136e302009-01-02 13:47:39 +00002033 tty_kref_put(tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002034}
2035
2036/* handle RX data for serial port */
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002037static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002038{
Alan Coxe136e302009-01-02 13:47:39 +00002039 struct tty_struct *tty;
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002040 int write_length_remaining = 0;
2041 int curr_write_len;
Alan Coxe136e302009-01-02 13:47:39 +00002042
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002043 /* Sanity check */
2044 if (urb == NULL || serial == NULL) {
2045 D1("serial = NULL");
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002046 return -2;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002047 }
2048
Denis Joseph Barrowd45eb812009-01-15 13:31:24 +00002049 /* All callers to put_rxbuf_data hold serial_lock */
Alan Coxe136e302009-01-02 13:47:39 +00002050 tty = tty_kref_get(serial->tty);
Alan Coxe136e302009-01-02 13:47:39 +00002051
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002052 /* Push data to tty */
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002053 if (tty) {
2054 write_length_remaining = urb->actual_length -
2055 serial->curr_rx_urb_offset;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002056 D1("data to push to tty");
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002057 while (write_length_remaining) {
Denis Joseph Barrow5839b412009-01-15 13:31:34 +00002058 if (test_bit(TTY_THROTTLED, &tty->flags)) {
2059 tty_kref_put(tty);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002060 return -1;
Denis Joseph Barrow5839b412009-01-15 13:31:34 +00002061 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002062 curr_write_len = tty_insert_flip_string
2063 (tty, urb->transfer_buffer +
2064 serial->curr_rx_urb_offset,
2065 write_length_remaining);
2066 serial->curr_rx_urb_offset += curr_write_len;
2067 write_length_remaining -= curr_write_len;
2068 tty_flip_buffer_push(tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002069 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002070 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002071 if (write_length_remaining == 0) {
2072 serial->curr_rx_urb_offset = 0;
2073 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002074 }
Alan Coxe136e302009-01-02 13:47:39 +00002075 tty_kref_put(tty);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002076 return write_length_remaining;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002077}
2078
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002079
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002080/* Base driver functions */
2081
2082static void hso_log_port(struct hso_device *hso_dev)
2083{
2084 char *port_type;
2085 char port_dev[20];
2086
2087 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2088 case HSO_PORT_CONTROL:
2089 port_type = "Control";
2090 break;
2091 case HSO_PORT_APP:
2092 port_type = "Application";
2093 break;
2094 case HSO_PORT_GPS:
2095 port_type = "GPS";
2096 break;
2097 case HSO_PORT_GPS_CONTROL:
2098 port_type = "GPS control";
2099 break;
2100 case HSO_PORT_APP2:
2101 port_type = "Application2";
2102 break;
2103 case HSO_PORT_PCSC:
2104 port_type = "PCSC";
2105 break;
2106 case HSO_PORT_DIAG:
2107 port_type = "Diagnostic";
2108 break;
2109 case HSO_PORT_DIAG2:
2110 port_type = "Diagnostic2";
2111 break;
2112 case HSO_PORT_MODEM:
2113 port_type = "Modem";
2114 break;
2115 case HSO_PORT_NETWORK:
2116 port_type = "Network";
2117 break;
2118 default:
2119 port_type = "Unknown";
2120 break;
2121 }
2122 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2123 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2124 } else
2125 sprintf(port_dev, "/dev/%s%d", tty_filename,
2126 dev2ser(hso_dev)->minor);
2127
2128 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2129 port_type, port_dev);
2130}
2131
2132static int hso_start_net_device(struct hso_device *hso_dev)
2133{
2134 int i, result = 0;
2135 struct hso_net *hso_net = dev2net(hso_dev);
2136
2137 if (!hso_net)
2138 return -ENODEV;
2139
2140 /* send URBs for all read buffers */
2141 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2142
2143 /* Prep a receive URB */
2144 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2145 hso_dev->usb,
2146 usb_rcvbulkpipe(hso_dev->usb,
2147 hso_net->in_endp->
2148 bEndpointAddress & 0x7F),
2149 hso_net->mux_bulk_rx_buf_pool[i],
2150 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2151 hso_net);
2152
2153 /* Put it out there so the device can send us stuff */
2154 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2155 GFP_NOIO);
2156 if (result)
2157 dev_warn(&hso_dev->usb->dev,
2158 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2159 i, result);
2160 }
2161
2162 return result;
2163}
2164
2165static int hso_stop_net_device(struct hso_device *hso_dev)
2166{
2167 int i;
2168 struct hso_net *hso_net = dev2net(hso_dev);
2169
2170 if (!hso_net)
2171 return -ENODEV;
2172
2173 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2174 if (hso_net->mux_bulk_rx_urb_pool[i])
2175 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2176
2177 }
2178 if (hso_net->mux_bulk_tx_urb)
2179 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2180
2181 return 0;
2182}
2183
2184static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2185{
2186 int i, result = 0;
2187 struct hso_serial *serial = dev2ser(hso_dev);
2188
2189 if (!serial)
2190 return -ENODEV;
2191
2192 /* If it is not the MUX port fill in and submit a bulk urb (already
2193 * allocated in hso_serial_start) */
2194 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2195 for (i = 0; i < serial->num_rx_urbs; i++) {
2196 usb_fill_bulk_urb(serial->rx_urb[i],
2197 serial->parent->usb,
2198 usb_rcvbulkpipe(serial->parent->usb,
2199 serial->in_endp->
2200 bEndpointAddress &
2201 0x7F),
2202 serial->rx_data[i],
2203 serial->rx_data_length,
2204 hso_std_serial_read_bulk_callback,
2205 serial);
2206 result = usb_submit_urb(serial->rx_urb[i], flags);
2207 if (result) {
2208 dev_warn(&serial->parent->usb->dev,
2209 "Failed to submit urb - res %d\n",
2210 result);
2211 break;
2212 }
2213 }
2214 } else {
2215 mutex_lock(&serial->shared_int->shared_int_lock);
2216 if (!serial->shared_int->use_count) {
2217 result =
2218 hso_mux_submit_intr_urb(serial->shared_int,
2219 hso_dev->usb, flags);
2220 }
2221 serial->shared_int->use_count++;
2222 mutex_unlock(&serial->shared_int->shared_int_lock);
2223 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002224 if (serial->tiocmget)
2225 tiocmget_submit_urb(serial,
2226 serial->tiocmget,
2227 serial->parent->usb);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002228 return result;
2229}
2230
2231static int hso_stop_serial_device(struct hso_device *hso_dev)
2232{
2233 int i;
2234 struct hso_serial *serial = dev2ser(hso_dev);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002235 struct hso_tiocmget *tiocmget;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002236
2237 if (!serial)
2238 return -ENODEV;
2239
2240 for (i = 0; i < serial->num_rx_urbs; i++) {
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002241 if (serial->rx_urb[i]) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002242 usb_kill_urb(serial->rx_urb[i]);
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002243 serial->rx_urb_filled[i] = 0;
2244 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002245 }
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02002246 serial->curr_rx_urb_idx = 0;
2247 serial->curr_rx_urb_offset = 0;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002248
2249 if (serial->tx_urb)
2250 usb_kill_urb(serial->tx_urb);
2251
2252 if (serial->shared_int) {
2253 mutex_lock(&serial->shared_int->shared_int_lock);
2254 if (serial->shared_int->use_count &&
2255 (--serial->shared_int->use_count == 0)) {
2256 struct urb *urb;
2257
2258 urb = serial->shared_int->shared_intr_urb;
2259 if (urb)
2260 usb_kill_urb(urb);
2261 }
2262 mutex_unlock(&serial->shared_int->shared_int_lock);
2263 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002264 tiocmget = serial->tiocmget;
2265 if (tiocmget) {
2266 wake_up_interruptible(&tiocmget->waitq);
2267 usb_kill_urb(tiocmget->urb);
2268 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002269
2270 return 0;
2271}
2272
2273static void hso_serial_common_free(struct hso_serial *serial)
2274{
2275 int i;
2276
2277 if (serial->parent->dev)
2278 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2279
2280 tty_unregister_device(tty_drv, serial->minor);
2281
2282 for (i = 0; i < serial->num_rx_urbs; i++) {
2283 /* unlink and free RX URB */
2284 usb_free_urb(serial->rx_urb[i]);
2285 /* free the RX buffer */
2286 kfree(serial->rx_data[i]);
2287 }
2288
2289 /* unlink and free TX URB */
2290 usb_free_urb(serial->tx_urb);
2291 kfree(serial->tx_data);
2292}
2293
2294static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2295 int rx_size, int tx_size)
2296{
2297 struct device *dev;
2298 int minor;
2299 int i;
2300
2301 minor = get_free_serial_index();
2302 if (minor < 0)
2303 goto exit;
2304
2305 /* register our minor number */
2306 serial->parent->dev = tty_register_device(tty_drv, minor,
2307 &serial->parent->interface->dev);
2308 dev = serial->parent->dev;
Greg Kroah-Hartman1aec5bd2009-04-30 12:19:31 +00002309 dev_set_drvdata(dev, serial->parent);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002310 i = device_create_file(dev, &dev_attr_hsotype);
2311
2312 /* fill in specific data for later use */
2313 serial->minor = minor;
2314 serial->magic = HSO_SERIAL_MAGIC;
2315 spin_lock_init(&serial->serial_lock);
Jiri Slaby5ce76e72012-04-02 13:54:05 +02002316 tty_port_init(&serial->port);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002317 serial->num_rx_urbs = num_urbs;
2318
2319 /* RX, allocate urb and initialize */
2320
2321 /* prepare our RX buffer */
2322 serial->rx_data_length = rx_size;
2323 for (i = 0; i < serial->num_rx_urbs; i++) {
2324 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2325 if (!serial->rx_urb[i]) {
2326 dev_err(dev, "Could not allocate urb?\n");
2327 goto exit;
2328 }
2329 serial->rx_urb[i]->transfer_buffer = NULL;
2330 serial->rx_urb[i]->transfer_buffer_length = 0;
2331 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2332 GFP_KERNEL);
2333 if (!serial->rx_data[i]) {
2334 dev_err(dev, "%s - Out of memory\n", __func__);
2335 goto exit;
2336 }
2337 }
2338
2339 /* TX, allocate urb and initialize */
2340 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2341 if (!serial->tx_urb) {
2342 dev_err(dev, "Could not allocate urb?\n");
2343 goto exit;
2344 }
2345 serial->tx_urb->transfer_buffer = NULL;
2346 serial->tx_urb->transfer_buffer_length = 0;
2347 /* prepare our TX buffer */
2348 serial->tx_data_count = 0;
2349 serial->tx_buffer_count = 0;
2350 serial->tx_data_length = tx_size;
2351 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2352 if (!serial->tx_data) {
Jan Dumon8a5c9c42010-01-05 04:53:00 +00002353 dev_err(dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002354 goto exit;
2355 }
2356 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2357 if (!serial->tx_buffer) {
Jan Dumon8a5c9c42010-01-05 04:53:00 +00002358 dev_err(dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002359 goto exit;
2360 }
2361
2362 return 0;
2363exit:
2364 hso_serial_common_free(serial);
2365 return -1;
2366}
2367
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002368/* Creates a general hso device */
2369static struct hso_device *hso_create_device(struct usb_interface *intf,
2370 int port_spec)
2371{
2372 struct hso_device *hso_dev;
2373
2374 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2375 if (!hso_dev)
2376 return NULL;
2377
2378 hso_dev->port_spec = port_spec;
2379 hso_dev->usb = interface_to_usbdev(intf);
2380 hso_dev->interface = intf;
2381 kref_init(&hso_dev->ref);
David S. Millerab153d82008-11-25 03:52:46 -08002382 mutex_init(&hso_dev->mutex);
2383
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002384 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2385 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
Jan Dumon68a351c2010-01-05 04:52:13 +00002386 INIT_WORK(&hso_dev->reset_device, reset_device);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002387
2388 return hso_dev;
2389}
2390
2391/* Removes a network device in the network device table */
2392static int remove_net_device(struct hso_device *hso_dev)
2393{
2394 int i;
2395
2396 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2397 if (network_table[i] == hso_dev) {
2398 network_table[i] = NULL;
2399 break;
2400 }
2401 }
2402 if (i == HSO_MAX_NET_DEVICES)
2403 return -1;
2404 return 0;
2405}
2406
2407/* Frees our network device */
2408static void hso_free_net_device(struct hso_device *hso_dev)
2409{
2410 int i;
2411 struct hso_net *hso_net = dev2net(hso_dev);
2412
2413 if (!hso_net)
2414 return;
2415
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002416 remove_net_device(hso_net->parent);
2417
Greg KH5e2cd082011-07-08 03:45:25 +00002418 if (hso_net->net)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002419 unregister_netdev(hso_net->net);
David S. Millerab153d82008-11-25 03:52:46 -08002420
Jan Dumon3b7d2b32009-04-01 22:57:20 +00002421 /* start freeing */
2422 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2423 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2424 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2425 hso_net->mux_bulk_rx_buf_pool[i] = NULL;
2426 }
2427 usb_free_urb(hso_net->mux_bulk_tx_urb);
2428 kfree(hso_net->mux_bulk_tx_buf);
2429 hso_net->mux_bulk_tx_buf = NULL;
2430
Greg KH5e2cd082011-07-08 03:45:25 +00002431 if (hso_net->net)
2432 free_netdev(hso_net->net);
2433
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002434 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002435}
2436
Stephen Hemmingerc266cb42009-03-20 19:35:52 +00002437static const struct net_device_ops hso_netdev_ops = {
2438 .ndo_open = hso_net_open,
2439 .ndo_stop = hso_net_close,
2440 .ndo_start_xmit = hso_net_start_xmit,
2441 .ndo_tx_timeout = hso_net_tx_timeout,
2442};
2443
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002444/* initialize the network interface */
2445static void hso_net_init(struct net_device *net)
2446{
2447 struct hso_net *hso_net = netdev_priv(net);
2448
2449 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2450
2451 /* fill in the other fields */
Stephen Hemmingerc266cb42009-03-20 19:35:52 +00002452 net->netdev_ops = &hso_netdev_ops;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002453 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2454 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2455 net->type = ARPHRD_NONE;
2456 net->mtu = DEFAULT_MTU - 14;
2457 net->tx_queue_len = 10;
2458 SET_ETHTOOL_OPS(net, &ops);
2459
2460 /* and initialize the semaphore */
2461 spin_lock_init(&hso_net->net_lock);
2462}
2463
2464/* Adds a network device in the network device table */
2465static int add_net_device(struct hso_device *hso_dev)
2466{
2467 int i;
2468
2469 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2470 if (network_table[i] == NULL) {
2471 network_table[i] = hso_dev;
2472 break;
2473 }
2474 }
2475 if (i == HSO_MAX_NET_DEVICES)
2476 return -1;
2477 return 0;
2478}
2479
Johannes Berg19d337d2009-06-02 13:01:37 +02002480static int hso_rfkill_set_block(void *data, bool blocked)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002481{
2482 struct hso_device *hso_dev = data;
Johannes Berg19d337d2009-06-02 13:01:37 +02002483 int enabled = !blocked;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002484 int rv;
2485
David S. Millerab153d82008-11-25 03:52:46 -08002486 mutex_lock(&hso_dev->mutex);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002487 if (hso_dev->usb_gone)
2488 rv = 0;
2489 else
2490 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2491 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2492 USB_CTRL_SET_TIMEOUT);
David S. Millerab153d82008-11-25 03:52:46 -08002493 mutex_unlock(&hso_dev->mutex);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002494 return rv;
2495}
2496
Johannes Berg19d337d2009-06-02 13:01:37 +02002497static const struct rfkill_ops hso_rfkill_ops = {
2498 .set_block = hso_rfkill_set_block,
2499};
2500
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002501/* Creates and sets up everything for rfkill */
2502static void hso_create_rfkill(struct hso_device *hso_dev,
2503 struct usb_interface *interface)
2504{
2505 struct hso_net *hso_net = dev2net(hso_dev);
Jonathan McDowell939a9512008-11-04 07:51:38 +00002506 struct device *dev = &hso_net->net->dev;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002507 char *rfkn;
2508
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002509 rfkn = kzalloc(20, GFP_KERNEL);
Johannes Berg19d337d2009-06-02 13:01:37 +02002510 if (!rfkn)
Jonathan McDowell939a9512008-11-04 07:51:38 +00002511 dev_err(dev, "%s - Out of memory\n", __func__);
Johannes Berg19d337d2009-06-02 13:01:37 +02002512
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002513 snprintf(rfkn, 20, "hso-%d",
2514 interface->altsetting->desc.bInterfaceNumber);
Johannes Berg19d337d2009-06-02 13:01:37 +02002515
2516 hso_net->rfkill = rfkill_alloc(rfkn,
2517 &interface_to_usbdev(interface)->dev,
2518 RFKILL_TYPE_WWAN,
2519 &hso_rfkill_ops, hso_dev);
2520 if (!hso_net->rfkill) {
2521 dev_err(dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002522 kfree(rfkn);
Johannes Berg19d337d2009-06-02 13:01:37 +02002523 return;
2524 }
2525 if (rfkill_register(hso_net->rfkill) < 0) {
2526 rfkill_destroy(hso_net->rfkill);
2527 kfree(rfkn);
Jonathan McDowell939a9512008-11-04 07:51:38 +00002528 hso_net->rfkill = NULL;
2529 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002530 return;
2531 }
2532}
2533
Marcel Holtmann384912e2009-08-31 21:08:19 +00002534static struct device_type hso_type = {
2535 .name = "wwan",
2536};
2537
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002538/* Creates our network device */
Jan Dumon0de8ca52009-04-01 22:59:07 +00002539static struct hso_device *hso_create_net_device(struct usb_interface *interface,
2540 int port_spec)
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002541{
2542 int result, i;
2543 struct net_device *net;
2544 struct hso_net *hso_net;
2545 struct hso_device *hso_dev;
2546
Jan Dumon0de8ca52009-04-01 22:59:07 +00002547 hso_dev = hso_create_device(interface, port_spec);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002548 if (!hso_dev)
2549 return NULL;
2550
2551 /* allocate our network device, then we can put in our private data */
2552 /* call hso_net_init to do the basic initialization */
2553 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2554 if (!net) {
2555 dev_err(&interface->dev, "Unable to create ethernet device\n");
2556 goto exit;
2557 }
2558
2559 hso_net = netdev_priv(net);
2560
2561 hso_dev->port_data.dev_net = hso_net;
2562 hso_net->net = net;
2563 hso_net->parent = hso_dev;
2564
2565 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2566 USB_DIR_IN);
2567 if (!hso_net->in_endp) {
2568 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2569 goto exit;
2570 }
2571 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2572 USB_DIR_OUT);
2573 if (!hso_net->out_endp) {
2574 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2575 goto exit;
2576 }
2577 SET_NETDEV_DEV(net, &interface->dev);
Marcel Holtmann384912e2009-08-31 21:08:19 +00002578 SET_NETDEV_DEVTYPE(net, &hso_type);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002579
2580 /* registering our net device */
2581 result = register_netdev(net);
2582 if (result) {
2583 dev_err(&interface->dev, "Failed to register device\n");
2584 goto exit;
2585 }
2586
2587 /* start allocating */
2588 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2589 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2590 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2591 dev_err(&interface->dev, "Could not allocate rx urb\n");
2592 goto exit;
2593 }
2594 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2595 GFP_KERNEL);
2596 if (!hso_net->mux_bulk_rx_buf_pool[i]) {
2597 dev_err(&interface->dev, "Could not allocate rx buf\n");
2598 goto exit;
2599 }
2600 }
2601 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2602 if (!hso_net->mux_bulk_tx_urb) {
2603 dev_err(&interface->dev, "Could not allocate tx urb\n");
2604 goto exit;
2605 }
2606 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2607 if (!hso_net->mux_bulk_tx_buf) {
2608 dev_err(&interface->dev, "Could not allocate tx buf\n");
2609 goto exit;
2610 }
2611
2612 add_net_device(hso_dev);
2613
2614 hso_log_port(hso_dev);
2615
2616 hso_create_rfkill(hso_dev, interface);
2617
2618 return hso_dev;
2619exit:
2620 hso_free_net_device(hso_dev);
2621 return NULL;
2622}
2623
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002624static void hso_free_tiomget(struct hso_serial *serial)
2625{
Jesper Juhl5b89db0e2011-02-13 11:15:35 +00002626 struct hso_tiocmget *tiocmget;
2627 if (!serial)
2628 return;
2629 tiocmget = serial->tiocmget;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002630 if (tiocmget) {
Jesper Juhl5b89db0e2011-02-13 11:15:35 +00002631 usb_free_urb(tiocmget->urb);
2632 tiocmget->urb = NULL;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002633 serial->tiocmget = NULL;
Jan Dumon3b7d2b32009-04-01 22:57:20 +00002634 kfree(tiocmget);
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002635 }
2636}
2637
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002638/* Frees an AT channel ( goes for both mux and non-mux ) */
2639static void hso_free_serial_device(struct hso_device *hso_dev)
2640{
2641 struct hso_serial *serial = dev2ser(hso_dev);
2642
2643 if (!serial)
2644 return;
2645 set_serial_by_index(serial->minor, NULL);
2646
2647 hso_serial_common_free(serial);
2648
2649 if (serial->shared_int) {
2650 mutex_lock(&serial->shared_int->shared_int_lock);
2651 if (--serial->shared_int->ref_count == 0)
2652 hso_free_shared_int(serial->shared_int);
2653 else
2654 mutex_unlock(&serial->shared_int->shared_int_lock);
2655 }
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002656 hso_free_tiomget(serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002657 kfree(serial);
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002658 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002659}
2660
2661/* Creates a bulk AT channel */
2662static struct hso_device *hso_create_bulk_serial_device(
2663 struct usb_interface *interface, int port)
2664{
2665 struct hso_device *hso_dev;
2666 struct hso_serial *serial;
2667 int num_urbs;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002668 struct hso_tiocmget *tiocmget;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002669
2670 hso_dev = hso_create_device(interface, port);
2671 if (!hso_dev)
2672 return NULL;
2673
2674 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2675 if (!serial)
2676 goto exit;
2677
2678 serial->parent = hso_dev;
2679 hso_dev->port_data.dev_serial = serial;
2680
Denis Joseph Barrow58eb17f2009-01-02 13:50:29 +00002681 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002682 num_urbs = 2;
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002683 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2684 GFP_KERNEL);
2685 /* it isn't going to break our heart if serial->tiocmget
2686 * allocation fails don't bother checking this.
2687 */
2688 if (serial->tiocmget) {
2689 tiocmget = serial->tiocmget;
2690 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2691 if (tiocmget->urb) {
2692 mutex_init(&tiocmget->mutex);
2693 init_waitqueue_head(&tiocmget->waitq);
2694 tiocmget->endp = hso_get_ep(
2695 interface,
2696 USB_ENDPOINT_XFER_INT,
2697 USB_DIR_IN);
2698 } else
2699 hso_free_tiomget(serial);
2700 }
2701 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002702 else
2703 num_urbs = 1;
2704
2705 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2706 BULK_URB_TX_SIZE))
2707 goto exit;
2708
2709 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2710 USB_DIR_IN);
2711 if (!serial->in_endp) {
2712 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
Adrian Bunke57b6412008-08-28 01:02:37 +03002713 goto exit2;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002714 }
2715
2716 if (!
2717 (serial->out_endp =
2718 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2719 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
Adrian Bunke57b6412008-08-28 01:02:37 +03002720 goto exit2;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002721 }
2722
2723 serial->write_data = hso_std_serial_write_data;
2724
2725 /* and record this serial */
2726 set_serial_by_index(serial->minor, serial);
2727
2728 /* setup the proc dirs and files if needed */
2729 hso_log_port(hso_dev);
2730
2731 /* done, return it */
2732 return hso_dev;
Adrian Bunke57b6412008-08-28 01:02:37 +03002733
2734exit2:
2735 hso_serial_common_free(serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002736exit:
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00002737 hso_free_tiomget(serial);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002738 kfree(serial);
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002739 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002740 return NULL;
2741}
2742
2743/* Creates a multiplexed AT channel */
2744static
2745struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2746 int port,
2747 struct hso_shared_int *mux)
2748{
2749 struct hso_device *hso_dev;
2750 struct hso_serial *serial;
2751 int port_spec;
2752
2753 port_spec = HSO_INTF_MUX;
2754 port_spec &= ~HSO_PORT_MASK;
2755
2756 port_spec |= hso_mux_to_port(port);
2757 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2758 return NULL;
2759
2760 hso_dev = hso_create_device(interface, port_spec);
2761 if (!hso_dev)
2762 return NULL;
2763
2764 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2765 if (!serial)
2766 goto exit;
2767
2768 hso_dev->port_data.dev_serial = serial;
2769 serial->parent = hso_dev;
2770
2771 if (hso_serial_common_create
2772 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2773 goto exit;
2774
2775 serial->tx_data_length--;
2776 serial->write_data = hso_mux_serial_write_data;
2777
2778 serial->shared_int = mux;
2779 mutex_lock(&serial->shared_int->shared_int_lock);
2780 serial->shared_int->ref_count++;
2781 mutex_unlock(&serial->shared_int->shared_int_lock);
2782
2783 /* and record this serial */
2784 set_serial_by_index(serial->minor, serial);
2785
2786 /* setup the proc dirs and files if needed */
2787 hso_log_port(hso_dev);
2788
2789 /* done, return it */
2790 return hso_dev;
2791
2792exit:
2793 if (serial) {
2794 tty_unregister_device(tty_drv, serial->minor);
2795 kfree(serial);
2796 }
2797 if (hso_dev)
Paulius Zaleckase44578e2009-02-23 05:58:27 +00002798 kfree(hso_dev);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002799 return NULL;
2800
2801}
2802
2803static void hso_free_shared_int(struct hso_shared_int *mux)
2804{
2805 usb_free_urb(mux->shared_intr_urb);
2806 kfree(mux->shared_intr_buf);
2807 mutex_unlock(&mux->shared_int_lock);
2808 kfree(mux);
2809}
2810
2811static
2812struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2813{
2814 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2815
2816 if (!mux)
2817 return NULL;
2818
2819 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2820 USB_DIR_IN);
2821 if (!mux->intr_endp) {
2822 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2823 goto exit;
2824 }
2825
2826 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2827 if (!mux->shared_intr_urb) {
Jan Dumon8a5c9c42010-01-05 04:53:00 +00002828 dev_err(&interface->dev, "Could not allocate intr urb?\n");
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002829 goto exit;
2830 }
Jan Dumond9ced802010-01-05 04:51:02 +00002831 mux->shared_intr_buf =
2832 kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
2833 GFP_KERNEL);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002834 if (!mux->shared_intr_buf) {
Jan Dumon8a5c9c42010-01-05 04:53:00 +00002835 dev_err(&interface->dev, "Could not allocate intr buf?\n");
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002836 goto exit;
2837 }
2838
2839 mutex_init(&mux->shared_int_lock);
2840
2841 return mux;
2842
2843exit:
2844 kfree(mux->shared_intr_buf);
2845 usb_free_urb(mux->shared_intr_urb);
2846 kfree(mux);
2847 return NULL;
2848}
2849
2850/* Gets the port spec for a certain interface */
2851static int hso_get_config_data(struct usb_interface *interface)
2852{
2853 struct usb_device *usbdev = interface_to_usbdev(interface);
2854 u8 config_data[17];
2855 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2856 s32 result;
2857
2858 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2859 0x86, 0xC0, 0, 0, config_data, 17,
2860 USB_CTRL_SET_TIMEOUT) != 0x11) {
2861 return -EIO;
2862 }
2863
2864 switch (config_data[if_num]) {
2865 case 0x0:
2866 result = 0;
2867 break;
2868 case 0x1:
2869 result = HSO_PORT_DIAG;
2870 break;
2871 case 0x2:
2872 result = HSO_PORT_GPS;
2873 break;
2874 case 0x3:
2875 result = HSO_PORT_GPS_CONTROL;
2876 break;
2877 case 0x4:
2878 result = HSO_PORT_APP;
2879 break;
2880 case 0x5:
2881 result = HSO_PORT_APP2;
2882 break;
2883 case 0x6:
2884 result = HSO_PORT_CONTROL;
2885 break;
2886 case 0x7:
2887 result = HSO_PORT_NETWORK;
2888 break;
2889 case 0x8:
2890 result = HSO_PORT_MODEM;
2891 break;
2892 case 0x9:
2893 result = HSO_PORT_MSD;
2894 break;
2895 case 0xa:
2896 result = HSO_PORT_PCSC;
2897 break;
2898 case 0xb:
2899 result = HSO_PORT_VOICE;
2900 break;
2901 default:
2902 result = 0;
2903 }
2904
2905 if (result)
2906 result |= HSO_INTF_BULK;
2907
2908 if (config_data[16] & 0x1)
2909 result |= HSO_INFO_CRC_BUG;
2910
2911 return result;
2912}
2913
2914/* called once for each interface upon device insertion */
2915static int hso_probe(struct usb_interface *interface,
2916 const struct usb_device_id *id)
2917{
2918 int mux, i, if_num, port_spec;
2919 unsigned char port_mask;
2920 struct hso_device *hso_dev = NULL;
2921 struct hso_shared_int *shared_int;
2922 struct hso_device *tmp_dev = NULL;
2923
2924 if_num = interface->altsetting->desc.bInterfaceNumber;
2925
2926 /* Get the interface/port specification from either driver_info or from
2927 * the device itself */
2928 if (id->driver_info)
2929 port_spec = ((u32 *)(id->driver_info))[if_num];
2930 else
2931 port_spec = hso_get_config_data(interface);
2932
2933 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2934 dev_err(&interface->dev, "Not our interface\n");
2935 return -ENODEV;
2936 }
2937 /* Check if we need to switch to alt interfaces prior to port
2938 * configuration */
2939 if (interface->num_altsetting > 1)
2940 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2941 interface->needs_remote_wakeup = 1;
2942
2943 /* Allocate new hso device(s) */
2944 switch (port_spec & HSO_INTF_MASK) {
2945 case HSO_INTF_MUX:
2946 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2947 /* Create the network device */
2948 if (!disable_net) {
Jan Dumon0de8ca52009-04-01 22:59:07 +00002949 hso_dev = hso_create_net_device(interface,
2950 port_spec);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002951 if (!hso_dev)
2952 goto exit;
2953 tmp_dev = hso_dev;
2954 }
2955 }
2956
2957 if (hso_get_mux_ports(interface, &port_mask))
2958 /* TODO: de-allocate everything */
2959 goto exit;
2960
2961 shared_int = hso_create_shared_int(interface);
2962 if (!shared_int)
2963 goto exit;
2964
2965 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2966 if (port_mask & i) {
2967 hso_dev = hso_create_mux_serial_device(
2968 interface, i, shared_int);
2969 if (!hso_dev)
2970 goto exit;
2971 }
2972 }
2973
2974 if (tmp_dev)
2975 hso_dev = tmp_dev;
2976 break;
2977
2978 case HSO_INTF_BULK:
2979 /* It's a regular bulk interface */
Filip Aben8e65c0e2010-11-25 03:40:50 +00002980 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2981 if (!disable_net)
2982 hso_dev =
2983 hso_create_net_device(interface, port_spec);
2984 } else {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002985 hso_dev =
2986 hso_create_bulk_serial_device(interface, port_spec);
Filip Aben8e65c0e2010-11-25 03:40:50 +00002987 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002988 if (!hso_dev)
2989 goto exit;
2990 break;
2991 default:
2992 goto exit;
2993 }
2994
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07002995 /* save our data pointer in this device */
2996 usb_set_intfdata(interface, hso_dev);
2997
2998 /* done */
2999 return 0;
3000exit:
3001 hso_free_interface(interface);
3002 return -ENODEV;
3003}
3004
3005/* device removed, cleaning up */
3006static void hso_disconnect(struct usb_interface *interface)
3007{
3008 hso_free_interface(interface);
3009
3010 /* remove reference of our private data */
3011 usb_set_intfdata(interface, NULL);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003012}
3013
3014static void async_get_intf(struct work_struct *data)
3015{
3016 struct hso_device *hso_dev =
3017 container_of(data, struct hso_device, async_get_intf);
3018 usb_autopm_get_interface(hso_dev->interface);
3019}
3020
3021static void async_put_intf(struct work_struct *data)
3022{
3023 struct hso_device *hso_dev =
3024 container_of(data, struct hso_device, async_put_intf);
3025 usb_autopm_put_interface(hso_dev->interface);
3026}
3027
3028static int hso_get_activity(struct hso_device *hso_dev)
3029{
3030 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3031 if (!hso_dev->is_active) {
3032 hso_dev->is_active = 1;
3033 schedule_work(&hso_dev->async_get_intf);
3034 }
3035 }
3036
3037 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3038 return -EAGAIN;
3039
3040 usb_mark_last_busy(hso_dev->usb);
3041
3042 return 0;
3043}
3044
3045static int hso_put_activity(struct hso_device *hso_dev)
3046{
3047 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3048 if (hso_dev->is_active) {
3049 hso_dev->is_active = 0;
3050 schedule_work(&hso_dev->async_put_intf);
3051 return -EAGAIN;
3052 }
3053 }
3054 hso_dev->is_active = 0;
3055 return 0;
3056}
3057
3058/* called by kernel when we need to suspend device */
3059static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3060{
3061 int i, result;
3062
3063 /* Stop all serial ports */
3064 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3065 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3066 result = hso_stop_serial_device(serial_table[i]);
3067 if (result)
3068 goto out;
3069 }
3070 }
3071
3072 /* Stop all network ports */
3073 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3074 if (network_table[i] &&
3075 (network_table[i]->interface == iface)) {
3076 result = hso_stop_net_device(network_table[i]);
3077 if (result)
3078 goto out;
3079 }
3080 }
3081
3082out:
3083 return 0;
3084}
3085
3086/* called by kernel when we need to resume device */
3087static int hso_resume(struct usb_interface *iface)
3088{
3089 int i, result = 0;
3090 struct hso_net *hso_net;
3091
3092 /* Start all serial ports */
3093 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3094 if (serial_table[i] && (serial_table[i]->interface == iface)) {
Jiri Slaby5ce76e72012-04-02 13:54:05 +02003095 if (dev2ser(serial_table[i])->port.count) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003096 result =
3097 hso_start_serial_device(serial_table[i], GFP_NOIO);
3098 hso_kick_transmit(dev2ser(serial_table[i]));
3099 if (result)
3100 goto out;
3101 }
3102 }
3103 }
3104
3105 /* Start all network ports */
3106 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3107 if (network_table[i] &&
3108 (network_table[i]->interface == iface)) {
3109 hso_net = dev2net(network_table[i]);
Denis Joseph Barrow89930b72008-11-25 00:30:48 -08003110 if (hso_net->flags & IFF_UP) {
3111 /* First transmit any lingering data,
3112 then restart the device. */
3113 if (hso_net->skb_tx_buf) {
3114 dev_dbg(&iface->dev,
3115 "Transmitting"
3116 " lingering data\n");
3117 hso_net_start_xmit(hso_net->skb_tx_buf,
3118 hso_net->net);
3119 hso_net->skb_tx_buf = NULL;
3120 }
3121 result = hso_start_net_device(network_table[i]);
3122 if (result)
3123 goto out;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003124 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003125 }
3126 }
3127
3128out:
3129 return result;
3130}
3131
Jan Dumon68a351c2010-01-05 04:52:13 +00003132static void reset_device(struct work_struct *data)
3133{
3134 struct hso_device *hso_dev =
3135 container_of(data, struct hso_device, reset_device);
3136 struct usb_device *usb = hso_dev->usb;
3137 int result;
3138
3139 if (hso_dev->usb_gone) {
3140 D1("No reset during disconnect\n");
3141 } else {
3142 result = usb_lock_device_for_reset(usb, hso_dev->interface);
3143 if (result < 0)
3144 D1("unable to lock device for reset: %d\n", result);
3145 else {
3146 usb_reset_device(usb);
3147 usb_unlock_device(usb);
3148 }
3149 }
3150}
3151
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003152static void hso_serial_ref_free(struct kref *ref)
3153{
3154 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3155
3156 hso_free_serial_device(hso_dev);
3157}
3158
3159static void hso_free_interface(struct usb_interface *interface)
3160{
3161 struct hso_serial *hso_dev;
Alan Coxe136e302009-01-02 13:47:39 +00003162 struct tty_struct *tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003163 int i;
3164
3165 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
Joe Perches8e95a202009-12-03 07:58:21 +00003166 if (serial_table[i] &&
3167 (serial_table[i]->interface == interface)) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003168 hso_dev = dev2ser(serial_table[i]);
Alan Coxe136e302009-01-02 13:47:39 +00003169 spin_lock_irq(&hso_dev->serial_lock);
3170 tty = tty_kref_get(hso_dev->tty);
3171 spin_unlock_irq(&hso_dev->serial_lock);
3172 if (tty)
3173 tty_hangup(tty);
David S. Millerab153d82008-11-25 03:52:46 -08003174 mutex_lock(&hso_dev->parent->mutex);
Alan Coxe136e302009-01-02 13:47:39 +00003175 tty_kref_put(tty);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003176 hso_dev->parent->usb_gone = 1;
David S. Millerab153d82008-11-25 03:52:46 -08003177 mutex_unlock(&hso_dev->parent->mutex);
3178 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003179 }
3180 }
3181
3182 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
Joe Perches8e95a202009-12-03 07:58:21 +00003183 if (network_table[i] &&
3184 (network_table[i]->interface == interface)) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003185 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3186 /* hso_stop_net_device doesn't stop the net queue since
3187 * traffic needs to start it again when suspended */
3188 netif_stop_queue(dev2net(network_table[i])->net);
3189 hso_stop_net_device(network_table[i]);
3190 cancel_work_sync(&network_table[i]->async_put_intf);
3191 cancel_work_sync(&network_table[i]->async_get_intf);
Johannes Berg19d337d2009-06-02 13:01:37 +02003192 if (rfk) {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003193 rfkill_unregister(rfk);
Johannes Berg19d337d2009-06-02 13:01:37 +02003194 rfkill_destroy(rfk);
3195 }
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003196 hso_free_net_device(network_table[i]);
3197 }
3198 }
3199}
3200
3201/* Helper functions */
3202
3203/* Get the endpoint ! */
3204static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3205 int type, int dir)
3206{
3207 int i;
3208 struct usb_host_interface *iface = intf->cur_altsetting;
3209 struct usb_endpoint_descriptor *endp;
3210
3211 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3212 endp = &iface->endpoint[i].desc;
3213 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
Julia Lawallf201a8a2008-12-29 00:21:07 +00003214 (usb_endpoint_type(endp) == type))
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003215 return endp;
3216 }
3217
3218 return NULL;
3219}
3220
3221/* Get the byte that describes which ports are enabled */
3222static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3223{
3224 int i;
3225 struct usb_host_interface *iface = intf->cur_altsetting;
3226
3227 if (iface->extralen == 3) {
3228 *ports = iface->extra[2];
3229 return 0;
3230 }
3231
3232 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3233 if (iface->endpoint[i].extralen == 3) {
3234 *ports = iface->endpoint[i].extra[2];
3235 return 0;
3236 }
3237 }
3238
3239 return -1;
3240}
3241
3242/* interrupt urb needs to be submitted, used for serial read of muxed port */
3243static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3244 struct usb_device *usb, gfp_t gfp)
3245{
3246 int result;
3247
3248 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3249 usb_rcvintpipe(usb,
3250 shared_int->intr_endp->bEndpointAddress & 0x7F),
3251 shared_int->shared_intr_buf,
Jan Dumond9ced802010-01-05 04:51:02 +00003252 1,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003253 intr_callback, shared_int,
3254 shared_int->intr_endp->bInterval);
3255
3256 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3257 if (result)
Jan Dumon8a5c9c42010-01-05 04:53:00 +00003258 dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003259 result);
3260
3261 return result;
3262}
3263
3264/* operations setup of the serial interface */
Greg Kroah-Hartman6c59f562008-08-08 12:02:14 -07003265static const struct tty_operations hso_serial_ops = {
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003266 .open = hso_serial_open,
3267 .close = hso_serial_close,
3268 .write = hso_serial_write,
3269 .write_room = hso_serial_write_room,
Denis Joseph Barrow542f5482009-01-02 13:47:52 +00003270 .ioctl = hso_serial_ioctl,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003271 .set_termios = hso_serial_set_termios,
3272 .chars_in_buffer = hso_serial_chars_in_buffer,
3273 .tiocmget = hso_serial_tiocmget,
3274 .tiocmset = hso_serial_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01003275 .get_icount = hso_get_count,
Denis Joseph Barrow8ef5ba62008-09-05 17:12:07 +02003276 .unthrottle = hso_unthrottle
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003277};
3278
3279static struct usb_driver hso_driver = {
3280 .name = driver_name,
3281 .probe = hso_probe,
3282 .disconnect = hso_disconnect,
3283 .id_table = hso_ids,
3284 .suspend = hso_suspend,
3285 .resume = hso_resume,
Denis Joseph Barrow9c8f92a2008-11-25 00:36:10 -08003286 .reset_resume = hso_resume,
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003287 .supports_autosuspend = 1,
3288};
3289
3290static int __init hso_init(void)
3291{
3292 int i;
3293 int result;
3294
3295 /* put it in the log */
3296 printk(KERN_INFO "hso: %s\n", version);
3297
3298 /* Initialise the serial table semaphore and table */
3299 spin_lock_init(&serial_table_lock);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003300 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3301 serial_table[i] = NULL;
3302
3303 /* allocate our driver using the proper amount of supported minors */
3304 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3305 if (!tty_drv)
3306 return -ENOMEM;
3307
3308 /* fill in all needed values */
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003309 tty_drv->driver_name = driver_name;
3310 tty_drv->name = tty_filename;
3311
3312 /* if major number is provided as parameter, use that one */
3313 if (tty_major)
3314 tty_drv->major = tty_major;
3315
3316 tty_drv->minor_start = 0;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003317 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3318 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3319 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3320 tty_drv->init_termios = tty_std_termios;
Alan Coxac9720c2009-01-02 13:47:45 +00003321 hso_init_termios(&tty_drv->init_termios);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003322 tty_set_operations(tty_drv, &hso_serial_ops);
3323
3324 /* register the tty driver */
3325 result = tty_register_driver(tty_drv);
3326 if (result) {
3327 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3328 __func__, result);
Jiri Slabyd2307882012-04-02 13:54:04 +02003329 goto err_free_tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003330 }
3331
3332 /* register this module as an usb driver */
3333 result = usb_register(&hso_driver);
3334 if (result) {
3335 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3336 result);
Jiri Slabyd2307882012-04-02 13:54:04 +02003337 goto err_unreg_tty;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003338 }
3339
3340 /* done */
3341 return 0;
Jiri Slabyd2307882012-04-02 13:54:04 +02003342err_unreg_tty:
3343 tty_unregister_driver(tty_drv);
3344err_free_tty:
3345 put_tty_driver(tty_drv);
3346 return result;
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003347}
3348
3349static void __exit hso_exit(void)
3350{
3351 printk(KERN_INFO "hso: unloaded\n");
3352
3353 tty_unregister_driver(tty_drv);
Jiri Slabyd2307882012-04-02 13:54:04 +02003354 put_tty_driver(tty_drv);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003355 /* deregister the usb driver */
3356 usb_deregister(&hso_driver);
3357}
3358
3359/* Module definitions */
3360module_init(hso_init);
3361module_exit(hso_exit);
3362
3363MODULE_AUTHOR(MOD_AUTHOR);
3364MODULE_DESCRIPTION(MOD_DESCRIPTION);
3365MODULE_LICENSE(MOD_LICENSE);
Greg Kroah-Hartman72dc1c02008-05-13 21:57:12 -07003366
3367/* change the debug level (eg: insmod hso.ko debug=0x04) */
3368MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3369module_param(debug, int, S_IRUGO | S_IWUSR);
3370
3371/* set the major tty number (eg: insmod hso.ko tty_major=245) */
3372MODULE_PARM_DESC(tty_major, "Set the major tty number");
3373module_param(tty_major, int, S_IRUGO | S_IWUSR);
3374
3375/* disable network interface (eg: insmod hso.ko disable_net=1) */
3376MODULE_PARM_DESC(disable_net, "Disable the network interface");
3377module_param(disable_net, int, S_IRUGO | S_IWUSR);