Skip to content

Commit c836bb6

Browse files
committed
Implement virtio-input devices
1 parent 2a81374 commit c836bb6

File tree

9 files changed

+1993
-12
lines changed

9 files changed

+1993
-12
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ ifeq ($(call has, VIRTIONET), 1)
4646
OBJS_EXTRA += netdev.o
4747
endif
4848

49+
# virtio-input
50+
ENABLE_VIRTIOINPUT ?= 1
51+
ifneq ($(UNAME_S),Linux)
52+
ENABLE_VIRTIOINPUT := 0
53+
endif
54+
$(call set-feature, VIRTIOINPUT)
55+
ifeq ($(call has, VIRTIOINPUT), 1)
56+
OBJS_EXTRA += virtio-input.o
57+
endif
58+
4959
# virtio-gpu
5060
ENABLE_VIRTIOGPU ?= 1
5161
ifneq ($(UNAME_S),Linux)

common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include "feature.h"
44

5+
#define BITS_PER_CHAR 8
6+
#define BITS_PER_LONG (BITS_PER_CHAR * sizeof(long))
7+
58
#define unlikely(x) __builtin_expect((x), 0)
69
#define likely(x) __builtin_expect((x), 1)
710

@@ -17,6 +20,16 @@ static inline int ilog2(int x)
1720
return 31 - __builtin_clz(x | 1);
1821
}
1922

23+
static inline void set_bit(unsigned long bit, unsigned long *word)
24+
{
25+
*word |= (1 << bit);
26+
}
27+
28+
static inline void bitmap_set_bit(unsigned long *map, unsigned long bit)
29+
{
30+
set_bit(bit % BITS_PER_LONG, &map[bit / BITS_PER_LONG]);
31+
}
32+
2033
/* Range check
2134
* For any variable range checking:
2235
* if (x >= minx && x <= maxx) ...

device.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#define DTB_SIZE (1 * 1024 * 1024)
1111
#define INITRD_SIZE (65 * 1024 * 1024)
1212

13+
#define SCREEN_WIDTH 1024
14+
#define SCREEN_HEIGHT 768
15+
1316
void ram_read(hart_t *core,
1417
uint32_t *mem,
1518
const uint32_t addr,
@@ -223,6 +226,64 @@ void virtio_gpu_add_scanout(virtio_gpu_state_t *vgpu,
223226
uint32_t height);
224227
#endif /* SEMU_HAS(VIRTIOGPU) */
225228

229+
/* VirtIO Input */
230+
231+
#if SEMU_HAS(VIRTIOINPUT)
232+
233+
#define IRQ_VINPUT_KEYBOARD 5
234+
#define IRQ_VINPUT_KEYBOARD_BIT (1 << IRQ_VINPUT_KEYBOARD)
235+
236+
#define IRQ_VINPUT_MOUSE 6
237+
#define IRQ_VINPUT_MOUSE_BIT (1 << IRQ_VINPUT_MOUSE)
238+
239+
typedef struct {
240+
uint32_t QueueNum;
241+
uint32_t QueueDesc;
242+
uint32_t QueueAvail;
243+
uint32_t QueueUsed;
244+
uint16_t last_avail;
245+
bool ready;
246+
} virtio_input_queue_t;
247+
248+
typedef struct {
249+
/* feature negotiation */
250+
uint32_t DeviceFeaturesSel;
251+
uint32_t DriverFeatures;
252+
uint32_t DriverFeaturesSel;
253+
/* queue config */
254+
uint32_t QueueSel;
255+
virtio_input_queue_t queues[2];
256+
/* status */
257+
uint32_t Status;
258+
uint32_t InterruptStatus;
259+
/* supplied by environment */
260+
uint32_t *ram;
261+
/* implementation-specific */
262+
int id; // FIXME
263+
void *priv;
264+
} virtio_input_state_t;
265+
266+
void virtio_input_read(hart_t *vm,
267+
virtio_input_state_t *vinput,
268+
uint32_t addr,
269+
uint8_t width,
270+
uint32_t *value);
271+
272+
void virtio_input_write(hart_t *vm,
273+
virtio_input_state_t *vinput,
274+
uint32_t addr,
275+
uint8_t width,
276+
uint32_t value);
277+
278+
void virtio_input_init(virtio_input_state_t *vinput);
279+
280+
void virtio_input_update_key(uint32_t key, uint32_t state);
281+
282+
void virtio_input_update_mouse_button_state(uint32_t button, bool pressed);
283+
284+
void virtio_input_update_cursor(uint32_t x, uint32_t y);
285+
#endif /* SEMU_HAS(VIRTIOINPUT) */
286+
226287
/* ACLINT MTIMER */
227288
typedef struct {
228289
/* A MTIMER device has two separate base addresses: one for the MTIME
@@ -326,6 +387,10 @@ typedef struct {
326387
#endif
327388
#if SEMU_HAS(VIRTIOGPU)
328389
virtio_gpu_state_t vgpu;
390+
#endif
391+
#if SEMU_HAS(VIRTIOINPUT)
392+
virtio_input_state_t vkeyboard;
393+
virtio_input_state_t vmouse;
329394
#endif
330395
/* ACLINT */
331396
mtimer_state_t mtimer;

feature.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
#define SEMU_FEATURE_VIRTIOGPU 1
1818
#endif
1919

20+
/* virtio-input */
21+
#ifndef SEMU_FEATURE_VIRTIOINPUT
22+
#define SEMU_FEATURE_VIRTIOINPUT 1
23+
#endif
24+
2025
/* Feature test macro */
2126
#define SEMU_HAS(x) SEMU_FEATURE_##x

0 commit comments

Comments
 (0)