/* 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 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); /* #170: after the first 9 bytes, the set is asked for again in full -- a wTotalLength that is * no longer than those 9 bytes would be asked for again, as "short", for ever */ { int cut = -1; uint16_t k; for (k = 0; k <= 9; k++) CHECK(usb_config_request_length(k, 128, &cut) == 0, "#170: wTotalLength %u: refused (0), not asked for again", k); CHECK(usb_config_request_length(32, 128, &cut) == 32 && cut == 0, "a BOT stick's 32 bytes: asked for whole, not cut"); CHECK(usb_config_request_length(128, 128, &cut) == 128 && cut == 0, "exactly the buffer: whole, not cut"); /* #171: longer than the buffer -- cut, and said to be */ CHECK(usb_config_request_length(300, 128, &cut) == 128 && cut == 1, "#171: 300 bytes into 128: 128 asked for, and cut (%d) said", cut); } return DONE(); }