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
7 * Copyright (C) 2009 by Samsung Electronics
8 * Author: Michal Nazarewicz (mina86@mina86.com)
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.
15 /* #define VERBOSE_DEBUG */
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
22 #include "gadget_chips.h"
26 * This CDC ACM function support just wraps control functions and
27 * notifications around the generic serial-over-usb code.
29 * Because CDC ACM is standardized by the USB-IF, many host operating
30 * systems have drivers for it. Accordingly, ACM is the preferred
31 * interop solution for serial-port type connections. The control
32 * models are often not necessary, and in any case don't do much in
33 * this bare-bones implementation.
35 * Note that even MS-Windows has some support for ACM. However, that
36 * support is somewhat broken because when you use ACM in a composite
37 * device, having multiple interfaces confuses the poor OS. It doesn't
38 * seem to understand CDC Union descriptors. The new "association"
39 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
49 /* lock is mostly for pending and notify_req ... they get accessed
50 * by callbacks both from tty (open/close/break) under its spinlock,
51 * and notify_req.complete() which can't use that lock.
55 struct usb_ep *notify;
56 struct usb_request *notify_req;
58 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
60 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
61 u16 port_handshake_bits;
62 #define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
63 #define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
65 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
67 #define ACM_CTRL_OVERRUN (1 << 6)
68 #define ACM_CTRL_PARITY (1 << 5)
69 #define ACM_CTRL_FRAMING (1 << 4)
70 #define ACM_CTRL_RI (1 << 3)
71 #define ACM_CTRL_BRK (1 << 2)
72 #define ACM_CTRL_DSR (1 << 1)
73 #define ACM_CTRL_DCD (1 << 0)
76 static inline struct f_acm *func_to_acm(struct usb_function *f)
78 return container_of(f, struct f_acm, port.func);
81 static inline struct f_acm *port_to_acm(struct gserial *p)
83 return container_of(p, struct f_acm, port);
86 /*-------------------------------------------------------------------------*/
88 /* notification endpoint uses smallish and infrequent fixed-size messages */
90 #define GS_NOTIFY_INTERVAL_MS 32
91 #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
93 /* interface and class descriptors: */
95 static struct usb_interface_assoc_descriptor
96 acm_iad_descriptor = {
97 .bLength = sizeof acm_iad_descriptor,
98 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
100 /* .bFirstInterface = DYNAMIC, */
101 .bInterfaceCount = 2, // control + data
102 .bFunctionClass = USB_CLASS_COMM,
103 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
104 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
105 /* .iFunction = DYNAMIC */
109 static struct usb_interface_descriptor acm_control_interface_desc = {
110 .bLength = USB_DT_INTERFACE_SIZE,
111 .bDescriptorType = USB_DT_INTERFACE,
112 /* .bInterfaceNumber = DYNAMIC */
114 .bInterfaceClass = USB_CLASS_COMM,
115 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
116 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
117 /* .iInterface = DYNAMIC */
120 static struct usb_interface_descriptor acm_data_interface_desc = {
121 .bLength = USB_DT_INTERFACE_SIZE,
122 .bDescriptorType = USB_DT_INTERFACE,
123 /* .bInterfaceNumber = DYNAMIC */
125 .bInterfaceClass = USB_CLASS_CDC_DATA,
126 .bInterfaceSubClass = 0,
127 .bInterfaceProtocol = 0,
128 /* .iInterface = DYNAMIC */
131 static struct usb_cdc_header_desc acm_header_desc = {
132 .bLength = sizeof(acm_header_desc),
133 .bDescriptorType = USB_DT_CS_INTERFACE,
134 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
135 .bcdCDC = cpu_to_le16(0x0110),
138 static struct usb_cdc_call_mgmt_descriptor
139 acm_call_mgmt_descriptor = {
140 .bLength = sizeof(acm_call_mgmt_descriptor),
141 .bDescriptorType = USB_DT_CS_INTERFACE,
142 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
144 /* .bDataInterface = DYNAMIC */
147 static struct usb_cdc_acm_descriptor acm_descriptor = {
148 .bLength = sizeof(acm_descriptor),
149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_ACM_TYPE,
151 .bmCapabilities = USB_CDC_CAP_LINE,
154 static struct usb_cdc_union_desc acm_union_desc = {
155 .bLength = sizeof(acm_union_desc),
156 .bDescriptorType = USB_DT_CS_INTERFACE,
157 .bDescriptorSubType = USB_CDC_UNION_TYPE,
158 /* .bMasterInterface0 = DYNAMIC */
159 /* .bSlaveInterface0 = DYNAMIC */
162 /* full speed support: */
164 static struct usb_endpoint_descriptor acm_fs_notify_desc = {
165 .bLength = USB_DT_ENDPOINT_SIZE,
166 .bDescriptorType = USB_DT_ENDPOINT,
167 .bEndpointAddress = USB_DIR_IN,
168 .bmAttributes = USB_ENDPOINT_XFER_INT,
169 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
170 .bInterval = GS_NOTIFY_INTERVAL_MS,
173 static struct usb_endpoint_descriptor acm_fs_in_desc = {
174 .bLength = USB_DT_ENDPOINT_SIZE,
175 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = USB_DIR_IN,
177 .bmAttributes = USB_ENDPOINT_XFER_BULK,
180 static struct usb_endpoint_descriptor acm_fs_out_desc = {
181 .bLength = USB_DT_ENDPOINT_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = USB_DIR_OUT,
184 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187 static struct usb_descriptor_header *acm_fs_function[] = {
188 (struct usb_descriptor_header *) &acm_iad_descriptor,
189 (struct usb_descriptor_header *) &acm_control_interface_desc,
190 (struct usb_descriptor_header *) &acm_header_desc,
191 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
192 (struct usb_descriptor_header *) &acm_descriptor,
193 (struct usb_descriptor_header *) &acm_union_desc,
194 (struct usb_descriptor_header *) &acm_fs_notify_desc,
195 (struct usb_descriptor_header *) &acm_data_interface_desc,
196 (struct usb_descriptor_header *) &acm_fs_in_desc,
197 (struct usb_descriptor_header *) &acm_fs_out_desc,
201 /* high speed support: */
202 static struct usb_endpoint_descriptor acm_hs_notify_desc = {
203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_INT,
207 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
208 .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
211 static struct usb_endpoint_descriptor acm_hs_in_desc = {
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_endpoint_descriptor acm_hs_out_desc = {
219 .bLength = USB_DT_ENDPOINT_SIZE,
220 .bDescriptorType = USB_DT_ENDPOINT,
221 .bmAttributes = USB_ENDPOINT_XFER_BULK,
222 .wMaxPacketSize = cpu_to_le16(512),
225 static struct usb_descriptor_header *acm_hs_function[] = {
226 (struct usb_descriptor_header *) &acm_iad_descriptor,
227 (struct usb_descriptor_header *) &acm_control_interface_desc,
228 (struct usb_descriptor_header *) &acm_header_desc,
229 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
230 (struct usb_descriptor_header *) &acm_descriptor,
231 (struct usb_descriptor_header *) &acm_union_desc,
232 (struct usb_descriptor_header *) &acm_hs_notify_desc,
233 (struct usb_descriptor_header *) &acm_data_interface_desc,
234 (struct usb_descriptor_header *) &acm_hs_in_desc,
235 (struct usb_descriptor_header *) &acm_hs_out_desc,
239 static struct usb_endpoint_descriptor acm_ss_in_desc = {
240 .bLength = USB_DT_ENDPOINT_SIZE,
241 .bDescriptorType = USB_DT_ENDPOINT,
242 .bmAttributes = USB_ENDPOINT_XFER_BULK,
243 .wMaxPacketSize = cpu_to_le16(1024),
246 static struct usb_endpoint_descriptor acm_ss_out_desc = {
247 .bLength = USB_DT_ENDPOINT_SIZE,
248 .bDescriptorType = USB_DT_ENDPOINT,
249 .bmAttributes = USB_ENDPOINT_XFER_BULK,
250 .wMaxPacketSize = cpu_to_le16(1024),
253 static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
254 .bLength = sizeof acm_ss_bulk_comp_desc,
255 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
258 static struct usb_descriptor_header *acm_ss_function[] = {
259 (struct usb_descriptor_header *) &acm_iad_descriptor,
260 (struct usb_descriptor_header *) &acm_control_interface_desc,
261 (struct usb_descriptor_header *) &acm_header_desc,
262 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
263 (struct usb_descriptor_header *) &acm_descriptor,
264 (struct usb_descriptor_header *) &acm_union_desc,
265 (struct usb_descriptor_header *) &acm_hs_notify_desc,
266 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
267 (struct usb_descriptor_header *) &acm_data_interface_desc,
268 (struct usb_descriptor_header *) &acm_ss_in_desc,
269 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
270 (struct usb_descriptor_header *) &acm_ss_out_desc,
271 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
275 /* string descriptors: */
277 #define ACM_CTRL_IDX 0
278 #define ACM_DATA_IDX 1
279 #define ACM_IAD_IDX 2
281 /* static strings, in UTF-8 */
282 static struct usb_string acm_string_defs[] = {
283 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
284 [ACM_DATA_IDX].s = "CDC ACM Data",
285 [ACM_IAD_IDX ].s = "CDC Serial",
286 { /* ZEROES END LIST */ },
289 static struct usb_gadget_strings acm_string_table = {
290 .language = 0x0409, /* en-us */
291 .strings = acm_string_defs,
294 static struct usb_gadget_strings *acm_strings[] = {
299 /*-------------------------------------------------------------------------*/
301 /* ACM control ... data handling is delegated to tty library code.
302 * The main task of this function is to activate and deactivate
303 * that code based on device state; track parameters like line
304 * speed, handshake state, and so on; and issue notifications.
307 static void acm_complete_set_line_coding(struct usb_ep *ep,
308 struct usb_request *req)
310 struct f_acm *acm = ep->driver_data;
311 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
313 if (req->status != 0) {
314 DBG(cdev, "acm ttyGS%d completion, err %d\n",
315 acm->port_num, req->status);
319 /* normal completion */
320 if (req->actual != sizeof(acm->port_line_coding)) {
321 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
322 acm->port_num, req->actual);
325 struct usb_cdc_line_coding *value = req->buf;
327 /* REVISIT: we currently just remember this data.
328 * If we change that, (a) validate it first, then
329 * (b) update whatever hardware needs updating,
330 * (c) worry about locking. This is information on
331 * the order of 9600-8-N-1 ... most of which means
332 * nothing unless we control a real RS232 line.
334 acm->port_line_coding = *value;
338 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
340 struct f_acm *acm = func_to_acm(f);
341 struct usb_composite_dev *cdev = f->config->cdev;
342 struct usb_request *req = cdev->req;
343 int value = -EOPNOTSUPP;
344 u16 w_index = le16_to_cpu(ctrl->wIndex);
345 u16 w_value = le16_to_cpu(ctrl->wValue);
346 u16 w_length = le16_to_cpu(ctrl->wLength);
348 /* composite driver infrastructure handles everything except
349 * CDC class messages; interface activation uses set_alt().
351 * Note CDC spec table 4 lists the ACM request profile. It requires
352 * encapsulated command support ... we don't handle any, and respond
353 * to them by stalling. Options include get/set/clear comm features
354 * (not that useful) and SEND_BREAK.
356 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
358 /* SET_LINE_CODING ... just read and save what the host sends */
359 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
360 | USB_CDC_REQ_SET_LINE_CODING:
361 if (w_length != sizeof(struct usb_cdc_line_coding)
362 || w_index != acm->ctrl_id)
366 cdev->gadget->ep0->driver_data = acm;
367 req->complete = acm_complete_set_line_coding;
370 /* GET_LINE_CODING ... return what host sent, or initial value */
371 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
372 | USB_CDC_REQ_GET_LINE_CODING:
373 if (w_index != acm->ctrl_id)
376 value = min_t(unsigned, w_length,
377 sizeof(struct usb_cdc_line_coding));
378 memcpy(req->buf, &acm->port_line_coding, value);
381 /* SET_CONTROL_LINE_STATE ... save what the host sent */
382 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
383 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
384 if (w_index != acm->ctrl_id)
389 /* FIXME we should not allow data to flow until the
390 * host sets the ACM_CTRL_DTR bit; and when it clears
391 * that bit, we should return to that no-flow state.
393 acm->port_handshake_bits = w_value;
398 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
399 ctrl->bRequestType, ctrl->bRequest,
400 w_value, w_index, w_length);
403 /* respond with data transfer or status phase? */
405 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
406 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
407 w_value, w_index, w_length);
410 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
412 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
413 acm->port_num, value);
416 /* device either stalls (value < 0) or reports success */
420 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
422 struct f_acm *acm = func_to_acm(f);
423 struct usb_composite_dev *cdev = f->config->cdev;
425 /* we know alt == 0, so this is an activation or a reset */
427 if (intf == acm->ctrl_id) {
428 if (acm->notify->driver_data) {
429 VDBG(cdev, "reset acm control interface %d\n", intf);
430 usb_ep_disable(acm->notify);
432 VDBG(cdev, "init acm ctrl interface %d\n", intf);
433 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
436 usb_ep_enable(acm->notify);
437 acm->notify->driver_data = acm;
439 } else if (intf == acm->data_id) {
440 if (acm->port.in->driver_data) {
441 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
442 gserial_disconnect(&acm->port);
444 if (!acm->port.in->desc || !acm->port.out->desc) {
445 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
446 if (config_ep_by_speed(cdev->gadget, f,
448 config_ep_by_speed(cdev->gadget, f,
450 acm->port.in->desc = NULL;
451 acm->port.out->desc = NULL;
455 gserial_connect(&acm->port, acm->port_num);
463 static void acm_disable(struct usb_function *f)
465 struct f_acm *acm = func_to_acm(f);
466 struct usb_composite_dev *cdev = f->config->cdev;
468 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
469 gserial_disconnect(&acm->port);
470 usb_ep_disable(acm->notify);
471 acm->notify->driver_data = NULL;
474 /*-------------------------------------------------------------------------*/
477 * acm_cdc_notify - issue CDC notification to host
478 * @acm: wraps host to be notified
479 * @type: notification type
480 * @value: Refer to cdc specs, wValue field.
481 * @data: data to be sent
482 * @length: size of data
483 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
485 * Returns zero on success or a negative errno.
487 * See section 6.3.5 of the CDC 1.1 specification for information
488 * about the only notification we issue: SerialState change.
490 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
491 void *data, unsigned length)
493 struct usb_ep *ep = acm->notify;
494 struct usb_request *req;
495 struct usb_cdc_notification *notify;
496 const unsigned len = sizeof(*notify) + length;
500 req = acm->notify_req;
501 acm->notify_req = NULL;
502 acm->pending = false;
508 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
509 | USB_RECIP_INTERFACE;
510 notify->bNotificationType = type;
511 notify->wValue = cpu_to_le16(value);
512 notify->wIndex = cpu_to_le16(acm->ctrl_id);
513 notify->wLength = cpu_to_le16(length);
514 memcpy(buf, data, length);
516 /* ep_queue() can complete immediately if it fills the fifo... */
517 spin_unlock(&acm->lock);
518 status = usb_ep_queue(ep, req, GFP_ATOMIC);
519 spin_lock(&acm->lock);
522 ERROR(acm->port.func.config->cdev,
523 "acm ttyGS%d can't notify serial state, %d\n",
524 acm->port_num, status);
525 acm->notify_req = req;
531 static int acm_notify_serial_state(struct f_acm *acm)
533 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
536 spin_lock(&acm->lock);
537 if (acm->notify_req) {
538 DBG(cdev, "acm ttyGS%d serial state %04x\n",
539 acm->port_num, acm->serial_state);
540 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
541 0, &acm->serial_state, sizeof(acm->serial_state));
546 spin_unlock(&acm->lock);
550 static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
552 struct f_acm *acm = req->context;
555 /* on this call path we do NOT hold the port spinlock,
556 * which is why ACM needs its own spinlock
558 spin_lock(&acm->lock);
559 if (req->status != -ESHUTDOWN)
561 acm->notify_req = req;
562 spin_unlock(&acm->lock);
565 acm_notify_serial_state(acm);
568 /* connect == the TTY link is open */
570 static void acm_connect(struct gserial *port)
572 struct f_acm *acm = port_to_acm(port);
574 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
575 acm_notify_serial_state(acm);
578 static void acm_disconnect(struct gserial *port)
580 struct f_acm *acm = port_to_acm(port);
582 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
583 acm_notify_serial_state(acm);
586 static int acm_send_break(struct gserial *port, int duration)
588 struct f_acm *acm = port_to_acm(port);
591 state = acm->serial_state;
592 state &= ~ACM_CTRL_BRK;
594 state |= ACM_CTRL_BRK;
596 acm->serial_state = state;
597 return acm_notify_serial_state(acm);
600 /*-------------------------------------------------------------------------*/
602 /* ACM function driver setup/binding */
604 acm_bind(struct usb_configuration *c, struct usb_function *f)
606 struct usb_composite_dev *cdev = c->cdev;
607 struct f_acm *acm = func_to_acm(f);
611 /* allocate instance-specific interface IDs, and patch descriptors */
612 status = usb_interface_id(c, f);
615 acm->ctrl_id = status;
616 acm_iad_descriptor.bFirstInterface = status;
618 acm_control_interface_desc.bInterfaceNumber = status;
619 acm_union_desc .bMasterInterface0 = status;
621 status = usb_interface_id(c, f);
624 acm->data_id = status;
626 acm_data_interface_desc.bInterfaceNumber = status;
627 acm_union_desc.bSlaveInterface0 = status;
628 acm_call_mgmt_descriptor.bDataInterface = status;
632 /* allocate instance-specific endpoints */
633 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
637 ep->driver_data = cdev; /* claim */
639 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
643 ep->driver_data = cdev; /* claim */
645 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
649 ep->driver_data = cdev; /* claim */
651 /* allocate notification */
652 acm->notify_req = gs_alloc_req(ep,
653 sizeof(struct usb_cdc_notification) + 2,
655 if (!acm->notify_req)
658 acm->notify_req->complete = acm_cdc_notify_complete;
659 acm->notify_req->context = acm;
661 /* support all relevant hardware speeds... we expect that when
662 * hardware is dual speed, all bulk-capable endpoints work at
665 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
666 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
667 acm_hs_notify_desc.bEndpointAddress =
668 acm_fs_notify_desc.bEndpointAddress;
670 acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
671 acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
673 status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
678 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
680 gadget_is_superspeed(c->cdev->gadget) ? "super" :
681 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
682 acm->port.in->name, acm->port.out->name,
688 gs_free_req(acm->notify, acm->notify_req);
690 /* we might as well release our claims on endpoints */
692 acm->notify->driver_data = NULL;
694 acm->port.out->driver_data = NULL;
696 acm->port.in->driver_data = NULL;
698 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
704 acm_unbind(struct usb_configuration *c, struct usb_function *f)
706 struct f_acm *acm = func_to_acm(f);
708 acm_string_defs[0].id = 0;
709 usb_free_all_descriptors(f);
710 gs_free_req(acm->notify, acm->notify_req);
714 /* Some controllers can't support CDC ACM ... */
715 static inline bool can_support_cdc(struct usb_configuration *c)
717 /* everything else is *probably* fine ... */
722 * acm_bind_config - add a CDC ACM function to a configuration
723 * @c: the configuration to support the CDC ACM instance
724 * @port_num: /dev/ttyGS* port this interface will use
725 * Context: single threaded during gadget setup
727 * Returns zero on success, else negative errno.
729 * Caller must have called @gserial_setup() with enough ports to
730 * handle all the ones it binds. Caller is also responsible
731 * for calling @gserial_cleanup() before module unload.
733 int acm_bind_config(struct usb_configuration *c, u8 port_num)
738 if (!can_support_cdc(c))
741 /* REVISIT might want instance-specific strings to help
742 * distinguish instances ...
745 /* maybe allocate device-global string IDs, and patch descriptors */
746 if (acm_string_defs[0].id == 0) {
747 status = usb_string_ids_tab(c->cdev, acm_string_defs);
750 acm_control_interface_desc.iInterface =
751 acm_string_defs[ACM_CTRL_IDX].id;
752 acm_data_interface_desc.iInterface =
753 acm_string_defs[ACM_DATA_IDX].id;
754 acm_iad_descriptor.iFunction = acm_string_defs[ACM_IAD_IDX].id;
757 /* allocate and initialize one new instance */
758 acm = kzalloc(sizeof *acm, GFP_KERNEL);
762 spin_lock_init(&acm->lock);
764 acm->port_num = port_num;
766 acm->port.connect = acm_connect;
767 acm->port.disconnect = acm_disconnect;
768 acm->port.send_break = acm_send_break;
770 acm->port.func.name = "acm";
771 acm->port.func.strings = acm_strings;
772 /* descriptors are per-instance copies */
773 acm->port.func.bind = acm_bind;
774 acm->port.func.unbind = acm_unbind;
775 acm->port.func.set_alt = acm_set_alt;
776 acm->port.func.setup = acm_setup;
777 acm->port.func.disable = acm_disable;
779 status = usb_add_function(c, &acm->port.func);