Boot protocol and feature reports: compiles; needs to be tested
This commit is contained in:
parent
2025d0c060
commit
24c6295ed4
|
@ -1 +1 @@
|
|||
Subproject commit e493a4c30e1b2f7c54a376327388b9e12262cbbf
|
||||
Subproject commit 48ae2309fd9cea600ff960b61a029892be1fdc1b
|
|
@ -60,7 +60,7 @@ STATIC mp_obj_t usb_hid_disable(void) {
|
|||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_0(usb_hid_disable_obj, usb_hid_disable);
|
||||
|
||||
//| def enable(devices: Optional[Sequence[Device]]) -> None:
|
||||
//| def enable(devices: Optional[Sequence[Device]], boot_device: int = 0) -> None:
|
||||
//| """Specify which USB HID devices that will be available.
|
||||
//| Can be called in ``boot.py``, before USB is connected.
|
||||
//|
|
||||
|
@ -68,15 +68,49 @@ MP_DEFINE_CONST_FUN_OBJ_0(usb_hid_disable_obj, usb_hid_disable);
|
|||
//| If `devices` is empty, HID is disabled. The order of the ``Devices``
|
||||
//| may matter to the host. For instance, for MacOS, put the mouse device
|
||||
//| before any Gamepad or Digitizer HID device or else it will not work.
|
||||
//| :param int boot_device: If non-zero, inform the host that support for a
|
||||
//| a boot HID device is available.
|
||||
//| If ``boot_device=1``, a boot keyboard is available.
|
||||
//| If ``boot_device=2``, a boot mouse is available. No other values are allowed.
|
||||
//| See below.
|
||||
//|
|
||||
//| If you enable too many devices at once, you will run out of USB endpoints.
|
||||
//| The number of available endpoints varies by microcontroller.
|
||||
//| CircuitPython will go into safe mode after running ``boot.py`` to inform you if
|
||||
//| not enough endpoints are available.
|
||||
//|
|
||||
//| **Boot Devices**
|
||||
//|
|
||||
//| Boot devices implement a fixed, predefined report descriptor, defined in
|
||||
//| https://www.usb.org/sites/default/files/hid1_12.pdf, Appendix B. A USB host
|
||||
//| can request to use the boot device if the USB device says it is available.
|
||||
//| Usually only a BIOS or other kind of boot-time, limited-functionality
|
||||
//| host needs boot keyboard support.
|
||||
//|
|
||||
//| For example, to make a boot keyboard available, you can use this code::
|
||||
//|
|
||||
//| usb_hid.enable((Device.KEYBOARD), boot_device=1)
|
||||
//|
|
||||
//| If the host requests the boot keyboard, the report descriptor provided by `Device.KEYBOARD`
|
||||
//| will be ignored, and the predefined report descriptor will be used.
|
||||
//| But if the host does not request the boot keyboard,
|
||||
//| the descriptor provided by `Device.KEYBOARD` will be used.
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t usb_hid_enable(mp_obj_t devices) {
|
||||
STATIC mp_obj_t usb_hid_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
usb_hid_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
||||
|
||||
enum { ARG_devices, ARG_boot_device };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_devices, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_boot_device, MP_ARG_OBJ, {.u_int = 0} },
|
||||
};
|
||||
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
mp_obj_t devices = args[ARG_devices].u_obj;
|
||||
const mp_int_t len = mp_obj_get_int(mp_obj_len(devices));
|
||||
for (mp_int_t i = 0; i < len; i++) {
|
||||
mp_obj_t item = mp_obj_subscr(devices, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL);
|
||||
|
@ -85,13 +119,37 @@ STATIC mp_obj_t usb_hid_enable(mp_obj_t devices) {
|
|||
}
|
||||
}
|
||||
|
||||
if (!common_hal_usb_hid_enable(devices)) {
|
||||
uint8_t boot_device =
|
||||
(uint8_t)mp_arg_validate_int_range(args[ARG_boot_device].u_int, 0, 2, MP_QSTR_boot_device);
|
||||
|
||||
if (!common_hal_usb_hid_enable(devices, boot_device)) {
|
||||
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(usb_hid_enable_obj, usb_hid_enable);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(usb_hid_enable_obj, 2, usb_hid_enable);
|
||||
|
||||
//| def get_boot_device() -> int:
|
||||
//| """
|
||||
//| :return: the boot device requested by the host, if any.
|
||||
//| Returns 0 if the host did not request a boot device, or if `usb_hid.enable()`
|
||||
//| was called with `boot_device=0`, the default, which disables boot device support.
|
||||
//| If the host did request aboot device,
|
||||
//| returns the value of ``boot_device`` set in `usb_hid.enable()`:
|
||||
//| ``1`` for a boot keyboard, or ``2`` for boot mouse.
|
||||
//| Your device driver should check and act on this value if the keyboard or mouse
|
||||
//| device you specified provides reports that differ from the standard boot keyboard or mouse.
|
||||
//| However, the standard devices provided by CircuitPython, `Device.KEYBOARD` and `Device.MOUSE`,
|
||||
//| describe reports that match the boot device reports, so you don't need to check this
|
||||
//| if you are using those devices.
|
||||
//| :rtype int:
|
||||
//| """
|
||||
//|
|
||||
STATIC mp_obj_t usb_hid_get_boot_device(void) {
|
||||
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_hid_get_boot_device());
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_0(usb_hid_get_boot_device_obj, usb_hid_get_boot_device);
|
||||
|
||||
// usb_hid.devices is set once the usb devices are determined, after boot.py runs.
|
||||
STATIC mp_map_elem_t usb_hid_module_globals_table[] = {
|
||||
|
@ -100,6 +158,7 @@ STATIC mp_map_elem_t usb_hid_module_globals_table[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_devices), mp_const_none },
|
||||
{ MP_ROM_QSTR(MP_QSTR_disable), MP_OBJ_FROM_PTR(&usb_hid_disable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_enable), MP_OBJ_FROM_PTR(&usb_hid_enable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_boot_device), MP_OBJ_FROM_PTR(&usb_hid_get_boot_device_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_MUTABLE_DICT(usb_hid_module_globals, usb_hid_module_globals_table);
|
||||
|
|
|
@ -36,6 +36,7 @@ extern mp_obj_tuple_t common_hal_usb_hid_devices;
|
|||
void usb_hid_set_devices(mp_obj_t devices);
|
||||
|
||||
bool common_hal_usb_hid_disable(void);
|
||||
bool common_hal_usb_hid_enable(const mp_obj_t devices_seq);
|
||||
bool common_hal_usb_hid_enable(const mp_obj_t devices_seq, uint8_t boot_device);
|
||||
uint8_t common_hal_usb_hid_get_boot_device(void);
|
||||
|
||||
#endif // SHARED_BINDINGS_USB_HID_H
|
||||
|
|
|
@ -244,7 +244,7 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t *
|
|||
}
|
||||
|
||||
mp_obj_t common_hal_usb_hid_device_get_last_received_report(usb_hid_device_obj_t *self, uint8_t report_id) {
|
||||
// report_id has already been validated for this deveice.
|
||||
// report_id has already been validated for this device.
|
||||
size_t id_idx = get_report_id_idx(self, report_id);
|
||||
return mp_obj_new_bytes(self->out_report_buffers[id_idx], self->out_report_lengths[id_idx]);
|
||||
}
|
||||
|
@ -266,11 +266,11 @@ void usb_hid_device_create_report_buffers(usb_hid_device_obj_t *self) {
|
|||
}
|
||||
|
||||
|
||||
// Callbacks invoked when we received Get_Report request through control endpoint
|
||||
// Callback invoked when we receive Get_Report request through control endpoint
|
||||
uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen) {
|
||||
(void)itf;
|
||||
// only support Input Report
|
||||
if (report_type != HID_REPORT_TYPE_INPUT) {
|
||||
// Support Input Report and Feature Report
|
||||
if (report_type != HID_REPORT_TYPE_INPUT && report_type != HID_REPORT_TYPE_FEATURE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -289,14 +289,14 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Callbacks invoked when we received Set_Report request through control endpoint
|
||||
// Callback invoked when we receive Set_Report request through control endpoint
|
||||
void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize) {
|
||||
(void)itf;
|
||||
if (report_type == HID_REPORT_TYPE_INVALID) {
|
||||
report_id = buffer[0];
|
||||
buffer++;
|
||||
bufsize--;
|
||||
} else if (report_type != HID_REPORT_TYPE_OUTPUT) {
|
||||
} else if (report_type != HID_REPORT_TYPE_OUTPUT && report_type != HID_REPORT_TYPE_FEATURE) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,9 @@ static const uint8_t usb_hid_descriptor_template[] = {
|
|||
0x02, // 4 bNumEndpoints 2
|
||||
0x03, // 5 bInterfaceClass: HID
|
||||
0x00, // 6 bInterfaceSubClass: NOBOOT
|
||||
#define HID_DESCRIPTOR_SUBCLASS_INDEX (6)
|
||||
0x00, // 7 bInterfaceProtocol: NONE
|
||||
#define HID_DESCRIPTOR_INTERFACE_PROTOCOL_INDEX (7)
|
||||
0xFF, // 8 iInterface (String Index) [SET AT RUNTIME]
|
||||
#define HID_DESCRIPTOR_INTERFACE_STRING_INDEX (8)
|
||||
|
||||
|
@ -81,6 +83,14 @@ static usb_hid_device_obj_t hid_devices[MAX_HID_DEVICES];
|
|||
// If 0, USB HID is disabled.
|
||||
static mp_int_t num_hid_devices;
|
||||
|
||||
// Which boot device is available 0: no boot devices, 1: boot keyboard, 2: boot mouse.
|
||||
// This value is set by usb_hid.enable(), and used to build the HID interface descriptor.
|
||||
// The value is remembered here from boot.py to code.py.
|
||||
static uint8_t hid_boot_device;
|
||||
|
||||
// Whether a boot device was requested by a SET_PROTOCOL request from the host.
|
||||
static bool hid_boot_device_requested;
|
||||
|
||||
// This tuple is store in usb_hid.devices.
|
||||
static mp_obj_tuple_t *hid_devices_tuple;
|
||||
|
||||
|
@ -100,9 +110,19 @@ bool usb_hid_enabled(void) {
|
|||
return num_hid_devices > 0;
|
||||
}
|
||||
|
||||
uint8_t usb_hid_boot_device(void) {
|
||||
return hid_boot_device;
|
||||
}
|
||||
|
||||
// Returns 1 or 2 if host requested a boot device and boot protocol was enabled in the interface descriptor.
|
||||
uint8_t common_hal_usb_hid_get_boot_device(void) {
|
||||
return hid_boot_device_requested ? hid_boot_device : 0;
|
||||
}
|
||||
|
||||
void usb_hid_set_defaults(void) {
|
||||
hid_boot_device = 0;
|
||||
common_hal_usb_hid_enable(
|
||||
CIRCUITPY_USB_HID_ENABLED_DEFAULT ? &default_hid_devices_tuple : mp_const_empty_tuple);
|
||||
CIRCUITPY_USB_HID_ENABLED_DEFAULT ? &default_hid_devices_tuple : mp_const_empty_tuple, 0);
|
||||
}
|
||||
|
||||
// This is the interface descriptor, not the report descriptor.
|
||||
|
@ -113,12 +133,17 @@ size_t usb_hid_descriptor_length(void) {
|
|||
static const char usb_hid_interface_name[] = USB_INTERFACE_NAME " HID";
|
||||
|
||||
// This is the interface descriptor, not the report descriptor.
|
||||
size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string, uint16_t report_descriptor_length) {
|
||||
size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string, uint16_t report_descriptor_length, uint8_t boot_device) {
|
||||
memcpy(descriptor_buf, usb_hid_descriptor_template, sizeof(usb_hid_descriptor_template));
|
||||
|
||||
descriptor_buf[HID_DESCRIPTOR_INTERFACE_INDEX] = descriptor_counts->current_interface;
|
||||
descriptor_counts->current_interface++;
|
||||
|
||||
if (boot_device > 0) {
|
||||
descriptor_buf[HID_DESCRIPTOR_SUBCLASS_INDEX] = 1; // BOOT protocol (device) available.
|
||||
descriptor_buf[HID_DESCRIPTOR_INTERFACE_PROTOCOL_INDEX] = boot_device; // 1: keyboard, 2: mouse
|
||||
}
|
||||
|
||||
usb_add_interface_string(*current_interface_string, usb_hid_interface_name);
|
||||
descriptor_buf[HID_DESCRIPTOR_INTERFACE_STRING_INDEX] = *current_interface_string;
|
||||
(*current_interface_string)++;
|
||||
|
@ -151,10 +176,10 @@ static void usb_hid_set_devices_from_hid_devices(void) {
|
|||
}
|
||||
|
||||
bool common_hal_usb_hid_disable(void) {
|
||||
return common_hal_usb_hid_enable(mp_const_empty_tuple);
|
||||
return common_hal_usb_hid_enable(mp_const_empty_tuple, 0);
|
||||
}
|
||||
|
||||
bool common_hal_usb_hid_enable(const mp_obj_t devices) {
|
||||
bool common_hal_usb_hid_enable(const mp_obj_t devices, uint8_t boot_device) {
|
||||
// We can't change the devices once we're connected.
|
||||
if (tud_connected()) {
|
||||
return false;
|
||||
|
@ -167,6 +192,8 @@ bool common_hal_usb_hid_enable(const mp_obj_t devices) {
|
|||
|
||||
num_hid_devices = num_devices;
|
||||
|
||||
hid_boot_device = boot_device;
|
||||
|
||||
// Remember the devices in static storage so they live across VMs.
|
||||
for (mp_int_t i = 0; i < num_hid_devices; i++) {
|
||||
// devices has already been validated to contain only usb_hid_device_obj_t objects.
|
||||
|
@ -182,6 +209,7 @@ bool common_hal_usb_hid_enable(const mp_obj_t devices) {
|
|||
|
||||
// Called when HID devices are ready to be used, when code.py or the REPL starts running.
|
||||
void usb_hid_setup_devices(void) {
|
||||
hid_boot_device_requested = false;
|
||||
usb_hid_set_devices_from_hid_devices();
|
||||
|
||||
// Create report buffers on the heap.
|
||||
|
@ -272,9 +300,15 @@ bool usb_hid_get_device_with_report_id(uint8_t report_id, usb_hid_device_obj_t *
|
|||
return false;
|
||||
}
|
||||
|
||||
// Invoked when GET HID REPORT DESCRIPTOR is received.
|
||||
// Application return pointer to descriptor
|
||||
// Callback invoked when we receive a GET HID REPORT DESCRIPTOR
|
||||
// Application returns pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
|
||||
return (uint8_t *)hid_report_descriptor_allocation->ptr;
|
||||
}
|
||||
|
||||
// Callback invoked when we receive a SET_PROTOCOL request.
|
||||
// Protocol is either HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
|
||||
void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol) {
|
||||
hid_boot_device_requested = (protocol == HID_PROTOCOL_BOOT);
|
||||
}
|
||||
|
|
|
@ -33,9 +33,10 @@
|
|||
extern usb_hid_device_obj_t usb_hid_devices[];
|
||||
|
||||
bool usb_hid_enabled(void);
|
||||
uint8_t usb_hid_boot_device(void);
|
||||
void usb_hid_set_defaults(void);
|
||||
|
||||
size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string, uint16_t report_descriptor_length);
|
||||
size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string, uint16_t report_descriptor_length, uint8_t boot_device);
|
||||
size_t usb_hid_descriptor_length(void);
|
||||
size_t usb_hid_report_descriptor_length(void);
|
||||
|
||||
|
|
|
@ -223,7 +223,7 @@ static void usb_build_configuration_descriptor(void) {
|
|||
if (usb_hid_enabled()) {
|
||||
descriptor_buf_remaining += usb_hid_add_descriptor(
|
||||
descriptor_buf_remaining, &descriptor_counts, ¤t_interface_string,
|
||||
usb_hid_report_descriptor_length());
|
||||
usb_hid_report_descriptor_length(), usb_hid_boot_device());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue