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