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.
20 #include <linux/kernel.h>
21 #include <linux/utsname.h>
22 #include <linux/device.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/cdc.h>
28 #include <linux/usb/gadget.h>
30 #include "gadget_chips.h"
35 #define GS_VERSION_STR "v2.2"
36 #define GS_VERSION_NUM 0x0202
38 #define GS_LONG_NAME "Gadget Serial"
39 #define GS_SHORT_NAME "g_serial"
42 #define GS_MINOR_START 0
44 #define GS_NUM_PORTS 16
46 #define GS_NUM_CONFIGS 1
47 #define GS_NO_CONFIG_ID 0
48 #define GS_BULK_CONFIG_ID 1
49 #define GS_ACM_CONFIG_ID 2
51 #define GS_MAX_NUM_INTERFACES 2
52 #define GS_BULK_INTERFACE_ID 0
53 #define GS_CONTROL_INTERFACE_ID 0
54 #define GS_DATA_INTERFACE_ID 1
56 #define GS_MAX_DESC_LEN 256
58 #define GS_DEFAULT_READ_Q_SIZE 32
59 #define GS_DEFAULT_WRITE_Q_SIZE 32
61 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
62 #define GS_TMP_BUF_SIZE 8192
64 #define GS_CLOSE_TIMEOUT 15
66 #define GS_DEFAULT_USE_ACM 0
68 #define GS_DEFAULT_DTE_RATE 9600
69 #define GS_DEFAULT_DATA_BITS 8
70 #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
71 #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
73 /* maxpacket and other transfer characteristics vary by speed. */
74 static inline struct usb_endpoint_descriptor *
75 choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
76 struct usb_endpoint_descriptor *fs)
78 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
91 #define gs_debug(format, arg...) \
92 do { if (debug) pr_debug(format, ## arg); } while (0)
93 #define gs_debug_level(level, format, arg...) \
94 do { if (debug >= level) pr_debug(format, ## arg); } while (0)
97 /* Thanks to NetChip Technologies for donating this product ID.
99 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
100 * Instead: allocate your own, using normal USB-IF procedures.
102 #define GS_VENDOR_ID 0x0525 /* NetChip */
103 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
104 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
106 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
107 #define GS_NOTIFY_MAXPACKET 8
114 /* circular buffer */
116 unsigned int buf_size;
122 /* list of requests */
123 struct gs_req_entry {
124 struct list_head re_entry;
125 struct usb_request *re_req;
128 /* the port structure holds info for each port, one for each minor number */
130 struct gs_dev *port_dev; /* pointer to device struct */
131 struct tty_struct *port_tty; /* pointer to tty struct */
132 spinlock_t port_lock;
135 int port_in_use; /* open/close in progress */
136 wait_queue_head_t port_write_wait;/* waiting to write */
137 struct gs_buf *port_write_buf;
138 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
139 u16 port_handshake_bits;
140 #define RS232_RTS (1 << 1)
141 #define RS232_DTE (1 << 0)
144 /* the device structure holds info for the USB device */
146 struct usb_gadget *dev_gadget; /* gadget device pointer */
147 spinlock_t dev_lock; /* lock for set/reset config */
148 int dev_config; /* configuration number */
149 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
150 struct usb_ep *dev_in_ep; /* address of in endpoint */
151 struct usb_ep *dev_out_ep; /* address of out endpoint */
152 struct usb_endpoint_descriptor /* descriptor of notify ep */
154 struct usb_endpoint_descriptor /* descriptor of in endpoint */
156 struct usb_endpoint_descriptor /* descriptor of out endpoint */
158 struct usb_request *dev_ctrl_req; /* control request */
159 struct list_head dev_req_list; /* list of write requests */
160 int dev_sched_port; /* round robin port scheduled */
161 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
168 static int __init gs_module_init(void);
169 static void __exit gs_module_exit(void);
172 static int gs_open(struct tty_struct *tty, struct file *file);
173 static void gs_close(struct tty_struct *tty, struct file *file);
174 static int gs_write(struct tty_struct *tty,
175 const unsigned char *buf, int count);
176 static int gs_put_char(struct tty_struct *tty, unsigned char ch);
177 static void gs_flush_chars(struct tty_struct *tty);
178 static int gs_write_room(struct tty_struct *tty);
179 static int gs_chars_in_buffer(struct tty_struct *tty);
180 static void gs_throttle(struct tty_struct * tty);
181 static void gs_unthrottle(struct tty_struct * tty);
182 static void gs_break(struct tty_struct *tty, int break_state);
183 static int gs_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old);
187 static int gs_send(struct gs_dev *dev);
188 static int gs_send_packet(struct gs_dev *dev, char *packet,
190 static int gs_recv_packet(struct gs_dev *dev, char *packet,
192 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
193 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
196 static int gs_bind(struct usb_gadget *gadget);
197 static void gs_unbind(struct usb_gadget *gadget);
198 static int gs_setup(struct usb_gadget *gadget,
199 const struct usb_ctrlrequest *ctrl);
200 static int gs_setup_standard(struct usb_gadget *gadget,
201 const struct usb_ctrlrequest *ctrl);
202 static int gs_setup_class(struct usb_gadget *gadget,
203 const struct usb_ctrlrequest *ctrl);
204 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
205 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
206 struct usb_request *req);
207 static void gs_disconnect(struct usb_gadget *gadget);
208 static int gs_set_config(struct gs_dev *dev, unsigned config);
209 static void gs_reset_config(struct gs_dev *dev);
210 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
211 u8 type, unsigned int index, int is_otg);
213 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
214 gfp_t kmalloc_flags);
215 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
217 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
218 gfp_t kmalloc_flags);
219 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
221 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
222 static void gs_free_ports(struct gs_dev *dev);
224 /* circular buffer */
225 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
226 static void gs_buf_free(struct gs_buf *gb);
227 static void gs_buf_clear(struct gs_buf *gb);
228 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
229 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
230 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
232 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
235 /* external functions */
236 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
241 static struct gs_dev *gs_device;
243 static const char *EP_IN_NAME;
244 static const char *EP_OUT_NAME;
245 static const char *EP_NOTIFY_NAME;
247 static struct mutex gs_open_close_lock[GS_NUM_PORTS];
249 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
250 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
252 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
254 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
257 /* tty driver struct */
258 static const struct tty_operations gs_tty_ops = {
262 .put_char = gs_put_char,
263 .flush_chars = gs_flush_chars,
264 .write_room = gs_write_room,
266 .set_termios = gs_set_termios,
267 .throttle = gs_throttle,
268 .unthrottle = gs_unthrottle,
269 .break_ctl = gs_break,
270 .chars_in_buffer = gs_chars_in_buffer,
272 static struct tty_driver *gs_tty_driver;
274 /* gadget driver struct */
275 static struct usb_gadget_driver gs_gadget_driver = {
276 #ifdef CONFIG_USB_GADGET_DUALSPEED
277 .speed = USB_SPEED_HIGH,
279 .speed = USB_SPEED_FULL,
280 #endif /* CONFIG_USB_GADGET_DUALSPEED */
281 .function = GS_LONG_NAME,
285 .disconnect = gs_disconnect,
287 .name = GS_SHORT_NAME,
292 /* USB descriptors */
294 #define GS_MANUFACTURER_STR_ID 1
295 #define GS_PRODUCT_STR_ID 2
296 #define GS_SERIAL_STR_ID 3
297 #define GS_BULK_CONFIG_STR_ID 4
298 #define GS_ACM_CONFIG_STR_ID 5
299 #define GS_CONTROL_STR_ID 6
300 #define GS_DATA_STR_ID 7
302 /* static strings, in UTF-8 */
303 static char manufacturer[50];
304 static struct usb_string gs_strings[] = {
305 { GS_MANUFACTURER_STR_ID, manufacturer },
306 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
307 { GS_SERIAL_STR_ID, "0" },
308 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
309 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
310 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
311 { GS_DATA_STR_ID, "Gadget Serial Data" },
312 { } /* end of list */
315 static struct usb_gadget_strings gs_string_table = {
316 .language = 0x0409, /* en-us */
317 .strings = gs_strings,
320 static struct usb_device_descriptor gs_device_desc = {
321 .bLength = USB_DT_DEVICE_SIZE,
322 .bDescriptorType = USB_DT_DEVICE,
323 .bcdUSB = __constant_cpu_to_le16(0x0200),
324 .bDeviceSubClass = 0,
325 .bDeviceProtocol = 0,
326 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
327 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
328 .iManufacturer = GS_MANUFACTURER_STR_ID,
329 .iProduct = GS_PRODUCT_STR_ID,
330 .iSerialNumber = GS_SERIAL_STR_ID,
331 .bNumConfigurations = GS_NUM_CONFIGS,
334 static struct usb_otg_descriptor gs_otg_descriptor = {
335 .bLength = sizeof(gs_otg_descriptor),
336 .bDescriptorType = USB_DT_OTG,
337 .bmAttributes = USB_OTG_SRP,
340 static struct usb_config_descriptor gs_bulk_config_desc = {
341 .bLength = USB_DT_CONFIG_SIZE,
342 .bDescriptorType = USB_DT_CONFIG,
343 /* .wTotalLength computed dynamically */
345 .bConfigurationValue = GS_BULK_CONFIG_ID,
346 .iConfiguration = GS_BULK_CONFIG_STR_ID,
347 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
351 static struct usb_config_descriptor gs_acm_config_desc = {
352 .bLength = USB_DT_CONFIG_SIZE,
353 .bDescriptorType = USB_DT_CONFIG,
354 /* .wTotalLength computed dynamically */
356 .bConfigurationValue = GS_ACM_CONFIG_ID,
357 .iConfiguration = GS_ACM_CONFIG_STR_ID,
358 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
362 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
363 .bLength = USB_DT_INTERFACE_SIZE,
364 .bDescriptorType = USB_DT_INTERFACE,
365 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
367 .bInterfaceClass = USB_CLASS_CDC_DATA,
368 .bInterfaceSubClass = 0,
369 .bInterfaceProtocol = 0,
370 .iInterface = GS_DATA_STR_ID,
373 static const struct usb_interface_descriptor gs_control_interface_desc = {
374 .bLength = USB_DT_INTERFACE_SIZE,
375 .bDescriptorType = USB_DT_INTERFACE,
376 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
378 .bInterfaceClass = USB_CLASS_COMM,
379 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
380 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
381 .iInterface = GS_CONTROL_STR_ID,
384 static const struct usb_interface_descriptor gs_data_interface_desc = {
385 .bLength = USB_DT_INTERFACE_SIZE,
386 .bDescriptorType = USB_DT_INTERFACE,
387 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
389 .bInterfaceClass = USB_CLASS_CDC_DATA,
390 .bInterfaceSubClass = 0,
391 .bInterfaceProtocol = 0,
392 .iInterface = GS_DATA_STR_ID,
395 static const struct usb_cdc_header_desc gs_header_desc = {
396 .bLength = sizeof(gs_header_desc),
397 .bDescriptorType = USB_DT_CS_INTERFACE,
398 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
399 .bcdCDC = __constant_cpu_to_le16(0x0110),
402 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
403 .bLength = sizeof(gs_call_mgmt_descriptor),
404 .bDescriptorType = USB_DT_CS_INTERFACE,
405 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
407 .bDataInterface = 1, /* index of data interface */
410 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
411 .bLength = sizeof(gs_acm_descriptor),
412 .bDescriptorType = USB_DT_CS_INTERFACE,
413 .bDescriptorSubType = USB_CDC_ACM_TYPE,
414 .bmCapabilities = (1 << 1),
417 static const struct usb_cdc_union_desc gs_union_desc = {
418 .bLength = sizeof(gs_union_desc),
419 .bDescriptorType = USB_DT_CS_INTERFACE,
420 .bDescriptorSubType = USB_CDC_UNION_TYPE,
421 .bMasterInterface0 = 0, /* index of control interface */
422 .bSlaveInterface0 = 1, /* index of data interface */
425 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
426 .bLength = USB_DT_ENDPOINT_SIZE,
427 .bDescriptorType = USB_DT_ENDPOINT,
428 .bEndpointAddress = USB_DIR_IN,
429 .bmAttributes = USB_ENDPOINT_XFER_INT,
430 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
431 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
434 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
435 .bLength = USB_DT_ENDPOINT_SIZE,
436 .bDescriptorType = USB_DT_ENDPOINT,
437 .bEndpointAddress = USB_DIR_IN,
438 .bmAttributes = USB_ENDPOINT_XFER_BULK,
441 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
442 .bLength = USB_DT_ENDPOINT_SIZE,
443 .bDescriptorType = USB_DT_ENDPOINT,
444 .bEndpointAddress = USB_DIR_OUT,
445 .bmAttributes = USB_ENDPOINT_XFER_BULK,
448 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
449 (struct usb_descriptor_header *) &gs_otg_descriptor,
450 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
451 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
452 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
456 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
457 (struct usb_descriptor_header *) &gs_otg_descriptor,
458 (struct usb_descriptor_header *) &gs_control_interface_desc,
459 (struct usb_descriptor_header *) &gs_header_desc,
460 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
461 (struct usb_descriptor_header *) &gs_acm_descriptor,
462 (struct usb_descriptor_header *) &gs_union_desc,
463 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
464 (struct usb_descriptor_header *) &gs_data_interface_desc,
465 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
466 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
470 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
471 .bLength = USB_DT_ENDPOINT_SIZE,
472 .bDescriptorType = USB_DT_ENDPOINT,
473 .bEndpointAddress = USB_DIR_IN,
474 .bmAttributes = USB_ENDPOINT_XFER_INT,
475 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
476 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
479 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
480 .bLength = USB_DT_ENDPOINT_SIZE,
481 .bDescriptorType = USB_DT_ENDPOINT,
482 .bmAttributes = USB_ENDPOINT_XFER_BULK,
483 .wMaxPacketSize = __constant_cpu_to_le16(512),
486 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
487 .bLength = USB_DT_ENDPOINT_SIZE,
488 .bDescriptorType = USB_DT_ENDPOINT,
489 .bmAttributes = USB_ENDPOINT_XFER_BULK,
490 .wMaxPacketSize = __constant_cpu_to_le16(512),
493 static struct usb_qualifier_descriptor gs_qualifier_desc = {
494 .bLength = sizeof(struct usb_qualifier_descriptor),
495 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
496 .bcdUSB = __constant_cpu_to_le16 (0x0200),
497 /* assumes ep0 uses the same value for both speeds ... */
498 .bNumConfigurations = GS_NUM_CONFIGS,
501 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
502 (struct usb_descriptor_header *) &gs_otg_descriptor,
503 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
504 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
505 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
509 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
510 (struct usb_descriptor_header *) &gs_otg_descriptor,
511 (struct usb_descriptor_header *) &gs_control_interface_desc,
512 (struct usb_descriptor_header *) &gs_header_desc,
513 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
514 (struct usb_descriptor_header *) &gs_acm_descriptor,
515 (struct usb_descriptor_header *) &gs_union_desc,
516 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
517 (struct usb_descriptor_header *) &gs_data_interface_desc,
518 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
519 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
525 MODULE_DESCRIPTION(GS_LONG_NAME);
526 MODULE_AUTHOR("Al Borchers");
527 MODULE_LICENSE("GPL");
530 module_param(debug, int, S_IRUGO|S_IWUSR);
531 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
534 module_param(read_q_size, uint, S_IRUGO);
535 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
537 module_param(write_q_size, uint, S_IRUGO);
538 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
540 module_param(write_buf_size, uint, S_IRUGO);
541 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
543 module_param(use_acm, uint, S_IRUGO);
544 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
546 module_init(gs_module_init);
547 module_exit(gs_module_exit);
552 * Register as a USB gadget driver and a tty driver.
554 static int __init gs_module_init(void)
559 retval = usb_gadget_register_driver(&gs_gadget_driver);
561 pr_err("gs_module_init: cannot register gadget driver, "
566 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
569 gs_tty_driver->owner = THIS_MODULE;
570 gs_tty_driver->driver_name = GS_SHORT_NAME;
571 gs_tty_driver->name = "ttygs";
572 gs_tty_driver->major = GS_MAJOR;
573 gs_tty_driver->minor_start = GS_MINOR_START;
574 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
575 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
576 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
577 gs_tty_driver->init_termios = tty_std_termios;
578 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
579 tty_set_operations(gs_tty_driver, &gs_tty_ops);
581 for (i=0; i < GS_NUM_PORTS; i++)
582 mutex_init(&gs_open_close_lock[i]);
584 retval = tty_register_driver(gs_tty_driver);
586 usb_gadget_unregister_driver(&gs_gadget_driver);
587 put_tty_driver(gs_tty_driver);
588 pr_err("gs_module_init: cannot register tty driver, "
593 pr_info("gs_module_init: %s %s loaded\n",
594 GS_LONG_NAME, GS_VERSION_STR);
601 * Unregister as a tty driver and a USB gadget driver.
603 static void __exit gs_module_exit(void)
605 tty_unregister_driver(gs_tty_driver);
606 put_tty_driver(gs_tty_driver);
607 usb_gadget_unregister_driver(&gs_gadget_driver);
609 pr_info("gs_module_exit: %s %s unloaded\n",
610 GS_LONG_NAME, GS_VERSION_STR);
618 static int gs_open(struct tty_struct *tty, struct file *file)
622 struct gs_port *port;
628 port_num = tty->index;
630 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
632 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
633 pr_err("gs_open: (%d,%p,%p) invalid port number\n",
634 port_num, tty, file);
641 pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
642 port_num, tty, file);
646 mtx = &gs_open_close_lock[port_num];
647 if (mutex_lock_interruptible(mtx)) {
648 pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n",
649 port_num, tty, file);
653 spin_lock_irqsave(&dev->dev_lock, flags);
655 if (dev->dev_config == GS_NO_CONFIG_ID) {
656 pr_err("gs_open: (%d,%p,%p) device is not connected\n",
657 port_num, tty, file);
659 goto exit_unlock_dev;
662 port = dev->dev_port[port_num];
665 pr_err("gs_open: (%d,%p,%p) NULL port pointer\n",
666 port_num, tty, file);
668 goto exit_unlock_dev;
671 spin_lock(&port->port_lock);
672 spin_unlock(&dev->dev_lock);
674 if (port->port_dev == NULL) {
675 pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n",
676 port_num, tty, file);
678 goto exit_unlock_port;
681 if (port->port_open_count > 0) {
682 ++port->port_open_count;
683 gs_debug("gs_open: (%d,%p,%p) already open\n",
684 port_num, tty, file);
686 goto exit_unlock_port;
689 tty->driver_data = NULL;
691 /* mark port as in use, we can drop port lock and sleep if necessary */
692 port->port_in_use = 1;
694 /* allocate write buffer on first open */
695 if (port->port_write_buf == NULL) {
696 spin_unlock_irqrestore(&port->port_lock, flags);
697 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
698 spin_lock_irqsave(&port->port_lock, flags);
700 /* might have been disconnected while asleep, check */
701 if (port->port_dev == NULL) {
702 pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n",
703 port_num, tty, file);
704 port->port_in_use = 0;
706 goto exit_unlock_port;
709 if ((port->port_write_buf=buf) == NULL) {
710 pr_err("gs_open: (%d,%p,%p) cannot allocate "
711 "port write buffer\n",
712 port_num, tty, file);
713 port->port_in_use = 0;
715 goto exit_unlock_port;
720 /* wait for carrier detect (not implemented) */
722 /* might have been disconnected while asleep, check */
723 if (port->port_dev == NULL) {
724 pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n",
725 port_num, tty, file);
726 port->port_in_use = 0;
728 goto exit_unlock_port;
731 tty->driver_data = port;
732 port->port_tty = tty;
733 port->port_open_count = 1;
734 port->port_in_use = 0;
736 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
741 spin_unlock_irqrestore(&port->port_lock, flags);
746 spin_unlock_irqrestore(&dev->dev_lock, flags);
756 #define GS_WRITE_FINISHED_EVENT_SAFELY(p) \
760 spin_lock_irq(&(p)->port_lock); \
761 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \
762 spin_unlock_irq(&(p)->port_lock); \
766 static void gs_close(struct tty_struct *tty, struct file *file)
768 struct gs_port *port = tty->driver_data;
772 pr_err("gs_close: NULL port pointer\n");
776 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
778 mtx = &gs_open_close_lock[port->port_num];
781 spin_lock_irq(&port->port_lock);
783 if (port->port_open_count == 0) {
784 pr_err("gs_close: (%d,%p,%p) port is already closed\n",
785 port->port_num, tty, file);
789 if (port->port_open_count > 1) {
790 --port->port_open_count;
794 /* free disconnected port on final close */
795 if (port->port_dev == NULL) {
800 /* mark port as closed but in use, we can drop port lock */
801 /* and sleep if necessary */
802 port->port_in_use = 1;
803 port->port_open_count = 0;
805 /* wait for write buffer to drain, or */
806 /* at most GS_CLOSE_TIMEOUT seconds */
807 if (gs_buf_data_avail(port->port_write_buf) > 0) {
808 spin_unlock_irq(&port->port_lock);
809 wait_event_interruptible_timeout(port->port_write_wait,
810 GS_WRITE_FINISHED_EVENT_SAFELY(port),
811 GS_CLOSE_TIMEOUT * HZ);
812 spin_lock_irq(&port->port_lock);
815 /* free disconnected port on final close */
816 /* (might have happened during the above sleep) */
817 if (port->port_dev == NULL) {
822 gs_buf_clear(port->port_write_buf);
824 tty->driver_data = NULL;
825 port->port_tty = NULL;
826 port->port_in_use = 0;
828 gs_debug("gs_close: (%d,%p,%p) completed\n",
829 port->port_num, tty, file);
832 spin_unlock_irq(&port->port_lock);
839 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
842 struct gs_port *port = tty->driver_data;
846 pr_err("gs_write: NULL port pointer\n");
850 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
856 spin_lock_irqsave(&port->port_lock, flags);
858 if (port->port_dev == NULL) {
859 pr_err("gs_write: (%d,%p) port is not connected\n",
860 port->port_num, tty);
865 if (port->port_open_count == 0) {
866 pr_err("gs_write: (%d,%p) port is closed\n",
867 port->port_num, tty);
872 count = gs_buf_put(port->port_write_buf, buf, count);
874 spin_unlock_irqrestore(&port->port_lock, flags);
878 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
884 spin_unlock_irqrestore(&port->port_lock, flags);
891 static int gs_put_char(struct tty_struct *tty, unsigned char ch)
894 struct gs_port *port = tty->driver_data;
898 pr_err("gs_put_char: NULL port pointer\n");
902 gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
903 port->port_num, tty, ch, __builtin_return_address(0));
905 spin_lock_irqsave(&port->port_lock, flags);
907 if (port->port_dev == NULL) {
908 pr_err("gs_put_char: (%d,%p) port is not connected\n",
909 port->port_num, tty);
913 if (port->port_open_count == 0) {
914 pr_err("gs_put_char: (%d,%p) port is closed\n",
915 port->port_num, tty);
919 ret = gs_buf_put(port->port_write_buf, &ch, 1);
922 spin_unlock_irqrestore(&port->port_lock, flags);
929 static void gs_flush_chars(struct tty_struct *tty)
932 struct gs_port *port = tty->driver_data;
935 pr_err("gs_flush_chars: NULL port pointer\n");
939 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
941 spin_lock_irqsave(&port->port_lock, flags);
943 if (port->port_dev == NULL) {
944 pr_err("gs_flush_chars: (%d,%p) port is not connected\n",
945 port->port_num, tty);
949 if (port->port_open_count == 0) {
950 pr_err("gs_flush_chars: (%d,%p) port is closed\n",
951 port->port_num, tty);
955 spin_unlock_irqrestore(&port->port_lock, flags);
962 spin_unlock_irqrestore(&port->port_lock, flags);
968 static int gs_write_room(struct tty_struct *tty)
973 struct gs_port *port = tty->driver_data;
979 spin_lock_irqsave(&port->port_lock, flags);
981 if (port->port_dev != NULL && port->port_open_count > 0
982 && port->port_write_buf != NULL)
983 room = gs_buf_space_avail(port->port_write_buf);
985 spin_unlock_irqrestore(&port->port_lock, flags);
987 gs_debug("gs_write_room: (%d,%p) room=%d\n",
988 port->port_num, tty, room);
996 static int gs_chars_in_buffer(struct tty_struct *tty)
1000 struct gs_port *port = tty->driver_data;
1005 spin_lock_irqsave(&port->port_lock, flags);
1007 if (port->port_dev != NULL && port->port_open_count > 0
1008 && port->port_write_buf != NULL)
1009 chars = gs_buf_data_avail(port->port_write_buf);
1011 spin_unlock_irqrestore(&port->port_lock, flags);
1013 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1014 port->port_num, tty, chars);
1022 static void gs_throttle(struct tty_struct *tty)
1029 static void gs_unthrottle(struct tty_struct *tty)
1036 static void gs_break(struct tty_struct *tty, int break_state)
1043 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1045 struct gs_port *port = tty->driver_data;
1048 pr_err("gs_ioctl: NULL port pointer\n");
1052 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1053 port->port_num, tty, file, cmd, arg);
1057 /* could not handle ioctl */
1058 return -ENOIOCTLCMD;
1064 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
1071 * This function finds available write requests, calls
1072 * gs_send_packet to fill these packets with data, and
1073 * continues until either there are no more write requests
1074 * available or no more data to send. This function is
1075 * run whenever data arrives or write requests are available.
1077 static int gs_send(struct gs_dev *dev)
1080 unsigned long flags;
1082 struct usb_request *req;
1083 struct gs_req_entry *req_entry;
1086 pr_err("gs_send: NULL device pointer\n");
1090 spin_lock_irqsave(&dev->dev_lock, flags);
1092 ep = dev->dev_in_ep;
1094 while(!list_empty(&dev->dev_req_list)) {
1096 req_entry = list_entry(dev->dev_req_list.next,
1097 struct gs_req_entry, re_entry);
1099 req = req_entry->re_req;
1101 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1104 gs_debug_level(3, "gs_send: len=%d, 0x%2.2x "
1105 "0x%2.2x 0x%2.2x ...\n", len,
1106 *((unsigned char *)req->buf),
1107 *((unsigned char *)req->buf+1),
1108 *((unsigned char *)req->buf+2));
1109 list_del(&req_entry->re_entry);
1111 spin_unlock_irqrestore(&dev->dev_lock, flags);
1112 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1114 "gs_send: cannot queue read request, ret=%d\n",
1116 spin_lock_irqsave(&dev->dev_lock, flags);
1119 spin_lock_irqsave(&dev->dev_lock, flags);
1126 spin_unlock_irqrestore(&dev->dev_lock, flags);
1134 * If there is data to send, a packet is built in the given
1135 * buffer and the size is returned. If there is no data to
1136 * send, 0 is returned. If there is any error a negative
1137 * error number is returned.
1139 * Called during USB completion routine, on interrupt time.
1141 * We assume that disconnect will not happen until all completion
1142 * routines have completed, so we can assume that the dev_port
1143 * array does not change during the lifetime of this function.
1145 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1148 struct gs_port *port;
1150 /* TEMPORARY -- only port 0 is supported right now */
1151 port = dev->dev_port[0];
1154 pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
1158 spin_lock(&port->port_lock);
1160 len = gs_buf_data_avail(port->port_write_buf);
1167 size = gs_buf_get(port->port_write_buf, packet, size);
1170 wake_up_interruptible(&port->port_tty->write_wait);
1173 spin_unlock(&port->port_lock);
1180 * Called for each USB packet received. Reads the packet
1181 * header and stuffs the data in the appropriate tty buffer.
1182 * Returns 0 if successful, or a negative error number.
1184 * Called during USB completion routine, on interrupt time.
1186 * We assume that disconnect will not happen until all completion
1187 * routines have completed, so we can assume that the dev_port
1188 * array does not change during the lifetime of this function.
1190 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1193 struct gs_port *port;
1195 struct tty_struct *tty;
1197 /* TEMPORARY -- only port 0 is supported right now */
1198 port = dev->dev_port[0];
1201 pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
1206 spin_lock(&port->port_lock);
1208 if (port->port_open_count == 0) {
1209 pr_err("gs_recv_packet: port=%d, port is closed\n",
1216 tty = port->port_tty;
1219 pr_err("gs_recv_packet: port=%d, NULL tty pointer\n",
1225 if (port->port_tty->magic != TTY_MAGIC) {
1226 pr_err("gs_recv_packet: port=%d, bad tty magic\n",
1232 len = tty_buffer_request_room(tty, size);
1234 tty_insert_flip_string(tty, packet, len);
1235 tty_flip_buffer_push(port->port_tty);
1236 wake_up_interruptible(&port->port_tty->read_wait);
1240 spin_unlock(&port->port_lock);
1247 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1250 struct gs_dev *dev = ep->driver_data;
1253 pr_err("gs_read_complete: NULL device pointer\n");
1257 switch(req->status) {
1259 /* normal completion */
1260 gs_recv_packet(dev, req->buf, req->actual);
1262 req->length = ep->maxpacket;
1263 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1265 "gs_read_complete: cannot queue read request, ret=%d\n",
1272 gs_debug("gs_read_complete: shutdown\n");
1273 gs_free_req(ep, req);
1279 "gs_read_complete: unexpected status error, status=%d\n",
1289 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1291 struct gs_dev *dev = ep->driver_data;
1292 struct gs_req_entry *gs_req = req->context;
1295 pr_err("gs_write_complete: NULL device pointer\n");
1299 switch(req->status) {
1301 /* normal completion */
1303 if (gs_req == NULL) {
1304 pr_err("gs_write_complete: NULL request pointer\n");
1308 spin_lock(&dev->dev_lock);
1309 list_add(&gs_req->re_entry, &dev->dev_req_list);
1310 spin_unlock(&dev->dev_lock);
1318 gs_debug("gs_write_complete: shutdown\n");
1319 gs_free_req(ep, req);
1324 "gs_write_complete: unexpected status error, status=%d\n",
1336 * Called on module load. Allocates and initializes the device
1337 * structure and a control request.
1339 static int __init gs_bind(struct usb_gadget *gadget)
1346 /* Some controllers can't support CDC ACM:
1347 * - sh doesn't support multiple interfaces or configs;
1348 * - sa1100 doesn't have a third interrupt endpoint
1350 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1353 gcnum = usb_gadget_controller_number(gadget);
1355 gs_device_desc.bcdDevice =
1356 cpu_to_le16(GS_VERSION_NUM | gcnum);
1358 pr_warning("gs_bind: controller '%s' not recognized\n",
1360 /* unrecognized, but safe unless bulk is REALLY quirky */
1361 gs_device_desc.bcdDevice =
1362 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1365 usb_ep_autoconfig_reset(gadget);
1367 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1370 EP_IN_NAME = ep->name;
1371 ep->driver_data = ep; /* claim the endpoint */
1373 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1376 EP_OUT_NAME = ep->name;
1377 ep->driver_data = ep; /* claim the endpoint */
1380 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1382 pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
1385 gs_device_desc.idProduct = __constant_cpu_to_le16(
1387 EP_NOTIFY_NAME = ep->name;
1388 ep->driver_data = ep; /* claim the endpoint */
1391 gs_device_desc.bDeviceClass = use_acm
1392 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1393 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1395 if (gadget_is_dualspeed(gadget)) {
1396 gs_qualifier_desc.bDeviceClass = use_acm
1397 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1398 /* assume ep0 uses the same packet size for both speeds */
1399 gs_qualifier_desc.bMaxPacketSize0 =
1400 gs_device_desc.bMaxPacketSize0;
1401 /* assume endpoints are dual-speed */
1402 gs_highspeed_notify_desc.bEndpointAddress =
1403 gs_fullspeed_notify_desc.bEndpointAddress;
1404 gs_highspeed_in_desc.bEndpointAddress =
1405 gs_fullspeed_in_desc.bEndpointAddress;
1406 gs_highspeed_out_desc.bEndpointAddress =
1407 gs_fullspeed_out_desc.bEndpointAddress;
1410 usb_gadget_set_selfpowered(gadget);
1412 if (gadget_is_otg(gadget)) {
1413 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1414 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1415 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1418 gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1422 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1423 init_utsname()->sysname, init_utsname()->release,
1426 dev->dev_gadget = gadget;
1427 spin_lock_init(&dev->dev_lock);
1428 INIT_LIST_HEAD(&dev->dev_req_list);
1429 set_gadget_data(gadget, dev);
1431 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1432 pr_err("gs_bind: cannot allocate ports\n");
1437 /* preallocate control response and buffer */
1438 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1440 if (dev->dev_ctrl_req == NULL) {
1444 dev->dev_ctrl_req->complete = gs_setup_complete;
1446 gadget->ep0->driver_data = dev;
1448 pr_info("gs_bind: %s %s bound\n",
1449 GS_LONG_NAME, GS_VERSION_STR);
1454 pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
1461 * Called on module unload. Frees the control request and device
1464 static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1466 struct gs_dev *dev = get_gadget_data(gadget);
1470 /* read/write requests already freed, only control request remains */
1472 if (dev->dev_ctrl_req != NULL) {
1473 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1474 dev->dev_ctrl_req = NULL;
1477 if (dev->dev_notify_ep)
1478 usb_ep_disable(dev->dev_notify_ep);
1480 usb_ep_disable(dev->dev_in_ep);
1481 if (dev->dev_out_ep)
1482 usb_ep_disable(dev->dev_out_ep);
1484 set_gadget_data(gadget, NULL);
1487 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1494 * Implements all the control endpoint functionality that's not
1495 * handled in hardware or the hardware driver.
1497 * Returns the size of the data sent to the host, or a negative
1500 static int gs_setup(struct usb_gadget *gadget,
1501 const struct usb_ctrlrequest *ctrl)
1503 int ret = -EOPNOTSUPP;
1504 struct gs_dev *dev = get_gadget_data(gadget);
1505 struct usb_request *req = dev->dev_ctrl_req;
1506 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1507 u16 wValue = le16_to_cpu(ctrl->wValue);
1508 u16 wLength = le16_to_cpu(ctrl->wLength);
1510 req->complete = gs_setup_complete;
1512 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1513 case USB_TYPE_STANDARD:
1514 ret = gs_setup_standard(gadget,ctrl);
1517 case USB_TYPE_CLASS:
1518 ret = gs_setup_class(gadget,ctrl);
1522 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1523 "value=%04x, index=%04x, length=%d\n",
1524 ctrl->bRequestType, ctrl->bRequest,
1525 wValue, wIndex, wLength);
1529 /* respond with data transfer before status phase? */
1532 req->zero = ret < wLength
1533 && (ret % gadget->ep0->maxpacket) == 0;
1534 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1536 pr_err("gs_setup: cannot queue response, ret=%d\n",
1539 gs_setup_complete(gadget->ep0, req);
1543 /* device either stalls (ret < 0) or reports success */
1547 static int gs_setup_standard(struct usb_gadget *gadget,
1548 const struct usb_ctrlrequest *ctrl)
1550 int ret = -EOPNOTSUPP;
1551 struct gs_dev *dev = get_gadget_data(gadget);
1552 struct usb_request *req = dev->dev_ctrl_req;
1553 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1554 u16 wValue = le16_to_cpu(ctrl->wValue);
1555 u16 wLength = le16_to_cpu(ctrl->wLength);
1557 switch (ctrl->bRequest) {
1558 case USB_REQ_GET_DESCRIPTOR:
1559 if (ctrl->bRequestType != USB_DIR_IN)
1562 switch (wValue >> 8) {
1565 (u16)sizeof(struct usb_device_descriptor));
1566 memcpy(req->buf, &gs_device_desc, ret);
1569 case USB_DT_DEVICE_QUALIFIER:
1570 if (!gadget_is_dualspeed(gadget))
1573 (u16)sizeof(struct usb_qualifier_descriptor));
1574 memcpy(req->buf, &gs_qualifier_desc, ret);
1577 case USB_DT_OTHER_SPEED_CONFIG:
1578 if (!gadget_is_dualspeed(gadget))
1582 ret = gs_build_config_buf(req->buf, gadget,
1583 wValue >> 8, wValue & 0xff,
1584 gadget_is_otg(gadget));
1586 ret = min(wLength, (u16)ret);
1590 /* wIndex == language code. */
1591 ret = usb_gadget_get_string(&gs_string_table,
1592 wValue & 0xff, req->buf);
1594 ret = min(wLength, (u16)ret);
1599 case USB_REQ_SET_CONFIGURATION:
1600 if (ctrl->bRequestType != 0)
1602 spin_lock(&dev->dev_lock);
1603 ret = gs_set_config(dev, wValue);
1604 spin_unlock(&dev->dev_lock);
1607 case USB_REQ_GET_CONFIGURATION:
1608 if (ctrl->bRequestType != USB_DIR_IN)
1610 *(u8 *)req->buf = dev->dev_config;
1611 ret = min(wLength, (u16)1);
1614 case USB_REQ_SET_INTERFACE:
1615 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1617 || wIndex >= GS_MAX_NUM_INTERFACES)
1619 if (dev->dev_config == GS_BULK_CONFIG_ID
1620 && wIndex != GS_BULK_INTERFACE_ID)
1622 /* no alternate interface settings */
1625 spin_lock(&dev->dev_lock);
1626 /* PXA hardware partially handles SET_INTERFACE;
1627 * we need to kluge around that interference. */
1628 if (gadget_is_pxa(gadget)) {
1629 ret = gs_set_config(dev, use_acm ?
1630 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1631 goto set_interface_done;
1633 if (dev->dev_config != GS_BULK_CONFIG_ID
1634 && wIndex == GS_CONTROL_INTERFACE_ID) {
1635 if (dev->dev_notify_ep) {
1636 usb_ep_disable(dev->dev_notify_ep);
1637 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1640 usb_ep_disable(dev->dev_in_ep);
1641 usb_ep_disable(dev->dev_out_ep);
1642 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1643 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1647 spin_unlock(&dev->dev_lock);
1650 case USB_REQ_GET_INTERFACE:
1651 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1652 || dev->dev_config == GS_NO_CONFIG_ID)
1654 if (wIndex >= GS_MAX_NUM_INTERFACES
1655 || (dev->dev_config == GS_BULK_CONFIG_ID
1656 && wIndex != GS_BULK_INTERFACE_ID)) {
1660 /* no alternate interface settings */
1661 *(u8 *)req->buf = 0;
1662 ret = min(wLength, (u16)1);
1666 pr_err("gs_setup: unknown standard request, type=%02x, "
1667 "request=%02x, value=%04x, index=%04x, length=%d\n",
1668 ctrl->bRequestType, ctrl->bRequest,
1669 wValue, wIndex, wLength);
1676 static int gs_setup_class(struct usb_gadget *gadget,
1677 const struct usb_ctrlrequest *ctrl)
1679 int ret = -EOPNOTSUPP;
1680 struct gs_dev *dev = get_gadget_data(gadget);
1681 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1682 struct usb_request *req = dev->dev_ctrl_req;
1683 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1684 u16 wValue = le16_to_cpu(ctrl->wValue);
1685 u16 wLength = le16_to_cpu(ctrl->wLength);
1687 switch (ctrl->bRequest) {
1688 case USB_CDC_REQ_SET_LINE_CODING:
1689 if (wLength != sizeof(struct usb_cdc_line_coding))
1692 req->complete = gs_setup_complete_set_line_coding;
1695 case USB_CDC_REQ_GET_LINE_CODING:
1696 ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
1698 spin_lock(&port->port_lock);
1699 memcpy(req->buf, &port->port_line_coding, ret);
1700 spin_unlock(&port->port_lock);
1704 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1709 /* REVISIT: we currently just remember this data.
1710 * If we change that, update whatever hardware needs
1713 spin_lock(&port->port_lock);
1714 port->port_handshake_bits = wValue;
1715 spin_unlock(&port->port_lock);
1720 /* NOTE: strictly speaking, we should accept AT-commands
1721 * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE.
1722 * But our call management descriptor says we don't handle
1723 * call management, so we should be able to get by without
1724 * handling those "required" commands (except by stalling).
1726 pr_err("gs_setup: unknown class request, "
1727 "type=%02x, request=%02x, value=%04x, "
1728 "index=%04x, length=%d\n",
1729 ctrl->bRequestType, ctrl->bRequest,
1730 wValue, wIndex, wLength);
1737 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
1738 struct usb_request *req)
1740 struct gs_dev *dev = ep->driver_data;
1741 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1743 switch (req->status) {
1745 /* normal completion */
1746 if (req->actual != sizeof(port->port_line_coding))
1747 usb_ep_set_halt(ep);
1749 struct usb_cdc_line_coding *value = req->buf;
1751 /* REVISIT: we currently just remember this data.
1752 * If we change that, (a) validate it first, then
1753 * (b) update whatever hardware needs updating.
1755 spin_lock(&port->port_lock);
1756 port->port_line_coding = *value;
1757 spin_unlock(&port->port_lock);
1763 gs_free_req(ep, req);
1776 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1778 if (req->status || req->actual != req->length) {
1779 pr_err("gs_setup_complete: status error, status=%d, "
1780 "actual=%d, length=%d\n",
1781 req->status, req->actual, req->length);
1788 * Called when the device is disconnected. Frees the closed
1789 * ports and disconnects open ports. Open ports will be freed
1790 * on close. Then reallocates the ports for the next connection.
1792 static void gs_disconnect(struct usb_gadget *gadget)
1794 unsigned long flags;
1795 struct gs_dev *dev = get_gadget_data(gadget);
1797 spin_lock_irqsave(&dev->dev_lock, flags);
1799 gs_reset_config(dev);
1801 /* free closed ports and disconnect open ports */
1802 /* (open ports will be freed when closed) */
1805 /* re-allocate ports for the next connection */
1806 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1807 pr_err("gs_disconnect: cannot re-allocate ports\n");
1809 spin_unlock_irqrestore(&dev->dev_lock, flags);
1811 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1817 * Configures the device by enabling device specific
1818 * optimizations, setting up the endpoints, allocating
1819 * read and write requests and queuing read requests.
1821 * The device lock must be held when calling this function.
1823 static int gs_set_config(struct gs_dev *dev, unsigned config)
1827 struct usb_gadget *gadget = dev->dev_gadget;
1829 struct usb_endpoint_descriptor *ep_desc;
1830 struct usb_request *req;
1831 struct gs_req_entry *req_entry;
1834 pr_err("gs_set_config: NULL device pointer\n");
1838 if (config == dev->dev_config)
1841 gs_reset_config(dev);
1844 case GS_NO_CONFIG_ID:
1846 case GS_BULK_CONFIG_ID:
1849 /* device specific optimizations */
1850 if (gadget_is_net2280(gadget))
1851 net2280_set_fifo_mode(gadget, 1);
1853 case GS_ACM_CONFIG_ID:
1856 /* device specific optimizations */
1857 if (gadget_is_net2280(gadget))
1858 net2280_set_fifo_mode(gadget, 1);
1864 dev->dev_config = config;
1866 gadget_for_each_ep(ep, gadget) {
1869 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1870 ep_desc = choose_ep_desc(gadget,
1871 &gs_highspeed_notify_desc,
1872 &gs_fullspeed_notify_desc);
1873 ret = usb_ep_enable(ep,ep_desc);
1875 ep->driver_data = dev;
1876 dev->dev_notify_ep = ep;
1877 dev->dev_notify_ep_desc = ep_desc;
1879 pr_err("gs_set_config: cannot enable NOTIFY "
1880 "endpoint %s, ret=%d\n",
1882 goto exit_reset_config;
1886 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1887 ep_desc = choose_ep_desc(gadget,
1888 &gs_highspeed_in_desc,
1889 &gs_fullspeed_in_desc);
1890 ret = usb_ep_enable(ep,ep_desc);
1892 ep->driver_data = dev;
1893 dev->dev_in_ep = ep;
1894 dev->dev_in_ep_desc = ep_desc;
1896 pr_err("gs_set_config: cannot enable IN "
1897 "endpoint %s, ret=%d\n",
1899 goto exit_reset_config;
1903 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1904 ep_desc = choose_ep_desc(gadget,
1905 &gs_highspeed_out_desc,
1906 &gs_fullspeed_out_desc);
1907 ret = usb_ep_enable(ep,ep_desc);
1909 ep->driver_data = dev;
1910 dev->dev_out_ep = ep;
1911 dev->dev_out_ep_desc = ep_desc;
1913 pr_err("gs_set_config: cannot enable OUT "
1914 "endpoint %s, ret=%d\n",
1916 goto exit_reset_config;
1922 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
1923 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1924 pr_err("gs_set_config: cannot find endpoints\n");
1926 goto exit_reset_config;
1929 /* allocate and queue read requests */
1930 ep = dev->dev_out_ep;
1931 for (i=0; i<read_q_size && ret == 0; i++) {
1932 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1933 req->complete = gs_read_complete;
1934 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1935 pr_err("gs_set_config: cannot queue read "
1936 "request, ret=%d\n", ret);
1939 pr_err("gs_set_config: cannot allocate "
1942 goto exit_reset_config;
1946 /* allocate write requests, and put on free list */
1947 ep = dev->dev_in_ep;
1948 for (i=0; i<write_q_size; i++) {
1949 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1950 req_entry->re_req->complete = gs_write_complete;
1951 list_add(&req_entry->re_entry, &dev->dev_req_list);
1953 pr_err("gs_set_config: cannot allocate "
1954 "write requests\n");
1956 goto exit_reset_config;
1960 /* REVISIT the ACM mode should be able to actually *issue* some
1961 * notifications, for at least serial state change events if
1962 * not also for network connection; say so in bmCapabilities.
1965 pr_info("gs_set_config: %s configured, %s speed %s config\n",
1967 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1968 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1973 gs_reset_config(dev);
1980 * Mark the device as not configured, disable all endpoints,
1981 * which forces completion of pending I/O and frees queued
1982 * requests, and free the remaining write requests on the
1985 * The device lock must be held when calling this function.
1987 static void gs_reset_config(struct gs_dev *dev)
1989 struct gs_req_entry *req_entry;
1992 pr_err("gs_reset_config: NULL device pointer\n");
1996 if (dev->dev_config == GS_NO_CONFIG_ID)
1999 dev->dev_config = GS_NO_CONFIG_ID;
2001 /* free write requests on the free list */
2002 while(!list_empty(&dev->dev_req_list)) {
2003 req_entry = list_entry(dev->dev_req_list.next,
2004 struct gs_req_entry, re_entry);
2005 list_del(&req_entry->re_entry);
2006 gs_free_req_entry(dev->dev_in_ep, req_entry);
2009 /* disable endpoints, forcing completion of pending i/o; */
2010 /* completion handlers free their requests in this case */
2011 if (dev->dev_notify_ep) {
2012 usb_ep_disable(dev->dev_notify_ep);
2013 dev->dev_notify_ep = NULL;
2015 if (dev->dev_in_ep) {
2016 usb_ep_disable(dev->dev_in_ep);
2017 dev->dev_in_ep = NULL;
2019 if (dev->dev_out_ep) {
2020 usb_ep_disable(dev->dev_out_ep);
2021 dev->dev_out_ep = NULL;
2026 * gs_build_config_buf
2028 * Builds the config descriptors in the given buffer and returns the
2029 * length, or a negative error number.
2031 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
2032 u8 type, unsigned int index, int is_otg)
2036 const struct usb_config_descriptor *config_desc;
2037 const struct usb_descriptor_header **function;
2039 if (index >= gs_device_desc.bNumConfigurations)
2042 /* other speed switches high and full speed */
2043 if (gadget_is_dualspeed(g)) {
2044 high_speed = (g->speed == USB_SPEED_HIGH);
2045 if (type == USB_DT_OTHER_SPEED_CONFIG)
2046 high_speed = !high_speed;
2050 config_desc = &gs_acm_config_desc;
2051 function = high_speed
2052 ? gs_acm_highspeed_function
2053 : gs_acm_fullspeed_function;
2055 config_desc = &gs_bulk_config_desc;
2056 function = high_speed
2057 ? gs_bulk_highspeed_function
2058 : gs_bulk_fullspeed_function;
2061 /* for now, don't advertise srp-only devices */
2065 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2069 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2077 * Allocate a usb_request and its buffer. Returns a pointer to the
2078 * usb_request or NULL if there is an error.
2080 static struct usb_request *
2081 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
2083 struct usb_request *req;
2088 req = usb_ep_alloc_request(ep, kmalloc_flags);
2092 req->buf = kmalloc(len, kmalloc_flags);
2093 if (req->buf == NULL) {
2094 usb_ep_free_request(ep, req);
2105 * Free a usb_request and its buffer.
2107 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2109 if (ep != NULL && req != NULL) {
2111 usb_ep_free_request(ep, req);
2116 * gs_alloc_req_entry
2118 * Allocates a request and its buffer, using the given
2119 * endpoint, buffer len, and kmalloc flags.
2121 static struct gs_req_entry *
2122 gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2124 struct gs_req_entry *req;
2126 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2130 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2131 if (req->re_req == NULL) {
2136 req->re_req->context = req;
2144 * Frees a request and its buffer.
2146 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2148 if (ep != NULL && req != NULL) {
2149 if (req->re_req != NULL)
2150 gs_free_req(ep, req->re_req);
2158 * Allocate all ports and set the gs_dev struct to point to them.
2159 * Return 0 if successful, or a negative error number.
2161 * The device lock is normally held when calling this function.
2163 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2166 struct gs_port *port;
2171 for (i=0; i<GS_NUM_PORTS; i++) {
2172 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2175 port->port_dev = dev;
2177 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
2178 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2179 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2180 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2181 spin_lock_init(&port->port_lock);
2182 init_waitqueue_head(&port->port_write_wait);
2184 dev->dev_port[i] = port;
2193 * Free all closed ports. Open ports are disconnected by
2194 * freeing their write buffers, setting their device pointers
2195 * and the pointers to them in the device to NULL. These
2196 * ports will be freed when closed.
2198 * The device lock is normally held when calling this function.
2200 static void gs_free_ports(struct gs_dev *dev)
2203 unsigned long flags;
2204 struct gs_port *port;
2209 for (i=0; i<GS_NUM_PORTS; i++) {
2210 if ((port=dev->dev_port[i]) != NULL) {
2211 dev->dev_port[i] = NULL;
2213 spin_lock_irqsave(&port->port_lock, flags);
2215 if (port->port_write_buf != NULL) {
2216 gs_buf_free(port->port_write_buf);
2217 port->port_write_buf = NULL;
2220 if (port->port_open_count > 0 || port->port_in_use) {
2221 port->port_dev = NULL;
2222 wake_up_interruptible(&port->port_write_wait);
2223 if (port->port_tty) {
2224 tty_hangup(port->port_tty);
2226 spin_unlock_irqrestore(&port->port_lock, flags);
2228 spin_unlock_irqrestore(&port->port_lock, flags);
2236 /* Circular Buffer */
2241 * Allocate a circular buffer and all associated memory.
2243 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2250 gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2254 gb->buf_buf = kmalloc(size, kmalloc_flags);
2255 if (gb->buf_buf == NULL) {
2260 gb->buf_size = size;
2261 gb->buf_get = gb->buf_put = gb->buf_buf;
2269 * Free the buffer and all associated memory.
2271 static void gs_buf_free(struct gs_buf *gb)
2282 * Clear out all data in the circular buffer.
2284 static void gs_buf_clear(struct gs_buf *gb)
2287 gb->buf_get = gb->buf_put;
2288 /* equivalent to a get of all data available */
2294 * Return the number of bytes of data available in the circular
2297 static unsigned int gs_buf_data_avail(struct gs_buf *gb)
2300 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2306 * gs_buf_space_avail
2308 * Return the number of bytes of space available in the circular
2311 static unsigned int gs_buf_space_avail(struct gs_buf *gb)
2314 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2322 * Copy data data from a user buffer and put it into the circular buffer.
2323 * Restrict to the amount of space available.
2325 * Return the number of bytes copied.
2328 gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2335 len = gs_buf_space_avail(gb);
2342 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2344 memcpy(gb->buf_put, buf, len);
2345 memcpy(gb->buf_buf, buf+len, count - len);
2346 gb->buf_put = gb->buf_buf + count - len;
2348 memcpy(gb->buf_put, buf, count);
2350 gb->buf_put += count;
2351 else /* count == len */
2352 gb->buf_put = gb->buf_buf;
2361 * Get data from the circular buffer and copy to the given buffer.
2362 * Restrict to the amount of data available.
2364 * Return the number of bytes copied.
2367 gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2374 len = gs_buf_data_avail(gb);
2381 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2383 memcpy(buf, gb->buf_get, len);
2384 memcpy(buf+len, gb->buf_buf, count - len);
2385 gb->buf_get = gb->buf_buf + count - len;
2387 memcpy(buf, gb->buf_get, count);
2389 gb->buf_get += count;
2390 else /* count == len */
2391 gb->buf_get = gb->buf_buf;