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