⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/drivers/usb/
mod.rs

1//! USB Bus Driver
2//!
3//! Implements USB host controller and device management.
4//!
5//! This module is organized into submodules:
6//! - [`device`]: USB device types, descriptors, and bus-level device management
7//! - [`host`]: USB host controller trait and UHCI controller implementation
8//! - [`transfer`]: USB transfer types and UHCI transfer descriptors
9//! - [`mass_storage`]: USB Mass Storage Bulk-Only Transport driver
10
11mod device;
12pub mod hid;
13mod host;
14pub mod hotplug;
15pub mod mass_storage;
16mod transfer;
17pub mod xhci;
18
19// Re-export all public types to maintain existing API
20use alloc::boxed::Box;
21
22pub use device::{
23    usb_classes, UsbBus, UsbConfiguration, UsbDevice, UsbDeviceDescriptor, UsbDirection,
24    UsbEndpoint, UsbEndpointType, UsbInterface, UsbPortStatus, UsbSpeed,
25};
26pub use host::{UhciController, UsbHostController};
27use spin::Mutex;
28pub use transfer::{UhciQh, UhciTd, UsbTransfer};
29
30use crate::sync::once_lock::OnceLock;
31
32/// Global USB bus instance
33static USB_BUS: OnceLock<Mutex<UsbBus>> = OnceLock::new();
34
35/// Initialize USB subsystem
36pub fn init() {
37    let usb_bus = UsbBus::new();
38    let _ = USB_BUS.set(Mutex::new(usb_bus));
39
40    // Add UHCI controller (placeholder)
41    let uhci = UhciController::new(0); // No actual hardware for now
42    if let Err(_e) = get_usb_bus().lock().add_controller(Box::new(uhci)) {
43        crate::println!("[USB] Failed to add UHCI controller: {}", _e);
44    }
45
46    // Register with driver framework
47    // Note: We create a new instance for the driver framework since Bus trait
48    // requires mut
49    let driver_framework = crate::services::driver_framework::get_driver_framework();
50    let bus_instance = UsbBus::new();
51
52    if let Err(_e) = driver_framework.register_bus(Box::new(bus_instance)) {
53        crate::println!("[USB] Failed to register USB bus: {}", _e);
54    } else {
55        crate::println!("[USB] USB bus driver initialized");
56    }
57}
58
59/// Get the global USB bus
60pub fn get_usb_bus() -> &'static Mutex<UsbBus> {
61    USB_BUS.get().expect("USB bus not initialized")
62}