blob: 5727dfb15a83efecb4ef7dc951149183a38e5108 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -07002/*
3 * Copyright (C) 2005-2007 Takahiro Hirofuchi
4 */
5
matt mooney099f79f2011-06-19 22:44:37 -07006#include "usbip_common.h"
7#include "vhci_driver.h"
Valentina Maneaec2ff622014-01-07 21:05:56 +02008#include <limits.h>
9#include <netdb.h>
Valentina Manea021aed82014-03-08 14:53:26 +020010#include <libudev.h>
Yuyang Duaa3ecb92017-05-22 18:20:16 +080011#include <dirent.h>
Valentina Maneaa744b7c2014-03-08 14:53:28 +020012#include "sysfs_utils.h"
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070013
matt mooneyf2fb62b2011-06-19 22:44:35 -070014#undef PROGNAME
15#define PROGNAME "libusbip"
16
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070017struct usbip_vhci_driver *vhci_driver;
Valentina Manea021aed82014-03-08 14:53:26 +020018struct udev *udev_context;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070019
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +010020static struct usbip_imported_device *
21imported_device_init(struct usbip_imported_device *idev, char *busid)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070022{
Valentina Manea021aed82014-03-08 14:53:26 +020023 struct udev_device *sudev;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070024
Valentina Manea021aed82014-03-08 14:53:26 +020025 sudev = udev_device_new_from_subsystem_sysname(udev_context,
26 "usb", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070027 if (!sudev) {
Valentina Manea021aed82014-03-08 14:53:26 +020028 dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070029 goto err;
30 }
31 read_usb_device(sudev, &idev->udev);
Valentina Manea021aed82014-03-08 14:53:26 +020032 udev_device_unref(sudev);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070033
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070034 return idev;
35
36err:
37 return NULL;
38}
39
Valentina Maneaa744b7c2014-03-08 14:53:28 +020040static int parse_status(const char *value)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070041{
42 int ret = 0;
43 char *c;
44
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070045 /* skip a header line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -040046 c = strchr(value, '\n');
47 if (!c)
48 return -1;
49 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070050
51 while (*c != '\0') {
52 int port, status, speed, devid;
53 unsigned long socket;
54 char lbusid[SYSFS_BUS_ID_SIZE];
Yuyang Due55dea82017-05-22 18:20:18 +080055 struct usbip_imported_device *idev;
Yuyang Du1c9de5b2017-06-08 13:04:10 +080056 char hub[3];
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070057
Yuyang Du1c9de5b2017-06-08 13:04:10 +080058 ret = sscanf(c, "%2s %d %d %d %x %lx %31s\n",
59 hub, &port, &status, &speed,
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070060 &devid, &socket, lbusid);
61
62 if (ret < 5) {
matt mooney25567a32011-06-19 22:44:38 -070063 dbg("sscanf failed: %d", ret);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070064 BUG();
65 }
66
Yuyang Du1c9de5b2017-06-08 13:04:10 +080067 dbg("hub %s port %d status %d speed %d devid %x",
68 hub, port, status, speed, devid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070069 dbg("socket %lx lbusid %s", socket, lbusid);
70
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070071 /* if a device is connected, look at it */
Yuyang Due55dea82017-05-22 18:20:18 +080072 idev = &vhci_driver->idev[port];
Yuyang Due55dea82017-05-22 18:20:18 +080073 memset(idev, 0, sizeof(*idev));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070074
Yuyang Du1c9de5b2017-06-08 13:04:10 +080075 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
Yuyang Due55dea82017-05-22 18:20:18 +080080 idev->port = port;
81 idev->status = status;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070082
Yuyang Due55dea82017-05-22 18:20:18 +080083 idev->devid = devid;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070084
Yuyang Due55dea82017-05-22 18:20:18 +080085 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;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070094 }
95 }
96
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070097 /* go to the next line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -040098 c = strchr(c, '\n');
99 if (!c)
100 break;
101 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700102 }
103
104 dbg("exit");
105
106 return 0;
107}
108
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800109#define MAX_STATUS_NAME 16
110
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700111static int refresh_imported_device_list(void)
112{
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200113 const char *attr_status;
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800114 char status[MAX_STATUS_NAME+1] = "status";
115 int i, ret;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700116
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800117 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 }
127
128 dbg("controller %d", i);
129
130 ret = parse_status(attr_status);
131 if (ret != 0)
132 return ret;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700133 }
134
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800135 return 0;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700136}
137
138static int get_nports(void)
139{
Yuyang Du37e47d52017-04-06 06:03:23 +0800140 const char *attr_nports;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700141
Yuyang Du37e47d52017-04-06 06:03:23 +0800142 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");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700145 return -1;
146 }
147
Yuyang Du37e47d52017-04-06 06:03:23 +0800148 return (int)strtoul(attr_nports, NULL, 10);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700149}
150
Yuyang Duaa3ecb92017-05-22 18:20:16 +0800151static int vhci_hcd_filter(const struct dirent *dirent)
152{
153 return strcmp(dirent->d_name, "vhci_hcd") >= 0;
154}
155
156static 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
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500178/*
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 */
187static int read_record(int rhport, char *host, unsigned long host_len,
188 char *port, unsigned long port_len, char *busid)
Valentina Maneaec2ff622014-01-07 21:05:56 +0200189{
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500190 int part;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200191 FILE *file;
192 char path[PATH_MAX+1];
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500193 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;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200201
202 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
203
204 file = fopen(path, "r");
205 if (!file) {
206 err("fopen");
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500207 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200208 return -1;
209 }
210
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500211 if (fgets(buffer, buffer_len, file) == NULL) {
212 err("fgets");
213 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200214 fclose(file);
215 return -1;
216 }
Valentina Maneaec2ff622014-01-07 21:05:56 +0200217 fclose(file);
218
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500219 /* 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
Valentina Maneaec2ff622014-01-07 21:05:56 +0200238 return 0;
239}
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700240
241/* ---------------------------------------------------------------------- */
242
243int usbip_vhci_driver_open(void)
244{
Valentina Manea021aed82014-03-08 14:53:26 +0200245 udev_context = udev_new();
246 if (!udev_context) {
247 err("udev_new failed");
248 return -1;
249 }
250
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200251 vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700252
253 /* will be freed in usbip_driver_close() */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200254 vhci_driver->hc_device =
255 udev_device_new_from_subsystem_sysname(udev_context,
256 USBIP_VHCI_BUS_TYPE,
Yuyang Dudff35652017-06-08 13:04:08 +0800257 USBIP_VHCI_DEVICE_NAME);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700258 if (!vhci_driver->hc_device) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200259 err("udev_device_new_from_subsystem_sysname failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700260 goto err;
261 }
262
263 vhci_driver->nports = get_nports();
matt mooney25567a32011-06-19 22:44:38 -0700264 dbg("available ports: %d", vhci_driver->nports);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700265
Yuyang Duc3509712017-05-22 18:20:15 +0800266 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
Yuyang Duaa3ecb92017-05-22 18:20:16 +0800274 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
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700282 if (refresh_imported_device_list())
283 goto err;
284
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700285 return 0;
286
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700287err:
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200288 udev_device_unref(vhci_driver->hc_device);
289
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700290 if (vhci_driver)
291 free(vhci_driver);
292
293 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200294
295 udev_unref(udev_context);
296
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700297 return -1;
298}
299
300
Pawel Lebioda19495512014-05-14 21:28:27 +0200301void usbip_vhci_driver_close(void)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700302{
303 if (!vhci_driver)
304 return;
305
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200306 udev_device_unref(vhci_driver->hc_device);
307
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700308 free(vhci_driver);
309
310 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200311
312 udev_unref(udev_context);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700313}
314
315
316int usbip_vhci_refresh_device_list(void)
317{
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700318
319 if (refresh_imported_device_list())
320 goto err;
321
322 return 0;
323err:
matt mooney25567a32011-06-19 22:44:38 -0700324 dbg("failed to refresh device list");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700325 return -1;
326}
327
328
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800329int usbip_vhci_get_free_port(uint32_t speed)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700330{
331 for (int i = 0; i < vhci_driver->nports; i++) {
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800332 if (speed == USB_SPEED_SUPER &&
333 vhci_driver->idev[i].hub != HUB_SPEED_SUPER)
334 continue;
335
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700336 if (vhci_driver->idev[i].status == VDEV_ST_NULL)
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800337 return vhci_driver->idev[i].port;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700338 }
339
340 return -1;
341}
342
343int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
344 uint32_t speed) {
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700345 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200346 char attach_attr_path[SYSFS_PATH_MAX];
347 char attr_attach[] = "attach";
348 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700349 int ret;
350
Masanari Iida54840812014-02-27 20:36:31 +0900351 snprintf(buff, sizeof(buff), "%u %d %u %u",
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700352 port, sockfd, devid, speed);
353 dbg("writing: %s", buff);
354
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200355 path = udev_device_get_syspath(vhci_driver->hc_device);
356 snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
357 path, attr_attach);
358 dbg("attach attribute path: %s", attach_attr_path);
359
360 ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700361 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200362 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700363 return -1;
364 }
365
matt mooney25567a32011-06-19 22:44:38 -0700366 dbg("attached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700367
368 return 0;
369}
370
371static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
372{
373 return (busnum << 16) | devnum;
374}
375
376/* will be removed */
377int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
378 uint8_t devnum, uint32_t speed)
379{
380 int devid = get_devid(busnum, devnum);
381
382 return usbip_vhci_attach_device2(port, sockfd, devid, speed);
383}
384
385int usbip_vhci_detach_device(uint8_t port)
386{
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200387 char detach_attr_path[SYSFS_PATH_MAX];
388 char attr_detach[] = "detach";
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700389 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200390 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700391 int ret;
392
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700393 snprintf(buff, sizeof(buff), "%u", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700394 dbg("writing: %s", buff);
395
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200396 path = udev_device_get_syspath(vhci_driver->hc_device);
397 snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
398 path, attr_detach);
399 dbg("detach attribute path: %s", detach_attr_path);
400
401 ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700402 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200403 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700404 return -1;
405 }
406
matt mooney25567a32011-06-19 22:44:38 -0700407 dbg("detached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700408
409 return 0;
410}
Valentina Maneaec2ff622014-01-07 21:05:56 +0200411
412int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
413{
414 char product_name[100];
415 char host[NI_MAXHOST] = "unknown host";
416 char serv[NI_MAXSERV] = "unknown port";
417 char remote_busid[SYSFS_BUS_ID_SIZE];
418 int ret;
419 int read_record_error = 0;
420
421 if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
422 return 0;
423
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500424 ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
425 remote_busid);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200426 if (ret) {
427 err("read_record");
428 read_record_error = 1;
429 }
430
431 printf("Port %02d: <%s> at %s\n", idev->port,
432 usbip_status_string(idev->status),
433 usbip_speed_string(idev->udev.speed));
434
435 usbip_names_get_product(product_name, sizeof(product_name),
436 idev->udev.idVendor, idev->udev.idProduct);
437
438 printf(" %s\n", product_name);
439
440 if (!read_record_error) {
441 printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
442 host, serv, remote_busid);
443 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
444 idev->busnum, idev->devnum);
445 } else {
446 printf("%10s -> unknown host, remote port and remote busid\n",
447 idev->udev.busid);
448 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
449 idev->busnum, idev->devnum);
450 }
451
452 return 0;
453}