-
Notifications
You must be signed in to change notification settings - Fork 68
/
usbswitcher.c
87 lines (71 loc) · 1.86 KB
/
usbswitcher.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
/*
* usbswitcher.c, by @joystick and @rpaleari
*
* Search for an attached USB Samsung device and switch it to a specific USB
* configuration (#2). Requires libusb [1] (use version 0.1).
*
* References:
* [1] http://libusb.org/
*/
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "usb.h"
/* This VID/PID pair is OK for all the Samsung phones we tested. However, you
may need to tune them for other device models. */
#define SAMSUNG_VENDOR_ID 0x04e8
#define SAMSUNG_PRODUCT_ID 0x6860
void _log(char tag, const char *fmt, ...) {
char msg[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
fprintf(stderr, "[%c] %s\n", tag, msg);
}
#define error(fmt, ...) _log('!', fmt, ## __VA_ARGS__)
#define info(fmt, ...) _log('*', fmt, ## __VA_ARGS__)
struct usb_device *find_device() {
struct usb_bus *bus;
struct usb_device *dev;
for (bus=usb_busses; bus != NULL; bus=bus->next) {
for (dev=bus->devices; dev; dev=dev->next) {
if ((dev->descriptor.idVendor == SAMSUNG_VENDOR_ID) &&
(dev->descriptor.idProduct == SAMSUNG_PRODUCT_ID))
return dev;
}
}
return NULL;
}
int main() {
int r;
struct usb_device *dev;
usb_dev_handle *udev;
usb_init();
usb_find_busses();
usb_find_devices();
dev = find_device();
if (!dev) {
error("Device not found");
exit(-1);
}
info("Device found, %d configuration(s)", dev->descriptor.bNumConfigurations);
assert(dev->descriptor.bNumConfigurations == 2);
udev = usb_open(dev);
if (!udev) {
error("Can't open device");
exit(-1);
}
info("Device opened, Switching to configuration #2");
usb_reset(udev);
r = usb_set_configuration(udev, 2);
if (r != 0) {
error("Configuration switch failed");
usb_close(udev);
exit(-1);
}
info("Configuration switched!");
usb_close(udev);
return 0;
}