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>
54 #define __wait_cond_interruptible(wq, condition, lock, flags, ret) \
56 wait_queue_t __wait; \
57 init_waitqueue_entry(&__wait, current); \
59 add_wait_queue(&wq, &__wait); \
61 set_current_state(TASK_INTERRUPTIBLE); \
64 if (!signal_pending(current)) { \
65 spin_unlock_irqrestore(lock, flags); \
67 spin_lock_irqsave(lock, flags); \
73 current->state = TASK_RUNNING; \
74 remove_wait_queue(&wq, &__wait); \
77 #define wait_cond_interruptible(wq, condition, lock, flags) \
81 __wait_cond_interruptible(wq, condition, lock, flags, \
86 #define __wait_cond_interruptible_timeout(wq, condition, lock, flags, \
89 signed long __timeout = timeout; \
90 wait_queue_t __wait; \
91 init_waitqueue_entry(&__wait, current); \
93 add_wait_queue(&wq, &__wait); \
95 set_current_state(TASK_INTERRUPTIBLE); \
100 if (!signal_pending(current)) { \
101 spin_unlock_irqrestore(lock, flags); \
102 __timeout = schedule_timeout(__timeout); \
103 spin_lock_irqsave(lock, flags); \
106 ret = -ERESTARTSYS; \
109 current->state = TASK_RUNNING; \
110 remove_wait_queue(&wq, &__wait); \
113 #define wait_cond_interruptible_timeout(wq, condition, lock, flags, \
118 __wait_cond_interruptible_timeout(wq, condition, lock, \
119 flags, timeout, __ret); \
126 #define GS_VERSION_STR "v0.1"
127 #define GS_VERSION_NUM 0x0001
129 #define GS_LONG_NAME "Gadget Serial"
130 #define GS_SHORT_NAME "g_serial"
133 #define GS_MINOR_START 0
135 #define GS_NUM_PORTS 16
137 #define GS_NUM_CONFIGS 1
138 #define GS_NO_CONFIG_ID 0
139 #define GS_BULK_CONFIG_ID 2
141 #define GS_NUM_INTERFACES 1
142 #define GS_INTERFACE_ID 0
143 #define GS_ALT_INTERFACE_ID 0
145 #define GS_NUM_ENDPOINTS 2
147 #define GS_MAX_DESC_LEN 256
149 #define GS_DEFAULT_READ_Q_SIZE 32
150 #define GS_DEFAULT_WRITE_Q_SIZE 32
152 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
153 #define GS_TMP_BUF_SIZE 8192
155 #define GS_CLOSE_TIMEOUT 15
159 static int debug = G_SERIAL_DEBUG;
161 #define gs_debug(format, arg...) \
162 do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
163 #define gs_debug_level(level, format, arg...) \
164 do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
168 #define gs_debug(format, arg...) \
170 #define gs_debug_level(level, format, arg...) \
173 #endif /* G_SERIAL_DEBUG */
176 /* USB Controllers */
179 * NetChip 2280, PCI based.
181 * This has half a dozen configurable endpoints, four with dedicated
182 * DMA channels to manage their FIFOs. It supports high speed.
183 * Those endpoints can be arranged in any desired configuration.
185 #ifdef CONFIG_USB_GADGET_NET2280
186 #define CHIP "net2280"
187 #define EP0_MAXPACKET 64
188 static const char EP_OUT_NAME[] = "ep-a";
190 static const char EP_IN_NAME[] = "ep-b";
193 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
195 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
197 static inline void hw_optimize(struct usb_gadget *gadget)
199 /* we can have bigger ep-a/ep-b fifos (2KB each, 4 packets
200 * for highspeed bulk) because we're not using ep-c/ep-d.
202 net2280_set_fifo_mode (gadget, 1);
208 * Dummy_hcd, software-based loopback controller.
210 * This imitates the abilities of the NetChip 2280, so we will use
211 * the same configuration.
213 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
215 #define EP0_MAXPACKET 64
216 static const char EP_OUT_NAME[] = "ep-a";
218 static const char EP_IN_NAME[] = "ep-b";
221 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
223 /* no hw optimizations to apply */
224 #define hw_optimize(g) do {} while (0)
229 * PXA-2xx UDC: widely used in second gen Linux-capable PDAs.
231 * This has fifteen fixed-function full speed endpoints, and it
232 * can support all USB transfer types.
234 * These supports three or four configurations, with fixed numbers.
235 * The hardware interprets SET_INTERFACE, net effect is that you
236 * can't use altsettings or reset the interfaces independently.
237 * So stick to a single interface.
239 #ifdef CONFIG_USB_GADGET_PXA2XX
240 #define CHIP "pxa2xx"
241 #define EP0_MAXPACKET 16
242 static const char EP_OUT_NAME[] = "ep2out-bulk";
244 static const char EP_IN_NAME[] = "ep1in-bulk";
246 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
248 /* no hw optimizations to apply */
249 #define hw_optimize(g) do {} while (0)
254 * SA-1100 UDC: widely used in first gen Linux-capable PDAs.
256 * This has only two fixed function endpoints, which can only
257 * be used for bulk (or interrupt) transfers. (Plus control.)
259 * Since it can't flush its TX fifos without disabling the UDC,
260 * the current configuration or altsettings can't change except
261 * in special situations. So this is a case of "choose it right
262 * during enumeration" ...
264 #ifdef CONFIG_USB_GADGET_SA1100
265 #define CHIP "sa1100"
266 #define EP0_MAXPACKET 8
267 static const char EP_OUT_NAME[] = "ep1out-bulk";
269 static const char EP_IN_NAME [] = "ep2in-bulk";
271 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
273 /* no hw optimizations to apply */
274 #define hw_optimize(g) do {} while (0)
279 * Toshiba TC86C001 ("Goku-S") UDC
281 * This has three semi-configurable full speed bulk/interrupt endpoints.
283 #ifdef CONFIG_USB_GADGET_GOKU
285 #define DRIVER_VERSION_NUM 0x0116
286 #define EP0_MAXPACKET 8
287 static const char EP_OUT_NAME [] = "ep1-bulk";
289 static const char EP_IN_NAME [] = "ep2-bulk";
291 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
293 /* no hw optimizations to apply */
294 #define hw_optimize(g) do {} while (0)
298 * USB Controller Defaults
300 #ifndef EP0_MAXPACKET
301 #error Configure some USB peripheral controller for g_serial!
305 /* default: say we rely on bus power */
307 /* else value must be USB_CONFIG_ATT_SELFPOWER */
310 #ifndef MAX_USB_POWER
311 /* any hub supports this steady state bus power consumption */
312 #define MAX_USB_POWER 100 /* mA */
316 /* default: this driver won't do remote wakeup */
318 /* else value must be USB_CONFIG_ATT_WAKEUP */
321 /* Thanks to NetChip Technologies for donating this product ID.
323 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
324 * Instead: allocate your own, using normal USB-IF procedures.
326 #define GS_VENDOR_ID 0x0525 /* NetChip */
327 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
334 /* circular buffer */
336 unsigned int buf_size;
342 /* list of requests */
343 struct gs_req_entry {
344 struct list_head re_entry;
345 struct usb_request *re_req;
348 /* the port structure holds info for each port, one for each minor number */
350 struct gs_dev *port_dev; /* pointer to device struct */
351 struct tty_struct *port_tty; /* pointer to tty struct */
352 spinlock_t port_lock;
355 int port_in_use; /* open/close in progress */
356 wait_queue_head_t port_write_wait;/* waiting to write */
357 struct gs_buf *port_write_buf;
360 /* the device structure holds info for the USB device */
362 struct usb_gadget *dev_gadget; /* gadget device pointer */
363 spinlock_t dev_lock; /* lock for set/reset config */
364 int dev_config; /* configuration number */
365 struct usb_ep *dev_in_ep; /* address of in endpoint */
366 struct usb_ep *dev_out_ep; /* address of out endpoint */
367 struct usb_request *dev_ctrl_req; /* control request */
368 struct list_head dev_req_list; /* list of write requests */
369 int dev_sched_port; /* round robin port scheduled */
370 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
377 static int __init gs_module_init(void);
378 static void __exit gs_module_exit(void);
381 static int gs_open(struct tty_struct *tty, struct file *file);
382 static void gs_close(struct tty_struct *tty, struct file *file);
383 static int gs_write(struct tty_struct *tty, int from_user,
384 const unsigned char *buf, int count);
385 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
386 static void gs_flush_chars(struct tty_struct *tty);
387 static int gs_write_room(struct tty_struct *tty);
388 static int gs_chars_in_buffer(struct tty_struct *tty);
389 static void gs_throttle(struct tty_struct * tty);
390 static void gs_unthrottle(struct tty_struct * tty);
391 static void gs_break(struct tty_struct *tty, int break_state);
392 static int gs_ioctl(struct tty_struct *tty, struct file *file,
393 unsigned int cmd, unsigned long arg);
394 static void gs_set_termios(struct tty_struct *tty, struct termios *old);
396 static int gs_send(struct gs_dev *dev);
397 static int gs_send_packet(struct gs_dev *dev, char *packet,
399 static int gs_recv_packet(struct gs_dev *dev, char *packet,
401 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
402 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
405 static int gs_bind(struct usb_gadget *gadget);
406 static void gs_unbind(struct usb_gadget *gadget);
407 static int gs_setup(struct usb_gadget *gadget,
408 const struct usb_ctrlrequest *ctrl);
409 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
410 static void gs_disconnect(struct usb_gadget *gadget);
411 static int gs_set_config(struct gs_dev *dev, unsigned config);
412 static void gs_reset_config(struct gs_dev *dev);
413 static int gs_build_config_desc(u8 *buf, enum usb_device_speed speed,
414 u8 type, unsigned int index);
416 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
418 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
420 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
422 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
424 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags);
425 static void gs_free_ports(struct gs_dev *dev);
427 /* circular buffer */
428 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags);
429 static void gs_buf_free(struct gs_buf *gb);
430 static void gs_buf_clear(struct gs_buf *gb);
431 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
432 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
433 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
435 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
441 static struct gs_dev *gs_device;
443 static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
445 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
446 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
448 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
450 static unsigned char gs_tmp_buf[GS_TMP_BUF_SIZE];
451 static struct semaphore gs_tmp_buf_sem;
453 /* tty driver struct */
454 static struct tty_operations gs_tty_ops = {
458 .put_char = gs_put_char,
459 .flush_chars = gs_flush_chars,
460 .write_room = gs_write_room,
462 .set_termios = gs_set_termios,
463 .throttle = gs_throttle,
464 .unthrottle = gs_unthrottle,
465 .break_ctl = gs_break,
466 .chars_in_buffer = gs_chars_in_buffer,
468 static struct tty_driver *gs_tty_driver;
470 /* gadget driver struct */
471 static struct usb_gadget_driver gs_gadget_driver = {
473 .speed = USB_SPEED_HIGH,
475 .speed = USB_SPEED_FULL,
477 .function = GS_LONG_NAME,
481 .disconnect = gs_disconnect,
483 .name = GS_SHORT_NAME,
484 /* .shutdown = ... */
491 /* USB descriptors */
493 #define GS_MANUFACTURER_STR_ID 1
494 #define GS_PRODUCT_STR_ID 2
495 #define GS_SERIAL_STR_ID 3
496 #define GS_CONFIG_STR_ID 4
498 /* static strings, in iso 8859/1 */
499 static struct usb_string gs_strings[] = {
500 { GS_MANUFACTURER_STR_ID, UTS_SYSNAME " " UTS_RELEASE " with " CHIP },
501 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
502 { GS_SERIAL_STR_ID, "0" },
503 { GS_CONFIG_STR_ID, "Bulk" },
504 { } /* end of list */
507 static struct usb_gadget_strings gs_string_table = {
508 .language = 0x0409, /* en-us */
509 .strings = gs_strings,
512 static const struct usb_device_descriptor gs_device_desc = {
513 .bLength = USB_DT_DEVICE_SIZE,
514 .bDescriptorType = USB_DT_DEVICE,
515 .bcdUSB = __constant_cpu_to_le16(0x0200),
516 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
517 .bMaxPacketSize0 = EP0_MAXPACKET,
518 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
519 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
520 .bcdDevice = __constant_cpu_to_le16(GS_VERSION_NUM),
521 .iManufacturer = GS_MANUFACTURER_STR_ID,
522 .iProduct = GS_PRODUCT_STR_ID,
523 .iSerialNumber = GS_SERIAL_STR_ID,
524 .bNumConfigurations = GS_NUM_CONFIGS,
527 static const struct usb_config_descriptor gs_config_desc = {
528 .bLength = USB_DT_CONFIG_SIZE,
529 .bDescriptorType = USB_DT_CONFIG,
530 /* .wTotalLength set by gs_build_config_desc */
531 .bNumInterfaces = GS_NUM_INTERFACES,
532 .bConfigurationValue = GS_BULK_CONFIG_ID,
533 .iConfiguration = GS_CONFIG_STR_ID,
534 .bmAttributes = USB_CONFIG_ATT_ONE | SELFPOWER | WAKEUP,
535 .bMaxPower = (MAX_USB_POWER + 1) / 2,
538 static const struct usb_interface_descriptor gs_interface_desc = {
539 .bLength = USB_DT_INTERFACE_SIZE,
540 .bDescriptorType = USB_DT_INTERFACE,
541 .bNumEndpoints = GS_NUM_ENDPOINTS,
542 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
543 .iInterface = GS_CONFIG_STR_ID,
546 static const struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
547 .bLength = USB_DT_ENDPOINT_SIZE,
548 .bDescriptorType = USB_DT_ENDPOINT,
549 .bEndpointAddress = EP_IN_NUM | USB_DIR_IN,
550 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551 .wMaxPacketSize = __constant_cpu_to_le16(64),
554 static const struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
555 .bLength = USB_DT_ENDPOINT_SIZE,
556 .bDescriptorType = USB_DT_ENDPOINT,
557 .bEndpointAddress = EP_OUT_NUM | USB_DIR_OUT,
558 .bmAttributes = USB_ENDPOINT_XFER_BULK,
559 .wMaxPacketSize = __constant_cpu_to_le16(64),
562 static const struct usb_endpoint_descriptor gs_highspeed_in_desc = {
563 .bLength = USB_DT_ENDPOINT_SIZE,
564 .bDescriptorType = USB_DT_ENDPOINT,
565 .bEndpointAddress = EP_IN_NUM | USB_DIR_IN,
566 .bmAttributes = USB_ENDPOINT_XFER_BULK,
567 .wMaxPacketSize = __constant_cpu_to_le16(512),
570 static const struct usb_endpoint_descriptor gs_highspeed_out_desc = {
571 .bLength = USB_DT_ENDPOINT_SIZE,
572 .bDescriptorType = USB_DT_ENDPOINT,
573 .bEndpointAddress = EP_OUT_NUM | USB_DIR_OUT,
574 .bmAttributes = USB_ENDPOINT_XFER_BULK,
575 .wMaxPacketSize = __constant_cpu_to_le16(512),
579 static const struct usb_qualifier_descriptor gs_qualifier_desc = {
580 .bLength = sizeof(struct usb_qualifier_descriptor),
581 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
582 .bcdUSB = __constant_cpu_to_le16 (0x0200),
583 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
584 /* assumes ep0 uses the same value for both speeds ... */
585 .bMaxPacketSize0 = EP0_MAXPACKET,
586 .bNumConfigurations = GS_NUM_CONFIGS,
592 MODULE_DESCRIPTION(GS_LONG_NAME);
593 MODULE_AUTHOR("Al Borchers");
594 MODULE_LICENSE("GPL");
597 MODULE_PARM(debug, "i");
598 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
601 MODULE_PARM(read_q_size, "i");
602 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
604 MODULE_PARM(write_q_size, "i");
605 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
607 MODULE_PARM(write_buf_size, "i");
608 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
610 module_init(gs_module_init);
611 module_exit(gs_module_exit);
616 * Register as a USB gadget driver and a tty driver.
618 static int __init gs_module_init(void)
623 retval = usb_gadget_register_driver(&gs_gadget_driver);
625 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
629 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
632 gs_tty_driver->owner = THIS_MODULE;
633 gs_tty_driver->driver_name = GS_SHORT_NAME;
634 gs_tty_driver->name = "ttygs";
635 gs_tty_driver->devfs_name = "usb/ttygs/";
636 gs_tty_driver->major = GS_MAJOR;
637 gs_tty_driver->minor_start = GS_MINOR_START;
638 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
639 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
640 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
641 gs_tty_driver->init_termios = tty_std_termios;
642 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
643 tty_set_operations(gs_tty_driver, &gs_tty_ops);
645 for (i=0; i < GS_NUM_PORTS; i++)
646 sema_init(&gs_open_close_sem[i], 1);
648 sema_init(&gs_tmp_buf_sem, 1);
650 retval = tty_register_driver(gs_tty_driver);
652 usb_gadget_unregister_driver(&gs_gadget_driver);
653 put_tty_driver(gs_tty_driver);
654 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
658 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
665 * Unregister as a tty driver and a USB gadget driver.
667 static void __exit gs_module_exit(void)
669 tty_unregister_driver(gs_tty_driver);
670 put_tty_driver(gs_tty_driver);
671 usb_gadget_unregister_driver(&gs_gadget_driver);
673 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
681 static int gs_open(struct tty_struct *tty, struct file *file)
685 struct gs_port *port;
688 struct semaphore *sem;
690 port_num = tty->index;
692 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
694 tty->driver_data = NULL;
696 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
697 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
698 port_num, tty, file);
705 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
706 port_num, tty, file);
710 sem = &gs_open_close_sem[port_num];
711 if (down_interruptible(sem)) {
713 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
714 port_num, tty, file);
718 spin_lock_irqsave(&dev->dev_lock, flags);
720 if (dev->dev_config == GS_NO_CONFIG_ID) {
722 "gs_open: (%d,%p,%p) device is not connected\n",
723 port_num, tty, file);
724 spin_unlock_irqrestore(&dev->dev_lock, flags);
729 port = dev->dev_port[port_num];
732 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
733 port_num, tty, file);
734 spin_unlock_irqrestore(&dev->dev_lock, flags);
739 spin_lock(&port->port_lock);
740 spin_unlock(&dev->dev_lock);
742 if (port->port_dev == NULL) {
743 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
744 port_num, tty, file);
745 spin_unlock_irqrestore(&port->port_lock, flags);
750 if (port->port_open_count > 0) {
751 ++port->port_open_count;
752 spin_unlock_irqrestore(&port->port_lock, flags);
753 gs_debug("gs_open: (%d,%p,%p) already open\n",
754 port_num, tty, file);
759 /* mark port as in use, we can drop port lock and sleep if necessary */
760 port->port_in_use = 1;
762 /* allocate write buffer on first open */
763 if (port->port_write_buf == NULL) {
764 spin_unlock_irqrestore(&port->port_lock, flags);
765 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
766 spin_lock_irqsave(&port->port_lock, flags);
768 /* might have been disconnected while asleep, check */
769 if (port->port_dev == NULL) {
771 "gs_open: (%d,%p,%p) port disconnected (2)\n",
772 port_num, tty, file);
773 port->port_in_use = 0;
774 spin_unlock_irqrestore(&port->port_lock, flags);
779 if ((port->port_write_buf=buf) == NULL) {
780 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
781 port_num, tty, file);
782 port->port_in_use = 0;
783 spin_unlock_irqrestore(&port->port_lock, flags);
790 /* wait for carrier detect (not implemented) */
792 /* might have been disconnected while asleep, check */
793 if (port->port_dev == NULL) {
794 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
795 port_num, tty, file);
796 port->port_in_use = 0;
797 spin_unlock_irqrestore(&port->port_lock, flags);
802 tty->driver_data = port;
803 port->port_tty = tty;
804 port->port_open_count = 1;
805 port->port_in_use = 0;
807 spin_unlock_irqrestore(&port->port_lock, flags);
810 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
818 static void gs_close(struct tty_struct *tty, struct file *file)
821 struct gs_port *port = tty->driver_data;
822 struct semaphore *sem;
825 printk(KERN_ERR "gs_close: NULL port pointer\n");
829 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
831 sem = &gs_open_close_sem[port->port_num];
834 spin_lock_irqsave(&port->port_lock, flags);
836 if (port->port_open_count == 0) {
838 "gs_close: (%d,%p,%p) port is already closed\n",
839 port->port_num, tty, file);
840 spin_unlock_irqrestore(&port->port_lock, flags);
845 if (port->port_open_count > 0) {
846 --port->port_open_count;
847 spin_unlock_irqrestore(&port->port_lock, flags);
852 /* free disconnected port on final close */
853 if (port->port_dev == NULL) {
855 spin_unlock_irqrestore(&port->port_lock, flags);
860 /* mark port as closed but in use, we can drop port lock */
861 /* and sleep if necessary */
862 port->port_in_use = 1;
863 port->port_open_count = 0;
865 /* wait for write buffer to drain, or */
866 /* at most GS_CLOSE_TIMEOUT seconds */
867 if (gs_buf_data_avail(port->port_write_buf) > 0) {
868 wait_cond_interruptible_timeout(port->port_write_wait,
869 port->port_dev == NULL
870 || gs_buf_data_avail(port->port_write_buf) == 0,
871 &port->port_lock, flags, GS_CLOSE_TIMEOUT * HZ);
874 /* free disconnected port on final close */
875 /* (might have happened during the above sleep) */
876 if (port->port_dev == NULL) {
878 spin_unlock_irqrestore(&port->port_lock, flags);
883 gs_buf_clear(port->port_write_buf);
885 tty->driver_data = NULL;
886 port->port_tty = NULL;
887 port->port_in_use = 0;
889 spin_unlock_irqrestore(&port->port_lock, flags);
892 gs_debug("gs_close: (%d,%p,%p) completed\n",
893 port->port_num, tty, file);
899 static int gs_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
902 struct gs_port *port = tty->driver_data;
905 printk(KERN_ERR "gs_write: NULL port pointer\n");
909 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
915 /* copy from user into tmp buffer, get tmp_buf semaphore */
917 if (count > GS_TMP_BUF_SIZE)
918 count = GS_TMP_BUF_SIZE;
919 down(&gs_tmp_buf_sem);
920 if (copy_from_user(gs_tmp_buf, buf, count) != 0) {
923 "gs_write: (%d,%p) cannot copy from user space\n",
924 port->port_num, tty);
930 spin_lock_irqsave(&port->port_lock, flags);
932 if (port->port_dev == NULL) {
933 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
934 port->port_num, tty);
935 spin_unlock_irqrestore(&port->port_lock, flags);
941 if (port->port_open_count == 0) {
942 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
943 port->port_num, tty);
944 spin_unlock_irqrestore(&port->port_lock, flags);
950 count = gs_buf_put(port->port_write_buf, buf, count);
952 spin_unlock_irqrestore(&port->port_lock, flags);
959 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
968 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
971 struct gs_port *port = tty->driver_data;
974 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
978 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));
980 spin_lock_irqsave(&port->port_lock, flags);
982 if (port->port_dev == NULL) {
983 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
984 port->port_num, tty);
985 spin_unlock_irqrestore(&port->port_lock, flags);
989 if (port->port_open_count == 0) {
990 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
991 port->port_num, tty);
992 spin_unlock_irqrestore(&port->port_lock, flags);
996 gs_buf_put(port->port_write_buf, &ch, 1);
998 spin_unlock_irqrestore(&port->port_lock, flags);
1004 static void gs_flush_chars(struct tty_struct *tty)
1006 unsigned long flags;
1007 struct gs_port *port = tty->driver_data;
1010 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
1014 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
1016 spin_lock_irqsave(&port->port_lock, flags);
1018 if (port->port_dev == NULL) {
1020 "gs_flush_chars: (%d,%p) port is not connected\n",
1021 port->port_num, tty);
1022 spin_unlock_irqrestore(&port->port_lock, flags);
1026 if (port->port_open_count == 0) {
1027 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
1028 port->port_num, tty);
1029 spin_unlock_irqrestore(&port->port_lock, flags);
1033 spin_unlock_irqrestore(&port->port_lock, flags);
1041 static int gs_write_room(struct tty_struct *tty)
1045 unsigned long flags;
1046 struct gs_port *port = tty->driver_data;
1052 spin_lock_irqsave(&port->port_lock, flags);
1054 if (port->port_dev != NULL && port->port_open_count > 0
1055 && port->port_write_buf != NULL)
1056 room = gs_buf_space_avail(port->port_write_buf);
1058 spin_unlock_irqrestore(&port->port_lock, flags);
1060 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1061 port->port_num, tty, room);
1067 * gs_chars_in_buffer
1069 static int gs_chars_in_buffer(struct tty_struct *tty)
1072 unsigned long flags;
1073 struct gs_port *port = tty->driver_data;
1078 spin_lock_irqsave(&port->port_lock, flags);
1080 if (port->port_dev != NULL && port->port_open_count > 0
1081 && port->port_write_buf != NULL)
1082 chars = gs_buf_data_avail(port->port_write_buf);
1084 spin_unlock_irqrestore(&port->port_lock, flags);
1086 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1087 port->port_num, tty, chars);
1095 static void gs_throttle(struct tty_struct *tty)
1102 static void gs_unthrottle(struct tty_struct *tty)
1109 static void gs_break(struct tty_struct *tty, int break_state)
1116 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1118 struct gs_port *port = tty->driver_data;
1121 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1125 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1126 port->port_num, tty, file, cmd, arg);
1130 /* could not handle ioctl */
1131 return -ENOIOCTLCMD;
1137 static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1144 * This function finds available write requests, calls
1145 * gs_send_packet to fill these packets with data, and
1146 * continues until either there are no more write requests
1147 * available or no more data to send. This function is
1148 * run whenever data arrives or write requests are available.
1150 static int gs_send(struct gs_dev *dev)
1153 unsigned long flags;
1155 struct usb_request *req;
1156 struct gs_req_entry *req_entry;
1159 printk(KERN_ERR "gs_send: NULL device pointer\n");
1163 spin_lock_irqsave(&dev->dev_lock, flags);
1165 ep = dev->dev_in_ep;
1167 while(!list_empty(&dev->dev_req_list)) {
1169 req_entry = list_entry(dev->dev_req_list.next,
1170 struct gs_req_entry, re_entry);
1172 req = req_entry->re_req;
1174 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1177 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));
1178 list_del(&req_entry->re_entry);
1180 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1182 "gs_send: cannot queue read request, ret=%d\n",
1192 spin_unlock_irqrestore(&dev->dev_lock, flags);
1200 * If there is data to send, a packet is built in the given
1201 * buffer and the size is returned. If there is no data to
1202 * send, 0 is returned. If there is any error a negative
1203 * error number is returned.
1205 * Called during USB completion routine, on interrupt time.
1207 * We assume that disconnect will not happen until all completion
1208 * routines have completed, so we can assume that the dev_port
1209 * array does not change during the lifetime of this function.
1211 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1214 struct gs_port *port;
1216 /* TEMPORARY -- only port 0 is supported right now */
1217 port = dev->dev_port[0];
1221 "gs_send_packet: port=%d, NULL port pointer\n",
1226 spin_lock(&port->port_lock);
1228 len = gs_buf_data_avail(port->port_write_buf);
1233 spin_unlock(&port->port_lock);
1237 size = gs_buf_get(port->port_write_buf, packet, size);
1239 wake_up_interruptible(&port->port_tty->write_wait);
1241 spin_unlock(&port->port_lock);
1249 * Called for each USB packet received. Reads the packet
1250 * header and stuffs the data in the appropriate tty buffer.
1251 * Returns 0 if successful, or a negative error number.
1253 * Called during USB completion routine, on interrupt time.
1255 * We assume that disconnect will not happen until all completion
1256 * routines have completed, so we can assume that the dev_port
1257 * array does not change during the lifetime of this function.
1259 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1262 struct gs_port *port;
1264 /* TEMPORARY -- only port 0 is supported right now */
1265 port = dev->dev_port[0];
1268 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1273 spin_lock(&port->port_lock);
1275 if (port->port_tty == NULL) {
1276 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1278 spin_unlock(&port->port_lock);
1282 if (port->port_tty->magic != TTY_MAGIC) {
1283 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1285 spin_unlock(&port->port_lock);
1289 len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);
1294 memcpy(port->port_tty->flip.char_buf_ptr, packet, size);
1295 port->port_tty->flip.char_buf_ptr += size;
1296 port->port_tty->flip.count += size;
1297 tty_flip_buffer_push(port->port_tty);
1298 wake_up_interruptible(&port->port_tty->read_wait);
1301 spin_unlock(&port->port_lock);
1309 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1312 struct gs_dev *dev = ep->driver_data;
1315 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1319 switch(req->status) {
1321 /* normal completion */
1322 gs_recv_packet(dev, req->buf, req->actual);
1324 req->length = ep->maxpacket;
1325 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1327 "gs_read_complete: cannot queue read request, ret=%d\n",
1334 gs_debug("gs_read_complete: shutdown\n");
1335 gs_free_req(ep, req);
1341 "gs_read_complete: unexpected status error, status=%d\n",
1351 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1353 struct gs_dev *dev = ep->driver_data;
1354 struct gs_req_entry *gs_req = req->context;
1357 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1361 switch(req->status) {
1363 /* normal completion */
1365 if (gs_req == NULL) {
1367 "gs_write_complete: NULL request pointer\n");
1371 spin_lock(&dev->dev_lock);
1372 list_add(&gs_req->re_entry, &dev->dev_req_list);
1373 spin_unlock(&dev->dev_lock);
1381 gs_debug("gs_write_complete: shutdown\n");
1382 gs_free_req(ep, req);
1387 "gs_write_complete: unexpected status error, status=%d\n",
1399 * Called on module load. Allocates and initializes the device
1400 * structure and a control request.
1402 static int gs_bind(struct usb_gadget *gadget)
1407 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1411 set_gadget_data(gadget, dev);
1413 memset(dev, 0, sizeof(struct gs_dev));
1414 dev->dev_gadget = gadget;
1415 spin_lock_init(&dev->dev_lock);
1416 INIT_LIST_HEAD(&dev->dev_req_list);
1418 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1419 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1424 /* preallocate control response and buffer */
1425 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1427 if (dev->dev_ctrl_req == NULL) {
1431 dev->dev_ctrl_req->complete = gs_setup_complete;
1433 gadget->ep0->driver_data = dev;
1435 printk(KERN_INFO "gs_bind: %s %s bound\n",
1436 GS_LONG_NAME, GS_VERSION_STR);
1444 * Called on module unload. Frees the control request and device
1447 static void gs_unbind(struct usb_gadget *gadget)
1449 struct gs_dev *dev = get_gadget_data(gadget);
1453 /* read/write requests already freed, only control request remains */
1455 if (dev->dev_ctrl_req != NULL)
1456 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1459 set_gadget_data(gadget, NULL);
1462 printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1469 * Implements all the control endpoint functionality that's not
1470 * handled in hardware or the hardware driver.
1472 * Returns the size of the data sent to the host, or a negative
1475 static int gs_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1477 int ret = -EOPNOTSUPP;
1478 unsigned int sv_config;
1479 struct gs_dev *dev = get_gadget_data(gadget);
1480 struct usb_request *req = dev->dev_ctrl_req;
1482 switch (ctrl->bRequest) {
1483 case USB_REQ_GET_DESCRIPTOR:
1484 if (ctrl->bRequestType != USB_DIR_IN)
1487 switch (ctrl->wValue >> 8) {
1489 ret = min(ctrl->wLength,
1490 (u16)sizeof(struct usb_device_descriptor));
1491 memcpy(req->buf, &gs_device_desc, ret);
1495 case USB_DT_DEVICE_QUALIFIER:
1496 ret = min(ctrl->wLength,
1497 (u16)sizeof(struct usb_qualifier_descriptor));
1498 memcpy(req->buf, &gs_qualifier_desc, ret);
1501 case USB_DT_OTHER_SPEED_CONFIG:
1502 #endif /* HIGHSPEED */
1504 ret = gs_build_config_desc(req->buf, gadget->speed,
1505 ctrl->wValue >> 8, ctrl->wValue & 0xff);
1507 ret = min(ctrl->wLength, (u16)ret);
1511 /* wIndex == language code. */
1512 ret = usb_gadget_get_string(&gs_string_table,
1513 ctrl->wValue & 0xff, req->buf);
1515 ret = min(ctrl->wLength, (u16)ret);
1520 case USB_REQ_SET_CONFIGURATION:
1521 if (ctrl->bRequestType != 0)
1523 spin_lock(&dev->dev_lock);
1524 ret = gs_set_config(dev, ctrl->wValue);
1525 spin_unlock(&dev->dev_lock);
1528 case USB_REQ_GET_CONFIGURATION:
1529 if (ctrl->bRequestType != USB_DIR_IN)
1531 *(u8 *)req->buf = dev->dev_config;
1532 ret = min(ctrl->wLength, (u16)1);
1535 case USB_REQ_SET_INTERFACE:
1536 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1538 spin_lock(&dev->dev_lock);
1539 if (dev->dev_config == GS_BULK_CONFIG_ID
1540 && ctrl->wIndex == GS_INTERFACE_ID
1541 && ctrl->wValue == GS_ALT_INTERFACE_ID) {
1542 sv_config = dev->dev_config;
1543 /* since there is only one interface, setting the */
1544 /* interface is equivalent to setting the config */
1545 gs_reset_config(dev);
1546 gs_set_config(dev, sv_config);
1549 spin_unlock(&dev->dev_lock);
1552 case USB_REQ_GET_INTERFACE:
1553 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1555 if (dev->dev_config == GS_NO_CONFIG_ID)
1557 if (ctrl->wIndex != GS_INTERFACE_ID) {
1561 *(u8 *)req->buf = GS_ALT_INTERFACE_ID;
1562 ret = min(ctrl->wLength, (u16)1);
1566 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1567 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1568 ctrl->wIndex, ctrl->wLength);
1573 /* respond with data transfer before status phase? */
1576 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1579 "gs_setup: cannot queue response, ret=%d\n",
1582 gs_setup_complete(gadget->ep0, req);
1586 /* device either stalls (ret < 0) or reports success */
1593 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1595 if (req->status || req->actual != req->length) {
1596 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1597 req->status, req->actual, req->length);
1604 * Called when the device is disconnected. Frees the closed
1605 * ports and disconnects open ports. Open ports will be freed
1606 * on close. Then reallocates the ports for the next connection.
1608 static void gs_disconnect(struct usb_gadget *gadget)
1610 unsigned long flags;
1611 struct gs_dev *dev = get_gadget_data(gadget);
1613 spin_lock_irqsave(&dev->dev_lock, flags);
1615 gs_reset_config(dev);
1617 /* free closed ports and disconnect open ports */
1618 /* (open ports will be freed when closed) */
1621 /* re-allocate ports for the next connection */
1622 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1623 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1625 spin_unlock_irqrestore(&dev->dev_lock, flags);
1627 printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1633 * Configures the device by enabling device specific
1634 * optimizations, setting up the endpoints, allocating
1635 * read and write requests and queuing read requests.
1637 * The device lock must be held when calling this function.
1639 static int gs_set_config(struct gs_dev *dev, unsigned config)
1643 struct usb_gadget *gadget = dev->dev_gadget;
1645 struct usb_request *req;
1646 struct gs_req_entry *req_entry;
1649 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1653 if (config == dev->dev_config)
1656 gs_reset_config(dev);
1658 if (config == GS_NO_CONFIG_ID)
1661 if (config != GS_BULK_CONFIG_ID)
1664 hw_optimize(gadget);
1666 gadget_for_each_ep(ep, gadget) {
1668 if (strcmp(ep->name, EP_IN_NAME) == 0) {
1669 ret = usb_ep_enable(ep,
1670 gadget->speed == USB_SPEED_HIGH ?
1671 &gs_highspeed_in_desc : &gs_fullspeed_in_desc);
1673 ep->driver_data = dev;
1674 dev->dev_in_ep = ep;
1676 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
1678 gs_reset_config(dev);
1683 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1684 ret = usb_ep_enable(ep,
1685 gadget->speed == USB_SPEED_HIGH ?
1686 &gs_highspeed_out_desc :
1687 &gs_fullspeed_out_desc);
1689 ep->driver_data = dev;
1690 dev->dev_out_ep = ep;
1692 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
1694 gs_reset_config(dev);
1701 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL) {
1702 gs_reset_config(dev);
1703 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
1707 /* allocate and queue read requests */
1708 ep = dev->dev_out_ep;
1709 for (i=0; i<read_q_size && ret == 0; i++) {
1710 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1711 req->complete = gs_read_complete;
1712 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1713 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
1717 gs_reset_config(dev);
1719 "gs_set_config: cannot allocate read requests\n");
1724 /* allocate write requests, and put on free list */
1725 ep = dev->dev_in_ep;
1726 for (i=0; i<write_q_size; i++) {
1727 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1728 req_entry->re_req->complete = gs_write_complete;
1729 list_add(&req_entry->re_entry, &dev->dev_req_list);
1731 gs_reset_config(dev);
1733 "gs_set_config: cannot allocate write requests\n");
1738 dev->dev_config = config;
1740 printk(KERN_INFO "gs_set_config: %s configured for %s speed\n",
1742 gadget->speed == USB_SPEED_HIGH ? "high" : "full");
1750 * Mark the device as not configured, disable all endpoints,
1751 * which forces completion of pending I/O and frees queued
1752 * requests, and free the remaining write requests on the
1755 * The device lock must be held when calling this function.
1757 static void gs_reset_config(struct gs_dev *dev)
1759 struct gs_req_entry *req_entry;
1762 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
1766 if (dev->dev_config == GS_NO_CONFIG_ID)
1769 dev->dev_config = GS_NO_CONFIG_ID;
1771 /* free write requests on the free list */
1772 while(!list_empty(&dev->dev_req_list)) {
1773 req_entry = list_entry(dev->dev_req_list.next,
1774 struct gs_req_entry, re_entry);
1775 list_del(&req_entry->re_entry);
1776 gs_free_req_entry(dev->dev_in_ep, req_entry);
1779 /* disable endpoints, forcing completion of pending i/o; */
1780 /* completion handlers free their requests in this case */
1781 if (dev->dev_in_ep) {
1782 usb_ep_disable(dev->dev_in_ep);
1783 dev->dev_in_ep = NULL;
1785 if (dev->dev_out_ep) {
1786 usb_ep_disable(dev->dev_out_ep);
1787 dev->dev_out_ep = NULL;
1792 * gs_build_config_desc
1794 * Builds a config descriptor in the given buffer and returns the
1795 * length, or a negative error number.
1797 static int gs_build_config_desc(u8 *buf, enum usb_device_speed speed, u8 type, unsigned int index)
1800 int len = USB_DT_CONFIG_SIZE + USB_DT_INTERFACE_SIZE
1801 + GS_NUM_ENDPOINTS * USB_DT_ENDPOINT_SIZE;
1803 /* only one config */
1807 memcpy(buf, &gs_config_desc, USB_DT_CONFIG_SIZE);
1808 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
1809 ((struct usb_config_descriptor *)buf)->wTotalLength = __constant_cpu_to_le16(len);
1810 buf += USB_DT_CONFIG_SIZE;
1812 memcpy(buf, &gs_interface_desc, USB_DT_INTERFACE_SIZE);
1813 buf += USB_DT_INTERFACE_SIZE;
1815 /* other speed switches high and full speed */
1816 high_speed = (speed == USB_SPEED_HIGH);
1817 if (type == USB_DT_OTHER_SPEED_CONFIG)
1818 high_speed = !high_speed;
1821 high_speed ? &gs_highspeed_in_desc : &gs_fullspeed_in_desc,
1822 USB_DT_ENDPOINT_SIZE);
1823 buf += USB_DT_ENDPOINT_SIZE;
1825 high_speed ? &gs_highspeed_out_desc : &gs_fullspeed_out_desc,
1826 USB_DT_ENDPOINT_SIZE);
1834 * Allocate a usb_request and its buffer. Returns a pointer to the
1835 * usb_request or NULL if there is an error.
1837 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, int kmalloc_flags)
1839 struct usb_request *req;
1844 req = usb_ep_alloc_request(ep, kmalloc_flags);
1848 req->buf = usb_ep_alloc_buffer(ep, len, &req->dma,
1850 if (req->buf == NULL) {
1851 usb_ep_free_request(ep, req);
1862 * Free a usb_request and its buffer.
1864 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
1866 if (ep != NULL && req != NULL) {
1867 if (req->buf != NULL)
1868 usb_ep_free_buffer(ep, req->buf, req->dma,
1870 usb_ep_free_request(ep, req);
1875 * gs_alloc_req_entry
1877 * Allocates a request and its buffer, using the given
1878 * endpoint, buffer len, and kmalloc flags.
1880 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len, int kmalloc_flags)
1882 struct gs_req_entry *req;
1884 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
1888 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
1889 if (req->re_req == NULL) {
1894 req->re_req->context = req;
1902 * Frees a request and its buffer.
1904 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
1906 if (ep != NULL && req != NULL) {
1907 if (req->re_req != NULL)
1908 gs_free_req(ep, req->re_req);
1916 * Allocate all ports and set the gs_dev struct to point to them.
1917 * Return 0 if successful, or a negative error number.
1919 * The device lock is normally held when calling this function.
1921 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags)
1924 struct gs_port *port;
1929 for (i=0; i<GS_NUM_PORTS; i++) {
1930 if ((port=(struct gs_port *)kmalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
1933 memset(port, 0, sizeof(struct gs_port));
1934 port->port_dev = dev;
1936 spin_lock_init(&port->port_lock);
1937 init_waitqueue_head(&port->port_write_wait);
1939 dev->dev_port[i] = port;
1948 * Free all closed ports. Open ports are disconnected by
1949 * freeing their write buffers, setting their device pointers
1950 * and the pointers to them in the device to NULL. These
1951 * ports will be freed when closed.
1953 * The device lock is normally held when calling this function.
1955 static void gs_free_ports(struct gs_dev *dev)
1958 unsigned long flags;
1959 struct gs_port *port;
1964 for (i=0; i<GS_NUM_PORTS; i++) {
1965 if ((port=dev->dev_port[i]) != NULL) {
1966 dev->dev_port[i] = NULL;
1968 spin_lock_irqsave(&port->port_lock, flags);
1970 if (port->port_write_buf != NULL) {
1971 gs_buf_free(port->port_write_buf);
1972 port->port_write_buf = NULL;
1975 if (port->port_open_count > 0 || port->port_in_use) {
1976 port->port_dev = NULL;
1977 wake_up_interruptible(&port->port_write_wait);
1978 wake_up_interruptible(&port->port_tty->read_wait);
1979 wake_up_interruptible(&port->port_tty->write_wait);
1984 spin_unlock_irqrestore(&port->port_lock, flags);
1989 /* Circular Buffer */
1994 * Allocate a circular buffer and all associated memory.
1996 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags)
2003 gb = (struct gs_buf *)kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2007 gb->buf_buf = kmalloc(size, kmalloc_flags);
2008 if (gb->buf_buf == NULL) {
2013 gb->buf_size = size;
2014 gb->buf_get = gb->buf_put = gb->buf_buf;
2022 * Free the buffer and all associated memory.
2024 void gs_buf_free(struct gs_buf *gb)
2027 if (gb->buf_buf != NULL)
2036 * Clear out all data in the circular buffer.
2038 void gs_buf_clear(struct gs_buf *gb)
2041 gb->buf_get = gb->buf_put;
2042 /* equivalent to a get of all data available */
2048 * Return the number of bytes of data available in the circular
2051 unsigned int gs_buf_data_avail(struct gs_buf *gb)
2054 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2060 * gs_buf_space_avail
2062 * Return the number of bytes of space available in the circular
2065 unsigned int gs_buf_space_avail(struct gs_buf *gb)
2068 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2076 * Copy data data from a user buffer and put it into the circular buffer.
2077 * Restrict to the amount of space available.
2079 * Return the number of bytes copied.
2081 unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2088 len = gs_buf_space_avail(gb);
2095 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2097 memcpy(gb->buf_put, buf, len);
2098 memcpy(gb->buf_buf, buf+len, count - len);
2099 gb->buf_put = gb->buf_buf + count - len;
2101 memcpy(gb->buf_put, buf, count);
2103 gb->buf_put += count;
2104 else /* count == len */
2105 gb->buf_put = gb->buf_buf;
2114 * Get data from the circular buffer and copy to the given buffer.
2115 * Restrict to the amount of data available.
2117 * Return the number of bytes copied.
2119 unsigned int gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2126 len = gs_buf_data_avail(gb);
2133 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2135 memcpy(buf, gb->buf_get, len);
2136 memcpy(buf+len, gb->buf_buf, count - len);
2137 gb->buf_get = gb->buf_buf + count - len;
2139 memcpy(buf, gb->buf_get, count);
2141 gb->buf_get += count;
2142 else /* count == len */
2143 gb->buf_get = gb->buf_buf;