1 // SPDX-License-Identifier: GPL-2.0+
3 * USB CDC serial (ACM) function driver
6 * Copyright (C) 2008 by David Brownell
7 * Copyright (C) 2008 by Nokia Corporation
8 * Copyright (C) 2009 by Samsung Electronics
18 #include <stdio_dev.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/composite.h>
25 #include <linux/usb/cdc.h>
27 #define REQ_SIZE_MAX 512
34 struct usb_ep *ep_out;
35 struct usb_ep *ep_notify;
37 struct usb_request *req_in;
38 struct usb_request *req_out;
46 struct usb_function usb_function;
48 struct usb_cdc_line_coding line_coding;
50 #define ACM_CTRL_RTS BIT(1) /* unused with full duplex */
51 #define ACM_CTRL_DTR BIT(0) /* host is ready for data r/w */
56 static struct f_acm *default_acm_function;
58 static inline struct f_acm *func_to_acm(struct usb_function *f)
60 return container_of(f, struct f_acm, usb_function);
63 static inline struct f_acm *stdio_to_acm(struct stdio_dev *s)
65 /* stdio dev is cloned on registration, do not use container_of */
69 static struct usb_interface_assoc_descriptor
70 acm_iad_descriptor = {
71 .bLength = sizeof(acm_iad_descriptor),
72 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
74 .bInterfaceCount = 2, // control + data
75 .bFunctionClass = USB_CLASS_COMM,
76 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
77 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
80 static struct usb_interface_descriptor acm_control_intf_desc = {
81 .bLength = USB_DT_INTERFACE_SIZE,
82 .bDescriptorType = USB_DT_INTERFACE,
84 .bInterfaceClass = USB_CLASS_COMM,
85 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
86 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
89 static struct usb_interface_descriptor acm_data_intf_desc = {
90 .bLength = sizeof(acm_data_intf_desc),
91 .bDescriptorType = USB_DT_INTERFACE,
93 .bInterfaceClass = USB_CLASS_CDC_DATA,
96 static struct usb_cdc_header_desc acm_header_desc = {
97 .bLength = sizeof(acm_header_desc),
98 .bDescriptorType = USB_DT_CS_INTERFACE,
99 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
100 .bcdCDC = __constant_cpu_to_le16(0x0110),
103 static struct usb_cdc_call_mgmt_descriptor acm_call_mgmt_desc = {
104 .bLength = sizeof(acm_call_mgmt_desc),
105 .bDescriptorType = USB_DT_CS_INTERFACE,
106 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
108 .bDataInterface = 0x01,
111 static struct usb_cdc_acm_descriptor acm_desc = {
112 .bLength = sizeof(acm_desc),
113 .bDescriptorType = USB_DT_CS_INTERFACE,
114 .bDescriptorSubType = USB_CDC_ACM_TYPE,
115 .bmCapabilities = USB_CDC_CAP_LINE,
118 static struct usb_cdc_union_desc acm_union_desc = {
119 .bLength = sizeof(acm_union_desc),
120 .bDescriptorType = USB_DT_CS_INTERFACE,
121 .bDescriptorSubType = USB_CDC_UNION_TYPE,
122 .bMasterInterface0 = 0x00,
123 .bSlaveInterface0 = 0x01,
126 static struct usb_endpoint_descriptor acm_fs_notify_desc = {
127 .bLength = USB_DT_ENDPOINT_SIZE,
128 .bDescriptorType = USB_DT_ENDPOINT,
129 .bEndpointAddress = 3 | USB_DIR_IN,
130 .bmAttributes = USB_ENDPOINT_XFER_INT,
131 .wMaxPacketSize = __constant_cpu_to_le16(64),
135 static struct usb_endpoint_descriptor acm_fs_in_desc = {
136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT,
138 .bEndpointAddress = USB_DIR_IN,
139 .bmAttributes = USB_ENDPOINT_XFER_BULK,
142 static struct usb_endpoint_descriptor acm_fs_out_desc = {
143 .bLength = USB_DT_ENDPOINT_SIZE,
144 .bDescriptorType = USB_DT_ENDPOINT,
145 .bEndpointAddress = USB_DIR_OUT,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
149 static struct usb_descriptor_header *acm_fs_function[] = {
150 (struct usb_descriptor_header *)&acm_iad_descriptor,
151 (struct usb_descriptor_header *)&acm_control_intf_desc,
152 (struct usb_descriptor_header *)&acm_header_desc,
153 (struct usb_descriptor_header *)&acm_call_mgmt_desc,
154 (struct usb_descriptor_header *)&acm_desc,
155 (struct usb_descriptor_header *)&acm_union_desc,
156 (struct usb_descriptor_header *)&acm_fs_notify_desc,
157 (struct usb_descriptor_header *)&acm_data_intf_desc,
158 (struct usb_descriptor_header *)&acm_fs_in_desc,
159 (struct usb_descriptor_header *)&acm_fs_out_desc,
163 static struct usb_endpoint_descriptor acm_hs_notify_desc = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bmAttributes = USB_ENDPOINT_XFER_INT,
167 .wMaxPacketSize = __constant_cpu_to_le16(64),
171 static struct usb_endpoint_descriptor acm_hs_in_desc = {
172 .bLength = USB_DT_ENDPOINT_SIZE,
173 .bDescriptorType = USB_DT_ENDPOINT,
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 .wMaxPacketSize = __constant_cpu_to_le16(512),
178 static struct usb_endpoint_descriptor acm_hs_out_desc = {
179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
182 .wMaxPacketSize = __constant_cpu_to_le16(512),
185 static struct usb_descriptor_header *acm_hs_function[] = {
186 (struct usb_descriptor_header *)&acm_iad_descriptor,
187 (struct usb_descriptor_header *)&acm_control_intf_desc,
188 (struct usb_descriptor_header *)&acm_header_desc,
189 (struct usb_descriptor_header *)&acm_call_mgmt_desc,
190 (struct usb_descriptor_header *)&acm_desc,
191 (struct usb_descriptor_header *)&acm_union_desc,
192 (struct usb_descriptor_header *)&acm_hs_notify_desc,
193 (struct usb_descriptor_header *)&acm_data_intf_desc,
194 (struct usb_descriptor_header *)&acm_hs_in_desc,
195 (struct usb_descriptor_header *)&acm_hs_out_desc,
199 static inline struct usb_endpoint_descriptor *
200 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
201 struct usb_endpoint_descriptor *fs)
203 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
208 static int acm_bind(struct usb_configuration *c, struct usb_function *f)
210 struct usb_gadget *gadget = c->cdev->gadget;
211 struct f_acm *f_acm = func_to_acm(f);
215 id = usb_interface_id(c, f);
219 acm_iad_descriptor.bFirstInterface = id;
220 acm_control_intf_desc.bInterfaceNumber = id;
221 acm_union_desc.bMasterInterface0 = id;
225 id = usb_interface_id(c, f);
229 acm_data_intf_desc.bInterfaceNumber = id;
230 acm_union_desc.bSlaveInterface0 = id;
231 acm_call_mgmt_desc.bDataInterface = id;
235 /* allocate instance-specific endpoints */
236 ep = usb_ep_autoconfig(gadget, &acm_fs_in_desc);
242 ep = usb_ep_autoconfig(gadget, &acm_fs_out_desc);
248 ep = usb_ep_autoconfig(gadget, &acm_fs_notify_desc);
252 f_acm->ep_notify = ep;
254 if (gadget_is_dualspeed(gadget)) {
255 /* Assume endpoint addresses are the same for both speeds */
256 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
257 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
258 acm_hs_notify_desc.bEndpointAddress = acm_fs_notify_desc.bEndpointAddress;
264 static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
266 struct f_acm *f_acm = func_to_acm(f);
268 if (default_acm_function == f_acm)
269 default_acm_function = NULL;
271 buf_free(&f_acm->rx_buf);
272 buf_free(&f_acm->tx_buf);
277 static void acm_notify_complete(struct usb_ep *ep, struct usb_request *req)
282 static void acm_tx_complete(struct usb_ep *ep, struct usb_request *req)
284 struct f_acm *f_acm = req->context;
289 static void acm_rx_complete(struct usb_ep *ep, struct usb_request *req)
291 struct f_acm *f_acm = req->context;
293 buf_push(&f_acm->rx_buf, req->buf, req->actual);
295 /* Queue RX req again */
297 usb_ep_queue(ep, req, 0);
300 static struct usb_request *acm_start_ep(struct usb_ep *ep, void *complete_cb,
303 struct usb_request *req;
305 req = usb_ep_alloc_request(ep, 0);
309 req->length = REQ_SIZE_MAX;
310 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, REQ_SIZE_MAX);
312 usb_ep_free_request(ep, req);
316 memset(req->buf, 0, req->length);
317 req->complete = complete_cb;
318 req->context = context;
323 static int acm_start_data(struct f_acm *f_acm, struct usb_gadget *gadget)
325 const struct usb_endpoint_descriptor *d;
329 d = ep_desc(gadget, &acm_hs_in_desc, &acm_fs_in_desc);
330 ret = usb_ep_enable(f_acm->ep_in, d);
334 f_acm->req_in = acm_start_ep(f_acm->ep_in, acm_tx_complete, f_acm);
337 d = ep_desc(gadget, &acm_hs_out_desc, &acm_fs_out_desc);
338 ret = usb_ep_enable(f_acm->ep_out, d);
342 f_acm->req_out = acm_start_ep(f_acm->ep_out, acm_rx_complete, f_acm);
344 /* Start OUT transfer (EP OUT) */
345 ret = usb_ep_queue(f_acm->ep_out, f_acm->req_out, 0);
352 static int acm_start_ctrl(struct f_acm *f_acm, struct usb_gadget *gadget)
354 const struct usb_endpoint_descriptor *d;
356 usb_ep_disable(f_acm->ep_notify);
358 d = ep_desc(gadget, &acm_hs_notify_desc, &acm_fs_notify_desc);
359 usb_ep_enable(f_acm->ep_notify, d);
361 acm_start_ep(f_acm->ep_notify, acm_notify_complete, f_acm);
366 static int acm_set_alt(struct usb_function *f, unsigned int intf, unsigned int alt)
368 struct usb_gadget *gadget = f->config->cdev->gadget;
369 struct f_acm *f_acm = func_to_acm(f);
371 if (intf == f_acm->ctrl_id) {
372 return acm_start_ctrl(f_acm, gadget);
373 } else if (intf == f_acm->data_id) {
374 acm_start_data(f_acm, gadget);
375 f_acm->connected = true;
383 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
385 struct usb_gadget *gadget = f->config->cdev->gadget;
386 struct usb_request *req = f->config->cdev->req;
387 u16 w_index = le16_to_cpu(ctrl->wIndex);
388 u16 w_value = le16_to_cpu(ctrl->wValue);
389 u16 w_length = le16_to_cpu(ctrl->wLength);
390 struct f_acm *f_acm = func_to_acm(f);
393 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
394 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
395 | USB_CDC_REQ_SET_LINE_CODING:
396 /* SET_LINE_CODING */
398 if (w_length != sizeof(f_acm->line_coding) || w_index != f_acm->ctrl_id)
403 memcpy(&f_acm->line_coding, req->buf, value);
406 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
407 | USB_CDC_REQ_GET_LINE_CODING:
408 /* GET_LINE_CODING */
410 if (w_length != sizeof(f_acm->line_coding) || w_index != f_acm->ctrl_id)
415 memcpy(req->buf, &f_acm->line_coding, value);
418 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
419 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
420 /* SET_CONTROL_LINE_STATE */
422 if (w_index != f_acm->ctrl_id)
427 f_acm->handshake_bits = w_value;
432 printf("invalid control req%02x.%02x v%04x i%04x l%d\n",
433 ctrl->bRequestType, ctrl->bRequest, w_value, w_index,
437 /* respond with data transfer or status phase? */
441 usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
447 static void acm_disable(struct usb_function *f)
449 struct f_acm *f_acm = func_to_acm(f);
451 usb_ep_disable(f_acm->ep_out);
452 usb_ep_disable(f_acm->ep_in);
453 usb_ep_disable(f_acm->ep_notify);
455 if (f_acm->req_out) {
456 free(f_acm->req_out->buf);
457 usb_ep_free_request(f_acm->ep_out, f_acm->req_out);
458 f_acm->req_out = NULL;
462 free(f_acm->req_in->buf);
463 usb_ep_free_request(f_acm->ep_in, f_acm->req_in);
464 f_acm->req_in = NULL;
468 /* static strings, in UTF-8 */
469 static struct usb_string acm_string_defs[] = {
470 [0].s = "CDC Abstract Control Model (ACM)",
471 [1].s = "CDC ACM Data",
472 [2].s = "CDC Serial",
473 { } /* end of list */
476 static struct usb_gadget_strings acm_string_table = {
477 .language = 0x0409, /* en-us */
478 .strings = acm_string_defs,
481 static struct usb_gadget_strings *acm_strings[] = {
486 static void __acm_tx(struct f_acm *f_acm)
491 dm_usb_gadget_handle_interrupts(f_acm->udc);
493 if (!(f_acm->handshake_bits & ACM_CTRL_DTR))
499 len = buf_pop(&f_acm->tx_buf, f_acm->req_in->buf, REQ_SIZE_MAX);
503 f_acm->req_in->length = len;
505 ret = usb_ep_queue(f_acm->ep_in, f_acm->req_in, 0);
509 f_acm->tx_on = false;
511 /* Do not reset the watchdog, if TX is stuck there is probably
517 static bool acm_connected(struct stdio_dev *dev)
519 struct f_acm *f_acm = stdio_to_acm(dev);
521 /* give a chance to process udc irq */
522 dm_usb_gadget_handle_interrupts(f_acm->udc);
524 return f_acm->connected;
527 static int acm_add(struct usb_configuration *c)
532 f_acm = calloc(1, sizeof(*f_acm));
536 f_acm->usb_function.name = "f_acm";
537 f_acm->usb_function.bind = acm_bind;
538 f_acm->usb_function.unbind = acm_unbind;
539 f_acm->usb_function.set_alt = acm_set_alt;
540 f_acm->usb_function.disable = acm_disable;
541 f_acm->usb_function.strings = acm_strings;
542 f_acm->usb_function.descriptors = acm_fs_function;
543 f_acm->usb_function.hs_descriptors = acm_hs_function;
544 f_acm->usb_function.setup = acm_setup;
546 status = udc_device_get_by_index(0, &f_acm->udc);
550 status = usb_add_function(c, &f_acm->usb_function);
556 buf_init(&f_acm->rx_buf, 2048);
557 buf_init(&f_acm->tx_buf, 2048);
559 if (!default_acm_function)
560 default_acm_function = f_acm;
565 DECLARE_GADGET_BIND_CALLBACK(usb_serial_acm, acm_add);
568 static int acm_stdio_tstc(struct stdio_dev *dev)
570 struct f_acm *f_acm = stdio_to_acm(dev);
572 dm_usb_gadget_handle_interrupts(f_acm->udc);
574 return (f_acm->rx_buf.size > 0);
577 static int acm_stdio_getc(struct stdio_dev *dev)
579 struct f_acm *f_acm = stdio_to_acm(dev);
582 /* Wait for a character to arrive. */
583 while (!acm_stdio_tstc(dev))
586 buf_pop(&f_acm->rx_buf, &c, 1);
591 static void acm_stdio_putc(struct stdio_dev *dev, const char c)
593 struct f_acm *f_acm = stdio_to_acm(dev);
596 buf_push(&f_acm->tx_buf, "\r", 1);
598 buf_push(&f_acm->tx_buf, &c, 1);
600 if (!f_acm->connected)
606 static void acm_stdio_puts(struct stdio_dev *dev, const char *str)
608 struct f_acm *f_acm = stdio_to_acm(dev);
612 buf_push(&f_acm->tx_buf, "\r", 1);
614 buf_push(&f_acm->tx_buf, str++, 1);
617 if (!f_acm->connected)
623 static int acm_stdio_start(struct stdio_dev *dev)
628 if (dev->priv) { /* function already exist */
632 ret = udc_device_get_by_index(0, &udc);
634 pr_err("USB init failed: %d\n", ret);
638 g_dnl_clear_detach();
640 ret = g_dnl_register("usb_serial_acm");
644 if (default_acm_function)
645 dev->priv = default_acm_function;
649 while (!acm_connected(dev)) {
659 static int acm_stdio_stop(struct stdio_dev *dev)
662 g_dnl_clear_detach();
667 int drv_usbacm_init(void)
669 struct stdio_dev stdio;
671 strcpy(stdio.name, "usbacm");
672 stdio.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
673 stdio.tstc = acm_stdio_tstc;
674 stdio.getc = acm_stdio_getc;
675 stdio.putc = acm_stdio_putc;
676 stdio.puts = acm_stdio_puts;
677 stdio.start = acm_stdio_start;
678 stdio.stop = acm_stdio_stop;
682 return stdio_register(&stdio);