Skip to content

Commit cb140c0

Browse files
Jerry ZhangGerrit Code Review
authored andcommitted
Merge changes I31e444f3,If07ff05f,If3ba190d
* changes: adb: Add io size and zero packet to usb_handle adb: Have device usb_handle return io size adb: Expose device usb_handle through libadbd
2 parents a63b330 + cda7c3b commit cb140c0

File tree

8 files changed

+50
-38
lines changed

8 files changed

+50
-38
lines changed

adb/Android.bp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ cc_library_static {
299299
"libqemu_pipe",
300300
"libbase",
301301
],
302+
303+
export_include_dirs: [
304+
"daemon/include",
305+
],
302306
}
303307

304308
cc_binary {

adb/client/usb_libusb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ int usb_write(usb_handle* h, const void* d, int len) {
589589

590590
int rc = perform_usb_transfer(h, info, std::move(lock));
591591
LOG(DEBUG) << "usb_write(" << len << ") = " << rc;
592-
return rc;
592+
return info->transfer->actual_length;
593593
}
594594

595595
int usb_read(usb_handle* h, void* d, int len) {

adb/client/usb_linux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,11 @@ int usb_write(usb_handle *h, const void *_data, int len)
418418
if (h->zero_mask && !(len & h->zero_mask)) {
419419
// If we need 0-markers and our transfer is an even multiple of the packet size,
420420
// then send a zero marker.
421-
return usb_bulk_write(h, _data, 0);
421+
return usb_bulk_write(h, _data, 0) == 0 ? n : -1;
422422
}
423423

424424
D("-- usb_write --");
425-
return 0;
425+
return n;
426426
}
427427

428428
int usb_read(usb_handle *h, void *_data, int len)

adb/client/usb_osx.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ int usb_write(usb_handle *handle, const void *buf, int len)
497497
}
498498
}
499499

500-
if (0 == result)
501-
return 0;
500+
if (!result)
501+
return len;
502502

503503
LOG(ERROR) << "usb_write failed with status: " << std::hex << result;
504504
return -1;

adb/client/usb_windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ int usb_write(usb_handle* handle, const void* data, int len) {
365365
}
366366
}
367367

368-
return 0;
368+
return written;
369369

370370
fail:
371371
// Any failure should cause us to kick the device instead of leaving it a

adb/daemon/usb.h renamed to adb/daemon/include/adbd/usb.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <atomic>
2020
#include <condition_variable>
2121
#include <mutex>
22+
#include <vector>
2223

2324
#include <asyncio/AsyncIO.h>
2425

@@ -54,5 +55,9 @@ struct usb_handle {
5455
// read and write threads.
5556
struct aio_block read_aiob;
5657
struct aio_block write_aiob;
58+
59+
bool reads_zero_packets;
60+
size_t io_size;
5761
};
5862

63+
usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size);

adb/daemon/usb.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <android-base/properties.h>
4242

4343
#include "adb.h"
44-
#include "daemon/usb.h"
44+
#include "adbd/usb.h"
4545
#include "transport.h"
4646

4747
using namespace std::chrono_literals;
@@ -53,7 +53,7 @@ using namespace std::chrono_literals;
5353
#define USB_FFS_BULK_SIZE 16384
5454

5555
// Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
56-
#define USB_FFS_NUM_BUFS ((MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
56+
#define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
5757

5858
#define cpu_to_le16(x) htole16(x)
5959
#define cpu_to_le32(x) htole32(x)
@@ -226,16 +226,16 @@ static const struct {
226226
},
227227
};
228228

229-
static void aio_block_init(aio_block* aiob) {
230-
aiob->iocb.resize(USB_FFS_NUM_BUFS);
231-
aiob->iocbs.resize(USB_FFS_NUM_BUFS);
232-
aiob->events.resize(USB_FFS_NUM_BUFS);
229+
static void aio_block_init(aio_block* aiob, unsigned num_bufs) {
230+
aiob->iocb.resize(num_bufs);
231+
aiob->iocbs.resize(num_bufs);
232+
aiob->events.resize(num_bufs);
233233
aiob->num_submitted = 0;
234-
for (unsigned i = 0; i < USB_FFS_NUM_BUFS; i++) {
234+
for (unsigned i = 0; i < num_bufs; i++) {
235235
aiob->iocbs[i] = &aiob->iocb[i];
236236
}
237237
memset(&aiob->ctx, 0, sizeof(aiob->ctx));
238-
if (io_setup(USB_FFS_NUM_BUFS, &aiob->ctx)) {
238+
if (io_setup(num_bufs, &aiob->ctx)) {
239239
D("[ aio: got error on io_setup (%d) ]", errno);
240240
}
241241
}
@@ -250,7 +250,7 @@ static int getMaxPacketSize(int ffs_fd) {
250250
}
251251
}
252252

253-
bool init_functionfs(struct usb_handle* h) {
253+
static bool init_functionfs(struct usb_handle* h) {
254254
LOG(INFO) << "initializing functionfs";
255255

256256
ssize_t ret;
@@ -318,6 +318,7 @@ bool init_functionfs(struct usb_handle* h) {
318318

319319
h->read_aiob.fd = h->bulk_out;
320320
h->write_aiob.fd = h->bulk_in;
321+
h->reads_zero_packets = true;
321322
return true;
322323

323324
err:
@@ -336,9 +337,7 @@ bool init_functionfs(struct usb_handle* h) {
336337
return false;
337338
}
338339

339-
static void usb_ffs_open_thread(void* x) {
340-
struct usb_handle* usb = (struct usb_handle*)x;
341-
340+
static void usb_ffs_open_thread(usb_handle *usb) {
342341
adb_thread_setname("usb ffs open");
343342

344343
while (true) {
@@ -370,6 +369,7 @@ static int usb_ffs_write(usb_handle* h, const void* data, int len) {
370369
D("about to write (fd=%d, len=%d)", h->bulk_in, len);
371370

372371
const char* buf = static_cast<const char*>(data);
372+
int orig_len = len;
373373
while (len > 0) {
374374
int write_len = std::min(USB_FFS_BULK_SIZE, len);
375375
int n = adb_write(h->bulk_in, buf, write_len);
@@ -382,13 +382,14 @@ static int usb_ffs_write(usb_handle* h, const void* data, int len) {
382382
}
383383

384384
D("[ done fd=%d ]", h->bulk_in);
385-
return 0;
385+
return orig_len;
386386
}
387387

388388
static int usb_ffs_read(usb_handle* h, void* data, int len) {
389389
D("about to read (fd=%d, len=%d)", h->bulk_out, len);
390390

391391
char* buf = static_cast<char*>(data);
392+
int orig_len = len;
392393
while (len > 0) {
393394
int read_len = std::min(USB_FFS_BULK_SIZE, len);
394395
int n = adb_read(h->bulk_out, buf, read_len);
@@ -401,14 +402,14 @@ static int usb_ffs_read(usb_handle* h, void* data, int len) {
401402
}
402403

403404
D("[ done fd=%d ]", h->bulk_out);
404-
return 0;
405+
return orig_len;
405406
}
406407

407408
static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
408409
aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
409410
bool zero_packet = false;
410411

411-
int num_bufs = len / USB_FFS_BULK_SIZE + (len % USB_FFS_BULK_SIZE == 0 ? 0 : 1);
412+
int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1);
412413
const char* cur_data = reinterpret_cast<const char*>(data);
413414
int packet_size = getMaxPacketSize(aiob->fd);
414415

@@ -418,7 +419,7 @@ static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
418419
}
419420

420421
for (int i = 0; i < num_bufs; i++) {
421-
int buf_len = std::min(len, USB_FFS_BULK_SIZE);
422+
int buf_len = std::min(len, static_cast<int>(h->io_size));
422423
io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
423424

424425
len -= buf_len;
@@ -427,7 +428,7 @@ static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
427428
if (len == 0 && buf_len % packet_size == 0 && read) {
428429
// adb does not expect the device to send a zero packet after data transfer,
429430
// but the host *does* send a zero packet for the device to read.
430-
zero_packet = true;
431+
zero_packet = h->reads_zero_packets;
431432
}
432433
}
433434
if (zero_packet) {
@@ -449,15 +450,17 @@ static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
449450
if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
450451
continue;
451452
}
453+
int ret = 0;
452454
for (int i = 0; i < num_bufs; i++) {
453455
if (aiob->events[i].res < 0) {
454456
errno = -aiob->events[i].res;
455457
PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
456458
<< " total bufs " << num_bufs;
457459
return -1;
458460
}
461+
ret += aiob->events[i].res;
459462
}
460-
return 0;
463+
return ret;
461464
}
462465
}
463466

@@ -505,9 +508,7 @@ static void usb_ffs_close(usb_handle* h) {
505508
h->notify.notify_one();
506509
}
507510

508-
static void usb_ffs_init() {
509-
D("[ usb_init - using FunctionFS ]");
510-
511+
usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size) {
511512
usb_handle* h = new usb_handle();
512513

513514
if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
@@ -518,20 +519,21 @@ static void usb_ffs_init() {
518519
} else {
519520
h->write = usb_ffs_aio_write;
520521
h->read = usb_ffs_aio_read;
521-
aio_block_init(&h->read_aiob);
522-
aio_block_init(&h->write_aiob);
522+
aio_block_init(&h->read_aiob, num_bufs);
523+
aio_block_init(&h->write_aiob, num_bufs);
523524
}
525+
h->io_size = io_size;
524526
h->kick = usb_ffs_kick;
525527
h->close = usb_ffs_close;
526-
527-
D("[ usb_init - starting thread ]");
528-
std::thread(usb_ffs_open_thread, h).detach();
528+
return h;
529529
}
530530

531531
void usb_init() {
532+
D("[ usb_init - using FunctionFS ]");
532533
dummy_fd = adb_open("/dev/null", O_WRONLY);
533534
CHECK_NE(dummy_fd, -1);
534-
usb_ffs_init();
535+
536+
std::thread(usb_ffs_open_thread, create_usb_handle(USB_FFS_NUM_BUFS, USB_FFS_BULK_SIZE)).detach();
535537
}
536538

537539
int usb_write(usb_handle* h, const void* data, int len) {

adb/transport_usb.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static int remote_read(apacket* p, usb_handle* usb) {
122122
// On Android devices, we rely on the kernel to provide buffered read.
123123
// So we can recover automatically from EOVERFLOW.
124124
static int remote_read(apacket* p, usb_handle* usb) {
125-
if (usb_read(usb, &p->msg, sizeof(amessage))) {
125+
if (usb_read(usb, &p->msg, sizeof(amessage)) != sizeof(amessage)) {
126126
PLOG(ERROR) << "remote usb: read terminated (message)";
127127
return -1;
128128
}
@@ -134,7 +134,8 @@ static int remote_read(apacket* p, usb_handle* usb) {
134134
}
135135

136136
p->payload.resize(p->msg.data_length);
137-
if (usb_read(usb, &p->payload[0], p->payload.size())) {
137+
if (usb_read(usb, &p->payload[0], p->payload.size())
138+
!= static_cast<int>(p->payload.size())) {
138139
PLOG(ERROR) << "remote usb: terminated (data)";
139140
return -1;
140141
}
@@ -154,14 +155,14 @@ bool UsbConnection::Read(apacket* packet) {
154155
}
155156

156157
bool UsbConnection::Write(apacket* packet) {
157-
unsigned size = packet->msg.data_length;
158+
int size = packet->msg.data_length;
158159

159-
if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != 0) {
160+
if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != sizeof(packet->msg)) {
160161
PLOG(ERROR) << "remote usb: 1 - write terminated";
161162
return false;
162163
}
163164

164-
if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != 0) {
165+
if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != size) {
165166
PLOG(ERROR) << "remote usb: 2 - write terminated";
166167
return false;
167168
}

0 commit comments

Comments
 (0)