blob: 16f4b6d3f87566844a7d98b89c27249d7906796b [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,
183 },
184 .sink = {
185 .bLength = sizeof(ss_descriptors.sink),
186 .bDescriptorType = USB_DT_ENDPOINT,
187 .bEndpointAddress = 2 | USB_DIR_IN,
188 .bmAttributes = USB_ENDPOINT_XFER_BULK,
189 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
190 },
191 .sink_comp = {
192 .bLength = sizeof(ss_descriptors.sink_comp),
193 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
194 },
195};
196
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700197struct usb_ext_compat_desc os_desc_compat = {
198 .bFirstInterfaceNumber = 0,
199 .Reserved1 = cpu_to_le32(1),
200 .CompatibleID = {0},
201 .SubCompatibleID = {0},
202 .Reserved2 = {0},
203};
204
205static struct usb_os_desc_header os_desc_header = {
206 .interface = cpu_to_le32(1),
207 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
208 .bcdVersion = cpu_to_le32(1),
209 .wIndex = cpu_to_le32(4),
210 .bCount = cpu_to_le32(1),
211 .Reserved = cpu_to_le32(0),
212};
213
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100214#define STR_INTERFACE_ "ADB Interface"
215
216static const struct {
217 struct usb_functionfs_strings_head header;
218 struct {
219 __le16 code;
220 const char str1[sizeof(STR_INTERFACE_)];
221 } __attribute__((packed)) lang0;
222} __attribute__((packed)) strings = {
223 .header = {
224 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
225 .length = cpu_to_le32(sizeof(strings)),
226 .str_count = cpu_to_le32(1),
227 .lang_count = cpu_to_le32(1),
228 },
229 .lang0 = {
230 cpu_to_le16(0x0409), /* en-us */
231 STR_INTERFACE_,
232 },
233};
234
Josh Gao44c688c2017-01-11 17:37:35 -0800235bool init_functionfs(struct usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100236 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700237 struct desc_v1 v1_descriptor;
238 struct desc_v2 v2_descriptor;
Jerry Zhang55205a52017-01-04 12:34:38 -0800239 size_t retries = 0;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700240
241 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
242 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phama190c802015-06-02 10:36:43 -0700243 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700244 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800245 v2_descriptor.fs_count = 3;
246 v2_descriptor.hs_count = 3;
Jack Phama190c802015-06-02 10:36:43 -0700247 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700248 v2_descriptor.os_count = 1;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700249 v2_descriptor.fs_descs = fs_descriptors;
250 v2_descriptor.hs_descs = hs_descriptors;
Jack Phama190c802015-06-02 10:36:43 -0700251 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700252 v2_descriptor.os_header = os_desc_header;
253 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100254
Siqi Lin8c407622016-05-26 13:04:52 -0700255 if (h->control < 0) { // might have already done this before
256 D("OPENING %s", USB_FFS_ADB_EP0);
257 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
258 if (h->control < 0) {
259 D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800260 goto err;
261 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100262
Siqi Lin8c407622016-05-26 13:04:52 -0700263 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
264 if (ret < 0) {
265 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
266 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
267 v1_descriptor.header.fs_count = 3;
268 v1_descriptor.header.hs_count = 3;
269 v1_descriptor.fs_descs = fs_descriptors;
270 v1_descriptor.hs_descs = hs_descriptors;
271 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
272 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
273 if (ret < 0) {
274 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
275 goto err;
276 }
277 }
278
279 ret = adb_write(h->control, &strings, sizeof(strings));
280 if (ret < 0) {
281 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
282 goto err;
283 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100284 }
285
286 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
287 if (h->bulk_out < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700288 D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100289 goto err;
290 }
291
292 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
293 if (h->bulk_in < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700294 D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100295 goto err;
296 }
297
Jerry Zhang55205a52017-01-04 12:34:38 -0800298 h->max_rw = MAX_PAYLOAD;
299 while (h->max_rw >= USB_FFS_BULK_SIZE && retries < ENDPOINT_ALLOC_RETRIES) {
300 int ret_in = ioctl(h->bulk_in, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
301 int errno_in = errno;
302 int ret_out = ioctl(h->bulk_out, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
303 int errno_out = errno;
304
305 if (ret_in || ret_out) {
306 if (errno_in == ENODEV || errno_out == ENODEV) {
307 std::this_thread::sleep_for(100ms);
308 retries += 1;
309 continue;
310 }
311 h->max_rw /= 2;
312 } else {
313 return true;
314 }
315 }
316
317 D("[ adb: cannot call endpoint alloc: errno=%d ]", errno);
318 // Kernel pre-allocation could have failed for recoverable reasons.
319 // Continue running with a safe max rw size.
320 h->max_rw *= 2;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700321 return true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100322
323err:
324 if (h->bulk_in > 0) {
325 adb_close(h->bulk_in);
326 h->bulk_in = -1;
327 }
328 if (h->bulk_out > 0) {
329 adb_close(h->bulk_out);
330 h->bulk_out = -1;
331 }
332 if (h->control > 0) {
333 adb_close(h->control);
334 h->control = -1;
335 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700336 return false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100337}
338
Josh Gaod9db09c2016-02-12 14:31:15 -0800339static void usb_ffs_open_thread(void* x) {
Josh Gao183b73e2017-01-11 14:39:19 -0800340 struct usb_handle* usb = (struct usb_handle*)x;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100341
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700342 adb_thread_setname("usb ffs open");
343
Elliott Hughesa7090b92015-04-17 17:03:59 -0700344 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100345 // wait until the USB device needs opening
Josh Gao0cd3ae12016-09-21 12:37:10 -0700346 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700347 while (!usb->open_new_connection) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700348 usb->notify.wait(lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700349 }
350 usb->open_new_connection = false;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700351 lock.unlock();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100352
Elliott Hughesa7090b92015-04-17 17:03:59 -0700353 while (true) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700354 if (init_functionfs(usb)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100355 break;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700356 }
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800357 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100358 }
Elliott Hughesffdec182016-09-23 15:40:03 -0700359 android::base::SetProperty("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100360
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700361 D("[ usb_thread - registering device ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100362 register_usb_transport(usb, 0, 0, 1);
363 }
364
365 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800366 abort();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100367}
368
Josh Gao6b531c42015-12-02 17:30:58 -0800369static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700370 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800371
Josh Gao6b531c42015-12-02 17:30:58 -0800372 const char* buf = static_cast<const char*>(data);
373 while (len > 0) {
Jerry Zhang55205a52017-01-04 12:34:38 -0800374 int write_len = std::min(h->max_rw, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800375 int n = adb_write(h->bulk_in, buf, write_len);
376 if (n < 0) {
377 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
378 return -1;
379 }
380 buf += n;
381 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100382 }
Josh Gao6b531c42015-12-02 17:30:58 -0800383
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700384 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100385 return 0;
386}
387
Josh Gao6b531c42015-12-02 17:30:58 -0800388static int usb_ffs_read(usb_handle* h, void* data, int len) {
389 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100390
Josh Gao6b531c42015-12-02 17:30:58 -0800391 char* buf = static_cast<char*>(data);
392 while (len > 0) {
Jerry Zhang55205a52017-01-04 12:34:38 -0800393 int read_len = std::min(h->max_rw, len);
Josh Gao0b195402015-12-16 11:00:08 -0800394 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800395 if (n < 0) {
396 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700397 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100398 }
Josh Gao6b531c42015-12-02 17:30:58 -0800399 buf += n;
400 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700401 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100402
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700403 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100404 return 0;
405}
406
Josh Gao183b73e2017-01-11 14:39:19 -0800407static void usb_ffs_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100408 int err;
409
410 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700411 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700412 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700413 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100414
415 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700416 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700417 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700418 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100419
Jack Pham4cbf1d82013-12-23 17:46:10 -0800420 // don't close ep0 here, since we may not need to reinitialize it with
421 // the same descriptors again. if however ep1/ep2 fail to re-open in
422 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700423 // Ditto the comment in usb_adb_kick.
424 h->kicked = true;
425 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
426 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
427}
428
Josh Gao183b73e2017-01-11 14:39:19 -0800429static void usb_ffs_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700430 h->kicked = false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100431 adb_close(h->bulk_out);
432 adb_close(h->bulk_in);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700433 // Notify usb_adb_open_thread to open a new connection.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700434 h->lock.lock();
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700435 h->open_new_connection = true;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700436 h->lock.unlock();
437 h->notify.notify_one();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438}
439
Josh Gao183b73e2017-01-11 14:39:19 -0800440static void usb_ffs_init() {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700441 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100442
Josh Gao0cd3ae12016-09-21 12:37:10 -0700443 usb_handle* h = new usb_handle();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700444
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100445 h->write = usb_ffs_write;
446 h->read = usb_ffs_read;
447 h->kick = usb_ffs_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700448 h->close = usb_ffs_close;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100449
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700450 D("[ usb_init - starting thread ]");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700451 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100452 fatal_errno("[ cannot create usb thread ]\n");
453 }
454}
455
Josh Gao183b73e2017-01-11 14:39:19 -0800456void usb_init() {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700457 dummy_fd = adb_open("/dev/null", O_WRONLY);
458 CHECK_NE(dummy_fd, -1);
Josh Gao183b73e2017-01-11 14:39:19 -0800459 usb_ffs_init();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100460}
461
Josh Gao183b73e2017-01-11 14:39:19 -0800462int usb_write(usb_handle* h, const void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100463 return h->write(h, data, len);
464}
465
Josh Gao183b73e2017-01-11 14:39:19 -0800466int usb_read(usb_handle* h, void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100467 return h->read(h, data, len);
468}
Josh Gao0cd3ae12016-09-21 12:37:10 -0700469
Josh Gao183b73e2017-01-11 14:39:19 -0800470int usb_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700471 h->close(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 return 0;
473}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100474
Josh Gao183b73e2017-01-11 14:39:19 -0800475void usb_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100476 h->kick(h);
477}