diff --git a/libqhid/qhiddevice_hidapi.cpp b/libqhid/qhiddevice_hidapi.cpp index eaca20f..76bebca 100644 --- a/libqhid/qhiddevice_hidapi.cpp +++ b/libqhid/qhiddevice_hidapi.cpp @@ -241,18 +241,24 @@ static void resetDevice(int vendorId, int deviceId) } #endif +static int hidapiUsed = 0; + QHIDDevicePrivate::QHIDDevicePrivate(QHIDDevice *q_ptr, int vendorId, int deviceId, int usagePage, int usage) : device(nullptr) , vendorId(vendorId) , deviceId(deviceId) , q_ptr(q_ptr) { - if (hid_init() != 0) + // Make sure we call hid_init() only once. + if (hidapiUsed == 0 && hid_init() != 0) { qWarning() << "hid_init failed, error" << errno; return; } + // Increment hidapi library usage counter. + ++hidapiUsed; + int interfaceNumber = -1; #ifdef WITH_LIBUSB_1_0 hidapiMissingFeatures( @@ -292,12 +298,18 @@ QHIDDevicePrivate::~QHIDDevicePrivate() device = nullptr; #ifdef WITH_LIBUSB_1_0 - // Until reset the keyboard interface will be ignored by the kernel. + // Until reset the keyboard interface will be ignored by the kernel + // since the hidapi library does detach the kernel driver and does + // not re-attach it back. resetDevice(vendorId, deviceId); #endif } - hid_exit(); + // Only the last one should call hid_exit(), + if (--hidapiUsed == 0) + { + hid_exit(); + } } bool QHIDDevicePrivate::isValid() const diff --git a/src/ms735.cpp b/src/ms735.cpp index 90d5746..178aed3 100644 --- a/src/ms735.cpp +++ b/src/ms735.cpp @@ -57,7 +57,6 @@ MS735::MS735(QObject *parent) , eventDevice(new QHIDDevice(VENDOR, PRODUCT, EVENT_USAGE_PAGE, EVENT_USAGE, this)) , monitor(new QHIDMonitor(VENDOR, PRODUCT, this)) , timerId(0) - { connect(monitor, SIGNAL(deviceArrival(QString)), this, SLOT(deviceArrival(QString))); connect(monitor, SIGNAL(deviceRemove()), this, SLOT(deviceRemove())); diff --git a/src/ms735.h b/src/ms735.h index 55196ec..29ad2a2 100644 --- a/src/ms735.h +++ b/src/ms735.h @@ -49,6 +49,7 @@ class MS735 : public QObject CmdControl = 0x0C, CmdButtons = 0x0D, CmdMacro = 0x0F, + CmdVersion = 0x10, // 0x91 => 01 01, this may be 1.1 version. CmdFlagGet = 0x80, CmdPing = CmdBlink | CmdFlagGet, CmdGetProfile = CmdProfile | CmdFlagGet,