Skip to content

Commit 16b78db

Browse files
author
Jerry Zhang
committed
adb: Have device usb_handle return io size
Previously, read and write would return 0 on success. Now it will return the number of bytes read/write. This is more consistent with other usb handles and is needed in order to handle partial packets (for fastbootd). Update usb_write in other usb handles to return amount written. Change transport_usb accordingly. Test: adb works Bug: 78793464 Change-Id: If07ff05fbc8120343f20661475d34f4e5ff805de
1 parent b156c60 commit 16b78db

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

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.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ static int usb_ffs_write(usb_handle* h, const void* data, int len) {
368368
D("about to write (fd=%d, len=%d)", h->bulk_in, len);
369369

370370
const char* buf = static_cast<const char*>(data);
371+
int orig_len = len;
371372
while (len > 0) {
372373
int write_len = std::min(USB_FFS_BULK_SIZE, len);
373374
int n = adb_write(h->bulk_in, buf, write_len);
@@ -380,13 +381,14 @@ static int usb_ffs_write(usb_handle* h, const void* data, int len) {
380381
}
381382

382383
D("[ done fd=%d ]", h->bulk_in);
383-
return 0;
384+
return orig_len;
384385
}
385386

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

389390
char* buf = static_cast<char*>(data);
391+
int orig_len = len;
390392
while (len > 0) {
391393
int read_len = std::min(USB_FFS_BULK_SIZE, len);
392394
int n = adb_read(h->bulk_out, buf, read_len);
@@ -399,7 +401,7 @@ static int usb_ffs_read(usb_handle* h, void* data, int len) {
399401
}
400402

401403
D("[ done fd=%d ]", h->bulk_out);
402-
return 0;
404+
return orig_len;
403405
}
404406

405407
static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
@@ -447,15 +449,17 @@ static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
447449
if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
448450
continue;
449451
}
452+
int ret = 0;
450453
for (int i = 0; i < num_bufs; i++) {
451454
if (aiob->events[i].res < 0) {
452455
errno = -aiob->events[i].res;
453456
PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
454457
<< " total bufs " << num_bufs;
455458
return -1;
456459
}
460+
ret += aiob->events[i].res;
457461
}
458-
return 0;
462+
return ret;
459463
}
460464
}
461465

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)