| 1 | |
| 2 | |
| 3 | * Copyright (C) 2015-2016 Samsung Electronics
|
| 4 | * Igor Kotrasinski <[email protected]>
|
| 5 | * Krzysztof Opasiak <[email protected]>
|
| 6 | *
|
| 7 | * Refactored from usbip_host_driver.c, which is:
|
| 8 | * Copyright (C) 2011 matt mooney <[email protected]>
|
| 9 | * 2005-2007 Takahiro Hirofuchi
|
| 10 |
|
| 11 |
|
| 12 | #include <sys/types.h>
|
| 13 | #include <sys/stat.h>
|
| 14 | #include <fcntl.h>
|
| 15 |
|
| 16 | #include <errno.h>
|
| 17 | #include <unistd.h>
|
| 18 |
|
| 19 | #include <libudev.h>
|
| 20 |
|
| 21 | #include "usbip_common.h"
|
| 22 | #include "usbip_host_common.h"
|
| 23 | #include "list.h"
|
| 24 | #include "sysfs_utils.h"
|
| 25 |
|
| 26 | extern struct udev *udev_context;
|
| 27 |
|
| 28 | static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
|
| 29 | {
|
| 30 | char status_attr_path[SYSFS_PATH_MAX];
|
| 31 | int size;
|
| 32 | int fd;
|
| 33 | int length;
|
| 34 | char status[2] = { 0 };
|
| 35 | int value = 0;
|
| 36 |
|
| 37 | size = snprintf(status_attr_path, sizeof(status_attr_path),
|
| 38 | "%s/usbip_status", udev->path);
|
| 39 | if (size < 0 || (unsigned int)size >= sizeof(status_attr_path)) {
|
| 40 | err("usbip_status path length %i >= %lu or < 0", size,
|
| 41 | (long unsigned)sizeof(status_attr_path));
|
| 42 | return -1;
|
| 43 | }
|
| 44 |
|
| 45 |
|
| 46 | fd = open(status_attr_path, O_RDONLY);
|
| 47 | if (fd < 0) {
|
| 48 | err("error opening attribute %s", status_attr_path);
|
| 49 | return -1;
|
| 50 | }
|
| 51 |
|
| 52 | length = read(fd, status, 1);
|
| 53 | if (length < 0) {
|
| 54 | err("error reading attribute %s", status_attr_path);
|
| 55 | close(fd);
|
| 56 | return -1;
|
| 57 | }
|
| 58 |
|
| 59 | value = atoi(status);
|
| 60 | close(fd);
|
| 61 | return value;
|
| 62 | }
|
| 63 |
|
| 64 | static
|
| 65 | struct usbip_exported_device *usbip_exported_device_new(
|
| 66 | struct usbip_host_driver *hdriver, const char *sdevpath)
|
| 67 | {
|
| 68 | struct usbip_exported_device *edev = NULL;
|
| 69 | struct usbip_exported_device *edev_old;
|
| 70 | size_t size;
|
| 71 | int i;
|
| 72 |
|
| 73 | edev = calloc(1, sizeof(struct usbip_exported_device));
|
| 74 |
|
| 75 | edev->sudev =
|
| 76 | udev_device_new_from_syspath(udev_context, sdevpath);
|
| 77 | if (!edev->sudev) {
|
| 78 | err("udev_device_new_from_syspath: %s", sdevpath);
|
| 79 | goto err;
|
| 80 | }
|
| 81 |
|
| 82 | if (hdriver->ops.read_device(edev->sudev, &edev->udev) < 0)
|
| 83 | goto err;
|
| 84 |
|
| 85 | edev->status = read_attr_usbip_status(&edev->udev);
|
| 86 | if (edev->status < 0)
|
| 87 | goto err;
|
| 88 |
|
| 89 |
|
| 90 | size = sizeof(struct usbip_exported_device) +
|
| 91 | edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
|
| 92 |
|
| 93 | edev_old = edev;
|
| 94 | edev = realloc(edev, size);
|
| 95 | if (!edev) {
|
| 96 | edev = edev_old;
|
| 97 | dbg("realloc failed");
|
| 98 | goto err;
|
| 99 | }
|
| 100 |
|
| 101 | for (i = 0; i < edev->udev.bNumInterfaces; i++) {
|
| 102 |
|
| 103 | if (!hdriver->ops.read_interface)
|
| 104 | break;
|
| 105 | hdriver->ops.read_interface(&edev->udev, i, &edev->uinf[i]);
|
| 106 | }
|
| 107 |
|
| 108 | return edev;
|
| 109 | err:
|
| 110 | if (edev->sudev)
|
| 111 | udev_device_unref(edev->sudev);
|
| 112 | if (edev)
|
| 113 | free(edev);
|
| 114 |
|
| 115 | return NULL;
|
| 116 | }
|
| 117 |
|
| 118 | static int refresh_exported_devices(struct usbip_host_driver *hdriver)
|
| 119 | {
|
| 120 | struct usbip_exported_device *edev;
|
| 121 | struct udev_enumerate *enumerate;
|
| 122 | struct udev_list_entry *devices, *dev_list_entry;
|
| 123 | struct udev_device *dev;
|
| 124 | const char *path;
|
| 125 |
|
| 126 | enumerate = udev_enumerate_new(udev_context);
|
| 127 | udev_enumerate_add_match_subsystem(enumerate, hdriver->udev_subsystem);
|
| 128 | udev_enumerate_scan_devices(enumerate);
|
| 129 |
|
| 130 | devices = udev_enumerate_get_list_entry(enumerate);
|
| 131 |
|
| 132 | udev_list_entry_foreach(dev_list_entry, devices) {
|
| 133 | path = udev_list_entry_get_name(dev_list_entry);
|
| 134 | dev = udev_device_new_from_syspath(udev_context,
|
| 135 | path);
|
| 136 | if (dev == NULL)
|
| 137 | continue;
|
| 138 |
|
| 139 |
|
| 140 | if (hdriver->ops.is_my_device(dev)) {
|
| 141 | edev = usbip_exported_device_new(hdriver, path);
|
| 142 | if (!edev) {
|
| 143 | dbg("usbip_exported_device_new failed");
|
| 144 | continue;
|
| 145 | }
|
| 146 |
|
| 147 | list_add(&edev->node, &hdriver->edev_list);
|
| 148 | hdriver->ndevs++;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | if (hdriver->ndevs == 0)
|
| 153 | info("Please load appropriate modules or export devices.");
|
| 154 |
|
| 155 | return 0;
|
| 156 | }
|
| 157 |
|
| 158 | static void usbip_exported_device_destroy(struct list_head *devs)
|
| 159 | {
|
| 160 | struct list_head *i, *tmp;
|
| 161 | struct usbip_exported_device *edev;
|
| 162 |
|
| 163 | list_for_each_safe(i, tmp, devs) {
|
| 164 | edev = list_entry(i, struct usbip_exported_device, node);
|
| 165 | list_del(i);
|
| 166 | free(edev);
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | int usbip_generic_driver_open(struct usbip_host_driver *hdriver)
|
| 171 | {
|
| 172 | int rc;
|
| 173 |
|
| 174 | udev_context = udev_new();
|
| 175 | if (!udev_context) {
|
| 176 | err("udev_new failed");
|
| 177 | return -1;
|
| 178 | }
|
| 179 |
|
| 180 | rc = refresh_exported_devices(hdriver);
|
| 181 | if (rc < 0)
|
| 182 | goto err;
|
| 183 | return 0;
|
| 184 | err:
|
| 185 | udev_unref(udev_context);
|
| 186 | return -1;
|
| 187 | }
|
| 188 |
|
| 189 | void usbip_generic_driver_close(struct usbip_host_driver *hdriver)
|
| 190 | {
|
| 191 | if (!hdriver)
|
| 192 | return;
|
| 193 |
|
| 194 | usbip_exported_device_destroy(&hdriver->edev_list);
|
| 195 |
|
| 196 | udev_unref(udev_context);
|
| 197 | }
|
| 198 |
|
| 199 | int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver)
|
| 200 | {
|
| 201 | int rc;
|
| 202 |
|
| 203 | usbip_exported_device_destroy(&hdriver->edev_list);
|
| 204 |
|
| 205 | hdriver->ndevs = 0;
|
| 206 | INIT_LIST_HEAD(&hdriver->edev_list);
|
| 207 |
|
| 208 | rc = refresh_exported_devices(hdriver);
|
| 209 | if (rc < 0)
|
| 210 | return -1;
|
| 211 |
|
| 212 | return 0;
|
| 213 | }
|
| 214 |
|
| 215 | int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
|
| 216 | {
|
| 217 | char attr_name[] = "usbip_sockfd";
|
| 218 | char sockfd_attr_path[SYSFS_PATH_MAX];
|
| 219 | int size;
|
| 220 | char sockfd_buff[30];
|
| 221 | int ret;
|
| 222 |
|
| 223 | if (edev->status != SDEV_ST_AVAILABLE) {
|
| 224 | dbg("device not available: %s", edev->udev.busid);
|
| 225 | switch (edev->status) {
|
| 226 | case SDEV_ST_ERROR:
|
| 227 | dbg("status SDEV_ST_ERROR");
|
| 228 | ret = ST_DEV_ERR;
|
| 229 | break;
|
| 230 | case SDEV_ST_USED:
|
| 231 | dbg("status SDEV_ST_USED");
|
| 232 | ret = ST_DEV_BUSY;
|
| 233 | break;
|
| 234 | default:
|
| 235 | dbg("status unknown: 0x%x", edev->status);
|
| 236 | ret = -1;
|
| 237 | }
|
| 238 | return ret;
|
| 239 | }
|
| 240 |
|
| 241 |
|
| 242 | size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
|
| 243 | edev->udev.path, attr_name);
|
| 244 | if (size < 0 || (unsigned int)size >= sizeof(sockfd_attr_path)) {
|
| 245 | err("exported device path length %i >= %lu or < 0", size,
|
| 246 | (long unsigned)sizeof(sockfd_attr_path));
|
| 247 | return -1;
|
| 248 | }
|
| 249 |
|
| 250 | size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
|
| 251 | if (size < 0 || (unsigned int)size >= sizeof(sockfd_buff)) {
|
| 252 | err("socket length %i >= %lu or < 0", size,
|
| 253 | (long unsigned)sizeof(sockfd_buff));
|
| 254 | return -1;
|
| 255 | }
|
| 256 |
|
| 257 | ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
|
| 258 | strlen(sockfd_buff));
|
| 259 | if (ret < 0) {
|
| 260 | err("write_sysfs_attribute failed: sockfd %s to %s",
|
| 261 | sockfd_buff, sockfd_attr_path);
|
| 262 | return ret;
|
| 263 | }
|
| 264 |
|
| 265 | info("connect: %s", edev->udev.busid);
|
| 266 |
|
| 267 | return ret;
|
| 268 | }
|
| 269 |
|
| 270 | struct usbip_exported_device *usbip_generic_get_device(
|
| 271 | struct usbip_host_driver *hdriver, int num)
|
| 272 | {
|
| 273 | struct list_head *i;
|
| 274 | struct usbip_exported_device *edev;
|
| 275 | int cnt = 0;
|
| 276 |
|
| 277 | list_for_each(i, &hdriver->edev_list) {
|
| 278 | edev = list_entry(i, struct usbip_exported_device, node);
|
| 279 | if (num == cnt)
|
| 280 | return edev;
|
| 281 | cnt++;
|
| 282 | }
|
| 283 |
|
| 284 | return NULL;
|
| 285 | }
|