refactor(usb): the configuration-descriptor walk moved out of the event loop, unchanged

xhci.c's XHCI_XFER_CONFIG_DESC_FULL case walked the descriptor set inline, in
the event loop, where nothing could test it. The walk is now
usb_config_parse() (kernel/src/usb/usb_config.c), returning what it found;
the case acts on it as before -- the same messages, the same decisions: the
first Interface descriptor only, a BOT interface's bulk endpoints (the last of
each direction), a boot keyboard's first interrupt IN endpoint, bLength 0
ending the walk, nothing read past len. One difference, in debug output only:
the bulk endpoints are logged IN then OUT, not in descriptor order.

kernel/tests/test_usb_config.c, 7 checks, pins the walk as moved: a BOT stick
as QEMU presents it, a boot keyboard with its HID descriptor, a mouse before a
drive (the first interface only), a drive with bulk IN only, bLength 0, an
endpoint cut by len, an interface cut before its protocol byte. All pass.
Built for amd64, aarch64, riscv64; the boots come with the protocol (a data
stick enumerates through this path). The fixes for #170 and #171 follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-12 17:59:52 +00:00
parent 37b1527279
commit 8b482bedb8
5 changed files with 247 additions and 132 deletions
+2 -1
View File
@@ -19,8 +19,9 @@ CC ?= cc
CFLAGS := -std=c99 -Wall -Wextra -Werror -O2 -g -I$(HERE) -I$(ROOT)/kernel/include \
-I$(ROOT)/kernel/include/starkernel -I$(ROOT)/v3/include
TESTS := test_gpt
TESTS := test_gpt test_usb_config
SRC_test_gpt := $(ROOT)/kernel/src/gpt.c
SRC_test_usb_config := $(ROOT)/kernel/src/usb/usb_config.c
define TEST_RULE
$(BUILD)/$(1): $(HERE)/$(1).c $(HERE)/fakes.c $(HERE)/fakes.h $(HERE)/harness.h $$(SRC_$(1))
+73
View File
@@ -0,0 +1,73 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* test_usb_config.c -- kernel/src/usb/usb_config.c on the host: the configuration descriptor sets a
* device can send, read as xhci.c's event loop reads them (moved out of it unchanged; plan
* 2026-11-12-kernel-host-tests, increment 2). Sets built byte by byte, as a device sends them.
*/
#include "harness.h"
#include "starkernel/usb_config.h"
#include <string.h>
static uint8_t set[256];
static uint16_t at;
static void begin(uint8_t config_value) { memset(set, 0, sizeof set); at = 0;
set[at++] = 9; set[at++] = 2; at += 2; set[at++] = 1; set[at++] = config_value; at += 3; }
static void iface(uint8_t number, uint8_t cls, uint8_t sub, uint8_t proto, uint8_t neps) {
set[at++] = 9; set[at++] = 4; set[at++] = number; set[at++] = 0; set[at++] = neps;
set[at++] = cls; set[at++] = sub; set[at++] = proto; set[at++] = 0; }
static void hid_desc(void) { set[at++] = 9; set[at++] = 0x21; at += 7; }
static void endpoint(uint8_t addr, uint8_t attr, uint16_t max, uint8_t interval) {
set[at++] = 7; set[at++] = 5; set[at++] = addr; set[at++] = attr;
set[at++] = (uint8_t)max; set[at++] = (uint8_t)(max >> 8); set[at++] = interval; }
static uint16_t end(void) { set[2] = (uint8_t)at; set[3] = (uint8_t)(at >> 8); return at; }
int main(void)
{
usb_config_t c;
uint16_t n;
/* a USB stick as QEMU's usb-storage presents it: Mass Storage / SCSI / BOT, bulk IN and OUT */
begin(1); iface(0, 0x08, 0x06, 0x50, 2); endpoint(0x81, 0x02, 512, 0); endpoint(0x02, 0x02, 512, 0);
n = end(); usb_config_parse(set, n, &c);
CHECK(c.kind == USB_CONFIG_MSC && c.bulk_in_ep == 0x81 && c.bulk_out_ep == 0x02 && c.bulk_in_max == 512 &&
c.bulk_out_max == 512 && c.config_value == 1, "a BOT stick: MSC, bulk 0x81 and 0x02, 512 each (kind %d, in %#x, out %#x)",
c.kind, c.bulk_in_ep, c.bulk_out_ep);
/* a boot keyboard: its HID descriptor between the interface and the endpoint */
begin(1); iface(0, 0x03, 0x01, 0x01, 1); hid_desc(); endpoint(0x81, 0x03, 8, 10);
n = end(); usb_config_parse(set, n, &c);
CHECK(c.kind == USB_CONFIG_KBD && c.hid_iface == 0 && c.hid_ep == 0x81 && c.hid_max == 8 && c.hid_interval == 10,
"a boot keyboard: KBD, interrupt IN 0x81, 8 bytes, interval 10 (kind %d, ep %#x)", c.kind, c.hid_ep);
/* only the first interface counts: a mouse first, a BOT drive second */
begin(1); iface(0, 0x03, 0x01, 0x02, 1); endpoint(0x81, 0x03, 4, 10);
iface(1, 0x08, 0x06, 0x50, 2); endpoint(0x82, 0x02, 512, 0); endpoint(0x03, 0x02, 512, 0);
n = end(); usb_config_parse(set, n, &c);
CHECK(c.kind == USB_CONFIG_OTHER && c.bulk_in_ep == 0, "a mouse first, a drive second: OTHER -- the first interface only (kind %d)", c.kind);
/* a BOT drive with no bulk OUT endpoint */
begin(2); iface(0, 0x08, 0x06, 0x50, 1); endpoint(0x81, 0x02, 64, 0);
n = end(); usb_config_parse(set, n, &c);
CHECK(c.kind == USB_CONFIG_MSC && c.bulk_in_ep == 0x81 && c.bulk_out_ep == 0 && c.config_value == 2,
"a BOT drive with bulk IN only: MSC, OUT 0 (the caller then skips Configure Endpoint)");
/* a descriptor of length 0 ends the walk: no interface found, and no loop */
begin(1); set[at++] = 0; set[at++] = 4; at += 7;
n = end(); usb_config_parse(set, n, &c);
CHECK(c.kind == USB_CONFIG_NO_INTERFACE, "bLength 0: no interface, the walk ends (kind %d)", c.kind);
/* an endpoint cut off by len is not read */
begin(1); iface(0, 0x08, 0x06, 0x50, 2); endpoint(0x81, 0x02, 512, 0); endpoint(0x02, 0x02, 512, 0);
n = end(); usb_config_parse(set, (uint16_t)(n - 3), &c);
CHECK(c.kind == USB_CONFIG_MSC && c.bulk_in_ep == 0x81 && c.bulk_out_ep == 0,
"the last endpoint cut by len: not read (out %#x)", c.bulk_out_ep);
/* a set too short to hold its interface's protocol byte */
begin(1); iface(0, 0x08, 0x06, 0x50, 0);
usb_config_parse(set, 16, &c);
CHECK(c.kind == USB_CONFIG_NO_INTERFACE, "an interface cut before its protocol byte: none found (kind %d)", c.kind);
return DONE();
}