blob: dc44f16fec45d7b32a83cc878d69e75acaaa88c9 [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
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_USB
18
19#include "sysdeps.h"
20
Badhri Jagan Sridharane1febe92015-04-21 11:09:32 -070021#include <cutils/properties.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022#include <dirent.h>
23#include <errno.h>
Dan Albertb302d122015-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 Project9ca14dc2009-03-03 19:32:55 -080032
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080033#include "adb.h"
Dan Albertb302d122015-02-24 15:51:19 -080034#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010036#define MAX_PACKET_SIZE_FS 64
37#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Can1fc58c52014-09-02 13:04:44 +080038#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010039
40#define cpu_to_le16(x) htole16(x)
41#define cpu_to_le32(x) htole32(x)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042
43struct usb_handle
44{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045 adb_cond_t notify;
46 adb_mutex_t lock;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010047
48 int (*write)(usb_handle *h, const void *data, int len);
49 int (*read)(usb_handle *h, void *data, int len);
50 void (*kick)(usb_handle *h);
51
52 // Legacy f_adb
53 int fd;
54
55 // FunctionFS
56 int control;
57 int bulk_out; /* "out" from the host's perspective => source for adbd */
58 int bulk_in; /* "in" from the host's perspective => sink for adbd */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059};
60
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070061struct func_desc {
62 struct usb_interface_descriptor intf;
63 struct usb_endpoint_descriptor_no_audio source;
64 struct usb_endpoint_descriptor_no_audio sink;
65} __attribute__((packed));
66
Jack Phamb64ab092015-06-02 10:36:43 -070067struct ss_func_desc {
68 struct usb_interface_descriptor intf;
69 struct usb_endpoint_descriptor_no_audio source;
70 struct usb_ss_ep_comp_descriptor source_comp;
71 struct usb_endpoint_descriptor_no_audio sink;
72 struct usb_ss_ep_comp_descriptor sink_comp;
73} __attribute__((packed));
74
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070075struct desc_v1 {
76 struct usb_functionfs_descs_head_v1 {
77 __le32 magic;
78 __le32 length;
79 __le32 fs_count;
80 __le32 hs_count;
81 } __attribute__((packed)) header;
82 struct func_desc fs_descs, hs_descs;
83} __attribute__((packed));
84
85struct desc_v2 {
Christopher Ferrisf7555b12015-01-23 17:09:56 -080086 struct usb_functionfs_descs_head_v2 header;
87 // The rest of the structure depends on the flags in the header.
88 __le32 fs_count;
89 __le32 hs_count;
Jack Phamb64ab092015-06-02 10:36:43 -070090 __le32 ss_count;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070091 struct func_desc fs_descs, hs_descs;
Jack Phamb64ab092015-06-02 10:36:43 -070092 struct ss_func_desc ss_descs;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070093} __attribute__((packed));
94
Elliott Hughes712416a2015-05-05 18:26:10 -070095static struct func_desc fs_descriptors = {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070096 .intf = {
97 .bLength = sizeof(fs_descriptors.intf),
98 .bDescriptorType = USB_DT_INTERFACE,
99 .bInterfaceNumber = 0,
100 .bNumEndpoints = 2,
101 .bInterfaceClass = ADB_CLASS,
102 .bInterfaceSubClass = ADB_SUBCLASS,
103 .bInterfaceProtocol = ADB_PROTOCOL,
104 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100105 },
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700106 .source = {
107 .bLength = sizeof(fs_descriptors.source),
108 .bDescriptorType = USB_DT_ENDPOINT,
109 .bEndpointAddress = 1 | USB_DIR_OUT,
110 .bmAttributes = USB_ENDPOINT_XFER_BULK,
111 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100112 },
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700113 .sink = {
114 .bLength = sizeof(fs_descriptors.sink),
115 .bDescriptorType = USB_DT_ENDPOINT,
116 .bEndpointAddress = 2 | USB_DIR_IN,
117 .bmAttributes = USB_ENDPOINT_XFER_BULK,
118 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
119 },
120};
121
Elliott Hughes712416a2015-05-05 18:26:10 -0700122static struct func_desc hs_descriptors = {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700123 .intf = {
124 .bLength = sizeof(hs_descriptors.intf),
125 .bDescriptorType = USB_DT_INTERFACE,
126 .bInterfaceNumber = 0,
127 .bNumEndpoints = 2,
128 .bInterfaceClass = ADB_CLASS,
129 .bInterfaceSubClass = ADB_SUBCLASS,
130 .bInterfaceProtocol = ADB_PROTOCOL,
131 .iInterface = 1, /* first string from the provided table */
132 },
133 .source = {
134 .bLength = sizeof(hs_descriptors.source),
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = 1 | USB_DIR_OUT,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
139 },
140 .sink = {
141 .bLength = sizeof(hs_descriptors.sink),
142 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = 2 | USB_DIR_IN,
144 .bmAttributes = USB_ENDPOINT_XFER_BULK,
145 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Can1fc58c52014-09-02 13:04:44 +0800146 },
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100147};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800148
Jack Phamb64ab092015-06-02 10:36:43 -0700149static struct ss_func_desc ss_descriptors = {
150 .intf = {
151 .bLength = sizeof(ss_descriptors.intf),
152 .bDescriptorType = USB_DT_INTERFACE,
153 .bInterfaceNumber = 0,
154 .bNumEndpoints = 2,
155 .bInterfaceClass = ADB_CLASS,
156 .bInterfaceSubClass = ADB_SUBCLASS,
157 .bInterfaceProtocol = ADB_PROTOCOL,
158 .iInterface = 1, /* first string from the provided table */
159 },
160 .source = {
161 .bLength = sizeof(ss_descriptors.source),
162 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = 1 | USB_DIR_OUT,
164 .bmAttributes = USB_ENDPOINT_XFER_BULK,
165 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
166 },
167 .source_comp = {
168 .bLength = sizeof(ss_descriptors.source_comp),
169 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
170 },
171 .sink = {
172 .bLength = sizeof(ss_descriptors.sink),
173 .bDescriptorType = USB_DT_ENDPOINT,
174 .bEndpointAddress = 2 | USB_DIR_IN,
175 .bmAttributes = USB_ENDPOINT_XFER_BULK,
176 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
177 },
178 .sink_comp = {
179 .bLength = sizeof(ss_descriptors.sink_comp),
180 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
181 },
182};
183
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100184#define STR_INTERFACE_ "ADB Interface"
185
186static const struct {
187 struct usb_functionfs_strings_head header;
188 struct {
189 __le16 code;
190 const char str1[sizeof(STR_INTERFACE_)];
191 } __attribute__((packed)) lang0;
192} __attribute__((packed)) strings = {
193 .header = {
194 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
195 .length = cpu_to_le32(sizeof(strings)),
196 .str_count = cpu_to_le32(1),
197 .lang_count = cpu_to_le32(1),
198 },
199 .lang0 = {
200 cpu_to_le16(0x0409), /* en-us */
201 STR_INTERFACE_,
202 },
203};
204
205
206
207static void *usb_adb_open_thread(void *x)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208{
209 struct usb_handle *usb = (struct usb_handle *)x;
210 int fd;
211
Elliott Hughes7cf35752015-04-17 17:03:59 -0700212 while (true) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800213 // wait until the USB device needs opening
214 adb_mutex_lock(&usb->lock);
215 while (usb->fd != -1)
216 adb_cond_wait(&usb->notify, &usb->lock);
217 adb_mutex_unlock(&usb->lock);
218
219 D("[ usb_thread - opening device ]\n");
220 do {
221 /* XXX use inotify? */
222 fd = unix_open("/dev/android_adb", O_RDWR);
223 if (fd < 0) {
224 // to support older kernels
225 fd = unix_open("/dev/android", O_RDWR);
226 }
227 if (fd < 0) {
228 adb_sleep_ms(1000);
229 }
230 } while (fd < 0);
231 D("[ opening device succeeded ]\n");
232
233 close_on_exec(fd);
234 usb->fd = fd;
235
236 D("[ usb_thread - registering device ]\n");
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700237 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800238 }
239
240 // never gets here
241 return 0;
242}
243
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100244static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800245{
246 int n;
247
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700248 D("about to write (fd=%d, len=%d)\n", h->fd, len);
Spencer Low3a2421b2015-05-22 20:09:06 -0700249 n = unix_write(h->fd, data, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250 if(n != len) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700251 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
252 h->fd, n, errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800253 return -1;
254 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700255 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800256 return 0;
257}
258
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100259static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800260{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700261 D("about to read (fd=%d, len=%d)\n", h->fd, len);
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100262 while (len > 0) {
263 // The kernel implementation of adb_read in f_adb.c doesn't support
264 // reads larger then 4096 bytes. Read the data in 4096 byte chunks to
265 // avoid the issue. (The ffs implementation doesn't have this limit.)
266 int bytes_to_read = len < 4096 ? len : 4096;
267 int n = unix_read(h->fd, data, bytes_to_read);
268 if (n != bytes_to_read) {
269 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
270 h->fd, n, errno, strerror(errno));
271 return -1;
272 }
273 len -= n;
274 data = ((char*)data) + n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700276 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277 return 0;
278}
279
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100280static void usb_adb_kick(usb_handle *h)
281{
282 D("usb_kick\n");
283 adb_mutex_lock(&h->lock);
Spencer Low3a2421b2015-05-22 20:09:06 -0700284 unix_close(h->fd);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100285 h->fd = -1;
286
287 // notify usb_adb_open_thread that we are disconnected
288 adb_cond_signal(&h->notify);
289 adb_mutex_unlock(&h->lock);
290}
291
292static void usb_adb_init()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293{
Elliott Hughes392692c2015-04-16 14:12:50 -0700294 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700295 if (h == nullptr) fatal("couldn't allocate usb_handle");
296
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100297 h->write = usb_adb_write;
298 h->read = usb_adb_read;
299 h->kick = usb_adb_kick;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300 h->fd = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100301
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800302 adb_cond_init(&h->notify, 0);
303 adb_mutex_init(&h->lock, 0);
304
Elliott Hughes392692c2015-04-16 14:12:50 -0700305 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306 // the enabling of the adb USB function in the kernel.
307 // We never touch this file again - just leave it open
308 // indefinitely so the kernel will know when we are running
309 // and when we are not.
Elliott Hughes392692c2015-04-16 14:12:50 -0700310 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311 if (fd < 0) {
312 D("failed to open /dev/android_adb_enable\n");
313 } else {
314 close_on_exec(fd);
315 }
316
317 D("[ usb_init - starting thread ]\n");
Elliott Hughesf2517142015-05-05 13:41:21 -0700318 if (!adb_thread_create(usb_adb_open_thread, h)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800319 fatal_errno("cannot create usb thread");
320 }
321}
322
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100324static void init_functionfs(struct usb_handle *h)
325{
326 ssize_t ret;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700327 struct desc_v1 v1_descriptor;
328 struct desc_v2 v2_descriptor;
329
330 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
331 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phamb64ab092015-06-02 10:36:43 -0700332 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
333 FUNCTIONFS_HAS_SS_DESC;
Christopher Ferrisf7555b12015-01-23 17:09:56 -0800334 v2_descriptor.fs_count = 3;
335 v2_descriptor.hs_count = 3;
Jack Phamb64ab092015-06-02 10:36:43 -0700336 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700337 v2_descriptor.fs_descs = fs_descriptors;
338 v2_descriptor.hs_descs = hs_descriptors;
Jack Phamb64ab092015-06-02 10:36:43 -0700339 v2_descriptor.ss_descs = ss_descriptors;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100340
Jack Pham6c3cef52013-12-23 17:46:10 -0800341 if (h->control < 0) { // might have already done this before
342 D("OPENING %s\n", USB_FFS_ADB_EP0);
343 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
344 if (h->control < 0) {
345 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
346 goto err;
347 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100348
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700349 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham6c3cef52013-12-23 17:46:10 -0800350 if (ret < 0) {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700351 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
352 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
353 v1_descriptor.header.fs_count = 3;
354 v1_descriptor.header.hs_count = 3;
355 v1_descriptor.fs_descs = fs_descriptors;
356 v1_descriptor.hs_descs = hs_descriptors;
357 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
358 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
359 if (ret < 0) {
360 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
361 goto err;
362 }
Jack Pham6c3cef52013-12-23 17:46:10 -0800363 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100364
Jack Pham6c3cef52013-12-23 17:46:10 -0800365 ret = adb_write(h->control, &strings, sizeof(strings));
366 if (ret < 0) {
367 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
368 goto err;
369 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100370 }
371
372 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
373 if (h->bulk_out < 0) {
374 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
375 goto err;
376 }
377
378 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
379 if (h->bulk_in < 0) {
380 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
381 goto err;
382 }
383
384 return;
385
386err:
387 if (h->bulk_in > 0) {
388 adb_close(h->bulk_in);
389 h->bulk_in = -1;
390 }
391 if (h->bulk_out > 0) {
392 adb_close(h->bulk_out);
393 h->bulk_out = -1;
394 }
395 if (h->control > 0) {
396 adb_close(h->control);
397 h->control = -1;
398 }
399 return;
400}
401
402static void *usb_ffs_open_thread(void *x)
403{
404 struct usb_handle *usb = (struct usb_handle *)x;
405
Elliott Hughes7cf35752015-04-17 17:03:59 -0700406 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100407 // wait until the USB device needs opening
408 adb_mutex_lock(&usb->lock);
Jack Pham6c3cef52013-12-23 17:46:10 -0800409 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100410 adb_cond_wait(&usb->notify, &usb->lock);
411 adb_mutex_unlock(&usb->lock);
412
Elliott Hughes7cf35752015-04-17 17:03:59 -0700413 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100414 init_functionfs(usb);
415
Jack Pham6c3cef52013-12-23 17:46:10 -0800416 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100417 break;
418
419 adb_sleep_ms(1000);
420 }
Badhri Jagan Sridharane1febe92015-04-21 11:09:32 -0700421 property_set("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100422
423 D("[ usb_thread - registering device ]\n");
424 register_usb_transport(usb, 0, 0, 1);
425 }
426
427 // never gets here
428 return 0;
429}
430
Elliott Hughes392692c2015-04-16 14:12:50 -0700431static int bulk_write(int bulk_in, const uint8_t* buf, size_t length)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100432{
433 size_t count = 0;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100434
Elliott Hughes0bd85872015-08-25 10:59:45 -0700435 while (count < length) {
436 int ret = adb_write(bulk_in, buf + count, length - count);
437 if (ret < 0) return -1;
438 count += ret;
439 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100440
441 D("[ bulk_write done fd=%d ]\n", bulk_in);
442 return count;
443}
444
Elliott Hughes392692c2015-04-16 14:12:50 -0700445static int usb_ffs_write(usb_handle* h, const void* data, int len)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100446{
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100447 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
Elliott Hughes392692c2015-04-16 14:12:50 -0700448 int n = bulk_write(h->bulk_in, reinterpret_cast<const uint8_t*>(data), len);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100449 if (n != len) {
Elliott Hughes392692c2015-04-16 14:12:50 -0700450 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_in, n, strerror(errno));
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100451 return -1;
452 }
453 D("[ done fd=%d ]\n", h->bulk_in);
454 return 0;
455}
456
Elliott Hughes392692c2015-04-16 14:12:50 -0700457static int bulk_read(int bulk_out, uint8_t* buf, size_t length)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100458{
459 size_t count = 0;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100460
Elliott Hughes0bd85872015-08-25 10:59:45 -0700461 while (count < length) {
462 int ret = adb_read(bulk_out, buf + count, length - count);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100463 if (ret < 0) {
Elliott Hughes0bd85872015-08-25 10:59:45 -0700464 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n", bulk_out, length, count);
465 return -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100466 }
Elliott Hughes0bd85872015-08-25 10:59:45 -0700467 count += ret;
468 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100469
470 return count;
471}
472
Elliott Hughes392692c2015-04-16 14:12:50 -0700473static int usb_ffs_read(usb_handle* h, void* data, int len)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100474{
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100475 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
Elliott Hughes392692c2015-04-16 14:12:50 -0700476 int n = bulk_read(h->bulk_out, reinterpret_cast<uint8_t*>(data), len);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100477 if (n != len) {
Elliott Hughes392692c2015-04-16 14:12:50 -0700478 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_out, n, strerror(errno));
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100479 return -1;
480 }
481 D("[ done fd=%d ]\n", h->bulk_out);
482 return 0;
483}
484
485static void usb_ffs_kick(usb_handle *h)
486{
487 int err;
488
489 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
490 if (err < 0)
Spencer Low5c761bd2015-07-21 02:06:26 -0700491 D("[ kick: source (fd=%d) clear halt failed (%d) ]\n", h->bulk_in, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100492
493 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
494 if (err < 0)
Spencer Low5c761bd2015-07-21 02:06:26 -0700495 D("[ kick: sink (fd=%d) clear halt failed (%d) ]\n", h->bulk_out, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100496
497 adb_mutex_lock(&h->lock);
Jack Pham6c3cef52013-12-23 17:46:10 -0800498
499 // don't close ep0 here, since we may not need to reinitialize it with
500 // the same descriptors again. if however ep1/ep2 fail to re-open in
501 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100502 adb_close(h->bulk_out);
503 adb_close(h->bulk_in);
Jack Pham6c3cef52013-12-23 17:46:10 -0800504 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100505
506 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800507 adb_cond_signal(&h->notify);
508 adb_mutex_unlock(&h->lock);
509}
510
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100511static void usb_ffs_init()
512{
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100513 D("[ usb_init - using FunctionFS ]\n");
514
Elliott Hughes392692c2015-04-16 14:12:50 -0700515 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700516 if (h == nullptr) fatal("couldn't allocate usb_handle");
517
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100518 h->write = usb_ffs_write;
519 h->read = usb_ffs_read;
520 h->kick = usb_ffs_kick;
Elliott Hughes392692c2015-04-16 14:12:50 -0700521 h->control = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100522 h->bulk_out = -1;
523 h->bulk_out = -1;
524
525 adb_cond_init(&h->notify, 0);
526 adb_mutex_init(&h->lock, 0);
527
528 D("[ usb_init - starting thread ]\n");
Elliott Hughesf2517142015-05-05 13:41:21 -0700529 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100530 fatal_errno("[ cannot create usb thread ]\n");
531 }
532}
533
534void usb_init()
535{
536 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
537 usb_ffs_init();
538 else
539 usb_adb_init();
540}
541
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100542int usb_write(usb_handle *h, const void *data, int len)
543{
544 return h->write(h, data, len);
545}
546
547int usb_read(usb_handle *h, void *data, int len)
548{
549 return h->read(h, data, len);
550}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800551int usb_close(usb_handle *h)
552{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800553 return 0;
554}
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100555
556void usb_kick(usb_handle *h)
557{
558 h->kick(h);
559}