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"
18
19 #include "include/pixelusb/UsbGadgetCommon.h"
20
21 namespace android {
22 namespace hardware {
23 namespace google {
24 namespace pixel {
25 namespace usb {
26
unlinkFunctions(const char * path)27 int unlinkFunctions(const char *path) {
28 DIR *config = opendir(path);
29 struct dirent *function;
30 char filepath[kMaxFilePathLength];
31 int ret = 0;
32
33 if (config == NULL)
34 return -1;
35
36 // d_type does not seems to be supported in /config
37 // so filtering by name.
38 while (((function = readdir(config)) != NULL)) {
39 if ((strstr(function->d_name, FUNCTION_NAME) == NULL))
40 continue;
41 // build the path for each file in the folder.
42 snprintf(filepath, kMaxFilePathLength, "%s/%s", path, function->d_name);
43 ret = remove(filepath);
44 if (ret) {
45 ALOGE("Unable remove file %s errno:%d", filepath, errno);
46 break;
47 }
48 }
49
50 closedir(config);
51 return ret;
52 }
53
addEpollFd(const unique_fd & epfd,const unique_fd & fd)54 int addEpollFd(const unique_fd &epfd, const unique_fd &fd) {
55 struct epoll_event event;
56 int ret;
57
58 event.data.fd = fd;
59 event.events = EPOLLIN;
60
61 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
62 if (ret)
63 ALOGE("epoll_ctl error %d", errno);
64
65 return ret;
66 }
67
linkFunction(const char * function,int index)68 int linkFunction(const char *function, int index) {
69 char functionPath[kMaxFilePathLength];
70 char link[kMaxFilePathLength];
71
72 snprintf(functionPath, kMaxFilePathLength, "%s%s", FUNCTIONS_PATH, function);
73 snprintf(link, kMaxFilePathLength, "%s%d", FUNCTION_PATH, index);
74 if (symlink(functionPath, link)) {
75 ALOGE("Cannot create symlink %s -> %s errno:%d", link, functionPath, errno);
76 return -1;
77 }
78 return 0;
79 }
80
setVidPid(const char * vid,const char * pid)81 Status setVidPid(const char *vid, const char *pid) {
82 if (!WriteStringToFile(vid, VENDOR_ID_PATH))
83 return Status::ERROR;
84
85 if (!WriteStringToFile(pid, PRODUCT_ID_PATH))
86 return Status::ERROR;
87
88 return Status::SUCCESS;
89 }
90
getVendorFunctions()91 std::string getVendorFunctions() {
92 if (GetProperty(kBuildType, "") == "user")
93 return "user";
94
95 std::string bootMode = GetProperty(PERSISTENT_BOOT_MODE, "");
96 std::string persistVendorFunctions = GetProperty(kPersistentVendorConfig, "");
97 std::string vendorFunctions = GetProperty(kVendorConfig, "");
98 std::string ret = "";
99
100 if (vendorFunctions != "") {
101 ret = vendorFunctions;
102 } else if (bootMode == "usbradio" || bootMode == "factory" || bootMode == "ffbm-00" ||
103 bootMode == "ffbm-01") {
104 if (persistVendorFunctions != "")
105 ret = persistVendorFunctions;
106 else
107 ret = "diag";
108 // vendor.usb.config will reflect the current configured functions
109 SetProperty(kVendorConfig, ret);
110 }
111
112 return ret;
113 }
114
resetGadget()115 Status resetGadget() {
116 ALOGI("setCurrentUsbFunctions None");
117
118 if (!WriteStringToFile("none", PULLUP_PATH))
119 ALOGI("Gadget cannot be pulled down");
120
121 if (!WriteStringToFile("0", DEVICE_CLASS_PATH))
122 return Status::ERROR;
123
124 if (!WriteStringToFile("0", DEVICE_SUB_CLASS_PATH))
125 return Status::ERROR;
126
127 if (!WriteStringToFile("0", DEVICE_PROTOCOL_PATH))
128 return Status::ERROR;
129
130 if (!WriteStringToFile("0", DESC_USE_PATH))
131 return Status::ERROR;
132
133 if (unlinkFunctions(CONFIG_PATH))
134 return Status::ERROR;
135
136 return Status::SUCCESS;
137 }
138
addGenericAndroidFunctions(MonitorFfs * monitorFfs,uint64_t functions,bool * ffsEnabled,int * functionCount)139 Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, bool *ffsEnabled,
140 int *functionCount) {
141 if (((functions & GadgetFunction::MTP) != 0)) {
142 *ffsEnabled = true;
143 ALOGI("setCurrentUsbFunctions mtp");
144 if (!WriteStringToFile("1", DESC_USE_PATH))
145 return Status::ERROR;
146
147 if (!monitorFfs->addInotifyFd("/dev/usb-ffs/mtp/"))
148 return Status::ERROR;
149
150 if (linkFunction("ffs.mtp", (*functionCount)++))
151 return Status::ERROR;
152
153 // Add endpoints to be monitored.
154 monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep1");
155 monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep2");
156 monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep3");
157 } else if (((functions & GadgetFunction::PTP) != 0)) {
158 *ffsEnabled = true;
159 ALOGI("setCurrentUsbFunctions ptp");
160 if (!WriteStringToFile("1", DESC_USE_PATH))
161 return Status::ERROR;
162
163 if (!monitorFfs->addInotifyFd("/dev/usb-ffs/ptp/"))
164 return Status::ERROR;
165
166 if (linkFunction("ffs.ptp", (*functionCount)++))
167 return Status::ERROR;
168
169 // Add endpoints to be monitored.
170 monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep1");
171 monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep2");
172 monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep3");
173 }
174
175 if ((functions & GadgetFunction::MIDI) != 0) {
176 ALOGI("setCurrentUsbFunctions MIDI");
177 if (linkFunction("midi.gs5", (*functionCount)++))
178 return Status::ERROR;
179 }
180
181 if ((functions & GadgetFunction::ACCESSORY) != 0) {
182 ALOGI("setCurrentUsbFunctions Accessory");
183 if (linkFunction("accessory.gs2", (*functionCount)++))
184 return Status::ERROR;
185 }
186
187 if ((functions & GadgetFunction::AUDIO_SOURCE) != 0) {
188 ALOGI("setCurrentUsbFunctions Audio Source");
189 if (linkFunction("audio_source.gs3", (*functionCount)++))
190 return Status::ERROR;
191 }
192
193 if ((functions & GadgetFunction::RNDIS) != 0) {
194 ALOGI("setCurrentUsbFunctions rndis");
195 if (linkFunction("gsi.rndis", (*functionCount)++))
196 return Status::ERROR;
197 }
198
199 return Status::SUCCESS;
200 }
201
addAdb(MonitorFfs * monitorFfs,int * functionCount)202 Status addAdb(MonitorFfs *monitorFfs, int *functionCount) {
203 ALOGI("setCurrentUsbFunctions Adb");
204 if (!WriteStringToFile("1", DESC_USE_PATH))
205 return Status::ERROR;
206
207 if (!monitorFfs->addInotifyFd("/dev/usb-ffs/adb/"))
208 return Status::ERROR;
209
210 if (linkFunction("ffs.adb", (*functionCount)++))
211 return Status::ERROR;
212 monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep1");
213 monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep2");
214 ALOGI("Service started");
215 return Status::SUCCESS;
216 }
217
218 } // namespace usb
219 } // namespace pixel
220 } // namespace google
221 } // namespace hardware
222 } // namespace android
223