• 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 "adapter_if.h"
17 #include <dirent.h>
18 #include <endian.h>
19 #include <fcntl.h>
20 #include <poll.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 
30 #include "usb_handle.h"
31 
32 #define HDF_LOG_TAG adapter_if
33 #define SLEEP_TIME  100000
34 #define OPEN_CNT    30
35 #define MAX_SIZE    8
36 
37 static struct RawUsbRamTestList *g_usbRamTestHead = NULL;
38 static bool g_usbRamTestFlag = false;
39 
UsbFnAdapterOpenFn(void)40 static int32_t UsbFnAdapterOpenFn(void)
41 {
42     int32_t i;
43     int32_t ep;
44     for (i = 0; i < OPEN_CNT; i++) {
45         ep = handle_open("/dev/fconfig");
46         if (ep > 0) {
47             break;
48         }
49         usleep(SLEEP_TIME);
50     }
51     if (ep < 0) {
52         HDF_LOGE("func not alloc!");
53     }
54     return ep;
55 }
56 
UsbFnAdapterClosefn(int32_t fd)57 static int32_t UsbFnAdapterClosefn(int32_t fd)
58 {
59     if (fd < 0) {
60         return HDF_ERR_INVALID_PARAM;
61     }
62     return handle_close(fd);
63 }
64 
UsbFnAdapterCreateFconfigString(struct FconfigString * const configString,const char * name)65 static int32_t UsbFnAdapterCreateFconfigString(struct FconfigString * const configString, const char *name)
66 {
67     if (configString == NULL || name == NULL) {
68         HDF_LOGE("%{public}s: configName is NULL", __func__);
69         return HDF_ERR_IO;
70     }
71 
72     size_t strLen = strlen(name);
73     configString->len = (uint32_t)strLen;
74     configString->s = UsbFnMemCalloc(strLen + 1);
75     if (configString->s == NULL) {
76         HDF_LOGE("%{public}s: UsbFnMemCalloc failed!", __func__);
77         return HDF_ERR_MALLOC_FAIL;
78     }
79 
80     int32_t ret = memcpy_s(configString->s, (strLen + 1), name, strLen);
81     if (ret != EOK) {
82         HDF_LOGE("%{public}s: memcpy_s failed!", __func__);
83         UsbFnMemFree(configString->s);
84         return HDF_ERR_MALLOC_FAIL;
85     }
86 
87     *(configString->s + configString->len) = '\0';
88     return 0;
89 }
90 
UsbFnAdapterWriteGadget(int32_t fd,int32_t cmd,struct FconfigString * gadgetName)91 static int32_t UsbFnAdapterWriteGadget(int32_t fd, int32_t cmd, struct FconfigString *gadgetName)
92 {
93     int32_t ret;
94 
95     if (gadgetName == NULL) {
96         HDF_LOGE("%{public}s: udcName is NULL", __func__);
97         return HDF_ERR_IO;
98     }
99     ret = handle_ioctl(fd, cmd, gadgetName);
100     if (ret != 0) {
101         HDF_LOGE("%{public}s: ioctl failed!", __func__);
102         return HDF_ERR_IO;
103     }
104     return 0;
105 }
106 
UsbFnAdapterWriteDevDesc(int32_t fd,struct FconfigString * const gadgetName,const struct UsbFnDeviceDesc * const descriptor)107 static int32_t UsbFnAdapterWriteDevDesc(
108     int32_t fd, struct FconfigString * const gadgetName, const struct UsbFnDeviceDesc * const descriptor)
109 {
110     struct FconfigDevDesc devDesc;
111 
112     if (gadgetName == NULL || descriptor == NULL) {
113         HDF_LOGE("%{public}s: udcName is NULL", __func__);
114         return HDF_ERR_IO;
115     }
116 
117     devDesc.gadgetName.len = gadgetName->len;
118     devDesc.gadgetName.s = gadgetName->s;
119     int32_t ret = memcpy_s(&devDesc.devDesc, sizeof(devDesc.devDesc), descriptor->deviceDesc, sizeof(devDesc.devDesc));
120     if (ret != EOK) {
121         HDF_LOGE("%{public}s: memcpy_s failed!", __func__);
122         return HDF_ERR_MALLOC_FAIL;
123     }
124 
125     ret = handle_ioctl(fd, FCONFIG_CMD_WRITE_DEV_DESC, &devDesc);
126     if (ret != 0) {
127         HDF_LOGE("%{public}s: ioctl failed!", __func__);
128         return HDF_ERR_MALLOC_FAIL;
129     }
130 
131     return 0;
132 }
133 
UsbFnAdapterWriteDevString(int32_t fd,struct FconfigDevStrings * const devStrings,const struct UsbFnStrings * const usbFnString)134 static int32_t UsbFnAdapterWriteDevString(
135     int32_t fd, struct FconfigDevStrings * const devStrings, const struct UsbFnStrings * const usbFnString)
136 {
137     struct UsbString *usbString = NULL;
138     int32_t jCount;
139     int32_t ret;
140 
141     devStrings->language = usbFnString->language;
142     devStrings->strCount = 0;
143     usbString = usbFnString->strings;
144     while (usbString->s) {
145         devStrings->strCount++;
146         usbString++;
147     }
148     devStrings->strings = UsbFnMemCalloc((devStrings->strCount + 1) * sizeof(struct FconfigUsbString));
149     if (devStrings->strings == NULL) {
150         HDF_LOGE("%{public}s: UsbFnMemCalloc failed!", __func__);
151         return HDF_ERR_MALLOC_FAIL;
152     }
153     devStrings->strings[devStrings->strCount].str.len = 0;
154     devStrings->strings[devStrings->strCount].str.s = NULL;
155     usbString = usbFnString->strings;
156     for (jCount = 0; jCount < (int)devStrings->strCount; jCount++) {
157         devStrings->strings[jCount].id = usbString[jCount].id;
158         ret = UsbFnAdapterCreateFconfigString(&devStrings->strings[jCount].str, usbString[jCount].s);
159         if (ret != HDF_SUCCESS) {
160             HDF_LOGE("%{public}s: create string failed!", __func__);
161             UsbFnMemFree(devStrings->strings[jCount].str.s);
162             goto FAIL;
163         }
164     }
165     ret = handle_ioctl(fd, FCONFIG_CMD_WRITE_STRINGS, devStrings);
166     if (ret != 0) {
167         HDF_LOGE("%{public}s: ioctl failed!", __func__);
168         goto FAIL;
169     }
170     for (jCount = 0; jCount < (int)devStrings->strCount; jCount++) {
171         UsbFnMemFree(devStrings->strings[jCount].str.s);
172     }
173     UsbFnMemFree(devStrings->strings);
174     return 0;
175 FAIL:
176     while ((--jCount) >= 0) {
177         UsbFnMemFree(devStrings->strings[jCount].str.s);
178     }
179     UsbFnMemFree(devStrings->strings);
180     return -1;
181 }
182 
UsbFnAdapterWriteDevStrings(int32_t fd,struct FconfigString * const gadgetName,const struct UsbFnDeviceDesc * descriptor)183 static int32_t UsbFnAdapterWriteDevStrings(
184     int32_t fd, struct FconfigString * const gadgetName, const struct UsbFnDeviceDesc *descriptor)
185 {
186     if (gadgetName == NULL || descriptor == NULL) {
187         HDF_LOGE("%{public}s: udcName is NULL", __func__);
188         return HDF_ERR_IO;
189     }
190 
191     struct FconfigDevStrings devStrings;
192     devStrings.gadgetName.len = gadgetName->len;
193     devStrings.gadgetName.s = gadgetName->s;
194     for (uint32_t iCount = 0; descriptor->deviceStrings[iCount]; iCount++) {
195         int32_t ret = UsbFnAdapterWriteDevString(fd, &devStrings, descriptor->deviceStrings[iCount]);
196         if (ret != 0) {
197             HDF_LOGE("%{public}s: UsbFnAdapterWriteDevString failed", __func__);
198             return HDF_ERR_IO;
199         }
200     }
201     return 0;
202 }
203 
UsbFnAdapterFillConfigDesc(struct UsbConfigDescriptor * const cfgDesc,struct UsbFnConfiguration * const usbFnConfig)204 static int32_t UsbFnAdapterFillConfigDesc(
205     struct UsbConfigDescriptor * const cfgDesc, struct UsbFnConfiguration * const usbFnConfig)
206 {
207     if (cfgDesc == NULL || usbFnConfig == NULL) {
208         HDF_LOGE("%{public}s: name is NULL", __func__);
209         return HDF_ERR_IO;
210     }
211     cfgDesc->bConfigurationValue = usbFnConfig->configurationValue;
212     cfgDesc->bmAttributes = usbFnConfig->attributes;
213     cfgDesc->bMaxPower = usbFnConfig->maxPower;
214     cfgDesc->iConfiguration = usbFnConfig->iConfiguration;
215     return 0;
216 }
217 
UsbFnAdapterOpenPipe(const char * funcName,int32_t epIndex)218 static int32_t UsbFnAdapterOpenPipe(const char *funcName, int32_t epIndex)
219 {
220     int32_t ret;
221     char epName[MAX_NAMELEN];
222     const char *pName = &epName[0];
223     int32_t i;
224     int32_t ep;
225     if (funcName == NULL || epIndex < 0) {
226         return HDF_ERR_INVALID_PARAM;
227     }
228 
229     ret = snprintf_s(epName, MAX_NAMELEN, MAX_NAMELEN - 1, "/dev/%s/ep%d", funcName, epIndex);
230     if (ret < 0) {
231         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
232         return HDF_ERR_IO;
233     }
234 
235     for (i = 0; i < OPEN_CNT; i++) {
236         ep = handle_open(pName);
237         if (ep > 0) {
238             break;
239         }
240         usleep(SLEEP_TIME);
241     }
242     if (ep < 0) {
243         HDF_LOGE("unable to open %s", epName);
244         return HDF_ERR_IO;
245     }
246     return ep;
247 }
248 
UsbFnAdapterClosePipe(int32_t ep)249 static int32_t UsbFnAdapterClosePipe(int32_t ep)
250 {
251     if (ep < 0) {
252         return HDF_ERR_INVALID_PARAM;
253     }
254 
255     return handle_close(ep);
256 }
257 
GetHeaderStr(struct UsbFnStrings ** const strings,struct UsbFunctionfsStringsHead * headerStr)258 static void GetHeaderStr(struct UsbFnStrings ** const strings, struct UsbFunctionfsStringsHead *headerStr)
259 {
260     uint32_t i, j;
261     uint32_t langCount = 0;
262     uint32_t strCount = 0;
263     uint32_t len = 0;
264     for (i = 0; strings[i] != NULL; i++) {
265         langCount++;
266         for (j = 0; strings[i]->strings[j].s; j++) {
267             len += strlen(strings[i]->strings[j].s) + sizeof(char);
268         }
269         strCount = j;
270     }
271     headerStr->magic = htole32(FUNCTIONFS_STRINGS_MAGIC);
272     headerStr->length = htole32(sizeof(struct UsbFunctionfsStringsHead) + langCount * sizeof(uint16_t) + len);
273     headerStr->strCount = strCount;
274     headerStr->langCount = langCount;
275 }
276 
UsbFnWriteStrings(int32_t ep0,struct UsbFnStrings ** const strings)277 static int32_t UsbFnWriteStrings(int32_t ep0, struct UsbFnStrings ** const strings)
278 {
279     uint8_t *str = NULL;
280     uint8_t *whereDec = NULL;
281     uint32_t i, j;
282     int32_t ret;
283     struct UsbFunctionfsStringsHead headerStr = {0};
284 
285     GetHeaderStr(strings, &headerStr);
286     str = UsbFnMemCalloc(headerStr.length);
287     if (str == NULL) {
288         return HDF_ERR_MALLOC_FAIL;
289     }
290 
291     whereDec = str;
292     ret = memcpy_s(whereDec, headerStr.length, &headerStr, sizeof(struct UsbFunctionfsStringsHead));
293     if (ret != EOK) {
294         goto ERR;
295     }
296     whereDec += sizeof(struct UsbFunctionfsStringsHead);
297 
298     for (i = 0; i < headerStr.langCount; i++) {
299         ret = memcpy_s(whereDec, headerStr.length - (whereDec - str), &strings[i]->language, sizeof(uint16_t));
300         if (ret != EOK) {
301             goto ERR;
302         }
303         whereDec += sizeof(uint16_t);
304         for (j = 0; j < headerStr.strCount; j++) {
305             if (strlen(strings[i]->strings[j].s)) {
306                 ret = memcpy_s(whereDec, headerStr.length - (whereDec - str), strings[i]->strings[j].s,
307                     strlen(strings[i]->strings[j].s));
308                 whereDec += strlen(strings[i]->strings[j].s) + sizeof(char);
309             } else {
310                 break;
311             }
312             if (ret != EOK) {
313                 goto ERR;
314             }
315         }
316     }
317 
318     if (handle_write(ep0, str, headerStr.length) < 0) {
319         goto ERR;
320     }
321     UsbFnMemFree(str);
322     return 0;
323 ERR:
324     UsbFnMemFree(str);
325     return HDF_FAILURE;
326 }
327 
GetCountAndHead(struct UsbFunctionfsDescsHeadV2 * header,uint32_t * fsCount,uint32_t * hsCount,uint32_t * ssCount,const struct UsbFnFunction * func)328 static void GetCountAndHead(struct UsbFunctionfsDescsHeadV2 *header, uint32_t *fsCount, uint32_t *hsCount,
329     uint32_t *ssCount, const struct UsbFnFunction *func)
330 {
331     int32_t i;
332     uint32_t lenCount = 0;
333     uint32_t lenDes = 0;
334     *fsCount = 0;
335     *hsCount = 0;
336     *ssCount = 0;
337 
338     for (i = 0; func->fsDescriptors[i] != NULL; i++) {
339         (*fsCount)++;
340         lenDes += func->fsDescriptors[i]->bLength;
341     }
342     for (i = 0; func->hsDescriptors[i] != NULL; i++) {
343         (*hsCount)++;
344         lenDes += func->hsDescriptors[i]->bLength;
345     }
346     for (i = 0; func->ssDescriptors[i] != NULL; i++) {
347         (*ssCount)++;
348         lenDes += func->ssDescriptors[i]->bLength;
349     }
350 
351     if (*fsCount) {
352         lenCount += sizeof(uint32_t);
353         header->flags |= htole32(FUNCTIONFS_HAS_FS_DESC);
354     }
355     if (*hsCount) {
356         lenCount += sizeof(uint32_t);
357         header->flags |= htole32(FUNCTIONFS_HAS_HS_DESC);
358     }
359     if (*ssCount) {
360         lenCount += sizeof(uint32_t);
361         header->flags |= htole32(FUNCTIONFS_HAS_SS_DESC);
362     }
363 
364     header->magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
365     header->length = htole32(sizeof(struct UsbFunctionfsDescsHeadV2) + lenCount + lenDes);
366 }
367 
WriteFuncDescriptors(uint8_t ** const whereDec,struct UsbDescriptorHeader ** const headDes)368 static int32_t WriteFuncDescriptors(uint8_t ** const whereDec, struct UsbDescriptorHeader ** const headDes)
369 {
370     for (uint32_t i = 0; headDes[i] != NULL; i++) {
371         if (memcpy_s(*whereDec, headDes[i]->bLength, headDes[i], headDes[i]->bLength) != EOK) {
372             HDF_LOGE("%{public}s: memcpy_s failed!", __func__);
373             return HDF_FAILURE;
374         }
375         *whereDec += headDes[i]->bLength;
376     }
377     return 0;
378 }
379 
CopyCount(uint8_t ** whereDec,uint32_t fsCount,uint32_t hsCount,uint32_t ssCount)380 static int32_t CopyCount(uint8_t **whereDec, uint32_t fsCount, uint32_t hsCount, uint32_t ssCount)
381 {
382     int32_t ret;
383     if (fsCount) {
384         ret = memcpy_s(*whereDec, sizeof(uint32_t), &fsCount, sizeof(uint32_t));
385         if (ret != EOK) {
386             return HDF_FAILURE;
387         }
388         *whereDec += sizeof(uint32_t);
389     }
390     if (hsCount) {
391         ret = memcpy_s(*whereDec, sizeof(uint32_t), &hsCount, sizeof(uint32_t));
392         if (ret != EOK) {
393             return HDF_FAILURE;
394         }
395         *whereDec += sizeof(uint32_t);
396     }
397     if (ssCount) {
398         ret = memcpy_s(*whereDec, sizeof(uint32_t), &ssCount, sizeof(uint32_t));
399         if (ret != EOK) {
400             return HDF_FAILURE;
401         }
402         *whereDec += sizeof(uint32_t);
403     }
404 
405     return 0;
406 }
407 
UsbFnAdapterCreatPipes(int32_t ep0,const struct UsbFnFunction * func)408 static int32_t UsbFnAdapterCreatPipes(int32_t ep0, const struct UsbFnFunction *func)
409 {
410     uint8_t *dec = NULL;
411     uint8_t *whereDec = NULL;
412     int32_t ret;
413     uint32_t fsCount;
414     uint32_t hsCount;
415     uint32_t ssCount;
416     struct UsbFunctionfsDescsHeadV2 header = {0};
417 
418     GetCountAndHead(&header, &fsCount, &hsCount, &ssCount, func);
419 
420     dec = UsbFnMemCalloc(header.length);
421     if (dec == NULL) {
422         HDF_LOGE("%{public}s: UsbFnMemCalloc failed!", __func__);
423         return HDF_ERR_MALLOC_FAIL;
424     }
425     whereDec = dec;
426 
427     ret = memcpy_s(whereDec, header.length, &header, sizeof(struct UsbFunctionfsDescsHeadV2));
428     if (ret != EOK) {
429         return HDF_FAILURE;
430     }
431     whereDec += sizeof(struct UsbFunctionfsDescsHeadV2);
432 
433     ret = CopyCount(&whereDec, fsCount, hsCount, ssCount);
434     if (ret != EOK) {
435         return HDF_FAILURE;
436     }
437 
438     ret = WriteFuncDescriptors(&whereDec, func->fsDescriptors);
439     if (ret != EOK) {
440         return HDF_FAILURE;
441     }
442 
443     ret = WriteFuncDescriptors(&whereDec, func->hsDescriptors);
444     if (ret != EOK) {
445         return HDF_FAILURE;
446     }
447 
448     ret = WriteFuncDescriptors(&whereDec, func->ssDescriptors);
449     if (ret != EOK) {
450         return HDF_FAILURE;
451     }
452 
453     if (handle_write(ep0, dec, header.length) < 0) {
454         HDF_LOGE("unable do write descriptors");
455         UsbFnMemFree(dec);
456         return HDF_ERR_IO;
457     }
458 
459     UsbFnMemFree(dec);
460     ret = UsbFnWriteStrings(ep0, func->strings);
461     return ret;
462 }
463 
UsbFnAdapterPipeCreateAndClose(int32_t fdEp0,struct UsbFnConfiguration * const usbFnConfig,int32_t iCount)464 void UsbFnAdapterPipeCreateAndClose(int32_t fdEp0, struct UsbFnConfiguration * const usbFnConfig, int32_t iCount)
465 {
466     if (UsbFnAdapterCreatPipes(fdEp0, usbFnConfig->functions[iCount]) != HDF_SUCCESS) {
467         goto FAIL2;
468     }
469 
470     if (UsbFnAdapterClosePipe(fdEp0) != HDF_SUCCESS) {
471         goto FAIL2;
472     }
473 FAIL2:
474     UsbFnAdapterClosePipe(fdEp0);
475 }
476 
UsbFnAdapterWriteFunctions(int32_t fd,struct UsbFnConfiguration * const usbFnConfig,int32_t cmd,struct FconfigString * const gadgetName,struct FconfigString * const configName)477 static int32_t UsbFnAdapterWriteFunctions(int32_t fd, struct UsbFnConfiguration * const usbFnConfig, int32_t cmd,
478     struct FconfigString * const gadgetName, struct FconfigString * const configName)
479 {
480     if (usbFnConfig == NULL || gadgetName == NULL || configName == NULL) {
481         HDF_LOGE("%{public}s: usbFnConfig is NULL", __func__);
482         return HDF_ERR_IO;
483     }
484 
485     int32_t fdEp0;
486     struct FconfigFuncInfo funcInfo;
487     funcInfo.gadgetName.len = gadgetName->len;
488     funcInfo.gadgetName.s = gadgetName->s;
489     funcInfo.configName.len = configName->len;
490     funcInfo.configName.s = configName->s;
491     for (uint32_t iCount = 0; usbFnConfig->functions[iCount] != NULL; iCount++) {
492         char tmp[MAX_PATHLEN];
493         if (memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN) != EOK) {
494             HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
495             return HDF_FAILURE;
496         }
497         int32_t ret =
498             snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "generic.%s", usbFnConfig->functions[iCount]->funcName);
499         if (ret < 0) {
500             return HDF_ERR_IO;
501         }
502         ret = UsbFnAdapterCreateFconfigString(&funcInfo.funcName, tmp);
503         if (ret != HDF_SUCCESS) {
504             return HDF_ERR_MALLOC_FAIL;
505         }
506         if (handle_ioctl(fd, cmd, &funcInfo) != 0) {
507             goto FAIL;
508         }
509         UsbFnMemFree(funcInfo.funcName.s);
510         if (cmd == FCONFIG_CMD_DROP_FUNCTION) {
511             continue;
512         }
513 
514         fdEp0 = UsbFnAdapterOpenPipe(usbFnConfig->functions[iCount]->funcName, 0);
515         if (fd < 0) {
516             goto FAIL;
517         }
518         UsbFnAdapterPipeCreateAndClose(fdEp0, usbFnConfig, iCount);
519     }
520     return 0;
521 FAIL:
522     UsbFnMemFree(funcInfo.funcName.s);
523     return -1;
524 }
525 
UsbFnAdapterWriteConfigs(int32_t fd,struct FconfigString * const gadgetName,const struct UsbFnDeviceDesc * const descriptor)526 static int32_t UsbFnAdapterWriteConfigs(
527     int32_t fd, struct FconfigString * const gadgetName, const struct UsbFnDeviceDesc * const descriptor)
528 {
529     struct FconfigCfgDesc configDesc;
530     char tmp[MAX_PATHLEN];
531 
532     if (gadgetName == NULL || descriptor == NULL || descriptor->configs == NULL) {
533         HDF_LOGE("%{public}s: name is NULL", __func__);
534         return HDF_ERR_IO;
535     }
536     for (uint32_t iCount = 0; descriptor->configs[iCount]; iCount++) {
537         configDesc.gadgetName.len = gadgetName->len;
538         configDesc.gadgetName.s = gadgetName->s;
539         uint8_t confVal = descriptor->configs[iCount]->configurationValue;
540         if (memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN) != EOK) {
541             HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
542             return HDF_FAILURE;
543         }
544         int32_t ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "b.%u", confVal);
545         if (ret < 0) {
546             return HDF_ERR_IO;
547         }
548         ret = UsbFnAdapterCreateFconfigString(&configDesc.configName, tmp);
549         if (ret != HDF_SUCCESS) {
550             HDF_LOGE("%{public}s: create config name failed!", __func__);
551             return HDF_ERR_MALLOC_FAIL;
552         }
553         ret = UsbFnAdapterFillConfigDesc(&configDesc.cfgDesc, descriptor->configs[iCount]);
554         if (ret != HDF_SUCCESS) {
555             HDF_LOGE("%{public}s: UsbFnMemCalloc failed!", __func__);
556             return HDF_ERR_MALLOC_FAIL;
557         }
558         ret = handle_ioctl(fd, FCONFIG_CMD_ADD_CONFIG, &configDesc);
559         if (ret != HDF_SUCCESS) {
560             HDF_LOGE("%{public}s: ioctl failed!", __func__);
561             return HDF_ERR_MALLOC_FAIL;
562         }
563         ret = UsbFnAdapterWriteFunctions(
564             fd, descriptor->configs[iCount], FCONFIG_CMD_MAKE_FUNCTION, gadgetName, &configDesc.configName);
565         if (ret != HDF_SUCCESS) {
566             HDF_LOGE("%{public}s: write func failed!", __func__);
567             return HDF_ERR_MALLOC_FAIL;
568         }
569         UsbFnMemFree(configDesc.configName.s);
570     }
571     return 0;
572 }
573 
UsbFnAdapterWriteFcofnigUDC(int32_t fd,int32_t cmd,struct FconfigString * const gadgetName,const char * udcName)574 static int32_t UsbFnAdapterWriteFcofnigUDC(
575     int32_t fd, int32_t cmd, struct FconfigString * const gadgetName, const char *udcName)
576 {
577     int32_t ret;
578     struct FconfigUdcInfo udcInfo;
579 
580     if (gadgetName == NULL || udcName == NULL) {
581         return HDF_ERR_INVALID_PARAM;
582     }
583     udcInfo.gadgetName.len = gadgetName->len;
584     udcInfo.gadgetName.s = gadgetName->s;
585     ret = UsbFnAdapterCreateFconfigString(&udcInfo.udcName, udcName);
586     if (ret != HDF_SUCCESS) {
587         HDF_LOGE("%{public}s: create udc_name failed!", __func__);
588         return HDF_ERR_IO;
589     }
590     ret = handle_ioctl(fd, cmd, &udcInfo);
591     if (ret != 0) {
592         HDF_LOGE("%{public}s: ioctl failed!", __func__);
593     }
594     UsbFnMemFree(udcInfo.udcName.s);
595 
596     return ret;
597 }
598 
UsbFnAdapterCreateDevice(const char * udcName,const char * devName,struct UsbFnDeviceDesc * descriptor)599 static int32_t UsbFnAdapterCreateDevice(const char *udcName, const char *devName, struct UsbFnDeviceDesc *descriptor)
600 {
601     int32_t fd;
602     int32_t ret;
603     struct FconfigString gadgetName;
604 
605     fd = UsbFnAdapterOpenFn();
606     if (fd < 0) {
607         return HDF_ERR_IO;
608     }
609     ret = UsbFnAdapterCreateFconfigString(&gadgetName, devName);
610     if (ret != HDF_SUCCESS) {
611         HDF_LOGE("%{public}s: create gadget name failed!", __func__);
612         goto FAIL;
613     }
614     ret = UsbFnAdapterWriteGadget(fd, FCONFIG_CMD_MAKE_GADGET, &gadgetName);
615     if (ret != HDF_SUCCESS) {
616         dprintf("%s: UsbFnAdapterWriteGadget failed!", __func__);
617         goto EXIT;
618     }
619     ret = UsbFnAdapterWriteDevDesc(fd, &gadgetName, descriptor);
620     if (ret != HDF_SUCCESS) {
621         dprintf("%s: UsbFnAdapterWriteDevDesc failed!", __func__);
622         goto EXIT;
623     }
624     ret = UsbFnAdapterWriteDevStrings(fd, &gadgetName, descriptor);
625     if (ret != HDF_SUCCESS) {
626         dprintf("%s: UsbFnAdapterWriteDevStrings failed!", __func__);
627         goto EXIT;
628     }
629     ret = UsbFnAdapterWriteConfigs(fd, &gadgetName, descriptor);
630     if (ret != HDF_SUCCESS) {
631         dprintf("%s: UsbFnAdapterWriteConfigs failed!", __func__);
632         goto EXIT;
633     }
634     ret = UsbFnAdapterWriteFcofnigUDC(fd, FCONFIG_CMD_ENABLE_UDC, &gadgetName, udcName);
635     if (ret != HDF_SUCCESS) {
636         dprintf("%s: UsbFnAdapterWriteFcofnigUDC failed!", __func__);
637         goto EXIT;
638     }
639     dprintf("%s: create device success!\n", __func__);
640 EXIT:
641     UsbFnMemFree(gadgetName.s);
642 FAIL:
643     if (UsbFnAdapterClosefn(fd) != 0) {
644         dprintf("%s[%d] close fconfig failed\n", __func__, __LINE__);
645     }
646 
647     return ret;
648 }
649 
UsbFnAdapterDelConfigs(int32_t configFd,struct FconfigString * const gadgetName,struct UsbFnDeviceDesc * const descriptor)650 static int32_t UsbFnAdapterDelConfigs(
651     int32_t configFd, struct FconfigString * const gadgetName, struct UsbFnDeviceDesc * const descriptor)
652 {
653     struct FconfigCfgDesc configDesc;
654     struct FconfigString configName;
655     char tmp[MAX_PATHLEN];
656     int32_t ret;
657     int32_t iCount;
658     uint8_t confVal;
659 
660     if (gadgetName == NULL || descriptor == NULL) {
661         HDF_LOGE("%{public}s: name is NULL", __func__);
662         return HDF_ERR_INVALID_PARAM;
663     }
664     for (iCount = 0; descriptor->configs[iCount]; iCount++) {
665         configDesc.gadgetName.len = gadgetName->len;
666         configDesc.gadgetName.s = gadgetName->s;
667         confVal = descriptor->configs[iCount]->configurationValue;
668         ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "b.%u", confVal);
669         if (ret < 0) {
670             HDF_LOGE("%{public}s: snprintf_s failed", __func__);
671             return HDF_ERR_IO;
672         }
673         ret = UsbFnAdapterCreateFconfigString(&configName, tmp);
674         if (ret != HDF_SUCCESS) {
675             HDF_LOGE("%{public}s: create config name failed!", __func__);
676             return HDF_ERR_MALLOC_FAIL;
677         }
678         configDesc.configName.len = configName.len;
679         configDesc.configName.s = configName.s;
680         ret = UsbFnAdapterWriteFunctions(
681             configFd, descriptor->configs[iCount], FCONFIG_CMD_DROP_FUNCTION, gadgetName, &configName);
682         if (ret != HDF_SUCCESS) {
683             HDF_LOGE("%{public}s: write func failed!", __func__);
684             goto FAIL;
685         }
686         ret = UsbFnAdapterFillConfigDesc(&configDesc.cfgDesc, descriptor->configs[iCount]);
687         if (ret != HDF_SUCCESS) {
688             HDF_LOGE("%{public}s: UsbFnMemCalloc failed!", __func__);
689             goto FAIL;
690         }
691         ret = handle_ioctl(configFd, FCONFIG_CMD_REMOVE_CONFIG, &configDesc);
692         if (ret != HDF_SUCCESS) {
693             HDF_LOGE("%{public}s: ioctl failed!", __func__);
694             goto FAIL;
695         }
696         UsbFnMemFree(configName.s);
697     }
698     return 0;
699 FAIL:
700     UsbFnMemFree(configName.s);
701     return -1;
702 }
703 
UsbFnAdapterDelDevice(const char * devName,const char * udcName,struct UsbFnDeviceDesc * descriptor)704 static int32_t UsbFnAdapterDelDevice(const char *devName, const char *udcName, struct UsbFnDeviceDesc *descriptor)
705 {
706     int32_t configFd;
707     int32_t ret;
708     struct FconfigString gadgetName;
709 
710     if (devName == NULL || udcName == NULL || descriptor == NULL) {
711         return HDF_ERR_INVALID_PARAM;
712     }
713     configFd = UsbFnAdapterOpenFn();
714     if (configFd <= 0) {
715         return HDF_ERR_IO;
716     }
717     ret = UsbFnAdapterCreateFconfigString(&gadgetName, devName);
718     if (ret != HDF_SUCCESS) {
719         HDF_LOGE("%{public}s: create gadget_name failed!", __func__);
720         return HDF_ERR_IO;
721     }
722     ret = UsbFnAdapterWriteFcofnigUDC(configFd, FCONFIG_CMD_DISABLE_UDC, &gadgetName, udcName);
723     if (ret != HDF_SUCCESS) {
724         goto FAIL;
725     }
726     ret = UsbFnAdapterDelConfigs(configFd, &gadgetName, descriptor);
727     if (ret != HDF_SUCCESS) {
728         goto FAIL;
729     }
730     ret = UsbFnAdapterWriteGadget(configFd, FCONFIG_CMD_DROP_GADGET, &gadgetName);
731     if (ret != HDF_SUCCESS) {
732         goto FAIL;
733     }
734     ret = UsbFnAdapterClosefn(configFd);
735 
736 FAIL:
737     UsbFnMemFree(gadgetName.s);
738     return ret;
739 }
740 
UsbFnAdapterGetPipeInfo(int32_t ep,struct UsbFnPipeInfo * const pipeInfo)741 static int32_t UsbFnAdapterGetPipeInfo(int32_t ep, struct UsbFnPipeInfo * const pipeInfo)
742 {
743     if (ep <= 0 || pipeInfo == NULL) {
744         return HDF_ERR_INVALID_PARAM;
745     }
746 
747     struct usb_endpoint_descriptor desc;
748     if (memset_s(&desc, sizeof(desc), 0, sizeof(desc)) != EOK) {
749         HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
750         return HDF_FAILURE;
751     }
752 
753     int32_t ret = handle_ioctl(ep, GENERIC_CMD_GET_PIPE_INFO, &desc);
754     if (ret != HDF_SUCCESS) {
755         HDF_LOGE("%{public}s: FUNCTIONFS_ENDPOINT_DESC failed!", __func__);
756         return HDF_ERR_IO;
757     }
758 
759     pipeInfo->type = desc.bmAttributes;
760     pipeInfo->dir = USB_PIPE_DIRECTION_OUT;
761     if (desc.bEndpointAddress & 0x80) {
762         pipeInfo->dir = USB_PIPE_DIRECTION_IN;
763     }
764 
765     pipeInfo->maxPacketSize = (desc.wMaxPacketSize[0] | (desc.wMaxPacketSize[1] << MAX_SIZE));
766     pipeInfo->interval = desc.bInterval;
767 
768     return 0;
769 }
770 
UsbFnAdapterQueueInit(int32_t ep)771 static int32_t UsbFnAdapterQueueInit(int32_t ep)
772 {
773     (void)ep;
774     return 0;
775 }
776 
UsbFnAdapterQueueDel(int32_t ep)777 static int32_t UsbFnAdapterQueueDel(int32_t ep)
778 {
779     (void)ep;
780     return 0;
781 }
782 
UsbFnAdapterReleaseBuf(int32_t ep,const struct GenericMemory * mem)783 static int32_t UsbFnAdapterReleaseBuf(int32_t ep, const struct GenericMemory *mem)
784 {
785     return handle_ioctl(ep, GENERIC_CMD_FREE_MEM, &mem);
786 }
787 
UsbFnAdapterPipeIo(int32_t ep,struct IoData * const ioData)788 static int32_t UsbFnAdapterPipeIo(int32_t ep, struct IoData * const ioData)
789 {
790     int32_t ret;
791     if (ep <= 0 || ioData == NULL) {
792         return HDF_ERR_INVALID_PARAM;
793     }
794     if (ioData->aio) {
795         ret = handle_write(ep, (void *)ioData->buf, ioData->len);
796     } else {
797         ret = handle_ioctl(ep, GENERIC_CMD_ENDPOINT_IO, ioData);
798     }
799     return ret;
800 }
801 
UsbFnAdapterCancelIo(int32_t ep,const struct IoData * const ioData)802 static int32_t UsbFnAdapterCancelIo(int32_t ep, const struct IoData * const ioData)
803 {
804     struct GenericMemory mem;
805 
806     mem.buf = ioData->buf;
807     mem.size = ioData->len;
808 
809     return handle_ioctl(ep, GENERIC_CMD_CANCEL_REQUEST, &mem);
810 }
811 
UsbFnAdapterRequestGetStatus(int32_t ep,const struct IoData * ioData)812 static int32_t UsbFnAdapterRequestGetStatus(int32_t ep, const struct IoData *ioData)
813 {
814     if (ep <= 0 || ioData == NULL) {
815         return HDF_ERR_INVALID_PARAM;
816     }
817     return handle_ioctl(ep, GENERIC_CMD_GET_REQ_STATUS, (void *)ioData);
818 }
819 
UsbFnAdapterMapAddr(int32_t ep,uint32_t len)820 static uint8_t *UsbFnAdapterMapAddr(int32_t ep, uint32_t len)
821 {
822     return handle_mmap(ep, len);
823 }
824 
UsbFnAdapterUnmapAddr(uint8_t * mapAddr,uint32_t len)825 static int32_t UsbFnAdapterUnmapAddr(uint8_t *mapAddr, uint32_t len)
826 {
827     (void)mapAddr;
828     (void)len;
829     return 0;
830 }
831 
Ep0Event(struct UsbFnEventAll * const event,struct FconfigPollFd * const pfds)832 static int32_t Ep0Event(struct UsbFnEventAll * const event, struct FconfigPollFd * const pfds)
833 {
834     int32_t ret;
835     uint8_t i;
836     for (i = 0; i < event->ep0Num; i++) {
837         if (pfds[i].revents & POLLIN) {
838             ret = handle_read(event->ep0[i], &event->ep0Event[i].ctrlEvent, sizeof(struct UsbFnCtrlEvent));
839             if (ret < 0) {
840                 HDF_LOGE("unable to read event from ep0");
841                 return ret;
842             }
843             event->ep0Event[i].type = USB_EP0_CTRL_EVENT;
844         } else if (pfds[i].revents & POLLOUT) {
845             ret = handle_ioctl(event->ep0[i], GENERIC_CMD_GET_EP0_EVENT, &event->ep0Event[i].reqEvent);
846             if (ret < 0) {
847                 HDF_LOGE("unable to read reqEvent from ep0");
848                 return ret;
849             }
850             event->ep0Event[i].type = USB_EP0_IO_COMPLETED;
851         }
852     }
853     return 0;
854 }
855 
EpEvent(struct UsbFnEventAll * const event,struct FconfigPollFd * const pfds)856 static int32_t EpEvent(struct UsbFnEventAll * const event, struct FconfigPollFd * const pfds)
857 {
858     uint8_t i;
859     int32_t ret;
860     for (i = 0; i < event->epNum; i++) {
861         if ((pfds[i + event->ep0Num].revents & POLLIN)) {
862             ret = handle_read(event->epx[i], event->reqEvent[i], MAX_REQUEST * sizeof(struct UsbFnReqEvent));
863             if (ret < 0) {
864                 HDF_LOGE("unable to read event from eps");
865                 return ret;
866             }
867             event->numEvent[i] = (uint8_t)(ret / sizeof(struct UsbFnReqEvent));
868         }
869     }
870     return 0;
871 }
872 
UsbFnAdapterPollEvent(struct UsbFnEventAll * event,int32_t timeout)873 static int32_t UsbFnAdapterPollEvent(struct UsbFnEventAll *event, int32_t timeout)
874 {
875     uint8_t i;
876     struct FconfigPollFd pfds[16] = {0};
877     struct FconfigPollFd *pfd = &pfds[0];
878     if (event == NULL) {
879         return HDF_ERR_INVALID_PARAM;
880     }
881     if ((event->ep0Num + event->epNum) == 0) {
882         return HDF_ERR_INVALID_PARAM;
883     }
884 
885     if (memset_s(&pfds, sizeof(pfds), 0, sizeof(pfds)) != EOK) {
886         HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
887         return HDF_FAILURE;
888     }
889 
890     for (i = 0; i < event->ep0Num; i++) {
891         if (event->ep0[i] <= 0) {
892             HDF_LOGE("%{public}s: ep[%d] = %d", __func__, i, event->ep0[i]);
893             return HDF_ERR_INVALID_PARAM;
894         }
895         pfds[i].fd = event->ep0[i];
896         pfds[i].events = POLLIN | POLLOUT;
897     }
898     for (i = 0; i < event->epNum; i++) {
899         if (event->epx[i] <= 0) {
900             HDF_LOGE("%{public}s: ep[%d] = %d", __func__, i, event->epx[i]);
901             return HDF_ERR_INVALID_PARAM;
902         }
903         pfds[i + event->ep0Num].fd = event->epx[i];
904         pfds[i + event->ep0Num].events = POLLIN;
905     }
906     for (i = 0; i < (event->ep0Num + event->epNum); i++) {
907         pfds[i].revents = (uint32_t)handle_poll(pfds[i].fd, timeout);
908         if (pfds[i].revents < 0) {
909             HDF_LOGE("%{public}s: handle_poll failed", __func__);
910             return HDF_ERR_INVALID_PARAM;
911         }
912     }
913     if (Ep0Event(event, pfd) < 0) {
914         HDF_LOGE("%{public}s: handle_poll failed", __func__);
915         return HDF_ERR_IO;
916     }
917     if (EpEvent(event, pfd) < 0) {
918         HDF_LOGE("%{public}s: handle_poll failed", __func__);
919         return HDF_ERR_IO;
920     }
921     return 0;
922 }
923 
UsbFnAdapterWriteUDC(const char * deviceName,const char * udcName,int32_t enable)924 static int32_t UsbFnAdapterWriteUDC(const char *deviceName, const char *udcName, int32_t enable)
925 {
926     struct FconfigUdcInfo udcInfo;
927 
928     int32_t configFd = UsbFnAdapterOpenFn();
929     if (configFd <= 0) {
930         return HDF_ERR_IO;
931     }
932 
933     int32_t ret = UsbFnAdapterCreateFconfigString(&udcInfo.gadgetName, deviceName);
934     if (ret != HDF_SUCCESS) {
935         HDF_LOGE("%{public}s: create gadget_name failed!", __func__);
936         return HDF_ERR_IO;
937     }
938 
939     int32_t cmd = enable ? FCONFIG_CMD_ENABLE_UDC : FCONFIG_CMD_DISABLE_UDC;
940     ret = UsbFnAdapterWriteFcofnigUDC(configFd, cmd, &udcInfo.gadgetName, udcName);
941     if (ret != HDF_SUCCESS) {
942         return HDF_ERR_IO;
943     }
944 
945     ret = UsbFnAdapterClosefn(configFd);
946     if (ret != 0) {
947         HDF_LOGE("%{public}s: close failed!", __func__);
948     }
949     return 0;
950 }
951 
UsbFnWriteProp(const char * deviceName,const char * propName,uint32_t propValue)952 static int32_t UsbFnWriteProp(const char *deviceName, const char *propName, uint32_t propValue)
953 {
954     struct FconfigDevdescInfo info;
955     if (deviceName == NULL || propName == NULL) {
956         HDF_LOGE("%{public}s: param invail!", __func__);
957         return HDF_ERR_IO;
958     }
959     int32_t ret = UsbFnAdapterCreateFconfigString(&info.gadgetName, deviceName);
960     if (ret != HDF_SUCCESS) {
961         HDF_LOGE("%{public}s: create gadget name failed!", __func__);
962         return HDF_ERR_IO;
963     }
964     info.prop.propName = propName;
965     info.prop.propValue = (uint32_t)propValue;
966     int32_t configFd = UsbFnAdapterOpenFn();
967     if (configFd <= 0) {
968         ret = HDF_ERR_IO;
969         goto FAIL;
970     }
971     ret = handle_ioctl(configFd, FCONFIG_CMD_CHAGE_DEVINFO, &info);
972     if (ret != 0) {
973         HDF_LOGE("%{public}s: ioctl failed!", __func__);
974         goto FAIL;
975     }
976     ret = UsbFnAdapterClosefn(configFd);
977     if (ret != 0) {
978         HDF_LOGE("%{public}s: close failed!", __func__);
979     }
980 FAIL:
981     UsbFnMemFree(info.gadgetName.s);
982 
983     return ret;
984 }
985 
UsbFnWriteDesString(const char * deviceName,uint16_t lang,const char * stringName,const char * stringValue)986 static int32_t UsbFnWriteDesString(
987     const char *deviceName, uint16_t lang, const char *stringName, const char *stringValue)
988 {
989     struct FconfigDevDescString info;
990     if (deviceName == NULL || stringName == NULL || stringValue == NULL) {
991         HDF_LOGE("%{public}s: param invail!", __func__);
992         return HDF_ERR_IO;
993     }
994     int32_t ret = UsbFnAdapterCreateFconfigString(&info.gadgetName, deviceName);
995     if (ret != HDF_SUCCESS) {
996         HDF_LOGE("%{public}s: create gadget name failed!", __func__);
997         return HDF_ERR_IO;
998     }
999     info.prop.lang = lang;
1000     info.prop.propName = stringName;
1001     info.prop.propValue = stringValue;
1002     int32_t configFd = UsbFnAdapterOpenFn();
1003     if (configFd <= 0) {
1004         dprintf("%s, %d\n", __func__, __LINE__);
1005         ret = HDF_ERR_IO;
1006         goto FAIL;
1007     }
1008     ret = handle_ioctl(configFd, FCONFIG_CMD_CHAGE_DEVSTRING, &info);
1009     if (ret != 0) {
1010         HDF_LOGE("%{public}s: ioctl failed!", __func__);
1011         goto FAIL;
1012     }
1013     ret = UsbFnAdapterClosefn(configFd);
1014     if (ret != 0) {
1015         HDF_LOGE("%{public}s: close failed!", __func__);
1016     }
1017 FAIL:
1018     UsbFnMemFree(info.gadgetName.s);
1019 
1020     return ret;
1021 }
1022 
UsbFnMemAlloc(size_t size)1023 static void *UsbFnMemAlloc(size_t size)
1024 {
1025     return UsbFnMemCalloc(size);
1026 }
1027 
UsbFnMemCalloc(size_t size)1028 static void *UsbFnMemCalloc(size_t size)
1029 {
1030     void *buf = OsalMemCalloc(size);
1031     if (buf == NULL) {
1032         HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1033         return NULL;
1034     }
1035 
1036     if (g_usbRamTestFlag) {
1037         if (g_usbRamTestHead == NULL) {
1038             g_usbRamTestHead = OsalMemCalloc(sizeof(struct RawUsbRamTestList));
1039             if (g_usbRamTestHead == NULL) {
1040                 HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1041                 OsalMemFree(buf);
1042                 return NULL;
1043             }
1044             OsalMutexInit(&g_usbRamTestHead->lock);
1045             DListHeadInit(&g_usbRamTestHead->list);
1046         }
1047         struct RawUsbRamTestList *testEntry = OsalMemCalloc(sizeof(struct RawUsbRamTestList));
1048         if (testEntry == NULL) {
1049             HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1050             OsalMemFree(buf);
1051             return NULL;
1052         }
1053         testEntry->address = (uintptr_t)buf;
1054         testEntry->size = size;
1055 
1056         struct RawUsbRamTestList *pos = NULL;
1057         uint32_t totalSize = 0;
1058         OsalMutexLock(&g_usbRamTestHead->lock);
1059         DListInsertTail(&testEntry->list, &g_usbRamTestHead->list);
1060         DLIST_FOR_EACH_ENTRY(pos, &g_usbRamTestHead->list, struct RawUsbRamTestList, list) {
1061             totalSize += pos->size;
1062         }
1063         OsalMutexUnlock(&g_usbRamTestHead->lock);
1064 
1065         HDF_LOGI("%{public}s add size=%{public}d totalSize=%{public}d", __func__, (uint32_t)size, totalSize);
1066     }
1067     return buf;
1068 }
1069 
UsbFnMemFree(const void * mem)1070 void UsbFnMemFree(const void *mem)
1071 {
1072     if (mem == NULL) {
1073         HDF_LOGE("%{public}s:%{public}d invalid param mem.", __func__, __LINE__);
1074         return;
1075     }
1076 
1077     if (g_usbRamTestFlag && (g_usbRamTestHead != NULL)) {
1078         struct RawUsbRamTestList *pos = NULL;
1079         struct RawUsbRamTestList *tmp = NULL;
1080         uint32_t totalSize = 0;
1081         uint32_t size = 0;
1082         OsalMutexLock(&g_usbRamTestHead->lock);
1083         DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_usbRamTestHead->list, struct RawUsbRamTestList, list) {
1084             if (pos->address == (uintptr_t)mem) {
1085                 size = pos->size;
1086                 DListRemove(&pos->list);
1087                 OsalMemFree(pos);
1088                 continue;
1089             }
1090             totalSize += pos->size;
1091         }
1092         OsalMutexUnlock(&g_usbRamTestHead->lock);
1093         HDF_LOGE("%{public}s:rm size = %{public}u, totalSize = %{public}u", __func__, size, totalSize);
1094     }
1095 
1096     if (mem != NULL) {
1097         OsalMemFree((void *)mem);
1098     }
1099 }
1100 
UsbFnAdpMemTestTrigger(bool enable)1101 int32_t UsbFnAdpMemTestTrigger(bool enable)
1102 {
1103     g_usbRamTestFlag = enable;
1104     return HDF_SUCCESS;
1105 }
1106 
1107 static struct UsbFnAdapterOps g_usbFnAdapter = {
1108     .createDevice = UsbFnAdapterCreateDevice,
1109     .delDevice = UsbFnAdapterDelDevice,
1110 
1111     .openPipe = UsbFnAdapterOpenPipe,
1112     .closePipe = UsbFnAdapterClosePipe,
1113     .getPipeInfo = UsbFnAdapterGetPipeInfo,
1114 
1115     .queueInit = UsbFnAdapterQueueInit,
1116     .queueDel = UsbFnAdapterQueueDel,
1117     .releaseBuf = UsbFnAdapterReleaseBuf,
1118     .pipeIo = UsbFnAdapterPipeIo,
1119     .cancelIo = UsbFnAdapterCancelIo,
1120     .getReqStatus = UsbFnAdapterRequestGetStatus,
1121     .mapAddr = UsbFnAdapterMapAddr,
1122     .unmapAddr = UsbFnAdapterUnmapAddr,
1123     .pollEvent = UsbFnAdapterPollEvent,
1124     .writeUDC = UsbFnAdapterWriteUDC,
1125     .writeProp = UsbFnWriteProp,
1126     .writeDesString = UsbFnWriteDesString,
1127 };
1128 
UsbFnAdapterGetOps(void)1129 struct UsbFnAdapterOps *UsbFnAdapterGetOps(void)
1130 {
1131     return &g_usbFnAdapter;
1132 }
1133