blob: e7d3c4854de9f14d78b3c8c603aa098b72602ae5 [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 Sridharanb5b43042014-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 {
72 struct usb_functionfs_descs_head_v2 {
73 __le32 magic;
74 __le32 length;
75 __le32 flags;
76 __le32 fs_count;
77 __le32 hs_count;
78 __le32 ss_count;
79 } __attribute__((packed)) header;
80 struct func_desc fs_descs, hs_descs;
81} __attribute__((packed));
82
83/*static const struct {
Da Zhou65ad9282014-01-17 01:04:07 -080084 struct usb_functionfs_descs_head header;
85 struct {
86 struct usb_interface_descriptor intf;
87 struct usb_endpoint_descriptor_no_audio source;
88 struct usb_endpoint_descriptor_no_audio sink;
89 } __attribute__((packed)) fs_descs, hs_descs;
90} __attribute__((packed)) descriptors = {
91 .header = {
92 .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
93 .length = cpu_to_le32(sizeof(descriptors)),
94 .fs_count = 3,
95 .hs_count = 3,
96 },
Badhri Jagan Sridharanb5b43042014-10-27 18:32:35 -070097
98*/
99
100struct func_desc fs_descriptors = {
101 .intf = {
102 .bLength = sizeof(fs_descriptors.intf),
103 .bDescriptorType = USB_DT_INTERFACE,
104 .bInterfaceNumber = 0,
105 .bNumEndpoints = 2,
106 .bInterfaceClass = ADB_CLASS,
107 .bInterfaceSubClass = ADB_SUBCLASS,
108 .bInterfaceProtocol = ADB_PROTOCOL,
109 .iInterface = 1, /* first string from the provided table */
Da Zhou65ad9282014-01-17 01:04:07 -0800110 },
Badhri Jagan Sridharanb5b43042014-10-27 18:32:35 -0700111 .source = {
112 .bLength = sizeof(fs_descriptors.source),
113 .bDescriptorType = USB_DT_ENDPOINT,
114 .bEndpointAddress = 1 | USB_DIR_OUT,
115 .bmAttributes = USB_ENDPOINT_XFER_BULK,
116 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
117 },
118 .sink = {
119 .bLength = sizeof(fs_descriptors.sink),
120 .bDescriptorType = USB_DT_ENDPOINT,
121 .bEndpointAddress = 2 | USB_DIR_IN,
122 .bmAttributes = USB_ENDPOINT_XFER_BULK,
123 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
124 },
125};
126
127struct func_desc hs_descriptors = {
128 .intf = {
129 .bLength = sizeof(hs_descriptors.intf),
130 .bDescriptorType = USB_DT_INTERFACE,
131 .bInterfaceNumber = 0,
132 .bNumEndpoints = 2,
133 .bInterfaceClass = ADB_CLASS,
134 .bInterfaceSubClass = ADB_SUBCLASS,
135 .bInterfaceProtocol = ADB_PROTOCOL,
136 .iInterface = 1, /* first string from the provided table */
137 },
138 .source = {
139 .bLength = sizeof(hs_descriptors.source),
140 .bDescriptorType = USB_DT_ENDPOINT,
141 .bEndpointAddress = 1 | USB_DIR_OUT,
142 .bmAttributes = USB_ENDPOINT_XFER_BULK,
143 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
144 },
145 .sink = {
146 .bLength = sizeof(hs_descriptors.sink),
147 .bDescriptorType = USB_DT_ENDPOINT,
148 .bEndpointAddress = 2 | USB_DIR_IN,
149 .bmAttributes = USB_ENDPOINT_XFER_BULK,
150 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Da Zhou65ad9282014-01-17 01:04:07 -0800151 },
152};
153
154#define STR_INTERFACE_ "ADB Interface"
155
156static const struct {
157 struct usb_functionfs_strings_head header;
158 struct {
159 __le16 code;
160 const char str1[sizeof(STR_INTERFACE_)];
161 } __attribute__((packed)) lang0;
162} __attribute__((packed)) strings = {
163 .header = {
164 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
165 .length = cpu_to_le32(sizeof(strings)),
166 .str_count = cpu_to_le32(1),
167 .lang_count = cpu_to_le32(1),
168 },
169 .lang0 = {
170 cpu_to_le16(0x0409), /* en-us */
171 STR_INTERFACE_,
172 },
Doug Zongker9270a202012-01-09 15:16:13 -0800173};
174
175void usb_cleanup()
176{
177 // nothing to do here
178}
179
Da Zhou65ad9282014-01-17 01:04:07 -0800180static void *usb_adb_open_thread(void *x)
Doug Zongker9270a202012-01-09 15:16:13 -0800181{
182 struct usb_handle *usb = (struct usb_handle *)x;
183 int fd;
184
185 while (1) {
186 // wait until the USB device needs opening
187 adb_mutex_lock(&usb->lock);
188 while (usb->fd != -1)
189 adb_cond_wait(&usb->notify, &usb->lock);
190 adb_mutex_unlock(&usb->lock);
191
192 D("[ usb_thread - opening device ]\n");
193 do {
194 /* XXX use inotify? */
195 fd = unix_open("/dev/android_adb", O_RDWR);
196 if (fd < 0) {
197 // to support older kernels
198 fd = unix_open("/dev/android", O_RDWR);
Da Zhou65ad9282014-01-17 01:04:07 -0800199 fprintf(stderr, "usb_adb_open_thread: %d\n", fd );
Doug Zongker9270a202012-01-09 15:16:13 -0800200 }
201 if (fd < 0) {
202 adb_sleep_ms(1000);
203 }
204 } while (fd < 0);
205 D("[ opening device succeeded ]\n");
206
207 close_on_exec(fd);
208 usb->fd = fd;
209
210 D("[ usb_thread - registering device ]\n");
211 register_usb_transport(usb, 0, 1);
212 }
213
214 // never gets here
215 return 0;
216}
217
Da Zhou65ad9282014-01-17 01:04:07 -0800218static int usb_adb_write(usb_handle *h, const void *data, int len)
Doug Zongker9270a202012-01-09 15:16:13 -0800219{
220 int n;
221
222 D("about to write (fd=%d, len=%d)\n", h->fd, len);
223 n = adb_write(h->fd, data, len);
224 if(n != len) {
225 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
226 h->fd, n, errno, strerror(errno));
227 return -1;
228 }
229 D("[ done fd=%d ]\n", h->fd);
230 return 0;
231}
232
Da Zhou65ad9282014-01-17 01:04:07 -0800233static int usb_adb_read(usb_handle *h, void *data, int len)
Doug Zongker9270a202012-01-09 15:16:13 -0800234{
235 int n;
236
237 D("about to read (fd=%d, len=%d)\n", h->fd, len);
238 n = adb_read(h->fd, data, len);
239 if(n != len) {
240 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
241 h->fd, n, errno, strerror(errno));
242 return -1;
243 }
244 D("[ done fd=%d ]\n", h->fd);
245 return 0;
246}
247
Da Zhou65ad9282014-01-17 01:04:07 -0800248static void usb_adb_kick(usb_handle *h)
249{
250 D("usb_kick\n");
251 adb_mutex_lock(&h->lock);
252 adb_close(h->fd);
253 h->fd = -1;
254
255 // notify usb_adb_open_thread that we are disconnected
256 adb_cond_signal(&h->notify);
257 adb_mutex_unlock(&h->lock);
258}
259
260static void usb_adb_init()
Doug Zongker9270a202012-01-09 15:16:13 -0800261{
262 usb_handle *h;
263 adb_thread_t tid;
264 int fd;
265
266 h = calloc(1, sizeof(usb_handle));
Da Zhou65ad9282014-01-17 01:04:07 -0800267
268 h->write = usb_adb_write;
269 h->read = usb_adb_read;
270 h->kick = usb_adb_kick;
Doug Zongker9270a202012-01-09 15:16:13 -0800271 h->fd = -1;
Da Zhou65ad9282014-01-17 01:04:07 -0800272
Doug Zongker9270a202012-01-09 15:16:13 -0800273 adb_cond_init(&h->notify, 0);
274 adb_mutex_init(&h->lock, 0);
275
Da Zhou65ad9282014-01-17 01:04:07 -0800276 fprintf(stderr, "Starting to open usb_init()\n");
Doug Zongker9270a202012-01-09 15:16:13 -0800277 // Open the file /dev/android_adb_enable to trigger
278 // the enabling of the adb USB function in the kernel.
279 // We never touch this file again - just leave it open
280 // indefinitely so the kernel will know when we are running
281 // and when we are not.
282 fd = unix_open("/dev/android_adb_enable", O_RDWR);
Da Zhou65ad9282014-01-17 01:04:07 -0800283 fprintf(stderr, "unix_open to open usb_init(): %d\n", fd);
Doug Zongker9270a202012-01-09 15:16:13 -0800284 if (fd < 0) {
285 D("failed to open /dev/android_adb_enable\n");
286 } else {
287 close_on_exec(fd);
288 }
289
290 D("[ usb_init - starting thread ]\n");
Da Zhou65ad9282014-01-17 01:04:07 -0800291 if(adb_thread_create(&tid, usb_adb_open_thread, h)){
Doug Zongker9270a202012-01-09 15:16:13 -0800292 fatal_errno("cannot create usb thread");
Da Zhou65ad9282014-01-17 01:04:07 -0800293 fprintf(stderr, "cannot create the usb thread()\n");
Doug Zongker9270a202012-01-09 15:16:13 -0800294 }
295}
296
Doug Zongker9270a202012-01-09 15:16:13 -0800297
Da Zhou65ad9282014-01-17 01:04:07 -0800298static void init_functionfs(struct usb_handle *h)
299{
300 ssize_t ret;
Badhri Jagan Sridharanb5b43042014-10-27 18:32:35 -0700301 struct desc_v1 v1_descriptor;
302 struct desc_v2 v2_descriptor;
303
304 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
305 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
306 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
307 v2_descriptor.header.fs_count = 3;
308 v2_descriptor.header.hs_count = 3;
309 v2_descriptor.header.ss_count = 0;
310 v2_descriptor.fs_descs = fs_descriptors;
311 v2_descriptor.hs_descs = hs_descriptors;
312
Da Zhou65ad9282014-01-17 01:04:07 -0800313
314 D("OPENING %s\n", USB_FFS_ADB_EP0);
315 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
316 if (h->control < 0) {
317 D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
318 goto err;
319 }
320
Badhri Jagan Sridharanb5b43042014-10-27 18:32:35 -0700321 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Da Zhou65ad9282014-01-17 01:04:07 -0800322 if (ret < 0) {
Badhri Jagan Sridharanb5b43042014-10-27 18:32:35 -0700323 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
324 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
325 v1_descriptor.header.fs_count = 3;
326 v1_descriptor.header.hs_count = 3;
327 v1_descriptor.fs_descs = fs_descriptors;
328 v1_descriptor.hs_descs = hs_descriptors;
329 D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
330 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
331 if (ret < 0) {
332 D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
333 goto err;
334 }
Da Zhou65ad9282014-01-17 01:04:07 -0800335 }
336
337 ret = adb_write(h->control, &strings, sizeof(strings));
338 if (ret < 0) {
339 D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
340 goto err;
341 }
342
343 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
344 if (h->bulk_out < 0) {
345 D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
346 goto err;
347 }
348
349 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
350 if (h->bulk_in < 0) {
351 D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
352 goto err;
353 }
354
355 return;
356
357err:
358 if (h->bulk_in > 0) {
359 adb_close(h->bulk_in);
360 h->bulk_in = -1;
361 }
362 if (h->bulk_out > 0) {
363 adb_close(h->bulk_out);
364 h->bulk_out = -1;
365 }
366 if (h->control > 0) {
367 adb_close(h->control);
368 h->control = -1;
369 }
370 return;
371}
372
373static void *usb_ffs_open_thread(void *x)
374{
375 struct usb_handle *usb = (struct usb_handle *)x;
376
377 while (1) {
378 // wait until the USB device needs opening
379 adb_mutex_lock(&usb->lock);
380 while (usb->control != -1)
381 adb_cond_wait(&usb->notify, &usb->lock);
382 adb_mutex_unlock(&usb->lock);
383
384 while (1) {
385 init_functionfs(usb);
386
387 if (usb->control >= 0)
388 break;
389
390 adb_sleep_ms(1000);
391 }
392
393 D("[ usb_thread - registering device ]\n");
394 register_usb_transport(usb, 0, 1);
395 }
396
397 // never gets here
398 return 0;
399}
400
401static int bulk_write(int bulk_in, const char *buf, size_t length)
402{
403 size_t count = 0;
404 int ret;
405
406 do {
407 ret = adb_write(bulk_in, buf + count, length - count);
408 if (ret < 0) {
409 if (errno != EINTR)
410 return ret;
411 } else {
412 count += ret;
413 }
414 } while (count < length);
415
416 D("[ bulk_write done fd=%d ]\n", bulk_in);
417 return count;
418}
419
420static int usb_ffs_write(usb_handle *h, const void *data, int len)
421{
422 int n;
423
424 D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
425 n = bulk_write(h->bulk_in, data, len);
426 if (n != len) {
427 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
428 h->bulk_in, n, errno, strerror(errno));
429 return -1;
430 }
431 D("[ done fd=%d ]\n", h->bulk_in);
432 return 0;
433}
434
435static int bulk_read(int bulk_out, char *buf, size_t length)
436{
437 size_t count = 0;
438 int ret;
439
440 do {
441 ret = adb_read(bulk_out, buf + count, length - count);
442 if (ret < 0) {
443 if (errno != EINTR) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700444 D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n",
Da Zhou65ad9282014-01-17 01:04:07 -0800445 bulk_out, length, count);
446 return ret;
447 }
448 } else {
449 count += ret;
450 }
451 } while (count < length);
452
453 return count;
454}
455
456static int usb_ffs_read(usb_handle *h, void *data, int len)
457{
458 int n;
459
460 D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
461 n = bulk_read(h->bulk_out, data, len);
462 if (n != len) {
463 D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
464 h->bulk_out, n, errno, strerror(errno));
465 return -1;
466 }
467 D("[ done fd=%d ]\n", h->bulk_out);
468 return 0;
469}
470
471static void usb_ffs_kick(usb_handle *h)
472{
473 int err;
474
475 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
476 if (err < 0)
477 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
478
479 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
480 if (err < 0)
481 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
482
483 adb_mutex_lock(&h->lock);
484 adb_close(h->control);
485 adb_close(h->bulk_out);
486 adb_close(h->bulk_in);
487 h->control = h->bulk_out = h->bulk_in = -1;
488
489 // notify usb_ffs_open_thread that we are disconnected
Doug Zongker9270a202012-01-09 15:16:13 -0800490 adb_cond_signal(&h->notify);
491 adb_mutex_unlock(&h->lock);
492}
493
Da Zhou65ad9282014-01-17 01:04:07 -0800494static void usb_ffs_init()
495{
496 usb_handle *h;
497 adb_thread_t tid;
498
499 D("[ usb_init - using FunctionFS ]\n");
500
501 h = calloc(1, sizeof(usb_handle));
502
503 h->write = usb_ffs_write;
504 h->read = usb_ffs_read;
505 h->kick = usb_ffs_kick;
506
507 h->control = -1;
508 h->bulk_out = -1;
509 h->bulk_out = -1;
510
511 adb_cond_init(&h->notify, 0);
512 adb_mutex_init(&h->lock, 0);
513
514 D("[ usb_init - starting thread ]\n");
515 if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
516 fatal_errno("[ cannot create usb thread ]\n");
517 }
518}
519
520void usb_init()
521{
522 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
523 usb_ffs_init();
524 else
525 usb_adb_init();
526}
527
528int usb_write(usb_handle *h, const void *data, int len)
529{
530 return h->write(h, data, len);
531}
532
533int usb_read(usb_handle *h, void *data, int len)
534{
535 return h->read(h, data, len);
536}
Doug Zongker9270a202012-01-09 15:16:13 -0800537int usb_close(usb_handle *h)
538{
539 // nothing to do here
540 return 0;
541}
Da Zhou65ad9282014-01-17 01:04:07 -0800542
543void usb_kick(usb_handle *h)
544{
545 h->kick(h);
546}