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