From: Alexander Ponyatykh Date: Fri, 15 Nov 2019 02:19:01 +0300 Subject: fix re_initLibG15() to properly reset internal state. Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/libusb/+bug/1357556 Forwarded: no Last-Update: 2015-05-17 =================================================================== --- libg15.c | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/libg15.c b/libg15.c index 31b2bd6..369ed40 100644 --- a/libg15.c +++ b/libg15.c @@ -283,19 +283,36 @@ static usb_dev_handle * findAndOpenG15() { return keyboard_device; } - -int re_initLibG15() +/* reset the keyboard, returning it to a known state */ +static int closeKeyboardDevice() { + int retval = G15_NO_ERROR; - usb_init(); + enospc_slowdown = shared_device = g15_keys_endpoint = g15_lcd_endpoint = 0; + found_devicetype = -1; - /** - * 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 */ + if (keyboard_device){ +#ifndef SUN_LIBUSB + retval = usb_release_interface (keyboard_device, 0); + usleep(50*1000); +#endif +#if 0 + retval = usb_reset(keyboard_device); + usleep(50*1000); +#endif + usb_close(keyboard_device); + keyboard_device=0; + return retval; + } + return -1; +} - if (!usb_find_devices()) - return G15_ERROR_OPENING_USB_DEVICE; +int re_initLibG15() +{ + closeKeyboardDevice(); + + usb_find_busses(); + usb_find_devices(); keyboard_device = findAndOpenG15(); if (!keyboard_device) @@ -327,28 +344,12 @@ int initLibG15() return retval; } -/* reset the keyboard, returning it to a known state */ int exitLibG15() { - int retval = G15_NO_ERROR; - if (keyboard_device){ -#ifndef SUN_LIBUSB - retval = usb_release_interface (keyboard_device, 0); - usleep(50*1000); -#endif -#if 0 - retval = usb_reset(keyboard_device); - usleep(50*1000); -#endif - usb_close(keyboard_device); - keyboard_device=0; - pthread_mutex_destroy(&libusb_mutex); - return retval; - } - return -1; + pthread_mutex_destroy(&libusb_mutex); + return closeKeyboardDevice(); } - static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char const *data) { /*