]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
0945b4fe TH |
2 | /* |
3 | * Copyright (C) 2005-2007 Takahiro Hirofuchi | |
4 | */ | |
5 | ||
099f79fa | 6 | #include "usbip_common.h" |
7 | #include "vhci_driver.h" | |
ec2ff627 VM |
8 | #include <limits.h> |
9 | #include <netdb.h> | |
021aed84 | 10 | #include <libudev.h> |
aa3ecb91 | 11 | #include <dirent.h> |
a744b7c6 | 12 | #include "sysfs_utils.h" |
0945b4fe | 13 | |
f2fb62b3 | 14 | #undef PROGNAME |
15 | #define PROGNAME "libusbip" | |
16 | ||
0945b4fe | 17 | struct usbip_vhci_driver *vhci_driver; |
021aed84 | 18 | struct udev *udev_context; |
0945b4fe | 19 | |
9db91e1b KK |
20 | static struct usbip_imported_device * |
21 | imported_device_init(struct usbip_imported_device *idev, char *busid) | |
0945b4fe | 22 | { |
021aed84 | 23 | struct udev_device *sudev; |
0945b4fe | 24 | |
021aed84 VM |
25 | sudev = udev_device_new_from_subsystem_sysname(udev_context, |
26 | "usb", busid); | |
0945b4fe | 27 | if (!sudev) { |
021aed84 | 28 | dbg("udev_device_new_from_subsystem_sysname failed: %s", busid); |
0945b4fe TH |
29 | goto err; |
30 | } | |
31 | read_usb_device(sudev, &idev->udev); | |
021aed84 | 32 | udev_device_unref(sudev); |
0945b4fe | 33 | |
0945b4fe TH |
34 | return idev; |
35 | ||
36 | err: | |
37 | return NULL; | |
38 | } | |
39 | ||
a744b7c6 | 40 | static int parse_status(const char *value) |
0945b4fe TH |
41 | { |
42 | int ret = 0; | |
43 | char *c; | |
44 | ||
0945b4fe | 45 | /* skip a header line */ |
2f5c638c CH |
46 | c = strchr(value, '\n'); |
47 | if (!c) | |
48 | return -1; | |
49 | c++; | |
0945b4fe TH |
50 | |
51 | while (*c != '\0') { | |
52 | int port, status, speed, devid; | |
53 | unsigned long socket; | |
54 | char lbusid[SYSFS_BUS_ID_SIZE]; | |
e55dea8e | 55 | struct usbip_imported_device *idev; |
1c9de5bf | 56 | char hub[3]; |
0945b4fe | 57 | |
1c9de5bf YD |
58 | ret = sscanf(c, "%2s %d %d %d %x %lx %31s\n", |
59 | hub, &port, &status, &speed, | |
0945b4fe TH |
60 | &devid, &socket, lbusid); |
61 | ||
62 | if (ret < 5) { | |
25567a39 | 63 | dbg("sscanf failed: %d", ret); |
0945b4fe TH |
64 | BUG(); |
65 | } | |
66 | ||
1c9de5bf YD |
67 | dbg("hub %s port %d status %d speed %d devid %x", |
68 | hub, port, status, speed, devid); | |
0945b4fe TH |
69 | dbg("socket %lx lbusid %s", socket, lbusid); |
70 | ||
0945b4fe | 71 | /* if a device is connected, look at it */ |
e55dea8e | 72 | idev = &vhci_driver->idev[port]; |
e55dea8e | 73 | memset(idev, 0, sizeof(*idev)); |
0945b4fe | 74 | |
1c9de5bf YD |
75 | if (strncmp("hs", hub, 2) == 0) |
76 | idev->hub = HUB_SPEED_HIGH; | |
77 | else /* strncmp("ss", hub, 2) == 0 */ | |
78 | idev->hub = HUB_SPEED_SUPER; | |
79 | ||
e55dea8e YD |
80 | idev->port = port; |
81 | idev->status = status; | |
0945b4fe | 82 | |
e55dea8e | 83 | idev->devid = devid; |
0945b4fe | 84 | |
e55dea8e YD |
85 | idev->busnum = (devid >> 16); |
86 | idev->devnum = (devid & 0x0000ffff); | |
87 | ||
88 | if (idev->status != VDEV_ST_NULL | |
89 | && idev->status != VDEV_ST_NOTASSIGNED) { | |
90 | idev = imported_device_init(idev, lbusid); | |
91 | if (!idev) { | |
92 | dbg("imported_device_init failed"); | |
93 | return -1; | |
0945b4fe TH |
94 | } |
95 | } | |
96 | ||
0945b4fe | 97 | /* go to the next line */ |
2f5c638c CH |
98 | c = strchr(c, '\n'); |
99 | if (!c) | |
100 | break; | |
101 | c++; | |
0945b4fe TH |
102 | } |
103 | ||
104 | dbg("exit"); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
82a2b827 | 109 | #define MAX_STATUS_NAME 18 |
fd92b7de | 110 | |
0945b4fe TH |
111 | static int refresh_imported_device_list(void) |
112 | { | |
a744b7c6 | 113 | const char *attr_status; |
fd92b7de YD |
114 | char status[MAX_STATUS_NAME+1] = "status"; |
115 | int i, ret; | |
116 | ||
117 | for (i = 0; i < vhci_driver->ncontrollers; i++) { | |
118 | if (i > 0) | |
119 | snprintf(status, sizeof(status), "status.%d", i); | |
120 | ||
121 | attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device, | |
122 | status); | |
123 | if (!attr_status) { | |
124 | err("udev_device_get_sysattr_value failed"); | |
125 | return -1; | |
126 | } | |
0945b4fe | 127 | |
fd92b7de YD |
128 | dbg("controller %d", i); |
129 | ||
130 | ret = parse_status(attr_status); | |
131 | if (ret != 0) | |
132 | return ret; | |
0945b4fe TH |
133 | } |
134 | ||
fd92b7de | 135 | return 0; |
0945b4fe TH |
136 | } |
137 | ||
138 | static int get_nports(void) | |
139 | { | |
37e47d5c | 140 | const char *attr_nports; |
0945b4fe | 141 | |
37e47d5c YD |
142 | attr_nports = udev_device_get_sysattr_value(vhci_driver->hc_device, "nports"); |
143 | if (!attr_nports) { | |
144 | err("udev_device_get_sysattr_value nports failed"); | |
0945b4fe TH |
145 | return -1; |
146 | } | |
147 | ||
37e47d5c | 148 | return (int)strtoul(attr_nports, NULL, 10); |
0945b4fe TH |
149 | } |
150 | ||
aa3ecb91 YD |
151 | static int vhci_hcd_filter(const struct dirent *dirent) |
152 | { | |
153 | return strcmp(dirent->d_name, "vhci_hcd") >= 0; | |
154 | } | |
155 | ||
156 | static int get_ncontrollers(void) | |
157 | { | |
158 | struct dirent **namelist; | |
159 | struct udev_device *platform; | |
160 | int n; | |
161 | ||
162 | platform = udev_device_get_parent(vhci_driver->hc_device); | |
163 | if (platform == NULL) | |
164 | return -1; | |
165 | ||
166 | n = scandir(udev_device_get_syspath(platform), &namelist, vhci_hcd_filter, NULL); | |
167 | if (n < 0) | |
168 | err("scandir failed"); | |
169 | else { | |
170 | for (int i = 0; i < n; i++) | |
171 | free(namelist[i]); | |
172 | free(namelist); | |
173 | } | |
174 | ||
175 | return n; | |
176 | } | |
177 | ||
a37d70eb MA |
178 | /* |
179 | * Read the given port's record. | |
180 | * | |
181 | * To avoid buffer overflow we will read the entire line and | |
182 | * validate each part's size. The initial buffer is padded by 4 to | |
183 | * accommodate the 2 spaces, 1 newline and an additional character | |
184 | * which is needed to properly validate the 3rd part without it being | |
185 | * truncated to an acceptable length. | |
186 | */ | |
187 | static int read_record(int rhport, char *host, unsigned long host_len, | |
188 | char *port, unsigned long port_len, char *busid) | |
ec2ff627 | 189 | { |
a37d70eb | 190 | int part; |
ec2ff627 VM |
191 | FILE *file; |
192 | char path[PATH_MAX+1]; | |
a37d70eb MA |
193 | char *buffer, *start, *end; |
194 | char delim[] = {' ', ' ', '\n'}; | |
195 | int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE}; | |
196 | size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4; | |
197 | ||
198 | buffer = malloc(buffer_len); | |
199 | if (!buffer) | |
200 | return -1; | |
ec2ff627 VM |
201 | |
202 | snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport); | |
203 | ||
204 | file = fopen(path, "r"); | |
205 | if (!file) { | |
206 | err("fopen"); | |
a37d70eb | 207 | free(buffer); |
ec2ff627 VM |
208 | return -1; |
209 | } | |
210 | ||
a37d70eb MA |
211 | if (fgets(buffer, buffer_len, file) == NULL) { |
212 | err("fgets"); | |
213 | free(buffer); | |
ec2ff627 VM |
214 | fclose(file); |
215 | return -1; | |
216 | } | |
ec2ff627 VM |
217 | fclose(file); |
218 | ||
a37d70eb MA |
219 | /* validate the length of each of the 3 parts */ |
220 | start = buffer; | |
221 | for (part = 0; part < 3; part++) { | |
222 | end = strchr(start, delim[part]); | |
223 | if (end == NULL || (end - start) > max_len[part]) { | |
224 | free(buffer); | |
225 | return -1; | |
226 | } | |
227 | start = end + 1; | |
228 | } | |
229 | ||
230 | if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) { | |
231 | err("sscanf"); | |
232 | free(buffer); | |
233 | return -1; | |
234 | } | |
235 | ||
236 | free(buffer); | |
237 | ||
ec2ff627 VM |
238 | return 0; |
239 | } | |
0945b4fe TH |
240 | |
241 | /* ---------------------------------------------------------------------- */ | |
242 | ||
243 | int usbip_vhci_driver_open(void) | |
244 | { | |
021aed84 VM |
245 | udev_context = udev_new(); |
246 | if (!udev_context) { | |
247 | err("udev_new failed"); | |
248 | return -1; | |
249 | } | |
250 | ||
a744b7c6 | 251 | vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver)); |
0945b4fe TH |
252 | |
253 | /* will be freed in usbip_driver_close() */ | |
a744b7c6 VM |
254 | vhci_driver->hc_device = |
255 | udev_device_new_from_subsystem_sysname(udev_context, | |
256 | USBIP_VHCI_BUS_TYPE, | |
dff3565b | 257 | USBIP_VHCI_DEVICE_NAME); |
0945b4fe | 258 | if (!vhci_driver->hc_device) { |
a744b7c6 | 259 | err("udev_device_new_from_subsystem_sysname failed"); |
0945b4fe TH |
260 | goto err; |
261 | } | |
262 | ||
263 | vhci_driver->nports = get_nports(); | |
25567a39 | 264 | dbg("available ports: %d", vhci_driver->nports); |
0945b4fe | 265 | |
c3509715 YD |
266 | if (vhci_driver->nports <= 0) { |
267 | err("no available ports"); | |
268 | goto err; | |
269 | } else if (vhci_driver->nports > MAXNPORT) { | |
270 | err("port number exceeds %d", MAXNPORT); | |
271 | goto err; | |
272 | } | |
273 | ||
aa3ecb91 YD |
274 | vhci_driver->ncontrollers = get_ncontrollers(); |
275 | dbg("available controllers: %d", vhci_driver->ncontrollers); | |
276 | ||
277 | if (vhci_driver->ncontrollers <=0) { | |
278 | err("no available usb controllers"); | |
279 | goto err; | |
280 | } | |
281 | ||
0945b4fe TH |
282 | if (refresh_imported_device_list()) |
283 | goto err; | |
284 | ||
0945b4fe TH |
285 | return 0; |
286 | ||
0945b4fe | 287 | err: |
a744b7c6 VM |
288 | udev_device_unref(vhci_driver->hc_device); |
289 | ||
0945b4fe TH |
290 | if (vhci_driver) |
291 | free(vhci_driver); | |
292 | ||
293 | vhci_driver = NULL; | |
021aed84 VM |
294 | |
295 | udev_unref(udev_context); | |
296 | ||
0945b4fe TH |
297 | return -1; |
298 | } | |
299 | ||
300 | ||
19495513 | 301 | void usbip_vhci_driver_close(void) |
0945b4fe TH |
302 | { |
303 | if (!vhci_driver) | |
304 | return; | |
305 | ||
a744b7c6 VM |
306 | udev_device_unref(vhci_driver->hc_device); |
307 | ||
0945b4fe TH |
308 | free(vhci_driver); |
309 | ||
310 | vhci_driver = NULL; | |
021aed84 VM |
311 | |
312 | udev_unref(udev_context); | |
0945b4fe TH |
313 | } |
314 | ||
315 | ||
316 | int usbip_vhci_refresh_device_list(void) | |
317 | { | |
0945b4fe TH |
318 | |
319 | if (refresh_imported_device_list()) | |
320 | goto err; | |
321 | ||
322 | return 0; | |
323 | err: | |
25567a39 | 324 | dbg("failed to refresh device list"); |
0945b4fe TH |
325 | return -1; |
326 | } | |
327 | ||
328 | ||
1c9de5bf | 329 | int usbip_vhci_get_free_port(uint32_t speed) |
0945b4fe TH |
330 | { |
331 | for (int i = 0; i < vhci_driver->nports; i++) { | |
1ac7c8a7 SK |
332 | |
333 | switch (speed) { | |
334 | case USB_SPEED_SUPER: | |
335 | if (vhci_driver->idev[i].hub != HUB_SPEED_SUPER) | |
336 | continue; | |
337 | break; | |
338 | default: | |
339 | if (vhci_driver->idev[i].hub != HUB_SPEED_HIGH) | |
340 | continue; | |
341 | break; | |
342 | } | |
1c9de5bf | 343 | |
0945b4fe | 344 | if (vhci_driver->idev[i].status == VDEV_ST_NULL) |
1c9de5bf | 345 | return vhci_driver->idev[i].port; |
0945b4fe TH |
346 | } |
347 | ||
348 | return -1; | |
349 | } | |
350 | ||
351 | int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid, | |
352 | uint32_t speed) { | |
0945b4fe | 353 | char buff[200]; /* what size should be ? */ |
a744b7c6 VM |
354 | char attach_attr_path[SYSFS_PATH_MAX]; |
355 | char attr_attach[] = "attach"; | |
356 | const char *path; | |
0945b4fe TH |
357 | int ret; |
358 | ||
5484081d | 359 | snprintf(buff, sizeof(buff), "%u %d %u %u", |
0945b4fe TH |
360 | port, sockfd, devid, speed); |
361 | dbg("writing: %s", buff); | |
362 | ||
a744b7c6 VM |
363 | path = udev_device_get_syspath(vhci_driver->hc_device); |
364 | snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s", | |
365 | path, attr_attach); | |
366 | dbg("attach attribute path: %s", attach_attr_path); | |
367 | ||
368 | ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff)); | |
0945b4fe | 369 | if (ret < 0) { |
a744b7c6 | 370 | dbg("write_sysfs_attribute failed"); |
0945b4fe TH |
371 | return -1; |
372 | } | |
373 | ||
25567a39 | 374 | dbg("attached port: %d", port); |
0945b4fe TH |
375 | |
376 | return 0; | |
377 | } | |
378 | ||
379 | static unsigned long get_devid(uint8_t busnum, uint8_t devnum) | |
380 | { | |
381 | return (busnum << 16) | devnum; | |
382 | } | |
383 | ||
384 | /* will be removed */ | |
385 | int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum, | |
386 | uint8_t devnum, uint32_t speed) | |
387 | { | |
388 | int devid = get_devid(busnum, devnum); | |
389 | ||
390 | return usbip_vhci_attach_device2(port, sockfd, devid, speed); | |
391 | } | |
392 | ||
393 | int usbip_vhci_detach_device(uint8_t port) | |
394 | { | |
a744b7c6 VM |
395 | char detach_attr_path[SYSFS_PATH_MAX]; |
396 | char attr_detach[] = "detach"; | |
0945b4fe | 397 | char buff[200]; /* what size should be ? */ |
a744b7c6 | 398 | const char *path; |
0945b4fe TH |
399 | int ret; |
400 | ||
0945b4fe | 401 | snprintf(buff, sizeof(buff), "%u", port); |
0945b4fe TH |
402 | dbg("writing: %s", buff); |
403 | ||
a744b7c6 VM |
404 | path = udev_device_get_syspath(vhci_driver->hc_device); |
405 | snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s", | |
406 | path, attr_detach); | |
407 | dbg("detach attribute path: %s", detach_attr_path); | |
408 | ||
409 | ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff)); | |
0945b4fe | 410 | if (ret < 0) { |
a744b7c6 | 411 | dbg("write_sysfs_attribute failed"); |
0945b4fe TH |
412 | return -1; |
413 | } | |
414 | ||
25567a39 | 415 | dbg("detached port: %d", port); |
0945b4fe TH |
416 | |
417 | return 0; | |
418 | } | |
ec2ff627 VM |
419 | |
420 | int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev) | |
421 | { | |
422 | char product_name[100]; | |
423 | char host[NI_MAXHOST] = "unknown host"; | |
424 | char serv[NI_MAXSERV] = "unknown port"; | |
425 | char remote_busid[SYSFS_BUS_ID_SIZE]; | |
426 | int ret; | |
427 | int read_record_error = 0; | |
428 | ||
429 | if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED) | |
430 | return 0; | |
431 | ||
a37d70eb MA |
432 | ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv), |
433 | remote_busid); | |
ec2ff627 VM |
434 | if (ret) { |
435 | err("read_record"); | |
436 | read_record_error = 1; | |
437 | } | |
438 | ||
439 | printf("Port %02d: <%s> at %s\n", idev->port, | |
440 | usbip_status_string(idev->status), | |
441 | usbip_speed_string(idev->udev.speed)); | |
442 | ||
443 | usbip_names_get_product(product_name, sizeof(product_name), | |
444 | idev->udev.idVendor, idev->udev.idProduct); | |
445 | ||
446 | printf(" %s\n", product_name); | |
447 | ||
448 | if (!read_record_error) { | |
449 | printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid, | |
450 | host, serv, remote_busid); | |
451 | printf("%10s -> remote bus/dev %03d/%03d\n", " ", | |
452 | idev->busnum, idev->devnum); | |
453 | } else { | |
454 | printf("%10s -> unknown host, remote port and remote busid\n", | |
455 | idev->udev.busid); | |
456 | printf("%10s -> remote bus/dev %03d/%03d\n", " ", | |
457 | idev->busnum, idev->devnum); | |
458 | } | |
459 | ||
460 | return 0; | |
461 | } |