blob: 885d7230e574e414667cf78ea7e64ac9db71d125 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "transport.h"
21
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080026#include "adb.h"
27
Yabin Cui3cf1b362017-03-10 16:01:01 -080028#if ADB_HOST
29
Josh Gao3734cf02017-05-02 15:01:09 -070030// Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
Yabin Cui3cf1b362017-03-10 16:01:01 -080031// to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
32static int UsbReadMessage(usb_handle* h, amessage* msg) {
33 D("UsbReadMessage");
Josh Gao3734cf02017-05-02 15:01:09 -070034
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 Cui3cf1b362017-03-10 16:01:01 -080044 }
Josh Gao3734cf02017-05-02 15:01:09 -070045 memcpy(msg, buffer, sizeof(*msg));
Yabin Cui3cf1b362017-03-10 16:01:01 -080046 return n;
47}
48
Josh Gao3734cf02017-05-02 15:01:09 -070049// Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
Yabin Cui3cf1b362017-03-10 16:01:01 -080050// to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
51static int UsbReadPayload(usb_handle* h, apacket* p) {
Josh Gao3734cf02017-05-02 15:01:09 -070052 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 Cui3cf1b362017-03-10 16:01:01 -080064 }
Josh Gao3734cf02017-05-02 15:01:09 -070065 CHECK(len <= sizeof(p->data));
66 return usb_read(h, &p->data, len);
Yabin Cui3cf1b362017-03-10 16:01:01 -080067}
68
69static 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
104err_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 Project9ca14dc2009-03-03 19:32:55 -0800120static int remote_read(apacket *p, atransport *t)
121{
Yabin Cui3cf1b362017-03-10 16:01:01 -0800122 if (usb_read(t->usb, &p->msg, sizeof(amessage))) {
Yabin Cui815ad882015-09-02 17:44:28 -0700123 D("remote usb: read terminated (message)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800124 return -1;
125 }
126
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100127 if(check_header(p, t)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700128 D("remote usb: check_header failed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800129 return -1;
130 }
131
132 if(p->msg.data_length) {
Yabin Cui3cf1b362017-03-10 16:01:01 -0800133 if (usb_read(t->usb, p->data, p->msg.data_length)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700134 D("remote usb: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135 return -1;
136 }
137 }
138
139 if(check_data(p)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700140 D("remote usb: check_data failed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800141 return -1;
142 }
143
144 return 0;
145}
Yabin Cui3cf1b362017-03-10 16:01:01 -0800146#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800147
148static int remote_write(apacket *p, atransport *t)
149{
150 unsigned size = p->msg.data_length;
151
Yabin Cui3cf1b362017-03-10 16:01:01 -0800152 if (usb_write(t->usb, &p->msg, sizeof(amessage))) {
Yabin Cui815ad882015-09-02 17:44:28 -0700153 D("remote usb: 1 - write terminated");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800154 return -1;
155 }
156 if(p->msg.data_length == 0) return 0;
Yabin Cui3cf1b362017-03-10 16:01:01 -0800157 if (usb_write(t->usb, &p->data, size)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700158 D("remote usb: 2 - write terminated");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800159 return -1;
160 }
161
162 return 0;
163}
164
165static void remote_close(atransport *t)
166{
167 usb_close(t->usb);
168 t->usb = 0;
169}
170
Yabin Cui3cf1b362017-03-10 16:01:01 -0800171static void remote_kick(atransport* t) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800172 usb_kick(t->usb);
173}
174
Yabin Cui3cf1b362017-03-10 16:01:01 -0800175void init_usb_transport(atransport* t, usb_handle* h) {
Yabin Cui815ad882015-09-02 17:44:28 -0700176 D("transport: usb");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177 t->close = remote_close;
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700178 t->SetKickFunction(remote_kick);
Yabin Cui3cf1b362017-03-10 16:01:01 -0800179 t->SetWriteFunction(remote_write);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800180 t->read_from_remote = remote_read;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800181 t->sync_token = 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800182 t->type = kTransportUsb;
183 t->usb = h;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184}
185
Josh Gao08dda212016-09-26 21:18:58 -0700186int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800187{
Elliott Hughes4b8538e2014-11-19 22:07:34 -0800188 return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800189}
Josh Gao210b63f2017-02-22 17:07:01 -0800190
191bool should_use_libusb() {
192#if defined(_WIN32) || !ADB_HOST
193 return false;
194#else
Josh Gaof6ed63c2017-03-02 13:23:56 -0800195 static bool disable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "0") == 0;
196 return !disable;
Josh Gao210b63f2017-02-22 17:07:01 -0800197#endif
198}