The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG USB |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/ioctl.h> |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 27 | #include <sys/mman.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Josh Gao | 613cbb4 | 2018-10-08 14:20:29 -0700 | [diff] [blame^] | 31 | #include <linux/usb/ch9.h> |
| 32 | #include <linux/usb/functionfs.h> |
| 33 | |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 34 | #include <algorithm> |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 35 | #include <atomic> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 36 | #include <chrono> |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 37 | #include <condition_variable> |
| 38 | #include <mutex> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 39 | #include <thread> |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 40 | |
| 41 | #include <android-base/logging.h> |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | #include "adb.h" |
Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 45 | #include "adbd/usb.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 46 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 48 | using namespace std::chrono_literals; |
| 49 | |
Josh Gao | 44c688c | 2017-01-11 17:37:35 -0800 | [diff] [blame] | 50 | #define MAX_PACKET_SIZE_FS 64 |
| 51 | #define MAX_PACKET_SIZE_HS 512 |
| 52 | #define MAX_PACKET_SIZE_SS 1024 |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 53 | |
Jerry Zhang | 55205a5 | 2017-01-04 12:34:38 -0800 | [diff] [blame] | 54 | #define USB_FFS_BULK_SIZE 16384 |
Josh Gao | ae72b5a | 2015-12-17 13:45:18 -0800 | [diff] [blame] | 55 | |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 56 | // Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs. |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 57 | #define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1) |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 58 | |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 59 | static unique_fd& dummy_fd = *new unique_fd(); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 60 | |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 61 | static void aio_block_init(aio_block* aiob, unsigned num_bufs) { |
| 62 | aiob->iocb.resize(num_bufs); |
| 63 | aiob->iocbs.resize(num_bufs); |
| 64 | aiob->events.resize(num_bufs); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 65 | aiob->num_submitted = 0; |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 66 | for (unsigned i = 0; i < num_bufs; i++) { |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 67 | aiob->iocbs[i] = &aiob->iocb[i]; |
| 68 | } |
Jerry Zhang | d8c1ae9 | 2018-05-15 16:30:10 -0700 | [diff] [blame] | 69 | memset(&aiob->ctx, 0, sizeof(aiob->ctx)); |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 70 | if (io_setup(num_bufs, &aiob->ctx)) { |
Jerry Zhang | d8c1ae9 | 2018-05-15 16:30:10 -0700 | [diff] [blame] | 71 | D("[ aio: got error on io_setup (%d) ]", errno); |
| 72 | } |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int getMaxPacketSize(int ffs_fd) { |
| 76 | usb_endpoint_descriptor desc; |
| 77 | if (ioctl(ffs_fd, FUNCTIONFS_ENDPOINT_DESC, reinterpret_cast<unsigned long>(&desc))) { |
| 78 | D("[ could not get endpoint descriptor! (%d) ]", errno); |
| 79 | return MAX_PACKET_SIZE_HS; |
| 80 | } else { |
| 81 | return desc.wMaxPacketSize; |
| 82 | } |
| 83 | } |
| 84 | |
Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 85 | static bool init_functionfs(struct usb_handle* h) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 86 | LOG(INFO) << "initializing functionfs"; |
Josh Gao | 613cbb4 | 2018-10-08 14:20:29 -0700 | [diff] [blame^] | 87 | if (!open_functionfs(&h->control, &h->bulk_out, &h->bulk_in)) { |
| 88 | return false; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Josh Gao | 613cbb4 | 2018-10-08 14:20:29 -0700 | [diff] [blame^] | 91 | h->read_aiob.fd = h->bulk_out.get(); |
| 92 | h->write_aiob.fd = h->bulk_in.get(); |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 93 | h->reads_zero_packets = true; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 94 | return true; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 97 | static void usb_ffs_open_thread(usb_handle *usb) { |
Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 98 | adb_thread_setname("usb ffs open"); |
| 99 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 100 | while (true) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 101 | // wait until the USB device needs opening |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 102 | std::unique_lock<std::mutex> lock(usb->lock); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 103 | while (!usb->open_new_connection) { |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 104 | usb->notify.wait(lock); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 105 | } |
| 106 | usb->open_new_connection = false; |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 107 | lock.unlock(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 108 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 109 | while (true) { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 110 | if (init_functionfs(usb)) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 111 | LOG(INFO) << "functionfs successfully initialized"; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 112 | break; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 113 | } |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 114 | std::this_thread::sleep_for(1s); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 117 | LOG(INFO) << "registering usb transport"; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 118 | register_usb_transport(usb, nullptr, nullptr, 1); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // never gets here |
Josh Gao | d9db09c | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 122 | abort(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 125 | static int usb_ffs_write(usb_handle* h, const void* data, int len) { |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 126 | D("about to write (fd=%d, len=%d)", h->bulk_in.get(), len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 127 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 128 | const char* buf = static_cast<const char*>(data); |
Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 129 | int orig_len = len; |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 130 | while (len > 0) { |
Jerry Zhang | 99499f1 | 2018-03-14 14:57:31 -0700 | [diff] [blame] | 131 | int write_len = std::min(USB_FFS_BULK_SIZE, len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 132 | int n = adb_write(h->bulk_in, buf, write_len); |
| 133 | if (n < 0) { |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 134 | D("ERROR: fd = %d, n = %d: %s", h->bulk_in.get(), n, strerror(errno)); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | buf += n; |
| 138 | len -= n; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 139 | } |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 140 | |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 141 | D("[ done fd=%d ]", h->bulk_in.get()); |
Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 142 | return orig_len; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 145 | static int usb_ffs_read(usb_handle* h, void* data, int len) { |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 146 | D("about to read (fd=%d, len=%d)", h->bulk_out.get(), len); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 147 | |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 148 | char* buf = static_cast<char*>(data); |
Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 149 | int orig_len = len; |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 150 | while (len > 0) { |
Jerry Zhang | 99499f1 | 2018-03-14 14:57:31 -0700 | [diff] [blame] | 151 | int read_len = std::min(USB_FFS_BULK_SIZE, len); |
Josh Gao | 0b19540 | 2015-12-16 11:00:08 -0800 | [diff] [blame] | 152 | int n = adb_read(h->bulk_out, buf, read_len); |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 153 | if (n < 0) { |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 154 | D("ERROR: fd = %d, n = %d: %s", h->bulk_out.get(), n, strerror(errno)); |
Elliott Hughes | 8fcd8bc | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 155 | return -1; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 156 | } |
Josh Gao | 6b531c4 | 2015-12-02 17:30:58 -0800 | [diff] [blame] | 157 | buf += n; |
| 158 | len -= n; |
Elliott Hughes | 8fcd8bc | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 159 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 160 | |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 161 | D("[ done fd=%d ]", h->bulk_out.get()); |
Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 162 | return orig_len; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 165 | static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) { |
| 166 | aio_block* aiob = read ? &h->read_aiob : &h->write_aiob; |
| 167 | bool zero_packet = false; |
| 168 | |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 169 | int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 170 | const char* cur_data = reinterpret_cast<const char*>(data); |
| 171 | int packet_size = getMaxPacketSize(aiob->fd); |
| 172 | |
| 173 | if (posix_madvise(const_cast<void*>(data), len, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED) < |
| 174 | 0) { |
| 175 | D("[ Failed to madvise: %d ]", errno); |
| 176 | } |
| 177 | |
| 178 | for (int i = 0; i < num_bufs; i++) { |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 179 | int buf_len = std::min(len, static_cast<int>(h->io_size)); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 180 | io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read); |
| 181 | |
| 182 | len -= buf_len; |
| 183 | cur_data += buf_len; |
| 184 | |
| 185 | if (len == 0 && buf_len % packet_size == 0 && read) { |
| 186 | // adb does not expect the device to send a zero packet after data transfer, |
| 187 | // but the host *does* send a zero packet for the device to read. |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 188 | zero_packet = h->reads_zero_packets; |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | if (zero_packet) { |
| 192 | io_prep(&aiob->iocb[num_bufs], aiob->fd, reinterpret_cast<const void*>(cur_data), |
| 193 | packet_size, 0, read); |
| 194 | num_bufs += 1; |
| 195 | } |
| 196 | |
Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 197 | while (true) { |
| 198 | if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) { |
| 199 | PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write"); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 200 | return -1; |
| 201 | } |
Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 202 | if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(), |
| 203 | nullptr)) < num_bufs) { |
| 204 | PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write"); |
| 205 | return -1; |
| 206 | } |
| 207 | if (num_bufs == 1 && aiob->events[0].res == -EINTR) { |
| 208 | continue; |
| 209 | } |
Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 210 | int ret = 0; |
Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 211 | for (int i = 0; i < num_bufs; i++) { |
| 212 | if (aiob->events[i].res < 0) { |
| 213 | errno = -aiob->events[i].res; |
| 214 | PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write") |
| 215 | << " total bufs " << num_bufs; |
| 216 | return -1; |
| 217 | } |
Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 218 | ret += aiob->events[i].res; |
Jerry Zhang | 5ba6802 | 2018-03-19 17:54:39 -0700 | [diff] [blame] | 219 | } |
Jerry Zhang | 16b78db | 2018-05-15 16:20:41 -0700 | [diff] [blame] | 220 | return ret; |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 221 | } |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int usb_ffs_aio_read(usb_handle* h, void* data, int len) { |
| 225 | return usb_ffs_do_aio(h, data, len, true); |
| 226 | } |
| 227 | |
| 228 | static int usb_ffs_aio_write(usb_handle* h, const void* data, int len) { |
| 229 | return usb_ffs_do_aio(h, data, len, false); |
| 230 | } |
| 231 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 232 | static void usb_ffs_kick(usb_handle* h) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 233 | int err; |
| 234 | |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 235 | err = ioctl(h->bulk_in.get(), FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 236 | if (err < 0) { |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 237 | D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in.get(), errno); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 238 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 239 | |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 240 | err = ioctl(h->bulk_out.get(), FUNCTIONFS_CLEAR_HALT); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 241 | if (err < 0) { |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 242 | D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out.get(), errno); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 243 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 244 | |
Jack Pham | 4cbf1d8 | 2013-12-23 17:46:10 -0800 | [diff] [blame] | 245 | // don't close ep0 here, since we may not need to reinitialize it with |
| 246 | // the same descriptors again. if however ep1/ep2 fail to re-open in |
| 247 | // init_functionfs, only then would we close and open ep0 again. |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 248 | // Ditto the comment in usb_adb_kick. |
| 249 | h->kicked = true; |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 250 | TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_out.get())); |
| 251 | TEMP_FAILURE_RETRY(dup2(dummy_fd.get(), h->bulk_in.get())); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 254 | static void usb_ffs_close(usb_handle* h) { |
Josh Gao | 184f571 | 2017-07-26 11:06:55 -0700 | [diff] [blame] | 255 | LOG(INFO) << "closing functionfs transport"; |
| 256 | |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 257 | h->kicked = false; |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 258 | h->bulk_out.reset(); |
| 259 | h->bulk_in.reset(); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 260 | |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 261 | // Notify usb_adb_open_thread to open a new connection. |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 262 | h->lock.lock(); |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 263 | h->open_new_connection = true; |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 264 | h->lock.unlock(); |
| 265 | h->notify.notify_one(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 268 | usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size) { |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 269 | usb_handle* h = new usb_handle(); |
Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 270 | |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 271 | if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) { |
| 272 | // Devices on older kernels (< 3.18) will not have aio support for ffs |
| 273 | // unless backported. Fall back on the non-aio functions instead. |
| 274 | h->write = usb_ffs_write; |
| 275 | h->read = usb_ffs_read; |
| 276 | } else { |
| 277 | h->write = usb_ffs_aio_write; |
| 278 | h->read = usb_ffs_aio_read; |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 279 | aio_block_init(&h->read_aiob, num_bufs); |
| 280 | aio_block_init(&h->write_aiob, num_bufs); |
Jerry Zhang | ecee434 | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 281 | } |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 282 | h->io_size = io_size; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 283 | h->kick = usb_ffs_kick; |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 284 | h->close = usb_ffs_close; |
Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 285 | return h; |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 288 | void usb_init() { |
Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 289 | D("[ usb_init - using FunctionFS ]"); |
Josh Gao | 860cc5a | 2018-08-21 15:44:49 -0700 | [diff] [blame] | 290 | dummy_fd.reset(adb_open("/dev/null", O_WRONLY | O_CLOEXEC)); |
| 291 | CHECK_NE(-1, dummy_fd.get()); |
Jerry Zhang | b156c60 | 2018-05-07 15:14:47 -0700 | [diff] [blame] | 292 | |
Jerry Zhang | cda7c3b | 2018-05-21 14:22:43 -0700 | [diff] [blame] | 293 | std::thread(usb_ffs_open_thread, create_usb_handle(USB_FFS_NUM_BUFS, USB_FFS_BULK_SIZE)).detach(); |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 296 | int usb_write(usb_handle* h, const void* data, int len) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 297 | return h->write(h, data, len); |
| 298 | } |
| 299 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 300 | int usb_read(usb_handle* h, void* data, int len) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 301 | return h->read(h, data, len); |
| 302 | } |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 303 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 304 | int usb_close(usb_handle* h) { |
Yabin Cui | 9b53e4c | 2016-04-05 14:51:52 -0700 | [diff] [blame] | 305 | h->close(h); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 306 | return 0; |
| 307 | } |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 308 | |
Josh Gao | 183b73e | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 309 | void usb_kick(usb_handle* h) { |
Andrzej Pietrasiewicz | fd96db1 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 310 | h->kick(h); |
| 311 | } |