Skip to content

Commit aea3c4d

Browse files
committed
wip
1 parent 556a126 commit aea3c4d

4 files changed

Lines changed: 42 additions & 35 deletions

File tree

shared-bindings/usb_hid/__init__.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,4 @@ typedef enum {
3838
USB_CONFIG_NON_DEVICE = 2,
3939
} usb_hid_configure_status;
4040

41-
usb_hid_configure_status common_hal_usb_hid_configure_usb(mp_obj_t devices);
42-
43-
#endif // SHARED_BINDINGS_USB_HID_H
41+
usb_hid_configure_status common_hal_usb_hid_configure_usb(mp_obj_t devices_seqf // SHARED_BINDINGS_USB_HID_H

shared-module/usb_hid/__init__.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ static const uint8_t usb_hid_descriptor_template[] = {
5353

5454
// Is the HID device enabled?
5555
bool usb_hid_enabled;
56-
supervisor_allocation *combined_hid_report_descriptor_allocation;
57-
supervisor_allocation *devices_allocation;
56+
supervisor_allocation *hid_report_descriptor_allocation;
57+
supervisor_allocation *hid_devices_allocation;
5858

5959

6060
// This is the interface descriptor, not the report descriptor.
@@ -64,7 +64,7 @@ size_t usb_hid_descriptor_length(void) {
6464

6565
static const char[] usb_hid_interface_name = USB_INTERFACE_NAME " HID";
6666

67-
// This is the interface descriptor, nto the report descriptor.
67+
// This is the interface descriptor, not the report descriptor.
6868
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) {
6969
memcpy(descriptor_buf, usb_hid_descriptor_template, sizeof(usb_hid_descriptor_template));
7070

@@ -78,27 +78,27 @@ size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interfac
7878
return sizeof(usb_hid_descriptor_template);
7979
}
8080

81-
usb_hid_configure_status common_hal_usb_hid_configure_usb(mp_obj_t devices) {
81+
usb_hid_configure_status common_hal_usb_hid_configure_usb(mp_obj_t devices_seq) {
8282
// We can't change the devices once we're connected.
8383
if (tud_connected()) {
8484
return USB_CONFIG_TOO_LATE;
8585
}
8686

8787
// Assume no devices to start.
8888
usb_hid_enabled = false;
89-
if (devices == mp_const_none) {
89+
if (devices_seq == mp_const_none) {
9090
return USB_CONFIG_OK;
9191
}
9292

9393
size_t total_report_descriptors_length = 0;
9494

9595
// Build a combined report descriptor
9696

97-
mp_int_t len = mp_obj_get_int(mp_obj_len(devices));
97+
mp_int_t len = mp_obj_get_int(mp_obj_len(devices_seq));
9898

9999
// First get the total size.
100100
for (size_t i = 0; i < len; i++) {
101-
mp_obj_t item = mp_obj_subscr(devices, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
101+
mp_obj_t item = mp_obj_subscr(devices_seq, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
102102
if (!MP_OBJ_IS_TYPE(item, &usb_hid_device_type)) {
103103
return USB_CONFIG_NON_DEVICE; for (size_t i = 0; i < len; i++) {
104104
mp_obj_t item = (devices, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
@@ -119,16 +119,16 @@ usb_hid_configure_status common_hal_usb_hid_configure_usb(mp_obj_t devices) {
119119
// Allocate storage that persists across VMs to build the combined descriptor
120120
// and to remember the device details.
121121

122-
// allocate_memory(length, highaddress=false, movable=true)
123-
combined_hid_report_descriptor_allocation = allocate_memory(total_report_descriptors_length, false, true);
122+
hid_report_descriptor_allocation =
123+
allocate_memory(total_report_descriptors_length, false /*highaddress*/, true /*movable*/);
124124

125-
devices_allocation = allocate_memory(sizeof(usb_hid_device_obj_t) * len);
126-
usb_hid_device_obj_t devices[] = (devices[]) device_details_allocation->ptr;
125+
hid_devices_allocation = allocate_memory(sizeof(usb_hid_device_obj_t) * len);
126+
usb_hid_device_obj_t hid_devices[] = (usb_hid_device_obj_t[]) hid_devices_allocation->ptr;
127127

128-
uint8_t *descriptor_start = combined_hid_report_descriptor_allocation->ptr;
128+
uint8_t *descriptor_start = (uint8_t *) hid_report_descriptor_allocation->ptr;
129129

130130
for (size_t i = 0; i < len; i++) {
131-
usb_hid_device_obj_t *device = MP_OBJ_TO_PTR(devices, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
131+
usb_hid_device_obj_t *device = MP_OBJ_TO_PTR(devices_seq, mp_obj_new_small_int(i), MP_OBJ_SENTINEL);
132132

133133
// Copy the report descriptor for this device.
134134
if (len == 1) {
@@ -146,18 +146,19 @@ usb_hid_configure_status common_hal_usb_hid_configure_usb(mp_obj_t devices) {
146146
}
147147

148148
// Copy the device data and discard any descriptor-bytes object pointer.
149-
memcpy(&devices[i], device, sizeof(usb_hid_device_obj_t));
150-
devices[i].descriptor_obj = mp_const_none;
149+
memcpy(&hid_devices[i], device, sizeof(usb_hid_device_obj_t));
150+
hid_devices[i].descriptor_obj = mp_const_none;
151151
}
152152

153153
}
154154

155155
void usb_hid_gc_collect(void) {
156156
// Once tud_mounted() is true, we're done with the constructed descriptors.
157157
if (tud_mounted()) {
158-
// GC will pick up the inaccessible blocks.
159-
usb_hid_devices_to_configure = NULL;
158+
free_memory(hid_report_descriptor_allocation);
159+
free_memory(usb_hid_devices_allocation);
160160
} else {
161-
gc_collect_ptr(usb_hid_devices);
161+
gc_collect_ptr(hid_report_descriptor_allocation->ptr);
162+
gc_collect_ptr(usb_hid_devices_allocation);
162163
}
163164
}

supervisor/shared/memory.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
enum {
3636
CIRCUITPY_SUPERVISOR_IMMOVABLE_ALLOC_COUNT =
37-
// stack + heap
37+
// stack + heap
3838
2
3939
#if INTERNAL_FLASH_FILESYSTEM == 0
4040
+ 1
@@ -45,8 +45,13 @@ enum {
4545
,
4646
CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT =
4747
0
48+
#if CIRCUITPY_USB
49+
+ 1 // device_descriptor_allocation
50+
+ 1 // config_descriptor_allocation
51+
#endif
4852
#if CIRCUITPY_USB_HID
49-
+ 2
53+
+ 1 // hid_report_descriptor_allocation
54+
+ 1 // hid_devices_allocation
5055
#endif
5156
#if CIRCUITPY_DISPLAYIO
5257
#if CIRCUITPY_TERMINALIO

supervisor/shared/usb/usb_desc.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@
4646

4747
#include "genhdr/autogen_usb_descriptor.h"
4848

49-
static uint8_t *device_descriptor;
50-
static uint8_t *config_descriptor;
51-
static uint8_t *hid_report_descriptor;
49+
supervisor_allocation *device_descriptor_allocation;
50+
supervisor_allocation *config_descriptor_allocation;
5251

5352
// Table for collecting interface strings (interface names) as descriptor is built.
5453
#define MAX_INTERFACE_STRINGS 16
@@ -114,14 +113,17 @@ void usb_desc_init(void) {
114113
// Null-terminate the string.
115114
serial_number_hex_string[sizeof(serial_number_hex_string)] = '\0';
116115

117-
// Set to zero when allocated; we depend on that.
116+
// Memory is cleared to zero when allocated; we depend on that.
118117
collected_interface_strings = m_malloc(MAX_INTERFACE_STRINGS + 1, false);
119118
current_interface_string = 1;
120119
}
121120

122121

123122
void usb_build_device_descriptor(uint16_t vid, uint16_t pid, uint8_t *current_interface_string) {
124-
device_descriptor = m_malloc(sizeof(device_descriptor_template), false);
123+
device_descriptor_allocation =
124+
allocate_memory(sizeof(device_descriptor_template), false /*highaddress*/, true /*movable*/);
125+
uint8_t *device_descriptor = (uint8_t *) device_descriptor_allocation->ptr;
126+
125127
memcpy(device_descriptor, device_descriptor_template, sizeof(device_descriptor_template));
126128

127129
device_descriptor[DEVICE_VID_LO_INDEX] = vid & 0xFF;
@@ -176,7 +178,10 @@ void usb_build_configuration_descriptor(uint16_t total_length, uint8_t num_inter
176178
#endif
177179

178180
// Now we now how big the configuration descriptor will be.
179-
configuration_descriptor = m_malloc(total_descriptor_length, false);
181+
182+
configuration_descriptor_allocation =
183+
allocate_memory(sizeof(configuration_descriptor_template), false /*highaddress*/, true /*movable*/);
184+
uint8_t *configuration_descriptor = (uint8_t *) device_descriptor_allocation->ptr;
180185

181186
// Copy the top-level template, and fix up its length.
182187
memcpy(config_descriptor, configuration_descriptor_template, sizeof(configuration_descriptor_template));
@@ -258,13 +263,11 @@ void usb_desc_gc_collect(void) {
258263
// Once tud_mounted() is true, we're done with the constructed descriptors.
259264
if (tud_mounted()) {
260265
// GC will pick up the inaccessible blocks.
261-
device_descriptor = NULL;
262-
configuration_descriptor = NULL;
263-
hid_report_descriptors = NULL;
266+
free_memory(device_descriptor_allocation);
267+
free_memory(configuration_descriptor_allocation);
264268
} else {
265-
gc_collect_ptr(device_descriptor);
266-
gc_collect_ptr(configuration_descriptor);
267-
gc_collect_ptr(hid_report_descriptors); // Collects children too.
269+
gc_collect_ptr(device_descriptor_allocation->ptr);
270+
gc_collect_ptr(configuration_descriptor_allocation->ptr);
268271
}
269272
}
270273

0 commit comments

Comments
 (0)