blob: 343f20cb7334ab2105536c7fd7226ad83698a383 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <dirent.h>
22#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <linux/usb/ch9.h>
24#include <linux/usb/functionfs.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/ioctl.h>
29#include <sys/types.h>
30#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080033#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010035#define MAX_PACKET_SIZE_FS 64
36#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +080037#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010038
39#define cpu_to_le16(x) htole16(x)
40#define cpu_to_le32(x) htole32(x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42struct usb_handle
43{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044 adb_cond_t notify;
45 adb_mutex_t lock;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010046
47 int (*write)(usb_handle *h, const void *data, int len);
48 int (*read)(usb_handle *h, void *data, int len);
49 void (*kick)(usb_handle *h);
50
51 // Legacy f_adb
52 int fd;
53
54 // FunctionFS
55 int control;
56 int bulk_out; /* "out" from the host's perspective => source for adbd */
57 int bulk_in; /* "in" from the host's perspective => sink for adbd */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058};
59
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070060struct func_desc {
61 struct usb_interface_descriptor intf;
62 struct usb_endpoint_descriptor_no_audio source;
63 struct usb_endpoint_descriptor_no_audio sink;
64} __attribute__((packed));
65
66struct desc_v1 {
67 struct usb_functionfs_descs_head_v1 {
68 __le32 magic;
69 __le32 length;
70 __le32 fs_count;
71 __le32 hs_count;
72 } __attribute__((packed)) header;
73 struct func_desc fs_descs, hs_descs;
74} __attribute__((packed));
75
76struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080077 struct usb_functionfs_descs_head_v2 header;
78 // The rest of the structure depends on the flags in the header.
79 __le32 fs_count;
80 __le32 hs_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070081 struct func_desc fs_descs, hs_descs;
82} __attribute__((packed));
83
84struct func_desc fs_descriptors = {
85 .intf = {
86 .bLength = sizeof(fs_descriptors.intf),
87 .bDescriptorType = USB_DT_INTERFACE,
88 .bInterfaceNumber = 0,
89 .bNumEndpoints = 2,
90 .bInterfaceClass = ADB_CLASS,
91 .bInterfaceSubClass = ADB_SUBCLASS,
92 .bInterfaceProtocol = ADB_PROTOCOL,
93 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010094 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070095 .source = {
96 .bLength = sizeof(fs_descriptors.source),
97 .bDescriptorType = USB_DT_ENDPOINT,
98 .bEndpointAddress = 1 | USB_DIR_OUT,
99 .bmAttributes = USB_ENDPOINT_XFER_BULK,
100 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100101 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700102 .sink = {
103 .bLength = sizeof(fs_descriptors.sink),
104 .bDescriptorType = USB_DT_ENDPOINT,
105 .bEndpointAddress = 2 | USB_DIR_IN,
106 .bmAttributes = USB_ENDPOINT_XFER_BULK,
107 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
108 },
109};
110
111struct func_desc hs_descriptors = {
112 .intf = {
113 .bLength = sizeof(hs_descriptors.intf),
114 .bDescriptorType = USB_DT_INTERFACE,
115 .bInterfaceNumber = 0,
116 .bNumEndpoints = 2,
117 .bInterfaceClass = ADB_CLASS,
118 .bInterfaceSubClass = ADB_SUBCLASS,
119 .bInterfaceProtocol = ADB_PROTOCOL,
120 .iInterface = 1, /* first string from the provided table */
121 },
122 .source = {
123 .bLength = sizeof(hs_descriptors.source),
124 .bDescriptorType = USB_DT_ENDPOINT,
125 .bEndpointAddress = 1 | USB_DIR_OUT,
126 .bmAttributes = USB_ENDPOINT_XFER_BULK,
127 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
128 },
129 .sink = {
130 .bLength = sizeof(hs_descriptors.sink),
131 .bDescriptorType = USB_DT_ENDPOINT,
132 .bEndpointAddress = 2 | USB_DIR_IN,
133 .bmAttributes = USB_ENDPOINT_XFER_BULK,
134 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800135 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100136};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100138#define STR_INTERFACE_ "ADB Interface"
139
140static const struct {
141 struct usb_functionfs_strings_head header;
142 struct {
143 __le16 code;
144 const char str1[sizeof(STR_INTERFACE_)];
145 } __attribute__((packed)) lang0;
146} __attribute__((packed)) strings = {
147 .header = {
148 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
149 .length = cpu_to_le32(sizeof(strings)),
150 .str_count = cpu_to_le32(1),
151 .lang_count = cpu_to_le32(1),
152 },
153 .lang0 = {
154 cpu_to_le16(0x0409), /* en-us */
155 STR_INTERFACE_,
156 },
157};
158
159
160
161static void *usb_adb_open_thread(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162{
163 struct usb_handle *usb = (struct usb_handle *)x;
164 int fd;
165
Elliott Hughesa7090b92015-04-17 17:03:59 -0700166 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 // wait until the USB device needs opening
168 adb_mutex_lock(&usb->lock);
169 while (usb->fd != -1)
170 adb_cond_wait(&usb->notify, &usb->lock);
171 adb_mutex_unlock(&usb->lock);
172
173 D("[ usb_thread - opening device ]\n");
174 do {
175 /* XXX use inotify? */
176 fd = unix_open("/dev/android_adb", O_RDWR);
177 if (fd < 0) {
178 // to support older kernels
179 fd = unix_open("/dev/android", O_RDWR);
180 }
181 if (fd < 0) {
182 adb_sleep_ms(1000);
183 }
184 } while (fd < 0);
185 D("[ opening device succeeded ]\n");
186
187 close_on_exec(fd);
188 usb->fd = fd;
189
190 D("[ usb_thread - registering device ]\n");
Scott Andersone109d262012-04-20 11:21:14 -0700191 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 }
193
194 // never gets here
195 return 0;
196}
197
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100198static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199{
200 int n;
201
JP Abgrall408fa572011-03-16 15:57:42 -0700202 D("about to write (fd=%d, len=%d)\n", h->fd, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 n = adb_write(h->fd, data, len);
204 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700205 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
206 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 return -1;
208 }
JP Abgrall408fa572011-03-16 15:57:42 -0700209 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 return 0;
211}
212
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100213static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214{
215 int n;
216
JP Abgrall408fa572011-03-16 15:57:42 -0700217 D("about to read (fd=%d, len=%d)\n", h->fd, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 n = adb_read(h->fd, data, len);
219 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700220 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
221 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 return -1;
223 }
JP Abgrall408fa572011-03-16 15:57:42 -0700224 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 return 0;
226}
227
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100228static void usb_adb_kick(usb_handle *h)
229{
230 D("usb_kick\n");
231 adb_mutex_lock(&h->lock);
232 adb_close(h->fd);
233 h->fd = -1;
234
235 // notify usb_adb_open_thread that we are disconnected
236 adb_cond_signal(&h->notify);
237 adb_mutex_unlock(&h->lock);
238}
239
240static void usb_adb_init()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241{
Elliott Hughes2acec912015-04-16 14:12:50 -0700242 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100243 h->write = usb_adb_write;
244 h->read = usb_adb_read;
245 h->kick = usb_adb_kick;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 h->fd = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100247
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 adb_cond_init(&h->notify, 0);
249 adb_mutex_init(&h->lock, 0);
250
Elliott Hughes2acec912015-04-16 14:12:50 -0700251 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 // the enabling of the adb USB function in the kernel.
253 // We never touch this file again - just leave it open
254 // indefinitely so the kernel will know when we are running
255 // and when we are not.
Elliott Hughes2acec912015-04-16 14:12:50 -0700256 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 if (fd < 0) {
258 D("failed to open /dev/android_adb_enable\n");
259 } else {
260 close_on_exec(fd);
261 }
262
263 D("[ usb_init - starting thread ]\n");
Elliott Hughes2acec912015-04-16 14:12:50 -0700264 adb_thread_t tid;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100265 if(adb_thread_create(&tid, usb_adb_open_thread, h)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 fatal_errno("cannot create usb thread");
267 }
268}
269
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100271static void init_functionfs(struct usb_handle *h)
272{
273 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700274 struct desc_v1 v1_descriptor;
275 struct desc_v2 v2_descriptor;
276
277 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
278 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
279 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800280 v2_descriptor.fs_count = 3;
281 v2_descriptor.hs_count = 3;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700282 v2_descriptor.fs_descs = fs_descriptors;
283 v2_descriptor.hs_descs = hs_descriptors;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100284
Jack Pham4cbf1d82013-12-23 17:46:10 -0800285 if (h->control < 0) { // might have already done this before
286 D("OPENING %s\n", USB_FFS_ADB_EP0);
287 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
288 if (h->control < 0) {
289 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
290 goto err;
291 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100292
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700293 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham4cbf1d82013-12-23 17:46:10 -0800294 if (ret < 0) {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700295 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
296 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
297 v1_descriptor.header.fs_count = 3;
298 v1_descriptor.header.hs_count = 3;
299 v1_descriptor.fs_descs = fs_descriptors;
300 v1_descriptor.hs_descs = hs_descriptors;
301 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
302 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
303 if (ret < 0) {
304 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
305 goto err;
306 }
Jack Pham4cbf1d82013-12-23 17:46:10 -0800307 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100308
Jack Pham4cbf1d82013-12-23 17:46:10 -0800309 ret = adb_write(h->control, &strings, sizeof(strings));
310 if (ret < 0) {
311 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
312 goto err;
313 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100314 }
315
316 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
317 if (h->bulk_out < 0) {
318 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
319 goto err;
320 }
321
322 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
323 if (h->bulk_in < 0) {
324 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
325 goto err;
326 }
327
328 return;
329
330err:
331 if (h->bulk_in > 0) {
332 adb_close(h->bulk_in);
333 h->bulk_in = -1;
334 }
335 if (h->bulk_out > 0) {
336 adb_close(h->bulk_out);
337 h->bulk_out = -1;
338 }
339 if (h->control > 0) {
340 adb_close(h->control);
341 h->control = -1;
342 }
343 return;
344}
345
346static void *usb_ffs_open_thread(void *x)
347{
348 struct usb_handle *usb = (struct usb_handle *)x;
349
Elliott Hughesa7090b92015-04-17 17:03:59 -0700350 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100351 // wait until the USB device needs opening
352 adb_mutex_lock(&usb->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800353 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100354 adb_cond_wait(&usb->notify, &usb->lock);
355 adb_mutex_unlock(&usb->lock);
356
Elliott Hughesa7090b92015-04-17 17:03:59 -0700357 while (true) {
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100358 init_functionfs(usb);
359
Jack Pham4cbf1d82013-12-23 17:46:10 -0800360 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100361 break;
362
363 adb_sleep_ms(1000);
364 }
365
366 D("[ usb_thread - registering device ]\n");
367 register_usb_transport(usb, 0, 0, 1);
368 }
369
370 // never gets here
371 return 0;
372}
373
Elliott Hughes2acec912015-04-16 14:12:50 -0700374static int bulk_write(int bulk_in, const uint8_t* buf, size_t length)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100375{
376 size_t count = 0;
377 int ret;
378
379 do {
380 ret = adb_write(bulk_in, buf + count, length - count);
381 if (ret < 0) {
382 if (errno != EINTR)
383 return ret;
384 } else {
385 count += ret;
386 }
387 } while (count < length);
388
389 D("[ bulk_write done fd=%d ]\n", bulk_in);
390 return count;
391}
392
Elliott Hughes2acec912015-04-16 14:12:50 -0700393static int usb_ffs_write(usb_handle* h, const void* data, int len)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100394{
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100395 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
Elliott Hughes2acec912015-04-16 14:12:50 -0700396 int n = bulk_write(h->bulk_in, reinterpret_cast<const uint8_t*>(data), len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100397 if (n != len) {
Elliott Hughes2acec912015-04-16 14:12:50 -0700398 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_in, n, strerror(errno));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100399 return -1;
400 }
401 D("[ done fd=%d ]\n", h->bulk_in);
402 return 0;
403}
404
Elliott Hughes2acec912015-04-16 14:12:50 -0700405static int bulk_read(int bulk_out, uint8_t* buf, size_t length)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100406{
407 size_t count = 0;
408 int ret;
409
410 do {
411 ret = adb_read(bulk_out, buf + count, length - count);
412 if (ret < 0) {
413 if (errno != EINTR) {
Elliott Hughesccecf142014-01-16 10:53:11 -0800414 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n",
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100415 bulk_out, length, count);
416 return ret;
417 }
418 } else {
419 count += ret;
420 }
421 } while (count < length);
422
423 return count;
424}
425
Elliott Hughes2acec912015-04-16 14:12:50 -0700426static int usb_ffs_read(usb_handle* h, void* data, int len)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100427{
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100428 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
Elliott Hughes2acec912015-04-16 14:12:50 -0700429 int n = bulk_read(h->bulk_out, reinterpret_cast<uint8_t*>(data), len);
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100430 if (n != len) {
Elliott Hughes2acec912015-04-16 14:12:50 -0700431 D("ERROR: fd = %d, n = %d: %s\n", h->bulk_out, n, strerror(errno));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100432 return -1;
433 }
434 D("[ done fd=%d ]\n", h->bulk_out);
435 return 0;
436}
437
438static void usb_ffs_kick(usb_handle *h)
439{
440 int err;
441
442 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
443 if (err < 0)
444 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
445
446 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
447 if (err < 0)
448 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
449
450 adb_mutex_lock(&h->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800451
452 // don't close ep0 here, since we may not need to reinitialize it with
453 // the same descriptors again. if however ep1/ep2 fail to re-open in
454 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100455 adb_close(h->bulk_out);
456 adb_close(h->bulk_in);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800457 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100458
459 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 adb_cond_signal(&h->notify);
461 adb_mutex_unlock(&h->lock);
462}
463
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100464static void usb_ffs_init()
465{
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100466 D("[ usb_init - using FunctionFS ]\n");
467
Elliott Hughes2acec912015-04-16 14:12:50 -0700468 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100469 h->write = usb_ffs_write;
470 h->read = usb_ffs_read;
471 h->kick = usb_ffs_kick;
Elliott Hughes2acec912015-04-16 14:12:50 -0700472 h->control = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100473 h->bulk_out = -1;
474 h->bulk_out = -1;
475
476 adb_cond_init(&h->notify, 0);
477 adb_mutex_init(&h->lock, 0);
478
479 D("[ usb_init - starting thread ]\n");
Elliott Hughes2acec912015-04-16 14:12:50 -0700480 adb_thread_t tid;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100481 if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
482 fatal_errno("[ cannot create usb thread ]\n");
483 }
484}
485
486void usb_init()
487{
488 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
489 usb_ffs_init();
490 else
491 usb_adb_init();
492}
493
494void usb_cleanup()
495{
496}
497
498int 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}