Lines Matching +full:min +full:- +full:x

4  * SPDX-License-Identifier: Apache-2.0
38 return setup->wLength && USB_REQTYPE_GET_DIR(setup->bmRequestType); in reqtype_is_to_host()
49 uds_ctx->ch9_data.ctrl_type = type; in ch9_set_ctrl_type()
54 return uds_ctx->ch9_data.ctrl_type; in ch9_get_ctrl_type()
62 if (setup->bRequest == USB_SREQ_SET_ADDRESS) { in post_status_stage()
63 ret = udc_set_address(uds_ctx->dev, setup->wValue); in post_status_stage()
65 LOG_ERR("Failed to set device address 0x%x", setup->wValue); in post_status_stage()
69 if (setup->bRequest == USB_SREQ_SET_FEATURE && in post_status_stage()
70 setup->wValue == USB_SFS_TEST_MODE) { in post_status_stage()
71 uint8_t mode = SF_TEST_MODE_SELECTOR(setup->wIndex); in post_status_stage()
73 ret = udc_test_mode(uds_ctx->dev, mode, false); in post_status_stage()
79 uds_ctx->ch9_data.post_status = false; in post_status_stage()
87 struct udc_device_caps caps = udc_caps(uds_ctx->dev); in sreq_set_address()
89 /* Not specified if wIndex or wLength is non-zero, treat as error */ in sreq_set_address()
90 if (setup->wValue > 127 || setup->wIndex || setup->wLength) { in sreq_set_address()
91 errno = -ENOTSUP; in sreq_set_address()
95 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE) { in sreq_set_address()
96 errno = -ENOTSUP; in sreq_set_address()
101 errno = -EPERM; in sreq_set_address()
108 ret = udc_set_address(uds_ctx->dev, setup->wValue); in sreq_set_address()
110 LOG_ERR("Failed to set device address 0x%x", setup->wValue); in sreq_set_address()
114 uds_ctx->ch9_data.post_status = true; in sreq_set_address()
117 if (usbd_state_is_address(uds_ctx) && setup->wValue == 0) { in sreq_set_address()
118 uds_ctx->ch9_data.state = USBD_STATE_DEFAULT; in sreq_set_address()
120 uds_ctx->ch9_data.state = USBD_STATE_ADDRESS; in sreq_set_address()
133 LOG_INF("Set Configuration Request value %u", setup->wValue); in sreq_set_configuration()
135 /* Not specified if wLength is non-zero, treat as error */ in sreq_set_configuration()
136 if (setup->wValue > UINT8_MAX || setup->wLength) { in sreq_set_configuration()
137 errno = -ENOTSUP; in sreq_set_configuration()
141 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE) { in sreq_set_configuration()
142 errno = -ENOTSUP; in sreq_set_configuration()
147 errno = -EPERM; in sreq_set_configuration()
151 if (setup->wValue && !usbd_config_exist(uds_ctx, speed, setup->wValue)) { in sreq_set_configuration()
152 errno = -EPERM; in sreq_set_configuration()
156 if (setup->wValue == usbd_get_config_value(uds_ctx)) { in sreq_set_configuration()
157 LOG_DBG("Already in the configuration %u", setup->wValue); in sreq_set_configuration()
161 ret = usbd_config_set(uds_ctx, setup->wValue); in sreq_set_configuration()
164 setup->wValue, ret); in sreq_set_configuration()
168 if (setup->wValue == 0) { in sreq_set_configuration()
170 uds_ctx->ch9_data.state = USBD_STATE_ADDRESS; in sreq_set_configuration()
172 uds_ctx->ch9_data.state = USBD_STATE_CONFIGURED; in sreq_set_configuration()
176 usbd_msg_pub_simple(uds_ctx, USBD_MSG_CONFIGURATION, setup->wValue); in sreq_set_configuration()
187 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_INTERFACE) { in sreq_set_interface()
188 errno = -ENOTSUP; in sreq_set_interface()
192 /* Not specified if wLength is non-zero, treat as error */ in sreq_set_interface()
193 if (setup->wLength) { in sreq_set_interface()
194 errno = -ENOTSUP; in sreq_set_interface()
198 if (setup->wValue > UINT8_MAX || setup->wIndex > UINT8_MAX) { in sreq_set_interface()
199 errno = -ENOTSUP; in sreq_set_interface()
204 errno = -EPERM; in sreq_set_interface()
208 ret = usbd_interface_set(uds_ctx, setup->wIndex, setup->wValue); in sreq_set_interface()
209 if (ret == -ENOENT) { in sreq_set_interface()
224 usbd_class_feature_halt(c_nd->c_data, ep, halted); in sreq_feature_halt_notify()
231 uint8_t ep = setup->wIndex; in sreq_clear_feature()
234 /* Not specified if wLength is non-zero, treat as error */ in sreq_clear_feature()
235 if (setup->wLength) { in sreq_clear_feature()
236 errno = -ENOTSUP; in sreq_clear_feature()
242 errno = -EPERM; in sreq_clear_feature()
246 if (usbd_state_is_address(uds_ctx) && setup->wIndex) { in sreq_clear_feature()
247 errno = -EPERM; in sreq_clear_feature()
251 switch (setup->RequestType.recipient) { in sreq_clear_feature()
253 if (setup->wIndex != 0) { in sreq_clear_feature()
254 errno = -EPERM; in sreq_clear_feature()
258 if (setup->wValue == USB_SFS_REMOTE_WAKEUP) { in sreq_clear_feature()
260 uds_ctx->status.rwup = false; in sreq_clear_feature()
264 if (setup->wValue == USB_SFS_ENDPOINT_HALT) { in sreq_clear_feature()
267 ret = (errno == -EPERM) ? errno : 0; in sreq_clear_feature()
286 uint8_t mode = SF_TEST_MODE_SELECTOR(setup->wIndex); in set_feature_test_mode()
288 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE || in set_feature_test_mode()
289 SF_TEST_LOWER_BYTE(setup->wIndex) != 0) { in set_feature_test_mode()
290 errno = -ENOTSUP; in set_feature_test_mode()
294 if (udc_test_mode(uds_ctx->dev, mode, true) != 0) { in set_feature_test_mode()
295 errno = -ENOTSUP; in set_feature_test_mode()
299 uds_ctx->ch9_data.post_status = true; in set_feature_test_mode()
307 uint8_t ep = setup->wIndex; in sreq_set_feature()
310 /* Not specified if wLength is non-zero, treat as error */ in sreq_set_feature()
311 if (setup->wLength) { in sreq_set_feature()
312 errno = -ENOTSUP; in sreq_set_feature()
316 if (unlikely(setup->wValue == USB_SFS_TEST_MODE)) { in sreq_set_feature()
325 errno = -EPERM; in sreq_set_feature()
329 if (usbd_state_is_address(uds_ctx) && setup->wIndex) { in sreq_set_feature()
330 errno = -EPERM; in sreq_set_feature()
334 switch (setup->RequestType.recipient) { in sreq_set_feature()
336 if (setup->wIndex != 0) { in sreq_set_feature()
337 errno = -EPERM; in sreq_set_feature()
341 if (setup->wValue == USB_SFS_REMOTE_WAKEUP) { in sreq_set_feature()
343 uds_ctx->status.rwup = true; in sreq_set_feature()
347 if (setup->wValue == USB_SFS_ENDPOINT_HALT) { in sreq_set_feature()
350 ret = (errno == -EPERM) ? errno : 0; in sreq_set_feature()
372 switch (setup->bRequest) { in std_request_to_device()
389 errno = -ENOTSUP; in std_request_to_device()
401 uint8_t ep = setup->wIndex; in sreq_get_status()
404 if (setup->wLength != sizeof(response)) { in sreq_get_status()
405 errno = -ENOTSUP; in sreq_get_status()
411 errno = -EPERM; in sreq_get_status()
415 if (usbd_state_is_address(uds_ctx) && setup->wIndex) { in sreq_get_status()
416 errno = -EPERM; in sreq_get_status()
420 switch (setup->RequestType.recipient) { in sreq_get_status()
422 if (setup->wIndex != 0) { in sreq_get_status()
423 errno = -EPERM; in sreq_get_status()
427 response = uds_ctx->status.rwup ? in sreq_get_status()
442 if (net_buf_tailroom(buf) < setup->wLength) { in sreq_get_status()
443 errno = -ENOMEM; in sreq_get_status()
447 LOG_DBG("Get Status response 0x%04x", response); in sreq_get_status()
454 * This function handles configuration and USB2.0 other-speed-configuration
472 * If the other-speed-configuration-descriptor is requested and the in sreq_get_desc_cfg()
476 errno = -ENOTSUP; in sreq_get_desc_cfg()
493 errno = -ENOTSUP; in sreq_get_desc_cfg()
499 memcpy(&other_desc, cfg_nd->desc, sizeof(other_desc)); in sreq_get_desc_cfg()
503 cfg_desc = cfg_nd->desc; in sreq_get_desc_cfg()
506 net_buf_add_mem(buf, cfg_desc, MIN(net_buf_tailroom(buf), cfg_desc->bLength)); in sreq_get_desc_cfg()
508 SYS_SLIST_FOR_EACH_CONTAINER(&cfg_nd->class_list, c_nd, node) { in sreq_get_desc_cfg()
511 dhp = usbd_class_get_desc(c_nd->c_data, get_desc_speed); in sreq_get_desc_cfg()
516 while (*dhp != NULL && (*dhp)->bLength != 0) { in sreq_get_desc_cfg()
517 len = MIN(net_buf_tailroom(buf), (*dhp)->bLength); in sreq_get_desc_cfg()
523 if (buf->len > setup->wLength) { in sreq_get_desc_cfg()
524 net_buf_remove_mem(buf, buf->len - setup->wLength); in sreq_get_desc_cfg()
527 LOG_DBG("Get Configuration descriptor %u, len %u", idx, buf->len); in sreq_get_desc_cfg()
539 ssize_t hwid_len = -ENOSYS; in get_sn_from_hwid()
546 if (hwid_len == -ENOSYS) { in get_sn_from_hwid()
561 /* Copy and convert ASCII-7 string descriptor to UTF16-LE */
567 .bDescriptorType = dn->bDescriptorType, in string_ascii7_to_utf16le()
573 if (dn->str.utype == USBD_DUT_STRING_SERIAL_NUMBER && dn->str.use_hwinfo) { in string_ascii7_to_utf16le()
577 errno = -ENOTSUP; in string_ascii7_to_utf16le()
584 head.bLength = dn->bLength; in string_ascii7_to_utf16le()
585 ascii7_str = (uint8_t *)dn->ptr; in string_ascii7_to_utf16le()
591 len = MIN(net_buf_tailroom(buf), MIN(head.bLength, wLength)); in string_ascii7_to_utf16le()
594 net_buf_add_mem(buf, &head, MIN(len, sizeof(head))); in string_ascii7_to_utf16le()
595 len -= MIN(len, sizeof(head)); in string_ascii7_to_utf16le()
599 "Only printable ascii-7 characters are allowed in USB " in string_ascii7_to_utf16le()
616 len = MIN(setup->wLength, net_buf_tailroom(buf)); in sreq_get_desc_dev()
620 head = uds_ctx->fs_desc; in sreq_get_desc_dev()
623 head = uds_ctx->hs_desc; in sreq_get_desc_dev()
626 errno = -ENOTSUP; in sreq_get_desc_dev()
630 net_buf_add_mem(buf, head, MIN(len, head->bLength)); in sreq_get_desc_dev()
645 errno = -ENOTSUP; in sreq_get_desc_str()
652 .bLength = d_nd->bLength, in sreq_get_desc_str()
653 .bDescriptorType = d_nd->bDescriptorType, in sreq_get_desc_str()
654 .bString = *(uint16_t *)d_nd->ptr, in sreq_get_desc_str()
657 len = MIN(setup->wLength, net_buf_tailroom(buf)); in sreq_get_desc_str()
658 net_buf_add_mem(buf, &langid, MIN(len, langid.bLength)); in sreq_get_desc_str()
661 string_ascii7_to_utf16le(d_nd, buf, setup->wLength); in sreq_get_desc_str()
671 /* At Full-Speed we want High-Speed descriptor and vice versa */ in sreq_get_dev_qualifier()
674 uds_ctx->hs_desc : uds_ctx->fs_desc; in sreq_get_dev_qualifier()
678 .bcdUSB = d_desc->bcdUSB, in sreq_get_dev_qualifier()
679 .bDeviceClass = d_desc->bDeviceClass, in sreq_get_dev_qualifier()
680 .bDeviceSubClass = d_desc->bDeviceSubClass, in sreq_get_dev_qualifier()
681 .bDeviceProtocol = d_desc->bDeviceProtocol, in sreq_get_dev_qualifier()
682 .bMaxPacketSize0 = d_desc->bMaxPacketSize0, in sreq_get_dev_qualifier()
683 .bNumConfigurations = d_desc->bNumConfigurations, in sreq_get_dev_qualifier()
693 errno = -ENOTSUP; in sreq_get_dev_qualifier()
698 len = MIN(setup->wLength, net_buf_tailroom(buf)); in sreq_get_dev_qualifier()
699 net_buf_add_mem(buf, &q_desc, MIN(len, q_desc.bLength)); in sreq_get_dev_qualifier()
709 root->bLength = sizeof(struct usb_bos_descriptor); in desc_fill_bos_root()
710 root->bDescriptorType = USB_DESC_BOS; in desc_fill_bos_root()
711 root->wTotalLength = root->bLength; in desc_fill_bos_root()
712 root->bNumDeviceCaps = 0; in desc_fill_bos_root()
714 SYS_DLIST_FOR_EACH_CONTAINER(&uds_ctx->descriptors, desc_nd, node) { in desc_fill_bos_root()
715 if (desc_nd->bDescriptorType == USB_DESC_BOS) { in desc_fill_bos_root()
716 root->wTotalLength += desc_nd->bLength; in desc_fill_bos_root()
717 root->bNumDeviceCaps++; in desc_fill_bos_root()
733 dev_dsc = uds_ctx->fs_desc; in sreq_get_desc_bos()
736 dev_dsc = uds_ctx->hs_desc; in sreq_get_desc_bos()
739 errno = -ENOTSUP; in sreq_get_desc_bos()
743 if (sys_le16_to_cpu(dev_dsc->bcdUSB) < 0x0201U) { in sreq_get_desc_bos()
744 errno = -ENOTSUP; in sreq_get_desc_bos()
749 len = MIN(net_buf_tailroom(buf), MIN(setup->wLength, bos.wTotalLength)); in sreq_get_desc_bos()
752 setup->wLength, bos.bLength, bos.wTotalLength, net_buf_tailroom(buf)); in sreq_get_desc_bos()
754 net_buf_add_mem(buf, &bos, MIN(len, bos.bLength)); in sreq_get_desc_bos()
756 len -= MIN(len, sizeof(bos)); in sreq_get_desc_bos()
761 SYS_DLIST_FOR_EACH_CONTAINER(&uds_ctx->descriptors, desc_nd, node) { in sreq_get_desc_bos()
762 if (desc_nd->bDescriptorType == USB_DESC_BOS) { in sreq_get_desc_bos()
764 desc_nd->bLength, len, net_buf_tailroom(buf)); in sreq_get_desc_bos()
765 net_buf_add_mem(buf, desc_nd->ptr, MIN(len, desc_nd->bLength)); in sreq_get_desc_bos()
767 len -= MIN(len, desc_nd->bLength); in sreq_get_desc_bos()
781 uint8_t desc_type = USB_GET_DESCRIPTOR_TYPE(setup->wValue); in sreq_get_descriptor()
782 uint8_t desc_idx = USB_GET_DESCRIPTOR_INDEX(setup->wValue); in sreq_get_descriptor()
787 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE) { in sreq_get_descriptor()
816 errno = -ENOTSUP; in sreq_get_descriptor()
829 errno = -EPERM; in sreq_get_configuration()
833 if (setup->wLength != sizeof(cfg)) { in sreq_get_configuration()
834 errno = -ENOTSUP; in sreq_get_configuration()
838 if (net_buf_tailroom(buf) < setup->wLength) { in sreq_get_configuration()
839 errno = -ENOMEM; in sreq_get_configuration()
856 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_INTERFACE) { in sreq_get_interface()
857 errno = -EPERM; in sreq_get_interface()
864 errno = -EPERM; in sreq_get_interface()
868 cfg_desc = cfg_nd->desc; in sreq_get_interface()
870 if (setup->wIndex > UINT8_MAX || in sreq_get_interface()
871 setup->wIndex > cfg_desc->bNumInterfaces) { in sreq_get_interface()
872 errno = -ENOTSUP; in sreq_get_interface()
876 if (usbd_get_alt_value(uds_ctx, setup->wIndex, &cur_alt)) { in sreq_get_interface()
877 errno = -ENOTSUP; in sreq_get_interface()
882 setup->wIndex, cur_alt); in sreq_get_interface()
884 if (setup->wLength != sizeof(cur_alt)) { in sreq_get_interface()
885 errno = -ENOTSUP; in sreq_get_interface()
889 if (net_buf_tailroom(buf) < setup->wLength) { in sreq_get_interface()
890 errno = -ENOMEM; in sreq_get_interface()
905 switch (setup->bRequest) { in std_request_to_host()
919 errno = -ENOTSUP; in std_request_to_host()
933 vreq_nd = usbd_device_get_vreq(uds_ctx, setup->bRequest); in vendor_device_request()
935 errno = -ENOTSUP; in vendor_device_request()
939 if (reqtype_is_to_device(setup) && vreq_nd->to_dev != NULL) { in vendor_device_request()
940 LOG_DBG("Vendor request 0x%02x to device", setup->bRequest); in vendor_device_request()
941 errno = vreq_nd->to_dev(uds_ctx, setup, buf); in vendor_device_request()
945 if (reqtype_is_to_host(setup) && vreq_nd->to_host != NULL) { in vendor_device_request()
946 LOG_DBG("Vendor request 0x%02x to host", setup->bRequest); in vendor_device_request()
947 errno = vreq_nd->to_host(uds_ctx, setup, buf); in vendor_device_request()
951 errno = -ENOTSUP; in vendor_device_request()
962 switch (setup->RequestType.recipient) { in nonstd_request()
964 c_nd = usbd_class_get_by_ep(uds_ctx, setup->wIndex); in nonstd_request()
967 c_nd = usbd_class_get_by_iface(uds_ctx, setup->wIndex); in nonstd_request()
970 c_nd = usbd_class_get_by_req(uds_ctx, setup->bRequest); in nonstd_request()
978 ret = usbd_class_control_to_dev(c_nd->c_data, setup, dbuf); in nonstd_request()
980 ret = usbd_class_control_to_host(c_nd->c_data, setup, dbuf); in nonstd_request()
997 switch (setup->RequestType.type) { in handle_setup_request()
1010 errno = -ENOTSUP; in handle_setup_request()
1017 if (errno == -ENOTSUP) { in handle_setup_request()
1020 if (errno == -EPERM) { in handle_setup_request()
1022 uds_ctx->ch9_data.state); in handle_setup_request()
1036 if (buf->len < sizeof(struct usb_setup_packet)) { in ctrl_xfer_get_setup()
1037 return -EINVAL; in ctrl_xfer_get_setup()
1040 memcpy(setup, buf->data, sizeof(struct usb_setup_packet)); in ctrl_xfer_get_setup()
1042 setup->wValue = sys_le16_to_cpu(setup->wValue); in ctrl_xfer_get_setup()
1043 setup->wIndex = sys_le16_to_cpu(setup->wIndex); in ctrl_xfer_get_setup()
1044 setup->wLength = sys_le16_to_cpu(setup->wLength); in ctrl_xfer_get_setup()
1048 buf_b = buf->frags; in ctrl_xfer_get_setup()
1051 return -ENODATA; in ctrl_xfer_get_setup()
1057 if (setup->wLength) { in ctrl_xfer_get_setup()
1058 if (!bi_b->data) { in ctrl_xfer_get_setup()
1060 return -EINVAL; in ctrl_xfer_get_setup()
1063 if (!bi_b->status) { in ctrl_xfer_get_setup()
1065 return -EINVAL; in ctrl_xfer_get_setup()
1069 if (!setup->wLength) { in ctrl_xfer_get_setup()
1070 LOG_ERR("device-to-host with wLength zero"); in ctrl_xfer_get_setup()
1071 return -ENOTSUP; in ctrl_xfer_get_setup()
1074 if (!bi_b->data) { in ctrl_xfer_get_setup()
1076 return -EINVAL; in ctrl_xfer_get_setup()
1094 if (bi->status) { in spool_data_out()
1111 if (USB_EP_GET_IDX(bi->ep)) { in usbd_handle_ctrl_xfer()
1113 return -EIO; in usbd_handle_ctrl_xfer()
1116 if (err && err != -ENOMEM && !bi->setup) { in usbd_handle_ctrl_xfer()
1117 if (err == -ECONNABORTED) { in usbd_handle_ctrl_xfer()
1118 LOG_INF("Transfer 0x%02x aborted (bus reset?)", bi->ep); in usbd_handle_ctrl_xfer()
1123 LOG_ERR("Control transfer for 0x%02x has error %d, halt", in usbd_handle_ctrl_xfer()
1124 bi->ep, err); in usbd_handle_ctrl_xfer()
1129 LOG_INF("Handle control %p ep 0x%02x, len %u, s:%u d:%u s:%u", in usbd_handle_ctrl_xfer()
1130 buf, bi->ep, buf->len, bi->setup, bi->data, bi->status); in usbd_handle_ctrl_xfer()
1132 if (bi->setup && bi->ep == USB_CONTROL_EP_OUT) { in usbd_handle_ctrl_xfer()
1168 if (reqtype_is_to_device(setup) && setup->wLength) { in usbd_handle_ctrl_xfer()
1185 if (bi->status && bi->ep == USB_CONTROL_EP_OUT) { in usbd_handle_ctrl_xfer()
1187 LOG_INF("s-in-status finished"); in usbd_handle_ctrl_xfer()
1189 LOG_WRN("Awaited s-in-status not finished"); in usbd_handle_ctrl_xfer()
1197 if (bi->status && bi->ep == USB_CONTROL_EP_IN) { in usbd_handle_ctrl_xfer()
1201 LOG_INF("s-(out)-status finished"); in usbd_handle_ctrl_xfer()
1202 if (unlikely(uds_ctx->ch9_data.post_status)) { in usbd_handle_ctrl_xfer()
1206 LOG_WRN("Awaited s-(out)-status not finished"); in usbd_handle_ctrl_xfer()
1217 * In the case there is -ENOMEM for data OUT stage halt in usbd_handle_ctrl_xfer()
1221 ret = udc_ep_set_halt(uds_ctx->dev, USB_CONTROL_EP_IN); in usbd_handle_ctrl_xfer()
1222 } else if (setup->wLength) { in usbd_handle_ctrl_xfer()
1223 uint8_t ep = (err == -ENOMEM) ? USB_CONTROL_EP_OUT : USB_CONTROL_EP_IN; in usbd_handle_ctrl_xfer()
1225 ret = udc_ep_set_halt(uds_ctx->dev, ep); in usbd_handle_ctrl_xfer()
1227 ret = udc_ep_set_halt(uds_ctx->dev, USB_CONTROL_EP_IN); in usbd_handle_ctrl_xfer()
1237 uds_ctx->ch9_data.state = USBD_STATE_DEFAULT; in usbd_init_control_pipe()