blob: 6d77f4ba7cc6ee42d82bb5ff092d70b27c57f8c7 [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 USB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <dirent.h>
22#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <linux/usb/ch9.h>
24#include <linux/usb/functionfs.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/ioctl.h>
29#include <sys/types.h>
30#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Josh Gaoae72b5a2015-12-17 13:45:18 -080032#include <algorithm>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070033#include <atomic>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080034#include <chrono>
Josh Gao0cd3ae12016-09-21 12:37:10 -070035#include <condition_variable>
36#include <mutex>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080037#include <thread>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070038
39#include <android-base/logging.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070040#include <android-base/properties.h>
Josh Gaoae72b5a2015-12-17 13:45:18 -080041
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include "adb.h"
Josh Gao44c688c2017-01-11 17:37:35 -080043#include "daemon/usb.h"
Dan Albert76649012015-02-24 15:51:19 -080044#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080046using namespace std::chrono_literals;
47
Josh Gao44c688c2017-01-11 17:37:35 -080048#define MAX_PACKET_SIZE_FS 64
49#define MAX_PACKET_SIZE_HS 512
50#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010051
Jerry Zhang55205a52017-01-04 12:34:38 -080052// Kernels before 3.3 have a 16KiB transfer limit That limit was replaced
53// with a 16MiB global limit in 3.3, but each URB submitted required a
54// contiguous kernel allocation, so you would get ENOMEM if you tried to
55// send something larger than the biggest available contiguous kernel
56// memory region. Large contiguous allocations could be unreliable
57// on a device kernel that has been running for a while fragmenting its
58// memory so we start with a larger allocation, and shrink the amount if
59// necessary.
60#define USB_FFS_BULK_SIZE 16384
Josh Gaoae72b5a2015-12-17 13:45:18 -080061
Josh Gao44c688c2017-01-11 17:37:35 -080062#define cpu_to_le16(x) htole16(x)
63#define cpu_to_le32(x) htole32(x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Jerry Zhang55205a52017-01-04 12:34:38 -080065#define FUNCTIONFS_ENDPOINT_ALLOC _IOR('g', 231, __u32)
66
67static constexpr size_t ENDPOINT_ALLOC_RETRIES = 10;
68
Yabin Cui9b53e4c2016-04-05 14:51:52 -070069static int dummy_fd = -1;
70
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070071struct func_desc {
72 struct usb_interface_descriptor intf;
73 struct usb_endpoint_descriptor_no_audio source;
74 struct usb_endpoint_descriptor_no_audio sink;
75} __attribute__((packed));
76
Jack Phama190c802015-06-02 10:36:43 -070077struct ss_func_desc {
78 struct usb_interface_descriptor intf;
79 struct usb_endpoint_descriptor_no_audio source;
80 struct usb_ss_ep_comp_descriptor source_comp;
81 struct usb_endpoint_descriptor_no_audio sink;
82 struct usb_ss_ep_comp_descriptor sink_comp;
83} __attribute__((packed));
84
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070085struct desc_v1 {
86 struct usb_functionfs_descs_head_v1 {
87 __le32 magic;
88 __le32 length;
89 __le32 fs_count;
90 __le32 hs_count;
91 } __attribute__((packed)) header;
92 struct func_desc fs_descs, hs_descs;
93} __attribute__((packed));
94
95struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080096 struct usb_functionfs_descs_head_v2 header;
97 // The rest of the structure depends on the flags in the header.
98 __le32 fs_count;
99 __le32 hs_count;
Jack Phama190c802015-06-02 10:36:43 -0700100 __le32 ss_count;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700101 __le32 os_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700102 struct func_desc fs_descs, hs_descs;
Jack Phama190c802015-06-02 10:36:43 -0700103 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700104 struct usb_os_desc_header os_header;
105 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700106} __attribute__((packed));
107
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700108static struct func_desc fs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700109 .intf = {
110 .bLength = sizeof(fs_descriptors.intf),
111 .bDescriptorType = USB_DT_INTERFACE,
112 .bInterfaceNumber = 0,
113 .bNumEndpoints = 2,
114 .bInterfaceClass = ADB_CLASS,
115 .bInterfaceSubClass = ADB_SUBCLASS,
116 .bInterfaceProtocol = ADB_PROTOCOL,
117 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100118 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700119 .source = {
120 .bLength = sizeof(fs_descriptors.source),
121 .bDescriptorType = USB_DT_ENDPOINT,
122 .bEndpointAddress = 1 | USB_DIR_OUT,
123 .bmAttributes = USB_ENDPOINT_XFER_BULK,
124 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100125 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700126 .sink = {
127 .bLength = sizeof(fs_descriptors.sink),
128 .bDescriptorType = USB_DT_ENDPOINT,
129 .bEndpointAddress = 2 | USB_DIR_IN,
130 .bmAttributes = USB_ENDPOINT_XFER_BULK,
131 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
132 },
133};
134
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700135static struct func_desc hs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700136 .intf = {
137 .bLength = sizeof(hs_descriptors.intf),
138 .bDescriptorType = USB_DT_INTERFACE,
139 .bInterfaceNumber = 0,
140 .bNumEndpoints = 2,
141 .bInterfaceClass = ADB_CLASS,
142 .bInterfaceSubClass = ADB_SUBCLASS,
143 .bInterfaceProtocol = ADB_PROTOCOL,
144 .iInterface = 1, /* first string from the provided table */
145 },
146 .source = {
147 .bLength = sizeof(hs_descriptors.source),
148 .bDescriptorType = USB_DT_ENDPOINT,
149 .bEndpointAddress = 1 | USB_DIR_OUT,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
151 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
152 },
153 .sink = {
154 .bLength = sizeof(hs_descriptors.sink),
155 .bDescriptorType = USB_DT_ENDPOINT,
156 .bEndpointAddress = 2 | USB_DIR_IN,
157 .bmAttributes = USB_ENDPOINT_XFER_BULK,
158 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800159 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100160};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
Jack Phama190c802015-06-02 10:36:43 -0700162static struct ss_func_desc ss_descriptors = {
163 .intf = {
164 .bLength = sizeof(ss_descriptors.intf),
165 .bDescriptorType = USB_DT_INTERFACE,
166 .bInterfaceNumber = 0,
167 .bNumEndpoints = 2,
168 .bInterfaceClass = ADB_CLASS,
169 .bInterfaceSubClass = ADB_SUBCLASS,
170 .bInterfaceProtocol = ADB_PROTOCOL,
171 .iInterface = 1, /* first string from the provided table */
172 },
173 .source = {
174 .bLength = sizeof(ss_descriptors.source),
175 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = 1 | USB_DIR_OUT,
177 .bmAttributes = USB_ENDPOINT_XFER_BULK,
178 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
179 },
180 .source_comp = {
181 .bLength = sizeof(ss_descriptors.source_comp),
182 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jerry Zhangb5a34a22017-02-10 17:43:00 -0800183 .bMaxBurst = 4,
Jack Phama190c802015-06-02 10:36:43 -0700184 },
185 .sink = {
186 .bLength = sizeof(ss_descriptors.sink),
187 .bDescriptorType = USB_DT_ENDPOINT,
188 .bEndpointAddress = 2 | USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_BULK,
190 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
191 },
192 .sink_comp = {
193 .bLength = sizeof(ss_descriptors.sink_comp),
194 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jerry Zhangb5a34a22017-02-10 17:43:00 -0800195 .bMaxBurst = 4,
Jack Phama190c802015-06-02 10:36:43 -0700196 },
197};
198
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700199struct usb_ext_compat_desc os_desc_compat = {
200 .bFirstInterfaceNumber = 0,
201 .Reserved1 = cpu_to_le32(1),
202 .CompatibleID = {0},
203 .SubCompatibleID = {0},
204 .Reserved2 = {0},
205};
206
207static struct usb_os_desc_header os_desc_header = {
208 .interface = cpu_to_le32(1),
209 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
210 .bcdVersion = cpu_to_le32(1),
211 .wIndex = cpu_to_le32(4),
212 .bCount = cpu_to_le32(1),
213 .Reserved = cpu_to_le32(0),
214};
215
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100216#define STR_INTERFACE_ "ADB Interface"
217
218static const struct {
219 struct usb_functionfs_strings_head header;
220 struct {
221 __le16 code;
222 const char str1[sizeof(STR_INTERFACE_)];
223 } __attribute__((packed)) lang0;
224} __attribute__((packed)) strings = {
225 .header = {
226 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
227 .length = cpu_to_le32(sizeof(strings)),
228 .str_count = cpu_to_le32(1),
229 .lang_count = cpu_to_le32(1),
230 },
231 .lang0 = {
232 cpu_to_le16(0x0409), /* en-us */
233 STR_INTERFACE_,
234 },
235};
236
Josh Gao44c688c2017-01-11 17:37:35 -0800237bool init_functionfs(struct usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100238 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700239 struct desc_v1 v1_descriptor;
240 struct desc_v2 v2_descriptor;
Jerry Zhang55205a52017-01-04 12:34:38 -0800241 size_t retries = 0;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700242
243 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
244 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phama190c802015-06-02 10:36:43 -0700245 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700246 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800247 v2_descriptor.fs_count = 3;
248 v2_descriptor.hs_count = 3;
Jack Phama190c802015-06-02 10:36:43 -0700249 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700250 v2_descriptor.os_count = 1;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700251 v2_descriptor.fs_descs = fs_descriptors;
252 v2_descriptor.hs_descs = hs_descriptors;
Jack Phama190c802015-06-02 10:36:43 -0700253 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700254 v2_descriptor.os_header = os_desc_header;
255 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100256
Siqi Lin8c407622016-05-26 13:04:52 -0700257 if (h->control < 0) { // might have already done this before
258 D("OPENING %s", USB_FFS_ADB_EP0);
259 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
260 if (h->control < 0) {
261 D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800262 goto err;
263 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100264
Siqi Lin8c407622016-05-26 13:04:52 -0700265 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
266 if (ret < 0) {
267 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
268 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
269 v1_descriptor.header.fs_count = 3;
270 v1_descriptor.header.hs_count = 3;
271 v1_descriptor.fs_descs = fs_descriptors;
272 v1_descriptor.hs_descs = hs_descriptors;
273 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
274 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
275 if (ret < 0) {
276 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
277 goto err;
278 }
279 }
280
281 ret = adb_write(h->control, &strings, sizeof(strings));
282 if (ret < 0) {
283 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
284 goto err;
285 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100286 }
287
288 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
289 if (h->bulk_out < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700290 D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100291 goto err;
292 }
293
294 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
295 if (h->bulk_in < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700296 D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100297 goto err;
298 }
299
Jerry Zhang55205a52017-01-04 12:34:38 -0800300 h->max_rw = MAX_PAYLOAD;
301 while (h->max_rw >= USB_FFS_BULK_SIZE && retries < ENDPOINT_ALLOC_RETRIES) {
302 int ret_in = ioctl(h->bulk_in, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
303 int errno_in = errno;
304 int ret_out = ioctl(h->bulk_out, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
305 int errno_out = errno;
306
307 if (ret_in || ret_out) {
308 if (errno_in == ENODEV || errno_out == ENODEV) {
309 std::this_thread::sleep_for(100ms);
310 retries += 1;
311 continue;
312 }
313 h->max_rw /= 2;
314 } else {
315 return true;
316 }
317 }
318
319 D("[ adb: cannot call endpoint alloc: errno=%d ]", errno);
320 // Kernel pre-allocation could have failed for recoverable reasons.
321 // Continue running with a safe max rw size.
322 h->max_rw *= 2;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700323 return true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100324
325err:
326 if (h->bulk_in > 0) {
327 adb_close(h->bulk_in);
328 h->bulk_in = -1;
329 }
330 if (h->bulk_out > 0) {
331 adb_close(h->bulk_out);
332 h->bulk_out = -1;
333 }
334 if (h->control > 0) {
335 adb_close(h->control);
336 h->control = -1;
337 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700338 return false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100339}
340
Josh Gaod9db09c2016-02-12 14:31:15 -0800341static void usb_ffs_open_thread(void* x) {
Josh Gao183b73e2017-01-11 14:39:19 -0800342 struct usb_handle* usb = (struct usb_handle*)x;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100343
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700344 adb_thread_setname("usb ffs open");
345
Elliott Hughesa7090b92015-04-17 17:03:59 -0700346 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100347 // wait until the USB device needs opening
Josh Gao0cd3ae12016-09-21 12:37:10 -0700348 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700349 while (!usb->open_new_connection) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700350 usb->notify.wait(lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700351 }
352 usb->open_new_connection = false;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700353 lock.unlock();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100354
Elliott Hughesa7090b92015-04-17 17:03:59 -0700355 while (true) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700356 if (init_functionfs(usb)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100357 break;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700358 }
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800359 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100360 }
Elliott Hughesffdec182016-09-23 15:40:03 -0700361 android::base::SetProperty("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100362
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700363 D("[ usb_thread - registering device ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100364 register_usb_transport(usb, 0, 0, 1);
365 }
366
367 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800368 abort();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100369}
370
Josh Gao6b531c42015-12-02 17:30:58 -0800371static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700372 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800373
Josh Gao6b531c42015-12-02 17:30:58 -0800374 const char* buf = static_cast<const char*>(data);
375 while (len > 0) {
Jerry Zhang55205a52017-01-04 12:34:38 -0800376 int write_len = std::min(h->max_rw, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800377 int n = adb_write(h->bulk_in, buf, write_len);
378 if (n < 0) {
379 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
380 return -1;
381 }
382 buf += n;
383 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100384 }
Josh Gao6b531c42015-12-02 17:30:58 -0800385
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700386 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100387 return 0;
388}
389
Josh Gao6b531c42015-12-02 17:30:58 -0800390static int usb_ffs_read(usb_handle* h, void* data, int len) {
391 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100392
Josh Gao6b531c42015-12-02 17:30:58 -0800393 char* buf = static_cast<char*>(data);
394 while (len > 0) {
Jerry Zhang55205a52017-01-04 12:34:38 -0800395 int read_len = std::min(h->max_rw, len);
Josh Gao0b195402015-12-16 11:00:08 -0800396 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800397 if (n < 0) {
398 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700399 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100400 }
Josh Gao6b531c42015-12-02 17:30:58 -0800401 buf += n;
402 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700403 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100404
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700405 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100406 return 0;
407}
408
Josh Gao183b73e2017-01-11 14:39:19 -0800409static void usb_ffs_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100410 int err;
411
412 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700413 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700414 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700415 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100416
417 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700418 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700419 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700420 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100421
Jack Pham4cbf1d82013-12-23 17:46:10 -0800422 // don't close ep0 here, since we may not need to reinitialize it with
423 // the same descriptors again. if however ep1/ep2 fail to re-open in
424 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700425 // Ditto the comment in usb_adb_kick.
426 h->kicked = true;
427 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
428 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
429}
430
Josh Gao183b73e2017-01-11 14:39:19 -0800431static void usb_ffs_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700432 h->kicked = false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100433 adb_close(h->bulk_out);
434 adb_close(h->bulk_in);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700435 // Notify usb_adb_open_thread to open a new connection.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700436 h->lock.lock();
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700437 h->open_new_connection = true;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700438 h->lock.unlock();
439 h->notify.notify_one();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440}
441
Josh Gao183b73e2017-01-11 14:39:19 -0800442static void usb_ffs_init() {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700443 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100444
Josh Gao0cd3ae12016-09-21 12:37:10 -0700445 usb_handle* h = new usb_handle();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700446
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100447 h->write = usb_ffs_write;
448 h->read = usb_ffs_read;
449 h->kick = usb_ffs_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700450 h->close = usb_ffs_close;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100451
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700452 D("[ usb_init - starting thread ]");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700453 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100454 fatal_errno("[ cannot create usb thread ]\n");
455 }
456}
457
Josh Gao183b73e2017-01-11 14:39:19 -0800458void usb_init() {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700459 dummy_fd = adb_open("/dev/null", O_WRONLY);
460 CHECK_NE(dummy_fd, -1);
Josh Gao183b73e2017-01-11 14:39:19 -0800461 usb_ffs_init();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100462}
463
Josh Gao183b73e2017-01-11 14:39:19 -0800464int usb_write(usb_handle* h, const void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100465 return h->write(h, data, len);
466}
467
Josh Gao183b73e2017-01-11 14:39:19 -0800468int usb_read(usb_handle* h, void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100469 return h->read(h, data, len);
470}
Josh Gao0cd3ae12016-09-21 12:37:10 -0700471
Josh Gao183b73e2017-01-11 14:39:19 -0800472int usb_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700473 h->close(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 return 0;
475}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100476
Josh Gao183b73e2017-01-11 14:39:19 -0800477void usb_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100478 h->kick(h);
479}