19 #include <sys/types.h>
29 #define PROGNAME "libusbip"
33 #define SYSFS_OPEN_RETRIES 100
39 struct sysfs_attribute *
attr;
63 if (
stat(attrpath, &s) == 0)
67 dbg(
"stat failed: %s", attrpath);
76 dbg(
"usbip_status not ready after %d retries",
79 dbg(
"warning: usbip_status ready after %d retries",
82 attr = sysfs_open_attribute(attrpath);
84 dbg(
"sysfs_open_attribute failed: %s", attrpath);
88 rc = sysfs_read_attribute(attr);
90 dbg(
"sysfs_read_attribute failed: %s", attrpath);
91 sysfs_close_attribute(attr);
95 value = atoi(attr->value);
97 sysfs_close_attribute(attr);
108 edev = calloc(1,
sizeof(*edev));
110 dbg(
"calloc failed");
114 edev->
sudev = sysfs_open_device_path(sdevpath);
116 dbg(
"sysfs_open_device_path failed: %s", sdevpath);
122 edev->
status = read_attr_usbip_status(&edev->
udev);
127 size =
sizeof(*edev) + edev->
udev.bNumInterfaces *
130 edev = realloc(edev, size);
132 dbg(
"realloc failed");
136 for (i = 0; i < edev->
udev.bNumInterfaces; i++)
141 if (edev && edev->
sudev)
142 sysfs_close_device(edev->
sudev);
149 static int check_new(
struct dlist *dlist,
struct sysfs_device *
target)
151 struct sysfs_device *
dev;
153 dlist_for_each_data(dlist, dev,
struct sysfs_device) {
154 if (!
strncmp(dev->bus_id, target->bus_id, SYSFS_BUS_ID_SIZE))
161 static void delete_nothing(
void *unused_data)
169 static int refresh_exported_devices(
void)
172 struct sysfs_device *suintf;
173 struct dlist *suintf_list;
175 struct sysfs_device *sudev;
176 struct dlist *sudev_list;
179 sudev_list = dlist_new_with_delete(
sizeof(
struct sysfs_device),
182 suintf_list = sysfs_get_driver_devices(host_driver->
sysfs_driver);
194 dlist_for_each_data(suintf_list, suintf,
struct sysfs_device) {
196 sudev = sysfs_get_device_parent(suintf);
198 dbg(
"sysfs_get_device_parent failed: %s", suintf->name);
202 if (check_new(sudev_list, sudev)) {
204 dlist_unshift(sudev_list, sudev);
208 dlist_for_each_data(sudev_list, sudev,
struct sysfs_device) {
209 edev = usbip_exported_device_new(sudev->path);
211 dbg(
"usbip_exported_device_new failed");
215 dlist_unshift(host_driver->
edev_list, edev);
216 host_driver->
ndevs++;
219 dlist_destroy(sudev_list);
224 static struct sysfs_driver *open_sysfs_host_driver(
void)
229 struct sysfs_driver *host_drv;
234 dbg(
"sysfs_get_mnt_path failed");
239 sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
242 host_drv = sysfs_open_driver_path(host_drv_path);
244 dbg(
"sysfs_open_driver_path failed");
251 static void usbip_exported_device_delete(
void *dev)
254 sysfs_close_device(edev->
sudev);
262 host_driver = calloc(1,
sizeof(*host_driver));
264 dbg(
"calloc failed");
268 host_driver->
ndevs = 0;
271 usbip_exported_device_delete);
273 dbg(
"dlist_new_with_delete failed");
274 goto err_free_host_driver;
279 goto err_destroy_edev_list;
281 rc = refresh_exported_devices();
283 goto err_close_sysfs_driver;
287 err_close_sysfs_driver:
289 err_destroy_edev_list:
291 err_free_host_driver:
319 host_driver->
ndevs = 0;
322 usbip_exported_device_delete);
324 dbg(
"dlist_new_with_delete failed");
328 rc = refresh_exported_devices();
339 struct sysfs_attribute *
attr;
340 char sockfd_buff[30];
344 dbg(
"device not available: %s", edev->
udev.busid);
347 dbg(
"status SDEV_ST_ERROR");
350 dbg(
"status SDEV_ST_USED");
353 dbg(
"status unknown: 0x%x", edev->
status);
359 snprintf(attr_path,
sizeof(attr_path),
"%s/%s:%d.%d/%s",
361 edev->
udev.bConfigurationValue, 0, attr_name);
363 attr = sysfs_open_attribute(attr_path);
365 dbg(
"sysfs_open_attribute failed: %s", attr_path);
369 snprintf(sockfd_buff,
sizeof(sockfd_buff),
"%d\n", sockfd);
370 dbg(
"write: %s", sockfd_buff);
372 ret = sysfs_write_attribute(attr, sockfd_buff,
strlen(sockfd_buff));
374 dbg(
"sysfs_write_attribute failed: sockfd %s to %s",
375 sockfd_buff, attr_path);
376 goto err_write_sockfd;
379 dbg(
"connect: %s", edev->
udev.busid);
382 sysfs_close_attribute(attr);
390 struct dlist *dlist = host_driver->
edev_list;