-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuclogic-probe.c
244 lines (216 loc) · 7.22 KB
/
uclogic-probe.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
/*
* Copyright (C) 2013 Nikolai Kondrashov
*
* This file is part of uclogic-tools.
*
* Uclogic-tools 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 2 of the License, or
* (at your option) any later version.
*
* Uclogic-tools 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 uclogic-tools; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Nikolai Kondrashov <spbnick@gmail.com>
*/
#include "config.h"
#include <libusb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <strings.h>
#ifndef HAVE_LIBUSB_STRERROR
static const char *
libusb_strerror(enum libusb_error err)
{
switch (err)
{
case LIBUSB_SUCCESS:
return "Success";
#define MAP(_name, _desc) \
case LIBUSB_ERROR_##_name: \
return _desc " (ERROR_" #_name ")"
MAP(IO,
"Input/output error");
MAP(INVALID_PARAM,
"Invalid parameter");
MAP(ACCESS,
"Access denied (insufficient permissions)");
MAP(NO_DEVICE,
"No such device (it may have been disconnected)");
MAP(NOT_FOUND,
"Entity not found");
MAP(BUSY,
"Resource busy");
MAP(TIMEOUT,
"Operation timed out");
MAP(OVERFLOW,
"Overflow");
MAP(PIPE,
"Pipe error");
MAP(INTERRUPTED,
"System call interrupted (perhaps due to signal)");
MAP(NO_MEM,
"Insufficient memory");
MAP(NOT_SUPPORTED,
"Operation not supported or unimplemented on this platform");
MAP(OTHER, "Other error");
#undef MAP
default:
return "Unknown error code";
}
}
#endif
#define GENERIC_ERROR(_fmt, _args...) \
fprintf(stderr, _fmt "\n", ##_args)
#define GENERIC_FAILURE(_fmt, _args...) \
GENERIC_ERROR("Failed to " _fmt, ##_args)
#define LIBUSB_FAILURE(_err, _fmt, _args...) \
GENERIC_FAILURE(_fmt ": %s", ##_args, libusb_strerror(_err))
#define ERROR_CLEANUP(_fmt, _args...) \
do { \
GENERIC_ERROR(_fmt, ##_args); \
goto cleanup; \
} while (0)
#define FAILURE_CLEANUP(_fmt, _args...) \
do { \
GENERIC_FAILURE(_fmt, ##_args); \
goto cleanup; \
} while (0)
#define LIBUSB_FAILURE_CLEANUP(_err, _fmt, _args...) \
do { \
LIBUSB_FAILURE(_err, _fmt, ##_args); \
goto cleanup; \
} while (0)
#define LIBUSB_GUARD(_expr, _fmt, _args...) \
do { \
enum libusb_error err = _expr; \
if (err < 0) \
LIBUSB_FAILURE_CLEANUP(err, _fmt, ##_args); \
} while (0)
static void
print_chunk(char type, unsigned char *ptr, int len)
{
printf("%c", type);
for (; len > 0; ptr++, len--)
printf(" %.2hhX", *ptr);
printf("\n");
fflush(stdout);
}
static int
probe(uint8_t bus_num, uint8_t dev_addr)
{
int result = 1;
libusb_context *ctx = NULL;
libusb_device **dev_list = NULL;
ssize_t dev_num;
size_t i;
libusb_device *dev;
libusb_device_handle *handle = NULL;
unsigned char buf[257];
int len;
uint8_t idx_list[] = {0x64, 0x65, 0x6E, 0x79, 0x7A,
0x7B, 0xC8, 0xC9, 0xCA};
uint8_t idx;
struct libusb_device_descriptor dev_desc;
LIBUSB_GUARD(libusb_init(&ctx), "initialize libusb");
LIBUSB_GUARD(dev_num = libusb_get_device_list(ctx, &dev_list),
"get device list");
for (i = 0; i < (size_t)dev_num; i++) {
dev = dev_list[i];
if (libusb_get_bus_number(dev) == bus_num &&
libusb_get_device_address(dev) == dev_addr)
break;
}
if (i == (size_t)dev_num)
ERROR_CLEANUP("Device not found");
LIBUSB_GUARD(libusb_open(dev, &handle), "open device");
libusb_free_device_list(dev_list, true);
dev_list = NULL;
LIBUSB_GUARD(libusb_get_device_descriptor(dev, &dev_desc),
"get device descriptor");
if (dev_desc.iManufacturer != 0) {
len = libusb_get_string_descriptor(handle, dev_desc.iManufacturer,
/* English (United States) */
0x0409,
buf, sizeof(buf));
if (len >= 0)
print_chunk('M', buf + 2, len - 2);
else if (len != LIBUSB_ERROR_PIPE) {
LIBUSB_FAILURE(len, "get manufacturer string descriptor");
goto cleanup;
}
}
if (dev_desc.iProduct != 0) {
len = libusb_get_string_descriptor(handle, dev_desc.iProduct,
/* English (United States) */
0x0409,
buf, sizeof(buf));
if (len >= 0)
print_chunk('P', buf + 2, len - 2);
else if (len != LIBUSB_ERROR_PIPE) {
LIBUSB_FAILURE(len, "get product string descriptor");
goto cleanup;
}
}
for (i = 0; i < sizeof(idx_list) / sizeof(*idx_list); i++)
{
idx = idx_list[i];
buf[0] = idx;
/* Attempt to get the descriptor */
len = libusb_get_string_descriptor(
handle, idx,
/* English (United States) */
0x0409,
buf + 1, sizeof(buf) - 1);
/* If the descriptor doesn't exist */
if (len == LIBUSB_ERROR_PIPE)
continue;
LIBUSB_GUARD(len, "get string descriptor 0x%.2X", idx);
/* Print the descriptor */
print_chunk('S', buf, len + 1);
}
result = 0;
cleanup:
libusb_close(handle);
libusb_free_device_list(dev_list, true);
libusb_exit(ctx);
return result;
}
static void
usage(FILE *file, const char *name)
{
fprintf(file,
"Usage: %s BUS_NUM DEV_ADDR\n"
"Probe a UC-Logic tablet.\n"
"\n"
"Arguments:\n"
" BUS_NUM Bus number.\n"
" DEV_ADDR Device address.\n"
"\n",
name);
}
int
main(int argc, char **argv)
{
const char *name;
name = rindex(argv[0], '/');
if (name == NULL)
name = argv[0];
else
name++;
if (argc != 3) {
fprintf(stderr, "Invalid number of arguments\n");
usage(stderr, name);
exit(1);
}
setbuf(stdout, NULL);
return probe((uint8_t)atoi(argv[1]),
(uint8_t)atoi(argv[2]));
}