1 /*
2 * Copyright (C) 2023 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-aidl"
18
19 #include <aidl/android/hardware/usb/gadget/GadgetFunction.h>
20 #include <android-base/file.h>
21 #include <android-base/properties.h>
22 #include <utils/Log.h>
23
24 #include "aidl/include/pixelusb/UsbGadgetAidlCommon.h"
25
26 namespace android {
27 namespace hardware {
28 namespace google {
29 namespace pixel {
30 namespace usb {
31
32 using ::aidl::android::hardware::usb::gadget::GadgetFunction;
33 using ::android::base::GetBoolProperty;
34 using ::android::base::GetProperty;
35 using ::android::base::SetProperty;
36 using ::android::base::WriteStringToFile;
37
setVidPid(const char * vid,const char * pid)38 Status setVidPid(const char *vid, const char *pid) {
39 return setVidPidCommon(vid, pid) ? Status::SUCCESS : Status::ERROR;
40 }
41
resetGadget()42 Status resetGadget() {
43 return resetGadgetCommon() ? Status::SUCCESS : Status::ERROR;
44 }
45
addGenericAndroidFunctions(MonitorFfs * monitorFfs,uint64_t functions,bool * ffsEnabled,int * functionCount)46 Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, bool *ffsEnabled,
47 int *functionCount) {
48 if (((functions & GadgetFunction::MTP) != 0)) {
49 *ffsEnabled = true;
50 ALOGI("setCurrentUsbFunctions mtp");
51 if (!WriteStringToFile("1", DESC_USE_PATH))
52 return Status::ERROR;
53
54 if (!monitorFfs->addInotifyFd("/dev/usb-ffs/mtp/"))
55 return Status::ERROR;
56
57 if (linkFunction("ffs.mtp", (*functionCount)++))
58 return Status::ERROR;
59
60 // Add endpoints to be monitored.
61 monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep1");
62 monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep2");
63 monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep3");
64 } else if (((functions & GadgetFunction::PTP) != 0)) {
65 *ffsEnabled = true;
66 ALOGI("setCurrentUsbFunctions ptp");
67 if (!WriteStringToFile("1", DESC_USE_PATH))
68 return Status::ERROR;
69
70 if (!monitorFfs->addInotifyFd("/dev/usb-ffs/ptp/"))
71 return Status::ERROR;
72
73 if (linkFunction("ffs.ptp", (*functionCount)++))
74 return Status::ERROR;
75
76 // Add endpoints to be monitored.
77 monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep1");
78 monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep2");
79 monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep3");
80 }
81
82 if ((functions & GadgetFunction::MIDI) != 0) {
83 ALOGI("setCurrentUsbFunctions MIDI");
84 if (linkFunction("midi.gs5", (*functionCount)++))
85 return Status::ERROR;
86 }
87
88 if ((functions & GadgetFunction::ACCESSORY) != 0) {
89 ALOGI("setCurrentUsbFunctions Accessory");
90 if (linkFunction("accessory.gs2", (*functionCount)++))
91 return Status::ERROR;
92 }
93
94 if ((functions & GadgetFunction::AUDIO_SOURCE) != 0) {
95 ALOGI("setCurrentUsbFunctions Audio Source");
96 if (linkFunction("audio_source.gs3", (*functionCount)++))
97 return Status::ERROR;
98 }
99
100 if ((functions & GadgetFunction::RNDIS) != 0) {
101 ALOGI("setCurrentUsbFunctions rndis");
102 std::string rndisFunction = GetProperty(kVendorRndisConfig, "");
103 if (rndisFunction != "") {
104 if (linkFunction(rndisFunction.c_str(), (*functionCount)++))
105 return Status::ERROR;
106 } else {
107 // link gsi.rndis for older pixel projects
108 if (linkFunction("gsi.rndis", (*functionCount)++))
109 return Status::ERROR;
110 }
111 }
112
113 if ((functions & GadgetFunction::UVC) != 0) {
114 if (!GetBoolProperty(kUvcEnabled, false)) {
115 ALOGE("UVC function disabled by config");
116 return Status::ERROR;
117 }
118
119 ALOGI("setCurrentUsbFunctions uvc");
120 if (linkFunction("uvc.0", (*functionCount)++)) {
121 return Status::ERROR;
122 }
123 }
124
125 return Status::SUCCESS;
126 }
127
addAdb(MonitorFfs * monitorFfs,int * functionCount)128 Status addAdb(MonitorFfs *monitorFfs, int *functionCount) {
129 ALOGI("setCurrentUsbFunctions Adb");
130 if (!WriteStringToFile("1", DESC_USE_PATH))
131 return Status::ERROR;
132
133 if (!monitorFfs->addInotifyFd("/dev/usb-ffs/adb/"))
134 return Status::ERROR;
135
136 if (linkFunction("ffs.adb", (*functionCount)++))
137 return Status::ERROR;
138
139 monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep1");
140 monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep2");
141 ALOGI("Service started");
142 return Status::SUCCESS;
143 }
144
145 } // namespace usb
146 } // namespace pixel
147 } // namespace google
148 } // namespace hardware
149 } // namespace android
150