tree-wide: fix assorted typos all over the place
[safe/jmp/linux-2.6] / 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
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 =               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 =       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 =       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 =       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 =       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                 /* FIXME we should not allow data to flow until the
344                  * host sets the ACM_CTRL_DTR bit; and when it clears
345                  * that bit, we should return to that no-flow state.
346                  */
347                 acm->port_handshake_bits = w_value;
348                 break;
349
350         default:
351 invalid:
352                 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
353                         ctrl->bRequestType, ctrl->bRequest,
354                         w_value, w_index, w_length);
355         }
356
357         /* respond with data transfer or status phase? */
358         if (value >= 0) {
359                 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
360                         acm->port_num, ctrl->bRequestType, ctrl->bRequest,
361                         w_value, w_index, w_length);
362                 req->zero = 0;
363                 req->length = value;
364                 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
365                 if (value < 0)
366                         ERROR(cdev, "acm response on ttyGS%d, err %d\n",
367                                         acm->port_num, value);
368         }
369
370         /* device either stalls (value < 0) or reports success */
371         return value;
372 }
373
374 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
375 {
376         struct f_acm            *acm = func_to_acm(f);
377         struct usb_composite_dev *cdev = f->config->cdev;
378
379         /* we know alt == 0, so this is an activation or a reset */
380
381         if (intf == acm->ctrl_id) {
382                 if (acm->notify->driver_data) {
383                         VDBG(cdev, "reset acm control interface %d\n", intf);
384                         usb_ep_disable(acm->notify);
385                 } else {
386                         VDBG(cdev, "init acm ctrl interface %d\n", intf);
387                         acm->notify_desc = ep_choose(cdev->gadget,
388                                         acm->hs.notify,
389                                         acm->fs.notify);
390                 }
391                 usb_ep_enable(acm->notify, acm->notify_desc);
392                 acm->notify->driver_data = acm;
393
394         } else if (intf == acm->data_id) {
395                 if (acm->port.in->driver_data) {
396                         DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
397                         gserial_disconnect(&acm->port);
398                 } else {
399                         DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
400                         acm->port.in_desc = ep_choose(cdev->gadget,
401                                         acm->hs.in, acm->fs.in);
402                         acm->port.out_desc = ep_choose(cdev->gadget,
403                                         acm->hs.out, acm->fs.out);
404                 }
405                 gserial_connect(&acm->port, acm->port_num);
406
407         } else
408                 return -EINVAL;
409
410         return 0;
411 }
412
413 static void acm_disable(struct usb_function *f)
414 {
415         struct f_acm    *acm = func_to_acm(f);
416         struct usb_composite_dev *cdev = f->config->cdev;
417
418         DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
419         gserial_disconnect(&acm->port);
420         usb_ep_disable(acm->notify);
421         acm->notify->driver_data = NULL;
422 }
423
424 /*-------------------------------------------------------------------------*/
425
426 /**
427  * acm_cdc_notify - issue CDC notification to host
428  * @acm: wraps host to be notified
429  * @type: notification type
430  * @value: Refer to cdc specs, wValue field.
431  * @data: data to be sent
432  * @length: size of data
433  * Context: irqs blocked, acm->lock held, acm_notify_req non-null
434  *
435  * Returns zero on success or a negative errno.
436  *
437  * See section 6.3.5 of the CDC 1.1 specification for information
438  * about the only notification we issue:  SerialState change.
439  */
440 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
441                 void *data, unsigned length)
442 {
443         struct usb_ep                   *ep = acm->notify;
444         struct usb_request              *req;
445         struct usb_cdc_notification     *notify;
446         const unsigned                  len = sizeof(*notify) + length;
447         void                            *buf;
448         int                             status;
449
450         req = acm->notify_req;
451         acm->notify_req = NULL;
452         acm->pending = false;
453
454         req->length = len;
455         notify = req->buf;
456         buf = notify + 1;
457
458         notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
459                         | USB_RECIP_INTERFACE;
460         notify->bNotificationType = type;
461         notify->wValue = cpu_to_le16(value);
462         notify->wIndex = cpu_to_le16(acm->ctrl_id);
463         notify->wLength = cpu_to_le16(length);
464         memcpy(buf, data, length);
465
466         /* ep_queue() can complete immediately if it fills the fifo... */
467         spin_unlock(&acm->lock);
468         status = usb_ep_queue(ep, req, GFP_ATOMIC);
469         spin_lock(&acm->lock);
470
471         if (status < 0) {
472                 ERROR(acm->port.func.config->cdev,
473                                 "acm ttyGS%d can't notify serial state, %d\n",
474                                 acm->port_num, status);
475                 acm->notify_req = req;
476         }
477
478         return status;
479 }
480
481 static int acm_notify_serial_state(struct f_acm *acm)
482 {
483         struct usb_composite_dev *cdev = acm->port.func.config->cdev;
484         int                     status;
485
486         spin_lock(&acm->lock);
487         if (acm->notify_req) {
488                 DBG(cdev, "acm ttyGS%d serial state %04x\n",
489                                 acm->port_num, acm->serial_state);
490                 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
491                                 0, &acm->serial_state, sizeof(acm->serial_state));
492         } else {
493                 acm->pending = true;
494                 status = 0;
495         }
496         spin_unlock(&acm->lock);
497         return status;
498 }
499
500 static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
501 {
502         struct f_acm            *acm = req->context;
503         u8                      doit = false;
504
505         /* on this call path we do NOT hold the port spinlock,
506          * which is why ACM needs its own spinlock
507          */
508         spin_lock(&acm->lock);
509         if (req->status != -ESHUTDOWN)
510                 doit = acm->pending;
511         acm->notify_req = req;
512         spin_unlock(&acm->lock);
513
514         if (doit)
515                 acm_notify_serial_state(acm);
516 }
517
518 /* connect == the TTY link is open */
519
520 static void acm_connect(struct gserial *port)
521 {
522         struct f_acm            *acm = port_to_acm(port);
523
524         acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
525         acm_notify_serial_state(acm);
526 }
527
528 static void acm_disconnect(struct gserial *port)
529 {
530         struct f_acm            *acm = port_to_acm(port);
531
532         acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
533         acm_notify_serial_state(acm);
534 }
535
536 static int acm_send_break(struct gserial *port, int duration)
537 {
538         struct f_acm            *acm = port_to_acm(port);
539         u16                     state;
540
541         state = acm->serial_state;
542         state &= ~ACM_CTRL_BRK;
543         if (duration)
544                 state |= ACM_CTRL_BRK;
545
546         acm->serial_state = state;
547         return acm_notify_serial_state(acm);
548 }
549
550 /*-------------------------------------------------------------------------*/
551
552 /* ACM function driver setup/binding */
553 static int __init
554 acm_bind(struct usb_configuration *c, struct usb_function *f)
555 {
556         struct usb_composite_dev *cdev = c->cdev;
557         struct f_acm            *acm = func_to_acm(f);
558         int                     status;
559         struct usb_ep           *ep;
560
561         /* allocate instance-specific interface IDs, and patch descriptors */
562         status = usb_interface_id(c, f);
563         if (status < 0)
564                 goto fail;
565         acm->ctrl_id = status;
566
567         acm_control_interface_desc.bInterfaceNumber = status;
568         acm_union_desc .bMasterInterface0 = status;
569
570         status = usb_interface_id(c, f);
571         if (status < 0)
572                 goto fail;
573         acm->data_id = status;
574
575         acm_data_interface_desc.bInterfaceNumber = status;
576         acm_union_desc.bSlaveInterface0 = status;
577         acm_call_mgmt_descriptor.bDataInterface = status;
578
579         status = -ENODEV;
580
581         /* allocate instance-specific endpoints */
582         ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
583         if (!ep)
584                 goto fail;
585         acm->port.in = ep;
586         ep->driver_data = cdev; /* claim */
587
588         ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
589         if (!ep)
590                 goto fail;
591         acm->port.out = ep;
592         ep->driver_data = cdev; /* claim */
593
594         ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
595         if (!ep)
596                 goto fail;
597         acm->notify = ep;
598         ep->driver_data = cdev; /* claim */
599
600         /* allocate notification */
601         acm->notify_req = gs_alloc_req(ep,
602                         sizeof(struct usb_cdc_notification) + 2,
603                         GFP_KERNEL);
604         if (!acm->notify_req)
605                 goto fail;
606
607         acm->notify_req->complete = acm_cdc_notify_complete;
608         acm->notify_req->context = acm;
609
610         /* copy descriptors, and track endpoint copies */
611         f->descriptors = usb_copy_descriptors(acm_fs_function);
612         if (!f->descriptors)
613                 goto fail;
614
615         acm->fs.in = usb_find_endpoint(acm_fs_function,
616                         f->descriptors, &acm_fs_in_desc);
617         acm->fs.out = usb_find_endpoint(acm_fs_function,
618                         f->descriptors, &acm_fs_out_desc);
619         acm->fs.notify = usb_find_endpoint(acm_fs_function,
620                         f->descriptors, &acm_fs_notify_desc);
621
622         /* support all relevant hardware speeds... we expect that when
623          * hardware is dual speed, all bulk-capable endpoints work at
624          * both speeds
625          */
626         if (gadget_is_dualspeed(c->cdev->gadget)) {
627                 acm_hs_in_desc.bEndpointAddress =
628                                 acm_fs_in_desc.bEndpointAddress;
629                 acm_hs_out_desc.bEndpointAddress =
630                                 acm_fs_out_desc.bEndpointAddress;
631                 acm_hs_notify_desc.bEndpointAddress =
632                                 acm_fs_notify_desc.bEndpointAddress;
633
634                 /* copy descriptors, and track endpoint copies */
635                 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
636
637                 acm->hs.in = usb_find_endpoint(acm_hs_function,
638                                 f->hs_descriptors, &acm_hs_in_desc);
639                 acm->hs.out = usb_find_endpoint(acm_hs_function,
640                                 f->hs_descriptors, &acm_hs_out_desc);
641                 acm->hs.notify = usb_find_endpoint(acm_hs_function,
642                                 f->hs_descriptors, &acm_hs_notify_desc);
643         }
644
645         DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
646                         acm->port_num,
647                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
648                         acm->port.in->name, acm->port.out->name,
649                         acm->notify->name);
650         return 0;
651
652 fail:
653         if (acm->notify_req)
654                 gs_free_req(acm->notify, acm->notify_req);
655
656         /* we might as well release our claims on endpoints */
657         if (acm->notify)
658                 acm->notify->driver_data = NULL;
659         if (acm->port.out)
660                 acm->port.out->driver_data = NULL;
661         if (acm->port.in)
662                 acm->port.in->driver_data = NULL;
663
664         ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
665
666         return status;
667 }
668
669 static void
670 acm_unbind(struct usb_configuration *c, struct usb_function *f)
671 {
672         struct f_acm            *acm = func_to_acm(f);
673
674         if (gadget_is_dualspeed(c->cdev->gadget))
675                 usb_free_descriptors(f->hs_descriptors);
676         usb_free_descriptors(f->descriptors);
677         gs_free_req(acm->notify, acm->notify_req);
678         kfree(acm);
679 }
680
681 /* Some controllers can't support CDC ACM ... */
682 static inline bool can_support_cdc(struct usb_configuration *c)
683 {
684         /* SH3 doesn't support multiple interfaces */
685         if (gadget_is_sh(c->cdev->gadget))
686                 return false;
687
688         /* sa1100 doesn't have a third interrupt endpoint */
689         if (gadget_is_sa1100(c->cdev->gadget))
690                 return false;
691
692         /* everything else is *probably* fine ... */
693         return true;
694 }
695
696 /**
697  * acm_bind_config - add a CDC ACM function to a configuration
698  * @c: the configuration to support the CDC ACM instance
699  * @port_num: /dev/ttyGS* port this interface will use
700  * Context: single threaded during gadget setup
701  *
702  * Returns zero on success, else negative errno.
703  *
704  * Caller must have called @gserial_setup() with enough ports to
705  * handle all the ones it binds.  Caller is also responsible
706  * for calling @gserial_cleanup() before module unload.
707  */
708 int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
709 {
710         struct f_acm    *acm;
711         int             status;
712
713         if (!can_support_cdc(c))
714                 return -EINVAL;
715
716         /* REVISIT might want instance-specific strings to help
717          * distinguish instances ...
718          */
719
720         /* maybe allocate device-global string IDs, and patch descriptors */
721         if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
722                 status = usb_string_id(c->cdev);
723                 if (status < 0)
724                         return status;
725                 acm_string_defs[ACM_CTRL_IDX].id = status;
726
727                 acm_control_interface_desc.iInterface = status;
728
729                 status = usb_string_id(c->cdev);
730                 if (status < 0)
731                         return status;
732                 acm_string_defs[ACM_DATA_IDX].id = status;
733
734                 acm_data_interface_desc.iInterface = status;
735         }
736
737         /* allocate and initialize one new instance */
738         acm = kzalloc(sizeof *acm, GFP_KERNEL);
739         if (!acm)
740                 return -ENOMEM;
741
742         spin_lock_init(&acm->lock);
743
744         acm->port_num = port_num;
745
746         acm->port.connect = acm_connect;
747         acm->port.disconnect = acm_disconnect;
748         acm->port.send_break = acm_send_break;
749
750         acm->port.func.name = "acm";
751         acm->port.func.strings = acm_strings;
752         /* descriptors are per-instance copies */
753         acm->port.func.bind = acm_bind;
754         acm->port.func.unbind = acm_unbind;
755         acm->port.func.set_alt = acm_set_alt;
756         acm->port.func.setup = acm_setup;
757         acm->port.func.disable = acm_disable;
758
759         status = usb_add_function(c, &acm->port.func);
760         if (status)
761                 kfree(acm);
762         return status;
763 }