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/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_gadget.h>
50 #include "gadget_chips.h"
55 #define __wait_cond_interruptible(wq, condition, lock, flags, ret) \
57 wait_queue_t __wait; \
58 init_waitqueue_entry(&__wait, current); \
60 add_wait_queue(&wq, &__wait); \
62 set_current_state(TASK_INTERRUPTIBLE); \
65 if (!signal_pending(current)) { \
66 spin_unlock_irqrestore(lock, flags); \
68 spin_lock_irqsave(lock, flags); \
74 current->state = TASK_RUNNING; \
75 remove_wait_queue(&wq, &__wait); \
78 #define wait_cond_interruptible(wq, condition, lock, flags) \
82 __wait_cond_interruptible(wq, condition, lock, flags, \
87 #define __wait_cond_interruptible_timeout(wq, condition, lock, flags, \
90 signed long __timeout = timeout; \
91 wait_queue_t __wait; \
92 init_waitqueue_entry(&__wait, current); \
94 add_wait_queue(&wq, &__wait); \
96 set_current_state(TASK_INTERRUPTIBLE); \
101 if (!signal_pending(current)) { \
102 spin_unlock_irqrestore(lock, flags); \
103 __timeout = schedule_timeout(__timeout); \
104 spin_lock_irqsave(lock, flags); \
107 ret = -ERESTARTSYS; \
110 current->state = TASK_RUNNING; \
111 remove_wait_queue(&wq, &__wait); \
114 #define wait_cond_interruptible_timeout(wq, condition, lock, flags, \
119 __wait_cond_interruptible_timeout(wq, condition, lock, \
120 flags, timeout, __ret); \
125 /* CDC-ACM Defines and Structures */
127 #define USB_CDC_SUBCLASS_ACM 2
129 #define USB_CDC_CTRL_PROTO_NONE 0
130 #define USB_CDC_CTRL_PROTO_AT 1
131 #define USB_CDC_CTRL_PROTO_VENDOR 0xff
133 #define USB_CDC_SUBTYPE_HEADER 0
134 #define USB_CDC_SUBTYPE_CALL_MGMT 1
135 #define USB_CDC_SUBTYPE_ACM 2
136 #define USB_CDC_SUBTYPE_UNION 6
138 #define USB_CDC_CALL_MGMT_CAP_CALL_MGMT 0x01
139 #define USB_CDC_CALL_MGMT_CAP_DATA_INTF 0x02
141 #define USB_CDC_REQ_SET_LINE_CODING 0x20
142 #define USB_CDC_REQ_GET_LINE_CODING 0x21
143 #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
145 #define USB_CDC_1_STOP_BITS 0
146 #define USB_CDC_1_5_STOP_BITS 1
147 #define USB_CDC_2_STOP_BITS 2
149 #define USB_CDC_NO_PARITY 0
150 #define USB_CDC_ODD_PARITY 1
151 #define USB_CDC_EVEN_PARITY 2
152 #define USB_CDC_MARK_PARITY 3
153 #define USB_CDC_SPACE_PARITY 4
155 /* Header Functional Descriptor from CDC spec 5.2.3.1 */
156 struct usb_cdc_header_desc {
159 u8 bDescriptorSubType;
161 } __attribute__ ((packed));
163 /* Call Management Descriptor from CDC spec 5.2.3.3 */
164 struct usb_cdc_call_mgmt_desc {
167 u8 bDescriptorSubType;
170 } __attribute__ ((packed));
172 /* Abstract Control Management Descriptor from CDC spec 5.2.3.4 */
173 struct usb_cdc_acm_desc {
176 u8 bDescriptorSubType;
178 } __attribute__ ((packed));
180 /* Union Functional Descriptor from CDC spec 5.2.3.8 */
181 struct usb_cdc_union_desc {
184 u8 bDescriptorSubType;
185 u8 bMasterInterface0;
187 /* ... and there could be other slave interfaces */
188 } __attribute__ ((packed));
190 /* Line Coding Structure from CDC spec 6.2.13 */
191 struct usb_cdc_line_coding {
196 } __attribute__ ((packed));
201 #define GS_VERSION_STR "v2.0"
202 #define GS_VERSION_NUM 0x0200
204 #define GS_LONG_NAME "Gadget Serial"
205 #define GS_SHORT_NAME "g_serial"
208 #define GS_MINOR_START 0
210 #define GS_NUM_PORTS 16
212 #define GS_NUM_CONFIGS 1
213 #define GS_NO_CONFIG_ID 0
214 #define GS_BULK_CONFIG_ID 1
215 #define GS_ACM_CONFIG_ID 2
217 #define GS_MAX_NUM_INTERFACES 2
218 #define GS_BULK_INTERFACE_ID 0
219 #define GS_CONTROL_INTERFACE_ID 0
220 #define GS_DATA_INTERFACE_ID 1
222 #define GS_MAX_DESC_LEN 256
224 #define GS_DEFAULT_READ_Q_SIZE 32
225 #define GS_DEFAULT_WRITE_Q_SIZE 32
227 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
228 #define GS_TMP_BUF_SIZE 8192
230 #define GS_CLOSE_TIMEOUT 15
232 #define GS_DEFAULT_USE_ACM 0
234 #define GS_DEFAULT_DTE_RATE 9600
235 #define GS_DEFAULT_DATA_BITS 8
236 #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
237 #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
239 /* select highspeed/fullspeed, hiding highspeed if not configured */
240 #ifdef CONFIG_USB_GADGET_DUALSPEED
241 #define GS_SPEED_SELECT(is_hs,hs,fs) ((is_hs) ? (hs) : (fs))
243 #define GS_SPEED_SELECT(is_hs,hs,fs) (fs)
244 #endif /* CONFIG_USB_GADGET_DUALSPEED */
248 static int debug = 1;
250 #define gs_debug(format, arg...) \
251 do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
252 #define gs_debug_level(level, format, arg...) \
253 do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
257 #define gs_debug(format, arg...) \
259 #define gs_debug_level(level, format, arg...) \
262 #endif /* GS_DEBUG */
264 /* Thanks to NetChip Technologies for donating this product ID.
266 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
267 * Instead: allocate your own, using normal USB-IF procedures.
269 #define GS_VENDOR_ID 0x0525 /* NetChip */
270 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
271 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
273 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
274 #define GS_NOTIFY_MAXPACKET 8
281 /* circular buffer */
283 unsigned int buf_size;
289 /* list of requests */
290 struct gs_req_entry {
291 struct list_head re_entry;
292 struct usb_request *re_req;
295 /* the port structure holds info for each port, one for each minor number */
297 struct gs_dev *port_dev; /* pointer to device struct */
298 struct tty_struct *port_tty; /* pointer to tty struct */
299 spinlock_t port_lock;
302 int port_in_use; /* open/close in progress */
303 wait_queue_head_t port_write_wait;/* waiting to write */
304 struct gs_buf *port_write_buf;
305 struct usb_cdc_line_coding port_line_coding;
308 /* the device structure holds info for the USB device */
310 struct usb_gadget *dev_gadget; /* gadget device pointer */
311 spinlock_t dev_lock; /* lock for set/reset config */
312 int dev_config; /* configuration number */
313 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
314 struct usb_ep *dev_in_ep; /* address of in endpoint */
315 struct usb_ep *dev_out_ep; /* address of out endpoint */
316 struct usb_endpoint_descriptor /* desciptor of notify ep */
318 struct usb_endpoint_descriptor /* descriptor of in endpoint */
320 struct usb_endpoint_descriptor /* descriptor of out endpoint */
322 struct usb_request *dev_ctrl_req; /* control request */
323 struct list_head dev_req_list; /* list of write requests */
324 int dev_sched_port; /* round robin port scheduled */
325 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
332 static int __init gs_module_init(void);
333 static void __exit gs_module_exit(void);
336 static int gs_open(struct tty_struct *tty, struct file *file);
337 static void gs_close(struct tty_struct *tty, struct file *file);
338 static int gs_write(struct tty_struct *tty,
339 const unsigned char *buf, int count);
340 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
341 static void gs_flush_chars(struct tty_struct *tty);
342 static int gs_write_room(struct tty_struct *tty);
343 static int gs_chars_in_buffer(struct tty_struct *tty);
344 static void gs_throttle(struct tty_struct * tty);
345 static void gs_unthrottle(struct tty_struct * tty);
346 static void gs_break(struct tty_struct *tty, int break_state);
347 static int gs_ioctl(struct tty_struct *tty, struct file *file,
348 unsigned int cmd, unsigned long arg);
349 static void gs_set_termios(struct tty_struct *tty, struct termios *old);
351 static int gs_send(struct gs_dev *dev);
352 static int gs_send_packet(struct gs_dev *dev, char *packet,
354 static int gs_recv_packet(struct gs_dev *dev, char *packet,
356 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
357 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
360 static int gs_bind(struct usb_gadget *gadget);
361 static void gs_unbind(struct usb_gadget *gadget);
362 static int gs_setup(struct usb_gadget *gadget,
363 const struct usb_ctrlrequest *ctrl);
364 static int gs_setup_standard(struct usb_gadget *gadget,
365 const struct usb_ctrlrequest *ctrl);
366 static int gs_setup_class(struct usb_gadget *gadget,
367 const struct usb_ctrlrequest *ctrl);
368 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
369 static void gs_disconnect(struct usb_gadget *gadget);
370 static int gs_set_config(struct gs_dev *dev, unsigned config);
371 static void gs_reset_config(struct gs_dev *dev);
372 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
373 u8 type, unsigned int index, int is_otg);
375 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
377 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
379 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
381 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
383 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags);
384 static void gs_free_ports(struct gs_dev *dev);
386 /* circular buffer */
387 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags);
388 static void gs_buf_free(struct gs_buf *gb);
389 static void gs_buf_clear(struct gs_buf *gb);
390 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
391 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
392 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
394 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
397 /* external functions */
398 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
403 static struct gs_dev *gs_device;
405 static const char *EP_IN_NAME;
406 static const char *EP_OUT_NAME;
407 static const char *EP_NOTIFY_NAME;
409 static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
411 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
412 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
414 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
416 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
419 /* tty driver struct */
420 static struct tty_operations gs_tty_ops = {
424 .put_char = gs_put_char,
425 .flush_chars = gs_flush_chars,
426 .write_room = gs_write_room,
428 .set_termios = gs_set_termios,
429 .throttle = gs_throttle,
430 .unthrottle = gs_unthrottle,
431 .break_ctl = gs_break,
432 .chars_in_buffer = gs_chars_in_buffer,
434 static struct tty_driver *gs_tty_driver;
436 /* gadget driver struct */
437 static struct usb_gadget_driver gs_gadget_driver = {
438 #ifdef CONFIG_USB_GADGET_DUALSPEED
439 .speed = USB_SPEED_HIGH,
441 .speed = USB_SPEED_FULL,
442 #endif /* CONFIG_USB_GADGET_DUALSPEED */
443 .function = GS_LONG_NAME,
447 .disconnect = gs_disconnect,
449 .name = GS_SHORT_NAME,
450 /* .shutdown = ... */
457 /* USB descriptors */
459 #define GS_MANUFACTURER_STR_ID 1
460 #define GS_PRODUCT_STR_ID 2
461 #define GS_SERIAL_STR_ID 3
462 #define GS_BULK_CONFIG_STR_ID 4
463 #define GS_ACM_CONFIG_STR_ID 5
464 #define GS_CONTROL_STR_ID 6
465 #define GS_DATA_STR_ID 7
467 /* static strings, in UTF-8 */
468 static char manufacturer[50];
469 static struct usb_string gs_strings[] = {
470 { GS_MANUFACTURER_STR_ID, manufacturer },
471 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
472 { GS_SERIAL_STR_ID, "0" },
473 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
474 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
475 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
476 { GS_DATA_STR_ID, "Gadget Serial Data" },
477 { } /* end of list */
480 static struct usb_gadget_strings gs_string_table = {
481 .language = 0x0409, /* en-us */
482 .strings = gs_strings,
485 static struct usb_device_descriptor gs_device_desc = {
486 .bLength = USB_DT_DEVICE_SIZE,
487 .bDescriptorType = USB_DT_DEVICE,
488 .bcdUSB = __constant_cpu_to_le16(0x0200),
489 .bDeviceSubClass = 0,
490 .bDeviceProtocol = 0,
491 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
492 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
493 .iManufacturer = GS_MANUFACTURER_STR_ID,
494 .iProduct = GS_PRODUCT_STR_ID,
495 .iSerialNumber = GS_SERIAL_STR_ID,
496 .bNumConfigurations = GS_NUM_CONFIGS,
499 static struct usb_otg_descriptor gs_otg_descriptor = {
500 .bLength = sizeof(gs_otg_descriptor),
501 .bDescriptorType = USB_DT_OTG,
502 .bmAttributes = USB_OTG_SRP,
505 static struct usb_config_descriptor gs_bulk_config_desc = {
506 .bLength = USB_DT_CONFIG_SIZE,
507 .bDescriptorType = USB_DT_CONFIG,
508 /* .wTotalLength computed dynamically */
510 .bConfigurationValue = GS_BULK_CONFIG_ID,
511 .iConfiguration = GS_BULK_CONFIG_STR_ID,
512 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
516 static struct usb_config_descriptor gs_acm_config_desc = {
517 .bLength = USB_DT_CONFIG_SIZE,
518 .bDescriptorType = USB_DT_CONFIG,
519 /* .wTotalLength computed dynamically */
521 .bConfigurationValue = GS_ACM_CONFIG_ID,
522 .iConfiguration = GS_ACM_CONFIG_STR_ID,
523 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
527 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
528 .bLength = USB_DT_INTERFACE_SIZE,
529 .bDescriptorType = USB_DT_INTERFACE,
530 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
532 .bInterfaceClass = USB_CLASS_CDC_DATA,
533 .bInterfaceSubClass = 0,
534 .bInterfaceProtocol = 0,
535 .iInterface = GS_DATA_STR_ID,
538 static const struct usb_interface_descriptor gs_control_interface_desc = {
539 .bLength = USB_DT_INTERFACE_SIZE,
540 .bDescriptorType = USB_DT_INTERFACE,
541 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
543 .bInterfaceClass = USB_CLASS_COMM,
544 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
545 .bInterfaceProtocol = USB_CDC_CTRL_PROTO_AT,
546 .iInterface = GS_CONTROL_STR_ID,
549 static const struct usb_interface_descriptor gs_data_interface_desc = {
550 .bLength = USB_DT_INTERFACE_SIZE,
551 .bDescriptorType = USB_DT_INTERFACE,
552 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
554 .bInterfaceClass = USB_CLASS_CDC_DATA,
555 .bInterfaceSubClass = 0,
556 .bInterfaceProtocol = 0,
557 .iInterface = GS_DATA_STR_ID,
560 static const struct usb_cdc_header_desc gs_header_desc = {
561 .bLength = sizeof(gs_header_desc),
562 .bDescriptorType = USB_DT_CS_INTERFACE,
563 .bDescriptorSubType = USB_CDC_SUBTYPE_HEADER,
564 .bcdCDC = __constant_cpu_to_le16(0x0110),
567 static const struct usb_cdc_call_mgmt_desc gs_call_mgmt_descriptor = {
568 .bLength = sizeof(gs_call_mgmt_descriptor),
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_SUBTYPE_CALL_MGMT,
572 .bDataInterface = 1, /* index of data interface */
575 static struct usb_cdc_acm_desc gs_acm_descriptor = {
576 .bLength = sizeof(gs_acm_descriptor),
577 .bDescriptorType = USB_DT_CS_INTERFACE,
578 .bDescriptorSubType = USB_CDC_SUBTYPE_ACM,
582 static const struct usb_cdc_union_desc gs_union_desc = {
583 .bLength = sizeof(gs_union_desc),
584 .bDescriptorType = USB_DT_CS_INTERFACE,
585 .bDescriptorSubType = USB_CDC_SUBTYPE_UNION,
586 .bMasterInterface0 = 0, /* index of control interface */
587 .bSlaveInterface0 = 1, /* index of data interface */
590 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
591 .bLength = USB_DT_ENDPOINT_SIZE,
592 .bDescriptorType = USB_DT_ENDPOINT,
593 .bEndpointAddress = USB_DIR_IN,
594 .bmAttributes = USB_ENDPOINT_XFER_INT,
595 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
596 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
599 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
600 .bLength = USB_DT_ENDPOINT_SIZE,
601 .bDescriptorType = USB_DT_ENDPOINT,
602 .bEndpointAddress = USB_DIR_IN,
603 .bmAttributes = USB_ENDPOINT_XFER_BULK,
606 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
607 .bLength = USB_DT_ENDPOINT_SIZE,
608 .bDescriptorType = USB_DT_ENDPOINT,
609 .bEndpointAddress = USB_DIR_OUT,
610 .bmAttributes = USB_ENDPOINT_XFER_BULK,
613 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
614 (struct usb_descriptor_header *) &gs_otg_descriptor,
615 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
616 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
617 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
621 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
622 (struct usb_descriptor_header *) &gs_otg_descriptor,
623 (struct usb_descriptor_header *) &gs_control_interface_desc,
624 (struct usb_descriptor_header *) &gs_header_desc,
625 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
626 (struct usb_descriptor_header *) &gs_acm_descriptor,
627 (struct usb_descriptor_header *) &gs_union_desc,
628 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
629 (struct usb_descriptor_header *) &gs_data_interface_desc,
630 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
631 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
635 #ifdef CONFIG_USB_GADGET_DUALSPEED
636 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
637 .bLength = USB_DT_ENDPOINT_SIZE,
638 .bDescriptorType = USB_DT_ENDPOINT,
639 .bEndpointAddress = USB_DIR_IN,
640 .bmAttributes = USB_ENDPOINT_XFER_INT,
641 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
642 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
645 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
646 .bLength = USB_DT_ENDPOINT_SIZE,
647 .bDescriptorType = USB_DT_ENDPOINT,
648 .bmAttributes = USB_ENDPOINT_XFER_BULK,
649 .wMaxPacketSize = __constant_cpu_to_le16(512),
652 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
653 .bLength = USB_DT_ENDPOINT_SIZE,
654 .bDescriptorType = USB_DT_ENDPOINT,
655 .bmAttributes = USB_ENDPOINT_XFER_BULK,
656 .wMaxPacketSize = __constant_cpu_to_le16(512),
659 static struct usb_qualifier_descriptor gs_qualifier_desc = {
660 .bLength = sizeof(struct usb_qualifier_descriptor),
661 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
662 .bcdUSB = __constant_cpu_to_le16 (0x0200),
663 /* assumes ep0 uses the same value for both speeds ... */
664 .bNumConfigurations = GS_NUM_CONFIGS,
667 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
668 (struct usb_descriptor_header *) &gs_otg_descriptor,
669 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
670 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
671 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
675 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
676 (struct usb_descriptor_header *) &gs_otg_descriptor,
677 (struct usb_descriptor_header *) &gs_control_interface_desc,
678 (struct usb_descriptor_header *) &gs_header_desc,
679 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
680 (struct usb_descriptor_header *) &gs_acm_descriptor,
681 (struct usb_descriptor_header *) &gs_union_desc,
682 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
683 (struct usb_descriptor_header *) &gs_data_interface_desc,
684 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
685 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
689 #endif /* CONFIG_USB_GADGET_DUALSPEED */
693 MODULE_DESCRIPTION(GS_LONG_NAME);
694 MODULE_AUTHOR("Al Borchers");
695 MODULE_LICENSE("GPL");
698 module_param(debug, int, S_IRUGO|S_IWUSR);
699 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
702 module_param(read_q_size, uint, S_IRUGO);
703 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
705 module_param(write_q_size, uint, S_IRUGO);
706 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
708 module_param(write_buf_size, uint, S_IRUGO);
709 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
711 module_param(use_acm, uint, S_IRUGO);
712 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
714 module_init(gs_module_init);
715 module_exit(gs_module_exit);
720 * Register as a USB gadget driver and a tty driver.
722 static int __init gs_module_init(void)
727 retval = usb_gadget_register_driver(&gs_gadget_driver);
729 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
733 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
736 gs_tty_driver->owner = THIS_MODULE;
737 gs_tty_driver->driver_name = GS_SHORT_NAME;
738 gs_tty_driver->name = "ttygs";
739 gs_tty_driver->devfs_name = "usb/ttygs/";
740 gs_tty_driver->major = GS_MAJOR;
741 gs_tty_driver->minor_start = GS_MINOR_START;
742 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
743 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
744 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
745 gs_tty_driver->init_termios = tty_std_termios;
746 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
747 tty_set_operations(gs_tty_driver, &gs_tty_ops);
749 for (i=0; i < GS_NUM_PORTS; i++)
750 sema_init(&gs_open_close_sem[i], 1);
752 retval = tty_register_driver(gs_tty_driver);
754 usb_gadget_unregister_driver(&gs_gadget_driver);
755 put_tty_driver(gs_tty_driver);
756 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
760 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
767 * Unregister as a tty driver and a USB gadget driver.
769 static void __exit gs_module_exit(void)
771 tty_unregister_driver(gs_tty_driver);
772 put_tty_driver(gs_tty_driver);
773 usb_gadget_unregister_driver(&gs_gadget_driver);
775 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
783 static int gs_open(struct tty_struct *tty, struct file *file)
787 struct gs_port *port;
790 struct semaphore *sem;
793 port_num = tty->index;
795 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
797 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
798 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
799 port_num, tty, file);
806 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
807 port_num, tty, file);
811 sem = &gs_open_close_sem[port_num];
812 if (down_interruptible(sem)) {
814 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
815 port_num, tty, file);
819 spin_lock_irqsave(&dev->dev_lock, flags);
821 if (dev->dev_config == GS_NO_CONFIG_ID) {
823 "gs_open: (%d,%p,%p) device is not connected\n",
824 port_num, tty, file);
826 goto exit_unlock_dev;
829 port = dev->dev_port[port_num];
832 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
833 port_num, tty, file);
835 goto exit_unlock_dev;
838 spin_lock(&port->port_lock);
839 spin_unlock(&dev->dev_lock);
841 if (port->port_dev == NULL) {
842 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
843 port_num, tty, file);
845 goto exit_unlock_port;
848 if (port->port_open_count > 0) {
849 ++port->port_open_count;
850 gs_debug("gs_open: (%d,%p,%p) already open\n",
851 port_num, tty, file);
853 goto exit_unlock_port;
856 tty->driver_data = NULL;
858 /* mark port as in use, we can drop port lock and sleep if necessary */
859 port->port_in_use = 1;
861 /* allocate write buffer on first open */
862 if (port->port_write_buf == NULL) {
863 spin_unlock_irqrestore(&port->port_lock, flags);
864 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
865 spin_lock_irqsave(&port->port_lock, flags);
867 /* might have been disconnected while asleep, check */
868 if (port->port_dev == NULL) {
870 "gs_open: (%d,%p,%p) port disconnected (2)\n",
871 port_num, tty, file);
872 port->port_in_use = 0;
874 goto exit_unlock_port;
877 if ((port->port_write_buf=buf) == NULL) {
878 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
879 port_num, tty, file);
880 port->port_in_use = 0;
882 goto exit_unlock_port;
887 /* wait for carrier detect (not implemented) */
889 /* might have been disconnected while asleep, check */
890 if (port->port_dev == NULL) {
891 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
892 port_num, tty, file);
893 port->port_in_use = 0;
895 goto exit_unlock_port;
898 tty->driver_data = port;
899 port->port_tty = tty;
900 port->port_open_count = 1;
901 port->port_in_use = 0;
903 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
908 spin_unlock_irqrestore(&port->port_lock, flags);
913 spin_unlock_irqrestore(&dev->dev_lock, flags);
922 static void gs_close(struct tty_struct *tty, struct file *file)
925 struct gs_port *port = tty->driver_data;
926 struct semaphore *sem;
929 printk(KERN_ERR "gs_close: NULL port pointer\n");
933 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
935 sem = &gs_open_close_sem[port->port_num];
938 spin_lock_irqsave(&port->port_lock, flags);
940 if (port->port_open_count == 0) {
942 "gs_close: (%d,%p,%p) port is already closed\n",
943 port->port_num, tty, file);
947 if (port->port_open_count > 1) {
948 --port->port_open_count;
952 /* free disconnected port on final close */
953 if (port->port_dev == NULL) {
958 /* mark port as closed but in use, we can drop port lock */
959 /* and sleep if necessary */
960 port->port_in_use = 1;
961 port->port_open_count = 0;
963 /* wait for write buffer to drain, or */
964 /* at most GS_CLOSE_TIMEOUT seconds */
965 if (gs_buf_data_avail(port->port_write_buf) > 0) {
966 wait_cond_interruptible_timeout(port->port_write_wait,
967 port->port_dev == NULL
968 || gs_buf_data_avail(port->port_write_buf) == 0,
969 &port->port_lock, flags, GS_CLOSE_TIMEOUT * HZ);
972 /* free disconnected port on final close */
973 /* (might have happened during the above sleep) */
974 if (port->port_dev == NULL) {
979 gs_buf_clear(port->port_write_buf);
981 tty->driver_data = NULL;
982 port->port_tty = NULL;
983 port->port_in_use = 0;
985 gs_debug("gs_close: (%d,%p,%p) completed\n",
986 port->port_num, tty, file);
989 spin_unlock_irqrestore(&port->port_lock, flags);
996 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
999 struct gs_port *port = tty->driver_data;
1003 printk(KERN_ERR "gs_write: NULL port pointer\n");
1007 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
1013 spin_lock_irqsave(&port->port_lock, flags);
1015 if (port->port_dev == NULL) {
1016 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
1017 port->port_num, tty);
1022 if (port->port_open_count == 0) {
1023 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
1024 port->port_num, tty);
1029 count = gs_buf_put(port->port_write_buf, buf, count);
1031 spin_unlock_irqrestore(&port->port_lock, flags);
1035 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
1041 spin_unlock_irqrestore(&port->port_lock, flags);
1048 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
1050 unsigned long flags;
1051 struct gs_port *port = tty->driver_data;
1054 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
1058 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));
1060 spin_lock_irqsave(&port->port_lock, flags);
1062 if (port->port_dev == NULL) {
1063 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
1064 port->port_num, tty);
1068 if (port->port_open_count == 0) {
1069 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
1070 port->port_num, tty);
1074 gs_buf_put(port->port_write_buf, &ch, 1);
1077 spin_unlock_irqrestore(&port->port_lock, flags);
1083 static void gs_flush_chars(struct tty_struct *tty)
1085 unsigned long flags;
1086 struct gs_port *port = tty->driver_data;
1089 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
1093 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
1095 spin_lock_irqsave(&port->port_lock, flags);
1097 if (port->port_dev == NULL) {
1099 "gs_flush_chars: (%d,%p) port is not connected\n",
1100 port->port_num, tty);
1104 if (port->port_open_count == 0) {
1105 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
1106 port->port_num, tty);
1110 spin_unlock_irqrestore(&port->port_lock, flags);
1117 spin_unlock_irqrestore(&port->port_lock, flags);
1123 static int gs_write_room(struct tty_struct *tty)
1127 unsigned long flags;
1128 struct gs_port *port = tty->driver_data;
1134 spin_lock_irqsave(&port->port_lock, flags);
1136 if (port->port_dev != NULL && port->port_open_count > 0
1137 && port->port_write_buf != NULL)
1138 room = gs_buf_space_avail(port->port_write_buf);
1140 spin_unlock_irqrestore(&port->port_lock, flags);
1142 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1143 port->port_num, tty, room);
1149 * gs_chars_in_buffer
1151 static int gs_chars_in_buffer(struct tty_struct *tty)
1154 unsigned long flags;
1155 struct gs_port *port = tty->driver_data;
1160 spin_lock_irqsave(&port->port_lock, flags);
1162 if (port->port_dev != NULL && port->port_open_count > 0
1163 && port->port_write_buf != NULL)
1164 chars = gs_buf_data_avail(port->port_write_buf);
1166 spin_unlock_irqrestore(&port->port_lock, flags);
1168 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1169 port->port_num, tty, chars);
1177 static void gs_throttle(struct tty_struct *tty)
1184 static void gs_unthrottle(struct tty_struct *tty)
1191 static void gs_break(struct tty_struct *tty, int break_state)
1198 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1200 struct gs_port *port = tty->driver_data;
1203 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1207 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1208 port->port_num, tty, file, cmd, arg);
1212 /* could not handle ioctl */
1213 return -ENOIOCTLCMD;
1219 static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1226 * This function finds available write requests, calls
1227 * gs_send_packet to fill these packets with data, and
1228 * continues until either there are no more write requests
1229 * available or no more data to send. This function is
1230 * run whenever data arrives or write requests are available.
1232 static int gs_send(struct gs_dev *dev)
1235 unsigned long flags;
1237 struct usb_request *req;
1238 struct gs_req_entry *req_entry;
1241 printk(KERN_ERR "gs_send: NULL device pointer\n");
1245 spin_lock_irqsave(&dev->dev_lock, flags);
1247 ep = dev->dev_in_ep;
1249 while(!list_empty(&dev->dev_req_list)) {
1251 req_entry = list_entry(dev->dev_req_list.next,
1252 struct gs_req_entry, re_entry);
1254 req = req_entry->re_req;
1256 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1259 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));
1260 list_del(&req_entry->re_entry);
1262 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1264 "gs_send: cannot queue read request, ret=%d\n",
1274 spin_unlock_irqrestore(&dev->dev_lock, flags);
1282 * If there is data to send, a packet is built in the given
1283 * buffer and the size is returned. If there is no data to
1284 * send, 0 is returned. If there is any error a negative
1285 * error number is returned.
1287 * Called during USB completion routine, on interrupt time.
1289 * We assume that disconnect will not happen until all completion
1290 * routines have completed, so we can assume that the dev_port
1291 * array does not change during the lifetime of this function.
1293 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1296 struct gs_port *port;
1298 /* TEMPORARY -- only port 0 is supported right now */
1299 port = dev->dev_port[0];
1303 "gs_send_packet: port=%d, NULL port pointer\n",
1308 spin_lock(&port->port_lock);
1310 len = gs_buf_data_avail(port->port_write_buf);
1317 size = gs_buf_get(port->port_write_buf, packet, size);
1320 wake_up_interruptible(&port->port_tty->write_wait);
1323 spin_unlock(&port->port_lock);
1330 * Called for each USB packet received. Reads the packet
1331 * header and stuffs the data in the appropriate tty buffer.
1332 * Returns 0 if successful, or a negative error number.
1334 * Called during USB completion routine, on interrupt time.
1336 * We assume that disconnect will not happen until all completion
1337 * routines have completed, so we can assume that the dev_port
1338 * array does not change during the lifetime of this function.
1340 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1343 struct gs_port *port;
1346 /* TEMPORARY -- only port 0 is supported right now */
1347 port = dev->dev_port[0];
1350 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1355 spin_lock(&port->port_lock);
1357 if (port->port_open_count == 0) {
1358 printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",
1364 if (port->port_tty == NULL) {
1365 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1371 if (port->port_tty->magic != TTY_MAGIC) {
1372 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1378 len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);
1383 memcpy(port->port_tty->flip.char_buf_ptr, packet, size);
1384 port->port_tty->flip.char_buf_ptr += size;
1385 port->port_tty->flip.count += size;
1386 tty_flip_buffer_push(port->port_tty);
1387 wake_up_interruptible(&port->port_tty->read_wait);
1393 spin_unlock(&port->port_lock);
1400 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1403 struct gs_dev *dev = ep->driver_data;
1406 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1410 switch(req->status) {
1412 /* normal completion */
1413 gs_recv_packet(dev, req->buf, req->actual);
1415 req->length = ep->maxpacket;
1416 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1418 "gs_read_complete: cannot queue read request, ret=%d\n",
1425 gs_debug("gs_read_complete: shutdown\n");
1426 gs_free_req(ep, req);
1432 "gs_read_complete: unexpected status error, status=%d\n",
1442 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1444 struct gs_dev *dev = ep->driver_data;
1445 struct gs_req_entry *gs_req = req->context;
1448 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1452 switch(req->status) {
1454 /* normal completion */
1456 if (gs_req == NULL) {
1458 "gs_write_complete: NULL request pointer\n");
1462 spin_lock(&dev->dev_lock);
1463 list_add(&gs_req->re_entry, &dev->dev_req_list);
1464 spin_unlock(&dev->dev_lock);
1472 gs_debug("gs_write_complete: shutdown\n");
1473 gs_free_req(ep, req);
1478 "gs_write_complete: unexpected status error, status=%d\n",
1490 * Called on module load. Allocates and initializes the device
1491 * structure and a control request.
1493 static int gs_bind(struct usb_gadget *gadget)
1499 /* device specific */
1500 if (gadget_is_net2280(gadget)) {
1501 gs_device_desc.bcdDevice =
1502 __constant_cpu_to_le16(GS_VERSION_NUM|0x0001);
1503 } else if (gadget_is_pxa(gadget)) {
1504 gs_device_desc.bcdDevice =
1505 __constant_cpu_to_le16(GS_VERSION_NUM|0x0002);
1506 } else if (gadget_is_sh(gadget)) {
1507 gs_device_desc.bcdDevice =
1508 __constant_cpu_to_le16(GS_VERSION_NUM|0x0003);
1509 /* sh doesn't support multiple interfaces or configs */
1511 } else if (gadget_is_sa1100(gadget)) {
1512 gs_device_desc.bcdDevice =
1513 __constant_cpu_to_le16(GS_VERSION_NUM|0x0004);
1514 /* sa1100 doesn't support necessary endpoints */
1516 } else if (gadget_is_goku(gadget)) {
1517 gs_device_desc.bcdDevice =
1518 __constant_cpu_to_le16(GS_VERSION_NUM|0x0005);
1519 } else if (gadget_is_mq11xx(gadget)) {
1520 gs_device_desc.bcdDevice =
1521 __constant_cpu_to_le16(GS_VERSION_NUM|0x0006);
1522 } else if (gadget_is_omap(gadget)) {
1523 gs_device_desc.bcdDevice =
1524 __constant_cpu_to_le16(GS_VERSION_NUM|0x0007);
1525 } else if (gadget_is_lh7a40x(gadget)) {
1526 gs_device_desc.bcdDevice =
1527 __constant_cpu_to_le16(GS_VERSION_NUM|0x0008);
1528 } else if (gadget_is_n9604(gadget)) {
1529 gs_device_desc.bcdDevice =
1530 __constant_cpu_to_le16(GS_VERSION_NUM|0x0009);
1531 } else if (gadget_is_pxa27x(gadget)) {
1532 gs_device_desc.bcdDevice =
1533 __constant_cpu_to_le16(GS_VERSION_NUM|0x0011);
1535 printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
1537 /* unrecognized, but safe unless bulk is REALLY quirky */
1538 gs_device_desc.bcdDevice =
1539 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1542 usb_ep_autoconfig_reset(gadget);
1544 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1547 EP_IN_NAME = ep->name;
1548 ep->driver_data = ep; /* claim the endpoint */
1550 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1553 EP_OUT_NAME = ep->name;
1554 ep->driver_data = ep; /* claim the endpoint */
1557 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1559 printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);
1562 gs_device_desc.idProduct = __constant_cpu_to_le16(
1564 EP_NOTIFY_NAME = ep->name;
1565 ep->driver_data = ep; /* claim the endpoint */
1568 gs_device_desc.bDeviceClass = use_acm
1569 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1570 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1572 #ifdef CONFIG_USB_GADGET_DUALSPEED
1573 gs_qualifier_desc.bDeviceClass = use_acm
1574 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1575 /* assume ep0 uses the same packet size for both speeds */
1576 gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
1577 /* assume endpoints are dual-speed */
1578 gs_highspeed_notify_desc.bEndpointAddress =
1579 gs_fullspeed_notify_desc.bEndpointAddress;
1580 gs_highspeed_in_desc.bEndpointAddress =
1581 gs_fullspeed_in_desc.bEndpointAddress;
1582 gs_highspeed_out_desc.bEndpointAddress =
1583 gs_fullspeed_out_desc.bEndpointAddress;
1584 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1586 usb_gadget_set_selfpowered(gadget);
1588 if (gadget->is_otg) {
1589 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1590 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1591 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1594 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1598 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1599 system_utsname.sysname, system_utsname.release,
1602 memset(dev, 0, sizeof(struct gs_dev));
1603 dev->dev_gadget = gadget;
1604 spin_lock_init(&dev->dev_lock);
1605 INIT_LIST_HEAD(&dev->dev_req_list);
1606 set_gadget_data(gadget, dev);
1608 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1609 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1614 /* preallocate control response and buffer */
1615 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1617 if (dev->dev_ctrl_req == NULL) {
1621 dev->dev_ctrl_req->complete = gs_setup_complete;
1623 gadget->ep0->driver_data = dev;
1625 printk(KERN_INFO "gs_bind: %s %s bound\n",
1626 GS_LONG_NAME, GS_VERSION_STR);
1631 printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);
1638 * Called on module unload. Frees the control request and device
1641 static void gs_unbind(struct usb_gadget *gadget)
1643 struct gs_dev *dev = get_gadget_data(gadget);
1647 /* read/write requests already freed, only control request remains */
1649 if (dev->dev_ctrl_req != NULL) {
1650 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1651 dev->dev_ctrl_req = NULL;
1655 set_gadget_data(gadget, NULL);
1658 printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1665 * Implements all the control endpoint functionality that's not
1666 * handled in hardware or the hardware driver.
1668 * Returns the size of the data sent to the host, or a negative
1671 static int gs_setup(struct usb_gadget *gadget,
1672 const struct usb_ctrlrequest *ctrl)
1674 int ret = -EOPNOTSUPP;
1675 struct gs_dev *dev = get_gadget_data(gadget);
1676 struct usb_request *req = dev->dev_ctrl_req;
1678 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1679 case USB_TYPE_STANDARD:
1680 ret = gs_setup_standard(gadget,ctrl);
1683 case USB_TYPE_CLASS:
1684 ret = gs_setup_class(gadget,ctrl);
1688 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1689 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1690 ctrl->wIndex, ctrl->wLength);
1694 /* respond with data transfer before status phase? */
1697 req->zero = ret < ctrl->wLength
1698 && (ret % gadget->ep0->maxpacket) == 0;
1699 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1701 printk(KERN_ERR "gs_setup: cannot queue response, ret=%d\n",
1704 gs_setup_complete(gadget->ep0, req);
1708 /* device either stalls (ret < 0) or reports success */
1712 static int gs_setup_standard(struct usb_gadget *gadget,
1713 const struct usb_ctrlrequest *ctrl)
1715 int ret = -EOPNOTSUPP;
1716 struct gs_dev *dev = get_gadget_data(gadget);
1717 struct usb_request *req = dev->dev_ctrl_req;
1719 switch (ctrl->bRequest) {
1720 case USB_REQ_GET_DESCRIPTOR:
1721 if (ctrl->bRequestType != USB_DIR_IN)
1724 switch (ctrl->wValue >> 8) {
1726 ret = min(ctrl->wLength,
1727 (u16)sizeof(struct usb_device_descriptor));
1728 memcpy(req->buf, &gs_device_desc, ret);
1731 #ifdef CONFIG_USB_GADGET_DUALSPEED
1732 case USB_DT_DEVICE_QUALIFIER:
1733 if (!gadget->is_dualspeed)
1735 ret = min(ctrl->wLength,
1736 (u16)sizeof(struct usb_qualifier_descriptor));
1737 memcpy(req->buf, &gs_qualifier_desc, ret);
1740 case USB_DT_OTHER_SPEED_CONFIG:
1741 if (!gadget->is_dualspeed)
1744 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1746 ret = gs_build_config_buf(req->buf, gadget->speed,
1747 ctrl->wValue >> 8, ctrl->wValue & 0xff,
1750 ret = min(ctrl->wLength, (u16)ret);
1754 /* wIndex == language code. */
1755 ret = usb_gadget_get_string(&gs_string_table,
1756 ctrl->wValue & 0xff, req->buf);
1758 ret = min(ctrl->wLength, (u16)ret);
1763 case USB_REQ_SET_CONFIGURATION:
1764 if (ctrl->bRequestType != 0)
1766 spin_lock(&dev->dev_lock);
1767 ret = gs_set_config(dev, ctrl->wValue);
1768 spin_unlock(&dev->dev_lock);
1771 case USB_REQ_GET_CONFIGURATION:
1772 if (ctrl->bRequestType != USB_DIR_IN)
1774 *(u8 *)req->buf = dev->dev_config;
1775 ret = min(ctrl->wLength, (u16)1);
1778 case USB_REQ_SET_INTERFACE:
1779 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1780 || !dev->dev_config || ctrl->wIndex >= GS_MAX_NUM_INTERFACES)
1782 if (dev->dev_config == GS_BULK_CONFIG_ID
1783 && ctrl->wIndex != GS_BULK_INTERFACE_ID)
1785 /* no alternate interface settings */
1786 if (ctrl->wValue != 0)
1788 spin_lock(&dev->dev_lock);
1789 /* PXA hardware partially handles SET_INTERFACE;
1790 * we need to kluge around that interference. */
1791 if (gadget_is_pxa(gadget)) {
1792 ret = gs_set_config(dev, use_acm ?
1793 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1794 goto set_interface_done;
1796 if (dev->dev_config != GS_BULK_CONFIG_ID
1797 && ctrl->wIndex == GS_CONTROL_INTERFACE_ID) {
1798 if (dev->dev_notify_ep) {
1799 usb_ep_disable(dev->dev_notify_ep);
1800 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1803 usb_ep_disable(dev->dev_in_ep);
1804 usb_ep_disable(dev->dev_out_ep);
1805 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1806 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1810 spin_unlock(&dev->dev_lock);
1813 case USB_REQ_GET_INTERFACE:
1814 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1815 || dev->dev_config == GS_NO_CONFIG_ID)
1817 if (ctrl->wIndex >= GS_MAX_NUM_INTERFACES
1818 || (dev->dev_config == GS_BULK_CONFIG_ID
1819 && ctrl->wIndex != GS_BULK_INTERFACE_ID)) {
1823 /* no alternate interface settings */
1824 *(u8 *)req->buf = 0;
1825 ret = min(ctrl->wLength, (u16)1);
1829 printk(KERN_ERR "gs_setup: unknown standard request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1830 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1831 ctrl->wIndex, ctrl->wLength);
1838 static int gs_setup_class(struct usb_gadget *gadget,
1839 const struct usb_ctrlrequest *ctrl)
1841 int ret = -EOPNOTSUPP;
1842 struct gs_dev *dev = get_gadget_data(gadget);
1843 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1844 struct usb_request *req = dev->dev_ctrl_req;
1846 switch (ctrl->bRequest) {
1847 case USB_CDC_REQ_SET_LINE_CODING:
1848 ret = min(ctrl->wLength,
1849 (u16)sizeof(struct usb_cdc_line_coding));
1851 spin_lock(&port->port_lock);
1852 memcpy(&port->port_line_coding, req->buf, ret);
1853 spin_unlock(&port->port_lock);
1857 case USB_CDC_REQ_GET_LINE_CODING:
1858 port = dev->dev_port[0]; /* ACM only has one port */
1859 ret = min(ctrl->wLength,
1860 (u16)sizeof(struct usb_cdc_line_coding));
1862 spin_lock(&port->port_lock);
1863 memcpy(req->buf, &port->port_line_coding, ret);
1864 spin_unlock(&port->port_lock);
1868 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1873 printk(KERN_ERR "gs_setup: unknown class request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1874 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1875 ctrl->wIndex, ctrl->wLength);
1885 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1887 if (req->status || req->actual != req->length) {
1888 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1889 req->status, req->actual, req->length);
1896 * Called when the device is disconnected. Frees the closed
1897 * ports and disconnects open ports. Open ports will be freed
1898 * on close. Then reallocates the ports for the next connection.
1900 static void gs_disconnect(struct usb_gadget *gadget)
1902 unsigned long flags;
1903 struct gs_dev *dev = get_gadget_data(gadget);
1905 spin_lock_irqsave(&dev->dev_lock, flags);
1907 gs_reset_config(dev);
1909 /* free closed ports and disconnect open ports */
1910 /* (open ports will be freed when closed) */
1913 /* re-allocate ports for the next connection */
1914 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1915 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1917 spin_unlock_irqrestore(&dev->dev_lock, flags);
1919 printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1925 * Configures the device by enabling device specific
1926 * optimizations, setting up the endpoints, allocating
1927 * read and write requests and queuing read requests.
1929 * The device lock must be held when calling this function.
1931 static int gs_set_config(struct gs_dev *dev, unsigned config)
1935 struct usb_gadget *gadget = dev->dev_gadget;
1937 struct usb_endpoint_descriptor *ep_desc;
1938 struct usb_request *req;
1939 struct gs_req_entry *req_entry;
1942 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1946 if (config == dev->dev_config)
1949 gs_reset_config(dev);
1952 case GS_NO_CONFIG_ID:
1954 case GS_BULK_CONFIG_ID:
1957 /* device specific optimizations */
1958 if (gadget_is_net2280(gadget))
1959 net2280_set_fifo_mode(gadget, 1);
1961 case GS_ACM_CONFIG_ID:
1964 /* device specific optimizations */
1965 if (gadget_is_net2280(gadget))
1966 net2280_set_fifo_mode(gadget, 1);
1972 dev->dev_config = config;
1974 gadget_for_each_ep(ep, gadget) {
1977 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1978 ep_desc = GS_SPEED_SELECT(
1979 gadget->speed == USB_SPEED_HIGH,
1980 &gs_highspeed_notify_desc,
1981 &gs_fullspeed_notify_desc);
1982 ret = usb_ep_enable(ep,ep_desc);
1984 ep->driver_data = dev;
1985 dev->dev_notify_ep = ep;
1986 dev->dev_notify_ep_desc = ep_desc;
1988 printk(KERN_ERR "gs_set_config: cannot enable notify endpoint %s, ret=%d\n",
1990 goto exit_reset_config;
1994 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1995 ep_desc = GS_SPEED_SELECT(
1996 gadget->speed == USB_SPEED_HIGH,
1997 &gs_highspeed_in_desc,
1998 &gs_fullspeed_in_desc);
1999 ret = usb_ep_enable(ep,ep_desc);
2001 ep->driver_data = dev;
2002 dev->dev_in_ep = ep;
2003 dev->dev_in_ep_desc = ep_desc;
2005 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
2007 goto exit_reset_config;
2011 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
2012 ep_desc = GS_SPEED_SELECT(
2013 gadget->speed == USB_SPEED_HIGH,
2014 &gs_highspeed_out_desc,
2015 &gs_fullspeed_out_desc);
2016 ret = usb_ep_enable(ep,ep_desc);
2018 ep->driver_data = dev;
2019 dev->dev_out_ep = ep;
2020 dev->dev_out_ep_desc = ep_desc;
2022 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
2024 goto exit_reset_config;
2030 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
2031 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
2032 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
2034 goto exit_reset_config;
2037 /* allocate and queue read requests */
2038 ep = dev->dev_out_ep;
2039 for (i=0; i<read_q_size && ret == 0; i++) {
2040 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
2041 req->complete = gs_read_complete;
2042 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
2043 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
2047 printk(KERN_ERR "gs_set_config: cannot allocate read requests\n");
2049 goto exit_reset_config;
2053 /* allocate write requests, and put on free list */
2054 ep = dev->dev_in_ep;
2055 for (i=0; i<write_q_size; i++) {
2056 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
2057 req_entry->re_req->complete = gs_write_complete;
2058 list_add(&req_entry->re_entry, &dev->dev_req_list);
2060 printk(KERN_ERR "gs_set_config: cannot allocate write requests\n");
2062 goto exit_reset_config;
2066 printk(KERN_INFO "gs_set_config: %s configured, %s speed %s config\n",
2068 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
2069 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
2074 gs_reset_config(dev);
2081 * Mark the device as not configured, disable all endpoints,
2082 * which forces completion of pending I/O and frees queued
2083 * requests, and free the remaining write requests on the
2086 * The device lock must be held when calling this function.
2088 static void gs_reset_config(struct gs_dev *dev)
2090 struct gs_req_entry *req_entry;
2093 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
2097 if (dev->dev_config == GS_NO_CONFIG_ID)
2100 dev->dev_config = GS_NO_CONFIG_ID;
2102 /* free write requests on the free list */
2103 while(!list_empty(&dev->dev_req_list)) {
2104 req_entry = list_entry(dev->dev_req_list.next,
2105 struct gs_req_entry, re_entry);
2106 list_del(&req_entry->re_entry);
2107 gs_free_req_entry(dev->dev_in_ep, req_entry);
2110 /* disable endpoints, forcing completion of pending i/o; */
2111 /* completion handlers free their requests in this case */
2112 if (dev->dev_notify_ep) {
2113 usb_ep_disable(dev->dev_notify_ep);
2114 dev->dev_notify_ep = NULL;
2116 if (dev->dev_in_ep) {
2117 usb_ep_disable(dev->dev_in_ep);
2118 dev->dev_in_ep = NULL;
2120 if (dev->dev_out_ep) {
2121 usb_ep_disable(dev->dev_out_ep);
2122 dev->dev_out_ep = NULL;
2127 * gs_build_config_buf
2129 * Builds the config descriptors in the given buffer and returns the
2130 * length, or a negative error number.
2132 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
2133 u8 type, unsigned int index, int is_otg)
2137 const struct usb_config_descriptor *config_desc;
2138 const struct usb_descriptor_header **function;
2140 if (index >= gs_device_desc.bNumConfigurations)
2143 /* other speed switches high and full speed */
2144 high_speed = (speed == USB_SPEED_HIGH);
2145 if (type == USB_DT_OTHER_SPEED_CONFIG)
2146 high_speed = !high_speed;
2149 config_desc = &gs_acm_config_desc;
2150 function = GS_SPEED_SELECT(high_speed,
2151 gs_acm_highspeed_function,
2152 gs_acm_fullspeed_function);
2154 config_desc = &gs_bulk_config_desc;
2155 function = GS_SPEED_SELECT(high_speed,
2156 gs_bulk_highspeed_function,
2157 gs_bulk_fullspeed_function);
2160 /* for now, don't advertise srp-only devices */
2164 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2168 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2176 * Allocate a usb_request and its buffer. Returns a pointer to the
2177 * usb_request or NULL if there is an error.
2179 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, int kmalloc_flags)
2181 struct usb_request *req;
2186 req = usb_ep_alloc_request(ep, kmalloc_flags);
2190 req->buf = kmalloc(len, kmalloc_flags);
2191 if (req->buf == NULL) {
2192 usb_ep_free_request(ep, req);
2203 * Free a usb_request and its buffer.
2205 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2207 if (ep != NULL && req != NULL) {
2209 usb_ep_free_request(ep, req);
2214 * gs_alloc_req_entry
2216 * Allocates a request and its buffer, using the given
2217 * endpoint, buffer len, and kmalloc flags.
2219 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len, int kmalloc_flags)
2221 struct gs_req_entry *req;
2223 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2227 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2228 if (req->re_req == NULL) {
2233 req->re_req->context = req;
2241 * Frees a request and its buffer.
2243 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2245 if (ep != NULL && req != NULL) {
2246 if (req->re_req != NULL)
2247 gs_free_req(ep, req->re_req);
2255 * Allocate all ports and set the gs_dev struct to point to them.
2256 * Return 0 if successful, or a negative error number.
2258 * The device lock is normally held when calling this function.
2260 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags)
2263 struct gs_port *port;
2268 for (i=0; i<GS_NUM_PORTS; i++) {
2269 if ((port=(struct gs_port *)kmalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2272 memset(port, 0, sizeof(struct gs_port));
2273 port->port_dev = dev;
2275 port->port_line_coding.dwDTERate = GS_DEFAULT_DTE_RATE;
2276 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2277 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2278 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2279 spin_lock_init(&port->port_lock);
2280 init_waitqueue_head(&port->port_write_wait);
2282 dev->dev_port[i] = port;
2291 * Free all closed ports. Open ports are disconnected by
2292 * freeing their write buffers, setting their device pointers
2293 * and the pointers to them in the device to NULL. These
2294 * ports will be freed when closed.
2296 * The device lock is normally held when calling this function.
2298 static void gs_free_ports(struct gs_dev *dev)
2301 unsigned long flags;
2302 struct gs_port *port;
2307 for (i=0; i<GS_NUM_PORTS; i++) {
2308 if ((port=dev->dev_port[i]) != NULL) {
2309 dev->dev_port[i] = NULL;
2311 spin_lock_irqsave(&port->port_lock, flags);
2313 if (port->port_write_buf != NULL) {
2314 gs_buf_free(port->port_write_buf);
2315 port->port_write_buf = NULL;
2318 if (port->port_open_count > 0 || port->port_in_use) {
2319 port->port_dev = NULL;
2320 wake_up_interruptible(&port->port_write_wait);
2321 if (port->port_tty) {
2322 wake_up_interruptible(&port->port_tty->read_wait);
2323 wake_up_interruptible(&port->port_tty->write_wait);
2325 spin_unlock_irqrestore(&port->port_lock, flags);
2334 /* Circular Buffer */
2339 * Allocate a circular buffer and all associated memory.
2341 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags)
2348 gb = (struct gs_buf *)kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2352 gb->buf_buf = kmalloc(size, kmalloc_flags);
2353 if (gb->buf_buf == NULL) {
2358 gb->buf_size = size;
2359 gb->buf_get = gb->buf_put = gb->buf_buf;
2367 * Free the buffer and all associated memory.
2369 void gs_buf_free(struct gs_buf *gb)
2372 if (gb->buf_buf != NULL)
2381 * Clear out all data in the circular buffer.
2383 void gs_buf_clear(struct gs_buf *gb)
2386 gb->buf_get = gb->buf_put;
2387 /* equivalent to a get of all data available */
2393 * Return the number of bytes of data available in the circular
2396 unsigned int gs_buf_data_avail(struct gs_buf *gb)
2399 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2405 * gs_buf_space_avail
2407 * Return the number of bytes of space available in the circular
2410 unsigned int gs_buf_space_avail(struct gs_buf *gb)
2413 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2421 * Copy data data from a user buffer and put it into the circular buffer.
2422 * Restrict to the amount of space available.
2424 * Return the number of bytes copied.
2426 unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2433 len = gs_buf_space_avail(gb);
2440 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2442 memcpy(gb->buf_put, buf, len);
2443 memcpy(gb->buf_buf, buf+len, count - len);
2444 gb->buf_put = gb->buf_buf + count - len;
2446 memcpy(gb->buf_put, buf, count);
2448 gb->buf_put += count;
2449 else /* count == len */
2450 gb->buf_put = gb->buf_buf;
2459 * Get data from the circular buffer and copy to the given buffer.
2460 * Restrict to the amount of data available.
2462 * Return the number of bytes copied.
2464 unsigned int gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2471 len = gs_buf_data_avail(gb);
2478 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2480 memcpy(buf, gb->buf_get, len);
2481 memcpy(buf+len, gb->buf_buf, count - len);
2482 gb->buf_get = gb->buf_buf + count - len;
2484 memcpy(buf, gb->buf_get, count);
2486 gb->buf_get += count;
2487 else /* count == len */
2488 gb->buf_get = gb->buf_buf;