1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "usbfn_cfg_mgr.h"
17 #include "usbfn_dev_mgr.h"
18 #include "usbfn_device.h"
19 #include "usbfn_interface.h"
20 #include "usbfn_io_mgr.h"
21 #include "usbfn_request.h"
22 #include "usbd_wrapper.h"
23
24 #define HDF_LOG_TAG usbfn_sdk_if
25
26 static struct UsbDeviceFunctionsInfo g_functionsInfo[] = {
27 {"f_generic.a", 1 },
28 {"f_generic.e", 1 << 1},
29 {"f_generic.mtp", 1 << 3},
30 {"f_generic.ptp", 1 << 4},
31 {NULL, 0 },
32 };
33
IsDescriptorOk(struct UsbFnDeviceDesc * des)34 static int32_t IsDescriptorOk(struct UsbFnDeviceDesc *des)
35 {
36 int32_t i, j;
37 struct UsbFnStrings **strings = NULL;
38 struct UsbFnFunction *functions = NULL;
39 if (des == NULL) {
40 HDF_LOGE("%{public}s: des null", __func__);
41 goto ERR_DES;
42 }
43 if (des->deviceDesc == NULL || des->deviceStrings == NULL || des->configs == NULL) {
44 HDF_LOGE("%{public}s: deviceDesc deviceStrings configs null", __func__);
45 goto ERR_DES;
46 }
47
48 strings = des->deviceStrings;
49 if (strings[0] == NULL) {
50 HDF_LOGE("%{public}s: strings null", __func__);
51 goto ERR_DES;
52 }
53
54 for (i = 0; des->configs[i] != NULL; i++) {
55 for (j = 0; des->configs[i]->functions[j] != NULL; j++) {
56 functions = des->configs[i]->functions[j];
57 if (strncmp(functions->funcName, FUNCTION_GENERIC, strlen(FUNCTION_GENERIC)) != 0) {
58 continue;
59 }
60 if (functions->fsDescriptors == NULL) {
61 HDF_LOGE("%{public}s: fsDescriptors null", __func__);
62 goto ERR_DES;
63 }
64 }
65 }
66 if (i == 0 || j == 0) {
67 HDF_LOGE("%{public}s: configs functions null", __func__);
68 goto ERR_DES;
69 }
70
71 return 0;
72 ERR_DES:
73 HDF_LOGE("%{public}s: DeviceDesc bad", __func__);
74 return HDF_ERR_INVALID_PARAM;
75 }
76
DoChangeFunction(struct UsbFnFunction * const function,struct UsbFnDescriptorData * const descriptor)77 static void DoChangeFunction(struct UsbFnFunction * const function, struct UsbFnDescriptorData * const descriptor)
78 {
79 uint32_t i;
80 struct UsbDeviceFunctionsInfo *funcInfo = g_functionsInfo;
81 for (i = 0; funcInfo[i].functionName != NULL; i++) {
82 if (strncmp(function->funcName, funcInfo[i].functionName, strlen(funcInfo[i].functionName)) == 0) {
83 if ((descriptor->functionMask & funcInfo[i].numberMask) != 0) {
84 function->enable = true;
85 HDF_LOGI("%{public}s: enable function = %{public}s", __func__, funcInfo[i].functionName);
86 } else {
87 function->enable = false;
88 HDF_LOGI("%{public}s: disable function = %{public}s", __func__, funcInfo[i].functionName);
89 }
90 }
91 }
92 }
93
UsbFnChangeFunction(struct UsbFnDeviceDesc * const des,struct UsbFnDescriptorData * const descriptor)94 static void UsbFnChangeFunction(struct UsbFnDeviceDesc * const des, struct UsbFnDescriptorData * const descriptor)
95 {
96 uint32_t i;
97 uint32_t j;
98 if (des == NULL || descriptor == NULL) {
99 HDF_LOGE("%{public}s: param is null", __func__);
100 return;
101 }
102 for (i = 0; des->configs[i] != NULL; i++) {
103 for (j = 0; des->configs[i]->functions[j] != NULL; j++) {
104 DoChangeFunction(des->configs[i]->functions[j], descriptor);
105 }
106 }
107 }
108
UsbFnCreateDevice(const char * udcName,struct UsbFnDescriptorData * descriptor)109 const struct UsbFnDevice *UsbFnCreateDevice(const char *udcName, struct UsbFnDescriptorData *descriptor)
110 {
111 int32_t ret;
112 const struct DeviceResourceNode *property = NULL;
113 struct UsbFnDeviceDesc *des = NULL;
114
115 if (udcName == NULL || descriptor == NULL) {
116 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
117 return NULL;
118 }
119 if (UsbFnMgrDeviceGet(udcName) != NULL) {
120 HDF_LOGE("%{public}s: udc %{public}s haved create", __func__, udcName);
121 return NULL;
122 }
123 HDF_LOGD("%{public}s: type=%{public}d, fMask=%{public}d", __func__, descriptor->type, descriptor->functionMask);
124 if (descriptor->type == USBFN_DESC_DATA_TYPE_PROP) {
125 property = descriptor->property;
126 HDF_LOGD("%{public}s: use descriptor in HCS", __func__);
127 des = UsbFnCfgMgrGetInstanceFromHCS(property);
128 if (des == NULL) {
129 HDF_LOGE("%{public}s: get descriptors from HCS failed", __func__);
130 return NULL;
131 }
132 } else {
133 des = descriptor->descriptor;
134 }
135 UsbFnChangeFunction(des, descriptor);
136 ret = IsDescriptorOk(des);
137 if (ret) {
138 if (descriptor->type == USBFN_DESC_DATA_TYPE_PROP && des != NULL) {
139 UsbFnCfgMgrFreeUsbFnDeviceDesc(des);
140 des = NULL;
141 }
142 return NULL;
143 }
144
145 return (struct UsbFnDevice *)UsbFnMgrDeviceCreate(udcName, des, property);
146 }
147
UsbFnRemoveDevice(struct UsbFnDevice * fnDevice)148 int32_t UsbFnRemoveDevice(struct UsbFnDevice *fnDevice)
149 {
150 if (fnDevice == NULL) {
151 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
152 return HDF_ERR_INVALID_PARAM;
153 }
154 return UsbFnMgrDeviceRemove(fnDevice);
155 }
156
UsbFnGetDevice(const char * udcName)157 const struct UsbFnDevice *UsbFnGetDevice(const char *udcName)
158 {
159 if (udcName == NULL) {
160 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
161 return NULL;
162 }
163 return (struct UsbFnDevice *)UsbFnMgrDeviceGet((const char *)udcName);
164 }
165
UsbFnGetDeviceState(struct UsbFnDevice * fnDevice,UsbFnDeviceState * devState)166 int32_t UsbFnGetDeviceState(struct UsbFnDevice *fnDevice, UsbFnDeviceState *devState)
167 {
168 if (fnDevice == NULL || devState == NULL) {
169 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
170 return HDF_ERR_INVALID_PARAM;
171 }
172 return UsbFnMgrDeviceGetState(fnDevice, devState);
173 }
174
UsbFnGetInterface(struct UsbFnDevice * fnDevice,uint8_t interfaceIndex)175 const struct UsbFnInterface *UsbFnGetInterface(struct UsbFnDevice *fnDevice, uint8_t interfaceIndex)
176 {
177 if (fnDevice == NULL) {
178 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
179 return NULL;
180 }
181 return (struct UsbFnInterface *)UsbFnMgrDeviceGetInterface(fnDevice, interfaceIndex);
182 }
183
UsbFnStartRecvInterfaceEvent(struct UsbFnInterface * interface,uint32_t eventMask,UsbFnEventCallback callback,void * context)184 int32_t UsbFnStartRecvInterfaceEvent(
185 struct UsbFnInterface *interface, uint32_t eventMask, UsbFnEventCallback callback, void *context)
186 {
187 if (interface == NULL || eventMask == 0 || callback == NULL) {
188 HDF_LOGE("%{public}s: INVALID_PARAM", __func__);
189 return HDF_ERR_INVALID_PARAM;
190 }
191 return UsbFnMgrStartRecvEvent(interface, eventMask, callback, context);
192 }
193
UsbFnStopRecvInterfaceEvent(struct UsbFnInterface * interface)194 int32_t UsbFnStopRecvInterfaceEvent(struct UsbFnInterface *interface)
195 {
196 if (interface == NULL) {
197 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
198 return HDF_ERR_INVALID_PARAM;
199 }
200 return UsbFnStopRecvEvent(interface);
201 }
202
UsbFnOpenInterface(struct UsbFnInterface * interface)203 UsbFnInterfaceHandle UsbFnOpenInterface(struct UsbFnInterface *interface)
204 {
205 if (interface == NULL) {
206 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
207 return NULL;
208 }
209 return (UsbFnInterfaceHandle)UsbFnIoMgrInterfaceOpen(interface);
210 }
211
UsbFnCloseInterface(UsbFnInterfaceHandle handle)212 int32_t UsbFnCloseInterface(UsbFnInterfaceHandle handle)
213 {
214 if (handle == NULL) {
215 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
216 return HDF_ERR_INVALID_PARAM;
217 }
218 return UsbFnIoMgrInterfaceClose((struct UsbHandleMgr *)handle);
219 }
220
UsbFnGetInterfacePipeInfo(struct UsbFnInterface * interface,uint8_t pipeId,struct UsbFnPipeInfo * info)221 int32_t UsbFnGetInterfacePipeInfo(struct UsbFnInterface *interface, uint8_t pipeId, struct UsbFnPipeInfo *info)
222 {
223 if (info == NULL || interface == NULL) {
224 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
225 return HDF_ERR_INVALID_PARAM;
226 }
227 return UsbFnIoMgrInterfaceGetPipeInfo(interface, pipeId, info);
228 }
229
UsbFnRegistInterfaceProp(const struct UsbFnInterface * interface,const struct UsbFnRegistInfo * registInfo)230 int32_t UsbFnRegistInterfaceProp(const struct UsbFnInterface *interface, const struct UsbFnRegistInfo *registInfo)
231 {
232 if (registInfo == NULL || interface == NULL) {
233 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
234 return HDF_ERR_INVALID_PARAM;
235 }
236 return UsbFnCfgMgrRegisterProp(interface, registInfo);
237 }
238
UsbFnGetInterfaceProp(const struct UsbFnInterface * interface,const char * name,char * value)239 int32_t UsbFnGetInterfaceProp(const struct UsbFnInterface *interface, const char *name, char *value)
240 {
241 if (name == NULL || interface == NULL || value == NULL) {
242 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
243 return HDF_ERR_INVALID_PARAM;
244 }
245 return UsbFnCfgMgrGetProp(interface, name, value);
246 }
247
UsbFnSetInterfaceProp(const struct UsbFnInterface * interface,const char * name,const char * value)248 int32_t UsbFnSetInterfaceProp(const struct UsbFnInterface *interface, const char *name, const char *value)
249 {
250 if (name == NULL || interface == NULL) {
251 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
252 return HDF_ERR_INVALID_PARAM;
253 }
254 return UsbFnCfgMgrSetProp(interface, name, value);
255 }
256
UsbFnAllocRequest(UsbFnInterfaceHandle handle,uint8_t pipe,uint32_t len)257 struct UsbFnRequest *UsbFnAllocRequest(UsbFnInterfaceHandle handle, uint8_t pipe, uint32_t len)
258 {
259 struct UsbHandleMgr *handleMgr = handle;
260 if (handle == NULL || len > MAX_BUFLEN || len == 0 || pipe >= handleMgr->numFd) {
261 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
262 return NULL;
263 }
264 return UsbFnIoMgrRequestAlloc(handleMgr, pipe + 1, len);
265 }
266
UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle,uint32_t len)267 struct UsbFnRequest *UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle, uint32_t len)
268 {
269 struct UsbHandleMgr *handleMgr = handle;
270 if (handle == NULL || len > MAX_BUFLEN || len == 0) {
271 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
272 return NULL;
273 }
274 return UsbFnIoMgrRequestAlloc(handleMgr, 0, len);
275 }
276
UsbFnFreeRequest(struct UsbFnRequest * req)277 int32_t UsbFnFreeRequest(struct UsbFnRequest *req)
278 {
279 if (req == NULL) {
280 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
281 return HDF_ERR_INVALID_PARAM;
282 }
283 return UsbFnIoMgrRequestFree(req);
284 }
285
UsbFnGetRequestStatus(struct UsbFnRequest * req,UsbRequestStatus * status)286 int32_t UsbFnGetRequestStatus(struct UsbFnRequest *req, UsbRequestStatus *status)
287 {
288 if (req == NULL || status == NULL) {
289 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
290 return HDF_ERR_INVALID_PARAM;
291 }
292 return UsbFnIoMgrRequestGetStatus(req, status);
293 }
294
UsbFnSubmitRequestAsync(struct UsbFnRequest * req)295 int32_t UsbFnSubmitRequestAsync(struct UsbFnRequest *req)
296 {
297 if (req == NULL) {
298 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
299 return HDF_ERR_INVALID_PARAM;
300 }
301 return UsbFnIoMgrRequestSubmitAsync(req);
302 }
303
UsbFnCancelRequest(struct UsbFnRequest * req)304 int32_t UsbFnCancelRequest(struct UsbFnRequest *req)
305 {
306 if (req == NULL) {
307 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
308 return HDF_ERR_INVALID_PARAM;
309 }
310 return UsbFnIoMgrRequestCancel(req);
311 }
312
UsbFnSubmitRequestSync(struct UsbFnRequest * req,uint32_t timeout)313 int32_t UsbFnSubmitRequestSync(struct UsbFnRequest *req, uint32_t timeout)
314 {
315 if (req == NULL) {
316 HDF_LOGE("%{public}s: INVALID PARAM", __func__);
317 return HDF_ERR_INVALID_PARAM;
318 }
319 return UsbFnIoMgrRequestSubmitSync(req, timeout);
320 }
321