Phase B (Core Device Support) — all drivers verified in QEMU: - Network: virtio-net cleanup, RTL8139, E1000, clause-22 MDIO PHY management, CAN bus, industrial protocols (Modbus/Profibus/EtherCAT), controller probe+dispatch - Block storage: RAM disk backend (write->read->verify PASSED), virtio-blk transport, backend dispatch, real MBR+GPT partition parsers, SD/eMMC command framework - GPIO: PL061 (verified), I2C: DesignWare (verified), SPI: PL022 (verified) Phase C (Advanced Features): - PCI: FULL PikeOS ARMv7 replica — transport-agnostic uos_pci_ops, config-address encoding, BAR sizing, capability walk, enumeration+bridge recursion, MSI/MSI-X - USB: PikeOS-style layered stack — usb.h contract, usb_core.cpp (enumeration state machine), usb_ehci.cpp (EHCI transport) - Display: FULL 1:1 PikeOS fbcon replica + copied font_8x16 Build foundation fixes: - Freestanding aeabi_runtime.cpp (__aeabi_uidiv/__aeabi_uldivmod) - PikeOS-style flat 4GB MMU section map + proper enable (unblocked device MMIO) - guest.h MAX_GUEST_IMAGE_SIZE 256MB->16MB (BSS was 259MB) - C/C++ linkage fixes, duplicate-virtio_net_init, MMIO access-size handling Phase D (PikeOS ARMv7 Microkernel Port): - D-1: Per-VM address spaces — cloned pgdirs, ASID-tagged TLB, 4K page walker, isolation PASSED (two guests, same VA->different PAs), guest fault recovery - D-2: IRQ dispatch backbone — 1024-slot dispatch table, real GICv2 hardware (GICD_CTLR/GICC_CTLR/GICC_PMR/GICC_IAR/GICC_EOIR), arm_irq_handler wired - D-3: Time subsystem — CNTVCT ns-since-boot, CNTP periodic ticker via D-2 - D-4: KDEV framework — linker-section driver registration, uos_kdev_init_all, name lookup - D-5: VFP/NEON — lazy enable (undef trap->CPACR+FPEXC.EN), FPEXC=0x40000000 - D-6: SMP — per-CPU state, MPIDR, IPI/SGI framework (reschedule+TLB flush) All uos_ naming (PikeOS p4_ convention adapted). Compiles -Werror freestanding C++17. Co-Authored-By: Claude <noreply@anthropic.com>
196 lines
7.2 KiB
C
196 lines
7.2 KiB
C
/*
|
|
* Universalisos USB Stack — PikeOS-style layered host stack
|
|
*
|
|
* There is no PikeOS USB host-controller driver in the available source tree
|
|
* (only an x86 legacy handoff stub), so this stack is built after PikeOS's
|
|
* layered philosophy: a controller-agnostic core talks to a pluggable Host
|
|
* Controller Driver (HCD) through uos_usb_hcd_ops_t. The EHCI transport
|
|
* (usb_ehci.cpp) implements that contract against the EHCI spec (rev 1.0),
|
|
* which PikeOS's usb_handoff.c lists as its reference document.
|
|
*
|
|
* Layout mirrors the rest of the Phase C drivers: core + framework (safe,
|
|
* no-hardware) HCD as default, real HCD (EHCI) registrable by a board port.
|
|
*
|
|
* Author: PortugalFuturista Hypervisor Development Team
|
|
*/
|
|
|
|
#ifndef UNIVERSALISOS_DRIVERS_USB_H
|
|
#define UNIVERSALISOS_DRIVERS_USB_H
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* ==========================================================================
|
|
* USB standard constants (USB 2.0 spec §9)
|
|
* ========================================================================== */
|
|
|
|
/* Request types (bmRequestType direction/type/recipient) */
|
|
#define UOS_USB_DIR_OUT 0x00u /* host -> device */
|
|
#define UOS_USB_DIR_IN 0x80u /* device -> host */
|
|
#define UOS_USB_TYPE_STANDARD 0x00u
|
|
#define UOS_USB_TYPE_CLASS 0x20u
|
|
#define UOS_USB_TYPE_VENDOR 0x40u
|
|
#define UOS_USB_RECIP_DEVICE 0x00u
|
|
#define UOS_USB_RECIP_INTERFACE 0x01u
|
|
#define UOS_USB_RECIP_ENDPOINT 0x02u
|
|
|
|
/* Standard device requests (bRequest) */
|
|
#define UOS_USB_REQ_GET_STATUS 0x00u
|
|
#define UOS_USB_REQ_CLEAR_FEATURE 0x01u
|
|
#define UOS_USB_REQ_SET_FEATURE 0x03u
|
|
#define UOS_USB_REQ_SET_ADDRESS 0x05u
|
|
#define UOS_USB_REQ_GET_DESCRIPTOR 0x06u
|
|
#define UOS_USB_REQ_SET_DESCRIPTOR 0x07u
|
|
#define UOS_USB_REQ_GET_CONFIG 0x08u
|
|
#define UOS_USB_REQ_SET_CONFIG 0x09u
|
|
#define UOS_USB_REQ_GET_INTERFACE 0x0au
|
|
#define UOS_USB_REQ_SET_INTERFACE 0x0bu
|
|
#define UOS_USB_REQ_SYNCH_FRAME 0x0cu
|
|
|
|
/* Descriptor types */
|
|
#define UOS_USB_DT_DEVICE 0x01u
|
|
#define UOS_USB_DT_CONFIGURATION 0x02u
|
|
#define UOS_USB_DT_STRING 0x03u
|
|
#define UOS_USB_DT_INTERFACE 0x04u
|
|
#define UOS_USB_DT_ENDPOINT 0x05u
|
|
|
|
/* Endpoint address bits */
|
|
#define UOS_USB_ENDPOINT_NUMBER_MASK 0x0fu
|
|
#define UOS_USB_ENDPOINT_DIR_MASK 0x80u
|
|
#define UOS_USB_ENDPOINT_IN 0x80u
|
|
#define UOS_USB_ENDPOINT_OUT 0x00u
|
|
|
|
/* Endpoint attributes (transfer type) */
|
|
#define UOS_USB_ENDPOINT_XFERTYPE_MASK 0x03u
|
|
#define UOS_USB_ENDPOINT_XFER_CONTROL 0x00u
|
|
#define UOS_USB_ENDPOINT_XFER_ISOC 0x01u
|
|
#define UOS_USB_ENDPOINT_XFER_BULK 0x02u
|
|
#define UOS_USB_ENDPOINT_XFER_INT 0x03u
|
|
|
|
/* USB speeds */
|
|
typedef enum {
|
|
UOS_USB_SPEED_UNKNOWN = 0,
|
|
UOS_USB_SPEED_LOW, /* 1.5 Mbps */
|
|
UOS_USB_SPEED_FULL, /* 12 Mbps */
|
|
UOS_USB_SPEED_HIGH, /* 480 Mbps */
|
|
} uos_usb_speed_t;
|
|
|
|
/* ==========================================================================
|
|
* Standard descriptors (USB 2.0 §9.6) — packed, little-endian on the wire
|
|
* ========================================================================== */
|
|
typedef struct __attribute__((packed)) {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType; /* UOS_USB_DT_DEVICE */
|
|
uint16_t bcdUSB;
|
|
uint8_t bDeviceClass;
|
|
uint8_t bDeviceSubClass;
|
|
uint8_t bDeviceProtocol;
|
|
uint8_t bMaxPacketSize0;
|
|
uint16_t idVendor;
|
|
uint16_t idProduct;
|
|
uint16_t bcdDevice;
|
|
uint8_t iManufacturer;
|
|
uint8_t iProduct;
|
|
uint8_t iSerialNumber;
|
|
uint8_t bNumConfigurations;
|
|
} uos_usb_device_descriptor_t;
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType; /* UOS_USB_DT_ENDPOINT */
|
|
uint8_t bEndpointAddress;
|
|
uint8_t bmAttributes;
|
|
uint16_t wMaxPacketSize;
|
|
uint8_t bInterval;
|
|
} uos_usb_endpoint_descriptor_t;
|
|
|
|
/* ==========================================================================
|
|
* USB device record (one per enumerated device)
|
|
* ========================================================================== */
|
|
#define UOS_USB_MAX_DEVICES 32u /* address space is 0..127; cap for the table */
|
|
#define UOS_USB_MAX_ENDPOINTS 16u
|
|
|
|
typedef struct {
|
|
uint8_t address; /* 0..127, 0 = unassigned */
|
|
uos_usb_speed_t speed;
|
|
uint8_t num_configs;
|
|
uint8_t config_value; /* active configuration */
|
|
uos_usb_device_descriptor_t descriptor;
|
|
uos_usb_endpoint_descriptor_t endpoints[UOS_USB_MAX_ENDPOINTS];
|
|
uint8_t num_endpoints;
|
|
bool present;
|
|
bool enumerated;
|
|
} uos_usb_device_t;
|
|
|
|
/* ==========================================================================
|
|
* Host-Controller-Driver (HCD) contract — the pluggable transport
|
|
*
|
|
* A board port implements this for its host controller (EHCI, xHCI, ...).
|
|
* The core builds USB requests and submits them through these callbacks.
|
|
* ========================================================================== */
|
|
|
|
/* A control setup packet (8 bytes). */
|
|
typedef struct __attribute__((packed)) {
|
|
uint8_t bmRequestType;
|
|
uint8_t bRequest;
|
|
uint16_t wValue;
|
|
uint16_t wIndex;
|
|
uint16_t wLength;
|
|
} uos_usb_setup_pkt_t;
|
|
|
|
typedef struct {
|
|
/* Controller lifecycle */
|
|
int (*init)(uint32_t mmio_base, uint32_t irq);
|
|
void (*shutdown)(void);
|
|
|
|
/* Root hub: returns the port count and the connect/speed of port N. */
|
|
unsigned int (*rh_port_count)(void);
|
|
bool (*rh_port_connected)(unsigned int port); /* true if a device is attached */
|
|
uos_usb_speed_t (*rh_port_speed)(unsigned int port);
|
|
int (*rh_port_reset)(unsigned int port); /* drive reset, ~50ms */
|
|
|
|
/* Control transfer: setup + optional data + status. Returns bytes transferred. */
|
|
int (*control_msg)(uint8_t dev_addr, const uos_usb_setup_pkt_t *setup,
|
|
void *data, uint16_t len);
|
|
|
|
/* Bulk transfer on endpoint (dir in OUT/IN bit of ep). Returns bytes. */
|
|
int (*bulk_xfer)(uint8_t dev_addr, uint8_t ep, void *buf, uint16_t len, bool in);
|
|
} uos_usb_hcd_ops_t;
|
|
|
|
/* Register a host controller driver. */
|
|
void uos_usb_register_hcd(const uos_usb_hcd_ops_t *hcd);
|
|
|
|
/* ==========================================================================
|
|
* Public core API
|
|
* ========================================================================== */
|
|
|
|
/** Initialize the USB core and register the default framework HCD.
|
|
* (Named uos_usb_stack_init to avoid clashing with the per-device UOS API's
|
|
* uos_usb_init(usb_id) in driver/uos_advanced.h.) */
|
|
int uos_usb_stack_init(void);
|
|
|
|
/** Enumerate the bus: reset each root-hub port, assign addresses, read descriptors. */
|
|
int uos_usb_enumerate(void);
|
|
|
|
/** Build + submit a standard control request (convenience over hcd->control_msg). */
|
|
int uos_usb_control(uint8_t dev_addr, uint8_t request_type, uint8_t request,
|
|
uint16_t value, uint16_t index, uint16_t length, void *data);
|
|
|
|
/** Device table access. */
|
|
uos_usb_device_t *uos_usb_get_device(unsigned int index);
|
|
unsigned int uos_usb_get_device_count(void);
|
|
|
|
/** Driver init / demonstration (called from kernel boot). */
|
|
void uos_usb_driver_init(void);
|
|
void uos_usb_driver_demo(void);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* UNIVERSALISOS_DRIVERS_USB_H */
|