use super::common::Feature;
use crate::hal::Hal;
use crate::queue::VirtQueue;
use crate::transport::Transport;
use crate::volatile::{volread, volwrite, ReadOnly, VolatileReadable, WriteOnly};
use crate::Error;
use alloc::{boxed::Box, string::String};
use core::cmp::min;
use core::mem::size_of;
use core::ptr::{addr_of, NonNull};
use zerocopy::{AsBytes, FromBytes, FromZeroes};
pub struct VirtIOInput<H: Hal, T: Transport> {
transport: T,
event_queue: VirtQueue<H, QUEUE_SIZE>,
status_queue: VirtQueue<H, QUEUE_SIZE>,
event_buf: Box<[InputEvent; 32]>,
config: NonNull<Config>,
}
impl<H: Hal, T: Transport> VirtIOInput<H, T> {
pub fn new(mut transport: T) -> Result<Self, Error> {
let mut event_buf = Box::new([InputEvent::default(); QUEUE_SIZE]);
let negotiated_features = transport.begin_init(SUPPORTED_FEATURES);
let config = transport.config_space::<Config>()?;
let mut event_queue = VirtQueue::new(
&mut transport,
QUEUE_EVENT,
negotiated_features.contains(Feature::RING_INDIRECT_DESC),
negotiated_features.contains(Feature::RING_EVENT_IDX),
)?;
let status_queue = VirtQueue::new(
&mut transport,
QUEUE_STATUS,
negotiated_features.contains(Feature::RING_INDIRECT_DESC),
negotiated_features.contains(Feature::RING_EVENT_IDX),
)?;
for (i, event) in event_buf.as_mut().iter_mut().enumerate() {
let token = unsafe { event_queue.add(&[], &mut [event.as_bytes_mut()])? };
assert_eq!(token, i as u16);
}
if event_queue.should_notify() {
transport.notify(QUEUE_EVENT);
}
transport.finish_init();
Ok(VirtIOInput {
transport,
event_queue,
status_queue,
event_buf,
config,
})
}
pub fn ack_interrupt(&mut self) -> bool {
self.transport.ack_interrupt()
}
pub fn pop_pending_event(&mut self) -> Option<InputEvent> {
if let Some(token) = self.event_queue.peek_used() {
let event = &mut self.event_buf[token as usize];
unsafe {
self.event_queue
.pop_used(token, &[], &mut [event.as_bytes_mut()])
.ok()?;
}
let event_saved = *event;
if let Ok(new_token) = unsafe { self.event_queue.add(&[], &mut [event.as_bytes_mut()]) }
{
assert_eq!(new_token, token);
if self.event_queue.should_notify() {
self.transport.notify(QUEUE_EVENT);
}
return Some(event_saved);
}
}
None
}
pub fn query_config_select(
&mut self,
select: InputConfigSelect,
subsel: u8,
out: &mut [u8],
) -> u8 {
let size;
unsafe {
volwrite!(self.config, select, select as u8);
volwrite!(self.config, subsel, subsel);
size = volread!(self.config, size);
let size_to_copy = min(usize::from(size), out.len());
for (i, out_item) in out.iter_mut().take(size_to_copy).enumerate() {
*out_item = addr_of!((*self.config.as_ptr()).data[i]).vread();
}
}
size
}
fn query_config_select_alloc(
&mut self,
select: InputConfigSelect,
subsel: u8,
) -> Result<Box<[u8]>, Error> {
unsafe {
volwrite!(self.config, select, select as u8);
volwrite!(self.config, subsel, subsel);
let size = usize::from(volread!(self.config, size));
if size > CONFIG_DATA_MAX_LENGTH {
return Err(Error::IoError);
}
let mut buf = u8::new_box_slice_zeroed(size);
for i in 0..size {
buf[i] = addr_of!((*self.config.as_ptr()).data[i]).vread();
}
Ok(buf)
}
}
fn query_config_string(
&mut self,
select: InputConfigSelect,
subsel: u8,
) -> Result<String, Error> {
Ok(String::from_utf8(
self.query_config_select_alloc(select, subsel)?.into(),
)?)
}
pub fn name(&mut self) -> Result<String, Error> {
self.query_config_string(InputConfigSelect::IdName, 0)
}
pub fn serial_number(&mut self) -> Result<String, Error> {
self.query_config_string(InputConfigSelect::IdSerial, 0)
}
pub fn ids(&mut self) -> Result<DevIDs, Error> {
let mut ids = DevIDs::default();
let size = self.query_config_select(InputConfigSelect::IdDevids, 0, ids.as_bytes_mut());
if usize::from(size) == size_of::<DevIDs>() {
Ok(ids)
} else {
Err(Error::IoError)
}
}
pub fn prop_bits(&mut self) -> Result<Box<[u8]>, Error> {
self.query_config_select_alloc(InputConfigSelect::PropBits, 0)
}
pub fn ev_bits(&mut self, event_type: u8) -> Result<Box<[u8]>, Error> {
self.query_config_select_alloc(InputConfigSelect::EvBits, event_type)
}
pub fn abs_info(&mut self, axis: u8) -> Result<AbsInfo, Error> {
let mut info = AbsInfo::default();
let size = self.query_config_select(InputConfigSelect::AbsInfo, axis, info.as_bytes_mut());
if usize::from(size) == size_of::<AbsInfo>() {
Ok(info)
} else {
Err(Error::IoError)
}
}
}
unsafe impl<H: Hal, T: Transport + Send> Send for VirtIOInput<H, T> where
VirtQueue<H, QUEUE_SIZE>: Send
{
}
unsafe impl<H: Hal, T: Transport + Sync> Sync for VirtIOInput<H, T> where
VirtQueue<H, QUEUE_SIZE>: Sync
{
}
impl<H: Hal, T: Transport> Drop for VirtIOInput<H, T> {
fn drop(&mut self) {
self.transport.queue_unset(QUEUE_EVENT);
self.transport.queue_unset(QUEUE_STATUS);
}
}
const CONFIG_DATA_MAX_LENGTH: usize = 128;
#[repr(u8)]
#[derive(Debug, Clone, Copy)]
pub enum InputConfigSelect {
IdName = 0x01,
IdSerial = 0x02,
IdDevids = 0x03,
PropBits = 0x10,
EvBits = 0x11,
AbsInfo = 0x12,
}
#[repr(C)]
struct Config {
select: WriteOnly<u8>,
subsel: WriteOnly<u8>,
size: ReadOnly<u8>,
_reserved: [ReadOnly<u8>; 5],
data: [ReadOnly<u8>; CONFIG_DATA_MAX_LENGTH],
}
#[repr(C)]
#[derive(AsBytes, Clone, Debug, Default, Eq, PartialEq, FromBytes, FromZeroes)]
pub struct AbsInfo {
pub min: u32,
pub max: u32,
pub fuzz: u32,
pub flat: u32,
pub res: u32,
}
#[repr(C)]
#[derive(AsBytes, Clone, Debug, Default, Eq, PartialEq, FromBytes, FromZeroes)]
pub struct DevIDs {
pub bustype: u16,
pub vendor: u16,
pub product: u16,
pub version: u16,
}
#[repr(C)]
#[derive(AsBytes, Clone, Copy, Debug, Default, FromBytes, FromZeroes)]
pub struct InputEvent {
pub event_type: u16,
pub code: u16,
pub value: u32,
}
const QUEUE_EVENT: u16 = 0;
const QUEUE_STATUS: u16 = 1;
const SUPPORTED_FEATURES: Feature = Feature::RING_EVENT_IDX.union(Feature::RING_INDIRECT_DESC);
const QUEUE_SIZE: usize = 32;
#[cfg(test)]
mod tests {
use super::*;
use crate::{
hal::fake::FakeHal,
transport::{
fake::{FakeTransport, QueueStatus, State},
DeviceType,
},
};
use alloc::{sync::Arc, vec};
use core::convert::TryInto;
use std::sync::Mutex;
#[test]
fn config() {
const DEFAULT_DATA: ReadOnly<u8> = ReadOnly::new(0);
let mut config_space = Config {
select: WriteOnly::default(),
subsel: WriteOnly::default(),
size: ReadOnly::new(0),
_reserved: Default::default(),
data: [DEFAULT_DATA; 128],
};
let state = Arc::new(Mutex::new(State {
queues: vec![QueueStatus::default(), QueueStatus::default()],
..Default::default()
}));
let transport = FakeTransport {
device_type: DeviceType::Block,
max_queue_size: QUEUE_SIZE.try_into().unwrap(),
device_features: 0,
config_space: NonNull::from(&mut config_space),
state: state.clone(),
};
let mut input = VirtIOInput::<FakeHal, FakeTransport<Config>>::new(transport).unwrap();
set_data(&mut config_space, "Test input device".as_bytes());
assert_eq!(input.name().unwrap(), "Test input device");
assert_eq!(config_space.select.0, InputConfigSelect::IdName as u8);
assert_eq!(config_space.subsel.0, 0);
set_data(&mut config_space, "Serial number".as_bytes());
assert_eq!(input.serial_number().unwrap(), "Serial number");
assert_eq!(config_space.select.0, InputConfigSelect::IdSerial as u8);
assert_eq!(config_space.subsel.0, 0);
let ids = DevIDs {
bustype: 0x4242,
product: 0x0067,
vendor: 0x1234,
version: 0x4321,
};
set_data(&mut config_space, ids.as_bytes());
assert_eq!(input.ids().unwrap(), ids);
assert_eq!(config_space.select.0, InputConfigSelect::IdDevids as u8);
assert_eq!(config_space.subsel.0, 0);
set_data(&mut config_space, &[0x12, 0x34, 0x56]);
assert_eq!(input.prop_bits().unwrap().as_ref(), &[0x12, 0x34, 0x56]);
assert_eq!(config_space.select.0, InputConfigSelect::PropBits as u8);
assert_eq!(config_space.subsel.0, 0);
set_data(&mut config_space, &[0x42, 0x66]);
assert_eq!(input.ev_bits(3).unwrap().as_ref(), &[0x42, 0x66]);
assert_eq!(config_space.select.0, InputConfigSelect::EvBits as u8);
assert_eq!(config_space.subsel.0, 3);
let abs_info = AbsInfo {
min: 12,
max: 1234,
fuzz: 4,
flat: 10,
res: 2,
};
set_data(&mut config_space, abs_info.as_bytes());
assert_eq!(input.abs_info(5).unwrap(), abs_info);
assert_eq!(config_space.select.0, InputConfigSelect::AbsInfo as u8);
assert_eq!(config_space.subsel.0, 5);
}
fn set_data(config_space: &mut Config, value: &[u8]) {
config_space.size.0 = value.len().try_into().unwrap();
for (i, &byte) in value.into_iter().enumerate() {
config_space.data[i].0 = byte;
}
}
}