phil-opp / blog_os

Writing an OS in Rust
http://os.phil-opp.com
Apache License 2.0
15.81k stars 1.09k forks source link

How to add USB support? #1304

Open IceBlizz6 opened 8 months ago

IceBlizz6 commented 8 months ago

Love the blog, finished the PS/2 keyboard support. Also gone through 3rd edition of what you have so far.

I really want to attempt USB support for keyboard but it seems challenging. Looked at several USB libraries already.

usb-device crate seems to assume that i already have an object of UsbBus, some examples for micro-controllers seems to get this from HAL crates, i did not find anything for x86 architecture.

Tried to ask around in the rust discord, Was pointed to xhci, and then from there was told that i needed to initialize PCI. I don't really know where to continue or what crate to use from here.

Would you be able to point me in the right direction and give an overview of which parts here would need to be implemented by me and which parts i could just take from existing crates?

bjorn3 commented 8 months ago

https://wiki.osdev.org/Pci has a fair bit of information on how to work with PCI. The pci-types crate can be useful too for interacting with the PCI configuration space.

IceBlizz6 commented 8 months ago

https://docs.rs/usb-device/latest/usb_device/device/index.html

// Create the device-specific USB peripheral driver. The exact name and arguments are device // specific, so check the documentation for your device driver crate. let usb_bus = device_specific_usb::UsbBus::new(...);

Example looks like this from usb_device crate.

Is it possible to pass something in here?

If i have to use pci-types crate then give an example of how i would use it?

bjorn3 commented 8 months ago

Is it possible to pass something in here?

You did have to implement the UsbBus trait yourself I think. I'm not familiar with how USB or the usb_device crates work, so I can't help you with that.

If i have to use pci-types crate then give an example of how i would use it?

This is what Redox OS uses: https://gitlab.redox-os.org/redox-os/drivers/-/blob/master/pcid/src/main.rs?ref_type=heads It is fairly messy IMO and it has to deal with the PCI interfacing running in a different process from the actual PCI drivers.

Hermit may be a better learning source: https://github.com/hermit-os/kernel/blob/main/src/arch/x86_64/kernel/pci.rs

kennystrawnmusic commented 8 months ago

Well USB depends on xHCI, so that needs to be set up first. Here's an example of that.

IceBlizz6 commented 8 months ago

Thank you for the links, i'll take a closer look at this tomorrow if i can find the time. If you got anything more then feel free to add it here.

I'm starting to understand why the guide went with PS/2 keyboard support. Is there a guide planned for USB keyboard later though? maybe i'll figure out a setup that could be used in a future guide.

boredcoder411 commented 3 months ago

Did you figure it out in the end? I am currently developing a FAT32 driver and it would be nice to have disk support to actually see if it works