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)
252 #ifdef CONFIG_USB_GADGET_OMAP
254 #define EP0_MAXPACKET 64
255 static const char EP_OUT_NAME [] = "ep2out-bulk";
257 static const char EP_IN_NAME [] = "ep1in-bulk";
259 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
260 /* supports remote wakeup, but this driver doesn't */
262 /* no hw optimizations to apply */
263 #define hw_optimize(g) do {} while (0)
268 * SA-1100 UDC: widely used in first gen Linux-capable PDAs.
270 * This has only two fixed function endpoints, which can only
271 * be used for bulk (or interrupt) transfers. (Plus control.)
273 * Since it can't flush its TX fifos without disabling the UDC,
274 * the current configuration or altsettings can't change except
275 * in special situations. So this is a case of "choose it right
276 * during enumeration" ...
278 #ifdef CONFIG_USB_GADGET_SA1100
279 #define CHIP "sa1100"
280 #define EP0_MAXPACKET 8
281 static const char EP_OUT_NAME[] = "ep1out-bulk";
283 static const char EP_IN_NAME [] = "ep2in-bulk";
285 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
287 /* no hw optimizations to apply */
288 #define hw_optimize(g) do {} while (0)
293 * Toshiba TC86C001 ("Goku-S") UDC
295 * This has three semi-configurable full speed bulk/interrupt endpoints.
297 #ifdef CONFIG_USB_GADGET_GOKU
299 #define DRIVER_VERSION_NUM 0x0116
300 #define EP0_MAXPACKET 8
301 static const char EP_OUT_NAME [] = "ep1-bulk";
303 static const char EP_IN_NAME [] = "ep2-bulk";
305 #define SELFPOWER USB_CONFIG_ATT_SELFPOWER
307 /* no hw optimizations to apply */
308 #define hw_optimize(g) do {} while (0)
312 * USB Controller Defaults
314 #ifndef EP0_MAXPACKET
315 #error Configure some USB peripheral controller for g_serial!
319 /* default: say we rely on bus power */
321 /* else value must be USB_CONFIG_ATT_SELFPOWER */
324 #ifndef MAX_USB_POWER
325 /* any hub supports this steady state bus power consumption */
326 #define MAX_USB_POWER 100 /* mA */
330 /* default: this driver won't do remote wakeup */
332 /* else value must be USB_CONFIG_ATT_WAKEUP */
335 /* Thanks to NetChip Technologies for donating this product ID.
337 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
338 * Instead: allocate your own, using normal USB-IF procedures.
340 #define GS_VENDOR_ID 0x0525 /* NetChip */
341 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
348 /* circular buffer */
350 unsigned int buf_size;
356 /* list of requests */
357 struct gs_req_entry {
358 struct list_head re_entry;
359 struct usb_request *re_req;
362 /* the port structure holds info for each port, one for each minor number */
364 struct gs_dev *port_dev; /* pointer to device struct */
365 struct tty_struct *port_tty; /* pointer to tty struct */
366 spinlock_t port_lock;
369 int port_in_use; /* open/close in progress */
370 wait_queue_head_t port_write_wait;/* waiting to write */
371 struct gs_buf *port_write_buf;
374 /* the device structure holds info for the USB device */
376 struct usb_gadget *dev_gadget; /* gadget device pointer */
377 spinlock_t dev_lock; /* lock for set/reset config */
378 int dev_config; /* configuration number */
379 struct usb_ep *dev_in_ep; /* address of in endpoint */
380 struct usb_ep *dev_out_ep; /* address of out endpoint */
381 struct usb_request *dev_ctrl_req; /* control request */
382 struct list_head dev_req_list; /* list of write requests */
383 int dev_sched_port; /* round robin port scheduled */
384 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
391 static int __init gs_module_init(void);
392 static void __exit gs_module_exit(void);
395 static int gs_open(struct tty_struct *tty, struct file *file);
396 static void gs_close(struct tty_struct *tty, struct file *file);
397 static int gs_write(struct tty_struct *tty, int from_user,
398 const unsigned char *buf, int count);
399 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
400 static void gs_flush_chars(struct tty_struct *tty);
401 static int gs_write_room(struct tty_struct *tty);
402 static int gs_chars_in_buffer(struct tty_struct *tty);
403 static void gs_throttle(struct tty_struct * tty);
404 static void gs_unthrottle(struct tty_struct * tty);
405 static void gs_break(struct tty_struct *tty, int break_state);
406 static int gs_ioctl(struct tty_struct *tty, struct file *file,
407 unsigned int cmd, unsigned long arg);
408 static void gs_set_termios(struct tty_struct *tty, struct termios *old);
410 static int gs_send(struct gs_dev *dev);
411 static int gs_send_packet(struct gs_dev *dev, char *packet,
413 static int gs_recv_packet(struct gs_dev *dev, char *packet,
415 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
416 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
419 static int gs_bind(struct usb_gadget *gadget);
420 static void gs_unbind(struct usb_gadget *gadget);
421 static int gs_setup(struct usb_gadget *gadget,
422 const struct usb_ctrlrequest *ctrl);
423 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
424 static void gs_disconnect(struct usb_gadget *gadget);
425 static int gs_set_config(struct gs_dev *dev, unsigned config);
426 static void gs_reset_config(struct gs_dev *dev);
427 static int gs_build_config_desc(u8 *buf, enum usb_device_speed speed,
428 u8 type, unsigned int index);
430 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
432 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
434 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
436 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
438 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags);
439 static void gs_free_ports(struct gs_dev *dev);
441 /* circular buffer */
442 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags);
443 static void gs_buf_free(struct gs_buf *gb);
444 static void gs_buf_clear(struct gs_buf *gb);
445 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
446 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
447 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
449 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
455 static struct gs_dev *gs_device;
457 static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
459 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
460 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
462 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
464 static unsigned char gs_tmp_buf[GS_TMP_BUF_SIZE];
465 static struct semaphore gs_tmp_buf_sem;
467 /* tty driver struct */
468 static struct tty_operations gs_tty_ops = {
472 .put_char = gs_put_char,
473 .flush_chars = gs_flush_chars,
474 .write_room = gs_write_room,
476 .set_termios = gs_set_termios,
477 .throttle = gs_throttle,
478 .unthrottle = gs_unthrottle,
479 .break_ctl = gs_break,
480 .chars_in_buffer = gs_chars_in_buffer,
482 static struct tty_driver *gs_tty_driver;
484 /* gadget driver struct */
485 static struct usb_gadget_driver gs_gadget_driver = {
487 .speed = USB_SPEED_HIGH,
489 .speed = USB_SPEED_FULL,
491 .function = GS_LONG_NAME,
495 .disconnect = gs_disconnect,
497 .name = GS_SHORT_NAME,
498 /* .shutdown = ... */
505 /* USB descriptors */
507 #define GS_MANUFACTURER_STR_ID 1
508 #define GS_PRODUCT_STR_ID 2
509 #define GS_SERIAL_STR_ID 3
510 #define GS_CONFIG_STR_ID 4
512 /* static strings, in iso 8859/1 */
513 static struct usb_string gs_strings[] = {
514 { GS_MANUFACTURER_STR_ID, UTS_SYSNAME " " UTS_RELEASE " with " CHIP },
515 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
516 { GS_SERIAL_STR_ID, "0" },
517 { GS_CONFIG_STR_ID, "Bulk" },
518 { } /* end of list */
521 static struct usb_gadget_strings gs_string_table = {
522 .language = 0x0409, /* en-us */
523 .strings = gs_strings,
526 static const struct usb_device_descriptor gs_device_desc = {
527 .bLength = USB_DT_DEVICE_SIZE,
528 .bDescriptorType = USB_DT_DEVICE,
529 .bcdUSB = __constant_cpu_to_le16(0x0200),
530 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
531 .bMaxPacketSize0 = EP0_MAXPACKET,
532 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
533 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
534 .bcdDevice = __constant_cpu_to_le16(GS_VERSION_NUM),
535 .iManufacturer = GS_MANUFACTURER_STR_ID,
536 .iProduct = GS_PRODUCT_STR_ID,
537 .iSerialNumber = GS_SERIAL_STR_ID,
538 .bNumConfigurations = GS_NUM_CONFIGS,
541 static const struct usb_config_descriptor gs_config_desc = {
542 .bLength = USB_DT_CONFIG_SIZE,
543 .bDescriptorType = USB_DT_CONFIG,
544 /* .wTotalLength set by gs_build_config_desc */
545 .bNumInterfaces = GS_NUM_INTERFACES,
546 .bConfigurationValue = GS_BULK_CONFIG_ID,
547 .iConfiguration = GS_CONFIG_STR_ID,
548 .bmAttributes = USB_CONFIG_ATT_ONE | SELFPOWER | WAKEUP,
549 .bMaxPower = (MAX_USB_POWER + 1) / 2,
552 static const struct usb_interface_descriptor gs_interface_desc = {
553 .bLength = USB_DT_INTERFACE_SIZE,
554 .bDescriptorType = USB_DT_INTERFACE,
555 .bNumEndpoints = GS_NUM_ENDPOINTS,
556 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
557 .iInterface = GS_CONFIG_STR_ID,
560 static const struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
561 .bLength = USB_DT_ENDPOINT_SIZE,
562 .bDescriptorType = USB_DT_ENDPOINT,
563 .bEndpointAddress = EP_IN_NUM | USB_DIR_IN,
564 .bmAttributes = USB_ENDPOINT_XFER_BULK,
565 .wMaxPacketSize = __constant_cpu_to_le16(64),
568 static const struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
569 .bLength = USB_DT_ENDPOINT_SIZE,
570 .bDescriptorType = USB_DT_ENDPOINT,
571 .bEndpointAddress = EP_OUT_NUM | USB_DIR_OUT,
572 .bmAttributes = USB_ENDPOINT_XFER_BULK,
573 .wMaxPacketSize = __constant_cpu_to_le16(64),
576 static const struct usb_endpoint_descriptor gs_highspeed_in_desc = {
577 .bLength = USB_DT_ENDPOINT_SIZE,
578 .bDescriptorType = USB_DT_ENDPOINT,
579 .bEndpointAddress = EP_IN_NUM | USB_DIR_IN,
580 .bmAttributes = USB_ENDPOINT_XFER_BULK,
581 .wMaxPacketSize = __constant_cpu_to_le16(512),
584 static const struct usb_endpoint_descriptor gs_highspeed_out_desc = {
585 .bLength = USB_DT_ENDPOINT_SIZE,
586 .bDescriptorType = USB_DT_ENDPOINT,
587 .bEndpointAddress = EP_OUT_NUM | USB_DIR_OUT,
588 .bmAttributes = USB_ENDPOINT_XFER_BULK,
589 .wMaxPacketSize = __constant_cpu_to_le16(512),
593 static const struct usb_qualifier_descriptor gs_qualifier_desc = {
594 .bLength = sizeof(struct usb_qualifier_descriptor),
595 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
596 .bcdUSB = __constant_cpu_to_le16 (0x0200),
597 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
598 /* assumes ep0 uses the same value for both speeds ... */
599 .bMaxPacketSize0 = EP0_MAXPACKET,
600 .bNumConfigurations = GS_NUM_CONFIGS,
606 MODULE_DESCRIPTION(GS_LONG_NAME);
607 MODULE_AUTHOR("Al Borchers");
608 MODULE_LICENSE("GPL");
611 MODULE_PARM(debug, "i");
612 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
615 MODULE_PARM(read_q_size, "i");
616 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
618 MODULE_PARM(write_q_size, "i");
619 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
621 MODULE_PARM(write_buf_size, "i");
622 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
624 module_init(gs_module_init);
625 module_exit(gs_module_exit);
630 * Register as a USB gadget driver and a tty driver.
632 static int __init gs_module_init(void)
637 retval = usb_gadget_register_driver(&gs_gadget_driver);
639 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
643 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
646 gs_tty_driver->owner = THIS_MODULE;
647 gs_tty_driver->driver_name = GS_SHORT_NAME;
648 gs_tty_driver->name = "ttygs";
649 gs_tty_driver->devfs_name = "usb/ttygs/";
650 gs_tty_driver->major = GS_MAJOR;
651 gs_tty_driver->minor_start = GS_MINOR_START;
652 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
653 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
654 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
655 gs_tty_driver->init_termios = tty_std_termios;
656 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
657 tty_set_operations(gs_tty_driver, &gs_tty_ops);
659 for (i=0; i < GS_NUM_PORTS; i++)
660 sema_init(&gs_open_close_sem[i], 1);
662 sema_init(&gs_tmp_buf_sem, 1);
664 retval = tty_register_driver(gs_tty_driver);
666 usb_gadget_unregister_driver(&gs_gadget_driver);
667 put_tty_driver(gs_tty_driver);
668 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
672 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
679 * Unregister as a tty driver and a USB gadget driver.
681 static void __exit gs_module_exit(void)
683 tty_unregister_driver(gs_tty_driver);
684 put_tty_driver(gs_tty_driver);
685 usb_gadget_unregister_driver(&gs_gadget_driver);
687 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
695 static int gs_open(struct tty_struct *tty, struct file *file)
699 struct gs_port *port;
702 struct semaphore *sem;
704 port_num = tty->index;
706 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
708 tty->driver_data = NULL;
710 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
711 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
712 port_num, tty, file);
719 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
720 port_num, tty, file);
724 sem = &gs_open_close_sem[port_num];
725 if (down_interruptible(sem)) {
727 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
728 port_num, tty, file);
732 spin_lock_irqsave(&dev->dev_lock, flags);
734 if (dev->dev_config == GS_NO_CONFIG_ID) {
736 "gs_open: (%d,%p,%p) device is not connected\n",
737 port_num, tty, file);
738 spin_unlock_irqrestore(&dev->dev_lock, flags);
743 port = dev->dev_port[port_num];
746 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
747 port_num, tty, file);
748 spin_unlock_irqrestore(&dev->dev_lock, flags);
753 spin_lock(&port->port_lock);
754 spin_unlock(&dev->dev_lock);
756 if (port->port_dev == NULL) {
757 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
758 port_num, tty, file);
759 spin_unlock_irqrestore(&port->port_lock, flags);
764 if (port->port_open_count > 0) {
765 ++port->port_open_count;
766 spin_unlock_irqrestore(&port->port_lock, flags);
767 gs_debug("gs_open: (%d,%p,%p) already open\n",
768 port_num, tty, file);
773 /* mark port as in use, we can drop port lock and sleep if necessary */
774 port->port_in_use = 1;
776 /* allocate write buffer on first open */
777 if (port->port_write_buf == NULL) {
778 spin_unlock_irqrestore(&port->port_lock, flags);
779 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
780 spin_lock_irqsave(&port->port_lock, flags);
782 /* might have been disconnected while asleep, check */
783 if (port->port_dev == NULL) {
785 "gs_open: (%d,%p,%p) port disconnected (2)\n",
786 port_num, tty, file);
787 port->port_in_use = 0;
788 spin_unlock_irqrestore(&port->port_lock, flags);
793 if ((port->port_write_buf=buf) == NULL) {
794 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
795 port_num, tty, file);
796 port->port_in_use = 0;
797 spin_unlock_irqrestore(&port->port_lock, flags);
804 /* wait for carrier detect (not implemented) */
806 /* might have been disconnected while asleep, check */
807 if (port->port_dev == NULL) {
808 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
809 port_num, tty, file);
810 port->port_in_use = 0;
811 spin_unlock_irqrestore(&port->port_lock, flags);
816 tty->driver_data = port;
817 port->port_tty = tty;
818 port->port_open_count = 1;
819 port->port_in_use = 0;
821 spin_unlock_irqrestore(&port->port_lock, flags);
824 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
832 static void gs_close(struct tty_struct *tty, struct file *file)
835 struct gs_port *port = tty->driver_data;
836 struct semaphore *sem;
839 printk(KERN_ERR "gs_close: NULL port pointer\n");
843 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
845 sem = &gs_open_close_sem[port->port_num];
848 spin_lock_irqsave(&port->port_lock, flags);
850 if (port->port_open_count == 0) {
852 "gs_close: (%d,%p,%p) port is already closed\n",
853 port->port_num, tty, file);
854 spin_unlock_irqrestore(&port->port_lock, flags);
859 if (port->port_open_count > 0) {
860 --port->port_open_count;
861 spin_unlock_irqrestore(&port->port_lock, flags);
866 /* free disconnected port on final close */
867 if (port->port_dev == NULL) {
869 spin_unlock_irqrestore(&port->port_lock, flags);
874 /* mark port as closed but in use, we can drop port lock */
875 /* and sleep if necessary */
876 port->port_in_use = 1;
877 port->port_open_count = 0;
879 /* wait for write buffer to drain, or */
880 /* at most GS_CLOSE_TIMEOUT seconds */
881 if (gs_buf_data_avail(port->port_write_buf) > 0) {
882 wait_cond_interruptible_timeout(port->port_write_wait,
883 port->port_dev == NULL
884 || gs_buf_data_avail(port->port_write_buf) == 0,
885 &port->port_lock, flags, GS_CLOSE_TIMEOUT * HZ);
888 /* free disconnected port on final close */
889 /* (might have happened during the above sleep) */
890 if (port->port_dev == NULL) {
892 spin_unlock_irqrestore(&port->port_lock, flags);
897 gs_buf_clear(port->port_write_buf);
899 tty->driver_data = NULL;
900 port->port_tty = NULL;
901 port->port_in_use = 0;
903 spin_unlock_irqrestore(&port->port_lock, flags);
906 gs_debug("gs_close: (%d,%p,%p) completed\n",
907 port->port_num, tty, file);
913 static int gs_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
916 struct gs_port *port = tty->driver_data;
919 printk(KERN_ERR "gs_write: NULL port pointer\n");
923 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
929 /* copy from user into tmp buffer, get tmp_buf semaphore */
931 if (count > GS_TMP_BUF_SIZE)
932 count = GS_TMP_BUF_SIZE;
933 down(&gs_tmp_buf_sem);
934 if (copy_from_user(gs_tmp_buf, buf, count) != 0) {
937 "gs_write: (%d,%p) cannot copy from user space\n",
938 port->port_num, tty);
944 spin_lock_irqsave(&port->port_lock, flags);
946 if (port->port_dev == NULL) {
947 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
948 port->port_num, tty);
949 spin_unlock_irqrestore(&port->port_lock, flags);
955 if (port->port_open_count == 0) {
956 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
957 port->port_num, tty);
958 spin_unlock_irqrestore(&port->port_lock, flags);
964 count = gs_buf_put(port->port_write_buf, buf, count);
966 spin_unlock_irqrestore(&port->port_lock, flags);
973 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
982 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
985 struct gs_port *port = tty->driver_data;
988 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
992 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));
994 spin_lock_irqsave(&port->port_lock, flags);
996 if (port->port_dev == NULL) {
997 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
998 port->port_num, tty);
999 spin_unlock_irqrestore(&port->port_lock, flags);
1003 if (port->port_open_count == 0) {
1004 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
1005 port->port_num, tty);
1006 spin_unlock_irqrestore(&port->port_lock, flags);
1010 gs_buf_put(port->port_write_buf, &ch, 1);
1012 spin_unlock_irqrestore(&port->port_lock, flags);
1018 static void gs_flush_chars(struct tty_struct *tty)
1020 unsigned long flags;
1021 struct gs_port *port = tty->driver_data;
1024 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
1028 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
1030 spin_lock_irqsave(&port->port_lock, flags);
1032 if (port->port_dev == NULL) {
1034 "gs_flush_chars: (%d,%p) port is not connected\n",
1035 port->port_num, tty);
1036 spin_unlock_irqrestore(&port->port_lock, flags);
1040 if (port->port_open_count == 0) {
1041 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
1042 port->port_num, tty);
1043 spin_unlock_irqrestore(&port->port_lock, flags);
1047 spin_unlock_irqrestore(&port->port_lock, flags);
1055 static int gs_write_room(struct tty_struct *tty)
1059 unsigned long flags;
1060 struct gs_port *port = tty->driver_data;
1066 spin_lock_irqsave(&port->port_lock, flags);
1068 if (port->port_dev != NULL && port->port_open_count > 0
1069 && port->port_write_buf != NULL)
1070 room = gs_buf_space_avail(port->port_write_buf);
1072 spin_unlock_irqrestore(&port->port_lock, flags);
1074 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1075 port->port_num, tty, room);
1081 * gs_chars_in_buffer
1083 static int gs_chars_in_buffer(struct tty_struct *tty)
1086 unsigned long flags;
1087 struct gs_port *port = tty->driver_data;
1092 spin_lock_irqsave(&port->port_lock, flags);
1094 if (port->port_dev != NULL && port->port_open_count > 0
1095 && port->port_write_buf != NULL)
1096 chars = gs_buf_data_avail(port->port_write_buf);
1098 spin_unlock_irqrestore(&port->port_lock, flags);
1100 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1101 port->port_num, tty, chars);
1109 static void gs_throttle(struct tty_struct *tty)
1116 static void gs_unthrottle(struct tty_struct *tty)
1123 static void gs_break(struct tty_struct *tty, int break_state)
1130 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1132 struct gs_port *port = tty->driver_data;
1135 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1139 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1140 port->port_num, tty, file, cmd, arg);
1144 /* could not handle ioctl */
1145 return -ENOIOCTLCMD;
1151 static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1158 * This function finds available write requests, calls
1159 * gs_send_packet to fill these packets with data, and
1160 * continues until either there are no more write requests
1161 * available or no more data to send. This function is
1162 * run whenever data arrives or write requests are available.
1164 static int gs_send(struct gs_dev *dev)
1167 unsigned long flags;
1169 struct usb_request *req;
1170 struct gs_req_entry *req_entry;
1173 printk(KERN_ERR "gs_send: NULL device pointer\n");
1177 spin_lock_irqsave(&dev->dev_lock, flags);
1179 ep = dev->dev_in_ep;
1181 while(!list_empty(&dev->dev_req_list)) {
1183 req_entry = list_entry(dev->dev_req_list.next,
1184 struct gs_req_entry, re_entry);
1186 req = req_entry->re_req;
1188 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1191 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));
1192 list_del(&req_entry->re_entry);
1194 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1196 "gs_send: cannot queue read request, ret=%d\n",
1206 spin_unlock_irqrestore(&dev->dev_lock, flags);
1214 * If there is data to send, a packet is built in the given
1215 * buffer and the size is returned. If there is no data to
1216 * send, 0 is returned. If there is any error a negative
1217 * error number is returned.
1219 * Called during USB completion routine, on interrupt time.
1221 * We assume that disconnect will not happen until all completion
1222 * routines have completed, so we can assume that the dev_port
1223 * array does not change during the lifetime of this function.
1225 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1228 struct gs_port *port;
1230 /* TEMPORARY -- only port 0 is supported right now */
1231 port = dev->dev_port[0];
1235 "gs_send_packet: port=%d, NULL port pointer\n",
1240 spin_lock(&port->port_lock);
1242 len = gs_buf_data_avail(port->port_write_buf);
1247 spin_unlock(&port->port_lock);
1251 size = gs_buf_get(port->port_write_buf, packet, size);
1253 wake_up_interruptible(&port->port_tty->write_wait);
1255 spin_unlock(&port->port_lock);
1263 * Called for each USB packet received. Reads the packet
1264 * header and stuffs the data in the appropriate tty buffer.
1265 * Returns 0 if successful, or a negative error number.
1267 * Called during USB completion routine, on interrupt time.
1269 * We assume that disconnect will not happen until all completion
1270 * routines have completed, so we can assume that the dev_port
1271 * array does not change during the lifetime of this function.
1273 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1276 struct gs_port *port;
1278 /* TEMPORARY -- only port 0 is supported right now */
1279 port = dev->dev_port[0];
1282 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1287 spin_lock(&port->port_lock);
1289 if (port->port_tty == NULL) {
1290 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1292 spin_unlock(&port->port_lock);
1296 if (port->port_tty->magic != TTY_MAGIC) {
1297 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1299 spin_unlock(&port->port_lock);
1303 len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);
1308 memcpy(port->port_tty->flip.char_buf_ptr, packet, size);
1309 port->port_tty->flip.char_buf_ptr += size;
1310 port->port_tty->flip.count += size;
1311 tty_flip_buffer_push(port->port_tty);
1312 wake_up_interruptible(&port->port_tty->read_wait);
1315 spin_unlock(&port->port_lock);
1323 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1326 struct gs_dev *dev = ep->driver_data;
1329 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1333 switch(req->status) {
1335 /* normal completion */
1336 gs_recv_packet(dev, req->buf, req->actual);
1338 req->length = ep->maxpacket;
1339 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1341 "gs_read_complete: cannot queue read request, ret=%d\n",
1348 gs_debug("gs_read_complete: shutdown\n");
1349 gs_free_req(ep, req);
1355 "gs_read_complete: unexpected status error, status=%d\n",
1365 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1367 struct gs_dev *dev = ep->driver_data;
1368 struct gs_req_entry *gs_req = req->context;
1371 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1375 switch(req->status) {
1377 /* normal completion */
1379 if (gs_req == NULL) {
1381 "gs_write_complete: NULL request pointer\n");
1385 spin_lock(&dev->dev_lock);
1386 list_add(&gs_req->re_entry, &dev->dev_req_list);
1387 spin_unlock(&dev->dev_lock);
1395 gs_debug("gs_write_complete: shutdown\n");
1396 gs_free_req(ep, req);
1401 "gs_write_complete: unexpected status error, status=%d\n",
1413 * Called on module load. Allocates and initializes the device
1414 * structure and a control request.
1416 static int gs_bind(struct usb_gadget *gadget)
1421 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1425 set_gadget_data(gadget, dev);
1427 memset(dev, 0, sizeof(struct gs_dev));
1428 dev->dev_gadget = gadget;
1429 spin_lock_init(&dev->dev_lock);
1430 INIT_LIST_HEAD(&dev->dev_req_list);
1432 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1433 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1438 /* preallocate control response and buffer */
1439 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1441 if (dev->dev_ctrl_req == NULL) {
1445 dev->dev_ctrl_req->complete = gs_setup_complete;
1447 gadget->ep0->driver_data = dev;
1449 printk(KERN_INFO "gs_bind: %s %s bound\n",
1450 GS_LONG_NAME, GS_VERSION_STR);
1458 * Called on module unload. Frees the control request and device
1461 static void gs_unbind(struct usb_gadget *gadget)
1463 struct gs_dev *dev = get_gadget_data(gadget);
1467 /* read/write requests already freed, only control request remains */
1469 if (dev->dev_ctrl_req != NULL)
1470 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1473 set_gadget_data(gadget, NULL);
1476 printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1483 * Implements all the control endpoint functionality that's not
1484 * handled in hardware or the hardware driver.
1486 * Returns the size of the data sent to the host, or a negative
1489 static int gs_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1491 int ret = -EOPNOTSUPP;
1492 unsigned int sv_config;
1493 struct gs_dev *dev = get_gadget_data(gadget);
1494 struct usb_request *req = dev->dev_ctrl_req;
1496 switch (ctrl->bRequest) {
1497 case USB_REQ_GET_DESCRIPTOR:
1498 if (ctrl->bRequestType != USB_DIR_IN)
1501 switch (ctrl->wValue >> 8) {
1503 ret = min(ctrl->wLength,
1504 (u16)sizeof(struct usb_device_descriptor));
1505 memcpy(req->buf, &gs_device_desc, ret);
1509 case USB_DT_DEVICE_QUALIFIER:
1510 ret = min(ctrl->wLength,
1511 (u16)sizeof(struct usb_qualifier_descriptor));
1512 memcpy(req->buf, &gs_qualifier_desc, ret);
1515 case USB_DT_OTHER_SPEED_CONFIG:
1516 #endif /* HIGHSPEED */
1518 ret = gs_build_config_desc(req->buf, gadget->speed,
1519 ctrl->wValue >> 8, ctrl->wValue & 0xff);
1521 ret = min(ctrl->wLength, (u16)ret);
1525 /* wIndex == language code. */
1526 ret = usb_gadget_get_string(&gs_string_table,
1527 ctrl->wValue & 0xff, req->buf);
1529 ret = min(ctrl->wLength, (u16)ret);
1534 case USB_REQ_SET_CONFIGURATION:
1535 if (ctrl->bRequestType != 0)
1537 spin_lock(&dev->dev_lock);
1538 ret = gs_set_config(dev, ctrl->wValue);
1539 spin_unlock(&dev->dev_lock);
1542 case USB_REQ_GET_CONFIGURATION:
1543 if (ctrl->bRequestType != USB_DIR_IN)
1545 *(u8 *)req->buf = dev->dev_config;
1546 ret = min(ctrl->wLength, (u16)1);
1549 case USB_REQ_SET_INTERFACE:
1550 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1552 spin_lock(&dev->dev_lock);
1553 if (dev->dev_config == GS_BULK_CONFIG_ID
1554 && ctrl->wIndex == GS_INTERFACE_ID
1555 && ctrl->wValue == GS_ALT_INTERFACE_ID) {
1556 sv_config = dev->dev_config;
1557 /* since there is only one interface, setting the */
1558 /* interface is equivalent to setting the config */
1559 gs_reset_config(dev);
1560 gs_set_config(dev, sv_config);
1563 spin_unlock(&dev->dev_lock);
1566 case USB_REQ_GET_INTERFACE:
1567 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1569 if (dev->dev_config == GS_NO_CONFIG_ID)
1571 if (ctrl->wIndex != GS_INTERFACE_ID) {
1575 *(u8 *)req->buf = GS_ALT_INTERFACE_ID;
1576 ret = min(ctrl->wLength, (u16)1);
1580 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1581 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1582 ctrl->wIndex, ctrl->wLength);
1587 /* respond with data transfer before status phase? */
1590 req->zero = ret < ctrl->wLength
1591 && (ret % gadget->ep0->maxpacket) == 0;
1592 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1595 "gs_setup: cannot queue response, ret=%d\n",
1598 gs_setup_complete(gadget->ep0, req);
1602 /* device either stalls (ret < 0) or reports success */
1609 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1611 if (req->status || req->actual != req->length) {
1612 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1613 req->status, req->actual, req->length);
1620 * Called when the device is disconnected. Frees the closed
1621 * ports and disconnects open ports. Open ports will be freed
1622 * on close. Then reallocates the ports for the next connection.
1624 static void gs_disconnect(struct usb_gadget *gadget)
1626 unsigned long flags;
1627 struct gs_dev *dev = get_gadget_data(gadget);
1629 spin_lock_irqsave(&dev->dev_lock, flags);
1631 gs_reset_config(dev);
1633 /* free closed ports and disconnect open ports */
1634 /* (open ports will be freed when closed) */
1637 /* re-allocate ports for the next connection */
1638 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1639 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1641 spin_unlock_irqrestore(&dev->dev_lock, flags);
1643 printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1649 * Configures the device by enabling device specific
1650 * optimizations, setting up the endpoints, allocating
1651 * read and write requests and queuing read requests.
1653 * The device lock must be held when calling this function.
1655 static int gs_set_config(struct gs_dev *dev, unsigned config)
1659 struct usb_gadget *gadget = dev->dev_gadget;
1661 struct usb_request *req;
1662 struct gs_req_entry *req_entry;
1665 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1669 if (config == dev->dev_config)
1672 gs_reset_config(dev);
1674 if (config == GS_NO_CONFIG_ID)
1677 if (config != GS_BULK_CONFIG_ID)
1680 hw_optimize(gadget);
1682 gadget_for_each_ep(ep, gadget) {
1684 if (strcmp(ep->name, EP_IN_NAME) == 0) {
1685 ret = usb_ep_enable(ep,
1686 gadget->speed == USB_SPEED_HIGH ?
1687 &gs_highspeed_in_desc : &gs_fullspeed_in_desc);
1689 ep->driver_data = dev;
1690 dev->dev_in_ep = ep;
1692 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
1694 gs_reset_config(dev);
1699 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1700 ret = usb_ep_enable(ep,
1701 gadget->speed == USB_SPEED_HIGH ?
1702 &gs_highspeed_out_desc :
1703 &gs_fullspeed_out_desc);
1705 ep->driver_data = dev;
1706 dev->dev_out_ep = ep;
1708 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
1710 gs_reset_config(dev);
1717 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL) {
1718 gs_reset_config(dev);
1719 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
1723 /* allocate and queue read requests */
1724 ep = dev->dev_out_ep;
1725 for (i=0; i<read_q_size && ret == 0; i++) {
1726 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1727 req->complete = gs_read_complete;
1728 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1729 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
1733 gs_reset_config(dev);
1735 "gs_set_config: cannot allocate read requests\n");
1740 /* allocate write requests, and put on free list */
1741 ep = dev->dev_in_ep;
1742 for (i=0; i<write_q_size; i++) {
1743 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1744 req_entry->re_req->complete = gs_write_complete;
1745 list_add(&req_entry->re_entry, &dev->dev_req_list);
1747 gs_reset_config(dev);
1749 "gs_set_config: cannot allocate write requests\n");
1754 dev->dev_config = config;
1756 printk(KERN_INFO "gs_set_config: %s configured for %s speed\n",
1758 gadget->speed == USB_SPEED_HIGH ? "high" : "full");
1766 * Mark the device as not configured, disable all endpoints,
1767 * which forces completion of pending I/O and frees queued
1768 * requests, and free the remaining write requests on the
1771 * The device lock must be held when calling this function.
1773 static void gs_reset_config(struct gs_dev *dev)
1775 struct gs_req_entry *req_entry;
1778 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
1782 if (dev->dev_config == GS_NO_CONFIG_ID)
1785 dev->dev_config = GS_NO_CONFIG_ID;
1787 /* free write requests on the free list */
1788 while(!list_empty(&dev->dev_req_list)) {
1789 req_entry = list_entry(dev->dev_req_list.next,
1790 struct gs_req_entry, re_entry);
1791 list_del(&req_entry->re_entry);
1792 gs_free_req_entry(dev->dev_in_ep, req_entry);
1795 /* disable endpoints, forcing completion of pending i/o; */
1796 /* completion handlers free their requests in this case */
1797 if (dev->dev_in_ep) {
1798 usb_ep_disable(dev->dev_in_ep);
1799 dev->dev_in_ep = NULL;
1801 if (dev->dev_out_ep) {
1802 usb_ep_disable(dev->dev_out_ep);
1803 dev->dev_out_ep = NULL;
1808 * gs_build_config_desc
1810 * Builds a config descriptor in the given buffer and returns the
1811 * length, or a negative error number.
1813 static int gs_build_config_desc(u8 *buf, enum usb_device_speed speed, u8 type, unsigned int index)
1816 int len = USB_DT_CONFIG_SIZE + USB_DT_INTERFACE_SIZE
1817 + GS_NUM_ENDPOINTS * USB_DT_ENDPOINT_SIZE;
1819 /* only one config */
1823 memcpy(buf, &gs_config_desc, USB_DT_CONFIG_SIZE);
1824 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
1825 ((struct usb_config_descriptor *)buf)->wTotalLength = __constant_cpu_to_le16(len);
1826 buf += USB_DT_CONFIG_SIZE;
1828 memcpy(buf, &gs_interface_desc, USB_DT_INTERFACE_SIZE);
1829 buf += USB_DT_INTERFACE_SIZE;
1831 /* other speed switches high and full speed */
1832 high_speed = (speed == USB_SPEED_HIGH);
1833 if (type == USB_DT_OTHER_SPEED_CONFIG)
1834 high_speed = !high_speed;
1837 high_speed ? &gs_highspeed_in_desc : &gs_fullspeed_in_desc,
1838 USB_DT_ENDPOINT_SIZE);
1839 buf += USB_DT_ENDPOINT_SIZE;
1841 high_speed ? &gs_highspeed_out_desc : &gs_fullspeed_out_desc,
1842 USB_DT_ENDPOINT_SIZE);
1850 * Allocate a usb_request and its buffer. Returns a pointer to the
1851 * usb_request or NULL if there is an error.
1853 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, int kmalloc_flags)
1855 struct usb_request *req;
1860 req = usb_ep_alloc_request(ep, kmalloc_flags);
1864 req->buf = usb_ep_alloc_buffer(ep, len, &req->dma,
1866 if (req->buf == NULL) {
1867 usb_ep_free_request(ep, req);
1878 * Free a usb_request and its buffer.
1880 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
1882 if (ep != NULL && req != NULL) {
1883 if (req->buf != NULL)
1884 usb_ep_free_buffer(ep, req->buf, req->dma,
1886 usb_ep_free_request(ep, req);
1891 * gs_alloc_req_entry
1893 * Allocates a request and its buffer, using the given
1894 * endpoint, buffer len, and kmalloc flags.
1896 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len, int kmalloc_flags)
1898 struct gs_req_entry *req;
1900 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
1904 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
1905 if (req->re_req == NULL) {
1910 req->re_req->context = req;
1918 * Frees a request and its buffer.
1920 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
1922 if (ep != NULL && req != NULL) {
1923 if (req->re_req != NULL)
1924 gs_free_req(ep, req->re_req);
1932 * Allocate all ports and set the gs_dev struct to point to them.
1933 * Return 0 if successful, or a negative error number.
1935 * The device lock is normally held when calling this function.
1937 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags)
1940 struct gs_port *port;
1945 for (i=0; i<GS_NUM_PORTS; i++) {
1946 if ((port=(struct gs_port *)kmalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
1949 memset(port, 0, sizeof(struct gs_port));
1950 port->port_dev = dev;
1952 spin_lock_init(&port->port_lock);
1953 init_waitqueue_head(&port->port_write_wait);
1955 dev->dev_port[i] = port;
1964 * Free all closed ports. Open ports are disconnected by
1965 * freeing their write buffers, setting their device pointers
1966 * and the pointers to them in the device to NULL. These
1967 * ports will be freed when closed.
1969 * The device lock is normally held when calling this function.
1971 static void gs_free_ports(struct gs_dev *dev)
1974 unsigned long flags;
1975 struct gs_port *port;
1980 for (i=0; i<GS_NUM_PORTS; i++) {
1981 if ((port=dev->dev_port[i]) != NULL) {
1982 dev->dev_port[i] = NULL;
1984 spin_lock_irqsave(&port->port_lock, flags);
1986 if (port->port_write_buf != NULL) {
1987 gs_buf_free(port->port_write_buf);
1988 port->port_write_buf = NULL;
1991 if (port->port_open_count > 0 || port->port_in_use) {
1992 port->port_dev = NULL;
1993 wake_up_interruptible(&port->port_write_wait);
1994 wake_up_interruptible(&port->port_tty->read_wait);
1995 wake_up_interruptible(&port->port_tty->write_wait);
2000 spin_unlock_irqrestore(&port->port_lock, flags);
2005 /* Circular Buffer */
2010 * Allocate a circular buffer and all associated memory.
2012 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags)
2019 gb = (struct gs_buf *)kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2023 gb->buf_buf = kmalloc(size, kmalloc_flags);
2024 if (gb->buf_buf == NULL) {
2029 gb->buf_size = size;
2030 gb->buf_get = gb->buf_put = gb->buf_buf;
2038 * Free the buffer and all associated memory.
2040 void gs_buf_free(struct gs_buf *gb)
2043 if (gb->buf_buf != NULL)
2052 * Clear out all data in the circular buffer.
2054 void gs_buf_clear(struct gs_buf *gb)
2057 gb->buf_get = gb->buf_put;
2058 /* equivalent to a get of all data available */
2064 * Return the number of bytes of data available in the circular
2067 unsigned int gs_buf_data_avail(struct gs_buf *gb)
2070 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2076 * gs_buf_space_avail
2078 * Return the number of bytes of space available in the circular
2081 unsigned int gs_buf_space_avail(struct gs_buf *gb)
2084 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2092 * Copy data data from a user buffer and put it into the circular buffer.
2093 * Restrict to the amount of space available.
2095 * Return the number of bytes copied.
2097 unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2104 len = gs_buf_space_avail(gb);
2111 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2113 memcpy(gb->buf_put, buf, len);
2114 memcpy(gb->buf_buf, buf+len, count - len);
2115 gb->buf_put = gb->buf_buf + count - len;
2117 memcpy(gb->buf_put, buf, count);
2119 gb->buf_put += count;
2120 else /* count == len */
2121 gb->buf_put = gb->buf_buf;
2130 * Get data from the circular buffer and copy to the given buffer.
2131 * Restrict to the amount of data available.
2133 * Return the number of bytes copied.
2135 unsigned int gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2142 len = gs_buf_data_avail(gb);
2149 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2151 memcpy(buf, gb->buf_get, len);
2152 memcpy(buf+len, gb->buf_buf, count - len);
2153 gb->buf_get = gb->buf_buf + count - len;
2155 memcpy(buf, gb->buf_get, count);
2157 gb->buf_get += count;
2158 else /* count == len */
2159 gb->buf_get = gb->buf_buf;