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