blob: f6f3a19ba385f9ef8347f6a5b91a2b935748b096 [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
39
40
Valentina Maneaa744b7c2014-03-08 14:53:28 +020041static int parse_status(const char *value)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070042{
43 int ret = 0;
44 char *c;
45
46
47 for (int i = 0; i < vhci_driver->nports; i++)
matt mooney950a4cd2011-05-27 01:44:10 -070048 memset(&vhci_driver->idev[i], 0, sizeof(vhci_driver->idev[i]));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070049
50
51 /* skip a header line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -040052 c = strchr(value, '\n');
53 if (!c)
54 return -1;
55 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070056
57 while (*c != '\0') {
58 int port, status, speed, devid;
59 unsigned long socket;
60 char lbusid[SYSFS_BUS_ID_SIZE];
61
Alan2d329272013-12-11 18:32:59 +000062 ret = sscanf(c, "%d %d %d %x %lx %31s\n",
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070063 &port, &status, &speed,
64 &devid, &socket, lbusid);
65
66 if (ret < 5) {
matt mooney25567a32011-06-19 22:44:38 -070067 dbg("sscanf failed: %d", ret);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070068 BUG();
69 }
70
71 dbg("port %d status %d speed %d devid %x",
72 port, status, speed, devid);
73 dbg("socket %lx lbusid %s", socket, lbusid);
74
75
76 /* if a device is connected, look at it */
77 {
78 struct usbip_imported_device *idev = &vhci_driver->idev[port];
79
80 idev->port = port;
81 idev->status = status;
82
83 idev->devid = devid;
84
85 idev->busnum = (devid >> 16);
86 idev->devnum = (devid & 0x0000ffff);
87
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +010088 if (idev->status != VDEV_ST_NULL
89 && idev->status != VDEV_ST_NOTASSIGNED) {
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070090 idev = imported_device_init(idev, lbusid);
91 if (!idev) {
matt mooney25567a32011-06-19 22:44:38 -070092 dbg("imported_device_init failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070093 return -1;
94 }
95 }
96 }
97
98
99 /* go to the next line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -0400100 c = strchr(c, '\n');
101 if (!c)
102 break;
103 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700104 }
105
106 dbg("exit");
107
108 return 0;
109}
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;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700114
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200115 attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
116 "status");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700117 if (!attr_status) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200118 err("udev_device_get_sysattr_value failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700119 return -1;
120 }
121
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200122 return parse_status(attr_status);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700123}
124
125static int get_nports(void)
126{
Yuyang Du37e47d52017-04-06 06:03:23 +0800127 const char *attr_nports;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700128
Yuyang Du37e47d52017-04-06 06:03:23 +0800129 attr_nports = udev_device_get_sysattr_value(vhci_driver->hc_device, "nports");
130 if (!attr_nports) {
131 err("udev_device_get_sysattr_value nports failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700132 return -1;
133 }
134
Yuyang Du37e47d52017-04-06 06:03:23 +0800135 return (int)strtoul(attr_nports, NULL, 10);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700136}
137
Yuyang Duaa3ecb92017-05-22 18:20:16 +0800138static int vhci_hcd_filter(const struct dirent *dirent)
139{
140 return strcmp(dirent->d_name, "vhci_hcd") >= 0;
141}
142
143static int get_ncontrollers(void)
144{
145 struct dirent **namelist;
146 struct udev_device *platform;
147 int n;
148
149 platform = udev_device_get_parent(vhci_driver->hc_device);
150 if (platform == NULL)
151 return -1;
152
153 n = scandir(udev_device_get_syspath(platform), &namelist, vhci_hcd_filter, NULL);
154 if (n < 0)
155 err("scandir failed");
156 else {
157 for (int i = 0; i < n; i++)
158 free(namelist[i]);
159 free(namelist);
160 }
161
162 return n;
163}
164
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500165/*
166 * Read the given port's record.
167 *
168 * To avoid buffer overflow we will read the entire line and
169 * validate each part's size. The initial buffer is padded by 4 to
170 * accommodate the 2 spaces, 1 newline and an additional character
171 * which is needed to properly validate the 3rd part without it being
172 * truncated to an acceptable length.
173 */
174static int read_record(int rhport, char *host, unsigned long host_len,
175 char *port, unsigned long port_len, char *busid)
Valentina Maneaec2ff622014-01-07 21:05:56 +0200176{
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500177 int part;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200178 FILE *file;
179 char path[PATH_MAX+1];
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500180 char *buffer, *start, *end;
181 char delim[] = {' ', ' ', '\n'};
182 int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE};
183 size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4;
184
185 buffer = malloc(buffer_len);
186 if (!buffer)
187 return -1;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200188
189 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
190
191 file = fopen(path, "r");
192 if (!file) {
193 err("fopen");
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500194 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200195 return -1;
196 }
197
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500198 if (fgets(buffer, buffer_len, file) == NULL) {
199 err("fgets");
200 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200201 fclose(file);
202 return -1;
203 }
Valentina Maneaec2ff622014-01-07 21:05:56 +0200204 fclose(file);
205
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500206 /* validate the length of each of the 3 parts */
207 start = buffer;
208 for (part = 0; part < 3; part++) {
209 end = strchr(start, delim[part]);
210 if (end == NULL || (end - start) > max_len[part]) {
211 free(buffer);
212 return -1;
213 }
214 start = end + 1;
215 }
216
217 if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) {
218 err("sscanf");
219 free(buffer);
220 return -1;
221 }
222
223 free(buffer);
224
Valentina Maneaec2ff622014-01-07 21:05:56 +0200225 return 0;
226}
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700227
228/* ---------------------------------------------------------------------- */
229
230int usbip_vhci_driver_open(void)
231{
Valentina Manea021aed82014-03-08 14:53:26 +0200232 udev_context = udev_new();
233 if (!udev_context) {
234 err("udev_new failed");
235 return -1;
236 }
237
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200238 vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700239
240 /* will be freed in usbip_driver_close() */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200241 vhci_driver->hc_device =
242 udev_device_new_from_subsystem_sysname(udev_context,
243 USBIP_VHCI_BUS_TYPE,
244 USBIP_VHCI_DRV_NAME);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700245 if (!vhci_driver->hc_device) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200246 err("udev_device_new_from_subsystem_sysname failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700247 goto err;
248 }
249
250 vhci_driver->nports = get_nports();
matt mooney25567a32011-06-19 22:44:38 -0700251 dbg("available ports: %d", vhci_driver->nports);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700252
Yuyang Duc3509712017-05-22 18:20:15 +0800253 if (vhci_driver->nports <= 0) {
254 err("no available ports");
255 goto err;
256 } else if (vhci_driver->nports > MAXNPORT) {
257 err("port number exceeds %d", MAXNPORT);
258 goto err;
259 }
260
Yuyang Duaa3ecb92017-05-22 18:20:16 +0800261 vhci_driver->ncontrollers = get_ncontrollers();
262 dbg("available controllers: %d", vhci_driver->ncontrollers);
263
264 if (vhci_driver->ncontrollers <=0) {
265 err("no available usb controllers");
266 goto err;
267 }
268
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700269 if (refresh_imported_device_list())
270 goto err;
271
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700272 return 0;
273
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700274err:
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200275 udev_device_unref(vhci_driver->hc_device);
276
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700277 if (vhci_driver)
278 free(vhci_driver);
279
280 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200281
282 udev_unref(udev_context);
283
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700284 return -1;
285}
286
287
Pawel Lebioda19495512014-05-14 21:28:27 +0200288void usbip_vhci_driver_close(void)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700289{
290 if (!vhci_driver)
291 return;
292
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200293 udev_device_unref(vhci_driver->hc_device);
294
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700295 free(vhci_driver);
296
297 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200298
299 udev_unref(udev_context);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700300}
301
302
303int usbip_vhci_refresh_device_list(void)
304{
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700305
306 if (refresh_imported_device_list())
307 goto err;
308
309 return 0;
310err:
matt mooney25567a32011-06-19 22:44:38 -0700311 dbg("failed to refresh device list");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700312 return -1;
313}
314
315
316int usbip_vhci_get_free_port(void)
317{
318 for (int i = 0; i < vhci_driver->nports; i++) {
319 if (vhci_driver->idev[i].status == VDEV_ST_NULL)
320 return i;
321 }
322
323 return -1;
324}
325
326int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
327 uint32_t speed) {
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700328 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200329 char attach_attr_path[SYSFS_PATH_MAX];
330 char attr_attach[] = "attach";
331 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700332 int ret;
333
Masanari Iida54840812014-02-27 20:36:31 +0900334 snprintf(buff, sizeof(buff), "%u %d %u %u",
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700335 port, sockfd, devid, speed);
336 dbg("writing: %s", buff);
337
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200338 path = udev_device_get_syspath(vhci_driver->hc_device);
339 snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
340 path, attr_attach);
341 dbg("attach attribute path: %s", attach_attr_path);
342
343 ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700344 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200345 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700346 return -1;
347 }
348
matt mooney25567a32011-06-19 22:44:38 -0700349 dbg("attached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700350
351 return 0;
352}
353
354static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
355{
356 return (busnum << 16) | devnum;
357}
358
359/* will be removed */
360int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
361 uint8_t devnum, uint32_t speed)
362{
363 int devid = get_devid(busnum, devnum);
364
365 return usbip_vhci_attach_device2(port, sockfd, devid, speed);
366}
367
368int usbip_vhci_detach_device(uint8_t port)
369{
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200370 char detach_attr_path[SYSFS_PATH_MAX];
371 char attr_detach[] = "detach";
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700372 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200373 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700374 int ret;
375
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700376 snprintf(buff, sizeof(buff), "%u", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700377 dbg("writing: %s", buff);
378
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200379 path = udev_device_get_syspath(vhci_driver->hc_device);
380 snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
381 path, attr_detach);
382 dbg("detach attribute path: %s", detach_attr_path);
383
384 ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700385 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200386 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700387 return -1;
388 }
389
matt mooney25567a32011-06-19 22:44:38 -0700390 dbg("detached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700391
392 return 0;
393}
Valentina Maneaec2ff622014-01-07 21:05:56 +0200394
395int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
396{
397 char product_name[100];
398 char host[NI_MAXHOST] = "unknown host";
399 char serv[NI_MAXSERV] = "unknown port";
400 char remote_busid[SYSFS_BUS_ID_SIZE];
401 int ret;
402 int read_record_error = 0;
403
404 if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
405 return 0;
406
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500407 ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
408 remote_busid);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200409 if (ret) {
410 err("read_record");
411 read_record_error = 1;
412 }
413
414 printf("Port %02d: <%s> at %s\n", idev->port,
415 usbip_status_string(idev->status),
416 usbip_speed_string(idev->udev.speed));
417
418 usbip_names_get_product(product_name, sizeof(product_name),
419 idev->udev.idVendor, idev->udev.idProduct);
420
421 printf(" %s\n", product_name);
422
423 if (!read_record_error) {
424 printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
425 host, serv, remote_busid);
426 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
427 idev->busnum, idev->devnum);
428 } else {
429 printf("%10s -> unknown host, remote port and remote busid\n",
430 idev->udev.busid);
431 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
432 idev->busnum, idev->devnum);
433 }
434
435 return 0;
436}