]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/gadget/serial.c
Merge branch 'for-2.6.26' of master.kernel.org:/pub/scm/linux/kernel/git/jwboyer...
[linux-2.6-omap-h63xx.git] / 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) 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)
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;    /* 8-N-1 etc */
139         u16                     port_handshake_bits;
140 #define RS232_RTS       (1 << 1)
141 #define RS232_DTE       (1 << 0)
142 };
143
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 */
162 };
163
164
165 /* Functions */
166
167 /* module */
168 static int __init gs_module_init(void);
169 static void __exit gs_module_exit(void);
170
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);
186
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);
194
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);
212
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);
216
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);
220
221 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
222 static void gs_free_ports(struct gs_dev *dev);
223
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);
234
235 /* external functions */
236 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
237
238
239 /* Globals */
240
241 static struct gs_dev *gs_device;
242
243 static const char *EP_IN_NAME;
244 static const char *EP_OUT_NAME;
245 static const char *EP_NOTIFY_NAME;
246
247 static struct mutex gs_open_close_lock[GS_NUM_PORTS];
248
249 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
250 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
251
252 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
253
254 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
255
256
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,
271 };
272 static struct tty_driver *gs_tty_driver;
273
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,
288         },
289 };
290
291
292 /* USB descriptors */
293
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
301
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 */
313 };
314
315 static struct usb_gadget_strings gs_string_table = {
316         .language =             0x0409, /* en-us */
317         .strings =              gs_strings,
318 };
319
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,
332 };
333
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,
338 };
339
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,
349 };
350
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,
360 };
361
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,
371 };
372
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,
382 };
383
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,
393 };
394
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),
400 };
401
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 */
408 };
409
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),
415 };
416
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 */
423 };
424
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,
432 };
433
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,
439 };
440
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,
446 };
447
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,
454 };
455
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,
468 };
469
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,
477 };
478
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),
484 };
485
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),
491 };
492
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,
499 };
500
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,
507 };
508
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,
521 };
522
523
524 /* Module */
525 MODULE_DESCRIPTION(GS_LONG_NAME);
526 MODULE_AUTHOR("Al Borchers");
527 MODULE_LICENSE("GPL");
528
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
533
534 module_param(read_q_size, uint, S_IRUGO);
535 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
536
537 module_param(write_q_size, uint, S_IRUGO);
538 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
539
540 module_param(write_buf_size, uint, S_IRUGO);
541 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
542
543 module_param(use_acm, uint, S_IRUGO);
544 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
545
546 module_init(gs_module_init);
547 module_exit(gs_module_exit);
548
549 /*
550 *  gs_module_init
551 *
552 *  Register as a USB gadget driver and a tty driver.
553 */
554 static int __init gs_module_init(void)
555 {
556         int i;
557         int retval;
558
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;
564         }
565
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);
580
581         for (i=0; i < GS_NUM_PORTS; i++)
582                 mutex_init(&gs_open_close_lock[i]);
583
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;
591         }
592
593         pr_info("gs_module_init: %s %s loaded\n",
594                         GS_LONG_NAME, GS_VERSION_STR);
595         return 0;
596 }
597
598 /*
599 * gs_module_exit
600 *
601 * Unregister as a tty driver and a USB gadget driver.
602 */
603 static void __exit gs_module_exit(void)
604 {
605         tty_unregister_driver(gs_tty_driver);
606         put_tty_driver(gs_tty_driver);
607         usb_gadget_unregister_driver(&gs_gadget_driver);
608
609         pr_info("gs_module_exit: %s %s unloaded\n",
610                         GS_LONG_NAME, GS_VERSION_STR);
611 }
612
613 /* TTY Driver */
614
615 /*
616  * gs_open
617  */
618 static int gs_open(struct tty_struct *tty, struct file *file)
619 {
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;
627
628         port_num = tty->index;
629
630         gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
631
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;
636         }
637
638         dev = gs_device;
639
640         if (dev == NULL) {
641                 pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
642                         port_num, tty, file);
643                 return -ENODEV;
644         }
645
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;
651         }
652
653         spin_lock_irqsave(&dev->dev_lock, flags);
654
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;
660         }
661
662         port = dev->dev_port[port_num];
663
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;
669         }
670
671         spin_lock(&port->port_lock);
672         spin_unlock(&dev->dev_lock);
673
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;
679         }
680
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;
687         }
688
689         tty->driver_data = NULL;
690
691         /* mark port as in use, we can drop port lock and sleep if necessary */
692         port->port_in_use = 1;
693
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);
699
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;
707                 }
708
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;
716                 }
717
718         }
719
720         /* wait for carrier detect (not implemented) */
721
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;
729         }
730
731         tty->driver_data = port;
732         port->port_tty = tty;
733         port->port_open_count = 1;
734         port->port_in_use = 0;
735
736         gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
737
738         ret = 0;
739
740 exit_unlock_port:
741         spin_unlock_irqrestore(&port->port_lock, flags);
742         mutex_unlock(mtx);
743         return ret;
744
745 exit_unlock_dev:
746         spin_unlock_irqrestore(&dev->dev_lock, flags);
747         mutex_unlock(mtx);
748         return ret;
749
750 }
751
752 /*
753  * gs_close
754  */
755
756 #define GS_WRITE_FINISHED_EVENT_SAFELY(p)                       \
757 ({                                                              \
758         int cond;                                               \
759                                                                 \
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;                                                   \
764 })
765
766 static void gs_close(struct tty_struct *tty, struct file *file)
767 {
768         struct gs_port *port = tty->driver_data;
769         struct mutex *mtx;
770
771         if (port == NULL) {
772                 pr_err("gs_close: NULL port pointer\n");
773                 return;
774         }
775
776         gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
777
778         mtx = &gs_open_close_lock[port->port_num];
779         mutex_lock(mtx);
780
781         spin_lock_irq(&port->port_lock);
782
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;
787         }
788
789         if (port->port_open_count > 1) {
790                 --port->port_open_count;
791                 goto exit;
792         }
793
794         /* free disconnected port on final close */
795         if (port->port_dev == NULL) {
796                 kfree(port);
797                 goto exit;
798         }
799
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;
804
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);
813         }
814
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;
820         }
821
822         gs_buf_clear(port->port_write_buf);
823
824         tty->driver_data = NULL;
825         port->port_tty = NULL;
826         port->port_in_use = 0;
827
828         gs_debug("gs_close: (%d,%p,%p) completed\n",
829                 port->port_num, tty, file);
830
831 exit:
832         spin_unlock_irq(&port->port_lock);
833         mutex_unlock(mtx);
834 }
835
836 /*
837  * gs_write
838  */
839 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
840 {
841         unsigned long flags;
842         struct gs_port *port = tty->driver_data;
843         int ret;
844
845         if (port == NULL) {
846                 pr_err("gs_write: NULL port pointer\n");
847                 return -EIO;
848         }
849
850         gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
851                 count);
852
853         if (count == 0)
854                 return 0;
855
856         spin_lock_irqsave(&port->port_lock, flags);
857
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;
863         }
864
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;
870         }
871
872         count = gs_buf_put(port->port_write_buf, buf, count);
873
874         spin_unlock_irqrestore(&port->port_lock, flags);
875
876         gs_send(gs_device);
877
878         gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
879                 count);
880
881         return count;
882
883 exit:
884         spin_unlock_irqrestore(&port->port_lock, flags);
885         return ret;
886 }
887
888 /*
889  * gs_put_char
890  */
891 static int gs_put_char(struct tty_struct *tty, unsigned char ch)
892 {
893         unsigned long flags;
894         struct gs_port *port = tty->driver_data;
895         int ret = 0;
896
897         if (port == NULL) {
898                 pr_err("gs_put_char: NULL port pointer\n");
899                 return 0;
900         }
901
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));
904
905         spin_lock_irqsave(&port->port_lock, flags);
906
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;
911         }
912
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;
917         }
918
919         ret = gs_buf_put(port->port_write_buf, &ch, 1);
920
921 exit:
922         spin_unlock_irqrestore(&port->port_lock, flags);
923         return ret;
924 }
925
926 /*
927  * gs_flush_chars
928  */
929 static void gs_flush_chars(struct tty_struct *tty)
930 {
931         unsigned long flags;
932         struct gs_port *port = tty->driver_data;
933
934         if (port == NULL) {
935                 pr_err("gs_flush_chars: NULL port pointer\n");
936                 return;
937         }
938
939         gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
940
941         spin_lock_irqsave(&port->port_lock, flags);
942
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;
947         }
948
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;
953         }
954
955         spin_unlock_irqrestore(&port->port_lock, flags);
956
957         gs_send(gs_device);
958
959         return;
960
961 exit:
962         spin_unlock_irqrestore(&port->port_lock, flags);
963 }
964
965 /*
966  * gs_write_room
967  */
968 static int gs_write_room(struct tty_struct *tty)
969 {
970
971         int room = 0;
972         unsigned long flags;
973         struct gs_port *port = tty->driver_data;
974
975
976         if (port == NULL)
977                 return 0;
978
979         spin_lock_irqsave(&port->port_lock, flags);
980
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);
984
985         spin_unlock_irqrestore(&port->port_lock, flags);
986
987         gs_debug("gs_write_room: (%d,%p) room=%d\n",
988                 port->port_num, tty, room);
989
990         return room;
991 }
992
993 /*
994  * gs_chars_in_buffer
995  */
996 static int gs_chars_in_buffer(struct tty_struct *tty)
997 {
998         int chars = 0;
999         unsigned long flags;
1000         struct gs_port *port = tty->driver_data;
1001
1002         if (port == NULL)
1003                 return 0;
1004
1005         spin_lock_irqsave(&port->port_lock, flags);
1006
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);
1010
1011         spin_unlock_irqrestore(&port->port_lock, flags);
1012
1013         gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1014                 port->port_num, tty, chars);
1015
1016         return chars;
1017 }
1018
1019 /*
1020  * gs_throttle
1021  */
1022 static void gs_throttle(struct tty_struct *tty)
1023 {
1024 }
1025
1026 /*
1027  * gs_unthrottle
1028  */
1029 static void gs_unthrottle(struct tty_struct *tty)
1030 {
1031 }
1032
1033 /*
1034  * gs_break
1035  */
1036 static void gs_break(struct tty_struct *tty, int break_state)
1037 {
1038 }
1039
1040 /*
1041  * gs_ioctl
1042  */
1043 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1044 {
1045         struct gs_port *port = tty->driver_data;
1046
1047         if (port == NULL) {
1048                 pr_err("gs_ioctl: NULL port pointer\n");
1049                 return -EIO;
1050         }
1051
1052         gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1053                 port->port_num, tty, file, cmd, arg);
1054
1055         /* handle ioctls */
1056
1057         /* could not handle ioctl */
1058         return -ENOIOCTLCMD;
1059 }
1060
1061 /*
1062  * gs_set_termios
1063  */
1064 static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
1065 {
1066 }
1067
1068 /*
1069 * gs_send
1070 *
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.
1076 */
1077 static int gs_send(struct gs_dev *dev)
1078 {
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;
1084
1085         if (dev == NULL) {
1086                 pr_err("gs_send: NULL device pointer\n");
1087                 return -ENODEV;
1088         }
1089
1090         spin_lock_irqsave(&dev->dev_lock, flags);
1091
1092         ep = dev->dev_in_ep;
1093
1094         while(!list_empty(&dev->dev_req_list)) {
1095
1096                 req_entry = list_entry(dev->dev_req_list.next,
1097                         struct gs_req_entry, re_entry);
1098
1099                 req = req_entry->re_req;
1100
1101                 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1102
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;
1118                         }
1119                         spin_lock_irqsave(&dev->dev_lock, flags);
1120                 } else {
1121                         break;
1122                 }
1123
1124         }
1125
1126         spin_unlock_irqrestore(&dev->dev_lock, flags);
1127
1128         return 0;
1129 }
1130
1131 /*
1132  * gs_send_packet
1133  *
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.
1138  *
1139  * Called during USB completion routine, on interrupt time.
1140  *
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.
1144  */
1145 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1146 {
1147         unsigned int len;
1148         struct gs_port *port;
1149
1150         /* TEMPORARY -- only port 0 is supported right now */
1151         port = dev->dev_port[0];
1152
1153         if (port == NULL) {
1154                 pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
1155                 return -EIO;
1156         }
1157
1158         spin_lock(&port->port_lock);
1159
1160         len = gs_buf_data_avail(port->port_write_buf);
1161         if (len < size)
1162                 size = len;
1163
1164         if (size == 0)
1165                 goto exit;
1166
1167         size = gs_buf_get(port->port_write_buf, packet, size);
1168
1169         if (port->port_tty)
1170                 wake_up_interruptible(&port->port_tty->write_wait);
1171
1172 exit:
1173         spin_unlock(&port->port_lock);
1174         return size;
1175 }
1176
1177 /*
1178  * gs_recv_packet
1179  *
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.
1183  *
1184  * Called during USB completion routine, on interrupt time.
1185  *
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.
1189  */
1190 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1191 {
1192         unsigned int len;
1193         struct gs_port *port;
1194         int ret;
1195         struct tty_struct *tty;
1196
1197         /* TEMPORARY -- only port 0 is supported right now */
1198         port = dev->dev_port[0];
1199
1200         if (port == NULL) {
1201                 pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
1202                         port->port_num);
1203                 return -EIO;
1204         }
1205
1206         spin_lock(&port->port_lock);
1207
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;
1213         }
1214
1215
1216         tty = port->port_tty;
1217
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;
1223         }
1224
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;
1230         }
1231
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);
1237         }
1238         ret = 0;
1239 exit:
1240         spin_unlock(&port->port_lock);
1241         return ret;
1242 }
1243
1244 /*
1245 * gs_read_complete
1246 */
1247 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1248 {
1249         int ret;
1250         struct gs_dev *dev = ep->driver_data;
1251
1252         if (dev == NULL) {
1253                 pr_err("gs_read_complete: NULL device pointer\n");
1254                 return;
1255         }
1256
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);
1267                 }
1268                 break;
1269
1270         case -ESHUTDOWN:
1271                 /* disconnect */
1272                 gs_debug("gs_read_complete: shutdown\n");
1273                 gs_free_req(ep, req);
1274                 break;
1275
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;
1283         }
1284 }
1285
1286 /*
1287 * gs_write_complete
1288 */
1289 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1290 {
1291         struct gs_dev *dev = ep->driver_data;
1292         struct gs_req_entry *gs_req = req->context;
1293
1294         if (dev == NULL) {
1295                 pr_err("gs_write_complete: NULL device pointer\n");
1296                 return;
1297         }
1298
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;
1306                 }
1307
1308                 spin_lock(&dev->dev_lock);
1309                 list_add(&gs_req->re_entry, &dev->dev_req_list);
1310                 spin_unlock(&dev->dev_lock);
1311
1312                 gs_send(dev);
1313
1314                 break;
1315
1316         case -ESHUTDOWN:
1317                 /* disconnect */
1318                 gs_debug("gs_write_complete: shutdown\n");
1319                 gs_free_req(ep, req);
1320                 break;
1321
1322         default:
1323                 pr_err(
1324                 "gs_write_complete: unexpected status error, status=%d\n",
1325                         req->status);
1326                 goto requeue;
1327                 break;
1328         }
1329 }
1330
1331 /* Gadget Driver */
1332
1333 /*
1334  * gs_bind
1335  *
1336  * Called on module load.  Allocates and initializes the device
1337  * structure and a control request.
1338  */
1339 static int __init gs_bind(struct usb_gadget *gadget)
1340 {
1341         int ret;
1342         struct usb_ep *ep;
1343         struct gs_dev *dev;
1344         int gcnum;
1345
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
1349          */
1350         if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
1351                 use_acm = 0;
1352
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);
1363         }
1364
1365         usb_ep_autoconfig_reset(gadget);
1366
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 */
1372
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 */
1378
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;
1384                 }
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 */
1389         }
1390
1391         gs_device_desc.bDeviceClass = use_acm
1392                 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1393         gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1394
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;
1408         }
1409
1410         usb_gadget_set_selfpowered(gadget);
1411
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;
1416         }
1417
1418         gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1419         if (dev == NULL)
1420                 return -ENOMEM;
1421
1422         snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1423                 init_utsname()->sysname, init_utsname()->release,
1424                 gadget->name);
1425
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);
1430
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;
1435         }
1436
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;
1443         }
1444         dev->dev_ctrl_req->complete = gs_setup_complete;
1445
1446         gadget->ep0->driver_data = dev;
1447
1448         pr_info("gs_bind: %s %s bound\n",
1449                 GS_LONG_NAME, GS_VERSION_STR);
1450
1451         return 0;
1452
1453 autoconf_fail:
1454         pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
1455         return -ENODEV;
1456 }
1457
1458 /*
1459  * gs_unbind
1460  *
1461  * Called on module unload.  Frees the control request and device
1462  * structure.
1463  */
1464 static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1465 {
1466         struct gs_dev *dev = get_gadget_data(gadget);
1467
1468         gs_device = NULL;
1469
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;
1475                 }
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);
1485         }
1486
1487         pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1488                 GS_VERSION_STR);
1489 }
1490
1491 /*
1492  * gs_setup
1493  *
1494  * Implements all the control endpoint functionality that's not
1495  * handled in hardware or the hardware driver.
1496  *
1497  * Returns the size of the data sent to the host, or a negative
1498  * error number.
1499  */
1500 static int gs_setup(struct usb_gadget *gadget,
1501         const struct usb_ctrlrequest *ctrl)
1502 {
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);
1509
1510         req->complete = gs_setup_complete;
1511
1512         switch (ctrl->bRequestType & USB_TYPE_MASK) {
1513         case USB_TYPE_STANDARD:
1514                 ret = gs_setup_standard(gadget,ctrl);
1515                 break;
1516
1517         case USB_TYPE_CLASS:
1518                 ret = gs_setup_class(gadget,ctrl);
1519                 break;
1520
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;
1527         }
1528
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);
1540                 }
1541         }
1542
1543         /* device either stalls (ret < 0) or reports success */
1544         return ret;
1545 }
1546
1547 static int gs_setup_standard(struct usb_gadget *gadget,
1548         const struct usb_ctrlrequest *ctrl)
1549 {
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);
1556
1557         switch (ctrl->bRequest) {
1558         case USB_REQ_GET_DESCRIPTOR:
1559                 if (ctrl->bRequestType != USB_DIR_IN)
1560                         break;
1561
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;
1568
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;
1576
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;
1588
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;
1596                 }
1597                 break;
1598
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;
1606
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;
1613
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;
1632                 }
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);
1638                         }
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);
1644                 }
1645                 ret = 0;
1646 set_interface_done:
1647                 spin_unlock(&dev->dev_lock);
1648                 break;
1649
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;
1659                 }
1660                 /* no alternate interface settings */
1661                 *(u8 *)req->buf = 0;
1662                 ret = min(wLength, (u16)1);
1663                 break;
1664
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;
1671         }
1672
1673         return ret;
1674 }
1675
1676 static int gs_setup_class(struct usb_gadget *gadget,
1677         const struct usb_ctrlrequest *ctrl)
1678 {
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);
1686
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;
1694
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);
1701                 }
1702                 break;
1703
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.
1712                          */
1713                         spin_lock(&port->port_lock);
1714                         port->port_handshake_bits = wValue;
1715                         spin_unlock(&port->port_lock);
1716                 }
1717                 break;
1718
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).
1725                  */
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;
1732         }
1733
1734         return ret;
1735 }
1736
1737 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
1738                 struct usb_request *req)
1739 {
1740         struct gs_dev *dev = ep->driver_data;
1741         struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1742
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;
1750
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.
1754                          */
1755                         spin_lock(&port->port_lock);
1756                         port->port_line_coding = *value;
1757                         spin_unlock(&port->port_lock);
1758                 }
1759                 break;
1760
1761         case -ESHUTDOWN:
1762                 /* disconnect */
1763                 gs_free_req(ep, req);
1764                 break;
1765
1766         default:
1767                 /* unexpected */
1768                 break;
1769         }
1770         return;
1771 }
1772
1773 /*
1774  * gs_setup_complete
1775  */
1776 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1777 {
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);
1782         }
1783 }
1784
1785 /*
1786  * gs_disconnect
1787  *
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.
1791  */
1792 static void gs_disconnect(struct usb_gadget *gadget)
1793 {
1794         unsigned long flags;
1795         struct gs_dev *dev = get_gadget_data(gadget);
1796
1797         spin_lock_irqsave(&dev->dev_lock, flags);
1798
1799         gs_reset_config(dev);
1800
1801         /* free closed ports and disconnect open ports */
1802         /* (open ports will be freed when closed) */
1803         gs_free_ports(dev);
1804
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");
1808
1809         spin_unlock_irqrestore(&dev->dev_lock, flags);
1810
1811         pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1812 }
1813
1814 /*
1815  * gs_set_config
1816  *
1817  * Configures the device by enabling device specific
1818  * optimizations, setting up the endpoints, allocating
1819  * read and write requests and queuing read requests.
1820  *
1821  * The device lock must be held when calling this function.
1822  */
1823 static int gs_set_config(struct gs_dev *dev, unsigned config)
1824 {
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;
1832
1833         if (dev == NULL) {
1834                 pr_err("gs_set_config: NULL device pointer\n");
1835                 return 0;
1836         }
1837
1838         if (config == dev->dev_config)
1839                 return 0;
1840
1841         gs_reset_config(dev);
1842
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;
1862         }
1863
1864         dev->dev_config = config;
1865
1866         gadget_for_each_ep(ep, gadget) {
1867
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;
1883                         }
1884                 }
1885
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;
1900                         }
1901                 }
1902
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;
1917                         }
1918                 }
1919
1920         }
1921
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;
1927         }
1928
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);
1937                         }
1938                 } else {
1939                         pr_err("gs_set_config: cannot allocate "
1940                                         "read requests\n");
1941                         ret = -ENOMEM;
1942                         goto exit_reset_config;
1943                 }
1944         }
1945
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;
1957                 }
1958         }
1959
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.
1963          */
1964
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");
1969
1970         return 0;
1971
1972 exit_reset_config:
1973         gs_reset_config(dev);
1974         return ret;
1975 }
1976
1977 /*
1978  * gs_reset_config
1979  *
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.
1984  *
1985  * The device lock must be held when calling this function.
1986  */
1987 static void gs_reset_config(struct gs_dev *dev)
1988 {
1989         struct gs_req_entry *req_entry;
1990
1991         if (dev == NULL) {
1992                 pr_err("gs_reset_config: NULL device pointer\n");
1993                 return;
1994         }
1995
1996         if (dev->dev_config == GS_NO_CONFIG_ID)
1997                 return;
1998
1999         dev->dev_config = GS_NO_CONFIG_ID;
2000
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);
2007         }
2008
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;
2014         }
2015         if (dev->dev_in_ep) {
2016                 usb_ep_disable(dev->dev_in_ep);
2017                 dev->dev_in_ep = NULL;
2018         }
2019         if (dev->dev_out_ep) {
2020                 usb_ep_disable(dev->dev_out_ep);
2021                 dev->dev_out_ep = NULL;
2022         }
2023 }
2024
2025 /*
2026  * gs_build_config_buf
2027  *
2028  * Builds the config descriptors in the given buffer and returns the
2029  * length, or a negative error number.
2030  */
2031 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
2032         u8 type, unsigned int index, int is_otg)
2033 {
2034         int len;
2035         int high_speed = 0;
2036         const struct usb_config_descriptor *config_desc;
2037         const struct usb_descriptor_header **function;
2038
2039         if (index >= gs_device_desc.bNumConfigurations)
2040                 return -EINVAL;
2041
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;
2047         }
2048
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;
2059         }
2060
2061         /* for now, don't advertise srp-only devices */
2062         if (!is_otg)
2063                 function++;
2064
2065         len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2066         if (len < 0)
2067                 return len;
2068
2069         ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2070
2071         return len;
2072 }
2073
2074 /*
2075  * gs_alloc_req
2076  *
2077  * Allocate a usb_request and its buffer.  Returns a pointer to the
2078  * usb_request or NULL if there is an error.
2079  */
2080 static struct usb_request *
2081 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
2082 {
2083         struct usb_request *req;
2084
2085         if (ep == NULL)
2086                 return NULL;
2087
2088         req = usb_ep_alloc_request(ep, kmalloc_flags);
2089
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;
2096                 }
2097         }
2098
2099         return req;
2100 }
2101
2102 /*
2103  * gs_free_req
2104  *
2105  * Free a usb_request and its buffer.
2106  */
2107 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2108 {
2109         if (ep != NULL && req != NULL) {
2110                 kfree(req->buf);
2111                 usb_ep_free_request(ep, req);
2112         }
2113 }
2114
2115 /*
2116  * gs_alloc_req_entry
2117  *
2118  * Allocates a request and its buffer, using the given
2119  * endpoint, buffer len, and kmalloc flags.
2120  */
2121 static struct gs_req_entry *
2122 gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2123 {
2124         struct gs_req_entry     *req;
2125
2126         req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2127         if (req == NULL)
2128                 return NULL;
2129
2130         req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2131         if (req->re_req == NULL) {
2132                 kfree(req);
2133                 return NULL;
2134         }
2135
2136         req->re_req->context = req;
2137
2138         return req;
2139 }
2140
2141 /*
2142  * gs_free_req_entry
2143  *
2144  * Frees a request and its buffer.
2145  */
2146 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2147 {
2148         if (ep != NULL && req != NULL) {
2149                 if (req->re_req != NULL)
2150                         gs_free_req(ep, req->re_req);
2151                 kfree(req);
2152         }
2153 }
2154
2155 /*
2156  * gs_alloc_ports
2157  *
2158  * Allocate all ports and set the gs_dev struct to point to them.
2159  * Return 0 if successful, or a negative error number.
2160  *
2161  * The device lock is normally held when calling this function.
2162  */
2163 static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2164 {
2165         int i;
2166         struct gs_port *port;
2167
2168         if (dev == NULL)
2169                 return -EIO;
2170
2171         for (i=0; i<GS_NUM_PORTS; i++) {
2172                 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2173                         return -ENOMEM;
2174
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);
2183
2184                 dev->dev_port[i] = port;
2185         }
2186
2187         return 0;
2188 }
2189
2190 /*
2191  * gs_free_ports
2192  *
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.
2197  *
2198  * The device lock is normally held when calling this function.
2199  */
2200 static void gs_free_ports(struct gs_dev *dev)
2201 {
2202         int i;
2203         unsigned long flags;
2204         struct gs_port *port;
2205
2206         if (dev == NULL)
2207                 return;
2208
2209         for (i=0; i<GS_NUM_PORTS; i++) {
2210                 if ((port=dev->dev_port[i]) != NULL) {
2211                         dev->dev_port[i] = NULL;
2212
2213                         spin_lock_irqsave(&port->port_lock, flags);
2214
2215                         if (port->port_write_buf != NULL) {
2216                                 gs_buf_free(port->port_write_buf);
2217                                 port->port_write_buf = NULL;
2218                         }
2219
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);
2225                                 }
2226                                 spin_unlock_irqrestore(&port->port_lock, flags);
2227                         } else {
2228                                 spin_unlock_irqrestore(&port->port_lock, flags);
2229                                 kfree(port);
2230                         }
2231
2232                 }
2233         }
2234 }
2235
2236 /* Circular Buffer */
2237
2238 /*
2239  * gs_buf_alloc
2240  *
2241  * Allocate a circular buffer and all associated memory.
2242  */
2243 static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
2244 {
2245         struct gs_buf *gb;
2246
2247         if (size == 0)
2248                 return NULL;
2249
2250         gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2251         if (gb == NULL)
2252                 return NULL;
2253
2254         gb->buf_buf = kmalloc(size, kmalloc_flags);
2255         if (gb->buf_buf == NULL) {
2256                 kfree(gb);
2257                 return NULL;
2258         }
2259
2260         gb->buf_size = size;
2261         gb->buf_get = gb->buf_put = gb->buf_buf;
2262
2263         return gb;
2264 }
2265
2266 /*
2267  * gs_buf_free
2268  *
2269  * Free the buffer and all associated memory.
2270  */
2271 static void gs_buf_free(struct gs_buf *gb)
2272 {
2273         if (gb) {
2274                 kfree(gb->buf_buf);
2275                 kfree(gb);
2276         }
2277 }
2278
2279 /*
2280  * gs_buf_clear
2281  *
2282  * Clear out all data in the circular buffer.
2283  */
2284 static void gs_buf_clear(struct gs_buf *gb)
2285 {
2286         if (gb != NULL)
2287                 gb->buf_get = gb->buf_put;
2288                 /* equivalent to a get of all data available */
2289 }
2290
2291 /*
2292  * gs_buf_data_avail
2293  *
2294  * Return the number of bytes of data available in the circular
2295  * buffer.
2296  */
2297 static unsigned int gs_buf_data_avail(struct gs_buf *gb)
2298 {
2299         if (gb != NULL)
2300                 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2301         else
2302                 return 0;
2303 }
2304
2305 /*
2306  * gs_buf_space_avail
2307  *
2308  * Return the number of bytes of space available in the circular
2309  * buffer.
2310  */
2311 static unsigned int gs_buf_space_avail(struct gs_buf *gb)
2312 {
2313         if (gb != NULL)
2314                 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2315         else
2316                 return 0;
2317 }
2318
2319 /*
2320  * gs_buf_put
2321  *
2322  * Copy data data from a user buffer and put it into the circular buffer.
2323  * Restrict to the amount of space available.
2324  *
2325  * Return the number of bytes copied.
2326  */
2327 static unsigned int
2328 gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2329 {
2330         unsigned int len;
2331
2332         if (gb == NULL)
2333                 return 0;
2334
2335         len  = gs_buf_space_avail(gb);
2336         if (count > len)
2337                 count = len;
2338
2339         if (count == 0)
2340                 return 0;
2341
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;
2353         }
2354
2355         return count;
2356 }
2357
2358 /*
2359  * gs_buf_get
2360  *
2361  * Get data from the circular buffer and copy to the given buffer.
2362  * Restrict to the amount of data available.
2363  *
2364  * Return the number of bytes copied.
2365  */
2366 static unsigned int
2367 gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2368 {
2369         unsigned int len;
2370
2371         if (gb == NULL)
2372                 return 0;
2373
2374         len = gs_buf_data_avail(gb);
2375         if (count > len)
2376                 count = len;
2377
2378         if (count == 0)
2379                 return 0;
2380
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;
2392         }
2393
2394         return count;
2395 }