-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
131 lines (114 loc) · 3.03 KB
/
driver.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* This file is generated with usbsnoop2libusb.pl from a usbsnoop log file. */
/* Latest version of the script should be at http://iki.fi/lindi/git/usbsnoop2libusb.git */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <ctype.h>
#include <usb.h>
#if 0
#include <linux/usbdevice_fs.h>
#define LIBUSB_AUGMENT
#include "libusb_augment.h"
#endif
struct usb_dev_handle *devh;
void release_usb_device(int dummy) {
int ret;
ret = usb_release_interface(devh, 0);
if (!ret)
printf("failed to release interface: %d\n", ret);
usb_close(devh);
if (!ret)
printf("failed to close interface: %d\n", ret);
exit(1);
}
void list_devices() {
struct usb_bus *bus;
for (bus = usb_get_busses(); bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next)
printf("0x%04x 0x%04x\n",
dev->descriptor.idVendor,
dev->descriptor.idProduct);
}
}
struct usb_device *find_device(int vendor, int product) {
struct usb_bus *bus;
for (bus = usb_get_busses(); bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == vendor
&& dev->descriptor.idProduct == product)
return dev;
}
}
return NULL;
}
void print_bytes(char *bytes, int len) {
int i;
if (len > 0) {
for (i=0; i<len; i++) {
printf("%02x ", (int)((unsigned char)bytes[i]));
}
printf("\"");
for (i=0; i<len; i++) {
printf("%c", isprint(bytes[i]) ? bytes[i] : '.');
}
printf("\"");
}
}
int main(int argc, char **argv) {
int ret, vendor, product;
struct usb_device *dev;
char buf[65535], *endptr;
#if 0
usb_urb *isourb;
struct timeval isotv;
char isobuf[393216];
#endif
usb_init();
usb_set_debug(255);
usb_find_busses();
usb_find_devices();
if (argc!=3) {
printf("usage: %s vendorID productID\n", argv[0]);
printf("ID numbers of currently attached devices:\n");
list_devices();
exit(1);
}
vendor = strtol(argv[1], &endptr, 16);
if (*endptr != '\0') {
printf("invalid vendor id\n");
exit(1);
}
product = strtol(argv[2], &endptr, 16);
if (*endptr != '\0') {
printf("invalid product id\n");
exit(1);
}
dev = find_device(vendor, product);
assert(dev);
devh = usb_open(dev);
assert(devh);
signal(SIGTERM, release_usb_device);
ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
printf("usb_get_driver_np returned %d\n", ret);
if (ret == 0) {
printf("interface 0 already claimed by driver \"%s\", attempting to detach it\n", buf);
ret = usb_detach_kernel_driver_np(devh, 0);
printf("usb_detach_kernel_driver_np returned %d\n", ret);
}
ret = usb_claim_interface(devh, 0);
if (ret != 0) {
printf("claim failed with error %d\n", ret);
exit(1);
}
ret = usb_set_altinterface(devh, 0);
assert(ret >= 0);
ret = usb_release_interface(devh, 0);
assert(ret == 0);
ret = usb_close(devh);
assert(ret == 0);
return 0;
}