Lines Matching +full:cdc +full:- +full:acm
1 // SPDX-License-Identifier: GPL-2.0+
3 * f_acm.c -- USB CDC serial (ACM) function driver
24 * This CDC ACM function support just wraps control functions and
25 * notifications around the generic serial-over-usb code.
27 * Because CDC ACM is standardized by the USB-IF, many host operating
28 * systems have drivers for it. Accordingly, ACM is the preferred
29 * interop solution for serial-port type connections. The control
31 * this bare-bones implementation.
33 * Note that even MS-Windows has some support for ACM. However, that
34 * support is somewhat broken because when you use ACM in a composite
36 * seem to understand CDC Union descriptors. The new "association"
37 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
56 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
58 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
60 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
74 /*-------------------------------------------------------------------------*/
76 /* notification endpoint uses smallish and infrequent fixed-size messages */
269 /* static strings, in UTF-8 */
271 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
272 [ACM_DATA_IDX].s = "CDC ACM Data",
273 [ACM_IAD_IDX ].s = "CDC Serial",
278 .language = 0x0409, /* en-us */
287 /*-------------------------------------------------------------------------*/
289 /* ACM control ... data handling is delegated to tty library code.
298 struct f_acm *acm = ep->driver_data; in acm_complete_set_line_coding() local
299 struct usb_composite_dev *cdev = acm->port.func.config->cdev; in acm_complete_set_line_coding()
301 if (req->status != 0) { in acm_complete_set_line_coding()
302 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d completion, err %d\n", in acm_complete_set_line_coding()
303 acm->port_num, req->status); in acm_complete_set_line_coding()
308 if (req->actual != sizeof(acm->port_line_coding)) { in acm_complete_set_line_coding()
309 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d short resp, len %d\n", in acm_complete_set_line_coding()
310 acm->port_num, req->actual); in acm_complete_set_line_coding()
313 struct usb_cdc_line_coding *value = req->buf; in acm_complete_set_line_coding()
319 * the order of 9600-8-N-1 ... most of which means in acm_complete_set_line_coding()
322 acm->port_line_coding = *value; in acm_complete_set_line_coding()
330 struct f_acm *acm = func_to_acm(f); in acm_setup() local
331 struct usb_composite_dev *cdev = f->config->cdev; in acm_setup()
332 struct usb_request *req = cdev->req; in acm_setup()
333 int value = -EOPNOTSUPP; in acm_setup()
334 u16 w_index = le16_to_cpu(ctrl->wIndex); in acm_setup()
335 u16 w_value = le16_to_cpu(ctrl->wValue); in acm_setup()
336 u16 w_length = le16_to_cpu(ctrl->wLength); in acm_setup()
339 * CDC class messages; interface activation uses set_alt(). in acm_setup()
341 * Note CDC spec table 4 lists the ACM request profile. It requires in acm_setup()
346 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { in acm_setup()
352 || w_index != acm->ctrl_id) in acm_setup()
356 cdev->gadget->ep0->driver_data = acm; in acm_setup()
357 req->complete = acm_complete_set_line_coding; in acm_setup()
363 if (w_index != acm->ctrl_id) in acm_setup()
368 memcpy(req->buf, &acm->port_line_coding, value); in acm_setup()
374 if (w_index != acm->ctrl_id) in acm_setup()
381 * that bit, we should return to that no-flow state. in acm_setup()
383 acm->port_handshake_bits = w_value; in acm_setup()
388 if (w_index != acm->ctrl_id) in acm_setup()
391 acm_send_break(&acm->port, w_value); in acm_setup()
396 dev_vdbg(&cdev->gadget->dev, in acm_setup()
398 ctrl->bRequestType, ctrl->bRequest, in acm_setup()
404 dev_dbg(&cdev->gadget->dev, in acm_setup()
405 "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n", in acm_setup()
406 acm->port_num, ctrl->bRequestType, ctrl->bRequest, in acm_setup()
408 req->zero = 0; in acm_setup()
409 req->length = value; in acm_setup()
410 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); in acm_setup()
412 ERROR(cdev, "acm response on ttyGS%d, err %d\n", in acm_setup()
413 acm->port_num, value); in acm_setup()
422 struct f_acm *acm = func_to_acm(f); in acm_set_alt() local
423 struct usb_composite_dev *cdev = f->config->cdev; in acm_set_alt()
427 if (intf == acm->ctrl_id) { in acm_set_alt()
428 if (acm->notify->enabled) { in acm_set_alt()
429 dev_vdbg(&cdev->gadget->dev, in acm_set_alt()
430 "reset acm control interface %d\n", intf); in acm_set_alt()
431 usb_ep_disable(acm->notify); in acm_set_alt()
434 if (!acm->notify->desc) in acm_set_alt()
435 if (config_ep_by_speed(cdev->gadget, f, acm->notify)) in acm_set_alt()
436 return -EINVAL; in acm_set_alt()
438 usb_ep_enable(acm->notify); in acm_set_alt()
440 } else if (intf == acm->data_id) { in acm_set_alt()
441 if (acm->notify->enabled) { in acm_set_alt()
442 dev_dbg(&cdev->gadget->dev, in acm_set_alt()
443 "reset acm ttyGS%d\n", acm->port_num); in acm_set_alt()
444 gserial_disconnect(&acm->port); in acm_set_alt()
446 if (!acm->port.in->desc || !acm->port.out->desc) { in acm_set_alt()
447 dev_dbg(&cdev->gadget->dev, in acm_set_alt()
448 "activate acm ttyGS%d\n", acm->port_num); in acm_set_alt()
449 if (config_ep_by_speed(cdev->gadget, f, in acm_set_alt()
450 acm->port.in) || in acm_set_alt()
451 config_ep_by_speed(cdev->gadget, f, in acm_set_alt()
452 acm->port.out)) { in acm_set_alt()
453 acm->port.in->desc = NULL; in acm_set_alt()
454 acm->port.out->desc = NULL; in acm_set_alt()
455 return -EINVAL; in acm_set_alt()
458 gserial_connect(&acm->port, acm->port_num); in acm_set_alt()
461 return -EINVAL; in acm_set_alt()
468 struct f_acm *acm = func_to_acm(f); in acm_disable() local
469 struct usb_composite_dev *cdev = f->config->cdev; in acm_disable()
471 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d deactivated\n", acm->port_num); in acm_disable()
472 gserial_disconnect(&acm->port); in acm_disable()
473 usb_ep_disable(acm->notify); in acm_disable()
476 /*-------------------------------------------------------------------------*/
479 * acm_cdc_notify - issue CDC notification to host
480 * @acm: wraps host to be notified
482 * @value: Refer to cdc specs, wValue field.
485 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
489 * See section 6.3.5 of the CDC 1.1 specification for information
492 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value, in acm_cdc_notify() argument
495 struct usb_ep *ep = acm->notify; in acm_cdc_notify()
502 req = acm->notify_req; in acm_cdc_notify()
503 acm->notify_req = NULL; in acm_cdc_notify()
504 acm->pending = false; in acm_cdc_notify()
506 req->length = len; in acm_cdc_notify()
507 notify = req->buf; in acm_cdc_notify()
510 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS in acm_cdc_notify()
512 notify->bNotificationType = type; in acm_cdc_notify()
513 notify->wValue = cpu_to_le16(value); in acm_cdc_notify()
514 notify->wIndex = cpu_to_le16(acm->ctrl_id); in acm_cdc_notify()
515 notify->wLength = cpu_to_le16(length); in acm_cdc_notify()
519 spin_unlock(&acm->lock); in acm_cdc_notify()
521 spin_lock(&acm->lock); in acm_cdc_notify()
524 ERROR(acm->port.func.config->cdev, in acm_cdc_notify()
525 "acm ttyGS%d can't notify serial state, %d\n", in acm_cdc_notify()
526 acm->port_num, status); in acm_cdc_notify()
527 acm->notify_req = req; in acm_cdc_notify()
533 static int acm_notify_serial_state(struct f_acm *acm) in acm_notify_serial_state() argument
535 struct usb_composite_dev *cdev = acm->port.func.config->cdev; in acm_notify_serial_state()
539 spin_lock(&acm->lock); in acm_notify_serial_state()
540 if (acm->notify_req) { in acm_notify_serial_state()
541 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d serial state %04x\n", in acm_notify_serial_state()
542 acm->port_num, acm->serial_state); in acm_notify_serial_state()
543 serial_state = cpu_to_le16(acm->serial_state); in acm_notify_serial_state()
544 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE, in acm_notify_serial_state()
545 0, &serial_state, sizeof(acm->serial_state)); in acm_notify_serial_state()
547 acm->pending = true; in acm_notify_serial_state()
550 spin_unlock(&acm->lock); in acm_notify_serial_state()
556 struct f_acm *acm = req->context; in acm_cdc_notify_complete() local
560 * which is why ACM needs its own spinlock in acm_cdc_notify_complete()
562 spin_lock(&acm->lock); in acm_cdc_notify_complete()
563 if (req->status != -ESHUTDOWN) in acm_cdc_notify_complete()
564 doit = acm->pending; in acm_cdc_notify_complete()
565 acm->notify_req = req; in acm_cdc_notify_complete()
566 spin_unlock(&acm->lock); in acm_cdc_notify_complete()
569 acm_notify_serial_state(acm); in acm_cdc_notify_complete()
576 struct f_acm *acm = port_to_acm(port); in acm_connect() local
578 acm->serial_state |= USB_CDC_SERIAL_STATE_DSR | USB_CDC_SERIAL_STATE_DCD; in acm_connect()
579 acm_notify_serial_state(acm); in acm_connect()
584 struct f_acm *acm = port_to_acm(port); in acm_disconnect() local
586 acm->serial_state &= ~(USB_CDC_SERIAL_STATE_DSR | USB_CDC_SERIAL_STATE_DCD); in acm_disconnect()
587 acm_notify_serial_state(acm); in acm_disconnect()
592 struct f_acm *acm = port_to_acm(port); in acm_send_break() local
595 state = acm->serial_state; in acm_send_break()
600 acm->serial_state = state; in acm_send_break()
601 return acm_notify_serial_state(acm); in acm_send_break()
604 /*-------------------------------------------------------------------------*/
606 /* ACM function driver setup/binding */
610 struct usb_composite_dev *cdev = c->cdev; in acm_bind()
611 struct f_acm *acm = func_to_acm(f); in acm_bind() local
616 /* REVISIT might want instance-specific strings to help in acm_bind()
620 /* maybe allocate device-global string IDs, and patch descriptors */ in acm_bind()
629 /* allocate instance-specific interface IDs, and patch descriptors */ in acm_bind()
633 acm->ctrl_id = status; in acm_bind()
642 acm->data_id = status; in acm_bind()
648 status = -ENODEV; in acm_bind()
650 /* allocate instance-specific endpoints */ in acm_bind()
651 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc); in acm_bind()
654 acm->port.in = ep; in acm_bind()
656 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc); in acm_bind()
659 acm->port.out = ep; in acm_bind()
661 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc); in acm_bind()
664 acm->notify = ep; in acm_bind()
667 acm->notify_req = gs_alloc_req(ep, in acm_bind()
670 if (!acm->notify_req) in acm_bind()
673 acm->notify_req->complete = acm_cdc_notify_complete; in acm_bind()
674 acm->notify_req->context = acm; in acm_bind()
677 * hardware is dual speed, all bulk-capable endpoints work at in acm_bind()
693 dev_dbg(&cdev->gadget->dev, in acm_bind()
694 "acm ttyGS%d: IN/%s OUT/%s NOTIFY/%s\n", in acm_bind()
695 acm->port_num, in acm_bind()
696 acm->port.in->name, acm->port.out->name, in acm_bind()
697 acm->notify->name); in acm_bind()
701 if (acm->notify_req) in acm_bind()
702 gs_free_req(acm->notify, acm->notify_req); in acm_bind()
704 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status); in acm_bind()
711 struct f_acm *acm = func_to_acm(f); in acm_unbind() local
715 if (acm->notify_req) in acm_unbind()
716 gs_free_req(acm->notify, acm->notify_req); in acm_unbind()
721 struct f_acm *acm = func_to_acm(f); in acm_free_func() local
723 kfree(acm); in acm_free_func()
728 struct f_acm *acm = func_to_acm(f); in acm_resume() local
730 gserial_resume(&acm->port); in acm_resume()
735 struct f_acm *acm = func_to_acm(f); in acm_suspend() local
737 gserial_suspend(&acm->port); in acm_suspend()
743 struct f_acm *acm; in acm_alloc_func() local
745 acm = kzalloc(sizeof(*acm), GFP_KERNEL); in acm_alloc_func()
746 if (!acm) in acm_alloc_func()
747 return ERR_PTR(-ENOMEM); in acm_alloc_func()
749 spin_lock_init(&acm->lock); in acm_alloc_func()
751 acm->port.connect = acm_connect; in acm_alloc_func()
752 acm->port.disconnect = acm_disconnect; in acm_alloc_func()
753 acm->port.send_break = acm_send_break; in acm_alloc_func()
755 acm->port.func.name = "acm"; in acm_alloc_func()
756 acm->port.func.strings = acm_strings; in acm_alloc_func()
757 /* descriptors are per-instance copies */ in acm_alloc_func()
758 acm->port.func.bind = acm_bind; in acm_alloc_func()
759 acm->port.func.set_alt = acm_set_alt; in acm_alloc_func()
760 acm->port.func.setup = acm_setup; in acm_alloc_func()
761 acm->port.func.disable = acm_disable; in acm_alloc_func()
764 acm->port_num = opts->port_num; in acm_alloc_func()
765 acm->port.func.unbind = acm_unbind; in acm_alloc_func()
766 acm->port.func.free_func = acm_free_func; in acm_alloc_func()
767 acm->port.func.resume = acm_resume; in acm_alloc_func()
768 acm->port.func.suspend = acm_suspend; in acm_alloc_func()
770 return &acm->port.func; in acm_alloc_func()
783 usb_put_function_instance(&opts->func_inst); in acm_attr_release()
795 return gserial_set_console(to_f_serial_opts(item)->port_num, in f_acm_console_store()
801 return gserial_get_console(to_f_serial_opts(item)->port_num, page); in f_acm_console_show()
810 return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num); in f_acm_port_num_show()
834 gserial_free_line(opts->port_num); in acm_free_instance()
845 return ERR_PTR(-ENOMEM); in acm_alloc_instance()
846 opts->func_inst.free_func_inst = acm_free_instance; in acm_alloc_instance()
847 ret = gserial_alloc_line(&opts->port_num); in acm_alloc_instance()
852 config_group_init_type_name(&opts->func_inst.group, "", in acm_alloc_instance()
854 return &opts->func_inst; in acm_alloc_instance()
856 DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);