blob: d34c45407c11742e4209e50cac0a0ae4286781c5 [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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_USB
18
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080034#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010036#define MAX_PACKET_SIZE_FS 64
37#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +080038#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-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 Projectdd7bc332009-03-03 19:32:55 -080042
43struct usb_handle
44{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045 adb_cond_t notify;
46 adb_mutex_t lock;
Andrzej Pietrasiewiczfd96db12012-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 Projectdd7bc332009-03-03 19:32:55 -080059};
60
Badhri Jagan Sridharanab3446d2014-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
67struct desc_v1 {
68 struct usb_functionfs_descs_head_v1 {
69 __le32 magic;
70 __le32 length;
71 __le32 fs_count;
72 __le32 hs_count;
73 } __attribute__((packed)) header;
74 struct func_desc fs_descs, hs_descs;
75} __attribute__((packed));
76
77struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080078 struct usb_functionfs_descs_head_v2 header;
79 // The rest of the structure depends on the flags in the header.
80 __le32 fs_count;
81 __le32 hs_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070082 struct func_desc fs_descs, hs_descs;
83} __attribute__((packed));
84
Elliott Hughes3edd54b2015-05-05 18:26:10 -070085static struct func_desc fs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070086 .intf = {
87 .bLength = sizeof(fs_descriptors.intf),
88 .bDescriptorType = USB_DT_INTERFACE,
89 .bInterfaceNumber = 0,
90 .bNumEndpoints = 2,
91 .bInterfaceClass = ADB_CLASS,
92 .bInterfaceSubClass = ADB_SUBCLASS,
93 .bInterfaceProtocol = ADB_PROTOCOL,
94 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010095 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070096 .source = {
97 .bLength = sizeof(fs_descriptors.source),
98 .bDescriptorType = USB_DT_ENDPOINT,
99 .bEndpointAddress = 1 | USB_DIR_OUT,
100 .bmAttributes = USB_ENDPOINT_XFER_BULK,
101 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100102 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700103 .sink = {
104 .bLength = sizeof(fs_descriptors.sink),
105 .bDescriptorType = USB_DT_ENDPOINT,
106 .bEndpointAddress = 2 | USB_DIR_IN,
107 .bmAttributes = USB_ENDPOINT_XFER_BULK,
108 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
109 },
110};
111
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700112static struct func_desc hs_descriptors = {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700113 .intf = {
114 .bLength = sizeof(hs_descriptors.intf),
115 .bDescriptorType = USB_DT_INTERFACE,
116 .bInterfaceNumber = 0,
117 .bNumEndpoints = 2,
118 .bInterfaceClass = ADB_CLASS,
119 .bInterfaceSubClass = ADB_SUBCLASS,
120 .bInterfaceProtocol = ADB_PROTOCOL,
121 .iInterface = 1, /* first string from the provided table */
122 },
123 .source = {
124 .bLength = sizeof(hs_descriptors.source),
125 .bDescriptorType = USB_DT_ENDPOINT,
126 .bEndpointAddress = 1 | USB_DIR_OUT,
127 .bmAttributes = USB_ENDPOINT_XFER_BULK,
128 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
129 },
130 .sink = {
131 .bLength = sizeof(hs_descriptors.sink),
132 .bDescriptorType = USB_DT_ENDPOINT,
133 .bEndpointAddress = 2 | USB_DIR_IN,
134 .bmAttributes = USB_ENDPOINT_XFER_BULK,
135 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800136 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100137};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100139#define STR_INTERFACE_ "ADB Interface"
140
141static const struct {
142 struct usb_functionfs_strings_head header;
143 struct {
144 __le16 code;
145 const char str1[sizeof(STR_INTERFACE_)];
146 } __attribute__((packed)) lang0;
147} __attribute__((packed)) strings = {
148 .header = {
149 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
150 .length = cpu_to_le32(sizeof(strings)),
151 .str_count = cpu_to_le32(1),
152 .lang_count = cpu_to_le32(1),
153 },
154 .lang0 = {
155 cpu_to_le16(0x0409), /* en-us */
156 STR_INTERFACE_,
157 },
158};
159
160
161
162static void *usb_adb_open_thread(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163{
164 struct usb_handle *usb = (struct usb_handle *)x;
165 int fd;
166
Elliott Hughesa7090b92015-04-17 17:03:59 -0700167 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 // wait until the USB device needs opening
169 adb_mutex_lock(&usb->lock);
170 while (usb->fd != -1)
171 adb_cond_wait(&usb->notify, &usb->lock);
172 adb_mutex_unlock(&usb->lock);
173
174 D("[ usb_thread - opening device ]\n");
175 do {
176 /* XXX use inotify? */
177 fd = unix_open("/dev/android_adb", O_RDWR);
178 if (fd < 0) {
179 // to support older kernels
180 fd = unix_open("/dev/android", O_RDWR);
181 }
182 if (fd < 0) {
183 adb_sleep_ms(1000);
184 }
185 } while (fd < 0);
186 D("[ opening device succeeded ]\n");
187
188 close_on_exec(fd);
189 usb->fd = fd;
190
191 D("[ usb_thread - registering device ]\n");
Scott Andersone109d262012-04-20 11:21:14 -0700192 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 }
194
195 // never gets here
196 return 0;
197}
198
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100199static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200{
201 int n;
202
JP Abgrall408fa572011-03-16 15:57:42 -0700203 D("about to write (fd=%d, len=%d)\n", h->fd, len);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700204 n = unix_write(h->fd, data, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700206 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
207 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 return -1;
209 }
JP Abgrall408fa572011-03-16 15:57:42 -0700210 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 return 0;
212}
213
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100214static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215{
216 int n;
217
JP Abgrall408fa572011-03-16 15:57:42 -0700218 D("about to read (fd=%d, len=%d)\n", h->fd, len);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700219 n = unix_read(h->fd, data, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700221 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
222 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 return -1;
224 }
JP Abgrall408fa572011-03-16 15:57:42 -0700225 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 return 0;
227}
228
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100229static void usb_adb_kick(usb_handle *h)
230{
231 D("usb_kick\n");
232 adb_mutex_lock(&h->lock);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700233 unix_close(h->fd);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100234 h->fd = -1;
235
236 // notify usb_adb_open_thread that we are disconnected
237 adb_cond_signal(&h->notify);
238 adb_mutex_unlock(&h->lock);
239}
240
241static void usb_adb_init()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242{
Elliott Hughes2acec912015-04-16 14:12:50 -0700243 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700244 if (h == nullptr) fatal("couldn't allocate usb_handle");
245
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100246 h->write = usb_adb_write;
247 h->read = usb_adb_read;
248 h->kick = usb_adb_kick;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 h->fd = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100250
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 adb_cond_init(&h->notify, 0);
252 adb_mutex_init(&h->lock, 0);
253
Elliott Hughes2acec912015-04-16 14:12:50 -0700254 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 // the enabling of the adb USB function in the kernel.
256 // We never touch this file again - just leave it open
257 // indefinitely so the kernel will know when we are running
258 // and when we are not.
Elliott Hughes2acec912015-04-16 14:12:50 -0700259 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 if (fd < 0) {
261 D("failed to open /dev/android_adb_enable\n");
262 } else {
263 close_on_exec(fd);
264 }
265
266 D("[ usb_init - starting thread ]\n");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700267 if (!adb_thread_create(usb_adb_open_thread, h)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 fatal_errno("cannot create usb thread");
269 }
270}
271
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100273static void init_functionfs(struct usb_handle *h)
274{
275 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700276 struct desc_v1 v1_descriptor;
277 struct desc_v2 v2_descriptor;
278
279 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
280 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
281 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800282 v2_descriptor.fs_count = 3;
283 v2_descriptor.hs_count = 3;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700284 v2_descriptor.fs_descs = fs_descriptors;
285 v2_descriptor.hs_descs = hs_descriptors;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100286
Jack Pham4cbf1d82013-12-23 17:46:10 -0800287 if (h->control < 0) { // might have already done this before
288 D("OPENING %s\n", USB_FFS_ADB_EP0);
289 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
290 if (h->control < 0) {
291 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
292 goto err;
293 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100294
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700295 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham4cbf1d82013-12-23 17:46:10 -0800296 if (ret < 0) {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700297 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
298 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
299 v1_descriptor.header.fs_count = 3;
300 v1_descriptor.header.hs_count = 3;
301 v1_descriptor.fs_descs = fs_descriptors;
302 v1_descriptor.hs_descs = hs_descriptors;
303 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
304 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
305 if (ret < 0) {
306 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
307 goto err;
308 }
Jack Pham4cbf1d82013-12-23 17:46:10 -0800309 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100310
Jack Pham4cbf1d82013-12-23 17:46:10 -0800311 ret = adb_write(h->control, &strings, sizeof(strings));
312 if (ret < 0) {
313 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
314 goto err;
315 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100316 }
317
318 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
319 if (h->bulk_out < 0) {
320 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
321 goto err;
322 }
323
324 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
325 if (h->bulk_in < 0) {
326 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
327 goto err;
328 }
329
330 return;
331
332err:
333 if (h->bulk_in > 0) {
334 adb_close(h->bulk_in);
335 h->bulk_in = -1;
336 }
337 if (h->bulk_out > 0) {
338 adb_close(h->bulk_out);
339 h->bulk_out = -1;
340 }
341 if (h->control > 0) {
342 adb_close(h->control);
343 h->control = -1;
344 }
345 return;
346}
347
348static void *usb_ffs_open_thread(void *x)
349{
350 struct usb_handle *usb = (struct usb_handle *)x;
351
Elliott Hughesa7090b92015-04-17 17:03:59 -0700352 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100353 // wait until the USB device needs opening
354 adb_mutex_lock(&usb->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800355 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100356 adb_cond_wait(&usb->notify, &usb->lock);
357 adb_mutex_unlock(&usb->lock);
358
Elliott Hughesa7090b92015-04-17 17:03:59 -0700359 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100360 init_functionfs(usb);
361
Jack Pham4cbf1d82013-12-23 17:46:10 -0800362 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100363 break;
364
365 adb_sleep_ms(1000);
366 }
Badhri Jagan Sridharan5f973702015-04-21 11:09:32 -0700367 property_set("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100368
369 D("[ usb_thread - registering device ]\n");
370 register_usb_transport(usb, 0, 0, 1);
371 }
372
373 // never gets here
374 return 0;
375}
376
Elliott Hughes2acec912015-04-16 14:12:50 -0700377static int bulk_write(int bulk_in, const uint8_t* buf, size_t length)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100378{
379 size_t count = 0;
380 int ret;
381
382 do {
383 ret = adb_write(bulk_in, buf + count, length - count);
384 if (ret < 0) {
385 if (errno != EINTR)
386 return ret;
387 } else {
388 count += ret;
389 }
390 } while (count < length);
391
392 D("[ bulk_write done fd=%d ]\n", bulk_in);
393 return count;
394}
395
Elliott Hughes2acec912015-04-16 14:12:50 -0700396static int usb_ffs_write(usb_handle* h, const void* data, int len)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100397{
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100398 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
Elliott Hughes2acec912015-04-16 14:12:50 -0700399 int n = bulk_write(h->bulk_in, reinterpret_cast<const uint8_t*>(data), len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100400 if (n != len) {
Elliott Hughes2acec912015-04-16 14:12:50 -0700401 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_in, n, strerror(errno));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100402 return -1;
403 }
404 D("[ done fd=%d ]\n", h->bulk_in);
405 return 0;
406}
407
Elliott Hughes2acec912015-04-16 14:12:50 -0700408static int bulk_read(int bulk_out, uint8_t* buf, size_t length)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100409{
410 size_t count = 0;
411 int ret;
412
413 do {
414 ret = adb_read(bulk_out, buf + count, length - count);
415 if (ret < 0) {
416 if (errno != EINTR) {
Elliott Hughesccecf142014-01-16 10:53:11 -0800417 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n",
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100418 bulk_out, length, count);
419 return ret;
420 }
421 } else {
422 count += ret;
423 }
424 } while (count < length);
425
426 return count;
427}
428
Elliott Hughes2acec912015-04-16 14:12:50 -0700429static int usb_ffs_read(usb_handle* h, void* data, int len)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100430{
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100431 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
Elliott Hughes2acec912015-04-16 14:12:50 -0700432 int n = bulk_read(h->bulk_out, reinterpret_cast<uint8_t*>(data), len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100433 if (n != len) {
Elliott Hughes2acec912015-04-16 14:12:50 -0700434 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_out, n, strerror(errno));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100435 return -1;
436 }
437 D("[ done fd=%d ]\n", h->bulk_out);
438 return 0;
439}
440
441static void usb_ffs_kick(usb_handle *h)
442{
443 int err;
444
445 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
446 if (err < 0)
447 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
448
449 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
450 if (err < 0)
451 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
452
453 adb_mutex_lock(&h->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800454
455 // don't close ep0 here, since we may not need to reinitialize it with
456 // the same descriptors again. if however ep1/ep2 fail to re-open in
457 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100458 adb_close(h->bulk_out);
459 adb_close(h->bulk_in);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800460 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100461
462 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 adb_cond_signal(&h->notify);
464 adb_mutex_unlock(&h->lock);
465}
466
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100467static void usb_ffs_init()
468{
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100469 D("[ usb_init - using FunctionFS ]\n");
470
Elliott Hughes2acec912015-04-16 14:12:50 -0700471 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700472 if (h == nullptr) fatal("couldn't allocate usb_handle");
473
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100474 h->write = usb_ffs_write;
475 h->read = usb_ffs_read;
476 h->kick = usb_ffs_kick;
Elliott Hughes2acec912015-04-16 14:12:50 -0700477 h->control = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100478 h->bulk_out = -1;
479 h->bulk_out = -1;
480
481 adb_cond_init(&h->notify, 0);
482 adb_mutex_init(&h->lock, 0);
483
484 D("[ usb_init - starting thread ]\n");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700485 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100486 fatal_errno("[ cannot create usb thread ]\n");
487 }
488}
489
490void usb_init()
491{
492 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
493 usb_ffs_init();
494 else
495 usb_adb_init();
496}
497
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100498int usb_write(usb_handle *h, const void *data, int len)
499{
500 return h->write(h, data, len);
501}
502
503int usb_read(usb_handle *h, void *data, int len)
504{
505 return h->read(h, data, len);
506}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507int usb_close(usb_handle *h)
508{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 return 0;
510}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100511
512void usb_kick(usb_handle *h)
513{
514 h->kick(h);
515}