2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
25 #include "usbip_common.h"
26 #include "usbip_host_driver.h"
29 #define PROGNAME "libusbip"
31 struct usbip_host_driver *host_driver;
33 #define SYSFS_OPEN_RETRIES 100
35 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
37 char attrpath[SYSFS_PATH_MAX];
38 struct sysfs_attribute *attr;
42 int retries = SYSFS_OPEN_RETRIES;
44 /* This access is racy!
46 * Just after detach, our driver removes the sysfs
47 * files and recreates them.
49 * We may try and fail to open the usbip_status of
50 * an exported device in the (short) window where
51 * it has been removed and not yet recreated.
53 * This is a bug in the interface. Nothing we can do
54 * except work around it here by polling for the sysfs
55 * usbip_status to reappear.
58 snprintf(attrpath, SYSFS_PATH_MAX, "%s/usbip_status",
62 if (stat(attrpath, &s) == 0)
65 if (errno != ENOENT) {
66 dbg("stat failed: %s", attrpath);
70 usleep(10000); /* 10ms */
75 dbg("usbip_status not ready after %d retries",
77 else if (retries < SYSFS_OPEN_RETRIES)
78 dbg("warning: usbip_status ready after %d retries",
79 SYSFS_OPEN_RETRIES - retries);
81 attr = sysfs_open_attribute(attrpath);
83 dbg("sysfs_open_attribute failed: %s", attrpath);
87 rc = sysfs_read_attribute(attr);
89 dbg("sysfs_read_attribute failed: %s", attrpath);
90 sysfs_close_attribute(attr);
94 value = atoi(attr->value);
96 sysfs_close_attribute(attr);
101 static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)
103 struct usbip_exported_device *edev = NULL;
104 struct usbip_exported_device *edev_old;
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);
120 read_usb_device(edev->sudev, &edev->udev);
122 edev->status = read_attr_usbip_status(&edev->udev);
123 if (edev->status < 0)
126 /* reallocate buffer to include usb interface data */
127 size = sizeof(*edev) + edev->udev.bNumInterfaces *
128 sizeof(struct usbip_usb_interface);
131 edev = realloc(edev, size);
134 dbg("realloc failed");
138 for (i = 0; i < edev->udev.bNumInterfaces; i++)
139 read_usb_interface(&edev->udev, i, &edev->uinf[i]);
143 if (edev && edev->sudev)
144 sysfs_close_device(edev->sudev);
151 static int check_new(struct dlist *dlist, struct sysfs_device *target)
153 struct sysfs_device *dev;
155 dlist_for_each_data(dlist, dev, struct sysfs_device) {
156 if (!strncmp(dev->bus_id, target->bus_id, SYSFS_BUS_ID_SIZE))
157 /* device found and is not new */
163 static void delete_nothing(void *unused_data)
166 * NOTE: Do not delete anything, but the container will be deleted.
171 static int refresh_exported_devices(void)
173 /* sysfs_device of usb_device */
174 struct sysfs_device *sudev;
175 struct dlist *sudev_list;
176 struct dlist *sudev_unique_list;
177 struct usbip_exported_device *edev;
179 sudev_unique_list = dlist_new_with_delete(sizeof(struct sysfs_device),
182 sudev_list = sysfs_get_driver_devices(host_driver->sysfs_driver);
186 * Not an error condition. There are simply no devices bound to
189 dbg("bind " USBIP_HOST_DRV_NAME ".ko to a usb device to be "
194 dlist_for_each_data(sudev_list, sudev, struct sysfs_device)
195 if (check_new(sudev_unique_list, sudev))
196 dlist_unshift(sudev_unique_list, sudev);
198 dlist_for_each_data(sudev_unique_list, sudev, struct sysfs_device) {
199 edev = usbip_exported_device_new(sudev->path);
202 dbg("usbip_exported_device_new failed");
206 dlist_unshift(host_driver->edev_list, edev);
207 host_driver->ndevs++;
210 dlist_destroy(sudev_unique_list);
215 static struct sysfs_driver *open_sysfs_host_driver(void)
217 char bus_type[] = "usb";
218 char sysfs_mntpath[SYSFS_PATH_MAX];
219 char host_drv_path[SYSFS_PATH_MAX];
220 struct sysfs_driver *host_drv;
223 rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
225 dbg("sysfs_get_mnt_path failed");
229 snprintf(host_drv_path, SYSFS_PATH_MAX, "%s/%s/%s/%s/%s",
230 sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
231 USBIP_HOST_DRV_NAME);
233 host_drv = sysfs_open_driver_path(host_drv_path);
235 dbg("sysfs_open_driver_path failed");
242 static void usbip_exported_device_delete(void *dev)
244 struct usbip_exported_device *edev = dev;
245 sysfs_close_device(edev->sudev);
249 int usbip_host_driver_open(void)
253 host_driver = calloc(1, sizeof(*host_driver));
255 dbg("calloc failed");
259 host_driver->ndevs = 0;
260 host_driver->edev_list =
261 dlist_new_with_delete(sizeof(struct usbip_exported_device),
262 usbip_exported_device_delete);
263 if (!host_driver->edev_list) {
264 dbg("dlist_new_with_delete failed");
265 goto err_free_host_driver;
268 host_driver->sysfs_driver = open_sysfs_host_driver();
269 if (!host_driver->sysfs_driver)
270 goto err_destroy_edev_list;
272 rc = refresh_exported_devices();
274 goto err_close_sysfs_driver;
278 err_close_sysfs_driver:
279 sysfs_close_driver(host_driver->sysfs_driver);
280 err_destroy_edev_list:
281 dlist_destroy(host_driver->edev_list);
282 err_free_host_driver:
289 void usbip_host_driver_close(void)
294 if (host_driver->edev_list)
295 dlist_destroy(host_driver->edev_list);
296 if (host_driver->sysfs_driver)
297 sysfs_close_driver(host_driver->sysfs_driver);
303 int usbip_host_refresh_device_list(void)
307 if (host_driver->edev_list)
308 dlist_destroy(host_driver->edev_list);
310 host_driver->ndevs = 0;
311 host_driver->edev_list =
312 dlist_new_with_delete(sizeof(struct usbip_exported_device),
313 usbip_exported_device_delete);
314 if (!host_driver->edev_list) {
315 dbg("dlist_new_with_delete failed");
319 rc = refresh_exported_devices();
326 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
328 char attr_name[] = "usbip_sockfd";
329 char attr_path[SYSFS_PATH_MAX];
330 struct sysfs_attribute *attr;
331 char sockfd_buff[30];
334 if (edev->status != SDEV_ST_AVAILABLE) {
335 dbg("device not available: %s", edev->udev.busid);
336 switch (edev->status) {
338 dbg("status SDEV_ST_ERROR");
341 dbg("status SDEV_ST_USED");
344 dbg("status unknown: 0x%x", edev->status);
349 /* only the first interface is true */
350 snprintf(attr_path, sizeof(attr_path), "%s/%s",
351 edev->udev.path, attr_name);
353 attr = sysfs_open_attribute(attr_path);
355 dbg("sysfs_open_attribute failed: %s", attr_path);
359 snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
360 dbg("write: %s", sockfd_buff);
362 ret = sysfs_write_attribute(attr, sockfd_buff, strlen(sockfd_buff));
364 dbg("sysfs_write_attribute failed: sockfd %s to %s",
365 sockfd_buff, attr_path);
366 goto err_write_sockfd;
369 dbg("connect: %s", edev->udev.busid);
372 sysfs_close_attribute(attr);
377 struct usbip_exported_device *usbip_host_get_device(int num)
379 struct usbip_exported_device *edev;
380 struct dlist *dlist = host_driver->edev_list;
383 dlist_for_each_data(dlist, edev, struct usbip_exported_device) {