virtio_input.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-11 GuEe-GUI the first version
  9. */
  10. #ifndef __VIRTIO_INPUT_H__
  11. #define __VIRTIO_INPUT_H__
  12. #include <rtdef.h>
  13. #include <virtio.h>
  14. #include <virtio_input_event_codes.h>
  15. #define VIRTIO_INPUT_QUEUE_EVENT 0
  16. #define VIRTIO_INPUT_QUEUE_STATUS 1
  17. #define VIRTIO_INPUT_EVENT_QUEUE_SIZE 64
  18. #define VIRTIO_INPUT_STATUS_QUEUE_SIZE 8
  19. #define VIRTIO_INPUT_QUEUE_MAX_SIZE (VIRTIO_INPUT_EVENT_QUEUE_SIZE > VIRTIO_INPUT_STATUS_QUEUE_SIZE ? \
  20. VIRTIO_INPUT_EVENT_QUEUE_SIZE : VIRTIO_INPUT_STATUS_QUEUE_SIZE)
  21. #define VIRTIO_INPUT_ABS_AXIS_X 0
  22. #define VIRTIO_INPUT_ABS_AXIS_Y 1
  23. enum virtio_input_type
  24. {
  25. VIRTIO_INPUT_TYPE_KEYBOARD,
  26. VIRTIO_INPUT_TYPE_MOUSE,
  27. VIRTIO_INPUT_TYPE_TABLET,
  28. VIRTIO_INPUT_TYPE_SIZE,
  29. };
  30. enum virtio_input_config_select
  31. {
  32. VIRTIO_INPUT_CFG_UNSET = 0x00,
  33. VIRTIO_INPUT_CFG_ID_NAME = 0x01,
  34. VIRTIO_INPUT_CFG_ID_SERIAL = 0x02,
  35. VIRTIO_INPUT_CFG_ID_DEVIDS = 0x03,
  36. VIRTIO_INPUT_CFG_PROP_BITS = 0x10,
  37. VIRTIO_INPUT_CFG_EV_BITS = 0x11,
  38. VIRTIO_INPUT_CFG_ABS_INFO = 0x12,
  39. };
  40. struct virtio_input_absinfo
  41. {
  42. rt_uint32_t min; /* Minimum value for the axis */
  43. rt_uint32_t max; /* Maximum value for the axis */
  44. rt_uint32_t fuzz; /* Fuzz value that is used to filter noise from the event stream */
  45. rt_uint32_t flat; /* Within this value will be discarded by joydev interface and reported as 0 instead */
  46. rt_uint32_t res; /* Resolution for the values reported for the axis */
  47. };
  48. struct virtio_input_devids
  49. {
  50. rt_uint16_t bustype;
  51. rt_uint16_t vendor;
  52. rt_uint16_t product;
  53. rt_uint16_t version;
  54. };
  55. struct virtio_input_config
  56. {
  57. rt_uint8_t select;
  58. rt_uint8_t subsel;
  59. rt_uint8_t size;
  60. rt_uint8_t reserved[5];
  61. union
  62. {
  63. char string[128];
  64. rt_uint8_t bitmap[128];
  65. struct virtio_input_absinfo abs;
  66. struct virtio_input_devids ids;
  67. };
  68. } __attribute__((packed));
  69. struct virtio_input_event
  70. {
  71. rt_uint16_t type;
  72. rt_uint16_t code;
  73. rt_uint32_t value;
  74. };
  75. #ifdef ARCH_CPU_64BIT
  76. #define BITS_PER_LONG 64
  77. #else
  78. #define BITS_PER_LONG 32
  79. #endif
  80. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  81. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  82. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  83. #define BITS_PER_BYTE 8
  84. #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
  85. #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
  86. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
  87. struct virtio_input_device
  88. {
  89. struct rt_device parent;
  90. struct virtio_device virtio_dev;
  91. rt_ubase_t ev_bit[BITS_TO_LONGS(EV_CNT)];
  92. rt_ubase_t key_bit[BITS_TO_LONGS(KEY_CNT)];
  93. rt_ubase_t rel_bit[BITS_TO_LONGS(REL_CNT)];
  94. rt_ubase_t abs_bit[BITS_TO_LONGS(ABS_CNT)];
  95. enum virtio_input_type type;
  96. struct virtio_input_config *config;
  97. /* Broadcast events */
  98. struct rt_mutex rw_mutex;
  99. void (*bsct_handler)(struct virtio_input_event event);
  100. struct virtio_input_event bcst_events[VIRTIO_INPUT_EVENT_QUEUE_SIZE];
  101. /* Receive events */
  102. struct virtio_input_event recv_events[VIRTIO_INPUT_EVENT_QUEUE_SIZE];
  103. /* Transmit status */
  104. struct virtio_input_event xmit_events[VIRTIO_INPUT_STATUS_QUEUE_SIZE];
  105. };
  106. enum
  107. {
  108. VIRTIO_DEVICE_CTRL_INPUT_GET_TYPE = 0x20,
  109. VIRTIO_DEVICE_CTRL_INPUT_BIND_BSCT_HANDLER,
  110. VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_X_INFO,
  111. VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_Y_INFO,
  112. VIRTIO_DEVICE_CTRL_INPUT_SET_STATUS,
  113. VIRTIO_DEVICE_CTRL_INPUT_GET_EV_BIT,
  114. VIRTIO_DEVICE_CTRL_INPUT_GET_KEY_BIT,
  115. VIRTIO_DEVICE_CTRL_INPUT_GET_REL_BIT,
  116. VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_BIT,
  117. };
  118. rt_err_t rt_virtio_input_init(rt_ubase_t *mmio_base, rt_uint32_t irq);
  119. #endif /* __VIRTIO_INPUT_H__ */