Skip to content

Commit cda7c3b

Browse files
author
Jerry Zhang
committed
adb: Add io size and zero packet to usb_handle
Fastboot protocol doesn't include zero packets, so make it possible to configure that. Allow fastbootd to see how many bytes the handle can read/write at once. Test: adb works Bug: 78793464 Change-Id: I31e444f384d9d0cdf1ea936439b2028f8151c3b8
1 parent 16b78db commit cda7c3b

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

adb/daemon/include/adbd/usb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ struct usb_handle {
5555
// read and write threads.
5656
struct aio_block read_aiob;
5757
struct aio_block write_aiob;
58+
59+
bool reads_zero_packets;
60+
size_t io_size;
5861
};
5962

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

adb/daemon/usb.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -318,6 +318,7 @@ static 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:
@@ -408,7 +409,7 @@ 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) {
@@ -507,7 +508,7 @@ static void usb_ffs_close(usb_handle* h) {
507508
h->notify.notify_one();
508509
}
509510

510-
usb_handle *create_usb_handle() {
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,9 +519,10 @@ usb_handle *create_usb_handle() {
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;
526528
return h;
@@ -531,7 +533,7 @@ void usb_init() {
531533
dummy_fd = adb_open("/dev/null", O_WRONLY);
532534
CHECK_NE(dummy_fd, -1);
533535

534-
std::thread(usb_ffs_open_thread, create_usb_handle()).detach();
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) {

0 commit comments

Comments
 (0)