2 * g_serial.c -- USB gadget serial driver
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
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/config.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/ioport.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/smp_lock.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/timer.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
33 #include <linux/utsname.h>
34 #include <linux/wait.h>
35 #include <linux/proc_fs.h>
36 #include <linux/device.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
40 #include <asm/byteorder.h>
43 #include <asm/system.h>
44 #include <asm/unaligned.h>
45 #include <asm/uaccess.h>
47 #include <linux/usb_ch9.h>
48 #include <linux/usb/cdc.h>
49 #include <linux/usb_gadget.h>
51 #include "gadget_chips.h"
56 #define GS_VERSION_STR "v2.2"
57 #define GS_VERSION_NUM 0x0202
59 #define GS_LONG_NAME "Gadget Serial"
60 #define GS_SHORT_NAME "g_serial"
63 #define GS_MINOR_START 0
65 #define GS_NUM_PORTS 16
67 #define GS_NUM_CONFIGS 1
68 #define GS_NO_CONFIG_ID 0
69 #define GS_BULK_CONFIG_ID 1
70 #define GS_ACM_CONFIG_ID 2
72 #define GS_MAX_NUM_INTERFACES 2
73 #define GS_BULK_INTERFACE_ID 0
74 #define GS_CONTROL_INTERFACE_ID 0
75 #define GS_DATA_INTERFACE_ID 1
77 #define GS_MAX_DESC_LEN 256
79 #define GS_DEFAULT_READ_Q_SIZE 32
80 #define GS_DEFAULT_WRITE_Q_SIZE 32
82 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
83 #define GS_TMP_BUF_SIZE 8192
85 #define GS_CLOSE_TIMEOUT 15
87 #define GS_DEFAULT_USE_ACM 0
89 #define GS_DEFAULT_DTE_RATE 9600
90 #define GS_DEFAULT_DATA_BITS 8
91 #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
92 #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
94 /* select highspeed/fullspeed, hiding highspeed if not configured */
95 #ifdef CONFIG_USB_GADGET_DUALSPEED
96 #define GS_SPEED_SELECT(is_hs,hs,fs) ((is_hs) ? (hs) : (fs))
98 #define GS_SPEED_SELECT(is_hs,hs,fs) (fs)
99 #endif /* CONFIG_USB_GADGET_DUALSPEED */
103 static int debug = 1;
105 #define gs_debug(format, arg...) \
106 do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
107 #define gs_debug_level(level, format, arg...) \
108 do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
112 #define gs_debug(format, arg...) \
114 #define gs_debug_level(level, format, arg...) \
117 #endif /* GS_DEBUG */
119 /* Thanks to NetChip Technologies for donating this product ID.
121 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
122 * Instead: allocate your own, using normal USB-IF procedures.
124 #define GS_VENDOR_ID 0x0525 /* NetChip */
125 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
126 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
128 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
129 #define GS_NOTIFY_MAXPACKET 8
136 /* circular buffer */
138 unsigned int buf_size;
144 /* list of requests */
145 struct gs_req_entry {
146 struct list_head re_entry;
147 struct usb_request *re_req;
150 /* the port structure holds info for each port, one for each minor number */
152 struct gs_dev *port_dev; /* pointer to device struct */
153 struct tty_struct *port_tty; /* pointer to tty struct */
154 spinlock_t port_lock;
157 int port_in_use; /* open/close in progress */
158 wait_queue_head_t port_write_wait;/* waiting to write */
159 struct gs_buf *port_write_buf;
160 struct usb_cdc_line_coding port_line_coding;
163 /* the device structure holds info for the USB device */
165 struct usb_gadget *dev_gadget; /* gadget device pointer */
166 spinlock_t dev_lock; /* lock for set/reset config */
167 int dev_config; /* configuration number */
168 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
169 struct usb_ep *dev_in_ep; /* address of in endpoint */
170 struct usb_ep *dev_out_ep; /* address of out endpoint */
171 struct usb_endpoint_descriptor /* descriptor of notify ep */
173 struct usb_endpoint_descriptor /* descriptor of in endpoint */
175 struct usb_endpoint_descriptor /* descriptor of out endpoint */
177 struct usb_request *dev_ctrl_req; /* control request */
178 struct list_head dev_req_list; /* list of write requests */
179 int dev_sched_port; /* round robin port scheduled */
180 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
187 static int __init gs_module_init(void);
188 static void __exit gs_module_exit(void);
191 static int gs_open(struct tty_struct *tty, struct file *file);
192 static void gs_close(struct tty_struct *tty, struct file *file);
193 static int gs_write(struct tty_struct *tty,
194 const unsigned char *buf, int count);
195 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
196 static void gs_flush_chars(struct tty_struct *tty);
197 static int gs_write_room(struct tty_struct *tty);
198 static int gs_chars_in_buffer(struct tty_struct *tty);
199 static void gs_throttle(struct tty_struct * tty);
200 static void gs_unthrottle(struct tty_struct * tty);
201 static void gs_break(struct tty_struct *tty, int break_state);
202 static int gs_ioctl(struct tty_struct *tty, struct file *file,
203 unsigned int cmd, unsigned long arg);
204 static void gs_set_termios(struct tty_struct *tty, struct termios *old);
206 static int gs_send(struct gs_dev *dev);
207 static int gs_send_packet(struct gs_dev *dev, char *packet,
209 static int gs_recv_packet(struct gs_dev *dev, char *packet,
211 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
212 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
215 static int gs_bind(struct usb_gadget *gadget);
216 static void gs_unbind(struct usb_gadget *gadget);
217 static int gs_setup(struct usb_gadget *gadget,
218 const struct usb_ctrlrequest *ctrl);
219 static int gs_setup_standard(struct usb_gadget *gadget,
220 const struct usb_ctrlrequest *ctrl);
221 static int gs_setup_class(struct usb_gadget *gadget,
222 const struct usb_ctrlrequest *ctrl);
223 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
224 static void gs_disconnect(struct usb_gadget *gadget);
225 static int gs_set_config(struct gs_dev *dev, unsigned config);
226 static void gs_reset_config(struct gs_dev *dev);
227 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
228 u8 type, unsigned int index, int is_otg);
230 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
231 gfp_t kmalloc_flags);
232 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
234 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
235 gfp_t kmalloc_flags);
236 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
238 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
239 static void gs_free_ports(struct gs_dev *dev);
241 /* circular buffer */
242 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
243 static void gs_buf_free(struct gs_buf *gb);
244 static void gs_buf_clear(struct gs_buf *gb);
245 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
246 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
247 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
249 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
252 /* external functions */
253 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
258 static struct gs_dev *gs_device;
260 static const char *EP_IN_NAME;
261 static const char *EP_OUT_NAME;
262 static const char *EP_NOTIFY_NAME;
264 static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
266 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
267 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
269 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
271 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
274 /* tty driver struct */
275 static struct tty_operations gs_tty_ops = {
279 .put_char = gs_put_char,
280 .flush_chars = gs_flush_chars,
281 .write_room = gs_write_room,
283 .set_termios = gs_set_termios,
284 .throttle = gs_throttle,
285 .unthrottle = gs_unthrottle,
286 .break_ctl = gs_break,
287 .chars_in_buffer = gs_chars_in_buffer,
289 static struct tty_driver *gs_tty_driver;
291 /* gadget driver struct */
292 static struct usb_gadget_driver gs_gadget_driver = {
293 #ifdef CONFIG_USB_GADGET_DUALSPEED
294 .speed = USB_SPEED_HIGH,
296 .speed = USB_SPEED_FULL,
297 #endif /* CONFIG_USB_GADGET_DUALSPEED */
298 .function = GS_LONG_NAME,
300 .unbind = __exit_p(gs_unbind),
302 .disconnect = gs_disconnect,
304 .name = GS_SHORT_NAME,
309 /* USB descriptors */
311 #define GS_MANUFACTURER_STR_ID 1
312 #define GS_PRODUCT_STR_ID 2
313 #define GS_SERIAL_STR_ID 3
314 #define GS_BULK_CONFIG_STR_ID 4
315 #define GS_ACM_CONFIG_STR_ID 5
316 #define GS_CONTROL_STR_ID 6
317 #define GS_DATA_STR_ID 7
319 /* static strings, in UTF-8 */
320 static char manufacturer[50];
321 static struct usb_string gs_strings[] = {
322 { GS_MANUFACTURER_STR_ID, manufacturer },
323 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
324 { GS_SERIAL_STR_ID, "0" },
325 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
326 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
327 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
328 { GS_DATA_STR_ID, "Gadget Serial Data" },
329 { } /* end of list */
332 static struct usb_gadget_strings gs_string_table = {
333 .language = 0x0409, /* en-us */
334 .strings = gs_strings,
337 static struct usb_device_descriptor gs_device_desc = {
338 .bLength = USB_DT_DEVICE_SIZE,
339 .bDescriptorType = USB_DT_DEVICE,
340 .bcdUSB = __constant_cpu_to_le16(0x0200),
341 .bDeviceSubClass = 0,
342 .bDeviceProtocol = 0,
343 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
344 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
345 .iManufacturer = GS_MANUFACTURER_STR_ID,
346 .iProduct = GS_PRODUCT_STR_ID,
347 .iSerialNumber = GS_SERIAL_STR_ID,
348 .bNumConfigurations = GS_NUM_CONFIGS,
351 static struct usb_otg_descriptor gs_otg_descriptor = {
352 .bLength = sizeof(gs_otg_descriptor),
353 .bDescriptorType = USB_DT_OTG,
354 .bmAttributes = USB_OTG_SRP,
357 static struct usb_config_descriptor gs_bulk_config_desc = {
358 .bLength = USB_DT_CONFIG_SIZE,
359 .bDescriptorType = USB_DT_CONFIG,
360 /* .wTotalLength computed dynamically */
362 .bConfigurationValue = GS_BULK_CONFIG_ID,
363 .iConfiguration = GS_BULK_CONFIG_STR_ID,
364 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
368 static struct usb_config_descriptor gs_acm_config_desc = {
369 .bLength = USB_DT_CONFIG_SIZE,
370 .bDescriptorType = USB_DT_CONFIG,
371 /* .wTotalLength computed dynamically */
373 .bConfigurationValue = GS_ACM_CONFIG_ID,
374 .iConfiguration = GS_ACM_CONFIG_STR_ID,
375 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
379 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
380 .bLength = USB_DT_INTERFACE_SIZE,
381 .bDescriptorType = USB_DT_INTERFACE,
382 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
384 .bInterfaceClass = USB_CLASS_CDC_DATA,
385 .bInterfaceSubClass = 0,
386 .bInterfaceProtocol = 0,
387 .iInterface = GS_DATA_STR_ID,
390 static const struct usb_interface_descriptor gs_control_interface_desc = {
391 .bLength = USB_DT_INTERFACE_SIZE,
392 .bDescriptorType = USB_DT_INTERFACE,
393 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
395 .bInterfaceClass = USB_CLASS_COMM,
396 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
397 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
398 .iInterface = GS_CONTROL_STR_ID,
401 static const struct usb_interface_descriptor gs_data_interface_desc = {
402 .bLength = USB_DT_INTERFACE_SIZE,
403 .bDescriptorType = USB_DT_INTERFACE,
404 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
406 .bInterfaceClass = USB_CLASS_CDC_DATA,
407 .bInterfaceSubClass = 0,
408 .bInterfaceProtocol = 0,
409 .iInterface = GS_DATA_STR_ID,
412 static const struct usb_cdc_header_desc gs_header_desc = {
413 .bLength = sizeof(gs_header_desc),
414 .bDescriptorType = USB_DT_CS_INTERFACE,
415 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
416 .bcdCDC = __constant_cpu_to_le16(0x0110),
419 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
420 .bLength = sizeof(gs_call_mgmt_descriptor),
421 .bDescriptorType = USB_DT_CS_INTERFACE,
422 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
424 .bDataInterface = 1, /* index of data interface */
427 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
428 .bLength = sizeof(gs_acm_descriptor),
429 .bDescriptorType = USB_DT_CS_INTERFACE,
430 .bDescriptorSubType = USB_CDC_ACM_TYPE,
434 static const struct usb_cdc_union_desc gs_union_desc = {
435 .bLength = sizeof(gs_union_desc),
436 .bDescriptorType = USB_DT_CS_INTERFACE,
437 .bDescriptorSubType = USB_CDC_UNION_TYPE,
438 .bMasterInterface0 = 0, /* index of control interface */
439 .bSlaveInterface0 = 1, /* index of data interface */
442 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
443 .bLength = USB_DT_ENDPOINT_SIZE,
444 .bDescriptorType = USB_DT_ENDPOINT,
445 .bEndpointAddress = USB_DIR_IN,
446 .bmAttributes = USB_ENDPOINT_XFER_INT,
447 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
448 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
451 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
452 .bLength = USB_DT_ENDPOINT_SIZE,
453 .bDescriptorType = USB_DT_ENDPOINT,
454 .bEndpointAddress = USB_DIR_IN,
455 .bmAttributes = USB_ENDPOINT_XFER_BULK,
458 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
459 .bLength = USB_DT_ENDPOINT_SIZE,
460 .bDescriptorType = USB_DT_ENDPOINT,
461 .bEndpointAddress = USB_DIR_OUT,
462 .bmAttributes = USB_ENDPOINT_XFER_BULK,
465 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
466 (struct usb_descriptor_header *) &gs_otg_descriptor,
467 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
468 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
469 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
473 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
474 (struct usb_descriptor_header *) &gs_otg_descriptor,
475 (struct usb_descriptor_header *) &gs_control_interface_desc,
476 (struct usb_descriptor_header *) &gs_header_desc,
477 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
478 (struct usb_descriptor_header *) &gs_acm_descriptor,
479 (struct usb_descriptor_header *) &gs_union_desc,
480 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
481 (struct usb_descriptor_header *) &gs_data_interface_desc,
482 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
483 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
487 #ifdef CONFIG_USB_GADGET_DUALSPEED
488 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
489 .bLength = USB_DT_ENDPOINT_SIZE,
490 .bDescriptorType = USB_DT_ENDPOINT,
491 .bEndpointAddress = USB_DIR_IN,
492 .bmAttributes = USB_ENDPOINT_XFER_INT,
493 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
494 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
497 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
498 .bLength = USB_DT_ENDPOINT_SIZE,
499 .bDescriptorType = USB_DT_ENDPOINT,
500 .bmAttributes = USB_ENDPOINT_XFER_BULK,
501 .wMaxPacketSize = __constant_cpu_to_le16(512),
504 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
505 .bLength = USB_DT_ENDPOINT_SIZE,
506 .bDescriptorType = USB_DT_ENDPOINT,
507 .bmAttributes = USB_ENDPOINT_XFER_BULK,
508 .wMaxPacketSize = __constant_cpu_to_le16(512),
511 static struct usb_qualifier_descriptor gs_qualifier_desc = {
512 .bLength = sizeof(struct usb_qualifier_descriptor),
513 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
514 .bcdUSB = __constant_cpu_to_le16 (0x0200),
515 /* assumes ep0 uses the same value for both speeds ... */
516 .bNumConfigurations = GS_NUM_CONFIGS,
519 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
520 (struct usb_descriptor_header *) &gs_otg_descriptor,
521 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
522 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
523 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
527 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
528 (struct usb_descriptor_header *) &gs_otg_descriptor,
529 (struct usb_descriptor_header *) &gs_control_interface_desc,
530 (struct usb_descriptor_header *) &gs_header_desc,
531 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
532 (struct usb_descriptor_header *) &gs_acm_descriptor,
533 (struct usb_descriptor_header *) &gs_union_desc,
534 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
535 (struct usb_descriptor_header *) &gs_data_interface_desc,
536 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
537 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
541 #endif /* CONFIG_USB_GADGET_DUALSPEED */
545 MODULE_DESCRIPTION(GS_LONG_NAME);
546 MODULE_AUTHOR("Al Borchers");
547 MODULE_LICENSE("GPL");
550 module_param(debug, int, S_IRUGO|S_IWUSR);
551 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
554 module_param(read_q_size, uint, S_IRUGO);
555 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
557 module_param(write_q_size, uint, S_IRUGO);
558 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
560 module_param(write_buf_size, uint, S_IRUGO);
561 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
563 module_param(use_acm, uint, S_IRUGO);
564 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
566 module_init(gs_module_init);
567 module_exit(gs_module_exit);
572 * Register as a USB gadget driver and a tty driver.
574 static int __init gs_module_init(void)
579 retval = usb_gadget_register_driver(&gs_gadget_driver);
581 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
585 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
588 gs_tty_driver->owner = THIS_MODULE;
589 gs_tty_driver->driver_name = GS_SHORT_NAME;
590 gs_tty_driver->name = "ttygs";
591 gs_tty_driver->major = GS_MAJOR;
592 gs_tty_driver->minor_start = GS_MINOR_START;
593 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
594 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
595 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
596 gs_tty_driver->init_termios = tty_std_termios;
597 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
598 tty_set_operations(gs_tty_driver, &gs_tty_ops);
600 for (i=0; i < GS_NUM_PORTS; i++)
601 sema_init(&gs_open_close_sem[i], 1);
603 retval = tty_register_driver(gs_tty_driver);
605 usb_gadget_unregister_driver(&gs_gadget_driver);
606 put_tty_driver(gs_tty_driver);
607 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
611 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
618 * Unregister as a tty driver and a USB gadget driver.
620 static void __exit gs_module_exit(void)
622 tty_unregister_driver(gs_tty_driver);
623 put_tty_driver(gs_tty_driver);
624 usb_gadget_unregister_driver(&gs_gadget_driver);
626 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
634 static int gs_open(struct tty_struct *tty, struct file *file)
638 struct gs_port *port;
641 struct semaphore *sem;
644 port_num = tty->index;
646 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
648 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
649 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
650 port_num, tty, file);
657 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
658 port_num, tty, file);
662 sem = &gs_open_close_sem[port_num];
663 if (down_interruptible(sem)) {
665 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
666 port_num, tty, file);
670 spin_lock_irqsave(&dev->dev_lock, flags);
672 if (dev->dev_config == GS_NO_CONFIG_ID) {
674 "gs_open: (%d,%p,%p) device is not connected\n",
675 port_num, tty, file);
677 goto exit_unlock_dev;
680 port = dev->dev_port[port_num];
683 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
684 port_num, tty, file);
686 goto exit_unlock_dev;
689 spin_lock(&port->port_lock);
690 spin_unlock(&dev->dev_lock);
692 if (port->port_dev == NULL) {
693 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
694 port_num, tty, file);
696 goto exit_unlock_port;
699 if (port->port_open_count > 0) {
700 ++port->port_open_count;
701 gs_debug("gs_open: (%d,%p,%p) already open\n",
702 port_num, tty, file);
704 goto exit_unlock_port;
707 tty->driver_data = NULL;
709 /* mark port as in use, we can drop port lock and sleep if necessary */
710 port->port_in_use = 1;
712 /* allocate write buffer on first open */
713 if (port->port_write_buf == NULL) {
714 spin_unlock_irqrestore(&port->port_lock, flags);
715 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
716 spin_lock_irqsave(&port->port_lock, flags);
718 /* might have been disconnected while asleep, check */
719 if (port->port_dev == NULL) {
721 "gs_open: (%d,%p,%p) port disconnected (2)\n",
722 port_num, tty, file);
723 port->port_in_use = 0;
725 goto exit_unlock_port;
728 if ((port->port_write_buf=buf) == NULL) {
729 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
730 port_num, tty, file);
731 port->port_in_use = 0;
733 goto exit_unlock_port;
738 /* wait for carrier detect (not implemented) */
740 /* might have been disconnected while asleep, check */
741 if (port->port_dev == NULL) {
742 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
743 port_num, tty, file);
744 port->port_in_use = 0;
746 goto exit_unlock_port;
749 tty->driver_data = port;
750 port->port_tty = tty;
751 port->port_open_count = 1;
752 port->port_in_use = 0;
754 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
759 spin_unlock_irqrestore(&port->port_lock, flags);
764 spin_unlock_irqrestore(&dev->dev_lock, flags);
774 #define GS_WRITE_FINISHED_EVENT_SAFELY(p) \
778 spin_lock_irq(&(p)->port_lock); \
779 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \
780 spin_unlock_irq(&(p)->port_lock); \
784 static void gs_close(struct tty_struct *tty, struct file *file)
786 struct gs_port *port = tty->driver_data;
787 struct semaphore *sem;
790 printk(KERN_ERR "gs_close: NULL port pointer\n");
794 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
796 sem = &gs_open_close_sem[port->port_num];
799 spin_lock_irq(&port->port_lock);
801 if (port->port_open_count == 0) {
803 "gs_close: (%d,%p,%p) port is already closed\n",
804 port->port_num, tty, file);
808 if (port->port_open_count > 1) {
809 --port->port_open_count;
813 /* free disconnected port on final close */
814 if (port->port_dev == NULL) {
819 /* mark port as closed but in use, we can drop port lock */
820 /* and sleep if necessary */
821 port->port_in_use = 1;
822 port->port_open_count = 0;
824 /* wait for write buffer to drain, or */
825 /* at most GS_CLOSE_TIMEOUT seconds */
826 if (gs_buf_data_avail(port->port_write_buf) > 0) {
827 spin_unlock_irq(&port->port_lock);
828 wait_event_interruptible_timeout(port->port_write_wait,
829 GS_WRITE_FINISHED_EVENT_SAFELY(port),
830 GS_CLOSE_TIMEOUT * HZ);
831 spin_lock_irq(&port->port_lock);
834 /* free disconnected port on final close */
835 /* (might have happened during the above sleep) */
836 if (port->port_dev == NULL) {
841 gs_buf_clear(port->port_write_buf);
843 tty->driver_data = NULL;
844 port->port_tty = NULL;
845 port->port_in_use = 0;
847 gs_debug("gs_close: (%d,%p,%p) completed\n",
848 port->port_num, tty, file);
851 spin_unlock_irq(&port->port_lock);
858 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
861 struct gs_port *port = tty->driver_data;
865 printk(KERN_ERR "gs_write: NULL port pointer\n");
869 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
875 spin_lock_irqsave(&port->port_lock, flags);
877 if (port->port_dev == NULL) {
878 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
879 port->port_num, tty);
884 if (port->port_open_count == 0) {
885 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
886 port->port_num, tty);
891 count = gs_buf_put(port->port_write_buf, buf, count);
893 spin_unlock_irqrestore(&port->port_lock, flags);
897 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
903 spin_unlock_irqrestore(&port->port_lock, flags);
910 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
913 struct gs_port *port = tty->driver_data;
916 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
920 gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p, %p, %p\n", port->port_num, tty, ch, __builtin_return_address(0), __builtin_return_address(1), __builtin_return_address(2));
922 spin_lock_irqsave(&port->port_lock, flags);
924 if (port->port_dev == NULL) {
925 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
926 port->port_num, tty);
930 if (port->port_open_count == 0) {
931 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
932 port->port_num, tty);
936 gs_buf_put(port->port_write_buf, &ch, 1);
939 spin_unlock_irqrestore(&port->port_lock, flags);
945 static void gs_flush_chars(struct tty_struct *tty)
948 struct gs_port *port = tty->driver_data;
951 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
955 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
957 spin_lock_irqsave(&port->port_lock, flags);
959 if (port->port_dev == NULL) {
961 "gs_flush_chars: (%d,%p) port is not connected\n",
962 port->port_num, tty);
966 if (port->port_open_count == 0) {
967 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
968 port->port_num, tty);
972 spin_unlock_irqrestore(&port->port_lock, flags);
979 spin_unlock_irqrestore(&port->port_lock, flags);
985 static int gs_write_room(struct tty_struct *tty)
990 struct gs_port *port = tty->driver_data;
996 spin_lock_irqsave(&port->port_lock, flags);
998 if (port->port_dev != NULL && port->port_open_count > 0
999 && port->port_write_buf != NULL)
1000 room = gs_buf_space_avail(port->port_write_buf);
1002 spin_unlock_irqrestore(&port->port_lock, flags);
1004 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1005 port->port_num, tty, room);
1011 * gs_chars_in_buffer
1013 static int gs_chars_in_buffer(struct tty_struct *tty)
1016 unsigned long flags;
1017 struct gs_port *port = tty->driver_data;
1022 spin_lock_irqsave(&port->port_lock, flags);
1024 if (port->port_dev != NULL && port->port_open_count > 0
1025 && port->port_write_buf != NULL)
1026 chars = gs_buf_data_avail(port->port_write_buf);
1028 spin_unlock_irqrestore(&port->port_lock, flags);
1030 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1031 port->port_num, tty, chars);
1039 static void gs_throttle(struct tty_struct *tty)
1046 static void gs_unthrottle(struct tty_struct *tty)
1053 static void gs_break(struct tty_struct *tty, int break_state)
1060 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1062 struct gs_port *port = tty->driver_data;
1065 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1069 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1070 port->port_num, tty, file, cmd, arg);
1074 /* could not handle ioctl */
1075 return -ENOIOCTLCMD;
1081 static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1088 * This function finds available write requests, calls
1089 * gs_send_packet to fill these packets with data, and
1090 * continues until either there are no more write requests
1091 * available or no more data to send. This function is
1092 * run whenever data arrives or write requests are available.
1094 static int gs_send(struct gs_dev *dev)
1097 unsigned long flags;
1099 struct usb_request *req;
1100 struct gs_req_entry *req_entry;
1103 printk(KERN_ERR "gs_send: NULL device pointer\n");
1107 spin_lock_irqsave(&dev->dev_lock, flags);
1109 ep = dev->dev_in_ep;
1111 while(!list_empty(&dev->dev_req_list)) {
1113 req_entry = list_entry(dev->dev_req_list.next,
1114 struct gs_req_entry, re_entry);
1116 req = req_entry->re_req;
1118 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1121 gs_debug_level(3, "gs_send: len=%d, 0x%2.2x 0x%2.2x 0x%2.2x ...\n", len, *((unsigned char *)req->buf), *((unsigned char *)req->buf+1), *((unsigned char *)req->buf+2));
1122 list_del(&req_entry->re_entry);
1124 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1126 "gs_send: cannot queue read request, ret=%d\n",
1136 spin_unlock_irqrestore(&dev->dev_lock, flags);
1144 * If there is data to send, a packet is built in the given
1145 * buffer and the size is returned. If there is no data to
1146 * send, 0 is returned. If there is any error a negative
1147 * error number is returned.
1149 * Called during USB completion routine, on interrupt time.
1151 * We assume that disconnect will not happen until all completion
1152 * routines have completed, so we can assume that the dev_port
1153 * array does not change during the lifetime of this function.
1155 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1158 struct gs_port *port;
1160 /* TEMPORARY -- only port 0 is supported right now */
1161 port = dev->dev_port[0];
1165 "gs_send_packet: port=%d, NULL port pointer\n",
1170 spin_lock(&port->port_lock);
1172 len = gs_buf_data_avail(port->port_write_buf);
1179 size = gs_buf_get(port->port_write_buf, packet, size);
1182 wake_up_interruptible(&port->port_tty->write_wait);
1185 spin_unlock(&port->port_lock);
1192 * Called for each USB packet received. Reads the packet
1193 * header and stuffs the data in the appropriate tty buffer.
1194 * Returns 0 if successful, or a negative error number.
1196 * Called during USB completion routine, on interrupt time.
1198 * We assume that disconnect will not happen until all completion
1199 * routines have completed, so we can assume that the dev_port
1200 * array does not change during the lifetime of this function.
1202 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1205 struct gs_port *port;
1207 struct tty_struct *tty;
1209 /* TEMPORARY -- only port 0 is supported right now */
1210 port = dev->dev_port[0];
1213 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1218 spin_lock(&port->port_lock);
1220 if (port->port_open_count == 0) {
1221 printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",
1228 tty = port->port_tty;
1231 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1237 if (port->port_tty->magic != TTY_MAGIC) {
1238 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1244 len = tty_buffer_request_room(tty, size);
1246 tty_insert_flip_string(tty, packet, len);
1247 tty_flip_buffer_push(port->port_tty);
1248 wake_up_interruptible(&port->port_tty->read_wait);
1252 spin_unlock(&port->port_lock);
1259 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1262 struct gs_dev *dev = ep->driver_data;
1265 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1269 switch(req->status) {
1271 /* normal completion */
1272 gs_recv_packet(dev, req->buf, req->actual);
1274 req->length = ep->maxpacket;
1275 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1277 "gs_read_complete: cannot queue read request, ret=%d\n",
1284 gs_debug("gs_read_complete: shutdown\n");
1285 gs_free_req(ep, req);
1291 "gs_read_complete: unexpected status error, status=%d\n",
1301 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1303 struct gs_dev *dev = ep->driver_data;
1304 struct gs_req_entry *gs_req = req->context;
1307 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1311 switch(req->status) {
1313 /* normal completion */
1315 if (gs_req == NULL) {
1317 "gs_write_complete: NULL request pointer\n");
1321 spin_lock(&dev->dev_lock);
1322 list_add(&gs_req->re_entry, &dev->dev_req_list);
1323 spin_unlock(&dev->dev_lock);
1331 gs_debug("gs_write_complete: shutdown\n");
1332 gs_free_req(ep, req);
1337 "gs_write_complete: unexpected status error, status=%d\n",
1349 * Called on module load. Allocates and initializes the device
1350 * structure and a control request.
1352 static int __init gs_bind(struct usb_gadget *gadget)
1359 /* Some controllers can't support CDC ACM:
1360 * - sh doesn't support multiple interfaces or configs;
1361 * - sa1100 doesn't have a third interrupt endpoint
1363 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1366 gcnum = usb_gadget_controller_number(gadget);
1368 gs_device_desc.bcdDevice =
1369 cpu_to_le16(GS_VERSION_NUM | gcnum);
1371 printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
1373 /* unrecognized, but safe unless bulk is REALLY quirky */
1374 gs_device_desc.bcdDevice =
1375 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1378 usb_ep_autoconfig_reset(gadget);
1380 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1383 EP_IN_NAME = ep->name;
1384 ep->driver_data = ep; /* claim the endpoint */
1386 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1389 EP_OUT_NAME = ep->name;
1390 ep->driver_data = ep; /* claim the endpoint */
1393 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1395 printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);
1398 gs_device_desc.idProduct = __constant_cpu_to_le16(
1400 EP_NOTIFY_NAME = ep->name;
1401 ep->driver_data = ep; /* claim the endpoint */
1404 gs_device_desc.bDeviceClass = use_acm
1405 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1406 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1408 #ifdef CONFIG_USB_GADGET_DUALSPEED
1409 gs_qualifier_desc.bDeviceClass = use_acm
1410 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1411 /* assume ep0 uses the same packet size for both speeds */
1412 gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
1413 /* assume endpoints are dual-speed */
1414 gs_highspeed_notify_desc.bEndpointAddress =
1415 gs_fullspeed_notify_desc.bEndpointAddress;
1416 gs_highspeed_in_desc.bEndpointAddress =
1417 gs_fullspeed_in_desc.bEndpointAddress;
1418 gs_highspeed_out_desc.bEndpointAddress =
1419 gs_fullspeed_out_desc.bEndpointAddress;
1420 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1422 usb_gadget_set_selfpowered(gadget);
1424 if (gadget->is_otg) {
1425 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1426 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1427 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1430 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1434 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1435 system_utsname.sysname, system_utsname.release,
1438 memset(dev, 0, sizeof(struct gs_dev));
1439 dev->dev_gadget = gadget;
1440 spin_lock_init(&dev->dev_lock);
1441 INIT_LIST_HEAD(&dev->dev_req_list);
1442 set_gadget_data(gadget, dev);
1444 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1445 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1450 /* preallocate control response and buffer */
1451 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1453 if (dev->dev_ctrl_req == NULL) {
1457 dev->dev_ctrl_req->complete = gs_setup_complete;
1459 gadget->ep0->driver_data = dev;
1461 printk(KERN_INFO "gs_bind: %s %s bound\n",
1462 GS_LONG_NAME, GS_VERSION_STR);
1467 printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);
1474 * Called on module unload. Frees the control request and device
1477 static void __exit gs_unbind(struct usb_gadget *gadget)
1479 struct gs_dev *dev = get_gadget_data(gadget);
1483 /* read/write requests already freed, only control request remains */
1485 if (dev->dev_ctrl_req != NULL) {
1486 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1487 dev->dev_ctrl_req = NULL;
1491 set_gadget_data(gadget, NULL);
1494 printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1501 * Implements all the control endpoint functionality that's not
1502 * handled in hardware or the hardware driver.
1504 * Returns the size of the data sent to the host, or a negative
1507 static int gs_setup(struct usb_gadget *gadget,
1508 const struct usb_ctrlrequest *ctrl)
1510 int ret = -EOPNOTSUPP;
1511 struct gs_dev *dev = get_gadget_data(gadget);
1512 struct usb_request *req = dev->dev_ctrl_req;
1513 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1514 u16 wValue = le16_to_cpu(ctrl->wValue);
1515 u16 wLength = le16_to_cpu(ctrl->wLength);
1517 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1518 case USB_TYPE_STANDARD:
1519 ret = gs_setup_standard(gadget,ctrl);
1522 case USB_TYPE_CLASS:
1523 ret = gs_setup_class(gadget,ctrl);
1527 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1528 ctrl->bRequestType, ctrl->bRequest,
1529 wValue, wIndex, wLength);
1533 /* respond with data transfer before status phase? */
1536 req->zero = ret < wLength
1537 && (ret % gadget->ep0->maxpacket) == 0;
1538 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1540 printk(KERN_ERR "gs_setup: cannot queue response, ret=%d\n",
1543 gs_setup_complete(gadget->ep0, req);
1547 /* device either stalls (ret < 0) or reports success */
1551 static int gs_setup_standard(struct usb_gadget *gadget,
1552 const struct usb_ctrlrequest *ctrl)
1554 int ret = -EOPNOTSUPP;
1555 struct gs_dev *dev = get_gadget_data(gadget);
1556 struct usb_request *req = dev->dev_ctrl_req;
1557 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1558 u16 wValue = le16_to_cpu(ctrl->wValue);
1559 u16 wLength = le16_to_cpu(ctrl->wLength);
1561 switch (ctrl->bRequest) {
1562 case USB_REQ_GET_DESCRIPTOR:
1563 if (ctrl->bRequestType != USB_DIR_IN)
1566 switch (wValue >> 8) {
1569 (u16)sizeof(struct usb_device_descriptor));
1570 memcpy(req->buf, &gs_device_desc, ret);
1573 #ifdef CONFIG_USB_GADGET_DUALSPEED
1574 case USB_DT_DEVICE_QUALIFIER:
1575 if (!gadget->is_dualspeed)
1578 (u16)sizeof(struct usb_qualifier_descriptor));
1579 memcpy(req->buf, &gs_qualifier_desc, ret);
1582 case USB_DT_OTHER_SPEED_CONFIG:
1583 if (!gadget->is_dualspeed)
1586 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1588 ret = gs_build_config_buf(req->buf, gadget->speed,
1589 wValue >> 8, wValue & 0xff,
1592 ret = min(wLength, (u16)ret);
1596 /* wIndex == language code. */
1597 ret = usb_gadget_get_string(&gs_string_table,
1598 wValue & 0xff, req->buf);
1600 ret = min(wLength, (u16)ret);
1605 case USB_REQ_SET_CONFIGURATION:
1606 if (ctrl->bRequestType != 0)
1608 spin_lock(&dev->dev_lock);
1609 ret = gs_set_config(dev, wValue);
1610 spin_unlock(&dev->dev_lock);
1613 case USB_REQ_GET_CONFIGURATION:
1614 if (ctrl->bRequestType != USB_DIR_IN)
1616 *(u8 *)req->buf = dev->dev_config;
1617 ret = min(wLength, (u16)1);
1620 case USB_REQ_SET_INTERFACE:
1621 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1623 || wIndex >= GS_MAX_NUM_INTERFACES)
1625 if (dev->dev_config == GS_BULK_CONFIG_ID
1626 && wIndex != GS_BULK_INTERFACE_ID)
1628 /* no alternate interface settings */
1631 spin_lock(&dev->dev_lock);
1632 /* PXA hardware partially handles SET_INTERFACE;
1633 * we need to kluge around that interference. */
1634 if (gadget_is_pxa(gadget)) {
1635 ret = gs_set_config(dev, use_acm ?
1636 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1637 goto set_interface_done;
1639 if (dev->dev_config != GS_BULK_CONFIG_ID
1640 && wIndex == GS_CONTROL_INTERFACE_ID) {
1641 if (dev->dev_notify_ep) {
1642 usb_ep_disable(dev->dev_notify_ep);
1643 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1646 usb_ep_disable(dev->dev_in_ep);
1647 usb_ep_disable(dev->dev_out_ep);
1648 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1649 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1653 spin_unlock(&dev->dev_lock);
1656 case USB_REQ_GET_INTERFACE:
1657 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1658 || dev->dev_config == GS_NO_CONFIG_ID)
1660 if (wIndex >= GS_MAX_NUM_INTERFACES
1661 || (dev->dev_config == GS_BULK_CONFIG_ID
1662 && wIndex != GS_BULK_INTERFACE_ID)) {
1666 /* no alternate interface settings */
1667 *(u8 *)req->buf = 0;
1668 ret = min(wLength, (u16)1);
1672 printk(KERN_ERR "gs_setup: unknown standard request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1673 ctrl->bRequestType, ctrl->bRequest,
1674 wValue, wIndex, wLength);
1681 static int gs_setup_class(struct usb_gadget *gadget,
1682 const struct usb_ctrlrequest *ctrl)
1684 int ret = -EOPNOTSUPP;
1685 struct gs_dev *dev = get_gadget_data(gadget);
1686 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1687 struct usb_request *req = dev->dev_ctrl_req;
1688 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1689 u16 wValue = le16_to_cpu(ctrl->wValue);
1690 u16 wLength = le16_to_cpu(ctrl->wLength);
1692 switch (ctrl->bRequest) {
1693 case USB_CDC_REQ_SET_LINE_CODING:
1695 (u16)sizeof(struct usb_cdc_line_coding));
1697 spin_lock(&port->port_lock);
1698 memcpy(&port->port_line_coding, req->buf, ret);
1699 spin_unlock(&port->port_lock);
1703 case USB_CDC_REQ_GET_LINE_CODING:
1704 port = dev->dev_port[0]; /* ACM only has one port */
1706 (u16)sizeof(struct usb_cdc_line_coding));
1708 spin_lock(&port->port_lock);
1709 memcpy(req->buf, &port->port_line_coding, ret);
1710 spin_unlock(&port->port_lock);
1714 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1719 printk(KERN_ERR "gs_setup: unknown class request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1720 ctrl->bRequestType, ctrl->bRequest,
1721 wValue, wIndex, wLength);
1731 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1733 if (req->status || req->actual != req->length) {
1734 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1735 req->status, req->actual, req->length);
1742 * Called when the device is disconnected. Frees the closed
1743 * ports and disconnects open ports. Open ports will be freed
1744 * on close. Then reallocates the ports for the next connection.
1746 static void gs_disconnect(struct usb_gadget *gadget)
1748 unsigned long flags;
1749 struct gs_dev *dev = get_gadget_data(gadget);
1751 spin_lock_irqsave(&dev->dev_lock, flags);
1753 gs_reset_config(dev);
1755 /* free closed ports and disconnect open ports */
1756 /* (open ports will be freed when closed) */
1759 /* re-allocate ports for the next connection */
1760 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1761 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1763 spin_unlock_irqrestore(&dev->dev_lock, flags);
1765 printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1771 * Configures the device by enabling device specific
1772 * optimizations, setting up the endpoints, allocating
1773 * read and write requests and queuing read requests.
1775 * The device lock must be held when calling this function.
1777 static int gs_set_config(struct gs_dev *dev, unsigned config)
1781 struct usb_gadget *gadget = dev->dev_gadget;
1783 struct usb_endpoint_descriptor *ep_desc;
1784 struct usb_request *req;
1785 struct gs_req_entry *req_entry;
1788 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1792 if (config == dev->dev_config)
1795 gs_reset_config(dev);
1798 case GS_NO_CONFIG_ID:
1800 case GS_BULK_CONFIG_ID:
1803 /* device specific optimizations */
1804 if (gadget_is_net2280(gadget))
1805 net2280_set_fifo_mode(gadget, 1);
1807 case GS_ACM_CONFIG_ID:
1810 /* device specific optimizations */
1811 if (gadget_is_net2280(gadget))
1812 net2280_set_fifo_mode(gadget, 1);
1818 dev->dev_config = config;
1820 gadget_for_each_ep(ep, gadget) {
1823 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1824 ep_desc = GS_SPEED_SELECT(
1825 gadget->speed == USB_SPEED_HIGH,
1826 &gs_highspeed_notify_desc,
1827 &gs_fullspeed_notify_desc);
1828 ret = usb_ep_enable(ep,ep_desc);
1830 ep->driver_data = dev;
1831 dev->dev_notify_ep = ep;
1832 dev->dev_notify_ep_desc = ep_desc;
1834 printk(KERN_ERR "gs_set_config: cannot enable notify endpoint %s, ret=%d\n",
1836 goto exit_reset_config;
1840 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1841 ep_desc = GS_SPEED_SELECT(
1842 gadget->speed == USB_SPEED_HIGH,
1843 &gs_highspeed_in_desc,
1844 &gs_fullspeed_in_desc);
1845 ret = usb_ep_enable(ep,ep_desc);
1847 ep->driver_data = dev;
1848 dev->dev_in_ep = ep;
1849 dev->dev_in_ep_desc = ep_desc;
1851 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
1853 goto exit_reset_config;
1857 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1858 ep_desc = GS_SPEED_SELECT(
1859 gadget->speed == USB_SPEED_HIGH,
1860 &gs_highspeed_out_desc,
1861 &gs_fullspeed_out_desc);
1862 ret = usb_ep_enable(ep,ep_desc);
1864 ep->driver_data = dev;
1865 dev->dev_out_ep = ep;
1866 dev->dev_out_ep_desc = ep_desc;
1868 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
1870 goto exit_reset_config;
1876 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
1877 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1878 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
1880 goto exit_reset_config;
1883 /* allocate and queue read requests */
1884 ep = dev->dev_out_ep;
1885 for (i=0; i<read_q_size && ret == 0; i++) {
1886 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1887 req->complete = gs_read_complete;
1888 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1889 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
1893 printk(KERN_ERR "gs_set_config: cannot allocate read requests\n");
1895 goto exit_reset_config;
1899 /* allocate write requests, and put on free list */
1900 ep = dev->dev_in_ep;
1901 for (i=0; i<write_q_size; i++) {
1902 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1903 req_entry->re_req->complete = gs_write_complete;
1904 list_add(&req_entry->re_entry, &dev->dev_req_list);
1906 printk(KERN_ERR "gs_set_config: cannot allocate write requests\n");
1908 goto exit_reset_config;
1912 printk(KERN_INFO "gs_set_config: %s configured, %s speed %s config\n",
1914 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1915 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1920 gs_reset_config(dev);
1927 * Mark the device as not configured, disable all endpoints,
1928 * which forces completion of pending I/O and frees queued
1929 * requests, and free the remaining write requests on the
1932 * The device lock must be held when calling this function.
1934 static void gs_reset_config(struct gs_dev *dev)
1936 struct gs_req_entry *req_entry;
1939 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
1943 if (dev->dev_config == GS_NO_CONFIG_ID)
1946 dev->dev_config = GS_NO_CONFIG_ID;
1948 /* free write requests on the free list */
1949 while(!list_empty(&dev->dev_req_list)) {
1950 req_entry = list_entry(dev->dev_req_list.next,
1951 struct gs_req_entry, re_entry);
1952 list_del(&req_entry->re_entry);
1953 gs_free_req_entry(dev->dev_in_ep, req_entry);
1956 /* disable endpoints, forcing completion of pending i/o; */
1957 /* completion handlers free their requests in this case */
1958 if (dev->dev_notify_ep) {
1959 usb_ep_disable(dev->dev_notify_ep);
1960 dev->dev_notify_ep = NULL;
1962 if (dev->dev_in_ep) {
1963 usb_ep_disable(dev->dev_in_ep);
1964 dev->dev_in_ep = NULL;
1966 if (dev->dev_out_ep) {
1967 usb_ep_disable(dev->dev_out_ep);
1968 dev->dev_out_ep = NULL;
1973 * gs_build_config_buf
1975 * Builds the config descriptors in the given buffer and returns the
1976 * length, or a negative error number.
1978 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
1979 u8 type, unsigned int index, int is_otg)
1983 const struct usb_config_descriptor *config_desc;
1984 const struct usb_descriptor_header **function;
1986 if (index >= gs_device_desc.bNumConfigurations)
1989 /* other speed switches high and full speed */
1990 high_speed = (speed == USB_SPEED_HIGH);
1991 if (type == USB_DT_OTHER_SPEED_CONFIG)
1992 high_speed = !high_speed;
1995 config_desc = &gs_acm_config_desc;
1996 function = GS_SPEED_SELECT(high_speed,
1997 gs_acm_highspeed_function,
1998 gs_acm_fullspeed_function);
2000 config_desc = &gs_bulk_config_desc;
2001 function = GS_SPEED_SELECT(high_speed,
2002 gs_bulk_highspeed_function,
2003 gs_bulk_fullspeed_function);
2006 /* for now, don't advertise srp-only devices */
2010 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2014 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2022 * Allocate a usb_request and its buffer. Returns a pointer to the
2023 * usb_request or NULL if there is an error.
2025 static struct usb_request *
2026 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
2028 struct usb_request *req;
2033 req = usb_ep_alloc_request(ep, kmalloc_flags);
2037 req->buf = kmalloc(len, kmalloc_flags);
2038 if (req->buf == NULL) {
2039 usb_ep_free_request(ep, req);
2050 * Free a usb_request and its buffer.
2052 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2054 if (ep != NULL && req != NULL) {
2056 usb_ep_free_request(ep, req);
2061 * gs_alloc_req_entry
2063 * Allocates a request and its buffer, using the given
2064 * endpoint, buffer len, and kmalloc flags.
2066 static struct gs_req_entry *
2067 gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2069 struct gs_req_entry *req;
2071 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2075 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2076 if (req->re_req == NULL) {
2081 req->re_req->context = req;
2089 * Frees a request and its buffer.
2091 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2093 if (ep != NULL && req != NULL) {
2094 if (req->re_req != NULL)
2095 gs_free_req(ep, req->re_req);
2103 * Allocate all ports and set the gs_dev struct to point to them.
2104 * Return 0 if successful, or a negative error number.
2106 * The device lock is normally held when calling this function.
2108 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2111 struct gs_port *port;
2116 for (i=0; i<GS_NUM_PORTS; i++) {
2117 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2120 port->port_dev = dev;
2122 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
2123 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2124 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2125 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2126 spin_lock_init(&port->port_lock);
2127 init_waitqueue_head(&port->port_write_wait);
2129 dev->dev_port[i] = port;
2138 * Free all closed ports. Open ports are disconnected by
2139 * freeing their write buffers, setting their device pointers
2140 * and the pointers to them in the device to NULL. These
2141 * ports will be freed when closed.
2143 * The device lock is normally held when calling this function.
2145 static void gs_free_ports(struct gs_dev *dev)
2148 unsigned long flags;
2149 struct gs_port *port;
2154 for (i=0; i<GS_NUM_PORTS; i++) {
2155 if ((port=dev->dev_port[i]) != NULL) {
2156 dev->dev_port[i] = NULL;
2158 spin_lock_irqsave(&port->port_lock, flags);
2160 if (port->port_write_buf != NULL) {
2161 gs_buf_free(port->port_write_buf);
2162 port->port_write_buf = NULL;
2165 if (port->port_open_count > 0 || port->port_in_use) {
2166 port->port_dev = NULL;
2167 wake_up_interruptible(&port->port_write_wait);
2168 if (port->port_tty) {
2169 wake_up_interruptible(&port->port_tty->read_wait);
2170 wake_up_interruptible(&port->port_tty->write_wait);
2172 spin_unlock_irqrestore(&port->port_lock, flags);
2174 spin_unlock_irqrestore(&port->port_lock, flags);
2182 /* Circular Buffer */
2187 * Allocate a circular buffer and all associated memory.
2189 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2196 gb = (struct gs_buf *)kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2200 gb->buf_buf = kmalloc(size, kmalloc_flags);
2201 if (gb->buf_buf == NULL) {
2206 gb->buf_size = size;
2207 gb->buf_get = gb->buf_put = gb->buf_buf;
2215 * Free the buffer and all associated memory.
2217 void gs_buf_free(struct gs_buf *gb)
2228 * Clear out all data in the circular buffer.
2230 void gs_buf_clear(struct gs_buf *gb)
2233 gb->buf_get = gb->buf_put;
2234 /* equivalent to a get of all data available */
2240 * Return the number of bytes of data available in the circular
2243 unsigned int gs_buf_data_avail(struct gs_buf *gb)
2246 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2252 * gs_buf_space_avail
2254 * Return the number of bytes of space available in the circular
2257 unsigned int gs_buf_space_avail(struct gs_buf *gb)
2260 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2268 * Copy data data from a user buffer and put it into the circular buffer.
2269 * Restrict to the amount of space available.
2271 * Return the number of bytes copied.
2273 unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2280 len = gs_buf_space_avail(gb);
2287 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2289 memcpy(gb->buf_put, buf, len);
2290 memcpy(gb->buf_buf, buf+len, count - len);
2291 gb->buf_put = gb->buf_buf + count - len;
2293 memcpy(gb->buf_put, buf, count);
2295 gb->buf_put += count;
2296 else /* count == len */
2297 gb->buf_put = gb->buf_buf;
2306 * Get data from the circular buffer and copy to the given buffer.
2307 * Restrict to the amount of data available.
2309 * Return the number of bytes copied.
2311 unsigned int gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2318 len = gs_buf_data_avail(gb);
2325 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2327 memcpy(buf, gb->buf_get, len);
2328 memcpy(buf+len, gb->buf_buf, count - len);
2329 gb->buf_get = gb->buf_buf + count - len;
2331 memcpy(buf, gb->buf_get, count);
2333 gb->buf_get += count;
2334 else /* count == len */
2335 gb->buf_get = gb->buf_buf;