/
novena-usb-hub.c
458 lines (374 loc) · 9.77 KB
/
novena-usb-hub.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright (C) 2006 Free Software Initiative of Japan
*
* Author: NIIBE Yutaka <gniibe at fsij.org>
* with changes by rss 14.03.2010
*
* This file can be distributed under the terms and conditions of the
* GNU General Public License version 2 (or later).
*
*/
#include <errno.h>
#include <libusb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdint.h>
struct state {
struct libusb_context *ctx;
char *progname;
};
#define USB_RT_HUB (LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_DEVICE)
#define USB_RT_PORT (LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_OTHER)
#define USB_PORT_FEAT_POWER 8
#define USB_PORT_FEAT_INDICATOR 22
#define COMMAND_SET_NONE 0
#define COMMAND_SET_LED 1
#define COMMAND_SET_POWER 2
#define HUB_LED_GREEN 2
#define HUB_CHAR_LPSM 0x0003
#define HUB_CHAR_PORTIND 0x0080
struct usb_hub_descriptor {
unsigned char bDescLength;
unsigned char bDescriptorType;
unsigned char bNbrPorts;
unsigned char wHubCharacteristics[2];
unsigned char bPwrOn2PwrGood;
unsigned char bHubContrCurrent;
unsigned char data[0];
};
#define CTRL_TIMEOUT 1000
#define USB_STATUS_SIZE 4
#define MAX_HUBS 128
struct hub_info {
int busnum, devnum;
struct libusb_device *dev;
int nport;
int indicator_support;
};
static const char *port_names[] = {
"Side (near SD slot)",
"100 Mbit Ethernet",
"PCIe",
"Downstream hub",
"Side (away from SD slot)",
"Front panel",
"Near speakers",
"LVDS board",
};
static int get_hub_number(libusb_device *dev) {
struct libusb_device *parent;
struct libusb_device_descriptor parent_device_desc;
uint16_t dev_vid, dev_pid;
int ret;
parent = libusb_get_parent(dev);
ret = libusb_get_device_descriptor(parent, &parent_device_desc);
if (ret)
return -1;
dev_vid = libusb_le16_to_cpu(parent_device_desc.idVendor);
dev_pid = libusb_le16_to_cpu(parent_device_desc.idProduct);
if (dev_vid == 0x1d6b && dev_pid == 0x0002) {
// If parent is "Linux Foundation 2.0 root hub", this is upstream hub
return 1;
}
if (dev_vid == 0x05e3 && dev_pid == 0x0614) {
// If parent is another hub ...
if (libusb_get_port_number(dev) == 4) {
// and we're connected to port #4...
if (get_hub_number(parent) == 1) {
// And it's upstream hub, then we're downstream one
return 2;
}
}
}
return 3;
}
static void hub_port_status(libusb_device_handle *uh, int hub_number) {
int i;
int ret;
struct usb_hub_descriptor d;
int nport;
char c;
if (hub_number == 0)
c = 'u';
else if (hub_number == 1)
c = 'd';
else
c = '?';
ret = libusb_control_transfer(uh,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS,
LIBUSB_REQUEST_GET_DESCRIPTOR,
0, 0, (unsigned char *)&d, sizeof(d), CTRL_TIMEOUT);
if (ret < 0) {
fprintf(stderr,
"Error %d, cannot read hub status, %s (%d)\n",
ret, strerror(errno), errno);
return;
}
nport = d.bNbrPorts;
printf("Hub Status (%d ports):\n", nport);
for (i = 0; i < nport; i++)
{
uint8_t buf[USB_STATUS_SIZE];
ret = libusb_control_transfer(uh,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_OTHER,
LIBUSB_REQUEST_GET_STATUS,
0, i + 1,
buf, sizeof(buf),
CTRL_TIMEOUT);
if (ret < 0)
{
fprintf (stderr,
"Error %d, cannot read port %d status, %s (%d)\n",
ret, i + 1, strerror(errno), errno);
continue;
}
printf(" Port %d (%c%d): %02x%02x.%02x%02x", i + 1,
c, i + 1,
buf[3], buf [2],
buf[1], buf [0]);
printf("%s%s%s%s%s",
(buf[2] & 0x10) ? " C_RESET" : "",
(buf[2] & 0x08) ? " C_OC" : "",
(buf[2] & 0x04) ? " C_SUSPEND" : "",
(buf[2] & 0x02) ? " C_ENABLE" : "",
(buf[2] & 0x01) ? " C_CONNECT" : "");
printf("%s%s%s%s%s%s%s%s%s%s",
(buf[1] & 0x10) ? " indicator" : "",
(buf[1] & 0x08) ? " test" : "",
(buf[1] & 0x04) ? " highspeed" : "",
(buf[1] & 0x02) ? " lowspeed" : "",
(buf[1] & 0x01) ? " power" : "",
(buf[0] & 0x10) ? " RESET" : "",
(buf[0] & 0x08) ? " oc" : "",
(buf[0] & 0x04) ? " suspend" : "",
(buf[0] & 0x02) ? " enable" : "",
(buf[0] & 0x01) ? " connect" : "");
if (((hub_number * 4) + i) < (sizeof(port_names) / sizeof(*port_names)))
printf(" - %s", port_names[hub_number * 4 + i]);
printf("\n");
}
}
static int set_port_power(struct state *st,
struct libusb_device *hub_dev,
int port,
int enabled) {
int feature = USB_PORT_FEAT_POWER;
int request;
int index;
int ret;
struct libusb_device_handle *hub = NULL;
if (enabled) {
request = LIBUSB_REQUEST_SET_FEATURE;
index = port;
}
else {
request = LIBUSB_REQUEST_CLEAR_FEATURE;
feature = USB_PORT_FEAT_POWER;
index = port;
}
ret = libusb_open(hub_dev, &hub);
if (ret) {
fprintf(stderr, "Unable to open USB hub: %d\n", ret);
return ret;
}
ret = libusb_control_transfer(hub, USB_RT_PORT,
request, feature, index,
NULL, 0, CTRL_TIMEOUT);
if (ret) {
fprintf(stderr, "Unable to send packet to USB hub: %d\n", ret);
libusb_close(hub);
return ret;
}
libusb_close(hub);
return ret;
}
static int get_port(struct state *st, char *port, int *portnum,
struct libusb_device **hub_dev) {
struct libusb_device **devs;
struct libusb_device *dev;
int current;
int ret = 0;
int bus_offset = 0;
int hub_number = 0;
if (*port == 'd')
bus_offset = 2;
else if (*port == 'u')
bus_offset = 1;
else
return -EINVAL;
*portnum = strtoul(port+1, NULL, 0);
if (*portnum < 1 || *portnum > 4)
return -EINVAL;
/* XXX These ports have their wires swapped */
if (bus_offset == 2) {
if (*portnum == 4)
*portnum = 1;
else if (*portnum == 1)
*portnum = 4;
}
if (libusb_get_device_list(st->ctx, &devs) < 0){
perror ("failed to access USB");
return 0;
}
for (current=0; (dev = devs[current]) != NULL; current++) {
struct libusb_device_descriptor device_desc;
struct libusb_config_descriptor *config_desc;
libusb_device_handle *uh;
uint16_t dev_vid, dev_pid;
ret = libusb_get_device_descriptor(dev, &device_desc);
if (ret)
goto err;
ret = libusb_get_active_config_descriptor(dev, &config_desc);
dev_vid = libusb_le16_to_cpu(device_desc.idVendor);
dev_pid = libusb_le16_to_cpu(device_desc.idProduct);
if (libusb_open(dev, &uh) != 0 )
continue;
if (dev_vid == 0x05e3 && dev_pid == 0x0614) {
hub_number = get_hub_number(dev);
if (hub_number == bus_offset) {
libusb_ref_device(dev);
*hub_dev = dev;
}
}
libusb_free_config_descriptor(config_desc);
libusb_close(uh);
}
err:
libusb_free_device_list(devs, 1);
return ret;
}
static int set_port(struct state *st, char *port, int val) {
struct libusb_device *hub;
int ret;
int portnum;
ret = get_port(st, port, &portnum, &hub);
if (ret == -EINVAL) {
fprintf(stderr, "Error: Must specify port as 'dN' or 'uN' "
"for either downstream or upstream hub.\n"
"Port number must be 1, 2, 3, or 4.\n");
return ret;
}
else if (ret)
return ret;
ret = set_port_power(st, hub, portnum, val);
libusb_unref_device(hub);
return ret;
}
static int port_enable(struct state *st, char *port) {
return set_port(st, port, 1);
}
static int port_disable(struct state *st, char *port) {
return set_port(st, port, 0);
}
static int list_ports(struct state *st) {
struct libusb_device **devs;
struct libusb_device *dev;
int current;
int hub_number;
int unknown_hub_number = 0;
int ret = 0;
if (libusb_get_device_list(st->ctx, &devs) < 0){
perror ("failed to access USB");
return 0;
}
for (current=0; (dev = devs[current]) != NULL; current++) {
struct libusb_device_descriptor device_desc;
struct libusb_config_descriptor *config_desc;
libusb_device_handle *uh;
uint16_t dev_vid, dev_pid;
ret = libusb_get_device_descriptor(dev, &device_desc);
if (ret)
goto err;
ret = libusb_get_active_config_descriptor(dev, &config_desc);
dev_vid = libusb_le16_to_cpu(device_desc.idVendor);
dev_pid = libusb_le16_to_cpu(device_desc.idProduct);
if (libusb_open(dev, &uh) != 0 )
continue;
if (dev_vid == 0x05e3 && dev_pid == 0x0614) {
hub_number = get_hub_number(dev);
if (hub_number == 1)
printf("Upstream hub - ");
else if (hub_number == 2)
printf("Downstream hub - ");
else
printf("Unknown hub %d - ", (++unknown_hub_number));
hub_port_status(uh, hub_number - 1);
}
libusb_free_config_descriptor(config_desc);
libusb_close(uh);
}
err:
libusb_free_device_list(devs, 1);
return ret;
}
static int print_help(struct state *st) {
fprintf(stderr,
"Usage: %s [-e PORT] [-d PORT] -l\n"
"\n"
"Where PORT is defined as 'u' or 'd' followed by the port number.\n"
"For example, 'u3' for upstream port 3, or 'd2' for downstream port 2.\n"
"To list port status, run with -l.\n"
"",
st->progname);
return 0;
}
static int novena_hub_init(struct state *st) {
int ret;
ret = libusb_init(&st->ctx);
if (ret)
return ret;
libusb_set_debug(st->ctx, 3);
return ret;
}
static int novena_hub_deinit(struct state *st) {
libusb_exit(st->ctx);
return 0;
}
int main(int argc, char * const *argv) {
int power_up = 0;
int power_down = 0;
int action_taken = 0;
int ret;
struct state st;
int ch;
struct option longopts[] = {
{ "list-ports", no_argument, NULL, 'l' },
{ "port-enable",required_argument, &power_up, 'e' },
{ "port-disable",required_argument, &power_down, 'd' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
memset(&st, 0, sizeof(st));
st.progname = argv[0];
ret = novena_hub_init(&st);
if (ret)
return ret;
while ((ch = getopt_long(argc, argv, "le:d:h", longopts, NULL)) != -1) {
switch (ch) {
case 'h':
print_help(&st);
action_taken = 1;
break;
case 'e':
port_enable(&st, optarg);
action_taken = 1;
break;
case 'd':
port_disable(&st, optarg);
action_taken = 1;
break;
case 'l':
list_ports(&st);
action_taken = 1;
break;
default:
break;
}
}
if (!action_taken)
print_help(&st);
novena_hub_deinit(&st);
return 0;
}