summaryrefslogtreecommitdiff
path: root/components/legacy/virtio/virtio_input.h
blob: 700088e19e7168010b706aeca83ac278e115cb54 (plain)
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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-11     GuEe-GUI     the first version
 */

#ifndef __VIRTIO_INPUT_H__
#define __VIRTIO_INPUT_H__

#include <rtdef.h>

#include <virtio.h>
#include <virtio_input_event_codes.h>

#define VIRTIO_INPUT_QUEUE_EVENT        0
#define VIRTIO_INPUT_QUEUE_STATUS       1
#define VIRTIO_INPUT_EVENT_QUEUE_SIZE   64
#define VIRTIO_INPUT_STATUS_QUEUE_SIZE  8
#define VIRTIO_INPUT_QUEUE_MAX_SIZE     (VIRTIO_INPUT_EVENT_QUEUE_SIZE > VIRTIO_INPUT_STATUS_QUEUE_SIZE ? \
        VIRTIO_INPUT_EVENT_QUEUE_SIZE : VIRTIO_INPUT_STATUS_QUEUE_SIZE)

#define VIRTIO_INPUT_ABS_AXIS_X         0
#define VIRTIO_INPUT_ABS_AXIS_Y         1

enum virtio_input_type
{
    VIRTIO_INPUT_TYPE_KEYBOARD,
    VIRTIO_INPUT_TYPE_MOUSE,
    VIRTIO_INPUT_TYPE_TABLET,

    VIRTIO_INPUT_TYPE_SIZE,
};

enum virtio_input_config_select
{
    VIRTIO_INPUT_CFG_UNSET      = 0x00,
    VIRTIO_INPUT_CFG_ID_NAME    = 0x01,
    VIRTIO_INPUT_CFG_ID_SERIAL  = 0x02,
    VIRTIO_INPUT_CFG_ID_DEVIDS  = 0x03,
    VIRTIO_INPUT_CFG_PROP_BITS  = 0x10,
    VIRTIO_INPUT_CFG_EV_BITS    = 0x11,
    VIRTIO_INPUT_CFG_ABS_INFO   = 0x12,
};

struct virtio_input_absinfo
{
    rt_uint32_t min;    /* Minimum value for the axis */
    rt_uint32_t max;    /* Maximum value for the axis */
    rt_uint32_t fuzz;   /* Fuzz value that is used to filter noise from the event stream */
    rt_uint32_t flat;   /* Within this value will be discarded by joydev interface and reported as 0 instead */
    rt_uint32_t res;    /* Resolution for the values reported for the axis */
};

struct virtio_input_devids
{
    rt_uint16_t bustype;
    rt_uint16_t vendor;
    rt_uint16_t product;
    rt_uint16_t version;
};

struct virtio_input_config
{
    rt_uint8_t select;
    rt_uint8_t subsel;
    rt_uint8_t size;
    rt_uint8_t reserved[5];

    union
    {
        char string[128];
        rt_uint8_t bitmap[128];
        struct virtio_input_absinfo abs;
        struct virtio_input_devids ids;
    };
} __attribute__((packed));

struct virtio_input_event
{
    rt_uint16_t type;
    rt_uint16_t code;
    rt_uint32_t value;
};

#ifdef ARCH_CPU_64BIT
#define BITS_PER_LONG 64
#else
#define BITS_PER_LONG 32
#endif
#define BIT_MASK(nr)        (1UL << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr)        ((nr) / BITS_PER_LONG)
#define DIV_ROUND_UP(n, d)  (((n) + (d) - 1) / (d))

#define BITS_PER_BYTE       8
#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
#define BITS_TO_BYTES(nr)   DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
#define BITS_TO_LONGS(nr)   DIV_ROUND_UP(nr, BITS_PER_TYPE(long))

struct virtio_input_device
{
    struct rt_device parent;

    struct virtio_device virtio_dev;

    rt_ubase_t ev_bit[BITS_TO_LONGS(EV_CNT)];
    rt_ubase_t key_bit[BITS_TO_LONGS(KEY_CNT)];
    rt_ubase_t rel_bit[BITS_TO_LONGS(REL_CNT)];
    rt_ubase_t abs_bit[BITS_TO_LONGS(ABS_CNT)];

    enum virtio_input_type type;
    struct virtio_input_config *config;

    /* Broadcast events */
    struct rt_mutex rw_mutex;
    void (*bsct_handler)(struct virtio_input_event event);
    struct virtio_input_event bcst_events[VIRTIO_INPUT_EVENT_QUEUE_SIZE];

    /* Receive events */
    struct virtio_input_event recv_events[VIRTIO_INPUT_EVENT_QUEUE_SIZE];

    /* Transmit status */
    struct virtio_input_event xmit_events[VIRTIO_INPUT_STATUS_QUEUE_SIZE];
};

enum
{
    VIRTIO_DEVICE_CTRL_INPUT_GET_TYPE           = 0x20,
    VIRTIO_DEVICE_CTRL_INPUT_BIND_BSCT_HANDLER,
    VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_X_INFO,
    VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_Y_INFO,
    VIRTIO_DEVICE_CTRL_INPUT_SET_STATUS,

    VIRTIO_DEVICE_CTRL_INPUT_GET_EV_BIT,
    VIRTIO_DEVICE_CTRL_INPUT_GET_KEY_BIT,
    VIRTIO_DEVICE_CTRL_INPUT_GET_REL_BIT,
    VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_BIT,
};

rt_err_t rt_virtio_input_init(rt_ubase_t *mmio_base, rt_uint32_t irq);

#endif /* __VIRTIO_INPUT_H__ */