The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRANSPORT |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
Pirama Arumuga Nainar | 5231aff | 2018-08-08 10:33:24 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 21 | #include "sysdeps.h" |
| 22 | #include "transport.h" |
| 23 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include "adb.h" |
| 29 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 30 | #if ADB_HOST |
| 31 | |
Josh Gao | a82f246 | 2017-12-06 15:06:14 -0800 | [diff] [blame] | 32 | #if defined(__APPLE__) |
| 33 | #define CHECK_PACKET_OVERFLOW 0 |
| 34 | #else |
| 35 | #define CHECK_PACKET_OVERFLOW 1 |
| 36 | #endif |
| 37 | |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 38 | // Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 39 | // to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html. |
| 40 | static int UsbReadMessage(usb_handle* h, amessage* msg) { |
| 41 | D("UsbReadMessage"); |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 42 | |
Josh Gao | a82f246 | 2017-12-06 15:06:14 -0800 | [diff] [blame] | 43 | #if CHECK_PACKET_OVERFLOW |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 44 | size_t usb_packet_size = usb_get_max_packet_size(h); |
Josh Gao | c441818 | 2017-08-28 14:43:24 -0700 | [diff] [blame] | 45 | CHECK_GE(usb_packet_size, sizeof(*msg)); |
| 46 | CHECK_LT(usb_packet_size, 4096ULL); |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 47 | |
| 48 | char buffer[4096]; |
| 49 | int n = usb_read(h, buffer, usb_packet_size); |
| 50 | if (n != sizeof(*msg)) { |
| 51 | D("usb_read returned unexpected length %d (expected %zu)", n, sizeof(*msg)); |
| 52 | return -1; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 53 | } |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 54 | memcpy(msg, buffer, sizeof(*msg)); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 55 | return n; |
Josh Gao | a82f246 | 2017-12-06 15:06:14 -0800 | [diff] [blame] | 56 | #else |
| 57 | return usb_read(h, msg, sizeof(*msg)); |
| 58 | #endif |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 61 | // Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 62 | // to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html. |
| 63 | static int UsbReadPayload(usb_handle* h, apacket* p) { |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 64 | D("UsbReadPayload(%d)", p->msg.data_length); |
| 65 | |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 66 | if (p->msg.data_length > MAX_PAYLOAD) { |
Josh Gao | b14756a | 2018-02-02 14:38:04 -0800 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | |
Josh Gao | a82f246 | 2017-12-06 15:06:14 -0800 | [diff] [blame] | 70 | #if CHECK_PACKET_OVERFLOW |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 71 | size_t usb_packet_size = usb_get_max_packet_size(h); |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 72 | |
| 73 | // Round the data length up to the nearest packet size boundary. |
| 74 | // The device won't send a zero packet for packet size aligned payloads, |
| 75 | // so don't read any more packets than needed. |
| 76 | size_t len = p->msg.data_length; |
| 77 | size_t rem_size = len % usb_packet_size; |
| 78 | if (rem_size) { |
| 79 | len += usb_packet_size - rem_size; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 80 | } |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 81 | |
| 82 | p->payload.resize(len); |
| 83 | int rc = usb_read(h, &p->payload[0], p->payload.size()); |
| 84 | if (rc != static_cast<int>(p->msg.data_length)) { |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | p->payload.resize(rc); |
| 89 | return rc; |
Josh Gao | a82f246 | 2017-12-06 15:06:14 -0800 | [diff] [blame] | 90 | #else |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 91 | p->payload.resize(p->msg.data_length); |
| 92 | return usb_read(h, &p->payload[0], p->payload.size()); |
Josh Gao | a82f246 | 2017-12-06 15:06:14 -0800 | [diff] [blame] | 93 | #endif |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 96 | static int remote_read(apacket* p, usb_handle* usb) { |
| 97 | int n = UsbReadMessage(usb, &p->msg); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 98 | if (n < 0) { |
| 99 | D("remote usb: read terminated (message)"); |
| 100 | return -1; |
| 101 | } |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 102 | if (static_cast<size_t>(n) != sizeof(p->msg)) { |
| 103 | D("remote usb: read received unexpected header length %d", n); |
| 104 | return -1; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 105 | } |
| 106 | if (p->msg.data_length) { |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 107 | n = UsbReadPayload(usb, p); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 108 | if (n < 0) { |
| 109 | D("remote usb: terminated (data)"); |
| 110 | return -1; |
| 111 | } |
| 112 | if (static_cast<uint32_t>(n) != p->msg.data_length) { |
| 113 | D("remote usb: read payload failed (need %u bytes, give %d bytes), skip it", |
| 114 | p->msg.data_length, n); |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 115 | return -1; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 118 | return 0; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | #else |
| 122 | |
| 123 | // On Android devices, we rely on the kernel to provide buffered read. |
| 124 | // So we can recover automatically from EOVERFLOW. |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 125 | static int remote_read(apacket* p, usb_handle* usb) { |
Jerry Zhang | 76d17db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 126 | if (usb_read(usb, &p->msg, sizeof(amessage)) != sizeof(amessage)) { |
Josh Gao | 03eba90 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 127 | PLOG(ERROR) << "remote usb: read terminated (message)"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | return -1; |
| 129 | } |
| 130 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 131 | if (p->msg.data_length) { |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 132 | if (p->msg.data_length > MAX_PAYLOAD) { |
Josh Gao | b14756a | 2018-02-02 14:38:04 -0800 | [diff] [blame] | 133 | PLOG(ERROR) << "remote usb: read overflow (data length = " << p->msg.data_length << ")"; |
| 134 | return -1; |
| 135 | } |
| 136 | |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 137 | p->payload.resize(p->msg.data_length); |
Jerry Zhang | 76d17db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 138 | if (usb_read(usb, &p->payload[0], p->payload.size()) |
| 139 | != static_cast<int>(p->payload.size())) { |
Josh Gao | 03eba90 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 140 | PLOG(ERROR) << "remote usb: terminated (data)"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | } |
| 144 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | return 0; |
| 146 | } |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 147 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 149 | UsbConnection::~UsbConnection() { |
| 150 | usb_close(handle_); |
| 151 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 153 | bool UsbConnection::Read(apacket* packet) { |
| 154 | int rc = remote_read(packet, handle_); |
| 155 | return rc == 0; |
| 156 | } |
| 157 | |
| 158 | bool UsbConnection::Write(apacket* packet) { |
Jerry Zhang | 76d17db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 159 | int size = packet->msg.data_length; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 160 | |
Jerry Zhang | 76d17db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 161 | if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != sizeof(packet->msg)) { |
Josh Gao | 03eba90 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 162 | PLOG(ERROR) << "remote usb: 1 - write terminated"; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 163 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | } |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 165 | |
Jerry Zhang | 76d17db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 166 | if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != size) { |
Josh Gao | 03eba90 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 167 | PLOG(ERROR) << "remote usb: 2 - write terminated"; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 168 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 171 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 174 | void UsbConnection::Reset() { |
| 175 | usb_reset(handle_); |
| 176 | usb_kick(handle_); |
| 177 | } |
| 178 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 179 | void UsbConnection::Close() { |
| 180 | usb_kick(handle_); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 183 | void init_usb_transport(atransport* t, usb_handle* h) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 184 | D("transport: usb"); |
Josh Gao | f2a988c | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 185 | auto connection = std::make_unique<UsbConnection>(h); |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 186 | t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(connection))); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | t->type = kTransportUsb; |
Josh Gao | 68f2c38 | 2018-12-11 13:11:52 -0800 | [diff] [blame] | 188 | t->SetUsbHandle(h); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 191 | int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) { |
Elliott Hughes | 4b8538e | 2014-11-19 22:07:34 -0800 | [diff] [blame] | 192 | return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | } |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 194 | |
| 195 | bool should_use_libusb() { |
Josh Gao | 511ff8a | 2017-12-08 13:05:40 -0800 | [diff] [blame] | 196 | #if !ADB_HOST |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 197 | return false; |
| 198 | #else |
Josh Gao | 1ca19aa | 2017-06-26 12:16:30 -0700 | [diff] [blame] | 199 | static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0; |
| 200 | return enable; |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 201 | #endif |
| 202 | } |