• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #define HDF_LOG_TAG usbfn_sdk_if
24 
25 static struct UsbDeviceFunctionsInfo g_functionsInfo[] = {
26     {"f_generic.a", 1},
27     {"f_generic.e", 1 << 1},
28     {NULL, 0},
29 };
30 
IsDescriptorOk(struct UsbFnDeviceDesc * des)31 static int32_t IsDescriptorOk(struct UsbFnDeviceDesc *des)
32 {
33     int32_t i, j;
34     struct UsbFnStrings **strings = NULL;
35     struct UsbFnFunction *functions = NULL;
36     if (des == NULL) {
37         HDF_LOGE("%{public}s: des null", __func__);
38         goto ERR_DES;
39     }
40     if (des->deviceDesc == NULL || des->deviceStrings == NULL || des->configs == NULL) {
41         HDF_LOGE("%{public}s: deviceDesc  deviceStrings configs null", __func__);
42         goto ERR_DES;
43     }
44 
45     strings = des->deviceStrings;
46     if (strings[0] == NULL) {
47         HDF_LOGE("%{public}s: strings null", __func__);
48         goto ERR_DES;
49     }
50 
51     for (i = 0; des->configs[i] != NULL; i++) {
52         for (j = 0; des->configs[i]->functions[j] != NULL; j++) {
53             functions = des->configs[i]->functions[j];
54             if (strncmp(functions->funcName, FUNCTION_GENERIC, strlen(FUNCTION_GENERIC)) != 0) {
55                 continue;
56             }
57             if (functions->fsDescriptors == NULL) {
58                 HDF_LOGE("%{public}s: fsDescriptors null", __func__);
59                 goto ERR_DES;
60             }
61         }
62     }
63     if (i == 0 || j == 0) {
64         HDF_LOGE("%{public}s: configs functions null", __func__);
65         goto ERR_DES;
66     }
67 
68     return 0;
69 ERR_DES:
70     HDF_LOGE("%{public}s: DeviceDesc bad", __func__);
71     return HDF_ERR_INVALID_PARAM;
72 }
73 
DoChangeFunction(struct UsbFnFunction * const function,struct UsbFnDescriptorData * const descriptor)74 static void DoChangeFunction(struct UsbFnFunction * const function, struct UsbFnDescriptorData * const descriptor)
75 {
76     uint32_t i;
77     struct UsbDeviceFunctionsInfo *funcInfo = g_functionsInfo;
78     for (i = 0; funcInfo[i].functionName != NULL; i++) {
79         if (strncmp(function->funcName, funcInfo[i].functionName, strlen(funcInfo[i].functionName)) == 0) {
80             if ((descriptor->functionMask & funcInfo[i].numberMask) != 0) {
81                 function->enable = true;
82                 HDF_LOGI("%{public}s: enable function = %{public}s", __func__, funcInfo[i].functionName);
83             } else {
84                 function->enable = false;
85                 HDF_LOGI("%{public}s: disable function = %{public}s", __func__, funcInfo[i].functionName);
86             }
87         }
88     }
89 }
90 
UsbFnChangeFunction(struct UsbFnDeviceDesc * const des,struct UsbFnDescriptorData * const descriptor)91 static void UsbFnChangeFunction(struct UsbFnDeviceDesc * const des, struct UsbFnDescriptorData * const descriptor)
92 {
93     uint32_t i;
94     uint32_t j;
95     if (des == NULL || descriptor == NULL) {
96         HDF_LOGE("%{public}s: param is null", __func__);
97         return;
98     }
99     for (i = 0; des->configs[i] != NULL; i++) {
100         for (j = 0; des->configs[i]->functions[j] != NULL; j++) {
101             DoChangeFunction(des->configs[i]->functions[j], descriptor);
102         }
103     }
104 }
105 
UsbFnCreateDevice(const char * udcName,struct UsbFnDescriptorData * descriptor)106 const struct UsbFnDevice *UsbFnCreateDevice(const char *udcName, struct UsbFnDescriptorData *descriptor)
107 {
108     int32_t ret;
109     const struct DeviceResourceNode *property = NULL;
110     struct UsbFnDeviceDesc *des = NULL;
111 
112     if (udcName == NULL || descriptor == NULL) {
113         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
114         return NULL;
115     }
116     if (UsbFnMgrDeviceGet(udcName) != NULL) {
117         HDF_LOGE("%{public}s:%{public}s haved create", __func__, udcName);
118         return NULL;
119     }
120     if (descriptor->type == USBFN_DESC_DATA_TYPE_PROP) {
121         property = descriptor->property;
122         des = UsbFnCfgMgrGetInstanceFromHCS(property);
123         if (des == NULL) {
124             HDF_LOGE("%{public}s:get descriptors from Hcs failed", __func__);
125             return NULL;
126         }
127         UsbFnChangeFunction(des, descriptor);
128     } else {
129         des = descriptor->descriptor;
130     }
131     ret = IsDescriptorOk(des);
132     if (ret) {
133         return NULL;
134     }
135 
136     return (struct UsbFnDevice *)UsbFnMgrDeviceCreate(udcName, des, property);
137 }
138 
UsbFnRemoveDevice(struct UsbFnDevice * fnDevice)139 int32_t UsbFnRemoveDevice(struct UsbFnDevice *fnDevice)
140 {
141     if (fnDevice == NULL) {
142         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
143         return HDF_ERR_INVALID_PARAM;
144     }
145     return UsbFnMgrDeviceRemove(fnDevice);
146 }
147 
UsbFnGetDevice(const char * udcName)148 const struct UsbFnDevice *UsbFnGetDevice(const char *udcName)
149 {
150     if (udcName == NULL) {
151         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
152         return NULL;
153     }
154     return (struct UsbFnDevice *)UsbFnMgrDeviceGet((const char *)udcName);
155 }
156 
UsbFnGetDeviceState(struct UsbFnDevice * fnDevice,UsbFnDeviceState * devState)157 int32_t UsbFnGetDeviceState(struct UsbFnDevice *fnDevice, UsbFnDeviceState *devState)
158 {
159     if (fnDevice == NULL || devState == NULL) {
160         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
161         return HDF_ERR_INVALID_PARAM;
162     }
163     return UsbFnMgrDeviceGetState(fnDevice, devState);
164 }
165 
UsbFnGetInterface(struct UsbFnDevice * fnDevice,uint8_t interfaceIndex)166 const struct UsbFnInterface *UsbFnGetInterface(struct UsbFnDevice *fnDevice, uint8_t interfaceIndex)
167 {
168     if (fnDevice == NULL) {
169         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
170         return NULL;
171     }
172     return (struct UsbFnInterface *)UsbFnMgrDeviceGetInterface(fnDevice, interfaceIndex);
173 }
174 
UsbFnStartRecvInterfaceEvent(struct UsbFnInterface * interface,uint32_t eventMask,UsbFnEventCallback callback,void * context)175 int32_t UsbFnStartRecvInterfaceEvent(
176     struct UsbFnInterface *interface, uint32_t eventMask, UsbFnEventCallback callback, void *context)
177 {
178     if (interface == NULL || eventMask == 0 || callback == NULL) {
179         HDF_LOGE("%{public}s: INVALID_PARAM", __func__);
180         return HDF_ERR_INVALID_PARAM;
181     }
182     return UsbFnMgrStartRecvEvent(interface, eventMask, callback, context);
183 }
184 
UsbFnStopRecvInterfaceEvent(struct UsbFnInterface * interface)185 int32_t UsbFnStopRecvInterfaceEvent(struct UsbFnInterface *interface)
186 {
187     if (interface == NULL) {
188         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
189         return HDF_ERR_INVALID_PARAM;
190     }
191     return UsbFnStopRecvEvent(interface);
192 }
193 
UsbFnOpenInterface(struct UsbFnInterface * interface)194 UsbFnInterfaceHandle UsbFnOpenInterface(struct UsbFnInterface *interface)
195 {
196     if (interface == NULL) {
197         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
198         return NULL;
199     }
200     return (UsbFnInterfaceHandle)UsbFnIoMgrInterfaceOpen(interface);
201 }
202 
UsbFnCloseInterface(UsbFnInterfaceHandle handle)203 int32_t UsbFnCloseInterface(UsbFnInterfaceHandle handle)
204 {
205     if (handle == NULL) {
206         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
207         return HDF_ERR_INVALID_PARAM;
208     }
209     return UsbFnIoMgrInterfaceClose((struct UsbHandleMgr *)handle);
210 }
211 
UsbFnGetInterfacePipeInfo(struct UsbFnInterface * interface,uint8_t pipeId,struct UsbFnPipeInfo * info)212 int32_t UsbFnGetInterfacePipeInfo(struct UsbFnInterface *interface, uint8_t pipeId, struct UsbFnPipeInfo *info)
213 {
214     if (info == NULL || interface == NULL) {
215         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
216         return HDF_ERR_INVALID_PARAM;
217     }
218     return UsbFnIoMgrInterfaceGetPipeInfo(interface, pipeId, info);
219 }
220 
UsbFnRegistInterfaceProp(const struct UsbFnInterface * interface,const struct UsbFnRegistInfo * registInfo)221 int32_t UsbFnRegistInterfaceProp(const struct UsbFnInterface *interface, const struct UsbFnRegistInfo *registInfo)
222 {
223     if (registInfo == NULL || interface == NULL) {
224         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
225         return HDF_ERR_INVALID_PARAM;
226     }
227     return UsbFnCfgMgrRegisterProp(interface, registInfo);
228 }
229 
UsbFnGetInterfaceProp(const struct UsbFnInterface * interface,const char * name,char * value)230 int32_t UsbFnGetInterfaceProp(const struct UsbFnInterface *interface, const char *name, char *value)
231 {
232     if (name == NULL || interface == NULL || value == NULL) {
233         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
234         return HDF_ERR_INVALID_PARAM;
235     }
236     return UsbFnCfgMgrGetProp(interface, name, value);
237 }
238 
UsbFnSetInterfaceProp(const struct UsbFnInterface * interface,const char * name,const char * value)239 int32_t UsbFnSetInterfaceProp(const struct UsbFnInterface *interface, const char *name, const char *value)
240 {
241     if (name == NULL || interface == NULL) {
242         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
243         return HDF_ERR_INVALID_PARAM;
244     }
245     return UsbFnCfgMgrSetProp(interface, name, value);
246 }
247 
UsbFnAllocRequest(UsbFnInterfaceHandle handle,uint8_t pipe,uint32_t len)248 struct UsbFnRequest *UsbFnAllocRequest(UsbFnInterfaceHandle handle, uint8_t pipe, uint32_t len)
249 {
250     struct UsbHandleMgr *handleMgr = handle;
251     if (handle == NULL || len > MAX_BUFLEN || len == 0 || pipe >= handleMgr->numFd) {
252         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
253         return NULL;
254     }
255     return UsbFnIoMgrRequestAlloc(handleMgr, pipe + 1, len);
256 }
257 
UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle,uint32_t len)258 struct UsbFnRequest *UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle, uint32_t len)
259 {
260     struct UsbHandleMgr *handleMgr = handle;
261     if (handle == NULL || len > MAX_BUFLEN || len == 0) {
262         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
263         return NULL;
264     }
265     return UsbFnIoMgrRequestAlloc(handleMgr, 0, len);
266 }
267 
UsbFnFreeRequest(struct UsbFnRequest * req)268 int32_t UsbFnFreeRequest(struct UsbFnRequest *req)
269 {
270     if (req == NULL) {
271         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
272         return HDF_ERR_INVALID_PARAM;
273     }
274     return UsbFnIoMgrRequestFree(req);
275 }
276 
UsbFnGetRequestStatus(struct UsbFnRequest * req,UsbRequestStatus * status)277 int32_t UsbFnGetRequestStatus(struct UsbFnRequest *req, UsbRequestStatus *status)
278 {
279     if (req == NULL || status == NULL) {
280         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
281         return HDF_ERR_INVALID_PARAM;
282     }
283     return UsbFnIoMgrRequestGetStatus(req, status);
284 }
285 
UsbFnSubmitRequestAsync(struct UsbFnRequest * req)286 int32_t UsbFnSubmitRequestAsync(struct UsbFnRequest *req)
287 {
288     if (req == NULL) {
289         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
290         return HDF_ERR_INVALID_PARAM;
291     }
292     return UsbFnIoMgrRequestSubmitAsync(req);
293 }
294 
UsbFnCancelRequest(struct UsbFnRequest * req)295 int32_t UsbFnCancelRequest(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 UsbFnIoMgrRequestCancel(req);
302 }
303 
UsbFnSubmitRequestSync(struct UsbFnRequest * req,uint32_t timeout)304 int32_t UsbFnSubmitRequestSync(struct UsbFnRequest *req, uint32_t timeout)
305 {
306     if (req == NULL) {
307         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
308         return HDF_ERR_INVALID_PARAM;
309     }
310     return UsbFnIoMgrRequestSubmitSync(req, timeout);
311 }
312 
UsbFnMemTestTrigger(bool enable)313 int32_t UsbFnMemTestTrigger(bool enable)
314 {
315     return UsbFnAdpMemTestTrigger(enable);
316 }
317