Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005-2007 Takahiro Hirofuchi |
| 3 | */ |
| 4 | |
matt mooney | 099f79f | 2011-06-19 22:44:37 -0700 | [diff] [blame] | 5 | #include "usbip_common.h" |
| 6 | #include "vhci_driver.h" |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | #include <netdb.h> |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 9 | #include <libudev.h> |
Yuyang Du | aa3ecb9 | 2017-05-22 18:20:16 +0800 | [diff] [blame^] | 10 | #include <dirent.h> |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 11 | #include "sysfs_utils.h" |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 12 | |
matt mooney | f2fb62b | 2011-06-19 22:44:35 -0700 | [diff] [blame] | 13 | #undef PROGNAME |
| 14 | #define PROGNAME "libusbip" |
| 15 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 16 | struct usbip_vhci_driver *vhci_driver; |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 17 | struct udev *udev_context; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 18 | |
Kurt Kanzenbach | 9db91e1 | 2013-02-22 12:13:29 +0100 | [diff] [blame] | 19 | static struct usbip_imported_device * |
| 20 | imported_device_init(struct usbip_imported_device *idev, char *busid) |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 21 | { |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 22 | struct udev_device *sudev; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 23 | |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 24 | sudev = udev_device_new_from_subsystem_sysname(udev_context, |
| 25 | "usb", busid); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 26 | if (!sudev) { |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 27 | dbg("udev_device_new_from_subsystem_sysname failed: %s", busid); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 28 | goto err; |
| 29 | } |
| 30 | read_usb_device(sudev, &idev->udev); |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 31 | udev_device_unref(sudev); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 32 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 33 | return idev; |
| 34 | |
| 35 | err: |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 41 | static int parse_status(const char *value) |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 42 | { |
| 43 | int ret = 0; |
| 44 | char *c; |
| 45 | |
| 46 | |
| 47 | for (int i = 0; i < vhci_driver->nports; i++) |
matt mooney | 950a4cd | 2011-05-27 01:44:10 -0700 | [diff] [blame] | 48 | memset(&vhci_driver->idev[i], 0, sizeof(vhci_driver->idev[i])); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | /* skip a header line */ |
Christopher Harvey | 2f5c638 | 2012-03-23 10:55:25 -0400 | [diff] [blame] | 52 | c = strchr(value, '\n'); |
| 53 | if (!c) |
| 54 | return -1; |
| 55 | c++; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 56 | |
| 57 | while (*c != '\0') { |
| 58 | int port, status, speed, devid; |
| 59 | unsigned long socket; |
| 60 | char lbusid[SYSFS_BUS_ID_SIZE]; |
| 61 | |
Alan | 2d32927 | 2013-12-11 18:32:59 +0000 | [diff] [blame] | 62 | ret = sscanf(c, "%d %d %d %x %lx %31s\n", |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 63 | &port, &status, &speed, |
| 64 | &devid, &socket, lbusid); |
| 65 | |
| 66 | if (ret < 5) { |
matt mooney | 25567a3 | 2011-06-19 22:44:38 -0700 | [diff] [blame] | 67 | dbg("sscanf failed: %d", ret); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 68 | BUG(); |
| 69 | } |
| 70 | |
| 71 | dbg("port %d status %d speed %d devid %x", |
| 72 | port, status, speed, devid); |
| 73 | dbg("socket %lx lbusid %s", socket, lbusid); |
| 74 | |
| 75 | |
| 76 | /* if a device is connected, look at it */ |
| 77 | { |
| 78 | struct usbip_imported_device *idev = &vhci_driver->idev[port]; |
| 79 | |
| 80 | idev->port = port; |
| 81 | idev->status = status; |
| 82 | |
| 83 | idev->devid = devid; |
| 84 | |
| 85 | idev->busnum = (devid >> 16); |
| 86 | idev->devnum = (devid & 0x0000ffff); |
| 87 | |
Kurt Kanzenbach | 9db91e1 | 2013-02-22 12:13:29 +0100 | [diff] [blame] | 88 | if (idev->status != VDEV_ST_NULL |
| 89 | && idev->status != VDEV_ST_NOTASSIGNED) { |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 90 | idev = imported_device_init(idev, lbusid); |
| 91 | if (!idev) { |
matt mooney | 25567a3 | 2011-06-19 22:44:38 -0700 | [diff] [blame] | 92 | dbg("imported_device_init failed"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 93 | return -1; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* go to the next line */ |
Christopher Harvey | 2f5c638 | 2012-03-23 10:55:25 -0400 | [diff] [blame] | 100 | c = strchr(c, '\n'); |
| 101 | if (!c) |
| 102 | break; |
| 103 | c++; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | dbg("exit"); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 111 | static int refresh_imported_device_list(void) |
| 112 | { |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 113 | const char *attr_status; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 114 | |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 115 | attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device, |
| 116 | "status"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 117 | if (!attr_status) { |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 118 | err("udev_device_get_sysattr_value failed"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 119 | return -1; |
| 120 | } |
| 121 | |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 122 | return parse_status(attr_status); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int get_nports(void) |
| 126 | { |
Yuyang Du | 37e47d5 | 2017-04-06 06:03:23 +0800 | [diff] [blame] | 127 | const char *attr_nports; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 128 | |
Yuyang Du | 37e47d5 | 2017-04-06 06:03:23 +0800 | [diff] [blame] | 129 | attr_nports = udev_device_get_sysattr_value(vhci_driver->hc_device, "nports"); |
| 130 | if (!attr_nports) { |
| 131 | err("udev_device_get_sysattr_value nports failed"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | |
Yuyang Du | 37e47d5 | 2017-04-06 06:03:23 +0800 | [diff] [blame] | 135 | return (int)strtoul(attr_nports, NULL, 10); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Yuyang Du | aa3ecb9 | 2017-05-22 18:20:16 +0800 | [diff] [blame^] | 138 | static int vhci_hcd_filter(const struct dirent *dirent) |
| 139 | { |
| 140 | return strcmp(dirent->d_name, "vhci_hcd") >= 0; |
| 141 | } |
| 142 | |
| 143 | static int get_ncontrollers(void) |
| 144 | { |
| 145 | struct dirent **namelist; |
| 146 | struct udev_device *platform; |
| 147 | int n; |
| 148 | |
| 149 | platform = udev_device_get_parent(vhci_driver->hc_device); |
| 150 | if (platform == NULL) |
| 151 | return -1; |
| 152 | |
| 153 | n = scandir(udev_device_get_syspath(platform), &namelist, vhci_hcd_filter, NULL); |
| 154 | if (n < 0) |
| 155 | err("scandir failed"); |
| 156 | else { |
| 157 | for (int i = 0; i < n; i++) |
| 158 | free(namelist[i]); |
| 159 | free(namelist); |
| 160 | } |
| 161 | |
| 162 | return n; |
| 163 | } |
| 164 | |
Mark Asselstine | a37d70e | 2014-02-12 21:47:56 -0500 | [diff] [blame] | 165 | /* |
| 166 | * Read the given port's record. |
| 167 | * |
| 168 | * To avoid buffer overflow we will read the entire line and |
| 169 | * validate each part's size. The initial buffer is padded by 4 to |
| 170 | * accommodate the 2 spaces, 1 newline and an additional character |
| 171 | * which is needed to properly validate the 3rd part without it being |
| 172 | * truncated to an acceptable length. |
| 173 | */ |
| 174 | static int read_record(int rhport, char *host, unsigned long host_len, |
| 175 | char *port, unsigned long port_len, char *busid) |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 176 | { |
Mark Asselstine | a37d70e | 2014-02-12 21:47:56 -0500 | [diff] [blame] | 177 | int part; |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 178 | FILE *file; |
| 179 | char path[PATH_MAX+1]; |
Mark Asselstine | a37d70e | 2014-02-12 21:47:56 -0500 | [diff] [blame] | 180 | char *buffer, *start, *end; |
| 181 | char delim[] = {' ', ' ', '\n'}; |
| 182 | int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE}; |
| 183 | size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4; |
| 184 | |
| 185 | buffer = malloc(buffer_len); |
| 186 | if (!buffer) |
| 187 | return -1; |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 188 | |
| 189 | snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport); |
| 190 | |
| 191 | file = fopen(path, "r"); |
| 192 | if (!file) { |
| 193 | err("fopen"); |
Mark Asselstine | a37d70e | 2014-02-12 21:47:56 -0500 | [diff] [blame] | 194 | free(buffer); |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 195 | return -1; |
| 196 | } |
| 197 | |
Mark Asselstine | a37d70e | 2014-02-12 21:47:56 -0500 | [diff] [blame] | 198 | if (fgets(buffer, buffer_len, file) == NULL) { |
| 199 | err("fgets"); |
| 200 | free(buffer); |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 201 | fclose(file); |
| 202 | return -1; |
| 203 | } |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 204 | fclose(file); |
| 205 | |
Mark Asselstine | a37d70e | 2014-02-12 21:47:56 -0500 | [diff] [blame] | 206 | /* validate the length of each of the 3 parts */ |
| 207 | start = buffer; |
| 208 | for (part = 0; part < 3; part++) { |
| 209 | end = strchr(start, delim[part]); |
| 210 | if (end == NULL || (end - start) > max_len[part]) { |
| 211 | free(buffer); |
| 212 | return -1; |
| 213 | } |
| 214 | start = end + 1; |
| 215 | } |
| 216 | |
| 217 | if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) { |
| 218 | err("sscanf"); |
| 219 | free(buffer); |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | free(buffer); |
| 224 | |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 227 | |
| 228 | /* ---------------------------------------------------------------------- */ |
| 229 | |
| 230 | int usbip_vhci_driver_open(void) |
| 231 | { |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 232 | udev_context = udev_new(); |
| 233 | if (!udev_context) { |
| 234 | err("udev_new failed"); |
| 235 | return -1; |
| 236 | } |
| 237 | |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 238 | vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver)); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 239 | |
| 240 | /* will be freed in usbip_driver_close() */ |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 241 | vhci_driver->hc_device = |
| 242 | udev_device_new_from_subsystem_sysname(udev_context, |
| 243 | USBIP_VHCI_BUS_TYPE, |
| 244 | USBIP_VHCI_DRV_NAME); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 245 | if (!vhci_driver->hc_device) { |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 246 | err("udev_device_new_from_subsystem_sysname failed"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 247 | goto err; |
| 248 | } |
| 249 | |
| 250 | vhci_driver->nports = get_nports(); |
matt mooney | 25567a3 | 2011-06-19 22:44:38 -0700 | [diff] [blame] | 251 | dbg("available ports: %d", vhci_driver->nports); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 252 | |
Yuyang Du | c350971 | 2017-05-22 18:20:15 +0800 | [diff] [blame] | 253 | if (vhci_driver->nports <= 0) { |
| 254 | err("no available ports"); |
| 255 | goto err; |
| 256 | } else if (vhci_driver->nports > MAXNPORT) { |
| 257 | err("port number exceeds %d", MAXNPORT); |
| 258 | goto err; |
| 259 | } |
| 260 | |
Yuyang Du | aa3ecb9 | 2017-05-22 18:20:16 +0800 | [diff] [blame^] | 261 | vhci_driver->ncontrollers = get_ncontrollers(); |
| 262 | dbg("available controllers: %d", vhci_driver->ncontrollers); |
| 263 | |
| 264 | if (vhci_driver->ncontrollers <=0) { |
| 265 | err("no available usb controllers"); |
| 266 | goto err; |
| 267 | } |
| 268 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 269 | if (refresh_imported_device_list()) |
| 270 | goto err; |
| 271 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 274 | err: |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 275 | udev_device_unref(vhci_driver->hc_device); |
| 276 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 277 | if (vhci_driver) |
| 278 | free(vhci_driver); |
| 279 | |
| 280 | vhci_driver = NULL; |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 281 | |
| 282 | udev_unref(udev_context); |
| 283 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | |
Pawel Lebioda | 1949551 | 2014-05-14 21:28:27 +0200 | [diff] [blame] | 288 | void usbip_vhci_driver_close(void) |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 289 | { |
| 290 | if (!vhci_driver) |
| 291 | return; |
| 292 | |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 293 | udev_device_unref(vhci_driver->hc_device); |
| 294 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 295 | free(vhci_driver); |
| 296 | |
| 297 | vhci_driver = NULL; |
Valentina Manea | 021aed8 | 2014-03-08 14:53:26 +0200 | [diff] [blame] | 298 | |
| 299 | udev_unref(udev_context); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | |
| 303 | int usbip_vhci_refresh_device_list(void) |
| 304 | { |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 305 | |
| 306 | if (refresh_imported_device_list()) |
| 307 | goto err; |
| 308 | |
| 309 | return 0; |
| 310 | err: |
matt mooney | 25567a3 | 2011-06-19 22:44:38 -0700 | [diff] [blame] | 311 | dbg("failed to refresh device list"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | int usbip_vhci_get_free_port(void) |
| 317 | { |
| 318 | for (int i = 0; i < vhci_driver->nports; i++) { |
| 319 | if (vhci_driver->idev[i].status == VDEV_ST_NULL) |
| 320 | return i; |
| 321 | } |
| 322 | |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid, |
| 327 | uint32_t speed) { |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 328 | char buff[200]; /* what size should be ? */ |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 329 | char attach_attr_path[SYSFS_PATH_MAX]; |
| 330 | char attr_attach[] = "attach"; |
| 331 | const char *path; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 332 | int ret; |
| 333 | |
Masanari Iida | 5484081 | 2014-02-27 20:36:31 +0900 | [diff] [blame] | 334 | snprintf(buff, sizeof(buff), "%u %d %u %u", |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 335 | port, sockfd, devid, speed); |
| 336 | dbg("writing: %s", buff); |
| 337 | |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 338 | path = udev_device_get_syspath(vhci_driver->hc_device); |
| 339 | snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s", |
| 340 | path, attr_attach); |
| 341 | dbg("attach attribute path: %s", attach_attr_path); |
| 342 | |
| 343 | ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff)); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 344 | if (ret < 0) { |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 345 | dbg("write_sysfs_attribute failed"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 346 | return -1; |
| 347 | } |
| 348 | |
matt mooney | 25567a3 | 2011-06-19 22:44:38 -0700 | [diff] [blame] | 349 | dbg("attached port: %d", port); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static unsigned long get_devid(uint8_t busnum, uint8_t devnum) |
| 355 | { |
| 356 | return (busnum << 16) | devnum; |
| 357 | } |
| 358 | |
| 359 | /* will be removed */ |
| 360 | int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum, |
| 361 | uint8_t devnum, uint32_t speed) |
| 362 | { |
| 363 | int devid = get_devid(busnum, devnum); |
| 364 | |
| 365 | return usbip_vhci_attach_device2(port, sockfd, devid, speed); |
| 366 | } |
| 367 | |
| 368 | int usbip_vhci_detach_device(uint8_t port) |
| 369 | { |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 370 | char detach_attr_path[SYSFS_PATH_MAX]; |
| 371 | char attr_detach[] = "detach"; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 372 | char buff[200]; /* what size should be ? */ |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 373 | const char *path; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 374 | int ret; |
| 375 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 376 | snprintf(buff, sizeof(buff), "%u", port); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 377 | dbg("writing: %s", buff); |
| 378 | |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 379 | path = udev_device_get_syspath(vhci_driver->hc_device); |
| 380 | snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s", |
| 381 | path, attr_detach); |
| 382 | dbg("detach attribute path: %s", detach_attr_path); |
| 383 | |
| 384 | ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff)); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 385 | if (ret < 0) { |
Valentina Manea | a744b7c | 2014-03-08 14:53:28 +0200 | [diff] [blame] | 386 | dbg("write_sysfs_attribute failed"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 387 | return -1; |
| 388 | } |
| 389 | |
matt mooney | 25567a3 | 2011-06-19 22:44:38 -0700 | [diff] [blame] | 390 | dbg("detached port: %d", port); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 394 | |
| 395 | int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev) |
| 396 | { |
| 397 | char product_name[100]; |
| 398 | char host[NI_MAXHOST] = "unknown host"; |
| 399 | char serv[NI_MAXSERV] = "unknown port"; |
| 400 | char remote_busid[SYSFS_BUS_ID_SIZE]; |
| 401 | int ret; |
| 402 | int read_record_error = 0; |
| 403 | |
| 404 | if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED) |
| 405 | return 0; |
| 406 | |
Mark Asselstine | a37d70e | 2014-02-12 21:47:56 -0500 | [diff] [blame] | 407 | ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv), |
| 408 | remote_busid); |
Valentina Manea | ec2ff62 | 2014-01-07 21:05:56 +0200 | [diff] [blame] | 409 | if (ret) { |
| 410 | err("read_record"); |
| 411 | read_record_error = 1; |
| 412 | } |
| 413 | |
| 414 | printf("Port %02d: <%s> at %s\n", idev->port, |
| 415 | usbip_status_string(idev->status), |
| 416 | usbip_speed_string(idev->udev.speed)); |
| 417 | |
| 418 | usbip_names_get_product(product_name, sizeof(product_name), |
| 419 | idev->udev.idVendor, idev->udev.idProduct); |
| 420 | |
| 421 | printf(" %s\n", product_name); |
| 422 | |
| 423 | if (!read_record_error) { |
| 424 | printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid, |
| 425 | host, serv, remote_busid); |
| 426 | printf("%10s -> remote bus/dev %03d/%03d\n", " ", |
| 427 | idev->busnum, idev->devnum); |
| 428 | } else { |
| 429 | printf("%10s -> unknown host, remote port and remote busid\n", |
| 430 | idev->udev.busid); |
| 431 | printf("%10s -> remote bus/dev %03d/%03d\n", " ", |
| 432 | idev->busnum, idev->devnum); |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |