9 #define PROGNAME "libusbip"
15 struct sysfs_device *sudev;
17 sudev = sysfs_open_device(
"usb", busid);
19 dbg(
"sysfs_open_device failed: %s", busid);
23 sysfs_close_device(sudev);
27 dlist_for_each_data(vhci_driver->
cdev_list, cdev,
34 new_cdev = calloc(1,
sizeof(*new_cdev));
38 memcpy(new_cdev, cdev,
sizeof(*new_cdev));
39 dlist_unshift(idev->
cdev_list, (
void*) new_cdev);
51 static int parse_status(
char *
value)
57 for (
int i = 0;
i < vhci_driver->
nports;
i++)
70 char lbusid[SYSFS_BUS_ID_SIZE];
72 ret =
sscanf(c,
"%d %d %d %x %lx %s\n",
73 &port, &status, &speed,
74 &devid, &socket, lbusid);
77 dbg(
"sscanf failed: %d", ret);
81 dbg(
"port %d status %d speed %d devid %x",
82 port, status, speed, devid);
83 dbg(
"socket %lx lbusid %s", socket, lbusid);
95 idev->
busnum = (devid >> 16);
96 idev->
devnum = (devid & 0x0000ffff);
100 dbg(
"dlist_new failed");
105 idev = imported_device_init(idev, lbusid);
107 dbg(
"imported_device_init failed");
127 static int check_usbip_device(
struct sysfs_class_device *
cdev)
134 snprintf(class_path,
sizeof(class_path),
"%s/device", cdev->path);
136 ret = sysfs_get_link(class_path, dev_path,
sizeof(dev_path));
141 usbip_cdev = calloc(1,
sizeof(*usbip_cdev));
143 dbg(
"calloc failed");
146 dlist_unshift(vhci_driver->
cdev_list, usbip_cdev);
151 dbg(
"found: %s %s", class_path, dev_path);
159 static int search_class_for_usbip_device(
char *
cname)
161 struct sysfs_class *
class;
162 struct dlist *cdev_list;
163 struct sysfs_class_device *
cdev;
166 class = sysfs_open_class(cname);
168 dbg(
"sysfs_open_class failed");
174 cdev_list = sysfs_get_class_devices(
class);
179 dlist_for_each_data(cdev_list, cdev,
struct sysfs_class_device) {
180 dbg(
"cdev: %s", cdev->name);
181 ret = check_usbip_device(cdev);
187 sysfs_close_class(
class);
193 static int refresh_class_device_list(
void)
196 struct dlist *cname_list;
203 dbg(
"sysfs_get_mnt_path failed");
207 snprintf(class_path,
sizeof(class_path),
"%s/%s", sysfs_mntpath,
211 cname_list = sysfs_open_directory_list(class_path);
213 dbg(
"sysfs_open_directory failed");
217 dlist_for_each_data(cname_list, cname,
char) {
218 ret = search_class_for_usbip_device(cname);
220 sysfs_close_list(cname_list);
225 sysfs_close_list(cname_list);
228 ret = search_class_for_usbip_device(SYSFS_BLOCK_NAME);
236 static int refresh_imported_device_list(
void)
238 struct sysfs_attribute *attr_status;
241 attr_status = sysfs_get_device_attr(vhci_driver->
hc_device,
"status");
243 dbg(
"sysfs_get_device_attr(\"status\") failed: %s",
248 dbg(
"name: %s path: %s len: %d method: %d value: %s",
249 attr_status->name, attr_status->path, attr_status->len,
250 attr_status->method, attr_status->value);
252 return parse_status(attr_status->value);
255 static int get_nports(
void)
259 struct sysfs_attribute *attr_status;
261 attr_status = sysfs_get_device_attr(vhci_driver->
hc_device,
"status");
263 dbg(
"sysfs_get_device_attr(\"status\") failed: %s",
268 dbg(
"name: %s path: %s len: %d method: %d value: %s",
269 attr_status->name, attr_status->path, attr_status->len,
270 attr_status->method, attr_status->value);
273 c =
strchr(attr_status->value,
'\n');
290 static int get_hc_busid(
char *sysfs_mntpath,
char *hc_busid)
292 struct sysfs_driver *sdriver;
295 struct sysfs_device *hc_dev;
296 struct dlist *hc_devs;
304 sdriver = sysfs_open_driver_path(sdriver_path);
306 dbg(
"sysfs_open_driver_path failed: %s", sdriver_path);
312 hc_devs = sysfs_get_driver_devices(sdriver);
314 dbg(
"sysfs_get_driver failed");
319 dlist_for_each_data(hc_devs, hc_dev,
struct sysfs_device) {
320 strncpy(hc_busid, hc_dev->bus_id, SYSFS_BUS_ID_SIZE);
325 sysfs_close_driver(sdriver);
330 dbg(
"%s not found", hc_busid);
340 char hc_busid[SYSFS_BUS_ID_SIZE];
344 dbg(
"calloc failed");
350 dbg(
"sysfs_get_mnt_path failed");
362 dbg(
"sysfs_open_device failed");
366 vhci_driver->
nports = get_nports();
368 dbg(
"available ports: %d", vhci_driver->
nports);
374 if (refresh_class_device_list())
377 if (refresh_imported_device_list())
388 sysfs_close_device(vhci_driver->
hc_device);
405 for (
int i = 0;
i < vhci_driver->
nports;
i++) {
406 if (vhci_driver->
idev[
i].cdev_list)
407 dlist_destroy(vhci_driver->
idev[
i].cdev_list);
411 sysfs_close_device(vhci_driver->
hc_device);
424 for (
int i = 0;
i < vhci_driver->
nports;
i++) {
425 if (vhci_driver->
idev[
i].cdev_list)
426 dlist_destroy(vhci_driver->
idev[
i].cdev_list);
433 if (refresh_class_device_list())
436 if (refresh_imported_device_list())
444 for (
int i = 0;
i < vhci_driver->
nports;
i++) {
445 if (vhci_driver->
idev[
i].cdev_list)
446 dlist_destroy(vhci_driver->
idev[
i].cdev_list);
449 dbg(
"failed to refresh device list");
456 for (
int i = 0;
i < vhci_driver->
nports;
i++) {
466 struct sysfs_attribute *attr_attach;
470 attr_attach = sysfs_get_device_attr(vhci_driver->
hc_device,
"attach");
472 dbg(
"sysfs_get_device_attr(\"attach\") failed: %s",
477 snprintf(buff,
sizeof(buff),
"%u %u %u %u",
478 port, sockfd, devid, speed);
479 dbg(
"writing: %s", buff);
481 ret = sysfs_write_attribute(attr_attach, buff,
strlen(buff));
483 dbg(
"sysfs_write_attribute failed");
487 dbg(
"attached port: %d", port);
494 return (busnum << 16) |
devnum;
501 int devid = get_devid(busnum, devnum);
508 struct sysfs_attribute *attr_detach;
512 attr_detach = sysfs_get_device_attr(vhci_driver->
hc_device,
"detach");
514 dbg(
"sysfs_get_device_attr(\"detach\") failed: %s",
519 snprintf(buff,
sizeof(buff),
"%u", port);
520 dbg(
"writing: %s", buff);
522 ret = sysfs_write_attribute(attr_detach, buff,
strlen(buff));
524 dbg(
"sysfs_write_attribute failed");
528 dbg(
"detached port: %d", port);