Ver Fonte

Cleanup (#10)

* cargo fmt

* Log device type as well as vendor ID.

* Improve Rustdoc comments.

* Remove commented-out code.

* Implement Copy and Clone for error type.
Andrew Walbran há 2 anos atrás
pai
commit
93f821cf2c
3 ficheiros alterados com 12 adições e 15 exclusões
  1. 6 6
      examples/riscv/src/main.rs
  2. 2 1
      src/hal.rs
  3. 4 8
      src/lib.rs

+ 6 - 6
examples/riscv/src/main.rs

@@ -5,10 +5,10 @@
 extern crate alloc;
 extern crate opensbi_rt;
 
+use alloc::vec;
 use device_tree::util::SliceRead;
 use device_tree::{DeviceTree, Node};
-use log::{info, LevelFilter, warn};
-use alloc::vec;
+use log::{info, warn, LevelFilter};
 use virtio_drivers::*;
 
 mod virtio_impl;
@@ -56,8 +56,9 @@ fn virtio_probe(node: &Node) {
         info!("walk dt addr={:#x}, size={:#x}", paddr, size);
         let header = unsafe { &mut *(vaddr as *mut VirtIOHeader) };
         info!(
-            "Detected virtio device with vendor id {:#X}",
-            header.vendor_id()
+            "Detected virtio device with vendor id {:#X}, device type {:?}",
+            header.vendor_id(),
+            header.device_type(),
         );
         info!("Device tree node {:?}", node);
         match header.device_type() {
@@ -102,8 +103,7 @@ fn virtio_gpu(header: &'static mut VirtIOHeader) {
 
 fn virtio_input(header: &'static mut VirtIOHeader) {
     //let mut event_buf = [0u64; 32];
-    let mut _input =
-        VirtIOInput::new(header).expect("failed to create input driver");
+    let mut _input = VirtIOInput::new(header).expect("failed to create input driver");
     // loop {
     //     input.ack_interrupt().expect("failed to ack");
     //     info!("mouse: {:?}", input.mouse_xy());

+ 2 - 1
src/hal.rs

@@ -3,6 +3,7 @@ use super::*;
 type VirtAddr = usize;
 type PhysAddr = usize;
 
+/// A region of contiguous physical memory used for DMA.
 pub struct DMA {
     paddr: u32,
     pages: u32,
@@ -28,7 +29,7 @@ impl DMA {
         phys_to_virt(self.paddr as usize)
     }
 
-    /// Page frame number
+    /// Returns the physical page frame number.
     pub fn pfn(&self) -> u32 {
         self.paddr >> 12
     }

+ 4 - 8
src/lib.rs

@@ -26,18 +26,14 @@ use self::queue::VirtQueue;
 use core::mem::size_of;
 use hal::*;
 
+/// The page size in bytes supported by the library (4 KiB).
 const PAGE_SIZE: usize = 0x1000;
 
 /// The type returned by driver methods.
 pub type Result<T = ()> = core::result::Result<T, Error>;
 
-// pub struct Error {
-//     kind: ErrorKind,
-//     reason: &'static str,
-// }
-
 /// The error type of VirtIO drivers.
-#[derive(Debug, Eq, PartialEq)]
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
 pub enum Error {
     /// The buffer is too small.
     BufferTooSmall,
@@ -58,12 +54,12 @@ fn align_up(size: usize) -> usize {
     (size + PAGE_SIZE) & !(PAGE_SIZE - 1)
 }
 
-/// Pages of `size`.
+/// The number of pages required to store `size` bytes, rounded up to a whole number of pages.
 fn pages(size: usize) -> usize {
     (size + PAGE_SIZE - 1) / PAGE_SIZE
 }
 
-/// Convert a struct into buffer.
+/// Convert a struct into a byte buffer.
 unsafe trait AsBuf: Sized {
     fn as_buf(&self) -> &[u8] {
         unsafe { core::slice::from_raw_parts(self as *const _ as _, size_of::<Self>()) }