blob: f519c73c6d996a43ab1bd24fc4e8696d23f85345 [file] [log] [blame]
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -07001/*
2 * Copyright (C) 2005-2007 Takahiro Hirofuchi
3 */
4
matt mooney099f79f2011-06-19 22:44:37 -07005#include "usbip_common.h"
6#include "vhci_driver.h"
Valentina Maneaec2ff622014-01-07 21:05:56 +02007#include <limits.h>
8#include <netdb.h>
Valentina Manea021aed82014-03-08 14:53:26 +02009#include <libudev.h>
Yuyang Duaa3ecb92017-05-22 18:20:16 +080010#include <dirent.h>
Valentina Maneaa744b7c2014-03-08 14:53:28 +020011#include "sysfs_utils.h"
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070012
matt mooneyf2fb62b2011-06-19 22:44:35 -070013#undef PROGNAME
14#define PROGNAME "libusbip"
15
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070016struct usbip_vhci_driver *vhci_driver;
Valentina Manea021aed82014-03-08 14:53:26 +020017struct udev *udev_context;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070018
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +010019static struct usbip_imported_device *
20imported_device_init(struct usbip_imported_device *idev, char *busid)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070021{
Valentina Manea021aed82014-03-08 14:53:26 +020022 struct udev_device *sudev;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070023
Valentina Manea021aed82014-03-08 14:53:26 +020024 sudev = udev_device_new_from_subsystem_sysname(udev_context,
25 "usb", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070026 if (!sudev) {
Valentina Manea021aed82014-03-08 14:53:26 +020027 dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070028 goto err;
29 }
30 read_usb_device(sudev, &idev->udev);
Valentina Manea021aed82014-03-08 14:53:26 +020031 udev_device_unref(sudev);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070032
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070033 return idev;
34
35err:
36 return NULL;
37}
38
Valentina Maneaa744b7c2014-03-08 14:53:28 +020039static int parse_status(const char *value)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070040{
41 int ret = 0;
42 char *c;
43
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070044 /* skip a header line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -040045 c = strchr(value, '\n');
46 if (!c)
47 return -1;
48 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070049
50 while (*c != '\0') {
51 int port, status, speed, devid;
52 unsigned long socket;
53 char lbusid[SYSFS_BUS_ID_SIZE];
Yuyang Due55dea82017-05-22 18:20:18 +080054 struct usbip_imported_device *idev;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070055
Alan2d329272013-12-11 18:32:59 +000056 ret = sscanf(c, "%d %d %d %x %lx %31s\n",
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070057 &port, &status, &speed,
58 &devid, &socket, lbusid);
59
60 if (ret < 5) {
matt mooney25567a32011-06-19 22:44:38 -070061 dbg("sscanf failed: %d", ret);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070062 BUG();
63 }
64
65 dbg("port %d status %d speed %d devid %x",
66 port, status, speed, devid);
67 dbg("socket %lx lbusid %s", socket, lbusid);
68
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070069 /* if a device is connected, look at it */
Yuyang Due55dea82017-05-22 18:20:18 +080070 idev = &vhci_driver->idev[port];
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070071
Yuyang Due55dea82017-05-22 18:20:18 +080072 memset(idev, 0, sizeof(*idev));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070073
Yuyang Due55dea82017-05-22 18:20:18 +080074 idev->port = port;
75 idev->status = status;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070076
Yuyang Due55dea82017-05-22 18:20:18 +080077 idev->devid = devid;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070078
Yuyang Due55dea82017-05-22 18:20:18 +080079 idev->busnum = (devid >> 16);
80 idev->devnum = (devid & 0x0000ffff);
81
82 if (idev->status != VDEV_ST_NULL
83 && idev->status != VDEV_ST_NOTASSIGNED) {
84 idev = imported_device_init(idev, lbusid);
85 if (!idev) {
86 dbg("imported_device_init failed");
87 return -1;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070088 }
89 }
90
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070091 /* go to the next line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -040092 c = strchr(c, '\n');
93 if (!c)
94 break;
95 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070096 }
97
98 dbg("exit");
99
100 return 0;
101}
102
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800103#define MAX_STATUS_NAME 16
104
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700105static int refresh_imported_device_list(void)
106{
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200107 const char *attr_status;
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800108 char status[MAX_STATUS_NAME+1] = "status";
109 int i, ret;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700110
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800111 for (i = 0; i < vhci_driver->ncontrollers; i++) {
112 if (i > 0)
113 snprintf(status, sizeof(status), "status.%d", i);
114
115 attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
116 status);
117 if (!attr_status) {
118 err("udev_device_get_sysattr_value failed");
119 return -1;
120 }
121
122 dbg("controller %d", i);
123
124 ret = parse_status(attr_status);
125 if (ret != 0)
126 return ret;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700127 }
128
Yuyang Dufd92b7d2017-05-22 18:20:17 +0800129 return 0;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700130}
131
132static int get_nports(void)
133{
Yuyang Du37e47d52017-04-06 06:03:23 +0800134 const char *attr_nports;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700135
Yuyang Du37e47d52017-04-06 06:03:23 +0800136 attr_nports = udev_device_get_sysattr_value(vhci_driver->hc_device, "nports");
137 if (!attr_nports) {
138 err("udev_device_get_sysattr_value nports failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700139 return -1;
140 }
141
Yuyang Du37e47d52017-04-06 06:03:23 +0800142 return (int)strtoul(attr_nports, NULL, 10);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700143}
144
Yuyang Duaa3ecb92017-05-22 18:20:16 +0800145static int vhci_hcd_filter(const struct dirent *dirent)
146{
147 return strcmp(dirent->d_name, "vhci_hcd") >= 0;
148}
149
150static int get_ncontrollers(void)
151{
152 struct dirent **namelist;
153 struct udev_device *platform;
154 int n;
155
156 platform = udev_device_get_parent(vhci_driver->hc_device);
157 if (platform == NULL)
158 return -1;
159
160 n = scandir(udev_device_get_syspath(platform), &namelist, vhci_hcd_filter, NULL);
161 if (n < 0)
162 err("scandir failed");
163 else {
164 for (int i = 0; i < n; i++)
165 free(namelist[i]);
166 free(namelist);
167 }
168
169 return n;
170}
171
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500172/*
173 * Read the given port's record.
174 *
175 * To avoid buffer overflow we will read the entire line and
176 * validate each part's size. The initial buffer is padded by 4 to
177 * accommodate the 2 spaces, 1 newline and an additional character
178 * which is needed to properly validate the 3rd part without it being
179 * truncated to an acceptable length.
180 */
181static int read_record(int rhport, char *host, unsigned long host_len,
182 char *port, unsigned long port_len, char *busid)
Valentina Maneaec2ff622014-01-07 21:05:56 +0200183{
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500184 int part;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200185 FILE *file;
186 char path[PATH_MAX+1];
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500187 char *buffer, *start, *end;
188 char delim[] = {' ', ' ', '\n'};
189 int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE};
190 size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4;
191
192 buffer = malloc(buffer_len);
193 if (!buffer)
194 return -1;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200195
196 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
197
198 file = fopen(path, "r");
199 if (!file) {
200 err("fopen");
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500201 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200202 return -1;
203 }
204
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500205 if (fgets(buffer, buffer_len, file) == NULL) {
206 err("fgets");
207 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200208 fclose(file);
209 return -1;
210 }
Valentina Maneaec2ff622014-01-07 21:05:56 +0200211 fclose(file);
212
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500213 /* validate the length of each of the 3 parts */
214 start = buffer;
215 for (part = 0; part < 3; part++) {
216 end = strchr(start, delim[part]);
217 if (end == NULL || (end - start) > max_len[part]) {
218 free(buffer);
219 return -1;
220 }
221 start = end + 1;
222 }
223
224 if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) {
225 err("sscanf");
226 free(buffer);
227 return -1;
228 }
229
230 free(buffer);
231
Valentina Maneaec2ff622014-01-07 21:05:56 +0200232 return 0;
233}
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700234
235/* ---------------------------------------------------------------------- */
236
237int usbip_vhci_driver_open(void)
238{
Valentina Manea021aed82014-03-08 14:53:26 +0200239 udev_context = udev_new();
240 if (!udev_context) {
241 err("udev_new failed");
242 return -1;
243 }
244
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200245 vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700246
247 /* will be freed in usbip_driver_close() */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200248 vhci_driver->hc_device =
249 udev_device_new_from_subsystem_sysname(udev_context,
250 USBIP_VHCI_BUS_TYPE,
251 USBIP_VHCI_DRV_NAME);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700252 if (!vhci_driver->hc_device) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200253 err("udev_device_new_from_subsystem_sysname failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700254 goto err;
255 }
256
257 vhci_driver->nports = get_nports();
matt mooney25567a32011-06-19 22:44:38 -0700258 dbg("available ports: %d", vhci_driver->nports);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700259
Yuyang Duc3509712017-05-22 18:20:15 +0800260 if (vhci_driver->nports <= 0) {
261 err("no available ports");
262 goto err;
263 } else if (vhci_driver->nports > MAXNPORT) {
264 err("port number exceeds %d", MAXNPORT);
265 goto err;
266 }
267
Yuyang Duaa3ecb92017-05-22 18:20:16 +0800268 vhci_driver->ncontrollers = get_ncontrollers();
269 dbg("available controllers: %d", vhci_driver->ncontrollers);
270
271 if (vhci_driver->ncontrollers <=0) {
272 err("no available usb controllers");
273 goto err;
274 }
275
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700276 if (refresh_imported_device_list())
277 goto err;
278
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700279 return 0;
280
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700281err:
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200282 udev_device_unref(vhci_driver->hc_device);
283
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700284 if (vhci_driver)
285 free(vhci_driver);
286
287 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200288
289 udev_unref(udev_context);
290
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700291 return -1;
292}
293
294
Pawel Lebioda19495512014-05-14 21:28:27 +0200295void usbip_vhci_driver_close(void)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700296{
297 if (!vhci_driver)
298 return;
299
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200300 udev_device_unref(vhci_driver->hc_device);
301
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700302 free(vhci_driver);
303
304 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200305
306 udev_unref(udev_context);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700307}
308
309
310int usbip_vhci_refresh_device_list(void)
311{
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700312
313 if (refresh_imported_device_list())
314 goto err;
315
316 return 0;
317err:
matt mooney25567a32011-06-19 22:44:38 -0700318 dbg("failed to refresh device list");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700319 return -1;
320}
321
322
323int usbip_vhci_get_free_port(void)
324{
325 for (int i = 0; i < vhci_driver->nports; i++) {
326 if (vhci_driver->idev[i].status == VDEV_ST_NULL)
327 return i;
328 }
329
330 return -1;
331}
332
333int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
334 uint32_t speed) {
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700335 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200336 char attach_attr_path[SYSFS_PATH_MAX];
337 char attr_attach[] = "attach";
338 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700339 int ret;
340
Masanari Iida54840812014-02-27 20:36:31 +0900341 snprintf(buff, sizeof(buff), "%u %d %u %u",
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700342 port, sockfd, devid, speed);
343 dbg("writing: %s", buff);
344
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200345 path = udev_device_get_syspath(vhci_driver->hc_device);
346 snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
347 path, attr_attach);
348 dbg("attach attribute path: %s", attach_attr_path);
349
350 ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700351 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200352 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700353 return -1;
354 }
355
matt mooney25567a32011-06-19 22:44:38 -0700356 dbg("attached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700357
358 return 0;
359}
360
361static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
362{
363 return (busnum << 16) | devnum;
364}
365
366/* will be removed */
367int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
368 uint8_t devnum, uint32_t speed)
369{
370 int devid = get_devid(busnum, devnum);
371
372 return usbip_vhci_attach_device2(port, sockfd, devid, speed);
373}
374
375int usbip_vhci_detach_device(uint8_t port)
376{
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200377 char detach_attr_path[SYSFS_PATH_MAX];
378 char attr_detach[] = "detach";
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700379 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200380 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700381 int ret;
382
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700383 snprintf(buff, sizeof(buff), "%u", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700384 dbg("writing: %s", buff);
385
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200386 path = udev_device_get_syspath(vhci_driver->hc_device);
387 snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
388 path, attr_detach);
389 dbg("detach attribute path: %s", detach_attr_path);
390
391 ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700392 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200393 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700394 return -1;
395 }
396
matt mooney25567a32011-06-19 22:44:38 -0700397 dbg("detached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700398
399 return 0;
400}
Valentina Maneaec2ff622014-01-07 21:05:56 +0200401
402int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
403{
404 char product_name[100];
405 char host[NI_MAXHOST] = "unknown host";
406 char serv[NI_MAXSERV] = "unknown port";
407 char remote_busid[SYSFS_BUS_ID_SIZE];
408 int ret;
409 int read_record_error = 0;
410
411 if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
412 return 0;
413
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500414 ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
415 remote_busid);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200416 if (ret) {
417 err("read_record");
418 read_record_error = 1;
419 }
420
421 printf("Port %02d: <%s> at %s\n", idev->port,
422 usbip_status_string(idev->status),
423 usbip_speed_string(idev->udev.speed));
424
425 usbip_names_get_product(product_name, sizeof(product_name),
426 idev->udev.idVendor, idev->udev.idProduct);
427
428 printf(" %s\n", product_name);
429
430 if (!read_record_error) {
431 printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
432 host, serv, remote_busid);
433 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
434 idev->busnum, idev->devnum);
435 } else {
436 printf("%10s -> unknown host, remote port and remote busid\n",
437 idev->udev.busid);
438 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
439 idev->busnum, idev->devnum);
440 }
441
442 return 0;
443}