blob: fd566f48e5240a372c680acb62d47720dede10a9 [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 {
76 struct usb_functionfs_descs_head_v2 {
77 __le32 magic;
78 __le32 length;
79 __le32 flags;
80 __le32 fs_count;
81 __le32 hs_count;
82 __le32 ss_count;
83 } __attribute__((packed)) header;
84 struct func_desc fs_descs, hs_descs;
85} __attribute__((packed));
86
87struct func_desc fs_descriptors = {
88 .intf = {
89 .bLength = sizeof(fs_descriptors.intf),
90 .bDescriptorType = USB_DT_INTERFACE,
91 .bInterfaceNumber = 0,
92 .bNumEndpoints = 2,
93 .bInterfaceClass = ADB_CLASS,
94 .bInterfaceSubClass = ADB_SUBCLASS,
95 .bInterfaceProtocol = ADB_PROTOCOL,
96 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +010097 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -070098 .source = {
99 .bLength = sizeof(fs_descriptors.source),
100 .bDescriptorType = USB_DT_ENDPOINT,
101 .bEndpointAddress = 1 | USB_DIR_OUT,
102 .bmAttributes = USB_ENDPOINT_XFER_BULK,
103 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100104 },
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700105 .sink = {
106 .bLength = sizeof(fs_descriptors.sink),
107 .bDescriptorType = USB_DT_ENDPOINT,
108 .bEndpointAddress = 2 | USB_DIR_IN,
109 .bmAttributes = USB_ENDPOINT_XFER_BULK,
110 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
111 },
112};
113
114struct func_desc hs_descriptors = {
115 .intf = {
116 .bLength = sizeof(hs_descriptors.intf),
117 .bDescriptorType = USB_DT_INTERFACE,
118 .bInterfaceNumber = 0,
119 .bNumEndpoints = 2,
120 .bInterfaceClass = ADB_CLASS,
121 .bInterfaceSubClass = ADB_SUBCLASS,
122 .bInterfaceProtocol = ADB_PROTOCOL,
123 .iInterface = 1, /* first string from the provided table */
124 },
125 .source = {
126 .bLength = sizeof(hs_descriptors.source),
127 .bDescriptorType = USB_DT_ENDPOINT,
128 .bEndpointAddress = 1 | USB_DIR_OUT,
129 .bmAttributes = USB_ENDPOINT_XFER_BULK,
130 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
131 },
132 .sink = {
133 .bLength = sizeof(hs_descriptors.sink),
134 .bDescriptorType = USB_DT_ENDPOINT,
135 .bEndpointAddress = 2 | USB_DIR_IN,
136 .bmAttributes = USB_ENDPOINT_XFER_BULK,
137 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Cand6ee9f22014-09-02 13:04:44 +0800138 },
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100139};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100141#define STR_INTERFACE_ "ADB Interface"
142
143static const struct {
144 struct usb_functionfs_strings_head header;
145 struct {
146 __le16 code;
147 const char str1[sizeof(STR_INTERFACE_)];
148 } __attribute__((packed)) lang0;
149} __attribute__((packed)) strings = {
150 .header = {
151 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
152 .length = cpu_to_le32(sizeof(strings)),
153 .str_count = cpu_to_le32(1),
154 .lang_count = cpu_to_le32(1),
155 },
156 .lang0 = {
157 cpu_to_le16(0x0409), /* en-us */
158 STR_INTERFACE_,
159 },
160};
161
162
163
164static void *usb_adb_open_thread(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165{
166 struct usb_handle *usb = (struct usb_handle *)x;
167 int fd;
168
169 while (1) {
170 // wait until the USB device needs opening
171 adb_mutex_lock(&usb->lock);
172 while (usb->fd != -1)
173 adb_cond_wait(&usb->notify, &usb->lock);
174 adb_mutex_unlock(&usb->lock);
175
176 D("[ usb_thread - opening device ]\n");
177 do {
178 /* XXX use inotify? */
179 fd = unix_open("/dev/android_adb", O_RDWR);
180 if (fd < 0) {
181 // to support older kernels
182 fd = unix_open("/dev/android", O_RDWR);
183 }
184 if (fd < 0) {
185 adb_sleep_ms(1000);
186 }
187 } while (fd < 0);
188 D("[ opening device succeeded ]\n");
189
190 close_on_exec(fd);
191 usb->fd = fd;
192
193 D("[ usb_thread - registering device ]\n");
Scott Andersone109d262012-04-20 11:21:14 -0700194 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 }
196
197 // never gets here
198 return 0;
199}
200
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100201static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202{
203 int n;
204
JP Abgrall408fa572011-03-16 15:57:42 -0700205 D("about to write (fd=%d, len=%d)\n", h->fd, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 n = adb_write(h->fd, data, len);
207 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700208 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
209 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 return -1;
211 }
JP Abgrall408fa572011-03-16 15:57:42 -0700212 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 return 0;
214}
215
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100216static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217{
218 int n;
219
JP Abgrall408fa572011-03-16 15:57:42 -0700220 D("about to read (fd=%d, len=%d)\n", h->fd, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 n = adb_read(h->fd, data, len);
222 if(n != len) {
JP Abgrall408fa572011-03-16 15:57:42 -0700223 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
224 h->fd, n, errno, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 return -1;
226 }
JP Abgrall408fa572011-03-16 15:57:42 -0700227 D("[ done fd=%d ]\n", h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 return 0;
229}
230
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100231static void usb_adb_kick(usb_handle *h)
232{
233 D("usb_kick\n");
234 adb_mutex_lock(&h->lock);
235 adb_close(h->fd);
236 h->fd = -1;
237
238 // notify usb_adb_open_thread that we are disconnected
239 adb_cond_signal(&h->notify);
240 adb_mutex_unlock(&h->lock);
241}
242
243static void usb_adb_init()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244{
245 usb_handle *h;
246 adb_thread_t tid;
247 int fd;
248
249 h = calloc(1, sizeof(usb_handle));
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100250
251 h->write = usb_adb_write;
252 h->read = usb_adb_read;
253 h->kick = usb_adb_kick;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 h->fd = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100255
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 adb_cond_init(&h->notify, 0);
257 adb_mutex_init(&h->lock, 0);
258
259 // Open the file /dev/android_adb_enable to trigger
260 // the enabling of the adb USB function in the kernel.
261 // We never touch this file again - just leave it open
262 // indefinitely so the kernel will know when we are running
263 // and when we are not.
264 fd = unix_open("/dev/android_adb_enable", O_RDWR);
265 if (fd < 0) {
266 D("failed to open /dev/android_adb_enable\n");
267 } else {
268 close_on_exec(fd);
269 }
270
271 D("[ usb_init - starting thread ]\n");
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100272 if(adb_thread_create(&tid, usb_adb_open_thread, h)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273 fatal_errno("cannot create usb thread");
274 }
275}
276
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100278static void init_functionfs(struct usb_handle *h)
279{
280 ssize_t ret;
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700281 struct desc_v1 v1_descriptor;
282 struct desc_v2 v2_descriptor;
283
284 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
285 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
286 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
287 v2_descriptor.header.fs_count = 3;
288 v2_descriptor.header.hs_count = 3;
289 v2_descriptor.header.ss_count = 0;
290 v2_descriptor.fs_descs = fs_descriptors;
291 v2_descriptor.hs_descs = hs_descriptors;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100292
Jack Pham4cbf1d82013-12-23 17:46:10 -0800293 if (h->control < 0) { // might have already done this before
294 D("OPENING %s\n", USB_FFS_ADB_EP0);
295 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
296 if (h->control < 0) {
297 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
298 goto err;
299 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100300
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700301 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham4cbf1d82013-12-23 17:46:10 -0800302 if (ret < 0) {
Badhri Jagan Sridharanab3446d2014-10-27 18:26:17 -0700303 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
304 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
305 v1_descriptor.header.fs_count = 3;
306 v1_descriptor.header.hs_count = 3;
307 v1_descriptor.fs_descs = fs_descriptors;
308 v1_descriptor.hs_descs = hs_descriptors;
309 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
310 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
311 if (ret < 0) {
312 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
313 goto err;
314 }
Jack Pham4cbf1d82013-12-23 17:46:10 -0800315 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100316
Jack Pham4cbf1d82013-12-23 17:46:10 -0800317 ret = adb_write(h->control, &strings, sizeof(strings));
318 if (ret < 0) {
319 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
320 goto err;
321 }
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100322 }
323
324 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
325 if (h->bulk_out < 0) {
326 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
327 goto err;
328 }
329
330 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
331 if (h->bulk_in < 0) {
332 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
333 goto err;
334 }
335
336 return;
337
338err:
339 if (h->bulk_in > 0) {
340 adb_close(h->bulk_in);
341 h->bulk_in = -1;
342 }
343 if (h->bulk_out > 0) {
344 adb_close(h->bulk_out);
345 h->bulk_out = -1;
346 }
347 if (h->control > 0) {
348 adb_close(h->control);
349 h->control = -1;
350 }
351 return;
352}
353
354static void *usb_ffs_open_thread(void *x)
355{
356 struct usb_handle *usb = (struct usb_handle *)x;
357
358 while (1) {
359 // wait until the USB device needs opening
360 adb_mutex_lock(&usb->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800361 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100362 adb_cond_wait(&usb->notify, &usb->lock);
363 adb_mutex_unlock(&usb->lock);
364
365 while (1) {
366 init_functionfs(usb);
367
Jack Pham4cbf1d82013-12-23 17:46:10 -0800368 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100369 break;
370
371 adb_sleep_ms(1000);
372 }
373
374 D("[ usb_thread - registering device ]\n");
375 register_usb_transport(usb, 0, 0, 1);
376 }
377
378 // never gets here
379 return 0;
380}
381
382static int bulk_write(int bulk_in, const char *buf, size_t length)
383{
384 size_t count = 0;
385 int ret;
386
387 do {
388 ret = adb_write(bulk_in, buf + count, length - count);
389 if (ret < 0) {
390 if (errno != EINTR)
391 return ret;
392 } else {
393 count += ret;
394 }
395 } while (count < length);
396
397 D("[ bulk_write done fd=%d ]\n", bulk_in);
398 return count;
399}
400
401static int usb_ffs_write(usb_handle *h, const void *data, int len)
402{
403 int n;
404
405 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
406 n = bulk_write(h->bulk_in, data, len);
407 if (n != len) {
408 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
409 h->bulk_in, n, errno, strerror(errno));
410 return -1;
411 }
412 D("[ done fd=%d ]\n", h->bulk_in);
413 return 0;
414}
415
416static int bulk_read(int bulk_out, char *buf, size_t length)
417{
418 size_t count = 0;
419 int ret;
420
421 do {
422 ret = adb_read(bulk_out, buf + count, length - count);
423 if (ret < 0) {
424 if (errno != EINTR) {
Elliott Hughesccecf142014-01-16 10:53:11 -0800425 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n",
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100426 bulk_out, length, count);
427 return ret;
428 }
429 } else {
430 count += ret;
431 }
432 } while (count < length);
433
434 return count;
435}
436
437static int usb_ffs_read(usb_handle *h, void *data, int len)
438{
439 int n;
440
441 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
442 n = bulk_read(h->bulk_out, data, len);
443 if (n != len) {
444 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
445 h->bulk_out, n, errno, strerror(errno));
446 return -1;
447 }
448 D("[ done fd=%d ]\n", h->bulk_out);
449 return 0;
450}
451
452static void usb_ffs_kick(usb_handle *h)
453{
454 int err;
455
456 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
457 if (err < 0)
458 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
459
460 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
461 if (err < 0)
462 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
463
464 adb_mutex_lock(&h->lock);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800465
466 // don't close ep0 here, since we may not need to reinitialize it with
467 // the same descriptors again. if however ep1/ep2 fail to re-open in
468 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100469 adb_close(h->bulk_out);
470 adb_close(h->bulk_in);
Jack Pham4cbf1d82013-12-23 17:46:10 -0800471 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100472
473 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 adb_cond_signal(&h->notify);
475 adb_mutex_unlock(&h->lock);
476}
477
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100478static void usb_ffs_init()
479{
480 usb_handle *h;
481 adb_thread_t tid;
482
483 D("[ usb_init - using FunctionFS ]\n");
484
485 h = calloc(1, sizeof(usb_handle));
486
487 h->write = usb_ffs_write;
488 h->read = usb_ffs_read;
489 h->kick = usb_ffs_kick;
490
491 h->control = -1;
492 h->bulk_out = -1;
493 h->bulk_out = -1;
494
495 adb_cond_init(&h->notify, 0);
496 adb_mutex_init(&h->lock, 0);
497
498 D("[ usb_init - starting thread ]\n");
499 if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
500 fatal_errno("[ cannot create usb thread ]\n");
501 }
502}
503
504void usb_init()
505{
506 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
507 usb_ffs_init();
508 else
509 usb_adb_init();
510}
511
512void usb_cleanup()
513{
514}
515
516int usb_write(usb_handle *h, const void *data, int len)
517{
518 return h->write(h, data, len);
519}
520
521int usb_read(usb_handle *h, void *data, int len)
522{
523 return h->read(h, data, len);
524}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525int usb_close(usb_handle *h)
526{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 return 0;
528}
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100529
530void usb_kick(usb_handle *h)
531{
532 h->kick(h);
533}