blob: b3a38dce5497cef034b801f1f892048b04b264be [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -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
Da Zhou65ad9282014-01-17 01:04:07 -080022#include <linux/usb/ch9.h>
23#include <linux/usb/functionfs.h>
Doug Zongker9270a202012-01-09 15:16:13 -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
Da Zhou65ad9282014-01-17 01:04:07 -080034#define MAX_PACKET_SIZE_FS 64
35#define MAX_PACKET_SIZE_HS 512
36
37#define cpu_to_le16(x) htole16(x)
38#define cpu_to_le32(x) htole32(x)
Doug Zongker9270a202012-01-09 15:16:13 -080039
40struct usb_handle
41{
42 int fd;
43 adb_cond_t notify;
44 adb_mutex_t lock;
Da Zhou65ad9282014-01-17 01:04:07 -080045
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 int control;
51 int bulk_out; /* "out" from the host's perspective => source for adbd */
52 int bulk_in; /* "in" from the host's perspective => sink for adbd */
53};
54
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -070055struct func_desc {
56 struct usb_interface_descriptor intf;
57 struct usb_endpoint_descriptor_no_audio source;
58 struct usb_endpoint_descriptor_no_audio sink;
59} __attribute__((packed));
60
61struct desc_v1 {
62 struct usb_functionfs_descs_head_v1 {
63 __le32 magic;
64 __le32 length;
65 __le32 fs_count;
66 __le32 hs_count;
67 } __attribute__((packed)) header;
68 struct func_desc fs_descs, hs_descs;
69} __attribute__((packed));
70
71struct desc_v2 {
Christopher Ferris3ed8ef02015-01-26 12:53:20 -080072 struct usb_functionfs_descs_head_v2 header;
73 // The rest of the structure depends on the flags in the header.
74 __le32 fs_count;
75 __le32 hs_count;
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -070076 struct func_desc fs_descs, hs_descs;
77} __attribute__((packed));
78
79/*static const struct {
Da Zhou65ad9282014-01-17 01:04:07 -080080 struct usb_functionfs_descs_head header;
81 struct {
82 struct usb_interface_descriptor intf;
83 struct usb_endpoint_descriptor_no_audio source;
84 struct usb_endpoint_descriptor_no_audio sink;
85 } __attribute__((packed)) fs_descs, hs_descs;
86} __attribute__((packed)) descriptors = {
87 .header = {
88 .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
89 .length = cpu_to_le32(sizeof(descriptors)),
90 .fs_count = 3,
91 .hs_count = 3,
92 },
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -070093
94*/
95
96struct func_desc fs_descriptors = {
97 .intf = {
98 .bLength = sizeof(fs_descriptors.intf),
99 .bDescriptorType = USB_DT_INTERFACE,
100 .bInterfaceNumber = 0,
101 .bNumEndpoints = 2,
102 .bInterfaceClass = ADB_CLASS,
103 .bInterfaceSubClass = ADB_SUBCLASS,
104 .bInterfaceProtocol = ADB_PROTOCOL,
105 .iInterface = 1, /* first string from the provided table */
Da Zhou65ad9282014-01-17 01:04:07 -0800106 },
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -0700107 .source = {
108 .bLength = sizeof(fs_descriptors.source),
109 .bDescriptorType = USB_DT_ENDPOINT,
110 .bEndpointAddress = 1 | USB_DIR_OUT,
111 .bmAttributes = USB_ENDPOINT_XFER_BULK,
112 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
113 },
114 .sink = {
115 .bLength = sizeof(fs_descriptors.sink),
116 .bDescriptorType = USB_DT_ENDPOINT,
117 .bEndpointAddress = 2 | USB_DIR_IN,
118 .bmAttributes = USB_ENDPOINT_XFER_BULK,
119 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
120 },
121};
122
123struct func_desc hs_descriptors = {
124 .intf = {
125 .bLength = sizeof(hs_descriptors.intf),
126 .bDescriptorType = USB_DT_INTERFACE,
127 .bInterfaceNumber = 0,
128 .bNumEndpoints = 2,
129 .bInterfaceClass = ADB_CLASS,
130 .bInterfaceSubClass = ADB_SUBCLASS,
131 .bInterfaceProtocol = ADB_PROTOCOL,
132 .iInterface = 1, /* first string from the provided table */
133 },
134 .source = {
135 .bLength = sizeof(hs_descriptors.source),
136 .bDescriptorType = USB_DT_ENDPOINT,
137 .bEndpointAddress = 1 | USB_DIR_OUT,
138 .bmAttributes = USB_ENDPOINT_XFER_BULK,
139 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
140 },
141 .sink = {
142 .bLength = sizeof(hs_descriptors.sink),
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = 2 | USB_DIR_IN,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Da Zhou65ad9282014-01-17 01:04:07 -0800147 },
148};
149
150#define STR_INTERFACE_ "ADB Interface"
151
152static const struct {
153 struct usb_functionfs_strings_head header;
154 struct {
155 __le16 code;
156 const char str1[sizeof(STR_INTERFACE_)];
157 } __attribute__((packed)) lang0;
158} __attribute__((packed)) strings = {
159 .header = {
160 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
161 .length = cpu_to_le32(sizeof(strings)),
162 .str_count = cpu_to_le32(1),
163 .lang_count = cpu_to_le32(1),
164 },
165 .lang0 = {
166 cpu_to_le16(0x0409), /* en-us */
167 STR_INTERFACE_,
168 },
Doug Zongker9270a202012-01-09 15:16:13 -0800169};
170
171void usb_cleanup()
172{
173 // nothing to do here
174}
175
Da Zhou65ad9282014-01-17 01:04:07 -0800176static void *usb_adb_open_thread(void *x)
Doug Zongker9270a202012-01-09 15:16:13 -0800177{
178 struct usb_handle *usb = (struct usb_handle *)x;
179 int fd;
180
181 while (1) {
182 // wait until the USB device needs opening
183 adb_mutex_lock(&usb->lock);
184 while (usb->fd != -1)
185 adb_cond_wait(&usb->notify, &usb->lock);
186 adb_mutex_unlock(&usb->lock);
187
188 D("[ usb_thread - opening device ]\n");
189 do {
190 /* XXX use inotify? */
191 fd = unix_open("/dev/android_adb", O_RDWR);
192 if (fd < 0) {
193 // to support older kernels
194 fd = unix_open("/dev/android", O_RDWR);
Da Zhou65ad9282014-01-17 01:04:07 -0800195 fprintf(stderr, "usb_adb_open_thread: %d\n", fd );
Doug Zongker9270a202012-01-09 15:16:13 -0800196 }
197 if (fd < 0) {
198 adb_sleep_ms(1000);
199 }
200 } while (fd < 0);
201 D("[ opening device succeeded ]\n");
202
203 close_on_exec(fd);
204 usb->fd = fd;
205
206 D("[ usb_thread - registering device ]\n");
207 register_usb_transport(usb, 0, 1);
208 }
209
210 // never gets here
211 return 0;
212}
213
Da Zhou65ad9282014-01-17 01:04:07 -0800214static int usb_adb_write(usb_handle *h, const void *data, int len)
Doug Zongker9270a202012-01-09 15:16:13 -0800215{
216 int n;
217
218 D("about to write (fd=%d, len=%d)\n", h->fd, len);
219 n = adb_write(h->fd, data, len);
220 if(n != len) {
221 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
222 h->fd, n, errno, strerror(errno));
223 return -1;
224 }
225 D("[ done fd=%d ]\n", h->fd);
226 return 0;
227}
228
Da Zhou65ad9282014-01-17 01:04:07 -0800229static int usb_adb_read(usb_handle *h, void *data, int len)
Doug Zongker9270a202012-01-09 15:16:13 -0800230{
231 int n;
232
233 D("about to read (fd=%d, len=%d)\n", h->fd, len);
234 n = adb_read(h->fd, data, len);
235 if(n != len) {
236 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
237 h->fd, n, errno, strerror(errno));
238 return -1;
239 }
240 D("[ done fd=%d ]\n", h->fd);
241 return 0;
242}
243
Da Zhou65ad9282014-01-17 01:04:07 -0800244static void usb_adb_kick(usb_handle *h)
245{
246 D("usb_kick\n");
247 adb_mutex_lock(&h->lock);
248 adb_close(h->fd);
249 h->fd = -1;
250
251 // notify usb_adb_open_thread that we are disconnected
252 adb_cond_signal(&h->notify);
253 adb_mutex_unlock(&h->lock);
254}
255
256static void usb_adb_init()
Doug Zongker9270a202012-01-09 15:16:13 -0800257{
258 usb_handle *h;
259 adb_thread_t tid;
260 int fd;
261
262 h = calloc(1, sizeof(usb_handle));
Da Zhou65ad9282014-01-17 01:04:07 -0800263
264 h->write = usb_adb_write;
265 h->read = usb_adb_read;
266 h->kick = usb_adb_kick;
Doug Zongker9270a202012-01-09 15:16:13 -0800267 h->fd = -1;
Da Zhou65ad9282014-01-17 01:04:07 -0800268
Doug Zongker9270a202012-01-09 15:16:13 -0800269 adb_cond_init(&h->notify, 0);
270 adb_mutex_init(&h->lock, 0);
271
Da Zhou65ad9282014-01-17 01:04:07 -0800272 fprintf(stderr, "Starting to open usb_init()\n");
Doug Zongker9270a202012-01-09 15:16:13 -0800273 // Open the file /dev/android_adb_enable to trigger
274 // the enabling of the adb USB function in the kernel.
275 // We never touch this file again - just leave it open
276 // indefinitely so the kernel will know when we are running
277 // and when we are not.
278 fd = unix_open("/dev/android_adb_enable", O_RDWR);
Da Zhou65ad9282014-01-17 01:04:07 -0800279 fprintf(stderr, "unix_open to open usb_init(): %d\n", fd);
Doug Zongker9270a202012-01-09 15:16:13 -0800280 if (fd < 0) {
281 D("failed to open /dev/android_adb_enable\n");
282 } else {
283 close_on_exec(fd);
284 }
285
286 D("[ usb_init - starting thread ]\n");
Da Zhou65ad9282014-01-17 01:04:07 -0800287 if(adb_thread_create(&tid, usb_adb_open_thread, h)){
Doug Zongker9270a202012-01-09 15:16:13 -0800288 fatal_errno("cannot create usb thread");
Da Zhou65ad9282014-01-17 01:04:07 -0800289 fprintf(stderr, "cannot create the usb thread()\n");
Doug Zongker9270a202012-01-09 15:16:13 -0800290 }
291}
292
Doug Zongker9270a202012-01-09 15:16:13 -0800293
Da Zhou65ad9282014-01-17 01:04:07 -0800294static void init_functionfs(struct usb_handle *h)
295{
296 ssize_t ret;
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -0700297 struct desc_v1 v1_descriptor;
298 struct desc_v2 v2_descriptor;
299
300 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
301 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
302 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Christopher Ferris3ed8ef02015-01-26 12:53:20 -0800303 v2_descriptor.fs_count = 3;
304 v2_descriptor.hs_count = 3;
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -0700305 v2_descriptor.fs_descs = fs_descriptors;
306 v2_descriptor.hs_descs = hs_descriptors;
307
Da Zhou65ad9282014-01-17 01:04:07 -0800308
309 D("OPENING %s\n", USB_FFS_ADB_EP0);
310 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
311 if (h->control < 0) {
312 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
313 goto err;
314 }
315
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -0700316 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Da Zhou65ad9282014-01-17 01:04:07 -0800317 if (ret < 0) {
Badhri Jagan Sridharan9e3cce52014-10-27 18:32:35 -0700318 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
319 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
320 v1_descriptor.header.fs_count = 3;
321 v1_descriptor.header.hs_count = 3;
322 v1_descriptor.fs_descs = fs_descriptors;
323 v1_descriptor.hs_descs = hs_descriptors;
324 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
325 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
326 if (ret < 0) {
327 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
328 goto err;
329 }
Da Zhou65ad9282014-01-17 01:04:07 -0800330 }
331
332 ret = adb_write(h->control, &strings, sizeof(strings));
333 if (ret < 0) {
334 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
335 goto err;
336 }
337
338 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
339 if (h->bulk_out < 0) {
340 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
341 goto err;
342 }
343
344 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
345 if (h->bulk_in < 0) {
346 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
347 goto err;
348 }
349
350 return;
351
352err:
353 if (h->bulk_in > 0) {
354 adb_close(h->bulk_in);
355 h->bulk_in = -1;
356 }
357 if (h->bulk_out > 0) {
358 adb_close(h->bulk_out);
359 h->bulk_out = -1;
360 }
361 if (h->control > 0) {
362 adb_close(h->control);
363 h->control = -1;
364 }
365 return;
366}
367
368static void *usb_ffs_open_thread(void *x)
369{
370 struct usb_handle *usb = (struct usb_handle *)x;
371
372 while (1) {
373 // wait until the USB device needs opening
374 adb_mutex_lock(&usb->lock);
375 while (usb->control != -1)
376 adb_cond_wait(&usb->notify, &usb->lock);
377 adb_mutex_unlock(&usb->lock);
378
379 while (1) {
380 init_functionfs(usb);
381
382 if (usb->control >= 0)
383 break;
384
385 adb_sleep_ms(1000);
386 }
387
388 D("[ usb_thread - registering device ]\n");
389 register_usb_transport(usb, 0, 1);
390 }
391
392 // never gets here
393 return 0;
394}
395
396static int bulk_write(int bulk_in, const char *buf, size_t length)
397{
398 size_t count = 0;
399 int ret;
400
401 do {
402 ret = adb_write(bulk_in, buf + count, length - count);
403 if (ret < 0) {
404 if (errno != EINTR)
405 return ret;
406 } else {
407 count += ret;
408 }
409 } while (count < length);
410
411 D("[ bulk_write done fd=%d ]\n", bulk_in);
412 return count;
413}
414
415static int usb_ffs_write(usb_handle *h, const void *data, int len)
416{
417 int n;
418
419 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
420 n = bulk_write(h->bulk_in, data, len);
421 if (n != len) {
422 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
423 h->bulk_in, n, errno, strerror(errno));
424 return -1;
425 }
426 D("[ done fd=%d ]\n", h->bulk_in);
427 return 0;
428}
429
430static int bulk_read(int bulk_out, char *buf, size_t length)
431{
432 size_t count = 0;
433 int ret;
434
435 do {
436 ret = adb_read(bulk_out, buf + count, length - count);
437 if (ret < 0) {
438 if (errno != EINTR) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700439 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n",
Da Zhou65ad9282014-01-17 01:04:07 -0800440 bulk_out, length, count);
441 return ret;
442 }
443 } else {
444 count += ret;
445 }
446 } while (count < length);
447
448 return count;
449}
450
451static int usb_ffs_read(usb_handle *h, void *data, int len)
452{
453 int n;
454
455 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
456 n = bulk_read(h->bulk_out, data, len);
457 if (n != len) {
458 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
459 h->bulk_out, n, errno, strerror(errno));
460 return -1;
461 }
462 D("[ done fd=%d ]\n", h->bulk_out);
463 return 0;
464}
465
466static void usb_ffs_kick(usb_handle *h)
467{
468 int err;
469
470 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
471 if (err < 0)
472 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
473
474 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
475 if (err < 0)
476 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
477
478 adb_mutex_lock(&h->lock);
479 adb_close(h->control);
480 adb_close(h->bulk_out);
481 adb_close(h->bulk_in);
482 h->control = h->bulk_out = h->bulk_in = -1;
483
484 // notify usb_ffs_open_thread that we are disconnected
Doug Zongker9270a202012-01-09 15:16:13 -0800485 adb_cond_signal(&h->notify);
486 adb_mutex_unlock(&h->lock);
487}
488
Da Zhou65ad9282014-01-17 01:04:07 -0800489static void usb_ffs_init()
490{
491 usb_handle *h;
492 adb_thread_t tid;
493
494 D("[ usb_init - using FunctionFS ]\n");
495
496 h = calloc(1, sizeof(usb_handle));
497
498 h->write = usb_ffs_write;
499 h->read = usb_ffs_read;
500 h->kick = usb_ffs_kick;
501
502 h->control = -1;
503 h->bulk_out = -1;
504 h->bulk_out = -1;
505
506 adb_cond_init(&h->notify, 0);
507 adb_mutex_init(&h->lock, 0);
508
509 D("[ usb_init - starting thread ]\n");
510 if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
511 fatal_errno("[ cannot create usb thread ]\n");
512 }
513}
514
515void usb_init()
516{
517 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
518 usb_ffs_init();
519 else
520 usb_adb_init();
521}
522
523int usb_write(usb_handle *h, const void *data, int len)
524{
525 return h->write(h, data, len);
526}
527
528int usb_read(usb_handle *h, void *data, int len)
529{
530 return h->read(h, data, len);
531}
Doug Zongker9270a202012-01-09 15:16:13 -0800532int usb_close(usb_handle *h)
533{
534 // nothing to do here
535 return 0;
536}
Da Zhou65ad9282014-01-17 01:04:07 -0800537
538void usb_kick(usb_handle *h)
539{
540 h->kick(h);
541}