Skip to content

Commit

Permalink
Merge pull request #13 from lemolatoon/impl-usb2.0-hub
Browse files Browse the repository at this point in the history
Impl USB2.0 Hub class driver
  • Loading branch information
lemolatoon authored Nov 10, 2023
2 parents 74ce0ab + e7429bf commit 7a4024e
Show file tree
Hide file tree
Showing 19 changed files with 1,420 additions and 390 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/target
/gen_font/target
/kernel-lib/target
/memo.txt
/memo.txt
/tmp
39 changes: 17 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
.PHONY: FORCE
PROFILE=debug
QEMU=qemu-system-x86_64
QEMU_ARGS= \
-drive if=pflash,file=ovmf/OVMF_CODE.fd,format=raw \
-drive if=pflash,file=ovmf/lemola_os_ovmf_vars.fd,format=raw,readonly \
-drive file=disk.img,format=raw \
-device nec-usb-xhci,id=xhci \
-device usb-hub,bus=xhci.0,port=4 \
-device usb-mouse,bus=xhci.0,port=4.4 \
-device usb-kbd,bus=xhci.0,port=4.5 \
-serial telnet::5555,server,nowait \
-no-reboot \
-no-shutdown \
-monitor stdio

ifeq ($(PROFILE),release)
CARGO_FLAGS=--release
Expand Down Expand Up @@ -40,32 +52,15 @@ install: bootloader/target/x86_64-unknown-uefi/$(PROFILE)/bootloader.efi kernel/
sudo umount mnt

run: disk.img
$(QEMU) \
-drive if=pflash,file=ovmf/OVMF_CODE.fd,format=raw \
-drive if=pflash,file=ovmf/lemola_os_ovmf_vars.fd,format=raw,readonly \
-drive file=disk.img,format=raw \
-device nec-usb-xhci,id=xhci \
-device usb-kbd \
-device usb-mouse \
-serial telnet::5555,server,nowait \
-no-reboot \
-no-shutdown \
-monitor stdio
$(QEMU) $(QEMU_ARGS)
# -device usb-mouse,bus=xhci.0
# -device usb-kbd,bus=xhci.0
# -device usb-mouse,bus=xhci.0
# for serial port
# telnet localhost 5555

run_gdb: disk.img
$(QEMU) \
-drive if=pflash,file=ovmf/OVMF_CODE.fd,format=raw \
-drive if=pflash,file=ovmf/lemola_os_ovmf_vars.fd,format=raw \
-drive file=disk.img,format=raw \
-device nec-usb-xhci,id=xhci \
-device usb-kbd \
-device usb-mouse \
-serial telnet::5555,server,nowait \
-monitor stdio \
-no-reboot \
-no-shutdown \
$(QEMU) $(QEMU_ARGS) \
-gdb tcp::12345 -S
# on gdb
# target remote localhost:12345
Expand Down
16 changes: 16 additions & 0 deletions kernel-lib/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ pub fn alloc_with_boundary<const SIZE: usize, T>(
) -> Result<Box<MaybeUninit<T>, &'_ FixedLengthAllocator<SIZE>>, LayoutError> {
let layout = Layout::from_size_align(core::mem::size_of::<T>(), alignment)?;
let ptr = alloc_with_boundary_raw(allocator, layout, boundary) as *mut MaybeUninit<T>;
log::debug!("allocating {:x}", ptr as usize);
let (until, heap_start, heap_end) = {
let allocator = crate::lock!(allocator.0);
(
allocator.next,
allocator.heap.as_ptr() as usize,
allocator.heap.as_ptr() as usize + SIZE,
)
};
log::debug!(
"[{:x}..{:x}] in [{:x}...{:x}]",
ptr as usize,
until,
heap_start,
heap_end
);
debug_assert!(!ptr.is_null());
Ok(unsafe { Box::from_raw_in(ptr, allocator) })
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/alloc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloc::boxed::Box;
use core::{alloc::LayoutError, mem::MaybeUninit};
use kernel_lib::alloc::FixedLengthAllocator;

const HEAP_SIZE: usize = 1 << 22;
const HEAP_SIZE: usize = 1 << 21;

pub type GlobalAllocator = FixedLengthAllocator<HEAP_SIZE>;

Expand Down
18 changes: 9 additions & 9 deletions kernel/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ impl log::Log for SerialAndVgaCharWriter {
)
.unwrap();
} else {
// let mut serial_writer = InstantWriter::new(|s| serial_print!("{}", s));
// DecoratedLog::write(
// &mut serial_writer,
// record.level(),
// record.args(),
// record.file().unwrap_or("<unknown>"),
// record.line().unwrap_or(0),
// )
// .unwrap();
let mut serial_writer = InstantWriter::new(|s| serial_print!("{}", s));
DecoratedLog::write(
&mut serial_writer,
record.level(),
record.args(),
record.file().unwrap_or("<unknown>"),
record.line().unwrap_or(0),
)
.unwrap();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion kernel/src/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fn xhci_interrupt_handler(_stack_frame: InterruptStackFrame, _index: u8, _error_
// }
// log::info!("end xhci interrupt handler called");
// });
log::debug!("xhci interrupt handler called, but ignored...");

write_local_apic_id(0xb0, 0);
}
Expand Down
9 changes: 3 additions & 6 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ extern "C" fn kernel_main(arg: *const KernelMainArg) -> ! {

pixcel_writer.fill_shape(Vector2D::new(30, 50), &MOUSE_CURSOR_SHAPE);

let controller = init_xhci_controller();
let class_drivers = kernel::usb::class_driver::ClassDriverManager::new(
callbacks::mouse(),
callbacks::keyboard(),
);
let class_drivers: &'static _ = unsafe { &*(&class_drivers as *const _) };
let controller = init_xhci_controller(class_drivers);
init_idt();

static_assertions::assert_impl_all!(DeviceContextInfo<MemoryMapper, &'static GlobalAllocator>: usb_host::USBHost);
Expand All @@ -62,11 +63,7 @@ extern "C" fn kernel_main(arg: *const KernelMainArg) -> ! {

let mut executor = Executor::new();
let controller: &'static _ = unsafe { &*(&controller as *const _) };
let class_drivers: &'static _ = unsafe { &*(&class_drivers as *const _) };
let polling_task = Task::new(
Priority::Default,
kernel::xhci::poll_forever(controller, class_drivers),
);
let polling_task = Task::new(Priority::Default, kernel::xhci::poll_forever(controller));
let lifegame_task = Task::new(Priority::Default, kernel::lifegame::do_lifegame());
executor.spawn(polling_task);
executor.spawn(lifegame_task);
Expand Down
8 changes: 5 additions & 3 deletions kernel/src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ fn write_serial(byte: u8) {
}

pub fn write_serial_str(string: &str) {
for byte in string.bytes() {
write_serial(byte);
}
x86_64::instructions::interrupts::without_interrupts(|| {
for byte in string.bytes() {
write_serial(byte);
}
});
}

static mut SERIAL_WRITER: SerialWriter = SerialWriter::new();
Expand Down
27 changes: 24 additions & 3 deletions kernel/src/usb/class_driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod callbacks;
pub mod hub;
pub mod keyboard;
pub mod mouse;

Expand All @@ -12,6 +13,7 @@ use usb_host::{
};
use usb_host::{Endpoint as EndpointTrait, USBHost};

use self::hub::HubDriver;
use self::keyboard::BootKeyboardDriver;
use self::mouse::MouseDriver;

Expand Down Expand Up @@ -749,6 +751,7 @@ unsafe fn to_slice_mut<T>(v: &mut T) -> &mut [u8] {
core::slice::from_raw_parts_mut(ptr, len)
}

#[derive(Debug)]
pub struct Endpoint {
address: u8,
endpoint_num: u8,
Expand Down Expand Up @@ -829,9 +832,9 @@ macro_rules! add_device {
addr: u8,
) -> Result<(), DriverError> {
let mut device = self.$device.lock(file!(), line!());
if Driver::want_device(&device.driver, &device_descriptor) {
if AsyncDriver::want_device(&device.driver, &device_descriptor) {
device.slot_id = Some(slot_id);
return Driver::add_device(&mut device.driver, device_descriptor, addr);
return AsyncDriver::add_device(&mut device.driver, device_descriptor, addr);
}

Err(DriverError::Permanent(addr, $err))
Expand Down Expand Up @@ -867,6 +870,7 @@ pub struct DriverInfo<T: AsyncDriver> {
pub enum DriverKind {
Mouse,
Keyboard,
Hub,
}

#[derive(Debug)]
Expand All @@ -877,6 +881,7 @@ where
{
mouse: Mutex<DriverInfo<MouseDriver<MF>>>,
keyboard: Mutex<DriverInfo<BootKeyboardDriver<KF>>>,
hub: Mutex<DriverInfo<HubDriver>>,
}

impl<MF, KF> ClassDriverManager<MF, KF>
Expand All @@ -895,7 +900,17 @@ where
driver: BootKeyboardDriver::new_boot_keyboard(keyboard_callback),
};
let keyboard = Mutex::new(keyboard);
Self { mouse, keyboard }

let hub = DriverInfo {
slot_id: None,
driver: HubDriver::new(),
};
let hub = Mutex::new(hub);
Self {
mouse,
keyboard,
hub,
}
}

// pub fn tick<'a>(
Expand Down Expand Up @@ -947,7 +962,13 @@ where
&self.keyboard
}

pub fn hub(&self) -> &Mutex<DriverInfo<HubDriver>> {
&self.hub
}

add_device!(add_mouse_device, mouse, "Mouse device not wanted");

add_device!(add_keyboard_device, keyboard, "Keyboard device not wanted");

add_device!(add_hub_device, hub, "Hub device not wanted");
}
Loading

0 comments on commit 7a4024e

Please sign in to comment.