iPXE
usbkbd.h
Go to the documentation of this file.
1 #ifndef _USBKBD_H
2 #define _USBKBD_H
3 
4 /** @file
5  *
6  * USB keyboard driver
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <assert.h>
13 #include <ipxe/usb.h>
14 #include <ipxe/usbhid.h>
15 
16 /** Keyboard protocol */
17 #define USBKBD_PROTOCOL 1
18 
19 /** A USB keyboard report */
21  /** Modifier keys */
23  /** Reserved */
25  /** Keycodes */
27 } __attribute__ (( packed ));
28 
29 /** USB modifier keys */
31  /** Left Ctrl key */
33  /** Left Shift key */
35  /** Left Alt key */
37  /** Left GUI key */
39  /** Right Ctrl key */
41  /** Right Shift key */
43  /** Right Alt key */
45  /** Right GUI key */
47 };
48 
49 /** Either Ctrl key */
50 #define USBKBD_CTRL ( USBKBD_CTRL_LEFT | USBKBD_CTRL_RIGHT )
51 
52 /** Either Shift key */
53 #define USBKBD_SHIFT ( USBKBD_SHIFT_LEFT | USBKBD_SHIFT_RIGHT )
54 
55 /** Either Alt key */
56 #define USBKBD_ALT ( USBKBD_ALT_LEFT | USBKBD_ALT_RIGHT )
57 
58 /** Either GUI key */
59 #define USBKBD_GUI ( USBKBD_GUI_LEFT | USBKBD_GUI_RIGHT )
60 
61 /** USB keycodes */
63  USBKBD_KEY_A = 0x04,
64  USBKBD_KEY_Z = 0x1d,
65  USBKBD_KEY_1 = 0x1e,
66  USBKBD_KEY_0 = 0x27,
72  USBKBD_KEY_F1 = 0x3a,
73  USBKBD_KEY_UP = 0x52,
78 };
79 
80 /** USB keyboard LEDs */
85 };
86 
87 /** Keyboard idle duration (in 4ms units)
88  *
89  * This is a policy decision. We choose to use an autorepeat rate of
90  * approximately 40ms.
91  */
92 #define USBKBD_IDLE_DURATION 10 /* 10 x 4ms = 40ms */
93 
94 /** Keyboard auto-repeat hold-off (in units of USBKBD_IDLE_DURATION)
95  *
96  * This is a policy decision. We choose to use an autorepeat delay of
97  * approximately 500ms.
98  */
99 #define USBKBD_HOLDOFF 12 /* 12 x 40ms = 480ms */
100 
101 /** Interrupt endpoint maximum fill level
102  *
103  * When idling, we are likely to poll the USB endpoint at only the
104  * 18.2Hz system timer tick rate. With a typical observed bInterval
105  * of 10ms (which will be rounded down to 8ms by the HCI drivers),
106  * this gives approximately 7 completions per poll.
107  */
108 #define USBKBD_INTR_MAX_FILL 8
109 
110 /** Keyboard buffer size
111  *
112  * Must be a power of two.
113  */
114 #define USBKBD_BUFSIZE 8
115 
116 /** A USB keyboard device */
117 struct usb_keyboard {
118  /** Name */
119  const char *name;
120  /** List of all USB keyboards */
121  struct list_head list;
122 
123  /** USB bus */
124  struct usb_bus *bus;
125  /** USB human interface device */
126  struct usb_hid hid;
127 
128  /** Most recent keyboard report */
130  /** Most recently pressed non-modifier key (if any) */
131  unsigned int keycode;
132  /** Autorepeat hold-off time (in number of completions reported) */
133  unsigned int holdoff;
134 
135  /** Keyboard LED state */
137  /** Keyboard LEDs changed */
139 
140  /** Keyboard buffer
141  *
142  * This stores iPXE key values.
143  */
144  unsigned int key[USBKBD_BUFSIZE];
145  /** Keyboard buffer producer counter */
146  unsigned int prod;
147  /** Keyboard buffer consumer counter */
148  unsigned int cons;
149  /** Keyboard buffer sub-consumer counter
150  *
151  * This represents the index within the ANSI escape sequence
152  * corresponding to an iPXE key value.
153  */
154  unsigned int subcons;
155 };
156 
157 /**
158  * Calculate keyboard buffer fill level
159  *
160  * @v kbd USB keyboard
161  * @ret fill Keyboard buffer fill level
162  */
163 static inline __attribute__ (( always_inline )) unsigned int
164 usbkbd_fill ( struct usb_keyboard *kbd ) {
165  unsigned int fill = ( kbd->prod - kbd->cons );
166 
167  assert ( fill <= USBKBD_BUFSIZE );
168  return fill;
169 }
170 
171 #endif /* _USBKBD_H */
Right Alt key.
Definition: usbkbd.h:44
#define __attribute__(x)
Definition: compiler.h:10
uint8_t keycode[6]
Keycodes.
Definition: usbkbd.h:26
unsigned int prod
Keyboard buffer producer counter.
Definition: usbkbd.h:146
usb_keycode
USB keycodes.
Definition: usbkbd.h:62
struct list_head list
List of all USB keyboards.
Definition: usbkbd.h:121
unsigned int subcons
Keyboard buffer sub-consumer counter.
Definition: usbkbd.h:154
usb_keyboard_modifier
USB modifier keys.
Definition: usbkbd.h:30
unsigned int key[USBKBD_BUFSIZE]
Keyboard buffer.
Definition: usbkbd.h:144
static unsigned int usbkbd_fill(struct usb_keyboard *kbd)
Calculate keyboard buffer fill level.
Definition: usbkbd.h:164
A doubly-linked list entry (or list head)
Definition: list.h:18
A USB keyboard report.
Definition: usbkbd.h:20
struct usb_bus * bus
USB bus.
Definition: usbkbd.h:124
uint8_t leds
Keyboard LED state.
Definition: usbkbd.h:136
uint8_t modifiers
Modifier keys.
Definition: usbkbd.h:22
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned int keycode
Most recently pressed non-modifier key (if any)
Definition: usbkbd.h:131
struct usb_hid hid
USB human interface device.
Definition: usbkbd.h:126
uint8_t leds_changed
Keyboard LEDs changed.
Definition: usbkbd.h:138
A USB human interface device.
Definition: usbhid.h:51
unsigned char uint8_t
Definition: stdint.h:10
unsigned int holdoff
Autorepeat hold-off time (in number of completions reported)
Definition: usbkbd.h:133
#define USBKBD_BUFSIZE
Keyboard buffer size.
Definition: usbkbd.h:114
const char * name
Name.
Definition: usbkbd.h:119
A USB keyboard device.
Definition: usbkbd.h:117
struct usb_keyboard_report report
Most recent keyboard report.
Definition: usbkbd.h:129
unsigned int cons
Keyboard buffer consumer counter.
Definition: usbkbd.h:148
Right Ctrl key.
Definition: usbkbd.h:40
Universal Serial Bus (USB)
USB human interface devices (HID)
uint8_t reserved
Reserved.
Definition: usbkbd.h:24
uint8_t fill
Length pair.
Definition: deflate.h:12
Right Shift key.
Definition: usbkbd.h:42
Left GUI key.
Definition: usbkbd.h:38
Left Alt key.
Definition: usbkbd.h:36
usb_keyboard_led
USB keyboard LEDs.
Definition: usbkbd.h:81
Left Shift key.
Definition: usbkbd.h:34
A USB bus.
Definition: usb.h:949
Left Ctrl key.
Definition: usbkbd.h:32
Right GUI key.
Definition: usbkbd.h:46