Remove IRQ-level malloc from USB setup

Fixes a hang found while debugging #614.  Do all memory allocations and
USB descriptor setup in main code prior to `tusb_init()`.  Avoids potential
deadlock in cases where the app is allocating while the USB port is being
set up.
parent c4623f42
...@@ -120,49 +120,49 @@ int __USBGetMouseReportID() { ...@@ -120,49 +120,49 @@ int __USBGetMouseReportID() {
return __USBInstallKeyboard ? 2 : 1; return __USBInstallKeyboard ? 2 : 1;
} }
static uint8_t *GetDescHIDReport(int *len) { static int __hid_report_len = 0;
static uint8_t *report = nullptr; static uint8_t *__hid_report = nullptr;
int report_len = 0;
if (report) { static uint8_t *GetDescHIDReport(int *len) {
free(report); if (len) {
report = nullptr; *len = __hid_report_len;
} }
return __hid_report;
}
static void __SetupDescHIDReport() {
if (__USBInstallKeyboard && __USBInstallMouse) { if (__USBInstallKeyboard && __USBInstallMouse) {
uint8_t desc_hid_report[] = { uint8_t desc_hid_report[] = {
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)), TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)),
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(2)) TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(2))
}; };
report_len = sizeof(desc_hid_report); __hid_report = (uint8_t *)malloc(sizeof(desc_hid_report));
report = (uint8_t *)malloc(report_len); if (__hid_report) {
if (report) { __hid_report_len = sizeof(desc_hid_report);
memcpy(report, desc_hid_report, report_len); memcpy(__hid_report, desc_hid_report, __hid_report_len);
} }
} else if (__USBInstallKeyboard && ! __USBInstallMouse) { } else if (__USBInstallKeyboard && ! __USBInstallMouse) {
uint8_t desc_hid_report[] = { uint8_t desc_hid_report[] = {
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)) TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1))
}; };
report_len = sizeof(desc_hid_report); __hid_report = (uint8_t *)malloc(sizeof(desc_hid_report));
report = (uint8_t *)malloc(report_len); if (__hid_report) {
if (report) { __hid_report_len = sizeof(desc_hid_report);
memcpy(report, desc_hid_report, report_len); memcpy(__hid_report, desc_hid_report, __hid_report_len);
} }
} else { // if (!__USBInstallKeyboard && __USBInstallMouse) { } else if (! __USBInstallKeyboard && __USBInstallMouse) {
uint8_t desc_hid_report[] = { uint8_t desc_hid_report[] = {
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1)) TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1))
}; };
report_len = sizeof(desc_hid_report); __hid_report = (uint8_t *)malloc(sizeof(desc_hid_report));
report = (uint8_t *)malloc(report_len); if (__hid_report) {
if (report) { __hid_report_len = sizeof(desc_hid_report);
memcpy(report, desc_hid_report, report_len); memcpy(__hid_report, desc_hid_report, __hid_report_len);
} }
} else {
__hid_report = nullptr;
__hid_report_len = 0;
} }
if (len) {
*len = report_len;
}
return report;
} }
// Invoked when received GET HID REPORT DESCRIPTOR // Invoked when received GET HID REPORT DESCRIPTOR
...@@ -173,11 +173,13 @@ uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) { ...@@ -173,11 +173,13 @@ uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) {
return GetDescHIDReport(nullptr); return GetDescHIDReport(nullptr);
} }
static uint8_t *usbd_desc_cfg = nullptr;
const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
(void)index; (void)index;
static uint8_t *usbd_desc_cfg = nullptr; return usbd_desc_cfg;
}
static void __SetupUSBDescriptor() {
if (!usbd_desc_cfg) { if (!usbd_desc_cfg) {
bool hasHID = __USBInstallKeyboard || __USBInstallMouse; bool hasHID = __USBInstallKeyboard || __USBInstallMouse;
...@@ -229,7 +231,6 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { ...@@ -229,7 +231,6 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
} }
} }
} }
return usbd_desc_cfg;
} }
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
...@@ -295,6 +296,9 @@ void __USBStart() { ...@@ -295,6 +296,9 @@ void __USBStart() {
return; return;
} }
__SetupDescHIDReport();
__SetupUSBDescriptor();
mutex_init(&__usb_mutex); mutex_init(&__usb_mutex);
tusb_init(); tusb_init();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment