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>
28 #include "usbip_common.h"
29 #include "usbip_host_driver.h"
31 #include "sysfs_utils.h"
34 #define PROGNAME "libusbip"
36 struct usbip_host_driver *host_driver;
37 struct udev *udev_context;
39 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
41 char status_attr_path[SYSFS_PATH_MAX];
47 snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
50 fd = open(status_attr_path, O_RDONLY);
52 err("error opening attribute %s", status_attr_path);
56 length = read(fd, &status, 1);
58 err("error reading attribute %s", status_attr_path);
63 value = atoi(&status);
69 struct usbip_exported_device *usbip_exported_device_new(const char *sdevpath)
71 struct usbip_exported_device *edev = NULL;
72 struct usbip_exported_device *edev_old;
76 edev = calloc(1, sizeof(struct usbip_exported_device));
78 edev->sudev = udev_device_new_from_syspath(udev_context, sdevpath);
80 err("udev_device_new_from_syspath: %s", sdevpath);
84 read_usb_device(edev->sudev, &edev->udev);
86 edev->status = read_attr_usbip_status(&edev->udev);
90 /* reallocate buffer to include usb interface data */
91 size = sizeof(struct usbip_exported_device) +
92 edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
95 edev = realloc(edev, size);
98 dbg("realloc failed");
102 for (i = 0; i < edev->udev.bNumInterfaces; i++)
103 read_usb_interface(&edev->udev, i, &edev->uinf[i]);
108 udev_device_unref(edev->sudev);
115 static int refresh_exported_devices(void)
117 struct usbip_exported_device *edev;
118 struct udev_enumerate *enumerate;
119 struct udev_list_entry *devices, *dev_list_entry;
120 struct udev_device *dev;
124 enumerate = udev_enumerate_new(udev_context);
125 udev_enumerate_add_match_subsystem(enumerate, "usb");
126 udev_enumerate_scan_devices(enumerate);
128 devices = udev_enumerate_get_list_entry(enumerate);
130 udev_list_entry_foreach(dev_list_entry, devices) {
131 path = udev_list_entry_get_name(dev_list_entry);
132 dev = udev_device_new_from_syspath(udev_context, path);
136 /* Check whether device uses usbip-host driver. */
137 driver = udev_device_get_driver(dev);
138 if (driver != NULL && !strcmp(driver, USBIP_HOST_DRV_NAME)) {
139 edev = usbip_exported_device_new(path);
141 dbg("usbip_exported_device_new failed");
145 list_add(&edev->node, &host_driver->edev_list);
146 host_driver->ndevs++;
153 static void usbip_exported_device_destroy(void)
155 struct list_head *i, *tmp;
156 struct usbip_exported_device *edev;
158 list_for_each_safe(i, tmp, &host_driver->edev_list) {
159 edev = list_entry(i, struct usbip_exported_device, node);
165 int usbip_host_driver_open(void)
169 udev_context = udev_new();
171 err("udev_new failed");
175 host_driver = calloc(1, sizeof(*host_driver));
177 host_driver->ndevs = 0;
178 INIT_LIST_HEAD(&host_driver->edev_list);
180 rc = refresh_exported_devices();
182 goto err_free_host_driver;
186 err_free_host_driver:
190 udev_unref(udev_context);
195 void usbip_host_driver_close(void)
200 usbip_exported_device_destroy();
205 udev_unref(udev_context);
208 int usbip_host_refresh_device_list(void)
212 usbip_exported_device_destroy();
214 host_driver->ndevs = 0;
215 INIT_LIST_HEAD(&host_driver->edev_list);
217 rc = refresh_exported_devices();
224 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
226 char attr_name[] = "usbip_sockfd";
227 char sockfd_attr_path[SYSFS_PATH_MAX];
228 char sockfd_buff[30];
231 if (edev->status != SDEV_ST_AVAILABLE) {
232 dbg("device not available: %s", edev->udev.busid);
233 switch (edev->status) {
235 dbg("status SDEV_ST_ERROR");
238 dbg("status SDEV_ST_USED");
241 dbg("status unknown: 0x%x", edev->status);
246 /* only the first interface is true */
247 snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
248 edev->udev.path, attr_name);
250 snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
252 ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
253 strlen(sockfd_buff));
255 err("write_sysfs_attribute failed: sockfd %s to %s",
256 sockfd_buff, sockfd_attr_path);
260 info("connect: %s", edev->udev.busid);
265 struct usbip_exported_device *usbip_host_get_device(int num)
268 struct usbip_exported_device *edev;
271 list_for_each(i, &host_driver->edev_list) {
272 edev = list_entry(i, struct usbip_exported_device, node);