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 0x2200
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 /* the port structure holds info for each port, one for each minor number */
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;
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)
140 /* the device structure holds info for the USB device */
142 struct usb_gadget *dev_gadget; /* gadget device pointer */
143 spinlock_t dev_lock; /* lock for set/reset config */
144 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 */
150 struct usb_endpoint_descriptor /* descriptor of in endpoint */
152 struct usb_endpoint_descriptor /* descriptor of out endpoint */
154 struct usb_request *dev_ctrl_req; /* control request */
155 struct list_head dev_req_list; /* list of write requests */
156 int dev_sched_port; /* round robin port scheduled */
157 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
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,
167 static int gs_recv_packet(struct gs_dev *dev, char *packet,
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);
172 /* gadget driver internals */
173 static int gs_set_config(struct gs_dev *dev, unsigned config);
174 static void gs_reset_config(struct gs_dev *dev);
175 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
176 u8 type, unsigned int index, int is_otg);
178 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
179 gfp_t kmalloc_flags);
180 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
182 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
183 static void gs_free_ports(struct gs_dev *dev);
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,
193 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
199 static struct gs_dev *gs_device;
201 static struct mutex gs_open_close_lock[GS_NUM_PORTS];
204 /*-------------------------------------------------------------------------*/
206 /* USB descriptors */
208 #define GS_MANUFACTURER_STR_ID 1
209 #define GS_PRODUCT_STR_ID 2
210 #define GS_SERIAL_STR_ID 3
211 #define GS_BULK_CONFIG_STR_ID 4
212 #define GS_ACM_CONFIG_STR_ID 5
213 #define GS_CONTROL_STR_ID 6
214 #define GS_DATA_STR_ID 7
216 /* static strings, in UTF-8 */
217 static char manufacturer[50];
218 static struct usb_string gs_strings[] = {
219 { GS_MANUFACTURER_STR_ID, manufacturer },
220 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
221 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
222 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
223 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
224 { GS_DATA_STR_ID, "Gadget Serial Data" },
225 { } /* end of list */
228 static struct usb_gadget_strings gs_string_table = {
229 .language = 0x0409, /* en-us */
230 .strings = gs_strings,
233 static struct usb_device_descriptor gs_device_desc = {
234 .bLength = USB_DT_DEVICE_SIZE,
235 .bDescriptorType = USB_DT_DEVICE,
236 .bcdUSB = __constant_cpu_to_le16(0x0200),
237 .bDeviceSubClass = 0,
238 .bDeviceProtocol = 0,
239 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
240 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
241 .iManufacturer = GS_MANUFACTURER_STR_ID,
242 .iProduct = GS_PRODUCT_STR_ID,
243 .bNumConfigurations = GS_NUM_CONFIGS,
246 static struct usb_otg_descriptor gs_otg_descriptor = {
247 .bLength = sizeof(gs_otg_descriptor),
248 .bDescriptorType = USB_DT_OTG,
249 .bmAttributes = USB_OTG_SRP,
252 static struct usb_config_descriptor gs_bulk_config_desc = {
253 .bLength = USB_DT_CONFIG_SIZE,
254 .bDescriptorType = USB_DT_CONFIG,
255 /* .wTotalLength computed dynamically */
257 .bConfigurationValue = GS_BULK_CONFIG_ID,
258 .iConfiguration = GS_BULK_CONFIG_STR_ID,
259 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
263 static struct usb_config_descriptor gs_acm_config_desc = {
264 .bLength = USB_DT_CONFIG_SIZE,
265 .bDescriptorType = USB_DT_CONFIG,
266 /* .wTotalLength computed dynamically */
268 .bConfigurationValue = GS_ACM_CONFIG_ID,
269 .iConfiguration = GS_ACM_CONFIG_STR_ID,
270 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
274 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
275 .bLength = USB_DT_INTERFACE_SIZE,
276 .bDescriptorType = USB_DT_INTERFACE,
277 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
279 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
280 .bInterfaceSubClass = 0,
281 .bInterfaceProtocol = 0,
282 .iInterface = GS_DATA_STR_ID,
285 static const struct usb_interface_descriptor gs_control_interface_desc = {
286 .bLength = USB_DT_INTERFACE_SIZE,
287 .bDescriptorType = USB_DT_INTERFACE,
288 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
290 .bInterfaceClass = USB_CLASS_COMM,
291 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
292 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
293 .iInterface = GS_CONTROL_STR_ID,
296 static const struct usb_interface_descriptor gs_data_interface_desc = {
297 .bLength = USB_DT_INTERFACE_SIZE,
298 .bDescriptorType = USB_DT_INTERFACE,
299 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
301 .bInterfaceClass = USB_CLASS_CDC_DATA,
302 .bInterfaceSubClass = 0,
303 .bInterfaceProtocol = 0,
304 .iInterface = GS_DATA_STR_ID,
307 static const struct usb_cdc_header_desc gs_header_desc = {
308 .bLength = sizeof(gs_header_desc),
309 .bDescriptorType = USB_DT_CS_INTERFACE,
310 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
311 .bcdCDC = __constant_cpu_to_le16(0x0110),
314 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
315 .bLength = sizeof(gs_call_mgmt_descriptor),
316 .bDescriptorType = USB_DT_CS_INTERFACE,
317 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
319 .bDataInterface = 1, /* index of data interface */
322 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
323 .bLength = sizeof(gs_acm_descriptor),
324 .bDescriptorType = USB_DT_CS_INTERFACE,
325 .bDescriptorSubType = USB_CDC_ACM_TYPE,
326 .bmCapabilities = (1 << 1),
329 static const struct usb_cdc_union_desc gs_union_desc = {
330 .bLength = sizeof(gs_union_desc),
331 .bDescriptorType = USB_DT_CS_INTERFACE,
332 .bDescriptorSubType = USB_CDC_UNION_TYPE,
333 .bMasterInterface0 = 0, /* index of control interface */
334 .bSlaveInterface0 = 1, /* index of data interface */
337 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
338 .bLength = USB_DT_ENDPOINT_SIZE,
339 .bDescriptorType = USB_DT_ENDPOINT,
340 .bEndpointAddress = USB_DIR_IN,
341 .bmAttributes = USB_ENDPOINT_XFER_INT,
342 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
343 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
346 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
347 .bLength = USB_DT_ENDPOINT_SIZE,
348 .bDescriptorType = USB_DT_ENDPOINT,
349 .bEndpointAddress = USB_DIR_IN,
350 .bmAttributes = USB_ENDPOINT_XFER_BULK,
353 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
354 .bLength = USB_DT_ENDPOINT_SIZE,
355 .bDescriptorType = USB_DT_ENDPOINT,
356 .bEndpointAddress = USB_DIR_OUT,
357 .bmAttributes = USB_ENDPOINT_XFER_BULK,
360 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
361 (struct usb_descriptor_header *) &gs_otg_descriptor,
362 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
363 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
364 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
368 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
369 (struct usb_descriptor_header *) &gs_otg_descriptor,
370 (struct usb_descriptor_header *) &gs_control_interface_desc,
371 (struct usb_descriptor_header *) &gs_header_desc,
372 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
373 (struct usb_descriptor_header *) &gs_acm_descriptor,
374 (struct usb_descriptor_header *) &gs_union_desc,
375 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
376 (struct usb_descriptor_header *) &gs_data_interface_desc,
377 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
378 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
382 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
383 .bLength = USB_DT_ENDPOINT_SIZE,
384 .bDescriptorType = USB_DT_ENDPOINT,
385 .bEndpointAddress = USB_DIR_IN,
386 .bmAttributes = USB_ENDPOINT_XFER_INT,
387 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
388 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
391 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
392 .bLength = USB_DT_ENDPOINT_SIZE,
393 .bDescriptorType = USB_DT_ENDPOINT,
394 .bmAttributes = USB_ENDPOINT_XFER_BULK,
395 .wMaxPacketSize = __constant_cpu_to_le16(512),
398 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
399 .bLength = USB_DT_ENDPOINT_SIZE,
400 .bDescriptorType = USB_DT_ENDPOINT,
401 .bmAttributes = USB_ENDPOINT_XFER_BULK,
402 .wMaxPacketSize = __constant_cpu_to_le16(512),
405 static struct usb_qualifier_descriptor gs_qualifier_desc = {
406 .bLength = sizeof(struct usb_qualifier_descriptor),
407 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
408 .bcdUSB = __constant_cpu_to_le16 (0x0200),
409 /* assumes ep0 uses the same value for both speeds ... */
410 .bNumConfigurations = GS_NUM_CONFIGS,
413 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
414 (struct usb_descriptor_header *) &gs_otg_descriptor,
415 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
416 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
417 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
421 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
422 (struct usb_descriptor_header *) &gs_otg_descriptor,
423 (struct usb_descriptor_header *) &gs_control_interface_desc,
424 (struct usb_descriptor_header *) &gs_header_desc,
425 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
426 (struct usb_descriptor_header *) &gs_acm_descriptor,
427 (struct usb_descriptor_header *) &gs_union_desc,
428 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
429 (struct usb_descriptor_header *) &gs_data_interface_desc,
430 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
431 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
436 /*-------------------------------------------------------------------------*/
439 MODULE_DESCRIPTION(GS_LONG_NAME);
440 MODULE_AUTHOR("Al Borchers");
441 MODULE_LICENSE("GPL");
444 module_param(debug, int, S_IRUGO|S_IWUSR);
445 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
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");
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");
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");
460 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
461 module_param(use_acm, uint, S_IRUGO);
462 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
464 /*-------------------------------------------------------------------------*/
471 static int gs_open(struct tty_struct *tty, struct file *file)
475 struct gs_port *port;
481 port_num = tty->index;
483 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
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);
494 pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
495 port_num, tty, file);
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);
506 spin_lock_irqsave(&dev->dev_lock, flags);
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);
512 goto exit_unlock_dev;
515 port = dev->dev_port[port_num];
518 pr_err("gs_open: (%d,%p,%p) NULL port pointer\n",
519 port_num, tty, file);
521 goto exit_unlock_dev;
524 spin_lock(&port->port_lock);
525 spin_unlock(&dev->dev_lock);
527 if (port->port_dev == NULL) {
528 pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n",
529 port_num, tty, file);
531 goto exit_unlock_port;
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);
539 goto exit_unlock_port;
542 tty->driver_data = NULL;
544 /* mark port as in use, we can drop port lock and sleep if necessary */
545 port->port_in_use = 1;
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);
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;
559 goto exit_unlock_port;
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;
568 goto exit_unlock_port;
573 /* wait for carrier detect (not implemented) */
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;
581 goto exit_unlock_port;
584 tty->driver_data = port;
585 port->port_tty = tty;
586 port->port_open_count = 1;
587 port->port_in_use = 0;
589 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
594 spin_unlock_irqrestore(&port->port_lock, flags);
599 spin_unlock_irqrestore(&dev->dev_lock, flags);
609 static int gs_write_finished_event_safely(struct gs_port *p)
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);
619 static void gs_close(struct tty_struct *tty, struct file *file)
621 struct gs_port *port = tty->driver_data;
625 pr_err("gs_close: NULL port pointer\n");
629 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
631 mtx = &gs_open_close_lock[port->port_num];
634 spin_lock_irq(&port->port_lock);
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);
642 if (port->port_open_count > 1) {
643 --port->port_open_count;
647 /* free disconnected port on final close */
648 if (port->port_dev == NULL) {
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;
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);
668 /* free disconnected port on final close */
669 /* (might have happened during the above sleep) */
670 if (port->port_dev == NULL) {
675 gs_buf_clear(port->port_write_buf);
677 tty->driver_data = NULL;
678 port->port_tty = NULL;
679 port->port_in_use = 0;
681 gs_debug("gs_close: (%d,%p,%p) completed\n",
682 port->port_num, tty, file);
685 spin_unlock_irq(&port->port_lock);
692 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
695 struct gs_port *port = tty->driver_data;
699 pr_err("gs_write: NULL port pointer\n");
703 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
709 spin_lock_irqsave(&port->port_lock, flags);
711 if (port->port_dev == NULL) {
712 pr_err("gs_write: (%d,%p) port is not connected\n",
713 port->port_num, tty);
718 if (port->port_open_count == 0) {
719 pr_err("gs_write: (%d,%p) port is closed\n",
720 port->port_num, tty);
725 count = gs_buf_put(port->port_write_buf, buf, count);
727 spin_unlock_irqrestore(&port->port_lock, flags);
731 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
737 spin_unlock_irqrestore(&port->port_lock, flags);
744 static int gs_put_char(struct tty_struct *tty, unsigned char ch)
747 struct gs_port *port = tty->driver_data;
751 pr_err("gs_put_char: NULL port pointer\n");
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));
758 spin_lock_irqsave(&port->port_lock, flags);
760 if (port->port_dev == NULL) {
761 pr_err("gs_put_char: (%d,%p) port is not connected\n",
762 port->port_num, tty);
766 if (port->port_open_count == 0) {
767 pr_err("gs_put_char: (%d,%p) port is closed\n",
768 port->port_num, tty);
772 ret = gs_buf_put(port->port_write_buf, &ch, 1);
775 spin_unlock_irqrestore(&port->port_lock, flags);
782 static void gs_flush_chars(struct tty_struct *tty)
785 struct gs_port *port = tty->driver_data;
788 pr_err("gs_flush_chars: NULL port pointer\n");
792 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
794 spin_lock_irqsave(&port->port_lock, flags);
796 if (port->port_dev == NULL) {
797 pr_err("gs_flush_chars: (%d,%p) port is not connected\n",
798 port->port_num, tty);
802 if (port->port_open_count == 0) {
803 pr_err("gs_flush_chars: (%d,%p) port is closed\n",
804 port->port_num, tty);
808 spin_unlock_irqrestore(&port->port_lock, flags);
815 spin_unlock_irqrestore(&port->port_lock, flags);
821 static int gs_write_room(struct tty_struct *tty)
826 struct gs_port *port = tty->driver_data;
832 spin_lock_irqsave(&port->port_lock, flags);
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);
838 spin_unlock_irqrestore(&port->port_lock, flags);
840 gs_debug("gs_write_room: (%d,%p) room=%d\n",
841 port->port_num, tty, room);
849 static int gs_chars_in_buffer(struct tty_struct *tty)
853 struct gs_port *port = tty->driver_data;
858 spin_lock_irqsave(&port->port_lock, flags);
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);
864 spin_unlock_irqrestore(&port->port_lock, flags);
866 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
867 port->port_num, tty, chars);
875 static void gs_throttle(struct tty_struct *tty)
882 static void gs_unthrottle(struct tty_struct *tty)
889 static void gs_break(struct tty_struct *tty, int break_state)
896 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
898 struct gs_port *port = tty->driver_data;
901 pr_err("gs_ioctl: NULL port pointer\n");
905 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
906 port->port_num, tty, file, cmd, arg);
910 /* could not handle ioctl */
917 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
921 static const struct tty_operations gs_tty_ops = {
925 .put_char = gs_put_char,
926 .flush_chars = gs_flush_chars,
927 .write_room = gs_write_room,
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,
936 /*-------------------------------------------------------------------------*/
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.
947 static int gs_send(struct gs_dev *dev)
952 struct usb_request *req;
955 pr_err("gs_send: NULL device pointer\n");
959 spin_lock_irqsave(&dev->dev_lock, flags);
963 while(!list_empty(&dev->dev_req_list)) {
965 req = list_entry(dev->dev_req_list.next,
966 struct usb_request, list);
968 len = gs_send_packet(dev, req->buf, ep->maxpacket);
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);
978 spin_unlock_irqrestore(&dev->dev_lock, flags);
979 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
981 "gs_send: cannot queue read request, ret=%d\n",
983 spin_lock_irqsave(&dev->dev_lock, flags);
986 spin_lock_irqsave(&dev->dev_lock, flags);
993 spin_unlock_irqrestore(&dev->dev_lock, flags);
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.
1006 * Called during USB completion routine, on interrupt time.
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.
1012 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1015 struct gs_port *port;
1017 /* TEMPORARY -- only port 0 is supported right now */
1018 port = dev->dev_port[0];
1021 pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
1025 spin_lock(&port->port_lock);
1027 len = gs_buf_data_avail(port->port_write_buf);
1034 size = gs_buf_get(port->port_write_buf, packet, size);
1037 wake_up_interruptible(&port->port_tty->write_wait);
1040 spin_unlock(&port->port_lock);
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.
1051 * Called during USB completion routine, on interrupt time.
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.
1057 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1060 struct gs_port *port;
1062 struct tty_struct *tty;
1064 /* TEMPORARY -- only port 0 is supported right now */
1065 port = dev->dev_port[0];
1068 pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
1073 spin_lock(&port->port_lock);
1075 if (port->port_open_count == 0) {
1076 pr_err("gs_recv_packet: port=%d, port is closed\n",
1083 tty = port->port_tty;
1086 pr_err("gs_recv_packet: port=%d, NULL tty pointer\n",
1092 if (port->port_tty->magic != TTY_MAGIC) {
1093 pr_err("gs_recv_packet: port=%d, bad tty magic\n",
1099 len = tty_buffer_request_room(tty, size);
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);
1107 spin_unlock(&port->port_lock);
1114 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1117 struct gs_dev *dev = ep->driver_data;
1120 pr_err("gs_read_complete: NULL device pointer\n");
1124 switch(req->status) {
1126 /* normal completion */
1127 gs_recv_packet(dev, req->buf, req->actual);
1129 req->length = ep->maxpacket;
1130 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1132 "gs_read_complete: cannot queue read request, ret=%d\n",
1139 gs_debug("gs_read_complete: shutdown\n");
1140 gs_free_req(ep, req);
1146 "gs_read_complete: unexpected status error, status=%d\n",
1156 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1158 struct gs_dev *dev = ep->driver_data;
1161 pr_err("gs_write_complete: NULL device pointer\n");
1165 switch(req->status) {
1167 /* normal completion */
1169 spin_lock(&dev->dev_lock);
1170 list_add(&req->list, &dev->dev_req_list);
1171 spin_unlock(&dev->dev_lock);
1179 gs_debug("gs_write_complete: shutdown\n");
1180 gs_free_req(ep, req);
1185 "gs_write_complete: unexpected status error, status=%d\n",
1192 /*-------------------------------------------------------------------------*/
1199 * Called on module unload. Frees the control request and device
1202 static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1204 struct gs_dev *dev = get_gadget_data(gadget);
1208 /* read/write requests already freed, only control request remains */
1210 if (dev->dev_ctrl_req != NULL) {
1211 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1212 dev->dev_ctrl_req = NULL;
1214 gs_reset_config(dev);
1217 set_gadget_data(gadget, NULL);
1220 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1227 * Called on module load. Allocates and initializes the device
1228 * structure and a control request.
1230 static int __init gs_bind(struct usb_gadget *gadget)
1237 /* Some controllers can't support CDC ACM:
1238 * - sh doesn't support multiple interfaces or configs;
1239 * - sa1100 doesn't have a third interrupt endpoint
1241 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1244 gcnum = usb_gadget_controller_number(gadget);
1246 gs_device_desc.bcdDevice =
1247 cpu_to_le16(GS_VERSION_NUM | gcnum);
1249 pr_warning("gs_bind: controller '%s' not recognized\n",
1251 /* unrecognized, but safe unless bulk is REALLY quirky */
1252 gs_device_desc.bcdDevice =
1253 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1256 dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1260 usb_ep_autoconfig_reset(gadget);
1262 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1265 dev->dev_in_ep = ep;
1266 ep->driver_data = dev; /* claim the endpoint */
1268 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1271 dev->dev_out_ep = ep;
1272 ep->driver_data = dev; /* claim the endpoint */
1275 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1277 pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
1280 gs_device_desc.idProduct = __constant_cpu_to_le16(
1282 dev->dev_notify_ep = ep;
1283 ep->driver_data = dev; /* claim the endpoint */
1286 gs_device_desc.bDeviceClass = use_acm
1287 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1288 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1290 if (gadget_is_dualspeed(gadget)) {
1291 gs_qualifier_desc.bDeviceClass = use_acm
1292 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1293 /* assume ep0 uses the same packet size for both speeds */
1294 gs_qualifier_desc.bMaxPacketSize0 =
1295 gs_device_desc.bMaxPacketSize0;
1296 /* assume endpoints are dual-speed */
1297 gs_highspeed_notify_desc.bEndpointAddress =
1298 gs_fullspeed_notify_desc.bEndpointAddress;
1299 gs_highspeed_in_desc.bEndpointAddress =
1300 gs_fullspeed_in_desc.bEndpointAddress;
1301 gs_highspeed_out_desc.bEndpointAddress =
1302 gs_fullspeed_out_desc.bEndpointAddress;
1305 usb_gadget_set_selfpowered(gadget);
1307 if (gadget_is_otg(gadget)) {
1308 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1309 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1310 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1315 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1316 init_utsname()->sysname, init_utsname()->release,
1319 dev->dev_gadget = gadget;
1320 spin_lock_init(&dev->dev_lock);
1321 INIT_LIST_HEAD(&dev->dev_req_list);
1322 set_gadget_data(gadget, dev);
1324 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1325 pr_err("gs_bind: cannot allocate ports\n");
1330 /* preallocate control response and buffer */
1331 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1333 if (dev->dev_ctrl_req == NULL) {
1337 gadget->ep0->driver_data = dev;
1339 pr_info("gs_bind: %s %s bound\n",
1340 GS_LONG_NAME, GS_VERSION_STR);
1346 pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
1350 static int gs_setup_standard(struct usb_gadget *gadget,
1351 const struct usb_ctrlrequest *ctrl)
1353 int ret = -EOPNOTSUPP;
1354 struct gs_dev *dev = get_gadget_data(gadget);
1355 struct usb_request *req = dev->dev_ctrl_req;
1356 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1357 u16 wValue = le16_to_cpu(ctrl->wValue);
1358 u16 wLength = le16_to_cpu(ctrl->wLength);
1360 switch (ctrl->bRequest) {
1361 case USB_REQ_GET_DESCRIPTOR:
1362 if (ctrl->bRequestType != USB_DIR_IN)
1365 switch (wValue >> 8) {
1368 (u16)sizeof(struct usb_device_descriptor));
1369 memcpy(req->buf, &gs_device_desc, ret);
1372 case USB_DT_DEVICE_QUALIFIER:
1373 if (!gadget_is_dualspeed(gadget))
1376 (u16)sizeof(struct usb_qualifier_descriptor));
1377 memcpy(req->buf, &gs_qualifier_desc, ret);
1380 case USB_DT_OTHER_SPEED_CONFIG:
1381 if (!gadget_is_dualspeed(gadget))
1385 ret = gs_build_config_buf(req->buf, gadget,
1386 wValue >> 8, wValue & 0xff,
1387 gadget_is_otg(gadget));
1389 ret = min(wLength, (u16)ret);
1393 /* wIndex == language code. */
1394 ret = usb_gadget_get_string(&gs_string_table,
1395 wValue & 0xff, req->buf);
1397 ret = min(wLength, (u16)ret);
1402 case USB_REQ_SET_CONFIGURATION:
1403 if (ctrl->bRequestType != 0)
1405 spin_lock(&dev->dev_lock);
1406 ret = gs_set_config(dev, wValue);
1407 spin_unlock(&dev->dev_lock);
1410 case USB_REQ_GET_CONFIGURATION:
1411 if (ctrl->bRequestType != USB_DIR_IN)
1413 *(u8 *)req->buf = dev->dev_config;
1414 ret = min(wLength, (u16)1);
1417 case USB_REQ_SET_INTERFACE:
1418 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1420 || wIndex >= GS_MAX_NUM_INTERFACES)
1422 if (dev->dev_config == GS_BULK_CONFIG_ID
1423 && wIndex != GS_BULK_INTERFACE_ID)
1425 /* no alternate interface settings */
1428 spin_lock(&dev->dev_lock);
1429 /* PXA hardware partially handles SET_INTERFACE;
1430 * we need to kluge around that interference. */
1431 if (gadget_is_pxa(gadget)) {
1432 ret = gs_set_config(dev, use_acm ?
1433 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1434 goto set_interface_done;
1436 if (dev->dev_config != GS_BULK_CONFIG_ID
1437 && wIndex == GS_CONTROL_INTERFACE_ID) {
1438 if (dev->dev_notify_ep) {
1439 usb_ep_disable(dev->dev_notify_ep);
1440 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1443 usb_ep_disable(dev->dev_in_ep);
1444 usb_ep_disable(dev->dev_out_ep);
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);
1450 spin_unlock(&dev->dev_lock);
1453 case USB_REQ_GET_INTERFACE:
1454 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1455 || dev->dev_config == GS_NO_CONFIG_ID)
1457 if (wIndex >= GS_MAX_NUM_INTERFACES
1458 || (dev->dev_config == GS_BULK_CONFIG_ID
1459 && wIndex != GS_BULK_INTERFACE_ID)) {
1463 /* no alternate interface settings */
1464 *(u8 *)req->buf = 0;
1465 ret = min(wLength, (u16)1);
1469 pr_err("gs_setup: unknown standard request, type=%02x, "
1470 "request=%02x, value=%04x, index=%04x, length=%d\n",
1471 ctrl->bRequestType, ctrl->bRequest,
1472 wValue, wIndex, wLength);
1479 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
1480 struct usb_request *req)
1482 struct gs_dev *dev = ep->driver_data;
1483 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1485 switch (req->status) {
1487 /* normal completion */
1488 if (req->actual != sizeof(port->port_line_coding))
1489 usb_ep_set_halt(ep);
1491 struct usb_cdc_line_coding *value = req->buf;
1493 /* REVISIT: we currently just remember this data.
1494 * If we change that, (a) validate it first, then
1495 * (b) update whatever hardware needs updating.
1497 spin_lock(&port->port_lock);
1498 port->port_line_coding = *value;
1499 spin_unlock(&port->port_lock);
1505 gs_free_req(ep, req);
1515 static int gs_setup_class(struct usb_gadget *gadget,
1516 const struct usb_ctrlrequest *ctrl)
1518 int ret = -EOPNOTSUPP;
1519 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;
1522 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1523 u16 wValue = le16_to_cpu(ctrl->wValue);
1524 u16 wLength = le16_to_cpu(ctrl->wLength);
1526 switch (ctrl->bRequest) {
1527 case USB_CDC_REQ_SET_LINE_CODING:
1528 if (wLength != sizeof(struct usb_cdc_line_coding))
1531 req->complete = gs_setup_complete_set_line_coding;
1534 case USB_CDC_REQ_GET_LINE_CODING:
1535 ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
1537 spin_lock(&port->port_lock);
1538 memcpy(req->buf, &port->port_line_coding, ret);
1539 spin_unlock(&port->port_lock);
1543 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1548 /* REVISIT: we currently just remember this data.
1549 * If we change that, update whatever hardware needs
1552 spin_lock(&port->port_lock);
1553 port->port_handshake_bits = wValue;
1554 spin_unlock(&port->port_lock);
1559 /* NOTE: strictly speaking, we should accept AT-commands
1560 * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE.
1561 * But our call management descriptor says we don't handle
1562 * call management, so we should be able to get by without
1563 * handling those "required" commands (except by stalling).
1565 pr_err("gs_setup: unknown class request, "
1566 "type=%02x, request=%02x, value=%04x, "
1567 "index=%04x, length=%d\n",
1568 ctrl->bRequestType, ctrl->bRequest,
1569 wValue, wIndex, wLength);
1579 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1581 if (req->status || req->actual != req->length) {
1582 pr_err("gs_setup_complete: status error, status=%d, "
1583 "actual=%d, length=%d\n",
1584 req->status, req->actual, req->length);
1591 * Implements all the control endpoint functionality that's not
1592 * handled in hardware or the hardware driver.
1594 * Returns the size of the data sent to the host, or a negative
1597 static int gs_setup(struct usb_gadget *gadget,
1598 const struct usb_ctrlrequest *ctrl)
1600 int ret = -EOPNOTSUPP;
1601 struct gs_dev *dev = get_gadget_data(gadget);
1602 struct usb_request *req = dev->dev_ctrl_req;
1603 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1604 u16 wValue = le16_to_cpu(ctrl->wValue);
1605 u16 wLength = le16_to_cpu(ctrl->wLength);
1607 req->complete = gs_setup_complete;
1609 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1610 case USB_TYPE_STANDARD:
1611 ret = gs_setup_standard(gadget, ctrl);
1614 case USB_TYPE_CLASS:
1615 ret = gs_setup_class(gadget, ctrl);
1619 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1620 "value=%04x, index=%04x, length=%d\n",
1621 ctrl->bRequestType, ctrl->bRequest,
1622 wValue, wIndex, wLength);
1626 /* respond with data transfer before status phase? */
1629 req->zero = ret < wLength
1630 && (ret % gadget->ep0->maxpacket) == 0;
1631 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1633 pr_err("gs_setup: cannot queue response, ret=%d\n",
1636 gs_setup_complete(gadget->ep0, req);
1640 /* device either stalls (ret < 0) or reports success */
1647 * Called when the device is disconnected. Frees the closed
1648 * ports and disconnects open ports. Open ports will be freed
1649 * on close. Then reallocates the ports for the next connection.
1651 static void gs_disconnect(struct usb_gadget *gadget)
1653 unsigned long flags;
1654 struct gs_dev *dev = get_gadget_data(gadget);
1656 spin_lock_irqsave(&dev->dev_lock, flags);
1658 gs_reset_config(dev);
1660 /* free closed ports and disconnect open ports */
1661 /* (open ports will be freed when closed) */
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");
1668 spin_unlock_irqrestore(&dev->dev_lock, flags);
1670 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1673 static struct usb_gadget_driver gs_gadget_driver = {
1674 #ifdef CONFIG_USB_GADGET_DUALSPEED
1675 .speed = USB_SPEED_HIGH,
1677 .speed = USB_SPEED_FULL,
1678 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1679 .function = GS_LONG_NAME,
1681 .unbind = gs_unbind,
1683 .disconnect = gs_disconnect,
1685 .name = GS_SHORT_NAME,
1686 .owner = THIS_MODULE,
1693 * Configures the device by enabling device specific
1694 * optimizations, setting up the endpoints, allocating
1695 * read and write requests and queuing read requests.
1697 * The device lock must be held when calling this function.
1699 static int gs_set_config(struct gs_dev *dev, unsigned config)
1703 struct usb_gadget *gadget = dev->dev_gadget;
1705 struct usb_endpoint_descriptor *out, *in, *notify;
1706 struct usb_request *req;
1709 pr_err("gs_set_config: NULL device pointer\n");
1713 if (config == dev->dev_config)
1716 gs_reset_config(dev);
1719 case GS_NO_CONFIG_ID:
1721 case GS_BULK_CONFIG_ID:
1725 case GS_ACM_CONFIG_ID:
1733 in = choose_ep_desc(gadget,
1734 &gs_highspeed_in_desc,
1735 &gs_fullspeed_in_desc);
1736 out = choose_ep_desc(gadget,
1737 &gs_highspeed_out_desc,
1738 &gs_fullspeed_out_desc);
1739 notify = dev->dev_notify_ep
1740 ? choose_ep_desc(gadget,
1741 &gs_highspeed_notify_desc,
1742 &gs_fullspeed_notify_desc)
1745 ret = usb_ep_enable(dev->dev_in_ep, in);
1747 dev->dev_in_ep_desc = in;
1749 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1750 __func__, "IN", dev->dev_in_ep->name, ret);
1754 ret = usb_ep_enable(dev->dev_out_ep, out);
1756 dev->dev_out_ep_desc = out;
1758 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1759 __func__, "OUT", dev->dev_out_ep->name, ret);
1761 usb_ep_disable(dev->dev_in_ep);
1766 ret = usb_ep_enable(dev->dev_notify_ep, notify);
1768 dev->dev_notify_ep_desc = notify;
1770 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1772 dev->dev_notify_ep->name, ret);
1773 usb_ep_disable(dev->dev_out_ep);
1778 dev->dev_config = config;
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);
1790 pr_err("gs_set_config: cannot allocate "
1793 goto exit_reset_config;
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);
1802 req->complete = gs_write_complete;
1803 list_add(&req->list, &dev->dev_req_list);
1805 pr_err("gs_set_config: cannot allocate "
1806 "write requests\n");
1808 goto exit_reset_config;
1812 /* REVISIT the ACM mode should be able to actually *issue* some
1813 * notifications, for at least serial state change events if
1814 * not also for network connection; say so in bmCapabilities.
1817 pr_info("gs_set_config: %s configured, %s speed %s config\n",
1819 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1820 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1825 gs_reset_config(dev);
1832 * Mark the device as not configured, disable all endpoints,
1833 * which forces completion of pending I/O and frees queued
1834 * requests, and free the remaining write requests on the
1837 * The device lock must be held when calling this function.
1839 static void gs_reset_config(struct gs_dev *dev)
1841 struct usb_request *req;
1844 pr_err("gs_reset_config: NULL device pointer\n");
1848 if (dev->dev_config == GS_NO_CONFIG_ID)
1851 dev->dev_config = GS_NO_CONFIG_ID;
1853 /* free write requests on the free list */
1854 while(!list_empty(&dev->dev_req_list)) {
1855 req = list_entry(dev->dev_req_list.next,
1856 struct usb_request, list);
1857 list_del(&req->list);
1858 gs_free_req(dev->dev_in_ep, req);
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);
1870 * gs_build_config_buf
1872 * Builds the config descriptors in the given buffer and returns the
1873 * length, or a negative error number.
1875 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
1876 u8 type, unsigned int index, int is_otg)
1880 const struct usb_config_descriptor *config_desc;
1881 const struct usb_descriptor_header **function;
1883 if (index >= gs_device_desc.bNumConfigurations)
1886 /* other speed switches high and full speed */
1887 if (gadget_is_dualspeed(g)) {
1888 high_speed = (g->speed == USB_SPEED_HIGH);
1889 if (type == USB_DT_OTHER_SPEED_CONFIG)
1890 high_speed = !high_speed;
1894 config_desc = &gs_acm_config_desc;
1895 function = high_speed
1896 ? gs_acm_highspeed_function
1897 : gs_acm_fullspeed_function;
1899 config_desc = &gs_bulk_config_desc;
1900 function = high_speed
1901 ? gs_bulk_highspeed_function
1902 : gs_bulk_fullspeed_function;
1905 /* for now, don't advertise srp-only devices */
1909 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
1913 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
1921 * Allocate a usb_request and its buffer. Returns a pointer to the
1922 * usb_request or NULL if there is an error.
1924 static struct usb_request *
1925 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
1927 struct usb_request *req;
1932 req = usb_ep_alloc_request(ep, kmalloc_flags);
1936 req->buf = kmalloc(len, kmalloc_flags);
1937 if (req->buf == NULL) {
1938 usb_ep_free_request(ep, req);
1949 * Free a usb_request and its buffer.
1951 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
1953 if (ep != NULL && req != NULL) {
1955 usb_ep_free_request(ep, req);
1962 * Allocate all ports and set the gs_dev struct to point to them.
1963 * Return 0 if successful, or a negative error number.
1965 * The device lock is normally held when calling this function.
1967 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
1970 struct gs_port *port;
1975 for (i=0; i<GS_NUM_PORTS; i++) {
1976 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
1979 port->port_dev = dev;
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);
1988 dev->dev_port[i] = port;
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.
2002 * The device lock is normally held when calling this function.
2004 static void gs_free_ports(struct gs_dev *dev)
2007 unsigned long flags;
2008 struct gs_port *port;
2013 for (i=0; i<GS_NUM_PORTS; i++) {
2014 if ((port=dev->dev_port[i]) != NULL) {
2015 dev->dev_port[i] = NULL;
2017 spin_lock_irqsave(&port->port_lock, flags);
2019 if (port->port_write_buf != NULL) {
2020 gs_buf_free(port->port_write_buf);
2021 port->port_write_buf = NULL;
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);
2030 spin_unlock_irqrestore(&port->port_lock, flags);
2032 spin_unlock_irqrestore(&port->port_lock, flags);
2040 /*-------------------------------------------------------------------------*/
2042 /* Circular Buffer */
2047 * Allocate a circular buffer and all associated memory.
2049 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2056 gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2060 gb->buf_buf = kmalloc(size, kmalloc_flags);
2061 if (gb->buf_buf == NULL) {
2066 gb->buf_size = size;
2067 gb->buf_get = gb->buf_put = gb->buf_buf;
2075 * Free the buffer and all associated memory.
2077 static void gs_buf_free(struct gs_buf *gb)
2088 * Clear out all data in the circular buffer.
2090 static void gs_buf_clear(struct gs_buf *gb)
2093 gb->buf_get = gb->buf_put;
2094 /* equivalent to a get of all data available */
2100 * Return the number of bytes of data available in the circular
2103 static unsigned int gs_buf_data_avail(struct gs_buf *gb)
2106 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2112 * gs_buf_space_avail
2114 * Return the number of bytes of space available in the circular
2117 static unsigned int gs_buf_space_avail(struct gs_buf *gb)
2120 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2128 * Copy data data from a user buffer and put it into the circular buffer.
2129 * Restrict to the amount of space available.
2131 * Return the number of bytes copied.
2134 gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2141 len = gs_buf_space_avail(gb);
2148 len = gb->buf_buf + gb->buf_size - gb->buf_put;
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;
2154 memcpy(gb->buf_put, buf, count);
2156 gb->buf_put += count;
2157 else /* count == len */
2158 gb->buf_put = gb->buf_buf;
2167 * Get data from the circular buffer and copy to the given buffer.
2168 * Restrict to the amount of data available.
2170 * Return the number of bytes copied.
2173 gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2180 len = gs_buf_data_avail(gb);
2187 len = gb->buf_buf + gb->buf_size - gb->buf_get;
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;
2193 memcpy(buf, gb->buf_get, count);
2195 gb->buf_get += count;
2196 else /* count == len */
2197 gb->buf_get = gb->buf_buf;
2203 /*-------------------------------------------------------------------------*/
2205 static struct tty_driver *gs_tty_driver;
2210 * Register as a USB gadget driver and a tty driver.
2212 static int __init gs_module_init(void)
2217 retval = usb_gadget_register_driver(&gs_gadget_driver);
2219 pr_err("gs_module_init: cannot register gadget driver, "
2220 "ret=%d\n", retval);
2224 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
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);
2243 for (i = 0; i < GS_NUM_PORTS; i++)
2244 mutex_init(&gs_open_close_lock[i]);
2246 retval = tty_register_driver(gs_tty_driver);
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);
2255 pr_info("gs_module_init: %s %s loaded\n",
2256 GS_LONG_NAME, GS_VERSION_STR);
2259 module_init(gs_module_init);
2264 * Unregister as a tty driver and a USB gadget driver.
2266 static void __exit gs_module_exit(void)
2268 tty_unregister_driver(gs_tty_driver);
2269 put_tty_driver(gs_tty_driver);
2270 usb_gadget_unregister_driver(&gs_gadget_driver);
2272 pr_info("gs_module_exit: %s %s unloaded\n",
2273 GS_LONG_NAME, GS_VERSION_STR);
2275 module_exit(gs_module_exit);