diff options
author | David Brownell <dbrownell@users.sourceforge.net> | 2008-06-19 20:52:07 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2008-07-21 18:15:59 -0400 |
commit | a7707adf9ee8de3c5b67e3793b98888f551ad00d (patch) | |
tree | 431137e4c39202f6395f64893966e3bdfa1349d2 /drivers/usb/gadget/serial.c | |
parent | c1dca562be8ada614ef193aa246c6f8705bcd6b9 (diff) |
usb gadget: use new serial core
Teach "gadget serial" to use the new abstracted (and bugfixed) TTY glue,
and remove all the orignal tangled-up code. Update the documentation
accordingly. This is a net object code shrink and cleanup; it should
make it a lot easier to see how the TTY glue should accomodate updates
to the TTY layer, be bugfixed, etc.
Notable behavior changes include: it can now support getty even when
there's no USB connection; it fits properly into the mdev/udev world;
and RX handling is better (throttling works, and low latency).
Configurations with scripts setting up the /dev/ttygserial device node
(with "experimental" major number) may want to change that to be a
symlink pointing to the /dev/ttyGS0 file, as a migration aid; else,
just switch entirely over to mdev/udev.
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/gadget/serial.c')
-rw-r--r-- | drivers/usb/gadget/serial.c | 1392 |
1 files changed, 76 insertions, 1316 deletions
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c index fa019fa73334..64ce7e00e98a 100644 --- a/drivers/usb/gadget/serial.c +++ b/drivers/usb/gadget/serial.c | |||
@@ -1,15 +1,8 @@ | |||
1 | /* | 1 | /* |
2 | * g_serial.c -- USB gadget serial driver | 2 | * serial.c -- USB gadget serial driver |
3 | * | 3 | * |
4 | * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com) | 4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) |
5 | * | 5 | * Copyright (C) 2008 by David Brownell |
6 | * This code is based in part on the Gadget Zero driver, which | ||
7 | * is Copyright (C) 2003 by David Brownell, all rights reserved. | ||
8 | * | ||
9 | * This code also borrows from usbserial.c, which is | ||
10 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) | ||
11 | * Copyright (C) 2000 Peter Berger (pberger@brimson.com) | ||
12 | * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com) | ||
13 | * | 6 | * |
14 | * This software is distributed under the terms of the GNU General | 7 | * This software is distributed under the terms of the GNU General |
15 | * Public License ("GPL") as published by the Free Software Foundation, | 8 | * Public License ("GPL") as published by the Free Software Foundation, |
@@ -22,23 +15,20 @@ | |||
22 | #include <linux/tty.h> | 15 | #include <linux/tty.h> |
23 | #include <linux/tty_flip.h> | 16 | #include <linux/tty_flip.h> |
24 | 17 | ||
25 | #include <linux/usb/ch9.h> | 18 | #include "u_serial.h" |
26 | #include <linux/usb/cdc.h> | ||
27 | #include <linux/usb/gadget.h> | ||
28 | |||
29 | #include "gadget_chips.h" | 19 | #include "gadget_chips.h" |
30 | 20 | ||
31 | 21 | ||
32 | /* Defines */ | 22 | /* Defines */ |
33 | 23 | ||
34 | #define GS_VERSION_STR "v2.2" | 24 | #define GS_VERSION_STR "v2.3" |
35 | #define GS_VERSION_NUM 0x2200 | 25 | #define GS_VERSION_NUM 0x2300 |
36 | 26 | ||
37 | #define GS_LONG_NAME "Gadget Serial" | 27 | #define GS_LONG_NAME "Gadget Serial" |
38 | #define GS_SHORT_NAME "g_serial" | 28 | #define GS_SHORT_NAME "g_serial" |
39 | 29 | ||
40 | #define GS_MAJOR 127 | 30 | #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR |
41 | #define GS_MINOR_START 0 | 31 | |
42 | 32 | ||
43 | /* REVISIT only one port is supported for now; | 33 | /* REVISIT only one port is supported for now; |
44 | * see gs_{send,recv}_packet() ... no multiplexing, | 34 | * see gs_{send,recv}_packet() ... no multiplexing, |
@@ -58,23 +48,8 @@ | |||
58 | 48 | ||
59 | #define GS_MAX_DESC_LEN 256 | 49 | #define GS_MAX_DESC_LEN 256 |
60 | 50 | ||
61 | #define GS_DEFAULT_READ_Q_SIZE 32 | ||
62 | #define GS_DEFAULT_WRITE_Q_SIZE 32 | ||
63 | |||
64 | #define GS_DEFAULT_WRITE_BUF_SIZE 8192 | ||
65 | #define GS_TMP_BUF_SIZE 8192 | ||
66 | |||
67 | #define GS_CLOSE_TIMEOUT 15 | ||
68 | |||
69 | #define GS_DEFAULT_USE_ACM 0 | 51 | #define GS_DEFAULT_USE_ACM 0 |
70 | 52 | ||
71 | /* 9600-8-N-1 ... matches init_termios.c_cflag and defaults | ||
72 | * expected by "usbser.sys" on MS-Windows. | ||
73 | */ | ||
74 | #define GS_DEFAULT_DTE_RATE 9600 | ||
75 | #define GS_DEFAULT_DATA_BITS 8 | ||
76 | #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY | ||
77 | #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS | ||
78 | 53 | ||
79 | /* maxpacket and other transfer characteristics vary by speed. */ | 54 | /* maxpacket and other transfer characteristics vary by speed. */ |
80 | static inline struct usb_endpoint_descriptor * | 55 | static inline struct usb_endpoint_descriptor * |
@@ -87,19 +62,6 @@ choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, | |||
87 | } | 62 | } |
88 | 63 | ||
89 | 64 | ||
90 | /* debug settings */ | ||
91 | #ifdef DEBUG | ||
92 | static int debug = 1; | ||
93 | #else | ||
94 | #define debug 0 | ||
95 | #endif | ||
96 | |||
97 | #define gs_debug(format, arg...) \ | ||
98 | do { if (debug) pr_debug(format, ## arg); } while (0) | ||
99 | #define gs_debug_level(level, format, arg...) \ | ||
100 | do { if (debug >= level) pr_debug(format, ## arg); } while (0) | ||
101 | |||
102 | |||
103 | /* Thanks to NetChip Technologies for donating this product ID. | 65 | /* Thanks to NetChip Technologies for donating this product ID. |
104 | * | 66 | * |
105 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | 67 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
@@ -112,63 +74,19 @@ static int debug = 1; | |||
112 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ | 74 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ |
113 | #define GS_NOTIFY_MAXPACKET 8 | 75 | #define GS_NOTIFY_MAXPACKET 8 |
114 | 76 | ||
115 | |||
116 | /* circular buffer */ | ||
117 | struct gs_buf { | ||
118 | unsigned int buf_size; | ||
119 | char *buf_buf; | ||
120 | char *buf_get; | ||
121 | char *buf_put; | ||
122 | }; | ||
123 | |||
124 | /* the port structure holds info for each port, one for each minor number */ | ||
125 | struct gs_port { | ||
126 | struct gs_dev *port_dev; /* pointer to device struct */ | ||
127 | struct tty_struct *port_tty; /* pointer to tty struct */ | ||
128 | spinlock_t port_lock; | ||
129 | int port_num; | ||
130 | int port_open_count; | ||
131 | int port_in_use; /* open/close in progress */ | ||
132 | wait_queue_head_t port_write_wait;/* waiting to write */ | ||
133 | struct gs_buf *port_write_buf; | ||
134 | struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ | ||
135 | u16 port_handshake_bits; | ||
136 | #define RS232_RTS (1 << 1) | ||
137 | #define RS232_DTE (1 << 0) | ||
138 | }; | ||
139 | |||
140 | /* the device structure holds info for the USB device */ | 77 | /* the device structure holds info for the USB device */ |
141 | struct gs_dev { | 78 | struct gs_dev { |
142 | struct usb_gadget *dev_gadget; /* gadget device pointer */ | 79 | struct usb_gadget *dev_gadget; /* gadget device pointer */ |
143 | spinlock_t dev_lock; /* lock for set/reset config */ | 80 | spinlock_t dev_lock; /* lock for set/reset config */ |
144 | int dev_config; /* configuration number */ | 81 | int dev_config; /* configuration number */ |
145 | struct usb_ep *dev_notify_ep; /* address of notify endpoint */ | ||
146 | struct usb_ep *dev_in_ep; /* address of in endpoint */ | ||
147 | struct usb_ep *dev_out_ep; /* address of out endpoint */ | ||
148 | struct usb_endpoint_descriptor /* descriptor of notify ep */ | ||
149 | *dev_notify_ep_desc; | ||
150 | struct usb_endpoint_descriptor /* descriptor of in endpoint */ | ||
151 | *dev_in_ep_desc; | ||
152 | struct usb_endpoint_descriptor /* descriptor of out endpoint */ | ||
153 | *dev_out_ep_desc; | ||
154 | struct usb_request *dev_ctrl_req; /* control request */ | 82 | struct usb_request *dev_ctrl_req; /* control request */ |
155 | struct list_head dev_req_list; /* list of write requests */ | 83 | |
156 | int dev_sched_port; /* round robin port scheduled */ | 84 | struct gserial gser; /* serial/tty port */ |
157 | struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */ | ||
158 | }; | 85 | }; |
159 | 86 | ||
160 | 87 | ||
161 | /* Functions */ | 88 | /* Functions */ |
162 | 89 | ||
163 | /* tty driver internals */ | ||
164 | static int gs_send(struct gs_dev *dev); | ||
165 | static int gs_send_packet(struct gs_dev *dev, char *packet, | ||
166 | unsigned int size); | ||
167 | static int gs_recv_packet(struct gs_dev *dev, char *packet, | ||
168 | unsigned int size); | ||
169 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req); | ||
170 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req); | ||
171 | |||
172 | /* gadget driver internals */ | 90 | /* gadget driver internals */ |
173 | static int gs_set_config(struct gs_dev *dev, unsigned config); | 91 | static int gs_set_config(struct gs_dev *dev, unsigned config); |
174 | static void gs_reset_config(struct gs_dev *dev); | 92 | static void gs_reset_config(struct gs_dev *dev); |
@@ -179,28 +97,6 @@ static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, | |||
179 | gfp_t kmalloc_flags); | 97 | gfp_t kmalloc_flags); |
180 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req); | 98 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req); |
181 | 99 | ||
182 | static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags); | ||
183 | static void gs_free_ports(struct gs_dev *dev); | ||
184 | |||
185 | /* circular buffer */ | ||
186 | static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags); | ||
187 | static void gs_buf_free(struct gs_buf *gb); | ||
188 | static void gs_buf_clear(struct gs_buf *gb); | ||
189 | static unsigned int gs_buf_data_avail(struct gs_buf *gb); | ||
190 | static unsigned int gs_buf_space_avail(struct gs_buf *gb); | ||
191 | static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, | ||
192 | unsigned int count); | ||
193 | static unsigned int gs_buf_get(struct gs_buf *gb, char *buf, | ||
194 | unsigned int count); | ||
195 | |||
196 | |||
197 | /* Globals */ | ||
198 | |||
199 | static struct gs_dev *gs_device; | ||
200 | |||
201 | static struct mutex gs_open_close_lock[GS_NUM_PORTS]; | ||
202 | |||
203 | |||
204 | /*-------------------------------------------------------------------------*/ | 100 | /*-------------------------------------------------------------------------*/ |
205 | 101 | ||
206 | /* USB descriptors */ | 102 | /* USB descriptors */ |
@@ -217,7 +113,7 @@ static struct mutex gs_open_close_lock[GS_NUM_PORTS]; | |||
217 | static char manufacturer[50]; | 113 | static char manufacturer[50]; |
218 | static struct usb_string gs_strings[] = { | 114 | static struct usb_string gs_strings[] = { |
219 | { GS_MANUFACTURER_STR_ID, manufacturer }, | 115 | { GS_MANUFACTURER_STR_ID, manufacturer }, |
220 | { GS_PRODUCT_STR_ID, GS_LONG_NAME }, | 116 | { GS_PRODUCT_STR_ID, GS_VERSION_NAME }, |
221 | { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" }, | 117 | { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" }, |
222 | { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" }, | 118 | { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" }, |
223 | { GS_CONTROL_STR_ID, "Gadget Serial Control" }, | 119 | { GS_CONTROL_STR_ID, "Gadget Serial Control" }, |
@@ -436,761 +332,17 @@ static const struct usb_descriptor_header *gs_acm_highspeed_function[] = { | |||
436 | /*-------------------------------------------------------------------------*/ | 332 | /*-------------------------------------------------------------------------*/ |
437 | 333 | ||
438 | /* Module */ | 334 | /* Module */ |
439 | MODULE_DESCRIPTION(GS_LONG_NAME); | 335 | MODULE_DESCRIPTION(GS_VERSION_NAME); |
440 | MODULE_AUTHOR("Al Borchers"); | 336 | MODULE_AUTHOR("Al Borchers"); |
337 | MODULE_AUTHOR("David Brownell"); | ||
441 | MODULE_LICENSE("GPL"); | 338 | MODULE_LICENSE("GPL"); |
442 | 339 | ||
443 | #ifdef DEBUG | ||
444 | module_param(debug, int, S_IRUGO|S_IWUSR); | ||
445 | MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on"); | ||
446 | #endif | ||
447 | |||
448 | static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE; | ||
449 | module_param(read_q_size, uint, S_IRUGO); | ||
450 | MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32"); | ||
451 | |||
452 | static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE; | ||
453 | module_param(write_q_size, uint, S_IRUGO); | ||
454 | MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32"); | ||
455 | |||
456 | static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE; | ||
457 | module_param(write_buf_size, uint, S_IRUGO); | ||
458 | MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192"); | ||
459 | |||
460 | static unsigned int use_acm = GS_DEFAULT_USE_ACM; | 340 | static unsigned int use_acm = GS_DEFAULT_USE_ACM; |
461 | module_param(use_acm, uint, S_IRUGO); | 341 | module_param(use_acm, uint, S_IRUGO); |
462 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no"); | 342 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no"); |
463 | 343 | ||
464 | /*-------------------------------------------------------------------------*/ | 344 | /*-------------------------------------------------------------------------*/ |
465 | 345 | ||
466 | /* TTY Driver */ | ||
467 | |||
468 | /* | ||
469 | * gs_open | ||
470 | */ | ||
471 | static int gs_open(struct tty_struct *tty, struct file *file) | ||
472 | { | ||
473 | int port_num; | ||
474 | unsigned long flags; | ||
475 | struct gs_port *port; | ||
476 | struct gs_dev *dev; | ||
477 | struct gs_buf *buf; | ||
478 | struct mutex *mtx; | ||
479 | int ret; | ||
480 | |||
481 | port_num = tty->index; | ||
482 | |||
483 | gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file); | ||
484 | |||
485 | if (port_num < 0 || port_num >= GS_NUM_PORTS) { | ||
486 | pr_err("gs_open: (%d,%p,%p) invalid port number\n", | ||
487 | port_num, tty, file); | ||
488 | return -ENODEV; | ||
489 | } | ||
490 | |||
491 | dev = gs_device; | ||
492 | |||
493 | if (dev == NULL) { | ||
494 | pr_err("gs_open: (%d,%p,%p) NULL device pointer\n", | ||
495 | port_num, tty, file); | ||
496 | return -ENODEV; | ||
497 | } | ||
498 | |||
499 | mtx = &gs_open_close_lock[port_num]; | ||
500 | if (mutex_lock_interruptible(mtx)) { | ||
501 | pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n", | ||
502 | port_num, tty, file); | ||
503 | return -ERESTARTSYS; | ||
504 | } | ||
505 | |||
506 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
507 | |||
508 | if (dev->dev_config == GS_NO_CONFIG_ID) { | ||
509 | pr_err("gs_open: (%d,%p,%p) device is not connected\n", | ||
510 | port_num, tty, file); | ||
511 | ret = -ENODEV; | ||
512 | goto exit_unlock_dev; | ||
513 | } | ||
514 | |||
515 | port = dev->dev_port[port_num]; | ||
516 | |||
517 | if (port == NULL) { | ||
518 | pr_err("gs_open: (%d,%p,%p) NULL port pointer\n", | ||
519 | port_num, tty, file); | ||
520 | ret = -ENODEV; | ||
521 | goto exit_unlock_dev; | ||
522 | } | ||
523 | |||
524 | spin_lock(&port->port_lock); | ||
525 | spin_unlock(&dev->dev_lock); | ||
526 | |||
527 | if (port->port_dev == NULL) { | ||
528 | pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n", | ||
529 | port_num, tty, file); | ||
530 | ret = -EIO; | ||
531 | goto exit_unlock_port; | ||
532 | } | ||
533 | |||
534 | if (port->port_open_count > 0) { | ||
535 | ++port->port_open_count; | ||
536 | gs_debug("gs_open: (%d,%p,%p) already open\n", | ||
537 | port_num, tty, file); | ||
538 | ret = 0; | ||
539 | goto exit_unlock_port; | ||
540 | } | ||
541 | |||
542 | tty->driver_data = NULL; | ||
543 | |||
544 | /* mark port as in use, we can drop port lock and sleep if necessary */ | ||
545 | port->port_in_use = 1; | ||
546 | |||
547 | /* allocate write buffer on first open */ | ||
548 | if (port->port_write_buf == NULL) { | ||
549 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
550 | buf = gs_buf_alloc(write_buf_size, GFP_KERNEL); | ||
551 | spin_lock_irqsave(&port->port_lock, flags); | ||
552 | |||
553 | /* might have been disconnected while asleep, check */ | ||
554 | if (port->port_dev == NULL) { | ||
555 | pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n", | ||
556 | port_num, tty, file); | ||
557 | port->port_in_use = 0; | ||
558 | ret = -EIO; | ||
559 | goto exit_unlock_port; | ||
560 | } | ||
561 | |||
562 | if ((port->port_write_buf=buf) == NULL) { | ||
563 | pr_err("gs_open: (%d,%p,%p) cannot allocate " | ||
564 | "port write buffer\n", | ||
565 | port_num, tty, file); | ||
566 | port->port_in_use = 0; | ||
567 | ret = -ENOMEM; | ||
568 | goto exit_unlock_port; | ||
569 | } | ||
570 | |||
571 | } | ||
572 | |||
573 | /* wait for carrier detect (not implemented) */ | ||
574 | |||
575 | /* might have been disconnected while asleep, check */ | ||
576 | if (port->port_dev == NULL) { | ||
577 | pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n", | ||
578 | port_num, tty, file); | ||
579 | port->port_in_use = 0; | ||
580 | ret = -EIO; | ||
581 | goto exit_unlock_port; | ||
582 | } | ||
583 | |||
584 | tty->driver_data = port; | ||
585 | port->port_tty = tty; | ||
586 | port->port_open_count = 1; | ||
587 | port->port_in_use = 0; | ||
588 | |||
589 | gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file); | ||
590 | |||
591 | ret = 0; | ||
592 | |||
593 | exit_unlock_port: | ||
594 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
595 | mutex_unlock(mtx); | ||
596 | return ret; | ||
597 | |||
598 | exit_unlock_dev: | ||
599 | spin_unlock_irqrestore(&dev->dev_lock, flags); | ||
600 | mutex_unlock(mtx); | ||
601 | return ret; | ||
602 | |||
603 | } | ||
604 | |||
605 | /* | ||
606 | * gs_close | ||
607 | */ | ||
608 | |||
609 | static int gs_write_finished_event_safely(struct gs_port *p) | ||
610 | { | ||
611 | int cond; | ||
612 | |||
613 | spin_lock_irq(&(p)->port_lock); | ||
614 | cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); | ||
615 | spin_unlock_irq(&(p)->port_lock); | ||
616 | return cond; | ||
617 | } | ||
618 | |||
619 | static void gs_close(struct tty_struct *tty, struct file *file) | ||
620 | { | ||
621 | struct gs_port *port = tty->driver_data; | ||
622 | struct mutex *mtx; | ||
623 | |||
624 | if (port == NULL) { | ||
625 | pr_err("gs_close: NULL port pointer\n"); | ||
626 | return; | ||
627 | } | ||
628 | |||
629 | gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file); | ||
630 | |||
631 | mtx = &gs_open_close_lock[port->port_num]; | ||
632 | mutex_lock(mtx); | ||
633 | |||
634 | spin_lock_irq(&port->port_lock); | ||
635 | |||
636 | if (port->port_open_count == 0) { | ||
637 | pr_err("gs_close: (%d,%p,%p) port is already closed\n", | ||
638 | port->port_num, tty, file); | ||
639 | goto exit; | ||
640 | } | ||
641 | |||
642 | if (port->port_open_count > 1) { | ||
643 | --port->port_open_count; | ||
644 | goto exit; | ||
645 | } | ||
646 | |||
647 | /* free disconnected port on final close */ | ||
648 | if (port->port_dev == NULL) { | ||
649 | kfree(port); | ||
650 | goto exit; | ||
651 | } | ||
652 | |||
653 | /* mark port as closed but in use, we can drop port lock */ | ||
654 | /* and sleep if necessary */ | ||
655 | port->port_in_use = 1; | ||
656 | port->port_open_count = 0; | ||
657 | |||
658 | /* wait for write buffer to drain, or */ | ||
659 | /* at most GS_CLOSE_TIMEOUT seconds */ | ||
660 | if (gs_buf_data_avail(port->port_write_buf) > 0) { | ||
661 | spin_unlock_irq(&port->port_lock); | ||
662 | wait_event_interruptible_timeout(port->port_write_wait, | ||
663 | gs_write_finished_event_safely(port), | ||
664 | GS_CLOSE_TIMEOUT * HZ); | ||
665 | spin_lock_irq(&port->port_lock); | ||
666 | } | ||
667 | |||
668 | /* free disconnected port on final close */ | ||
669 | /* (might have happened during the above sleep) */ | ||
670 | if (port->port_dev == NULL) { | ||
671 | kfree(port); | ||
672 | goto exit; | ||
673 | } | ||
674 | |||
675 | gs_buf_clear(port->port_write_buf); | ||
676 | |||
677 | tty->driver_data = NULL; | ||
678 | port->port_tty = NULL; | ||
679 | port->port_in_use = 0; | ||
680 | |||
681 | gs_debug("gs_close: (%d,%p,%p) completed\n", | ||
682 | port->port_num, tty, file); | ||
683 | |||
684 | exit: | ||
685 | spin_unlock_irq(&port->port_lock); | ||
686 | mutex_unlock(mtx); | ||
687 | } | ||
688 | |||
689 | /* | ||
690 | * gs_write | ||
691 | */ | ||
692 | static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) | ||
693 | { | ||
694 | unsigned long flags; | ||
695 | struct gs_port *port = tty->driver_data; | ||
696 | int ret; | ||
697 | |||
698 | if (port == NULL) { | ||
699 | pr_err("gs_write: NULL port pointer\n"); | ||
700 | return -EIO; | ||
701 | } | ||
702 | |||
703 | gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty, | ||
704 | count); | ||
705 | |||
706 | if (count == 0) | ||
707 | return 0; | ||
708 | |||
709 | spin_lock_irqsave(&port->port_lock, flags); | ||
710 | |||
711 | if (port->port_dev == NULL) { | ||
712 | pr_err("gs_write: (%d,%p) port is not connected\n", | ||
713 | port->port_num, tty); | ||
714 | ret = -EIO; | ||
715 | goto exit; | ||
716 | } | ||
717 | |||
718 | if (port->port_open_count == 0) { | ||
719 | pr_err("gs_write: (%d,%p) port is closed\n", | ||
720 | port->port_num, tty); | ||
721 | ret = -EBADF; | ||
722 | goto exit; | ||
723 | } | ||
724 | |||
725 | count = gs_buf_put(port->port_write_buf, buf, count); | ||
726 | |||
727 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
728 | |||
729 | gs_send(gs_device); | ||
730 | |||
731 | gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty, | ||
732 | count); | ||
733 | |||
734 | return count; | ||
735 | |||
736 | exit: | ||
737 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
738 | return ret; | ||
739 | } | ||
740 | |||
741 | /* | ||
742 | * gs_put_char | ||
743 | */ | ||
744 | static int gs_put_char(struct tty_struct *tty, unsigned char ch) | ||
745 | { | ||
746 | unsigned long flags; | ||
747 | struct gs_port *port = tty->driver_data; | ||
748 | int ret = 0; | ||
749 | |||
750 | if (port == NULL) { | ||
751 | pr_err("gs_put_char: NULL port pointer\n"); | ||
752 | return 0; | ||
753 | } | ||
754 | |||
755 | gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n", | ||
756 | port->port_num, tty, ch, __builtin_return_address(0)); | ||
757 | |||
758 | spin_lock_irqsave(&port->port_lock, flags); | ||
759 | |||
760 | if (port->port_dev == NULL) { | ||
761 | pr_err("gs_put_char: (%d,%p) port is not connected\n", | ||
762 | port->port_num, tty); | ||
763 | goto exit; | ||
764 | } | ||
765 | |||
766 | if (port->port_open_count == 0) { | ||
767 | pr_err("gs_put_char: (%d,%p) port is closed\n", | ||
768 | port->port_num, tty); | ||
769 | goto exit; | ||
770 | } | ||
771 | |||
772 | ret = gs_buf_put(port->port_write_buf, &ch, 1); | ||
773 | |||
774 | exit: | ||
775 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
776 | return ret; | ||
777 | } | ||
778 | |||
779 | /* | ||
780 | * gs_flush_chars | ||
781 | */ | ||
782 | static void gs_flush_chars(struct tty_struct *tty) | ||
783 | { | ||
784 | unsigned long flags; | ||
785 | struct gs_port *port = tty->driver_data; | ||
786 | |||
787 | if (port == NULL) { | ||
788 | pr_err("gs_flush_chars: NULL port pointer\n"); | ||
789 | return; | ||
790 | } | ||
791 | |||
792 | gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); | ||
793 | |||
794 | spin_lock_irqsave(&port->port_lock, flags); | ||
795 | |||
796 | if (port->port_dev == NULL) { | ||
797 | pr_err("gs_flush_chars: (%d,%p) port is not connected\n", | ||
798 | port->port_num, tty); | ||
799 | goto exit; | ||
800 | } | ||
801 | |||
802 | if (port->port_open_count == 0) { | ||
803 | pr_err("gs_flush_chars: (%d,%p) port is closed\n", | ||
804 | port->port_num, tty); | ||
805 | goto exit; | ||
806 | } | ||
807 | |||
808 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
809 | |||
810 | gs_send(gs_device); | ||
811 | |||
812 | return; | ||
813 | |||
814 | exit: | ||
815 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
816 | } | ||
817 | |||
818 | /* | ||
819 | * gs_write_room | ||
820 | */ | ||
821 | static int gs_write_room(struct tty_struct *tty) | ||
822 | { | ||
823 | |||
824 | int room = 0; | ||
825 | unsigned long flags; | ||
826 | struct gs_port *port = tty->driver_data; | ||
827 | |||
828 | |||
829 | if (port == NULL) | ||
830 | return 0; | ||
831 | |||
832 | spin_lock_irqsave(&port->port_lock, flags); | ||
833 | |||
834 | if (port->port_dev != NULL && port->port_open_count > 0 | ||
835 | && port->port_write_buf != NULL) | ||
836 | room = gs_buf_space_avail(port->port_write_buf); | ||
837 | |||
838 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
839 | |||
840 | gs_debug("gs_write_room: (%d,%p) room=%d\n", | ||
841 | port->port_num, tty, room); | ||
842 | |||
843 | return room; | ||
844 | } | ||
845 | |||
846 | /* | ||
847 | * gs_chars_in_buffer | ||
848 | */ | ||
849 | static int gs_chars_in_buffer(struct tty_struct *tty) | ||
850 | { | ||
851 | int chars = 0; | ||
852 | unsigned long flags; | ||
853 | struct gs_port *port = tty->driver_data; | ||
854 | |||
855 | if (port == NULL) | ||
856 | return 0; | ||
857 | |||
858 | spin_lock_irqsave(&port->port_lock, flags); | ||
859 | |||
860 | if (port->port_dev != NULL && port->port_open_count > 0 | ||
861 | && port->port_write_buf != NULL) | ||
862 | chars = gs_buf_data_avail(port->port_write_buf); | ||
863 | |||
864 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
865 | |||
866 | gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n", | ||
867 | port->port_num, tty, chars); | ||
868 | |||
869 | return chars; | ||
870 | } | ||
871 | |||
872 | /* | ||
873 | * gs_throttle | ||
874 | */ | ||
875 | static void gs_throttle(struct tty_struct *tty) | ||
876 | { | ||
877 | } | ||
878 | |||
879 | /* | ||
880 | * gs_unthrottle | ||
881 | */ | ||
882 | static void gs_unthrottle(struct tty_struct *tty) | ||
883 | { | ||
884 | } | ||
885 | |||
886 | /* | ||
887 | * gs_break | ||
888 | */ | ||
889 | static void gs_break(struct tty_struct *tty, int break_state) | ||
890 | { | ||
891 | } | ||
892 | |||
893 | /* | ||
894 | * gs_ioctl | ||
895 | */ | ||
896 | static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) | ||
897 | { | ||
898 | struct gs_port *port = tty->driver_data; | ||
899 | |||
900 | if (port == NULL) { | ||
901 | pr_err("gs_ioctl: NULL port pointer\n"); | ||
902 | return -EIO; | ||
903 | } | ||
904 | |||
905 | gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n", | ||
906 | port->port_num, tty, file, cmd, arg); | ||
907 | |||
908 | /* handle ioctls */ | ||
909 | |||
910 | /* could not handle ioctl */ | ||
911 | return -ENOIOCTLCMD; | ||
912 | } | ||
913 | |||
914 | /* | ||
915 | * gs_set_termios | ||
916 | */ | ||
917 | static void gs_set_termios(struct tty_struct *tty, struct ktermios *old) | ||
918 | { | ||
919 | } | ||
920 | |||
921 | static const struct tty_operations gs_tty_ops = { | ||
922 | .open = gs_open, | ||
923 | .close = gs_close, | ||
924 | .write = gs_write, | ||
925 | .put_char = gs_put_char, | ||
926 | .flush_chars = gs_flush_chars, | ||
927 | .write_room = gs_write_room, | ||
928 | .ioctl = gs_ioctl, | ||
929 | .set_termios = gs_set_termios, | ||
930 | .throttle = gs_throttle, | ||
931 | .unthrottle = gs_unthrottle, | ||
932 | .break_ctl = gs_break, | ||
933 | .chars_in_buffer = gs_chars_in_buffer, | ||
934 | }; | ||
935 | |||
936 | /*-------------------------------------------------------------------------*/ | ||
937 | |||
938 | /* | ||
939 | * gs_send | ||
940 | * | ||
941 | * This function finds available write requests, calls | ||
942 | * gs_send_packet to fill these packets with data, and | ||
943 | * continues until either there are no more write requests | ||
944 | * available or no more data to send. This function is | ||
945 | * run whenever data arrives or write requests are available. | ||
946 | */ | ||
947 | static int gs_send(struct gs_dev *dev) | ||
948 | { | ||
949 | int ret,len; | ||
950 | unsigned long flags; | ||
951 | struct usb_ep *ep; | ||
952 | struct usb_request *req; | ||
953 | |||
954 | if (dev == NULL) { | ||
955 | pr_err("gs_send: NULL device pointer\n"); | ||
956 | return -ENODEV; | ||
957 | } | ||
958 | |||
959 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
960 | |||
961 | ep = dev->dev_in_ep; | ||
962 | |||
963 | while(!list_empty(&dev->dev_req_list)) { | ||
964 | |||
965 | req = list_entry(dev->dev_req_list.next, | ||
966 | struct usb_request, list); | ||
967 | |||
968 | len = gs_send_packet(dev, req->buf, ep->maxpacket); | ||
969 | |||
970 | if (len > 0) { | ||
971 | gs_debug_level(3, "gs_send: len=%d, 0x%2.2x " | ||
972 | "0x%2.2x 0x%2.2x ...\n", len, | ||
973 | *((unsigned char *)req->buf), | ||
974 | *((unsigned char *)req->buf+1), | ||
975 | *((unsigned char *)req->buf+2)); | ||
976 | list_del(&req->list); | ||
977 | req->length = len; | ||
978 | spin_unlock_irqrestore(&dev->dev_lock, flags); | ||
979 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { | ||
980 | pr_err( | ||
981 | "gs_send: cannot queue read request, ret=%d\n", | ||
982 | ret); | ||
983 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
984 | break; | ||
985 | } | ||
986 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
987 | } else { | ||
988 | break; | ||
989 | } | ||
990 | |||
991 | } | ||
992 | |||
993 | spin_unlock_irqrestore(&dev->dev_lock, flags); | ||
994 | |||
995 | return 0; | ||
996 | } | ||
997 | |||
998 | /* | ||
999 | * gs_send_packet | ||
1000 | * | ||
1001 | * If there is data to send, a packet is built in the given | ||
1002 | * buffer and the size is returned. If there is no data to | ||
1003 | * send, 0 is returned. If there is any error a negative | ||
1004 | * error number is returned. | ||
1005 | * | ||
1006 | * Called during USB completion routine, on interrupt time. | ||
1007 | * | ||
1008 | * We assume that disconnect will not happen until all completion | ||
1009 | * routines have completed, so we can assume that the dev_port | ||
1010 | * array does not change during the lifetime of this function. | ||
1011 | */ | ||
1012 | static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size) | ||
1013 | { | ||
1014 | unsigned int len; | ||
1015 | struct gs_port *port; | ||
1016 | |||
1017 | /* TEMPORARY -- only port 0 is supported right now */ | ||
1018 | port = dev->dev_port[0]; | ||
1019 | |||
1020 | if (port == NULL) { | ||
1021 | pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0); | ||
1022 | return -EIO; | ||
1023 | } | ||
1024 | |||
1025 | spin_lock(&port->port_lock); | ||
1026 | |||
1027 | len = gs_buf_data_avail(port->port_write_buf); | ||
1028 | if (len < size) | ||
1029 | size = len; | ||
1030 | |||
1031 | if (size == 0) | ||
1032 | goto exit; | ||
1033 | |||
1034 | size = gs_buf_get(port->port_write_buf, packet, size); | ||
1035 | |||
1036 | if (port->port_tty) | ||
1037 | wake_up_interruptible(&port->port_tty->write_wait); | ||
1038 | |||
1039 | exit: | ||
1040 | spin_unlock(&port->port_lock); | ||
1041 | return size; | ||
1042 | } | ||
1043 | |||
1044 | /* | ||
1045 | * gs_recv_packet | ||
1046 | * | ||
1047 | * Called for each USB packet received. Reads the packet | ||
1048 | * header and stuffs the data in the appropriate tty buffer. | ||
1049 | * Returns 0 if successful, or a negative error number. | ||
1050 | * | ||
1051 | * Called during USB completion routine, on interrupt time. | ||
1052 | * | ||
1053 | * We assume that disconnect will not happen until all completion | ||
1054 | * routines have completed, so we can assume that the dev_port | ||
1055 | * array does not change during the lifetime of this function. | ||
1056 | */ | ||
1057 | static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size) | ||
1058 | { | ||
1059 | unsigned int len; | ||
1060 | struct gs_port *port; | ||
1061 | int ret; | ||
1062 | struct tty_struct *tty; | ||
1063 | |||
1064 | /* TEMPORARY -- only port 0 is supported right now */ | ||
1065 | port = dev->dev_port[0]; | ||
1066 | |||
1067 | if (port == NULL) { | ||
1068 | pr_err("gs_recv_packet: port=%d, NULL port pointer\n", | ||
1069 | port->port_num); | ||
1070 | return -EIO; | ||
1071 | } | ||
1072 | |||
1073 | spin_lock(&port->port_lock); | ||
1074 | |||
1075 | if (port->port_open_count == 0) { | ||
1076 | pr_err("gs_recv_packet: port=%d, port is closed\n", | ||
1077 | port->port_num); | ||
1078 | ret = -EIO; | ||
1079 | goto exit; | ||
1080 | } | ||
1081 | |||
1082 | |||
1083 | tty = port->port_tty; | ||
1084 | |||
1085 | if (tty == NULL) { | ||
1086 | pr_err("gs_recv_packet: port=%d, NULL tty pointer\n", | ||
1087 | port->port_num); | ||
1088 | ret = -EIO; | ||
1089 | goto exit; | ||
1090 | } | ||
1091 | |||
1092 | if (port->port_tty->magic != TTY_MAGIC) { | ||
1093 | pr_err("gs_recv_packet: port=%d, bad tty magic\n", | ||
1094 | port->port_num); | ||
1095 | ret = -EIO; | ||
1096 | goto exit; | ||
1097 | } | ||
1098 | |||
1099 | len = tty_buffer_request_room(tty, size); | ||
1100 | if (len > 0) { | ||
1101 | tty_insert_flip_string(tty, packet, len); | ||
1102 | tty_flip_buffer_push(port->port_tty); | ||
1103 | wake_up_interruptible(&port->port_tty->read_wait); | ||
1104 | } | ||
1105 | ret = 0; | ||
1106 | exit: | ||
1107 | spin_unlock(&port->port_lock); | ||
1108 | return ret; | ||
1109 | } | ||
1110 | |||
1111 | /* | ||
1112 | * gs_read_complete | ||
1113 | */ | ||
1114 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req) | ||
1115 | { | ||
1116 | int ret; | ||
1117 | struct gs_dev *dev = ep->driver_data; | ||
1118 | |||
1119 | if (dev == NULL) { | ||
1120 | pr_err("gs_read_complete: NULL device pointer\n"); | ||
1121 | return; | ||
1122 | } | ||
1123 | |||
1124 | switch(req->status) { | ||
1125 | case 0: | ||
1126 | /* normal completion */ | ||
1127 | gs_recv_packet(dev, req->buf, req->actual); | ||
1128 | requeue: | ||
1129 | req->length = ep->maxpacket; | ||
1130 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { | ||
1131 | pr_err( | ||
1132 | "gs_read_complete: cannot queue read request, ret=%d\n", | ||
1133 | ret); | ||
1134 | } | ||
1135 | break; | ||
1136 | |||
1137 | case -ESHUTDOWN: | ||
1138 | /* disconnect */ | ||
1139 | gs_debug("gs_read_complete: shutdown\n"); | ||
1140 | gs_free_req(ep, req); | ||
1141 | break; | ||
1142 | |||
1143 | default: | ||
1144 | /* unexpected */ | ||
1145 | pr_err( | ||
1146 | "gs_read_complete: unexpected status error, status=%d\n", | ||
1147 | req->status); | ||
1148 | goto requeue; | ||
1149 | break; | ||
1150 | } | ||
1151 | } | ||
1152 | |||
1153 | /* | ||
1154 | * gs_write_complete | ||
1155 | */ | ||
1156 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) | ||
1157 | { | ||
1158 | struct gs_dev *dev = ep->driver_data; | ||
1159 | |||
1160 | if (dev == NULL) { | ||
1161 | pr_err("gs_write_complete: NULL device pointer\n"); | ||
1162 | return; | ||
1163 | } | ||
1164 | |||
1165 | switch(req->status) { | ||
1166 | case 0: | ||
1167 | /* normal completion */ | ||
1168 | requeue: | ||
1169 | spin_lock(&dev->dev_lock); | ||
1170 | list_add(&req->list, &dev->dev_req_list); | ||
1171 | spin_unlock(&dev->dev_lock); | ||
1172 | |||
1173 | gs_send(dev); | ||
1174 | |||
1175 | break; | ||
1176 | |||
1177 | case -ESHUTDOWN: | ||
1178 | /* disconnect */ | ||
1179 | gs_debug("gs_write_complete: shutdown\n"); | ||
1180 | gs_free_req(ep, req); | ||
1181 | break; | ||
1182 | |||
1183 | default: | ||
1184 | pr_err( | ||
1185 | "gs_write_complete: unexpected status error, status=%d\n", | ||
1186 | req->status); | ||
1187 | goto requeue; | ||
1188 | break; | ||
1189 | } | ||
1190 | } | ||
1191 | |||
1192 | /*-------------------------------------------------------------------------*/ | ||
1193 | |||
1194 | /* Gadget Driver */ | 346 | /* Gadget Driver */ |
1195 | 347 | ||
1196 | /* | 348 | /* |
@@ -1199,12 +351,10 @@ requeue: | |||
1199 | * Called on module unload. Frees the control request and device | 351 | * Called on module unload. Frees the control request and device |
1200 | * structure. | 352 | * structure. |
1201 | */ | 353 | */ |
1202 | static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget) | 354 | static void __exit gs_unbind(struct usb_gadget *gadget) |
1203 | { | 355 | { |
1204 | struct gs_dev *dev = get_gadget_data(gadget); | 356 | struct gs_dev *dev = get_gadget_data(gadget); |
1205 | 357 | ||
1206 | gs_device = NULL; | ||
1207 | |||
1208 | /* read/write requests already freed, only control request remains */ | 358 | /* read/write requests already freed, only control request remains */ |
1209 | if (dev != NULL) { | 359 | if (dev != NULL) { |
1210 | if (dev->dev_ctrl_req != NULL) { | 360 | if (dev->dev_ctrl_req != NULL) { |
@@ -1212,13 +362,13 @@ static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget) | |||
1212 | dev->dev_ctrl_req = NULL; | 362 | dev->dev_ctrl_req = NULL; |
1213 | } | 363 | } |
1214 | gs_reset_config(dev); | 364 | gs_reset_config(dev); |
1215 | gs_free_ports(dev); | ||
1216 | kfree(dev); | 365 | kfree(dev); |
1217 | set_gadget_data(gadget, NULL); | 366 | set_gadget_data(gadget, NULL); |
1218 | } | 367 | } |
1219 | 368 | ||
1220 | pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME, | 369 | pr_info("gs_unbind: %s unbound\n", GS_VERSION_NAME); |
1221 | GS_VERSION_STR); | 370 | |
371 | gserial_cleanup(); | ||
1222 | } | 372 | } |
1223 | 373 | ||
1224 | /* | 374 | /* |
@@ -1234,6 +384,10 @@ static int __init gs_bind(struct usb_gadget *gadget) | |||
1234 | struct gs_dev *dev; | 384 | struct gs_dev *dev; |
1235 | int gcnum; | 385 | int gcnum; |
1236 | 386 | ||
387 | ret = gserial_setup(gadget, GS_NUM_PORTS); | ||
388 | if (ret < 0) | ||
389 | return ret; | ||
390 | |||
1237 | /* Some controllers can't support CDC ACM: | 391 | /* Some controllers can't support CDC ACM: |
1238 | * - sh doesn't support multiple interfaces or configs; | 392 | * - sh doesn't support multiple interfaces or configs; |
1239 | * - sa1100 doesn't have a third interrupt endpoint | 393 | * - sa1100 doesn't have a third interrupt endpoint |
@@ -1254,21 +408,24 @@ static int __init gs_bind(struct usb_gadget *gadget) | |||
1254 | } | 408 | } |
1255 | 409 | ||
1256 | dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL); | 410 | dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL); |
1257 | if (dev == NULL) | 411 | if (dev == NULL) { |
1258 | return -ENOMEM; | 412 | ret = -ENOMEM; |
413 | goto autoconf_fail; | ||
414 | } | ||
1259 | 415 | ||
1260 | usb_ep_autoconfig_reset(gadget); | 416 | usb_ep_autoconfig_reset(gadget); |
417 | ret = -ENXIO; | ||
1261 | 418 | ||
1262 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc); | 419 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc); |
1263 | if (!ep) | 420 | if (!ep) |
1264 | goto autoconf_fail; | 421 | goto autoconf_fail; |
1265 | dev->dev_in_ep = ep; | 422 | dev->gser.in = ep; |
1266 | ep->driver_data = dev; /* claim the endpoint */ | 423 | ep->driver_data = dev; /* claim the endpoint */ |
1267 | 424 | ||
1268 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc); | 425 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc); |
1269 | if (!ep) | 426 | if (!ep) |
1270 | goto autoconf_fail; | 427 | goto autoconf_fail; |
1271 | dev->dev_out_ep = ep; | 428 | dev->gser.out = ep; |
1272 | ep->driver_data = dev; /* claim the endpoint */ | 429 | ep->driver_data = dev; /* claim the endpoint */ |
1273 | 430 | ||
1274 | if (use_acm) { | 431 | if (use_acm) { |
@@ -1279,7 +436,7 @@ static int __init gs_bind(struct usb_gadget *gadget) | |||
1279 | } | 436 | } |
1280 | gs_device_desc.idProduct = __constant_cpu_to_le16( | 437 | gs_device_desc.idProduct = __constant_cpu_to_le16( |
1281 | GS_CDC_PRODUCT_ID), | 438 | GS_CDC_PRODUCT_ID), |
1282 | dev->dev_notify_ep = ep; | 439 | dev->gser.notify = ep; |
1283 | ep->driver_data = dev; /* claim the endpoint */ | 440 | ep->driver_data = dev; /* claim the endpoint */ |
1284 | } | 441 | } |
1285 | 442 | ||
@@ -1310,41 +467,32 @@ static int __init gs_bind(struct usb_gadget *gadget) | |||
1310 | gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | 467 | gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
1311 | } | 468 | } |
1312 | 469 | ||
1313 | gs_device = dev; | ||
1314 | |||
1315 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", | 470 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", |
1316 | init_utsname()->sysname, init_utsname()->release, | 471 | init_utsname()->sysname, init_utsname()->release, |
1317 | gadget->name); | 472 | gadget->name); |
1318 | 473 | ||
1319 | dev->dev_gadget = gadget; | 474 | dev->dev_gadget = gadget; |
1320 | spin_lock_init(&dev->dev_lock); | 475 | spin_lock_init(&dev->dev_lock); |
1321 | INIT_LIST_HEAD(&dev->dev_req_list); | ||
1322 | set_gadget_data(gadget, dev); | 476 | set_gadget_data(gadget, dev); |
1323 | 477 | ||
1324 | if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) { | ||
1325 | pr_err("gs_bind: cannot allocate ports\n"); | ||
1326 | gs_unbind(gadget); | ||
1327 | return ret; | ||
1328 | } | ||
1329 | |||
1330 | /* preallocate control response and buffer */ | 478 | /* preallocate control response and buffer */ |
1331 | dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN, | 479 | dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN, |
1332 | GFP_KERNEL); | 480 | GFP_KERNEL); |
1333 | if (dev->dev_ctrl_req == NULL) { | 481 | if (dev->dev_ctrl_req == NULL) { |
1334 | gs_unbind(gadget); | 482 | ret = -ENOMEM; |
1335 | return -ENOMEM; | 483 | goto autoconf_fail; |
1336 | } | 484 | } |
1337 | gadget->ep0->driver_data = dev; | 485 | gadget->ep0->driver_data = dev; |
1338 | 486 | ||
1339 | pr_info("gs_bind: %s %s bound\n", | 487 | pr_info("gs_bind: %s bound\n", GS_VERSION_NAME); |
1340 | GS_LONG_NAME, GS_VERSION_STR); | ||
1341 | 488 | ||
1342 | return 0; | 489 | return 0; |
1343 | 490 | ||
1344 | autoconf_fail: | 491 | autoconf_fail: |
1345 | kfree(dev); | 492 | kfree(dev); |
1346 | pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name); | 493 | gserial_cleanup(); |
1347 | return -ENODEV; | 494 | pr_err("gs_bind: to %s, err %d\n", gadget->name, ret); |
495 | return ret; | ||
1348 | } | 496 | } |
1349 | 497 | ||
1350 | static int gs_setup_standard(struct usb_gadget *gadget, | 498 | static int gs_setup_standard(struct usb_gadget *gadget, |
@@ -1435,15 +583,14 @@ static int gs_setup_standard(struct usb_gadget *gadget, | |||
1435 | } | 583 | } |
1436 | if (dev->dev_config != GS_BULK_CONFIG_ID | 584 | if (dev->dev_config != GS_BULK_CONFIG_ID |
1437 | && wIndex == GS_CONTROL_INTERFACE_ID) { | 585 | && wIndex == GS_CONTROL_INTERFACE_ID) { |
1438 | if (dev->dev_notify_ep) { | 586 | if (dev->gser.notify) { |
1439 | usb_ep_disable(dev->dev_notify_ep); | 587 | usb_ep_disable(dev->gser.notify); |
1440 | usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc); | 588 | usb_ep_enable(dev->gser.notify, |
589 | dev->gser.notify_desc); | ||
1441 | } | 590 | } |
1442 | } else { | 591 | } else { |
1443 | usb_ep_disable(dev->dev_in_ep); | 592 | gserial_connect(&dev->gser, 0); |
1444 | usb_ep_disable(dev->dev_out_ep); | 593 | gserial_disconnect(&dev->gser); |
1445 | usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc); | ||
1446 | usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc); | ||
1447 | } | 594 | } |
1448 | ret = 0; | 595 | ret = 0; |
1449 | set_interface_done: | 596 | set_interface_done: |
@@ -1480,23 +627,22 @@ static void gs_setup_complete_set_line_coding(struct usb_ep *ep, | |||
1480 | struct usb_request *req) | 627 | struct usb_request *req) |
1481 | { | 628 | { |
1482 | struct gs_dev *dev = ep->driver_data; | 629 | struct gs_dev *dev = ep->driver_data; |
1483 | struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ | ||
1484 | 630 | ||
1485 | switch (req->status) { | 631 | switch (req->status) { |
1486 | case 0: | 632 | case 0: |
1487 | /* normal completion */ | 633 | /* normal completion */ |
1488 | if (req->actual != sizeof(port->port_line_coding)) | 634 | if (req->actual != sizeof(dev->gser.port_line_coding)) |
1489 | usb_ep_set_halt(ep); | 635 | usb_ep_set_halt(ep); |
1490 | else if (port) { | 636 | else { |
1491 | struct usb_cdc_line_coding *value = req->buf; | 637 | struct usb_cdc_line_coding *value = req->buf; |
1492 | 638 | ||
1493 | /* REVISIT: we currently just remember this data. | 639 | /* REVISIT: we currently just remember this data. |
1494 | * If we change that, (a) validate it first, then | 640 | * If we change that, (a) validate it first, then |
1495 | * (b) update whatever hardware needs updating. | 641 | * (b) update whatever hardware needs updating. |
1496 | */ | 642 | */ |
1497 | spin_lock(&port->port_lock); | 643 | spin_lock(&dev->dev_lock); |
1498 | port->port_line_coding = *value; | 644 | dev->gser.port_line_coding = *value; |
1499 | spin_unlock(&port->port_lock); | 645 | spin_unlock(&dev->dev_lock); |
1500 | } | 646 | } |
1501 | break; | 647 | break; |
1502 | 648 | ||
@@ -1517,7 +663,6 @@ static int gs_setup_class(struct usb_gadget *gadget, | |||
1517 | { | 663 | { |
1518 | int ret = -EOPNOTSUPP; | 664 | int ret = -EOPNOTSUPP; |
1519 | struct gs_dev *dev = get_gadget_data(gadget); | 665 | struct gs_dev *dev = get_gadget_data(gadget); |
1520 | struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ | ||
1521 | struct usb_request *req = dev->dev_ctrl_req; | 666 | struct usb_request *req = dev->dev_ctrl_req; |
1522 | u16 wIndex = le16_to_cpu(ctrl->wIndex); | 667 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
1523 | u16 wValue = le16_to_cpu(ctrl->wValue); | 668 | u16 wValue = le16_to_cpu(ctrl->wValue); |
@@ -1533,26 +678,22 @@ static int gs_setup_class(struct usb_gadget *gadget, | |||
1533 | 678 | ||
1534 | case USB_CDC_REQ_GET_LINE_CODING: | 679 | case USB_CDC_REQ_GET_LINE_CODING: |
1535 | ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding)); | 680 | ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding)); |
1536 | if (port) { | 681 | spin_lock(&dev->dev_lock); |
1537 | spin_lock(&port->port_lock); | 682 | memcpy(req->buf, &dev->gser.port_line_coding, ret); |
1538 | memcpy(req->buf, &port->port_line_coding, ret); | 683 | spin_unlock(&dev->dev_lock); |
1539 | spin_unlock(&port->port_lock); | ||
1540 | } | ||
1541 | break; | 684 | break; |
1542 | 685 | ||
1543 | case USB_CDC_REQ_SET_CONTROL_LINE_STATE: | 686 | case USB_CDC_REQ_SET_CONTROL_LINE_STATE: |
1544 | if (wLength != 0) | 687 | if (wLength != 0) |
1545 | break; | 688 | break; |
1546 | ret = 0; | 689 | ret = 0; |
1547 | if (port) { | 690 | /* REVISIT: we currently just remember this data. |
1548 | /* REVISIT: we currently just remember this data. | 691 | * If we change that, update whatever hardware needs |
1549 | * If we change that, update whatever hardware needs | 692 | * updating. |
1550 | * updating. | 693 | */ |
1551 | */ | 694 | spin_lock(&dev->dev_lock); |
1552 | spin_lock(&port->port_lock); | 695 | dev->gser.port_handshake_bits = wValue; |
1553 | port->port_handshake_bits = wValue; | 696 | spin_unlock(&dev->dev_lock); |
1554 | spin_unlock(&port->port_lock); | ||
1555 | } | ||
1556 | break; | 697 | break; |
1557 | 698 | ||
1558 | default: | 699 | default: |
@@ -1654,17 +795,7 @@ static void gs_disconnect(struct usb_gadget *gadget) | |||
1654 | struct gs_dev *dev = get_gadget_data(gadget); | 795 | struct gs_dev *dev = get_gadget_data(gadget); |
1655 | 796 | ||
1656 | spin_lock_irqsave(&dev->dev_lock, flags); | 797 | spin_lock_irqsave(&dev->dev_lock, flags); |
1657 | |||
1658 | gs_reset_config(dev); | 798 | gs_reset_config(dev); |
1659 | |||
1660 | /* free closed ports and disconnect open ports */ | ||
1661 | /* (open ports will be freed when closed) */ | ||
1662 | gs_free_ports(dev); | ||
1663 | |||
1664 | /* re-allocate ports for the next connection */ | ||
1665 | if (gs_alloc_ports(dev, GFP_ATOMIC) != 0) | ||
1666 | pr_err("gs_disconnect: cannot re-allocate ports\n"); | ||
1667 | |||
1668 | spin_unlock_irqrestore(&dev->dev_lock, flags); | 799 | spin_unlock_irqrestore(&dev->dev_lock, flags); |
1669 | 800 | ||
1670 | pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME); | 801 | pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME); |
@@ -1698,17 +829,8 @@ static struct usb_gadget_driver gs_gadget_driver = { | |||
1698 | */ | 829 | */ |
1699 | static int gs_set_config(struct gs_dev *dev, unsigned config) | 830 | static int gs_set_config(struct gs_dev *dev, unsigned config) |
1700 | { | 831 | { |
1701 | int i; | ||
1702 | int ret = 0; | 832 | int ret = 0; |
1703 | struct usb_gadget *gadget = dev->dev_gadget; | 833 | struct usb_gadget *gadget = dev->dev_gadget; |
1704 | struct usb_ep *ep; | ||
1705 | struct usb_endpoint_descriptor *out, *in, *notify; | ||
1706 | struct usb_request *req; | ||
1707 | |||
1708 | if (dev == NULL) { | ||
1709 | pr_err("gs_set_config: NULL device pointer\n"); | ||
1710 | return 0; | ||
1711 | } | ||
1712 | 834 | ||
1713 | if (config == dev->dev_config) | 835 | if (config == dev->dev_config) |
1714 | return 0; | 836 | return 0; |
@@ -1730,85 +852,37 @@ static int gs_set_config(struct gs_dev *dev, unsigned config) | |||
1730 | return -EINVAL; | 852 | return -EINVAL; |
1731 | } | 853 | } |
1732 | 854 | ||
1733 | in = choose_ep_desc(gadget, | 855 | dev->gser.in_desc = choose_ep_desc(gadget, |
1734 | &gs_highspeed_in_desc, | 856 | &gs_highspeed_in_desc, |
1735 | &gs_fullspeed_in_desc); | 857 | &gs_fullspeed_in_desc); |
1736 | out = choose_ep_desc(gadget, | 858 | dev->gser.out_desc = choose_ep_desc(gadget, |
1737 | &gs_highspeed_out_desc, | 859 | &gs_highspeed_out_desc, |
1738 | &gs_fullspeed_out_desc); | 860 | &gs_fullspeed_out_desc); |
1739 | notify = dev->dev_notify_ep | 861 | dev->gser.notify_desc = dev->gser.notify |
1740 | ? choose_ep_desc(gadget, | 862 | ? choose_ep_desc(gadget, |
1741 | &gs_highspeed_notify_desc, | 863 | &gs_highspeed_notify_desc, |
1742 | &gs_fullspeed_notify_desc) | 864 | &gs_fullspeed_notify_desc) |
1743 | : NULL; | 865 | : NULL; |
1744 | 866 | ||
1745 | ret = usb_ep_enable(dev->dev_in_ep, in); | 867 | /* only support one "serial" port for now */ |
1746 | if (ret == 0) { | 868 | if (dev->gser.notify) { |
1747 | dev->dev_in_ep_desc = in; | 869 | ret = usb_ep_enable(dev->gser.notify, dev->gser.notify_desc); |
1748 | } else { | 870 | if (ret < 0) |
1749 | pr_debug("%s: cannot enable %s %s, ret=%d\n", | 871 | return ret; |
1750 | __func__, "IN", dev->dev_in_ep->name, ret); | 872 | dev->gser.notify->driver_data = dev; |
1751 | return ret; | ||
1752 | } | ||
1753 | |||
1754 | ret = usb_ep_enable(dev->dev_out_ep, out); | ||
1755 | if (ret == 0) { | ||
1756 | dev->dev_out_ep_desc = out; | ||
1757 | } else { | ||
1758 | pr_debug("%s: cannot enable %s %s, ret=%d\n", | ||
1759 | __func__, "OUT", dev->dev_out_ep->name, ret); | ||
1760 | fail0: | ||
1761 | usb_ep_disable(dev->dev_in_ep); | ||
1762 | return ret; | ||
1763 | } | 873 | } |
1764 | 874 | ||
1765 | if (notify) { | 875 | ret = gserial_connect(&dev->gser, 0); |
1766 | ret = usb_ep_enable(dev->dev_notify_ep, notify); | 876 | if (ret < 0) { |
1767 | if (ret == 0) { | 877 | if (dev->gser.notify) { |
1768 | dev->dev_notify_ep_desc = notify; | 878 | usb_ep_disable(dev->gser.notify); |
1769 | } else { | 879 | dev->gser.notify->driver_data = NULL; |
1770 | pr_debug("%s: cannot enable %s %s, ret=%d\n", | ||
1771 | __func__, "NOTIFY", | ||
1772 | dev->dev_notify_ep->name, ret); | ||
1773 | usb_ep_disable(dev->dev_out_ep); | ||
1774 | goto fail0; | ||
1775 | } | 880 | } |
881 | return ret; | ||
1776 | } | 882 | } |
1777 | 883 | ||
1778 | dev->dev_config = config; | 884 | dev->dev_config = config; |
1779 | 885 | ||
1780 | /* allocate and queue read requests */ | ||
1781 | ep = dev->dev_out_ep; | ||
1782 | for (i=0; i<read_q_size && ret == 0; i++) { | ||
1783 | if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) { | ||
1784 | req->complete = gs_read_complete; | ||
1785 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { | ||
1786 | pr_err("gs_set_config: cannot queue read " | ||
1787 | "request, ret=%d\n", ret); | ||
1788 | } | ||
1789 | } else { | ||
1790 | pr_err("gs_set_config: cannot allocate " | ||
1791 | "read requests\n"); | ||
1792 | ret = -ENOMEM; | ||
1793 | goto exit_reset_config; | ||
1794 | } | ||
1795 | } | ||
1796 | |||
1797 | /* allocate write requests, and put on free list */ | ||
1798 | ep = dev->dev_in_ep; | ||
1799 | for (i=0; i<write_q_size; i++) { | ||
1800 | req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); | ||
1801 | if (req) { | ||
1802 | req->complete = gs_write_complete; | ||
1803 | list_add(&req->list, &dev->dev_req_list); | ||
1804 | } else { | ||
1805 | pr_err("gs_set_config: cannot allocate " | ||
1806 | "write requests\n"); | ||
1807 | ret = -ENOMEM; | ||
1808 | goto exit_reset_config; | ||
1809 | } | ||
1810 | } | ||
1811 | |||
1812 | /* REVISIT the ACM mode should be able to actually *issue* some | 886 | /* REVISIT the ACM mode should be able to actually *issue* some |
1813 | * notifications, for at least serial state change events if | 887 | * notifications, for at least serial state change events if |
1814 | * not also for network connection; say so in bmCapabilities. | 888 | * not also for network connection; say so in bmCapabilities. |
@@ -1820,10 +894,6 @@ fail0: | |||
1820 | config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM"); | 894 | config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM"); |
1821 | 895 | ||
1822 | return 0; | 896 | return 0; |
1823 | |||
1824 | exit_reset_config: | ||
1825 | gs_reset_config(dev); | ||
1826 | return ret; | ||
1827 | } | 897 | } |
1828 | 898 | ||
1829 | /* | 899 | /* |
@@ -1838,32 +908,16 @@ exit_reset_config: | |||
1838 | */ | 908 | */ |
1839 | static void gs_reset_config(struct gs_dev *dev) | 909 | static void gs_reset_config(struct gs_dev *dev) |
1840 | { | 910 | { |
1841 | struct usb_request *req; | ||
1842 | |||
1843 | if (dev == NULL) { | ||
1844 | pr_err("gs_reset_config: NULL device pointer\n"); | ||
1845 | return; | ||
1846 | } | ||
1847 | |||
1848 | if (dev->dev_config == GS_NO_CONFIG_ID) | 911 | if (dev->dev_config == GS_NO_CONFIG_ID) |
1849 | return; | 912 | return; |
1850 | 913 | ||
1851 | dev->dev_config = GS_NO_CONFIG_ID; | 914 | dev->dev_config = GS_NO_CONFIG_ID; |
1852 | 915 | ||
1853 | /* free write requests on the free list */ | 916 | gserial_disconnect(&dev->gser); |
1854 | while(!list_empty(&dev->dev_req_list)) { | 917 | if (dev->gser.notify) { |
1855 | req = list_entry(dev->dev_req_list.next, | 918 | usb_ep_disable(dev->gser.notify); |
1856 | struct usb_request, list); | 919 | dev->gser.notify->driver_data = NULL; |
1857 | list_del(&req->list); | ||
1858 | gs_free_req(dev->dev_in_ep, req); | ||
1859 | } | 920 | } |
1860 | |||
1861 | /* disable endpoints, forcing completion of pending i/o; */ | ||
1862 | /* completion handlers free their requests in this case */ | ||
1863 | if (dev->dev_notify_ep) | ||
1864 | usb_ep_disable(dev->dev_notify_ep); | ||
1865 | usb_ep_disable(dev->dev_in_ep); | ||
1866 | usb_ep_disable(dev->dev_out_ep); | ||
1867 | } | 921 | } |
1868 | 922 | ||
1869 | /* | 923 | /* |
@@ -1956,254 +1010,8 @@ static void gs_free_req(struct usb_ep *ep, struct usb_request *req) | |||
1956 | } | 1010 | } |
1957 | } | 1011 | } |
1958 | 1012 | ||
1959 | /* | ||
1960 | * gs_alloc_ports | ||
1961 | * | ||
1962 | * Allocate all ports and set the gs_dev struct to point to them. | ||
1963 | * Return 0 if successful, or a negative error number. | ||
1964 | * | ||
1965 | * The device lock is normally held when calling this function. | ||
1966 | */ | ||
1967 | static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags) | ||
1968 | { | ||
1969 | int i; | ||
1970 | struct gs_port *port; | ||
1971 | |||
1972 | if (dev == NULL) | ||
1973 | return -EIO; | ||
1974 | |||
1975 | for (i=0; i<GS_NUM_PORTS; i++) { | ||
1976 | if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL) | ||
1977 | return -ENOMEM; | ||
1978 | |||
1979 | port->port_dev = dev; | ||
1980 | port->port_num = i; | ||
1981 | port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE); | ||
1982 | port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT; | ||
1983 | port->port_line_coding.bParityType = GS_DEFAULT_PARITY; | ||
1984 | port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS; | ||
1985 | spin_lock_init(&port->port_lock); | ||
1986 | init_waitqueue_head(&port->port_write_wait); | ||
1987 | |||
1988 | dev->dev_port[i] = port; | ||
1989 | } | ||
1990 | |||
1991 | return 0; | ||
1992 | } | ||
1993 | |||
1994 | /* | ||
1995 | * gs_free_ports | ||
1996 | * | ||
1997 | * Free all closed ports. Open ports are disconnected by | ||
1998 | * freeing their write buffers, setting their device pointers | ||
1999 | * and the pointers to them in the device to NULL. These | ||
2000 | * ports will be freed when closed. | ||
2001 | * | ||
2002 | * The device lock is normally held when calling this function. | ||
2003 | */ | ||
2004 | static void gs_free_ports(struct gs_dev *dev) | ||
2005 | { | ||
2006 | int i; | ||
2007 | unsigned long flags; | ||
2008 | struct gs_port *port; | ||
2009 | |||
2010 | if (dev == NULL) | ||
2011 | return; | ||
2012 | |||
2013 | for (i=0; i<GS_NUM_PORTS; i++) { | ||
2014 | if ((port=dev->dev_port[i]) != NULL) { | ||
2015 | dev->dev_port[i] = NULL; | ||
2016 | |||
2017 | spin_lock_irqsave(&port->port_lock, flags); | ||
2018 | |||
2019 | if (port->port_write_buf != NULL) { | ||
2020 | gs_buf_free(port->port_write_buf); | ||
2021 | port->port_write_buf = NULL; | ||
2022 | } | ||
2023 | |||
2024 | if (port->port_open_count > 0 || port->port_in_use) { | ||
2025 | port->port_dev = NULL; | ||
2026 | wake_up_interruptible(&port->port_write_wait); | ||
2027 | if (port->port_tty) { | ||
2028 | tty_hangup(port->port_tty); | ||
2029 | } | ||
2030 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
2031 | } else { | ||
2032 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
2033 | kfree(port); | ||
2034 | } | ||
2035 | |||
2036 | } | ||
2037 | } | ||
2038 | } | ||
2039 | |||
2040 | /*-------------------------------------------------------------------------*/ | ||
2041 | |||
2042 | /* Circular Buffer */ | ||
2043 | |||
2044 | /* | ||
2045 | * gs_buf_alloc | ||
2046 | * | ||
2047 | * Allocate a circular buffer and all associated memory. | ||
2048 | */ | ||
2049 | static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags) | ||
2050 | { | ||
2051 | struct gs_buf *gb; | ||
2052 | |||
2053 | if (size == 0) | ||
2054 | return NULL; | ||
2055 | |||
2056 | gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags); | ||
2057 | if (gb == NULL) | ||
2058 | return NULL; | ||
2059 | |||
2060 | gb->buf_buf = kmalloc(size, kmalloc_flags); | ||
2061 | if (gb->buf_buf == NULL) { | ||
2062 | kfree(gb); | ||
2063 | return NULL; | ||
2064 | } | ||
2065 | |||
2066 | gb->buf_size = size; | ||
2067 | gb->buf_get = gb->buf_put = gb->buf_buf; | ||
2068 | |||
2069 | return gb; | ||
2070 | } | ||
2071 | |||
2072 | /* | ||
2073 | * gs_buf_free | ||
2074 | * | ||
2075 | * Free the buffer and all associated memory. | ||
2076 | */ | ||
2077 | static void gs_buf_free(struct gs_buf *gb) | ||
2078 | { | ||
2079 | if (gb) { | ||
2080 | kfree(gb->buf_buf); | ||
2081 | kfree(gb); | ||
2082 | } | ||
2083 | } | ||
2084 | |||
2085 | /* | ||
2086 | * gs_buf_clear | ||
2087 | * | ||
2088 | * Clear out all data in the circular buffer. | ||
2089 | */ | ||
2090 | static void gs_buf_clear(struct gs_buf *gb) | ||
2091 | { | ||
2092 | if (gb != NULL) | ||
2093 | gb->buf_get = gb->buf_put; | ||
2094 | /* equivalent to a get of all data available */ | ||
2095 | } | ||
2096 | |||
2097 | /* | ||
2098 | * gs_buf_data_avail | ||
2099 | * | ||
2100 | * Return the number of bytes of data available in the circular | ||
2101 | * buffer. | ||
2102 | */ | ||
2103 | static unsigned int gs_buf_data_avail(struct gs_buf *gb) | ||
2104 | { | ||
2105 | if (gb != NULL) | ||
2106 | return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size; | ||
2107 | else | ||
2108 | return 0; | ||
2109 | } | ||
2110 | |||
2111 | /* | ||
2112 | * gs_buf_space_avail | ||
2113 | * | ||
2114 | * Return the number of bytes of space available in the circular | ||
2115 | * buffer. | ||
2116 | */ | ||
2117 | static unsigned int gs_buf_space_avail(struct gs_buf *gb) | ||
2118 | { | ||
2119 | if (gb != NULL) | ||
2120 | return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size; | ||
2121 | else | ||
2122 | return 0; | ||
2123 | } | ||
2124 | |||
2125 | /* | ||
2126 | * gs_buf_put | ||
2127 | * | ||
2128 | * Copy data data from a user buffer and put it into the circular buffer. | ||
2129 | * Restrict to the amount of space available. | ||
2130 | * | ||
2131 | * Return the number of bytes copied. | ||
2132 | */ | ||
2133 | static unsigned int | ||
2134 | gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count) | ||
2135 | { | ||
2136 | unsigned int len; | ||
2137 | |||
2138 | if (gb == NULL) | ||
2139 | return 0; | ||
2140 | |||
2141 | len = gs_buf_space_avail(gb); | ||
2142 | if (count > len) | ||
2143 | count = len; | ||
2144 | |||
2145 | if (count == 0) | ||
2146 | return 0; | ||
2147 | |||
2148 | len = gb->buf_buf + gb->buf_size - gb->buf_put; | ||
2149 | if (count > len) { | ||
2150 | memcpy(gb->buf_put, buf, len); | ||
2151 | memcpy(gb->buf_buf, buf+len, count - len); | ||
2152 | gb->buf_put = gb->buf_buf + count - len; | ||
2153 | } else { | ||
2154 | memcpy(gb->buf_put, buf, count); | ||
2155 | if (count < len) | ||
2156 | gb->buf_put += count; | ||
2157 | else /* count == len */ | ||
2158 | gb->buf_put = gb->buf_buf; | ||
2159 | } | ||
2160 | |||
2161 | return count; | ||
2162 | } | ||
2163 | |||
2164 | /* | ||
2165 | * gs_buf_get | ||
2166 | * | ||
2167 | * Get data from the circular buffer and copy to the given buffer. | ||
2168 | * Restrict to the amount of data available. | ||
2169 | * | ||
2170 | * Return the number of bytes copied. | ||
2171 | */ | ||
2172 | static unsigned int | ||
2173 | gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count) | ||
2174 | { | ||
2175 | unsigned int len; | ||
2176 | |||
2177 | if (gb == NULL) | ||
2178 | return 0; | ||
2179 | |||
2180 | len = gs_buf_data_avail(gb); | ||
2181 | if (count > len) | ||
2182 | count = len; | ||
2183 | |||
2184 | if (count == 0) | ||
2185 | return 0; | ||
2186 | |||
2187 | len = gb->buf_buf + gb->buf_size - gb->buf_get; | ||
2188 | if (count > len) { | ||
2189 | memcpy(buf, gb->buf_get, len); | ||
2190 | memcpy(buf+len, gb->buf_buf, count - len); | ||
2191 | gb->buf_get = gb->buf_buf + count - len; | ||
2192 | } else { | ||
2193 | memcpy(buf, gb->buf_get, count); | ||
2194 | if (count < len) | ||
2195 | gb->buf_get += count; | ||
2196 | else /* count == len */ | ||
2197 | gb->buf_get = gb->buf_buf; | ||
2198 | } | ||
2199 | |||
2200 | return count; | ||
2201 | } | ||
2202 | |||
2203 | /*-------------------------------------------------------------------------*/ | 1013 | /*-------------------------------------------------------------------------*/ |
2204 | 1014 | ||
2205 | static struct tty_driver *gs_tty_driver; | ||
2206 | |||
2207 | /* | 1015 | /* |
2208 | * gs_module_init | 1016 | * gs_module_init |
2209 | * | 1017 | * |
@@ -2211,50 +1019,7 @@ static struct tty_driver *gs_tty_driver; | |||
2211 | */ | 1019 | */ |
2212 | static int __init gs_module_init(void) | 1020 | static int __init gs_module_init(void) |
2213 | { | 1021 | { |
2214 | int i; | 1022 | return usb_gadget_register_driver(&gs_gadget_driver); |
2215 | int retval; | ||
2216 | |||
2217 | retval = usb_gadget_register_driver(&gs_gadget_driver); | ||
2218 | if (retval) { | ||
2219 | pr_err("gs_module_init: cannot register gadget driver, " | ||
2220 | "ret=%d\n", retval); | ||
2221 | return retval; | ||
2222 | } | ||
2223 | |||
2224 | gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS); | ||
2225 | if (!gs_tty_driver) | ||
2226 | return -ENOMEM; | ||
2227 | gs_tty_driver->owner = THIS_MODULE; | ||
2228 | gs_tty_driver->driver_name = GS_SHORT_NAME; | ||
2229 | gs_tty_driver->name = "ttygs"; | ||
2230 | gs_tty_driver->major = GS_MAJOR; | ||
2231 | gs_tty_driver->minor_start = GS_MINOR_START; | ||
2232 | gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; | ||
2233 | gs_tty_driver->subtype = SERIAL_TYPE_NORMAL; | ||
2234 | gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; | ||
2235 | gs_tty_driver->init_termios = tty_std_termios; | ||
2236 | /* must match GS_DEFAULT_DTE_RATE and friends */ | ||
2237 | gs_tty_driver->init_termios.c_cflag = | ||
2238 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; | ||
2239 | gs_tty_driver->init_termios.c_ispeed = GS_DEFAULT_DTE_RATE; | ||
2240 | gs_tty_driver->init_termios.c_ospeed = GS_DEFAULT_DTE_RATE; | ||
2241 | tty_set_operations(gs_tty_driver, &gs_tty_ops); | ||
2242 | |||
2243 | for (i = 0; i < GS_NUM_PORTS; i++) | ||
2244 | mutex_init(&gs_open_close_lock[i]); | ||
2245 | |||
2246 | retval = tty_register_driver(gs_tty_driver); | ||
2247 | if (retval) { | ||
2248 | usb_gadget_unregister_driver(&gs_gadget_driver); | ||
2249 | put_tty_driver(gs_tty_driver); | ||
2250 | pr_err("gs_module_init: cannot register tty driver, " | ||
2251 | "ret=%d\n", retval); | ||
2252 | return retval; | ||
2253 | } | ||
2254 | |||
2255 | pr_info("gs_module_init: %s %s loaded\n", | ||
2256 | GS_LONG_NAME, GS_VERSION_STR); | ||
2257 | return 0; | ||
2258 | } | 1023 | } |
2259 | module_init(gs_module_init); | 1024 | module_init(gs_module_init); |
2260 | 1025 | ||
@@ -2265,11 +1030,6 @@ module_init(gs_module_init); | |||
2265 | */ | 1030 | */ |
2266 | static void __exit gs_module_exit(void) | 1031 | static void __exit gs_module_exit(void) |
2267 | { | 1032 | { |
2268 | tty_unregister_driver(gs_tty_driver); | ||
2269 | put_tty_driver(gs_tty_driver); | ||
2270 | usb_gadget_unregister_driver(&gs_gadget_driver); | 1033 | usb_gadget_unregister_driver(&gs_gadget_driver); |
2271 | |||
2272 | pr_info("gs_module_exit: %s %s unloaded\n", | ||
2273 | GS_LONG_NAME, GS_VERSION_STR); | ||
2274 | } | 1034 | } |
2275 | module_exit(gs_module_exit); | 1035 | module_exit(gs_module_exit); |