blob: d7565f63d54783a81cc73d6692e8224d89125175 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "transport.h"
21
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include "adb.h"
27
Yabin Cuib5e11412017-03-10 16:01:01 -080028#if ADB_HOST
29
Josh Gao801048c2017-12-06 15:06:14 -080030#if defined(__APPLE__)
31#define CHECK_PACKET_OVERFLOW 0
32#else
33#define CHECK_PACKET_OVERFLOW 1
34#endif
35
Josh Gaoef3d3432017-05-02 15:01:09 -070036// Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
Yabin Cuib5e11412017-03-10 16:01:01 -080037// to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
38static int UsbReadMessage(usb_handle* h, amessage* msg) {
39 D("UsbReadMessage");
Josh Gaoef3d3432017-05-02 15:01:09 -070040
Josh Gao801048c2017-12-06 15:06:14 -080041#if CHECK_PACKET_OVERFLOW
Josh Gaoef3d3432017-05-02 15:01:09 -070042 size_t usb_packet_size = usb_get_max_packet_size(h);
Josh Gaoe4672092017-08-28 14:43:24 -070043 CHECK_GE(usb_packet_size, sizeof(*msg));
44 CHECK_LT(usb_packet_size, 4096ULL);
Josh Gaoef3d3432017-05-02 15:01:09 -070045
46 char buffer[4096];
47 int n = usb_read(h, buffer, usb_packet_size);
48 if (n != sizeof(*msg)) {
49 D("usb_read returned unexpected length %d (expected %zu)", n, sizeof(*msg));
50 return -1;
Yabin Cuib5e11412017-03-10 16:01:01 -080051 }
Josh Gaoef3d3432017-05-02 15:01:09 -070052 memcpy(msg, buffer, sizeof(*msg));
Yabin Cuib5e11412017-03-10 16:01:01 -080053 return n;
Josh Gao801048c2017-12-06 15:06:14 -080054#else
55 return usb_read(h, msg, sizeof(*msg));
56#endif
Yabin Cuib5e11412017-03-10 16:01:01 -080057}
58
Josh Gaoef3d3432017-05-02 15:01:09 -070059// Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
Yabin Cuib5e11412017-03-10 16:01:01 -080060// to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
61static int UsbReadPayload(usb_handle* h, apacket* p) {
Josh Gaoef3d3432017-05-02 15:01:09 -070062 D("UsbReadPayload(%d)", p->msg.data_length);
63
Josh Gaof571fcb2018-02-05 18:49:10 -080064 if (p->msg.data_length > MAX_PAYLOAD) {
Josh Gao5caaebd2018-02-02 14:38:04 -080065 return -1;
66 }
67
Josh Gao801048c2017-12-06 15:06:14 -080068#if CHECK_PACKET_OVERFLOW
Josh Gaoef3d3432017-05-02 15:01:09 -070069 size_t usb_packet_size = usb_get_max_packet_size(h);
Josh Gaoef3d3432017-05-02 15:01:09 -070070
71 // Round the data length up to the nearest packet size boundary.
72 // The device won't send a zero packet for packet size aligned payloads,
73 // so don't read any more packets than needed.
74 size_t len = p->msg.data_length;
75 size_t rem_size = len % usb_packet_size;
76 if (rem_size) {
77 len += usb_packet_size - rem_size;
Yabin Cuib5e11412017-03-10 16:01:01 -080078 }
Josh Gaof571fcb2018-02-05 18:49:10 -080079
80 p->payload.resize(len);
81 int rc = usb_read(h, &p->payload[0], p->payload.size());
82 if (rc != static_cast<int>(p->msg.data_length)) {
83 return -1;
84 }
85
86 p->payload.resize(rc);
87 return rc;
Josh Gao801048c2017-12-06 15:06:14 -080088#else
Josh Gaof571fcb2018-02-05 18:49:10 -080089 p->payload.resize(p->msg.data_length);
90 return usb_read(h, &p->payload[0], p->payload.size());
Josh Gao801048c2017-12-06 15:06:14 -080091#endif
Yabin Cuib5e11412017-03-10 16:01:01 -080092}
93
Josh Gaob800d882018-01-28 20:32:46 -080094static int remote_read(apacket* p, usb_handle* usb) {
95 int n = UsbReadMessage(usb, &p->msg);
Yabin Cuib5e11412017-03-10 16:01:01 -080096 if (n < 0) {
97 D("remote usb: read terminated (message)");
98 return -1;
99 }
Josh Gaob800d882018-01-28 20:32:46 -0800100 if (static_cast<size_t>(n) != sizeof(p->msg)) {
101 D("remote usb: read received unexpected header length %d", n);
102 return -1;
Yabin Cuib5e11412017-03-10 16:01:01 -0800103 }
104 if (p->msg.data_length) {
Josh Gaob800d882018-01-28 20:32:46 -0800105 n = UsbReadPayload(usb, p);
Yabin Cuib5e11412017-03-10 16:01:01 -0800106 if (n < 0) {
107 D("remote usb: terminated (data)");
108 return -1;
109 }
110 if (static_cast<uint32_t>(n) != p->msg.data_length) {
111 D("remote usb: read payload failed (need %u bytes, give %d bytes), skip it",
112 p->msg.data_length, n);
Josh Gaob800d882018-01-28 20:32:46 -0800113 return -1;
Yabin Cuib5e11412017-03-10 16:01:01 -0800114 }
115 }
Yabin Cuib5e11412017-03-10 16:01:01 -0800116 return 0;
Yabin Cuib5e11412017-03-10 16:01:01 -0800117}
118
119#else
120
121// On Android devices, we rely on the kernel to provide buffered read.
122// So we can recover automatically from EOVERFLOW.
Josh Gaob800d882018-01-28 20:32:46 -0800123static int remote_read(apacket* p, usb_handle* usb) {
124 if (usb_read(usb, &p->msg, sizeof(amessage))) {
Josh Gao184f5712017-07-26 11:06:55 -0700125 PLOG(ERROR) << "remote usb: read terminated (message)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 return -1;
127 }
128
Josh Gao36dadca2017-05-16 15:02:45 -0700129 if (p->msg.data_length) {
Josh Gaof571fcb2018-02-05 18:49:10 -0800130 if (p->msg.data_length > MAX_PAYLOAD) {
Josh Gao5caaebd2018-02-02 14:38:04 -0800131 PLOG(ERROR) << "remote usb: read overflow (data length = " << p->msg.data_length << ")";
132 return -1;
133 }
134
Josh Gaof571fcb2018-02-05 18:49:10 -0800135 p->payload.resize(p->msg.data_length);
136 if (usb_read(usb, &p->payload[0], p->payload.size())) {
Josh Gao184f5712017-07-26 11:06:55 -0700137 PLOG(ERROR) << "remote usb: terminated (data)";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 return -1;
139 }
140 }
141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 return 0;
143}
Yabin Cuib5e11412017-03-10 16:01:01 -0800144#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145
Josh Gaob800d882018-01-28 20:32:46 -0800146UsbConnection::~UsbConnection() {
147 usb_close(handle_);
148}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
Josh Gaob800d882018-01-28 20:32:46 -0800150bool UsbConnection::Read(apacket* packet) {
151 int rc = remote_read(packet, handle_);
152 return rc == 0;
153}
154
155bool UsbConnection::Write(apacket* packet) {
156 unsigned size = packet->msg.data_length;
157
158 if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700159 PLOG(ERROR) << "remote usb: 1 - write terminated";
Josh Gaob800d882018-01-28 20:32:46 -0800160 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 }
Josh Gaob800d882018-01-28 20:32:46 -0800162
Josh Gaof571fcb2018-02-05 18:49:10 -0800163 if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != 0) {
Josh Gao184f5712017-07-26 11:06:55 -0700164 PLOG(ERROR) << "remote usb: 2 - write terminated";
Josh Gaob800d882018-01-28 20:32:46 -0800165 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 }
167
Josh Gaob800d882018-01-28 20:32:46 -0800168 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169}
170
Josh Gaob800d882018-01-28 20:32:46 -0800171void UsbConnection::Close() {
172 usb_kick(handle_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173}
174
Yabin Cuib5e11412017-03-10 16:01:01 -0800175void init_usb_transport(atransport* t, usb_handle* h) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700176 D("transport: usb");
Josh Gaob800d882018-01-28 20:32:46 -0800177 t->connection.reset(new UsbConnection(h));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 t->sync_token = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 t->type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180}
181
Josh Gaob800d882018-01-28 20:32:46 -0800182int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) {
Elliott Hughes55664902014-11-19 22:07:34 -0800183 return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184}
Josh Gao5d1756c2017-02-22 17:07:01 -0800185
186bool should_use_libusb() {
Josh Gao94259962017-12-08 13:05:40 -0800187#if !ADB_HOST
Josh Gao5d1756c2017-02-22 17:07:01 -0800188 return false;
189#else
Josh Gao969110c2017-06-26 12:16:30 -0700190 static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0;
191 return enable;
Josh Gao5d1756c2017-02-22 17:07:01 -0800192#endif
193}