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