Skip to content

Commit ac0d10b

Browse files
authored
HID: report descriptor for boot keyboard (#426)
1 parent a333956 commit ac0d10b

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

core/src/core/usb/hid.zig

+67
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ pub const LocalItem = enum(u4) {
263263
};
264264

265265
pub const UsageTable = struct {
266+
const desktop: [1]u8 = "\x01".*;
267+
const keyboard: [1]u8 = "\x07".*;
268+
const led: [1]u8 = "\x08".*;
266269
const fido: [2]u8 = "\xD0\xF1".*;
267270
const vendor: [2]u8 = "\x00\xFF".*;
268271
};
@@ -273,6 +276,10 @@ pub const FidoAllianceUsage = struct {
273276
const data_out: [1]u8 = "\x21".*;
274277
};
275278

279+
pub const DesktopUsage = struct {
280+
const keyboard: [1]u8 = "\x06".*;
281+
};
282+
276283
const HID_DATA: u8 = 0 << 0;
277284
const HID_CONSTANT: u8 = 1 << 0;
278285

@@ -373,6 +380,24 @@ pub fn hid_usage_page(comptime n: u2, usage: [n]u8) [n + 1]u8 {
373380
);
374381
}
375382

383+
pub fn hid_usage_min(comptime n: u2, data: [n]u8) [n + 1]u8 {
384+
return hid_report_item(
385+
n,
386+
@intFromEnum(ReportItemTypes.Local),
387+
@intFromEnum(LocalItem.UsageMin),
388+
data,
389+
);
390+
}
391+
392+
pub fn hid_usage_max(comptime n: u2, data: [n]u8) [n + 1]u8 {
393+
return hid_report_item(
394+
n,
395+
@intFromEnum(ReportItemTypes.Local),
396+
@intFromEnum(LocalItem.UsageMax),
397+
data,
398+
);
399+
}
400+
376401
pub fn hid_logical_min(comptime n: u2, data: [n]u8) [n + 1]u8 {
377402
return hid_report_item(
378403
n,
@@ -465,6 +490,48 @@ pub const ReportDescriptorGenericInOut = hid_usage_page(2, UsageTable.vendor) //
465490
// End
466491
++ hid_collection_end();
467492

493+
/// Common keyboard report format, conforming to the boot protocol.
494+
/// See Appendix B.1 of the USB HID specification:
495+
/// https://usb.org/sites/default/files/hid1_11.pdf
496+
pub const ReportDescriptorKeyboard = hid_usage_page(1, UsageTable.desktop) //
497+
++ hid_usage(1, DesktopUsage.keyboard) //
498+
++ hid_collection(.Application) //
499+
// Input: modifier key bitmap
500+
++ hid_usage_page(1, UsageTable.keyboard) //
501+
++ hid_usage_min(1, "\xe0".*) //
502+
++ hid_usage_max(1, "\xe7".*) //
503+
++ hid_logical_min(1, "\x00".*) //
504+
++ hid_logical_max(1, "\x01".*) //
505+
++ hid_report_count(1, "\x08".*) //
506+
++ hid_report_size(1, "\x01".*) //
507+
++ hid_input(HID_DATA | HID_VARIABLE | HID_ABSOLUTE) //
508+
// Reserved 8 bits
509+
++ hid_report_count(1, "\x01".*) //
510+
++ hid_report_size(1, "\x08".*) //
511+
++ hid_input(HID_CONSTANT) //
512+
// Output: indicator LEDs
513+
++ hid_usage_page(1, UsageTable.led) //
514+
++ hid_usage_min(1, "\x01".*) //
515+
++ hid_usage_max(1, "\x05".*) //
516+
++ hid_report_count(1, "\x05".*) //
517+
++ hid_report_size(1, "\x01".*) //
518+
++ hid_output(HID_DATA | HID_VARIABLE | HID_ABSOLUTE) //
519+
// Padding
520+
++ hid_report_count(1, "\x01".*) //
521+
++ hid_report_size(1, "\x03".*) //
522+
++ hid_output(HID_CONSTANT) //
523+
// Input: up to 6 pressed key codes
524+
++ hid_usage_page(1, UsageTable.keyboard) //
525+
++ hid_usage_min(1, "\x00".*) //
526+
++ hid_usage_max(2, "\xff\x00".*) //
527+
++ hid_logical_min(1, "\x00".*) //
528+
++ hid_logical_max(2, "\xff\x00".*) //
529+
++ hid_report_count(1, "\x06".*) //
530+
++ hid_report_size(1, "\x08".*) //
531+
++ hid_output(HID_DATA | HID_ARRAY | HID_ABSOLUTE) //
532+
// End
533+
++ hid_collection_end();
534+
468535
pub const HidClassDriver = struct {
469536

470537
device: ?types.UsbDevice = null,

0 commit comments

Comments
 (0)