Delay calling libusb_set_option() until libusb is initialized
G15daemon calls libg15Debug() before initLibG15(). This works well with the legacy library, but the new one requres a context to be initialized before calling any functions. This patch adds tracking of libusb initialization status and never calls libusb_set_option() when library is not initialized.
This commit is contained in:
1 parent
92e60dd1a4
commit
98449afc5d
1 file changed
+63
-47
@@ -30,7 +30,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
AC_C_CONST
|
||||
--- a/libg15.c
|
||||
+++ b/libg15.c
|
||||
@@ -24,14 +24,14 @@
|
||||
@@ -24,14 +24,15 @@
|
||||
#include "libg15.h"
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
@@ -45,20 +45,22 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
+static libusb_device_handle *keyboard_device = 0;
|
||||
static int libg15_debugging_enabled = 0;
|
||||
-static int enospc_slowdown = 0;
|
||||
+static int libusb_initialized = 0;
|
||||
|
||||
static int found_devicetype = -1;
|
||||
static int shared_device = 0;
|
||||
@@ -67,7 +67,8 @@
|
||||
@@ -67,7 +68,9 @@
|
||||
void libg15Debug(int option) {
|
||||
|
||||
libg15_debugging_enabled = option;
|
||||
- usb_set_debug(option);
|
||||
+ if (option)
|
||||
+ libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
|
||||
+ if (option && libusb_initialized) {
|
||||
+ libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_WARNING);
|
||||
+ }
|
||||
}
|
||||
|
||||
/* debugging wrapper */
|
||||
@@ -86,191 +87,205 @@
|
||||
@@ -86,191 +89,209 @@
|
||||
|
||||
/* return number of connected and supported devices */
|
||||
int g15NumberOfConnectedDevices() {
|
||||
@@ -102,23 +104,26 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
static int initLibUsb()
|
||||
{
|
||||
- usb_init();
|
||||
-
|
||||
+ int errcode = libusb_init(NULL);
|
||||
|
||||
- /**
|
||||
- * usb_find_busses and usb_find_devices both report the number of devices
|
||||
- * / busses added / removed since the last call. since this is the first
|
||||
- * call we have to return values != 0 or else we didnt find anything */
|
||||
+ int errcode = libusb_init(NULL);
|
||||
|
||||
- if (!usb_find_busses())
|
||||
- return G15_ERROR_OPENING_USB_DEVICE;
|
||||
-
|
||||
- if (!usb_find_devices())
|
||||
- if (!usb_find_busses())
|
||||
+ if (errcode != 0)
|
||||
+ {
|
||||
+ g15_log(stderr, G15_LOG_WARN, "Failed to initialize libusb-1.0: %s\n", libusb_strerror(errcode));
|
||||
return G15_ERROR_OPENING_USB_DEVICE;
|
||||
+ }
|
||||
|
||||
- if (!usb_find_devices())
|
||||
- return G15_ERROR_OPENING_USB_DEVICE;
|
||||
+ libusb_initialized = 1;
|
||||
+
|
||||
+ libg15Debug(libg15_debugging_enabled);
|
||||
|
||||
return G15_NO_ERROR;
|
||||
}
|
||||
|
||||
@@ -139,17 +144,17 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
+ int ret;
|
||||
+
|
||||
+ ssize_t cnt = libusb_get_device_list(NULL, &list);
|
||||
|
||||
- for (bus = usb_busses; bus; bus = bus->next)
|
||||
+
|
||||
+ if (cnt < 0)
|
||||
{
|
||||
- for (dev = bus->devices; dev; dev = dev->next)
|
||||
+ {
|
||||
+ g15_log(stderr, G15_LOG_WARN, "Unable to list USB devices: %s\n", libusb_strerror(cnt));
|
||||
+ return 0;
|
||||
+ }
|
||||
+
|
||||
|
||||
- for (bus = usb_busses; bus; bus = bus->next)
|
||||
+ for (ssize_t devno = 0; devno < cnt; devno++)
|
||||
+ {
|
||||
{
|
||||
- for (dev = bus->devices; dev; dev = dev->next)
|
||||
+ libusb_get_device_descriptor(list[devno], &desc);
|
||||
+ if (desc.idVendor == handled_device.vendorid && desc.idProduct == handled_device.productid)
|
||||
{
|
||||
@@ -185,14 +190,13 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
+ }
|
||||
+
|
||||
+ found_devicetype = device_index;
|
||||
+
|
||||
+ g15_log(stderr,G15_LOG_INFO,"Found %s, trying to open it\n",handled_device.name);
|
||||
|
||||
- usleep(50*1000);
|
||||
+ g15_log(stderr,G15_LOG_INFO,"Found %s, trying to open it\n",handled_device.name);
|
||||
+
|
||||
+ ret = libusb_open(dev, &devh);
|
||||
+ libusb_free_device_list(list, 1);
|
||||
|
||||
- g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n",dev->descriptor.bNumConfigurations);
|
||||
+
|
||||
+ if (ret || !devh)
|
||||
+ {
|
||||
+ g15_log(stderr,G15_LOG_WARN, "Error, could not open the keyboard: %s\n", libusb_strerror(ret));
|
||||
@@ -202,21 +206,17 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
+ usleep(50*1000);
|
||||
+
|
||||
+ g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n", desc.bNumConfigurations);
|
||||
|
||||
- /* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
|
||||
- if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
|
||||
- shared_device = 1;
|
||||
+
|
||||
+ /* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
|
||||
+ if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
|
||||
+ shared_device = 1;
|
||||
|
||||
- for (j = 0; j<dev->descriptor.bNumConfigurations;j++){
|
||||
- struct usb_config_descriptor *cfg = &dev->config[j];
|
||||
+
|
||||
+ for (int j = 0; j < desc.bNumConfigurations; j++)
|
||||
+ {
|
||||
+ struct libusb_config_descriptor *cfg;
|
||||
+ if (libusb_get_config_descriptor(dev, j, &cfg)) continue;
|
||||
+
|
||||
|
||||
- g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n",dev->descriptor.bNumConfigurations);
|
||||
+ for (int i = 0; i < cfg->bNumInterfaces; i++)
|
||||
+ {
|
||||
+ if (g15DeviceCapabilities() & G15_DEVICE_G510)
|
||||
@@ -224,15 +224,17 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
+ if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
|
||||
+ }
|
||||
|
||||
- for (i=0;i<cfg->bNumInterfaces; i++){
|
||||
- if (g15DeviceCapabilities()&G15_DEVICE_G510){
|
||||
- if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
|
||||
- /* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
|
||||
- if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
|
||||
- shared_device = 1;
|
||||
+ const struct libusb_interface *ifp = &cfg->interface[i];
|
||||
+ /* if endpoints are already known, finish up */
|
||||
+ if (g15_keys_endpoint && g15_lcd_endpoint)
|
||||
+ break;
|
||||
+ g15_log(stderr, G15_LOG_INFO, "Device has %i Alternate Settings\n", ifp->num_altsetting);
|
||||
+
|
||||
|
||||
- for (j = 0; j<dev->descriptor.bNumConfigurations;j++){
|
||||
- struct usb_config_descriptor *cfg = &dev->config[j];
|
||||
+ for (int k=0; k < ifp->num_altsetting; k++)
|
||||
+ {
|
||||
+ const struct libusb_interface_descriptor *as = &ifp->altsetting[k];
|
||||
@@ -241,7 +243,10 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
+ {
|
||||
+ g15_log(stderr, G15_LOG_INFO, "Interface %i has %i Endpoints\n", i, as->bNumEndpoints);
|
||||
+ usleep(50*1000);
|
||||
+
|
||||
|
||||
- for (i=0;i<cfg->bNumInterfaces; i++){
|
||||
- if (g15DeviceCapabilities()&G15_DEVICE_G510){
|
||||
- if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
|
||||
+ if (libusb_kernel_driver_active(devh, i) == 1)
|
||||
+ {
|
||||
+ g15_log(stderr, G15_LOG_INFO, "Trying to detach driver currently attached.\n");
|
||||
@@ -409,7 +414,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
int i;
|
||||
for (i=0; g15_devices[i].name !=NULL ;i++){
|
||||
g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name);
|
||||
@@ -288,19 +303,13 @@
|
||||
@@ -288,19 +309,13 @@
|
||||
{
|
||||
int retval = G15_NO_ERROR;
|
||||
|
||||
@@ -434,7 +439,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
keyboard_device=0;
|
||||
return retval;
|
||||
}
|
||||
@@ -311,9 +320,6 @@
|
||||
@@ -311,9 +326,6 @@
|
||||
{
|
||||
closeKeyboardDevice();
|
||||
|
||||
@@ -444,7 +449,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
keyboard_device = findAndOpenG15();
|
||||
if (!keyboard_device)
|
||||
return G15_ERROR_OPENING_USB_DEVICE;
|
||||
@@ -330,10 +336,6 @@
|
||||
@@ -330,10 +342,6 @@
|
||||
|
||||
g15_log(stderr,G15_LOG_INFO,"%s\n",PACKAGE_STRING);
|
||||
|
||||
@@ -455,7 +460,18 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
g15NumberOfConnectedDevices();
|
||||
|
||||
keyboard_device = findAndOpenG15();
|
||||
@@ -428,36 +430,6 @@
|
||||
@@ -347,7 +355,9 @@
|
||||
int exitLibG15()
|
||||
{
|
||||
pthread_mutex_destroy(&libusb_mutex);
|
||||
- return closeKeyboardDevice();
|
||||
+ int ret = closeKeyboardDevice();
|
||||
+ libusb_exit(NULL);
|
||||
+ libusb_initialized = 0;
|
||||
}
|
||||
|
||||
static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char const *data)
|
||||
@@ -428,36 +438,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,7 +508,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
int writePixmapToLCD(unsigned char const *data)
|
||||
{
|
||||
int ret = 0;
|
||||
@@ -474,43 +446,27 @@
|
||||
@@ -474,43 +454,27 @@
|
||||
|
||||
/* the keyboard needs this magic byte */
|
||||
lcd_buffer[0] = 0x03;
|
||||
@@ -554,7 +570,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -535,7 +491,9 @@
|
||||
@@ -535,7 +499,9 @@
|
||||
usb_data[3] = 18;
|
||||
}
|
||||
pthread_mutex_lock(&libusb_mutex);
|
||||
@@ -565,7 +581,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
pthread_mutex_unlock(&libusb_mutex);
|
||||
return retval;
|
||||
}
|
||||
@@ -553,7 +511,9 @@
|
||||
@@ -553,7 +519,9 @@
|
||||
return G15_ERROR_UNSUPPORTED;
|
||||
|
||||
pthread_mutex_lock(&libusb_mutex);
|
||||
@@ -576,7 +592,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
pthread_mutex_unlock(&libusb_mutex);
|
||||
return retval;
|
||||
}
|
||||
@@ -578,7 +538,9 @@
|
||||
@@ -578,7 +546,9 @@
|
||||
usb_data[2] = 0x00;
|
||||
}
|
||||
pthread_mutex_lock(&libusb_mutex);
|
||||
@@ -587,7 +603,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
pthread_mutex_unlock(&libusb_mutex);
|
||||
return retval;
|
||||
}
|
||||
@@ -604,7 +566,9 @@
|
||||
@@ -604,7 +574,9 @@
|
||||
usb_data[2] = 0x0;
|
||||
}
|
||||
pthread_mutex_lock(&libusb_mutex);
|
||||
@@ -598,7 +614,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
pthread_mutex_unlock(&libusb_mutex);
|
||||
return retval;
|
||||
}
|
||||
@@ -619,7 +583,9 @@
|
||||
@@ -619,7 +591,9 @@
|
||||
usb_data[3] = b;
|
||||
|
||||
pthread_mutex_lock(&libusb_mutex);
|
||||
@@ -609,7 +625,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
pthread_mutex_unlock(&libusb_mutex);
|
||||
return retval;
|
||||
}
|
||||
@@ -1033,18 +999,30 @@
|
||||
@@ -1033,18 +1007,30 @@
|
||||
int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout)
|
||||
{
|
||||
unsigned char buffer[G15_KEY_READ_LENGTH];
|
||||
@@ -647,7 +663,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
return G15_ERROR_TRY_AGAIN;
|
||||
}
|
||||
|
||||
@@ -1054,7 +1032,7 @@
|
||||
@@ -1054,7 +1040,7 @@
|
||||
return G15_NO_ERROR;
|
||||
}
|
||||
|
||||
@@ -656,7 +672,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
|
||||
case 4:
|
||||
processKeyEvent4Byte(pressed_keys, buffer);
|
||||
return G15_NO_ERROR;
|
||||
@@ -1065,6 +1043,6 @@
|
||||
@@ -1065,6 +1051,6 @@
|
||||
processKeyEvent9Byte(pressed_keys, buffer);
|
||||
return G15_NO_ERROR;
|
||||
default:
|
||||
|
||||
Reference in new issue
Block a user