blob: a35c210f93f6bda008e98139d435b4f58f153b41 [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
Josh Gaoae72b5a2015-12-17 13:45:18 -080052// Writes larger than 16k fail on some devices (seed with 3.10.49-g209ea2f in particular).
53#define USB_FFS_MAX_WRITE 16384
54
55// The kernel allocates a contiguous buffer for reads, which can fail for large ones due to
56// fragmentation. 16k chosen arbitrarily to match the write limit.
57#define USB_FFS_MAX_READ 16384
58
Josh Gao44c688c2017-01-11 17:37:35 -080059#define cpu_to_le16(x) htole16(x)
60#define cpu_to_le32(x) htole32(x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Yabin Cui9b53e4c2016-04-05 14:51:52 -070062static int dummy_fd = -1;
63
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070064struct func_desc {
65 struct usb_interface_descriptor intf;
66 struct usb_endpoint_descriptor_no_audio source;
67 struct usb_endpoint_descriptor_no_audio sink;
68} __attribute__((packed));
69
Jack Phama190c802015-06-02 10:36:43 -070070struct ss_func_desc {
71 struct usb_interface_descriptor intf;
72 struct usb_endpoint_descriptor_no_audio source;
73 struct usb_ss_ep_comp_descriptor source_comp;
74 struct usb_endpoint_descriptor_no_audio sink;
75 struct usb_ss_ep_comp_descriptor sink_comp;
76} __attribute__((packed));
77
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070078struct desc_v1 {
79 struct usb_functionfs_descs_head_v1 {
80 __le32 magic;
81 __le32 length;
82 __le32 fs_count;
83 __le32 hs_count;
84 } __attribute__((packed)) header;
85 struct func_desc fs_descs, hs_descs;
86} __attribute__((packed));
87
88struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080089 struct usb_functionfs_descs_head_v2 header;
90 // The rest of the structure depends on the flags in the header.
91 __le32 fs_count;
92 __le32 hs_count;
Jack Phama190c802015-06-02 10:36:43 -070093 __le32 ss_count;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -070094 __le32 os_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070095 struct func_desc fs_descs, hs_descs;
Jack Phama190c802015-06-02 10:36:43 -070096 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -070097 struct usb_os_desc_header os_header;
98 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070099} __attribute__((packed));
100
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700101static struct func_desc fs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700102 .intf = {
103 .bLength = sizeof(fs_descriptors.intf),
104 .bDescriptorType = USB_DT_INTERFACE,
105 .bInterfaceNumber = 0,
106 .bNumEndpoints = 2,
107 .bInterfaceClass = ADB_CLASS,
108 .bInterfaceSubClass = ADB_SUBCLASS,
109 .bInterfaceProtocol = ADB_PROTOCOL,
110 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100111 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700112 .source = {
113 .bLength = sizeof(fs_descriptors.source),
114 .bDescriptorType = USB_DT_ENDPOINT,
115 .bEndpointAddress = 1 | USB_DIR_OUT,
116 .bmAttributes = USB_ENDPOINT_XFER_BULK,
117 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100118 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700119 .sink = {
120 .bLength = sizeof(fs_descriptors.sink),
121 .bDescriptorType = USB_DT_ENDPOINT,
122 .bEndpointAddress = 2 | USB_DIR_IN,
123 .bmAttributes = USB_ENDPOINT_XFER_BULK,
124 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
125 },
126};
127
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700128static struct func_desc hs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700129 .intf = {
130 .bLength = sizeof(hs_descriptors.intf),
131 .bDescriptorType = USB_DT_INTERFACE,
132 .bInterfaceNumber = 0,
133 .bNumEndpoints = 2,
134 .bInterfaceClass = ADB_CLASS,
135 .bInterfaceSubClass = ADB_SUBCLASS,
136 .bInterfaceProtocol = ADB_PROTOCOL,
137 .iInterface = 1, /* first string from the provided table */
138 },
139 .source = {
140 .bLength = sizeof(hs_descriptors.source),
141 .bDescriptorType = USB_DT_ENDPOINT,
142 .bEndpointAddress = 1 | USB_DIR_OUT,
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
144 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
145 },
146 .sink = {
147 .bLength = sizeof(hs_descriptors.sink),
148 .bDescriptorType = USB_DT_ENDPOINT,
149 .bEndpointAddress = 2 | USB_DIR_IN,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
151 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800152 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100153};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Jack Phama190c802015-06-02 10:36:43 -0700155static struct ss_func_desc ss_descriptors = {
156 .intf = {
157 .bLength = sizeof(ss_descriptors.intf),
158 .bDescriptorType = USB_DT_INTERFACE,
159 .bInterfaceNumber = 0,
160 .bNumEndpoints = 2,
161 .bInterfaceClass = ADB_CLASS,
162 .bInterfaceSubClass = ADB_SUBCLASS,
163 .bInterfaceProtocol = ADB_PROTOCOL,
164 .iInterface = 1, /* first string from the provided table */
165 },
166 .source = {
167 .bLength = sizeof(ss_descriptors.source),
168 .bDescriptorType = USB_DT_ENDPOINT,
169 .bEndpointAddress = 1 | USB_DIR_OUT,
170 .bmAttributes = USB_ENDPOINT_XFER_BULK,
171 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
172 },
173 .source_comp = {
174 .bLength = sizeof(ss_descriptors.source_comp),
175 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
176 },
177 .sink = {
178 .bLength = sizeof(ss_descriptors.sink),
179 .bDescriptorType = USB_DT_ENDPOINT,
180 .bEndpointAddress = 2 | USB_DIR_IN,
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
182 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
183 },
184 .sink_comp = {
185 .bLength = sizeof(ss_descriptors.sink_comp),
186 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
187 },
188};
189
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700190struct usb_ext_compat_desc os_desc_compat = {
191 .bFirstInterfaceNumber = 0,
192 .Reserved1 = cpu_to_le32(1),
193 .CompatibleID = {0},
194 .SubCompatibleID = {0},
195 .Reserved2 = {0},
196};
197
198static struct usb_os_desc_header os_desc_header = {
199 .interface = cpu_to_le32(1),
200 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
201 .bcdVersion = cpu_to_le32(1),
202 .wIndex = cpu_to_le32(4),
203 .bCount = cpu_to_le32(1),
204 .Reserved = cpu_to_le32(0),
205};
206
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100207#define STR_INTERFACE_ "ADB Interface"
208
209static const struct {
210 struct usb_functionfs_strings_head header;
211 struct {
212 __le16 code;
213 const char str1[sizeof(STR_INTERFACE_)];
214 } __attribute__((packed)) lang0;
215} __attribute__((packed)) strings = {
216 .header = {
217 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
218 .length = cpu_to_le32(sizeof(strings)),
219 .str_count = cpu_to_le32(1),
220 .lang_count = cpu_to_le32(1),
221 },
222 .lang0 = {
223 cpu_to_le16(0x0409), /* en-us */
224 STR_INTERFACE_,
225 },
226};
227
Josh Gao44c688c2017-01-11 17:37:35 -0800228bool init_functionfs(struct usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100229 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700230 struct desc_v1 v1_descriptor;
231 struct desc_v2 v2_descriptor;
232
233 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
234 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phama190c802015-06-02 10:36:43 -0700235 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700236 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800237 v2_descriptor.fs_count = 3;
238 v2_descriptor.hs_count = 3;
Jack Phama190c802015-06-02 10:36:43 -0700239 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700240 v2_descriptor.os_count = 1;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700241 v2_descriptor.fs_descs = fs_descriptors;
242 v2_descriptor.hs_descs = hs_descriptors;
Jack Phama190c802015-06-02 10:36:43 -0700243 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700244 v2_descriptor.os_header = os_desc_header;
245 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100246
Siqi Lin8c407622016-05-26 13:04:52 -0700247 if (h->control < 0) { // might have already done this before
248 D("OPENING %s", USB_FFS_ADB_EP0);
249 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
250 if (h->control < 0) {
251 D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800252 goto err;
253 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100254
Siqi Lin8c407622016-05-26 13:04:52 -0700255 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
256 if (ret < 0) {
257 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
258 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
259 v1_descriptor.header.fs_count = 3;
260 v1_descriptor.header.hs_count = 3;
261 v1_descriptor.fs_descs = fs_descriptors;
262 v1_descriptor.hs_descs = hs_descriptors;
263 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
264 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
265 if (ret < 0) {
266 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
267 goto err;
268 }
269 }
270
271 ret = adb_write(h->control, &strings, sizeof(strings));
272 if (ret < 0) {
273 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
274 goto err;
275 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100276 }
277
278 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
279 if (h->bulk_out < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700280 D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100281 goto err;
282 }
283
284 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
285 if (h->bulk_in < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700286 D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100287 goto err;
288 }
289
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700290 return true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100291
292err:
293 if (h->bulk_in > 0) {
294 adb_close(h->bulk_in);
295 h->bulk_in = -1;
296 }
297 if (h->bulk_out > 0) {
298 adb_close(h->bulk_out);
299 h->bulk_out = -1;
300 }
301 if (h->control > 0) {
302 adb_close(h->control);
303 h->control = -1;
304 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700305 return false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100306}
307
Josh Gaod9db09c2016-02-12 14:31:15 -0800308static void usb_ffs_open_thread(void* x) {
Josh Gao183b73e2017-01-11 14:39:19 -0800309 struct usb_handle* usb = (struct usb_handle*)x;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100310
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700311 adb_thread_setname("usb ffs open");
312
Elliott Hughesa7090b92015-04-17 17:03:59 -0700313 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100314 // wait until the USB device needs opening
Josh Gao0cd3ae12016-09-21 12:37:10 -0700315 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700316 while (!usb->open_new_connection) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700317 usb->notify.wait(lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700318 }
319 usb->open_new_connection = false;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700320 lock.unlock();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100321
Elliott Hughesa7090b92015-04-17 17:03:59 -0700322 while (true) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700323 if (init_functionfs(usb)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100324 break;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700325 }
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800326 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100327 }
Elliott Hughesffdec182016-09-23 15:40:03 -0700328 android::base::SetProperty("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100329
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700330 D("[ usb_thread - registering device ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100331 register_usb_transport(usb, 0, 0, 1);
332 }
333
334 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800335 abort();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100336}
337
Josh Gao6b531c42015-12-02 17:30:58 -0800338static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700339 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800340
Josh Gao6b531c42015-12-02 17:30:58 -0800341 const char* buf = static_cast<const char*>(data);
342 while (len > 0) {
Josh Gaoae72b5a2015-12-17 13:45:18 -0800343 int write_len = std::min(USB_FFS_MAX_WRITE, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800344 int n = adb_write(h->bulk_in, buf, write_len);
345 if (n < 0) {
346 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
347 return -1;
348 }
349 buf += n;
350 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100351 }
Josh Gao6b531c42015-12-02 17:30:58 -0800352
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700353 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100354 return 0;
355}
356
Josh Gao6b531c42015-12-02 17:30:58 -0800357static int usb_ffs_read(usb_handle* h, void* data, int len) {
358 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100359
Josh Gao6b531c42015-12-02 17:30:58 -0800360 char* buf = static_cast<char*>(data);
361 while (len > 0) {
Josh Gaoae72b5a2015-12-17 13:45:18 -0800362 int read_len = std::min(USB_FFS_MAX_READ, len);
Josh Gao0b195402015-12-16 11:00:08 -0800363 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800364 if (n < 0) {
365 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700366 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100367 }
Josh Gao6b531c42015-12-02 17:30:58 -0800368 buf += n;
369 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700370 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100371
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700372 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100373 return 0;
374}
375
Josh Gao183b73e2017-01-11 14:39:19 -0800376static void usb_ffs_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100377 int err;
378
379 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700380 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700381 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700382 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100383
384 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700385 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700386 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700387 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100388
Jack Pham4cbf1d82013-12-23 17:46:10 -0800389 // don't close ep0 here, since we may not need to reinitialize it with
390 // the same descriptors again. if however ep1/ep2 fail to re-open in
391 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700392 // Ditto the comment in usb_adb_kick.
393 h->kicked = true;
394 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
395 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
396}
397
Josh Gao183b73e2017-01-11 14:39:19 -0800398static void usb_ffs_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700399 h->kicked = false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100400 adb_close(h->bulk_out);
401 adb_close(h->bulk_in);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700402 // Notify usb_adb_open_thread to open a new connection.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700403 h->lock.lock();
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700404 h->open_new_connection = true;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700405 h->lock.unlock();
406 h->notify.notify_one();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407}
408
Josh Gao183b73e2017-01-11 14:39:19 -0800409static void usb_ffs_init() {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700410 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100411
Josh Gao0cd3ae12016-09-21 12:37:10 -0700412 usb_handle* h = new usb_handle();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700413
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100414 h->write = usb_ffs_write;
415 h->read = usb_ffs_read;
416 h->kick = usb_ffs_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700417 h->close = usb_ffs_close;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100418
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700419 D("[ usb_init - starting thread ]");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700420 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100421 fatal_errno("[ cannot create usb thread ]\n");
422 }
423}
424
Josh Gao183b73e2017-01-11 14:39:19 -0800425void usb_init() {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700426 dummy_fd = adb_open("/dev/null", O_WRONLY);
427 CHECK_NE(dummy_fd, -1);
Josh Gao183b73e2017-01-11 14:39:19 -0800428 usb_ffs_init();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100429}
430
Josh Gao183b73e2017-01-11 14:39:19 -0800431int usb_write(usb_handle* h, const void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100432 return h->write(h, data, len);
433}
434
Josh Gao183b73e2017-01-11 14:39:19 -0800435int usb_read(usb_handle* h, void* data, int len) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100436 return h->read(h, data, len);
437}
Josh Gao0cd3ae12016-09-21 12:37:10 -0700438
Josh Gao183b73e2017-01-11 14:39:19 -0800439int usb_close(usb_handle* h) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700440 h->close(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 return 0;
442}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100443
Josh Gao183b73e2017-01-11 14:39:19 -0800444void usb_kick(usb_handle* h) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100445 h->kick(h);
446}