On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / usb / gadget / f_acm.c
blob7953948bfe4a3e93892447dace87e774ec3cc09c
1 /*
2 * f_acm.c -- USB CDC serial (ACM) function driver
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
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.
13 /* #define VERBOSE_DEBUG */
15 #include <linux/kernel.h>
16 #include <linux/device.h>
18 #include "u_serial.h"
19 #include "gadget_chips.h"
23 * This CDC ACM function support just wraps control functions and
24 * notifications around the generic serial-over-usb code.
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.
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.
39 struct acm_ep_descs {
40 struct usb_endpoint_descriptor *in;
41 struct usb_endpoint_descriptor *out;
42 struct usb_endpoint_descriptor *notify;
45 struct f_acm {
46 struct gserial port;
47 u8 ctrl_id, data_id;
48 u8 port_num;
50 u8 pending;
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.
56 spinlock_t lock;
58 struct acm_ep_descs fs;
59 struct acm_ep_descs hs;
61 struct usb_ep *notify;
62 struct usb_endpoint_descriptor *notify_desc;
63 struct usb_request *notify_req;
65 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
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 */
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)
83 static inline struct f_acm *func_to_acm(struct usb_function *f)
85 return container_of(f, struct f_acm, port.func);
88 static inline struct f_acm *port_to_acm(struct gserial *p)
90 return container_of(p, struct f_acm, port);
93 /*-------------------------------------------------------------------------*/
95 /* notification endpoint uses smallish and infrequent fixed-size messages */
97 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
98 #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
100 /* interface and class descriptors: */
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 */
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 */
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),
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 */
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,
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 */
155 /* full speed support: */
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,
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,
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,
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,
193 /* high speed support: */
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,
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),
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),
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,
231 /* string descriptors: */
233 #define ACM_CTRL_IDX 0
234 #define ACM_DATA_IDX 1
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 */ },
243 static struct usb_gadget_strings acm_string_table = {
244 .language = 0x0409, /* en-us */
245 .strings = acm_string_defs,
248 static struct usb_gadget_strings *acm_strings[] = {
249 &acm_string_table,
250 NULL,
253 /*-------------------------------------------------------------------------*/
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.
261 static void acm_complete_set_line_coding(struct usb_ep *ep,
262 struct usb_request *req)
264 struct f_acm *acm = ep->driver_data;
265 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
267 if (req->status != 0) {
268 DBG(cdev, "acm ttyGS%d completion, err %d\n",
269 acm->port_num, req->status);
270 return;
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;
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.
288 acm->port_line_coding = *value;
292 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
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);
302 /* composite driver infrastructure handles everything except
303 * CDC class messages; interface activation uses set_alt().
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.
310 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
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;
319 value = w_length;
320 cdev->gadget->ep0->driver_data = acm;
321 req->complete = acm_complete_set_line_coding;
322 break;
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;
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;
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;
341 value = 0;
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.
347 acm->port_handshake_bits = w_value;
348 break;
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);
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);
370 /* device either stalls (value < 0) or reports success */
371 return value;
374 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
376 struct f_acm *acm = func_to_acm(f);
377 struct usb_composite_dev *cdev = f->config->cdev;
379 /* we know alt == 0, so this is an activation or a reset */
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);
391 usb_ep_enable(acm->notify, acm->notify_desc);
392 acm->notify->driver_data = acm;
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);
405 gserial_connect(&acm->port, acm->port_num);
407 } else
408 return -EINVAL;
410 return 0;
413 static void acm_disable(struct usb_function *f)
415 struct f_acm *acm = func_to_acm(f);
416 struct usb_composite_dev *cdev = f->config->cdev;
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;
424 /*-------------------------------------------------------------------------*/
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
435 * Returns zero on sucess or a negative errno.
437 * See section 6.3.5 of the CDC 1.1 specification for information
438 * about the only notification we issue: SerialState change.
440 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
441 void *data, unsigned length)
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;
450 req = acm->notify_req;
451 acm->notify_req = NULL;
452 acm->pending = false;
454 req->length = len;
455 notify = req->buf;
456 buf = notify + 1;
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);
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);
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;
478 return status;
481 static int acm_notify_serial_state(struct f_acm *acm)
483 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
484 int status;
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;
496 spin_unlock(&acm->lock);
497 return status;
500 static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
502 struct f_acm *acm = req->context;
503 u8 doit = false;
505 /* on this call path we do NOT hold the port spinlock,
506 * which is why ACM needs its own spinlock
508 spin_lock(&acm->lock);
509 if (req->status != -ESHUTDOWN)
510 doit = acm->pending;
511 acm->notify_req = req;
512 spin_unlock(&acm->lock);
514 if (doit)
515 acm_notify_serial_state(acm);
518 /* connect == the TTY link is open */
520 static void acm_connect(struct gserial *port)
522 struct f_acm *acm = port_to_acm(port);
524 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
525 acm_notify_serial_state(acm);
528 static void acm_disconnect(struct gserial *port)
530 struct f_acm *acm = port_to_acm(port);
532 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
533 acm_notify_serial_state(acm);
536 static int acm_send_break(struct gserial *port, int duration)
538 struct f_acm *acm = port_to_acm(port);
539 u16 state;
541 state = acm->serial_state;
542 state &= ~ACM_CTRL_BRK;
543 if (duration)
544 state |= ACM_CTRL_BRK;
546 acm->serial_state = state;
547 return acm_notify_serial_state(acm);
550 /*-------------------------------------------------------------------------*/
552 /* ACM function driver setup/binding */
553 static int __init
554 acm_bind(struct usb_configuration *c, struct usb_function *f)
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;
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;
567 acm_control_interface_desc.bInterfaceNumber = status;
568 acm_union_desc .bMasterInterface0 = status;
570 status = usb_interface_id(c, f);
571 if (status < 0)
572 goto fail;
573 acm->data_id = status;
575 acm_data_interface_desc.bInterfaceNumber = status;
576 acm_union_desc.bSlaveInterface0 = status;
577 acm_call_mgmt_descriptor.bDataInterface = status;
579 status = -ENODEV;
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 */
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 */
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 */
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;
607 acm->notify_req->complete = acm_cdc_notify_complete;
608 acm->notify_req->context = acm;
610 /* copy descriptors, and track endpoint copies */
611 f->descriptors = usb_copy_descriptors(acm_fs_function);
612 if (!f->descriptors)
613 goto fail;
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);
622 /* support all relevant hardware speeds... we expect that when
623 * hardware is dual speed, all bulk-capable endpoints work at
624 * both speeds
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;
634 /* copy descriptors, and track endpoint copies */
635 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
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);
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;
652 fail:
653 if (acm->notify_req)
654 gs_free_req(acm->notify, acm->notify_req);
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;
664 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
666 return status;
669 static void
670 acm_unbind(struct usb_configuration *c, struct usb_function *f)
672 struct f_acm *acm = func_to_acm(f);
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);
681 /* Some controllers can't support CDC ACM ... */
682 static inline bool can_support_cdc(struct usb_configuration *c)
684 /* SH3 doesn't support multiple interfaces */
685 if (gadget_is_sh(c->cdev->gadget))
686 return false;
688 /* sa1100 doesn't have a third interrupt endpoint */
689 if (gadget_is_sa1100(c->cdev->gadget))
690 return false;
692 /* everything else is *probably* fine ... */
693 return true;
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
702 * Returns zero on success, else negative errno.
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.
708 int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
710 struct f_acm *acm;
711 int status;
713 if (!can_support_cdc(c))
714 return -EINVAL;
716 /* REVISIT might want instance-specific strings to help
717 * distinguish instances ...
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;
727 acm_control_interface_desc.iInterface = status;
729 status = usb_string_id(c->cdev);
730 if (status < 0)
731 return status;
732 acm_string_defs[ACM_DATA_IDX].id = status;
734 acm_data_interface_desc.iInterface = status;
737 /* allocate and initialize one new instance */
738 acm = kzalloc(sizeof *acm, GFP_KERNEL);
739 if (!acm)
740 return -ENOMEM;
742 spin_lock_init(&acm->lock);
744 acm->port_num = port_num;
746 acm->port.connect = acm_connect;
747 acm->port.disconnect = acm_disconnect;
748 acm->port.send_break = acm_send_break;
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;
759 status = usb_add_function(c, &acm->port.func);
760 if (status)
761 kfree(acm);
762 return status;