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