blob: ee6b37c7190e93c8500d7fbb2c944b395090963d [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010022#include <linux/usb/ch9.h>
23#include <linux/usb/functionfs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <sys/ioctl.h>
25#include <sys/types.h>
26#include <dirent.h>
27#include <errno.h>
28
29#include "sysdeps.h"
30
31#define TRACE_TAG TRACE_USB
32#include "adb.h"
33
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010034#define MAX_PACKET_SIZE_FS 64
35#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +080036#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010037
38#define cpu_to_le16(x) htole16(x)
39#define cpu_to_le32(x) htole32(x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
41struct usb_handle
42{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043 adb_cond_t notify;
44 adb_mutex_t lock;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010045
46 int (*write)(usb_handle *h, const void *data, int len);
47 int (*read)(usb_handle *h, void *data, int len);
48 void (*kick)(usb_handle *h);
49
50 // Legacy f_adb
51 int fd;
52
53 // FunctionFS
54 int control;
55 int bulk_out; /* "out" from the host's perspective => source for adbd */
56 int bulk_in; /* "in" from the host's perspective => sink for adbd */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057};
58
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070059struct func_desc {
60 struct usb_interface_descriptor intf;
61 struct usb_endpoint_descriptor_no_audio source;
62 struct usb_endpoint_descriptor_no_audio sink;
63} __attribute__((packed));
64
65struct desc_v1 {
66 struct usb_functionfs_descs_head_v1 {
67 __le32 magic;
68 __le32 length;
69 __le32 fs_count;
70 __le32 hs_count;
71 } __attribute__((packed)) header;
72 struct func_desc fs_descs, hs_descs;
73} __attribute__((packed));
74
75struct desc_v2 {
Christopher Ferrisc49f51c2015-01-23 17:09:56 -080076 struct usb_functionfs_descs_head_v2 header;
77 // The rest of the structure depends on the flags in the header.
78 __le32 fs_count;
79 __le32 hs_count;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070080 struct func_desc fs_descs, hs_descs;
81} __attribute__((packed));
82
83struct func_desc fs_descriptors = {
84 .intf = {
85 .bLength = sizeof(fs_descriptors.intf),
86 .bDescriptorType = USB_DT_INTERFACE,
87 .bInterfaceNumber = 0,
88 .bNumEndpoints = 2,
89 .bInterfaceClass = ADB_CLASS,
90 .bInterfaceSubClass = ADB_SUBCLASS,
91 .bInterfaceProtocol = ADB_PROTOCOL,
92 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010093 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070094 .source = {
95 .bLength = sizeof(fs_descriptors.source),
96 .bDescriptorType = USB_DT_ENDPOINT,
97 .bEndpointAddress = 1 | USB_DIR_OUT,
98 .bmAttributes = USB_ENDPOINT_XFER_BULK,
99 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100100 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700101 .sink = {
102 .bLength = sizeof(fs_descriptors.sink),
103 .bDescriptorType = USB_DT_ENDPOINT,
104 .bEndpointAddress = 2 | USB_DIR_IN,
105 .bmAttributes = USB_ENDPOINT_XFER_BULK,
106 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
107 },
108};
109
110struct func_desc hs_descriptors = {
111 .intf = {
112 .bLength = sizeof(hs_descriptors.intf),
113 .bDescriptorType = USB_DT_INTERFACE,
114 .bInterfaceNumber = 0,
115 .bNumEndpoints = 2,
116 .bInterfaceClass = ADB_CLASS,
117 .bInterfaceSubClass = ADB_SUBCLASS,
118 .bInterfaceProtocol = ADB_PROTOCOL,
119 .iInterface = 1, /* first string from the provided table */
120 },
121 .source = {
122 .bLength = sizeof(hs_descriptors.source),
123 .bDescriptorType = USB_DT_ENDPOINT,
124 .bEndpointAddress = 1 | USB_DIR_OUT,
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
126 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
127 },
128 .sink = {
129 .bLength = sizeof(hs_descriptors.sink),
130 .bDescriptorType = USB_DT_ENDPOINT,
131 .bEndpointAddress = 2 | USB_DIR_IN,
132 .bmAttributes = USB_ENDPOINT_XFER_BULK,
133 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800134 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100135};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100137#define STR_INTERFACE_ "ADB Interface"
138
139static const struct {
140 struct usb_functionfs_strings_head header;
141 struct {
142 __le16 code;
143 const char str1[sizeof(STR_INTERFACE_)];
144 } __attribute__((packed)) lang0;
145} __attribute__((packed)) strings = {
146 .header = {
147 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
148 .length = cpu_to_le32(sizeof(strings)),
149 .str_count = cpu_to_le32(1),
150 .lang_count = cpu_to_le32(1),
151 },
152 .lang0 = {
153 cpu_to_le16(0x0409), /* en-us */
154 STR_INTERFACE_,
155 },
156};
157
158
159
160static void *usb_adb_open_thread(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161{
162 struct usb_handle *usb = (struct usb_handle *)x;
163 int fd;
164
165 while (1) {
166 // wait until the USB device needs opening
167 adb_mutex_lock(&usb->lock);
168 while (usb->fd != -1)
169 adb_cond_wait(&usb->notify, &usb->lock);
170 adb_mutex_unlock(&usb->lock);
171
172 D("[ usb_thread - opening device ]\n");
173 do {
174 /* XXX use inotify? */
175 fd = unix_open("/dev/android_adb", O_RDWR);
176 if (fd < 0) {
177 // to support older kernels
178 fd = unix_open("/dev/android", O_RDWR);
179 }
180 if (fd < 0) {
181 adb_sleep_ms(1000);
182 }
183 } while (fd < 0);
184 D("[ opening device succeeded ]\n");
185
186 close_on_exec(fd);
187 usb->fd = fd;
188
189 D("[ usb_thread - registering device ]\n");
Scott Andersone109d262012-04-20 11:21:14 -0700190 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 }
192
193 // never gets here
194 return 0;
195}
196
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100197static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198{
199 int n;
200
JP Abgrall408fa572011-03-16 15:57:42 -0700201 D("about to write (fd=%d, len=%d)\n", h->fd, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 n = adb_write(h->fd, data, len);
203 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700204 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
205 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return -1;
207 }
JP Abgrall408fa572011-03-16 15:57:42 -0700208 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 return 0;
210}
211
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100212static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213{
214 int n;
215
JP Abgrall408fa572011-03-16 15:57:42 -0700216 D("about to read (fd=%d, len=%d)\n", h->fd, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 n = adb_read(h->fd, data, len);
218 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700219 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
220 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 return -1;
222 }
JP Abgrall408fa572011-03-16 15:57:42 -0700223 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 return 0;
225}
226
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100227static void usb_adb_kick(usb_handle *h)
228{
229 D("usb_kick\n");
230 adb_mutex_lock(&h->lock);
231 adb_close(h->fd);
232 h->fd = -1;
233
234 // notify usb_adb_open_thread that we are disconnected
235 adb_cond_signal(&h->notify);
236 adb_mutex_unlock(&h->lock);
237}
238
239static void usb_adb_init()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240{
241 usb_handle *h;
242 adb_thread_t tid;
243 int fd;
244
245 h = calloc(1, sizeof(usb_handle));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100246
247 h->write = usb_adb_write;
248 h->read = usb_adb_read;
249 h->kick = usb_adb_kick;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 h->fd = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100251
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 adb_cond_init(&h->notify, 0);
253 adb_mutex_init(&h->lock, 0);
254
255 // Open the file /dev/android_adb_enable to trigger
256 // the enabling of the adb USB function in the kernel.
257 // We never touch this file again - just leave it open
258 // indefinitely so the kernel will know when we are running
259 // and when we are not.
260 fd = unix_open("/dev/android_adb_enable", O_RDWR);
261 if (fd < 0) {
262 D("failed to open /dev/android_adb_enable\n");
263 } else {
264 close_on_exec(fd);
265 }
266
267 D("[ usb_init - starting thread ]\n");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100268 if(adb_thread_create(&tid, usb_adb_open_thread, h)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 fatal_errno("cannot create usb thread");
270 }
271}
272
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100274static void init_functionfs(struct usb_handle *h)
275{
276 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700277 struct desc_v1 v1_descriptor;
278 struct desc_v2 v2_descriptor;
279
280 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
281 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
282 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Christopher Ferrisc49f51c2015-01-23 17:09:56 -0800283 v2_descriptor.fs_count = 3;
284 v2_descriptor.hs_count = 3;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700285 v2_descriptor.fs_descs = fs_descriptors;
286 v2_descriptor.hs_descs = hs_descriptors;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100287
Jack Pham4cbf1d82013-12-23 17:46:10 -0800288 if (h->control < 0) { // might have already done this before
289 D("OPENING %s\n", USB_FFS_ADB_EP0);
290 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
291 if (h->control < 0) {
292 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
293 goto err;
294 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100295
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700296 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham4cbf1d82013-12-23 17:46:10 -0800297 if (ret < 0) {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700298 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
299 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
300 v1_descriptor.header.fs_count = 3;
301 v1_descriptor.header.hs_count = 3;
302 v1_descriptor.fs_descs = fs_descriptors;
303 v1_descriptor.hs_descs = hs_descriptors;
304 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
305 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
306 if (ret < 0) {
307 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
308 goto err;
309 }
Jack Pham4cbf1d82013-12-23 17:46:10 -0800310 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100311
Jack Pham4cbf1d82013-12-23 17:46:10 -0800312 ret = adb_write(h->control, &strings, sizeof(strings));
313 if (ret < 0) {
314 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
315 goto err;
316 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100317 }
318
319 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
320 if (h->bulk_out < 0) {
321 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
322 goto err;
323 }
324
325 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
326 if (h->bulk_in < 0) {
327 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
328 goto err;
329 }
330
331 return;
332
333err:
334 if (h->bulk_in > 0) {
335 adb_close(h->bulk_in);
336 h->bulk_in = -1;
337 }
338 if (h->bulk_out > 0) {
339 adb_close(h->bulk_out);
340 h->bulk_out = -1;
341 }
342 if (h->control > 0) {
343 adb_close(h->control);
344 h->control = -1;
345 }
346 return;
347}
348
349static void *usb_ffs_open_thread(void *x)
350{
351 struct usb_handle *usb = (struct usb_handle *)x;
352
353 while (1) {
354 // wait until the USB device needs opening
355 adb_mutex_lock(&usb->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800356 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100357 adb_cond_wait(&usb->notify, &usb->lock);
358 adb_mutex_unlock(&usb->lock);
359
360 while (1) {
361 init_functionfs(usb);
362
Jack Pham4cbf1d82013-12-23 17:46:10 -0800363 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100364 break;
365
366 adb_sleep_ms(1000);
367 }
368
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
377static int bulk_write(int bulk_in, const char *buf, size_t length)
378{
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
396static int usb_ffs_write(usb_handle *h, const void *data, int len)
397{
398 int n;
399
400 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
401 n = bulk_write(h->bulk_in, data, len);
402 if (n != len) {
403 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
404 h->bulk_in, n, errno, strerror(errno));
405 return -1;
406 }
407 D("[ done fd=%d ]\n", h->bulk_in);
408 return 0;
409}
410
411static int bulk_read(int bulk_out, char *buf, size_t length)
412{
413 size_t count = 0;
414 int ret;
415
416 do {
417 ret = adb_read(bulk_out, buf + count, length - count);
418 if (ret < 0) {
419 if (errno != EINTR) {
Elliott Hughesccecf142014-01-16 10:53:11 -0800420 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n",
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100421 bulk_out, length, count);
422 return ret;
423 }
424 } else {
425 count += ret;
426 }
427 } while (count < length);
428
429 return count;
430}
431
432static int usb_ffs_read(usb_handle *h, void *data, int len)
433{
434 int n;
435
436 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
437 n = bulk_read(h->bulk_out, data, len);
438 if (n != len) {
439 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
440 h->bulk_out, n, errno, strerror(errno));
441 return -1;
442 }
443 D("[ done fd=%d ]\n", h->bulk_out);
444 return 0;
445}
446
447static void usb_ffs_kick(usb_handle *h)
448{
449 int err;
450
451 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
452 if (err < 0)
453 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
454
455 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
456 if (err < 0)
457 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
458
459 adb_mutex_lock(&h->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800460
461 // don't close ep0 here, since we may not need to reinitialize it with
462 // the same descriptors again. if however ep1/ep2 fail to re-open in
463 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100464 adb_close(h->bulk_out);
465 adb_close(h->bulk_in);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800466 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100467
468 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 adb_cond_signal(&h->notify);
470 adb_mutex_unlock(&h->lock);
471}
472
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100473static void usb_ffs_init()
474{
475 usb_handle *h;
476 adb_thread_t tid;
477
478 D("[ usb_init - using FunctionFS ]\n");
479
480 h = calloc(1, sizeof(usb_handle));
481
482 h->write = usb_ffs_write;
483 h->read = usb_ffs_read;
484 h->kick = usb_ffs_kick;
485
486 h->control = -1;
487 h->bulk_out = -1;
488 h->bulk_out = -1;
489
490 adb_cond_init(&h->notify, 0);
491 adb_mutex_init(&h->lock, 0);
492
493 D("[ usb_init - starting thread ]\n");
494 if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
495 fatal_errno("[ cannot create usb thread ]\n");
496 }
497}
498
499void usb_init()
500{
501 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
502 usb_ffs_init();
503 else
504 usb_adb_init();
505}
506
507void usb_cleanup()
508{
509}
510
511int usb_write(usb_handle *h, const void *data, int len)
512{
513 return h->write(h, data, len);
514}
515
516int usb_read(usb_handle *h, void *data, int len)
517{
518 return h->read(h, data, len);
519}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520int usb_close(usb_handle *h)
521{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 return 0;
523}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100524
525void usb_kick(usb_handle *h)
526{
527 h->kick(h);
528}