Remove configure() from Mtp

Instead of configuring the server each
time the connection happens, UsbService
will now pass in the control fd with the
descriptors already written. Also, PTP
endpoints have been moved to their own
directory at /dev/usb-ffs/ptp rather than
using the same ones as mtp.

Bug: 72877174
Test: Verify PTP and MTP config changes work
Change-Id: I9a0336afd610aa397c9947568c308afb89b27c08
diff --git a/media/mtp/MtpFfsHandle.cpp b/media/mtp/MtpFfsHandle.cpp
index 217e0c9..952b907 100644
--- a/media/mtp/MtpFfsHandle.cpp
+++ b/media/mtp/MtpFfsHandle.cpp
@@ -39,10 +39,6 @@
 
 namespace {
 
-constexpr char FFS_MTP_EP_IN[] = "/dev/usb-ffs/mtp/ep1";
-constexpr char FFS_MTP_EP_OUT[] = "/dev/usb-ffs/mtp/ep2";
-constexpr char FFS_MTP_EP_INTR[] = "/dev/usb-ffs/mtp/ep3";
-
 constexpr unsigned AIO_BUFS_MAX = 128;
 constexpr unsigned AIO_BUF_LEN = 16384;
 
@@ -73,7 +69,9 @@
     }
 }
 
-MtpFfsHandle::MtpFfsHandle() {}
+MtpFfsHandle::MtpFfsHandle(int controlFd) {
+    mControl.reset(controlFd);
+}
 
 MtpFfsHandle::~MtpFfsHandle() {}
 
@@ -83,27 +81,27 @@
     mBulkOut.reset();
 }
 
-bool MtpFfsHandle::openEndpoints() {
+bool MtpFfsHandle::openEndpoints(bool ptp) {
     if (mBulkIn < 0) {
-        mBulkIn.reset(TEMP_FAILURE_RETRY(open(FFS_MTP_EP_IN, O_RDWR)));
+        mBulkIn.reset(TEMP_FAILURE_RETRY(open(ptp ? FFS_PTP_EP_IN : FFS_MTP_EP_IN, O_RDWR)));
         if (mBulkIn < 0) {
-            PLOG(ERROR) << FFS_MTP_EP_IN << ": cannot open bulk in ep";
+            PLOG(ERROR) << (ptp ? FFS_PTP_EP_IN : FFS_MTP_EP_IN) << ": cannot open bulk in ep";
             return false;
         }
     }
 
     if (mBulkOut < 0) {
-        mBulkOut.reset(TEMP_FAILURE_RETRY(open(FFS_MTP_EP_OUT, O_RDWR)));
+        mBulkOut.reset(TEMP_FAILURE_RETRY(open(ptp ? FFS_PTP_EP_OUT : FFS_MTP_EP_OUT, O_RDWR)));
         if (mBulkOut < 0) {
-            PLOG(ERROR) << FFS_MTP_EP_OUT << ": cannot open bulk out ep";
+            PLOG(ERROR) << (ptp ? FFS_PTP_EP_OUT : FFS_MTP_EP_OUT) << ": cannot open bulk out ep";
             return false;
         }
     }
 
     if (mIntr < 0) {
-        mIntr.reset(TEMP_FAILURE_RETRY(open(FFS_MTP_EP_INTR, O_RDWR)));
+        mIntr.reset(TEMP_FAILURE_RETRY(open(ptp ? FFS_PTP_EP_INTR : FFS_MTP_EP_INTR, O_RDWR)));
         if (mIntr < 0) {
-            PLOG(ERROR) << FFS_MTP_EP_INTR << ": cannot open intr ep";
+            PLOG(ERROR) << (ptp ? FFS_PTP_EP_INTR : FFS_MTP_EP_INTR) << ": cannot open intr ep";
             return false;
         }
     }
@@ -121,39 +119,8 @@
         PLOG(ERROR) << "Failed to fadvise";
 }
 
-bool MtpFfsHandle::initFunctionfs() {
-    if (mControl < 0) { // might have already done this before
-        mControl.reset(TEMP_FAILURE_RETRY(open(FFS_MTP_EP0, O_RDWR)));
-        if (mControl < 0) {
-            PLOG(ERROR) << FFS_MTP_EP0 << ": cannot open control endpoint";
-            return false;
-        }
-        if (!writeDescriptors()) {
-            closeConfig();
-            return false;
-        }
-    }
-    return true;
-}
-
-bool MtpFfsHandle::writeDescriptors() {
-    ssize_t ret = TEMP_FAILURE_RETRY(::write(mControl,
-                &(mPtp ? ptp_desc_v2 : mtp_desc_v2), sizeof(desc_v2)));
-    if (ret < 0) {
-        PLOG(ERROR) << FFS_MTP_EP0 << "Switching to V1 descriptor format";
-        ret = TEMP_FAILURE_RETRY(::write(mControl,
-                    &(mPtp ? ptp_desc_v1 : mtp_desc_v1), sizeof(desc_v1)));
-        if (ret < 0) {
-            PLOG(ERROR) << FFS_MTP_EP0 << "Writing descriptors failed";
-            return false;
-        }
-    }
-    ret = TEMP_FAILURE_RETRY(::write(mControl, &mtp_strings, sizeof(mtp_strings)));
-    if (ret < 0) {
-        PLOG(ERROR) << FFS_MTP_EP0 << "Writing strings failed";
-        return false;
-    }
-    return true;
+bool MtpFfsHandle::writeDescriptors(bool ptp) {
+    return ::android::writeDescriptors(mControl, ptp);
 }
 
 void MtpFfsHandle::closeConfig() {
@@ -197,11 +164,9 @@
         switch (event->type) {
         case FUNCTIONFS_BIND:
         case FUNCTIONFS_ENABLE:
-        case FUNCTIONFS_RESUME:
             ret = 0;
             errno = 0;
             break;
-        case FUNCTIONFS_SUSPEND:
         case FUNCTIONFS_UNBIND:
         case FUNCTIONFS_DISABLE:
             errno = ESHUTDOWN;
@@ -211,6 +176,9 @@
             if (handleControlRequest(&event->u.setup) == -1)
                 ret = -1;
             break;
+        case FUNCTIONFS_SUSPEND:
+        case FUNCTIONFS_RESUME:
+            break;
         default:
             LOG(ERROR) << "Mtp Event " << event->type << " (unknown)";
         }
@@ -277,10 +245,8 @@
     return 0;
 }
 
-int MtpFfsHandle::start() {
-    mLock.lock();
-
-    if (!openEndpoints())
+int MtpFfsHandle::start(bool ptp) {
+    if (!openEndpoints(ptp))
         return -1;
 
     for (unsigned i = 0; i < NUM_IO_BUFS; i++) {
@@ -309,33 +275,10 @@
     return 0;
 }
 
-int MtpFfsHandle::configure(bool usePtp) {
-    // Wait till previous server invocation has closed
-    if (!mLock.try_lock_for(std::chrono::milliseconds(300))) {
-        LOG(ERROR) << "MtpServer was unable to get configure lock";
-        return -1;
-    }
-    int ret = 0;
-
-    // If ptp is changed, the configuration must be rewritten
-    if (mPtp != usePtp) {
-        closeEndpoints();
-        closeConfig();
-    }
-    mPtp = usePtp;
-
-    if (!initFunctionfs()) {
-        ret = -1;
-    }
-
-    mLock.unlock();
-    return ret;
-}
-
 void MtpFfsHandle::close() {
     io_destroy(mCtx);
     closeEndpoints();
-    mLock.unlock();
+    closeConfig();
 }
 
 int MtpFfsHandle::waitEvents(struct io_buffer *buf, int min_events, struct io_event *events,