-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.c
270 lines (220 loc) · 10.1 KB
/
select.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
*
*
* gcc select.c -lusb-1.0 -o select
./select 1 : F1 profile
./select 2 : F2 profile
./select 3 : F3 profile
./select 4 : internal profile
./select 5 : Error, reset keyboard, set to default configuration.
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
#define VENDOR 0x0951
#define PRODUCT 0x16dc
int claim_interfaces(libusb_device *device, libusb_device_handle *handle) {
int i = 0, res = 0;
struct libusb_config_descriptor *config = NULL;
res = libusb_get_config_descriptor(device, 0, &config);
if(res != 0) {
goto cleanup;
}
for(i = 0; i < config->bNumInterfaces; ++i) {
res = libusb_claim_interface(handle, i);
if(res != LIBUSB_SUCCESS) {
for(i; i >= 0; --i) {
libusb_release_interface(handle, i);
}
printf("[%d] Failed claiming interface.", res);
break;
}
}
cleanup:
libusb_free_config_descriptor(config);
return res;
}
int release_interfaces(libusb_device *device, libusb_device_handle *handle) {
int i = 0, res = 0;
struct libusb_config_descriptor *config = NULL;
res = libusb_get_config_descriptor(device, 0, &config);
if(res != 0) {
goto cleanup;
}
for(i = 0; i < config->bNumInterfaces; ++i) {
libusb_release_interface(handle, i);
}
cleanup:
libusb_free_config_descriptor(config);
return res;
}
int find_device(int vendor, int product, libusb_device **output, libusb_device_handle **handle) {
libusb_device **list;
libusb_device *device = NULL;
struct libusb_device_descriptor desc;
int status = 0, res = 0;
ssize_t i = 0;
ssize_t cnt = libusb_get_device_list(NULL, &list);
*output = NULL;
*handle = NULL;
if(cnt < 0) {
fprintf(stderr, "No USB devices discovered.\n");
status = 1;
goto cleanup;
}
for(i = 0; i < cnt; ++i) {
device = list[i];
res = libusb_get_device_descriptor(device, &desc);
if(res != 0) {
fprintf(stderr, "[%d] Error getting device descriptor.\n", res);
continue;
}
if(desc.idVendor == vendor && desc.idProduct == product) {
*output = device;
break;
}
}
if(*output != NULL) {
res = libusb_open(device, handle);
if(res == LIBUSB_SUCCESS) {
status = 0;
}else{
status = 2;
fprintf(stderr, "[%d] Failed opening USB device.\n", res);
}
} else {
status = 3;
}
cleanup:
libusb_free_device_list(list, 1);
return status;
}
int main(int argc, char *argv[]) {
libusb_device *device = NULL;
libusb_device_handle *handle = NULL;
int status = 0;
char data[512];
int v,i,len,m;
unsigned xlen=264;
if (argc<2) return 0;
m=atoi(argv[1]);
if (m<1 || m>4) return 0;
char INIT0[] = {
0x07,0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
char INIT1[] = {
0x07,0x01,0x00,0x00,0x00,0x04,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
char INITREAD[] = {
0x09,0x00,0x00,0x81,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
INIT0[2]=m;
INIT1[2]=m;
libusb_init(NULL);
// libusb_set_debug(NULL, 255);
status = find_device(VENDOR, PRODUCT, &device, &handle);
if(status != 0) {
fprintf(stderr, "[%d] Failed to find device.\n", status);
goto cleanup;
}
libusb_set_auto_detach_kernel_driver(handle,1);
claim_interfaces(device,handle);
//SEND INIT0
v=libusb_control_transfer(handle, // Dev Handle
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, // RequestType = 0x21
LIBUSB_REQUEST_SET_CONFIGURATION, // Request = 0x09
0x307,// Value => Descriptor type | Descriptor index
2, // Index => interface number
INIT0,// Data
xlen,// Length
10000);// Timeout
v=libusb_interrupt_transfer(handle,0x83,data,300,&len,10000);
printf ("I0:INT IN : LEN=%i\n",len);
printf ("DATA : "); for (i=0;i<len;i++) printf ("%02X ",data[i]); printf ("\n");
/*int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
uint8_t request_type,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int timeout);*/
//RECEIVE
v=libusb_control_transfer(handle, // Dev Handle
LIBUSB_ENDPOINT_IN |
LIBUSB_REQUEST_TYPE_CLASS |
LIBUSB_RECIPIENT_INTERFACE, // RequestType = 0xA1
LIBUSB_REQUEST_CLEAR_FEATURE, // Request = 0x01
0x307,// Value
2, // Index => interface number
INITREAD,// Data
xlen,// Length
10000);// Timeout
printf ("II:INT IN : LEN=%i\n",len);
printf ("DATA : "); for (i=0;i<50;i++) printf ("%02X ",INITREAD[i]); printf ("\n");
//SEND INIT1
v=libusb_control_transfer(handle, // Dev Handle
LIBUSB_ENDPOINT_OUT |
LIBUSB_REQUEST_TYPE_CLASS |
LIBUSB_RECIPIENT_INTERFACE, // RequestType = 0x21
LIBUSB_REQUEST_SET_CONFIGURATION, // Request = 0x09
0x307,// Value
2, // Index
INIT1,// Data
xlen,// Length
10000);// Timeout
v=libusb_interrupt_transfer(handle,0x83,data,300,&len,10000);
printf ("I2:INT IN : LEN=%i\n",len);
printf ("DATA : "); for (i=0;i<len;i++) printf ("%02X ",data[i]); printf ("\n");
release_interfaces(device,handle);
libusb_close(handle);
cleanup:
libusb_exit(NULL);
return status;
}