blob: 1cc7f68b6fe0448e5658932d547cd606aad218fe [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include <dirent.h>
22#include <errno.h>
Dan Albertb302d122015-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 Project9ca14dc2009-03-03 19:32:55 -080031
Josh Gaoc95bb3d2015-12-17 13:45:18 -080032#include <algorithm>
Yabin Cui6760b2a2016-04-05 14:51:52 -070033#include <atomic>
Elliott Hughes73925982016-11-15 12:37:32 -080034#include <chrono>
Josh Gaoe7daf572016-09-21 12:37:10 -070035#include <condition_variable>
36#include <mutex>
Elliott Hughes73925982016-11-15 12:37:32 -080037#include <thread>
Yabin Cui6760b2a2016-04-05 14:51:52 -070038
39#include <android-base/logging.h>
Elliott Hughes8b249d22016-09-23 15:40:03 -070040#include <android-base/properties.h>
Josh Gaoc95bb3d2015-12-17 13:45:18 -080041
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#include "adb.h"
Dan Albertb302d122015-02-24 15:51:19 -080043#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044
Elliott Hughes73925982016-11-15 12:37:32 -080045using namespace std::chrono_literals;
46
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010047#define MAX_PACKET_SIZE_FS 64
48#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Can1fc58c52014-09-02 13:04:44 +080049#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010050
Josh Gaoc95bb3d2015-12-17 13:45:18 -080051// Writes larger than 16k fail on some devices (seed with 3.10.49-g209ea2f in particular).
52#define USB_FFS_MAX_WRITE 16384
53
54// The kernel allocates a contiguous buffer for reads, which can fail for large ones due to
55// fragmentation. 16k chosen arbitrarily to match the write limit.
56#define USB_FFS_MAX_READ 16384
57
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010058#define cpu_to_le16(x) htole16(x)
59#define cpu_to_le32(x) htole32(x)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060
Yabin Cui6760b2a2016-04-05 14:51:52 -070061static int dummy_fd = -1;
62
Josh Gaoe7daf572016-09-21 12:37:10 -070063struct usb_handle {
64 usb_handle() : kicked(false) {
65 }
66
67 std::condition_variable notify;
68 std::mutex lock;
Yabin Cui6760b2a2016-04-05 14:51:52 -070069 std::atomic<bool> kicked;
Josh Gaoe7daf572016-09-21 12:37:10 -070070 bool open_new_connection = true;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010071
72 int (*write)(usb_handle *h, const void *data, int len);
73 int (*read)(usb_handle *h, void *data, int len);
74 void (*kick)(usb_handle *h);
Yabin Cui6760b2a2016-04-05 14:51:52 -070075 void (*close)(usb_handle *h);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010076
77 // Legacy f_adb
Josh Gaoe7daf572016-09-21 12:37:10 -070078 int fd = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010079
80 // FunctionFS
Josh Gaoe7daf572016-09-21 12:37:10 -070081 int control = -1;
82 int bulk_out = -1; /* "out" from the host's perspective => source for adbd */
83 int bulk_in = -1; /* "in" from the host's perspective => sink for adbd */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080084};
85
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070086struct func_desc {
87 struct usb_interface_descriptor intf;
88 struct usb_endpoint_descriptor_no_audio source;
89 struct usb_endpoint_descriptor_no_audio sink;
90} __attribute__((packed));
91
Jack Phamb64ab092015-06-02 10:36:43 -070092struct ss_func_desc {
93 struct usb_interface_descriptor intf;
94 struct usb_endpoint_descriptor_no_audio source;
95 struct usb_ss_ep_comp_descriptor source_comp;
96 struct usb_endpoint_descriptor_no_audio sink;
97 struct usb_ss_ep_comp_descriptor sink_comp;
98} __attribute__((packed));
99
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700100struct desc_v1 {
101 struct usb_functionfs_descs_head_v1 {
102 __le32 magic;
103 __le32 length;
104 __le32 fs_count;
105 __le32 hs_count;
106 } __attribute__((packed)) header;
107 struct func_desc fs_descs, hs_descs;
108} __attribute__((packed));
109
110struct desc_v2 {
Christopher Ferrisf7555b12015-01-23 17:09:56 -0800111 struct usb_functionfs_descs_head_v2 header;
112 // The rest of the structure depends on the flags in the header.
113 __le32 fs_count;
114 __le32 hs_count;
Jack Phamb64ab092015-06-02 10:36:43 -0700115 __le32 ss_count;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700116 __le32 os_count;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700117 struct func_desc fs_descs, hs_descs;
Jack Phamb64ab092015-06-02 10:36:43 -0700118 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700119 struct usb_os_desc_header os_header;
120 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700121} __attribute__((packed));
122
Elliott Hughes712416a2015-05-05 18:26:10 -0700123static struct func_desc fs_descriptors = {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700124 .intf = {
125 .bLength = sizeof(fs_descriptors.intf),
126 .bDescriptorType = USB_DT_INTERFACE,
127 .bInterfaceNumber = 0,
128 .bNumEndpoints = 2,
129 .bInterfaceClass = ADB_CLASS,
130 .bInterfaceSubClass = ADB_SUBCLASS,
131 .bInterfaceProtocol = ADB_PROTOCOL,
132 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100133 },
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700134 .source = {
135 .bLength = sizeof(fs_descriptors.source),
136 .bDescriptorType = USB_DT_ENDPOINT,
137 .bEndpointAddress = 1 | USB_DIR_OUT,
138 .bmAttributes = USB_ENDPOINT_XFER_BULK,
139 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100140 },
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700141 .sink = {
142 .bLength = sizeof(fs_descriptors.sink),
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = 2 | USB_DIR_IN,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
147 },
148};
149
Elliott Hughes712416a2015-05-05 18:26:10 -0700150static struct func_desc hs_descriptors = {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700151 .intf = {
152 .bLength = sizeof(hs_descriptors.intf),
153 .bDescriptorType = USB_DT_INTERFACE,
154 .bInterfaceNumber = 0,
155 .bNumEndpoints = 2,
156 .bInterfaceClass = ADB_CLASS,
157 .bInterfaceSubClass = ADB_SUBCLASS,
158 .bInterfaceProtocol = ADB_PROTOCOL,
159 .iInterface = 1, /* first string from the provided table */
160 },
161 .source = {
162 .bLength = sizeof(hs_descriptors.source),
163 .bDescriptorType = USB_DT_ENDPOINT,
164 .bEndpointAddress = 1 | USB_DIR_OUT,
165 .bmAttributes = USB_ENDPOINT_XFER_BULK,
166 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
167 },
168 .sink = {
169 .bLength = sizeof(hs_descriptors.sink),
170 .bDescriptorType = USB_DT_ENDPOINT,
171 .bEndpointAddress = 2 | USB_DIR_IN,
172 .bmAttributes = USB_ENDPOINT_XFER_BULK,
173 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Can1fc58c52014-09-02 13:04:44 +0800174 },
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100175};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800176
Jack Phamb64ab092015-06-02 10:36:43 -0700177static struct ss_func_desc ss_descriptors = {
178 .intf = {
179 .bLength = sizeof(ss_descriptors.intf),
180 .bDescriptorType = USB_DT_INTERFACE,
181 .bInterfaceNumber = 0,
182 .bNumEndpoints = 2,
183 .bInterfaceClass = ADB_CLASS,
184 .bInterfaceSubClass = ADB_SUBCLASS,
185 .bInterfaceProtocol = ADB_PROTOCOL,
186 .iInterface = 1, /* first string from the provided table */
187 },
188 .source = {
189 .bLength = sizeof(ss_descriptors.source),
190 .bDescriptorType = USB_DT_ENDPOINT,
191 .bEndpointAddress = 1 | USB_DIR_OUT,
192 .bmAttributes = USB_ENDPOINT_XFER_BULK,
193 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
194 },
195 .source_comp = {
196 .bLength = sizeof(ss_descriptors.source_comp),
197 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
198 },
199 .sink = {
200 .bLength = sizeof(ss_descriptors.sink),
201 .bDescriptorType = USB_DT_ENDPOINT,
202 .bEndpointAddress = 2 | USB_DIR_IN,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
205 },
206 .sink_comp = {
207 .bLength = sizeof(ss_descriptors.sink_comp),
208 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
209 },
210};
211
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700212struct usb_ext_compat_desc os_desc_compat = {
213 .bFirstInterfaceNumber = 0,
214 .Reserved1 = cpu_to_le32(1),
215 .CompatibleID = {0},
216 .SubCompatibleID = {0},
217 .Reserved2 = {0},
218};
219
220static struct usb_os_desc_header os_desc_header = {
221 .interface = cpu_to_le32(1),
222 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
223 .bcdVersion = cpu_to_le32(1),
224 .wIndex = cpu_to_le32(4),
225 .bCount = cpu_to_le32(1),
226 .Reserved = cpu_to_le32(0),
227};
228
229
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100230#define STR_INTERFACE_ "ADB Interface"
231
232static const struct {
233 struct usb_functionfs_strings_head header;
234 struct {
235 __le16 code;
236 const char str1[sizeof(STR_INTERFACE_)];
237 } __attribute__((packed)) lang0;
238} __attribute__((packed)) strings = {
239 .header = {
240 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
241 .length = cpu_to_le32(sizeof(strings)),
242 .str_count = cpu_to_le32(1),
243 .lang_count = cpu_to_le32(1),
244 },
245 .lang0 = {
246 cpu_to_le16(0x0409), /* en-us */
247 STR_INTERFACE_,
248 },
249};
250
Josh Gao382ea6e2016-02-12 14:31:15 -0800251static void usb_adb_open_thread(void* x) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252 struct usb_handle *usb = (struct usb_handle *)x;
253 int fd;
254
Siva Velusamy2669cf92015-08-28 16:37:29 -0700255 adb_thread_setname("usb open");
256
Elliott Hughes7cf35752015-04-17 17:03:59 -0700257 while (true) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 // wait until the USB device needs opening
Josh Gaoe7daf572016-09-21 12:37:10 -0700259 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui6760b2a2016-04-05 14:51:52 -0700260 while (!usb->open_new_connection) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700261 usb->notify.wait(lock);
Yabin Cui6760b2a2016-04-05 14:51:52 -0700262 }
263 usb->open_new_connection = false;
Josh Gaoe7daf572016-09-21 12:37:10 -0700264 lock.unlock();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265
Yabin Cui815ad882015-09-02 17:44:28 -0700266 D("[ usb_thread - opening device ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800267 do {
268 /* XXX use inotify? */
269 fd = unix_open("/dev/android_adb", O_RDWR);
270 if (fd < 0) {
271 // to support older kernels
272 fd = unix_open("/dev/android", O_RDWR);
273 }
274 if (fd < 0) {
Elliott Hughes73925982016-11-15 12:37:32 -0800275 std::this_thread::sleep_for(1s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 }
277 } while (fd < 0);
Yabin Cui815ad882015-09-02 17:44:28 -0700278 D("[ opening device succeeded ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279
280 close_on_exec(fd);
281 usb->fd = fd;
282
Yabin Cui815ad882015-09-02 17:44:28 -0700283 D("[ usb_thread - registering device ]");
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700284 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800285 }
286
287 // never gets here
Josh Gao382ea6e2016-02-12 14:31:15 -0800288 abort();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800289}
290
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100291static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800292{
293 int n;
294
Yabin Cui815ad882015-09-02 17:44:28 -0700295 D("about to write (fd=%d, len=%d)", h->fd, len);
Spencer Low3a2421b2015-05-22 20:09:06 -0700296 n = unix_write(h->fd, data, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297 if(n != len) {
Yabin Cui815ad882015-09-02 17:44:28 -0700298 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700299 h->fd, n, errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300 return -1;
301 }
Yabin Cui6760b2a2016-04-05 14:51:52 -0700302 if (h->kicked) {
303 D("usb_adb_write finished due to kicked");
304 return -1;
305 }
Yabin Cui815ad882015-09-02 17:44:28 -0700306 D("[ done fd=%d ]", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800307 return 0;
308}
309
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100310static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311{
Yabin Cui815ad882015-09-02 17:44:28 -0700312 D("about to read (fd=%d, len=%d)", h->fd, len);
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100313 while (len > 0) {
314 // The kernel implementation of adb_read in f_adb.c doesn't support
315 // reads larger then 4096 bytes. Read the data in 4096 byte chunks to
316 // avoid the issue. (The ffs implementation doesn't have this limit.)
317 int bytes_to_read = len < 4096 ? len : 4096;
318 int n = unix_read(h->fd, data, bytes_to_read);
319 if (n != bytes_to_read) {
Yabin Cui815ad882015-09-02 17:44:28 -0700320 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100321 h->fd, n, errno, strerror(errno));
322 return -1;
323 }
Yabin Cui6760b2a2016-04-05 14:51:52 -0700324 if (h->kicked) {
325 D("usb_adb_read finished due to kicked");
326 return -1;
327 }
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100328 len -= n;
329 data = ((char*)data) + n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330 }
Yabin Cui815ad882015-09-02 17:44:28 -0700331 D("[ done fd=%d ]", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800332 return 0;
333}
334
Yabin Cui6760b2a2016-04-05 14:51:52 -0700335static void usb_adb_kick(usb_handle *h) {
Yabin Cui815ad882015-09-02 17:44:28 -0700336 D("usb_kick");
Yabin Cui6760b2a2016-04-05 14:51:52 -0700337 // Other threads may be calling usb_adb_read/usb_adb_write at the same time.
338 // If we close h->fd, the file descriptor will be reused to open other files,
339 // and the read/write thread may operate on the wrong file. So instead
340 // we set the kicked flag and reopen h->fd to a dummy file here. After read/write
341 // threads finish, we close h->fd in usb_adb_close().
342 h->kicked = true;
343 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->fd));
344}
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100345
Yabin Cui6760b2a2016-04-05 14:51:52 -0700346static void usb_adb_close(usb_handle *h) {
347 h->kicked = false;
348 adb_close(h->fd);
349 // Notify usb_adb_open_thread to open a new connection.
Josh Gaoe7daf572016-09-21 12:37:10 -0700350 h->lock.lock();
Yabin Cui6760b2a2016-04-05 14:51:52 -0700351 h->open_new_connection = true;
Josh Gaoe7daf572016-09-21 12:37:10 -0700352 h->lock.unlock();
353 h->notify.notify_one();
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100354}
355
356static void usb_adb_init()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800357{
Josh Gaoe7daf572016-09-21 12:37:10 -0700358 usb_handle* h = new usb_handle();
Elliott Hughesd0269c92015-04-21 19:39:52 -0700359
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100360 h->write = usb_adb_write;
361 h->read = usb_adb_read;
362 h->kick = usb_adb_kick;
Yabin Cui6760b2a2016-04-05 14:51:52 -0700363 h->close = usb_adb_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800364
Elliott Hughes392692c2015-04-16 14:12:50 -0700365 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800366 // the enabling of the adb USB function in the kernel.
367 // We never touch this file again - just leave it open
368 // indefinitely so the kernel will know when we are running
369 // and when we are not.
Elliott Hughes392692c2015-04-16 14:12:50 -0700370 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371 if (fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700372 D("failed to open /dev/android_adb_enable");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800373 } else {
374 close_on_exec(fd);
375 }
376
Yabin Cui815ad882015-09-02 17:44:28 -0700377 D("[ usb_init - starting thread ]");
Elliott Hughesf2517142015-05-05 13:41:21 -0700378 if (!adb_thread_create(usb_adb_open_thread, h)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 fatal_errno("cannot create usb thread");
380 }
381}
382
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383
Yabin Cui6760b2a2016-04-05 14:51:52 -0700384static bool init_functionfs(struct usb_handle *h)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100385{
386 ssize_t ret;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700387 struct desc_v1 v1_descriptor;
388 struct desc_v2 v2_descriptor;
389
390 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
391 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phamb64ab092015-06-02 10:36:43 -0700392 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700393 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisf7555b12015-01-23 17:09:56 -0800394 v2_descriptor.fs_count = 3;
395 v2_descriptor.hs_count = 3;
Jack Phamb64ab092015-06-02 10:36:43 -0700396 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700397 v2_descriptor.os_count = 1;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700398 v2_descriptor.fs_descs = fs_descriptors;
399 v2_descriptor.hs_descs = hs_descriptors;
Jack Phamb64ab092015-06-02 10:36:43 -0700400 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700401 v2_descriptor.os_header = os_desc_header;
402 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100403
Siqi Lin686558e2016-05-26 13:04:52 -0700404 if (h->control < 0) { // might have already done this before
405 D("OPENING %s", USB_FFS_ADB_EP0);
406 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
407 if (h->control < 0) {
408 D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham6c3cef52013-12-23 17:46:10 -0800409 goto err;
410 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100411
Siqi Lin686558e2016-05-26 13:04:52 -0700412 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
413 if (ret < 0) {
414 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
415 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
416 v1_descriptor.header.fs_count = 3;
417 v1_descriptor.header.hs_count = 3;
418 v1_descriptor.fs_descs = fs_descriptors;
419 v1_descriptor.hs_descs = hs_descriptors;
420 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
421 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
422 if (ret < 0) {
423 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
424 goto err;
425 }
426 }
427
428 ret = adb_write(h->control, &strings, sizeof(strings));
429 if (ret < 0) {
430 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
431 goto err;
432 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100433 }
434
435 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
436 if (h->bulk_out < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700437 D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100438 goto err;
439 }
440
441 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
442 if (h->bulk_in < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700443 D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100444 goto err;
445 }
446
Yabin Cui6760b2a2016-04-05 14:51:52 -0700447 return true;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100448
449err:
450 if (h->bulk_in > 0) {
451 adb_close(h->bulk_in);
452 h->bulk_in = -1;
453 }
454 if (h->bulk_out > 0) {
455 adb_close(h->bulk_out);
456 h->bulk_out = -1;
457 }
458 if (h->control > 0) {
459 adb_close(h->control);
460 h->control = -1;
461 }
Yabin Cui6760b2a2016-04-05 14:51:52 -0700462 return false;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100463}
464
Josh Gao382ea6e2016-02-12 14:31:15 -0800465static void usb_ffs_open_thread(void* x) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100466 struct usb_handle *usb = (struct usb_handle *)x;
467
Siva Velusamy2669cf92015-08-28 16:37:29 -0700468 adb_thread_setname("usb ffs open");
469
Elliott Hughes7cf35752015-04-17 17:03:59 -0700470 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100471 // wait until the USB device needs opening
Josh Gaoe7daf572016-09-21 12:37:10 -0700472 std::unique_lock<std::mutex> lock(usb->lock);
Yabin Cui6760b2a2016-04-05 14:51:52 -0700473 while (!usb->open_new_connection) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700474 usb->notify.wait(lock);
Yabin Cui6760b2a2016-04-05 14:51:52 -0700475 }
476 usb->open_new_connection = false;
Josh Gaoe7daf572016-09-21 12:37:10 -0700477 lock.unlock();
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100478
Elliott Hughes7cf35752015-04-17 17:03:59 -0700479 while (true) {
Yabin Cui6760b2a2016-04-05 14:51:52 -0700480 if (init_functionfs(usb)) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100481 break;
Yabin Cui6760b2a2016-04-05 14:51:52 -0700482 }
Elliott Hughes73925982016-11-15 12:37:32 -0800483 std::this_thread::sleep_for(1s);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100484 }
Elliott Hughes8b249d22016-09-23 15:40:03 -0700485 android::base::SetProperty("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100486
Yabin Cui815ad882015-09-02 17:44:28 -0700487 D("[ usb_thread - registering device ]");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100488 register_usb_transport(usb, 0, 0, 1);
489 }
490
491 // never gets here
Josh Gao382ea6e2016-02-12 14:31:15 -0800492 abort();
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100493}
494
Josh Gao8e701252015-12-02 17:30:58 -0800495static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui815ad882015-09-02 17:44:28 -0700496 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao8e701252015-12-02 17:30:58 -0800497
Josh Gao8e701252015-12-02 17:30:58 -0800498 const char* buf = static_cast<const char*>(data);
499 while (len > 0) {
Josh Gaoc95bb3d2015-12-17 13:45:18 -0800500 int write_len = std::min(USB_FFS_MAX_WRITE, len);
Josh Gao8e701252015-12-02 17:30:58 -0800501 int n = adb_write(h->bulk_in, buf, write_len);
502 if (n < 0) {
503 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
504 return -1;
505 }
506 buf += n;
507 len -= n;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100508 }
Josh Gao8e701252015-12-02 17:30:58 -0800509
Yabin Cui815ad882015-09-02 17:44:28 -0700510 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100511 return 0;
512}
513
Josh Gao8e701252015-12-02 17:30:58 -0800514static int usb_ffs_read(usb_handle* h, void* data, int len) {
515 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100516
Josh Gao8e701252015-12-02 17:30:58 -0800517 char* buf = static_cast<char*>(data);
518 while (len > 0) {
Josh Gaoc95bb3d2015-12-17 13:45:18 -0800519 int read_len = std::min(USB_FFS_MAX_READ, len);
Josh Gao83e329e2015-12-16 11:00:08 -0800520 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao8e701252015-12-02 17:30:58 -0800521 if (n < 0) {
522 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes0bd85872015-08-25 10:59:45 -0700523 return -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100524 }
Josh Gao8e701252015-12-02 17:30:58 -0800525 buf += n;
526 len -= n;
Elliott Hughes0bd85872015-08-25 10:59:45 -0700527 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100528
Yabin Cui815ad882015-09-02 17:44:28 -0700529 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100530 return 0;
531}
532
533static void usb_ffs_kick(usb_handle *h)
534{
535 int err;
536
537 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700538 if (err < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700539 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700540 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100541
542 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700543 if (err < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700544 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700545 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100546
Jack Pham6c3cef52013-12-23 17:46:10 -0800547 // don't close ep0 here, since we may not need to reinitialize it with
548 // the same descriptors again. if however ep1/ep2 fail to re-open in
549 // init_functionfs, only then would we close and open ep0 again.
Yabin Cui6760b2a2016-04-05 14:51:52 -0700550 // Ditto the comment in usb_adb_kick.
551 h->kicked = true;
552 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_out));
553 TEMP_FAILURE_RETRY(dup2(dummy_fd, h->bulk_in));
554}
555
556static void usb_ffs_close(usb_handle *h) {
557 h->kicked = false;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100558 adb_close(h->bulk_out);
559 adb_close(h->bulk_in);
Yabin Cui6760b2a2016-04-05 14:51:52 -0700560 // Notify usb_adb_open_thread to open a new connection.
Josh Gaoe7daf572016-09-21 12:37:10 -0700561 h->lock.lock();
Yabin Cui6760b2a2016-04-05 14:51:52 -0700562 h->open_new_connection = true;
Josh Gaoe7daf572016-09-21 12:37:10 -0700563 h->lock.unlock();
564 h->notify.notify_one();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800565}
566
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100567static void usb_ffs_init()
568{
Yabin Cui815ad882015-09-02 17:44:28 -0700569 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100570
Josh Gaoe7daf572016-09-21 12:37:10 -0700571 usb_handle* h = new usb_handle();
Elliott Hughesd0269c92015-04-21 19:39:52 -0700572
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100573 h->write = usb_ffs_write;
574 h->read = usb_ffs_read;
575 h->kick = usb_ffs_kick;
Yabin Cui6760b2a2016-04-05 14:51:52 -0700576 h->close = usb_ffs_close;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100577
Yabin Cui815ad882015-09-02 17:44:28 -0700578 D("[ usb_init - starting thread ]");
Elliott Hughesf2517142015-05-05 13:41:21 -0700579 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100580 fatal_errno("[ cannot create usb thread ]\n");
581 }
582}
583
584void usb_init()
585{
Yabin Cui6760b2a2016-04-05 14:51:52 -0700586 dummy_fd = adb_open("/dev/null", O_WRONLY);
587 CHECK_NE(dummy_fd, -1);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100588 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
589 usb_ffs_init();
590 else
591 usb_adb_init();
592}
593
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100594int usb_write(usb_handle *h, const void *data, int len)
595{
596 return h->write(h, data, len);
597}
598
599int usb_read(usb_handle *h, void *data, int len)
600{
601 return h->read(h, data, len);
602}
Josh Gaoe7daf572016-09-21 12:37:10 -0700603
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800604int usb_close(usb_handle *h)
605{
Yabin Cui6760b2a2016-04-05 14:51:52 -0700606 h->close(h);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800607 return 0;
608}
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100609
610void usb_kick(usb_handle *h)
611{
612 h->kick(h);
613}