blob: a4f1a70544e8d0d5773e0d3f01d5066f253c7e5c [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>
34
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080036#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010038#define MAX_PACKET_SIZE_FS 64
39#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +080040#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010041
Josh Gaoae72b5a2015-12-17 13:45:18 -080042// Writes larger than 16k fail on some devices (seed with 3.10.49-g209ea2f in particular).
43#define USB_FFS_MAX_WRITE 16384
44
45// The kernel allocates a contiguous buffer for reads, which can fail for large ones due to
46// fragmentation. 16k chosen arbitrarily to match the write limit.
47#define USB_FFS_MAX_READ 16384
48
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010049#define cpu_to_le16(x) htole16(x)
50#define cpu_to_le32(x) htole32(x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52struct usb_handle
53{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054 adb_cond_t notify;
55 adb_mutex_t lock;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010056
57 int (*write)(usb_handle *h, const void *data, int len);
58 int (*read)(usb_handle *h, void *data, int len);
59 void (*kick)(usb_handle *h);
60
61 // Legacy f_adb
62 int fd;
63
64 // FunctionFS
65 int control;
66 int bulk_out; /* "out" from the host's perspective => source for adbd */
67 int bulk_in; /* "in" from the host's perspective => sink for adbd */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068};
69
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070070struct func_desc {
71 struct usb_interface_descriptor intf;
72 struct usb_endpoint_descriptor_no_audio source;
73 struct usb_endpoint_descriptor_no_audio sink;
74} __attribute__((packed));
75
Jack Phama190c802015-06-02 10:36:43 -070076struct ss_func_desc {
77 struct usb_interface_descriptor intf;
78 struct usb_endpoint_descriptor_no_audio source;
79 struct usb_ss_ep_comp_descriptor source_comp;
80 struct usb_endpoint_descriptor_no_audio sink;
81 struct usb_ss_ep_comp_descriptor sink_comp;
82} __attribute__((packed));
83
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070084struct desc_v1 {
85 struct usb_functionfs_descs_head_v1 {
86 __le32 magic;
87 __le32 length;
88 __le32 fs_count;
89 __le32 hs_count;
90 } __attribute__((packed)) header;
91 struct func_desc fs_descs, hs_descs;
92} __attribute__((packed));
93
94struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080095 struct usb_functionfs_descs_head_v2 header;
96 // The rest of the structure depends on the flags in the header.
97 __le32 fs_count;
98 __le32 hs_count;
Jack Phama190c802015-06-02 10:36:43 -070099 __le32 ss_count;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700100 __le32 os_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700101 struct func_desc fs_descs, hs_descs;
Jack Phama190c802015-06-02 10:36:43 -0700102 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700103 struct usb_os_desc_header os_header;
104 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700105} __attribute__((packed));
106
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700107static struct func_desc fs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700108 .intf = {
109 .bLength = sizeof(fs_descriptors.intf),
110 .bDescriptorType = USB_DT_INTERFACE,
111 .bInterfaceNumber = 0,
112 .bNumEndpoints = 2,
113 .bInterfaceClass = ADB_CLASS,
114 .bInterfaceSubClass = ADB_SUBCLASS,
115 .bInterfaceProtocol = ADB_PROTOCOL,
116 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100117 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700118 .source = {
119 .bLength = sizeof(fs_descriptors.source),
120 .bDescriptorType = USB_DT_ENDPOINT,
121 .bEndpointAddress = 1 | USB_DIR_OUT,
122 .bmAttributes = USB_ENDPOINT_XFER_BULK,
123 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100124 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700125 .sink = {
126 .bLength = sizeof(fs_descriptors.sink),
127 .bDescriptorType = USB_DT_ENDPOINT,
128 .bEndpointAddress = 2 | USB_DIR_IN,
129 .bmAttributes = USB_ENDPOINT_XFER_BULK,
130 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
131 },
132};
133
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700134static struct func_desc hs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700135 .intf = {
136 .bLength = sizeof(hs_descriptors.intf),
137 .bDescriptorType = USB_DT_INTERFACE,
138 .bInterfaceNumber = 0,
139 .bNumEndpoints = 2,
140 .bInterfaceClass = ADB_CLASS,
141 .bInterfaceSubClass = ADB_SUBCLASS,
142 .bInterfaceProtocol = ADB_PROTOCOL,
143 .iInterface = 1, /* first string from the provided table */
144 },
145 .source = {
146 .bLength = sizeof(hs_descriptors.source),
147 .bDescriptorType = USB_DT_ENDPOINT,
148 .bEndpointAddress = 1 | USB_DIR_OUT,
149 .bmAttributes = USB_ENDPOINT_XFER_BULK,
150 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
151 },
152 .sink = {
153 .bLength = sizeof(hs_descriptors.sink),
154 .bDescriptorType = USB_DT_ENDPOINT,
155 .bEndpointAddress = 2 | USB_DIR_IN,
156 .bmAttributes = USB_ENDPOINT_XFER_BULK,
157 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800158 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100159};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
Jack Phama190c802015-06-02 10:36:43 -0700161static struct ss_func_desc ss_descriptors = {
162 .intf = {
163 .bLength = sizeof(ss_descriptors.intf),
164 .bDescriptorType = USB_DT_INTERFACE,
165 .bInterfaceNumber = 0,
166 .bNumEndpoints = 2,
167 .bInterfaceClass = ADB_CLASS,
168 .bInterfaceSubClass = ADB_SUBCLASS,
169 .bInterfaceProtocol = ADB_PROTOCOL,
170 .iInterface = 1, /* first string from the provided table */
171 },
172 .source = {
173 .bLength = sizeof(ss_descriptors.source),
174 .bDescriptorType = USB_DT_ENDPOINT,
175 .bEndpointAddress = 1 | USB_DIR_OUT,
176 .bmAttributes = USB_ENDPOINT_XFER_BULK,
177 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
178 },
179 .source_comp = {
180 .bLength = sizeof(ss_descriptors.source_comp),
181 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
182 },
183 .sink = {
184 .bLength = sizeof(ss_descriptors.sink),
185 .bDescriptorType = USB_DT_ENDPOINT,
186 .bEndpointAddress = 2 | USB_DIR_IN,
187 .bmAttributes = USB_ENDPOINT_XFER_BULK,
188 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
189 },
190 .sink_comp = {
191 .bLength = sizeof(ss_descriptors.sink_comp),
192 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
193 },
194};
195
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700196struct usb_ext_compat_desc os_desc_compat = {
197 .bFirstInterfaceNumber = 0,
198 .Reserved1 = cpu_to_le32(1),
199 .CompatibleID = {0},
200 .SubCompatibleID = {0},
201 .Reserved2 = {0},
202};
203
204static struct usb_os_desc_header os_desc_header = {
205 .interface = cpu_to_le32(1),
206 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
207 .bcdVersion = cpu_to_le32(1),
208 .wIndex = cpu_to_le32(4),
209 .bCount = cpu_to_le32(1),
210 .Reserved = cpu_to_le32(0),
211};
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
235
236
237static void *usb_adb_open_thread(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238{
239 struct usb_handle *usb = (struct usb_handle *)x;
240 int fd;
241
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700242 adb_thread_setname("usb open");
243
Elliott Hughesa7090b92015-04-17 17:03:59 -0700244 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 // wait until the USB device needs opening
246 adb_mutex_lock(&usb->lock);
247 while (usb->fd != -1)
248 adb_cond_wait(&usb->notify, &usb->lock);
249 adb_mutex_unlock(&usb->lock);
250
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700251 D("[ usb_thread - opening device ]");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 do {
253 /* XXX use inotify? */
254 fd = unix_open("/dev/android_adb", O_RDWR);
255 if (fd < 0) {
256 // to support older kernels
257 fd = unix_open("/dev/android", O_RDWR);
258 }
259 if (fd < 0) {
260 adb_sleep_ms(1000);
261 }
262 } while (fd < 0);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700263 D("[ opening device succeeded ]");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264
265 close_on_exec(fd);
266 usb->fd = fd;
267
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700268 D("[ usb_thread - registering device ]");
Scott Andersone109d262012-04-20 11:21:14 -0700269 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 }
271
272 // never gets here
273 return 0;
274}
275
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100276static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277{
278 int n;
279
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700280 D("about to write (fd=%d, len=%d)", h->fd, len);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700281 n = unix_write(h->fd, data, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 if(n != len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700283 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
JP Abgrall408fa572011-03-16 15:57:42 -0700284 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 return -1;
286 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700287 D("[ done fd=%d ]", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 return 0;
289}
290
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100291static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700293 D("about to read (fd=%d, len=%d)", h->fd, len);
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100294 while (len > 0) {
295 // The kernel implementation of adb_read in f_adb.c doesn't support
296 // reads larger then 4096 bytes. Read the data in 4096 byte chunks to
297 // avoid the issue. (The ffs implementation doesn't have this limit.)
298 int bytes_to_read = len < 4096 ? len : 4096;
299 int n = unix_read(h->fd, data, bytes_to_read);
300 if (n != bytes_to_read) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700301 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100302 h->fd, n, errno, strerror(errno));
303 return -1;
304 }
305 len -= n;
306 data = ((char*)data) + n;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700308 D("[ done fd=%d ]", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 return 0;
310}
311
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100312static void usb_adb_kick(usb_handle *h)
313{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700314 D("usb_kick");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100315 adb_mutex_lock(&h->lock);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700316 unix_close(h->fd);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100317 h->fd = -1;
318
319 // notify usb_adb_open_thread that we are disconnected
320 adb_cond_signal(&h->notify);
321 adb_mutex_unlock(&h->lock);
322}
323
324static void usb_adb_init()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325{
Elliott Hughes2acec912015-04-16 14:12:50 -0700326 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700327 if (h == nullptr) fatal("couldn't allocate usb_handle");
328
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100329 h->write = usb_adb_write;
330 h->read = usb_adb_read;
331 h->kick = usb_adb_kick;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 h->fd = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 adb_cond_init(&h->notify, 0);
335 adb_mutex_init(&h->lock, 0);
336
Elliott Hughes2acec912015-04-16 14:12:50 -0700337 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 // the enabling of the adb USB function in the kernel.
339 // We never touch this file again - just leave it open
340 // indefinitely so the kernel will know when we are running
341 // and when we are not.
Elliott Hughes2acec912015-04-16 14:12:50 -0700342 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 if (fd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700344 D("failed to open /dev/android_adb_enable");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 } else {
346 close_on_exec(fd);
347 }
348
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700349 D("[ usb_init - starting thread ]");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700350 if (!adb_thread_create(usb_adb_open_thread, h)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 fatal_errno("cannot create usb thread");
352 }
353}
354
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100356static void init_functionfs(struct usb_handle *h)
357{
358 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700359 struct desc_v1 v1_descriptor;
360 struct desc_v2 v2_descriptor;
361
362 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
363 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phama190c802015-06-02 10:36:43 -0700364 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700365 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800366 v2_descriptor.fs_count = 3;
367 v2_descriptor.hs_count = 3;
Jack Phama190c802015-06-02 10:36:43 -0700368 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700369 v2_descriptor.os_count = 1;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700370 v2_descriptor.fs_descs = fs_descriptors;
371 v2_descriptor.hs_descs = hs_descriptors;
Jack Phama190c802015-06-02 10:36:43 -0700372 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanca2a0bd2015-10-05 13:04:03 -0700373 v2_descriptor.os_header = os_desc_header;
374 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100375
Jack Pham4cbf1d82013-12-23 17:46:10 -0800376 if (h->control < 0) { // might have already done this before
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700377 D("OPENING %s", USB_FFS_ADB_EP0);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800378 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
379 if (h->control < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700380 D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800381 goto err;
382 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100383
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700384 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham4cbf1d82013-12-23 17:46:10 -0800385 if (ret < 0) {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700386 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
387 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
388 v1_descriptor.header.fs_count = 3;
389 v1_descriptor.header.hs_count = 3;
390 v1_descriptor.fs_descs = fs_descriptors;
391 v1_descriptor.hs_descs = hs_descriptors;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700392 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700393 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
394 if (ret < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700395 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700396 goto err;
397 }
Jack Pham4cbf1d82013-12-23 17:46:10 -0800398 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100399
Jack Pham4cbf1d82013-12-23 17:46:10 -0800400 ret = adb_write(h->control, &strings, sizeof(strings));
401 if (ret < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700402 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800403 goto err;
404 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100405 }
406
407 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
408 if (h->bulk_out < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700409 D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100410 goto err;
411 }
412
413 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
414 if (h->bulk_in < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700415 D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100416 goto err;
417 }
418
419 return;
420
421err:
422 if (h->bulk_in > 0) {
423 adb_close(h->bulk_in);
424 h->bulk_in = -1;
425 }
426 if (h->bulk_out > 0) {
427 adb_close(h->bulk_out);
428 h->bulk_out = -1;
429 }
430 if (h->control > 0) {
431 adb_close(h->control);
432 h->control = -1;
433 }
434 return;
435}
436
437static void *usb_ffs_open_thread(void *x)
438{
439 struct usb_handle *usb = (struct usb_handle *)x;
440
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700441 adb_thread_setname("usb ffs open");
442
Elliott Hughesa7090b92015-04-17 17:03:59 -0700443 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100444 // wait until the USB device needs opening
445 adb_mutex_lock(&usb->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800446 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100447 adb_cond_wait(&usb->notify, &usb->lock);
448 adb_mutex_unlock(&usb->lock);
449
Elliott Hughesa7090b92015-04-17 17:03:59 -0700450 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100451 init_functionfs(usb);
452
Jack Pham4cbf1d82013-12-23 17:46:10 -0800453 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100454 break;
455
456 adb_sleep_ms(1000);
457 }
Badhri Jagan Sridharan5f973702015-04-21 11:09:32 -0700458 property_set("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100459
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700460 D("[ usb_thread - registering device ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100461 register_usb_transport(usb, 0, 0, 1);
462 }
463
464 // never gets here
465 return 0;
466}
467
Josh Gao6b531c42015-12-02 17:30:58 -0800468static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700469 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800470
Josh Gao6b531c42015-12-02 17:30:58 -0800471 const char* buf = static_cast<const char*>(data);
472 while (len > 0) {
Josh Gaoae72b5a2015-12-17 13:45:18 -0800473 int write_len = std::min(USB_FFS_MAX_WRITE, len);
Josh Gao6b531c42015-12-02 17:30:58 -0800474 int n = adb_write(h->bulk_in, buf, write_len);
475 if (n < 0) {
476 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
477 return -1;
478 }
479 buf += n;
480 len -= n;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100481 }
Josh Gao6b531c42015-12-02 17:30:58 -0800482
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700483 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100484 return 0;
485}
486
Josh Gao6b531c42015-12-02 17:30:58 -0800487static int usb_ffs_read(usb_handle* h, void* data, int len) {
488 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100489
Josh Gao6b531c42015-12-02 17:30:58 -0800490 char* buf = static_cast<char*>(data);
491 while (len > 0) {
Josh Gaoae72b5a2015-12-17 13:45:18 -0800492 int read_len = std::min(USB_FFS_MAX_READ, len);
Josh Gao0b195402015-12-16 11:00:08 -0800493 int n = adb_read(h->bulk_out, buf, read_len);
Josh Gao6b531c42015-12-02 17:30:58 -0800494 if (n < 0) {
495 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700496 return -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100497 }
Josh Gao6b531c42015-12-02 17:30:58 -0800498 buf += n;
499 len -= n;
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700500 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100501
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700502 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100503 return 0;
504}
505
506static void usb_ffs_kick(usb_handle *h)
507{
508 int err;
509
510 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700511 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700512 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700513 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100514
515 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700516 if (err < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700517 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700518 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100519
520 adb_mutex_lock(&h->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800521
522 // don't close ep0 here, since we may not need to reinitialize it with
523 // the same descriptors again. if however ep1/ep2 fail to re-open in
524 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100525 adb_close(h->bulk_out);
526 adb_close(h->bulk_in);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800527 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100528
529 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 adb_cond_signal(&h->notify);
531 adb_mutex_unlock(&h->lock);
532}
533
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100534static void usb_ffs_init()
535{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700536 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100537
Elliott Hughes2acec912015-04-16 14:12:50 -0700538 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700539 if (h == nullptr) fatal("couldn't allocate usb_handle");
540
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100541 h->write = usb_ffs_write;
542 h->read = usb_ffs_read;
543 h->kick = usb_ffs_kick;
Elliott Hughes2acec912015-04-16 14:12:50 -0700544 h->control = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100545 h->bulk_out = -1;
546 h->bulk_out = -1;
547
548 adb_cond_init(&h->notify, 0);
549 adb_mutex_init(&h->lock, 0);
550
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700551 D("[ usb_init - starting thread ]");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700552 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100553 fatal_errno("[ cannot create usb thread ]\n");
554 }
555}
556
557void usb_init()
558{
559 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
560 usb_ffs_init();
561 else
562 usb_adb_init();
563}
564
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100565int usb_write(usb_handle *h, const void *data, int len)
566{
567 return h->write(h, data, len);
568}
569
570int usb_read(usb_handle *h, void *data, int len)
571{
572 return h->read(h, data, len);
573}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574int usb_close(usb_handle *h)
575{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576 return 0;
577}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100578
579void usb_kick(usb_handle *h)
580{
581 h->kick(h);
582}