Commit 2dbccf06 authored by Johann Fischer's avatar Johann Fischer Committed by Carles Cufi
Browse files

usb: device_next: do not set alternate 0 for non-existent interfaces

A request to set alternate zero for a non-existent interface
would succeed because of the inappropriate order of checks.
Move the check if the desired alternate is the same as the
current one after the check if interface exists.

Signed-off-by: default avatarJohann Fischer <johann.fischer@nordicsemi.no>
parent aa5b6db0
Loading
Loading
Loading
Loading
+1 −14
Changes for subsys/usb/device_next/usbd_ch9.c: 1 added line, 14 removed lines.
Original line number Diff line number Diff line
@@ -146,7 +146,6 @@ static int sreq_set_configuration(struct usbd_contex *const uds_ctx)
static int sreq_set_interface(struct usbd_contex *const uds_ctx)
{
	struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
	uint8_t cur_alt;
	int ret;

	if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_INTERFACE) {
@@ -170,21 +169,9 @@ static int sreq_set_interface(struct usbd_contex *const uds_ctx)
		return 0;
	}

	if (usbd_get_alt_value(uds_ctx, setup->wIndex, &cur_alt)) {
		errno = -ENOTSUP;
		return 0;
	}

	LOG_INF("Set Interfaces %u, alternate %u -> %u",
		setup->wIndex, cur_alt, setup->wValue);

	if (setup->wValue == cur_alt) {
		return 0;
	}

	ret = usbd_interface_set(uds_ctx, setup->wIndex, setup->wValue);
	if (ret == -ENOENT) {
		LOG_INF("Interface alternate does not exist");
		LOG_INF("Interface or alternate does not exist");
		errno = ret;
		ret = 0;
	}
+8 −3
Changes for subsys/usb/device_next/usbd_interface.c: 8 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -172,14 +172,19 @@ int usbd_interface_set(struct usbd_contex *const uds_ctx,
	uint8_t cur_alt;
	int ret;

	class = usbd_class_get_by_iface(uds_ctx, iface);
	if (class == NULL) {
		return -ENOENT;
	}

	ret = usbd_get_alt_value(uds_ctx, iface, &cur_alt);
	if (ret) {
		return ret;
	}

	class = usbd_class_get_by_iface(uds_ctx, iface);
	if (class == NULL) {
		return -ENODATA;
	LOG_INF("Set Interfaces %u, alternate %u -> %u", iface, cur_alt, alt);
	if (alt == cur_alt) {
		return 0;
	}

	/* Test if interface or interface alternate exist */