blob: 0ba6b4bb2997dbcc4f967eca75433fe73478f544 [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
Badhri Jagan Sridharan5f973702015-04-21 11:09:32 -070021#include <cutils/properties.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <dirent.h>
23#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080024#include <linux/usb/ch9.h>
25#include <linux/usb/functionfs.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ioctl.h>
30#include <sys/types.h>
31#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Josh Gaoae72b5a2015-12-17 13:45:18 -080033#include <algorithm>
Yabin Cui9b53e4c2016-04-05 14:51:52 -070034#include <atomic>
35
36#include <android-base/logging.h>
Josh Gaoae72b5a2015-12-17 13:45:18 -080037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080039#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010041#define MAX_PACKET_SIZE_FS 64
42#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +080043#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010044
Josh Gaoae72b5a2015-12-17 13:45:18 -080045// Writes larger than 16k fail on some devices (seed with 3.10.49-g209ea2f in particular).
46#define USB_FFS_MAX_WRITE 16384
47
48// The kernel allocates a contiguous buffer for reads, which can fail for large ones due to
49// fragmentation. 16k chosen arbitrarily to match the write limit.
50#define USB_FFS_MAX_READ 16384
51
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010052#define cpu_to_le16(x) htole16(x)
53#define cpu_to_le32(x) htole32(x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Yabin Cui9b53e4c2016-04-05 14:51:52 -070055static int dummy_fd = -1;
56
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057struct usb_handle
58{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059 adb_cond_t notify;
60 adb_mutex_t lock;
Yabin Cui9b53e4c2016-04-05 14:51:52 -070061 bool open_new_connection;
62 std::atomic<bool> kicked;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010063
64 int (*write)(usb_handle *h, const void *data, int len);
65 int (*read)(usb_handle *h, void *data, int len);
66 void (*kick)(usb_handle *h);
Yabin Cui9b53e4c2016-04-05 14:51:52 -070067 void (*close)(usb_handle *h);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010068
69 // Legacy f_adb
70 int fd;
71
72 // FunctionFS
73 int control;
74 int bulk_out; /* "out" from the host's perspective => source for adbd */
75 int bulk_in; /* "in" from the host's perspective => sink for adbd */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076};
77
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070078struct func_desc {
79 struct usb_interface_descriptor intf;
80 struct usb_endpoint_descriptor_no_audio source;
81 struct usb_endpoint_descriptor_no_audio sink;
82} __attribute__((packed));
83
Jack Phama190c802015-06-02 10:36:43 -070084struct ss_func_desc {
85 struct usb_interface_descriptor intf;
86 struct usb_endpoint_descriptor_no_audio source;
87 struct usb_ss_ep_comp_descriptor source_comp;
88 struct usb_endpoint_descriptor_no_audio sink;
89 struct usb_ss_ep_comp_descriptor sink_comp;
90} __attribute__((packed));
91
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070092struct desc_v1 {
93 struct usb_functionfs_descs_head_v1 {
94 __le32 magic;
95 __le32 length;
96 __le32 fs_count;
97 __le32 hs_count;
98 } __attribute__((packed)) header;
99 struct func_desc fs_descs, hs_descs;
100} __attribute__((packed));
101
102struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800103 struct usb_functionfs_descs_head_v2 header;
104 // The rest of the structure depends on the flags in the header.
105 __le32 fs_count;
106 __le32 hs_count;
Jack Phama190c802015-06-02 10:36:43 -0700107 __le32 ss_count;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700108 __le32 os_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700109 struct func_desc fs_descs, hs_descs;
Jack Phama190c802015-06-02 10:36:43 -0700110 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700111 struct usb_os_desc_header os_header;
112 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700113} __attribute__((packed));
114
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700115static struct func_desc fs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700116 .intf = {
117 .bLength = sizeof(fs_descriptors.intf),
118 .bDescriptorType = USB_DT_INTERFACE,
119 .bInterfaceNumber = 0,
120 .bNumEndpoints = 2,
121 .bInterfaceClass = ADB_CLASS,
122 .bInterfaceSubClass = ADB_SUBCLASS,
123 .bInterfaceProtocol = ADB_PROTOCOL,
124 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100125 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700126 .source = {
127 .bLength = sizeof(fs_descriptors.source),
128 .bDescriptorType = USB_DT_ENDPOINT,
129 .bEndpointAddress = 1 | USB_DIR_OUT,
130 .bmAttributes = USB_ENDPOINT_XFER_BULK,
131 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100132 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700133 .sink = {
134 .bLength = sizeof(fs_descriptors.sink),
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = 2 | USB_DIR_IN,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
139 },
140};
141
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700142static struct func_desc hs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700143 .intf = {
144 .bLength = sizeof(hs_descriptors.intf),
145 .bDescriptorType = USB_DT_INTERFACE,
146 .bInterfaceNumber = 0,
147 .bNumEndpoints = 2,
148 .bInterfaceClass = ADB_CLASS,
149 .bInterfaceSubClass = ADB_SUBCLASS,
150 .bInterfaceProtocol = ADB_PROTOCOL,
151 .iInterface = 1, /* first string from the provided table */
152 },
153 .source = {
154 .bLength = sizeof(hs_descriptors.source),
155 .bDescriptorType = USB_DT_ENDPOINT,
156 .bEndpointAddress = 1 | USB_DIR_OUT,
157 .bmAttributes = USB_ENDPOINT_XFER_BULK,
158 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
159 },
160 .sink = {
161 .bLength = sizeof(hs_descriptors.sink),
162 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = 2 | USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_XFER_BULK,
165 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800166 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100167};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Jack Phama190c802015-06-02 10:36:43 -0700169static struct ss_func_desc ss_descriptors = {
170 .intf = {
171 .bLength = sizeof(ss_descriptors.intf),
172 .bDescriptorType = USB_DT_INTERFACE,
173 .bInterfaceNumber = 0,
174 .bNumEndpoints = 2,
175 .bInterfaceClass = ADB_CLASS,
176 .bInterfaceSubClass = ADB_SUBCLASS,
177 .bInterfaceProtocol = ADB_PROTOCOL,
178 .iInterface = 1, /* first string from the provided table */
179 },
180 .source = {
181 .bLength = sizeof(ss_descriptors.source),
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = 1 | USB_DIR_OUT,
184 .bmAttributes = USB_ENDPOINT_XFER_BULK,
185 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
186 },
187 .source_comp = {
188 .bLength = sizeof(ss_descriptors.source_comp),
189 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
190 },
191 .sink = {
192 .bLength = sizeof(ss_descriptors.sink),
193 .bDescriptorType = USB_DT_ENDPOINT,
194 .bEndpointAddress = 2 | USB_DIR_IN,
195 .bmAttributes = USB_ENDPOINT_XFER_BULK,
196 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
197 },
198 .sink_comp = {
199 .bLength = sizeof(ss_descriptors.sink_comp),
200 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
201 },
202};
203
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700204struct usb_ext_compat_desc os_desc_compat = {
205 .bFirstInterfaceNumber = 0,
206 .Reserved1 = cpu_to_le32(1),
207 .CompatibleID = {0},
208 .SubCompatibleID = {0},
209 .Reserved2 = {0},
210};
211
212static struct usb_os_desc_header os_desc_header = {
213 .interface = cpu_to_le32(1),
214 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
215 .bcdVersion = cpu_to_le32(1),
216 .wIndex = cpu_to_le32(4),
217 .bCount = cpu_to_le32(1),
218 .Reserved = cpu_to_le32(0),
219};
220
221
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100222#define STR_INTERFACE_ "ADB Interface"
223
224static const struct {
225 struct usb_functionfs_strings_head header;
226 struct {
227 __le16 code;
228 const char str1[sizeof(STR_INTERFACE_)];
229 } __attribute__((packed)) lang0;
230} __attribute__((packed)) strings = {
231 .header = {
232 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
233 .length = cpu_to_le32(sizeof(strings)),
234 .str_count = cpu_to_le32(1),
235 .lang_count = cpu_to_le32(1),
236 },
237 .lang0 = {
238 cpu_to_le16(0x0409), /* en-us */
239 STR_INTERFACE_,
240 },
241};
242
Josh Gaod9db09c2016-02-12 14:31:15 -0800243static void usb_adb_open_thread(void* x) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 struct usb_handle *usb = (struct usb_handle *)x;
245 int fd;
246
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700247 adb_thread_setname("usb open");
248
Elliott Hughesa7090b92015-04-17 17:03:59 -0700249 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 // wait until the USB device needs opening
251 adb_mutex_lock(&usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700252 while (!usb->open_new_connection) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 adb_cond_wait(&usb->notify, &usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700254 }
255 usb->open_new_connection = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 adb_mutex_unlock(&usb->lock);
257
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700258 D("[ usb_thread - opening device ]");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 do {
260 /* XXX use inotify? */
261 fd = unix_open("/dev/android_adb", O_RDWR);
262 if (fd < 0) {
263 // to support older kernels
264 fd = unix_open("/dev/android", O_RDWR);
265 }
266 if (fd < 0) {
267 adb_sleep_ms(1000);
268 }
269 } while (fd < 0);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700270 D("[ opening device succeeded ]");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
272 close_on_exec(fd);
273 usb->fd = fd;
274
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700275 D("[ usb_thread - registering device ]");
Scott Andersone109d262012-04-20 11:21:14 -0700276 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 }
278
279 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800280 abort();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281}
282
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100283static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284{
285 int n;
286
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700287 D("about to write (fd=%d, len=%d)", h->fd, len);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700288 n = unix_write(h->fd, data, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 if(n != len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700290 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
JP Abgrall408fa572011-03-16 15:57:42 -0700291 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 return -1;
293 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700294 if (h->kicked) {
295 D("usb_adb_write finished due to kicked");
296 return -1;
297 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700298 D("[ done fd=%d ]", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 return 0;
300}
301
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100302static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700304 D("about to read (fd=%d, len=%d)", h->fd, len);
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100305 while (len > 0) {
306 // The kernel implementation of adb_read in f_adb.c doesn't support
307 // reads larger then 4096 bytes. Read the data in 4096 byte chunks to
308 // avoid the issue. (The ffs implementation doesn't have this limit.)
309 int bytes_to_read = len < 4096 ? len : 4096;
310 int n = unix_read(h->fd, data, bytes_to_read);
311 if (n != bytes_to_read) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700312 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100313 h->fd, n, errno, strerror(errno));
314 return -1;
315 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700316 if (h->kicked) {
317 D("usb_adb_read finished due to kicked");
318 return -1;
319 }
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100320 len -= n;
321 data = ((char*)data) + n;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700323 D("[ done fd=%d ]", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 return 0;
325}
326
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700327static void usb_adb_kick(usb_handle *h) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700328 D("usb_kick");
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700329 // Other threads may be calling usb_adb_read/usb_adb_write at the same time.
330 // If we close h->fd, the file descriptor will be reused to open other files,
331 // and the read/write thread may operate on the wrong file. So instead
332 // we set the kicked flag and reopen h->fd to a dummy file here. After read/write
333 // threads finish, we close h->fd in usb_adb_close().
334 h->kicked = true;
335 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->fd));
336}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100337
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700338static void usb_adb_close(usb_handle *h) {
339 h->kicked = false;
340 adb_close(h->fd);
341 // Notify usb_adb_open_thread to open a new connection.
342 adb_mutex_lock(&h->lock);
343 h->open_new_connection = true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100344 adb_cond_signal(&h->notify);
345 adb_mutex_unlock(&h->lock);
346}
347
348static void usb_adb_init()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349{
Elliott Hughes2acec912015-04-16 14:12:50 -0700350 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700351 if (h == nullptr) fatal("couldn't allocate usb_handle");
352
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100353 h->write = usb_adb_write;
354 h->read = usb_adb_read;
355 h->kick = usb_adb_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700356 h->close = usb_adb_close;
357 h->kicked = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 h->fd = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100359
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700360 h->open_new_connection = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 adb_cond_init(&h->notify, 0);
362 adb_mutex_init(&h->lock, 0);
363
Elliott Hughes2acec912015-04-16 14:12:50 -0700364 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 // the enabling of the adb USB function in the kernel.
366 // We never touch this file again - just leave it open
367 // indefinitely so the kernel will know when we are running
368 // and when we are not.
Elliott Hughes2acec912015-04-16 14:12:50 -0700369 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 if (fd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700371 D("failed to open /dev/android_adb_enable");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 } else {
373 close_on_exec(fd);
374 }
375
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700376 D("[ usb_init - starting thread ]");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700377 if (!adb_thread_create(usb_adb_open_thread, h)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 fatal_errno("cannot create usb thread");
379 }
380}
381
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700383static bool init_functionfs(struct usb_handle *h)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100384{
385 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700386 struct desc_v1 v1_descriptor;
387 struct desc_v2 v2_descriptor;
388
389 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
390 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phama190c802015-06-02 10:36:43 -0700391 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700392 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800393 v2_descriptor.fs_count = 3;
394 v2_descriptor.hs_count = 3;
Jack Phama190c802015-06-02 10:36:43 -0700395 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700396 v2_descriptor.os_count = 1;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700397 v2_descriptor.fs_descs = fs_descriptors;
398 v2_descriptor.hs_descs = hs_descriptors;
Jack Phama190c802015-06-02 10:36:43 -0700399 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700400 v2_descriptor.os_header = os_desc_header;
401 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100402
Siqi Lin57de0512016-05-26 13:04:52 -0700403 if (h->control < 0) { // might have already done this before
404 D("OPENING %s", USB_FFS_ADB_EP0);
405 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
406 if (h->control < 0) {
407 D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800408 goto err;
409 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100410
Siqi Lin57de0512016-05-26 13:04:52 -0700411 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
412 if (ret < 0) {
413 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
414 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
415 v1_descriptor.header.fs_count = 3;
416 v1_descriptor.header.hs_count = 3;
417 v1_descriptor.fs_descs = fs_descriptors;
418 v1_descriptor.hs_descs = hs_descriptors;
419 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
420 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
421 if (ret < 0) {
422 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
423 goto err;
424 }
425 }
426
427 ret = adb_write(h->control, &strings, sizeof(strings));
428 if (ret < 0) {
429 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
430 goto err;
431 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100432 }
433
434 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
435 if (h->bulk_out < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700436 D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100437 goto err;
438 }
439
440 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
441 if (h->bulk_in < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700442 D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100443 goto err;
444 }
445
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700446 return true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100447
448err:
449 if (h->bulk_in > 0) {
450 adb_close(h->bulk_in);
451 h->bulk_in = -1;
452 }
453 if (h->bulk_out > 0) {
454 adb_close(h->bulk_out);
455 h->bulk_out = -1;
456 }
457 if (h->control > 0) {
458 adb_close(h->control);
459 h->control = -1;
460 }
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700461 return false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100462}
463
Josh Gaod9db09c2016-02-12 14:31:15 -0800464static void usb_ffs_open_thread(void* x) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100465 struct usb_handle *usb = (struct usb_handle *)x;
466
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700467 adb_thread_setname("usb ffs open");
468
Elliott Hughesa7090b92015-04-17 17:03:59 -0700469 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100470 // wait until the USB device needs opening
471 adb_mutex_lock(&usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700472 while (!usb->open_new_connection) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100473 adb_cond_wait(&usb->notify, &usb->lock);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700474 }
475 usb->open_new_connection = false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100476 adb_mutex_unlock(&usb->lock);
477
Elliott Hughesa7090b92015-04-17 17:03:59 -0700478 while (true) {
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700479 if (init_functionfs(usb)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100480 break;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700481 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100482 adb_sleep_ms(1000);
483 }
Badhri Jagan Sridharan5f973702015-04-21 11:09:32 -0700484 property_set("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100485
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700486 D("[ usb_thread - registering device ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100487 register_usb_transport(usb, 0, 0, 1);
488 }
489
490 // never gets here
Josh Gaod9db09c2016-02-12 14:31:15 -0800491 abort();
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100492}
493
Josh Gao6b531c42015-12-02 17:30:58 -0800494static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700495 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800496
Josh Gao6b531c42015-12-02 17:30:58 -0800497 const char* buf = static_cast<const char*>(data);
498 while (len > 0) {
Josh Gaoae72b5a2015-12-17 13:45:18 -0800499 int write_len = std::min(USB_FFS_MAX_WRITE, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800500 int n = adb_write(h->bulk_in, buf, write_len);
501 if (n < 0) {
502 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
503 return -1;
504 }
505 buf += n;
506 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100507 }
Josh Gao6b531c42015-12-02 17:30:58 -0800508
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700509 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100510 return 0;
511}
512
Josh Gao6b531c42015-12-02 17:30:58 -0800513static int usb_ffs_read(usb_handle* h, void* data, int len) {
514 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100515
Josh Gao6b531c42015-12-02 17:30:58 -0800516 char* buf = static_cast<char*>(data);
517 while (len > 0) {
Josh Gaoae72b5a2015-12-17 13:45:18 -0800518 int read_len = std::min(USB_FFS_MAX_READ, len);
Josh Gao0b195402015-12-16 11:00:08 -0800519 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800520 if (n < 0) {
521 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700522 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100523 }
Josh Gao6b531c42015-12-02 17:30:58 -0800524 buf += n;
525 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700526 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100527
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700528 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100529 return 0;
530}
531
532static void usb_ffs_kick(usb_handle *h)
533{
534 int err;
535
536 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700537 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700538 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700539 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100540
541 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700542 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700543 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700544 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100545
Jack Pham4cbf1d82013-12-23 17:46:10 -0800546 // don't close ep0 here, since we may not need to reinitialize it with
547 // the same descriptors again. if however ep1/ep2 fail to re-open in
548 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700549 // Ditto the comment in usb_adb_kick.
550 h->kicked = true;
551 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
552 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
553}
554
555static void usb_ffs_close(usb_handle *h) {
556 h->kicked = false;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100557 adb_close(h->bulk_out);
558 adb_close(h->bulk_in);
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700559 // Notify usb_adb_open_thread to open a new connection.
560 adb_mutex_lock(&h->lock);
561 h->open_new_connection = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 adb_cond_signal(&h->notify);
563 adb_mutex_unlock(&h->lock);
564}
565
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100566static void usb_ffs_init()
567{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700568 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100569
Elliott Hughes2acec912015-04-16 14:12:50 -0700570 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700571 if (h == nullptr) fatal("couldn't allocate usb_handle");
572
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100573 h->write = usb_ffs_write;
574 h->read = usb_ffs_read;
575 h->kick = usb_ffs_kick;
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700576 h->close = usb_ffs_close;
577 h->kicked = false;
Elliott Hughes2acec912015-04-16 14:12:50 -0700578 h->control = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100579 h->bulk_out = -1;
580 h->bulk_out = -1;
581
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700582 h->open_new_connection = true;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100583 adb_cond_init(&h->notify, 0);
584 adb_mutex_init(&h->lock, 0);
585
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700586 D("[ usb_init - starting thread ]");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700587 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100588 fatal_errno("[ cannot create usb thread ]\n");
589 }
590}
591
592void usb_init()
593{
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700594 dummy_fd = adb_open("/dev/null", O_WRONLY);
595 CHECK_NE(dummy_fd, -1);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100596 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
597 usb_ffs_init();
598 else
599 usb_adb_init();
600}
601
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100602int usb_write(usb_handle *h, const void *data, int len)
603{
604 return h->write(h, data, len);
605}
606
607int usb_read(usb_handle *h, void *data, int len)
608{
609 return h->read(h, data, len);
610}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611int usb_close(usb_handle *h)
612{
Yabin Cui9b53e4c2016-04-05 14:51:52 -0700613 h->close(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614 return 0;
615}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100616
617void usb_kick(usb_handle *h)
618{
619 h->kick(h);
620}