浏览代码

Support virtio-input config select and subsel

Yuekai Jia 3 年之前
父节点
当前提交
2aaf7d60c5
共有 3 个文件被更改,包括 41 次插入16 次删除
  1. 1 1
      Cargo.toml
  2. 39 14
      src/input.rs
  3. 1 1
      src/lib.rs

+ 1 - 1
Cargo.toml

@@ -1,7 +1,7 @@
 [package]
 name = "virtio-drivers"
 version = "0.1.0"
-authors = ["Jiajie Chen <noc@jiegec.ac.cn>", "Runji Wang <wangrunji0408@163.com>"]
+authors = ["Jiajie Chen <noc@jiegec.ac.cn>", "Runji Wang <wangrunji0408@163.com>", "Yuekai Jia <equation618@gmail.com>"]
 edition = "2018"
 description = "VirtIO guest drivers."
 

+ 39 - 14
src/input.rs

@@ -2,7 +2,7 @@ use super::*;
 use alloc::boxed::Box;
 use bitflags::*;
 use log::*;
-use volatile::Volatile;
+use volatile::{ReadOnly, WriteOnly};
 
 /// Virtual human interface devices such as keyboards, mice and tablets.
 ///
@@ -28,10 +28,6 @@ impl<'a> VirtIOInput<'a> {
             (features & supported_features).bits()
         });
 
-        // read configuration space
-        let config = unsafe { &mut *(header.config_space() as *mut Config) };
-        info!("Config: {:?}", config);
-
         let mut event_queue = VirtQueue::new(header, QUEUE_EVENT, QUEUE_SIZE as u16)?;
         let status_queue = VirtQueue::new(header, QUEUE_STATUS, QUEUE_SIZE as u16)?;
         for (i, event) in event_buf.as_mut().iter_mut().enumerate() {
@@ -65,28 +61,57 @@ impl<'a> VirtIOInput<'a> {
         }
         None
     }
+
+    /// Query a specific piece of information by `select` and `subsel`, and write
+    /// result to `out`, return the result size.
+    pub fn query_config_select(
+        &mut self,
+        select: InputConfigSelect,
+        subsel: u8,
+        out: &mut [u8],
+    ) -> u8 {
+        let config = unsafe { &mut *(self.header.config_space() as *mut Config) };
+        config.select.write(select as u8);
+        config.subsel.write(subsel);
+        let size = config.size.read();
+        let data = config.data.read();
+        out[..size as usize].copy_from_slice(&data[..size as usize]);
+        size
+    }
 }
 
+/// Select value used for [`VirtIOInput::query_config_select()`].
 #[repr(u8)]
-#[derive(Debug)]
-enum Cfg {
-    Unset = 0x00,
+#[derive(Debug, Clone, Copy)]
+pub enum InputConfigSelect {
+    /// Returns the name of the device, in u.string. subsel is zero.
     IdName = 0x01,
+    /// Returns the serial number of the device, in u.string. subsel is zero.
     IdSerial = 0x02,
+    /// Returns ID information of the device, in u.ids. subsel is zero.
     IdDevids = 0x03,
+    /// Returns input properties of the device, in u.bitmap. subsel is zero.
+    /// Individual bits in the bitmap correspond to INPUT_PROP_* constants used
+    /// by the underlying evdev implementation.
     PropBits = 0x10,
+    /// subsel specifies the event type using EV_* constants in the underlying
+    /// evdev implementation. If size is non-zero the event type is supported
+    /// and a bitmap of supported event codes is returned in u.bitmap. Individual
+    /// bits in the bitmap correspond to implementation-defined input event codes,
+    /// for example keys or pointing device axes.
     EvBits = 0x11,
+    /// subsel specifies the absolute axis using ABS_* constants in the underlying
+    /// evdev implementation. Information about the axis will be returned in u.abs.
     AbsInfo = 0x12,
 }
 
 #[repr(C)]
-#[derive(Debug)]
 struct Config {
-    select: Volatile<u8>,
-    subsel: Volatile<u8>,
-    size: u8,
-    reversed: [u8; 5],
-    data: [u8; 32],
+    select: WriteOnly<u8>,
+    subsel: WriteOnly<u8>,
+    size: ReadOnly<u8>,
+    _reversed: [ReadOnly<u8>; 5],
+    data: ReadOnly<[u8; 128]>,
 }
 
 #[repr(C)]

+ 1 - 1
src/lib.rs

@@ -23,7 +23,7 @@ pub use self::blk::VirtIOBlk;
 pub use self::console::VirtIOConsole;
 pub use self::gpu::VirtIOGpu;
 pub use self::header::*;
-pub use self::input::{InputEvent, VirtIOInput};
+pub use self::input::{InputConfigSelect, InputEvent, VirtIOInput};
 pub use self::net::VirtIONet;
 use self::queue::VirtQueue;
 use core::mem::size_of;