Skip to content

Commit 0d92558

Browse files
authored
Merge pull request adafruit#4800 from dhalbert/too-many-hid-devices
Handle too many HID devices properly
2 parents de89fc7 + c6eb028 commit 0d92558

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

shared-module/usb_hid/__init__.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static const uint8_t usb_hid_descriptor_template[] = {
7979
static supervisor_allocation *hid_report_descriptor_allocation;
8080
static usb_hid_device_obj_t hid_devices[MAX_HID_DEVICES];
8181
// If 0, USB HID is disabled.
82-
static mp_int_t hid_devices_num;
82+
static mp_int_t num_hid_devices;
8383

8484
// This tuple is store in usb_hid.devices.
8585
static mp_obj_tuple_t *hid_devices_tuple;
@@ -97,7 +97,7 @@ static mp_obj_tuple_t default_hid_devices_tuple = {
9797
};
9898

9999
bool usb_hid_enabled(void) {
100-
return hid_devices_num > 0;
100+
return num_hid_devices > 0;
101101
}
102102

103103
void usb_hid_set_defaults(void) {
@@ -140,13 +140,13 @@ size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *desc
140140
// Make up a fresh tuple containing the device objects saved in the static
141141
// devices table. Save the tuple in usb_hid.devices.
142142
static void usb_hid_set_devices_from_hid_devices(void) {
143-
mp_obj_t tuple_items[hid_devices_num];
144-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
143+
mp_obj_t tuple_items[num_hid_devices];
144+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
145145
tuple_items[i] = &hid_devices[i];
146146
}
147147

148148
// Remember tuple for gc purposes.
149-
hid_devices_tuple = mp_obj_new_tuple(hid_devices_num, tuple_items);
149+
hid_devices_tuple = mp_obj_new_tuple(num_hid_devices, tuple_items);
150150
usb_hid_set_devices(hid_devices_tuple);
151151
}
152152

@@ -160,13 +160,15 @@ bool common_hal_usb_hid_enable(const mp_obj_t devices) {
160160
return false;
161161
}
162162

163-
hid_devices_num = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(devices));
164-
if (hid_devices_num > MAX_HID_DEVICES) {
163+
const mp_int_t num_devices = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(devices));
164+
if (num_devices > MAX_HID_DEVICES) {
165165
mp_raise_ValueError_varg(translate("No more than %d HID devices allowed"), MAX_HID_DEVICES);
166166
}
167167

168+
num_hid_devices = num_devices;
169+
168170
// Remember the devices in static storage so they live across VMs.
169-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
171+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
170172
// devices has already been validated to contain only usb_hid_device_obj_t objects.
171173
usb_hid_device_obj_t *device =
172174
MP_OBJ_TO_PTR(mp_obj_subscr(devices, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
@@ -183,20 +185,20 @@ void usb_hid_setup_devices(void) {
183185
usb_hid_set_devices_from_hid_devices();
184186

185187
// Create report buffers on the heap.
186-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
188+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
187189
usb_hid_device_create_report_buffers(&hid_devices[i]);
188190
}
189191
}
190192

191193
// Total length of the report descriptor, with all configured devices.
192194
size_t usb_hid_report_descriptor_length(void) {
193195
size_t total_hid_report_descriptor_length = 0;
194-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
196+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
195197
total_hid_report_descriptor_length += hid_devices[i].report_descriptor_length;
196198
}
197199

198200
// Don't need space for a report id if there's only one device.
199-
if (hid_devices_num == 1) {
201+
if (num_hid_devices == 1) {
200202
total_hid_report_descriptor_length -= 2;
201203
}
202204
return total_hid_report_descriptor_length;
@@ -210,10 +212,10 @@ void usb_hid_build_report_descriptor(uint8_t *report_descriptor_space, size_t re
210212

211213
uint8_t *report_descriptor_start = report_descriptor_space;
212214

213-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
215+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
214216
usb_hid_device_obj_t *device = &hid_devices[i];
215217
// Copy the report descriptor for this device.
216-
if (hid_devices_num == 1) {
218+
if (num_hid_devices == 1) {
217219
// There's only one device, so it shouldn't have a report ID.
218220
// Copy the descriptor, but splice out the report id indicator and value (2 bytes).
219221
memcpy(report_descriptor_start, device->report_descriptor, device->report_id_index - 1);
@@ -258,13 +260,13 @@ void usb_hid_gc_collect(void) {
258260
gc_collect_ptr(hid_devices_tuple);
259261

260262
// Mark any heap pointers in the static device list as in use.
261-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
263+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
262264
gc_collect_ptr(&hid_devices[i]);
263265
}
264266
}
265267

266268
usb_hid_device_obj_t *usb_hid_get_device_with_report_id(uint8_t report_id) {
267-
for (uint8_t i = 0; i < hid_devices_num; i++) {
269+
for (uint8_t i = 0; i < num_hid_devices; i++) {
268270
usb_hid_device_obj_t *device = &hid_devices[i];
269271
if (device->report_id == report_id) {
270272
return &hid_devices[i];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy