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