From 1a6a2a7b75dec3b20311bc6d89923fde3222ba1c Mon Sep 17 00:00:00 2001 From: zvecr Date: Mon, 18 Feb 2019 17:03:44 +0000 Subject: Relocate usb_detach to utils (#5121) * Relocate usb_detach to utils * Relocate usb_detach to utils - fix up files after move --- util/usb_detach/Makefile | 18 ++++++++++++++++++ util/usb_detach/readme.md | 14 ++++++++++++++ util/usb_detach/usb_detach.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 util/usb_detach/Makefile create mode 100644 util/usb_detach/readme.md create mode 100644 util/usb_detach/usb_detach.c (limited to 'util') diff --git a/util/usb_detach/Makefile b/util/usb_detach/Makefile new file mode 100644 index 000000000..533c1928f --- /dev/null +++ b/util/usb_detach/Makefile @@ -0,0 +1,18 @@ +# the compiler: gcc for C program, define as g++ for C++ +CC = gcc + +# compiler flags: +# -g adds debugging information to the executable file +# -Wall turns on most, but not all, compiler warnings +CFLAGS = -g -Wall + +# the build target executable: +TARGET = usb_detach + +all: $(TARGET) + +$(TARGET): $(TARGET).c + $(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c + +clean: + $(RM) $(TARGET) diff --git a/util/usb_detach/readme.md b/util/usb_detach/readme.md new file mode 100644 index 000000000..ac42944ee --- /dev/null +++ b/util/usb_detach/readme.md @@ -0,0 +1,14 @@ +# usb_detach + +When trying to flash on Linux, you may encounter a "Resource Unavailable" error. This means that Linux's HID driver has taken exclusive control of the keyboard, and the program script can't flash it. +This program can force Linux to give up a device, so that the programming script can reset it. + +## To compile: +```bash +make clean && make +``` + +## To run: +1. Use `lsusb` to discover the Bus and Device numbers for your keyboard. +2. Run the program: `sudo ./usb_detach /dev/bus/usb//`. +3. Build and program the firmware as normal. diff --git a/util/usb_detach/usb_detach.c b/util/usb_detach/usb_detach.c new file mode 100644 index 000000000..786ab5e67 --- /dev/null +++ b/util/usb_detach/usb_detach.c @@ -0,0 +1,34 @@ +/* Found at https://www.linuxquestions.org/questions/linux-hardware-18/how-to-unclaim-usb-device-558138/#post3406986 */ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char**argv) +{ + struct usbdevfs_ioctl command; + int ret; + int fd; + int i; + if (argc>1) { + fd = open(argv[1],O_RDWR); + if (fd<1){ + perror("unable to open file"); + return 1; + } + for (i=0;i<255;i++){ // hack: should fetch how many interface there is. + command.ifno = i; + command.ioctl_code = USBDEVFS_DISCONNECT; + command.data = NULL; + ret = ioctl(fd, USBDEVFS_IOCTL, &command); + if(ret!=-1) + printf("un claimed interface %d %d\n",i,ret); + } + } else { + printf ("usage: %s /dev/bus/usb/BUS/DEVICE\n",argv[0]); + printf("Release all interfaces of this usb device for usage in virtualisation\n"); + } +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b