1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "libpixelusb-hidl"
18 
19 #include <android-base/file.h>
20 #include <android-base/properties.h>
21 #include <utils/Log.h>
22 
23 #include "hidl/include/pixelusb/UsbGadgetCommon.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace google {
28 namespace pixel {
29 namespace usb {
30 
31 using ::android::base::GetProperty;
32 using ::android::base::SetProperty;
33 using ::android::base::WriteStringToFile;
34 using ::android::hardware::usb::gadget::V1_0::GadgetFunction;
35 
setVidPid(const char * vid,const char * pid)36 Status setVidPid(const char *vid, const char *pid) {
37     return setVidPidCommon(vid, pid) ? Status::SUCCESS : Status::ERROR;
38 }
39 
resetGadget()40 Status resetGadget() {
41     return resetGadgetCommon() ? Status::SUCCESS : Status::ERROR;
42 }
43 
addGenericAndroidFunctions(MonitorFfs * monitorFfs,uint64_t functions,bool * ffsEnabled,int * functionCount)44 Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, bool *ffsEnabled,
45                                   int *functionCount) {
46     if (((functions & GadgetFunction::MTP) != 0)) {
47         *ffsEnabled = true;
48         ALOGI("setCurrentUsbFunctions mtp");
49         if (!WriteStringToFile("1", DESC_USE_PATH))
50             return Status::ERROR;
51 
52         if (!monitorFfs->addInotifyFd("/dev/usb-ffs/mtp/"))
53             return Status::ERROR;
54 
55         if (linkFunction("ffs.mtp", (*functionCount)++))
56             return Status::ERROR;
57 
58         // Add endpoints to be monitored.
59         monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep1");
60         monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep2");
61         monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep3");
62     } else if (((functions & GadgetFunction::PTP) != 0)) {
63         *ffsEnabled = true;
64         ALOGI("setCurrentUsbFunctions ptp");
65         if (!WriteStringToFile("1", DESC_USE_PATH))
66             return Status::ERROR;
67 
68         if (!monitorFfs->addInotifyFd("/dev/usb-ffs/ptp/"))
69             return Status::ERROR;
70 
71         if (linkFunction("ffs.ptp", (*functionCount)++))
72             return Status::ERROR;
73 
74         // Add endpoints to be monitored.
75         monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep1");
76         monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep2");
77         monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep3");
78     }
79 
80     if ((functions & GadgetFunction::MIDI) != 0) {
81         ALOGI("setCurrentUsbFunctions MIDI");
82         if (linkFunction("midi.gs5", (*functionCount)++))
83             return Status::ERROR;
84     }
85 
86     if ((functions & GadgetFunction::ACCESSORY) != 0) {
87         ALOGI("setCurrentUsbFunctions Accessory");
88         if (linkFunction("accessory.gs2", (*functionCount)++))
89             return Status::ERROR;
90     }
91 
92     if ((functions & GadgetFunction::AUDIO_SOURCE) != 0) {
93         ALOGI("setCurrentUsbFunctions Audio Source");
94         if (linkFunction("audio_source.gs3", (*functionCount)++))
95             return Status::ERROR;
96     }
97 
98     if ((functions & GadgetFunction::RNDIS) != 0) {
99         ALOGI("setCurrentUsbFunctions rndis");
100         std::string rndisFunction = GetProperty(kVendorRndisConfig, "");
101         if (rndisFunction != "") {
102             if (linkFunction(rndisFunction.c_str(), (*functionCount)++))
103                 return Status::ERROR;
104         } else {
105             // link gsi.rndis for older pixel projects
106             if (linkFunction("gsi.rndis", (*functionCount)++))
107                 return Status::ERROR;
108         }
109     }
110 
111     return Status::SUCCESS;
112 }
113 
addAdb(MonitorFfs * monitorFfs,int * functionCount)114 Status addAdb(MonitorFfs *monitorFfs, int *functionCount) {
115     ALOGI("setCurrentUsbFunctions Adb");
116     if (!WriteStringToFile("1", DESC_USE_PATH))
117         return Status::ERROR;
118 
119     if (!monitorFfs->addInotifyFd("/dev/usb-ffs/adb/"))
120         return Status::ERROR;
121 
122     if (linkFunction("ffs.adb", (*functionCount)++))
123         return Status::ERROR;
124 
125     monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep1");
126     monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep2");
127     ALOGI("Service started");
128     return Status::SUCCESS;
129 }
130 
131 }  // namespace usb
132 }  // namespace pixel
133 }  // namespace google
134 }  // namespace hardware
135 }  // namespace android
136