afdf71f76f7e42c3b33107c632cbc4bc138bb4b7
[safe/jmp/linux-2.6] / drivers / usb / gadget / serial.c
1 /*
2  * g_serial.c -- USB gadget serial driver
3  *
4  * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com)
5  *
6  * This code is based in part on the Gadget Zero driver, which
7  * is Copyright (C) 2003 by David Brownell, all rights reserved.
8  *
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)
13  *
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.
17  *
18  */
19
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>
25
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/cdc.h>
28 #include <linux/usb_gadget.h>
29
30 #include "gadget_chips.h"
31
32
33 /* Defines */
34
35 #define GS_VERSION_STR                  "v2.2"
36 #define GS_VERSION_NUM                  0x0202
37
38 #define GS_LONG_NAME                    "Gadget Serial"
39 #define GS_SHORT_NAME                   "g_serial"
40
41 #define GS_MAJOR                        127
42 #define GS_MINOR_START                  0
43
44 #define GS_NUM_PORTS                    16
45
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
50
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
55
56 #define GS_MAX_DESC_LEN                 256
57
58 #define GS_DEFAULT_READ_Q_SIZE          32
59 #define GS_DEFAULT_WRITE_Q_SIZE         32
60
61 #define GS_DEFAULT_WRITE_BUF_SIZE       8192
62 #define GS_TMP_BUF_SIZE                 8192
63
64 #define GS_CLOSE_TIMEOUT                15
65
66 #define GS_DEFAULT_USE_ACM              0
67
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
72
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)
77 {
78         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
79                 return hs;
80         return fs;
81 }
82
83
84 /* debug settings */
85 #ifdef DEBUG
86 static int debug = 1;
87 #else
88 #define debug 0
89 #endif
90
91 #define gs_debug(format, arg...) \
92         do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
93 #define gs_debug_level(level, format, arg...) \
94         do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
95
96
97 /* Thanks to NetChip Technologies for donating this product ID.
98  *
99  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
100  * Instead:  allocate your own, using normal USB-IF procedures.
101  */
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 */
105
106 #define GS_LOG2_NOTIFY_INTERVAL         5       /* 1 << 5 == 32 msec */
107 #define GS_NOTIFY_MAXPACKET             8
108
109
110 /* Structures */
111
112 struct gs_dev;
113
114 /* circular buffer */
115 struct gs_buf {
116         unsigned int            buf_size;
117         char                    *buf_buf;
118         char                    *buf_get;
119         char                    *buf_put;
120 };
121
122 /* list of requests */
123 struct gs_req_entry {
124         struct list_head        re_entry;
125         struct usb_request      *re_req;
126 };
127
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;
139 };
140
141 /* the device structure holds info for the USB device */
142 struct gs_dev {
143         struct usb_gadget       *dev_gadget;    /* gadget device pointer */
144         spinlock_t              dev_lock;       /* lock for set/reset config */
145         int                     dev_config;     /* configuration number */
146         struct usb_ep           *dev_notify_ep; /* address of notify endpoint */
147         struct usb_ep           *dev_in_ep;     /* address of in endpoint */
148         struct usb_ep           *dev_out_ep;    /* address of out endpoint */
149         struct usb_endpoint_descriptor          /* descriptor of notify ep */
150                                 *dev_notify_ep_desc;
151         struct usb_endpoint_descriptor          /* descriptor of in endpoint */
152                                 *dev_in_ep_desc;
153         struct usb_endpoint_descriptor          /* descriptor of out endpoint */
154                                 *dev_out_ep_desc;
155         struct usb_request      *dev_ctrl_req;  /* control request */
156         struct list_head        dev_req_list;   /* list of write requests */
157         int                     dev_sched_port; /* round robin port scheduled */
158         struct gs_port          *dev_port[GS_NUM_PORTS]; /* the ports */
159 };
160
161
162 /* Functions */
163
164 /* module */
165 static int __init gs_module_init(void);
166 static void __exit gs_module_exit(void);
167
168 /* tty driver */
169 static int gs_open(struct tty_struct *tty, struct file *file);
170 static void gs_close(struct tty_struct *tty, struct file *file);
171 static int gs_write(struct tty_struct *tty,
172         const unsigned char *buf, int count);
173 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
174 static void gs_flush_chars(struct tty_struct *tty);
175 static int gs_write_room(struct tty_struct *tty);
176 static int gs_chars_in_buffer(struct tty_struct *tty);
177 static void gs_throttle(struct tty_struct * tty);
178 static void gs_unthrottle(struct tty_struct * tty);
179 static void gs_break(struct tty_struct *tty, int break_state);
180 static int  gs_ioctl(struct tty_struct *tty, struct file *file,
181         unsigned int cmd, unsigned long arg);
182 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old);
183
184 static int gs_send(struct gs_dev *dev);
185 static int gs_send_packet(struct gs_dev *dev, char *packet,
186         unsigned int size);
187 static int gs_recv_packet(struct gs_dev *dev, char *packet,
188         unsigned int size);
189 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
190 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
191
192 /* gadget driver */
193 static int gs_bind(struct usb_gadget *gadget);
194 static void gs_unbind(struct usb_gadget *gadget);
195 static int gs_setup(struct usb_gadget *gadget,
196         const struct usb_ctrlrequest *ctrl);
197 static int gs_setup_standard(struct usb_gadget *gadget,
198         const struct usb_ctrlrequest *ctrl);
199 static int gs_setup_class(struct usb_gadget *gadget,
200         const struct usb_ctrlrequest *ctrl);
201 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
202 static void gs_disconnect(struct usb_gadget *gadget);
203 static int gs_set_config(struct gs_dev *dev, unsigned config);
204 static void gs_reset_config(struct gs_dev *dev);
205 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
206                 u8 type, unsigned int index, int is_otg);
207
208 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
209         gfp_t kmalloc_flags);
210 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
211
212 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
213         gfp_t kmalloc_flags);
214 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
215
216 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
217 static void gs_free_ports(struct gs_dev *dev);
218
219 /* circular buffer */
220 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
221 static void gs_buf_free(struct gs_buf *gb);
222 static void gs_buf_clear(struct gs_buf *gb);
223 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
224 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
225 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
226         unsigned int count);
227 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
228         unsigned int count);
229
230 /* external functions */
231 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
232
233
234 /* Globals */
235
236 static struct gs_dev *gs_device;
237
238 static const char *EP_IN_NAME;
239 static const char *EP_OUT_NAME;
240 static const char *EP_NOTIFY_NAME;
241
242 static struct mutex gs_open_close_lock[GS_NUM_PORTS];
243
244 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
245 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
246
247 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
248
249 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
250
251
252 /* tty driver struct */
253 static const struct tty_operations gs_tty_ops = {
254         .open =                 gs_open,
255         .close =                gs_close,
256         .write =                gs_write,
257         .put_char =             gs_put_char,
258         .flush_chars =          gs_flush_chars,
259         .write_room =           gs_write_room,
260         .ioctl =                gs_ioctl,
261         .set_termios =          gs_set_termios,
262         .throttle =             gs_throttle,
263         .unthrottle =           gs_unthrottle,
264         .break_ctl =            gs_break,
265         .chars_in_buffer =      gs_chars_in_buffer,
266 };
267 static struct tty_driver *gs_tty_driver;
268
269 /* gadget driver struct */
270 static struct usb_gadget_driver gs_gadget_driver = {
271 #ifdef CONFIG_USB_GADGET_DUALSPEED
272         .speed =                USB_SPEED_HIGH,
273 #else
274         .speed =                USB_SPEED_FULL,
275 #endif /* CONFIG_USB_GADGET_DUALSPEED */
276         .function =             GS_LONG_NAME,
277         .bind =                 gs_bind,
278         .unbind =               gs_unbind,
279         .setup =                gs_setup,
280         .disconnect =           gs_disconnect,
281         .driver = {
282                 .name =         GS_SHORT_NAME,
283         },
284 };
285
286
287 /* USB descriptors */
288
289 #define GS_MANUFACTURER_STR_ID  1
290 #define GS_PRODUCT_STR_ID       2
291 #define GS_SERIAL_STR_ID        3
292 #define GS_BULK_CONFIG_STR_ID   4
293 #define GS_ACM_CONFIG_STR_ID    5
294 #define GS_CONTROL_STR_ID       6
295 #define GS_DATA_STR_ID          7
296
297 /* static strings, in UTF-8 */
298 static char manufacturer[50];
299 static struct usb_string gs_strings[] = {
300         { GS_MANUFACTURER_STR_ID, manufacturer },
301         { GS_PRODUCT_STR_ID, GS_LONG_NAME },
302         { GS_SERIAL_STR_ID, "0" },
303         { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
304         { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
305         { GS_CONTROL_STR_ID, "Gadget Serial Control" },
306         { GS_DATA_STR_ID, "Gadget Serial Data" },
307         {  } /* end of list */
308 };
309
310 static struct usb_gadget_strings gs_string_table = {
311         .language =             0x0409, /* en-us */
312         .strings =              gs_strings,
313 };
314
315 static struct usb_device_descriptor gs_device_desc = {
316         .bLength =              USB_DT_DEVICE_SIZE,
317         .bDescriptorType =      USB_DT_DEVICE,
318         .bcdUSB =               __constant_cpu_to_le16(0x0200),
319         .bDeviceSubClass =      0,
320         .bDeviceProtocol =      0,
321         .idVendor =             __constant_cpu_to_le16(GS_VENDOR_ID),
322         .idProduct =            __constant_cpu_to_le16(GS_PRODUCT_ID),
323         .iManufacturer =        GS_MANUFACTURER_STR_ID,
324         .iProduct =             GS_PRODUCT_STR_ID,
325         .iSerialNumber =        GS_SERIAL_STR_ID,
326         .bNumConfigurations =   GS_NUM_CONFIGS,
327 };
328
329 static struct usb_otg_descriptor gs_otg_descriptor = {
330         .bLength =              sizeof(gs_otg_descriptor),
331         .bDescriptorType =      USB_DT_OTG,
332         .bmAttributes =         USB_OTG_SRP,
333 };
334
335 static struct usb_config_descriptor gs_bulk_config_desc = {
336         .bLength =              USB_DT_CONFIG_SIZE,
337         .bDescriptorType =      USB_DT_CONFIG,
338         /* .wTotalLength computed dynamically */
339         .bNumInterfaces =       1,
340         .bConfigurationValue =  GS_BULK_CONFIG_ID,
341         .iConfiguration =       GS_BULK_CONFIG_STR_ID,
342         .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
343         .bMaxPower =            1,
344 };
345
346 static struct usb_config_descriptor gs_acm_config_desc = {
347         .bLength =              USB_DT_CONFIG_SIZE,
348         .bDescriptorType =      USB_DT_CONFIG,
349         /* .wTotalLength computed dynamically */
350         .bNumInterfaces =       2,
351         .bConfigurationValue =  GS_ACM_CONFIG_ID,
352         .iConfiguration =       GS_ACM_CONFIG_STR_ID,
353         .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
354         .bMaxPower =            1,
355 };
356
357 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
358         .bLength =              USB_DT_INTERFACE_SIZE,
359         .bDescriptorType =      USB_DT_INTERFACE,
360         .bInterfaceNumber =     GS_BULK_INTERFACE_ID,
361         .bNumEndpoints =        2,
362         .bInterfaceClass =      USB_CLASS_CDC_DATA,
363         .bInterfaceSubClass =   0,
364         .bInterfaceProtocol =   0,
365         .iInterface =           GS_DATA_STR_ID,
366 };
367
368 static const struct usb_interface_descriptor gs_control_interface_desc = {
369         .bLength =              USB_DT_INTERFACE_SIZE,
370         .bDescriptorType =      USB_DT_INTERFACE,
371         .bInterfaceNumber =     GS_CONTROL_INTERFACE_ID,
372         .bNumEndpoints =        1,
373         .bInterfaceClass =      USB_CLASS_COMM,
374         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
375         .bInterfaceProtocol =   USB_CDC_ACM_PROTO_AT_V25TER,
376         .iInterface =           GS_CONTROL_STR_ID,
377 };
378
379 static const struct usb_interface_descriptor gs_data_interface_desc = {
380         .bLength =              USB_DT_INTERFACE_SIZE,
381         .bDescriptorType =      USB_DT_INTERFACE,
382         .bInterfaceNumber =     GS_DATA_INTERFACE_ID,
383         .bNumEndpoints =        2,
384         .bInterfaceClass =      USB_CLASS_CDC_DATA,
385         .bInterfaceSubClass =   0,
386         .bInterfaceProtocol =   0,
387         .iInterface =           GS_DATA_STR_ID,
388 };
389
390 static const struct usb_cdc_header_desc gs_header_desc = {
391         .bLength =              sizeof(gs_header_desc),
392         .bDescriptorType =      USB_DT_CS_INTERFACE,
393         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
394         .bcdCDC =               __constant_cpu_to_le16(0x0110),
395 };
396
397 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
398         .bLength =              sizeof(gs_call_mgmt_descriptor),
399         .bDescriptorType =      USB_DT_CS_INTERFACE,
400         .bDescriptorSubType =   USB_CDC_CALL_MANAGEMENT_TYPE,
401         .bmCapabilities =       0,
402         .bDataInterface =       1,      /* index of data interface */
403 };
404
405 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
406         .bLength =              sizeof(gs_acm_descriptor),
407         .bDescriptorType =      USB_DT_CS_INTERFACE,
408         .bDescriptorSubType =   USB_CDC_ACM_TYPE,
409         .bmCapabilities =       0,
410 };
411
412 static const struct usb_cdc_union_desc gs_union_desc = {
413         .bLength =              sizeof(gs_union_desc),
414         .bDescriptorType =      USB_DT_CS_INTERFACE,
415         .bDescriptorSubType =   USB_CDC_UNION_TYPE,
416         .bMasterInterface0 =    0,      /* index of control interface */
417         .bSlaveInterface0 =     1,      /* index of data interface */
418 };
419
420 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
421         .bLength =              USB_DT_ENDPOINT_SIZE,
422         .bDescriptorType =      USB_DT_ENDPOINT,
423         .bEndpointAddress =     USB_DIR_IN,
424         .bmAttributes =         USB_ENDPOINT_XFER_INT,
425         .wMaxPacketSize =       __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
426         .bInterval =            1 << GS_LOG2_NOTIFY_INTERVAL,
427 };
428
429 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
430         .bLength =              USB_DT_ENDPOINT_SIZE,
431         .bDescriptorType =      USB_DT_ENDPOINT,
432         .bEndpointAddress =     USB_DIR_IN,
433         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
434 };
435
436 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
437         .bLength =              USB_DT_ENDPOINT_SIZE,
438         .bDescriptorType =      USB_DT_ENDPOINT,
439         .bEndpointAddress =     USB_DIR_OUT,
440         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
441 };
442
443 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
444         (struct usb_descriptor_header *) &gs_otg_descriptor,
445         (struct usb_descriptor_header *) &gs_bulk_interface_desc,
446         (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
447         (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
448         NULL,
449 };
450
451 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
452         (struct usb_descriptor_header *) &gs_otg_descriptor,
453         (struct usb_descriptor_header *) &gs_control_interface_desc,
454         (struct usb_descriptor_header *) &gs_header_desc,
455         (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
456         (struct usb_descriptor_header *) &gs_acm_descriptor,
457         (struct usb_descriptor_header *) &gs_union_desc,
458         (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
459         (struct usb_descriptor_header *) &gs_data_interface_desc,
460         (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
461         (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
462         NULL,
463 };
464
465 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
466         .bLength =              USB_DT_ENDPOINT_SIZE,
467         .bDescriptorType =      USB_DT_ENDPOINT,
468         .bEndpointAddress =     USB_DIR_IN,
469         .bmAttributes =         USB_ENDPOINT_XFER_INT,
470         .wMaxPacketSize =       __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
471         .bInterval =            GS_LOG2_NOTIFY_INTERVAL+4,
472 };
473
474 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
475         .bLength =              USB_DT_ENDPOINT_SIZE,
476         .bDescriptorType =      USB_DT_ENDPOINT,
477         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
478         .wMaxPacketSize =       __constant_cpu_to_le16(512),
479 };
480
481 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
482         .bLength =              USB_DT_ENDPOINT_SIZE,
483         .bDescriptorType =      USB_DT_ENDPOINT,
484         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
485         .wMaxPacketSize =       __constant_cpu_to_le16(512),
486 };
487
488 static struct usb_qualifier_descriptor gs_qualifier_desc = {
489         .bLength =              sizeof(struct usb_qualifier_descriptor),
490         .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
491         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
492         /* assumes ep0 uses the same value for both speeds ... */
493         .bNumConfigurations =   GS_NUM_CONFIGS,
494 };
495
496 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
497         (struct usb_descriptor_header *) &gs_otg_descriptor,
498         (struct usb_descriptor_header *) &gs_bulk_interface_desc,
499         (struct usb_descriptor_header *) &gs_highspeed_in_desc,
500         (struct usb_descriptor_header *) &gs_highspeed_out_desc,
501         NULL,
502 };
503
504 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
505         (struct usb_descriptor_header *) &gs_otg_descriptor,
506         (struct usb_descriptor_header *) &gs_control_interface_desc,
507         (struct usb_descriptor_header *) &gs_header_desc,
508         (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
509         (struct usb_descriptor_header *) &gs_acm_descriptor,
510         (struct usb_descriptor_header *) &gs_union_desc,
511         (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
512         (struct usb_descriptor_header *) &gs_data_interface_desc,
513         (struct usb_descriptor_header *) &gs_highspeed_in_desc,
514         (struct usb_descriptor_header *) &gs_highspeed_out_desc,
515         NULL,
516 };
517
518
519 /* Module */
520 MODULE_DESCRIPTION(GS_LONG_NAME);
521 MODULE_AUTHOR("Al Borchers");
522 MODULE_LICENSE("GPL");
523
524 #ifdef DEBUG
525 module_param(debug, int, S_IRUGO|S_IWUSR);
526 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
527 #endif
528
529 module_param(read_q_size, uint, S_IRUGO);
530 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
531
532 module_param(write_q_size, uint, S_IRUGO);
533 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
534
535 module_param(write_buf_size, uint, S_IRUGO);
536 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
537
538 module_param(use_acm, uint, S_IRUGO);
539 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
540
541 module_init(gs_module_init);
542 module_exit(gs_module_exit);
543
544 /*
545 *  gs_module_init
546 *
547 *  Register as a USB gadget driver and a tty driver.
548 */
549 static int __init gs_module_init(void)
550 {
551         int i;
552         int retval;
553
554         retval = usb_gadget_register_driver(&gs_gadget_driver);
555         if (retval) {
556                 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
557                 return retval;
558         }
559
560         gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
561         if (!gs_tty_driver)
562                 return -ENOMEM;
563         gs_tty_driver->owner = THIS_MODULE;
564         gs_tty_driver->driver_name = GS_SHORT_NAME;
565         gs_tty_driver->name = "ttygs";
566         gs_tty_driver->major = GS_MAJOR;
567         gs_tty_driver->minor_start = GS_MINOR_START;
568         gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
569         gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
570         gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
571         gs_tty_driver->init_termios = tty_std_termios;
572         gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
573         tty_set_operations(gs_tty_driver, &gs_tty_ops);
574
575         for (i=0; i < GS_NUM_PORTS; i++)
576                 mutex_init(&gs_open_close_lock[i]);
577
578         retval = tty_register_driver(gs_tty_driver);
579         if (retval) {
580                 usb_gadget_unregister_driver(&gs_gadget_driver);
581                 put_tty_driver(gs_tty_driver);
582                 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
583                 return retval;
584         }
585
586         printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
587         return 0;
588 }
589
590 /*
591 * gs_module_exit
592 *
593 * Unregister as a tty driver and a USB gadget driver.
594 */
595 static void __exit gs_module_exit(void)
596 {
597         tty_unregister_driver(gs_tty_driver);
598         put_tty_driver(gs_tty_driver);
599         usb_gadget_unregister_driver(&gs_gadget_driver);
600
601         printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
602 }
603
604 /* TTY Driver */
605
606 /*
607  * gs_open
608  */
609 static int gs_open(struct tty_struct *tty, struct file *file)
610 {
611         int port_num;
612         unsigned long flags;
613         struct gs_port *port;
614         struct gs_dev *dev;
615         struct gs_buf *buf;
616         struct mutex *mtx;
617         int ret;
618
619         port_num = tty->index;
620
621         gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
622
623         if (port_num < 0 || port_num >= GS_NUM_PORTS) {
624                 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
625                         port_num, tty, file);
626                 return -ENODEV;
627         }
628
629         dev = gs_device;
630
631         if (dev == NULL) {
632                 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
633                         port_num, tty, file);
634                 return -ENODEV;
635         }
636
637         mtx = &gs_open_close_lock[port_num];
638         if (mutex_lock_interruptible(mtx)) {
639                 printk(KERN_ERR
640                 "gs_open: (%d,%p,%p) interrupted waiting for mutex\n",
641                         port_num, tty, file);
642                 return -ERESTARTSYS;
643         }
644
645         spin_lock_irqsave(&dev->dev_lock, flags);
646
647         if (dev->dev_config == GS_NO_CONFIG_ID) {
648                 printk(KERN_ERR
649                         "gs_open: (%d,%p,%p) device is not connected\n",
650                         port_num, tty, file);
651                 ret = -ENODEV;
652                 goto exit_unlock_dev;
653         }
654
655         port = dev->dev_port[port_num];
656
657         if (port == NULL) {
658                 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
659                         port_num, tty, file);
660                 ret = -ENODEV;
661                 goto exit_unlock_dev;
662         }
663
664         spin_lock(&port->port_lock);
665         spin_unlock(&dev->dev_lock);
666
667         if (port->port_dev == NULL) {
668                 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
669                         port_num, tty, file);
670                 ret = -EIO;
671                 goto exit_unlock_port;
672         }
673
674         if (port->port_open_count > 0) {
675                 ++port->port_open_count;
676                 gs_debug("gs_open: (%d,%p,%p) already open\n",
677                         port_num, tty, file);
678                 ret = 0;
679                 goto exit_unlock_port;
680         }
681
682         tty->driver_data = NULL;
683
684         /* mark port as in use, we can drop port lock and sleep if necessary */
685         port->port_in_use = 1;
686
687         /* allocate write buffer on first open */
688         if (port->port_write_buf == NULL) {
689                 spin_unlock_irqrestore(&port->port_lock, flags);
690                 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
691                 spin_lock_irqsave(&port->port_lock, flags);
692
693                 /* might have been disconnected while asleep, check */
694                 if (port->port_dev == NULL) {
695                         printk(KERN_ERR
696                                 "gs_open: (%d,%p,%p) port disconnected (2)\n",
697                                 port_num, tty, file);
698                         port->port_in_use = 0;
699                         ret = -EIO;
700                         goto exit_unlock_port;
701                 }
702
703                 if ((port->port_write_buf=buf) == NULL) {
704                         printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
705                                 port_num, tty, file);
706                         port->port_in_use = 0;
707                         ret = -ENOMEM;
708                         goto exit_unlock_port;
709                 }
710
711         }
712
713         /* wait for carrier detect (not implemented) */
714
715         /* might have been disconnected while asleep, check */
716         if (port->port_dev == NULL) {
717                 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
718                         port_num, tty, file);
719                 port->port_in_use = 0;
720                 ret = -EIO;
721                 goto exit_unlock_port;
722         }
723
724         tty->driver_data = port;
725         port->port_tty = tty;
726         port->port_open_count = 1;
727         port->port_in_use = 0;
728
729         gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
730
731         ret = 0;
732
733 exit_unlock_port:
734         spin_unlock_irqrestore(&port->port_lock, flags);
735         mutex_unlock(mtx);
736         return ret;
737
738 exit_unlock_dev:
739         spin_unlock_irqrestore(&dev->dev_lock, flags);
740         mutex_unlock(mtx);
741         return ret;
742
743 }
744
745 /*
746  * gs_close
747  */
748
749 #define GS_WRITE_FINISHED_EVENT_SAFELY(p)                       \
750 ({                                                              \
751         int cond;                                               \
752                                                                 \
753         spin_lock_irq(&(p)->port_lock);                         \
754         cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \
755         spin_unlock_irq(&(p)->port_lock);                       \
756         cond;                                                   \
757 })
758
759 static void gs_close(struct tty_struct *tty, struct file *file)
760 {
761         struct gs_port *port = tty->driver_data;
762         struct mutex *mtx;
763
764         if (port == NULL) {
765                 printk(KERN_ERR "gs_close: NULL port pointer\n");
766                 return;
767         }
768
769         gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
770
771         mtx = &gs_open_close_lock[port->port_num];
772         mutex_lock(mtx);
773
774         spin_lock_irq(&port->port_lock);
775
776         if (port->port_open_count == 0) {
777                 printk(KERN_ERR
778                         "gs_close: (%d,%p,%p) port is already closed\n",
779                         port->port_num, tty, file);
780                 goto exit;
781         }
782
783         if (port->port_open_count > 1) {
784                 --port->port_open_count;
785                 goto exit;
786         }
787
788         /* free disconnected port on final close */
789         if (port->port_dev == NULL) {
790                 kfree(port);
791                 goto exit;
792         }
793
794         /* mark port as closed but in use, we can drop port lock */
795         /* and sleep if necessary */
796         port->port_in_use = 1;
797         port->port_open_count = 0;
798
799         /* wait for write buffer to drain, or */
800         /* at most GS_CLOSE_TIMEOUT seconds */
801         if (gs_buf_data_avail(port->port_write_buf) > 0) {
802                 spin_unlock_irq(&port->port_lock);
803                 wait_event_interruptible_timeout(port->port_write_wait,
804                                         GS_WRITE_FINISHED_EVENT_SAFELY(port),
805                                         GS_CLOSE_TIMEOUT * HZ);
806                 spin_lock_irq(&port->port_lock);
807         }
808
809         /* free disconnected port on final close */
810         /* (might have happened during the above sleep) */
811         if (port->port_dev == NULL) {
812                 kfree(port);
813                 goto exit;
814         }
815
816         gs_buf_clear(port->port_write_buf);
817
818         tty->driver_data = NULL;
819         port->port_tty = NULL;
820         port->port_in_use = 0;
821
822         gs_debug("gs_close: (%d,%p,%p) completed\n",
823                 port->port_num, tty, file);
824
825 exit:
826         spin_unlock_irq(&port->port_lock);
827         mutex_unlock(mtx);
828 }
829
830 /*
831  * gs_write
832  */
833 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
834 {
835         unsigned long flags;
836         struct gs_port *port = tty->driver_data;
837         int ret;
838
839         if (port == NULL) {
840                 printk(KERN_ERR "gs_write: NULL port pointer\n");
841                 return -EIO;
842         }
843
844         gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
845                 count);
846
847         if (count == 0)
848                 return 0;
849
850         spin_lock_irqsave(&port->port_lock, flags);
851
852         if (port->port_dev == NULL) {
853                 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
854                         port->port_num, tty);
855                 ret = -EIO;
856                 goto exit;
857         }
858
859         if (port->port_open_count == 0) {
860                 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
861                         port->port_num, tty);
862                 ret = -EBADF;
863                 goto exit;
864         }
865
866         count = gs_buf_put(port->port_write_buf, buf, count);
867
868         spin_unlock_irqrestore(&port->port_lock, flags);
869
870         gs_send(gs_device);
871
872         gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
873                 count);
874
875         return count;
876
877 exit:
878         spin_unlock_irqrestore(&port->port_lock, flags);
879         return ret;
880 }
881
882 /*
883  * gs_put_char
884  */
885 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
886 {
887         unsigned long flags;
888         struct gs_port *port = tty->driver_data;
889
890         if (port == NULL) {
891                 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
892                 return;
893         }
894
895         gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
896                 port->port_num, tty, ch, __builtin_return_address(0));
897
898         spin_lock_irqsave(&port->port_lock, flags);
899
900         if (port->port_dev == NULL) {
901                 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
902                         port->port_num, tty);
903                 goto exit;
904         }
905
906         if (port->port_open_count == 0) {
907                 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
908                         port->port_num, tty);
909                 goto exit;
910         }
911
912         gs_buf_put(port->port_write_buf, &ch, 1);
913
914 exit:
915         spin_unlock_irqrestore(&port->port_lock, flags);
916 }
917
918 /*
919  * gs_flush_chars
920  */
921 static void gs_flush_chars(struct tty_struct *tty)
922 {
923         unsigned long flags;
924         struct gs_port *port = tty->driver_data;
925
926         if (port == NULL) {
927                 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
928                 return;
929         }
930
931         gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
932
933         spin_lock_irqsave(&port->port_lock, flags);
934
935         if (port->port_dev == NULL) {
936                 printk(KERN_ERR
937                         "gs_flush_chars: (%d,%p) port is not connected\n",
938                         port->port_num, tty);
939                 goto exit;
940         }
941
942         if (port->port_open_count == 0) {
943                 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
944                         port->port_num, tty);
945                 goto exit;
946         }
947
948         spin_unlock_irqrestore(&port->port_lock, flags);
949
950         gs_send(gs_device);
951
952         return;
953
954 exit:
955         spin_unlock_irqrestore(&port->port_lock, flags);
956 }
957
958 /*
959  * gs_write_room
960  */
961 static int gs_write_room(struct tty_struct *tty)
962 {
963
964         int room = 0;
965         unsigned long flags;
966         struct gs_port *port = tty->driver_data;
967
968
969         if (port == NULL)
970                 return 0;
971
972         spin_lock_irqsave(&port->port_lock, flags);
973
974         if (port->port_dev != NULL && port->port_open_count > 0
975         && port->port_write_buf != NULL)
976                 room = gs_buf_space_avail(port->port_write_buf);
977
978         spin_unlock_irqrestore(&port->port_lock, flags);
979
980         gs_debug("gs_write_room: (%d,%p) room=%d\n",
981                 port->port_num, tty, room);
982
983         return room;
984 }
985
986 /*
987  * gs_chars_in_buffer
988  */
989 static int gs_chars_in_buffer(struct tty_struct *tty)
990 {
991         int chars = 0;
992         unsigned long flags;
993         struct gs_port *port = tty->driver_data;
994
995         if (port == NULL)
996                 return 0;
997
998         spin_lock_irqsave(&port->port_lock, flags);
999
1000         if (port->port_dev != NULL && port->port_open_count > 0
1001         && port->port_write_buf != NULL)
1002                 chars = gs_buf_data_avail(port->port_write_buf);
1003
1004         spin_unlock_irqrestore(&port->port_lock, flags);
1005
1006         gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1007                 port->port_num, tty, chars);
1008
1009         return chars;
1010 }
1011
1012 /*
1013  * gs_throttle
1014  */
1015 static void gs_throttle(struct tty_struct *tty)
1016 {
1017 }
1018
1019 /*
1020  * gs_unthrottle
1021  */
1022 static void gs_unthrottle(struct tty_struct *tty)
1023 {
1024 }
1025
1026 /*
1027  * gs_break
1028  */
1029 static void gs_break(struct tty_struct *tty, int break_state)
1030 {
1031 }
1032
1033 /*
1034  * gs_ioctl
1035  */
1036 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1037 {
1038         struct gs_port *port = tty->driver_data;
1039
1040         if (port == NULL) {
1041                 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1042                 return -EIO;
1043         }
1044
1045         gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1046                 port->port_num, tty, file, cmd, arg);
1047
1048         /* handle ioctls */
1049
1050         /* could not handle ioctl */
1051         return -ENOIOCTLCMD;
1052 }
1053
1054 /*
1055  * gs_set_termios
1056  */
1057 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
1058 {
1059 }
1060
1061 /*
1062 * gs_send
1063 *
1064 * This function finds available write requests, calls
1065 * gs_send_packet to fill these packets with data, and
1066 * continues until either there are no more write requests
1067 * available or no more data to send.  This function is
1068 * run whenever data arrives or write requests are available.
1069 */
1070 static int gs_send(struct gs_dev *dev)
1071 {
1072         int ret,len;
1073         unsigned long flags;
1074         struct usb_ep *ep;
1075         struct usb_request *req;
1076         struct gs_req_entry *req_entry;
1077
1078         if (dev == NULL) {
1079                 printk(KERN_ERR "gs_send: NULL device pointer\n");
1080                 return -ENODEV;
1081         }
1082
1083         spin_lock_irqsave(&dev->dev_lock, flags);
1084
1085         ep = dev->dev_in_ep;
1086
1087         while(!list_empty(&dev->dev_req_list)) {
1088
1089                 req_entry = list_entry(dev->dev_req_list.next,
1090                         struct gs_req_entry, re_entry);
1091
1092                 req = req_entry->re_req;
1093
1094                 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1095
1096                 if (len > 0) {
1097                         gs_debug_level(3, "gs_send: len=%d, 0x%2.2x "
1098                                         "0x%2.2x 0x%2.2x ...\n", len,
1099                                         *((unsigned char *)req->buf),
1100                                         *((unsigned char *)req->buf+1),
1101                                         *((unsigned char *)req->buf+2));
1102                         list_del(&req_entry->re_entry);
1103                         req->length = len;
1104                         spin_unlock_irqrestore(&dev->dev_lock, flags);
1105                         if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1106                                 printk(KERN_ERR
1107                                 "gs_send: cannot queue read request, ret=%d\n",
1108                                         ret);
1109                                 spin_lock_irqsave(&dev->dev_lock, flags);
1110                                 break;
1111                         }
1112                         spin_lock_irqsave(&dev->dev_lock, flags);
1113                 } else {
1114                         break;
1115                 }
1116
1117         }
1118
1119         spin_unlock_irqrestore(&dev->dev_lock, flags);
1120
1121         return 0;
1122 }
1123
1124 /*
1125  * gs_send_packet
1126  *
1127  * If there is data to send, a packet is built in the given
1128  * buffer and the size is returned.  If there is no data to
1129  * send, 0 is returned.  If there is any error a negative
1130  * error number is returned.
1131  *
1132  * Called during USB completion routine, on interrupt time.
1133  *
1134  * We assume that disconnect will not happen until all completion
1135  * routines have completed, so we can assume that the dev_port
1136  * array does not change during the lifetime of this function.
1137  */
1138 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1139 {
1140         unsigned int len;
1141         struct gs_port *port;
1142
1143         /* TEMPORARY -- only port 0 is supported right now */
1144         port = dev->dev_port[0];
1145
1146         if (port == NULL) {
1147                 printk(KERN_ERR
1148                         "gs_send_packet: port=%d, NULL port pointer\n",
1149                         0);
1150                 return -EIO;
1151         }
1152
1153         spin_lock(&port->port_lock);
1154
1155         len = gs_buf_data_avail(port->port_write_buf);
1156         if (len < size)
1157                 size = len;
1158
1159         if (size == 0)
1160                 goto exit;
1161
1162         size = gs_buf_get(port->port_write_buf, packet, size);
1163
1164         if (port->port_tty)
1165                 wake_up_interruptible(&port->port_tty->write_wait);
1166
1167 exit:
1168         spin_unlock(&port->port_lock);
1169         return size;
1170 }
1171
1172 /*
1173  * gs_recv_packet
1174  *
1175  * Called for each USB packet received.  Reads the packet
1176  * header and stuffs the data in the appropriate tty buffer.
1177  * Returns 0 if successful, or a negative error number.
1178  *
1179  * Called during USB completion routine, on interrupt time.
1180  *
1181  * We assume that disconnect will not happen until all completion
1182  * routines have completed, so we can assume that the dev_port
1183  * array does not change during the lifetime of this function.
1184  */
1185 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1186 {
1187         unsigned int len;
1188         struct gs_port *port;
1189         int ret;
1190         struct tty_struct *tty;
1191
1192         /* TEMPORARY -- only port 0 is supported right now */
1193         port = dev->dev_port[0];
1194
1195         if (port == NULL) {
1196                 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1197                         port->port_num);
1198                 return -EIO;
1199         }
1200
1201         spin_lock(&port->port_lock);
1202
1203         if (port->port_open_count == 0) {
1204                 printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",
1205                         port->port_num);
1206                 ret = -EIO;
1207                 goto exit;
1208         }
1209
1210
1211         tty = port->port_tty;
1212
1213         if (tty == NULL) {
1214                 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1215                         port->port_num);
1216                 ret = -EIO;
1217                 goto exit;
1218         }
1219
1220         if (port->port_tty->magic != TTY_MAGIC) {
1221                 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1222                         port->port_num);
1223                 ret = -EIO;
1224                 goto exit;
1225         }
1226
1227         len = tty_buffer_request_room(tty, size);
1228         if (len > 0) {
1229                 tty_insert_flip_string(tty, packet, len);
1230                 tty_flip_buffer_push(port->port_tty);
1231                 wake_up_interruptible(&port->port_tty->read_wait);
1232         }
1233         ret = 0;
1234 exit:
1235         spin_unlock(&port->port_lock);
1236         return ret;
1237 }
1238
1239 /*
1240 * gs_read_complete
1241 */
1242 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1243 {
1244         int ret;
1245         struct gs_dev *dev = ep->driver_data;
1246
1247         if (dev == NULL) {
1248                 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1249                 return;
1250         }
1251
1252         switch(req->status) {
1253         case 0:
1254                 /* normal completion */
1255                 gs_recv_packet(dev, req->buf, req->actual);
1256 requeue:
1257                 req->length = ep->maxpacket;
1258                 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1259                         printk(KERN_ERR
1260                         "gs_read_complete: cannot queue read request, ret=%d\n",
1261                                 ret);
1262                 }
1263                 break;
1264
1265         case -ESHUTDOWN:
1266                 /* disconnect */
1267                 gs_debug("gs_read_complete: shutdown\n");
1268                 gs_free_req(ep, req);
1269                 break;
1270
1271         default:
1272                 /* unexpected */
1273                 printk(KERN_ERR
1274                 "gs_read_complete: unexpected status error, status=%d\n",
1275                         req->status);
1276                 goto requeue;
1277                 break;
1278         }
1279 }
1280
1281 /*
1282 * gs_write_complete
1283 */
1284 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1285 {
1286         struct gs_dev *dev = ep->driver_data;
1287         struct gs_req_entry *gs_req = req->context;
1288
1289         if (dev == NULL) {
1290                 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1291                 return;
1292         }
1293
1294         switch(req->status) {
1295         case 0:
1296                 /* normal completion */
1297 requeue:
1298                 if (gs_req == NULL) {
1299                         printk(KERN_ERR
1300                                 "gs_write_complete: NULL request pointer\n");
1301                         return;
1302                 }
1303
1304                 spin_lock(&dev->dev_lock);
1305                 list_add(&gs_req->re_entry, &dev->dev_req_list);
1306                 spin_unlock(&dev->dev_lock);
1307
1308                 gs_send(dev);
1309
1310                 break;
1311
1312         case -ESHUTDOWN:
1313                 /* disconnect */
1314                 gs_debug("gs_write_complete: shutdown\n");
1315                 gs_free_req(ep, req);
1316                 break;
1317
1318         default:
1319                 printk(KERN_ERR
1320                 "gs_write_complete: unexpected status error, status=%d\n",
1321                         req->status);
1322                 goto requeue;
1323                 break;
1324         }
1325 }
1326
1327 /* Gadget Driver */
1328
1329 /*
1330  * gs_bind
1331  *
1332  * Called on module load.  Allocates and initializes the device
1333  * structure and a control request.
1334  */
1335 static int __init gs_bind(struct usb_gadget *gadget)
1336 {
1337         int ret;
1338         struct usb_ep *ep;
1339         struct gs_dev *dev;
1340         int gcnum;
1341
1342         /* Some controllers can't support CDC ACM:
1343          * - sh doesn't support multiple interfaces or configs;
1344          * - sa1100 doesn't have a third interrupt endpoint
1345          */
1346         if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1347                 use_acm = 0;
1348
1349         gcnum = usb_gadget_controller_number(gadget);
1350         if (gcnum >= 0)
1351                 gs_device_desc.bcdDevice =
1352                                 cpu_to_le16(GS_VERSION_NUM | gcnum);
1353         else {
1354                 printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
1355                         gadget->name);
1356                 /* unrecognized, but safe unless bulk is REALLY quirky */
1357                 gs_device_desc.bcdDevice =
1358                         __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1359         }
1360
1361         usb_ep_autoconfig_reset(gadget);
1362
1363         ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1364         if (!ep)
1365                 goto autoconf_fail;
1366         EP_IN_NAME = ep->name;
1367         ep->driver_data = ep;   /* claim the endpoint */
1368
1369         ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1370         if (!ep)
1371                 goto autoconf_fail;
1372         EP_OUT_NAME = ep->name;
1373         ep->driver_data = ep;   /* claim the endpoint */
1374
1375         if (use_acm) {
1376                 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1377                 if (!ep) {
1378                         printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);
1379                         goto autoconf_fail;
1380                 }
1381                 gs_device_desc.idProduct = __constant_cpu_to_le16(
1382                                                 GS_CDC_PRODUCT_ID),
1383                 EP_NOTIFY_NAME = ep->name;
1384                 ep->driver_data = ep;   /* claim the endpoint */
1385         }
1386
1387         gs_device_desc.bDeviceClass = use_acm
1388                 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1389         gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1390
1391         if (gadget_is_dualspeed(gadget)) {
1392                 gs_qualifier_desc.bDeviceClass = use_acm
1393                         ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1394                 /* assume ep0 uses the same packet size for both speeds */
1395                 gs_qualifier_desc.bMaxPacketSize0 =
1396                         gs_device_desc.bMaxPacketSize0;
1397                 /* assume endpoints are dual-speed */
1398                 gs_highspeed_notify_desc.bEndpointAddress =
1399                         gs_fullspeed_notify_desc.bEndpointAddress;
1400                 gs_highspeed_in_desc.bEndpointAddress =
1401                         gs_fullspeed_in_desc.bEndpointAddress;
1402                 gs_highspeed_out_desc.bEndpointAddress =
1403                         gs_fullspeed_out_desc.bEndpointAddress;
1404         }
1405
1406         usb_gadget_set_selfpowered(gadget);
1407
1408         if (gadget_is_otg(gadget)) {
1409                 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1410                 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1411                 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1412         }
1413
1414         gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1415         if (dev == NULL)
1416                 return -ENOMEM;
1417
1418         snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1419                 init_utsname()->sysname, init_utsname()->release,
1420                 gadget->name);
1421
1422         dev->dev_gadget = gadget;
1423         spin_lock_init(&dev->dev_lock);
1424         INIT_LIST_HEAD(&dev->dev_req_list);
1425         set_gadget_data(gadget, dev);
1426
1427         if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1428                 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1429                 gs_unbind(gadget);
1430                 return ret;
1431         }
1432
1433         /* preallocate control response and buffer */
1434         dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1435                 GFP_KERNEL);
1436         if (dev->dev_ctrl_req == NULL) {
1437                 gs_unbind(gadget);
1438                 return -ENOMEM;
1439         }
1440         dev->dev_ctrl_req->complete = gs_setup_complete;
1441
1442         gadget->ep0->driver_data = dev;
1443
1444         printk(KERN_INFO "gs_bind: %s %s bound\n",
1445                 GS_LONG_NAME, GS_VERSION_STR);
1446
1447         return 0;
1448
1449 autoconf_fail:
1450         printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);
1451         return -ENODEV;
1452 }
1453
1454 /*
1455  * gs_unbind
1456  *
1457  * Called on module unload.  Frees the control request and device
1458  * structure.
1459  */
1460 static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1461 {
1462         struct gs_dev *dev = get_gadget_data(gadget);
1463
1464         gs_device = NULL;
1465
1466         /* read/write requests already freed, only control request remains */
1467         if (dev != NULL) {
1468                 if (dev->dev_ctrl_req != NULL) {
1469                         gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1470                         dev->dev_ctrl_req = NULL;
1471                 }
1472                 gs_free_ports(dev);
1473                 kfree(dev);
1474                 set_gadget_data(gadget, NULL);
1475         }
1476
1477         printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1478                 GS_VERSION_STR);
1479 }
1480
1481 /*
1482  * gs_setup
1483  *
1484  * Implements all the control endpoint functionality that's not
1485  * handled in hardware or the hardware driver.
1486  *
1487  * Returns the size of the data sent to the host, or a negative
1488  * error number.
1489  */
1490 static int gs_setup(struct usb_gadget *gadget,
1491         const struct usb_ctrlrequest *ctrl)
1492 {
1493         int ret = -EOPNOTSUPP;
1494         struct gs_dev *dev = get_gadget_data(gadget);
1495         struct usb_request *req = dev->dev_ctrl_req;
1496         u16 wIndex = le16_to_cpu(ctrl->wIndex);
1497         u16 wValue = le16_to_cpu(ctrl->wValue);
1498         u16 wLength = le16_to_cpu(ctrl->wLength);
1499
1500         switch (ctrl->bRequestType & USB_TYPE_MASK) {
1501         case USB_TYPE_STANDARD:
1502                 ret = gs_setup_standard(gadget,ctrl);
1503                 break;
1504
1505         case USB_TYPE_CLASS:
1506                 ret = gs_setup_class(gadget,ctrl);
1507                 break;
1508
1509         default:
1510                 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1511                         ctrl->bRequestType, ctrl->bRequest,
1512                         wValue, wIndex, wLength);
1513                 break;
1514         }
1515
1516         /* respond with data transfer before status phase? */
1517         if (ret >= 0) {
1518                 req->length = ret;
1519                 req->zero = ret < wLength
1520                                 && (ret % gadget->ep0->maxpacket) == 0;
1521                 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1522                 if (ret < 0) {
1523                         printk(KERN_ERR "gs_setup: cannot queue response, ret=%d\n",
1524                                 ret);
1525                         req->status = 0;
1526                         gs_setup_complete(gadget->ep0, req);
1527                 }
1528         }
1529
1530         /* device either stalls (ret < 0) or reports success */
1531         return ret;
1532 }
1533
1534 static int gs_setup_standard(struct usb_gadget *gadget,
1535         const struct usb_ctrlrequest *ctrl)
1536 {
1537         int ret = -EOPNOTSUPP;
1538         struct gs_dev *dev = get_gadget_data(gadget);
1539         struct usb_request *req = dev->dev_ctrl_req;
1540         u16 wIndex = le16_to_cpu(ctrl->wIndex);
1541         u16 wValue = le16_to_cpu(ctrl->wValue);
1542         u16 wLength = le16_to_cpu(ctrl->wLength);
1543
1544         switch (ctrl->bRequest) {
1545         case USB_REQ_GET_DESCRIPTOR:
1546                 if (ctrl->bRequestType != USB_DIR_IN)
1547                         break;
1548
1549                 switch (wValue >> 8) {
1550                 case USB_DT_DEVICE:
1551                         ret = min(wLength,
1552                                 (u16)sizeof(struct usb_device_descriptor));
1553                         memcpy(req->buf, &gs_device_desc, ret);
1554                         break;
1555
1556                 case USB_DT_DEVICE_QUALIFIER:
1557                         if (!gadget_is_dualspeed(gadget))
1558                                 break;
1559                         ret = min(wLength,
1560                                 (u16)sizeof(struct usb_qualifier_descriptor));
1561                         memcpy(req->buf, &gs_qualifier_desc, ret);
1562                         break;
1563
1564                 case USB_DT_OTHER_SPEED_CONFIG:
1565                         if (!gadget_is_dualspeed(gadget))
1566                                 break;
1567                         /* fall through */
1568                 case USB_DT_CONFIG:
1569                         ret = gs_build_config_buf(req->buf, gadget,
1570                                 wValue >> 8, wValue & 0xff,
1571                                 gadget_is_otg(gadget));
1572                         if (ret >= 0)
1573                                 ret = min(wLength, (u16)ret);
1574                         break;
1575
1576                 case USB_DT_STRING:
1577                         /* wIndex == language code. */
1578                         ret = usb_gadget_get_string(&gs_string_table,
1579                                 wValue & 0xff, req->buf);
1580                         if (ret >= 0)
1581                                 ret = min(wLength, (u16)ret);
1582                         break;
1583                 }
1584                 break;
1585
1586         case USB_REQ_SET_CONFIGURATION:
1587                 if (ctrl->bRequestType != 0)
1588                         break;
1589                 spin_lock(&dev->dev_lock);
1590                 ret = gs_set_config(dev, wValue);
1591                 spin_unlock(&dev->dev_lock);
1592                 break;
1593
1594         case USB_REQ_GET_CONFIGURATION:
1595                 if (ctrl->bRequestType != USB_DIR_IN)
1596                         break;
1597                 *(u8 *)req->buf = dev->dev_config;
1598                 ret = min(wLength, (u16)1);
1599                 break;
1600
1601         case USB_REQ_SET_INTERFACE:
1602                 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1603                                 || !dev->dev_config
1604                                 || wIndex >= GS_MAX_NUM_INTERFACES)
1605                         break;
1606                 if (dev->dev_config == GS_BULK_CONFIG_ID
1607                                 && wIndex != GS_BULK_INTERFACE_ID)
1608                         break;
1609                 /* no alternate interface settings */
1610                 if (wValue != 0)
1611                         break;
1612                 spin_lock(&dev->dev_lock);
1613                 /* PXA hardware partially handles SET_INTERFACE;
1614                  * we need to kluge around that interference.  */
1615                 if (gadget_is_pxa(gadget)) {
1616                         ret = gs_set_config(dev, use_acm ?
1617                                 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1618                         goto set_interface_done;
1619                 }
1620                 if (dev->dev_config != GS_BULK_CONFIG_ID
1621                                 && wIndex == GS_CONTROL_INTERFACE_ID) {
1622                         if (dev->dev_notify_ep) {
1623                                 usb_ep_disable(dev->dev_notify_ep);
1624                                 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1625                         }
1626                 } else {
1627                         usb_ep_disable(dev->dev_in_ep);
1628                         usb_ep_disable(dev->dev_out_ep);
1629                         usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1630                         usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1631                 }
1632                 ret = 0;
1633 set_interface_done:
1634                 spin_unlock(&dev->dev_lock);
1635                 break;
1636
1637         case USB_REQ_GET_INTERFACE:
1638                 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1639                 || dev->dev_config == GS_NO_CONFIG_ID)
1640                         break;
1641                 if (wIndex >= GS_MAX_NUM_INTERFACES
1642                                 || (dev->dev_config == GS_BULK_CONFIG_ID
1643                                 && wIndex != GS_BULK_INTERFACE_ID)) {
1644                         ret = -EDOM;
1645                         break;
1646                 }
1647                 /* no alternate interface settings */
1648                 *(u8 *)req->buf = 0;
1649                 ret = min(wLength, (u16)1);
1650                 break;
1651
1652         default:
1653                 printk(KERN_ERR "gs_setup: unknown standard request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1654                         ctrl->bRequestType, ctrl->bRequest,
1655                         wValue, wIndex, wLength);
1656                 break;
1657         }
1658
1659         return ret;
1660 }
1661
1662 static int gs_setup_class(struct usb_gadget *gadget,
1663         const struct usb_ctrlrequest *ctrl)
1664 {
1665         int ret = -EOPNOTSUPP;
1666         struct gs_dev *dev = get_gadget_data(gadget);
1667         struct gs_port *port = dev->dev_port[0];        /* ACM only has one port */
1668         struct usb_request *req = dev->dev_ctrl_req;
1669         u16 wIndex = le16_to_cpu(ctrl->wIndex);
1670         u16 wValue = le16_to_cpu(ctrl->wValue);
1671         u16 wLength = le16_to_cpu(ctrl->wLength);
1672
1673         switch (ctrl->bRequest) {
1674         case USB_CDC_REQ_SET_LINE_CODING:
1675                 /* FIXME Submit req to read the data; have its completion
1676                  * handler copy that data to port->port_line_coding (iff
1677                  * it's valid) and maybe pass it on.  Until then, fail.
1678                  */
1679                 printk(KERN_WARNING "gs_setup: set_line_coding "
1680                                 "unuspported\n");
1681                 break;
1682
1683         case USB_CDC_REQ_GET_LINE_CODING:
1684                 port = dev->dev_port[0];        /* ACM only has one port */
1685                 ret = min(wLength,
1686                         (u16)sizeof(struct usb_cdc_line_coding));
1687                 if (port) {
1688                         spin_lock(&port->port_lock);
1689                         memcpy(req->buf, &port->port_line_coding, ret);
1690                         spin_unlock(&port->port_lock);
1691                 }
1692                 break;
1693
1694         case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1695                 /* FIXME Submit req to read the data; have its completion
1696                  * handler use that to set the state (iff it's valid) and
1697                  * maybe pass it on.  Until then, fail.
1698                  */
1699                 printk(KERN_WARNING "gs_setup: set_control_line_state "
1700                                 "unuspported\n");
1701                 break;
1702
1703         default:
1704                 printk(KERN_ERR "gs_setup: unknown class request, "
1705                                 "type=%02x, request=%02x, value=%04x, "
1706                                 "index=%04x, length=%d\n",
1707                         ctrl->bRequestType, ctrl->bRequest,
1708                         wValue, wIndex, wLength);
1709                 break;
1710         }
1711
1712         return ret;
1713 }
1714
1715 /*
1716  * gs_setup_complete
1717  */
1718 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1719 {
1720         if (req->status || req->actual != req->length) {
1721                 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1722                         req->status, req->actual, req->length);
1723         }
1724 }
1725
1726 /*
1727  * gs_disconnect
1728  *
1729  * Called when the device is disconnected.  Frees the closed
1730  * ports and disconnects open ports.  Open ports will be freed
1731  * on close.  Then reallocates the ports for the next connection.
1732  */
1733 static void gs_disconnect(struct usb_gadget *gadget)
1734 {
1735         unsigned long flags;
1736         struct gs_dev *dev = get_gadget_data(gadget);
1737
1738         spin_lock_irqsave(&dev->dev_lock, flags);
1739
1740         gs_reset_config(dev);
1741
1742         /* free closed ports and disconnect open ports */
1743         /* (open ports will be freed when closed) */
1744         gs_free_ports(dev);
1745
1746         /* re-allocate ports for the next connection */
1747         if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1748                 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1749
1750         spin_unlock_irqrestore(&dev->dev_lock, flags);
1751
1752         printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1753 }
1754
1755 /*
1756  * gs_set_config
1757  *
1758  * Configures the device by enabling device specific
1759  * optimizations, setting up the endpoints, allocating
1760  * read and write requests and queuing read requests.
1761  *
1762  * The device lock must be held when calling this function.
1763  */
1764 static int gs_set_config(struct gs_dev *dev, unsigned config)
1765 {
1766         int i;
1767         int ret = 0;
1768         struct usb_gadget *gadget = dev->dev_gadget;
1769         struct usb_ep *ep;
1770         struct usb_endpoint_descriptor *ep_desc;
1771         struct usb_request *req;
1772         struct gs_req_entry *req_entry;
1773
1774         if (dev == NULL) {
1775                 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1776                 return 0;
1777         }
1778
1779         if (config == dev->dev_config)
1780                 return 0;
1781
1782         gs_reset_config(dev);
1783
1784         switch (config) {
1785         case GS_NO_CONFIG_ID:
1786                 return 0;
1787         case GS_BULK_CONFIG_ID:
1788                 if (use_acm)
1789                         return -EINVAL;
1790                 /* device specific optimizations */
1791                 if (gadget_is_net2280(gadget))
1792                         net2280_set_fifo_mode(gadget, 1);
1793                 break;
1794         case GS_ACM_CONFIG_ID:
1795                 if (!use_acm)
1796                         return -EINVAL;
1797                 /* device specific optimizations */
1798                 if (gadget_is_net2280(gadget))
1799                         net2280_set_fifo_mode(gadget, 1);
1800                 break;
1801         default:
1802                 return -EINVAL;
1803         }
1804
1805         dev->dev_config = config;
1806
1807         gadget_for_each_ep(ep, gadget) {
1808
1809                 if (EP_NOTIFY_NAME
1810                 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1811                         ep_desc = choose_ep_desc(gadget,
1812                                 &gs_highspeed_notify_desc,
1813                                 &gs_fullspeed_notify_desc);
1814                         ret = usb_ep_enable(ep,ep_desc);
1815                         if (ret == 0) {
1816                                 ep->driver_data = dev;
1817                                 dev->dev_notify_ep = ep;
1818                                 dev->dev_notify_ep_desc = ep_desc;
1819                         } else {
1820                                 printk(KERN_ERR "gs_set_config: cannot enable notify endpoint %s, ret=%d\n",
1821                                         ep->name, ret);
1822                                 goto exit_reset_config;
1823                         }
1824                 }
1825
1826                 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1827                         ep_desc = choose_ep_desc(gadget,
1828                                 &gs_highspeed_in_desc,
1829                                 &gs_fullspeed_in_desc);
1830                         ret = usb_ep_enable(ep,ep_desc);
1831                         if (ret == 0) {
1832                                 ep->driver_data = dev;
1833                                 dev->dev_in_ep = ep;
1834                                 dev->dev_in_ep_desc = ep_desc;
1835                         } else {
1836                                 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
1837                                         ep->name, ret);
1838                                 goto exit_reset_config;
1839                         }
1840                 }
1841
1842                 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1843                         ep_desc = choose_ep_desc(gadget,
1844                                 &gs_highspeed_out_desc,
1845                                 &gs_fullspeed_out_desc);
1846                         ret = usb_ep_enable(ep,ep_desc);
1847                         if (ret == 0) {
1848                                 ep->driver_data = dev;
1849                                 dev->dev_out_ep = ep;
1850                                 dev->dev_out_ep_desc = ep_desc;
1851                         } else {
1852                                 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
1853                                         ep->name, ret);
1854                                 goto exit_reset_config;
1855                         }
1856                 }
1857
1858         }
1859
1860         if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
1861         || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1862                 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
1863                 ret = -ENODEV;
1864                 goto exit_reset_config;
1865         }
1866
1867         /* allocate and queue read requests */
1868         ep = dev->dev_out_ep;
1869         for (i=0; i<read_q_size && ret == 0; i++) {
1870                 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1871                         req->complete = gs_read_complete;
1872                         if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1873                                 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
1874                                         ret);
1875                         }
1876                 } else {
1877                         printk(KERN_ERR "gs_set_config: cannot allocate read requests\n");
1878                         ret = -ENOMEM;
1879                         goto exit_reset_config;
1880                 }
1881         }
1882
1883         /* allocate write requests, and put on free list */
1884         ep = dev->dev_in_ep;
1885         for (i=0; i<write_q_size; i++) {
1886                 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1887                         req_entry->re_req->complete = gs_write_complete;
1888                         list_add(&req_entry->re_entry, &dev->dev_req_list);
1889                 } else {
1890                         printk(KERN_ERR "gs_set_config: cannot allocate write requests\n");
1891                         ret = -ENOMEM;
1892                         goto exit_reset_config;
1893                 }
1894         }
1895
1896         printk(KERN_INFO "gs_set_config: %s configured, %s speed %s config\n",
1897                 GS_LONG_NAME,
1898                 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1899                 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1900
1901         return 0;
1902
1903 exit_reset_config:
1904         gs_reset_config(dev);
1905         return ret;
1906 }
1907
1908 /*
1909  * gs_reset_config
1910  *
1911  * Mark the device as not configured, disable all endpoints,
1912  * which forces completion of pending I/O and frees queued
1913  * requests, and free the remaining write requests on the
1914  * free list.
1915  *
1916  * The device lock must be held when calling this function.
1917  */
1918 static void gs_reset_config(struct gs_dev *dev)
1919 {
1920         struct gs_req_entry *req_entry;
1921
1922         if (dev == NULL) {
1923                 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
1924                 return;
1925         }
1926
1927         if (dev->dev_config == GS_NO_CONFIG_ID)
1928                 return;
1929
1930         dev->dev_config = GS_NO_CONFIG_ID;
1931
1932         /* free write requests on the free list */
1933         while(!list_empty(&dev->dev_req_list)) {
1934                 req_entry = list_entry(dev->dev_req_list.next,
1935                         struct gs_req_entry, re_entry);
1936                 list_del(&req_entry->re_entry);
1937                 gs_free_req_entry(dev->dev_in_ep, req_entry);
1938         }
1939
1940         /* disable endpoints, forcing completion of pending i/o; */
1941         /* completion handlers free their requests in this case */
1942         if (dev->dev_notify_ep) {
1943                 usb_ep_disable(dev->dev_notify_ep);
1944                 dev->dev_notify_ep = NULL;
1945         }
1946         if (dev->dev_in_ep) {
1947                 usb_ep_disable(dev->dev_in_ep);
1948                 dev->dev_in_ep = NULL;
1949         }
1950         if (dev->dev_out_ep) {
1951                 usb_ep_disable(dev->dev_out_ep);
1952                 dev->dev_out_ep = NULL;
1953         }
1954 }
1955
1956 /*
1957  * gs_build_config_buf
1958  *
1959  * Builds the config descriptors in the given buffer and returns the
1960  * length, or a negative error number.
1961  */
1962 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
1963         u8 type, unsigned int index, int is_otg)
1964 {
1965         int len;
1966         int high_speed = 0;
1967         const struct usb_config_descriptor *config_desc;
1968         const struct usb_descriptor_header **function;
1969
1970         if (index >= gs_device_desc.bNumConfigurations)
1971                 return -EINVAL;
1972
1973         /* other speed switches high and full speed */
1974         if (gadget_is_dualspeed(g)) {
1975                 high_speed = (g->speed == USB_SPEED_HIGH);
1976                 if (type == USB_DT_OTHER_SPEED_CONFIG)
1977                         high_speed = !high_speed;
1978         }
1979
1980         if (use_acm) {
1981                 config_desc = &gs_acm_config_desc;
1982                 function = high_speed
1983                         ? gs_acm_highspeed_function
1984                         : gs_acm_fullspeed_function;
1985         } else {
1986                 config_desc = &gs_bulk_config_desc;
1987                 function = high_speed
1988                         ? gs_bulk_highspeed_function
1989                         : gs_bulk_fullspeed_function;
1990         }
1991
1992         /* for now, don't advertise srp-only devices */
1993         if (!is_otg)
1994                 function++;
1995
1996         len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
1997         if (len < 0)
1998                 return len;
1999
2000         ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2001
2002         return len;
2003 }
2004
2005 /*
2006  * gs_alloc_req
2007  *
2008  * Allocate a usb_request and its buffer.  Returns a pointer to the
2009  * usb_request or NULL if there is an error.
2010  */
2011 static struct usb_request *
2012 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
2013 {
2014         struct usb_request *req;
2015
2016         if (ep == NULL)
2017                 return NULL;
2018
2019         req = usb_ep_alloc_request(ep, kmalloc_flags);
2020
2021         if (req != NULL) {
2022                 req->length = len;
2023                 req->buf = kmalloc(len, kmalloc_flags);
2024                 if (req->buf == NULL) {
2025                         usb_ep_free_request(ep, req);
2026                         return NULL;
2027                 }
2028         }
2029
2030         return req;
2031 }
2032
2033 /*
2034  * gs_free_req
2035  *
2036  * Free a usb_request and its buffer.
2037  */
2038 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2039 {
2040         if (ep != NULL && req != NULL) {
2041                 kfree(req->buf);
2042                 usb_ep_free_request(ep, req);
2043         }
2044 }
2045
2046 /*
2047  * gs_alloc_req_entry
2048  *
2049  * Allocates a request and its buffer, using the given
2050  * endpoint, buffer len, and kmalloc flags.
2051  */
2052 static struct gs_req_entry *
2053 gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2054 {
2055         struct gs_req_entry     *req;
2056
2057         req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2058         if (req == NULL)
2059                 return NULL;
2060
2061         req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2062         if (req->re_req == NULL) {
2063                 kfree(req);
2064                 return NULL;
2065         }
2066
2067         req->re_req->context = req;
2068
2069         return req;
2070 }
2071
2072 /*
2073  * gs_free_req_entry
2074  *
2075  * Frees a request and its buffer.
2076  */
2077 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2078 {
2079         if (ep != NULL && req != NULL) {
2080                 if (req->re_req != NULL)
2081                         gs_free_req(ep, req->re_req);
2082                 kfree(req);
2083         }
2084 }
2085
2086 /*
2087  * gs_alloc_ports
2088  *
2089  * Allocate all ports and set the gs_dev struct to point to them.
2090  * Return 0 if successful, or a negative error number.
2091  *
2092  * The device lock is normally held when calling this function.
2093  */
2094 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2095 {
2096         int i;
2097         struct gs_port *port;
2098
2099         if (dev == NULL)
2100                 return -EIO;
2101
2102         for (i=0; i<GS_NUM_PORTS; i++) {
2103                 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2104                         return -ENOMEM;
2105
2106                 port->port_dev = dev;
2107                 port->port_num = i;
2108                 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
2109                 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2110                 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2111                 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2112                 spin_lock_init(&port->port_lock);
2113                 init_waitqueue_head(&port->port_write_wait);
2114
2115                 dev->dev_port[i] = port;
2116         }
2117
2118         return 0;
2119 }
2120
2121 /*
2122  * gs_free_ports
2123  *
2124  * Free all closed ports.  Open ports are disconnected by
2125  * freeing their write buffers, setting their device pointers
2126  * and the pointers to them in the device to NULL.  These
2127  * ports will be freed when closed.
2128  *
2129  * The device lock is normally held when calling this function.
2130  */
2131 static void gs_free_ports(struct gs_dev *dev)
2132 {
2133         int i;
2134         unsigned long flags;
2135         struct gs_port *port;
2136
2137         if (dev == NULL)
2138                 return;
2139
2140         for (i=0; i<GS_NUM_PORTS; i++) {
2141                 if ((port=dev->dev_port[i]) != NULL) {
2142                         dev->dev_port[i] = NULL;
2143
2144                         spin_lock_irqsave(&port->port_lock, flags);
2145
2146                         if (port->port_write_buf != NULL) {
2147                                 gs_buf_free(port->port_write_buf);
2148                                 port->port_write_buf = NULL;
2149                         }
2150
2151                         if (port->port_open_count > 0 || port->port_in_use) {
2152                                 port->port_dev = NULL;
2153                                 wake_up_interruptible(&port->port_write_wait);
2154                                 if (port->port_tty) {
2155                                         wake_up_interruptible(&port->port_tty->read_wait);
2156                                         wake_up_interruptible(&port->port_tty->write_wait);
2157                                 }
2158                                 spin_unlock_irqrestore(&port->port_lock, flags);
2159                         } else {
2160                                 spin_unlock_irqrestore(&port->port_lock, flags);
2161                                 kfree(port);
2162                         }
2163
2164                 }
2165         }
2166 }
2167
2168 /* Circular Buffer */
2169
2170 /*
2171  * gs_buf_alloc
2172  *
2173  * Allocate a circular buffer and all associated memory.
2174  */
2175 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2176 {
2177         struct gs_buf *gb;
2178
2179         if (size == 0)
2180                 return NULL;
2181
2182         gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2183         if (gb == NULL)
2184                 return NULL;
2185
2186         gb->buf_buf = kmalloc(size, kmalloc_flags);
2187         if (gb->buf_buf == NULL) {
2188                 kfree(gb);
2189                 return NULL;
2190         }
2191
2192         gb->buf_size = size;
2193         gb->buf_get = gb->buf_put = gb->buf_buf;
2194
2195         return gb;
2196 }
2197
2198 /*
2199  * gs_buf_free
2200  *
2201  * Free the buffer and all associated memory.
2202  */
2203 static void gs_buf_free(struct gs_buf *gb)
2204 {
2205         if (gb) {
2206                 kfree(gb->buf_buf);
2207                 kfree(gb);
2208         }
2209 }
2210
2211 /*
2212  * gs_buf_clear
2213  *
2214  * Clear out all data in the circular buffer.
2215  */
2216 static void gs_buf_clear(struct gs_buf *gb)
2217 {
2218         if (gb != NULL)
2219                 gb->buf_get = gb->buf_put;
2220                 /* equivalent to a get of all data available */
2221 }
2222
2223 /*
2224  * gs_buf_data_avail
2225  *
2226  * Return the number of bytes of data available in the circular
2227  * buffer.
2228  */
2229 static unsigned int gs_buf_data_avail(struct gs_buf *gb)
2230 {
2231         if (gb != NULL)
2232                 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2233         else
2234                 return 0;
2235 }
2236
2237 /*
2238  * gs_buf_space_avail
2239  *
2240  * Return the number of bytes of space available in the circular
2241  * buffer.
2242  */
2243 static unsigned int gs_buf_space_avail(struct gs_buf *gb)
2244 {
2245         if (gb != NULL)
2246                 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2247         else
2248                 return 0;
2249 }
2250
2251 /*
2252  * gs_buf_put
2253  *
2254  * Copy data data from a user buffer and put it into the circular buffer.
2255  * Restrict to the amount of space available.
2256  *
2257  * Return the number of bytes copied.
2258  */
2259 static unsigned int
2260 gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2261 {
2262         unsigned int len;
2263
2264         if (gb == NULL)
2265                 return 0;
2266
2267         len  = gs_buf_space_avail(gb);
2268         if (count > len)
2269                 count = len;
2270
2271         if (count == 0)
2272                 return 0;
2273
2274         len = gb->buf_buf + gb->buf_size - gb->buf_put;
2275         if (count > len) {
2276                 memcpy(gb->buf_put, buf, len);
2277                 memcpy(gb->buf_buf, buf+len, count - len);
2278                 gb->buf_put = gb->buf_buf + count - len;
2279         } else {
2280                 memcpy(gb->buf_put, buf, count);
2281                 if (count < len)
2282                         gb->buf_put += count;
2283                 else /* count == len */
2284                         gb->buf_put = gb->buf_buf;
2285         }
2286
2287         return count;
2288 }
2289
2290 /*
2291  * gs_buf_get
2292  *
2293  * Get data from the circular buffer and copy to the given buffer.
2294  * Restrict to the amount of data available.
2295  *
2296  * Return the number of bytes copied.
2297  */
2298 static unsigned int
2299 gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2300 {
2301         unsigned int len;
2302
2303         if (gb == NULL)
2304                 return 0;
2305
2306         len = gs_buf_data_avail(gb);
2307         if (count > len)
2308                 count = len;
2309
2310         if (count == 0)
2311                 return 0;
2312
2313         len = gb->buf_buf + gb->buf_size - gb->buf_get;
2314         if (count > len) {
2315                 memcpy(buf, gb->buf_get, len);
2316                 memcpy(buf+len, gb->buf_buf, count - len);
2317                 gb->buf_get = gb->buf_buf + count - len;
2318         } else {
2319                 memcpy(buf, gb->buf_get, count);
2320                 if (count < len)
2321                         gb->buf_get += count;
2322                 else /* count == len */
2323                         gb->buf_get = gb->buf_buf;
2324         }
2325
2326         return count;
2327 }