blob: c5e7452f10d3803d56af93d19a4176f1705bba60 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Badhri Jagan Sridharane1febe92015-04-21 11:09:32 -070021#include <cutils/properties.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022#include <dirent.h>
23#include <errno.h>
Dan Albertb302d122015-02-24 15:51:19 -080024#include <linux/usb/ch9.h>
25#include <linux/usb/functionfs.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ioctl.h>
30#include <sys/types.h>
31#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080032
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080033#include "adb.h"
Dan Albertb302d122015-02-24 15:51:19 -080034#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010036#define MAX_PACKET_SIZE_FS 64
37#define MAX_PACKET_SIZE_HS 512
Zhuang Jin Can1fc58c52014-09-02 13:04:44 +080038#define MAX_PACKET_SIZE_SS 1024
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010039
40#define cpu_to_le16(x) htole16(x)
41#define cpu_to_le32(x) htole32(x)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042
43struct usb_handle
44{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045 adb_cond_t notify;
46 adb_mutex_t lock;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +010047
48 int (*write)(usb_handle *h, const void *data, int len);
49 int (*read)(usb_handle *h, void *data, int len);
50 void (*kick)(usb_handle *h);
51
52 // Legacy f_adb
53 int fd;
54
55 // FunctionFS
56 int control;
57 int bulk_out; /* "out" from the host's perspective => source for adbd */
58 int bulk_in; /* "in" from the host's perspective => sink for adbd */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059};
60
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070061struct func_desc {
62 struct usb_interface_descriptor intf;
63 struct usb_endpoint_descriptor_no_audio source;
64 struct usb_endpoint_descriptor_no_audio sink;
65} __attribute__((packed));
66
Jack Phamb64ab092015-06-02 10:36:43 -070067struct ss_func_desc {
68 struct usb_interface_descriptor intf;
69 struct usb_endpoint_descriptor_no_audio source;
70 struct usb_ss_ep_comp_descriptor source_comp;
71 struct usb_endpoint_descriptor_no_audio sink;
72 struct usb_ss_ep_comp_descriptor sink_comp;
73} __attribute__((packed));
74
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070075struct desc_v1 {
76 struct usb_functionfs_descs_head_v1 {
77 __le32 magic;
78 __le32 length;
79 __le32 fs_count;
80 __le32 hs_count;
81 } __attribute__((packed)) header;
82 struct func_desc fs_descs, hs_descs;
83} __attribute__((packed));
84
85struct desc_v2 {
Christopher Ferrisf7555b12015-01-23 17:09:56 -080086 struct usb_functionfs_descs_head_v2 header;
87 // The rest of the structure depends on the flags in the header.
88 __le32 fs_count;
89 __le32 hs_count;
Jack Phamb64ab092015-06-02 10:36:43 -070090 __le32 ss_count;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -070091 __le32 os_count;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070092 struct func_desc fs_descs, hs_descs;
Jack Phamb64ab092015-06-02 10:36:43 -070093 struct ss_func_desc ss_descs;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -070094 struct usb_os_desc_header os_header;
95 struct usb_ext_compat_desc os_desc;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070096} __attribute__((packed));
97
Elliott Hughes712416a2015-05-05 18:26:10 -070098static struct func_desc fs_descriptors = {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -070099 .intf = {
100 .bLength = sizeof(fs_descriptors.intf),
101 .bDescriptorType = USB_DT_INTERFACE,
102 .bInterfaceNumber = 0,
103 .bNumEndpoints = 2,
104 .bInterfaceClass = ADB_CLASS,
105 .bInterfaceSubClass = ADB_SUBCLASS,
106 .bInterfaceProtocol = ADB_PROTOCOL,
107 .iInterface = 1, /* first string from the provided table */
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100108 },
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700109 .source = {
110 .bLength = sizeof(fs_descriptors.source),
111 .bDescriptorType = USB_DT_ENDPOINT,
112 .bEndpointAddress = 1 | USB_DIR_OUT,
113 .bmAttributes = USB_ENDPOINT_XFER_BULK,
114 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100115 },
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700116 .sink = {
117 .bLength = sizeof(fs_descriptors.sink),
118 .bDescriptorType = USB_DT_ENDPOINT,
119 .bEndpointAddress = 2 | USB_DIR_IN,
120 .bmAttributes = USB_ENDPOINT_XFER_BULK,
121 .wMaxPacketSize = MAX_PACKET_SIZE_FS,
122 },
123};
124
Elliott Hughes712416a2015-05-05 18:26:10 -0700125static struct func_desc hs_descriptors = {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700126 .intf = {
127 .bLength = sizeof(hs_descriptors.intf),
128 .bDescriptorType = USB_DT_INTERFACE,
129 .bInterfaceNumber = 0,
130 .bNumEndpoints = 2,
131 .bInterfaceClass = ADB_CLASS,
132 .bInterfaceSubClass = ADB_SUBCLASS,
133 .bInterfaceProtocol = ADB_PROTOCOL,
134 .iInterface = 1, /* first string from the provided table */
135 },
136 .source = {
137 .bLength = sizeof(hs_descriptors.source),
138 .bDescriptorType = USB_DT_ENDPOINT,
139 .bEndpointAddress = 1 | USB_DIR_OUT,
140 .bmAttributes = USB_ENDPOINT_XFER_BULK,
141 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
142 },
143 .sink = {
144 .bLength = sizeof(hs_descriptors.sink),
145 .bDescriptorType = USB_DT_ENDPOINT,
146 .bEndpointAddress = 2 | USB_DIR_IN,
147 .bmAttributes = USB_ENDPOINT_XFER_BULK,
148 .wMaxPacketSize = MAX_PACKET_SIZE_HS,
Zhuang Jin Can1fc58c52014-09-02 13:04:44 +0800149 },
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100150};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800151
Jack Phamb64ab092015-06-02 10:36:43 -0700152static struct ss_func_desc ss_descriptors = {
153 .intf = {
154 .bLength = sizeof(ss_descriptors.intf),
155 .bDescriptorType = USB_DT_INTERFACE,
156 .bInterfaceNumber = 0,
157 .bNumEndpoints = 2,
158 .bInterfaceClass = ADB_CLASS,
159 .bInterfaceSubClass = ADB_SUBCLASS,
160 .bInterfaceProtocol = ADB_PROTOCOL,
161 .iInterface = 1, /* first string from the provided table */
162 },
163 .source = {
164 .bLength = sizeof(ss_descriptors.source),
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = 1 | USB_DIR_OUT,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK,
168 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
169 },
170 .source_comp = {
171 .bLength = sizeof(ss_descriptors.source_comp),
172 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
173 },
174 .sink = {
175 .bLength = sizeof(ss_descriptors.sink),
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = 2 | USB_DIR_IN,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179 .wMaxPacketSize = MAX_PACKET_SIZE_SS,
180 },
181 .sink_comp = {
182 .bLength = sizeof(ss_descriptors.sink_comp),
183 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
184 },
185};
186
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700187struct usb_ext_compat_desc os_desc_compat = {
188 .bFirstInterfaceNumber = 0,
189 .Reserved1 = cpu_to_le32(1),
190 .CompatibleID = {0},
191 .SubCompatibleID = {0},
192 .Reserved2 = {0},
193};
194
195static struct usb_os_desc_header os_desc_header = {
196 .interface = cpu_to_le32(1),
197 .dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
198 .bcdVersion = cpu_to_le32(1),
199 .wIndex = cpu_to_le32(4),
200 .bCount = cpu_to_le32(1),
201 .Reserved = cpu_to_le32(0),
202};
203
204
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100205#define STR_INTERFACE_ "ADB Interface"
206
207static const struct {
208 struct usb_functionfs_strings_head header;
209 struct {
210 __le16 code;
211 const char str1[sizeof(STR_INTERFACE_)];
212 } __attribute__((packed)) lang0;
213} __attribute__((packed)) strings = {
214 .header = {
215 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
216 .length = cpu_to_le32(sizeof(strings)),
217 .str_count = cpu_to_le32(1),
218 .lang_count = cpu_to_le32(1),
219 },
220 .lang0 = {
221 cpu_to_le16(0x0409), /* en-us */
222 STR_INTERFACE_,
223 },
224};
225
226
227
228static void *usb_adb_open_thread(void *x)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229{
230 struct usb_handle *usb = (struct usb_handle *)x;
231 int fd;
232
Siva Velusamy2669cf92015-08-28 16:37:29 -0700233 adb_thread_setname("usb open");
234
Elliott Hughes7cf35752015-04-17 17:03:59 -0700235 while (true) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236 // wait until the USB device needs opening
237 adb_mutex_lock(&usb->lock);
238 while (usb->fd != -1)
239 adb_cond_wait(&usb->notify, &usb->lock);
240 adb_mutex_unlock(&usb->lock);
241
Yabin Cui815ad882015-09-02 17:44:28 -0700242 D("[ usb_thread - opening device ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243 do {
244 /* XXX use inotify? */
245 fd = unix_open("/dev/android_adb", O_RDWR);
246 if (fd < 0) {
247 // to support older kernels
248 fd = unix_open("/dev/android", O_RDWR);
249 }
250 if (fd < 0) {
251 adb_sleep_ms(1000);
252 }
253 } while (fd < 0);
Yabin Cui815ad882015-09-02 17:44:28 -0700254 D("[ opening device succeeded ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255
256 close_on_exec(fd);
257 usb->fd = fd;
258
Yabin Cui815ad882015-09-02 17:44:28 -0700259 D("[ usb_thread - registering device ]");
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700260 register_usb_transport(usb, 0, 0, 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 }
262
263 // never gets here
264 return 0;
265}
266
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100267static int usb_adb_write(usb_handle *h, const void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800268{
269 int n;
270
Yabin Cui815ad882015-09-02 17:44:28 -0700271 D("about to write (fd=%d, len=%d)", h->fd, len);
Spencer Low3a2421b2015-05-22 20:09:06 -0700272 n = unix_write(h->fd, data, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800273 if(n != len) {
Yabin Cui815ad882015-09-02 17:44:28 -0700274 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700275 h->fd, n, errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 return -1;
277 }
Yabin Cui815ad882015-09-02 17:44:28 -0700278 D("[ done fd=%d ]", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279 return 0;
280}
281
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100282static int usb_adb_read(usb_handle *h, void *data, int len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283{
Yabin Cui815ad882015-09-02 17:44:28 -0700284 D("about to read (fd=%d, len=%d)", h->fd, len);
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100285 while (len > 0) {
286 // The kernel implementation of adb_read in f_adb.c doesn't support
287 // reads larger then 4096 bytes. Read the data in 4096 byte chunks to
288 // avoid the issue. (The ffs implementation doesn't have this limit.)
289 int bytes_to_read = len < 4096 ? len : 4096;
290 int n = unix_read(h->fd, data, bytes_to_read);
291 if (n != bytes_to_read) {
Yabin Cui815ad882015-09-02 17:44:28 -0700292 D("ERROR: fd = %d, n = %d, errno = %d (%s)",
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100293 h->fd, n, errno, strerror(errno));
294 return -1;
295 }
296 len -= n;
297 data = ((char*)data) + n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800298 }
Yabin Cui815ad882015-09-02 17:44:28 -0700299 D("[ done fd=%d ]", h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300 return 0;
301}
302
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100303static void usb_adb_kick(usb_handle *h)
304{
Yabin Cui815ad882015-09-02 17:44:28 -0700305 D("usb_kick");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100306 adb_mutex_lock(&h->lock);
Spencer Low3a2421b2015-05-22 20:09:06 -0700307 unix_close(h->fd);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100308 h->fd = -1;
309
310 // notify usb_adb_open_thread that we are disconnected
311 adb_cond_signal(&h->notify);
312 adb_mutex_unlock(&h->lock);
313}
314
315static void usb_adb_init()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316{
Elliott Hughes392692c2015-04-16 14:12:50 -0700317 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700318 if (h == nullptr) fatal("couldn't allocate usb_handle");
319
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100320 h->write = usb_adb_write;
321 h->read = usb_adb_read;
322 h->kick = usb_adb_kick;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323 h->fd = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100324
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800325 adb_cond_init(&h->notify, 0);
326 adb_mutex_init(&h->lock, 0);
327
Elliott Hughes392692c2015-04-16 14:12:50 -0700328 // Open the file /dev/android_adb_enable to trigger
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800329 // the enabling of the adb USB function in the kernel.
330 // We never touch this file again - just leave it open
331 // indefinitely so the kernel will know when we are running
332 // and when we are not.
Elliott Hughes392692c2015-04-16 14:12:50 -0700333 int fd = unix_open("/dev/android_adb_enable", O_RDWR);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800334 if (fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700335 D("failed to open /dev/android_adb_enable");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800336 } else {
337 close_on_exec(fd);
338 }
339
Yabin Cui815ad882015-09-02 17:44:28 -0700340 D("[ usb_init - starting thread ]");
Elliott Hughesf2517142015-05-05 13:41:21 -0700341 if (!adb_thread_create(usb_adb_open_thread, h)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342 fatal_errno("cannot create usb thread");
343 }
344}
345
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100347static void init_functionfs(struct usb_handle *h)
348{
349 ssize_t ret;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700350 struct desc_v1 v1_descriptor;
351 struct desc_v2 v2_descriptor;
352
353 v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
354 v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
Jack Phamb64ab092015-06-02 10:36:43 -0700355 v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700356 FUNCTIONFS_HAS_SS_DESC | FUNCTIONFS_HAS_MS_OS_DESC;
Christopher Ferrisf7555b12015-01-23 17:09:56 -0800357 v2_descriptor.fs_count = 3;
358 v2_descriptor.hs_count = 3;
Jack Phamb64ab092015-06-02 10:36:43 -0700359 v2_descriptor.ss_count = 5;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700360 v2_descriptor.os_count = 1;
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700361 v2_descriptor.fs_descs = fs_descriptors;
362 v2_descriptor.hs_descs = hs_descriptors;
Jack Phamb64ab092015-06-02 10:36:43 -0700363 v2_descriptor.ss_descs = ss_descriptors;
Badhri Jagan Sridharanf1500ca2015-10-05 13:04:03 -0700364 v2_descriptor.os_header = os_desc_header;
365 v2_descriptor.os_desc = os_desc_compat;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100366
Jack Pham6c3cef52013-12-23 17:46:10 -0800367 if (h->control < 0) { // might have already done this before
Yabin Cui815ad882015-09-02 17:44:28 -0700368 D("OPENING %s", USB_FFS_ADB_EP0);
Jack Pham6c3cef52013-12-23 17:46:10 -0800369 h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
370 if (h->control < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700371 D("[ %s: cannot open control endpoint: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham6c3cef52013-12-23 17:46:10 -0800372 goto err;
373 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100374
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700375 ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
Jack Pham6c3cef52013-12-23 17:46:10 -0800376 if (ret < 0) {
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700377 v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
378 v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
379 v1_descriptor.header.fs_count = 3;
380 v1_descriptor.header.hs_count = 3;
381 v1_descriptor.fs_descs = fs_descriptors;
382 v1_descriptor.hs_descs = hs_descriptors;
Yabin Cui815ad882015-09-02 17:44:28 -0700383 D("[ %s: Switching to V1_descriptor format errno=%d ]", USB_FFS_ADB_EP0, errno);
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700384 ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
385 if (ret < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700386 D("[ %s: write descriptors failed: errno=%d ]", USB_FFS_ADB_EP0, errno);
Badhri Jagan Sridharand6a63732014-10-27 18:26:17 -0700387 goto err;
388 }
Jack Pham6c3cef52013-12-23 17:46:10 -0800389 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100390
Jack Pham6c3cef52013-12-23 17:46:10 -0800391 ret = adb_write(h->control, &strings, sizeof(strings));
392 if (ret < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700393 D("[ %s: writing strings failed: errno=%d]", USB_FFS_ADB_EP0, errno);
Jack Pham6c3cef52013-12-23 17:46:10 -0800394 goto err;
395 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100396 }
397
398 h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
399 if (h->bulk_out < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700400 D("[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_ADB_OUT, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100401 goto err;
402 }
403
404 h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
405 if (h->bulk_in < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700406 D("[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_ADB_IN, errno);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100407 goto err;
408 }
409
410 return;
411
412err:
413 if (h->bulk_in > 0) {
414 adb_close(h->bulk_in);
415 h->bulk_in = -1;
416 }
417 if (h->bulk_out > 0) {
418 adb_close(h->bulk_out);
419 h->bulk_out = -1;
420 }
421 if (h->control > 0) {
422 adb_close(h->control);
423 h->control = -1;
424 }
425 return;
426}
427
428static void *usb_ffs_open_thread(void *x)
429{
430 struct usb_handle *usb = (struct usb_handle *)x;
431
Siva Velusamy2669cf92015-08-28 16:37:29 -0700432 adb_thread_setname("usb ffs open");
433
Elliott Hughes7cf35752015-04-17 17:03:59 -0700434 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100435 // wait until the USB device needs opening
436 adb_mutex_lock(&usb->lock);
Jack Pham6c3cef52013-12-23 17:46:10 -0800437 while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100438 adb_cond_wait(&usb->notify, &usb->lock);
439 adb_mutex_unlock(&usb->lock);
440
Elliott Hughes7cf35752015-04-17 17:03:59 -0700441 while (true) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100442 init_functionfs(usb);
443
Jack Pham6c3cef52013-12-23 17:46:10 -0800444 if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100445 break;
446
447 adb_sleep_ms(1000);
448 }
Badhri Jagan Sridharane1febe92015-04-21 11:09:32 -0700449 property_set("sys.usb.ffs.ready", "1");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100450
Yabin Cui815ad882015-09-02 17:44:28 -0700451 D("[ usb_thread - registering device ]");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100452 register_usb_transport(usb, 0, 0, 1);
453 }
454
455 // never gets here
456 return 0;
457}
458
Josh Gao8e701252015-12-02 17:30:58 -0800459static int usb_ffs_write(usb_handle* h, const void* data, int len) {
Yabin Cui815ad882015-09-02 17:44:28 -0700460 D("about to write (fd=%d, len=%d)", h->bulk_in, len);
Josh Gao8e701252015-12-02 17:30:58 -0800461
462 // Writes larger than 16k fail on some devices (seed with 3.10.49-g209ea2f in particular).
463 const char* buf = static_cast<const char*>(data);
464 while (len > 0) {
465 int write_len = (len > 16384) ? 16384 : len;
466 int n = adb_write(h->bulk_in, buf, write_len);
467 if (n < 0) {
468 D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
469 return -1;
470 }
471 buf += n;
472 len -= n;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100473 }
Josh Gao8e701252015-12-02 17:30:58 -0800474
Yabin Cui815ad882015-09-02 17:44:28 -0700475 D("[ done fd=%d ]", h->bulk_in);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100476 return 0;
477}
478
Josh Gao8e701252015-12-02 17:30:58 -0800479static int usb_ffs_read(usb_handle* h, void* data, int len) {
480 D("about to read (fd=%d, len=%d)", h->bulk_out, len);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100481
Josh Gao8e701252015-12-02 17:30:58 -0800482 char* buf = static_cast<char*>(data);
483 while (len > 0) {
484 int n = adb_read(h->bulk_out, buf, len);
485 if (n < 0) {
486 D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
Elliott Hughes0bd85872015-08-25 10:59:45 -0700487 return -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100488 }
Josh Gao8e701252015-12-02 17:30:58 -0800489 buf += n;
490 len -= n;
Elliott Hughes0bd85872015-08-25 10:59:45 -0700491 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100492
Yabin Cui815ad882015-09-02 17:44:28 -0700493 D("[ done fd=%d ]", h->bulk_out);
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100494 return 0;
495}
496
497static void usb_ffs_kick(usb_handle *h)
498{
499 int err;
500
501 err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700502 if (err < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700503 D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700504 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100505
506 err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700507 if (err < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700508 D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
Yabin Cui19bec5b2015-09-22 15:52:57 -0700509 }
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100510
511 adb_mutex_lock(&h->lock);
Jack Pham6c3cef52013-12-23 17:46:10 -0800512
513 // don't close ep0 here, since we may not need to reinitialize it with
514 // the same descriptors again. if however ep1/ep2 fail to re-open in
515 // init_functionfs, only then would we close and open ep0 again.
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100516 adb_close(h->bulk_out);
517 adb_close(h->bulk_in);
Jack Pham6c3cef52013-12-23 17:46:10 -0800518 h->bulk_out = h->bulk_in = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100519
520 // notify usb_ffs_open_thread that we are disconnected
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800521 adb_cond_signal(&h->notify);
522 adb_mutex_unlock(&h->lock);
523}
524
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100525static void usb_ffs_init()
526{
Yabin Cui815ad882015-09-02 17:44:28 -0700527 D("[ usb_init - using FunctionFS ]");
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100528
Elliott Hughes392692c2015-04-16 14:12:50 -0700529 usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
Elliott Hughesd0269c92015-04-21 19:39:52 -0700530 if (h == nullptr) fatal("couldn't allocate usb_handle");
531
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100532 h->write = usb_ffs_write;
533 h->read = usb_ffs_read;
534 h->kick = usb_ffs_kick;
Elliott Hughes392692c2015-04-16 14:12:50 -0700535 h->control = -1;
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100536 h->bulk_out = -1;
537 h->bulk_out = -1;
538
539 adb_cond_init(&h->notify, 0);
540 adb_mutex_init(&h->lock, 0);
541
Yabin Cui815ad882015-09-02 17:44:28 -0700542 D("[ usb_init - starting thread ]");
Elliott Hughesf2517142015-05-05 13:41:21 -0700543 if (!adb_thread_create(usb_ffs_open_thread, h)) {
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100544 fatal_errno("[ cannot create usb thread ]\n");
545 }
546}
547
548void usb_init()
549{
550 if (access(USB_FFS_ADB_EP0, F_OK) == 0)
551 usb_ffs_init();
552 else
553 usb_adb_init();
554}
555
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100556int usb_write(usb_handle *h, const void *data, int len)
557{
558 return h->write(h, data, len);
559}
560
561int usb_read(usb_handle *h, void *data, int len)
562{
563 return h->read(h, data, len);
564}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800565int usb_close(usb_handle *h)
566{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800567 return 0;
568}
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100569
570void usb_kick(usb_handle *h)
571{
572 h->kick(h);
573}
David Pursell6e5c7eb2015-12-02 15:14:31 -0800574
575// kCsNoPerm is a host-side issue, we can just ignore it here.
576std::string UsbNoPermissionsShortHelpText() {
577 return "";
578}
579
580std::string UsbNoPermissionsLongHelpText() {
581 return "";
582}