Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aff7bd0c authored by Jerry Zhang's avatar Jerry Zhang Committed by android-build-merger
Browse files

Merge "Use FUNCTIONFS_ENDPOINT_ALLOC_BUFFER ioctl to improve performance." am:...

Merge "Use FUNCTIONFS_ENDPOINT_ALLOC_BUFFER ioctl to improve performance." am: 20adbc00 am: ba6324e7
am: 7ceeb445

Change-Id: I46a95f788daf5c91219adb1e651324801df47680
parents cb680c9c 7ceeb445
Loading
Loading
Loading
Loading
+23 −0
Original line number Original line Diff line number Diff line
@@ -38,6 +38,8 @@
#define cpu_to_le16(x)  htole16(x)
#define cpu_to_le16(x)  htole16(x)
#define cpu_to_le32(x)  htole32(x)
#define cpu_to_le32(x)  htole32(x)


#define FUNCTIONFS_ENDPOINT_ALLOC       _IOR('g', 131, __u32)

namespace {
namespace {


constexpr char FFS_MTP_EP_IN[] = "/dev/usb-ffs/mtp/ep1";
constexpr char FFS_MTP_EP_IN[] = "/dev/usb-ffs/mtp/ep1";
@@ -467,6 +469,24 @@ void MtpFfsHandle::close() {
    mLock.unlock();
    mLock.unlock();
}
}


class ScopedEndpointBufferAlloc {
private:
    const int mFd;
    const unsigned int mAllocSize;
public:
    ScopedEndpointBufferAlloc(int fd, unsigned alloc_size) :
        mFd(fd),
        mAllocSize(alloc_size) {
        if (ioctl(mFd, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(mAllocSize)))
            PLOG(DEBUG) << "FFS endpoint alloc failed!";
    }

    ~ScopedEndpointBufferAlloc() {
        if (ioctl(mFd, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(0)))
            PLOG(DEBUG) << "FFS endpoint alloc reset failed!";
    }
};

/* Read from USB and write to a local file. */
/* Read from USB and write to a local file. */
int MtpFfsHandle::receiveFile(mtp_file_range mfr) {
int MtpFfsHandle::receiveFile(mtp_file_range mfr) {
    // When receiving files, the incoming length is given in 32 bits.
    // When receiving files, the incoming length is given in 32 bits.
@@ -494,6 +514,7 @@ int MtpFfsHandle::receiveFile(mtp_file_range mfr) {
    bool write = false;
    bool write = false;


    posix_fadvise(mfr.fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE);
    posix_fadvise(mfr.fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE);
    ScopedEndpointBufferAlloc(mBulkOut, mMaxRead);


    // Break down the file into pieces that fit in buffers
    // Break down the file into pieces that fit in buffers
    while (file_length > 0 || write) {
    while (file_length > 0 || write) {
@@ -609,6 +630,8 @@ int MtpFfsHandle::sendFile(mtp_file_range mfr) {
    if (writeHandle(mBulkIn, data, packet_size) == -1) return -1;
    if (writeHandle(mBulkIn, data, packet_size) == -1) return -1;
    if (file_length == 0) return 0;
    if (file_length == 0) return 0;


    ScopedEndpointBufferAlloc(mBulkIn, mMaxWrite);

    // Break down the file into pieces that fit in buffers
    // Break down the file into pieces that fit in buffers
    while(file_length > 0) {
    while(file_length > 0) {
        if (read) {
        if (read) {