@@ -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
}
@@ -318,6 +318,7 @@ static 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:
@@ -408,7 +409,7 @@ 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) {
@@ -507,7 +508,7 @@ static void usb_ffs_close(usb_handle* h) {
507
508
h->notify .notify_one ();
508
509
}
509
510
510
- usb_handle *create_usb_handle () {
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,9 +519,10 @@ usb_handle *create_usb_handle() {
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
528
return h;
@@ -531,7 +533,7 @@ void usb_init() {
531
533
dummy_fd = adb_open (" /dev/null" , O_WRONLY);
532
534
CHECK_NE (dummy_fd, -1 );
533
535
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 ();
535
537
}
536
538
537
539
int usb_write (usb_handle* h, const void * data, int len) {
0 commit comments