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