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/module.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/timer.h>
30 #include <linux/list.h>
31 #include <linux/interrupt.h>
32 #include <linux/utsname.h>
33 #include <linux/wait.h>
34 #include <linux/proc_fs.h>
35 #include <linux/device.h>
36 #include <linux/tty.h>
37 #include <linux/tty_flip.h>
39 #include <asm/byteorder.h>
42 #include <asm/system.h>
43 #include <asm/unaligned.h>
44 #include <asm/uaccess.h>
46 #include <linux/usb_ch9.h>
47 #include <linux/usb/cdc.h>
48 #include <linux/usb_gadget.h>
50 #include "gadget_chips.h"
55 #define GS_VERSION_STR "v2.2"
56 #define GS_VERSION_NUM 0x0202
58 #define GS_LONG_NAME "Gadget Serial"
59 #define GS_SHORT_NAME "g_serial"
62 #define GS_MINOR_START 0
64 #define GS_NUM_PORTS 16
66 #define GS_NUM_CONFIGS 1
67 #define GS_NO_CONFIG_ID 0
68 #define GS_BULK_CONFIG_ID 1
69 #define GS_ACM_CONFIG_ID 2
71 #define GS_MAX_NUM_INTERFACES 2
72 #define GS_BULK_INTERFACE_ID 0
73 #define GS_CONTROL_INTERFACE_ID 0
74 #define GS_DATA_INTERFACE_ID 1
76 #define GS_MAX_DESC_LEN 256
78 #define GS_DEFAULT_READ_Q_SIZE 32
79 #define GS_DEFAULT_WRITE_Q_SIZE 32
81 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
82 #define GS_TMP_BUF_SIZE 8192
84 #define GS_CLOSE_TIMEOUT 15
86 #define GS_DEFAULT_USE_ACM 0
88 #define GS_DEFAULT_DTE_RATE 9600
89 #define GS_DEFAULT_DATA_BITS 8
90 #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
91 #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
93 /* select highspeed/fullspeed, hiding highspeed if not configured */
94 #ifdef CONFIG_USB_GADGET_DUALSPEED
95 #define GS_SPEED_SELECT(is_hs,hs,fs) ((is_hs) ? (hs) : (fs))
97 #define GS_SPEED_SELECT(is_hs,hs,fs) (fs)
98 #endif /* CONFIG_USB_GADGET_DUALSPEED */
102 static int debug = 1;
104 #define gs_debug(format, arg...) \
105 do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
106 #define gs_debug_level(level, format, arg...) \
107 do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
111 #define gs_debug(format, arg...) \
113 #define gs_debug_level(level, format, arg...) \
116 #endif /* GS_DEBUG */
118 /* Thanks to NetChip Technologies for donating this product ID.
120 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
121 * Instead: allocate your own, using normal USB-IF procedures.
123 #define GS_VENDOR_ID 0x0525 /* NetChip */
124 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
125 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
127 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
128 #define GS_NOTIFY_MAXPACKET 8
135 /* circular buffer */
137 unsigned int buf_size;
143 /* list of requests */
144 struct gs_req_entry {
145 struct list_head re_entry;
146 struct usb_request *re_req;
149 /* the port structure holds info for each port, one for each minor number */
151 struct gs_dev *port_dev; /* pointer to device struct */
152 struct tty_struct *port_tty; /* pointer to tty struct */
153 spinlock_t port_lock;
156 int port_in_use; /* open/close in progress */
157 wait_queue_head_t port_write_wait;/* waiting to write */
158 struct gs_buf *port_write_buf;
159 struct usb_cdc_line_coding port_line_coding;
162 /* the device structure holds info for the USB device */
164 struct usb_gadget *dev_gadget; /* gadget device pointer */
165 spinlock_t dev_lock; /* lock for set/reset config */
166 int dev_config; /* configuration number */
167 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
168 struct usb_ep *dev_in_ep; /* address of in endpoint */
169 struct usb_ep *dev_out_ep; /* address of out endpoint */
170 struct usb_endpoint_descriptor /* descriptor of notify ep */
172 struct usb_endpoint_descriptor /* descriptor of in endpoint */
174 struct usb_endpoint_descriptor /* descriptor of out endpoint */
176 struct usb_request *dev_ctrl_req; /* control request */
177 struct list_head dev_req_list; /* list of write requests */
178 int dev_sched_port; /* round robin port scheduled */
179 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
186 static int __init gs_module_init(void);
187 static void __exit gs_module_exit(void);
190 static int gs_open(struct tty_struct *tty, struct file *file);
191 static void gs_close(struct tty_struct *tty, struct file *file);
192 static int gs_write(struct tty_struct *tty,
193 const unsigned char *buf, int count);
194 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
195 static void gs_flush_chars(struct tty_struct *tty);
196 static int gs_write_room(struct tty_struct *tty);
197 static int gs_chars_in_buffer(struct tty_struct *tty);
198 static void gs_throttle(struct tty_struct * tty);
199 static void gs_unthrottle(struct tty_struct * tty);
200 static void gs_break(struct tty_struct *tty, int break_state);
201 static int gs_ioctl(struct tty_struct *tty, struct file *file,
202 unsigned int cmd, unsigned long arg);
203 static void gs_set_termios(struct tty_struct *tty, struct termios *old);
205 static int gs_send(struct gs_dev *dev);
206 static int gs_send_packet(struct gs_dev *dev, char *packet,
208 static int gs_recv_packet(struct gs_dev *dev, char *packet,
210 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
211 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
214 static int gs_bind(struct usb_gadget *gadget);
215 static void gs_unbind(struct usb_gadget *gadget);
216 static int gs_setup(struct usb_gadget *gadget,
217 const struct usb_ctrlrequest *ctrl);
218 static int gs_setup_standard(struct usb_gadget *gadget,
219 const struct usb_ctrlrequest *ctrl);
220 static int gs_setup_class(struct usb_gadget *gadget,
221 const struct usb_ctrlrequest *ctrl);
222 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
223 static void gs_disconnect(struct usb_gadget *gadget);
224 static int gs_set_config(struct gs_dev *dev, unsigned config);
225 static void gs_reset_config(struct gs_dev *dev);
226 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
227 u8 type, unsigned int index, int is_otg);
229 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
230 gfp_t kmalloc_flags);
231 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
233 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
234 gfp_t kmalloc_flags);
235 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
237 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
238 static void gs_free_ports(struct gs_dev *dev);
240 /* circular buffer */
241 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
242 static void gs_buf_free(struct gs_buf *gb);
243 static void gs_buf_clear(struct gs_buf *gb);
244 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
245 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
246 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
248 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
251 /* external functions */
252 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
257 static struct gs_dev *gs_device;
259 static const char *EP_IN_NAME;
260 static const char *EP_OUT_NAME;
261 static const char *EP_NOTIFY_NAME;
263 static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
265 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
266 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
268 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
270 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
273 /* tty driver struct */
274 static struct tty_operations gs_tty_ops = {
278 .put_char = gs_put_char,
279 .flush_chars = gs_flush_chars,
280 .write_room = gs_write_room,
282 .set_termios = gs_set_termios,
283 .throttle = gs_throttle,
284 .unthrottle = gs_unthrottle,
285 .break_ctl = gs_break,
286 .chars_in_buffer = gs_chars_in_buffer,
288 static struct tty_driver *gs_tty_driver;
290 /* gadget driver struct */
291 static struct usb_gadget_driver gs_gadget_driver = {
292 #ifdef CONFIG_USB_GADGET_DUALSPEED
293 .speed = USB_SPEED_HIGH,
295 .speed = USB_SPEED_FULL,
296 #endif /* CONFIG_USB_GADGET_DUALSPEED */
297 .function = GS_LONG_NAME,
299 .unbind = __exit_p(gs_unbind),
301 .disconnect = gs_disconnect,
303 .name = GS_SHORT_NAME,
308 /* USB descriptors */
310 #define GS_MANUFACTURER_STR_ID 1
311 #define GS_PRODUCT_STR_ID 2
312 #define GS_SERIAL_STR_ID 3
313 #define GS_BULK_CONFIG_STR_ID 4
314 #define GS_ACM_CONFIG_STR_ID 5
315 #define GS_CONTROL_STR_ID 6
316 #define GS_DATA_STR_ID 7
318 /* static strings, in UTF-8 */
319 static char manufacturer[50];
320 static struct usb_string gs_strings[] = {
321 { GS_MANUFACTURER_STR_ID, manufacturer },
322 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
323 { GS_SERIAL_STR_ID, "0" },
324 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
325 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
326 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
327 { GS_DATA_STR_ID, "Gadget Serial Data" },
328 { } /* end of list */
331 static struct usb_gadget_strings gs_string_table = {
332 .language = 0x0409, /* en-us */
333 .strings = gs_strings,
336 static struct usb_device_descriptor gs_device_desc = {
337 .bLength = USB_DT_DEVICE_SIZE,
338 .bDescriptorType = USB_DT_DEVICE,
339 .bcdUSB = __constant_cpu_to_le16(0x0200),
340 .bDeviceSubClass = 0,
341 .bDeviceProtocol = 0,
342 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
343 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
344 .iManufacturer = GS_MANUFACTURER_STR_ID,
345 .iProduct = GS_PRODUCT_STR_ID,
346 .iSerialNumber = GS_SERIAL_STR_ID,
347 .bNumConfigurations = GS_NUM_CONFIGS,
350 static struct usb_otg_descriptor gs_otg_descriptor = {
351 .bLength = sizeof(gs_otg_descriptor),
352 .bDescriptorType = USB_DT_OTG,
353 .bmAttributes = USB_OTG_SRP,
356 static struct usb_config_descriptor gs_bulk_config_desc = {
357 .bLength = USB_DT_CONFIG_SIZE,
358 .bDescriptorType = USB_DT_CONFIG,
359 /* .wTotalLength computed dynamically */
361 .bConfigurationValue = GS_BULK_CONFIG_ID,
362 .iConfiguration = GS_BULK_CONFIG_STR_ID,
363 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
367 static struct usb_config_descriptor gs_acm_config_desc = {
368 .bLength = USB_DT_CONFIG_SIZE,
369 .bDescriptorType = USB_DT_CONFIG,
370 /* .wTotalLength computed dynamically */
372 .bConfigurationValue = GS_ACM_CONFIG_ID,
373 .iConfiguration = GS_ACM_CONFIG_STR_ID,
374 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
378 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
379 .bLength = USB_DT_INTERFACE_SIZE,
380 .bDescriptorType = USB_DT_INTERFACE,
381 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
383 .bInterfaceClass = USB_CLASS_CDC_DATA,
384 .bInterfaceSubClass = 0,
385 .bInterfaceProtocol = 0,
386 .iInterface = GS_DATA_STR_ID,
389 static const struct usb_interface_descriptor gs_control_interface_desc = {
390 .bLength = USB_DT_INTERFACE_SIZE,
391 .bDescriptorType = USB_DT_INTERFACE,
392 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
394 .bInterfaceClass = USB_CLASS_COMM,
395 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
396 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
397 .iInterface = GS_CONTROL_STR_ID,
400 static const struct usb_interface_descriptor gs_data_interface_desc = {
401 .bLength = USB_DT_INTERFACE_SIZE,
402 .bDescriptorType = USB_DT_INTERFACE,
403 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
405 .bInterfaceClass = USB_CLASS_CDC_DATA,
406 .bInterfaceSubClass = 0,
407 .bInterfaceProtocol = 0,
408 .iInterface = GS_DATA_STR_ID,
411 static const struct usb_cdc_header_desc gs_header_desc = {
412 .bLength = sizeof(gs_header_desc),
413 .bDescriptorType = USB_DT_CS_INTERFACE,
414 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
415 .bcdCDC = __constant_cpu_to_le16(0x0110),
418 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
419 .bLength = sizeof(gs_call_mgmt_descriptor),
420 .bDescriptorType = USB_DT_CS_INTERFACE,
421 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
423 .bDataInterface = 1, /* index of data interface */
426 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
427 .bLength = sizeof(gs_acm_descriptor),
428 .bDescriptorType = USB_DT_CS_INTERFACE,
429 .bDescriptorSubType = USB_CDC_ACM_TYPE,
433 static const struct usb_cdc_union_desc gs_union_desc = {
434 .bLength = sizeof(gs_union_desc),
435 .bDescriptorType = USB_DT_CS_INTERFACE,
436 .bDescriptorSubType = USB_CDC_UNION_TYPE,
437 .bMasterInterface0 = 0, /* index of control interface */
438 .bSlaveInterface0 = 1, /* index of data interface */
441 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
442 .bLength = USB_DT_ENDPOINT_SIZE,
443 .bDescriptorType = USB_DT_ENDPOINT,
444 .bEndpointAddress = USB_DIR_IN,
445 .bmAttributes = USB_ENDPOINT_XFER_INT,
446 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
447 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
450 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
451 .bLength = USB_DT_ENDPOINT_SIZE,
452 .bDescriptorType = USB_DT_ENDPOINT,
453 .bEndpointAddress = USB_DIR_IN,
454 .bmAttributes = USB_ENDPOINT_XFER_BULK,
457 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
458 .bLength = USB_DT_ENDPOINT_SIZE,
459 .bDescriptorType = USB_DT_ENDPOINT,
460 .bEndpointAddress = USB_DIR_OUT,
461 .bmAttributes = USB_ENDPOINT_XFER_BULK,
464 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
465 (struct usb_descriptor_header *) &gs_otg_descriptor,
466 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
467 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
468 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
472 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
473 (struct usb_descriptor_header *) &gs_otg_descriptor,
474 (struct usb_descriptor_header *) &gs_control_interface_desc,
475 (struct usb_descriptor_header *) &gs_header_desc,
476 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
477 (struct usb_descriptor_header *) &gs_acm_descriptor,
478 (struct usb_descriptor_header *) &gs_union_desc,
479 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
480 (struct usb_descriptor_header *) &gs_data_interface_desc,
481 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
482 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
486 #ifdef CONFIG_USB_GADGET_DUALSPEED
487 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
488 .bLength = USB_DT_ENDPOINT_SIZE,
489 .bDescriptorType = USB_DT_ENDPOINT,
490 .bEndpointAddress = USB_DIR_IN,
491 .bmAttributes = USB_ENDPOINT_XFER_INT,
492 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
493 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
496 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
497 .bLength = USB_DT_ENDPOINT_SIZE,
498 .bDescriptorType = USB_DT_ENDPOINT,
499 .bmAttributes = USB_ENDPOINT_XFER_BULK,
500 .wMaxPacketSize = __constant_cpu_to_le16(512),
503 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
504 .bLength = USB_DT_ENDPOINT_SIZE,
505 .bDescriptorType = USB_DT_ENDPOINT,
506 .bmAttributes = USB_ENDPOINT_XFER_BULK,
507 .wMaxPacketSize = __constant_cpu_to_le16(512),
510 static struct usb_qualifier_descriptor gs_qualifier_desc = {
511 .bLength = sizeof(struct usb_qualifier_descriptor),
512 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
513 .bcdUSB = __constant_cpu_to_le16 (0x0200),
514 /* assumes ep0 uses the same value for both speeds ... */
515 .bNumConfigurations = GS_NUM_CONFIGS,
518 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
519 (struct usb_descriptor_header *) &gs_otg_descriptor,
520 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
521 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
522 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
526 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
527 (struct usb_descriptor_header *) &gs_otg_descriptor,
528 (struct usb_descriptor_header *) &gs_control_interface_desc,
529 (struct usb_descriptor_header *) &gs_header_desc,
530 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
531 (struct usb_descriptor_header *) &gs_acm_descriptor,
532 (struct usb_descriptor_header *) &gs_union_desc,
533 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
534 (struct usb_descriptor_header *) &gs_data_interface_desc,
535 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
536 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
540 #endif /* CONFIG_USB_GADGET_DUALSPEED */
544 MODULE_DESCRIPTION(GS_LONG_NAME);
545 MODULE_AUTHOR("Al Borchers");
546 MODULE_LICENSE("GPL");
549 module_param(debug, int, S_IRUGO|S_IWUSR);
550 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
553 module_param(read_q_size, uint, S_IRUGO);
554 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
556 module_param(write_q_size, uint, S_IRUGO);
557 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
559 module_param(write_buf_size, uint, S_IRUGO);
560 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
562 module_param(use_acm, uint, S_IRUGO);
563 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
565 module_init(gs_module_init);
566 module_exit(gs_module_exit);
571 * Register as a USB gadget driver and a tty driver.
573 static int __init gs_module_init(void)
578 retval = usb_gadget_register_driver(&gs_gadget_driver);
580 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
584 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
587 gs_tty_driver->owner = THIS_MODULE;
588 gs_tty_driver->driver_name = GS_SHORT_NAME;
589 gs_tty_driver->name = "ttygs";
590 gs_tty_driver->major = GS_MAJOR;
591 gs_tty_driver->minor_start = GS_MINOR_START;
592 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
593 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
594 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
595 gs_tty_driver->init_termios = tty_std_termios;
596 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
597 tty_set_operations(gs_tty_driver, &gs_tty_ops);
599 for (i=0; i < GS_NUM_PORTS; i++)
600 sema_init(&gs_open_close_sem[i], 1);
602 retval = tty_register_driver(gs_tty_driver);
604 usb_gadget_unregister_driver(&gs_gadget_driver);
605 put_tty_driver(gs_tty_driver);
606 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
610 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
617 * Unregister as a tty driver and a USB gadget driver.
619 static void __exit gs_module_exit(void)
621 tty_unregister_driver(gs_tty_driver);
622 put_tty_driver(gs_tty_driver);
623 usb_gadget_unregister_driver(&gs_gadget_driver);
625 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
633 static int gs_open(struct tty_struct *tty, struct file *file)
637 struct gs_port *port;
640 struct semaphore *sem;
643 port_num = tty->index;
645 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
647 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
648 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
649 port_num, tty, file);
656 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
657 port_num, tty, file);
661 sem = &gs_open_close_sem[port_num];
662 if (down_interruptible(sem)) {
664 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
665 port_num, tty, file);
669 spin_lock_irqsave(&dev->dev_lock, flags);
671 if (dev->dev_config == GS_NO_CONFIG_ID) {
673 "gs_open: (%d,%p,%p) device is not connected\n",
674 port_num, tty, file);
676 goto exit_unlock_dev;
679 port = dev->dev_port[port_num];
682 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
683 port_num, tty, file);
685 goto exit_unlock_dev;
688 spin_lock(&port->port_lock);
689 spin_unlock(&dev->dev_lock);
691 if (port->port_dev == NULL) {
692 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
693 port_num, tty, file);
695 goto exit_unlock_port;
698 if (port->port_open_count > 0) {
699 ++port->port_open_count;
700 gs_debug("gs_open: (%d,%p,%p) already open\n",
701 port_num, tty, file);
703 goto exit_unlock_port;
706 tty->driver_data = NULL;
708 /* mark port as in use, we can drop port lock and sleep if necessary */
709 port->port_in_use = 1;
711 /* allocate write buffer on first open */
712 if (port->port_write_buf == NULL) {
713 spin_unlock_irqrestore(&port->port_lock, flags);
714 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
715 spin_lock_irqsave(&port->port_lock, flags);
717 /* might have been disconnected while asleep, check */
718 if (port->port_dev == NULL) {
720 "gs_open: (%d,%p,%p) port disconnected (2)\n",
721 port_num, tty, file);
722 port->port_in_use = 0;
724 goto exit_unlock_port;
727 if ((port->port_write_buf=buf) == NULL) {
728 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
729 port_num, tty, file);
730 port->port_in_use = 0;
732 goto exit_unlock_port;
737 /* wait for carrier detect (not implemented) */
739 /* might have been disconnected while asleep, check */
740 if (port->port_dev == NULL) {
741 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
742 port_num, tty, file);
743 port->port_in_use = 0;
745 goto exit_unlock_port;
748 tty->driver_data = port;
749 port->port_tty = tty;
750 port->port_open_count = 1;
751 port->port_in_use = 0;
753 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
758 spin_unlock_irqrestore(&port->port_lock, flags);
763 spin_unlock_irqrestore(&dev->dev_lock, flags);
773 #define GS_WRITE_FINISHED_EVENT_SAFELY(p) \
777 spin_lock_irq(&(p)->port_lock); \
778 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \
779 spin_unlock_irq(&(p)->port_lock); \
783 static void gs_close(struct tty_struct *tty, struct file *file)
785 struct gs_port *port = tty->driver_data;
786 struct semaphore *sem;
789 printk(KERN_ERR "gs_close: NULL port pointer\n");
793 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
795 sem = &gs_open_close_sem[port->port_num];
798 spin_lock_irq(&port->port_lock);
800 if (port->port_open_count == 0) {
802 "gs_close: (%d,%p,%p) port is already closed\n",
803 port->port_num, tty, file);
807 if (port->port_open_count > 1) {
808 --port->port_open_count;
812 /* free disconnected port on final close */
813 if (port->port_dev == NULL) {
818 /* mark port as closed but in use, we can drop port lock */
819 /* and sleep if necessary */
820 port->port_in_use = 1;
821 port->port_open_count = 0;
823 /* wait for write buffer to drain, or */
824 /* at most GS_CLOSE_TIMEOUT seconds */
825 if (gs_buf_data_avail(port->port_write_buf) > 0) {
826 spin_unlock_irq(&port->port_lock);
827 wait_event_interruptible_timeout(port->port_write_wait,
828 GS_WRITE_FINISHED_EVENT_SAFELY(port),
829 GS_CLOSE_TIMEOUT * HZ);
830 spin_lock_irq(&port->port_lock);
833 /* free disconnected port on final close */
834 /* (might have happened during the above sleep) */
835 if (port->port_dev == NULL) {
840 gs_buf_clear(port->port_write_buf);
842 tty->driver_data = NULL;
843 port->port_tty = NULL;
844 port->port_in_use = 0;
846 gs_debug("gs_close: (%d,%p,%p) completed\n",
847 port->port_num, tty, file);
850 spin_unlock_irq(&port->port_lock);
857 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
860 struct gs_port *port = tty->driver_data;
864 printk(KERN_ERR "gs_write: NULL port pointer\n");
868 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
874 spin_lock_irqsave(&port->port_lock, flags);
876 if (port->port_dev == NULL) {
877 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
878 port->port_num, tty);
883 if (port->port_open_count == 0) {
884 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
885 port->port_num, tty);
890 count = gs_buf_put(port->port_write_buf, buf, count);
892 spin_unlock_irqrestore(&port->port_lock, flags);
896 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
902 spin_unlock_irqrestore(&port->port_lock, flags);
909 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
912 struct gs_port *port = tty->driver_data;
915 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
919 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));
921 spin_lock_irqsave(&port->port_lock, flags);
923 if (port->port_dev == NULL) {
924 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
925 port->port_num, tty);
929 if (port->port_open_count == 0) {
930 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
931 port->port_num, tty);
935 gs_buf_put(port->port_write_buf, &ch, 1);
938 spin_unlock_irqrestore(&port->port_lock, flags);
944 static void gs_flush_chars(struct tty_struct *tty)
947 struct gs_port *port = tty->driver_data;
950 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
954 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
956 spin_lock_irqsave(&port->port_lock, flags);
958 if (port->port_dev == NULL) {
960 "gs_flush_chars: (%d,%p) port is not connected\n",
961 port->port_num, tty);
965 if (port->port_open_count == 0) {
966 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
967 port->port_num, tty);
971 spin_unlock_irqrestore(&port->port_lock, flags);
978 spin_unlock_irqrestore(&port->port_lock, flags);
984 static int gs_write_room(struct tty_struct *tty)
989 struct gs_port *port = tty->driver_data;
995 spin_lock_irqsave(&port->port_lock, flags);
997 if (port->port_dev != NULL && port->port_open_count > 0
998 && port->port_write_buf != NULL)
999 room = gs_buf_space_avail(port->port_write_buf);
1001 spin_unlock_irqrestore(&port->port_lock, flags);
1003 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1004 port->port_num, tty, room);
1010 * gs_chars_in_buffer
1012 static int gs_chars_in_buffer(struct tty_struct *tty)
1015 unsigned long flags;
1016 struct gs_port *port = tty->driver_data;
1021 spin_lock_irqsave(&port->port_lock, flags);
1023 if (port->port_dev != NULL && port->port_open_count > 0
1024 && port->port_write_buf != NULL)
1025 chars = gs_buf_data_avail(port->port_write_buf);
1027 spin_unlock_irqrestore(&port->port_lock, flags);
1029 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1030 port->port_num, tty, chars);
1038 static void gs_throttle(struct tty_struct *tty)
1045 static void gs_unthrottle(struct tty_struct *tty)
1052 static void gs_break(struct tty_struct *tty, int break_state)
1059 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1061 struct gs_port *port = tty->driver_data;
1064 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1068 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1069 port->port_num, tty, file, cmd, arg);
1073 /* could not handle ioctl */
1074 return -ENOIOCTLCMD;
1080 static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1087 * This function finds available write requests, calls
1088 * gs_send_packet to fill these packets with data, and
1089 * continues until either there are no more write requests
1090 * available or no more data to send. This function is
1091 * run whenever data arrives or write requests are available.
1093 static int gs_send(struct gs_dev *dev)
1096 unsigned long flags;
1098 struct usb_request *req;
1099 struct gs_req_entry *req_entry;
1102 printk(KERN_ERR "gs_send: NULL device pointer\n");
1106 spin_lock_irqsave(&dev->dev_lock, flags);
1108 ep = dev->dev_in_ep;
1110 while(!list_empty(&dev->dev_req_list)) {
1112 req_entry = list_entry(dev->dev_req_list.next,
1113 struct gs_req_entry, re_entry);
1115 req = req_entry->re_req;
1117 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1120 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));
1121 list_del(&req_entry->re_entry);
1123 spin_unlock_irqrestore(&dev->dev_lock, flags);
1124 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1126 "gs_send: cannot queue read request, ret=%d\n",
1128 spin_lock_irqsave(&dev->dev_lock, flags);
1131 spin_lock_irqsave(&dev->dev_lock, flags);
1138 spin_unlock_irqrestore(&dev->dev_lock, flags);
1146 * If there is data to send, a packet is built in the given
1147 * buffer and the size is returned. If there is no data to
1148 * send, 0 is returned. If there is any error a negative
1149 * error number is returned.
1151 * Called during USB completion routine, on interrupt time.
1153 * We assume that disconnect will not happen until all completion
1154 * routines have completed, so we can assume that the dev_port
1155 * array does not change during the lifetime of this function.
1157 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1160 struct gs_port *port;
1162 /* TEMPORARY -- only port 0 is supported right now */
1163 port = dev->dev_port[0];
1167 "gs_send_packet: port=%d, NULL port pointer\n",
1172 spin_lock(&port->port_lock);
1174 len = gs_buf_data_avail(port->port_write_buf);
1181 size = gs_buf_get(port->port_write_buf, packet, size);
1184 wake_up_interruptible(&port->port_tty->write_wait);
1187 spin_unlock(&port->port_lock);
1194 * Called for each USB packet received. Reads the packet
1195 * header and stuffs the data in the appropriate tty buffer.
1196 * Returns 0 if successful, or a negative error number.
1198 * Called during USB completion routine, on interrupt time.
1200 * We assume that disconnect will not happen until all completion
1201 * routines have completed, so we can assume that the dev_port
1202 * array does not change during the lifetime of this function.
1204 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1207 struct gs_port *port;
1209 struct tty_struct *tty;
1211 /* TEMPORARY -- only port 0 is supported right now */
1212 port = dev->dev_port[0];
1215 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1220 spin_lock(&port->port_lock);
1222 if (port->port_open_count == 0) {
1223 printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",
1230 tty = port->port_tty;
1233 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1239 if (port->port_tty->magic != TTY_MAGIC) {
1240 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1246 len = tty_buffer_request_room(tty, size);
1248 tty_insert_flip_string(tty, packet, len);
1249 tty_flip_buffer_push(port->port_tty);
1250 wake_up_interruptible(&port->port_tty->read_wait);
1254 spin_unlock(&port->port_lock);
1261 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1264 struct gs_dev *dev = ep->driver_data;
1267 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1271 switch(req->status) {
1273 /* normal completion */
1274 gs_recv_packet(dev, req->buf, req->actual);
1276 req->length = ep->maxpacket;
1277 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1279 "gs_read_complete: cannot queue read request, ret=%d\n",
1286 gs_debug("gs_read_complete: shutdown\n");
1287 gs_free_req(ep, req);
1293 "gs_read_complete: unexpected status error, status=%d\n",
1303 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1305 struct gs_dev *dev = ep->driver_data;
1306 struct gs_req_entry *gs_req = req->context;
1309 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1313 switch(req->status) {
1315 /* normal completion */
1317 if (gs_req == NULL) {
1319 "gs_write_complete: NULL request pointer\n");
1323 spin_lock(&dev->dev_lock);
1324 list_add(&gs_req->re_entry, &dev->dev_req_list);
1325 spin_unlock(&dev->dev_lock);
1333 gs_debug("gs_write_complete: shutdown\n");
1334 gs_free_req(ep, req);
1339 "gs_write_complete: unexpected status error, status=%d\n",
1351 * Called on module load. Allocates and initializes the device
1352 * structure and a control request.
1354 static int __init gs_bind(struct usb_gadget *gadget)
1361 /* Some controllers can't support CDC ACM:
1362 * - sh doesn't support multiple interfaces or configs;
1363 * - sa1100 doesn't have a third interrupt endpoint
1365 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1368 gcnum = usb_gadget_controller_number(gadget);
1370 gs_device_desc.bcdDevice =
1371 cpu_to_le16(GS_VERSION_NUM | gcnum);
1373 printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
1375 /* unrecognized, but safe unless bulk is REALLY quirky */
1376 gs_device_desc.bcdDevice =
1377 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1380 usb_ep_autoconfig_reset(gadget);
1382 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1385 EP_IN_NAME = ep->name;
1386 ep->driver_data = ep; /* claim the endpoint */
1388 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1391 EP_OUT_NAME = ep->name;
1392 ep->driver_data = ep; /* claim the endpoint */
1395 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1397 printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);
1400 gs_device_desc.idProduct = __constant_cpu_to_le16(
1402 EP_NOTIFY_NAME = ep->name;
1403 ep->driver_data = ep; /* claim the endpoint */
1406 gs_device_desc.bDeviceClass = use_acm
1407 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1408 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1410 #ifdef CONFIG_USB_GADGET_DUALSPEED
1411 gs_qualifier_desc.bDeviceClass = use_acm
1412 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1413 /* assume ep0 uses the same packet size for both speeds */
1414 gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
1415 /* assume endpoints are dual-speed */
1416 gs_highspeed_notify_desc.bEndpointAddress =
1417 gs_fullspeed_notify_desc.bEndpointAddress;
1418 gs_highspeed_in_desc.bEndpointAddress =
1419 gs_fullspeed_in_desc.bEndpointAddress;
1420 gs_highspeed_out_desc.bEndpointAddress =
1421 gs_fullspeed_out_desc.bEndpointAddress;
1422 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1424 usb_gadget_set_selfpowered(gadget);
1426 if (gadget->is_otg) {
1427 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1428 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1429 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1432 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1436 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1437 system_utsname.sysname, system_utsname.release,
1440 memset(dev, 0, sizeof(struct gs_dev));
1441 dev->dev_gadget = gadget;
1442 spin_lock_init(&dev->dev_lock);
1443 INIT_LIST_HEAD(&dev->dev_req_list);
1444 set_gadget_data(gadget, dev);
1446 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1447 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1452 /* preallocate control response and buffer */
1453 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1455 if (dev->dev_ctrl_req == NULL) {
1459 dev->dev_ctrl_req->complete = gs_setup_complete;
1461 gadget->ep0->driver_data = dev;
1463 printk(KERN_INFO "gs_bind: %s %s bound\n",
1464 GS_LONG_NAME, GS_VERSION_STR);
1469 printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);
1476 * Called on module unload. Frees the control request and device
1479 static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1481 struct gs_dev *dev = get_gadget_data(gadget);
1485 /* read/write requests already freed, only control request remains */
1487 if (dev->dev_ctrl_req != NULL) {
1488 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1489 dev->dev_ctrl_req = NULL;
1493 set_gadget_data(gadget, NULL);
1496 printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1503 * Implements all the control endpoint functionality that's not
1504 * handled in hardware or the hardware driver.
1506 * Returns the size of the data sent to the host, or a negative
1509 static int gs_setup(struct usb_gadget *gadget,
1510 const struct usb_ctrlrequest *ctrl)
1512 int ret = -EOPNOTSUPP;
1513 struct gs_dev *dev = get_gadget_data(gadget);
1514 struct usb_request *req = dev->dev_ctrl_req;
1515 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1516 u16 wValue = le16_to_cpu(ctrl->wValue);
1517 u16 wLength = le16_to_cpu(ctrl->wLength);
1519 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1520 case USB_TYPE_STANDARD:
1521 ret = gs_setup_standard(gadget,ctrl);
1524 case USB_TYPE_CLASS:
1525 ret = gs_setup_class(gadget,ctrl);
1529 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1530 ctrl->bRequestType, ctrl->bRequest,
1531 wValue, wIndex, wLength);
1535 /* respond with data transfer before status phase? */
1538 req->zero = ret < wLength
1539 && (ret % gadget->ep0->maxpacket) == 0;
1540 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1542 printk(KERN_ERR "gs_setup: cannot queue response, ret=%d\n",
1545 gs_setup_complete(gadget->ep0, req);
1549 /* device either stalls (ret < 0) or reports success */
1553 static int gs_setup_standard(struct usb_gadget *gadget,
1554 const struct usb_ctrlrequest *ctrl)
1556 int ret = -EOPNOTSUPP;
1557 struct gs_dev *dev = get_gadget_data(gadget);
1558 struct usb_request *req = dev->dev_ctrl_req;
1559 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1560 u16 wValue = le16_to_cpu(ctrl->wValue);
1561 u16 wLength = le16_to_cpu(ctrl->wLength);
1563 switch (ctrl->bRequest) {
1564 case USB_REQ_GET_DESCRIPTOR:
1565 if (ctrl->bRequestType != USB_DIR_IN)
1568 switch (wValue >> 8) {
1571 (u16)sizeof(struct usb_device_descriptor));
1572 memcpy(req->buf, &gs_device_desc, ret);
1575 #ifdef CONFIG_USB_GADGET_DUALSPEED
1576 case USB_DT_DEVICE_QUALIFIER:
1577 if (!gadget->is_dualspeed)
1580 (u16)sizeof(struct usb_qualifier_descriptor));
1581 memcpy(req->buf, &gs_qualifier_desc, ret);
1584 case USB_DT_OTHER_SPEED_CONFIG:
1585 if (!gadget->is_dualspeed)
1588 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1590 ret = gs_build_config_buf(req->buf, gadget->speed,
1591 wValue >> 8, wValue & 0xff,
1594 ret = min(wLength, (u16)ret);
1598 /* wIndex == language code. */
1599 ret = usb_gadget_get_string(&gs_string_table,
1600 wValue & 0xff, req->buf);
1602 ret = min(wLength, (u16)ret);
1607 case USB_REQ_SET_CONFIGURATION:
1608 if (ctrl->bRequestType != 0)
1610 spin_lock(&dev->dev_lock);
1611 ret = gs_set_config(dev, wValue);
1612 spin_unlock(&dev->dev_lock);
1615 case USB_REQ_GET_CONFIGURATION:
1616 if (ctrl->bRequestType != USB_DIR_IN)
1618 *(u8 *)req->buf = dev->dev_config;
1619 ret = min(wLength, (u16)1);
1622 case USB_REQ_SET_INTERFACE:
1623 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1625 || wIndex >= GS_MAX_NUM_INTERFACES)
1627 if (dev->dev_config == GS_BULK_CONFIG_ID
1628 && wIndex != GS_BULK_INTERFACE_ID)
1630 /* no alternate interface settings */
1633 spin_lock(&dev->dev_lock);
1634 /* PXA hardware partially handles SET_INTERFACE;
1635 * we need to kluge around that interference. */
1636 if (gadget_is_pxa(gadget)) {
1637 ret = gs_set_config(dev, use_acm ?
1638 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1639 goto set_interface_done;
1641 if (dev->dev_config != GS_BULK_CONFIG_ID
1642 && wIndex == GS_CONTROL_INTERFACE_ID) {
1643 if (dev->dev_notify_ep) {
1644 usb_ep_disable(dev->dev_notify_ep);
1645 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1648 usb_ep_disable(dev->dev_in_ep);
1649 usb_ep_disable(dev->dev_out_ep);
1650 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1651 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1655 spin_unlock(&dev->dev_lock);
1658 case USB_REQ_GET_INTERFACE:
1659 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1660 || dev->dev_config == GS_NO_CONFIG_ID)
1662 if (wIndex >= GS_MAX_NUM_INTERFACES
1663 || (dev->dev_config == GS_BULK_CONFIG_ID
1664 && wIndex != GS_BULK_INTERFACE_ID)) {
1668 /* no alternate interface settings */
1669 *(u8 *)req->buf = 0;
1670 ret = min(wLength, (u16)1);
1674 printk(KERN_ERR "gs_setup: unknown standard request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1675 ctrl->bRequestType, ctrl->bRequest,
1676 wValue, wIndex, wLength);
1683 static int gs_setup_class(struct usb_gadget *gadget,
1684 const struct usb_ctrlrequest *ctrl)
1686 int ret = -EOPNOTSUPP;
1687 struct gs_dev *dev = get_gadget_data(gadget);
1688 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1689 struct usb_request *req = dev->dev_ctrl_req;
1690 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1691 u16 wValue = le16_to_cpu(ctrl->wValue);
1692 u16 wLength = le16_to_cpu(ctrl->wLength);
1694 switch (ctrl->bRequest) {
1695 case USB_CDC_REQ_SET_LINE_CODING:
1697 (u16)sizeof(struct usb_cdc_line_coding));
1699 spin_lock(&port->port_lock);
1700 memcpy(&port->port_line_coding, req->buf, ret);
1701 spin_unlock(&port->port_lock);
1705 case USB_CDC_REQ_GET_LINE_CODING:
1706 port = dev->dev_port[0]; /* ACM only has one port */
1708 (u16)sizeof(struct usb_cdc_line_coding));
1710 spin_lock(&port->port_lock);
1711 memcpy(req->buf, &port->port_line_coding, ret);
1712 spin_unlock(&port->port_lock);
1716 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1721 printk(KERN_ERR "gs_setup: unknown class request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1722 ctrl->bRequestType, ctrl->bRequest,
1723 wValue, wIndex, wLength);
1733 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1735 if (req->status || req->actual != req->length) {
1736 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1737 req->status, req->actual, req->length);
1744 * Called when the device is disconnected. Frees the closed
1745 * ports and disconnects open ports. Open ports will be freed
1746 * on close. Then reallocates the ports for the next connection.
1748 static void gs_disconnect(struct usb_gadget *gadget)
1750 unsigned long flags;
1751 struct gs_dev *dev = get_gadget_data(gadget);
1753 spin_lock_irqsave(&dev->dev_lock, flags);
1755 gs_reset_config(dev);
1757 /* free closed ports and disconnect open ports */
1758 /* (open ports will be freed when closed) */
1761 /* re-allocate ports for the next connection */
1762 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1763 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1765 spin_unlock_irqrestore(&dev->dev_lock, flags);
1767 printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1773 * Configures the device by enabling device specific
1774 * optimizations, setting up the endpoints, allocating
1775 * read and write requests and queuing read requests.
1777 * The device lock must be held when calling this function.
1779 static int gs_set_config(struct gs_dev *dev, unsigned config)
1783 struct usb_gadget *gadget = dev->dev_gadget;
1785 struct usb_endpoint_descriptor *ep_desc;
1786 struct usb_request *req;
1787 struct gs_req_entry *req_entry;
1790 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1794 if (config == dev->dev_config)
1797 gs_reset_config(dev);
1800 case GS_NO_CONFIG_ID:
1802 case GS_BULK_CONFIG_ID:
1805 /* device specific optimizations */
1806 if (gadget_is_net2280(gadget))
1807 net2280_set_fifo_mode(gadget, 1);
1809 case GS_ACM_CONFIG_ID:
1812 /* device specific optimizations */
1813 if (gadget_is_net2280(gadget))
1814 net2280_set_fifo_mode(gadget, 1);
1820 dev->dev_config = config;
1822 gadget_for_each_ep(ep, gadget) {
1825 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1826 ep_desc = GS_SPEED_SELECT(
1827 gadget->speed == USB_SPEED_HIGH,
1828 &gs_highspeed_notify_desc,
1829 &gs_fullspeed_notify_desc);
1830 ret = usb_ep_enable(ep,ep_desc);
1832 ep->driver_data = dev;
1833 dev->dev_notify_ep = ep;
1834 dev->dev_notify_ep_desc = ep_desc;
1836 printk(KERN_ERR "gs_set_config: cannot enable notify endpoint %s, ret=%d\n",
1838 goto exit_reset_config;
1842 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1843 ep_desc = GS_SPEED_SELECT(
1844 gadget->speed == USB_SPEED_HIGH,
1845 &gs_highspeed_in_desc,
1846 &gs_fullspeed_in_desc);
1847 ret = usb_ep_enable(ep,ep_desc);
1849 ep->driver_data = dev;
1850 dev->dev_in_ep = ep;
1851 dev->dev_in_ep_desc = ep_desc;
1853 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
1855 goto exit_reset_config;
1859 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1860 ep_desc = GS_SPEED_SELECT(
1861 gadget->speed == USB_SPEED_HIGH,
1862 &gs_highspeed_out_desc,
1863 &gs_fullspeed_out_desc);
1864 ret = usb_ep_enable(ep,ep_desc);
1866 ep->driver_data = dev;
1867 dev->dev_out_ep = ep;
1868 dev->dev_out_ep_desc = ep_desc;
1870 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
1872 goto exit_reset_config;
1878 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
1879 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1880 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
1882 goto exit_reset_config;
1885 /* allocate and queue read requests */
1886 ep = dev->dev_out_ep;
1887 for (i=0; i<read_q_size && ret == 0; i++) {
1888 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1889 req->complete = gs_read_complete;
1890 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1891 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
1895 printk(KERN_ERR "gs_set_config: cannot allocate read requests\n");
1897 goto exit_reset_config;
1901 /* allocate write requests, and put on free list */
1902 ep = dev->dev_in_ep;
1903 for (i=0; i<write_q_size; i++) {
1904 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1905 req_entry->re_req->complete = gs_write_complete;
1906 list_add(&req_entry->re_entry, &dev->dev_req_list);
1908 printk(KERN_ERR "gs_set_config: cannot allocate write requests\n");
1910 goto exit_reset_config;
1914 printk(KERN_INFO "gs_set_config: %s configured, %s speed %s config\n",
1916 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1917 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1922 gs_reset_config(dev);
1929 * Mark the device as not configured, disable all endpoints,
1930 * which forces completion of pending I/O and frees queued
1931 * requests, and free the remaining write requests on the
1934 * The device lock must be held when calling this function.
1936 static void gs_reset_config(struct gs_dev *dev)
1938 struct gs_req_entry *req_entry;
1941 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
1945 if (dev->dev_config == GS_NO_CONFIG_ID)
1948 dev->dev_config = GS_NO_CONFIG_ID;
1950 /* free write requests on the free list */
1951 while(!list_empty(&dev->dev_req_list)) {
1952 req_entry = list_entry(dev->dev_req_list.next,
1953 struct gs_req_entry, re_entry);
1954 list_del(&req_entry->re_entry);
1955 gs_free_req_entry(dev->dev_in_ep, req_entry);
1958 /* disable endpoints, forcing completion of pending i/o; */
1959 /* completion handlers free their requests in this case */
1960 if (dev->dev_notify_ep) {
1961 usb_ep_disable(dev->dev_notify_ep);
1962 dev->dev_notify_ep = NULL;
1964 if (dev->dev_in_ep) {
1965 usb_ep_disable(dev->dev_in_ep);
1966 dev->dev_in_ep = NULL;
1968 if (dev->dev_out_ep) {
1969 usb_ep_disable(dev->dev_out_ep);
1970 dev->dev_out_ep = NULL;
1975 * gs_build_config_buf
1977 * Builds the config descriptors in the given buffer and returns the
1978 * length, or a negative error number.
1980 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
1981 u8 type, unsigned int index, int is_otg)
1985 const struct usb_config_descriptor *config_desc;
1986 const struct usb_descriptor_header **function;
1988 if (index >= gs_device_desc.bNumConfigurations)
1991 /* other speed switches high and full speed */
1992 high_speed = (speed == USB_SPEED_HIGH);
1993 if (type == USB_DT_OTHER_SPEED_CONFIG)
1994 high_speed = !high_speed;
1997 config_desc = &gs_acm_config_desc;
1998 function = GS_SPEED_SELECT(high_speed,
1999 gs_acm_highspeed_function,
2000 gs_acm_fullspeed_function);
2002 config_desc = &gs_bulk_config_desc;
2003 function = GS_SPEED_SELECT(high_speed,
2004 gs_bulk_highspeed_function,
2005 gs_bulk_fullspeed_function);
2008 /* for now, don't advertise srp-only devices */
2012 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2016 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2024 * Allocate a usb_request and its buffer. Returns a pointer to the
2025 * usb_request or NULL if there is an error.
2027 static struct usb_request *
2028 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
2030 struct usb_request *req;
2035 req = usb_ep_alloc_request(ep, kmalloc_flags);
2039 req->buf = kmalloc(len, kmalloc_flags);
2040 if (req->buf == NULL) {
2041 usb_ep_free_request(ep, req);
2052 * Free a usb_request and its buffer.
2054 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2056 if (ep != NULL && req != NULL) {
2058 usb_ep_free_request(ep, req);
2063 * gs_alloc_req_entry
2065 * Allocates a request and its buffer, using the given
2066 * endpoint, buffer len, and kmalloc flags.
2068 static struct gs_req_entry *
2069 gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2071 struct gs_req_entry *req;
2073 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2077 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2078 if (req->re_req == NULL) {
2083 req->re_req->context = req;
2091 * Frees a request and its buffer.
2093 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2095 if (ep != NULL && req != NULL) {
2096 if (req->re_req != NULL)
2097 gs_free_req(ep, req->re_req);
2105 * Allocate all ports and set the gs_dev struct to point to them.
2106 * Return 0 if successful, or a negative error number.
2108 * The device lock is normally held when calling this function.
2110 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2113 struct gs_port *port;
2118 for (i=0; i<GS_NUM_PORTS; i++) {
2119 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2122 port->port_dev = dev;
2124 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
2125 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2126 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2127 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2128 spin_lock_init(&port->port_lock);
2129 init_waitqueue_head(&port->port_write_wait);
2131 dev->dev_port[i] = port;
2140 * Free all closed ports. Open ports are disconnected by
2141 * freeing their write buffers, setting their device pointers
2142 * and the pointers to them in the device to NULL. These
2143 * ports will be freed when closed.
2145 * The device lock is normally held when calling this function.
2147 static void gs_free_ports(struct gs_dev *dev)
2150 unsigned long flags;
2151 struct gs_port *port;
2156 for (i=0; i<GS_NUM_PORTS; i++) {
2157 if ((port=dev->dev_port[i]) != NULL) {
2158 dev->dev_port[i] = NULL;
2160 spin_lock_irqsave(&port->port_lock, flags);
2162 if (port->port_write_buf != NULL) {
2163 gs_buf_free(port->port_write_buf);
2164 port->port_write_buf = NULL;
2167 if (port->port_open_count > 0 || port->port_in_use) {
2168 port->port_dev = NULL;
2169 wake_up_interruptible(&port->port_write_wait);
2170 if (port->port_tty) {
2171 wake_up_interruptible(&port->port_tty->read_wait);
2172 wake_up_interruptible(&port->port_tty->write_wait);
2174 spin_unlock_irqrestore(&port->port_lock, flags);
2176 spin_unlock_irqrestore(&port->port_lock, flags);
2184 /* Circular Buffer */
2189 * Allocate a circular buffer and all associated memory.
2191 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2198 gb = (struct gs_buf *)kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2202 gb->buf_buf = kmalloc(size, kmalloc_flags);
2203 if (gb->buf_buf == NULL) {
2208 gb->buf_size = size;
2209 gb->buf_get = gb->buf_put = gb->buf_buf;
2217 * Free the buffer and all associated memory.
2219 void gs_buf_free(struct gs_buf *gb)
2230 * Clear out all data in the circular buffer.
2232 void gs_buf_clear(struct gs_buf *gb)
2235 gb->buf_get = gb->buf_put;
2236 /* equivalent to a get of all data available */
2242 * Return the number of bytes of data available in the circular
2245 unsigned int gs_buf_data_avail(struct gs_buf *gb)
2248 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2254 * gs_buf_space_avail
2256 * Return the number of bytes of space available in the circular
2259 unsigned int gs_buf_space_avail(struct gs_buf *gb)
2262 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2270 * Copy data data from a user buffer and put it into the circular buffer.
2271 * Restrict to the amount of space available.
2273 * Return the number of bytes copied.
2275 unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2282 len = gs_buf_space_avail(gb);
2289 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2291 memcpy(gb->buf_put, buf, len);
2292 memcpy(gb->buf_buf, buf+len, count - len);
2293 gb->buf_put = gb->buf_buf + count - len;
2295 memcpy(gb->buf_put, buf, count);
2297 gb->buf_put += count;
2298 else /* count == len */
2299 gb->buf_put = gb->buf_buf;
2308 * Get data from the circular buffer and copy to the given buffer.
2309 * Restrict to the amount of data available.
2311 * Return the number of bytes copied.
2313 unsigned int gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2320 len = gs_buf_data_avail(gb);
2327 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2329 memcpy(buf, gb->buf_get, len);
2330 memcpy(buf+len, gb->buf_buf, count - len);
2331 gb->buf_get = gb->buf_buf + count - len;
2333 memcpy(buf, gb->buf_get, count);
2335 gb->buf_get += count;
2336 else /* count == len */
2337 gb->buf_get = gb->buf_buf;