Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * USB CDC serial (ACM) function driver |
| 4 | * |
| 5 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) |
| 6 | * Copyright (C) 2008 by David Brownell |
| 7 | * Copyright (C) 2008 by Nokia Corporation |
| 8 | * Copyright (C) 2009 by Samsung Electronics |
| 9 | * Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> |
| 10 | */ |
| 11 | |
| 12 | #include <circbuf.h> |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 13 | #include <console.h> |
| 14 | #include <errno.h> |
| 15 | #include <g_dnl.h> |
| 16 | #include <malloc.h> |
| 17 | #include <memalign.h> |
| 18 | #include <stdio_dev.h> |
| 19 | #include <version.h> |
| 20 | #include <watchdog.h> |
| 21 | |
| 22 | #include <linux/usb/ch9.h> |
| 23 | #include <linux/usb/gadget.h> |
| 24 | #include <linux/usb/composite.h> |
| 25 | #include <linux/usb/cdc.h> |
| 26 | |
| 27 | #define REQ_SIZE_MAX 512 |
| 28 | |
| 29 | struct f_acm { |
| 30 | int ctrl_id; |
| 31 | int data_id; |
| 32 | |
| 33 | struct usb_ep *ep_in; |
| 34 | struct usb_ep *ep_out; |
| 35 | struct usb_ep *ep_notify; |
| 36 | |
| 37 | struct usb_request *req_in; |
| 38 | struct usb_request *req_out; |
| 39 | |
| 40 | bool connected; |
| 41 | bool tx_on; |
| 42 | |
| 43 | circbuf_t rx_buf; |
| 44 | circbuf_t tx_buf; |
| 45 | |
| 46 | struct usb_function usb_function; |
| 47 | |
| 48 | struct usb_cdc_line_coding line_coding; |
| 49 | u16 handshake_bits; |
| 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 */ |
| 52 | |
Marek Vasut | ce1b62d | 2023-09-01 11:50:00 +0200 | [diff] [blame] | 53 | struct udevice *udc; |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static struct f_acm *default_acm_function; |
| 57 | |
| 58 | static inline struct f_acm *func_to_acm(struct usb_function *f) |
| 59 | { |
| 60 | return container_of(f, struct f_acm, usb_function); |
| 61 | } |
| 62 | |
| 63 | static inline struct f_acm *stdio_to_acm(struct stdio_dev *s) |
| 64 | { |
| 65 | /* stdio dev is cloned on registration, do not use container_of */ |
| 66 | return s->priv; |
| 67 | } |
| 68 | |
| 69 | static struct usb_interface_assoc_descriptor |
| 70 | acm_iad_descriptor = { |
| 71 | .bLength = sizeof(acm_iad_descriptor), |
| 72 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
| 73 | .bFirstInterface = 0, |
| 74 | .bInterfaceCount = 2, // control + data |
| 75 | .bFunctionClass = USB_CLASS_COMM, |
| 76 | .bFunctionSubClass = USB_CDC_SUBCLASS_ACM, |
| 77 | .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER, |
| 78 | }; |
| 79 | |
| 80 | static struct usb_interface_descriptor acm_control_intf_desc = { |
| 81 | .bLength = USB_DT_INTERFACE_SIZE, |
| 82 | .bDescriptorType = USB_DT_INTERFACE, |
| 83 | .bNumEndpoints = 1, |
| 84 | .bInterfaceClass = USB_CLASS_COMM, |
| 85 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 86 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, |
| 87 | }; |
| 88 | |
| 89 | static struct usb_interface_descriptor acm_data_intf_desc = { |
| 90 | .bLength = sizeof(acm_data_intf_desc), |
| 91 | .bDescriptorType = USB_DT_INTERFACE, |
| 92 | .bNumEndpoints = 2, |
| 93 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 94 | }; |
| 95 | |
| 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), |
| 101 | }; |
| 102 | |
| 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, |
| 107 | .bmCapabilities = 0, |
| 108 | .bDataInterface = 0x01, |
| 109 | }; |
| 110 | |
| 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, |
| 116 | }; |
| 117 | |
| 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, |
| 124 | }; |
| 125 | |
| 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), |
| 132 | .bInterval = 32, |
| 133 | }; |
| 134 | |
| 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, |
| 140 | }; |
| 141 | |
| 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, |
| 147 | }; |
| 148 | |
| 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, |
| 160 | NULL, |
| 161 | }; |
| 162 | |
| 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), |
| 168 | .bInterval = 11, |
| 169 | }; |
| 170 | |
| 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), |
| 176 | }; |
| 177 | |
| 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), |
| 183 | }; |
| 184 | |
| 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, |
| 196 | NULL, |
| 197 | }; |
| 198 | |
| 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) |
| 202 | { |
| 203 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 204 | return hs; |
| 205 | return fs; |
| 206 | } |
| 207 | |
| 208 | static int acm_bind(struct usb_configuration *c, struct usb_function *f) |
| 209 | { |
| 210 | struct usb_gadget *gadget = c->cdev->gadget; |
| 211 | struct f_acm *f_acm = func_to_acm(f); |
| 212 | struct usb_ep *ep; |
| 213 | int id; |
| 214 | |
| 215 | id = usb_interface_id(c, f); |
| 216 | if (id < 0) |
| 217 | return id; |
| 218 | |
| 219 | acm_iad_descriptor.bFirstInterface = id; |
| 220 | acm_control_intf_desc.bInterfaceNumber = id; |
| 221 | acm_union_desc.bMasterInterface0 = id; |
| 222 | |
| 223 | f_acm->ctrl_id = id; |
| 224 | |
| 225 | id = usb_interface_id(c, f); |
| 226 | if (id < 0) |
| 227 | return id; |
| 228 | |
| 229 | acm_data_intf_desc.bInterfaceNumber = id; |
| 230 | acm_union_desc.bSlaveInterface0 = id; |
| 231 | acm_call_mgmt_desc.bDataInterface = id; |
| 232 | |
| 233 | f_acm->data_id = id; |
| 234 | |
| 235 | /* allocate instance-specific endpoints */ |
| 236 | ep = usb_ep_autoconfig(gadget, &acm_fs_in_desc); |
| 237 | if (!ep) |
| 238 | return -ENODEV; |
| 239 | |
| 240 | f_acm->ep_in = ep; |
| 241 | |
| 242 | ep = usb_ep_autoconfig(gadget, &acm_fs_out_desc); |
| 243 | if (!ep) |
| 244 | return -ENODEV; |
| 245 | |
| 246 | f_acm->ep_out = ep; |
| 247 | |
| 248 | ep = usb_ep_autoconfig(gadget, &acm_fs_notify_desc); |
| 249 | if (!ep) |
| 250 | return -ENODEV; |
| 251 | |
| 252 | f_acm->ep_notify = ep; |
| 253 | |
| 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; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static void acm_unbind(struct usb_configuration *c, struct usb_function *f) |
| 265 | { |
| 266 | struct f_acm *f_acm = func_to_acm(f); |
| 267 | |
| 268 | if (default_acm_function == f_acm) |
| 269 | default_acm_function = NULL; |
| 270 | |
| 271 | buf_free(&f_acm->rx_buf); |
| 272 | buf_free(&f_acm->tx_buf); |
| 273 | |
| 274 | free(f_acm); |
| 275 | } |
| 276 | |
| 277 | static void acm_notify_complete(struct usb_ep *ep, struct usb_request *req) |
| 278 | { |
| 279 | /* nothing to do */ |
| 280 | } |
| 281 | |
| 282 | static void acm_tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 283 | { |
| 284 | struct f_acm *f_acm = req->context; |
| 285 | |
| 286 | f_acm->tx_on = true; |
| 287 | } |
| 288 | |
| 289 | static void acm_rx_complete(struct usb_ep *ep, struct usb_request *req) |
| 290 | { |
| 291 | struct f_acm *f_acm = req->context; |
| 292 | |
| 293 | buf_push(&f_acm->rx_buf, req->buf, req->actual); |
| 294 | |
| 295 | /* Queue RX req again */ |
| 296 | req->actual = 0; |
| 297 | usb_ep_queue(ep, req, 0); |
| 298 | } |
| 299 | |
| 300 | static struct usb_request *acm_start_ep(struct usb_ep *ep, void *complete_cb, |
| 301 | void *context) |
| 302 | { |
| 303 | struct usb_request *req; |
| 304 | |
| 305 | req = usb_ep_alloc_request(ep, 0); |
| 306 | if (!req) |
| 307 | return NULL; |
| 308 | |
| 309 | req->length = REQ_SIZE_MAX; |
| 310 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, REQ_SIZE_MAX); |
| 311 | if (!req->buf) { |
| 312 | usb_ep_free_request(ep, req); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | memset(req->buf, 0, req->length); |
| 317 | req->complete = complete_cb; |
| 318 | req->context = context; |
| 319 | |
| 320 | return req; |
| 321 | } |
| 322 | |
| 323 | static int acm_start_data(struct f_acm *f_acm, struct usb_gadget *gadget) |
| 324 | { |
| 325 | const struct usb_endpoint_descriptor *d; |
| 326 | int ret; |
| 327 | |
| 328 | /* EP IN */ |
| 329 | d = ep_desc(gadget, &acm_hs_in_desc, &acm_fs_in_desc); |
| 330 | ret = usb_ep_enable(f_acm->ep_in, d); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | |
| 334 | f_acm->req_in = acm_start_ep(f_acm->ep_in, acm_tx_complete, f_acm); |
| 335 | |
| 336 | /* EP OUT */ |
| 337 | d = ep_desc(gadget, &acm_hs_out_desc, &acm_fs_out_desc); |
| 338 | ret = usb_ep_enable(f_acm->ep_out, d); |
| 339 | if (ret) |
| 340 | return ret; |
| 341 | |
| 342 | f_acm->req_out = acm_start_ep(f_acm->ep_out, acm_rx_complete, f_acm); |
| 343 | |
| 344 | /* Start OUT transfer (EP OUT) */ |
| 345 | ret = usb_ep_queue(f_acm->ep_out, f_acm->req_out, 0); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static int acm_start_ctrl(struct f_acm *f_acm, struct usb_gadget *gadget) |
| 353 | { |
| 354 | const struct usb_endpoint_descriptor *d; |
| 355 | |
| 356 | usb_ep_disable(f_acm->ep_notify); |
| 357 | |
| 358 | d = ep_desc(gadget, &acm_hs_notify_desc, &acm_fs_notify_desc); |
| 359 | usb_ep_enable(f_acm->ep_notify, d); |
| 360 | |
| 361 | acm_start_ep(f_acm->ep_notify, acm_notify_complete, f_acm); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int acm_set_alt(struct usb_function *f, unsigned int intf, unsigned int alt) |
| 367 | { |
| 368 | struct usb_gadget *gadget = f->config->cdev->gadget; |
| 369 | struct f_acm *f_acm = func_to_acm(f); |
| 370 | |
| 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; |
| 376 | f_acm->tx_on = true; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 384 | { |
| 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); |
| 391 | int value = -1; |
| 392 | |
| 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 */ |
| 397 | |
| 398 | if (w_length != sizeof(f_acm->line_coding) || w_index != f_acm->ctrl_id) |
| 399 | goto invalid; |
| 400 | |
| 401 | value = w_length; |
| 402 | |
| 403 | memcpy(&f_acm->line_coding, req->buf, value); |
| 404 | |
| 405 | break; |
| 406 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 407 | | USB_CDC_REQ_GET_LINE_CODING: |
| 408 | /* GET_LINE_CODING */ |
| 409 | |
| 410 | if (w_length != sizeof(f_acm->line_coding) || w_index != f_acm->ctrl_id) |
| 411 | goto invalid; |
| 412 | |
| 413 | value = w_length; |
| 414 | |
| 415 | memcpy(req->buf, &f_acm->line_coding, value); |
| 416 | |
| 417 | break; |
| 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 */ |
| 421 | |
| 422 | if (w_index != f_acm->ctrl_id) |
| 423 | goto invalid; |
| 424 | |
| 425 | value = 0; |
| 426 | |
| 427 | f_acm->handshake_bits = w_value; |
| 428 | |
| 429 | break; |
| 430 | default: |
| 431 | invalid: |
| 432 | printf("invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 433 | ctrl->bRequestType, ctrl->bRequest, w_value, w_index, |
| 434 | w_length); |
| 435 | } |
| 436 | |
| 437 | /* respond with data transfer or status phase? */ |
| 438 | if (value >= 0) { |
| 439 | req->zero = 0; |
| 440 | req->length = value; |
| 441 | usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static void acm_disable(struct usb_function *f) |
| 448 | { |
| 449 | struct f_acm *f_acm = func_to_acm(f); |
| 450 | |
| 451 | usb_ep_disable(f_acm->ep_out); |
| 452 | usb_ep_disable(f_acm->ep_in); |
| 453 | usb_ep_disable(f_acm->ep_notify); |
| 454 | |
| 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; |
| 459 | } |
| 460 | |
| 461 | if (f_acm->req_in) { |
| 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; |
| 465 | } |
| 466 | } |
| 467 | |
| 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 */ |
| 474 | }; |
| 475 | |
| 476 | static struct usb_gadget_strings acm_string_table = { |
| 477 | .language = 0x0409, /* en-us */ |
| 478 | .strings = acm_string_defs, |
| 479 | }; |
| 480 | |
| 481 | static struct usb_gadget_strings *acm_strings[] = { |
| 482 | &acm_string_table, |
| 483 | NULL, |
| 484 | }; |
| 485 | |
| 486 | static void __acm_tx(struct f_acm *f_acm) |
| 487 | { |
| 488 | int len, ret; |
| 489 | |
| 490 | do { |
Marek Vasut | ce1b62d | 2023-09-01 11:50:00 +0200 | [diff] [blame] | 491 | dm_usb_gadget_handle_interrupts(f_acm->udc); |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 492 | |
| 493 | if (!(f_acm->handshake_bits & ACM_CTRL_DTR)) |
| 494 | break; |
| 495 | |
| 496 | if (!f_acm->tx_on) |
| 497 | continue; |
| 498 | |
| 499 | len = buf_pop(&f_acm->tx_buf, f_acm->req_in->buf, REQ_SIZE_MAX); |
| 500 | if (!len) |
| 501 | break; |
| 502 | |
| 503 | f_acm->req_in->length = len; |
| 504 | |
| 505 | ret = usb_ep_queue(f_acm->ep_in, f_acm->req_in, 0); |
| 506 | if (ret) |
| 507 | break; |
| 508 | |
| 509 | f_acm->tx_on = false; |
| 510 | |
| 511 | /* Do not reset the watchdog, if TX is stuck there is probably |
| 512 | * a real issue. |
| 513 | */ |
| 514 | } while (1); |
| 515 | } |
| 516 | |
| 517 | static bool acm_connected(struct stdio_dev *dev) |
| 518 | { |
| 519 | struct f_acm *f_acm = stdio_to_acm(dev); |
| 520 | |
| 521 | /* give a chance to process udc irq */ |
Marek Vasut | ce1b62d | 2023-09-01 11:50:00 +0200 | [diff] [blame] | 522 | dm_usb_gadget_handle_interrupts(f_acm->udc); |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 523 | |
| 524 | return f_acm->connected; |
| 525 | } |
| 526 | |
| 527 | static int acm_add(struct usb_configuration *c) |
| 528 | { |
| 529 | struct f_acm *f_acm; |
| 530 | int status; |
| 531 | |
| 532 | f_acm = calloc(1, sizeof(*f_acm)); |
| 533 | if (!f_acm) |
| 534 | return -ENOMEM; |
| 535 | |
| 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; |
Marek Vasut | ce1b62d | 2023-09-01 11:50:00 +0200 | [diff] [blame] | 545 | |
| 546 | status = udc_device_get_by_index(0, &f_acm->udc); |
| 547 | if (status) |
| 548 | return status; |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 549 | |
| 550 | status = usb_add_function(c, &f_acm->usb_function); |
| 551 | if (status) { |
| 552 | free(f_acm); |
| 553 | return status; |
| 554 | } |
| 555 | |
| 556 | buf_init(&f_acm->rx_buf, 2048); |
| 557 | buf_init(&f_acm->tx_buf, 2048); |
| 558 | |
| 559 | if (!default_acm_function) |
| 560 | default_acm_function = f_acm; |
| 561 | |
| 562 | return status; |
| 563 | } |
| 564 | |
| 565 | DECLARE_GADGET_BIND_CALLBACK(usb_serial_acm, acm_add); |
| 566 | |
| 567 | /* STDIO */ |
| 568 | static int acm_stdio_tstc(struct stdio_dev *dev) |
| 569 | { |
| 570 | struct f_acm *f_acm = stdio_to_acm(dev); |
| 571 | |
Marek Vasut | ce1b62d | 2023-09-01 11:50:00 +0200 | [diff] [blame] | 572 | dm_usb_gadget_handle_interrupts(f_acm->udc); |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 573 | |
| 574 | return (f_acm->rx_buf.size > 0); |
| 575 | } |
| 576 | |
| 577 | static int acm_stdio_getc(struct stdio_dev *dev) |
| 578 | { |
| 579 | struct f_acm *f_acm = stdio_to_acm(dev); |
| 580 | char c; |
| 581 | |
| 582 | /* Wait for a character to arrive. */ |
| 583 | while (!acm_stdio_tstc(dev)) |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 584 | schedule(); |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 585 | |
| 586 | buf_pop(&f_acm->rx_buf, &c, 1); |
| 587 | |
| 588 | return c; |
| 589 | } |
| 590 | |
| 591 | static void acm_stdio_putc(struct stdio_dev *dev, const char c) |
| 592 | { |
| 593 | struct f_acm *f_acm = stdio_to_acm(dev); |
| 594 | |
| 595 | if (c == '\n') |
| 596 | buf_push(&f_acm->tx_buf, "\r", 1); |
| 597 | |
| 598 | buf_push(&f_acm->tx_buf, &c, 1); |
| 599 | |
| 600 | if (!f_acm->connected) |
| 601 | return; |
| 602 | |
| 603 | __acm_tx(f_acm); |
| 604 | } |
| 605 | |
| 606 | static void acm_stdio_puts(struct stdio_dev *dev, const char *str) |
| 607 | { |
| 608 | struct f_acm *f_acm = stdio_to_acm(dev); |
| 609 | |
| 610 | while (*str) { |
| 611 | if (*str == '\n') |
| 612 | buf_push(&f_acm->tx_buf, "\r", 1); |
| 613 | |
| 614 | buf_push(&f_acm->tx_buf, str++, 1); |
| 615 | } |
| 616 | |
| 617 | if (!f_acm->connected) |
| 618 | return; |
| 619 | |
| 620 | __acm_tx(f_acm); |
| 621 | } |
| 622 | |
| 623 | static int acm_stdio_start(struct stdio_dev *dev) |
| 624 | { |
Caleb Connolly | d4fe66f | 2024-03-20 14:30:49 +0000 | [diff] [blame] | 625 | struct udevice *udc; |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 626 | int ret; |
| 627 | |
| 628 | if (dev->priv) { /* function already exist */ |
| 629 | return 0; |
| 630 | } |
| 631 | |
Caleb Connolly | d4fe66f | 2024-03-20 14:30:49 +0000 | [diff] [blame] | 632 | ret = udc_device_get_by_index(0, &udc); |
| 633 | if (ret) { |
| 634 | pr_err("USB init failed: %d\n", ret); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | g_dnl_clear_detach(); |
| 639 | |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 640 | ret = g_dnl_register("usb_serial_acm"); |
| 641 | if (ret) |
| 642 | return ret; |
| 643 | |
| 644 | if (default_acm_function) |
| 645 | dev->priv = default_acm_function; |
| 646 | else |
| 647 | return -ENODEV; |
| 648 | |
| 649 | while (!acm_connected(dev)) { |
| 650 | if (ctrlc()) |
| 651 | return -ECANCELED; |
| 652 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 653 | schedule(); |
Loic Poulain | eb78f83 | 2021-11-25 18:16:15 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int acm_stdio_stop(struct stdio_dev *dev) |
| 660 | { |
| 661 | g_dnl_unregister(); |
| 662 | g_dnl_clear_detach(); |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | int drv_usbacm_init(void) |
| 668 | { |
| 669 | struct stdio_dev stdio; |
| 670 | |
| 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; |
| 679 | stdio.priv = NULL; |
| 680 | stdio.ext = 0; |
| 681 | |
| 682 | return stdio_register(&stdio); |
| 683 | } |