blob: e1d759438ac773f399ae4f63a6ae7ed935afe6ab [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
Siva Velusamy2669cf92015-08-28 16:37:29 -0700212 adb_thread_setname("usb open");
213
Elliott Hughes7cf35752015-04-17 17:03:59 -0700214 while (true) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215 // wait until the USB device needs opening
216 adb_mutex_lock(&usb->lock);
217 while (usb->fd != -1)
218 adb_cond_wait(&usb->notify, &usb->lock);
219 adb_mutex_unlock(&usb->lock);
220
221 D("[ usb_thread - opening device ]\n");
222 do {
223 /* XXX use inotify? */
224 fd = unix_open("/dev/android_adb", O_RDWR);
225 if (fd < 0) {
226 // to support older kernels
227 fd = unix_open("/dev/android", O_RDWR);
228 }
229 if (fd < 0) {
230 adb_sleep_ms(1000);
231 }
232 } while (fd < 0);
233 D("[ opening device succeeded ]\n");
234
235 close_on_exec(fd);
236 usb->fd = fd;
237
238 D("[ usb_thread - registering device ]\n");
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700239 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 }
241
242 // never gets here
243 return 0;
244}
245
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100246static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247{
248 int n;
249
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700250 D("about to write (fd=%d, len=%d)\n", h->fd, len);
Spencer Low3a2421b2015-05-22 20:09:06 -0700251 n = unix_write(h->fd, data, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252 if(n != len) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700253 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
254 h->fd, n, errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255 return -1;
256 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700257 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 return 0;
259}
260
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100261static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700263 D("about to read (fd=%d, len=%d)\n", h->fd, len);
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100264 while (len > 0) {
265 // The kernel implementation of adb_read in f_adb.c doesn't support
266 // reads larger then 4096 bytes. Read the data in 4096 byte chunks to
267 // avoid the issue. (The ffs implementation doesn't have this limit.)
268 int bytes_to_read = len < 4096 ? len : 4096;
269 int n = unix_read(h->fd, data, bytes_to_read);
270 if (n != bytes_to_read) {
271 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
272 h->fd, n, errno, strerror(errno));
273 return -1;
274 }
275 len -= n;
276 data = ((char*)data) + n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700278 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279 return 0;
280}
281
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100282static void usb_adb_kick(usb_handle *h)
283{
284 D("usb_kick\n");
285 adb_mutex_lock(&h->lock);
Spencer Low3a2421b2015-05-22 20:09:06 -0700286 unix_close(h->fd);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100287 h->fd = -1;
288
289 // notify usb_adb_open_thread that we are disconnected
290 adb_cond_signal(&h->notify);
291 adb_mutex_unlock(&h->lock);
292}
293
294static void usb_adb_init()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800295{
Elliott Hughes392692c2015-04-16 14:12:50 -0700296 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700297 if (h == nullptr) fatal("couldn't allocate usb_handle");
298
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100299 h->write = usb_adb_write;
300 h->read = usb_adb_read;
301 h->kick = usb_adb_kick;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800302 h->fd = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100303
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800304 adb_cond_init(&h->notify, 0);
305 adb_mutex_init(&h->lock, 0);
306
Elliott Hughes392692c2015-04-16 14:12:50 -0700307 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800308 // the enabling of the adb USB function in the kernel.
309 // We never touch this file again - just leave it open
310 // indefinitely so the kernel will know when we are running
311 // and when we are not.
Elliott Hughes392692c2015-04-16 14:12:50 -0700312 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800313 if (fd < 0) {
314 D("failed to open /dev/android_adb_enable\n");
315 } else {
316 close_on_exec(fd);
317 }
318
319 D("[ usb_init - starting thread ]\n");
Elliott Hughesf2517142015-05-05 13:41:21 -0700320 if (!adb_thread_create(usb_adb_open_thread, h)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800321 fatal_errno("cannot create usb thread");
322 }
323}
324
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800325
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100326static void init_functionfs(struct usb_handle *h)
327{
328 ssize_t ret;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700329 struct desc_v1 v1_descriptor;
330 struct desc_v2 v2_descriptor;
331
332 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
333 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phamb64ab092015-06-02 10:36:43 -0700334 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
335 FUNCTIONFS_HAS_SS_DESC;
Christopher Ferrisf7555b12015-01-23 17:09:56 -0800336 v2_descriptor.fs_count = 3;
337 v2_descriptor.hs_count = 3;
Jack Phamb64ab092015-06-02 10:36:43 -0700338 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700339 v2_descriptor.fs_descs = fs_descriptors;
340 v2_descriptor.hs_descs = hs_descriptors;
Jack Phamb64ab092015-06-02 10:36:43 -0700341 v2_descriptor.ss_descs = ss_descriptors;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100342
Jack Pham6c3cef52013-12-23 17:46:10 -0800343 if (h->control < 0) { // might have already done this before
344 D("OPENING %s\n", USB_FFS_ADB_EP0);
345 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
346 if (h->control < 0) {
347 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
348 goto err;
349 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100350
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700351 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham6c3cef52013-12-23 17:46:10 -0800352 if (ret < 0) {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700353 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
354 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
355 v1_descriptor.header.fs_count = 3;
356 v1_descriptor.header.hs_count = 3;
357 v1_descriptor.fs_descs = fs_descriptors;
358 v1_descriptor.hs_descs = hs_descriptors;
359 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
360 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
361 if (ret < 0) {
362 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
363 goto err;
364 }
Jack Pham6c3cef52013-12-23 17:46:10 -0800365 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100366
Jack Pham6c3cef52013-12-23 17:46:10 -0800367 ret = adb_write(h->control, &strings, sizeof(strings));
368 if (ret < 0) {
369 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
370 goto err;
371 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100372 }
373
374 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
375 if (h->bulk_out < 0) {
376 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
377 goto err;
378 }
379
380 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
381 if (h->bulk_in < 0) {
382 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
383 goto err;
384 }
385
386 return;
387
388err:
389 if (h->bulk_in > 0) {
390 adb_close(h->bulk_in);
391 h->bulk_in = -1;
392 }
393 if (h->bulk_out > 0) {
394 adb_close(h->bulk_out);
395 h->bulk_out = -1;
396 }
397 if (h->control > 0) {
398 adb_close(h->control);
399 h->control = -1;
400 }
401 return;
402}
403
404static void *usb_ffs_open_thread(void *x)
405{
406 struct usb_handle *usb = (struct usb_handle *)x;
407
Siva Velusamy2669cf92015-08-28 16:37:29 -0700408 adb_thread_setname("usb ffs open");
409
Elliott Hughes7cf35752015-04-17 17:03:59 -0700410 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100411 // wait until the USB device needs opening
412 adb_mutex_lock(&usb->lock);
Jack Pham6c3cef52013-12-23 17:46:10 -0800413 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100414 adb_cond_wait(&usb->notify, &usb->lock);
415 adb_mutex_unlock(&usb->lock);
416
Elliott Hughes7cf35752015-04-17 17:03:59 -0700417 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100418 init_functionfs(usb);
419
Jack Pham6c3cef52013-12-23 17:46:10 -0800420 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100421 break;
422
423 adb_sleep_ms(1000);
424 }
Badhri Jagan Sridharane1febe92015-04-21 11:09:32 -0700425 property_set("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100426
427 D("[ usb_thread - registering device ]\n");
428 register_usb_transport(usb, 0, 0, 1);
429 }
430
431 // never gets here
432 return 0;
433}
434
Elliott Hughes392692c2015-04-16 14:12:50 -0700435static int bulk_write(int bulk_in, const uint8_t* buf, size_t length)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100436{
437 size_t count = 0;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100438
Elliott Hughes0bd85872015-08-25 10:59:45 -0700439 while (count < length) {
440 int ret = adb_write(bulk_in, buf + count, length - count);
441 if (ret < 0) return -1;
442 count += ret;
443 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100444
445 D("[ bulk_write done fd=%d ]\n", bulk_in);
446 return count;
447}
448
Elliott Hughes392692c2015-04-16 14:12:50 -0700449static int usb_ffs_write(usb_handle* h, const void* data, int len)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100450{
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100451 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
Elliott Hughes392692c2015-04-16 14:12:50 -0700452 int n = bulk_write(h->bulk_in, reinterpret_cast<const uint8_t*>(data), len);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100453 if (n != len) {
Elliott Hughes392692c2015-04-16 14:12:50 -0700454 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_in, n, strerror(errno));
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100455 return -1;
456 }
457 D("[ done fd=%d ]\n", h->bulk_in);
458 return 0;
459}
460
Elliott Hughes392692c2015-04-16 14:12:50 -0700461static int bulk_read(int bulk_out, uint8_t* buf, size_t length)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100462{
463 size_t count = 0;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100464
Elliott Hughes0bd85872015-08-25 10:59:45 -0700465 while (count < length) {
466 int ret = adb_read(bulk_out, buf + count, length - count);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100467 if (ret < 0) {
Elliott Hughes0bd85872015-08-25 10:59:45 -0700468 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n", bulk_out, length, count);
469 return -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100470 }
Elliott Hughes0bd85872015-08-25 10:59:45 -0700471 count += ret;
472 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100473
474 return count;
475}
476
Elliott Hughes392692c2015-04-16 14:12:50 -0700477static int usb_ffs_read(usb_handle* h, void* data, int len)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100478{
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100479 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
Elliott Hughes392692c2015-04-16 14:12:50 -0700480 int n = bulk_read(h->bulk_out, reinterpret_cast<uint8_t*>(data), len);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100481 if (n != len) {
Elliott Hughes392692c2015-04-16 14:12:50 -0700482 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_out, n, strerror(errno));
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100483 return -1;
484 }
485 D("[ done fd=%d ]\n", h->bulk_out);
486 return 0;
487}
488
489static void usb_ffs_kick(usb_handle *h)
490{
491 int err;
492
493 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
494 if (err < 0)
Spencer Low5c761bd2015-07-21 02:06:26 -0700495 D("[ kick: source (fd=%d) clear halt failed (%d) ]\n", h->bulk_in, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100496
497 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
498 if (err < 0)
Spencer Low5c761bd2015-07-21 02:06:26 -0700499 D("[ kick: sink (fd=%d) clear halt failed (%d) ]\n", h->bulk_out, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100500
501 adb_mutex_lock(&h->lock);
Jack Pham6c3cef52013-12-23 17:46:10 -0800502
503 // don't close ep0 here, since we may not need to reinitialize it with
504 // the same descriptors again. if however ep1/ep2 fail to re-open in
505 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100506 adb_close(h->bulk_out);
507 adb_close(h->bulk_in);
Jack Pham6c3cef52013-12-23 17:46:10 -0800508 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100509
510 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800511 adb_cond_signal(&h->notify);
512 adb_mutex_unlock(&h->lock);
513}
514
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100515static void usb_ffs_init()
516{
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100517 D("[ usb_init - using FunctionFS ]\n");
518
Elliott Hughes392692c2015-04-16 14:12:50 -0700519 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700520 if (h == nullptr) fatal("couldn't allocate usb_handle");
521
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100522 h->write = usb_ffs_write;
523 h->read = usb_ffs_read;
524 h->kick = usb_ffs_kick;
Elliott Hughes392692c2015-04-16 14:12:50 -0700525 h->control = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100526 h->bulk_out = -1;
527 h->bulk_out = -1;
528
529 adb_cond_init(&h->notify, 0);
530 adb_mutex_init(&h->lock, 0);
531
532 D("[ usb_init - starting thread ]\n");
Elliott Hughesf2517142015-05-05 13:41:21 -0700533 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100534 fatal_errno("[ cannot create usb thread ]\n");
535 }
536}
537
538void usb_init()
539{
540 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
541 usb_ffs_init();
542 else
543 usb_adb_init();
544}
545
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100546int usb_write(usb_handle *h, const void *data, int len)
547{
548 return h->write(h, data, len);
549}
550
551int usb_read(usb_handle *h, void *data, int len)
552{
553 return h->read(h, data, len);
554}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800555int usb_close(usb_handle *h)
556{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800557 return 0;
558}
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100559
560void usb_kick(usb_handle *h)
561{
562 h->kick(h);
563}