You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usbkbd.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef _USBKBD_H
  2. #define _USBKBD_H
  3. /** @file
  4. *
  5. * USB keyboard driver
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <assert.h>
  10. #include <ipxe/usb.h>
  11. #include <ipxe/usbhid.h>
  12. /** Keyboard protocol */
  13. #define USBKBD_PROTOCOL 1
  14. /** A USB keyboard report */
  15. struct usb_keyboard_report {
  16. /** Modifier keys */
  17. uint8_t modifiers;
  18. /** Reserved */
  19. uint8_t reserved;
  20. /** Keycodes */
  21. uint8_t keycode[6];
  22. } __attribute__ (( packed ));
  23. /** USB modifier keys */
  24. enum usb_keyboard_modifier {
  25. /** Left Ctrl key */
  26. USBKBD_CTRL_LEFT = 0x01,
  27. /** Left Shift key */
  28. USBKBD_SHIFT_LEFT = 0x02,
  29. /** Left Alt key */
  30. USBKBD_ALT_LEFT = 0x04,
  31. /** Left GUI key */
  32. USBKBD_GUI_LEFT = 0x08,
  33. /** Right Ctrl key */
  34. USBKBD_CTRL_RIGHT = 0x10,
  35. /** Right Shift key */
  36. USBKBD_SHIFT_RIGHT = 0x20,
  37. /** Right Alt key */
  38. USBKBD_ALT_RIGHT = 0x40,
  39. /** Right GUI key */
  40. USBKBD_GUI_RIGHT = 0x80,
  41. };
  42. /** Either Ctrl key */
  43. #define USBKBD_CTRL ( USBKBD_CTRL_LEFT | USBKBD_CTRL_RIGHT )
  44. /** Either Shift key */
  45. #define USBKBD_SHIFT ( USBKBD_SHIFT_LEFT | USBKBD_SHIFT_RIGHT )
  46. /** Either Alt key */
  47. #define USBKBD_ALT ( USBKBD_ALT_LEFT | USBKBD_ALT_RIGHT )
  48. /** Either GUI key */
  49. #define USBKBD_GUI ( USBKBD_GUI_LEFT | USBKBD_GUI_RIGHT )
  50. /** USB keycodes */
  51. enum usb_keycode {
  52. USBKBD_KEY_A = 0x04,
  53. USBKBD_KEY_Z = 0x1d,
  54. USBKBD_KEY_1 = 0x1e,
  55. USBKBD_KEY_0 = 0x27,
  56. USBKBD_KEY_ENTER = 0x28,
  57. USBKBD_KEY_SPACE = 0x2c,
  58. USBKBD_KEY_MINUS = 0x2d,
  59. USBKBD_KEY_SLASH = 0x38,
  60. USBKBD_KEY_CAPS_LOCK = 0x39,
  61. USBKBD_KEY_F1 = 0x3a,
  62. USBKBD_KEY_UP = 0x52,
  63. USBKBD_KEY_NUM_LOCK = 0x53,
  64. USBKBD_KEY_PAD_ENTER = 0x58,
  65. USBKBD_KEY_PAD_1 = 0x59,
  66. USBKBD_KEY_PAD_DOT = 0x63,
  67. };
  68. /** USB keyboard LEDs */
  69. enum usb_keyboard_led {
  70. USBKBD_LED_NUM_LOCK = 0x01,
  71. USBKBD_LED_CAPS_LOCK = 0x02,
  72. USBKBD_LED_SCROLL_LOCK = 0x04,
  73. };
  74. /** Keyboard idle duration (in 4ms units)
  75. *
  76. * This is a policy decision. We choose to use an autorepeat rate of
  77. * approximately 40ms.
  78. */
  79. #define USBKBD_IDLE_DURATION 10 /* 10 x 4ms = 40ms */
  80. /** Keyboard auto-repeat hold-off (in units of USBKBD_IDLE_DURATION)
  81. *
  82. * This is a policy decision. We choose to use an autorepeat delay of
  83. * approximately 500ms.
  84. */
  85. #define USBKBD_HOLDOFF 12 /* 12 x 40ms = 480ms */
  86. /** Interrupt endpoint maximum fill level
  87. *
  88. * When idling, we are likely to poll the USB endpoint at only the
  89. * 18.2Hz system timer tick rate. With a typical observed bInterval
  90. * of 10ms (which will be rounded down to 8ms by the HCI drivers),
  91. * this gives approximately 7 completions per poll.
  92. */
  93. #define USBKBD_INTR_MAX_FILL 8
  94. /** Keyboard buffer size
  95. *
  96. * Must be a power of two.
  97. */
  98. #define USBKBD_BUFSIZE 8
  99. /** A USB keyboard device */
  100. struct usb_keyboard {
  101. /** Name */
  102. const char *name;
  103. /** List of all USB keyboards */
  104. struct list_head list;
  105. /** USB bus */
  106. struct usb_bus *bus;
  107. /** USB human interface device */
  108. struct usb_hid hid;
  109. /** Most recent keyboard report */
  110. struct usb_keyboard_report report;
  111. /** Most recently pressed non-modifier key (if any) */
  112. unsigned int keycode;
  113. /** Autorepeat hold-off time (in number of completions reported) */
  114. unsigned int holdoff;
  115. /** Keyboard LED state */
  116. uint8_t leds;
  117. /** Keyboard LEDs changed */
  118. uint8_t leds_changed;
  119. /** Keyboard buffer
  120. *
  121. * This stores iPXE key values.
  122. */
  123. unsigned int key[USBKBD_BUFSIZE];
  124. /** Keyboard buffer producer counter */
  125. unsigned int prod;
  126. /** Keyboard buffer consumer counter */
  127. unsigned int cons;
  128. /** Keyboard buffer sub-consumer counter
  129. *
  130. * This represents the index within the ANSI escape sequence
  131. * corresponding to an iPXE key value.
  132. */
  133. unsigned int subcons;
  134. };
  135. /**
  136. * Calculate keyboard buffer fill level
  137. *
  138. * @v kbd USB keyboard
  139. * @ret fill Keyboard buffer fill level
  140. */
  141. static inline __attribute__ (( always_inline )) unsigned int
  142. usbkbd_fill ( struct usb_keyboard *kbd ) {
  143. unsigned int fill = ( kbd->prod - kbd->cons );
  144. assert ( fill <= USBKBD_BUFSIZE );
  145. return fill;
  146. }
  147. #endif /* _USBKBD_H */