circuitpython/shared-module/usb_hid/__init__.c

183 lines
7.5 KiB
C
Raw Normal View History

2018-07-31 05:42:04 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 hathach for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2021-04-21 23:25:36 -04:00
static const uint8_t usb_hid_descriptor_template[] = {
0x09, // 0 bLength
0x21, // 1 bDescriptorType (HID)
0x11, 0x01, // 2 bcdHID 1.11
0x00, // 3 bCountryCode
0x01, // 4 bNumDescriptors
0x22, // 5 bDescriptorType[0] (HID)
0xFF, 0xFF, // 6,7 wDescriptorLength[0] [SET AT RUNTIME: lo, hi]
2021-04-23 00:18:05 -04:00
#define HID_DESCRIPTOR_LENGTH_INDEX (6)
2021-04-21 23:25:36 -04:00
0x07, // 8 bLength
0x05, // 9 bDescriptorType (Endpoint)
0xFF, // 10 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x80 | endpoint]
2021-04-23 00:18:05 -04:00
#define HID_IN_ENDPOINT_INDEX (10)
2021-04-21 23:25:36 -04:00
0x03, // 11 bmAttributes (Interrupt)
0x40, 0x00, // 12,13 wMaxPacketSize 64
0x08, // 14 bInterval 8 (unit depends on device speed)
0x07, // 15 bLength
0x05, // 16 bDescriptorType (Endpoint)
0xFF, // 17 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
2021-04-23 00:18:05 -04:00
#define HID_OUT_ENDPOINT_INDEX (17)
2021-04-21 23:25:36 -04:00
0x03, // 18 bmAttributes (Interrupt)
0x40, 0x00, // 19,20 wMaxPacketSize 64
0x08, // 21 bInterval 8 (unit depends on device speed)
};
2021-04-25 23:17:41 -04:00
// Sequence of devices to configure.
static mp_obj_t hid_devices_seq;
2021-04-21 23:25:36 -04:00
// Is the HID device enabled?
bool usb_hid_enabled;
2021-04-25 10:23:59 -04:00
supervisor_allocation *hid_report_descriptor_allocation;
supervisor_allocation *hid_devices_allocation;
2021-04-21 23:25:36 -04:00
2021-04-23 21:44:13 -04:00
// This is the interface descriptor, not the report descriptor.
2021-04-21 23:25:36 -04:00
size_t usb_hid_descriptor_length(void) {
return sizeof(usb_hid_descriptor);
}
2021-04-23 21:44:13 -04:00
static const char[] usb_hid_interface_name = USB_INTERFACE_NAME " HID";
2021-04-21 23:25:36 -04:00
2021-04-25 10:23:59 -04:00
// This is the interface descriptor, not the report descriptor.
2021-04-21 23:25:36 -04:00
size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interface, uint8_t *current_endpoint, uint8_t* current_interface_string, uint16_t report_descriptor_length) {
memcpy(descriptor_buf, usb_hid_descriptor_template, sizeof(usb_hid_descriptor_template));
descriptor_buf[HID_DESCRIPTOR_LENGTH_INDEX] = report_descriptor_length & 0xFF;
descriptor_buf[HID_DESCRIPTOR_LENGTH_INDEX + 1] = (report_descriptor_length >> 8);
descriptor_buf[HID_IN_ENDPOINT_INDEX] = USB_HID_EP_NUM_IN ? USB_HID_EP_NUM_IN : *current_endpoint;
descriptor_buf[HID_OUT_ENDPOINT_INDEX] = 0x80 | (USB_HID_EP_NUM_OUT ? USB_HID_EP_NUM_OUT : *current_endpoint);
(*current_endpoint)++:
return sizeof(usb_hid_descriptor_template);
}
2021-04-23 00:18:05 -04:00
2021-04-25 23:17:41 -04:00
static mp_obj_t default_hid_devices[] = {
MP_OBJ_FROM_PTR(usb_hid_device_keyboard_obj),
MP_OBJ_FROM_PTR(usb_hid_device_mouse_obj),
};
// Set the default list of devices that will be included. Called before boot.py runs, in the boot.py VM.
void common_hal_usb_hid_configure_usb_defaults(void) {
common_hal_usb_hid_configure_usb(mp_obj_new_tuple(sizeof(default_hid_devices), default_hid_devices));
}
bool common_hal_usb_hid_configure_usb(mp_obj_t devices_seq) {
2021-04-23 00:18:05 -04:00
// We can't change the devices once we're connected.
if (tud_connected()) {
2021-04-25 23:17:41 -04:00
return false;
2021-04-23 00:18:05 -04:00
}
2021-04-25 23:17:41 -04:00
// Remember the devices for use in usb_hid_post_boot_py.
hid_devices_seq = devices_seq;
return true;
}
2021-04-23 00:18:05 -04:00
2021-04-25 23:17:41 -04:00
// Build the combined HID report descriptor and save the chosen devices.
// Both are saved in supervisor allocations.
void usb_hid_post_boot_py(void) {
2021-04-23 21:44:13 -04:00
size_t total_report_descriptors_length = 0;
// Build a combined report descriptor
2021-04-25 23:17:41 -04:00
mp_int_t len = mp_obj_get_int(mp_obj_len(hid_devices_seq));
2021-04-23 21:44:13 -04:00
// First get the total size.
for (size_t i = 0; i < len; i++) {
2021-04-25 10:23:59 -04:00
mp_obj_t item = mp_obj_subscr(devices_seq, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
2021-04-23 21:44:13 -04:00
if (!MP_OBJ_IS_TYPE(item, &usb_hid_device_type)) {
return USB_CONFIG_NON_DEVICE; for (size_t i = 0; i < len; i++) {
mp_obj_t item = (devices, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
if (!MP_OBJ_IS_TYPE(item, &usb_hid_device_type)) {
return USB_CONFIG_NON_DEVICE;
}
total_report_descriptors_length += device->report_descriptor_length;
}
}
total_report_descriptors_length += device->report_descriptor_length;
}
if (len == 1) {
// Don't need space for a report id if there's only one device.
total_report_descriptors_length -= 2;
}
// Allocate storage that persists across VMs to build the combined descriptor
// and to remember the device details.
2021-04-25 10:23:59 -04:00
hid_report_descriptor_allocation =
allocate_memory(total_report_descriptors_length, false /*highaddress*/, true /*movable*/);
2021-04-23 21:44:13 -04:00
2021-04-25 10:23:59 -04:00
hid_devices_allocation = allocate_memory(sizeof(usb_hid_device_obj_t) * len);
usb_hid_device_obj_t hid_devices[] = (usb_hid_device_obj_t[]) hid_devices_allocation->ptr;
2021-04-23 21:44:13 -04:00
2021-04-25 10:23:59 -04:00
uint8_t *descriptor_start = (uint8_t *) hid_report_descriptor_allocation->ptr;
2021-04-23 21:44:13 -04:00
for (size_t i = 0; i < len; i++) {
2021-04-25 23:17:41 -04:00
usb_hid_device_obj_t *device = MP_OBJ_TO_PTR(hid_devices_seq, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
2021-04-23 21:44:13 -04:00
// Copy the report descriptor for this device.
if (len == 1) {
// Theres only one device, so it shouldn't have a report ID.
// Copy the descriptor, but splice out the report id indicator and value (2 bytes).
memcpy(descriptor_start, device->descriptor, device->report_id_index - 1);
descriptor_start += device->report_id_index - 1;
memcpy(descriptor_start, device->descriptor + device->report_id_index + 1,
device->report_descriptor_length - device->report_id_index - 1);
} else {
// Copy the whole descriptor and fill in the report id.
memcpy(descriptor_start, device->descriptor, device->descriptor_len);
descriptor_start[device->report_id_index] = i + 1;
descriptor_start += device->descriptor_len;
}
// Copy the device data and discard any descriptor-bytes object pointer.
2021-04-25 10:23:59 -04:00
memcpy(&hid_devices[i], device, sizeof(usb_hid_device_obj_t));
hid_devices[i].descriptor_obj = mp_const_none;
2021-04-23 21:44:13 -04:00
}
2021-04-25 23:17:41 -04:00
// No longer keeping the Python object of devices to configure.
hid_devices_seq = MP_OBJ_NULL;
2021-04-23 21:44:13 -04:00
}
2021-04-23 00:18:05 -04:00
void usb_hid_gc_collect(void) {
// Once tud_mounted() is true, we're done with the constructed descriptors.
if (tud_mounted()) {
2021-04-25 10:23:59 -04:00
free_memory(hid_report_descriptor_allocation);
free_memory(usb_hid_devices_allocation);
2021-04-23 00:18:05 -04:00
} else {
2021-04-25 23:17:41 -04:00
gc_collect_ptr(hid_devices_seq);
2021-04-25 10:23:59 -04:00
gc_collect_ptr(hid_report_descriptor_allocation->ptr);
2021-04-25 23:17:41 -04:00
gc_collect_ptr(usb_hid_devices_allocation_ptr);
2021-04-23 00:18:05 -04:00
}
}