Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "usb.h" |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <atomic> |
| 24 | #include <chrono> |
| 25 | #include <memory> |
| 26 | #include <mutex> |
| 27 | #include <string> |
| 28 | #include <thread> |
| 29 | #include <unordered_map> |
| 30 | |
| 31 | #include <libusb/libusb.h> |
| 32 | |
| 33 | #include <android-base/file.h> |
| 34 | #include <android-base/logging.h> |
| 35 | #include <android-base/quick_exit.h> |
| 36 | #include <android-base/stringprintf.h> |
| 37 | #include <android-base/strings.h> |
| 38 | |
| 39 | #include "adb.h" |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 40 | #include "adb_utils.h" |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 41 | #include "transport.h" |
| 42 | #include "usb.h" |
| 43 | |
| 44 | using namespace std::literals; |
| 45 | |
| 46 | using android::base::StringPrintf; |
| 47 | |
| 48 | // RAII wrappers for libusb. |
| 49 | struct ConfigDescriptorDeleter { |
| 50 | void operator()(libusb_config_descriptor* desc) { |
| 51 | libusb_free_config_descriptor(desc); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | using unique_config_descriptor = std::unique_ptr<libusb_config_descriptor, ConfigDescriptorDeleter>; |
| 56 | |
| 57 | struct DeviceHandleDeleter { |
| 58 | void operator()(libusb_device_handle* h) { |
| 59 | libusb_close(h); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | using unique_device_handle = std::unique_ptr<libusb_device_handle, DeviceHandleDeleter>; |
| 64 | |
| 65 | struct transfer_info { |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 66 | transfer_info(const char* name, uint16_t zero_mask, bool is_bulk_out) |
| 67 | : name(name), |
| 68 | transfer(libusb_alloc_transfer(0)), |
| 69 | is_bulk_out(is_bulk_out), |
| 70 | zero_mask(zero_mask) {} |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 71 | |
| 72 | ~transfer_info() { |
| 73 | libusb_free_transfer(transfer); |
| 74 | } |
| 75 | |
| 76 | const char* name; |
| 77 | libusb_transfer* transfer; |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 78 | bool is_bulk_out; |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 79 | bool transfer_complete; |
| 80 | std::condition_variable cv; |
| 81 | std::mutex mutex; |
| 82 | uint16_t zero_mask; |
| 83 | |
| 84 | void Notify() { |
| 85 | LOG(DEBUG) << "notifying " << name << " transfer complete"; |
| 86 | transfer_complete = true; |
| 87 | cv.notify_one(); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | namespace libusb { |
| 92 | struct usb_handle : public ::usb_handle { |
| 93 | usb_handle(const std::string& device_address, const std::string& serial, |
| 94 | unique_device_handle&& device_handle, uint8_t interface, uint8_t bulk_in, |
Josh Gao | ef3d343 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 95 | uint8_t bulk_out, size_t zero_mask, size_t max_packet_size) |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 96 | : device_address(device_address), |
| 97 | serial(serial), |
| 98 | closing(false), |
| 99 | device_handle(device_handle.release()), |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 100 | read("read", zero_mask, false), |
| 101 | write("write", zero_mask, true), |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 102 | interface(interface), |
| 103 | bulk_in(bulk_in), |
Josh Gao | ef3d343 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 104 | bulk_out(bulk_out), |
| 105 | max_packet_size(max_packet_size) {} |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 106 | |
| 107 | ~usb_handle() { |
| 108 | Close(); |
| 109 | } |
| 110 | |
| 111 | void Close() { |
| 112 | std::unique_lock<std::mutex> lock(device_handle_mutex); |
| 113 | // Cancelling transfers will trigger more Closes, so make sure this only happens once. |
| 114 | if (closing) { |
| 115 | return; |
| 116 | } |
| 117 | closing = true; |
| 118 | |
| 119 | // Make sure that no new transfers come in. |
| 120 | libusb_device_handle* handle = device_handle; |
| 121 | if (!handle) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | device_handle = nullptr; |
| 126 | |
| 127 | // Cancel already dispatched transfers. |
| 128 | libusb_cancel_transfer(read.transfer); |
| 129 | libusb_cancel_transfer(write.transfer); |
| 130 | |
| 131 | libusb_release_interface(handle, interface); |
| 132 | libusb_close(handle); |
| 133 | } |
| 134 | |
| 135 | std::string device_address; |
| 136 | std::string serial; |
| 137 | |
| 138 | std::atomic<bool> closing; |
| 139 | std::mutex device_handle_mutex; |
| 140 | libusb_device_handle* device_handle; |
| 141 | |
| 142 | transfer_info read; |
| 143 | transfer_info write; |
| 144 | |
| 145 | uint8_t interface; |
| 146 | uint8_t bulk_in; |
| 147 | uint8_t bulk_out; |
Josh Gao | ef3d343 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 148 | |
| 149 | size_t max_packet_size; |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | static auto& usb_handles = *new std::unordered_map<std::string, std::unique_ptr<usb_handle>>(); |
| 153 | static auto& usb_handles_mutex = *new std::mutex(); |
| 154 | |
Josh Gao | 6da1cd4 | 2017-05-12 11:21:30 -0700 | [diff] [blame] | 155 | static libusb_hotplug_callback_handle hotplug_handle; |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 156 | |
| 157 | static std::string get_device_address(libusb_device* device) { |
| 158 | return StringPrintf("usb:%d:%d", libusb_get_bus_number(device), |
| 159 | libusb_get_device_address(device)); |
| 160 | } |
| 161 | |
Elliott Hughes | 4acb360 | 2017-05-05 16:26:00 -0700 | [diff] [blame] | 162 | #if defined(__linux__) |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 163 | static std::string get_device_serial_path(libusb_device* device) { |
| 164 | uint8_t ports[7]; |
| 165 | int port_count = libusb_get_port_numbers(device, ports, 7); |
| 166 | if (port_count < 0) return ""; |
| 167 | |
| 168 | std::string path = |
| 169 | StringPrintf("/sys/bus/usb/devices/%d-%d", libusb_get_bus_number(device), ports[0]); |
| 170 | for (int port = 1; port < port_count; ++port) { |
| 171 | path += StringPrintf(".%d", ports[port]); |
| 172 | } |
| 173 | path += "/serial"; |
| 174 | return path; |
| 175 | } |
Josh Gao | 3f60a96 | 2017-05-12 14:46:50 -0700 | [diff] [blame] | 176 | |
| 177 | static std::string get_device_dev_path(libusb_device* device) { |
| 178 | uint8_t ports[7]; |
| 179 | int port_count = libusb_get_port_numbers(device, ports, 7); |
| 180 | if (port_count < 0) return ""; |
| 181 | return StringPrintf("/dev/bus/usb/%03u/%03u", libusb_get_bus_number(device), ports[0]); |
| 182 | } |
Elliott Hughes | 4acb360 | 2017-05-05 16:26:00 -0700 | [diff] [blame] | 183 | #endif |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 184 | |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 185 | static bool endpoint_is_output(uint8_t endpoint) { |
| 186 | return (endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT; |
| 187 | } |
| 188 | |
| 189 | static bool should_perform_zero_transfer(uint8_t endpoint, size_t write_length, uint16_t zero_mask) { |
| 190 | return endpoint_is_output(endpoint) && write_length != 0 && zero_mask != 0 && |
| 191 | (write_length & zero_mask) == 0; |
| 192 | } |
| 193 | |
Josh Gao | 8bf37d7 | 2017-05-05 18:19:21 -0700 | [diff] [blame] | 194 | static void process_device(libusb_device* device) { |
| 195 | std::string device_address = get_device_address(device); |
| 196 | std::string device_serial; |
| 197 | |
| 198 | // Figure out if we want to open the device. |
| 199 | libusb_device_descriptor device_desc; |
| 200 | int rc = libusb_get_device_descriptor(device, &device_desc); |
| 201 | if (rc != 0) { |
| 202 | LOG(WARNING) << "failed to get device descriptor for device at " << device_address << ": " |
| 203 | << libusb_error_name(rc); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | if (device_desc.bDeviceClass != LIBUSB_CLASS_PER_INTERFACE) { |
| 208 | // Assume that all Android devices have the device class set to per interface. |
| 209 | // TODO: Is this assumption valid? |
| 210 | LOG(VERBOSE) << "skipping device with incorrect class at " << device_address; |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | libusb_config_descriptor* config_raw; |
| 215 | rc = libusb_get_active_config_descriptor(device, &config_raw); |
| 216 | if (rc != 0) { |
| 217 | LOG(WARNING) << "failed to get active config descriptor for device at " << device_address |
| 218 | << ": " << libusb_error_name(rc); |
| 219 | return; |
| 220 | } |
| 221 | const unique_config_descriptor config(config_raw); |
| 222 | |
| 223 | // Use size_t for interface_num so <iostream>s don't mangle it. |
| 224 | size_t interface_num; |
| 225 | uint16_t zero_mask; |
| 226 | uint8_t bulk_in = 0, bulk_out = 0; |
| 227 | size_t packet_size = 0; |
| 228 | bool found_adb = false; |
| 229 | |
| 230 | for (interface_num = 0; interface_num < config->bNumInterfaces; ++interface_num) { |
| 231 | const libusb_interface& interface = config->interface[interface_num]; |
| 232 | if (interface.num_altsetting != 1) { |
| 233 | // Assume that interfaces with alternate settings aren't adb interfaces. |
| 234 | // TODO: Is this assumption valid? |
| 235 | LOG(VERBOSE) << "skipping interface with incorrect num_altsetting at " << device_address |
| 236 | << " (interface " << interface_num << ")"; |
Josh Gao | 425aefd | 2017-05-12 15:12:32 -0700 | [diff] [blame] | 237 | continue; |
Josh Gao | 8bf37d7 | 2017-05-05 18:19:21 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | const libusb_interface_descriptor& interface_desc = interface.altsetting[0]; |
| 241 | if (!is_adb_interface(interface_desc.bInterfaceClass, interface_desc.bInterfaceSubClass, |
| 242 | interface_desc.bInterfaceProtocol)) { |
| 243 | LOG(VERBOSE) << "skipping non-adb interface at " << device_address << " (interface " |
| 244 | << interface_num << ")"; |
Josh Gao | 425aefd | 2017-05-12 15:12:32 -0700 | [diff] [blame] | 245 | continue; |
Josh Gao | 8bf37d7 | 2017-05-05 18:19:21 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | LOG(VERBOSE) << "found potential adb interface at " << device_address << " (interface " |
| 249 | << interface_num << ")"; |
| 250 | |
| 251 | bool found_in = false; |
| 252 | bool found_out = false; |
| 253 | for (size_t endpoint_num = 0; endpoint_num < interface_desc.bNumEndpoints; ++endpoint_num) { |
| 254 | const auto& endpoint_desc = interface_desc.endpoint[endpoint_num]; |
| 255 | const uint8_t endpoint_addr = endpoint_desc.bEndpointAddress; |
| 256 | const uint8_t endpoint_attr = endpoint_desc.bmAttributes; |
| 257 | |
| 258 | const uint8_t transfer_type = endpoint_attr & LIBUSB_TRANSFER_TYPE_MASK; |
| 259 | |
| 260 | if (transfer_type != LIBUSB_TRANSFER_TYPE_BULK) { |
Josh Gao | 425aefd | 2017-05-12 15:12:32 -0700 | [diff] [blame] | 261 | continue; |
Josh Gao | 8bf37d7 | 2017-05-05 18:19:21 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | if (endpoint_is_output(endpoint_addr) && !found_out) { |
| 265 | found_out = true; |
| 266 | bulk_out = endpoint_addr; |
| 267 | zero_mask = endpoint_desc.wMaxPacketSize - 1; |
| 268 | } else if (!endpoint_is_output(endpoint_addr) && !found_in) { |
| 269 | found_in = true; |
| 270 | bulk_in = endpoint_addr; |
| 271 | } |
| 272 | |
| 273 | size_t endpoint_packet_size = endpoint_desc.wMaxPacketSize; |
| 274 | CHECK(endpoint_packet_size != 0); |
| 275 | if (packet_size == 0) { |
| 276 | packet_size = endpoint_packet_size; |
| 277 | } else { |
| 278 | CHECK(packet_size == endpoint_packet_size); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (found_in && found_out) { |
| 283 | found_adb = true; |
| 284 | break; |
| 285 | } else { |
| 286 | LOG(VERBOSE) << "rejecting potential adb interface at " << device_address |
| 287 | << "(interface " << interface_num << "): missing bulk endpoints " |
| 288 | << "(found_in = " << found_in << ", found_out = " << found_out << ")"; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if (!found_adb) { |
| 293 | LOG(VERBOSE) << "skipping device with no adb interfaces at " << device_address; |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | { |
| 298 | std::unique_lock<std::mutex> lock(usb_handles_mutex); |
| 299 | if (usb_handles.find(device_address) != usb_handles.end()) { |
| 300 | LOG(VERBOSE) << "device at " << device_address |
| 301 | << " has already been registered, skipping"; |
| 302 | return; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | bool writable = true; |
| 307 | libusb_device_handle* handle_raw = nullptr; |
| 308 | rc = libusb_open(device, &handle_raw); |
| 309 | unique_device_handle handle(handle_raw); |
| 310 | if (rc == 0) { |
| 311 | LOG(DEBUG) << "successfully opened adb device at " << device_address << ", " |
| 312 | << StringPrintf("bulk_in = %#x, bulk_out = %#x", bulk_in, bulk_out); |
| 313 | |
| 314 | device_serial.resize(255); |
| 315 | rc = libusb_get_string_descriptor_ascii(handle_raw, device_desc.iSerialNumber, |
| 316 | reinterpret_cast<unsigned char*>(&device_serial[0]), |
| 317 | device_serial.length()); |
| 318 | if (rc == 0) { |
| 319 | LOG(WARNING) << "received empty serial from device at " << device_address; |
| 320 | return; |
| 321 | } else if (rc < 0) { |
| 322 | LOG(WARNING) << "failed to get serial from device at " << device_address |
| 323 | << libusb_error_name(rc); |
| 324 | return; |
| 325 | } |
| 326 | device_serial.resize(rc); |
| 327 | |
| 328 | // WARNING: this isn't released via RAII. |
| 329 | rc = libusb_claim_interface(handle.get(), interface_num); |
| 330 | if (rc != 0) { |
| 331 | LOG(WARNING) << "failed to claim adb interface for device '" << device_serial << "'" |
| 332 | << libusb_error_name(rc); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | for (uint8_t endpoint : {bulk_in, bulk_out}) { |
| 337 | rc = libusb_clear_halt(handle.get(), endpoint); |
| 338 | if (rc != 0) { |
| 339 | LOG(WARNING) << "failed to clear halt on device '" << device_serial |
| 340 | << "' endpoint 0x" << std::hex << endpoint << ": " |
| 341 | << libusb_error_name(rc); |
| 342 | libusb_release_interface(handle.get(), interface_num); |
| 343 | return; |
| 344 | } |
| 345 | } |
| 346 | } else { |
| 347 | LOG(WARNING) << "failed to open usb device at " << device_address << ": " |
| 348 | << libusb_error_name(rc); |
| 349 | writable = false; |
| 350 | |
| 351 | #if defined(__linux__) |
| 352 | // libusb doesn't think we should be messing around with devices we don't have |
| 353 | // write access to, but Linux at least lets us get the serial number anyway. |
| 354 | if (!android::base::ReadFileToString(get_device_serial_path(device), &device_serial)) { |
| 355 | // We don't actually want to treat an unknown serial as an error because |
| 356 | // devices aren't able to communicate a serial number in early bringup. |
| 357 | // http://b/20883914 |
| 358 | device_serial = "unknown"; |
| 359 | } |
| 360 | device_serial = android::base::Trim(device_serial); |
| 361 | #else |
| 362 | // On Mac OS and Windows, we're screwed. But I don't think this situation actually |
| 363 | // happens on those OSes. |
| 364 | return; |
| 365 | #endif |
| 366 | } |
| 367 | |
| 368 | auto result = |
| 369 | std::make_unique<usb_handle>(device_address, device_serial, std::move(handle), |
| 370 | interface_num, bulk_in, bulk_out, zero_mask, packet_size); |
| 371 | usb_handle* usb_handle_raw = result.get(); |
| 372 | |
| 373 | { |
| 374 | std::unique_lock<std::mutex> lock(usb_handles_mutex); |
| 375 | usb_handles[device_address] = std::move(result); |
Josh Gao | 8bf37d7 | 2017-05-05 18:19:21 -0700 | [diff] [blame] | 376 | |
Josh Gao | 7dd382d | 2017-06-01 15:34:21 -0700 | [diff] [blame] | 377 | register_usb_transport(usb_handle_raw, device_serial.c_str(), device_address.c_str(), |
| 378 | writable); |
| 379 | } |
Josh Gao | 8bf37d7 | 2017-05-05 18:19:21 -0700 | [diff] [blame] | 380 | LOG(INFO) << "registered new usb device '" << device_serial << "'"; |
| 381 | } |
| 382 | |
Josh Gao | 3f60a96 | 2017-05-12 14:46:50 -0700 | [diff] [blame] | 383 | static std::atomic<int> connecting_devices(0); |
| 384 | |
| 385 | static void device_connected(libusb_device* device) { |
| 386 | #if defined(__linux__) |
| 387 | // Android's host linux libusb uses netlink instead of udev for device hotplug notification, |
Josh Gao | 5b8b10e | 2017-06-01 15:42:26 -0700 | [diff] [blame] | 388 | // which means we can get hotplug notifications before udev has updated ownership/perms on the |
| 389 | // device. Since we're not going to be able to link against the system's libudev any time soon, |
| 390 | // hack around this by inserting a sleep. |
Josh Gao | 3f60a96 | 2017-05-12 14:46:50 -0700 | [diff] [blame] | 391 | auto thread = std::thread([device]() { |
| 392 | std::string device_path = get_device_dev_path(device); |
Josh Gao | 5b8b10e | 2017-06-01 15:42:26 -0700 | [diff] [blame] | 393 | std::this_thread::sleep_for(1s); |
Josh Gao | 3f60a96 | 2017-05-12 14:46:50 -0700 | [diff] [blame] | 394 | |
| 395 | process_device(device); |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 396 | if (--connecting_devices == 0) { |
| 397 | adb_notify_device_scan_complete(); |
| 398 | } |
Josh Gao | 3f60a96 | 2017-05-12 14:46:50 -0700 | [diff] [blame] | 399 | }); |
| 400 | thread.detach(); |
| 401 | #else |
| 402 | process_device(device); |
| 403 | #endif |
| 404 | } |
| 405 | |
| 406 | static void device_disconnected(libusb_device* device) { |
Josh Gao | 6da1cd4 | 2017-05-12 11:21:30 -0700 | [diff] [blame] | 407 | std::string device_address = get_device_address(device); |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 408 | |
Josh Gao | 6da1cd4 | 2017-05-12 11:21:30 -0700 | [diff] [blame] | 409 | LOG(INFO) << "device disconnected: " << device_address; |
| 410 | std::unique_lock<std::mutex> lock(usb_handles_mutex); |
| 411 | auto it = usb_handles.find(device_address); |
| 412 | if (it != usb_handles.end()) { |
| 413 | if (!it->second->device_handle) { |
| 414 | // If the handle is null, we were never able to open the device. |
| 415 | unregister_usb_transport(it->second.get()); |
Josh Gao | 60b8c26 | 2017-06-05 15:08:13 -0700 | [diff] [blame] | 416 | usb_handles.erase(it); |
| 417 | } else { |
| 418 | // Closure of the transport will erase the usb_handle. |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 419 | } |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 423 | static auto& hotplug_queue = *new BlockingQueue<std::pair<libusb_hotplug_event, libusb_device*>>(); |
| 424 | static void hotplug_thread() { |
| 425 | adb_thread_setname("libusb hotplug"); |
| 426 | while (true) { |
| 427 | hotplug_queue.PopAll([](std::pair<libusb_hotplug_event, libusb_device*> pair) { |
| 428 | libusb_hotplug_event event = pair.first; |
| 429 | libusb_device* device = pair.second; |
| 430 | if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) { |
| 431 | device_connected(device); |
| 432 | } else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) { |
| 433 | device_disconnected(device); |
| 434 | } |
| 435 | }); |
| 436 | } |
| 437 | } |
| 438 | |
Josh Gao | 6da1cd4 | 2017-05-12 11:21:30 -0700 | [diff] [blame] | 439 | static int hotplug_callback(libusb_context*, libusb_device* device, libusb_hotplug_event event, |
| 440 | void*) { |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 441 | // We're called with the libusb lock taken. Call these on a separate thread outside of this |
Josh Gao | d1a3e8f | 2017-05-30 17:03:41 -0700 | [diff] [blame] | 442 | // function so that the usb_handle mutex is always taken before the libusb mutex. |
Josh Gao | 0b13c89 | 2017-05-31 11:54:56 -0700 | [diff] [blame] | 443 | static std::once_flag once; |
| 444 | std::call_once(once, []() { std::thread(hotplug_thread).detach(); }); |
| 445 | |
| 446 | if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) { |
| 447 | ++connecting_devices; |
| 448 | } |
| 449 | hotplug_queue.Push({event, device}); |
Josh Gao | 6da1cd4 | 2017-05-12 11:21:30 -0700 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 453 | void usb_init() { |
| 454 | LOG(DEBUG) << "initializing libusb..."; |
| 455 | int rc = libusb_init(nullptr); |
| 456 | if (rc != 0) { |
| 457 | LOG(FATAL) << "failed to initialize libusb: " << libusb_error_name(rc); |
| 458 | } |
| 459 | |
Josh Gao | 6da1cd4 | 2017-05-12 11:21:30 -0700 | [diff] [blame] | 460 | // Register the hotplug callback. |
| 461 | rc = libusb_hotplug_register_callback( |
| 462 | nullptr, static_cast<libusb_hotplug_event>(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | |
| 463 | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), |
| 464 | LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, |
| 465 | LIBUSB_CLASS_PER_INTERFACE, hotplug_callback, nullptr, &hotplug_handle); |
| 466 | |
| 467 | if (rc != LIBUSB_SUCCESS) { |
| 468 | LOG(FATAL) << "failed to register libusb hotplug callback"; |
| 469 | } |
| 470 | |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 471 | // Spawn a thread for libusb_handle_events. |
| 472 | std::thread([]() { |
| 473 | adb_thread_setname("libusb"); |
| 474 | while (true) { |
| 475 | libusb_handle_events(nullptr); |
| 476 | } |
| 477 | }).detach(); |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void usb_cleanup() { |
Josh Gao | 6da1cd4 | 2017-05-12 11:21:30 -0700 | [diff] [blame] | 481 | libusb_hotplug_deregister_callback(nullptr, hotplug_handle); |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // Dispatch a libusb transfer, unlock |device_lock|, and then wait for the result. |
| 485 | static int perform_usb_transfer(usb_handle* h, transfer_info* info, |
| 486 | std::unique_lock<std::mutex> device_lock) { |
| 487 | libusb_transfer* transfer = info->transfer; |
| 488 | |
| 489 | transfer->user_data = info; |
| 490 | transfer->callback = [](libusb_transfer* transfer) { |
| 491 | transfer_info* info = static_cast<transfer_info*>(transfer->user_data); |
| 492 | |
| 493 | LOG(DEBUG) << info->name << " transfer callback entered"; |
| 494 | |
| 495 | // Make sure that the original submitter has made it to the condition_variable wait. |
| 496 | std::unique_lock<std::mutex> lock(info->mutex); |
| 497 | |
| 498 | LOG(DEBUG) << info->name << " callback successfully acquired lock"; |
| 499 | |
| 500 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { |
| 501 | LOG(WARNING) << info->name |
| 502 | << " transfer failed: " << libusb_error_name(transfer->status); |
| 503 | info->Notify(); |
| 504 | return; |
| 505 | } |
| 506 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 507 | // usb_read() can return when receiving some data. |
| 508 | if (info->is_bulk_out && transfer->actual_length != transfer->length) { |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 509 | LOG(DEBUG) << info->name << " transfer incomplete, resubmitting"; |
| 510 | transfer->length -= transfer->actual_length; |
| 511 | transfer->buffer += transfer->actual_length; |
| 512 | int rc = libusb_submit_transfer(transfer); |
| 513 | if (rc != 0) { |
| 514 | LOG(WARNING) << "failed to submit " << info->name |
| 515 | << " transfer: " << libusb_error_name(rc); |
| 516 | transfer->status = LIBUSB_TRANSFER_ERROR; |
| 517 | info->Notify(); |
| 518 | } |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (should_perform_zero_transfer(transfer->endpoint, transfer->length, info->zero_mask)) { |
| 523 | LOG(DEBUG) << "submitting zero-length write"; |
| 524 | transfer->length = 0; |
| 525 | int rc = libusb_submit_transfer(transfer); |
| 526 | if (rc != 0) { |
| 527 | LOG(WARNING) << "failed to submit zero-length write: " << libusb_error_name(rc); |
| 528 | transfer->status = LIBUSB_TRANSFER_ERROR; |
| 529 | info->Notify(); |
| 530 | } |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | LOG(VERBOSE) << info->name << "transfer fully complete"; |
| 535 | info->Notify(); |
| 536 | }; |
| 537 | |
| 538 | LOG(DEBUG) << "locking " << info->name << " transfer_info mutex"; |
| 539 | std::unique_lock<std::mutex> lock(info->mutex); |
| 540 | info->transfer_complete = false; |
| 541 | LOG(DEBUG) << "submitting " << info->name << " transfer"; |
| 542 | int rc = libusb_submit_transfer(transfer); |
| 543 | if (rc != 0) { |
| 544 | LOG(WARNING) << "failed to submit " << info->name << " transfer: " << libusb_error_name(rc); |
| 545 | errno = EIO; |
| 546 | return -1; |
| 547 | } |
| 548 | |
| 549 | LOG(DEBUG) << info->name << " transfer successfully submitted"; |
| 550 | device_lock.unlock(); |
| 551 | info->cv.wait(lock, [info]() { return info->transfer_complete; }); |
| 552 | if (transfer->status != 0) { |
| 553 | errno = EIO; |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | int usb_write(usb_handle* h, const void* d, int len) { |
| 561 | LOG(DEBUG) << "usb_write of length " << len; |
| 562 | |
| 563 | std::unique_lock<std::mutex> lock(h->device_handle_mutex); |
| 564 | if (!h->device_handle) { |
| 565 | errno = EIO; |
| 566 | return -1; |
| 567 | } |
| 568 | |
| 569 | transfer_info* info = &h->write; |
| 570 | info->transfer->dev_handle = h->device_handle; |
| 571 | info->transfer->flags = 0; |
| 572 | info->transfer->endpoint = h->bulk_out; |
| 573 | info->transfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 574 | info->transfer->length = len; |
| 575 | info->transfer->buffer = reinterpret_cast<unsigned char*>(const_cast<void*>(d)); |
| 576 | info->transfer->num_iso_packets = 0; |
| 577 | |
| 578 | int rc = perform_usb_transfer(h, info, std::move(lock)); |
| 579 | LOG(DEBUG) << "usb_write(" << len << ") = " << rc; |
| 580 | return rc; |
| 581 | } |
| 582 | |
| 583 | int usb_read(usb_handle* h, void* d, int len) { |
| 584 | LOG(DEBUG) << "usb_read of length " << len; |
| 585 | |
| 586 | std::unique_lock<std::mutex> lock(h->device_handle_mutex); |
| 587 | if (!h->device_handle) { |
| 588 | errno = EIO; |
| 589 | return -1; |
| 590 | } |
| 591 | |
| 592 | transfer_info* info = &h->read; |
| 593 | info->transfer->dev_handle = h->device_handle; |
| 594 | info->transfer->flags = 0; |
| 595 | info->transfer->endpoint = h->bulk_in; |
| 596 | info->transfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 597 | info->transfer->length = len; |
| 598 | info->transfer->buffer = reinterpret_cast<unsigned char*>(d); |
| 599 | info->transfer->num_iso_packets = 0; |
| 600 | |
| 601 | int rc = perform_usb_transfer(h, info, std::move(lock)); |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 602 | LOG(DEBUG) << "usb_read(" << len << ") = " << rc << ", actual_length " |
| 603 | << info->transfer->actual_length; |
| 604 | if (rc < 0) { |
| 605 | return rc; |
| 606 | } |
| 607 | return info->transfer->actual_length; |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | int usb_close(usb_handle* h) { |
| 611 | std::unique_lock<std::mutex> lock(usb_handles_mutex); |
| 612 | auto it = usb_handles.find(h->device_address); |
| 613 | if (it == usb_handles.end()) { |
| 614 | LOG(FATAL) << "attempted to close unregistered usb_handle for '" << h->serial << "'"; |
| 615 | } |
| 616 | usb_handles.erase(h->device_address); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | void usb_kick(usb_handle* h) { |
| 621 | h->Close(); |
| 622 | } |
Josh Gao | ef3d343 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 623 | |
| 624 | size_t usb_get_max_packet_size(usb_handle* h) { |
| 625 | CHECK(h->max_packet_size != 0); |
| 626 | return h->max_packet_size; |
| 627 | } |
| 628 | |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 629 | } // namespace libusb |