41
41
#include < android-base/properties.h>
42
42
43
43
#include " adb.h"
44
- #include " daemon /usb.h"
44
+ #include " adbd /usb.h"
45
45
#include " transport.h"
46
46
47
47
using namespace std ::chrono_literals;
@@ -53,7 +53,7 @@ using namespace std::chrono_literals;
53
53
#define USB_FFS_BULK_SIZE 16384
54
54
55
55
// 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 )
57
57
58
58
#define cpu_to_le16 (x ) htole16(x)
59
59
#define cpu_to_le32 (x ) htole32(x)
@@ -226,16 +226,16 @@ static const struct {
226
226
},
227
227
};
228
228
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 );
233
233
aiob->num_submitted = 0 ;
234
- for (unsigned i = 0 ; i < USB_FFS_NUM_BUFS ; i++) {
234
+ for (unsigned i = 0 ; i < num_bufs ; i++) {
235
235
aiob->iocbs [i] = &aiob->iocb [i];
236
236
}
237
237
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 )) {
239
239
D (" [ aio: got error on io_setup (%d) ]" , errno);
240
240
}
241
241
}
@@ -250,7 +250,7 @@ static int getMaxPacketSize(int ffs_fd) {
250
250
}
251
251
}
252
252
253
- bool init_functionfs (struct usb_handle * h) {
253
+ static bool init_functionfs (struct usb_handle * h) {
254
254
LOG (INFO) << " initializing functionfs" ;
255
255
256
256
ssize_t ret;
@@ -318,6 +318,7 @@ bool init_functionfs(struct usb_handle* h) {
318
318
319
319
h->read_aiob .fd = h->bulk_out ;
320
320
h->write_aiob .fd = h->bulk_in ;
321
+ h->reads_zero_packets = true ;
321
322
return true ;
322
323
323
324
err:
@@ -336,9 +337,7 @@ bool init_functionfs(struct usb_handle* h) {
336
337
return false ;
337
338
}
338
339
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) {
342
341
adb_thread_setname (" usb ffs open" );
343
342
344
343
while (true ) {
@@ -370,6 +369,7 @@ static int usb_ffs_write(usb_handle* h, const void* data, int len) {
370
369
D (" about to write (fd=%d, len=%d)" , h->bulk_in , len);
371
370
372
371
const char * buf = static_cast <const char *>(data);
372
+ int orig_len = len;
373
373
while (len > 0 ) {
374
374
int write_len = std::min (USB_FFS_BULK_SIZE, len);
375
375
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) {
382
382
}
383
383
384
384
D (" [ done fd=%d ]" , h->bulk_in );
385
- return 0 ;
385
+ return orig_len ;
386
386
}
387
387
388
388
static int usb_ffs_read (usb_handle* h, void * data, int len) {
389
389
D (" about to read (fd=%d, len=%d)" , h->bulk_out , len);
390
390
391
391
char * buf = static_cast <char *>(data);
392
+ int orig_len = len;
392
393
while (len > 0 ) {
393
394
int read_len = std::min (USB_FFS_BULK_SIZE, len);
394
395
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) {
401
402
}
402
403
403
404
D (" [ done fd=%d ]" , h->bulk_out );
404
- return 0 ;
405
+ return orig_len ;
405
406
}
406
407
407
408
static int usb_ffs_do_aio (usb_handle* h, const void * data, int len, bool read) {
408
409
aio_block* aiob = read ? &h->read_aiob : &h->write_aiob ;
409
410
bool zero_packet = false ;
410
411
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 );
412
413
const char * cur_data = reinterpret_cast <const char *>(data);
413
414
int packet_size = getMaxPacketSize (aiob->fd );
414
415
@@ -418,7 +419,7 @@ static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
418
419
}
419
420
420
421
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 ) );
422
423
io_prep (&aiob->iocb [i], aiob->fd , cur_data, buf_len, 0 , read);
423
424
424
425
len -= buf_len;
@@ -427,7 +428,7 @@ static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
427
428
if (len == 0 && buf_len % packet_size == 0 && read) {
428
429
// adb does not expect the device to send a zero packet after data transfer,
429
430
// but the host *does* send a zero packet for the device to read.
430
- zero_packet = true ;
431
+ zero_packet = h-> reads_zero_packets ;
431
432
}
432
433
}
433
434
if (zero_packet) {
@@ -449,15 +450,17 @@ static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
449
450
if (num_bufs == 1 && aiob->events [0 ].res == -EINTR) {
450
451
continue ;
451
452
}
453
+ int ret = 0 ;
452
454
for (int i = 0 ; i < num_bufs; i++) {
453
455
if (aiob->events [i].res < 0 ) {
454
456
errno = -aiob->events [i].res ;
455
457
PLOG (ERROR) << " aio: got error event on " << (read ? " read" : " write" )
456
458
<< " total bufs " << num_bufs;
457
459
return -1 ;
458
460
}
461
+ ret += aiob->events [i].res ;
459
462
}
460
- return 0 ;
463
+ return ret ;
461
464
}
462
465
}
463
466
@@ -505,9 +508,7 @@ static void usb_ffs_close(usb_handle* h) {
505
508
h->notify .notify_one ();
506
509
}
507
510
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) {
511
512
usb_handle* h = new usb_handle ();
512
513
513
514
if (android::base::GetBoolProperty (" sys.usb.ffs.aio_compat" , false )) {
@@ -518,20 +519,21 @@ static void usb_ffs_init() {
518
519
} else {
519
520
h->write = usb_ffs_aio_write;
520
521
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 );
523
524
}
525
+ h->io_size = io_size;
524
526
h->kick = usb_ffs_kick;
525
527
h->close = usb_ffs_close;
526
-
527
- D (" [ usb_init - starting thread ]" );
528
- std::thread (usb_ffs_open_thread, h).detach ();
528
+ return h;
529
529
}
530
530
531
531
void usb_init () {
532
+ D (" [ usb_init - using FunctionFS ]" );
532
533
dummy_fd = adb_open (" /dev/null" , O_WRONLY);
533
534
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 ();
535
537
}
536
538
537
539
int usb_write (usb_handle* h, const void * data, int len) {
0 commit comments