diff options
Diffstat (limited to 'src/usb.c')
| -rw-r--r-- | src/usb.c | 680 |
1 files changed, 680 insertions, 0 deletions
diff --git a/src/usb.c b/src/usb.c new file mode 100644 index 0000000..f5e8092 --- /dev/null +++ b/src/usb.c | |||
| @@ -0,0 +1,680 @@ | |||
| 1 | /* | ||
| 2 | * usb.c | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> | ||
| 5 | * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
| 6 | * Copyright (C) 2009 Martin Szulecki <opensuse@sukimashita.com> | ||
| 7 | * Copyright (C) 2014 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@xamarin.com> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License as published by | ||
| 11 | * the Free Software Foundation, either version 2 or version 3. | ||
| 12 | * | ||
| 13 | * This program is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU General Public License | ||
| 19 | * along with this program; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <stdio.h> | ||
| 28 | #include <stdlib.h> | ||
| 29 | #include <stdint.h> | ||
| 30 | #include <string.h> | ||
| 31 | |||
| 32 | #include <libusb.h> | ||
| 33 | |||
| 34 | #include "usb.h" | ||
| 35 | #include "log.h" | ||
| 36 | #include "device.h" | ||
| 37 | #include "utils.h" | ||
| 38 | |||
| 39 | // interval for device connection/disconnection polling, in milliseconds | ||
| 40 | // we need this because there is currently no asynchronous device discovery mechanism in libusb | ||
| 41 | #define DEVICE_POLL_TIME 1000 | ||
| 42 | |||
| 43 | // Number of parallel bulk transfers we have running for reading data from the device. | ||
| 44 | // Older versions of usbmuxd kept only 1, which leads to a mostly dormant USB port. | ||
| 45 | // 3 seems to be an all round sensible number - giving better read perf than | ||
| 46 | // Apples usbmuxd, at least. | ||
| 47 | #define NUM_RX_LOOPS 3 | ||
| 48 | |||
| 49 | struct usb_device { | ||
| 50 | libusb_device_handle *dev; | ||
| 51 | uint8_t bus, address; | ||
| 52 | uint16_t vid, pid; | ||
| 53 | char serial[256]; | ||
| 54 | int alive; | ||
| 55 | uint8_t interface, ep_in, ep_out; | ||
| 56 | struct collection rx_xfers; | ||
| 57 | struct collection tx_xfers; | ||
| 58 | int wMaxPacketSize; | ||
| 59 | uint64_t speed; | ||
| 60 | }; | ||
| 61 | |||
| 62 | static struct collection device_list; | ||
| 63 | |||
| 64 | static struct timeval next_dev_poll_time; | ||
| 65 | |||
| 66 | static int devlist_failures; | ||
| 67 | static int device_polling; | ||
| 68 | |||
| 69 | static void usb_disconnect(struct usb_device *dev) | ||
| 70 | { | ||
| 71 | if(!dev->dev) { | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | // kill the rx xfer and tx xfers and try to make sure the callbacks | ||
| 76 | // get called before we free the device | ||
| 77 | FOREACH(struct libusb_transfer *xfer, &dev->rx_xfers) { | ||
| 78 | usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling RX xfer %p", xfer); | ||
| 79 | libusb_cancel_transfer(xfer); | ||
| 80 | } ENDFOREACH | ||
| 81 | |||
| 82 | FOREACH(struct libusb_transfer *xfer, &dev->tx_xfers) { | ||
| 83 | usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling TX xfer %p", xfer); | ||
| 84 | libusb_cancel_transfer(xfer); | ||
| 85 | } ENDFOREACH | ||
| 86 | |||
| 87 | // Busy-wait until all xfers are closed | ||
| 88 | while(collection_count(&dev->rx_xfers) || collection_count(&dev->tx_xfers)) { | ||
| 89 | struct timeval tv; | ||
| 90 | int res; | ||
| 91 | |||
| 92 | tv.tv_sec = 0; | ||
| 93 | tv.tv_usec = 1000; | ||
| 94 | if((res = libusb_handle_events_timeout(NULL, &tv)) < 0) { | ||
| 95 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout for usb_disconnect failed: %d", res); | ||
| 96 | break; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | collection_free(&dev->tx_xfers); | ||
| 101 | collection_free(&dev->rx_xfers); | ||
| 102 | libusb_release_interface(dev->dev, dev->interface); | ||
| 103 | libusb_close(dev->dev); | ||
| 104 | dev->dev = NULL; | ||
| 105 | collection_remove(&device_list, dev); | ||
| 106 | free(dev); | ||
| 107 | } | ||
| 108 | |||
| 109 | static void reap_dead_devices(void) { | ||
| 110 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 111 | if(!usbdev->alive) { | ||
| 112 | device_remove(usbdev); | ||
| 113 | usb_disconnect(usbdev); | ||
| 114 | } | ||
| 115 | } ENDFOREACH | ||
| 116 | } | ||
| 117 | |||
| 118 | // Callback from write operation | ||
| 119 | static void tx_callback(struct libusb_transfer *xfer) | ||
| 120 | { | ||
| 121 | struct usb_device *dev = xfer->user_data; | ||
| 122 | usbmuxd_log(LL_SPEW, "TX callback dev %d-%d len %d -> %d status %d", dev->bus, dev->address, xfer->length, xfer->actual_length, xfer->status); | ||
| 123 | if(xfer->status != LIBUSB_TRANSFER_COMPLETED) { | ||
| 124 | switch(xfer->status) { | ||
| 125 | case LIBUSB_TRANSFER_COMPLETED: //shut up compiler | ||
| 126 | case LIBUSB_TRANSFER_ERROR: | ||
| 127 | // funny, this happens when we disconnect the device while waiting for a transfer, sometimes | ||
| 128 | usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to error or disconnect", dev->bus, dev->address); | ||
| 129 | break; | ||
| 130 | case LIBUSB_TRANSFER_TIMED_OUT: | ||
| 131 | usbmuxd_log(LL_ERROR, "TX transfer timed out for device %d-%d", dev->bus, dev->address); | ||
| 132 | break; | ||
| 133 | case LIBUSB_TRANSFER_CANCELLED: | ||
| 134 | usbmuxd_log(LL_DEBUG, "Device %d-%d TX transfer cancelled", dev->bus, dev->address); | ||
| 135 | break; | ||
| 136 | case LIBUSB_TRANSFER_STALL: | ||
| 137 | usbmuxd_log(LL_ERROR, "TX transfer stalled for device %d-%d", dev->bus, dev->address); | ||
| 138 | break; | ||
| 139 | case LIBUSB_TRANSFER_NO_DEVICE: | ||
| 140 | // other times, this happens, and also even when we abort the transfer after device removal | ||
| 141 | usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to disconnect", dev->bus, dev->address); | ||
| 142 | break; | ||
| 143 | case LIBUSB_TRANSFER_OVERFLOW: | ||
| 144 | usbmuxd_log(LL_ERROR, "TX transfer overflow for device %d-%d", dev->bus, dev->address); | ||
| 145 | break; | ||
| 146 | // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad) | ||
| 147 | default: | ||
| 148 | // this should never be reached. | ||
| 149 | break; | ||
| 150 | } | ||
| 151 | // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events | ||
| 152 | // we'll do device_remove there too | ||
| 153 | dev->alive = 0; | ||
| 154 | } | ||
| 155 | if(xfer->buffer) | ||
| 156 | free(xfer->buffer); | ||
| 157 | collection_remove(&dev->tx_xfers, xfer); | ||
| 158 | libusb_free_transfer(xfer); | ||
| 159 | } | ||
| 160 | |||
| 161 | int usb_send(struct usb_device *dev, const unsigned char *buf, int length) | ||
| 162 | { | ||
| 163 | int res; | ||
| 164 | struct libusb_transfer *xfer = libusb_alloc_transfer(0); | ||
| 165 | libusb_fill_bulk_transfer(xfer, dev->dev, dev->ep_out, (void*)buf, length, tx_callback, dev, 0); | ||
| 166 | if((res = libusb_submit_transfer(xfer)) < 0) { | ||
| 167 | usbmuxd_log(LL_ERROR, "Failed to submit TX transfer %p len %d to device %d-%d: %d", buf, length, dev->bus, dev->address, res); | ||
| 168 | libusb_free_transfer(xfer); | ||
| 169 | return res; | ||
| 170 | } | ||
| 171 | collection_add(&dev->tx_xfers, xfer); | ||
| 172 | if (length % dev->wMaxPacketSize == 0) { | ||
| 173 | usbmuxd_log(LL_DEBUG, "Send ZLP"); | ||
| 174 | // Send Zero Length Packet | ||
| 175 | xfer = libusb_alloc_transfer(0); | ||
| 176 | void *buffer = malloc(1); | ||
| 177 | libusb_fill_bulk_transfer(xfer, dev->dev, dev->ep_out, buffer, 0, tx_callback, dev, 0); | ||
| 178 | if((res = libusb_submit_transfer(xfer)) < 0) { | ||
| 179 | usbmuxd_log(LL_ERROR, "Failed to submit TX ZLP transfer to device %d-%d: %d", dev->bus, dev->address, res); | ||
| 180 | libusb_free_transfer(xfer); | ||
| 181 | return res; | ||
| 182 | } | ||
| 183 | collection_add(&dev->tx_xfers, xfer); | ||
| 184 | } | ||
| 185 | return 0; | ||
| 186 | } | ||
| 187 | |||
| 188 | // Callback from read operation | ||
| 189 | // Under normal operation this issues a new read transfer request immediately, | ||
| 190 | // doing a kind of read-callback loop | ||
| 191 | static void rx_callback(struct libusb_transfer *xfer) | ||
| 192 | { | ||
| 193 | struct usb_device *dev = xfer->user_data; | ||
| 194 | usbmuxd_log(LL_SPEW, "RX callback dev %d-%d len %d status %d", dev->bus, dev->address, xfer->actual_length, xfer->status); | ||
| 195 | if(xfer->status == LIBUSB_TRANSFER_COMPLETED) { | ||
| 196 | device_data_input(dev, xfer->buffer, xfer->actual_length); | ||
| 197 | libusb_submit_transfer(xfer); | ||
| 198 | } else { | ||
| 199 | switch(xfer->status) { | ||
| 200 | case LIBUSB_TRANSFER_COMPLETED: //shut up compiler | ||
| 201 | case LIBUSB_TRANSFER_ERROR: | ||
| 202 | // funny, this happens when we disconnect the device while waiting for a transfer, sometimes | ||
| 203 | usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to error or disconnect", dev->bus, dev->address); | ||
| 204 | break; | ||
| 205 | case LIBUSB_TRANSFER_TIMED_OUT: | ||
| 206 | usbmuxd_log(LL_ERROR, "RX transfer timed out for device %d-%d", dev->bus, dev->address); | ||
| 207 | break; | ||
| 208 | case LIBUSB_TRANSFER_CANCELLED: | ||
| 209 | usbmuxd_log(LL_DEBUG, "Device %d-%d RX transfer cancelled", dev->bus, dev->address); | ||
| 210 | break; | ||
| 211 | case LIBUSB_TRANSFER_STALL: | ||
| 212 | usbmuxd_log(LL_ERROR, "RX transfer stalled for device %d-%d", dev->bus, dev->address); | ||
| 213 | break; | ||
| 214 | case LIBUSB_TRANSFER_NO_DEVICE: | ||
| 215 | // other times, this happens, and also even when we abort the transfer after device removal | ||
| 216 | usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to disconnect", dev->bus, dev->address); | ||
| 217 | break; | ||
| 218 | case LIBUSB_TRANSFER_OVERFLOW: | ||
| 219 | usbmuxd_log(LL_ERROR, "RX transfer overflow for device %d-%d", dev->bus, dev->address); | ||
| 220 | break; | ||
| 221 | // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad) | ||
| 222 | default: | ||
| 223 | // this should never be reached. | ||
| 224 | break; | ||
| 225 | } | ||
| 226 | |||
| 227 | free(xfer->buffer); | ||
| 228 | collection_remove(&dev->rx_xfers, xfer); | ||
| 229 | libusb_free_transfer(xfer); | ||
| 230 | |||
| 231 | // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events | ||
| 232 | // we'll do device_remove there too | ||
| 233 | dev->alive = 0; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | // Start a read-callback loop for this device | ||
| 238 | static int start_rx_loop(struct usb_device *dev) | ||
| 239 | { | ||
| 240 | int res; | ||
| 241 | void *buf; | ||
| 242 | struct libusb_transfer *xfer = libusb_alloc_transfer(0); | ||
| 243 | buf = malloc(USB_MRU); | ||
| 244 | libusb_fill_bulk_transfer(xfer, dev->dev, dev->ep_in, buf, USB_MRU, rx_callback, dev, 0); | ||
| 245 | if((res = libusb_submit_transfer(xfer)) != 0) { | ||
| 246 | usbmuxd_log(LL_ERROR, "Failed to submit RX transfer to device %d-%d: %d", dev->bus, dev->address, res); | ||
| 247 | libusb_free_transfer(xfer); | ||
| 248 | return res; | ||
| 249 | } | ||
| 250 | |||
| 251 | collection_add(&dev->rx_xfers, xfer); | ||
| 252 | |||
| 253 | return 0; | ||
| 254 | } | ||
| 255 | |||
| 256 | int usb_discover(void) | ||
| 257 | { | ||
| 258 | int cnt, i, j, res; | ||
| 259 | int valid_count = 0; | ||
| 260 | libusb_device **devs; | ||
| 261 | |||
| 262 | cnt = libusb_get_device_list(NULL, &devs); | ||
| 263 | if(cnt < 0) { | ||
| 264 | usbmuxd_log(LL_WARNING, "Could not get device list: %d", cnt); | ||
| 265 | devlist_failures++; | ||
| 266 | // sometimes libusb fails getting the device list if you've just removed something | ||
| 267 | if(devlist_failures > 5) { | ||
| 268 | usbmuxd_log(LL_FATAL, "Too many errors getting device list"); | ||
| 269 | return cnt; | ||
| 270 | } else { | ||
| 271 | get_tick_count(&next_dev_poll_time); | ||
| 272 | next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000; | ||
| 273 | next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000; | ||
| 274 | next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000; | ||
| 275 | return 0; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | devlist_failures = 0; | ||
| 279 | |||
| 280 | usbmuxd_log(LL_SPEW, "usb_discover: scanning %d devices", cnt); | ||
| 281 | |||
| 282 | // Mark all devices as dead, and do a mark-sweep like | ||
| 283 | // collection of dead devices | ||
| 284 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 285 | usbdev->alive = 0; | ||
| 286 | } ENDFOREACH | ||
| 287 | |||
| 288 | // Enumerate all USB devices and mark the ones we already know | ||
| 289 | // about as live, again | ||
| 290 | for(i=0; i<cnt; i++) { | ||
| 291 | // the following are non-blocking operations on the device list | ||
| 292 | libusb_device *dev = devs[i]; | ||
| 293 | uint8_t bus = libusb_get_bus_number(dev); | ||
| 294 | uint8_t address = libusb_get_device_address(dev); | ||
| 295 | struct libusb_device_descriptor devdesc; | ||
| 296 | int found = 0; | ||
| 297 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 298 | if(usbdev->bus == bus && usbdev->address == address) { | ||
| 299 | valid_count++; | ||
| 300 | usbdev->alive = 1; | ||
| 301 | found = 1; | ||
| 302 | break; | ||
| 303 | } | ||
| 304 | } ENDFOREACH | ||
| 305 | if(found) | ||
| 306 | continue; //device already found | ||
| 307 | if((res = libusb_get_device_descriptor(dev, &devdesc)) != 0) { | ||
| 308 | usbmuxd_log(LL_WARNING, "Could not get device descriptor for device %d-%d: %d", bus, address, res); | ||
| 309 | continue; | ||
| 310 | } | ||
| 311 | if(devdesc.idVendor != VID_APPLE) | ||
| 312 | continue; | ||
| 313 | if((devdesc.idProduct < PID_RANGE_LOW) || | ||
| 314 | (devdesc.idProduct > PID_RANGE_MAX)) | ||
| 315 | continue; | ||
| 316 | libusb_device_handle *handle; | ||
| 317 | usbmuxd_log(LL_INFO, "Found new device with v/p %04x:%04x at %d-%d", devdesc.idVendor, devdesc.idProduct, bus, address); | ||
| 318 | // potentially blocking operations follow; they will only run when new devices are detected, which is acceptable | ||
| 319 | if((res = libusb_open(dev, &handle)) != 0) { | ||
| 320 | usbmuxd_log(LL_WARNING, "Could not open device %d-%d: %d", bus, address, res); | ||
| 321 | continue; | ||
| 322 | } | ||
| 323 | |||
| 324 | int current_config = 0; | ||
| 325 | if((res = libusb_get_configuration(handle, ¤t_config)) != 0) { | ||
| 326 | usbmuxd_log(LL_WARNING, "Could not get configuration for device %d-%d: %d", bus, address, res); | ||
| 327 | libusb_close(handle); | ||
| 328 | continue; | ||
| 329 | } | ||
| 330 | if (current_config != devdesc.bNumConfigurations) { | ||
| 331 | struct libusb_config_descriptor *config; | ||
| 332 | if((res = libusb_get_active_config_descriptor(dev, &config)) != 0) { | ||
| 333 | usbmuxd_log(LL_NOTICE, "Could not get old configuration descriptor for device %d-%d: %d", bus, address, res); | ||
| 334 | } else { | ||
| 335 | for(j=0; j<config->bNumInterfaces; j++) { | ||
| 336 | const struct libusb_interface_descriptor *intf = &config->interface[j].altsetting[0]; | ||
| 337 | if((res = libusb_kernel_driver_active(handle, intf->bInterfaceNumber)) < 0) { | ||
| 338 | usbmuxd_log(LL_NOTICE, "Could not check kernel ownership of interface %d for device %d-%d: %d", intf->bInterfaceNumber, bus, address, res); | ||
| 339 | continue; | ||
| 340 | } | ||
| 341 | if(res == 1) { | ||
| 342 | usbmuxd_log(LL_INFO, "Detaching kernel driver for device %d-%d, interface %d", bus, address, intf->bInterfaceNumber); | ||
| 343 | if((res = libusb_detach_kernel_driver(handle, intf->bInterfaceNumber)) < 0) { | ||
| 344 | usbmuxd_log(LL_WARNING, "Could not detach kernel driver (%d), configuration change will probably fail!", res); | ||
| 345 | continue; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | } | ||
| 349 | libusb_free_config_descriptor(config); | ||
| 350 | } | ||
| 351 | |||
| 352 | usbmuxd_log(LL_INFO, "Setting configuration for device %d-%d, from %d to %d", bus, address, current_config, devdesc.bNumConfigurations); | ||
| 353 | if((res = libusb_set_configuration(handle, devdesc.bNumConfigurations)) != 0) { | ||
| 354 | usbmuxd_log(LL_WARNING, "Could not set configuration %d for device %d-%d: %d", devdesc.bNumConfigurations, bus, address, res); | ||
| 355 | libusb_close(handle); | ||
| 356 | continue; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | struct libusb_config_descriptor *config; | ||
| 361 | if((res = libusb_get_active_config_descriptor(dev, &config)) != 0) { | ||
| 362 | usbmuxd_log(LL_WARNING, "Could not get configuration descriptor for device %d-%d: %d", bus, address, res); | ||
| 363 | libusb_close(handle); | ||
| 364 | continue; | ||
| 365 | } | ||
| 366 | |||
| 367 | struct usb_device *usbdev; | ||
| 368 | usbdev = malloc(sizeof(struct usb_device)); | ||
| 369 | memset(usbdev, 0, sizeof(*usbdev)); | ||
| 370 | |||
| 371 | for(j=0; j<config->bNumInterfaces; j++) { | ||
| 372 | const struct libusb_interface_descriptor *intf = &config->interface[j].altsetting[0]; | ||
| 373 | if(intf->bInterfaceClass != INTERFACE_CLASS || | ||
| 374 | intf->bInterfaceSubClass != INTERFACE_SUBCLASS || | ||
| 375 | intf->bInterfaceProtocol != INTERFACE_PROTOCOL) | ||
| 376 | continue; | ||
| 377 | if(intf->bNumEndpoints != 2) { | ||
| 378 | usbmuxd_log(LL_WARNING, "Endpoint count mismatch for interface %d of device %d-%d", intf->bInterfaceNumber, bus, address); | ||
| 379 | continue; | ||
| 380 | } | ||
| 381 | if((intf->endpoint[0].bEndpointAddress & 0x80) == LIBUSB_ENDPOINT_OUT && | ||
| 382 | (intf->endpoint[1].bEndpointAddress & 0x80) == LIBUSB_ENDPOINT_IN) { | ||
| 383 | usbdev->interface = intf->bInterfaceNumber; | ||
| 384 | usbdev->ep_out = intf->endpoint[0].bEndpointAddress; | ||
| 385 | usbdev->ep_in = intf->endpoint[1].bEndpointAddress; | ||
| 386 | usbmuxd_log(LL_INFO, "Found interface %d with endpoints %02x/%02x for device %d-%d", usbdev->interface, usbdev->ep_out, usbdev->ep_in, bus, address); | ||
| 387 | break; | ||
| 388 | } else if((intf->endpoint[1].bEndpointAddress & 0x80) == LIBUSB_ENDPOINT_OUT && | ||
| 389 | (intf->endpoint[0].bEndpointAddress & 0x80) == LIBUSB_ENDPOINT_IN) { | ||
| 390 | usbdev->interface = intf->bInterfaceNumber; | ||
| 391 | usbdev->ep_out = intf->endpoint[1].bEndpointAddress; | ||
| 392 | usbdev->ep_in = intf->endpoint[0].bEndpointAddress; | ||
| 393 | usbmuxd_log(LL_INFO, "Found interface %d with swapped endpoints %02x/%02x for device %d-%d", usbdev->interface, usbdev->ep_out, usbdev->ep_in, bus, address); | ||
| 394 | break; | ||
| 395 | } else { | ||
| 396 | usbmuxd_log(LL_WARNING, "Endpoint type mismatch for interface %d of device %d-%d", intf->bInterfaceNumber, bus, address); | ||
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | if(j == config->bNumInterfaces) { | ||
| 401 | usbmuxd_log(LL_WARNING, "Could not find a suitable USB interface for device %d-%d", bus, address); | ||
| 402 | libusb_free_config_descriptor(config); | ||
| 403 | libusb_close(handle); | ||
| 404 | free(usbdev); | ||
| 405 | continue; | ||
| 406 | } | ||
| 407 | |||
| 408 | libusb_free_config_descriptor(config); | ||
| 409 | |||
| 410 | if((res = libusb_claim_interface(handle, usbdev->interface)) != 0) { | ||
| 411 | usbmuxd_log(LL_WARNING, "Could not claim interface %d for device %d-%d: %d", usbdev->interface, bus, address, res); | ||
| 412 | libusb_close(handle); | ||
| 413 | free(usbdev); | ||
| 414 | continue; | ||
| 415 | } | ||
| 416 | |||
| 417 | if((res = libusb_get_string_descriptor_ascii(handle, devdesc.iSerialNumber, (uint8_t *)usbdev->serial, 256)) <= 0) { | ||
| 418 | usbmuxd_log(LL_WARNING, "Could not get serial number for device %d-%d: %d", bus, address, res); | ||
| 419 | libusb_release_interface(handle, usbdev->interface); | ||
| 420 | libusb_close(handle); | ||
| 421 | free(usbdev); | ||
| 422 | continue; | ||
| 423 | } | ||
| 424 | usbdev->serial[res] = 0; | ||
| 425 | usbdev->bus = bus; | ||
| 426 | usbdev->address = address; | ||
| 427 | usbdev->vid = devdesc.idVendor; | ||
| 428 | usbdev->pid = devdesc.idProduct; | ||
| 429 | usbdev->speed = 480000000; | ||
| 430 | usbdev->dev = handle; | ||
| 431 | usbdev->alive = 1; | ||
| 432 | usbdev->wMaxPacketSize = libusb_get_max_packet_size(dev, usbdev->ep_out); | ||
| 433 | if (usbdev->wMaxPacketSize <= 0) { | ||
| 434 | usbmuxd_log(LL_ERROR, "Could not determine wMaxPacketSize for device %d-%d, setting to 64", usbdev->bus, usbdev->address); | ||
| 435 | usbdev->wMaxPacketSize = 64; | ||
| 436 | } else { | ||
| 437 | usbmuxd_log(LL_INFO, "Using wMaxPacketSize=%d for device %d-%d", usbdev->wMaxPacketSize, usbdev->bus, usbdev->address); | ||
| 438 | } | ||
| 439 | |||
| 440 | switch (libusb_get_device_speed(dev)) { | ||
| 441 | case LIBUSB_SPEED_LOW: | ||
| 442 | usbdev->speed = 1500000; | ||
| 443 | break; | ||
| 444 | case LIBUSB_SPEED_FULL: | ||
| 445 | usbdev->speed = 12000000; | ||
| 446 | break; | ||
| 447 | case LIBUSB_SPEED_SUPER: | ||
| 448 | usbdev->speed = 5000000000; | ||
| 449 | break; | ||
| 450 | case LIBUSB_SPEED_HIGH: | ||
| 451 | case LIBUSB_SPEED_UNKNOWN: | ||
| 452 | default: | ||
| 453 | usbdev->speed = 480000000; | ||
| 454 | break; | ||
| 455 | } | ||
| 456 | |||
| 457 | usbmuxd_log(LL_INFO, "USB Speed is %g MBit/s for device %d-%d", (double)(usbdev->speed / 1000000.0), usbdev->bus, usbdev->address); | ||
| 458 | |||
| 459 | collection_init(&usbdev->tx_xfers); | ||
| 460 | collection_init(&usbdev->rx_xfers); | ||
| 461 | |||
| 462 | collection_add(&device_list, usbdev); | ||
| 463 | |||
| 464 | if(device_add(usbdev) < 0) { | ||
| 465 | usb_disconnect(usbdev); | ||
| 466 | continue; | ||
| 467 | } | ||
| 468 | |||
| 469 | // Spin up NUM_RX_LOOPS parallel usb data retrieval loops | ||
| 470 | // Old usbmuxds used only 1 rx loop, but that leaves the | ||
| 471 | // USB port sleeping most of the time | ||
| 472 | int rx_loops = NUM_RX_LOOPS; | ||
| 473 | for (rx_loops = NUM_RX_LOOPS; rx_loops > 0; rx_loops--) { | ||
| 474 | if(start_rx_loop(usbdev) < 0) { | ||
| 475 | usbmuxd_log(LL_WARNING, "Failed to start RX loop number %d", NUM_RX_LOOPS - rx_loops); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | // Ensure we have at least 1 RX loop going | ||
| 480 | if (rx_loops == NUM_RX_LOOPS) { | ||
| 481 | usbmuxd_log(LL_FATAL, "Failed to start any RX loop for device %d-%d", | ||
| 482 | usbdev->bus, usbdev->address); | ||
| 483 | device_remove(usbdev); | ||
| 484 | usb_disconnect(usbdev); | ||
| 485 | continue; | ||
| 486 | } else if (rx_loops > 0) { | ||
| 487 | usbmuxd_log(LL_WARNING, "Failed to start all %d RX loops. Going on with %d loops. " | ||
| 488 | "This may have negative impact on device read speed.", | ||
| 489 | NUM_RX_LOOPS, NUM_RX_LOOPS - rx_loops); | ||
| 490 | } else { | ||
| 491 | usbmuxd_log(LL_DEBUG, "All %d RX loops started successfully", NUM_RX_LOOPS); | ||
| 492 | } | ||
| 493 | |||
| 494 | valid_count++; | ||
| 495 | } | ||
| 496 | |||
| 497 | // Clean out any device we didn't mark back as live | ||
| 498 | reap_dead_devices(); | ||
| 499 | |||
| 500 | libusb_free_device_list(devs, 1); | ||
| 501 | |||
| 502 | get_tick_count(&next_dev_poll_time); | ||
| 503 | next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000; | ||
| 504 | next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000; | ||
| 505 | next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000; | ||
| 506 | |||
| 507 | return valid_count; | ||
| 508 | } | ||
| 509 | |||
| 510 | const char *usb_get_serial(struct usb_device *dev) | ||
| 511 | { | ||
| 512 | if(!dev->dev) { | ||
| 513 | return NULL; | ||
| 514 | } | ||
| 515 | return dev->serial; | ||
| 516 | } | ||
| 517 | |||
| 518 | uint32_t usb_get_location(struct usb_device *dev) | ||
| 519 | { | ||
| 520 | if(!dev->dev) { | ||
| 521 | return 0; | ||
| 522 | } | ||
| 523 | return (dev->bus << 16) | dev->address; | ||
| 524 | } | ||
| 525 | |||
| 526 | uint16_t usb_get_pid(struct usb_device *dev) | ||
| 527 | { | ||
| 528 | if(!dev->dev) { | ||
| 529 | return 0; | ||
| 530 | } | ||
| 531 | return dev->pid; | ||
| 532 | } | ||
| 533 | |||
| 534 | uint64_t usb_get_speed(struct usb_device *dev) | ||
| 535 | { | ||
| 536 | if (!dev->dev) { | ||
| 537 | return 0; | ||
| 538 | } | ||
| 539 | return dev->speed; | ||
| 540 | } | ||
| 541 | |||
| 542 | void usb_get_fds(struct fdlist *list) | ||
| 543 | { | ||
| 544 | const struct libusb_pollfd **usbfds; | ||
| 545 | const struct libusb_pollfd **p; | ||
| 546 | usbfds = libusb_get_pollfds(NULL); | ||
| 547 | if(!usbfds) { | ||
| 548 | usbmuxd_log(LL_ERROR, "libusb_get_pollfds failed"); | ||
| 549 | return; | ||
| 550 | } | ||
| 551 | p = usbfds; | ||
| 552 | while(*p) { | ||
| 553 | fdlist_add(list, FD_USB, (*p)->fd, (*p)->events); | ||
| 554 | p++; | ||
| 555 | } | ||
| 556 | free(usbfds); | ||
| 557 | } | ||
| 558 | |||
| 559 | void usb_autodiscover(int enable) | ||
| 560 | { | ||
| 561 | usbmuxd_log(LL_DEBUG, "usb polling enable: %d", enable); | ||
| 562 | device_polling = enable; | ||
| 563 | } | ||
| 564 | |||
| 565 | static int dev_poll_remain_ms(void) | ||
| 566 | { | ||
| 567 | int msecs; | ||
| 568 | struct timeval tv; | ||
| 569 | if(!device_polling) | ||
| 570 | return 100000; // devices will never be polled if this is > 0 | ||
| 571 | get_tick_count(&tv); | ||
| 572 | msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000; | ||
| 573 | msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000; | ||
| 574 | if(msecs < 0) | ||
| 575 | return 0; | ||
| 576 | return msecs; | ||
| 577 | } | ||
| 578 | |||
| 579 | int usb_get_timeout(void) | ||
| 580 | { | ||
| 581 | struct timeval tv; | ||
| 582 | int msec; | ||
| 583 | int res; | ||
| 584 | int pollrem; | ||
| 585 | pollrem = dev_poll_remain_ms(); | ||
| 586 | res = libusb_get_next_timeout(NULL, &tv); | ||
| 587 | if(res == 0) | ||
| 588 | return pollrem; | ||
| 589 | if(res < 0) { | ||
| 590 | usbmuxd_log(LL_ERROR, "libusb_get_next_timeout failed: %d", res); | ||
| 591 | return pollrem; | ||
| 592 | } | ||
| 593 | msec = tv.tv_sec * 1000; | ||
| 594 | msec += tv.tv_usec / 1000; | ||
| 595 | if(msec > pollrem) | ||
| 596 | return pollrem; | ||
| 597 | return msec; | ||
| 598 | } | ||
| 599 | |||
| 600 | int usb_process(void) | ||
| 601 | { | ||
| 602 | int res; | ||
| 603 | struct timeval tv; | ||
| 604 | tv.tv_sec = tv.tv_usec = 0; | ||
| 605 | res = libusb_handle_events_timeout(NULL, &tv); | ||
| 606 | if(res < 0) { | ||
| 607 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res); | ||
| 608 | return res; | ||
| 609 | } | ||
| 610 | |||
| 611 | // reap devices marked dead due to an RX error | ||
| 612 | reap_dead_devices(); | ||
| 613 | |||
| 614 | if(dev_poll_remain_ms() <= 0) { | ||
| 615 | res = usb_discover(); | ||
| 616 | if(res < 0) { | ||
| 617 | usbmuxd_log(LL_ERROR, "usb_discover failed: %d", res); | ||
| 618 | return res; | ||
| 619 | } | ||
| 620 | } | ||
| 621 | return 0; | ||
| 622 | } | ||
| 623 | |||
| 624 | int usb_process_timeout(int msec) | ||
| 625 | { | ||
| 626 | int res; | ||
| 627 | struct timeval tleft, tcur, tfin; | ||
| 628 | get_tick_count(&tcur); | ||
| 629 | tfin.tv_sec = tcur.tv_sec + (msec / 1000); | ||
| 630 | tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000; | ||
| 631 | tfin.tv_sec += tfin.tv_usec / 1000000; | ||
| 632 | tfin.tv_usec %= 1000000; | ||
| 633 | while((tfin.tv_sec > tcur.tv_sec) || ((tfin.tv_sec == tcur.tv_sec) && (tfin.tv_usec > tcur.tv_usec))) { | ||
| 634 | tleft.tv_sec = tfin.tv_sec - tcur.tv_sec; | ||
| 635 | tleft.tv_usec = tfin.tv_usec - tcur.tv_usec; | ||
| 636 | if(tleft.tv_usec < 0) { | ||
| 637 | tleft.tv_usec += 1000000; | ||
| 638 | tleft.tv_sec -= 1; | ||
| 639 | } | ||
| 640 | res = libusb_handle_events_timeout(NULL, &tleft); | ||
| 641 | if(res < 0) { | ||
| 642 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res); | ||
| 643 | return res; | ||
| 644 | } | ||
| 645 | // reap devices marked dead due to an RX error | ||
| 646 | reap_dead_devices(); | ||
| 647 | get_tick_count(&tcur); | ||
| 648 | } | ||
| 649 | return 0; | ||
| 650 | } | ||
| 651 | |||
| 652 | int usb_init(void) | ||
| 653 | { | ||
| 654 | int res; | ||
| 655 | usbmuxd_log(LL_DEBUG, "usb_init for linux / libusb 1.0"); | ||
| 656 | |||
| 657 | devlist_failures = 0; | ||
| 658 | device_polling = 1; | ||
| 659 | res = libusb_init(NULL); | ||
| 660 | //libusb_set_debug(NULL, 3); | ||
| 661 | if(res != 0) { | ||
| 662 | usbmuxd_log(LL_FATAL, "libusb_init failed: %d", res); | ||
| 663 | return -1; | ||
| 664 | } | ||
| 665 | |||
| 666 | collection_init(&device_list); | ||
| 667 | |||
| 668 | return usb_discover(); | ||
| 669 | } | ||
| 670 | |||
| 671 | void usb_shutdown(void) | ||
| 672 | { | ||
| 673 | usbmuxd_log(LL_DEBUG, "usb_shutdown"); | ||
| 674 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 675 | device_remove(usbdev); | ||
| 676 | usb_disconnect(usbdev); | ||
| 677 | } ENDFOREACH | ||
| 678 | collection_free(&device_list); | ||
| 679 | libusb_exit(NULL); | ||
| 680 | } | ||
