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