1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use super::*;
use alloc::boxed::Box;
use bitflags::*;
use log::*;
use volatile::{ReadOnly, WriteOnly};
pub struct VirtIOInput<'a> {
header: &'static mut VirtIOHeader,
event_queue: VirtQueue<'a>,
status_queue: VirtQueue<'a>,
event_buf: Box<[InputEvent; 32]>,
}
impl<'a> VirtIOInput<'a> {
pub fn new(header: &'static mut VirtIOHeader) -> Result<Self> {
let mut event_buf = Box::new([InputEvent::default(); QUEUE_SIZE]);
header.begin_init(|features| {
let features = Feature::from_bits_truncate(features);
info!("Device features: {:?}", features);
let supported_features = Feature::empty();
(features & supported_features).bits()
});
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() {
let token = event_queue.add(&[], &[event.as_buf_mut()])?;
assert_eq!(token, i as u16);
}
header.finish_init();
Ok(VirtIOInput {
header,
event_queue,
status_queue,
event_buf,
})
}
pub fn ack_interrupt(&mut self) -> bool {
self.header.ack_interrupt()
}
pub fn pop_pending_event(&mut self) -> Option<InputEvent> {
if let Ok((token, _)) = self.event_queue.pop_used() {
let event = &mut self.event_buf[token as usize];
if self.event_queue.add(&[], &[event.as_buf_mut()]).is_ok() {
return Some(*event);
}
}
None
}
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
}
}
#[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>,
_reversed: [ReadOnly<u8>; 5],
data: ReadOnly<[u8; 128]>,
}
#[repr(C)]
#[derive(Debug)]
struct AbsInfo {
min: u32,
max: u32,
fuzz: u32,
flat: u32,
res: u32,
}
#[repr(C)]
#[derive(Debug)]
struct DevIDs {
bustype: u16,
vendor: u16,
product: u16,
version: u16,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct InputEvent {
pub event_type: u16,
pub code: u16,
pub value: u32,
}
unsafe impl AsBuf for InputEvent {}
bitflags! {
struct Feature: u64 {
const NOTIFY_ON_EMPTY = 1 << 24;
const ANY_LAYOUT = 1 << 27;
const RING_INDIRECT_DESC = 1 << 28;
const RING_EVENT_IDX = 1 << 29;
const UNUSED = 1 << 30;
const VERSION_1 = 1 << 32;
const ACCESS_PLATFORM = 1 << 33;
const RING_PACKED = 1 << 34;
const IN_ORDER = 1 << 35;
const ORDER_PLATFORM = 1 << 36;
const SR_IOV = 1 << 37;
const NOTIFICATION_DATA = 1 << 38;
}
}
const QUEUE_EVENT: usize = 0;
const QUEUE_STATUS: usize = 1;
const QUEUE_SIZE: usize = 32;