usb serial gadget: CDC ACM fixes
[linux-2.6/mini2440.git] / drivers / usb / gadget / serial.c
blob54cdd6f940346732fe38379ad75cd542a66e5709
1 /*
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/kernel.h>
21 #include <linux/utsname.h>
22 #include <linux/device.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/cdc.h>
28 #include <linux/usb/gadget.h>
30 #include "gadget_chips.h"
33 /* Defines */
35 #define GS_VERSION_STR "v2.2"
36 #define GS_VERSION_NUM 0x0202
38 #define GS_LONG_NAME "Gadget Serial"
39 #define GS_SHORT_NAME "g_serial"
41 #define GS_MAJOR 127
42 #define GS_MINOR_START 0
44 #define GS_NUM_PORTS 16
46 #define GS_NUM_CONFIGS 1
47 #define GS_NO_CONFIG_ID 0
48 #define GS_BULK_CONFIG_ID 1
49 #define GS_ACM_CONFIG_ID 2
51 #define GS_MAX_NUM_INTERFACES 2
52 #define GS_BULK_INTERFACE_ID 0
53 #define GS_CONTROL_INTERFACE_ID 0
54 #define GS_DATA_INTERFACE_ID 1
56 #define GS_MAX_DESC_LEN 256
58 #define GS_DEFAULT_READ_Q_SIZE 32
59 #define GS_DEFAULT_WRITE_Q_SIZE 32
61 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
62 #define GS_TMP_BUF_SIZE 8192
64 #define GS_CLOSE_TIMEOUT 15
66 #define GS_DEFAULT_USE_ACM 0
68 #define GS_DEFAULT_DTE_RATE 9600
69 #define GS_DEFAULT_DATA_BITS 8
70 #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
71 #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
73 /* maxpacket and other transfer characteristics vary by speed. */
74 static inline struct usb_endpoint_descriptor *
75 choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
76 struct usb_endpoint_descriptor *fs)
78 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
79 return hs;
80 return fs;
84 /* debug settings */
85 #ifdef DEBUG
86 static int debug = 1;
87 #else
88 #define debug 0
89 #endif
91 #define gs_debug(format, arg...) \
92 do { if (debug) pr_debug(format, ## arg); } while (0)
93 #define gs_debug_level(level, format, arg...) \
94 do { if (debug >= level) pr_debug(format, ## arg); } while (0)
97 /* Thanks to NetChip Technologies for donating this product ID.
99 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
100 * Instead: allocate your own, using normal USB-IF procedures.
102 #define GS_VENDOR_ID 0x0525 /* NetChip */
103 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
104 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
106 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
107 #define GS_NOTIFY_MAXPACKET 8
110 /* Structures */
112 struct gs_dev;
114 /* circular buffer */
115 struct gs_buf {
116 unsigned int buf_size;
117 char *buf_buf;
118 char *buf_get;
119 char *buf_put;
122 /* list of requests */
123 struct gs_req_entry {
124 struct list_head re_entry;
125 struct usb_request *re_req;
128 /* the port structure holds info for each port, one for each minor number */
129 struct gs_port {
130 struct gs_dev *port_dev; /* pointer to device struct */
131 struct tty_struct *port_tty; /* pointer to tty struct */
132 spinlock_t port_lock;
133 int port_num;
134 int port_open_count;
135 int port_in_use; /* open/close in progress */
136 wait_queue_head_t port_write_wait;/* waiting to write */
137 struct gs_buf *port_write_buf;
138 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
139 u16 port_handshake_bits;
140 #define RS232_RTS (1 << 1)
141 #define RS232_DTE (1 << 0)
144 /* the device structure holds info for the USB device */
145 struct gs_dev {
146 struct usb_gadget *dev_gadget; /* gadget device pointer */
147 spinlock_t dev_lock; /* lock for set/reset config */
148 int dev_config; /* configuration number */
149 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
150 struct usb_ep *dev_in_ep; /* address of in endpoint */
151 struct usb_ep *dev_out_ep; /* address of out endpoint */
152 struct usb_endpoint_descriptor /* descriptor of notify ep */
153 *dev_notify_ep_desc;
154 struct usb_endpoint_descriptor /* descriptor of in endpoint */
155 *dev_in_ep_desc;
156 struct usb_endpoint_descriptor /* descriptor of out endpoint */
157 *dev_out_ep_desc;
158 struct usb_request *dev_ctrl_req; /* control request */
159 struct list_head dev_req_list; /* list of write requests */
160 int dev_sched_port; /* round robin port scheduled */
161 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
165 /* Functions */
167 /* module */
168 static int __init gs_module_init(void);
169 static void __exit gs_module_exit(void);
171 /* tty driver */
172 static int gs_open(struct tty_struct *tty, struct file *file);
173 static void gs_close(struct tty_struct *tty, struct file *file);
174 static int gs_write(struct tty_struct *tty,
175 const unsigned char *buf, int count);
176 static int gs_put_char(struct tty_struct *tty, unsigned char ch);
177 static void gs_flush_chars(struct tty_struct *tty);
178 static int gs_write_room(struct tty_struct *tty);
179 static int gs_chars_in_buffer(struct tty_struct *tty);
180 static void gs_throttle(struct tty_struct * tty);
181 static void gs_unthrottle(struct tty_struct * tty);
182 static void gs_break(struct tty_struct *tty, int break_state);
183 static int gs_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old);
187 static int gs_send(struct gs_dev *dev);
188 static int gs_send_packet(struct gs_dev *dev, char *packet,
189 unsigned int size);
190 static int gs_recv_packet(struct gs_dev *dev, char *packet,
191 unsigned int size);
192 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
193 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
195 /* gadget driver */
196 static int gs_bind(struct usb_gadget *gadget);
197 static void gs_unbind(struct usb_gadget *gadget);
198 static int gs_setup(struct usb_gadget *gadget,
199 const struct usb_ctrlrequest *ctrl);
200 static int gs_setup_standard(struct usb_gadget *gadget,
201 const struct usb_ctrlrequest *ctrl);
202 static int gs_setup_class(struct usb_gadget *gadget,
203 const struct usb_ctrlrequest *ctrl);
204 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
205 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
206 struct usb_request *req);
207 static void gs_disconnect(struct usb_gadget *gadget);
208 static int gs_set_config(struct gs_dev *dev, unsigned config);
209 static void gs_reset_config(struct gs_dev *dev);
210 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
211 u8 type, unsigned int index, int is_otg);
213 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
214 gfp_t kmalloc_flags);
215 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
217 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
218 gfp_t kmalloc_flags);
219 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
221 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
222 static void gs_free_ports(struct gs_dev *dev);
224 /* circular buffer */
225 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
226 static void gs_buf_free(struct gs_buf *gb);
227 static void gs_buf_clear(struct gs_buf *gb);
228 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
229 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
230 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
231 unsigned int count);
232 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
233 unsigned int count);
235 /* external functions */
236 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
239 /* Globals */
241 static struct gs_dev *gs_device;
243 static const char *EP_IN_NAME;
244 static const char *EP_OUT_NAME;
245 static const char *EP_NOTIFY_NAME;
247 static struct mutex gs_open_close_lock[GS_NUM_PORTS];
249 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
250 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
252 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
254 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
257 /* tty driver struct */
258 static const struct tty_operations gs_tty_ops = {
259 .open = gs_open,
260 .close = gs_close,
261 .write = gs_write,
262 .put_char = gs_put_char,
263 .flush_chars = gs_flush_chars,
264 .write_room = gs_write_room,
265 .ioctl = gs_ioctl,
266 .set_termios = gs_set_termios,
267 .throttle = gs_throttle,
268 .unthrottle = gs_unthrottle,
269 .break_ctl = gs_break,
270 .chars_in_buffer = gs_chars_in_buffer,
272 static struct tty_driver *gs_tty_driver;
274 /* gadget driver struct */
275 static struct usb_gadget_driver gs_gadget_driver = {
276 #ifdef CONFIG_USB_GADGET_DUALSPEED
277 .speed = USB_SPEED_HIGH,
278 #else
279 .speed = USB_SPEED_FULL,
280 #endif /* CONFIG_USB_GADGET_DUALSPEED */
281 .function = GS_LONG_NAME,
282 .bind = gs_bind,
283 .unbind = gs_unbind,
284 .setup = gs_setup,
285 .disconnect = gs_disconnect,
286 .driver = {
287 .name = GS_SHORT_NAME,
292 /* USB descriptors */
294 #define GS_MANUFACTURER_STR_ID 1
295 #define GS_PRODUCT_STR_ID 2
296 #define GS_SERIAL_STR_ID 3
297 #define GS_BULK_CONFIG_STR_ID 4
298 #define GS_ACM_CONFIG_STR_ID 5
299 #define GS_CONTROL_STR_ID 6
300 #define GS_DATA_STR_ID 7
302 /* static strings, in UTF-8 */
303 static char manufacturer[50];
304 static struct usb_string gs_strings[] = {
305 { GS_MANUFACTURER_STR_ID, manufacturer },
306 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
307 { GS_SERIAL_STR_ID, "0" },
308 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
309 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
310 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
311 { GS_DATA_STR_ID, "Gadget Serial Data" },
312 { } /* end of list */
315 static struct usb_gadget_strings gs_string_table = {
316 .language = 0x0409, /* en-us */
317 .strings = gs_strings,
320 static struct usb_device_descriptor gs_device_desc = {
321 .bLength = USB_DT_DEVICE_SIZE,
322 .bDescriptorType = USB_DT_DEVICE,
323 .bcdUSB = __constant_cpu_to_le16(0x0200),
324 .bDeviceSubClass = 0,
325 .bDeviceProtocol = 0,
326 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
327 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
328 .iManufacturer = GS_MANUFACTURER_STR_ID,
329 .iProduct = GS_PRODUCT_STR_ID,
330 .iSerialNumber = GS_SERIAL_STR_ID,
331 .bNumConfigurations = GS_NUM_CONFIGS,
334 static struct usb_otg_descriptor gs_otg_descriptor = {
335 .bLength = sizeof(gs_otg_descriptor),
336 .bDescriptorType = USB_DT_OTG,
337 .bmAttributes = USB_OTG_SRP,
340 static struct usb_config_descriptor gs_bulk_config_desc = {
341 .bLength = USB_DT_CONFIG_SIZE,
342 .bDescriptorType = USB_DT_CONFIG,
343 /* .wTotalLength computed dynamically */
344 .bNumInterfaces = 1,
345 .bConfigurationValue = GS_BULK_CONFIG_ID,
346 .iConfiguration = GS_BULK_CONFIG_STR_ID,
347 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
348 .bMaxPower = 1,
351 static struct usb_config_descriptor gs_acm_config_desc = {
352 .bLength = USB_DT_CONFIG_SIZE,
353 .bDescriptorType = USB_DT_CONFIG,
354 /* .wTotalLength computed dynamically */
355 .bNumInterfaces = 2,
356 .bConfigurationValue = GS_ACM_CONFIG_ID,
357 .iConfiguration = GS_ACM_CONFIG_STR_ID,
358 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
359 .bMaxPower = 1,
362 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
363 .bLength = USB_DT_INTERFACE_SIZE,
364 .bDescriptorType = USB_DT_INTERFACE,
365 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
366 .bNumEndpoints = 2,
367 .bInterfaceClass = USB_CLASS_CDC_DATA,
368 .bInterfaceSubClass = 0,
369 .bInterfaceProtocol = 0,
370 .iInterface = GS_DATA_STR_ID,
373 static const struct usb_interface_descriptor gs_control_interface_desc = {
374 .bLength = USB_DT_INTERFACE_SIZE,
375 .bDescriptorType = USB_DT_INTERFACE,
376 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
377 .bNumEndpoints = 1,
378 .bInterfaceClass = USB_CLASS_COMM,
379 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
380 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
381 .iInterface = GS_CONTROL_STR_ID,
384 static const struct usb_interface_descriptor gs_data_interface_desc = {
385 .bLength = USB_DT_INTERFACE_SIZE,
386 .bDescriptorType = USB_DT_INTERFACE,
387 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
388 .bNumEndpoints = 2,
389 .bInterfaceClass = USB_CLASS_CDC_DATA,
390 .bInterfaceSubClass = 0,
391 .bInterfaceProtocol = 0,
392 .iInterface = GS_DATA_STR_ID,
395 static const struct usb_cdc_header_desc gs_header_desc = {
396 .bLength = sizeof(gs_header_desc),
397 .bDescriptorType = USB_DT_CS_INTERFACE,
398 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
399 .bcdCDC = __constant_cpu_to_le16(0x0110),
402 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
403 .bLength = sizeof(gs_call_mgmt_descriptor),
404 .bDescriptorType = USB_DT_CS_INTERFACE,
405 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
406 .bmCapabilities = 0,
407 .bDataInterface = 1, /* index of data interface */
410 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
411 .bLength = sizeof(gs_acm_descriptor),
412 .bDescriptorType = USB_DT_CS_INTERFACE,
413 .bDescriptorSubType = USB_CDC_ACM_TYPE,
414 .bmCapabilities = (1 << 1),
417 static const struct usb_cdc_union_desc gs_union_desc = {
418 .bLength = sizeof(gs_union_desc),
419 .bDescriptorType = USB_DT_CS_INTERFACE,
420 .bDescriptorSubType = USB_CDC_UNION_TYPE,
421 .bMasterInterface0 = 0, /* index of control interface */
422 .bSlaveInterface0 = 1, /* index of data interface */
425 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
426 .bLength = USB_DT_ENDPOINT_SIZE,
427 .bDescriptorType = USB_DT_ENDPOINT,
428 .bEndpointAddress = USB_DIR_IN,
429 .bmAttributes = USB_ENDPOINT_XFER_INT,
430 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
431 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
434 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
435 .bLength = USB_DT_ENDPOINT_SIZE,
436 .bDescriptorType = USB_DT_ENDPOINT,
437 .bEndpointAddress = USB_DIR_IN,
438 .bmAttributes = USB_ENDPOINT_XFER_BULK,
441 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
442 .bLength = USB_DT_ENDPOINT_SIZE,
443 .bDescriptorType = USB_DT_ENDPOINT,
444 .bEndpointAddress = USB_DIR_OUT,
445 .bmAttributes = USB_ENDPOINT_XFER_BULK,
448 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
449 (struct usb_descriptor_header *) &gs_otg_descriptor,
450 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
451 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
452 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
453 NULL,
456 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
457 (struct usb_descriptor_header *) &gs_otg_descriptor,
458 (struct usb_descriptor_header *) &gs_control_interface_desc,
459 (struct usb_descriptor_header *) &gs_header_desc,
460 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
461 (struct usb_descriptor_header *) &gs_acm_descriptor,
462 (struct usb_descriptor_header *) &gs_union_desc,
463 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
464 (struct usb_descriptor_header *) &gs_data_interface_desc,
465 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
466 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
467 NULL,
470 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
471 .bLength = USB_DT_ENDPOINT_SIZE,
472 .bDescriptorType = USB_DT_ENDPOINT,
473 .bEndpointAddress = USB_DIR_IN,
474 .bmAttributes = USB_ENDPOINT_XFER_INT,
475 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
476 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
479 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
480 .bLength = USB_DT_ENDPOINT_SIZE,
481 .bDescriptorType = USB_DT_ENDPOINT,
482 .bmAttributes = USB_ENDPOINT_XFER_BULK,
483 .wMaxPacketSize = __constant_cpu_to_le16(512),
486 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
487 .bLength = USB_DT_ENDPOINT_SIZE,
488 .bDescriptorType = USB_DT_ENDPOINT,
489 .bmAttributes = USB_ENDPOINT_XFER_BULK,
490 .wMaxPacketSize = __constant_cpu_to_le16(512),
493 static struct usb_qualifier_descriptor gs_qualifier_desc = {
494 .bLength = sizeof(struct usb_qualifier_descriptor),
495 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
496 .bcdUSB = __constant_cpu_to_le16 (0x0200),
497 /* assumes ep0 uses the same value for both speeds ... */
498 .bNumConfigurations = GS_NUM_CONFIGS,
501 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
502 (struct usb_descriptor_header *) &gs_otg_descriptor,
503 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
504 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
505 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
506 NULL,
509 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
510 (struct usb_descriptor_header *) &gs_otg_descriptor,
511 (struct usb_descriptor_header *) &gs_control_interface_desc,
512 (struct usb_descriptor_header *) &gs_header_desc,
513 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
514 (struct usb_descriptor_header *) &gs_acm_descriptor,
515 (struct usb_descriptor_header *) &gs_union_desc,
516 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
517 (struct usb_descriptor_header *) &gs_data_interface_desc,
518 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
519 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
520 NULL,
524 /* Module */
525 MODULE_DESCRIPTION(GS_LONG_NAME);
526 MODULE_AUTHOR("Al Borchers");
527 MODULE_LICENSE("GPL");
529 #ifdef DEBUG
530 module_param(debug, int, S_IRUGO|S_IWUSR);
531 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
532 #endif
534 module_param(read_q_size, uint, S_IRUGO);
535 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
537 module_param(write_q_size, uint, S_IRUGO);
538 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
540 module_param(write_buf_size, uint, S_IRUGO);
541 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
543 module_param(use_acm, uint, S_IRUGO);
544 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
546 module_init(gs_module_init);
547 module_exit(gs_module_exit);
550 * gs_module_init
552 * Register as a USB gadget driver and a tty driver.
554 static int __init gs_module_init(void)
556 int i;
557 int retval;
559 retval = usb_gadget_register_driver(&gs_gadget_driver);
560 if (retval) {
561 pr_err("gs_module_init: cannot register gadget driver, "
562 "ret=%d\n", retval);
563 return retval;
566 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
567 if (!gs_tty_driver)
568 return -ENOMEM;
569 gs_tty_driver->owner = THIS_MODULE;
570 gs_tty_driver->driver_name = GS_SHORT_NAME;
571 gs_tty_driver->name = "ttygs";
572 gs_tty_driver->major = GS_MAJOR;
573 gs_tty_driver->minor_start = GS_MINOR_START;
574 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
575 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
576 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
577 gs_tty_driver->init_termios = tty_std_termios;
578 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
579 tty_set_operations(gs_tty_driver, &gs_tty_ops);
581 for (i=0; i < GS_NUM_PORTS; i++)
582 mutex_init(&gs_open_close_lock[i]);
584 retval = tty_register_driver(gs_tty_driver);
585 if (retval) {
586 usb_gadget_unregister_driver(&gs_gadget_driver);
587 put_tty_driver(gs_tty_driver);
588 pr_err("gs_module_init: cannot register tty driver, "
589 "ret=%d\n", retval);
590 return retval;
593 pr_info("gs_module_init: %s %s loaded\n",
594 GS_LONG_NAME, GS_VERSION_STR);
595 return 0;
599 * gs_module_exit
601 * Unregister as a tty driver and a USB gadget driver.
603 static void __exit gs_module_exit(void)
605 tty_unregister_driver(gs_tty_driver);
606 put_tty_driver(gs_tty_driver);
607 usb_gadget_unregister_driver(&gs_gadget_driver);
609 pr_info("gs_module_exit: %s %s unloaded\n",
610 GS_LONG_NAME, GS_VERSION_STR);
613 /* TTY Driver */
616 * gs_open
618 static int gs_open(struct tty_struct *tty, struct file *file)
620 int port_num;
621 unsigned long flags;
622 struct gs_port *port;
623 struct gs_dev *dev;
624 struct gs_buf *buf;
625 struct mutex *mtx;
626 int ret;
628 port_num = tty->index;
630 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
632 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
633 pr_err("gs_open: (%d,%p,%p) invalid port number\n",
634 port_num, tty, file);
635 return -ENODEV;
638 dev = gs_device;
640 if (dev == NULL) {
641 pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
642 port_num, tty, file);
643 return -ENODEV;
646 mtx = &gs_open_close_lock[port_num];
647 if (mutex_lock_interruptible(mtx)) {
648 pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n",
649 port_num, tty, file);
650 return -ERESTARTSYS;
653 spin_lock_irqsave(&dev->dev_lock, flags);
655 if (dev->dev_config == GS_NO_CONFIG_ID) {
656 pr_err("gs_open: (%d,%p,%p) device is not connected\n",
657 port_num, tty, file);
658 ret = -ENODEV;
659 goto exit_unlock_dev;
662 port = dev->dev_port[port_num];
664 if (port == NULL) {
665 pr_err("gs_open: (%d,%p,%p) NULL port pointer\n",
666 port_num, tty, file);
667 ret = -ENODEV;
668 goto exit_unlock_dev;
671 spin_lock(&port->port_lock);
672 spin_unlock(&dev->dev_lock);
674 if (port->port_dev == NULL) {
675 pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n",
676 port_num, tty, file);
677 ret = -EIO;
678 goto exit_unlock_port;
681 if (port->port_open_count > 0) {
682 ++port->port_open_count;
683 gs_debug("gs_open: (%d,%p,%p) already open\n",
684 port_num, tty, file);
685 ret = 0;
686 goto exit_unlock_port;
689 tty->driver_data = NULL;
691 /* mark port as in use, we can drop port lock and sleep if necessary */
692 port->port_in_use = 1;
694 /* allocate write buffer on first open */
695 if (port->port_write_buf == NULL) {
696 spin_unlock_irqrestore(&port->port_lock, flags);
697 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
698 spin_lock_irqsave(&port->port_lock, flags);
700 /* might have been disconnected while asleep, check */
701 if (port->port_dev == NULL) {
702 pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n",
703 port_num, tty, file);
704 port->port_in_use = 0;
705 ret = -EIO;
706 goto exit_unlock_port;
709 if ((port->port_write_buf=buf) == NULL) {
710 pr_err("gs_open: (%d,%p,%p) cannot allocate "
711 "port write buffer\n",
712 port_num, tty, file);
713 port->port_in_use = 0;
714 ret = -ENOMEM;
715 goto exit_unlock_port;
720 /* wait for carrier detect (not implemented) */
722 /* might have been disconnected while asleep, check */
723 if (port->port_dev == NULL) {
724 pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n",
725 port_num, tty, file);
726 port->port_in_use = 0;
727 ret = -EIO;
728 goto exit_unlock_port;
731 tty->driver_data = port;
732 port->port_tty = tty;
733 port->port_open_count = 1;
734 port->port_in_use = 0;
736 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
738 ret = 0;
740 exit_unlock_port:
741 spin_unlock_irqrestore(&port->port_lock, flags);
742 mutex_unlock(mtx);
743 return ret;
745 exit_unlock_dev:
746 spin_unlock_irqrestore(&dev->dev_lock, flags);
747 mutex_unlock(mtx);
748 return ret;
753 * gs_close
756 #define GS_WRITE_FINISHED_EVENT_SAFELY(p) \
757 ({ \
758 int cond; \
760 spin_lock_irq(&(p)->port_lock); \
761 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \
762 spin_unlock_irq(&(p)->port_lock); \
763 cond; \
766 static void gs_close(struct tty_struct *tty, struct file *file)
768 struct gs_port *port = tty->driver_data;
769 struct mutex *mtx;
771 if (port == NULL) {
772 pr_err("gs_close: NULL port pointer\n");
773 return;
776 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
778 mtx = &gs_open_close_lock[port->port_num];
779 mutex_lock(mtx);
781 spin_lock_irq(&port->port_lock);
783 if (port->port_open_count == 0) {
784 pr_err("gs_close: (%d,%p,%p) port is already closed\n",
785 port->port_num, tty, file);
786 goto exit;
789 if (port->port_open_count > 1) {
790 --port->port_open_count;
791 goto exit;
794 /* free disconnected port on final close */
795 if (port->port_dev == NULL) {
796 kfree(port);
797 goto exit;
800 /* mark port as closed but in use, we can drop port lock */
801 /* and sleep if necessary */
802 port->port_in_use = 1;
803 port->port_open_count = 0;
805 /* wait for write buffer to drain, or */
806 /* at most GS_CLOSE_TIMEOUT seconds */
807 if (gs_buf_data_avail(port->port_write_buf) > 0) {
808 spin_unlock_irq(&port->port_lock);
809 wait_event_interruptible_timeout(port->port_write_wait,
810 GS_WRITE_FINISHED_EVENT_SAFELY(port),
811 GS_CLOSE_TIMEOUT * HZ);
812 spin_lock_irq(&port->port_lock);
815 /* free disconnected port on final close */
816 /* (might have happened during the above sleep) */
817 if (port->port_dev == NULL) {
818 kfree(port);
819 goto exit;
822 gs_buf_clear(port->port_write_buf);
824 tty->driver_data = NULL;
825 port->port_tty = NULL;
826 port->port_in_use = 0;
828 gs_debug("gs_close: (%d,%p,%p) completed\n",
829 port->port_num, tty, file);
831 exit:
832 spin_unlock_irq(&port->port_lock);
833 mutex_unlock(mtx);
837 * gs_write
839 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
841 unsigned long flags;
842 struct gs_port *port = tty->driver_data;
843 int ret;
845 if (port == NULL) {
846 pr_err("gs_write: NULL port pointer\n");
847 return -EIO;
850 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
851 count);
853 if (count == 0)
854 return 0;
856 spin_lock_irqsave(&port->port_lock, flags);
858 if (port->port_dev == NULL) {
859 pr_err("gs_write: (%d,%p) port is not connected\n",
860 port->port_num, tty);
861 ret = -EIO;
862 goto exit;
865 if (port->port_open_count == 0) {
866 pr_err("gs_write: (%d,%p) port is closed\n",
867 port->port_num, tty);
868 ret = -EBADF;
869 goto exit;
872 count = gs_buf_put(port->port_write_buf, buf, count);
874 spin_unlock_irqrestore(&port->port_lock, flags);
876 gs_send(gs_device);
878 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
879 count);
881 return count;
883 exit:
884 spin_unlock_irqrestore(&port->port_lock, flags);
885 return ret;
889 * gs_put_char
891 static int gs_put_char(struct tty_struct *tty, unsigned char ch)
893 unsigned long flags;
894 struct gs_port *port = tty->driver_data;
895 int ret = 0;
897 if (port == NULL) {
898 pr_err("gs_put_char: NULL port pointer\n");
899 return 0;
902 gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
903 port->port_num, tty, ch, __builtin_return_address(0));
905 spin_lock_irqsave(&port->port_lock, flags);
907 if (port->port_dev == NULL) {
908 pr_err("gs_put_char: (%d,%p) port is not connected\n",
909 port->port_num, tty);
910 goto exit;
913 if (port->port_open_count == 0) {
914 pr_err("gs_put_char: (%d,%p) port is closed\n",
915 port->port_num, tty);
916 goto exit;
919 ret = gs_buf_put(port->port_write_buf, &ch, 1);
921 exit:
922 spin_unlock_irqrestore(&port->port_lock, flags);
923 return ret;
927 * gs_flush_chars
929 static void gs_flush_chars(struct tty_struct *tty)
931 unsigned long flags;
932 struct gs_port *port = tty->driver_data;
934 if (port == NULL) {
935 pr_err("gs_flush_chars: NULL port pointer\n");
936 return;
939 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
941 spin_lock_irqsave(&port->port_lock, flags);
943 if (port->port_dev == NULL) {
944 pr_err("gs_flush_chars: (%d,%p) port is not connected\n",
945 port->port_num, tty);
946 goto exit;
949 if (port->port_open_count == 0) {
950 pr_err("gs_flush_chars: (%d,%p) port is closed\n",
951 port->port_num, tty);
952 goto exit;
955 spin_unlock_irqrestore(&port->port_lock, flags);
957 gs_send(gs_device);
959 return;
961 exit:
962 spin_unlock_irqrestore(&port->port_lock, flags);
966 * gs_write_room
968 static int gs_write_room(struct tty_struct *tty)
971 int room = 0;
972 unsigned long flags;
973 struct gs_port *port = tty->driver_data;
976 if (port == NULL)
977 return 0;
979 spin_lock_irqsave(&port->port_lock, flags);
981 if (port->port_dev != NULL && port->port_open_count > 0
982 && port->port_write_buf != NULL)
983 room = gs_buf_space_avail(port->port_write_buf);
985 spin_unlock_irqrestore(&port->port_lock, flags);
987 gs_debug("gs_write_room: (%d,%p) room=%d\n",
988 port->port_num, tty, room);
990 return room;
994 * gs_chars_in_buffer
996 static int gs_chars_in_buffer(struct tty_struct *tty)
998 int chars = 0;
999 unsigned long flags;
1000 struct gs_port *port = tty->driver_data;
1002 if (port == NULL)
1003 return 0;
1005 spin_lock_irqsave(&port->port_lock, flags);
1007 if (port->port_dev != NULL && port->port_open_count > 0
1008 && port->port_write_buf != NULL)
1009 chars = gs_buf_data_avail(port->port_write_buf);
1011 spin_unlock_irqrestore(&port->port_lock, flags);
1013 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1014 port->port_num, tty, chars);
1016 return chars;
1020 * gs_throttle
1022 static void gs_throttle(struct tty_struct *tty)
1027 * gs_unthrottle
1029 static void gs_unthrottle(struct tty_struct *tty)
1034 * gs_break
1036 static void gs_break(struct tty_struct *tty, int break_state)
1041 * gs_ioctl
1043 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1045 struct gs_port *port = tty->driver_data;
1047 if (port == NULL) {
1048 pr_err("gs_ioctl: NULL port pointer\n");
1049 return -EIO;
1052 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1053 port->port_num, tty, file, cmd, arg);
1055 /* handle ioctls */
1057 /* could not handle ioctl */
1058 return -ENOIOCTLCMD;
1062 * gs_set_termios
1064 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
1069 * gs_send
1071 * This function finds available write requests, calls
1072 * gs_send_packet to fill these packets with data, and
1073 * continues until either there are no more write requests
1074 * available or no more data to send. This function is
1075 * run whenever data arrives or write requests are available.
1077 static int gs_send(struct gs_dev *dev)
1079 int ret,len;
1080 unsigned long flags;
1081 struct usb_ep *ep;
1082 struct usb_request *req;
1083 struct gs_req_entry *req_entry;
1085 if (dev == NULL) {
1086 pr_err("gs_send: NULL device pointer\n");
1087 return -ENODEV;
1090 spin_lock_irqsave(&dev->dev_lock, flags);
1092 ep = dev->dev_in_ep;
1094 while(!list_empty(&dev->dev_req_list)) {
1096 req_entry = list_entry(dev->dev_req_list.next,
1097 struct gs_req_entry, re_entry);
1099 req = req_entry->re_req;
1101 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1103 if (len > 0) {
1104 gs_debug_level(3, "gs_send: len=%d, 0x%2.2x "
1105 "0x%2.2x 0x%2.2x ...\n", len,
1106 *((unsigned char *)req->buf),
1107 *((unsigned char *)req->buf+1),
1108 *((unsigned char *)req->buf+2));
1109 list_del(&req_entry->re_entry);
1110 req->length = len;
1111 spin_unlock_irqrestore(&dev->dev_lock, flags);
1112 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1113 pr_err(
1114 "gs_send: cannot queue read request, ret=%d\n",
1115 ret);
1116 spin_lock_irqsave(&dev->dev_lock, flags);
1117 break;
1119 spin_lock_irqsave(&dev->dev_lock, flags);
1120 } else {
1121 break;
1126 spin_unlock_irqrestore(&dev->dev_lock, flags);
1128 return 0;
1132 * gs_send_packet
1134 * If there is data to send, a packet is built in the given
1135 * buffer and the size is returned. If there is no data to
1136 * send, 0 is returned. If there is any error a negative
1137 * error number is returned.
1139 * Called during USB completion routine, on interrupt time.
1141 * We assume that disconnect will not happen until all completion
1142 * routines have completed, so we can assume that the dev_port
1143 * array does not change during the lifetime of this function.
1145 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1147 unsigned int len;
1148 struct gs_port *port;
1150 /* TEMPORARY -- only port 0 is supported right now */
1151 port = dev->dev_port[0];
1153 if (port == NULL) {
1154 pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
1155 return -EIO;
1158 spin_lock(&port->port_lock);
1160 len = gs_buf_data_avail(port->port_write_buf);
1161 if (len < size)
1162 size = len;
1164 if (size == 0)
1165 goto exit;
1167 size = gs_buf_get(port->port_write_buf, packet, size);
1169 if (port->port_tty)
1170 wake_up_interruptible(&port->port_tty->write_wait);
1172 exit:
1173 spin_unlock(&port->port_lock);
1174 return size;
1178 * gs_recv_packet
1180 * Called for each USB packet received. Reads the packet
1181 * header and stuffs the data in the appropriate tty buffer.
1182 * Returns 0 if successful, or a negative error number.
1184 * Called during USB completion routine, on interrupt time.
1186 * We assume that disconnect will not happen until all completion
1187 * routines have completed, so we can assume that the dev_port
1188 * array does not change during the lifetime of this function.
1190 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1192 unsigned int len;
1193 struct gs_port *port;
1194 int ret;
1195 struct tty_struct *tty;
1197 /* TEMPORARY -- only port 0 is supported right now */
1198 port = dev->dev_port[0];
1200 if (port == NULL) {
1201 pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
1202 port->port_num);
1203 return -EIO;
1206 spin_lock(&port->port_lock);
1208 if (port->port_open_count == 0) {
1209 pr_err("gs_recv_packet: port=%d, port is closed\n",
1210 port->port_num);
1211 ret = -EIO;
1212 goto exit;
1216 tty = port->port_tty;
1218 if (tty == NULL) {
1219 pr_err("gs_recv_packet: port=%d, NULL tty pointer\n",
1220 port->port_num);
1221 ret = -EIO;
1222 goto exit;
1225 if (port->port_tty->magic != TTY_MAGIC) {
1226 pr_err("gs_recv_packet: port=%d, bad tty magic\n",
1227 port->port_num);
1228 ret = -EIO;
1229 goto exit;
1232 len = tty_buffer_request_room(tty, size);
1233 if (len > 0) {
1234 tty_insert_flip_string(tty, packet, len);
1235 tty_flip_buffer_push(port->port_tty);
1236 wake_up_interruptible(&port->port_tty->read_wait);
1238 ret = 0;
1239 exit:
1240 spin_unlock(&port->port_lock);
1241 return ret;
1245 * gs_read_complete
1247 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1249 int ret;
1250 struct gs_dev *dev = ep->driver_data;
1252 if (dev == NULL) {
1253 pr_err("gs_read_complete: NULL device pointer\n");
1254 return;
1257 switch(req->status) {
1258 case 0:
1259 /* normal completion */
1260 gs_recv_packet(dev, req->buf, req->actual);
1261 requeue:
1262 req->length = ep->maxpacket;
1263 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1264 pr_err(
1265 "gs_read_complete: cannot queue read request, ret=%d\n",
1266 ret);
1268 break;
1270 case -ESHUTDOWN:
1271 /* disconnect */
1272 gs_debug("gs_read_complete: shutdown\n");
1273 gs_free_req(ep, req);
1274 break;
1276 default:
1277 /* unexpected */
1278 pr_err(
1279 "gs_read_complete: unexpected status error, status=%d\n",
1280 req->status);
1281 goto requeue;
1282 break;
1287 * gs_write_complete
1289 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1291 struct gs_dev *dev = ep->driver_data;
1292 struct gs_req_entry *gs_req = req->context;
1294 if (dev == NULL) {
1295 pr_err("gs_write_complete: NULL device pointer\n");
1296 return;
1299 switch(req->status) {
1300 case 0:
1301 /* normal completion */
1302 requeue:
1303 if (gs_req == NULL) {
1304 pr_err("gs_write_complete: NULL request pointer\n");
1305 return;
1308 spin_lock(&dev->dev_lock);
1309 list_add(&gs_req->re_entry, &dev->dev_req_list);
1310 spin_unlock(&dev->dev_lock);
1312 gs_send(dev);
1314 break;
1316 case -ESHUTDOWN:
1317 /* disconnect */
1318 gs_debug("gs_write_complete: shutdown\n");
1319 gs_free_req(ep, req);
1320 break;
1322 default:
1323 pr_err(
1324 "gs_write_complete: unexpected status error, status=%d\n",
1325 req->status);
1326 goto requeue;
1327 break;
1331 /* Gadget Driver */
1334 * gs_bind
1336 * Called on module load. Allocates and initializes the device
1337 * structure and a control request.
1339 static int __init gs_bind(struct usb_gadget *gadget)
1341 int ret;
1342 struct usb_ep *ep;
1343 struct gs_dev *dev;
1344 int gcnum;
1346 /* Some controllers can't support CDC ACM:
1347 * - sh doesn't support multiple interfaces or configs;
1348 * - sa1100 doesn't have a third interrupt endpoint
1350 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1351 use_acm = 0;
1353 gcnum = usb_gadget_controller_number(gadget);
1354 if (gcnum >= 0)
1355 gs_device_desc.bcdDevice =
1356 cpu_to_le16(GS_VERSION_NUM | gcnum);
1357 else {
1358 pr_warning("gs_bind: controller '%s' not recognized\n",
1359 gadget->name);
1360 /* unrecognized, but safe unless bulk is REALLY quirky */
1361 gs_device_desc.bcdDevice =
1362 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1365 usb_ep_autoconfig_reset(gadget);
1367 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1368 if (!ep)
1369 goto autoconf_fail;
1370 EP_IN_NAME = ep->name;
1371 ep->driver_data = ep; /* claim the endpoint */
1373 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1374 if (!ep)
1375 goto autoconf_fail;
1376 EP_OUT_NAME = ep->name;
1377 ep->driver_data = ep; /* claim the endpoint */
1379 if (use_acm) {
1380 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1381 if (!ep) {
1382 pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
1383 goto autoconf_fail;
1385 gs_device_desc.idProduct = __constant_cpu_to_le16(
1386 GS_CDC_PRODUCT_ID),
1387 EP_NOTIFY_NAME = ep->name;
1388 ep->driver_data = ep; /* claim the endpoint */
1391 gs_device_desc.bDeviceClass = use_acm
1392 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1393 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1395 if (gadget_is_dualspeed(gadget)) {
1396 gs_qualifier_desc.bDeviceClass = use_acm
1397 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1398 /* assume ep0 uses the same packet size for both speeds */
1399 gs_qualifier_desc.bMaxPacketSize0 =
1400 gs_device_desc.bMaxPacketSize0;
1401 /* assume endpoints are dual-speed */
1402 gs_highspeed_notify_desc.bEndpointAddress =
1403 gs_fullspeed_notify_desc.bEndpointAddress;
1404 gs_highspeed_in_desc.bEndpointAddress =
1405 gs_fullspeed_in_desc.bEndpointAddress;
1406 gs_highspeed_out_desc.bEndpointAddress =
1407 gs_fullspeed_out_desc.bEndpointAddress;
1410 usb_gadget_set_selfpowered(gadget);
1412 if (gadget_is_otg(gadget)) {
1413 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1414 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1415 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1418 gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1419 if (dev == NULL)
1420 return -ENOMEM;
1422 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1423 init_utsname()->sysname, init_utsname()->release,
1424 gadget->name);
1426 dev->dev_gadget = gadget;
1427 spin_lock_init(&dev->dev_lock);
1428 INIT_LIST_HEAD(&dev->dev_req_list);
1429 set_gadget_data(gadget, dev);
1431 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1432 pr_err("gs_bind: cannot allocate ports\n");
1433 gs_unbind(gadget);
1434 return ret;
1437 /* preallocate control response and buffer */
1438 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1439 GFP_KERNEL);
1440 if (dev->dev_ctrl_req == NULL) {
1441 gs_unbind(gadget);
1442 return -ENOMEM;
1444 dev->dev_ctrl_req->complete = gs_setup_complete;
1446 gadget->ep0->driver_data = dev;
1448 pr_info("gs_bind: %s %s bound\n",
1449 GS_LONG_NAME, GS_VERSION_STR);
1451 return 0;
1453 autoconf_fail:
1454 pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
1455 return -ENODEV;
1459 * gs_unbind
1461 * Called on module unload. Frees the control request and device
1462 * structure.
1464 static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1466 struct gs_dev *dev = get_gadget_data(gadget);
1468 gs_device = NULL;
1470 /* read/write requests already freed, only control request remains */
1471 if (dev != NULL) {
1472 if (dev->dev_ctrl_req != NULL) {
1473 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1474 dev->dev_ctrl_req = NULL;
1476 gs_free_ports(dev);
1477 if (dev->dev_notify_ep)
1478 usb_ep_disable(dev->dev_notify_ep);
1479 if (dev->dev_in_ep)
1480 usb_ep_disable(dev->dev_in_ep);
1481 if (dev->dev_out_ep)
1482 usb_ep_disable(dev->dev_out_ep);
1483 kfree(dev);
1484 set_gadget_data(gadget, NULL);
1487 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1488 GS_VERSION_STR);
1492 * gs_setup
1494 * Implements all the control endpoint functionality that's not
1495 * handled in hardware or the hardware driver.
1497 * Returns the size of the data sent to the host, or a negative
1498 * error number.
1500 static int gs_setup(struct usb_gadget *gadget,
1501 const struct usb_ctrlrequest *ctrl)
1503 int ret = -EOPNOTSUPP;
1504 struct gs_dev *dev = get_gadget_data(gadget);
1505 struct usb_request *req = dev->dev_ctrl_req;
1506 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1507 u16 wValue = le16_to_cpu(ctrl->wValue);
1508 u16 wLength = le16_to_cpu(ctrl->wLength);
1510 req->complete = gs_setup_complete;
1512 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1513 case USB_TYPE_STANDARD:
1514 ret = gs_setup_standard(gadget,ctrl);
1515 break;
1517 case USB_TYPE_CLASS:
1518 ret = gs_setup_class(gadget,ctrl);
1519 break;
1521 default:
1522 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1523 "value=%04x, index=%04x, length=%d\n",
1524 ctrl->bRequestType, ctrl->bRequest,
1525 wValue, wIndex, wLength);
1526 break;
1529 /* respond with data transfer before status phase? */
1530 if (ret >= 0) {
1531 req->length = ret;
1532 req->zero = ret < wLength
1533 && (ret % gadget->ep0->maxpacket) == 0;
1534 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1535 if (ret < 0) {
1536 pr_err("gs_setup: cannot queue response, ret=%d\n",
1537 ret);
1538 req->status = 0;
1539 gs_setup_complete(gadget->ep0, req);
1543 /* device either stalls (ret < 0) or reports success */
1544 return ret;
1547 static int gs_setup_standard(struct usb_gadget *gadget,
1548 const struct usb_ctrlrequest *ctrl)
1550 int ret = -EOPNOTSUPP;
1551 struct gs_dev *dev = get_gadget_data(gadget);
1552 struct usb_request *req = dev->dev_ctrl_req;
1553 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1554 u16 wValue = le16_to_cpu(ctrl->wValue);
1555 u16 wLength = le16_to_cpu(ctrl->wLength);
1557 switch (ctrl->bRequest) {
1558 case USB_REQ_GET_DESCRIPTOR:
1559 if (ctrl->bRequestType != USB_DIR_IN)
1560 break;
1562 switch (wValue >> 8) {
1563 case USB_DT_DEVICE:
1564 ret = min(wLength,
1565 (u16)sizeof(struct usb_device_descriptor));
1566 memcpy(req->buf, &gs_device_desc, ret);
1567 break;
1569 case USB_DT_DEVICE_QUALIFIER:
1570 if (!gadget_is_dualspeed(gadget))
1571 break;
1572 ret = min(wLength,
1573 (u16)sizeof(struct usb_qualifier_descriptor));
1574 memcpy(req->buf, &gs_qualifier_desc, ret);
1575 break;
1577 case USB_DT_OTHER_SPEED_CONFIG:
1578 if (!gadget_is_dualspeed(gadget))
1579 break;
1580 /* fall through */
1581 case USB_DT_CONFIG:
1582 ret = gs_build_config_buf(req->buf, gadget,
1583 wValue >> 8, wValue & 0xff,
1584 gadget_is_otg(gadget));
1585 if (ret >= 0)
1586 ret = min(wLength, (u16)ret);
1587 break;
1589 case USB_DT_STRING:
1590 /* wIndex == language code. */
1591 ret = usb_gadget_get_string(&gs_string_table,
1592 wValue & 0xff, req->buf);
1593 if (ret >= 0)
1594 ret = min(wLength, (u16)ret);
1595 break;
1597 break;
1599 case USB_REQ_SET_CONFIGURATION:
1600 if (ctrl->bRequestType != 0)
1601 break;
1602 spin_lock(&dev->dev_lock);
1603 ret = gs_set_config(dev, wValue);
1604 spin_unlock(&dev->dev_lock);
1605 break;
1607 case USB_REQ_GET_CONFIGURATION:
1608 if (ctrl->bRequestType != USB_DIR_IN)
1609 break;
1610 *(u8 *)req->buf = dev->dev_config;
1611 ret = min(wLength, (u16)1);
1612 break;
1614 case USB_REQ_SET_INTERFACE:
1615 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1616 || !dev->dev_config
1617 || wIndex >= GS_MAX_NUM_INTERFACES)
1618 break;
1619 if (dev->dev_config == GS_BULK_CONFIG_ID
1620 && wIndex != GS_BULK_INTERFACE_ID)
1621 break;
1622 /* no alternate interface settings */
1623 if (wValue != 0)
1624 break;
1625 spin_lock(&dev->dev_lock);
1626 /* PXA hardware partially handles SET_INTERFACE;
1627 * we need to kluge around that interference. */
1628 if (gadget_is_pxa(gadget)) {
1629 ret = gs_set_config(dev, use_acm ?
1630 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1631 goto set_interface_done;
1633 if (dev->dev_config != GS_BULK_CONFIG_ID
1634 && wIndex == GS_CONTROL_INTERFACE_ID) {
1635 if (dev->dev_notify_ep) {
1636 usb_ep_disable(dev->dev_notify_ep);
1637 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1639 } else {
1640 usb_ep_disable(dev->dev_in_ep);
1641 usb_ep_disable(dev->dev_out_ep);
1642 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1643 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1645 ret = 0;
1646 set_interface_done:
1647 spin_unlock(&dev->dev_lock);
1648 break;
1650 case USB_REQ_GET_INTERFACE:
1651 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1652 || dev->dev_config == GS_NO_CONFIG_ID)
1653 break;
1654 if (wIndex >= GS_MAX_NUM_INTERFACES
1655 || (dev->dev_config == GS_BULK_CONFIG_ID
1656 && wIndex != GS_BULK_INTERFACE_ID)) {
1657 ret = -EDOM;
1658 break;
1660 /* no alternate interface settings */
1661 *(u8 *)req->buf = 0;
1662 ret = min(wLength, (u16)1);
1663 break;
1665 default:
1666 pr_err("gs_setup: unknown standard request, type=%02x, "
1667 "request=%02x, value=%04x, index=%04x, length=%d\n",
1668 ctrl->bRequestType, ctrl->bRequest,
1669 wValue, wIndex, wLength);
1670 break;
1673 return ret;
1676 static int gs_setup_class(struct usb_gadget *gadget,
1677 const struct usb_ctrlrequest *ctrl)
1679 int ret = -EOPNOTSUPP;
1680 struct gs_dev *dev = get_gadget_data(gadget);
1681 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1682 struct usb_request *req = dev->dev_ctrl_req;
1683 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1684 u16 wValue = le16_to_cpu(ctrl->wValue);
1685 u16 wLength = le16_to_cpu(ctrl->wLength);
1687 switch (ctrl->bRequest) {
1688 case USB_CDC_REQ_SET_LINE_CODING:
1689 if (wLength != sizeof(struct usb_cdc_line_coding))
1690 break;
1691 ret = wLength;
1692 req->complete = gs_setup_complete_set_line_coding;
1693 break;
1695 case USB_CDC_REQ_GET_LINE_CODING:
1696 ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
1697 if (port) {
1698 spin_lock(&port->port_lock);
1699 memcpy(req->buf, &port->port_line_coding, ret);
1700 spin_unlock(&port->port_lock);
1702 break;
1704 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1705 if (wLength != 0)
1706 break;
1707 ret = 0;
1708 if (port) {
1709 /* REVISIT: we currently just remember this data.
1710 * If we change that, update whatever hardware needs
1711 * updating.
1713 spin_lock(&port->port_lock);
1714 port->port_handshake_bits = wValue;
1715 spin_unlock(&port->port_lock);
1717 break;
1719 default:
1720 /* NOTE: strictly speaking, we should accept AT-commands
1721 * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE.
1722 * But our call management descriptor says we don't handle
1723 * call management, so we should be able to get by without
1724 * handling those "required" commands (except by stalling).
1726 pr_err("gs_setup: unknown class request, "
1727 "type=%02x, request=%02x, value=%04x, "
1728 "index=%04x, length=%d\n",
1729 ctrl->bRequestType, ctrl->bRequest,
1730 wValue, wIndex, wLength);
1731 break;
1734 return ret;
1737 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
1738 struct usb_request *req)
1740 struct gs_dev *dev = ep->driver_data;
1741 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1743 switch (req->status) {
1744 case 0:
1745 /* normal completion */
1746 if (req->actual != sizeof(port->port_line_coding))
1747 usb_ep_set_halt(ep);
1748 else if (port) {
1749 struct usb_cdc_line_coding *value = req->buf;
1751 /* REVISIT: we currently just remember this data.
1752 * If we change that, (a) validate it first, then
1753 * (b) update whatever hardware needs updating.
1755 spin_lock(&port->port_lock);
1756 port->port_line_coding = *value;
1757 spin_unlock(&port->port_lock);
1759 break;
1761 case -ESHUTDOWN:
1762 /* disconnect */
1763 gs_free_req(ep, req);
1764 break;
1766 default:
1767 /* unexpected */
1768 break;
1770 return;
1774 * gs_setup_complete
1776 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1778 if (req->status || req->actual != req->length) {
1779 pr_err("gs_setup_complete: status error, status=%d, "
1780 "actual=%d, length=%d\n",
1781 req->status, req->actual, req->length);
1786 * gs_disconnect
1788 * Called when the device is disconnected. Frees the closed
1789 * ports and disconnects open ports. Open ports will be freed
1790 * on close. Then reallocates the ports for the next connection.
1792 static void gs_disconnect(struct usb_gadget *gadget)
1794 unsigned long flags;
1795 struct gs_dev *dev = get_gadget_data(gadget);
1797 spin_lock_irqsave(&dev->dev_lock, flags);
1799 gs_reset_config(dev);
1801 /* free closed ports and disconnect open ports */
1802 /* (open ports will be freed when closed) */
1803 gs_free_ports(dev);
1805 /* re-allocate ports for the next connection */
1806 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1807 pr_err("gs_disconnect: cannot re-allocate ports\n");
1809 spin_unlock_irqrestore(&dev->dev_lock, flags);
1811 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1815 * gs_set_config
1817 * Configures the device by enabling device specific
1818 * optimizations, setting up the endpoints, allocating
1819 * read and write requests and queuing read requests.
1821 * The device lock must be held when calling this function.
1823 static int gs_set_config(struct gs_dev *dev, unsigned config)
1825 int i;
1826 int ret = 0;
1827 struct usb_gadget *gadget = dev->dev_gadget;
1828 struct usb_ep *ep;
1829 struct usb_endpoint_descriptor *ep_desc;
1830 struct usb_request *req;
1831 struct gs_req_entry *req_entry;
1833 if (dev == NULL) {
1834 pr_err("gs_set_config: NULL device pointer\n");
1835 return 0;
1838 if (config == dev->dev_config)
1839 return 0;
1841 gs_reset_config(dev);
1843 switch (config) {
1844 case GS_NO_CONFIG_ID:
1845 return 0;
1846 case GS_BULK_CONFIG_ID:
1847 if (use_acm)
1848 return -EINVAL;
1849 /* device specific optimizations */
1850 if (gadget_is_net2280(gadget))
1851 net2280_set_fifo_mode(gadget, 1);
1852 break;
1853 case GS_ACM_CONFIG_ID:
1854 if (!use_acm)
1855 return -EINVAL;
1856 /* device specific optimizations */
1857 if (gadget_is_net2280(gadget))
1858 net2280_set_fifo_mode(gadget, 1);
1859 break;
1860 default:
1861 return -EINVAL;
1864 dev->dev_config = config;
1866 gadget_for_each_ep(ep, gadget) {
1868 if (EP_NOTIFY_NAME
1869 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1870 ep_desc = choose_ep_desc(gadget,
1871 &gs_highspeed_notify_desc,
1872 &gs_fullspeed_notify_desc);
1873 ret = usb_ep_enable(ep,ep_desc);
1874 if (ret == 0) {
1875 ep->driver_data = dev;
1876 dev->dev_notify_ep = ep;
1877 dev->dev_notify_ep_desc = ep_desc;
1878 } else {
1879 pr_err("gs_set_config: cannot enable NOTIFY "
1880 "endpoint %s, ret=%d\n",
1881 ep->name, ret);
1882 goto exit_reset_config;
1886 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1887 ep_desc = choose_ep_desc(gadget,
1888 &gs_highspeed_in_desc,
1889 &gs_fullspeed_in_desc);
1890 ret = usb_ep_enable(ep,ep_desc);
1891 if (ret == 0) {
1892 ep->driver_data = dev;
1893 dev->dev_in_ep = ep;
1894 dev->dev_in_ep_desc = ep_desc;
1895 } else {
1896 pr_err("gs_set_config: cannot enable IN "
1897 "endpoint %s, ret=%d\n",
1898 ep->name, ret);
1899 goto exit_reset_config;
1903 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1904 ep_desc = choose_ep_desc(gadget,
1905 &gs_highspeed_out_desc,
1906 &gs_fullspeed_out_desc);
1907 ret = usb_ep_enable(ep,ep_desc);
1908 if (ret == 0) {
1909 ep->driver_data = dev;
1910 dev->dev_out_ep = ep;
1911 dev->dev_out_ep_desc = ep_desc;
1912 } else {
1913 pr_err("gs_set_config: cannot enable OUT "
1914 "endpoint %s, ret=%d\n",
1915 ep->name, ret);
1916 goto exit_reset_config;
1922 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
1923 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1924 pr_err("gs_set_config: cannot find endpoints\n");
1925 ret = -ENODEV;
1926 goto exit_reset_config;
1929 /* allocate and queue read requests */
1930 ep = dev->dev_out_ep;
1931 for (i=0; i<read_q_size && ret == 0; i++) {
1932 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1933 req->complete = gs_read_complete;
1934 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1935 pr_err("gs_set_config: cannot queue read "
1936 "request, ret=%d\n", ret);
1938 } else {
1939 pr_err("gs_set_config: cannot allocate "
1940 "read requests\n");
1941 ret = -ENOMEM;
1942 goto exit_reset_config;
1946 /* allocate write requests, and put on free list */
1947 ep = dev->dev_in_ep;
1948 for (i=0; i<write_q_size; i++) {
1949 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1950 req_entry->re_req->complete = gs_write_complete;
1951 list_add(&req_entry->re_entry, &dev->dev_req_list);
1952 } else {
1953 pr_err("gs_set_config: cannot allocate "
1954 "write requests\n");
1955 ret = -ENOMEM;
1956 goto exit_reset_config;
1960 /* REVISIT the ACM mode should be able to actually *issue* some
1961 * notifications, for at least serial state change events if
1962 * not also for network connection; say so in bmCapabilities.
1965 pr_info("gs_set_config: %s configured, %s speed %s config\n",
1966 GS_LONG_NAME,
1967 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1968 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1970 return 0;
1972 exit_reset_config:
1973 gs_reset_config(dev);
1974 return ret;
1978 * gs_reset_config
1980 * Mark the device as not configured, disable all endpoints,
1981 * which forces completion of pending I/O and frees queued
1982 * requests, and free the remaining write requests on the
1983 * free list.
1985 * The device lock must be held when calling this function.
1987 static void gs_reset_config(struct gs_dev *dev)
1989 struct gs_req_entry *req_entry;
1991 if (dev == NULL) {
1992 pr_err("gs_reset_config: NULL device pointer\n");
1993 return;
1996 if (dev->dev_config == GS_NO_CONFIG_ID)
1997 return;
1999 dev->dev_config = GS_NO_CONFIG_ID;
2001 /* free write requests on the free list */
2002 while(!list_empty(&dev->dev_req_list)) {
2003 req_entry = list_entry(dev->dev_req_list.next,
2004 struct gs_req_entry, re_entry);
2005 list_del(&req_entry->re_entry);
2006 gs_free_req_entry(dev->dev_in_ep, req_entry);
2009 /* disable endpoints, forcing completion of pending i/o; */
2010 /* completion handlers free their requests in this case */
2011 if (dev->dev_notify_ep) {
2012 usb_ep_disable(dev->dev_notify_ep);
2013 dev->dev_notify_ep = NULL;
2015 if (dev->dev_in_ep) {
2016 usb_ep_disable(dev->dev_in_ep);
2017 dev->dev_in_ep = NULL;
2019 if (dev->dev_out_ep) {
2020 usb_ep_disable(dev->dev_out_ep);
2021 dev->dev_out_ep = NULL;
2026 * gs_build_config_buf
2028 * Builds the config descriptors in the given buffer and returns the
2029 * length, or a negative error number.
2031 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
2032 u8 type, unsigned int index, int is_otg)
2034 int len;
2035 int high_speed = 0;
2036 const struct usb_config_descriptor *config_desc;
2037 const struct usb_descriptor_header **function;
2039 if (index >= gs_device_desc.bNumConfigurations)
2040 return -EINVAL;
2042 /* other speed switches high and full speed */
2043 if (gadget_is_dualspeed(g)) {
2044 high_speed = (g->speed == USB_SPEED_HIGH);
2045 if (type == USB_DT_OTHER_SPEED_CONFIG)
2046 high_speed = !high_speed;
2049 if (use_acm) {
2050 config_desc = &gs_acm_config_desc;
2051 function = high_speed
2052 ? gs_acm_highspeed_function
2053 : gs_acm_fullspeed_function;
2054 } else {
2055 config_desc = &gs_bulk_config_desc;
2056 function = high_speed
2057 ? gs_bulk_highspeed_function
2058 : gs_bulk_fullspeed_function;
2061 /* for now, don't advertise srp-only devices */
2062 if (!is_otg)
2063 function++;
2065 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2066 if (len < 0)
2067 return len;
2069 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2071 return len;
2075 * gs_alloc_req
2077 * Allocate a usb_request and its buffer. Returns a pointer to the
2078 * usb_request or NULL if there is an error.
2080 static struct usb_request *
2081 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
2083 struct usb_request *req;
2085 if (ep == NULL)
2086 return NULL;
2088 req = usb_ep_alloc_request(ep, kmalloc_flags);
2090 if (req != NULL) {
2091 req->length = len;
2092 req->buf = kmalloc(len, kmalloc_flags);
2093 if (req->buf == NULL) {
2094 usb_ep_free_request(ep, req);
2095 return NULL;
2099 return req;
2103 * gs_free_req
2105 * Free a usb_request and its buffer.
2107 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2109 if (ep != NULL && req != NULL) {
2110 kfree(req->buf);
2111 usb_ep_free_request(ep, req);
2116 * gs_alloc_req_entry
2118 * Allocates a request and its buffer, using the given
2119 * endpoint, buffer len, and kmalloc flags.
2121 static struct gs_req_entry *
2122 gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2124 struct gs_req_entry *req;
2126 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2127 if (req == NULL)
2128 return NULL;
2130 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2131 if (req->re_req == NULL) {
2132 kfree(req);
2133 return NULL;
2136 req->re_req->context = req;
2138 return req;
2142 * gs_free_req_entry
2144 * Frees a request and its buffer.
2146 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2148 if (ep != NULL && req != NULL) {
2149 if (req->re_req != NULL)
2150 gs_free_req(ep, req->re_req);
2151 kfree(req);
2156 * gs_alloc_ports
2158 * Allocate all ports and set the gs_dev struct to point to them.
2159 * Return 0 if successful, or a negative error number.
2161 * The device lock is normally held when calling this function.
2163 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2165 int i;
2166 struct gs_port *port;
2168 if (dev == NULL)
2169 return -EIO;
2171 for (i=0; i<GS_NUM_PORTS; i++) {
2172 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2173 return -ENOMEM;
2175 port->port_dev = dev;
2176 port->port_num = i;
2177 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
2178 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2179 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2180 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2181 spin_lock_init(&port->port_lock);
2182 init_waitqueue_head(&port->port_write_wait);
2184 dev->dev_port[i] = port;
2187 return 0;
2191 * gs_free_ports
2193 * Free all closed ports. Open ports are disconnected by
2194 * freeing their write buffers, setting their device pointers
2195 * and the pointers to them in the device to NULL. These
2196 * ports will be freed when closed.
2198 * The device lock is normally held when calling this function.
2200 static void gs_free_ports(struct gs_dev *dev)
2202 int i;
2203 unsigned long flags;
2204 struct gs_port *port;
2206 if (dev == NULL)
2207 return;
2209 for (i=0; i<GS_NUM_PORTS; i++) {
2210 if ((port=dev->dev_port[i]) != NULL) {
2211 dev->dev_port[i] = NULL;
2213 spin_lock_irqsave(&port->port_lock, flags);
2215 if (port->port_write_buf != NULL) {
2216 gs_buf_free(port->port_write_buf);
2217 port->port_write_buf = NULL;
2220 if (port->port_open_count > 0 || port->port_in_use) {
2221 port->port_dev = NULL;
2222 wake_up_interruptible(&port->port_write_wait);
2223 if (port->port_tty) {
2224 tty_hangup(port->port_tty);
2226 spin_unlock_irqrestore(&port->port_lock, flags);
2227 } else {
2228 spin_unlock_irqrestore(&port->port_lock, flags);
2229 kfree(port);
2236 /* Circular Buffer */
2239 * gs_buf_alloc
2241 * Allocate a circular buffer and all associated memory.
2243 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2245 struct gs_buf *gb;
2247 if (size == 0)
2248 return NULL;
2250 gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2251 if (gb == NULL)
2252 return NULL;
2254 gb->buf_buf = kmalloc(size, kmalloc_flags);
2255 if (gb->buf_buf == NULL) {
2256 kfree(gb);
2257 return NULL;
2260 gb->buf_size = size;
2261 gb->buf_get = gb->buf_put = gb->buf_buf;
2263 return gb;
2267 * gs_buf_free
2269 * Free the buffer and all associated memory.
2271 static void gs_buf_free(struct gs_buf *gb)
2273 if (gb) {
2274 kfree(gb->buf_buf);
2275 kfree(gb);
2280 * gs_buf_clear
2282 * Clear out all data in the circular buffer.
2284 static void gs_buf_clear(struct gs_buf *gb)
2286 if (gb != NULL)
2287 gb->buf_get = gb->buf_put;
2288 /* equivalent to a get of all data available */
2292 * gs_buf_data_avail
2294 * Return the number of bytes of data available in the circular
2295 * buffer.
2297 static unsigned int gs_buf_data_avail(struct gs_buf *gb)
2299 if (gb != NULL)
2300 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2301 else
2302 return 0;
2306 * gs_buf_space_avail
2308 * Return the number of bytes of space available in the circular
2309 * buffer.
2311 static unsigned int gs_buf_space_avail(struct gs_buf *gb)
2313 if (gb != NULL)
2314 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2315 else
2316 return 0;
2320 * gs_buf_put
2322 * Copy data data from a user buffer and put it into the circular buffer.
2323 * Restrict to the amount of space available.
2325 * Return the number of bytes copied.
2327 static unsigned int
2328 gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2330 unsigned int len;
2332 if (gb == NULL)
2333 return 0;
2335 len = gs_buf_space_avail(gb);
2336 if (count > len)
2337 count = len;
2339 if (count == 0)
2340 return 0;
2342 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2343 if (count > len) {
2344 memcpy(gb->buf_put, buf, len);
2345 memcpy(gb->buf_buf, buf+len, count - len);
2346 gb->buf_put = gb->buf_buf + count - len;
2347 } else {
2348 memcpy(gb->buf_put, buf, count);
2349 if (count < len)
2350 gb->buf_put += count;
2351 else /* count == len */
2352 gb->buf_put = gb->buf_buf;
2355 return count;
2359 * gs_buf_get
2361 * Get data from the circular buffer and copy to the given buffer.
2362 * Restrict to the amount of data available.
2364 * Return the number of bytes copied.
2366 static unsigned int
2367 gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2369 unsigned int len;
2371 if (gb == NULL)
2372 return 0;
2374 len = gs_buf_data_avail(gb);
2375 if (count > len)
2376 count = len;
2378 if (count == 0)
2379 return 0;
2381 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2382 if (count > len) {
2383 memcpy(buf, gb->buf_get, len);
2384 memcpy(buf+len, gb->buf_buf, count - len);
2385 gb->buf_get = gb->buf_buf + count - len;
2386 } else {
2387 memcpy(buf, gb->buf_get, count);
2388 if (count < len)
2389 gb->buf_get += count;
2390 else /* count == len */
2391 gb->buf_get = gb->buf_buf;
2394 return count;