• 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_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 int32_t IsDescriptorOk(struct UsbFnDeviceDesc *des)
26 {
27     int32_t 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 
ChangeDescriptorDoSwitch(struct UsbDescriptorHeader * descriptor,int8_t * intfIndex)65 static void ChangeDescriptorDoSwitch(struct UsbDescriptorHeader *descriptor, int8_t *intfIndex)
66 {
67     switch (descriptor->bDescriptorType) {
68         case USB_DDK_DT_INTERFACE_ASSOCIATION:
69             {
70                 struct UsbInterfaceAssocDescriptor *iadDescriptor = NULL;
71                 iadDescriptor = (struct UsbInterfaceAssocDescriptor *)descriptor;
72                 iadDescriptor->bFirstInterface = 0;
73             }
74             break;
75         case USB_DDK_DT_INTERFACE:
76             {
77                 struct UsbInterfaceDescriptor *intfDescriptor = NULL;
78                 intfDescriptor = (struct UsbInterfaceDescriptor *)descriptor;
79                 intfDescriptor->bInterfaceNumber = (*intfIndex)++;
80             }
81             break;
82         case USB_DDK_DT_CS_INTERFACE:
83             {
84                 struct UsbCdcUnionDesc *unionDescriptor = NULL;
85                 if (descriptor->bLength == sizeof(struct UsbCdcUnionDesc)) {
86                     unionDescriptor = (struct UsbCdcUnionDesc *)descriptor;
87                     if (unionDescriptor->bDescriptorSubType == USB_DDK_CDC_UNION_TYPE) {
88                         unionDescriptor->bMasterInterface0 = 0;
89                         unionDescriptor->bSlaveInterface0 = 1;
90                     }
91                 }
92             }
93             break;
94         default:
95             break;
96     }
97 }
98 
UsbFnChangeDescriptor(struct UsbDescriptorHeader ** descriptors)99 static void UsbFnChangeDescriptor(struct UsbDescriptorHeader **descriptors)
100 {
101     int8_t iCount;
102     int8_t intfIndex = 0;
103     if (descriptors == NULL) {
104         HDF_LOGE("%s: param is null", __func__);
105         return;
106     }
107     for (iCount = 0; (descriptors[iCount] != NULL); iCount++) {
108         ChangeDescriptorDoSwitch(descriptors[iCount], &intfIndex);
109     }
110 }
111 
UsbFnChangeDescInfo(uint8_t functionMask,struct UsbFnFunction * function)112 static void UsbFnChangeDescInfo(uint8_t functionMask, struct UsbFnFunction *function)
113 {
114     if (FUNCTION_ECM_MASK & functionMask) {
115         HDF_LOGI("%s: not need change", __func__);
116         return;
117     }
118     UsbFnChangeDescriptor(function->fsDescriptors);
119     UsbFnChangeDescriptor(function->hsDescriptors);
120     UsbFnChangeDescriptor(function->ssDescriptors);
121     UsbFnChangeDescriptor(function->sspDescriptors);
122 }
123 
DoChangeFunction(struct UsbFnFunction * function,struct UsbFnDescriptorData * descriptor)124 static void DoChangeFunction(struct UsbFnFunction *function, struct UsbFnDescriptorData *descriptor)
125 {
126     function->enable = true;
127     if (strncmp(function->funcName,
128         FUNCTION_GENERIC_ACM, strlen(FUNCTION_GENERIC_ACM)) == 0) {
129         if (descriptor->functionMask & FUNCTION_ACM_MASK) {
130             UsbFnChangeDescInfo(descriptor->functionMask, function);
131             HDF_LOGI("%s:  enable function = %s", __func__, FUNCTION_GENERIC_ACM);
132         } else {
133             function->enable = false;
134             HDF_LOGI("%s:  disable function = %s", __func__, FUNCTION_GENERIC_ACM);
135         }
136     } else if (strncmp(function->funcName,
137         FUNCTION_GENERIC_ECM, strlen(FUNCTION_GENERIC_ECM)) == 0) {
138         if (descriptor->functionMask & FUNCTION_ECM_MASK) {
139             function->enable = true;
140             HDF_LOGI("%s:  enable function = %s", __func__, FUNCTION_GENERIC_ECM);
141         } else {
142             function->enable = false;
143             HDF_LOGI("%s:  disable function = %s", __func__, FUNCTION_GENERIC_ECM);
144         }
145     } else {
146         HDF_LOGE("%s: unspport function = %s", __func__,
147             function->funcName);
148     }
149 }
150 
UsbFnChangeFunction(struct UsbFnDeviceDesc * des,struct UsbFnDescriptorData * descriptor)151 static void UsbFnChangeFunction(struct UsbFnDeviceDesc *des, struct UsbFnDescriptorData *descriptor)
152 {
153     uint32_t i;
154     uint32_t j;
155     if (des == NULL || descriptor == NULL) {
156         HDF_LOGE("%s: param is null", __func__);
157         return;
158     }
159     for (i = 0; des->configs[i] != NULL; i++) {
160         for (j = 0; des->configs[i]->functions[j] != NULL; j++) {
161             DoChangeFunction(des->configs[i]->functions[j], descriptor);
162         }
163     }
164 }
UsbFnCreateDevice(const char * udcName,struct UsbFnDescriptorData * descriptor)165 const struct UsbFnDevice *UsbFnCreateDevice(const char *udcName,
166     struct UsbFnDescriptorData *descriptor)
167 {
168     int32_t ret;
169     const struct DeviceResourceNode *property = NULL;
170     struct UsbFnDeviceDesc *des = NULL;
171 
172     if (udcName == NULL || descriptor == NULL) {
173         HDF_LOGE("%s: INVALID PARAM", __func__);
174         return NULL;
175     }
176     if (UsbFnMgrDeviceGet(udcName)) {
177         HDF_LOGE("%s:%s haved create!", __func__, udcName);
178         return NULL;
179     }
180     if (descriptor->type == USBFN_DESC_DATA_TYPE_PROP) {
181         property = descriptor->property;
182         des = UsbFnCfgMgrGetInstanceFromHCS(property);
183         if (des == NULL) {
184             HDF_LOGE("%s:get descriptors from Hcs failed!", __func__);
185             return NULL;
186         }
187         UsbFnChangeFunction(des, descriptor);
188     } else {
189         des = descriptor->descriptor;
190     }
191     ret = IsDescriptorOk(des);
192     if (ret) {
193         return NULL;
194     }
195 
196     return (struct UsbFnDevice *)UsbFnMgrDeviceCreate(udcName, des, property);
197 }
198 
UsbFnRemoveDevice(struct UsbFnDevice * fnDevice)199 int32_t UsbFnRemoveDevice(struct UsbFnDevice *fnDevice)
200 {
201     if (fnDevice == NULL) {
202         HDF_LOGE("%s: INVALID PARAM", __func__);
203         return HDF_ERR_INVALID_PARAM;
204     }
205     return UsbFnMgrDeviceRemove(fnDevice);
206 }
207 
UsbFnGetDevice(const char * udcName)208 const struct UsbFnDevice *UsbFnGetDevice(const char *udcName)
209 {
210     if (udcName == NULL) {
211         HDF_LOGE("%s: INVALID PARAM", __func__);
212         return NULL;
213     }
214     return (struct UsbFnDevice *)UsbFnMgrDeviceGet((const char *)udcName);
215 }
216 
UsbFnGetDeviceState(struct UsbFnDevice * fnDevice,UsbFnDeviceState * devState)217 int32_t UsbFnGetDeviceState(struct UsbFnDevice *fnDevice, UsbFnDeviceState *devState)
218 {
219     if (fnDevice == NULL || devState == NULL) {
220         HDF_LOGE("%s: INVALID PARAM", __func__);
221         return HDF_ERR_INVALID_PARAM;
222     }
223     return UsbFnMgrDeviceGetState(fnDevice, devState);
224 }
225 
UsbFnGetInterface(struct UsbFnDevice * fnDevice,uint8_t interfaceIndex)226 const struct UsbFnInterface *UsbFnGetInterface(struct UsbFnDevice *fnDevice,
227     uint8_t interfaceIndex)
228 {
229     if (fnDevice == NULL) {
230         HDF_LOGE("%s: INVALID PARAM", __func__);
231         return NULL;
232     }
233     return (struct UsbFnInterface *)UsbFnMgrDeviceGetInterface(fnDevice, interfaceIndex);
234 }
235 
UsbFnStartRecvInterfaceEvent(struct UsbFnInterface * interface,uint32_t eventMask,UsbFnEventCallback callback,void * context)236 int32_t UsbFnStartRecvInterfaceEvent(struct UsbFnInterface *interface,
237     uint32_t eventMask, UsbFnEventCallback callback, void *context)
238 {
239     if (interface == NULL || eventMask == 0 || callback == NULL) {
240         HDF_LOGE("%s: INVALID_PARAM", __func__);
241         return HDF_ERR_INVALID_PARAM;
242     }
243     return UsbFnMgrStartRecvEvent(interface, eventMask, callback, context);
244 }
245 
UsbFnStopRecvInterfaceEvent(struct UsbFnInterface * interface)246 int32_t UsbFnStopRecvInterfaceEvent(struct UsbFnInterface *interface)
247 {
248     if (interface == NULL) {
249         HDF_LOGE("%s: INVALID PARAM", __func__);
250         return HDF_ERR_INVALID_PARAM;
251     }
252     return UsbFnStopRecvEvent(interface);
253 }
254 
UsbFnOpenInterface(struct UsbFnInterface * interface)255 UsbFnInterfaceHandle UsbFnOpenInterface(struct UsbFnInterface *interface)
256 {
257     if (interface == NULL) {
258         HDF_LOGE("%s: INVALID PARAM", __func__);
259         return NULL;
260     }
261     return (UsbFnInterfaceHandle)UsbFnIoMgrInterfaceOpen(interface);
262 }
263 
UsbFnCloseInterface(UsbFnInterfaceHandle handle)264 int32_t UsbFnCloseInterface(UsbFnInterfaceHandle handle)
265 {
266     if (handle == NULL) {
267         HDF_LOGE("%s: INVALID PARAM", __func__);
268         return HDF_ERR_INVALID_PARAM;
269     }
270     return UsbFnIoMgrInterfaceClose((struct UsbHandleMgr *)handle);
271 }
272 
UsbFnGetInterfacePipeInfo(struct UsbFnInterface * interface,uint8_t pipeId,struct UsbFnPipeInfo * info)273 int32_t UsbFnGetInterfacePipeInfo(struct UsbFnInterface *interface,
274     uint8_t pipeId, struct UsbFnPipeInfo *info)
275 {
276     if (info == NULL || interface == NULL) {
277         HDF_LOGE("%s: INVALID PARAM", __func__);
278         return HDF_ERR_INVALID_PARAM;
279     }
280     return UsbFnIoMgrInterfaceGetPipeInfo(interface, pipeId, info);
281 }
282 
UsbFnRegistInterfaceProp(const struct UsbFnInterface * interface,const struct UsbFnRegistInfo * registInfo)283 int32_t UsbFnRegistInterfaceProp(const struct UsbFnInterface *interface,
284     const struct UsbFnRegistInfo *registInfo)
285 {
286     if (registInfo == NULL || interface == NULL) {
287         HDF_LOGE("%s: INVALID PARAM", __func__);
288         return HDF_ERR_INVALID_PARAM;
289     }
290     return UsbFnCfgMgrRegisterProp(interface, registInfo);
291 }
292 
UsbFnGetInterfaceProp(const struct UsbFnInterface * interface,const char * name,char * value)293 int32_t UsbFnGetInterfaceProp(const struct UsbFnInterface *interface,
294     const char *name, char *value)
295 {
296     if (name == NULL || interface == NULL || value == NULL) {
297         HDF_LOGE("%s: INVALID PARAM", __func__);
298         return HDF_ERR_INVALID_PARAM;
299     }
300     return UsbFnCfgMgrGetProp(interface, name, value);
301 }
302 
UsbFnSetInterfaceProp(const struct UsbFnInterface * interface,const char * name,const char * value)303 int32_t UsbFnSetInterfaceProp(const struct UsbFnInterface *interface,
304     const char *name, const char *value)
305 {
306     if (name == NULL || interface == NULL) {
307         HDF_LOGE("%s: INVALID PARAM", __func__);
308         return HDF_ERR_INVALID_PARAM;
309     }
310     return UsbFnCfgMgrSetProp(interface, name, value);
311 }
312 
UsbFnAllocRequest(UsbFnInterfaceHandle handle,uint8_t pipe,uint32_t len)313 struct UsbFnRequest *UsbFnAllocRequest(UsbFnInterfaceHandle handle, uint8_t pipe, uint32_t len)
314 {
315     struct UsbHandleMgr *handleMgr = handle;
316     if (handle == NULL || len > MAX_BUFLEN || len == 0 || pipe >= handleMgr->numFd) {
317         HDF_LOGE("%s: INVALID PARAM", __func__);
318         return NULL;
319     }
320     return UsbFnIoMgrRequestAlloc(handleMgr, pipe + 1, len);
321 }
322 
UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle,uint32_t len)323 struct UsbFnRequest *UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle, uint32_t len)
324 {
325     struct UsbHandleMgr *handleMgr = handle;
326     if (handle == NULL || len > MAX_BUFLEN || len == 0) {
327         HDF_LOGE("%s: INVALID PARAM", __func__);
328         return NULL;
329     }
330     return UsbFnIoMgrRequestAlloc(handleMgr, 0, len);
331 }
332 
UsbFnFreeRequest(struct UsbFnRequest * req)333 int32_t UsbFnFreeRequest(struct UsbFnRequest *req)
334 {
335     if (req == NULL) {
336         HDF_LOGE("%s: INVALID PARAM", __func__);
337         return HDF_ERR_INVALID_PARAM;
338     }
339     return  UsbFnIoMgrRequestFree(req);
340 }
341 
UsbFnGetRequestStatus(struct UsbFnRequest * req,UsbRequestStatus * status)342 int32_t UsbFnGetRequestStatus(struct UsbFnRequest *req, UsbRequestStatus *status)
343 {
344     if (req == NULL || status == NULL) {
345         HDF_LOGE("%s: INVALID PARAM", __func__);
346         return HDF_ERR_INVALID_PARAM;
347     }
348     return UsbFnIoMgrRequestGetStatus(req, status);
349 }
350 
UsbFnSubmitRequestAsync(struct UsbFnRequest * req)351 int32_t UsbFnSubmitRequestAsync(struct UsbFnRequest *req)
352 {
353     if (req == NULL) {
354         HDF_LOGE("%s: INVALID PARAM", __func__);
355         return HDF_ERR_INVALID_PARAM;
356     }
357     return UsbFnIoMgrRequestSubmitAsync(req);
358 }
359 
UsbFnCancelRequest(struct UsbFnRequest * req)360 int32_t UsbFnCancelRequest(struct UsbFnRequest *req)
361 {
362     if (req == NULL) {
363         HDF_LOGE("%s: INVALID PARAM", __func__);
364         return HDF_ERR_INVALID_PARAM;
365     }
366     return UsbFnIoMgrRequestCancel(req);
367 }
368 
UsbFnSubmitRequestSync(struct UsbFnRequest * req,uint32_t timeout)369 int32_t UsbFnSubmitRequestSync(struct UsbFnRequest *req, uint32_t timeout)
370 {
371     if (req == NULL) {
372         HDF_LOGE("%s: INVALID PARAM", __func__);
373         return HDF_ERR_INVALID_PARAM;
374     }
375     return UsbFnIoMgrRequestSubmitSync(req, timeout);
376 }
377 
UsbFnMemTestTrigger(bool enable)378 int32_t UsbFnMemTestTrigger(bool enable)
379 {
380     return UsbFnAdpMemTestTrigger(enable);
381 }
382 
383