forked from codazoda/hub-ctrl.c
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhub-ctrl.c
227 lines (193 loc) · 6.22 KB
/
hub-ctrl.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
// sudo apt-get install libusb-dev
// gcc -o hub-ctrl hub-ctrl.c -lusb -std=c99
#include <usb.h>
#include <stdio.h>
#include <string.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define USB_RT_HUB (USB_TYPE_CLASS | USB_RECIP_DEVICE)
#define USB_RT_PORT (USB_TYPE_CLASS | USB_RECIP_OTHER)
#define USB_PORT_FEAT_POWER 8
#define USB_DIR_IN 0x80 /* to host */
#define HUB_CHAR_LPSM 0x0003
#define HUB_CHAR_PORTIND 0x0080
#define CTRL_TIMEOUT 1000
#define USB_STATUS_SIZE 4
#define MAX_HUBS 128
struct hub_metadata {
unsigned char bDescLength;
unsigned char bDescriptorType;
unsigned char bNbrPorts;
unsigned char wHubCharacteristics[2];
unsigned char bPwrOn2PwrGood;
unsigned char bHubContrCurrent;
unsigned char data[0];
};
struct hub_info {
int busnum;
int devnum;
struct usb_device *dev;
int port_count;
};
static struct hub_info hubs[MAX_HUBS];
static int hub_count = 0;
static void exit_with_usage (const char *progname) {
printf ("Usage: %s [-H <Hub> | -B <Bus> -D <Dev>] -P <Port> -p <0|1>\n", progname);
exit (1);
}
static void list_ports (usb_dev_handle *usb_handle, int port_count) {
for (int i = 1; i <= port_count; i++) {
char buf[USB_STATUS_SIZE];
if (usb_control_msg (usb_handle, USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_OTHER, USB_REQ_GET_STATUS, 0, i, buf, USB_STATUS_SIZE, CTRL_TIMEOUT) < 0) {
printf (ANSI_COLOR_RED "> Cannot read port %d status\n" ANSI_COLOR_RESET, i);
break;
}
if (i == port_count) { // last item in the list
printf(" └");
}
else {
printf(" ├");
}
printf("─ Port %2d:%s%s%s%s%s%s%s%s%s%s\n", i,
(buf[1] & 0x01) ? " power" : "",
(buf[1] & 0x02) ? " low-speed" : "",
(buf[1] & 0x04) ? " high-speed" : "",
(buf[1] & 0x08) ? " test" : "",
(buf[1] & 0x10) ? " indicator" : "",
(buf[0] & 0x01) ? " connect" : "",
(buf[0] & 0x02) ? " enable" : "",
(buf[0] & 0x04) ? " suspend" : "",
(buf[0] & 0x08) ? " oc" : "",
(buf[0] & 0x10) ? " RESET" : "");
}
printf(ANSI_COLOR_RESET);
}
static void list_hubs (int port) {
struct usb_bus *bus;
bus = usb_get_busses();
if (bus == NULL) {
printf (ANSI_COLOR_RED "> Failed to access USB bus." ANSI_COLOR_RESET);
exit(1);
}
for (bus; bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
continue;
}
usb_dev_handle *usb_handle;
usb_handle = usb_open (dev);
if (usb_handle != NULL) {
char buf[1024];
if (port == 0) {
int len;
struct hub_metadata *hub_data = (struct hub_metadata *)buf;
if ((len = usb_control_msg (usb_handle, USB_DIR_IN | USB_RT_HUB, USB_REQ_GET_DESCRIPTOR, USB_DT_HUB << 8, 0, buf, sizeof (buf), CTRL_TIMEOUT)) > sizeof (struct hub_metadata)) {
printf ("Hub %d (Bus %d, Dev %d) ", hub_count, atoi(bus->dirname), dev->devnum);
switch ((hub_data->wHubCharacteristics[0] & HUB_CHAR_LPSM)) {
case 0:
printf (ANSI_COLOR_YELLOW "- ganged power switching\n");
break;
case 1:
printf (ANSI_COLOR_GREEN "- individual power switching\n");
break;
case 2:
case 3:
printf (ANSI_COLOR_RED "- no power switching\n");
break;
}
}
else {
perror (ANSI_COLOR_RED "> Can't get hub descriptor" ANSI_COLOR_RESET);
usb_close (usb_handle);
continue;
}
}
int port_count = buf[2];
hubs[hub_count].busnum = atoi(bus->dirname);
hubs[hub_count].devnum = dev->devnum;
hubs[hub_count].dev = dev;
hubs[hub_count].port_count = port_count;
hub_count++;
list_ports (usb_handle, port_count); // print port status of given hub
usb_close (usb_handle);
}
}
}
if (hub_count == 0) {
printf (ANSI_COLOR_RED "> No hub found.\n" ANSI_COLOR_RESET);
exit(1);
}
}
int get_hub (int busnum, int devnum) {
for (int i = 0; i < hub_count; i++) {
if (hubs[i].busnum == busnum && hubs[i].devnum == devnum) {
return i;
}
}
return -1;
}
int main (int argc, const char *argv[]) {
int busnum = 0;
int devnum = 0;
int port = 0;
int power = 1;
int hub = -1;
usb_dev_handle *usb_handle = NULL;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'H':
hub = atoi(argv[++i]);
break;
case 'B':
if (hub == -1) {
busnum = atoi(argv[++i]);
}
break;
case 'D':
if (hub == -1) {
devnum = atoi(argv[++i]);
}
break;
case 'P':
port = atoi(argv[++i]);
break;
case 'p':
power = atoi(argv[++i]);
break;
default:
exit_with_usage (argv[0]);
}
}
else {
exit_with_usage (argv[0]);
}
}
usb_init();
usb_find_busses();
usb_find_devices();
list_hubs(port); // port is only used to decide to print details or not
if (port == 0) {// no port number given, stop here
exit (0);
}
if (hub < 0) {// no hub number given
hub = get_hub(busnum, devnum);
}
if (hub >= 0 && hub < hub_count) {// valid hub number
usb_handle = usb_open (hubs[hub].dev);
}
if (usb_handle == NULL) {
printf (ANSI_COLOR_RED "> Device not found.\n" ANSI_COLOR_RESET);
exit(1);
}
if (usb_control_msg(usb_handle, USB_RT_PORT, power ? USB_REQ_SET_FEATURE : USB_REQ_CLEAR_FEATURE, USB_PORT_FEAT_POWER, port, NULL, 0, CTRL_TIMEOUT) < 0) {
printf (ANSI_COLOR_RED "> Failed to control.\n" ANSI_COLOR_RESET);
exit(1);
}
list_ports(usb_handle, hubs[hub].port_count);
printf ("> Hub:%d Bus:%d Devive:%d Port:%d power->%s\n",hub, busnum, devnum, port, power ? "on" : "off");
exit(0);
}