e9256408757ffdf3a7f5cc3a832bf8b503c808c9
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / serial / option.c
1 /*
2 Option Card (PCMCIA to) USB to Serial Driver
3
4 Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
5
6 This driver is free software; you can redistribute it and/or modify
7 it under the terms of Version 2 of the GNU General Public License as
8 published by the Free Software Foundation.
9
10 Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
11
12 History:
13
14 2005-05-19 v0.1 Initial version, based on incomplete docs
15 and analysis of misbehavior with the standard driver
16 2005-05-20 v0.2 Extended the input buffer to avoid losing
17 random 64-byte chunks of data
18 2005-05-21 v0.3 implemented chars_in_buffer()
19 turned on low_latency
20 simplified the code somewhat
21 2005-05-24 v0.4 option_write() sometimes deadlocked under heavy load
22 removed some dead code
23 added sponsor notice
24 coding style clean-up
25 2005-06-20 v0.4.1 add missing braces :-/
26 killed end-of-line whitespace
27 2005-07-15 v0.4.2 rename WLAN product to FUSION, add FUSION2
28
29 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
30
31 */
32
33 #define DRIVER_VERSION "v0.4"
34 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
35 #define DRIVER_DESC "Option Card (PC-Card to) USB to Serial Driver"
36
37 #include <linux/config.h>
38 #include <linux/kernel.h>
39 #include <linux/jiffies.h>
40 #include <linux/errno.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/module.h>
44 #include <linux/usb.h>
45 #include "usb-serial.h"
46
47 /* Function prototypes */
48 static int option_open (struct usb_serial_port *port, struct file *filp);
49 static void option_close (struct usb_serial_port *port, struct file *filp);
50 static int option_startup (struct usb_serial *serial);
51 static void option_shutdown (struct usb_serial *serial);
52 static void option_rx_throttle (struct usb_serial_port *port);
53 static void option_rx_unthrottle (struct usb_serial_port *port);
54 static int option_write_room (struct usb_serial_port *port);
55
56 static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
57
58 static int option_write (struct usb_serial_port *port,
59 const unsigned char *buf, int count);
60
61 static int option_chars_in_buffer (struct usb_serial_port *port);
62 static int option_ioctl (struct usb_serial_port *port, struct file *file,
63 unsigned int cmd, unsigned long arg);
64 static void option_set_termios (struct usb_serial_port *port,
65 struct termios *old);
66 static void option_break_ctl (struct usb_serial_port *port, int break_state);
67 static int option_tiocmget (struct usb_serial_port *port, struct file *file);
68 static int option_tiocmset (struct usb_serial_port *port, struct file *file,
69 unsigned int set, unsigned int clear);
70 static int option_send_setup (struct usb_serial_port *port);
71
72 /* Vendor and product IDs */
73 #define OPTION_VENDOR_ID 0x0AF0
74
75 #define OPTION_PRODUCT_OLD 0x5000
76 #define OPTION_PRODUCT_FUSION 0x6000
77 #define OPTION_PRODUCT_FUSION2 0x6300
78
79
80 static struct usb_device_id option_ids[] = {
81 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
82 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
83 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
84 { } /* Terminating entry */
85 };
86
87 MODULE_DEVICE_TABLE(usb, option_ids);
88
89 static struct usb_driver option_driver = {
90 .owner = THIS_MODULE,
91 .name = "option",
92 .probe = usb_serial_probe,
93 .disconnect = usb_serial_disconnect,
94 .id_table = option_ids,
95 };
96
97 /* The card has three separate interfaces, wich the serial driver
98 * recognizes separately, thus num_port=1.
99 */
100 static struct usb_serial_device_type option_3port_device = {
101 .owner = THIS_MODULE,
102 .name = "Option 3G data card",
103 .short_name = "option",
104 .id_table = option_ids,
105 .num_interrupt_in = NUM_DONT_CARE,
106 .num_bulk_in = NUM_DONT_CARE,
107 .num_bulk_out = NUM_DONT_CARE,
108 .num_ports = 1, /* 3, but the card reports its ports separately */
109 .open = option_open,
110 .close = option_close,
111 .write = option_write,
112 .write_room = option_write_room,
113 .chars_in_buffer = option_chars_in_buffer,
114 .throttle = option_rx_throttle,
115 .unthrottle = option_rx_unthrottle,
116 .ioctl = option_ioctl,
117 .set_termios = option_set_termios,
118 .break_ctl = option_break_ctl,
119 .tiocmget = option_tiocmget,
120 .tiocmset = option_tiocmset,
121 .attach = option_startup,
122 .shutdown = option_shutdown,
123 .read_int_callback = option_instat_callback,
124 };
125
126 #ifdef CONFIG_USB_DEBUG
127 static int debug;
128 #else
129 #define debug 0
130 #endif
131
132
133 /* per port private data */
134
135 #define N_IN_URB 4
136 #define N_OUT_URB 1
137 #define IN_BUFLEN 1024
138 #define OUT_BUFLEN 128
139
140 struct option_port_private {
141 /* Input endpoints and buffer for this port */
142 struct urb *in_urbs[N_IN_URB];
143 char in_buffer[N_IN_URB][IN_BUFLEN];
144 /* Output endpoints and buffer for this port */
145 struct urb *out_urbs[N_OUT_URB];
146 char out_buffer[N_OUT_URB][OUT_BUFLEN];
147
148 /* Settings for the port */
149 int rts_state; /* Handshaking pins (outputs) */
150 int dtr_state;
151 int cts_state; /* Handshaking pins (inputs) */
152 int dsr_state;
153 int dcd_state;
154 int ri_state;
155
156 unsigned long tx_start_time[N_OUT_URB];
157 };
158
159
160 /* Functions used by new usb-serial code. */
161 static int __init
162 option_init (void)
163 {
164 int retval;
165 retval = usb_serial_register(&option_3port_device);
166 if (retval)
167 goto failed_3port_device_register;
168 retval = usb_register(&option_driver);
169 if (retval)
170 goto failed_driver_register;
171
172 info(DRIVER_DESC ": " DRIVER_VERSION);
173
174 return 0;
175
176 failed_driver_register:
177 usb_serial_deregister (&option_3port_device);
178 failed_3port_device_register:
179 return retval;
180 }
181
182 static void __exit
183 option_exit (void)
184 {
185 usb_deregister (&option_driver);
186 usb_serial_deregister (&option_3port_device);
187 }
188
189 module_init(option_init);
190 module_exit(option_exit);
191
192 static void
193 option_rx_throttle (struct usb_serial_port *port)
194 {
195 dbg("%s", __FUNCTION__);
196 }
197
198
199 static void
200 option_rx_unthrottle (struct usb_serial_port *port)
201 {
202 dbg("%s", __FUNCTION__);
203 }
204
205
206 static void
207 option_break_ctl (struct usb_serial_port *port, int break_state)
208 {
209 /* Unfortunately, I don't know how to send a break */
210 dbg("%s", __FUNCTION__);
211 }
212
213
214 static void
215 option_set_termios (struct usb_serial_port *port,
216 struct termios *old_termios)
217 {
218 dbg("%s", __FUNCTION__);
219
220 option_send_setup(port);
221 }
222
223 static int
224 option_tiocmget (struct usb_serial_port *port, struct file *file)
225 {
226 unsigned int value;
227 struct option_port_private *portdata;
228
229 portdata = usb_get_serial_port_data(port);
230
231 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
232 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
233 ((portdata->cts_state) ? TIOCM_CTS : 0) |
234 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
235 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
236 ((portdata->ri_state) ? TIOCM_RNG : 0);
237
238 return value;
239 }
240
241 static int
242 option_tiocmset (struct usb_serial_port *port, struct file *file,
243 unsigned int set, unsigned int clear)
244 {
245 struct option_port_private *portdata;
246
247 portdata = usb_get_serial_port_data(port);
248
249 if (set & TIOCM_RTS)
250 portdata->rts_state = 1;
251 if (set & TIOCM_DTR)
252 portdata->dtr_state = 1;
253
254 if (clear & TIOCM_RTS)
255 portdata->rts_state = 0;
256 if (clear & TIOCM_DTR)
257 portdata->dtr_state = 0;
258 return option_send_setup(port);
259 }
260
261 static int
262 option_ioctl (struct usb_serial_port *port, struct file *file,
263 unsigned int cmd, unsigned long arg)
264 {
265 return -ENOIOCTLCMD;
266 }
267
268 /* Write */
269 static int
270 option_write (struct usb_serial_port *port,
271 const unsigned char *buf, int count)
272 {
273 struct option_port_private *portdata;
274 int i;
275 int left, todo;
276 struct urb *this_urb = NULL; /* spurious */
277 int err;
278
279 portdata = usb_get_serial_port_data(port);
280
281 dbg("%s: write (%d chars)", __FUNCTION__, count);
282
283 i = 0;
284 left = count;
285 for (i=0; left > 0 && i < N_OUT_URB; i++) {
286 todo = left;
287 if (todo > OUT_BUFLEN)
288 todo = OUT_BUFLEN;
289
290 this_urb = portdata->out_urbs[i];
291 if (this_urb->status == -EINPROGRESS) {
292 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
293 continue;
294 if (time_before(jiffies, portdata->tx_start_time[i] + 10 * HZ))
295 continue;
296 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
297 usb_unlink_urb(this_urb);
298 continue;
299 }
300 if (this_urb->status != 0)
301 dbg("usb_write %p failed (err=%d)", this_urb, this_urb->status);
302
303 dbg("%s: endpoint %d buf %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), i);
304
305 /* send the data */
306 memcpy (this_urb->transfer_buffer, buf, todo);
307 this_urb->transfer_buffer_length = todo;
308
309 this_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
310 this_urb->dev = port->serial->dev;
311 err = usb_submit_urb(this_urb, GFP_ATOMIC);
312 if (err) {
313 dbg("usb_submit_urb %p (write bulk) failed (%d, has %d)", this_urb, err, this_urb->status);
314 continue;
315 }
316 portdata->tx_start_time[i] = jiffies;
317 buf += todo;
318 left -= todo;
319 }
320
321 count -= left;
322 dbg("%s: wrote (did %d)", __FUNCTION__, count);
323 return count;
324 }
325
326 static void
327 option_indat_callback (struct urb *urb, struct pt_regs *regs)
328 {
329 int i, err;
330 int endpoint;
331 struct usb_serial_port *port;
332 struct tty_struct *tty;
333 unsigned char *data = urb->transfer_buffer;
334
335 dbg("%s: %p", __FUNCTION__, urb);
336
337 endpoint = usb_pipeendpoint(urb->pipe);
338 port = (struct usb_serial_port *) urb->context;
339
340 if (urb->status) {
341 dbg("%s: nonzero status: %d on endpoint %02x.",
342 __FUNCTION__, urb->status, endpoint);
343 } else {
344 tty = port->tty;
345 if (urb->actual_length) {
346 for (i = 0; i < urb->actual_length ; ++i) {
347 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
348 tty_flip_buffer_push(tty);
349 tty_insert_flip_char(tty, data[i], 0);
350 }
351 tty_flip_buffer_push(tty);
352 } else {
353 dbg("%s: empty read urb received", __FUNCTION__);
354 }
355
356 /* Resubmit urb so we continue receiving */
357 if (port->open_count && urb->status != -ESHUTDOWN) {
358 err = usb_submit_urb(urb, GFP_ATOMIC);
359 if (err)
360 printk(KERN_ERR "%s: resubmit read urb failed. (%d)", __FUNCTION__, err);
361 }
362 }
363 return;
364 }
365
366 static void
367 option_outdat_callback (struct urb *urb, struct pt_regs *regs)
368 {
369 struct usb_serial_port *port;
370
371 dbg("%s", __FUNCTION__);
372
373 port = (struct usb_serial_port *) urb->context;
374
375 if (port->open_count)
376 schedule_work(&port->work);
377 }
378
379 static void
380 option_instat_callback (struct urb *urb, struct pt_regs *regs)
381 {
382 int err;
383 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
384 struct option_port_private *portdata = usb_get_serial_port_data(port);
385 struct usb_serial *serial = port->serial;
386
387 dbg("%s", __FUNCTION__);
388 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
389
390 if (urb->status == 0) {
391 struct usb_ctrlrequest *req_pkt =
392 (struct usb_ctrlrequest *)urb->transfer_buffer;
393
394 if (!req_pkt) {
395 dbg("%s: NULL req_pkt\n", __FUNCTION__);
396 return;
397 }
398 if ((req_pkt->bRequestType == 0xA1) && (req_pkt->bRequest == 0x20)) {
399 int old_dcd_state;
400 unsigned char signals = *((unsigned char *)
401 urb->transfer_buffer + sizeof(struct usb_ctrlrequest));
402
403 dbg("%s: signal x%x", __FUNCTION__, signals);
404
405 old_dcd_state = portdata->dcd_state;
406 portdata->cts_state = 1;
407 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
408 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
409 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
410
411 if (port->tty && !C_CLOCAL(port->tty)
412 && old_dcd_state && !portdata->dcd_state) {
413 tty_hangup(port->tty);
414 }
415 } else
416 dbg("%s: type %x req %x", __FUNCTION__, req_pkt->bRequestType,req_pkt->bRequest);
417 } else
418 dbg("%s: error %d", __FUNCTION__, urb->status);
419
420 /* Resubmit urb so we continue receiving IRQ data */
421 if (urb->status != -ESHUTDOWN) {
422 urb->dev = serial->dev;
423 err = usb_submit_urb(urb, GFP_ATOMIC);
424 if (err)
425 dbg("%s: resubmit intr urb failed. (%d)", __FUNCTION__, err);
426 }
427 }
428
429
430 static int
431 option_write_room (struct usb_serial_port *port)
432 {
433 struct option_port_private *portdata;
434 int i;
435 int data_len = 0;
436 struct urb *this_urb;
437
438 portdata = usb_get_serial_port_data(port);
439
440 for (i=0; i < N_OUT_URB; i++) {
441 this_urb = portdata->out_urbs[i];
442 if (this_urb && this_urb->status != -EINPROGRESS)
443 data_len += OUT_BUFLEN;
444 }
445
446 dbg("%s: %d", __FUNCTION__, data_len);
447 return data_len;
448 }
449
450
451 static int
452 option_chars_in_buffer (struct usb_serial_port *port)
453 {
454 struct option_port_private *portdata;
455 int i;
456 int data_len = 0;
457 struct urb *this_urb;
458
459 portdata = usb_get_serial_port_data(port);
460
461 for (i=0; i < N_OUT_URB; i++) {
462 this_urb = portdata->out_urbs[i];
463 if (this_urb && this_urb->status == -EINPROGRESS)
464 data_len += this_urb->transfer_buffer_length;
465 }
466 dbg("%s: %d", __FUNCTION__, data_len);
467 return data_len;
468 }
469
470
471 static int
472 option_open (struct usb_serial_port *port, struct file *filp)
473 {
474 struct option_port_private *portdata;
475 struct usb_serial *serial = port->serial;
476 int i, err;
477 struct urb *urb;
478
479 portdata = usb_get_serial_port_data(port);
480
481 dbg("%s", __FUNCTION__);
482
483 /* Set some sane defaults */
484 portdata->rts_state = 1;
485 portdata->dtr_state = 1;
486
487 /* Reset low level data toggle and start reading from endpoints */
488 for (i = 0; i < N_IN_URB; i++) {
489 urb = portdata->in_urbs[i];
490 if (! urb)
491 continue;
492 if (urb->dev != serial->dev) {
493 dbg("%s: dev %p != %p", __FUNCTION__, urb->dev, serial->dev);
494 continue;
495 }
496
497 /* make sure endpoint data toggle is synchronized with the device */
498
499 usb_clear_halt(urb->dev, urb->pipe);
500
501 err = usb_submit_urb(urb, GFP_KERNEL);
502 if (err) {
503 dbg("%s: submit urb %d failed (%d) %d", __FUNCTION__, i, err,
504 urb->transfer_buffer_length);
505 }
506 }
507
508 /* Reset low level data toggle on out endpoints */
509 for (i = 0; i < N_OUT_URB; i++) {
510 urb = portdata->out_urbs[i];
511 if (! urb)
512 continue;
513 urb->dev = serial->dev;
514 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
515 }
516
517 port->tty->low_latency = 1;
518
519 option_send_setup(port);
520
521 return (0);
522 }
523
524 static inline void
525 stop_urb (struct urb *urb)
526 {
527 if (urb && urb->status == -EINPROGRESS) {
528 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
529 usb_kill_urb(urb);
530 }
531 }
532
533 static void
534 option_close (struct usb_serial_port *port, struct file *filp)
535 {
536 int i;
537 struct usb_serial *serial = port->serial;
538 struct option_port_private *portdata;
539
540 dbg("%s", __FUNCTION__);
541 portdata = usb_get_serial_port_data(port);
542
543 portdata->rts_state = 0;
544 portdata->dtr_state = 0;
545
546 if (serial->dev) {
547 option_send_setup(port);
548
549 /* Stop reading/writing urbs */
550 for (i = 0; i < N_IN_URB; i++)
551 stop_urb(portdata->in_urbs[i]);
552 for (i = 0; i < N_OUT_URB; i++)
553 stop_urb(portdata->out_urbs[i]);
554 }
555 port->tty = NULL;
556 }
557
558
559 /* Helper functions used by option_setup_urbs */
560 static struct urb *
561 option_setup_urb (struct usb_serial *serial, int endpoint,
562 int dir, void *ctx, char *buf, int len,
563 void (*callback)(struct urb *, struct pt_regs *regs))
564 {
565 struct urb *urb;
566
567 if (endpoint == -1)
568 return NULL; /* endpoint not needed */
569
570 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
571 if (urb == NULL) {
572 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
573 return NULL;
574 }
575
576 /* Fill URB using supplied data. */
577 usb_fill_bulk_urb(urb, serial->dev,
578 usb_sndbulkpipe(serial->dev, endpoint) | dir,
579 buf, len, callback, ctx);
580
581 return urb;
582 }
583
584 /* Setup urbs */
585 static void
586 option_setup_urbs (struct usb_serial *serial)
587 {
588 int j;
589 struct usb_serial_port *port;
590 struct option_port_private *portdata;
591
592 dbg("%s", __FUNCTION__);
593
594 port = serial->port[0];
595 portdata = usb_get_serial_port_data(port);
596
597 /* Do indat endpoints first */
598 for (j = 0; j <= N_IN_URB; ++j) {
599 portdata->in_urbs[j] = option_setup_urb (serial,
600 port->bulk_in_endpointAddress, USB_DIR_IN, port,
601 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
602 }
603
604 /* outdat endpoints */
605 for (j = 0; j <= N_OUT_URB; ++j) {
606 portdata->out_urbs[j] = option_setup_urb (serial,
607 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
608 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
609 }
610 }
611
612
613 static int
614 option_send_setup (struct usb_serial_port *port)
615 {
616 struct usb_serial *serial = port->serial;
617 struct option_port_private *portdata;
618
619 dbg("%s", __FUNCTION__);
620
621 portdata = usb_get_serial_port_data(port);
622
623 if (port->tty) {
624 int val = 0;
625 if (portdata->dtr_state)
626 val |= 0x01;
627 if (portdata->rts_state)
628 val |= 0x02;
629
630 return usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
631 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
632 }
633
634 return 0;
635 }
636
637
638 static int
639 option_startup (struct usb_serial *serial)
640 {
641 int i, err;
642 struct usb_serial_port *port;
643 struct option_port_private *portdata;
644
645 dbg("%s", __FUNCTION__);
646
647 /* Now setup per port private data */
648 for (i = 0; i < serial->num_ports; i++) {
649 port = serial->port[i];
650 portdata = kmalloc(sizeof(struct option_port_private), GFP_KERNEL);
651 if (!portdata) {
652 dbg("%s: kmalloc for option_port_private (%d) failed!.", __FUNCTION__, i);
653 return (1);
654 }
655 memset(portdata, 0, sizeof(struct option_port_private));
656
657 usb_set_serial_port_data(port, portdata);
658
659 if (! port->interrupt_in_urb)
660 continue;
661 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
662 if (err)
663 dbg("%s: submit irq_in urb failed %d", __FUNCTION__, err);
664 }
665
666 option_setup_urbs(serial);
667
668 return (0);
669 }
670
671 static void
672 option_shutdown (struct usb_serial *serial)
673 {
674 int i, j;
675 struct usb_serial_port *port;
676 struct option_port_private *portdata;
677
678 dbg("%s", __FUNCTION__);
679
680 /* Stop reading/writing urbs */
681 for (i = 0; i < serial->num_ports; ++i) {
682 port = serial->port[i];
683 portdata = usb_get_serial_port_data(port);
684 for (j = 0; j < N_IN_URB; j++)
685 stop_urb(portdata->in_urbs[j]);
686 for (j = 0; j < N_OUT_URB; j++)
687 stop_urb(portdata->out_urbs[j]);
688 }
689
690 /* Now free them */
691 for (i = 0; i < serial->num_ports; ++i) {
692 port = serial->port[i];
693 portdata = usb_get_serial_port_data(port);
694
695 for (j = 0; j < N_IN_URB; j++) {
696 if (portdata->in_urbs[j]) {
697 usb_free_urb(portdata->in_urbs[j]);
698 portdata->in_urbs[j] = NULL;
699 }
700 }
701 for (j = 0; j < N_OUT_URB; j++) {
702 if (portdata->out_urbs[j]) {
703 usb_free_urb(portdata->out_urbs[j]);
704 portdata->out_urbs[j] = NULL;
705 }
706 }
707 }
708
709 /* Now free per port private data */
710 for (i = 0; i < serial->num_ports; i++) {
711 port = serial->port[i];
712 kfree(usb_get_serial_port_data(port));
713 }
714 }
715
716 MODULE_AUTHOR(DRIVER_AUTHOR);
717 MODULE_DESCRIPTION(DRIVER_DESC);
718 MODULE_VERSION(DRIVER_VERSION);
719 MODULE_LICENSE("GPL");
720
721 #ifdef CONFIG_USB_DEBUG
722 module_param(debug, bool, S_IRUGO | S_IWUSR);
723 MODULE_PARM_DESC(debug, "Debug messages");
724 #endif
725