tizen 2.4 release
[kernel/u-boot-tm1.git] / drivers / usb / gadget / f_acm.c
1 /*
2  * f_acm.c -- USB CDC serial (ACM) function driver
3  *
4  * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5  * Copyright (C) 2008 by David Brownell
6  * Copyright (C) 2008 by Nokia Corporation
7  *
8  * This software is distributed under the terms of the GNU General
9  * Public License ("GPL") as published by the Free Software Foundation,
10  * either version 2 of that License or (at your option) any later version.
11  */
12
13 /* #define VERBOSE_DEBUG */
14
15 //#include <linux/kernel.h>
16 //#include <linux/device.h>
17 //#include <linux/usb/android_composite.h>
18
19 #include "u_serial.h"
20 #include "gadget_chips.h"
21 #include <ubi_uboot.h>
22 #include <linux/types.h>
23
24
25 #define GFP_ATOMIC ((gfp_t) 0)
26
27 /*
28  * This CDC ACM function support just wraps control functions and
29  * notifications around the generic serial-over-usb code.
30  *
31  * Because CDC ACM is standardized by the USB-IF, many host operating
32  * systems have drivers for it.  Accordingly, ACM is the preferred
33  * interop solution for serial-port type connections.  The control
34  * models are often not necessary, and in any case don't do much in
35  * this bare-bones implementation.
36  *
37  * Note that even MS-Windows has some support for ACM.  However, that
38  * support is somewhat broken because when you use ACM in a composite
39  * device, having multiple interfaces confuses the poor OS.  It doesn't
40  * seem to understand CDC Union descriptors.  The new "association"
41  * descriptors (roughly equivalent to CDC Unions) may sometimes help.
42  */
43
44 struct acm_ep_descs {
45         struct usb_endpoint_descriptor  *in;
46         struct usb_endpoint_descriptor  *out;
47         struct usb_endpoint_descriptor  *notify;
48 };
49
50 struct f_acm {
51         struct gserial                  port;
52         u8                              ctrl_id, data_id;
53         u8                              port_num;
54
55         u8                              pending;
56
57         /* lock is mostly for pending and notify_req ... they get accessed
58          * by callbacks both from tty (open/close/break) under its spinlock,
59          * and notify_req.complete() which can't use that lock.
60          */
61         spinlock_t                      lock;
62
63         struct acm_ep_descs             fs;
64         struct acm_ep_descs             hs;
65
66         struct usb_ep                   *notify;
67         struct usb_endpoint_descriptor  *notify_desc;
68         struct usb_request              *notify_req;
69
70         struct usb_cdc_line_coding      port_line_coding;       /* 8-N-1 etc */
71
72         /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
73         u16                             port_handshake_bits;
74 #define ACM_CTRL_RTS    (1 << 1)        /* unused with full duplex */
75 #define ACM_CTRL_DTR    (1 << 0)        /* host is ready for data r/w */
76
77         /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
78         u16                             serial_state;
79 #define ACM_CTRL_OVERRUN        (1 << 6)
80 #define ACM_CTRL_PARITY         (1 << 5)
81 #define ACM_CTRL_FRAMING        (1 << 4)
82 #define ACM_CTRL_RI             (1 << 3)
83 #define ACM_CTRL_BRK            (1 << 2)
84 #define ACM_CTRL_DSR            (1 << 1)
85 #define ACM_CTRL_DCD            (1 << 0)
86 };
87
88 static inline struct f_acm *func_to_acm(struct usb_function *f)
89 {
90         return container_of(f, struct f_acm, port.func);
91 }
92
93 static inline struct f_acm *port_to_acm(struct gserial *p)
94 {
95         return container_of(p, struct f_acm, port);
96 }
97
98 /*-------------------------------------------------------------------------*/
99
100 /* notification endpoint uses smallish and infrequent fixed-size messages */
101
102 #define GS_LOG2_NOTIFY_INTERVAL         5       /* 1 << 5 == 32 msec */
103 #define GS_NOTIFY_MAXPACKET             10      /* notification + 2 bytes */
104
105 /* interface and class descriptors: */
106
107 static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
108         .bLength =              USB_DT_INTERFACE_SIZE,
109         .bDescriptorType =      USB_DT_INTERFACE,
110         /* .bInterfaceNumber = DYNAMIC */
111         .bNumEndpoints =        1,
112         .bInterfaceClass =      USB_CLASS_COMM,
113         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
114         .bInterfaceProtocol =   USB_CDC_ACM_PROTO_AT_V25TER,
115         /* .iInterface = DYNAMIC */
116 };
117
118 static struct usb_interface_descriptor acm_data_interface_desc __initdata = {
119         .bLength =              USB_DT_INTERFACE_SIZE,
120         .bDescriptorType =      USB_DT_INTERFACE,
121         /* .bInterfaceNumber = DYNAMIC */
122         .bNumEndpoints =        2,
123         .bInterfaceClass =      USB_CLASS_CDC_DATA,
124         .bInterfaceSubClass =   0,
125         .bInterfaceProtocol =   0,
126         /* .iInterface = DYNAMIC */
127 };
128
129 static struct usb_cdc_header_desc acm_header_desc __initdata = {
130         .bLength =              sizeof(acm_header_desc),
131         .bDescriptorType =      USB_DT_CS_INTERFACE,
132         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
133         .bcdCDC =               cpu_to_le16(0x0110),
134 };
135
136 static struct usb_cdc_call_mgmt_descriptor
137 acm_call_mgmt_descriptor __initdata = {
138         .bLength =              sizeof(acm_call_mgmt_descriptor),
139         .bDescriptorType =      USB_DT_CS_INTERFACE,
140         .bDescriptorSubType =   USB_CDC_CALL_MANAGEMENT_TYPE,
141         .bmCapabilities =       0,
142         /* .bDataInterface = DYNAMIC */
143 };
144
145 static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
146         .bLength =              sizeof(acm_descriptor),
147         .bDescriptorType =      USB_DT_CS_INTERFACE,
148         .bDescriptorSubType =   USB_CDC_ACM_TYPE,
149         .bmCapabilities =       USB_CDC_CAP_LINE,
150 };
151
152 static struct usb_cdc_union_desc acm_union_desc __initdata = {
153         .bLength =              sizeof(acm_union_desc),
154         .bDescriptorType =      USB_DT_CS_INTERFACE,
155         .bDescriptorSubType =   USB_CDC_UNION_TYPE,
156         /* .bMasterInterface0 = DYNAMIC */
157         /* .bSlaveInterface0 =  DYNAMIC */
158 };
159
160 /* full speed support: */
161
162 static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = {
163         .bLength =              USB_DT_ENDPOINT_SIZE,
164         .bDescriptorType =      USB_DT_ENDPOINT,
165         .bEndpointAddress =     USB_DIR_IN,
166         .bmAttributes =         USB_ENDPOINT_XFER_INT,
167         .wMaxPacketSize =       cpu_to_le16(GS_NOTIFY_MAXPACKET),
168         .bInterval =            1 << GS_LOG2_NOTIFY_INTERVAL,
169 };
170
171 static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = {
172         .bLength =              USB_DT_ENDPOINT_SIZE,
173         .bDescriptorType =      USB_DT_ENDPOINT,
174         .bEndpointAddress =     USB_DIR_IN,
175         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
176 };
177
178 static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = {
179         .bLength =              USB_DT_ENDPOINT_SIZE,
180         .bDescriptorType =      USB_DT_ENDPOINT,
181         .bEndpointAddress =     USB_DIR_OUT,
182         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
183 };
184
185 static struct usb_descriptor_header *acm_fs_function[] __initdata = {
186         (struct usb_descriptor_header *) &acm_control_interface_desc,
187         (struct usb_descriptor_header *) &acm_header_desc,
188         (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
189         (struct usb_descriptor_header *) &acm_descriptor,
190         (struct usb_descriptor_header *) &acm_union_desc,
191         (struct usb_descriptor_header *) &acm_fs_notify_desc,
192         (struct usb_descriptor_header *) &acm_data_interface_desc,
193         (struct usb_descriptor_header *) &acm_fs_in_desc,
194         (struct usb_descriptor_header *) &acm_fs_out_desc,
195         NULL,
196 };
197
198 /* high speed support: */
199
200 static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = {
201         .bLength =              USB_DT_ENDPOINT_SIZE,
202         .bDescriptorType =      USB_DT_ENDPOINT,
203         .bEndpointAddress =     USB_DIR_IN,
204         .bmAttributes =         USB_ENDPOINT_XFER_INT,
205         .wMaxPacketSize =       cpu_to_le16(GS_NOTIFY_MAXPACKET),
206         .bInterval =            GS_LOG2_NOTIFY_INTERVAL+4,
207 };
208
209 static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = {
210         .bLength =              USB_DT_ENDPOINT_SIZE,
211         .bDescriptorType =      USB_DT_ENDPOINT,
212         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
213         .wMaxPacketSize =       cpu_to_le16(512),
214 };
215
216 static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = {
217         .bLength =              USB_DT_ENDPOINT_SIZE,
218         .bDescriptorType =      USB_DT_ENDPOINT,
219         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
220         .wMaxPacketSize =       cpu_to_le16(512),
221 };
222
223 static struct usb_descriptor_header *acm_hs_function[] __initdata = {
224         (struct usb_descriptor_header *) &acm_control_interface_desc,
225         (struct usb_descriptor_header *) &acm_header_desc,
226         (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
227         (struct usb_descriptor_header *) &acm_descriptor,
228         (struct usb_descriptor_header *) &acm_union_desc,
229         (struct usb_descriptor_header *) &acm_hs_notify_desc,
230         (struct usb_descriptor_header *) &acm_data_interface_desc,
231         (struct usb_descriptor_header *) &acm_hs_in_desc,
232         (struct usb_descriptor_header *) &acm_hs_out_desc,
233         NULL,
234 };
235
236 /* string descriptors: */
237
238 #define ACM_CTRL_IDX    0
239 #define ACM_DATA_IDX    1
240
241 /* static strings, in UTF-8 */
242 static struct usb_string acm_string_defs[] = {
243         [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
244         [ACM_DATA_IDX].s = "CDC ACM Data",
245         {  /* ZEROES END LIST */ },
246 };
247
248 static struct usb_gadget_strings acm_string_table = {
249         .language =             0x0409, /* en-us */
250         .strings =              acm_string_defs,
251 };
252
253 static struct usb_gadget_strings *acm_strings[] = {
254         &acm_string_table,
255         NULL,
256 };
257
258 /*-------------------------------------------------------------------------*/
259
260 /* ACM control ... data handling is delegated to tty library code.
261  * The main task of this function is to activate and deactivate
262  * that code based on device state; track parameters like line
263  * speed, handshake state, and so on; and issue notifications.
264  */
265
266 static void acm_complete_set_line_coding(struct usb_ep *ep,
267                 struct usb_request *req)
268 {
269         struct f_acm    *acm = ep->driver_data;
270         struct usb_composite_dev *cdev = acm->port.func.config->cdev;
271
272         if (req->status != 0) {
273                 DBG(cdev, "acm ttyGS%d completion, err %d\n",
274                                 acm->port_num, req->status);
275                 return;
276         }
277
278         /* normal completion */
279         if (req->actual != sizeof(acm->port_line_coding)) {
280                 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
281                                 acm->port_num, req->actual);
282                 usb_ep_set_halt(ep);
283         } else {
284                 struct usb_cdc_line_coding      *value = req->buf;
285
286                 /* REVISIT:  we currently just remember this data.
287                  * If we change that, (a) validate it first, then
288                  * (b) update whatever hardware needs updating,
289                  * (c) worry about locking.  This is information on
290                  * the order of 9600-8-N-1 ... most of which means
291                  * nothing unless we control a real RS232 line.
292                  */
293                 acm->port_line_coding = *value;
294         }
295 }
296
297 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
298 {
299         struct f_acm            *acm = func_to_acm(f);
300         struct usb_composite_dev *cdev = f->config->cdev;
301         struct usb_request      *req = cdev->req;
302         int                     value = -EOPNOTSUPP;
303         u16                     w_index = le16_to_cpu(ctrl->wIndex);
304         u16                     w_value = le16_to_cpu(ctrl->wValue);
305         u16                     w_length = le16_to_cpu(ctrl->wLength);
306
307         /* composite driver infrastructure handles everything except
308          * CDC class messages; interface activation uses set_alt().
309          *
310          * Note CDC spec table 4 lists the ACM request profile.  It requires
311          * encapsulated command support ... we don't handle any, and respond
312          * to them by stalling.  Options include get/set/clear comm features
313          * (not that useful) and SEND_BREAK.
314          */
315         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
316
317         /* SET_LINE_CODING ... just read and save what the host sends */
318         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
319                         | USB_CDC_REQ_SET_LINE_CODING:
320                 if (w_length != sizeof(struct usb_cdc_line_coding)
321                                 || w_index != acm->ctrl_id)
322                         goto invalid;
323
324                 value = w_length;
325                 cdev->gadget->ep0->driver_data = acm;
326                 req->complete = acm_complete_set_line_coding;
327                 break;
328
329         /* GET_LINE_CODING ... return what host sent, or initial value */
330         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
331                         | USB_CDC_REQ_GET_LINE_CODING:
332                 if (w_index != acm->ctrl_id)
333                         goto invalid;
334
335                 value = min_t(unsigned, w_length,
336                                 sizeof(struct usb_cdc_line_coding));
337                 memcpy(req->buf, &acm->port_line_coding, value);
338                 break;
339
340         /* SET_CONTROL_LINE_STATE ... save what the host sent */
341         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
342                         | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
343                 if (w_index != acm->ctrl_id)
344                         goto invalid;
345
346                 value = 0;
347
348                 /* FIXME we should not allow data to flow until the
349                  * host sets the ACM_CTRL_DTR bit; and when it clears
350                  * that bit, we should return to that no-flow state.
351                  */
352                 acm->port_handshake_bits = w_value;
353                 break;
354
355         default:
356 invalid:
357                 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
358                         ctrl->bRequestType, ctrl->bRequest,
359                         w_value, w_index, w_length);
360         }
361
362         /* respond with data transfer or status phase? */
363         if (value >= 0) {
364                 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
365                         acm->port_num, ctrl->bRequestType, ctrl->bRequest,
366                         w_value, w_index, w_length);
367                 req->zero = 0;
368                 req->length = value;
369                 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
370                 if (value < 0)
371                         ERROR(cdev, "acm response on ttyGS%d, err %d\n",
372                                         acm->port_num, value);
373         }
374
375         /* device either stalls (value < 0) or reports success */
376         return value;
377 }
378
379 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
380 {
381         struct f_acm            *acm = func_to_acm(f);
382         struct usb_composite_dev *cdev = f->config->cdev;
383
384         /* we know alt == 0, so this is an activation or a reset */
385
386         if (intf == acm->ctrl_id) {
387                 if (acm->notify->driver_data) {
388                         VDBG(cdev, "reset acm control interface %d\n", intf);
389                         usb_ep_disable(acm->notify);
390                 } else {
391                         VDBG(cdev, "init acm ctrl interface %d\n", intf);
392                 }
393                 acm->notify_desc = ep_choose(cdev->gadget,
394                                 acm->hs.notify,
395                                 acm->fs.notify);
396                 usb_ep_enable(acm->notify, acm->notify_desc);
397                 acm->notify->driver_data = acm;
398
399         } else if (intf == acm->data_id) {
400                 if (acm->port.in->driver_data) {
401                         DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
402                         gserial_disconnect(&acm->port);
403                 } else {
404                         DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
405                 }
406                 acm->port.in_desc = ep_choose(cdev->gadget,
407                                 acm->hs.in, acm->fs.in);
408                 acm->port.out_desc = ep_choose(cdev->gadget,
409                                 acm->hs.out, acm->fs.out);
410                 gserial_connect(&acm->port, acm->port_num);
411
412         } else
413                 return -EINVAL;
414
415         return 0;
416 }
417
418 static void acm_disable(struct usb_function *f)
419 {
420         struct f_acm    *acm = func_to_acm(f);
421         struct usb_composite_dev *cdev = f->config->cdev;
422
423         DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
424         gserial_disconnect(&acm->port);
425         usb_ep_disable(acm->notify);
426         acm->notify->driver_data = NULL;
427 }
428
429 /*-------------------------------------------------------------------------*/
430
431 /**
432  * acm_cdc_notify - issue CDC notification to host
433  * @acm: wraps host to be notified
434  * @type: notification type
435  * @value: Refer to cdc specs, wValue field.
436  * @data: data to be sent
437  * @length: size of data
438  * Context: irqs blocked, acm->lock held, acm_notify_req non-null
439  *
440  * Returns zero on sucess or a negative errno.
441  *
442  * See section 6.3.5 of the CDC 1.1 specification for information
443  * about the only notification we issue:  SerialState change.
444  */
445 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
446                 void *data, unsigned length)
447 {
448         struct usb_ep                   *ep = acm->notify;
449         struct usb_request              *req;
450         struct usb_cdc_notification     *notify;
451         const unsigned                  len = sizeof(*notify) + length;
452         void                            *buf;
453         int                             status;
454
455         req = acm->notify_req;
456         acm->notify_req = NULL;
457         acm->pending = false;
458
459         req->length = len;
460         notify = req->buf;
461         buf = notify + 1;
462
463         notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
464                         | USB_RECIP_INTERFACE;
465         notify->bNotificationType = type;
466         notify->wValue = cpu_to_le16(value);
467         notify->wIndex = cpu_to_le16(acm->ctrl_id);
468         notify->wLength = cpu_to_le16(length);
469         memcpy(buf, data, length);
470
471         /* ep_queue() can complete immediately if it fills the fifo... */
472         spin_unlock(&acm->lock);
473         status = usb_ep_queue(ep, req, GFP_ATOMIC);
474         spin_lock(&acm->lock);
475
476         if (status < 0) {
477                 ERROR(acm->port.func.config->cdev,
478                                 "acm ttyGS%d can't notify serial state, %d\n",
479                                 acm->port_num, status);
480                 acm->notify_req = req;
481         }
482
483         return status;
484 }
485
486 static int acm_notify_serial_state(struct f_acm *acm)
487 {
488         struct usb_composite_dev *cdev = acm->port.func.config->cdev;
489         int                     status;
490
491         spin_lock(&acm->lock);
492         if (acm->notify_req) {
493                 DBG(cdev, "acm ttyGS%d serial state %04x\n",
494                                 acm->port_num, acm->serial_state);
495                 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
496                                 0, &acm->serial_state, sizeof(acm->serial_state));
497         } else {
498                 acm->pending = true;
499                 status = 0;
500         }
501         spin_unlock(&acm->lock);
502         return status;
503 }
504
505 static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
506 {
507         struct f_acm            *acm = req->context;
508         u8                      doit = false;
509
510         /* on this call path we do NOT hold the port spinlock,
511          * which is why ACM needs its own spinlock
512          */
513         spin_lock(&acm->lock);
514         if (req->status != -ESHUTDOWN)
515                 doit = acm->pending;
516         acm->notify_req = req;
517         spin_unlock(&acm->lock);
518
519         if (doit)
520                 acm_notify_serial_state(acm);
521 }
522
523 /* connect == the TTY link is open */
524
525 static void acm_connect(struct gserial *port)
526 {
527         struct f_acm            *acm = port_to_acm(port);
528
529         acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
530         acm_notify_serial_state(acm);
531 }
532
533 static void acm_disconnect(struct gserial *port)
534 {
535         struct f_acm            *acm = port_to_acm(port);
536
537         acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
538         acm_notify_serial_state(acm);
539 }
540
541 static int acm_send_break(struct gserial *port, int duration)
542 {
543         struct f_acm            *acm = port_to_acm(port);
544         u16                     state;
545
546         state = acm->serial_state;
547         state &= ~ACM_CTRL_BRK;
548         if (duration)
549                 state |= ACM_CTRL_BRK;
550
551         acm->serial_state = state;
552         return acm_notify_serial_state(acm);
553 }
554
555 /*-------------------------------------------------------------------------*/
556
557 /* ACM function driver setup/binding */
558 static int __init
559 acm_bind(struct usb_configuration *c, struct usb_function *f)
560 {
561         struct usb_composite_dev *cdev = c->cdev;
562         struct f_acm            *acm = func_to_acm(f);
563         int                     status;
564         struct usb_ep           *ep;
565
566         /* allocate instance-specific interface IDs, and patch descriptors */
567         status = usb_interface_id(c, f);
568         if (status < 0)
569                 goto fail;
570         acm->ctrl_id = status;
571
572         acm_control_interface_desc.bInterfaceNumber = status;
573         acm_union_desc .bMasterInterface0 = status;
574
575         status = usb_interface_id(c, f);
576         if (status < 0)
577                 goto fail;
578         acm->data_id = status;
579
580         acm_data_interface_desc.bInterfaceNumber = status;
581         acm_union_desc.bSlaveInterface0 = status;
582         acm_call_mgmt_descriptor.bDataInterface = status;
583
584         status = -ENODEV;
585
586         /* allocate instance-specific endpoints */
587         ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
588         if (!ep)
589                 goto fail;
590         acm->port.in = ep;
591         ep->driver_data = cdev; /* claim */
592
593         ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
594         if (!ep)
595                 goto fail;
596         acm->port.out = ep;
597         ep->driver_data = cdev; /* claim */
598
599         ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
600         if (!ep)
601                 goto fail;
602         acm->notify = ep;
603         ep->driver_data = cdev; /* claim */
604
605         /* allocate notification */
606         acm->notify_req = gs_alloc_req(ep,
607                         sizeof(struct usb_cdc_notification) + 2,
608                         GFP_KERNEL);
609         if (!acm->notify_req)
610                 goto fail;
611
612         acm->notify_req->complete = acm_cdc_notify_complete;
613         acm->notify_req->context = acm;
614
615         /* copy descriptors, and track endpoint copies */
616         f->descriptors = usb_copy_descriptors(acm_fs_function);
617         if (!f->descriptors)
618                 goto fail;
619
620         acm->fs.in = usb_find_endpoint(acm_fs_function,
621                         f->descriptors, &acm_fs_in_desc);
622         acm->fs.out = usb_find_endpoint(acm_fs_function,
623                         f->descriptors, &acm_fs_out_desc);
624         acm->fs.notify = usb_find_endpoint(acm_fs_function,
625                         f->descriptors, &acm_fs_notify_desc);
626
627         /* support all relevant hardware speeds... we expect that when
628          * hardware is dual speed, all bulk-capable endpoints work at
629          * both speeds
630          */
631         if (gadget_is_dualspeed(c->cdev->gadget)) {
632                 acm_hs_in_desc.bEndpointAddress =
633                                 acm_fs_in_desc.bEndpointAddress;
634                 acm_hs_out_desc.bEndpointAddress =
635                                 acm_fs_out_desc.bEndpointAddress;
636                 acm_hs_notify_desc.bEndpointAddress =
637                                 acm_fs_notify_desc.bEndpointAddress;
638
639                 /* copy descriptors, and track endpoint copies */
640                 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
641
642                 acm->hs.in = usb_find_endpoint(acm_hs_function,
643                                 f->hs_descriptors, &acm_hs_in_desc);
644                 acm->hs.out = usb_find_endpoint(acm_hs_function,
645                                 f->hs_descriptors, &acm_hs_out_desc);
646                 acm->hs.notify = usb_find_endpoint(acm_hs_function,
647                                 f->hs_descriptors, &acm_hs_notify_desc);
648         }
649
650         DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
651                         acm->port_num,
652                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
653                         acm->port.in->name, acm->port.out->name,
654                         acm->notify->name);
655         return 0;
656
657 fail:
658         if (acm->notify_req)
659                 gs_free_req(acm->notify, acm->notify_req);
660
661         /* we might as well release our claims on endpoints */
662         if (acm->notify)
663                 acm->notify->driver_data = NULL;
664         if (acm->port.out)
665                 acm->port.out->driver_data = NULL;
666         if (acm->port.in)
667                 acm->port.in->driver_data = NULL;
668
669         ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
670
671         return status;
672 }
673
674 static void
675 acm_unbind(struct usb_configuration *c, struct usb_function *f)
676 {
677         struct f_acm            *acm = func_to_acm(f);
678
679         if (gadget_is_dualspeed(c->cdev->gadget))
680                 usb_free_descriptors(f->hs_descriptors);
681         usb_free_descriptors(f->descriptors);
682         if(acm->notify_req)
683                 gs_free_req(acm->notify, acm->notify_req);
684         kfree(acm);
685 }
686
687 /* Some controllers can't support CDC ACM ... */
688 static inline bool can_support_cdc(struct usb_configuration *c)
689 {
690         /* SH3 doesn't support multiple interfaces */
691         if (gadget_is_sh(c->cdev->gadget))
692                 return false;
693
694         /* sa1100 doesn't have a third interrupt endpoint */
695         if (gadget_is_sa1100(c->cdev->gadget))
696                 return false;
697
698         /* everything else is *probably* fine ... */
699         return true;
700 }
701
702 /**
703  * acm_bind_config - add a CDC ACM function to a configuration
704  * @c: the configuration to support the CDC ACM instance
705  * @port_num: /dev/ttyGS* port this interface will use
706  * Context: single threaded during gadget setup
707  *
708  * Returns zero on success, else negative errno.
709  *
710  * Caller must have called @gserial_setup() with enough ports to
711  * handle all the ones it binds.  Caller is also responsible
712  * for calling @gserial_cleanup() before module unload.
713  */
714 int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
715 {
716         struct f_acm    *acm;
717         int             status;
718
719         if (!can_support_cdc(c))
720                 return -EINVAL;
721
722         /* REVISIT might want instance-specific strings to help
723          * distinguish instances ...
724          */
725
726         /* maybe allocate device-global string IDs, and patch descriptors */
727         if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
728                 status = usb_string_id(c->cdev);
729                 if (status < 0)
730                         return status;
731                 acm_string_defs[ACM_CTRL_IDX].id = status;
732
733                 acm_control_interface_desc.iInterface = status;
734
735                 status = usb_string_id(c->cdev);
736                 if (status < 0)
737                         return status;
738                 acm_string_defs[ACM_DATA_IDX].id = status;
739
740                 acm_data_interface_desc.iInterface = status;
741         }
742
743         /* allocate and initialize one new instance */
744         acm = kzalloc(sizeof *acm, GFP_KERNEL);
745         if (!acm)
746                 return -ENOMEM;
747
748         spin_lock_init(&acm->lock);
749
750         acm->port_num = port_num;
751
752         acm->port.connect = acm_connect;
753         acm->port.disconnect = acm_disconnect;
754         acm->port.send_break = acm_send_break;
755
756         acm->port.func.name = "acm";
757         acm->port.func.strings = acm_strings;
758         /* descriptors are per-instance copies */
759         acm->port.func.bind = acm_bind;
760         acm->port.func.unbind = acm_unbind;
761         acm->port.func.set_alt = acm_set_alt;
762         acm->port.func.setup = acm_setup;
763         acm->port.func.disable = acm_disable;
764
765         status = usb_add_function(c, &acm->port.func);
766         if (status)
767                 kfree(acm);
768         return status;
769 }
770
771 #ifdef CONFIG_USB_ANDROID_ACM
772
773 int acm_function_bind_config(struct usb_configuration *c)
774 {
775         int ret = acm_bind_config(c, 0);
776         if (ret == 0)
777                 gserial_setup(c->cdev->gadget, 1);
778         return ret;
779 }
780
781 static struct android_usb_function acm_function = {
782         .name = "acm",
783         .bind_config = acm_function_bind_config,
784 };
785
786 static int __init f_acm_init(void)
787 {
788         printf("f_acm init\n");
789         android_register_function(&acm_function);
790         return 0;
791 }
792 module_init(init);
793
794 #endif /* CONFIG_USB_ANDROID_ACM */