blob: 7025f283ce784404fd1f93fc98c2f36b305f113b [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"
Josh Gao4b640842017-05-31 11:54:56 -070040#include "adb_utils.h"
Josh Gaob7366922016-09-28 12:32:45 -070041#include "transport.h"
42#include "usb.h"
43
44using namespace std::literals;
45
46using android::base::StringPrintf;
47
48// RAII wrappers for libusb.
49struct ConfigDescriptorDeleter {
50 void operator()(libusb_config_descriptor* desc) {
51 libusb_free_config_descriptor(desc);
52 }
53};
54
55using unique_config_descriptor = std::unique_ptr<libusb_config_descriptor, ConfigDescriptorDeleter>;
56
57struct DeviceHandleDeleter {
58 void operator()(libusb_device_handle* h) {
59 libusb_close(h);
60 }
61};
62
63using unique_device_handle = std::unique_ptr<libusb_device_handle, DeviceHandleDeleter>;
64
65struct transfer_info {
Yabin Cui3cf1b362017-03-10 16:01:01 -080066 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 Gaob7366922016-09-28 12:32:45 -070071
72 ~transfer_info() {
73 libusb_free_transfer(transfer);
74 }
75
76 const char* name;
77 libusb_transfer* transfer;
Yabin Cui3cf1b362017-03-10 16:01:01 -080078 bool is_bulk_out;
Josh Gaob7366922016-09-28 12:32:45 -070079 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
91namespace libusb {
92struct 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 Gao3734cf02017-05-02 15:01:09 -070095 uint8_t bulk_out, size_t zero_mask, size_t max_packet_size)
Josh Gaob7366922016-09-28 12:32:45 -070096 : device_address(device_address),
97 serial(serial),
98 closing(false),
99 device_handle(device_handle.release()),
Yabin Cui3cf1b362017-03-10 16:01:01 -0800100 read("read", zero_mask, false),
101 write("write", zero_mask, true),
Josh Gaob7366922016-09-28 12:32:45 -0700102 interface(interface),
103 bulk_in(bulk_in),
Josh Gao3734cf02017-05-02 15:01:09 -0700104 bulk_out(bulk_out),
105 max_packet_size(max_packet_size) {}
Josh Gaob7366922016-09-28 12:32:45 -0700106
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 Gao3734cf02017-05-02 15:01:09 -0700148
149 size_t max_packet_size;
Josh Gaob7366922016-09-28 12:32:45 -0700150};
151
152static auto& usb_handles = *new std::unordered_map<std::string, std::unique_ptr<usb_handle>>();
153static auto& usb_handles_mutex = *new std::mutex();
154
Josh Gao95238412017-05-12 11:21:30 -0700155static libusb_hotplug_callback_handle hotplug_handle;
Josh Gaob7366922016-09-28 12:32:45 -0700156
157static 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 Hughesac16a0f2017-05-05 16:26:00 -0700162#if defined(__linux__)
Elliott Hughes3e9e74e2017-05-03 17:25:34 -0700163static 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 Gao70dc7372017-05-12 14:46:50 -0700176
177static 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 Hughesac16a0f2017-05-05 16:26:00 -0700183#endif
Elliott Hughes3e9e74e2017-05-03 17:25:34 -0700184
Josh Gaob7366922016-09-28 12:32:45 -0700185static bool endpoint_is_output(uint8_t endpoint) {
186 return (endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT;
187}
188
189static 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 Gao9a700fd2017-05-05 18:19:21 -0700194static 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 Gaoa96dc5f2017-05-12 15:12:32 -0700237 continue;
Josh Gao9a700fd2017-05-05 18:19:21 -0700238 }
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 Gaoa96dc5f2017-05-12 15:12:32 -0700245 continue;
Josh Gao9a700fd2017-05-05 18:19:21 -0700246 }
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 Gaoa96dc5f2017-05-12 15:12:32 -0700261 continue;
Josh Gao9a700fd2017-05-05 18:19:21 -0700262 }
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 Gao9a700fd2017-05-05 18:19:21 -0700376
Josh Gao1a2eacc2017-06-01 15:34:21 -0700377 register_usb_transport(usb_handle_raw, device_serial.c_str(), device_address.c_str(),
378 writable);
379 }
Josh Gao9a700fd2017-05-05 18:19:21 -0700380 LOG(INFO) << "registered new usb device '" << device_serial << "'";
381}
382
Josh Gao70dc7372017-05-12 14:46:50 -0700383static std::atomic<int> connecting_devices(0);
384
385static 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 Gaobe9c8372017-06-01 15:42:26 -0700388 // 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 Gao70dc7372017-05-12 14:46:50 -0700391 auto thread = std::thread([device]() {
392 std::string device_path = get_device_dev_path(device);
Josh Gaobe9c8372017-06-01 15:42:26 -0700393 std::this_thread::sleep_for(1s);
Josh Gao70dc7372017-05-12 14:46:50 -0700394
395 process_device(device);
Josh Gao4b640842017-05-31 11:54:56 -0700396 if (--connecting_devices == 0) {
397 adb_notify_device_scan_complete();
398 }
Josh Gao70dc7372017-05-12 14:46:50 -0700399 });
400 thread.detach();
401#else
402 process_device(device);
403#endif
404}
405
406static void device_disconnected(libusb_device* device) {
Josh Gao95238412017-05-12 11:21:30 -0700407 std::string device_address = get_device_address(device);
Josh Gaob7366922016-09-28 12:32:45 -0700408
Josh Gao95238412017-05-12 11:21:30 -0700409 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.
Josh Gao4d299742017-09-13 13:40:57 -0700415
416 // Temporarily release the usb handles mutex to avoid deadlock.
417 std::unique_ptr<usb_handle> handle = std::move(it->second);
Josh Gao8bb766e2017-06-05 15:08:13 -0700418 usb_handles.erase(it);
Josh Gao4d299742017-09-13 13:40:57 -0700419 lock.unlock();
420 unregister_usb_transport(handle.get());
421 lock.lock();
Josh Gao8bb766e2017-06-05 15:08:13 -0700422 } else {
423 // Closure of the transport will erase the usb_handle.
Josh Gaob7366922016-09-28 12:32:45 -0700424 }
Josh Gaob7366922016-09-28 12:32:45 -0700425 }
426}
427
Josh Gao4b640842017-05-31 11:54:56 -0700428static auto& hotplug_queue = *new BlockingQueue<std::pair<libusb_hotplug_event, libusb_device*>>();
429static void hotplug_thread() {
430 adb_thread_setname("libusb hotplug");
431 while (true) {
432 hotplug_queue.PopAll([](std::pair<libusb_hotplug_event, libusb_device*> pair) {
433 libusb_hotplug_event event = pair.first;
434 libusb_device* device = pair.second;
435 if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) {
436 device_connected(device);
437 } else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) {
438 device_disconnected(device);
439 }
440 });
441 }
442}
443
Josh Gao95238412017-05-12 11:21:30 -0700444static int hotplug_callback(libusb_context*, libusb_device* device, libusb_hotplug_event event,
445 void*) {
Josh Gao4b640842017-05-31 11:54:56 -0700446 // We're called with the libusb lock taken. Call these on a separate thread outside of this
Josh Gao09628bb2017-05-30 17:03:41 -0700447 // function so that the usb_handle mutex is always taken before the libusb mutex.
Josh Gao4b640842017-05-31 11:54:56 -0700448 static std::once_flag once;
449 std::call_once(once, []() { std::thread(hotplug_thread).detach(); });
450
451 if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) {
452 ++connecting_devices;
453 }
454 hotplug_queue.Push({event, device});
Josh Gao95238412017-05-12 11:21:30 -0700455 return 0;
456}
457
Josh Gaob7366922016-09-28 12:32:45 -0700458void usb_init() {
459 LOG(DEBUG) << "initializing libusb...";
460 int rc = libusb_init(nullptr);
461 if (rc != 0) {
462 LOG(FATAL) << "failed to initialize libusb: " << libusb_error_name(rc);
463 }
464
Josh Gao95238412017-05-12 11:21:30 -0700465 // Register the hotplug callback.
466 rc = libusb_hotplug_register_callback(
467 nullptr, static_cast<libusb_hotplug_event>(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
468 LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
469 LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY,
470 LIBUSB_CLASS_PER_INTERFACE, hotplug_callback, nullptr, &hotplug_handle);
471
472 if (rc != LIBUSB_SUCCESS) {
473 LOG(FATAL) << "failed to register libusb hotplug callback";
474 }
475
Josh Gaob7366922016-09-28 12:32:45 -0700476 // Spawn a thread for libusb_handle_events.
477 std::thread([]() {
478 adb_thread_setname("libusb");
479 while (true) {
480 libusb_handle_events(nullptr);
481 }
482 }).detach();
Josh Gao165460f2017-05-09 13:43:35 -0700483}
484
485void usb_cleanup() {
Josh Gao95238412017-05-12 11:21:30 -0700486 libusb_hotplug_deregister_callback(nullptr, hotplug_handle);
Josh Gaob7366922016-09-28 12:32:45 -0700487}
488
489// Dispatch a libusb transfer, unlock |device_lock|, and then wait for the result.
490static int perform_usb_transfer(usb_handle* h, transfer_info* info,
491 std::unique_lock<std::mutex> device_lock) {
492 libusb_transfer* transfer = info->transfer;
493
494 transfer->user_data = info;
495 transfer->callback = [](libusb_transfer* transfer) {
496 transfer_info* info = static_cast<transfer_info*>(transfer->user_data);
497
498 LOG(DEBUG) << info->name << " transfer callback entered";
499
500 // Make sure that the original submitter has made it to the condition_variable wait.
501 std::unique_lock<std::mutex> lock(info->mutex);
502
503 LOG(DEBUG) << info->name << " callback successfully acquired lock";
504
505 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
506 LOG(WARNING) << info->name
507 << " transfer failed: " << libusb_error_name(transfer->status);
508 info->Notify();
509 return;
510 }
511
Yabin Cui3cf1b362017-03-10 16:01:01 -0800512 // usb_read() can return when receiving some data.
513 if (info->is_bulk_out && transfer->actual_length != transfer->length) {
Josh Gaob7366922016-09-28 12:32:45 -0700514 LOG(DEBUG) << info->name << " transfer incomplete, resubmitting";
515 transfer->length -= transfer->actual_length;
516 transfer->buffer += transfer->actual_length;
517 int rc = libusb_submit_transfer(transfer);
518 if (rc != 0) {
519 LOG(WARNING) << "failed to submit " << info->name
520 << " transfer: " << libusb_error_name(rc);
521 transfer->status = LIBUSB_TRANSFER_ERROR;
522 info->Notify();
523 }
524 return;
525 }
526
527 if (should_perform_zero_transfer(transfer->endpoint, transfer->length, info->zero_mask)) {
528 LOG(DEBUG) << "submitting zero-length write";
529 transfer->length = 0;
530 int rc = libusb_submit_transfer(transfer);
531 if (rc != 0) {
532 LOG(WARNING) << "failed to submit zero-length write: " << libusb_error_name(rc);
533 transfer->status = LIBUSB_TRANSFER_ERROR;
534 info->Notify();
535 }
536 return;
537 }
538
539 LOG(VERBOSE) << info->name << "transfer fully complete";
540 info->Notify();
541 };
542
543 LOG(DEBUG) << "locking " << info->name << " transfer_info mutex";
544 std::unique_lock<std::mutex> lock(info->mutex);
545 info->transfer_complete = false;
546 LOG(DEBUG) << "submitting " << info->name << " transfer";
547 int rc = libusb_submit_transfer(transfer);
548 if (rc != 0) {
549 LOG(WARNING) << "failed to submit " << info->name << " transfer: " << libusb_error_name(rc);
550 errno = EIO;
551 return -1;
552 }
553
554 LOG(DEBUG) << info->name << " transfer successfully submitted";
555 device_lock.unlock();
556 info->cv.wait(lock, [info]() { return info->transfer_complete; });
557 if (transfer->status != 0) {
558 errno = EIO;
559 return -1;
560 }
561
562 return 0;
563}
564
565int usb_write(usb_handle* h, const void* d, int len) {
566 LOG(DEBUG) << "usb_write of length " << len;
567
568 std::unique_lock<std::mutex> lock(h->device_handle_mutex);
569 if (!h->device_handle) {
570 errno = EIO;
571 return -1;
572 }
573
574 transfer_info* info = &h->write;
575 info->transfer->dev_handle = h->device_handle;
576 info->transfer->flags = 0;
577 info->transfer->endpoint = h->bulk_out;
578 info->transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
579 info->transfer->length = len;
580 info->transfer->buffer = reinterpret_cast<unsigned char*>(const_cast<void*>(d));
581 info->transfer->num_iso_packets = 0;
582
583 int rc = perform_usb_transfer(h, info, std::move(lock));
584 LOG(DEBUG) << "usb_write(" << len << ") = " << rc;
585 return rc;
586}
587
588int usb_read(usb_handle* h, void* d, int len) {
589 LOG(DEBUG) << "usb_read of length " << len;
590
591 std::unique_lock<std::mutex> lock(h->device_handle_mutex);
592 if (!h->device_handle) {
593 errno = EIO;
594 return -1;
595 }
596
597 transfer_info* info = &h->read;
598 info->transfer->dev_handle = h->device_handle;
599 info->transfer->flags = 0;
600 info->transfer->endpoint = h->bulk_in;
601 info->transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
602 info->transfer->length = len;
603 info->transfer->buffer = reinterpret_cast<unsigned char*>(d);
604 info->transfer->num_iso_packets = 0;
605
606 int rc = perform_usb_transfer(h, info, std::move(lock));
Yabin Cui3cf1b362017-03-10 16:01:01 -0800607 LOG(DEBUG) << "usb_read(" << len << ") = " << rc << ", actual_length "
608 << info->transfer->actual_length;
609 if (rc < 0) {
610 return rc;
611 }
612 return info->transfer->actual_length;
Josh Gaob7366922016-09-28 12:32:45 -0700613}
614
615int usb_close(usb_handle* h) {
616 std::unique_lock<std::mutex> lock(usb_handles_mutex);
617 auto it = usb_handles.find(h->device_address);
618 if (it == usb_handles.end()) {
619 LOG(FATAL) << "attempted to close unregistered usb_handle for '" << h->serial << "'";
620 }
621 usb_handles.erase(h->device_address);
622 return 0;
623}
624
625void usb_kick(usb_handle* h) {
626 h->Close();
627}
Josh Gao3734cf02017-05-02 15:01:09 -0700628
629size_t usb_get_max_packet_size(usb_handle* h) {
630 CHECK(h->max_packet_size != 0);
631 return h->max_packet_size;
632}
633
Josh Gaob7366922016-09-28 12:32:45 -0700634} // namespace libusb