Extract HID report size for EDBG debug tools via HID endpoint descriptor.

Removed hidapi bodge (where we were mimicking the hid_device_ struct, to
obtain the report size via `(hid_device_*)->input_ep_max_packet_size`).
This commit is contained in:
Nav
2023-01-14 03:03:10 +00:00
parent e3e37ace4b
commit dfaac9e30f
7 changed files with 84 additions and 82 deletions

View File

@@ -30,14 +30,19 @@ namespace Bloom::DebugToolDrivers
void EdbgDevice::init() {
UsbDevice::init();
auto cmsisHidInterface = Usb::HidInterface(this->cmsisHidInterfaceNumber, this->vendorId, this->productId);
this->detachKernelDriverFromInterface(cmsisHidInterface.interfaceNumber);
this->detachKernelDriverFromInterface(this->cmsisHidInterfaceNumber);
if (this->configurationIndex.has_value()) {
this->setConfiguration(this->configurationIndex.value());
}
auto cmsisHidInterface = Usb::HidInterface(
this->cmsisHidInterfaceNumber,
this->getCmsisHidReportSize(),
this->vendorId,
this->productId
);
cmsisHidInterface.init();
this->edbgInterface = std::make_unique<EdbgInterface>(std::move(cmsisHidInterface));
@@ -125,4 +130,33 @@ namespace Bloom::DebugToolDrivers
this->sessionStarted = false;
}
std::uint16_t EdbgDevice::getCmsisHidReportSize() {
const auto activeConfigDescriptor = this->getConfigDescriptor();
for (auto interfaceIndex = 0; interfaceIndex < activeConfigDescriptor->bNumInterfaces; ++interfaceIndex) {
const auto* interfaceDescriptor = (activeConfigDescriptor->interface + interfaceIndex)->altsetting;
if (interfaceDescriptor->bInterfaceNumber != this->cmsisHidInterfaceNumber) {
continue;
}
for (auto endpointIndex = 0; endpointIndex < activeConfigDescriptor->bNumInterfaces; ++endpointIndex) {
const auto* endpointDescriptor = (interfaceDescriptor->endpoint + endpointIndex);
if ((endpointDescriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) != LIBUSB_ENDPOINT_IN) {
// Not an IN endpoint
continue;
}
return endpointDescriptor->wMaxPacketSize;
}
}
throw DeviceInitializationFailure(
"Failed to obtain CMSIS-DAP HID report size via endpoint descriptor - could not find IN endpoint for "
"selected configuration value (" + std::to_string(activeConfigDescriptor->bConfigurationValue)
+ ") and interface number (" + std::to_string(this->cmsisHidInterfaceNumber) + ")"
);
}
}