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/uts.h>
34 #include <linux/version.h>
35 #include <linux/wait.h>
36 #include <linux/proc_fs.h>
37 #include <linux/device.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
41 #include <asm/byteorder.h>
44 #include <asm/system.h>
45 #include <asm/unaligned.h>
46 #include <asm/uaccess.h>
48 #include <linux/usb_ch9.h>
49 #include <linux/usb_gadget.h>
51 #include "gadget_chips.h"
56 #define __wait_cond_interruptible(wq, condition, lock, flags, ret) \
58 wait_queue_t __wait; \
59 init_waitqueue_entry(&__wait, current); \
61 add_wait_queue(&wq, &__wait); \
63 set_current_state(TASK_INTERRUPTIBLE); \
66 if (!signal_pending(current)) { \
67 spin_unlock_irqrestore(lock, flags); \
69 spin_lock_irqsave(lock, flags); \
75 current->state = TASK_RUNNING; \
76 remove_wait_queue(&wq, &__wait); \
79 #define wait_cond_interruptible(wq, condition, lock, flags) \
83 __wait_cond_interruptible(wq, condition, lock, flags, \
88 #define __wait_cond_interruptible_timeout(wq, condition, lock, flags, \
91 signed long __timeout = timeout; \
92 wait_queue_t __wait; \
93 init_waitqueue_entry(&__wait, current); \
95 add_wait_queue(&wq, &__wait); \
97 set_current_state(TASK_INTERRUPTIBLE); \
102 if (!signal_pending(current)) { \
103 spin_unlock_irqrestore(lock, flags); \
104 __timeout = schedule_timeout(__timeout); \
105 spin_lock_irqsave(lock, flags); \
108 ret = -ERESTARTSYS; \
111 current->state = TASK_RUNNING; \
112 remove_wait_queue(&wq, &__wait); \
115 #define wait_cond_interruptible_timeout(wq, condition, lock, flags, \
120 __wait_cond_interruptible_timeout(wq, condition, lock, \
121 flags, timeout, __ret); \
126 /* CDC-ACM Defines and Structures */
128 #define USB_CDC_SUBCLASS_ACM 2
130 #define USB_CDC_CTRL_PROTO_NONE 0
131 #define USB_CDC_CTRL_PROTO_AT 1
132 #define USB_CDC_CTRL_PROTO_VENDOR 0xff
134 #define USB_CDC_SUBTYPE_HEADER 0
135 #define USB_CDC_SUBTYPE_CALL_MGMT 1
136 #define USB_CDC_SUBTYPE_ACM 2
137 #define USB_CDC_SUBTYPE_UNION 6
139 #define USB_CDC_CALL_MGMT_CAP_CALL_MGMT 0x01
140 #define USB_CDC_CALL_MGMT_CAP_DATA_INTF 0x02
142 #define USB_CDC_REQ_SET_LINE_CODING 0x20
143 #define USB_CDC_REQ_GET_LINE_CODING 0x21
144 #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
146 #define USB_CDC_1_STOP_BITS 0
147 #define USB_CDC_1_5_STOP_BITS 1
148 #define USB_CDC_2_STOP_BITS 2
150 #define USB_CDC_NO_PARITY 0
151 #define USB_CDC_ODD_PARITY 1
152 #define USB_CDC_EVEN_PARITY 2
153 #define USB_CDC_MARK_PARITY 3
154 #define USB_CDC_SPACE_PARITY 4
156 /* Header Functional Descriptor from CDC spec 5.2.3.1 */
157 struct usb_cdc_header_desc {
160 u8 bDescriptorSubType;
162 } __attribute__ ((packed));
164 /* Call Management Descriptor from CDC spec 5.2.3.3 */
165 struct usb_cdc_call_mgmt_desc {
168 u8 bDescriptorSubType;
171 } __attribute__ ((packed));
173 /* Abstract Control Management Descriptor from CDC spec 5.2.3.4 */
174 struct usb_cdc_acm_desc {
177 u8 bDescriptorSubType;
179 } __attribute__ ((packed));
181 /* Union Functional Descriptor from CDC spec 5.2.3.8 */
182 struct usb_cdc_union_desc {
185 u8 bDescriptorSubType;
186 u8 bMasterInterface0;
188 /* ... and there could be other slave interfaces */
189 } __attribute__ ((packed));
191 /* Line Coding Structure from CDC spec 6.2.13 */
192 struct usb_cdc_line_coding {
197 } __attribute__ ((packed));
202 #define GS_VERSION_STR "v2.0"
203 #define GS_VERSION_NUM 0x0200
205 #define GS_LONG_NAME "Gadget Serial"
206 #define GS_SHORT_NAME "g_serial"
209 #define GS_MINOR_START 0
211 #define GS_NUM_PORTS 16
213 #define GS_NUM_CONFIGS 1
214 #define GS_NO_CONFIG_ID 0
215 #define GS_BULK_CONFIG_ID 1
216 #define GS_ACM_CONFIG_ID 2
218 #define GS_MAX_NUM_INTERFACES 2
219 #define GS_BULK_INTERFACE_ID 0
220 #define GS_CONTROL_INTERFACE_ID 0
221 #define GS_DATA_INTERFACE_ID 1
223 #define GS_MAX_DESC_LEN 256
225 #define GS_DEFAULT_READ_Q_SIZE 32
226 #define GS_DEFAULT_WRITE_Q_SIZE 32
228 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
229 #define GS_TMP_BUF_SIZE 8192
231 #define GS_CLOSE_TIMEOUT 15
233 #define GS_DEFAULT_USE_ACM 0
235 #define GS_DEFAULT_DTE_RATE 9600
236 #define GS_DEFAULT_DATA_BITS 8
237 #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
238 #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
240 /* select highspeed/fullspeed, hiding highspeed if not configured */
241 #ifdef CONFIG_USB_GADGET_DUALSPEED
242 #define GS_SPEED_SELECT(is_hs,hs,fs) ((is_hs) ? (hs) : (fs))
244 #define GS_SPEED_SELECT(is_hs,hs,fs) (fs)
245 #endif /* CONFIG_USB_GADGET_DUALSPEED */
249 static int debug = 1;
251 #define gs_debug(format, arg...) \
252 do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
253 #define gs_debug_level(level, format, arg...) \
254 do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
258 #define gs_debug(format, arg...) \
260 #define gs_debug_level(level, format, arg...) \
263 #endif /* GS_DEBUG */
265 /* Thanks to NetChip Technologies for donating this product ID.
267 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
268 * Instead: allocate your own, using normal USB-IF procedures.
270 #define GS_VENDOR_ID 0x0525 /* NetChip */
271 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
272 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
274 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
275 #define GS_NOTIFY_MAXPACKET 8
282 /* circular buffer */
284 unsigned int buf_size;
290 /* list of requests */
291 struct gs_req_entry {
292 struct list_head re_entry;
293 struct usb_request *re_req;
296 /* the port structure holds info for each port, one for each minor number */
298 struct gs_dev *port_dev; /* pointer to device struct */
299 struct tty_struct *port_tty; /* pointer to tty struct */
300 spinlock_t port_lock;
303 int port_in_use; /* open/close in progress */
304 wait_queue_head_t port_write_wait;/* waiting to write */
305 struct gs_buf *port_write_buf;
306 struct usb_cdc_line_coding port_line_coding;
309 /* the device structure holds info for the USB device */
311 struct usb_gadget *dev_gadget; /* gadget device pointer */
312 spinlock_t dev_lock; /* lock for set/reset config */
313 int dev_config; /* configuration number */
314 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
315 struct usb_ep *dev_in_ep; /* address of in endpoint */
316 struct usb_ep *dev_out_ep; /* address of out endpoint */
317 struct usb_endpoint_descriptor /* desciptor of notify ep */
319 struct usb_endpoint_descriptor /* descriptor of in endpoint */
321 struct usb_endpoint_descriptor /* descriptor of out endpoint */
323 struct usb_request *dev_ctrl_req; /* control request */
324 struct list_head dev_req_list; /* list of write requests */
325 int dev_sched_port; /* round robin port scheduled */
326 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
333 static int __init gs_module_init(void);
334 static void __exit gs_module_exit(void);
337 static int gs_open(struct tty_struct *tty, struct file *file);
338 static void gs_close(struct tty_struct *tty, struct file *file);
339 static int gs_write(struct tty_struct *tty,
340 const unsigned char *buf, int count);
341 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
342 static void gs_flush_chars(struct tty_struct *tty);
343 static int gs_write_room(struct tty_struct *tty);
344 static int gs_chars_in_buffer(struct tty_struct *tty);
345 static void gs_throttle(struct tty_struct * tty);
346 static void gs_unthrottle(struct tty_struct * tty);
347 static void gs_break(struct tty_struct *tty, int break_state);
348 static int gs_ioctl(struct tty_struct *tty, struct file *file,
349 unsigned int cmd, unsigned long arg);
350 static void gs_set_termios(struct tty_struct *tty, struct termios *old);
352 static int gs_send(struct gs_dev *dev);
353 static int gs_send_packet(struct gs_dev *dev, char *packet,
355 static int gs_recv_packet(struct gs_dev *dev, char *packet,
357 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
358 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
361 static int gs_bind(struct usb_gadget *gadget);
362 static void gs_unbind(struct usb_gadget *gadget);
363 static int gs_setup(struct usb_gadget *gadget,
364 const struct usb_ctrlrequest *ctrl);
365 static int gs_setup_standard(struct usb_gadget *gadget,
366 const struct usb_ctrlrequest *ctrl);
367 static int gs_setup_class(struct usb_gadget *gadget,
368 const struct usb_ctrlrequest *ctrl);
369 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
370 static void gs_disconnect(struct usb_gadget *gadget);
371 static int gs_set_config(struct gs_dev *dev, unsigned config);
372 static void gs_reset_config(struct gs_dev *dev);
373 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
374 u8 type, unsigned int index, int is_otg);
376 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
378 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
380 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
382 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
384 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags);
385 static void gs_free_ports(struct gs_dev *dev);
387 /* circular buffer */
388 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags);
389 static void gs_buf_free(struct gs_buf *gb);
390 static void gs_buf_clear(struct gs_buf *gb);
391 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
392 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
393 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
395 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
398 /* external functions */
399 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
404 static struct gs_dev *gs_device;
406 static const char *EP_IN_NAME;
407 static const char *EP_OUT_NAME;
408 static const char *EP_NOTIFY_NAME;
410 static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
412 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
413 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
415 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
417 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
420 /* tty driver struct */
421 static struct tty_operations gs_tty_ops = {
425 .put_char = gs_put_char,
426 .flush_chars = gs_flush_chars,
427 .write_room = gs_write_room,
429 .set_termios = gs_set_termios,
430 .throttle = gs_throttle,
431 .unthrottle = gs_unthrottle,
432 .break_ctl = gs_break,
433 .chars_in_buffer = gs_chars_in_buffer,
435 static struct tty_driver *gs_tty_driver;
437 /* gadget driver struct */
438 static struct usb_gadget_driver gs_gadget_driver = {
439 #ifdef CONFIG_USB_GADGET_DUALSPEED
440 .speed = USB_SPEED_HIGH,
442 .speed = USB_SPEED_FULL,
443 #endif /* CONFIG_USB_GADGET_DUALSPEED */
444 .function = GS_LONG_NAME,
448 .disconnect = gs_disconnect,
450 .name = GS_SHORT_NAME,
451 /* .shutdown = ... */
458 /* USB descriptors */
460 #define GS_MANUFACTURER_STR_ID 1
461 #define GS_PRODUCT_STR_ID 2
462 #define GS_SERIAL_STR_ID 3
463 #define GS_BULK_CONFIG_STR_ID 4
464 #define GS_ACM_CONFIG_STR_ID 5
465 #define GS_CONTROL_STR_ID 6
466 #define GS_DATA_STR_ID 7
468 /* static strings, in UTF-8 */
469 static char manufacturer[50];
470 static struct usb_string gs_strings[] = {
471 { GS_MANUFACTURER_STR_ID, manufacturer },
472 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
473 { GS_SERIAL_STR_ID, "0" },
474 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
475 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
476 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
477 { GS_DATA_STR_ID, "Gadget Serial Data" },
478 { } /* end of list */
481 static struct usb_gadget_strings gs_string_table = {
482 .language = 0x0409, /* en-us */
483 .strings = gs_strings,
486 static struct usb_device_descriptor gs_device_desc = {
487 .bLength = USB_DT_DEVICE_SIZE,
488 .bDescriptorType = USB_DT_DEVICE,
489 .bcdUSB = __constant_cpu_to_le16(0x0200),
490 .bDeviceSubClass = 0,
491 .bDeviceProtocol = 0,
492 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
493 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
494 .iManufacturer = GS_MANUFACTURER_STR_ID,
495 .iProduct = GS_PRODUCT_STR_ID,
496 .iSerialNumber = GS_SERIAL_STR_ID,
497 .bNumConfigurations = GS_NUM_CONFIGS,
500 static struct usb_otg_descriptor gs_otg_descriptor = {
501 .bLength = sizeof(gs_otg_descriptor),
502 .bDescriptorType = USB_DT_OTG,
503 .bmAttributes = USB_OTG_SRP,
506 static struct usb_config_descriptor gs_bulk_config_desc = {
507 .bLength = USB_DT_CONFIG_SIZE,
508 .bDescriptorType = USB_DT_CONFIG,
509 /* .wTotalLength computed dynamically */
511 .bConfigurationValue = GS_BULK_CONFIG_ID,
512 .iConfiguration = GS_BULK_CONFIG_STR_ID,
513 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
517 static struct usb_config_descriptor gs_acm_config_desc = {
518 .bLength = USB_DT_CONFIG_SIZE,
519 .bDescriptorType = USB_DT_CONFIG,
520 /* .wTotalLength computed dynamically */
522 .bConfigurationValue = GS_ACM_CONFIG_ID,
523 .iConfiguration = GS_ACM_CONFIG_STR_ID,
524 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
528 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
529 .bLength = USB_DT_INTERFACE_SIZE,
530 .bDescriptorType = USB_DT_INTERFACE,
531 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
533 .bInterfaceClass = USB_CLASS_CDC_DATA,
534 .bInterfaceSubClass = 0,
535 .bInterfaceProtocol = 0,
536 .iInterface = GS_DATA_STR_ID,
539 static const struct usb_interface_descriptor gs_control_interface_desc = {
540 .bLength = USB_DT_INTERFACE_SIZE,
541 .bDescriptorType = USB_DT_INTERFACE,
542 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
544 .bInterfaceClass = USB_CLASS_COMM,
545 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
546 .bInterfaceProtocol = USB_CDC_CTRL_PROTO_AT,
547 .iInterface = GS_CONTROL_STR_ID,
550 static const struct usb_interface_descriptor gs_data_interface_desc = {
551 .bLength = USB_DT_INTERFACE_SIZE,
552 .bDescriptorType = USB_DT_INTERFACE,
553 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
555 .bInterfaceClass = USB_CLASS_CDC_DATA,
556 .bInterfaceSubClass = 0,
557 .bInterfaceProtocol = 0,
558 .iInterface = GS_DATA_STR_ID,
561 static const struct usb_cdc_header_desc gs_header_desc = {
562 .bLength = sizeof(gs_header_desc),
563 .bDescriptorType = USB_DT_CS_INTERFACE,
564 .bDescriptorSubType = USB_CDC_SUBTYPE_HEADER,
565 .bcdCDC = __constant_cpu_to_le16(0x0110),
568 static const struct usb_cdc_call_mgmt_desc gs_call_mgmt_descriptor = {
569 .bLength = sizeof(gs_call_mgmt_descriptor),
570 .bDescriptorType = USB_DT_CS_INTERFACE,
571 .bDescriptorSubType = USB_CDC_SUBTYPE_CALL_MGMT,
573 .bDataInterface = 1, /* index of data interface */
576 static struct usb_cdc_acm_desc gs_acm_descriptor = {
577 .bLength = sizeof(gs_acm_descriptor),
578 .bDescriptorType = USB_DT_CS_INTERFACE,
579 .bDescriptorSubType = USB_CDC_SUBTYPE_ACM,
583 static const struct usb_cdc_union_desc gs_union_desc = {
584 .bLength = sizeof(gs_union_desc),
585 .bDescriptorType = USB_DT_CS_INTERFACE,
586 .bDescriptorSubType = USB_CDC_SUBTYPE_UNION,
587 .bMasterInterface0 = 0, /* index of control interface */
588 .bSlaveInterface0 = 1, /* index of data interface */
591 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
592 .bLength = USB_DT_ENDPOINT_SIZE,
593 .bDescriptorType = USB_DT_ENDPOINT,
594 .bEndpointAddress = USB_DIR_IN,
595 .bmAttributes = USB_ENDPOINT_XFER_INT,
596 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
597 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
600 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
601 .bLength = USB_DT_ENDPOINT_SIZE,
602 .bDescriptorType = USB_DT_ENDPOINT,
603 .bEndpointAddress = USB_DIR_IN,
604 .bmAttributes = USB_ENDPOINT_XFER_BULK,
607 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
608 .bLength = USB_DT_ENDPOINT_SIZE,
609 .bDescriptorType = USB_DT_ENDPOINT,
610 .bEndpointAddress = USB_DIR_OUT,
611 .bmAttributes = USB_ENDPOINT_XFER_BULK,
614 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
615 (struct usb_descriptor_header *) &gs_otg_descriptor,
616 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
617 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
618 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
622 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
623 (struct usb_descriptor_header *) &gs_otg_descriptor,
624 (struct usb_descriptor_header *) &gs_control_interface_desc,
625 (struct usb_descriptor_header *) &gs_header_desc,
626 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
627 (struct usb_descriptor_header *) &gs_acm_descriptor,
628 (struct usb_descriptor_header *) &gs_union_desc,
629 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
630 (struct usb_descriptor_header *) &gs_data_interface_desc,
631 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
632 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
636 #ifdef CONFIG_USB_GADGET_DUALSPEED
637 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
638 .bLength = USB_DT_ENDPOINT_SIZE,
639 .bDescriptorType = USB_DT_ENDPOINT,
640 .bEndpointAddress = USB_DIR_IN,
641 .bmAttributes = USB_ENDPOINT_XFER_INT,
642 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
643 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
646 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
649 .bmAttributes = USB_ENDPOINT_XFER_BULK,
650 .wMaxPacketSize = __constant_cpu_to_le16(512),
653 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
654 .bLength = USB_DT_ENDPOINT_SIZE,
655 .bDescriptorType = USB_DT_ENDPOINT,
656 .bmAttributes = USB_ENDPOINT_XFER_BULK,
657 .wMaxPacketSize = __constant_cpu_to_le16(512),
660 static struct usb_qualifier_descriptor gs_qualifier_desc = {
661 .bLength = sizeof(struct usb_qualifier_descriptor),
662 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
663 .bcdUSB = __constant_cpu_to_le16 (0x0200),
664 /* assumes ep0 uses the same value for both speeds ... */
665 .bNumConfigurations = GS_NUM_CONFIGS,
668 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
669 (struct usb_descriptor_header *) &gs_otg_descriptor,
670 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
671 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
672 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
676 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
677 (struct usb_descriptor_header *) &gs_otg_descriptor,
678 (struct usb_descriptor_header *) &gs_control_interface_desc,
679 (struct usb_descriptor_header *) &gs_header_desc,
680 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
681 (struct usb_descriptor_header *) &gs_acm_descriptor,
682 (struct usb_descriptor_header *) &gs_union_desc,
683 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
684 (struct usb_descriptor_header *) &gs_data_interface_desc,
685 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
686 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
690 #endif /* CONFIG_USB_GADGET_DUALSPEED */
694 MODULE_DESCRIPTION(GS_LONG_NAME);
695 MODULE_AUTHOR("Al Borchers");
696 MODULE_LICENSE("GPL");
699 module_param(debug, int, S_IRUGO|S_IWUSR);
700 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
703 module_param(read_q_size, uint, S_IRUGO);
704 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
706 module_param(write_q_size, uint, S_IRUGO);
707 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
709 module_param(write_buf_size, uint, S_IRUGO);
710 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
712 module_param(use_acm, uint, S_IRUGO);
713 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
715 module_init(gs_module_init);
716 module_exit(gs_module_exit);
721 * Register as a USB gadget driver and a tty driver.
723 static int __init gs_module_init(void)
728 retval = usb_gadget_register_driver(&gs_gadget_driver);
730 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
734 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
737 gs_tty_driver->owner = THIS_MODULE;
738 gs_tty_driver->driver_name = GS_SHORT_NAME;
739 gs_tty_driver->name = "ttygs";
740 gs_tty_driver->devfs_name = "usb/ttygs/";
741 gs_tty_driver->major = GS_MAJOR;
742 gs_tty_driver->minor_start = GS_MINOR_START;
743 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
744 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
745 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
746 gs_tty_driver->init_termios = tty_std_termios;
747 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
748 tty_set_operations(gs_tty_driver, &gs_tty_ops);
750 for (i=0; i < GS_NUM_PORTS; i++)
751 sema_init(&gs_open_close_sem[i], 1);
753 retval = tty_register_driver(gs_tty_driver);
755 usb_gadget_unregister_driver(&gs_gadget_driver);
756 put_tty_driver(gs_tty_driver);
757 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
761 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
768 * Unregister as a tty driver and a USB gadget driver.
770 static void __exit gs_module_exit(void)
772 tty_unregister_driver(gs_tty_driver);
773 put_tty_driver(gs_tty_driver);
774 usb_gadget_unregister_driver(&gs_gadget_driver);
776 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
784 static int gs_open(struct tty_struct *tty, struct file *file)
788 struct gs_port *port;
791 struct semaphore *sem;
794 port_num = tty->index;
796 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
798 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
799 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
800 port_num, tty, file);
807 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
808 port_num, tty, file);
812 sem = &gs_open_close_sem[port_num];
813 if (down_interruptible(sem)) {
815 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
816 port_num, tty, file);
820 spin_lock_irqsave(&dev->dev_lock, flags);
822 if (dev->dev_config == GS_NO_CONFIG_ID) {
824 "gs_open: (%d,%p,%p) device is not connected\n",
825 port_num, tty, file);
827 goto exit_unlock_dev;
830 port = dev->dev_port[port_num];
833 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
834 port_num, tty, file);
836 goto exit_unlock_dev;
839 spin_lock(&port->port_lock);
840 spin_unlock(&dev->dev_lock);
842 if (port->port_dev == NULL) {
843 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
844 port_num, tty, file);
846 goto exit_unlock_port;
849 if (port->port_open_count > 0) {
850 ++port->port_open_count;
851 gs_debug("gs_open: (%d,%p,%p) already open\n",
852 port_num, tty, file);
854 goto exit_unlock_port;
857 tty->driver_data = NULL;
859 /* mark port as in use, we can drop port lock and sleep if necessary */
860 port->port_in_use = 1;
862 /* allocate write buffer on first open */
863 if (port->port_write_buf == NULL) {
864 spin_unlock_irqrestore(&port->port_lock, flags);
865 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
866 spin_lock_irqsave(&port->port_lock, flags);
868 /* might have been disconnected while asleep, check */
869 if (port->port_dev == NULL) {
871 "gs_open: (%d,%p,%p) port disconnected (2)\n",
872 port_num, tty, file);
873 port->port_in_use = 0;
875 goto exit_unlock_port;
878 if ((port->port_write_buf=buf) == NULL) {
879 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
880 port_num, tty, file);
881 port->port_in_use = 0;
883 goto exit_unlock_port;
888 /* wait for carrier detect (not implemented) */
890 /* might have been disconnected while asleep, check */
891 if (port->port_dev == NULL) {
892 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
893 port_num, tty, file);
894 port->port_in_use = 0;
896 goto exit_unlock_port;
899 tty->driver_data = port;
900 port->port_tty = tty;
901 port->port_open_count = 1;
902 port->port_in_use = 0;
904 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
909 spin_unlock_irqrestore(&port->port_lock, flags);
914 spin_unlock_irqrestore(&dev->dev_lock, flags);
923 static void gs_close(struct tty_struct *tty, struct file *file)
926 struct gs_port *port = tty->driver_data;
927 struct semaphore *sem;
930 printk(KERN_ERR "gs_close: NULL port pointer\n");
934 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
936 sem = &gs_open_close_sem[port->port_num];
939 spin_lock_irqsave(&port->port_lock, flags);
941 if (port->port_open_count == 0) {
943 "gs_close: (%d,%p,%p) port is already closed\n",
944 port->port_num, tty, file);
948 if (port->port_open_count > 1) {
949 --port->port_open_count;
953 /* free disconnected port on final close */
954 if (port->port_dev == NULL) {
959 /* mark port as closed but in use, we can drop port lock */
960 /* and sleep if necessary */
961 port->port_in_use = 1;
962 port->port_open_count = 0;
964 /* wait for write buffer to drain, or */
965 /* at most GS_CLOSE_TIMEOUT seconds */
966 if (gs_buf_data_avail(port->port_write_buf) > 0) {
967 wait_cond_interruptible_timeout(port->port_write_wait,
968 port->port_dev == NULL
969 || gs_buf_data_avail(port->port_write_buf) == 0,
970 &port->port_lock, flags, GS_CLOSE_TIMEOUT * HZ);
973 /* free disconnected port on final close */
974 /* (might have happened during the above sleep) */
975 if (port->port_dev == NULL) {
980 gs_buf_clear(port->port_write_buf);
982 tty->driver_data = NULL;
983 port->port_tty = NULL;
984 port->port_in_use = 0;
986 gs_debug("gs_close: (%d,%p,%p) completed\n",
987 port->port_num, tty, file);
990 spin_unlock_irqrestore(&port->port_lock, flags);
997 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
1000 struct gs_port *port = tty->driver_data;
1004 printk(KERN_ERR "gs_write: NULL port pointer\n");
1008 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
1014 spin_lock_irqsave(&port->port_lock, flags);
1016 if (port->port_dev == NULL) {
1017 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
1018 port->port_num, tty);
1023 if (port->port_open_count == 0) {
1024 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
1025 port->port_num, tty);
1030 count = gs_buf_put(port->port_write_buf, buf, count);
1032 spin_unlock_irqrestore(&port->port_lock, flags);
1036 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
1042 spin_unlock_irqrestore(&port->port_lock, flags);
1049 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
1051 unsigned long flags;
1052 struct gs_port *port = tty->driver_data;
1055 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
1059 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));
1061 spin_lock_irqsave(&port->port_lock, flags);
1063 if (port->port_dev == NULL) {
1064 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
1065 port->port_num, tty);
1069 if (port->port_open_count == 0) {
1070 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
1071 port->port_num, tty);
1075 gs_buf_put(port->port_write_buf, &ch, 1);
1078 spin_unlock_irqrestore(&port->port_lock, flags);
1084 static void gs_flush_chars(struct tty_struct *tty)
1086 unsigned long flags;
1087 struct gs_port *port = tty->driver_data;
1090 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
1094 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
1096 spin_lock_irqsave(&port->port_lock, flags);
1098 if (port->port_dev == NULL) {
1100 "gs_flush_chars: (%d,%p) port is not connected\n",
1101 port->port_num, tty);
1105 if (port->port_open_count == 0) {
1106 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
1107 port->port_num, tty);
1111 spin_unlock_irqrestore(&port->port_lock, flags);
1118 spin_unlock_irqrestore(&port->port_lock, flags);
1124 static int gs_write_room(struct tty_struct *tty)
1128 unsigned long flags;
1129 struct gs_port *port = tty->driver_data;
1135 spin_lock_irqsave(&port->port_lock, flags);
1137 if (port->port_dev != NULL && port->port_open_count > 0
1138 && port->port_write_buf != NULL)
1139 room = gs_buf_space_avail(port->port_write_buf);
1141 spin_unlock_irqrestore(&port->port_lock, flags);
1143 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1144 port->port_num, tty, room);
1150 * gs_chars_in_buffer
1152 static int gs_chars_in_buffer(struct tty_struct *tty)
1155 unsigned long flags;
1156 struct gs_port *port = tty->driver_data;
1161 spin_lock_irqsave(&port->port_lock, flags);
1163 if (port->port_dev != NULL && port->port_open_count > 0
1164 && port->port_write_buf != NULL)
1165 chars = gs_buf_data_avail(port->port_write_buf);
1167 spin_unlock_irqrestore(&port->port_lock, flags);
1169 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1170 port->port_num, tty, chars);
1178 static void gs_throttle(struct tty_struct *tty)
1185 static void gs_unthrottle(struct tty_struct *tty)
1192 static void gs_break(struct tty_struct *tty, int break_state)
1199 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1201 struct gs_port *port = tty->driver_data;
1204 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1208 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1209 port->port_num, tty, file, cmd, arg);
1213 /* could not handle ioctl */
1214 return -ENOIOCTLCMD;
1220 static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1227 * This function finds available write requests, calls
1228 * gs_send_packet to fill these packets with data, and
1229 * continues until either there are no more write requests
1230 * available or no more data to send. This function is
1231 * run whenever data arrives or write requests are available.
1233 static int gs_send(struct gs_dev *dev)
1236 unsigned long flags;
1238 struct usb_request *req;
1239 struct gs_req_entry *req_entry;
1242 printk(KERN_ERR "gs_send: NULL device pointer\n");
1246 spin_lock_irqsave(&dev->dev_lock, flags);
1248 ep = dev->dev_in_ep;
1250 while(!list_empty(&dev->dev_req_list)) {
1252 req_entry = list_entry(dev->dev_req_list.next,
1253 struct gs_req_entry, re_entry);
1255 req = req_entry->re_req;
1257 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1260 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));
1261 list_del(&req_entry->re_entry);
1263 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1265 "gs_send: cannot queue read request, ret=%d\n",
1275 spin_unlock_irqrestore(&dev->dev_lock, flags);
1283 * If there is data to send, a packet is built in the given
1284 * buffer and the size is returned. If there is no data to
1285 * send, 0 is returned. If there is any error a negative
1286 * error number is returned.
1288 * Called during USB completion routine, on interrupt time.
1290 * We assume that disconnect will not happen until all completion
1291 * routines have completed, so we can assume that the dev_port
1292 * array does not change during the lifetime of this function.
1294 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1297 struct gs_port *port;
1299 /* TEMPORARY -- only port 0 is supported right now */
1300 port = dev->dev_port[0];
1304 "gs_send_packet: port=%d, NULL port pointer\n",
1309 spin_lock(&port->port_lock);
1311 len = gs_buf_data_avail(port->port_write_buf);
1318 size = gs_buf_get(port->port_write_buf, packet, size);
1321 wake_up_interruptible(&port->port_tty->write_wait);
1324 spin_unlock(&port->port_lock);
1331 * Called for each USB packet received. Reads the packet
1332 * header and stuffs the data in the appropriate tty buffer.
1333 * Returns 0 if successful, or a negative error number.
1335 * Called during USB completion routine, on interrupt time.
1337 * We assume that disconnect will not happen until all completion
1338 * routines have completed, so we can assume that the dev_port
1339 * array does not change during the lifetime of this function.
1341 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1344 struct gs_port *port;
1347 /* TEMPORARY -- only port 0 is supported right now */
1348 port = dev->dev_port[0];
1351 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1356 spin_lock(&port->port_lock);
1358 if (port->port_open_count == 0) {
1359 printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",
1365 if (port->port_tty == NULL) {
1366 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1372 if (port->port_tty->magic != TTY_MAGIC) {
1373 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1379 len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);
1384 memcpy(port->port_tty->flip.char_buf_ptr, packet, size);
1385 port->port_tty->flip.char_buf_ptr += size;
1386 port->port_tty->flip.count += size;
1387 tty_flip_buffer_push(port->port_tty);
1388 wake_up_interruptible(&port->port_tty->read_wait);
1394 spin_unlock(&port->port_lock);
1401 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1404 struct gs_dev *dev = ep->driver_data;
1407 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1411 switch(req->status) {
1413 /* normal completion */
1414 gs_recv_packet(dev, req->buf, req->actual);
1416 req->length = ep->maxpacket;
1417 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1419 "gs_read_complete: cannot queue read request, ret=%d\n",
1426 gs_debug("gs_read_complete: shutdown\n");
1427 gs_free_req(ep, req);
1433 "gs_read_complete: unexpected status error, status=%d\n",
1443 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1445 struct gs_dev *dev = ep->driver_data;
1446 struct gs_req_entry *gs_req = req->context;
1449 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1453 switch(req->status) {
1455 /* normal completion */
1457 if (gs_req == NULL) {
1459 "gs_write_complete: NULL request pointer\n");
1463 spin_lock(&dev->dev_lock);
1464 list_add(&gs_req->re_entry, &dev->dev_req_list);
1465 spin_unlock(&dev->dev_lock);
1473 gs_debug("gs_write_complete: shutdown\n");
1474 gs_free_req(ep, req);
1479 "gs_write_complete: unexpected status error, status=%d\n",
1491 * Called on module load. Allocates and initializes the device
1492 * structure and a control request.
1494 static int gs_bind(struct usb_gadget *gadget)
1500 /* device specific */
1501 if (gadget_is_net2280(gadget)) {
1502 gs_device_desc.bcdDevice =
1503 __constant_cpu_to_le16(GS_VERSION_NUM|0x0001);
1504 } else if (gadget_is_pxa(gadget)) {
1505 gs_device_desc.bcdDevice =
1506 __constant_cpu_to_le16(GS_VERSION_NUM|0x0002);
1507 } else if (gadget_is_sh(gadget)) {
1508 gs_device_desc.bcdDevice =
1509 __constant_cpu_to_le16(GS_VERSION_NUM|0x0003);
1510 /* sh doesn't support multiple interfaces or configs */
1512 } else if (gadget_is_sa1100(gadget)) {
1513 gs_device_desc.bcdDevice =
1514 __constant_cpu_to_le16(GS_VERSION_NUM|0x0004);
1515 /* sa1100 doesn't support necessary endpoints */
1517 } else if (gadget_is_goku(gadget)) {
1518 gs_device_desc.bcdDevice =
1519 __constant_cpu_to_le16(GS_VERSION_NUM|0x0005);
1520 } else if (gadget_is_mq11xx(gadget)) {
1521 gs_device_desc.bcdDevice =
1522 __constant_cpu_to_le16(GS_VERSION_NUM|0x0006);
1523 } else if (gadget_is_omap(gadget)) {
1524 gs_device_desc.bcdDevice =
1525 __constant_cpu_to_le16(GS_VERSION_NUM|0x0007);
1526 } else if (gadget_is_lh7a40x(gadget)) {
1527 gs_device_desc.bcdDevice =
1528 __constant_cpu_to_le16(GS_VERSION_NUM|0x0008);
1529 } else if (gadget_is_n9604(gadget)) {
1530 gs_device_desc.bcdDevice =
1531 __constant_cpu_to_le16(GS_VERSION_NUM|0x0009);
1532 } else if (gadget_is_pxa27x(gadget)) {
1533 gs_device_desc.bcdDevice =
1534 __constant_cpu_to_le16(GS_VERSION_NUM|0x0011);
1536 printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
1538 /* unrecognized, but safe unless bulk is REALLY quirky */
1539 gs_device_desc.bcdDevice =
1540 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1543 usb_ep_autoconfig_reset(gadget);
1545 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1548 EP_IN_NAME = ep->name;
1549 ep->driver_data = ep; /* claim the endpoint */
1551 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1554 EP_OUT_NAME = ep->name;
1555 ep->driver_data = ep; /* claim the endpoint */
1558 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1560 printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);
1563 gs_device_desc.idProduct = __constant_cpu_to_le16(
1565 EP_NOTIFY_NAME = ep->name;
1566 ep->driver_data = ep; /* claim the endpoint */
1569 gs_device_desc.bDeviceClass = use_acm
1570 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1571 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1573 #ifdef CONFIG_USB_GADGET_DUALSPEED
1574 gs_qualifier_desc.bDeviceClass = use_acm
1575 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1576 /* assume ep0 uses the same packet size for both speeds */
1577 gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
1578 /* assume endpoints are dual-speed */
1579 gs_highspeed_notify_desc.bEndpointAddress =
1580 gs_fullspeed_notify_desc.bEndpointAddress;
1581 gs_highspeed_in_desc.bEndpointAddress =
1582 gs_fullspeed_in_desc.bEndpointAddress;
1583 gs_highspeed_out_desc.bEndpointAddress =
1584 gs_fullspeed_out_desc.bEndpointAddress;
1585 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1587 usb_gadget_set_selfpowered(gadget);
1589 if (gadget->is_otg) {
1590 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1591 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1592 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1595 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1599 snprintf(manufacturer, sizeof(manufacturer),
1600 UTS_SYSNAME " " UTS_RELEASE " with %s", gadget->name);
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);
2329 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;