blob: 1274f326242ca73b1bf98f4ff8b146a266aaa2df [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>
Valentina Maneaa744b7c2014-03-08 14:53:28 +020010#include "sysfs_utils.h"
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070011
matt mooneyf2fb62b2011-06-19 22:44:35 -070012#undef PROGNAME
13#define PROGNAME "libusbip"
14
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070015struct usbip_vhci_driver *vhci_driver;
Valentina Manea021aed82014-03-08 14:53:26 +020016struct udev *udev_context;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070017
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +010018static struct usbip_imported_device *
19imported_device_init(struct usbip_imported_device *idev, char *busid)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070020{
Valentina Manea021aed82014-03-08 14:53:26 +020021 struct udev_device *sudev;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070022
Valentina Manea021aed82014-03-08 14:53:26 +020023 sudev = udev_device_new_from_subsystem_sysname(udev_context,
24 "usb", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070025 if (!sudev) {
Valentina Manea021aed82014-03-08 14:53:26 +020026 dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070027 goto err;
28 }
29 read_usb_device(sudev, &idev->udev);
Valentina Manea021aed82014-03-08 14:53:26 +020030 udev_device_unref(sudev);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070031
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070032 return idev;
33
34err:
35 return NULL;
36}
37
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
45
46 for (int i = 0; i < vhci_driver->nports; i++)
matt mooney950a4cd2011-05-27 01:44:10 -070047 memset(&vhci_driver->idev[i], 0, sizeof(vhci_driver->idev[i]));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070048
49
50 /* skip a header line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -040051 c = strchr(value, '\n');
52 if (!c)
53 return -1;
54 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070055
56 while (*c != '\0') {
57 int port, status, speed, devid;
Shuah Khance601a02017-12-07 14:16:49 -070058 int sockfd;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070059 char lbusid[SYSFS_BUS_ID_SIZE];
60
Shuah Khance601a02017-12-07 14:16:49 -070061 ret = sscanf(c, "%d %d %d %x %u %31s\n",
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070062 &port, &status, &speed,
Shuah Khance601a02017-12-07 14:16:49 -070063 &devid, &sockfd, lbusid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070064
65 if (ret < 5) {
matt mooney25567a32011-06-19 22:44:38 -070066 dbg("sscanf failed: %d", ret);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070067 BUG();
68 }
69
70 dbg("port %d status %d speed %d devid %x",
71 port, status, speed, devid);
Shuah Khance601a02017-12-07 14:16:49 -070072 dbg("sockfd %u lbusid %s", sockfd, lbusid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070073
74
75 /* if a device is connected, look at it */
76 {
77 struct usbip_imported_device *idev = &vhci_driver->idev[port];
78
79 idev->port = port;
80 idev->status = status;
81
82 idev->devid = devid;
83
84 idev->busnum = (devid >> 16);
85 idev->devnum = (devid & 0x0000ffff);
86
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +010087 if (idev->status != VDEV_ST_NULL
88 && idev->status != VDEV_ST_NOTASSIGNED) {
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070089 idev = imported_device_init(idev, lbusid);
90 if (!idev) {
matt mooney25567a32011-06-19 22:44:38 -070091 dbg("imported_device_init failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070092 return -1;
93 }
94 }
95 }
96
97
98 /* go to the next line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -040099 c = strchr(c, '\n');
100 if (!c)
101 break;
102 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700103 }
104
105 dbg("exit");
106
107 return 0;
108}
109
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700110static int refresh_imported_device_list(void)
111{
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200112 const char *attr_status;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700113
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200114 attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
115 "status");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700116 if (!attr_status) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200117 err("udev_device_get_sysattr_value failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700118 return -1;
119 }
120
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200121 return parse_status(attr_status);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700122}
123
124static int get_nports(void)
125{
matt mooney25567a32011-06-19 22:44:38 -0700126 char *c;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700127 int nports = 0;
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200128 const char *attr_status;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700129
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200130 attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
131 "status");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700132 if (!attr_status) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200133 err("udev_device_get_sysattr_value failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700134 return -1;
135 }
136
matt mooney25567a32011-06-19 22:44:38 -0700137 /* skip a header line */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200138 c = strchr(attr_status, '\n');
Christopher Harvey2f5c6382012-03-23 10:55:25 -0400139 if (!c)
140 return 0;
141 c++;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700142
matt mooney25567a32011-06-19 22:44:38 -0700143 while (*c != '\0') {
144 /* go to the next line */
Christopher Harvey2f5c6382012-03-23 10:55:25 -0400145 c = strchr(c, '\n');
146 if (!c)
147 return nports;
148 c++;
matt mooney25567a32011-06-19 22:44:38 -0700149 nports += 1;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700150 }
151
152 return nports;
153}
154
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500155/*
156 * Read the given port's record.
157 *
158 * To avoid buffer overflow we will read the entire line and
159 * validate each part's size. The initial buffer is padded by 4 to
160 * accommodate the 2 spaces, 1 newline and an additional character
161 * which is needed to properly validate the 3rd part without it being
162 * truncated to an acceptable length.
163 */
164static int read_record(int rhport, char *host, unsigned long host_len,
165 char *port, unsigned long port_len, char *busid)
Valentina Maneaec2ff622014-01-07 21:05:56 +0200166{
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500167 int part;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200168 FILE *file;
169 char path[PATH_MAX+1];
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500170 char *buffer, *start, *end;
171 char delim[] = {' ', ' ', '\n'};
172 int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE};
173 size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4;
174
175 buffer = malloc(buffer_len);
176 if (!buffer)
177 return -1;
Valentina Maneaec2ff622014-01-07 21:05:56 +0200178
179 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
180
181 file = fopen(path, "r");
182 if (!file) {
183 err("fopen");
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500184 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200185 return -1;
186 }
187
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500188 if (fgets(buffer, buffer_len, file) == NULL) {
189 err("fgets");
190 free(buffer);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200191 fclose(file);
192 return -1;
193 }
Valentina Maneaec2ff622014-01-07 21:05:56 +0200194 fclose(file);
195
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500196 /* validate the length of each of the 3 parts */
197 start = buffer;
198 for (part = 0; part < 3; part++) {
199 end = strchr(start, delim[part]);
200 if (end == NULL || (end - start) > max_len[part]) {
201 free(buffer);
202 return -1;
203 }
204 start = end + 1;
205 }
206
207 if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) {
208 err("sscanf");
209 free(buffer);
210 return -1;
211 }
212
213 free(buffer);
214
Valentina Maneaec2ff622014-01-07 21:05:56 +0200215 return 0;
216}
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700217
218/* ---------------------------------------------------------------------- */
219
220int usbip_vhci_driver_open(void)
221{
Valentina Manea021aed82014-03-08 14:53:26 +0200222 udev_context = udev_new();
223 if (!udev_context) {
224 err("udev_new failed");
225 return -1;
226 }
227
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200228 vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700229
230 /* will be freed in usbip_driver_close() */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200231 vhci_driver->hc_device =
232 udev_device_new_from_subsystem_sysname(udev_context,
233 USBIP_VHCI_BUS_TYPE,
234 USBIP_VHCI_DRV_NAME);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700235 if (!vhci_driver->hc_device) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200236 err("udev_device_new_from_subsystem_sysname failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700237 goto err;
238 }
239
240 vhci_driver->nports = get_nports();
241
matt mooney25567a32011-06-19 22:44:38 -0700242 dbg("available ports: %d", vhci_driver->nports);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700243
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700244 if (refresh_imported_device_list())
245 goto err;
246
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700247 return 0;
248
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700249err:
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200250 udev_device_unref(vhci_driver->hc_device);
251
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700252 if (vhci_driver)
253 free(vhci_driver);
254
255 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200256
257 udev_unref(udev_context);
258
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700259 return -1;
260}
261
262
Pawel Lebioda19495512014-05-14 21:28:27 +0200263void usbip_vhci_driver_close(void)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700264{
265 if (!vhci_driver)
266 return;
267
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200268 udev_device_unref(vhci_driver->hc_device);
269
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700270 free(vhci_driver);
271
272 vhci_driver = NULL;
Valentina Manea021aed82014-03-08 14:53:26 +0200273
274 udev_unref(udev_context);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700275}
276
277
278int usbip_vhci_refresh_device_list(void)
279{
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700280
281 if (refresh_imported_device_list())
282 goto err;
283
284 return 0;
285err:
matt mooney25567a32011-06-19 22:44:38 -0700286 dbg("failed to refresh device list");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700287 return -1;
288}
289
290
291int usbip_vhci_get_free_port(void)
292{
293 for (int i = 0; i < vhci_driver->nports; i++) {
294 if (vhci_driver->idev[i].status == VDEV_ST_NULL)
295 return i;
296 }
297
298 return -1;
299}
300
301int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
302 uint32_t speed) {
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700303 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200304 char attach_attr_path[SYSFS_PATH_MAX];
305 char attr_attach[] = "attach";
306 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700307 int ret;
308
Masanari Iida54840812014-02-27 20:36:31 +0900309 snprintf(buff, sizeof(buff), "%u %d %u %u",
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700310 port, sockfd, devid, speed);
311 dbg("writing: %s", buff);
312
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200313 path = udev_device_get_syspath(vhci_driver->hc_device);
314 snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
315 path, attr_attach);
316 dbg("attach attribute path: %s", attach_attr_path);
317
318 ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700319 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200320 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700321 return -1;
322 }
323
matt mooney25567a32011-06-19 22:44:38 -0700324 dbg("attached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700325
326 return 0;
327}
328
329static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
330{
331 return (busnum << 16) | devnum;
332}
333
334/* will be removed */
335int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
336 uint8_t devnum, uint32_t speed)
337{
338 int devid = get_devid(busnum, devnum);
339
340 return usbip_vhci_attach_device2(port, sockfd, devid, speed);
341}
342
343int usbip_vhci_detach_device(uint8_t port)
344{
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200345 char detach_attr_path[SYSFS_PATH_MAX];
346 char attr_detach[] = "detach";
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700347 char buff[200]; /* what size should be ? */
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200348 const char *path;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700349 int ret;
350
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700351 snprintf(buff, sizeof(buff), "%u", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700352 dbg("writing: %s", buff);
353
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200354 path = udev_device_get_syspath(vhci_driver->hc_device);
355 snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
356 path, attr_detach);
357 dbg("detach attribute path: %s", detach_attr_path);
358
359 ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff));
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700360 if (ret < 0) {
Valentina Maneaa744b7c2014-03-08 14:53:28 +0200361 dbg("write_sysfs_attribute failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700362 return -1;
363 }
364
matt mooney25567a32011-06-19 22:44:38 -0700365 dbg("detached port: %d", port);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700366
367 return 0;
368}
Valentina Maneaec2ff622014-01-07 21:05:56 +0200369
370int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
371{
372 char product_name[100];
373 char host[NI_MAXHOST] = "unknown host";
374 char serv[NI_MAXSERV] = "unknown port";
375 char remote_busid[SYSFS_BUS_ID_SIZE];
376 int ret;
377 int read_record_error = 0;
378
379 if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
380 return 0;
381
Mark Asselstinea37d70e2014-02-12 21:47:56 -0500382 ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
383 remote_busid);
Valentina Maneaec2ff622014-01-07 21:05:56 +0200384 if (ret) {
385 err("read_record");
386 read_record_error = 1;
387 }
388
389 printf("Port %02d: <%s> at %s\n", idev->port,
390 usbip_status_string(idev->status),
391 usbip_speed_string(idev->udev.speed));
392
393 usbip_names_get_product(product_name, sizeof(product_name),
394 idev->udev.idVendor, idev->udev.idProduct);
395
396 printf(" %s\n", product_name);
397
398 if (!read_record_error) {
399 printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
400 host, serv, remote_busid);
401 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
402 idev->busnum, idev->devnum);
403 } else {
404 printf("%10s -> unknown host, remote port and remote busid\n",
405 idev->udev.busid);
406 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
407 idev->busnum, idev->devnum);
408 }
409
410 return 0;
411}