59
59
static grub_usb_device_t usbdev;
61
/* Valid values for bmRequestType. See HID definition version 1.11 section
63
#define USB_HID_HOST_TO_DEVICE 0x21
64
#define USB_HID_DEVICE_TO_HOST 0xA1
66
/* Valid values for bRequest. See HID definition version 1.11 section 7.2. */
67
#define USB_HID_GET_REPORT 0x01
68
#define USB_HID_GET_IDLE 0x02
69
#define USB_HID_GET_PROTOCOL 0x03
70
#define USB_HID_SET_REPORT 0x09
71
#define USB_HID_SET_IDLE 0x0A
72
#define USB_HID_SET_PROTOCOL 0x0B
75
62
grub_usb_hid (void)
103
90
grub_usb_iterate (usb_iterate);
105
92
/* Place the device in boot mode. */
106
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_PROTOCOL,
93
grub_usb_control_msg (usbdev, 0x21, 0x0B, 0, 0, 0, 0);
109
95
/* Reports every time an event occurs and not more often than that. */
110
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_IDLE,
96
grub_usb_control_msg (usbdev, 0x21, 0x0A, 0<<8, 0, 0, 0);
115
grub_usb_keyboard_getreport (grub_usb_device_t dev, grub_uint8_t *report)
100
grub_usb_keyboard_getreport (grub_usb_device_t dev, unsigned char *report)
117
return grub_usb_control_msg (dev, USB_HID_DEVICE_TO_HOST, USB_HID_GET_REPORT,
118
0, 0, 8, (char *) report);
102
return grub_usb_control_msg (dev, (1 << 7) | (1 << 5) | 1, 0x01, 0, 0,
124
109
grub_usb_keyboard_checkkey (void)
126
grub_uint8_t data[8];
111
unsigned char data[8];
129
grub_uint64_t currtime;
133
currtime = grub_get_time_ms ();
117
for (i = 0; i < 50; i++)
136
119
/* Get_Report. */
137
120
err = grub_usb_keyboard_getreport (usbdev, data);
139
/* Implement a timeout. */
140
if (grub_get_time_ms () > currtime + timeout)
122
if (! err && data[2])
143
while (err || !data[2]);
145
126
if (err || !data[2])
166
147
/* Wait until the key is released. */
167
148
while (!err && data[2])
169
err = grub_usb_control_msg (usbdev, USB_HID_DEVICE_TO_HOST,
170
USB_HID_GET_REPORT, 0, 0,
150
err = grub_usb_control_msg (usbdev, (1 << 7) | (1 << 5) | 1, 0x01, 0, 0,
171
151
sizeof (data), (char *) data);
172
152
grub_dprintf ("usb_keyboard",
173
153
"report2: 0x%02x 0x%02x 0x%02x 0x%02x"
258
grub_usb_keyboard_getkeystatus (void)
260
grub_uint8_t data[8];
263
grub_uint64_t currtime;
266
/* Set idle time to the minimum offered by the spec (4 milliseconds) so
267
that we can find out the current state. */
268
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_IDLE,
271
currtime = grub_get_time_ms ();
275
err = grub_usb_keyboard_getreport (usbdev, data);
277
/* Implement a timeout. */
278
if (grub_get_time_ms () > currtime + timeout)
281
while (err || !data[0]);
283
/* Go back to reporting every time an event occurs and not more often than
285
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_IDLE,
288
/* We allowed a while for modifiers to show up in the report, but it is
289
not an error if they never did. */
293
grub_dprintf ("usb_keyboard",
294
"report: 0x%02x 0x%02x 0x%02x 0x%02x"
295
" 0x%02x 0x%02x 0x%02x 0x%02x\n",
296
data[0], data[1], data[2], data[3],
297
data[4], data[5], data[6], data[7]);
299
/* Check Shift, Control, and Alt status. */
300
if (data[0] & 0x02 || data[0] & 0x20)
301
mods |= GRUB_TERM_STATUS_SHIFT;
302
if (data[0] & 0x01 || data[0] & 0x10)
303
mods |= GRUB_TERM_STATUS_CTRL;
304
if (data[0] & 0x04 || data[0] & 0x40)
305
mods |= GRUB_TERM_STATUS_ALT;
307
grub_errno = GRUB_ERR_NONE;
312
237
static struct grub_term_input grub_usb_keyboard_term =
314
239
.name = "usb_keyboard",
315
240
.checkkey = grub_usb_keyboard_checkkey,
316
241
.getkey = grub_usb_keyboard_getkey,
317
.getkeystatus = grub_usb_keyboard_getkeystatus,