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