2 * g_serial.c -- USB gadget serial driver
4 * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com)
6 * This code is based in part on the Gadget Zero driver, which
7 * is Copyright (C) 2003 by David Brownell, all rights reserved.
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)
14 * This software is distributed under the terms of the GNU General
15 * Public License ("GPL") as published by the Free Software Foundation,
16 * either version 2 of that License or (at your option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/utsname.h>
21 #include <linux/device.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/cdc.h>
27 #include <linux/usb/gadget.h>
29 #include "gadget_chips.h"
34 #define GS_VERSION_STR "v2.2"
35 #define GS_VERSION_NUM 0x0202
37 #define GS_LONG_NAME "Gadget Serial"
38 #define GS_SHORT_NAME "g_serial"
41 #define GS_MINOR_START 0
43 /* REVISIT only one port is supported for now;
44 * see gs_{send,recv}_packet() ... no multiplexing,
45 * and no support for multiple ACM devices.
47 #define GS_NUM_PORTS 1
49 #define GS_NUM_CONFIGS 1
50 #define GS_NO_CONFIG_ID 0
51 #define GS_BULK_CONFIG_ID 1
52 #define GS_ACM_CONFIG_ID 2
54 #define GS_MAX_NUM_INTERFACES 2
55 #define GS_BULK_INTERFACE_ID 0
56 #define GS_CONTROL_INTERFACE_ID 0
57 #define GS_DATA_INTERFACE_ID 1
59 #define GS_MAX_DESC_LEN 256
61 #define GS_DEFAULT_READ_Q_SIZE 32
62 #define GS_DEFAULT_WRITE_Q_SIZE 32
64 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
65 #define GS_TMP_BUF_SIZE 8192
67 #define GS_CLOSE_TIMEOUT 15
69 #define GS_DEFAULT_USE_ACM 0
71 /* 9600-8-N-1 ... matches init_termios.c_cflag and defaults
72 * expected by "usbser.sys" on MS-Windows.
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
79 /* maxpacket and other transfer characteristics vary by speed. */
80 static inline struct usb_endpoint_descriptor *
81 choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
82 struct usb_endpoint_descriptor *fs)
84 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
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)
103 /* Thanks to NetChip Technologies for donating this product ID.
105 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
106 * Instead: allocate your own, using normal USB-IF procedures.
108 #define GS_VENDOR_ID 0x0525 /* NetChip */
109 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
110 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
112 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
113 #define GS_NOTIFY_MAXPACKET 8
116 /* circular buffer */
118 unsigned int buf_size;
124 /* list of requests */
125 struct gs_req_entry {
126 struct list_head re_entry;
127 struct usb_request *re_req;
130 /* the port structure holds info for each port, one for each minor number */
132 struct gs_dev *port_dev; /* pointer to device struct */
133 struct tty_struct *port_tty; /* pointer to tty struct */
134 spinlock_t port_lock;
137 int port_in_use; /* open/close in progress */
138 wait_queue_head_t port_write_wait;/* waiting to write */
139 struct gs_buf *port_write_buf;
140 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
141 u16 port_handshake_bits;
142 #define RS232_RTS (1 << 1)
143 #define RS232_DTE (1 << 0)
146 /* the device structure holds info for the USB device */
148 struct usb_gadget *dev_gadget; /* gadget device pointer */
149 spinlock_t dev_lock; /* lock for set/reset config */
150 int dev_config; /* configuration number */
151 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
152 struct usb_ep *dev_in_ep; /* address of in endpoint */
153 struct usb_ep *dev_out_ep; /* address of out endpoint */
154 struct usb_endpoint_descriptor /* descriptor of notify ep */
156 struct usb_endpoint_descriptor /* descriptor of in endpoint */
158 struct usb_endpoint_descriptor /* descriptor of out endpoint */
160 struct usb_request *dev_ctrl_req; /* control request */
161 struct list_head dev_req_list; /* list of write requests */
162 int dev_sched_port; /* round robin port scheduled */
163 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
169 /* tty driver internals */
170 static int gs_send(struct gs_dev *dev);
171 static int gs_send_packet(struct gs_dev *dev, char *packet,
173 static int gs_recv_packet(struct gs_dev *dev, char *packet,
175 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
176 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
178 /* gadget driver internals */
179 static int gs_set_config(struct gs_dev *dev, unsigned config);
180 static void gs_reset_config(struct gs_dev *dev);
181 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
182 u8 type, unsigned int index, int is_otg);
184 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
185 gfp_t kmalloc_flags);
186 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
188 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
189 gfp_t kmalloc_flags);
190 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
192 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
193 static void gs_free_ports(struct gs_dev *dev);
195 /* circular buffer */
196 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
197 static void gs_buf_free(struct gs_buf *gb);
198 static void gs_buf_clear(struct gs_buf *gb);
199 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
200 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
201 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
203 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
209 static struct gs_dev *gs_device;
211 static const char *EP_IN_NAME;
212 static const char *EP_OUT_NAME;
213 static const char *EP_NOTIFY_NAME;
215 static struct mutex gs_open_close_lock[GS_NUM_PORTS];
218 /*-------------------------------------------------------------------------*/
220 /* USB descriptors */
222 #define GS_MANUFACTURER_STR_ID 1
223 #define GS_PRODUCT_STR_ID 2
224 #define GS_SERIAL_STR_ID 3
225 #define GS_BULK_CONFIG_STR_ID 4
226 #define GS_ACM_CONFIG_STR_ID 5
227 #define GS_CONTROL_STR_ID 6
228 #define GS_DATA_STR_ID 7
230 /* static strings, in UTF-8 */
231 static char manufacturer[50];
232 static struct usb_string gs_strings[] = {
233 { GS_MANUFACTURER_STR_ID, manufacturer },
234 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
235 { GS_SERIAL_STR_ID, "0" },
236 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
237 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
238 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
239 { GS_DATA_STR_ID, "Gadget Serial Data" },
240 { } /* end of list */
243 static struct usb_gadget_strings gs_string_table = {
244 .language = 0x0409, /* en-us */
245 .strings = gs_strings,
248 static struct usb_device_descriptor gs_device_desc = {
249 .bLength = USB_DT_DEVICE_SIZE,
250 .bDescriptorType = USB_DT_DEVICE,
251 .bcdUSB = __constant_cpu_to_le16(0x0200),
252 .bDeviceSubClass = 0,
253 .bDeviceProtocol = 0,
254 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
255 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
256 .iManufacturer = GS_MANUFACTURER_STR_ID,
257 .iProduct = GS_PRODUCT_STR_ID,
258 .iSerialNumber = GS_SERIAL_STR_ID,
259 .bNumConfigurations = GS_NUM_CONFIGS,
262 static struct usb_otg_descriptor gs_otg_descriptor = {
263 .bLength = sizeof(gs_otg_descriptor),
264 .bDescriptorType = USB_DT_OTG,
265 .bmAttributes = USB_OTG_SRP,
268 static struct usb_config_descriptor gs_bulk_config_desc = {
269 .bLength = USB_DT_CONFIG_SIZE,
270 .bDescriptorType = USB_DT_CONFIG,
271 /* .wTotalLength computed dynamically */
273 .bConfigurationValue = GS_BULK_CONFIG_ID,
274 .iConfiguration = GS_BULK_CONFIG_STR_ID,
275 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
279 static struct usb_config_descriptor gs_acm_config_desc = {
280 .bLength = USB_DT_CONFIG_SIZE,
281 .bDescriptorType = USB_DT_CONFIG,
282 /* .wTotalLength computed dynamically */
284 .bConfigurationValue = GS_ACM_CONFIG_ID,
285 .iConfiguration = GS_ACM_CONFIG_STR_ID,
286 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
290 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
291 .bLength = USB_DT_INTERFACE_SIZE,
292 .bDescriptorType = USB_DT_INTERFACE,
293 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
295 .bInterfaceClass = USB_CLASS_CDC_DATA,
296 .bInterfaceSubClass = 0,
297 .bInterfaceProtocol = 0,
298 .iInterface = GS_DATA_STR_ID,
301 static const struct usb_interface_descriptor gs_control_interface_desc = {
302 .bLength = USB_DT_INTERFACE_SIZE,
303 .bDescriptorType = USB_DT_INTERFACE,
304 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
306 .bInterfaceClass = USB_CLASS_COMM,
307 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
308 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
309 .iInterface = GS_CONTROL_STR_ID,
312 static const struct usb_interface_descriptor gs_data_interface_desc = {
313 .bLength = USB_DT_INTERFACE_SIZE,
314 .bDescriptorType = USB_DT_INTERFACE,
315 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
317 .bInterfaceClass = USB_CLASS_CDC_DATA,
318 .bInterfaceSubClass = 0,
319 .bInterfaceProtocol = 0,
320 .iInterface = GS_DATA_STR_ID,
323 static const struct usb_cdc_header_desc gs_header_desc = {
324 .bLength = sizeof(gs_header_desc),
325 .bDescriptorType = USB_DT_CS_INTERFACE,
326 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
327 .bcdCDC = __constant_cpu_to_le16(0x0110),
330 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
331 .bLength = sizeof(gs_call_mgmt_descriptor),
332 .bDescriptorType = USB_DT_CS_INTERFACE,
333 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
335 .bDataInterface = 1, /* index of data interface */
338 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
339 .bLength = sizeof(gs_acm_descriptor),
340 .bDescriptorType = USB_DT_CS_INTERFACE,
341 .bDescriptorSubType = USB_CDC_ACM_TYPE,
342 .bmCapabilities = (1 << 1),
345 static const struct usb_cdc_union_desc gs_union_desc = {
346 .bLength = sizeof(gs_union_desc),
347 .bDescriptorType = USB_DT_CS_INTERFACE,
348 .bDescriptorSubType = USB_CDC_UNION_TYPE,
349 .bMasterInterface0 = 0, /* index of control interface */
350 .bSlaveInterface0 = 1, /* index of data interface */
353 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
354 .bLength = USB_DT_ENDPOINT_SIZE,
355 .bDescriptorType = USB_DT_ENDPOINT,
356 .bEndpointAddress = USB_DIR_IN,
357 .bmAttributes = USB_ENDPOINT_XFER_INT,
358 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
359 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
362 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
363 .bLength = USB_DT_ENDPOINT_SIZE,
364 .bDescriptorType = USB_DT_ENDPOINT,
365 .bEndpointAddress = USB_DIR_IN,
366 .bmAttributes = USB_ENDPOINT_XFER_BULK,
369 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
370 .bLength = USB_DT_ENDPOINT_SIZE,
371 .bDescriptorType = USB_DT_ENDPOINT,
372 .bEndpointAddress = USB_DIR_OUT,
373 .bmAttributes = USB_ENDPOINT_XFER_BULK,
376 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
377 (struct usb_descriptor_header *) &gs_otg_descriptor,
378 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
379 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
380 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
384 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
385 (struct usb_descriptor_header *) &gs_otg_descriptor,
386 (struct usb_descriptor_header *) &gs_control_interface_desc,
387 (struct usb_descriptor_header *) &gs_header_desc,
388 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
389 (struct usb_descriptor_header *) &gs_acm_descriptor,
390 (struct usb_descriptor_header *) &gs_union_desc,
391 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
392 (struct usb_descriptor_header *) &gs_data_interface_desc,
393 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
394 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
398 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
399 .bLength = USB_DT_ENDPOINT_SIZE,
400 .bDescriptorType = USB_DT_ENDPOINT,
401 .bEndpointAddress = USB_DIR_IN,
402 .bmAttributes = USB_ENDPOINT_XFER_INT,
403 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
404 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
407 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
408 .bLength = USB_DT_ENDPOINT_SIZE,
409 .bDescriptorType = USB_DT_ENDPOINT,
410 .bmAttributes = USB_ENDPOINT_XFER_BULK,
411 .wMaxPacketSize = __constant_cpu_to_le16(512),
414 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
415 .bLength = USB_DT_ENDPOINT_SIZE,
416 .bDescriptorType = USB_DT_ENDPOINT,
417 .bmAttributes = USB_ENDPOINT_XFER_BULK,
418 .wMaxPacketSize = __constant_cpu_to_le16(512),
421 static struct usb_qualifier_descriptor gs_qualifier_desc = {
422 .bLength = sizeof(struct usb_qualifier_descriptor),
423 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
424 .bcdUSB = __constant_cpu_to_le16 (0x0200),
425 /* assumes ep0 uses the same value for both speeds ... */
426 .bNumConfigurations = GS_NUM_CONFIGS,
429 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
430 (struct usb_descriptor_header *) &gs_otg_descriptor,
431 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
432 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
433 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
437 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
438 (struct usb_descriptor_header *) &gs_otg_descriptor,
439 (struct usb_descriptor_header *) &gs_control_interface_desc,
440 (struct usb_descriptor_header *) &gs_header_desc,
441 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
442 (struct usb_descriptor_header *) &gs_acm_descriptor,
443 (struct usb_descriptor_header *) &gs_union_desc,
444 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
445 (struct usb_descriptor_header *) &gs_data_interface_desc,
446 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
447 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
452 /*-------------------------------------------------------------------------*/
455 MODULE_DESCRIPTION(GS_LONG_NAME);
456 MODULE_AUTHOR("Al Borchers");
457 MODULE_LICENSE("GPL");
460 module_param(debug, int, S_IRUGO|S_IWUSR);
461 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
464 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
465 module_param(read_q_size, uint, S_IRUGO);
466 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
468 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
469 module_param(write_q_size, uint, S_IRUGO);
470 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
472 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
473 module_param(write_buf_size, uint, S_IRUGO);
474 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
476 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
477 module_param(use_acm, uint, S_IRUGO);
478 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
480 /*-------------------------------------------------------------------------*/
487 static int gs_open(struct tty_struct *tty, struct file *file)
491 struct gs_port *port;
497 port_num = tty->index;
499 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
501 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
502 pr_err("gs_open: (%d,%p,%p) invalid port number\n",
503 port_num, tty, file);
510 pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
511 port_num, tty, file);
515 mtx = &gs_open_close_lock[port_num];
516 if (mutex_lock_interruptible(mtx)) {
517 pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n",
518 port_num, tty, file);
522 spin_lock_irqsave(&dev->dev_lock, flags);
524 if (dev->dev_config == GS_NO_CONFIG_ID) {
525 pr_err("gs_open: (%d,%p,%p) device is not connected\n",
526 port_num, tty, file);
528 goto exit_unlock_dev;
531 port = dev->dev_port[port_num];
534 pr_err("gs_open: (%d,%p,%p) NULL port pointer\n",
535 port_num, tty, file);
537 goto exit_unlock_dev;
540 spin_lock(&port->port_lock);
541 spin_unlock(&dev->dev_lock);
543 if (port->port_dev == NULL) {
544 pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n",
545 port_num, tty, file);
547 goto exit_unlock_port;
550 if (port->port_open_count > 0) {
551 ++port->port_open_count;
552 gs_debug("gs_open: (%d,%p,%p) already open\n",
553 port_num, tty, file);
555 goto exit_unlock_port;
558 tty->driver_data = NULL;
560 /* mark port as in use, we can drop port lock and sleep if necessary */
561 port->port_in_use = 1;
563 /* allocate write buffer on first open */
564 if (port->port_write_buf == NULL) {
565 spin_unlock_irqrestore(&port->port_lock, flags);
566 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
567 spin_lock_irqsave(&port->port_lock, flags);
569 /* might have been disconnected while asleep, check */
570 if (port->port_dev == NULL) {
571 pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n",
572 port_num, tty, file);
573 port->port_in_use = 0;
575 goto exit_unlock_port;
578 if ((port->port_write_buf=buf) == NULL) {
579 pr_err("gs_open: (%d,%p,%p) cannot allocate "
580 "port write buffer\n",
581 port_num, tty, file);
582 port->port_in_use = 0;
584 goto exit_unlock_port;
589 /* wait for carrier detect (not implemented) */
591 /* might have been disconnected while asleep, check */
592 if (port->port_dev == NULL) {
593 pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n",
594 port_num, tty, file);
595 port->port_in_use = 0;
597 goto exit_unlock_port;
600 tty->driver_data = port;
601 port->port_tty = tty;
602 port->port_open_count = 1;
603 port->port_in_use = 0;
605 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
610 spin_unlock_irqrestore(&port->port_lock, flags);
615 spin_unlock_irqrestore(&dev->dev_lock, flags);
625 static int gs_write_finished_event_safely(struct gs_port *p)
629 spin_lock_irq(&(p)->port_lock);
630 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf);
631 spin_unlock_irq(&(p)->port_lock);
635 static void gs_close(struct tty_struct *tty, struct file *file)
637 struct gs_port *port = tty->driver_data;
641 pr_err("gs_close: NULL port pointer\n");
645 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
647 mtx = &gs_open_close_lock[port->port_num];
650 spin_lock_irq(&port->port_lock);
652 if (port->port_open_count == 0) {
653 pr_err("gs_close: (%d,%p,%p) port is already closed\n",
654 port->port_num, tty, file);
658 if (port->port_open_count > 1) {
659 --port->port_open_count;
663 /* free disconnected port on final close */
664 if (port->port_dev == NULL) {
669 /* mark port as closed but in use, we can drop port lock */
670 /* and sleep if necessary */
671 port->port_in_use = 1;
672 port->port_open_count = 0;
674 /* wait for write buffer to drain, or */
675 /* at most GS_CLOSE_TIMEOUT seconds */
676 if (gs_buf_data_avail(port->port_write_buf) > 0) {
677 spin_unlock_irq(&port->port_lock);
678 wait_event_interruptible_timeout(port->port_write_wait,
679 gs_write_finished_event_safely(port),
680 GS_CLOSE_TIMEOUT * HZ);
681 spin_lock_irq(&port->port_lock);
684 /* free disconnected port on final close */
685 /* (might have happened during the above sleep) */
686 if (port->port_dev == NULL) {
691 gs_buf_clear(port->port_write_buf);
693 tty->driver_data = NULL;
694 port->port_tty = NULL;
695 port->port_in_use = 0;
697 gs_debug("gs_close: (%d,%p,%p) completed\n",
698 port->port_num, tty, file);
701 spin_unlock_irq(&port->port_lock);
708 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
711 struct gs_port *port = tty->driver_data;
715 pr_err("gs_write: NULL port pointer\n");
719 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
725 spin_lock_irqsave(&port->port_lock, flags);
727 if (port->port_dev == NULL) {
728 pr_err("gs_write: (%d,%p) port is not connected\n",
729 port->port_num, tty);
734 if (port->port_open_count == 0) {
735 pr_err("gs_write: (%d,%p) port is closed\n",
736 port->port_num, tty);
741 count = gs_buf_put(port->port_write_buf, buf, count);
743 spin_unlock_irqrestore(&port->port_lock, flags);
747 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
753 spin_unlock_irqrestore(&port->port_lock, flags);
760 static int gs_put_char(struct tty_struct *tty, unsigned char ch)
763 struct gs_port *port = tty->driver_data;
767 pr_err("gs_put_char: NULL port pointer\n");
771 gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
772 port->port_num, tty, ch, __builtin_return_address(0));
774 spin_lock_irqsave(&port->port_lock, flags);
776 if (port->port_dev == NULL) {
777 pr_err("gs_put_char: (%d,%p) port is not connected\n",
778 port->port_num, tty);
782 if (port->port_open_count == 0) {
783 pr_err("gs_put_char: (%d,%p) port is closed\n",
784 port->port_num, tty);
788 ret = gs_buf_put(port->port_write_buf, &ch, 1);
791 spin_unlock_irqrestore(&port->port_lock, flags);
798 static void gs_flush_chars(struct tty_struct *tty)
801 struct gs_port *port = tty->driver_data;
804 pr_err("gs_flush_chars: NULL port pointer\n");
808 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
810 spin_lock_irqsave(&port->port_lock, flags);
812 if (port->port_dev == NULL) {
813 pr_err("gs_flush_chars: (%d,%p) port is not connected\n",
814 port->port_num, tty);
818 if (port->port_open_count == 0) {
819 pr_err("gs_flush_chars: (%d,%p) port is closed\n",
820 port->port_num, tty);
824 spin_unlock_irqrestore(&port->port_lock, flags);
831 spin_unlock_irqrestore(&port->port_lock, flags);
837 static int gs_write_room(struct tty_struct *tty)
842 struct gs_port *port = tty->driver_data;
848 spin_lock_irqsave(&port->port_lock, flags);
850 if (port->port_dev != NULL && port->port_open_count > 0
851 && port->port_write_buf != NULL)
852 room = gs_buf_space_avail(port->port_write_buf);
854 spin_unlock_irqrestore(&port->port_lock, flags);
856 gs_debug("gs_write_room: (%d,%p) room=%d\n",
857 port->port_num, tty, room);
865 static int gs_chars_in_buffer(struct tty_struct *tty)
869 struct gs_port *port = tty->driver_data;
874 spin_lock_irqsave(&port->port_lock, flags);
876 if (port->port_dev != NULL && port->port_open_count > 0
877 && port->port_write_buf != NULL)
878 chars = gs_buf_data_avail(port->port_write_buf);
880 spin_unlock_irqrestore(&port->port_lock, flags);
882 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
883 port->port_num, tty, chars);
891 static void gs_throttle(struct tty_struct *tty)
898 static void gs_unthrottle(struct tty_struct *tty)
905 static void gs_break(struct tty_struct *tty, int break_state)
912 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
914 struct gs_port *port = tty->driver_data;
917 pr_err("gs_ioctl: NULL port pointer\n");
921 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
922 port->port_num, tty, file, cmd, arg);
926 /* could not handle ioctl */
933 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
937 static const struct tty_operations gs_tty_ops = {
941 .put_char = gs_put_char,
942 .flush_chars = gs_flush_chars,
943 .write_room = gs_write_room,
945 .set_termios = gs_set_termios,
946 .throttle = gs_throttle,
947 .unthrottle = gs_unthrottle,
948 .break_ctl = gs_break,
949 .chars_in_buffer = gs_chars_in_buffer,
952 /*-------------------------------------------------------------------------*/
957 * This function finds available write requests, calls
958 * gs_send_packet to fill these packets with data, and
959 * continues until either there are no more write requests
960 * available or no more data to send. This function is
961 * run whenever data arrives or write requests are available.
963 static int gs_send(struct gs_dev *dev)
968 struct usb_request *req;
969 struct gs_req_entry *req_entry;
972 pr_err("gs_send: NULL device pointer\n");
976 spin_lock_irqsave(&dev->dev_lock, flags);
980 while(!list_empty(&dev->dev_req_list)) {
982 req_entry = list_entry(dev->dev_req_list.next,
983 struct gs_req_entry, re_entry);
985 req = req_entry->re_req;
987 len = gs_send_packet(dev, req->buf, ep->maxpacket);
990 gs_debug_level(3, "gs_send: len=%d, 0x%2.2x "
991 "0x%2.2x 0x%2.2x ...\n", len,
992 *((unsigned char *)req->buf),
993 *((unsigned char *)req->buf+1),
994 *((unsigned char *)req->buf+2));
995 list_del(&req_entry->re_entry);
997 spin_unlock_irqrestore(&dev->dev_lock, flags);
998 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1000 "gs_send: cannot queue read request, ret=%d\n",
1002 spin_lock_irqsave(&dev->dev_lock, flags);
1005 spin_lock_irqsave(&dev->dev_lock, flags);
1012 spin_unlock_irqrestore(&dev->dev_lock, flags);
1020 * If there is data to send, a packet is built in the given
1021 * buffer and the size is returned. If there is no data to
1022 * send, 0 is returned. If there is any error a negative
1023 * error number is returned.
1025 * Called during USB completion routine, on interrupt time.
1027 * We assume that disconnect will not happen until all completion
1028 * routines have completed, so we can assume that the dev_port
1029 * array does not change during the lifetime of this function.
1031 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1034 struct gs_port *port;
1036 /* TEMPORARY -- only port 0 is supported right now */
1037 port = dev->dev_port[0];
1040 pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
1044 spin_lock(&port->port_lock);
1046 len = gs_buf_data_avail(port->port_write_buf);
1053 size = gs_buf_get(port->port_write_buf, packet, size);
1056 wake_up_interruptible(&port->port_tty->write_wait);
1059 spin_unlock(&port->port_lock);
1066 * Called for each USB packet received. Reads the packet
1067 * header and stuffs the data in the appropriate tty buffer.
1068 * Returns 0 if successful, or a negative error number.
1070 * Called during USB completion routine, on interrupt time.
1072 * We assume that disconnect will not happen until all completion
1073 * routines have completed, so we can assume that the dev_port
1074 * array does not change during the lifetime of this function.
1076 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1079 struct gs_port *port;
1081 struct tty_struct *tty;
1083 /* TEMPORARY -- only port 0 is supported right now */
1084 port = dev->dev_port[0];
1087 pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
1092 spin_lock(&port->port_lock);
1094 if (port->port_open_count == 0) {
1095 pr_err("gs_recv_packet: port=%d, port is closed\n",
1102 tty = port->port_tty;
1105 pr_err("gs_recv_packet: port=%d, NULL tty pointer\n",
1111 if (port->port_tty->magic != TTY_MAGIC) {
1112 pr_err("gs_recv_packet: port=%d, bad tty magic\n",
1118 len = tty_buffer_request_room(tty, size);
1120 tty_insert_flip_string(tty, packet, len);
1121 tty_flip_buffer_push(port->port_tty);
1122 wake_up_interruptible(&port->port_tty->read_wait);
1126 spin_unlock(&port->port_lock);
1133 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1136 struct gs_dev *dev = ep->driver_data;
1139 pr_err("gs_read_complete: NULL device pointer\n");
1143 switch(req->status) {
1145 /* normal completion */
1146 gs_recv_packet(dev, req->buf, req->actual);
1148 req->length = ep->maxpacket;
1149 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1151 "gs_read_complete: cannot queue read request, ret=%d\n",
1158 gs_debug("gs_read_complete: shutdown\n");
1159 gs_free_req(ep, req);
1165 "gs_read_complete: unexpected status error, status=%d\n",
1175 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1177 struct gs_dev *dev = ep->driver_data;
1178 struct gs_req_entry *gs_req = req->context;
1181 pr_err("gs_write_complete: NULL device pointer\n");
1185 switch(req->status) {
1187 /* normal completion */
1189 if (gs_req == NULL) {
1190 pr_err("gs_write_complete: NULL request pointer\n");
1194 spin_lock(&dev->dev_lock);
1195 list_add(&gs_req->re_entry, &dev->dev_req_list);
1196 spin_unlock(&dev->dev_lock);
1204 gs_debug("gs_write_complete: shutdown\n");
1205 gs_free_req(ep, req);
1210 "gs_write_complete: unexpected status error, status=%d\n",
1217 /*-------------------------------------------------------------------------*/
1224 * Called on module unload. Frees the control request and device
1227 static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1229 struct gs_dev *dev = get_gadget_data(gadget);
1233 /* read/write requests already freed, only control request remains */
1235 if (dev->dev_ctrl_req != NULL) {
1236 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1237 dev->dev_ctrl_req = NULL;
1240 if (dev->dev_notify_ep)
1241 usb_ep_disable(dev->dev_notify_ep);
1243 usb_ep_disable(dev->dev_in_ep);
1244 if (dev->dev_out_ep)
1245 usb_ep_disable(dev->dev_out_ep);
1247 set_gadget_data(gadget, NULL);
1250 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1257 * Called on module load. Allocates and initializes the device
1258 * structure and a control request.
1260 static int __init gs_bind(struct usb_gadget *gadget)
1267 /* Some controllers can't support CDC ACM:
1268 * - sh doesn't support multiple interfaces or configs;
1269 * - sa1100 doesn't have a third interrupt endpoint
1271 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1274 gcnum = usb_gadget_controller_number(gadget);
1276 gs_device_desc.bcdDevice =
1277 cpu_to_le16(GS_VERSION_NUM | gcnum);
1279 pr_warning("gs_bind: controller '%s' not recognized\n",
1281 /* unrecognized, but safe unless bulk is REALLY quirky */
1282 gs_device_desc.bcdDevice =
1283 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1286 usb_ep_autoconfig_reset(gadget);
1288 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1291 EP_IN_NAME = ep->name;
1292 ep->driver_data = ep; /* claim the endpoint */
1294 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1297 EP_OUT_NAME = ep->name;
1298 ep->driver_data = ep; /* claim the endpoint */
1301 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1303 pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
1306 gs_device_desc.idProduct = __constant_cpu_to_le16(
1308 EP_NOTIFY_NAME = ep->name;
1309 ep->driver_data = ep; /* claim the endpoint */
1312 gs_device_desc.bDeviceClass = use_acm
1313 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1314 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1316 if (gadget_is_dualspeed(gadget)) {
1317 gs_qualifier_desc.bDeviceClass = use_acm
1318 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1319 /* assume ep0 uses the same packet size for both speeds */
1320 gs_qualifier_desc.bMaxPacketSize0 =
1321 gs_device_desc.bMaxPacketSize0;
1322 /* assume endpoints are dual-speed */
1323 gs_highspeed_notify_desc.bEndpointAddress =
1324 gs_fullspeed_notify_desc.bEndpointAddress;
1325 gs_highspeed_in_desc.bEndpointAddress =
1326 gs_fullspeed_in_desc.bEndpointAddress;
1327 gs_highspeed_out_desc.bEndpointAddress =
1328 gs_fullspeed_out_desc.bEndpointAddress;
1331 usb_gadget_set_selfpowered(gadget);
1333 if (gadget_is_otg(gadget)) {
1334 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1335 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1336 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1339 gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1343 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1344 init_utsname()->sysname, init_utsname()->release,
1347 dev->dev_gadget = gadget;
1348 spin_lock_init(&dev->dev_lock);
1349 INIT_LIST_HEAD(&dev->dev_req_list);
1350 set_gadget_data(gadget, dev);
1352 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1353 pr_err("gs_bind: cannot allocate ports\n");
1358 /* preallocate control response and buffer */
1359 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1361 if (dev->dev_ctrl_req == NULL) {
1365 gadget->ep0->driver_data = dev;
1367 pr_info("gs_bind: %s %s bound\n",
1368 GS_LONG_NAME, GS_VERSION_STR);
1373 pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
1377 static int gs_setup_standard(struct usb_gadget *gadget,
1378 const struct usb_ctrlrequest *ctrl)
1380 int ret = -EOPNOTSUPP;
1381 struct gs_dev *dev = get_gadget_data(gadget);
1382 struct usb_request *req = dev->dev_ctrl_req;
1383 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1384 u16 wValue = le16_to_cpu(ctrl->wValue);
1385 u16 wLength = le16_to_cpu(ctrl->wLength);
1387 switch (ctrl->bRequest) {
1388 case USB_REQ_GET_DESCRIPTOR:
1389 if (ctrl->bRequestType != USB_DIR_IN)
1392 switch (wValue >> 8) {
1395 (u16)sizeof(struct usb_device_descriptor));
1396 memcpy(req->buf, &gs_device_desc, ret);
1399 case USB_DT_DEVICE_QUALIFIER:
1400 if (!gadget_is_dualspeed(gadget))
1403 (u16)sizeof(struct usb_qualifier_descriptor));
1404 memcpy(req->buf, &gs_qualifier_desc, ret);
1407 case USB_DT_OTHER_SPEED_CONFIG:
1408 if (!gadget_is_dualspeed(gadget))
1412 ret = gs_build_config_buf(req->buf, gadget,
1413 wValue >> 8, wValue & 0xff,
1414 gadget_is_otg(gadget));
1416 ret = min(wLength, (u16)ret);
1420 /* wIndex == language code. */
1421 ret = usb_gadget_get_string(&gs_string_table,
1422 wValue & 0xff, req->buf);
1424 ret = min(wLength, (u16)ret);
1429 case USB_REQ_SET_CONFIGURATION:
1430 if (ctrl->bRequestType != 0)
1432 spin_lock(&dev->dev_lock);
1433 ret = gs_set_config(dev, wValue);
1434 spin_unlock(&dev->dev_lock);
1437 case USB_REQ_GET_CONFIGURATION:
1438 if (ctrl->bRequestType != USB_DIR_IN)
1440 *(u8 *)req->buf = dev->dev_config;
1441 ret = min(wLength, (u16)1);
1444 case USB_REQ_SET_INTERFACE:
1445 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1447 || wIndex >= GS_MAX_NUM_INTERFACES)
1449 if (dev->dev_config == GS_BULK_CONFIG_ID
1450 && wIndex != GS_BULK_INTERFACE_ID)
1452 /* no alternate interface settings */
1455 spin_lock(&dev->dev_lock);
1456 /* PXA hardware partially handles SET_INTERFACE;
1457 * we need to kluge around that interference. */
1458 if (gadget_is_pxa(gadget)) {
1459 ret = gs_set_config(dev, use_acm ?
1460 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1461 goto set_interface_done;
1463 if (dev->dev_config != GS_BULK_CONFIG_ID
1464 && wIndex == GS_CONTROL_INTERFACE_ID) {
1465 if (dev->dev_notify_ep) {
1466 usb_ep_disable(dev->dev_notify_ep);
1467 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1470 usb_ep_disable(dev->dev_in_ep);
1471 usb_ep_disable(dev->dev_out_ep);
1472 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1473 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1477 spin_unlock(&dev->dev_lock);
1480 case USB_REQ_GET_INTERFACE:
1481 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1482 || dev->dev_config == GS_NO_CONFIG_ID)
1484 if (wIndex >= GS_MAX_NUM_INTERFACES
1485 || (dev->dev_config == GS_BULK_CONFIG_ID
1486 && wIndex != GS_BULK_INTERFACE_ID)) {
1490 /* no alternate interface settings */
1491 *(u8 *)req->buf = 0;
1492 ret = min(wLength, (u16)1);
1496 pr_err("gs_setup: unknown standard request, type=%02x, "
1497 "request=%02x, value=%04x, index=%04x, length=%d\n",
1498 ctrl->bRequestType, ctrl->bRequest,
1499 wValue, wIndex, wLength);
1506 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
1507 struct usb_request *req)
1509 struct gs_dev *dev = ep->driver_data;
1510 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1512 switch (req->status) {
1514 /* normal completion */
1515 if (req->actual != sizeof(port->port_line_coding))
1516 usb_ep_set_halt(ep);
1518 struct usb_cdc_line_coding *value = req->buf;
1520 /* REVISIT: we currently just remember this data.
1521 * If we change that, (a) validate it first, then
1522 * (b) update whatever hardware needs updating.
1524 spin_lock(&port->port_lock);
1525 port->port_line_coding = *value;
1526 spin_unlock(&port->port_lock);
1532 gs_free_req(ep, req);
1542 static int gs_setup_class(struct usb_gadget *gadget,
1543 const struct usb_ctrlrequest *ctrl)
1545 int ret = -EOPNOTSUPP;
1546 struct gs_dev *dev = get_gadget_data(gadget);
1547 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1548 struct usb_request *req = dev->dev_ctrl_req;
1549 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1550 u16 wValue = le16_to_cpu(ctrl->wValue);
1551 u16 wLength = le16_to_cpu(ctrl->wLength);
1553 switch (ctrl->bRequest) {
1554 case USB_CDC_REQ_SET_LINE_CODING:
1555 if (wLength != sizeof(struct usb_cdc_line_coding))
1558 req->complete = gs_setup_complete_set_line_coding;
1561 case USB_CDC_REQ_GET_LINE_CODING:
1562 ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
1564 spin_lock(&port->port_lock);
1565 memcpy(req->buf, &port->port_line_coding, ret);
1566 spin_unlock(&port->port_lock);
1570 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1575 /* REVISIT: we currently just remember this data.
1576 * If we change that, update whatever hardware needs
1579 spin_lock(&port->port_lock);
1580 port->port_handshake_bits = wValue;
1581 spin_unlock(&port->port_lock);
1586 /* NOTE: strictly speaking, we should accept AT-commands
1587 * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE.
1588 * But our call management descriptor says we don't handle
1589 * call management, so we should be able to get by without
1590 * handling those "required" commands (except by stalling).
1592 pr_err("gs_setup: unknown class request, "
1593 "type=%02x, request=%02x, value=%04x, "
1594 "index=%04x, length=%d\n",
1595 ctrl->bRequestType, ctrl->bRequest,
1596 wValue, wIndex, wLength);
1606 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1608 if (req->status || req->actual != req->length) {
1609 pr_err("gs_setup_complete: status error, status=%d, "
1610 "actual=%d, length=%d\n",
1611 req->status, req->actual, req->length);
1618 * Implements all the control endpoint functionality that's not
1619 * handled in hardware or the hardware driver.
1621 * Returns the size of the data sent to the host, or a negative
1624 static int gs_setup(struct usb_gadget *gadget,
1625 const struct usb_ctrlrequest *ctrl)
1627 int ret = -EOPNOTSUPP;
1628 struct gs_dev *dev = get_gadget_data(gadget);
1629 struct usb_request *req = dev->dev_ctrl_req;
1630 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1631 u16 wValue = le16_to_cpu(ctrl->wValue);
1632 u16 wLength = le16_to_cpu(ctrl->wLength);
1634 req->complete = gs_setup_complete;
1636 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1637 case USB_TYPE_STANDARD:
1638 ret = gs_setup_standard(gadget, ctrl);
1641 case USB_TYPE_CLASS:
1642 ret = gs_setup_class(gadget, ctrl);
1646 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1647 "value=%04x, index=%04x, length=%d\n",
1648 ctrl->bRequestType, ctrl->bRequest,
1649 wValue, wIndex, wLength);
1653 /* respond with data transfer before status phase? */
1656 req->zero = ret < wLength
1657 && (ret % gadget->ep0->maxpacket) == 0;
1658 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1660 pr_err("gs_setup: cannot queue response, ret=%d\n",
1663 gs_setup_complete(gadget->ep0, req);
1667 /* device either stalls (ret < 0) or reports success */
1674 * Called when the device is disconnected. Frees the closed
1675 * ports and disconnects open ports. Open ports will be freed
1676 * on close. Then reallocates the ports for the next connection.
1678 static void gs_disconnect(struct usb_gadget *gadget)
1680 unsigned long flags;
1681 struct gs_dev *dev = get_gadget_data(gadget);
1683 spin_lock_irqsave(&dev->dev_lock, flags);
1685 gs_reset_config(dev);
1687 /* free closed ports and disconnect open ports */
1688 /* (open ports will be freed when closed) */
1691 /* re-allocate ports for the next connection */
1692 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1693 pr_err("gs_disconnect: cannot re-allocate ports\n");
1695 spin_unlock_irqrestore(&dev->dev_lock, flags);
1697 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1700 static struct usb_gadget_driver gs_gadget_driver = {
1701 #ifdef CONFIG_USB_GADGET_DUALSPEED
1702 .speed = USB_SPEED_HIGH,
1704 .speed = USB_SPEED_FULL,
1705 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1706 .function = GS_LONG_NAME,
1708 .unbind = gs_unbind,
1710 .disconnect = gs_disconnect,
1712 .name = GS_SHORT_NAME,
1713 .owner = THIS_MODULE,
1720 * Configures the device by enabling device specific
1721 * optimizations, setting up the endpoints, allocating
1722 * read and write requests and queuing read requests.
1724 * The device lock must be held when calling this function.
1726 static int gs_set_config(struct gs_dev *dev, unsigned config)
1730 struct usb_gadget *gadget = dev->dev_gadget;
1732 struct usb_endpoint_descriptor *ep_desc;
1733 struct usb_request *req;
1734 struct gs_req_entry *req_entry;
1737 pr_err("gs_set_config: NULL device pointer\n");
1741 if (config == dev->dev_config)
1744 gs_reset_config(dev);
1747 case GS_NO_CONFIG_ID:
1749 case GS_BULK_CONFIG_ID:
1753 case GS_ACM_CONFIG_ID:
1761 dev->dev_config = config;
1763 gadget_for_each_ep(ep, gadget) {
1766 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1767 ep_desc = choose_ep_desc(gadget,
1768 &gs_highspeed_notify_desc,
1769 &gs_fullspeed_notify_desc);
1770 ret = usb_ep_enable(ep,ep_desc);
1772 ep->driver_data = dev;
1773 dev->dev_notify_ep = ep;
1774 dev->dev_notify_ep_desc = ep_desc;
1776 pr_err("gs_set_config: cannot enable NOTIFY "
1777 "endpoint %s, ret=%d\n",
1779 goto exit_reset_config;
1783 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1784 ep_desc = choose_ep_desc(gadget,
1785 &gs_highspeed_in_desc,
1786 &gs_fullspeed_in_desc);
1787 ret = usb_ep_enable(ep,ep_desc);
1789 ep->driver_data = dev;
1790 dev->dev_in_ep = ep;
1791 dev->dev_in_ep_desc = ep_desc;
1793 pr_err("gs_set_config: cannot enable IN "
1794 "endpoint %s, ret=%d\n",
1796 goto exit_reset_config;
1800 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1801 ep_desc = choose_ep_desc(gadget,
1802 &gs_highspeed_out_desc,
1803 &gs_fullspeed_out_desc);
1804 ret = usb_ep_enable(ep,ep_desc);
1806 ep->driver_data = dev;
1807 dev->dev_out_ep = ep;
1808 dev->dev_out_ep_desc = ep_desc;
1810 pr_err("gs_set_config: cannot enable OUT "
1811 "endpoint %s, ret=%d\n",
1813 goto exit_reset_config;
1819 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
1820 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1821 pr_err("gs_set_config: cannot find endpoints\n");
1823 goto exit_reset_config;
1826 /* allocate and queue read requests */
1827 ep = dev->dev_out_ep;
1828 for (i=0; i<read_q_size && ret == 0; i++) {
1829 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1830 req->complete = gs_read_complete;
1831 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1832 pr_err("gs_set_config: cannot queue read "
1833 "request, ret=%d\n", ret);
1836 pr_err("gs_set_config: cannot allocate "
1839 goto exit_reset_config;
1843 /* allocate write requests, and put on free list */
1844 ep = dev->dev_in_ep;
1845 for (i=0; i<write_q_size; i++) {
1846 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1847 req_entry->re_req->complete = gs_write_complete;
1848 list_add(&req_entry->re_entry, &dev->dev_req_list);
1850 pr_err("gs_set_config: cannot allocate "
1851 "write requests\n");
1853 goto exit_reset_config;
1857 /* REVISIT the ACM mode should be able to actually *issue* some
1858 * notifications, for at least serial state change events if
1859 * not also for network connection; say so in bmCapabilities.
1862 pr_info("gs_set_config: %s configured, %s speed %s config\n",
1864 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1865 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1870 gs_reset_config(dev);
1877 * Mark the device as not configured, disable all endpoints,
1878 * which forces completion of pending I/O and frees queued
1879 * requests, and free the remaining write requests on the
1882 * The device lock must be held when calling this function.
1884 static void gs_reset_config(struct gs_dev *dev)
1886 struct gs_req_entry *req_entry;
1889 pr_err("gs_reset_config: NULL device pointer\n");
1893 if (dev->dev_config == GS_NO_CONFIG_ID)
1896 dev->dev_config = GS_NO_CONFIG_ID;
1898 /* free write requests on the free list */
1899 while(!list_empty(&dev->dev_req_list)) {
1900 req_entry = list_entry(dev->dev_req_list.next,
1901 struct gs_req_entry, re_entry);
1902 list_del(&req_entry->re_entry);
1903 gs_free_req_entry(dev->dev_in_ep, req_entry);
1906 /* disable endpoints, forcing completion of pending i/o; */
1907 /* completion handlers free their requests in this case */
1908 if (dev->dev_notify_ep) {
1909 usb_ep_disable(dev->dev_notify_ep);
1910 dev->dev_notify_ep = NULL;
1912 if (dev->dev_in_ep) {
1913 usb_ep_disable(dev->dev_in_ep);
1914 dev->dev_in_ep = NULL;
1916 if (dev->dev_out_ep) {
1917 usb_ep_disable(dev->dev_out_ep);
1918 dev->dev_out_ep = NULL;
1923 * gs_build_config_buf
1925 * Builds the config descriptors in the given buffer and returns the
1926 * length, or a negative error number.
1928 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
1929 u8 type, unsigned int index, int is_otg)
1933 const struct usb_config_descriptor *config_desc;
1934 const struct usb_descriptor_header **function;
1936 if (index >= gs_device_desc.bNumConfigurations)
1939 /* other speed switches high and full speed */
1940 if (gadget_is_dualspeed(g)) {
1941 high_speed = (g->speed == USB_SPEED_HIGH);
1942 if (type == USB_DT_OTHER_SPEED_CONFIG)
1943 high_speed = !high_speed;
1947 config_desc = &gs_acm_config_desc;
1948 function = high_speed
1949 ? gs_acm_highspeed_function
1950 : gs_acm_fullspeed_function;
1952 config_desc = &gs_bulk_config_desc;
1953 function = high_speed
1954 ? gs_bulk_highspeed_function
1955 : gs_bulk_fullspeed_function;
1958 /* for now, don't advertise srp-only devices */
1962 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
1966 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
1974 * Allocate a usb_request and its buffer. Returns a pointer to the
1975 * usb_request or NULL if there is an error.
1977 static struct usb_request *
1978 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
1980 struct usb_request *req;
1985 req = usb_ep_alloc_request(ep, kmalloc_flags);
1989 req->buf = kmalloc(len, kmalloc_flags);
1990 if (req->buf == NULL) {
1991 usb_ep_free_request(ep, req);
2002 * Free a usb_request and its buffer.
2004 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2006 if (ep != NULL && req != NULL) {
2008 usb_ep_free_request(ep, req);
2013 * gs_alloc_req_entry
2015 * Allocates a request and its buffer, using the given
2016 * endpoint, buffer len, and kmalloc flags.
2018 static struct gs_req_entry *
2019 gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2021 struct gs_req_entry *req;
2023 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2027 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2028 if (req->re_req == NULL) {
2033 req->re_req->context = req;
2041 * Frees a request and its buffer.
2043 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2045 if (ep != NULL && req != NULL) {
2046 if (req->re_req != NULL)
2047 gs_free_req(ep, req->re_req);
2055 * Allocate all ports and set the gs_dev struct to point to them.
2056 * Return 0 if successful, or a negative error number.
2058 * The device lock is normally held when calling this function.
2060 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2063 struct gs_port *port;
2068 for (i=0; i<GS_NUM_PORTS; i++) {
2069 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2072 port->port_dev = dev;
2074 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
2075 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2076 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2077 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2078 spin_lock_init(&port->port_lock);
2079 init_waitqueue_head(&port->port_write_wait);
2081 dev->dev_port[i] = port;
2090 * Free all closed ports. Open ports are disconnected by
2091 * freeing their write buffers, setting their device pointers
2092 * and the pointers to them in the device to NULL. These
2093 * ports will be freed when closed.
2095 * The device lock is normally held when calling this function.
2097 static void gs_free_ports(struct gs_dev *dev)
2100 unsigned long flags;
2101 struct gs_port *port;
2106 for (i=0; i<GS_NUM_PORTS; i++) {
2107 if ((port=dev->dev_port[i]) != NULL) {
2108 dev->dev_port[i] = NULL;
2110 spin_lock_irqsave(&port->port_lock, flags);
2112 if (port->port_write_buf != NULL) {
2113 gs_buf_free(port->port_write_buf);
2114 port->port_write_buf = NULL;
2117 if (port->port_open_count > 0 || port->port_in_use) {
2118 port->port_dev = NULL;
2119 wake_up_interruptible(&port->port_write_wait);
2120 if (port->port_tty) {
2121 tty_hangup(port->port_tty);
2123 spin_unlock_irqrestore(&port->port_lock, flags);
2125 spin_unlock_irqrestore(&port->port_lock, flags);
2133 /*-------------------------------------------------------------------------*/
2135 /* Circular Buffer */
2140 * Allocate a circular buffer and all associated memory.
2142 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2149 gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2153 gb->buf_buf = kmalloc(size, kmalloc_flags);
2154 if (gb->buf_buf == NULL) {
2159 gb->buf_size = size;
2160 gb->buf_get = gb->buf_put = gb->buf_buf;
2168 * Free the buffer and all associated memory.
2170 static void gs_buf_free(struct gs_buf *gb)
2181 * Clear out all data in the circular buffer.
2183 static void gs_buf_clear(struct gs_buf *gb)
2186 gb->buf_get = gb->buf_put;
2187 /* equivalent to a get of all data available */
2193 * Return the number of bytes of data available in the circular
2196 static unsigned int gs_buf_data_avail(struct gs_buf *gb)
2199 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2205 * gs_buf_space_avail
2207 * Return the number of bytes of space available in the circular
2210 static unsigned int gs_buf_space_avail(struct gs_buf *gb)
2213 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2221 * Copy data data from a user buffer and put it into the circular buffer.
2222 * Restrict to the amount of space available.
2224 * Return the number of bytes copied.
2227 gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2234 len = gs_buf_space_avail(gb);
2241 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2243 memcpy(gb->buf_put, buf, len);
2244 memcpy(gb->buf_buf, buf+len, count - len);
2245 gb->buf_put = gb->buf_buf + count - len;
2247 memcpy(gb->buf_put, buf, count);
2249 gb->buf_put += count;
2250 else /* count == len */
2251 gb->buf_put = gb->buf_buf;
2260 * Get data from the circular buffer and copy to the given buffer.
2261 * Restrict to the amount of data available.
2263 * Return the number of bytes copied.
2266 gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2273 len = gs_buf_data_avail(gb);
2280 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2282 memcpy(buf, gb->buf_get, len);
2283 memcpy(buf+len, gb->buf_buf, count - len);
2284 gb->buf_get = gb->buf_buf + count - len;
2286 memcpy(buf, gb->buf_get, count);
2288 gb->buf_get += count;
2289 else /* count == len */
2290 gb->buf_get = gb->buf_buf;
2296 /*-------------------------------------------------------------------------*/
2298 static struct tty_driver *gs_tty_driver;
2303 * Register as a USB gadget driver and a tty driver.
2305 static int __init gs_module_init(void)
2310 retval = usb_gadget_register_driver(&gs_gadget_driver);
2312 pr_err("gs_module_init: cannot register gadget driver, "
2313 "ret=%d\n", retval);
2317 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
2320 gs_tty_driver->owner = THIS_MODULE;
2321 gs_tty_driver->driver_name = GS_SHORT_NAME;
2322 gs_tty_driver->name = "ttygs";
2323 gs_tty_driver->major = GS_MAJOR;
2324 gs_tty_driver->minor_start = GS_MINOR_START;
2325 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2326 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
2327 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2328 gs_tty_driver->init_termios = tty_std_termios;
2329 /* must match GS_DEFAULT_DTE_RATE and friends */
2330 gs_tty_driver->init_termios.c_cflag =
2331 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2332 gs_tty_driver->init_termios.c_ispeed = GS_DEFAULT_DTE_RATE;
2333 gs_tty_driver->init_termios.c_ospeed = GS_DEFAULT_DTE_RATE;
2334 tty_set_operations(gs_tty_driver, &gs_tty_ops);
2336 for (i = 0; i < GS_NUM_PORTS; i++)
2337 mutex_init(&gs_open_close_lock[i]);
2339 retval = tty_register_driver(gs_tty_driver);
2341 usb_gadget_unregister_driver(&gs_gadget_driver);
2342 put_tty_driver(gs_tty_driver);
2343 pr_err("gs_module_init: cannot register tty driver, "
2344 "ret=%d\n", retval);
2348 pr_info("gs_module_init: %s %s loaded\n",
2349 GS_LONG_NAME, GS_VERSION_STR);
2352 module_init(gs_module_init);
2357 * Unregister as a tty driver and a USB gadget driver.
2359 static void __exit gs_module_exit(void)
2361 tty_unregister_driver(gs_tty_driver);
2362 put_tty_driver(gs_tty_driver);
2363 usb_gadget_unregister_driver(&gs_gadget_driver);
2365 pr_info("gs_module_exit: %s %s unloaded\n",
2366 GS_LONG_NAME, GS_VERSION_STR);
2368 module_exit(gs_module_exit);