From 45363b7c3107c41f224b44184972a821345f29b2 Mon Sep 17 00:00:00 2001 From: Swiftb0y <12380386+Swiftb0y@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:57:05 +0200 Subject: [PATCH] refactor: remove OS-specific hidapi code --- src/controllers/bulk/bulkcontroller.cpp | 38 +++++++------------------ src/controllers/bulk/bulkcontroller.h | 14 ++++----- src/controllers/bulk/bulksupported.h | 13 +++++---- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/controllers/bulk/bulkcontroller.cpp b/src/controllers/bulk/bulkcontroller.cpp index 24c9e1ee7b0..aacced6ecbb 100644 --- a/src/controllers/bulk/bulkcontroller.cpp +++ b/src/controllers/bulk/bulkcontroller.cpp @@ -132,12 +132,10 @@ bool BulkController::matchProductInfo(const ProductInfo& product) { return false; } -#if defined(__WINDOWS__) || defined(__APPLE__) value = product.interface_number.toInt(&ok, 16); - if (!ok || m_interfaceNumber != static_cast(value)) { + if (!ok || m_interfaceNumber != value) { return false; } -#endif // Match found return true; @@ -161,9 +159,7 @@ int BulkController::open() { } m_inEndpointAddr = pDevice->endpoints.in_epaddr; m_outEndpointAddr = pDevice->endpoints.out_epaddr; -#if defined(__WINDOWS__) || defined(__APPLE__) m_interfaceNumber = pDevice->endpoints.interface_number; -#endif // XXX: we should enumerate devices and match vendor, product, and serial if (m_phandle == nullptr) { @@ -175,31 +171,21 @@ int BulkController::open() { qCWarning(m_logBase) << "Unable to open USB Bulk device" << getName(); return -1; } - -#if defined(__WINDOWS__) || defined(__APPLE__) - if (m_interfaceNumber && libusb_kernel_driver_active(m_phandle, m_interfaceNumber) == 1) { - qCDebug(m_logBase) << "Found a driver active for" << getName(); - if (libusb_detach_kernel_driver(m_phandle, 0) == 0) - qCDebug(m_logBase) << "Kernel driver detached for" << getName(); - else { - qCWarning(m_logBase) << "Couldn't detach kernel driver for" << getName(); - libusb_close(m_phandle); - return -1; - } + if (libusb_set_auto_detach_kernel_driver(m_phandle, true) == LIBUSB_ERROR_NOT_SUPPORTED) { + qCDebug(m_logBase) << "unable to automatically detach kernel driver for" << getName(); } - if (m_interfaceNumber) { - int ret = libusb_claim_interface(m_phandle, m_interfaceNumber); - if (ret < 0) { + if (m_interfaceNumber.has_value()) { + int error = libusb_claim_interface(m_phandle, *m_interfaceNumber); + if (error < 0) { qCWarning(m_logBase) << "Cannot claim interface for" << getName() - << ":" << libusb_error_name(ret); + << ":" << libusb_error_name(error); libusb_close(m_phandle); return -1; } else { qCDebug(m_logBase) << "Claimed interface for" << getName(); } } -#endif setOpen(true); startEngine(); @@ -254,15 +240,13 @@ int BulkController::close() { stopEngine(); // Close device -#if defined(__WINDOWS__) || defined(__APPLE__) - if (m_interfaceNumber) { - int ret = libusb_release_interface(m_phandle, m_interfaceNumber); - if (ret < 0) { + if (m_interfaceNumber.has_value()) { + int error = libusb_release_interface(m_phandle, *m_interfaceNumber); + if (error < 0) { qCWarning(m_logBase) << "Cannot release interface for" << getName() - << ":" << libusb_error_name(ret); + << ":" << libusb_error_name(error); } } -#endif qCInfo(m_logBase) << " Closing device"; libusb_close(m_phandle); m_phandle = nullptr; diff --git a/src/controllers/bulk/bulkcontroller.h b/src/controllers/bulk/bulkcontroller.h index f7376c8ccf9..f2f1b0e9416 100644 --- a/src/controllers/bulk/bulkcontroller.h +++ b/src/controllers/bulk/bulkcontroller.h @@ -2,6 +2,7 @@ #include #include +#include #include "controllers/controller.h" #include "controllers/hid/legacyhidcontrollermapping.h" @@ -72,13 +73,12 @@ class BulkController : public Controller { // Local copies of things we need from desc - unsigned short m_vendorId; - unsigned short m_productId; - unsigned char m_inEndpointAddr; - unsigned char m_outEndpointAddr; -#if defined(__WINDOWS__) || defined(__APPLE__) - unsigned int m_interfaceNumber; -#endif + std::uint16_t m_vendorId; + std::uint16_t m_productId; + std::uint8_t m_inEndpointAddr; + std::uint8_t m_outEndpointAddr; + std::optional m_interfaceNumber; + QString m_manufacturer; QString m_product; diff --git a/src/controllers/bulk/bulksupported.h b/src/controllers/bulk/bulksupported.h index 49d3f05a636..a7bf6c916d6 100644 --- a/src/controllers/bulk/bulksupported.h +++ b/src/controllers/bulk/bulksupported.h @@ -1,6 +1,7 @@ #pragma once #include +#include struct bulk_device_id { std::uint16_t vendor_id; @@ -12,7 +13,9 @@ struct bulk_device_id { struct bulk_device_endpoints { std::uint8_t in_epaddr; std::uint8_t out_epaddr; - std::uint8_t interface_number; + // we may not know the interface, in which case we should not try to claim it. + // these devices are likely unusable on windows without claiming the correct interface. + std::optional interface_number; }; struct bulk_support_lookup { @@ -21,8 +24,8 @@ struct bulk_support_lookup { }; constexpr static bulk_support_lookup bulk_supported[] = { - {{0x06f8, 0xb105}, {0x82, 0x03, 0}}, // Hercules MP3e2 - {{0x06f8, 0xb107}, {0x83, 0x03, 0}}, // Hercules Mk4 - {{0x06f8, 0xb100}, {0x86, 0x06, 0}}, // Hercules Mk2 - {{0x06f8, 0xb120}, {0x82, 0x03, 0}}, // Hercules MP3 LE / Glow + {{0x06f8, 0xb105}, {0x82, 0x03, std::nullopt}}, // Hercules MP3e2 + {{0x06f8, 0xb107}, {0x83, 0x03, std::nullopt}}, // Hercules Mk4 + {{0x06f8, 0xb100}, {0x86, 0x06, std::nullopt}}, // Hercules Mk2 + {{0x06f8, 0xb120}, {0x82, 0x03, std::nullopt}}, // Hercules MP3 LE / Glow };