-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusb.c
82 lines (71 loc) · 2.81 KB
/
usb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright 2021 paulguy <[email protected]>
*
* This file is part of gmmkctl.
*
* gmmkctl is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gmmkctl is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gmmkctl. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "usb.h"
int open_libusb_devices(libusb_context *ctx,
libusb_device_handle **devs,
unsigned int maxDevs,
unsigned short int idVendor,
unsigned short int idProduct,
int interface) {
enum libusb_error e;
libusb_device **devList;
struct libusb_device_descriptor desc;
int i;
unsigned int devNum;
if((e = libusb_get_device_list(ctx, &devList)) < 0) {
fprintf(stderr, "Couldn't get device list: %s\n", libusb_strerror(e));
return(0);
}
devNum = 0;
for(i = 0; devList[i] != NULL; i++) {
if((e = libusb_get_device_descriptor(devList[i], &desc)) < 0) {
fprintf(stderr, "Couldn't get device descriptor: %s\n", libusb_strerror(e));
continue;
}
if(desc.idVendor == idVendor && desc.idProduct == idProduct) {
if((e = libusb_open(devList[i], &(devs[devNum]))) < 0) {
fprintf(stderr, "Couldn't open device: %s\n", libusb_strerror(e));
continue;
}
if((e = libusb_detach_kernel_driver(devs[devNum], interface)) < 0) {
if(e == LIBUSB_ERROR_NOT_FOUND) {
/* don't do anything, there's no driver to detach. */
} else if(e == LIBUSB_ERROR_NOT_SUPPORTED) {
fprintf(stderr, "WARNING: Couldn't try to detach kernel driver!\n");
} else {
fprintf(stderr, "Couldn't detach driver: %s\n", libusb_strerror(e));
libusb_close(devs[devNum]);
continue;
}
}
if((e = libusb_claim_interface(devs[devNum], interface)) < 0) {
fprintf(stderr, "Couldn't claim interface: %s\n", libusb_strerror(e));
libusb_close(devs[devNum]);
continue;
}
devNum++;
if(devNum == maxDevs) {
break;
}
}
}
libusb_free_device_list(devList, 1);
return(devNum);
}