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 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "transport.h" |
| 21 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include "adb.h" |
| 27 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 28 | #if ADB_HOST |
| 29 | |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 30 | // 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] | 31 | // to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html. |
| 32 | static int UsbReadMessage(usb_handle* h, amessage* msg) { |
| 33 | D("UsbReadMessage"); |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 34 | |
| 35 | size_t usb_packet_size = usb_get_max_packet_size(h); |
| 36 | CHECK(usb_packet_size >= sizeof(*msg)); |
| 37 | CHECK(usb_packet_size < 4096); |
| 38 | |
| 39 | char buffer[4096]; |
| 40 | int n = usb_read(h, buffer, usb_packet_size); |
| 41 | if (n != sizeof(*msg)) { |
| 42 | D("usb_read returned unexpected length %d (expected %zu)", n, sizeof(*msg)); |
| 43 | return -1; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 44 | } |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 45 | memcpy(msg, buffer, sizeof(*msg)); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 46 | return n; |
| 47 | } |
| 48 | |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 49 | // 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] | 50 | // to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html. |
| 51 | static int UsbReadPayload(usb_handle* h, apacket* p) { |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 52 | D("UsbReadPayload(%d)", p->msg.data_length); |
| 53 | |
| 54 | size_t usb_packet_size = usb_get_max_packet_size(h); |
| 55 | CHECK(sizeof(p->data) % usb_packet_size == 0); |
| 56 | |
| 57 | // Round the data length up to the nearest packet size boundary. |
| 58 | // The device won't send a zero packet for packet size aligned payloads, |
| 59 | // so don't read any more packets than needed. |
| 60 | size_t len = p->msg.data_length; |
| 61 | size_t rem_size = len % usb_packet_size; |
| 62 | if (rem_size) { |
| 63 | len += usb_packet_size - rem_size; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 64 | } |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 65 | CHECK(len <= sizeof(p->data)); |
| 66 | return usb_read(h, &p->data, len); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int remote_read(apacket* p, atransport* t) { |
| 70 | int n = UsbReadMessage(t->usb, &p->msg); |
| 71 | if (n < 0) { |
| 72 | D("remote usb: read terminated (message)"); |
| 73 | return -1; |
| 74 | } |
| 75 | if (static_cast<size_t>(n) != sizeof(p->msg) || check_header(p, t)) { |
| 76 | D("remote usb: check_header failed, skip it"); |
| 77 | goto err_msg; |
| 78 | } |
| 79 | if (t->GetConnectionState() == kCsOffline) { |
| 80 | // If we read a wrong msg header declaring a large message payload, don't read its payload. |
| 81 | // Otherwise we may miss true messages from the device. |
| 82 | if (p->msg.command != A_CNXN && p->msg.command != A_AUTH) { |
| 83 | goto err_msg; |
| 84 | } |
| 85 | } |
| 86 | if (p->msg.data_length) { |
| 87 | n = UsbReadPayload(t->usb, p); |
| 88 | if (n < 0) { |
| 89 | D("remote usb: terminated (data)"); |
| 90 | return -1; |
| 91 | } |
| 92 | if (static_cast<uint32_t>(n) != p->msg.data_length) { |
| 93 | D("remote usb: read payload failed (need %u bytes, give %d bytes), skip it", |
| 94 | p->msg.data_length, n); |
| 95 | goto err_msg; |
| 96 | } |
| 97 | } |
| 98 | if (check_data(p)) { |
| 99 | D("remote usb: check_data failed, skip it"); |
| 100 | goto err_msg; |
| 101 | } |
| 102 | return 0; |
| 103 | |
| 104 | err_msg: |
| 105 | p->msg.command = 0; |
| 106 | if (t->GetConnectionState() == kCsOffline) { |
| 107 | // If the data toggle of ep_out on device and ep_in on host are not the same, we may receive |
| 108 | // an error message. In this case, resend one A_CNXN message to connect the device. |
| 109 | if (t->SetSendConnectOnError()) { |
| 110 | SendConnectOnHost(t); |
| 111 | } |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | #else |
| 117 | |
| 118 | // On Android devices, we rely on the kernel to provide buffered read. |
| 119 | // So we can recover automatically from EOVERFLOW. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | static int remote_read(apacket *p, atransport *t) |
| 121 | { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 122 | if (usb_read(t->usb, &p->msg, sizeof(amessage))) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 123 | D("remote usb: read terminated (message)"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 127 | if(check_header(p, t)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 128 | D("remote usb: check_header failed"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | if(p->msg.data_length) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 133 | if (usb_read(t->usb, p->data, p->msg.data_length)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 134 | D("remote usb: terminated (data)"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if(check_data(p)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 140 | D("remote usb: check_data failed"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 146 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | |
| 148 | static int remote_write(apacket *p, atransport *t) |
| 149 | { |
| 150 | unsigned size = p->msg.data_length; |
| 151 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 152 | if (usb_write(t->usb, &p->msg, sizeof(amessage))) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 153 | D("remote usb: 1 - write terminated"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | return -1; |
| 155 | } |
| 156 | if(p->msg.data_length == 0) return 0; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 157 | if (usb_write(t->usb, &p->data, size)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 158 | D("remote usb: 2 - write terminated"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static void remote_close(atransport *t) |
| 166 | { |
| 167 | usb_close(t->usb); |
| 168 | t->usb = 0; |
| 169 | } |
| 170 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 171 | static void remote_kick(atransport* t) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | usb_kick(t->usb); |
| 173 | } |
| 174 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 175 | void init_usb_transport(atransport* t, usb_handle* h) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 176 | D("transport: usb"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | t->close = remote_close; |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 178 | t->SetKickFunction(remote_kick); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 179 | t->SetWriteFunction(remote_write); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | t->read_from_remote = remote_read; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | t->sync_token = 1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | t->type = kTransportUsb; |
| 183 | t->usb = h; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Josh Gao | 08dda21 | 2016-09-26 21:18:58 -0700 | [diff] [blame] | 186 | int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | { |
Elliott Hughes | 4b8538e | 2014-11-19 22:07:34 -0800 | [diff] [blame] | 188 | 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] | 189 | } |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 190 | |
| 191 | bool should_use_libusb() { |
| 192 | #if defined(_WIN32) || !ADB_HOST |
| 193 | return false; |
| 194 | #else |
Josh Gao | f6ed63c | 2017-03-02 13:23:56 -0800 | [diff] [blame] | 195 | static bool disable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "0") == 0; |
| 196 | return !disable; |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 197 | #endif |
| 198 | } |