• 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 
18 #include <dirent.h>
19 #include <endian.h>
20 #include <fcntl.h>
21 #include <poll.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/eventfd.h>
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 
32 #include "osal_time.h"
33 
34 #define HDF_LOG_TAG adapter_if
35 #define SLEEP_DELAY 100000
36 #define OPEN_CNT    30
37 
38 static struct RawUsbRamTestList *g_usbRamTestHead = NULL;
39 static bool g_usbRamTestFlag = false;
40 
IsDirExist(const char * path)41 static bool IsDirExist(const char *path)
42 {
43     DIR *dir = NULL;
44 
45     if (path == NULL) {
46         HDF_LOGE("%{public}s:%{public}d invalid param path.", __func__, __LINE__);
47         return false;
48     }
49 
50     dir = opendir(path);
51     if (dir == NULL) {
52         HDF_LOGE("%{public}s: %{public}d: opendir failed!, path is %{public}s, errno is %{public}d", __func__, __LINE__,
53             path, errno);
54         return false;
55     }
56     closedir(dir);
57     return true;
58 }
59 
IsDir(const char * path)60 static bool IsDir(const char *path)
61 {
62     struct stat statBuf;
63     if (lstat(path, &statBuf) == 0) {
64         return S_ISDIR(statBuf.st_mode) != 0;
65     }
66     return false;
67 }
68 
GetFilePath(const char * path,const char * fileName,char * filePath)69 static void GetFilePath(const char *path, const char *fileName, char *filePath)
70 {
71     int32_t ret = strcpy_s(filePath, MAX_PATHLEN - 1, path);
72     if (ret != EOK) {
73         HDF_LOGE("%{public}s: strcpy_s failed", __func__);
74         return;
75     }
76 
77     if (filePath[strlen(path) - 1] != '/') {
78         ret = strcat_s(filePath, MAX_PATHLEN - 1, "/");
79         if (ret != EOK) {
80             HDF_LOGE("%{public}s: strcat_s failed", __func__);
81             return;
82         }
83     }
84 
85     ret = strcat_s(filePath, MAX_PATHLEN - 1, fileName);
86     if (ret != EOK) {
87         HDF_LOGE("%{public}s: strcat_s failed", __func__);
88     }
89 }
90 
IsSpecialDir(const char * path)91 static bool IsSpecialDir(const char *path)
92 {
93     return (strcmp(path, ".") == 0) || strcmp(path, "..") == 0;
94 }
95 
DeleteFile(const char * path)96 static void DeleteFile(const char *path)
97 {
98     DIR *dir = NULL;
99     struct dirent *dirInfo = NULL;
100 
101     if (IsDir(path)) {
102         if ((dir = opendir(path)) == NULL) {
103             return;
104         }
105         char filePath[PATH_MAX];
106         while ((dirInfo = readdir(dir)) != NULL) {
107             GetFilePath(path, dirInfo->d_name, filePath);
108             if (IsSpecialDir(dirInfo->d_name)) {
109                 continue;
110             }
111             DeleteFile(filePath);
112             (void)remove(filePath);
113         }
114         closedir(dir);
115     }
116 }
117 
IsDeviceDirExist(const char * deviceName)118 static bool IsDeviceDirExist(const char *deviceName)
119 {
120     char tmp[MAX_PATHLEN] = {0};
121     int32_t ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s", CONFIGFS_DIR, deviceName);
122     if (ret < 0) {
123         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
124         return false;
125     }
126 
127     return IsDirExist(tmp);
128 }
129 
UsbFnWriteFile(const char * path,const char * str)130 static int32_t UsbFnWriteFile(const char *path, const char *str)
131 {
132     size_t ret;
133     if (strlen(str) == 0) {
134         return 0;
135     }
136 
137     FILE *fp = fopen(path, "w");
138     if (fp == NULL) {
139         HDF_LOGE("%{public}s: UsbFnWriteFile failed", __func__);
140         return HDF_ERR_BAD_FD;
141     }
142 
143     ret = fwrite(str, strlen(str), 1, fp);
144     if (ret != 1) {
145         (void)fclose(fp);
146         return HDF_FAILURE;
147     }
148     (void)fclose(fp);
149     return 0;
150 }
151 
UsbFnWriteProp(const char * deviceName,const char * propName,uint32_t propValue)152 static int32_t UsbFnWriteProp(const char *deviceName, const char *propName, uint32_t propValue)
153 {
154     char tmp[MAX_PATHLEN] = {0};
155     char tmpVal[MAX_NAMELEN] = {0};
156     int32_t ret;
157     if (deviceName == NULL || propName == NULL) {
158         HDF_LOGE("%{public}s: INVALID PARAM", __func__);
159         return HDF_ERR_INVALID_PARAM;
160     }
161     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/%s", CONFIGFS_DIR, deviceName, propName);
162     if (ret < 0) {
163         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
164         return HDF_ERR_IO;
165     }
166 
167     ret = snprintf_s(tmpVal, MAX_NAMELEN, MAX_NAMELEN - 1, "0x%x", propValue);
168     if (ret < 0) {
169         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
170         return HDF_ERR_IO;
171     }
172 
173     return UsbFnWriteFile(tmp, tmpVal);
174 }
175 
UsbFnWriteConfString(const char * deviceName,int32_t configVal,uint16_t lang,const char * stringValue)176 static int32_t UsbFnWriteConfString(const char *deviceName, int32_t configVal, uint16_t lang, const char *stringValue)
177 {
178     int32_t ret;
179     char tmp[MAX_PATHLEN] = {0};
180     char tmpPath[MAX_PATHLEN] = {0};
181     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/configs/b.%d/strings/0x%x", CONFIGFS_DIR, deviceName,
182         configVal, lang);
183     if (ret < 0) {
184         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
185         return HDF_ERR_IO;
186     }
187 
188     if (!IsDirExist(tmp)) {
189         ret = mkdir(tmp, S_IREAD | S_IWRITE);
190         if (ret != 0) {
191             HDF_LOGE("%{public}s: mkdir failed", __func__);
192             return HDF_ERR_IO;
193         }
194     }
195 
196     ret = snprintf_s(tmpPath, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/configuration", tmp);
197     if (ret < 0) {
198         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
199         return HDF_ERR_IO;
200     }
201 
202     ret = UsbFnWriteFile(tmpPath, stringValue);
203     return ret;
204 }
205 
UsbFnWriteDesString(const char * deviceName,uint16_t lang,const char * stringName,const char * stringValue)206 static int32_t UsbFnWriteDesString(
207     const char *deviceName, uint16_t lang, const char *stringName, const char *stringValue)
208 {
209     int32_t ret;
210     char tmp[MAX_PATHLEN] = {0};
211     char tmpPath[MAX_PATHLEN] = {0};
212     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/strings/0x%x", CONFIGFS_DIR, deviceName, lang);
213     if (ret < 0) {
214         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
215         return HDF_ERR_IO;
216     }
217     if (!IsDirExist(tmp)) {
218         ret = mkdir(tmp, S_IREAD | S_IWRITE);
219         if (ret != 0) {
220             HDF_LOGE("%{public}s: mkdir failed", __func__);
221             return HDF_ERR_IO;
222         }
223     }
224 
225     ret = snprintf_s(tmpPath, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s", tmp, stringName);
226     if (ret < 0) {
227         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
228         return HDF_ERR_IO;
229     }
230 
231     ret = UsbFnWriteFile(tmpPath, stringValue);
232     return ret;
233 }
234 
UsbFnAdapterCreateFunc(const char * configPath,const char * funcPath)235 static int32_t UsbFnAdapterCreateFunc(const char *configPath, const char *funcPath)
236 {
237     int32_t ret;
238 
239     ret = mkdir(funcPath, S_IREAD | S_IWRITE);
240     if (ret != 0) {
241         HDF_LOGE("%{public}s: mkdir failed", __func__);
242         return HDF_ERR_IO;
243     }
244 
245     ret = symlink(funcPath, configPath);
246     if (ret != 0) {
247         HDF_LOGE("%{public}s: symlink failed", __func__);
248         return HDF_ERR_IO;
249     }
250     usleep(SLEEP_DELAY);
251     return 0;
252 }
253 
UsbFnReadFile(const char * path,char * str,uint16_t len)254 static int32_t UsbFnReadFile(const char *path, char *str, uint16_t len)
255 {
256     FILE *fp = fopen(path, "r");
257     if (fp == NULL) {
258         HDF_LOGE("%{public}s: fopen failed", __func__);
259         return HDF_ERR_BAD_FD;
260     }
261     if (fread(str, len, 1, fp) != 1) {
262         HDF_LOGE("%{public}s: fread failed", __func__);
263         (void)fclose(fp);
264         return HDF_ERR_IO;
265     }
266     (void)fclose(fp);
267     return 0;
268 }
269 
UsbFnAdapterWriteUDC(const char * deviceName,const char * udcName,int32_t enable)270 static int32_t UsbFnAdapterWriteUDC(const char *deviceName, const char *udcName, int32_t enable)
271 {
272     char tmp[MAX_PATHLEN] = {0};
273     if (deviceName == NULL || udcName == NULL || IsDeviceDirExist(deviceName) == false) {
274         return HDF_ERR_INVALID_PARAM;
275     }
276 
277     int32_t ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/UDC", CONFIGFS_DIR, deviceName);
278     if (ret < 0) {
279         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
280         return HDF_ERR_IO;
281     }
282     if (enable != 0) {
283         (void)UsbFnWriteFile(tmp, udcName);
284         char udcTmp[MAX_NAMELEN] = {0};
285         for (int32_t i = 0; i < OPEN_CNT; i++) {
286             (void)UsbFnReadFile(tmp, udcTmp, strlen(udcName));
287             if (!strcmp(udcName, udcTmp)) {
288                 return 0;
289             }
290             usleep(SLEEP_DELAY);
291         }
292         if (strcmp(udcName, udcTmp)) {
293             return HDF_ERR_IO;
294         }
295     } else {
296         (void)UsbFnWriteFile(tmp, "\n");
297     }
298     return 0;
299 }
300 
UsbFnAdapterOpenFn(void)301 static int32_t UsbFnAdapterOpenFn(void)
302 {
303     int32_t i;
304     int32_t ep = -1;
305     for (i = 0; i < OPEN_CNT; i++) {
306         ep = open(USBFN_DEV, O_RDWR);
307         if (ep > 0) {
308             break;
309         }
310         usleep(SLEEP_DELAY);
311     }
312     if (ep < 0) {
313         HDF_LOGE("func not alloc");
314     }
315     return ep;
316 }
317 
UsbFnAdapterClosefn(int32_t fd)318 static int32_t UsbFnAdapterClosefn(int32_t fd)
319 {
320     if (fd <= 0) {
321         return HDF_ERR_INVALID_PARAM;
322     }
323     return close(fd);
324 }
325 
UsbFnAdapterCreatInterface(const char * interfaceName,int32_t nameLen)326 static int32_t UsbFnAdapterCreatInterface(const char *interfaceName, int32_t nameLen)
327 {
328     int32_t ret;
329     int32_t fd;
330     struct FuncNew fnnew;
331     if (interfaceName == NULL || nameLen <= 0) {
332         return HDF_ERR_INVALID_PARAM;
333     }
334     fd = UsbFnAdapterOpenFn();
335     if (fd <= 0) {
336         HDF_LOGE("%{public}s: UsbFnAdapterOpenFn failed", __func__);
337         return HDF_ERR_IO;
338     }
339 
340     fnnew.nameLen = (int32_t)nameLen;
341     ret = snprintf_s(fnnew.name, MAX_NAMELEN, MAX_NAMELEN - 1, "%s", interfaceName);
342     if (ret < 0) {
343         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
344         return HDF_ERR_IO;
345     }
346     ret = ioctl(fd, FUNCTIONFS_NEWFN, &fnnew);
347     if (ret != 0) {
348         HDF_LOGE("%{public}s: FUNCTIONFS_NEWFN failed", __func__);
349         return HDF_ERR_IO;
350     }
351     ret = UsbFnAdapterClosefn(fd);
352     usleep(SLEEP_DELAY);
353     return ret;
354 }
355 
UsbFnAdapterDelInterface(const char * interfaceName,int32_t nameLen)356 static int32_t UsbFnAdapterDelInterface(const char *interfaceName, int32_t nameLen)
357 {
358     int32_t ret;
359     struct FuncNew fnnew;
360     if (interfaceName == NULL || nameLen <= 0) {
361         return HDF_ERR_INVALID_PARAM;
362     }
363 
364     int32_t fd = UsbFnAdapterOpenFn();
365     if (fd <= 0) {
366         return HDF_ERR_IO;
367     }
368 
369     fnnew.nameLen = (uint32_t)nameLen;
370     ret = snprintf_s(fnnew.name, MAX_NAMELEN, MAX_NAMELEN - 1, "%s", interfaceName);
371     if (ret < 0) {
372         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
373         return HDF_ERR_IO;
374     }
375     ret = ioctl(fd, FUNCTIONFS_DELFN, &fnnew);
376     if (ret != 0) {
377         HDF_LOGE("%{public}s: FUNCTIONFS_DELFN failed", __func__);
378         return HDF_ERR_IO;
379     }
380     ret = UsbFnAdapterClosefn(fd);
381     return ret;
382 }
383 
UsbFnAdapterOpenPipe(const char * interfaceName,int32_t epIndex)384 static int32_t UsbFnAdapterOpenPipe(const char *interfaceName, int32_t epIndex)
385 {
386     if (interfaceName == NULL || epIndex < 0) {
387         return HDF_ERR_INVALID_PARAM;
388     }
389 
390     char epName[MAX_NAMELEN];
391     int32_t ret = snprintf_s(epName, MAX_NAMELEN, MAX_NAMELEN - 1, "/dev/functionfs/%s.ep%d", interfaceName, epIndex);
392     if (ret < 0) {
393         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
394         return HDF_ERR_IO;
395     }
396 
397     int32_t ep = -1;
398     for (int32_t i = 0; i < OPEN_CNT; i++) {
399         ep = open(epName, O_RDWR);
400         if (ep > 0) {
401             break;
402         }
403         usleep(SLEEP_DELAY);
404     }
405     if (ep < 0) {
406         HDF_LOGE("unable to open %s", epName);
407         return HDF_DEV_ERR_NO_DEVICE;
408     }
409     return ep;
410 }
411 
UsbFnAdapterClosePipe(int32_t ep)412 static int32_t UsbFnAdapterClosePipe(int32_t ep)
413 {
414     if (ep <= 0) {
415         return HDF_ERR_INVALID_PARAM;
416     }
417 
418     return close(ep);
419 }
420 
GetHeaderStr(struct UsbFnStrings ** const strings,struct UsbFunctionfsStringsHead * headerStr)421 static void GetHeaderStr(struct UsbFnStrings ** const strings, struct UsbFunctionfsStringsHead *headerStr)
422 {
423     uint32_t i, j;
424     uint32_t langCount = 0;
425     uint32_t strCount = 0;
426     uint32_t len = 0;
427     for (i = 0; strings[i] != NULL; i++) {
428         langCount++;
429         for (j = 0; strings[i]->strings[j].s; j++) {
430             len += strlen(strings[i]->strings[j].s) + sizeof(char);
431         }
432         strCount = j;
433     }
434     headerStr->magic = htole32(FUNCTIONFS_STRINGS_MAGIC);
435     headerStr->length = htole32(sizeof(struct UsbFunctionfsStringsHead) + langCount * sizeof(uint16_t) + len);
436     headerStr->strCount = strCount;
437     headerStr->langCount = langCount;
438 }
439 
UsbFnWriteStrings(int32_t ep0,struct UsbFnStrings ** const strings)440 static int32_t UsbFnWriteStrings(int32_t ep0, struct UsbFnStrings ** const strings)
441 {
442     uint8_t *str = NULL;
443     uint8_t *whereDec = NULL;
444     uint32_t i, j;
445     int32_t ret;
446     struct UsbFunctionfsStringsHead headerStr = {0};
447 
448     GetHeaderStr(strings, &headerStr);
449     str = UsbFnMemCalloc(headerStr.length);
450     if (str == NULL) {
451         return HDF_ERR_MALLOC_FAIL;
452     }
453 
454     whereDec = str;
455     ret = memcpy_s(whereDec, headerStr.length, &headerStr, sizeof(struct UsbFunctionfsStringsHead));
456     if (ret != EOK) {
457         goto ERR;
458     }
459     whereDec += sizeof(struct UsbFunctionfsStringsHead);
460 
461     for (i = 0; i < headerStr.langCount; i++) {
462         ret = memcpy_s(whereDec, headerStr.length - (whereDec - str), &strings[i]->language, sizeof(uint16_t));
463         if (ret != EOK) {
464             goto ERR;
465         }
466         whereDec += sizeof(uint16_t);
467         for (j = 0; j < headerStr.strCount; j++) {
468             if (strlen(strings[i]->strings[j].s)) {
469                 ret = memcpy_s(whereDec, headerStr.length - (whereDec - str), strings[i]->strings[j].s,
470                     strlen(strings[i]->strings[j].s));
471                 whereDec += strlen(strings[i]->strings[j].s) + sizeof(char);
472             } else {
473                 break;
474             }
475             if (ret != EOK) {
476                 goto ERR;
477             }
478         }
479     }
480 
481     if (write(ep0, str, headerStr.length) < 0) {
482         goto ERR;
483     }
484     UsbFnMemFree(str);
485     return 0;
486 ERR:
487     UsbFnMemFree(str);
488     return HDF_FAILURE;
489 }
490 
CopyCount(uint8_t ** whereDec,uint32_t fsCount,uint32_t hsCount,uint32_t ssCount)491 static int32_t CopyCount(uint8_t **whereDec, uint32_t fsCount, uint32_t hsCount, uint32_t ssCount)
492 {
493     int32_t ret;
494     if (fsCount != 0) {
495         ret = memcpy_s(*whereDec, sizeof(uint32_t), &fsCount, sizeof(uint32_t));
496         if (ret != EOK) {
497             return HDF_FAILURE;
498         }
499         *whereDec += sizeof(uint32_t);
500     }
501     if (hsCount != 0) {
502         ret = memcpy_s(*whereDec, sizeof(uint32_t), &hsCount, sizeof(uint32_t));
503         if (ret != EOK) {
504             return HDF_FAILURE;
505         }
506         *whereDec += sizeof(uint32_t);
507     }
508     if (ssCount != 0) {
509         ret = memcpy_s(*whereDec, sizeof(uint32_t), &ssCount, sizeof(uint32_t));
510         if (ret != EOK) {
511             return HDF_FAILURE;
512         }
513         *whereDec += sizeof(uint32_t);
514     }
515 
516     return 0;
517 }
518 
WriteFuncDescriptors(uint8_t ** const whereDec,struct UsbDescriptorHeader ** const headDes)519 static int32_t WriteFuncDescriptors(uint8_t ** const whereDec, struct UsbDescriptorHeader ** const headDes)
520 {
521     for (uint32_t i = 0; headDes[i] != NULL; i++) {
522         if (memcpy_s(*whereDec, headDes[i]->bLength, headDes[i], headDes[i]->bLength) != EOK) {
523             HDF_LOGE("%{public}s: memcpy_s failed", __func__);
524             return HDF_FAILURE;
525         }
526         *whereDec += headDes[i]->bLength;
527     }
528     return 0;
529 }
530 
GetCountAndHead(struct UsbFunctionfsDescsHeadV2 * header,uint32_t * fsCount,uint32_t * hsCount,uint32_t * ssCount,const struct UsbFnFunction * func)531 static void GetCountAndHead(struct UsbFunctionfsDescsHeadV2 *header, uint32_t *fsCount, uint32_t *hsCount,
532     uint32_t *ssCount, const struct UsbFnFunction *func)
533 {
534     int32_t i;
535     uint32_t lenCount = 0;
536     uint32_t lenDes = 0;
537     *fsCount = 0;
538     *hsCount = 0;
539     *ssCount = 0;
540 
541     for (i = 0; func->fsDescriptors[i] != NULL; i++) {
542         (*fsCount)++;
543         lenDes += func->fsDescriptors[i]->bLength;
544     }
545     for (i = 0; func->hsDescriptors[i] != NULL; i++) {
546         (*hsCount)++;
547         lenDes += func->hsDescriptors[i]->bLength;
548     }
549     for (i = 0; func->ssDescriptors[i] != NULL; i++) {
550         (*ssCount)++;
551         lenDes += func->ssDescriptors[i]->bLength;
552     }
553 
554     if (*fsCount != 0) {
555         lenCount += sizeof(uint32_t);
556         header->flags |= htole32(FUNCTIONFS_HAS_FS_DESC);
557     }
558     if (*hsCount != 0) {
559         lenCount += sizeof(uint32_t);
560         header->flags |= htole32(FUNCTIONFS_HAS_HS_DESC);
561     }
562     if (*ssCount != 0) {
563         lenCount += sizeof(uint32_t);
564         header->flags |= htole32(FUNCTIONFS_HAS_SS_DESC);
565     }
566 
567     header->magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
568     header->length = htole32(sizeof(struct UsbFunctionfsDescsHeadV2) + lenCount + lenDes);
569 }
570 
UsbFnAdapterCreatPipes(int32_t ep0,const struct UsbFnFunction * func)571 static int32_t UsbFnAdapterCreatPipes(int32_t ep0, const struct UsbFnFunction *func)
572 {
573     uint8_t *dec = NULL;
574     uint8_t *whereDec = NULL;
575     int32_t ret;
576     uint32_t fsCount;
577     uint32_t hsCount;
578     uint32_t ssCount;
579     struct UsbFunctionfsDescsHeadV2 header = {0};
580 
581     GetCountAndHead(&header, &fsCount, &hsCount, &ssCount, func);
582 
583     dec = UsbFnMemCalloc(header.length);
584     if (dec == NULL) {
585         HDF_LOGE("%{public}s: UsbFnMemCalloc failed", __func__);
586         return HDF_ERR_MALLOC_FAIL;
587     }
588     whereDec = dec;
589 
590     ret = memcpy_s(whereDec, header.length, &header, sizeof(struct UsbFunctionfsDescsHeadV2));
591     if (ret != EOK) {
592         return HDF_FAILURE;
593     }
594     whereDec += sizeof(struct UsbFunctionfsDescsHeadV2);
595 
596     ret = CopyCount(&whereDec, fsCount, hsCount, ssCount);
597     if (ret != EOK) {
598         return HDF_FAILURE;
599     }
600 
601     ret = WriteFuncDescriptors(&whereDec, func->fsDescriptors);
602     if (ret != EOK) {
603         return HDF_FAILURE;
604     }
605 
606     ret = WriteFuncDescriptors(&whereDec, func->hsDescriptors);
607     if (ret != EOK) {
608         return HDF_FAILURE;
609     }
610 
611     ret = WriteFuncDescriptors(&whereDec, func->ssDescriptors);
612     if (ret != EOK) {
613         return HDF_FAILURE;
614     }
615 
616     if (write(ep0, dec, header.length) < 0) {
617         HDF_LOGE("unable do write descriptors");
618         UsbFnMemFree(dec);
619         return HDF_ERR_IO;
620     }
621 
622     UsbFnMemFree(dec);
623     ret = UsbFnWriteStrings(ep0, func->strings);
624 
625     usleep(SLEEP_DELAY);
626     return ret;
627 }
628 
WriteDeviceId(const char * devName,const struct UsbDeviceDescriptor * desc)629 static int32_t WriteDeviceId(const char *devName, const struct UsbDeviceDescriptor *desc)
630 {
631     int32_t ret;
632     ret = UsbFnWriteProp(devName, "idVendor", desc->idVendor);
633     if (ret != HDF_SUCCESS) {
634         return HDF_ERR_INVALID_PARAM;
635     }
636     ret = UsbFnWriteProp(devName, "idProduct", desc->idProduct);
637     if (ret != HDF_SUCCESS) {
638         return HDF_ERR_INVALID_PARAM;
639     }
640     ret = UsbFnWriteProp(devName, "bcdUSB", desc->bcdUSB);
641     if (ret != HDF_SUCCESS) {
642         return HDF_ERR_INVALID_PARAM;
643     }
644     ret = UsbFnWriteProp(devName, "bcdDevice", desc->bcdDevice);
645     if (ret != HDF_SUCCESS) {
646         return HDF_ERR_INVALID_PARAM;
647     }
648     ret = UsbFnWriteProp(devName, "bDeviceClass", desc->bDeviceClass);
649     if (ret != HDF_SUCCESS) {
650         return HDF_ERR_INVALID_PARAM;
651     }
652     ret = UsbFnWriteProp(devName, "bDeviceSubClass", desc->bDeviceSubClass);
653     if (ret != HDF_SUCCESS) {
654         return HDF_ERR_INVALID_PARAM;
655     }
656     ret = UsbFnWriteProp(devName, "bDeviceProtocol", desc->bDeviceProtocol);
657     if (ret != HDF_SUCCESS) {
658         return HDF_ERR_INVALID_PARAM;
659     }
660     ret = UsbFnWriteProp(devName, "bMaxPacketSize0", desc->bMaxPacketSize0);
661     if (ret != HDF_SUCCESS) {
662         return HDF_ERR_INVALID_PARAM;
663     }
664     return 0;
665 }
666 
WriteDeviceDescriptor(const char * devName,const struct UsbDeviceDescriptor * desc,struct UsbFnStrings ** strings)667 static int32_t WriteDeviceDescriptor(
668     const char *devName, const struct UsbDeviceDescriptor *desc, struct UsbFnStrings **strings)
669 {
670     int32_t i, ret;
671     ret = WriteDeviceId(devName, desc);
672     if (ret != HDF_SUCCESS) {
673         return HDF_ERR_INVALID_PARAM;
674     }
675 
676     for (i = 0; strings[i] != NULL; i++) {
677         ret = UsbFnWriteDesString(
678             devName, strings[i]->language, "manufacturer", strings[i]->strings[desc->iManufacturer].s);
679         if (ret != HDF_SUCCESS) {
680             return HDF_ERR_INVALID_PARAM;
681         }
682         ret = UsbFnWriteDesString(devName, strings[i]->language, "product", strings[i]->strings[desc->iProduct].s);
683         if (ret != HDF_SUCCESS) {
684             return HDF_ERR_INVALID_PARAM;
685         }
686         ret = UsbFnWriteDesString(
687             devName, strings[i]->language, "serialnumber", strings[i]->strings[desc->iSerialNumber].s);
688         if (ret != HDF_SUCCESS) {
689             return HDF_ERR_INVALID_PARAM;
690         }
691     }
692     return 0;
693 }
694 
CreatDeviceDir(const char * devName)695 static int32_t CreatDeviceDir(const char *devName)
696 {
697     int32_t ret;
698     char tmp[MAX_PATHLEN];
699     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
700     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s", CONFIGFS_DIR, devName);
701     if (ret < 0) {
702         return HDF_ERR_IO;
703     }
704     if (!IsDirExist(tmp)) {
705         ret = mkdir(tmp, S_IREAD | S_IWRITE);
706         if (ret != 0) {
707             HDF_LOGE("%{public}s: mkdir failed", __func__);
708             return HDF_ERR_IO;
709         }
710     }
711     return 0;
712 }
713 
WriteConfPowerAttributes(const char * devName,struct UsbFnConfiguration * config,uint8_t confVal)714 static int32_t WriteConfPowerAttributes(const char *devName, struct UsbFnConfiguration *config, uint8_t confVal)
715 {
716     int32_t ret;
717     char configName[MAX_PATHLEN];
718     char tmp[MAX_PATHLEN], val[MAX_NAMELEN];
719     (void)memset_s(configName, MAX_PATHLEN, 0, MAX_PATHLEN);
720     ret = snprintf_s(configName, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/configs/b.%u", CONFIGFS_DIR, devName, confVal);
721     if (ret < 0) {
722         return HDF_ERR_IO;
723     }
724     if (!IsDirExist(configName)) {
725         ret = mkdir(configName, S_IREAD | S_IWRITE);
726         if (ret != 0) {
727             return HDF_ERR_IO;
728         }
729     }
730     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
731     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/MaxPower", configName);
732     if (ret < 0) {
733         return HDF_ERR_IO;
734     }
735     (void)memset_s(val, MAX_NAMELEN, 0, MAX_NAMELEN);
736     ret = snprintf_s(val, MAX_NAMELEN, MAX_NAMELEN - 1, "%u", config->maxPower);
737     if (ret < 0) {
738         return HDF_ERR_IO;
739     }
740     ret = UsbFnWriteFile(tmp, val);
741     if (ret < 0) {
742         return HDF_ERR_INVALID_PARAM;
743     }
744 
745     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
746     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/bmAttributes", configName);
747     if (ret < 0) {
748         return HDF_ERR_IO;
749     }
750     (void)memset_s(val, MAX_NAMELEN, 0, MAX_NAMELEN);
751     ret = snprintf_s(val, MAX_NAMELEN, MAX_NAMELEN - 1, "0x%x", config->attributes);
752     if (ret < 0) {
753         return HDF_ERR_IO;
754     }
755     ret = UsbFnWriteFile(tmp, val);
756     if (ret < 0) {
757         return HDF_ERR_INVALID_PARAM;
758     }
759     return 0;
760 }
761 
CreatKernelFunc(const char * devName,const struct UsbFnFunction * functions,uint8_t confVal)762 static int32_t CreatKernelFunc(const char *devName, const struct UsbFnFunction *functions, uint8_t confVal)
763 {
764     int32_t ret;
765     char configPath[MAX_PATHLEN];
766     char funcPath[MAX_PATHLEN];
767 
768     (void)memset_s(funcPath, MAX_PATHLEN, 0, MAX_PATHLEN);
769     ret = snprintf_s(
770         funcPath, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/functions/%s", CONFIGFS_DIR, devName, functions->funcName);
771     if (ret < 0) {
772         return HDF_ERR_IO;
773     }
774 
775     (void)memset_s(configPath, MAX_PATHLEN, 0, MAX_PATHLEN);
776     ret = snprintf_s(configPath, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/configs/b.%u/%s", CONFIGFS_DIR, devName, confVal,
777         functions->funcName);
778     if (ret < 0) {
779         return HDF_ERR_IO;
780     }
781     ret = UsbFnAdapterCreateFunc(configPath, funcPath);
782     if (ret != HDF_SUCCESS) {
783         HDF_LOGE("%{public}s: UsbFnAdapterCreateFunc failed", __func__);
784         return HDF_ERR_IO;
785     }
786     return ret;
787 }
788 
789 static void CleanConfigFs(const char *devName, const char *funcName);
CreatFunc(const char * devName,const struct UsbFnFunction * functions,uint8_t confVal)790 static int32_t CreatFunc(const char *devName, const struct UsbFnFunction *functions, uint8_t confVal)
791 {
792     int32_t fd, ret;
793     char interfaceName[MAX_NAMELEN];
794 
795     ret = CreatKernelFunc(devName, functions, confVal);
796     if (ret < 0) {
797         return HDF_ERR_IO;
798     }
799 
800     (void)memset_s(interfaceName, MAX_NAMELEN, 0, MAX_NAMELEN);
801     ret = snprintf_s(interfaceName, MAX_NAMELEN, MAX_NAMELEN - 1, "%s", functions->funcName);
802     if (ret < 0) {
803         return HDF_ERR_IO;
804     }
805     ret = UsbFnAdapterCreatInterface(interfaceName, strlen(interfaceName));
806     if (ret != HDF_SUCCESS) {
807         HDF_LOGE("%{public}s: UsbFnAdapterCreatInterface failed", __func__);
808         CleanConfigFs(devName, interfaceName);
809         return HDF_ERR_IO;
810     }
811 
812     fd = UsbFnAdapterOpenPipe(interfaceName, 0);
813     if (fd <= 0) {
814         HDF_LOGE("%{public}s: UsbFnAdapterOpenPipe failed", __func__);
815         CleanConfigFs(devName, interfaceName);
816         return HDF_ERR_IO;
817     }
818     ret = UsbFnAdapterCreatPipes(fd, functions);
819     if (ret != HDF_SUCCESS) {
820         HDF_LOGE("%{public}s: UsbFnAdapterCreatPipes failed", __func__);
821         return HDF_ERR_IO;
822     }
823     ret = UsbFnAdapterClosePipe(fd);
824     if (ret != HDF_SUCCESS) {
825         HDF_LOGE("%{public}s: UsbFnAdapterClosePipe failed", __func__);
826         return HDF_ERR_IO;
827     }
828     return 0;
829 }
830 
DelConfigDevice(const char * deviceName)831 static void DelConfigDevice(const char *deviceName)
832 {
833     int32_t ret;
834     char tmp[MAX_PATHLEN];
835     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
836     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/configs", CONFIGFS_DIR, deviceName);
837     if (ret < 0) {
838         return;
839     }
840     DeleteFile(tmp);
841 
842     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
843     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/functions", CONFIGFS_DIR, deviceName);
844     if (ret < 0) {
845         return;
846     }
847     DeleteFile(tmp);
848 
849     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
850     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/strings", CONFIGFS_DIR, deviceName);
851     if (ret < 0) {
852         return;
853     }
854     DeleteFile(tmp);
855 
856     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
857     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s", CONFIGFS_DIR, deviceName);
858     if (ret < 0) {
859         return;
860     }
861     rmdir(tmp);
862 }
863 
CleanConfigFs(const char * devName,const char * funcName)864 static void CleanConfigFs(const char *devName, const char *funcName)
865 {
866     int32_t ret;
867     char tmp[MAX_PATHLEN];
868 
869     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
870     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/configs/b.1/%s", CONFIGFS_DIR, devName, funcName);
871     if (ret < 0) {
872         return;
873     }
874     (void)remove(tmp);
875 
876     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
877     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/functions/%s", CONFIGFS_DIR, devName, funcName);
878     if (ret < 0) {
879         return;
880     }
881     (void)remove(tmp);
882 }
883 
CleanFunction(const char * devName,const char * funcName)884 static void CleanFunction(const char *devName, const char *funcName)
885 {
886     int32_t ret;
887     int32_t nameLength = (int32_t)strlen(funcName);
888     ret = UsbFnAdapterDelInterface(funcName, nameLength);
889     if (ret != HDF_SUCCESS) {
890         HDF_LOGE("%{public}s: UsbFnAdapterDelInterface failed", __func__);
891         return;
892     }
893     CleanConfigFs(devName, funcName);
894 }
895 
UsbFnAdapterCleanDevice(const char * devName)896 static void UsbFnAdapterCleanDevice(const char *devName)
897 {
898     int32_t ret;
899     char tmp[MAX_PATHLEN];
900     DIR *dir = NULL;
901     struct dirent *ptr = NULL;
902 
903     (void)memset_s(tmp, MAX_PATHLEN, 0, MAX_PATHLEN);
904     ret = snprintf_s(tmp, MAX_PATHLEN, MAX_PATHLEN - 1, "%s/%s/functions/", CONFIGFS_DIR, devName);
905     if (ret < 0) {
906         HDF_LOGE("%{public}s: snprintf_s failed", __func__);
907         return;
908     }
909     if ((dir = opendir(tmp)) == NULL) {
910         return;
911     }
912     while ((ptr = readdir(dir)) != NULL) {
913         if (strncmp(ptr->d_name, FUNCTION_GENERIC, strlen(FUNCTION_GENERIC))) {
914             continue;
915         }
916         CleanFunction(devName, ptr->d_name);
917     }
918     closedir(dir);
919 }
920 
UsbFnAdapterDelDevice(const char * deviceName,const char * udcName,struct UsbFnDeviceDesc * des)921 static int32_t UsbFnAdapterDelDevice(const char *deviceName, const char *udcName, struct UsbFnDeviceDesc *des)
922 {
923     uint32_t i, j;
924     int32_t ret;
925     if (deviceName == NULL) {
926         return HDF_ERR_INVALID_PARAM;
927     }
928     ret = UsbFnAdapterWriteUDC(deviceName, udcName, 0);
929     if (ret < 0) {
930         return ret;
931     }
932     for (i = 0; des->configs[i] != NULL; i++) {
933         for (j = 0; des->configs[i]->functions[j] != NULL; j++) {
934             if (des->configs[i]->functions[j]->enable == false) {
935                 continue;
936             }
937             if (strncmp(des->configs[i]->functions[j]->funcName, FUNCTION_GENERIC, strlen(FUNCTION_GENERIC)) != 0) {
938                 CleanConfigFs(deviceName, des->configs[i]->functions[j]->funcName);
939                 continue;
940             }
941             CleanFunction(deviceName, des->configs[i]->functions[j]->funcName);
942         }
943     }
944 
945     if (strcmp("g1", deviceName) != 0) {
946         DelConfigDevice(deviceName);
947     }
948     return 0;
949 }
950 
CreateFun(struct UsbFnFunction * function,const char * devName,uint8_t * confVal,int32_t * ret)951 static bool CreateFun(struct UsbFnFunction *function, const char *devName, uint8_t *confVal, int32_t *ret)
952 {
953     if (function == NULL || devName == NULL || confVal == NULL || ret == NULL) {
954         return false;
955     }
956 
957     if (function->enable == false) {
958         return false;
959     }
960     if (strncmp(function->funcName, FUNCTION_GENERIC, strlen(FUNCTION_GENERIC)) != 0) {
961         *ret = CreatKernelFunc(devName, function, *confVal);
962     } else {
963         *ret = CreatFunc(devName, function, *confVal);
964     }
965     return true;
966 }
967 
UsbFnAdapterCreateDevice(const char * udcName,const char * devName,struct UsbFnDeviceDesc * descriptor)968 static int32_t UsbFnAdapterCreateDevice(const char *udcName, const char *devName, struct UsbFnDeviceDesc *descriptor)
969 {
970     uint32_t i, j;
971     int32_t ret;
972     uint8_t confVal;
973 
974     UsbFnAdapterCleanDevice(devName);
975 
976     ret = CreatDeviceDir(devName);
977     if (ret != HDF_SUCCESS) {
978         return HDF_ERR_IO;
979     }
980 
981     ret = WriteDeviceDescriptor(devName, descriptor->deviceDesc, descriptor->deviceStrings);
982     if (ret != HDF_SUCCESS) {
983         HDF_LOGE("%{public}s: WriteDeviceDescriptor failed", __func__);
984         return HDF_ERR_IO;
985     }
986 
987     for (i = 0; descriptor->configs[i] != NULL; i++) {
988         confVal = descriptor->configs[i]->configurationValue;
989         ret = WriteConfPowerAttributes(devName, descriptor->configs[i], confVal);
990         if (ret != HDF_SUCCESS) {
991             HDF_LOGE("%{public}s: WriteConfPowerAttributes failed", __func__);
992             return HDF_ERR_IO;
993         }
994 
995         for (j = 0; descriptor->deviceStrings[j] != NULL; j++) {
996             ret = UsbFnWriteConfString(devName, confVal, descriptor->deviceStrings[j]->language,
997                 descriptor->deviceStrings[j]->strings[descriptor->configs[i]->iConfiguration].s);
998             if (ret < 0) {
999                 HDF_LOGE("%{public}s: UsbFnWriteConfString failed", __func__);
1000                 return HDF_ERR_INVALID_PARAM;
1001             }
1002         }
1003 
1004         for (j = 0; descriptor->configs[i]->functions[j] != NULL; j++) {
1005             if (!CreateFun(descriptor->configs[i]->functions[j], devName, &confVal, &ret)) {
1006                 continue;
1007             }
1008             if (ret < 0) {
1009                 HDF_LOGE("%{public}s: CreatFunc failed", __func__);
1010                 (void)UsbFnAdapterWriteUDC(devName, "none", 0);
1011                 (void)UsbFnAdapterWriteUDC(devName, udcName, 1);
1012                 return HDF_ERR_INVALID_PARAM;
1013             }
1014         }
1015     }
1016 
1017     return HDF_SUCCESS;
1018 }
1019 
UsbFnAdapterGetPipeInfo(int32_t ep,struct UsbFnPipeInfo * const pipeInfo)1020 static int32_t UsbFnAdapterGetPipeInfo(int32_t ep, struct UsbFnPipeInfo * const pipeInfo)
1021 {
1022     int32_t ret;
1023     if (ep <= 0 || pipeInfo == NULL) {
1024         return HDF_ERR_INVALID_PARAM;
1025     }
1026 
1027     struct UsbEndpointDescriptor desc;
1028     ret = ioctl(ep, FUNCTIONFS_ENDPOINT_DESC, &desc);
1029     if (ret != 0) {
1030         HDF_LOGE("%{public}s: FUNCTIONFS_ENDPOINT_DESC failed", __func__);
1031         return HDF_ERR_IO;
1032     }
1033 
1034     pipeInfo->type = desc.bmAttributes;
1035     pipeInfo->dir = USB_PIPE_DIRECTION_OUT;
1036     if (desc.bEndpointAddress & 0x80) {
1037         pipeInfo->dir = USB_PIPE_DIRECTION_IN;
1038     }
1039 
1040     pipeInfo->maxPacketSize = desc.wMaxPacketSize;
1041     pipeInfo->interval = desc.bInterval;
1042 
1043     return ret;
1044 }
1045 
UsbFnAdapterQueueInit(int32_t ep)1046 static int32_t UsbFnAdapterQueueInit(int32_t ep)
1047 {
1048     if (ep <= 0) {
1049         return HDF_ERR_INVALID_PARAM;
1050     }
1051     return ioctl(ep, FUNCTIONFS_ENDPOINT_QUEUE_INIT, 0);
1052 }
1053 
UsbFnAdapterQueueDel(int32_t ep)1054 static int32_t UsbFnAdapterQueueDel(int32_t ep)
1055 {
1056     if (ep <= 0) {
1057         return HDF_ERR_INVALID_PARAM;
1058     }
1059 
1060     return ioctl(ep, FUNCTIONFS_ENDPOINT_QUEUE_DEL, 0);
1061 }
1062 
UsbFnAdapterReleaseBuf(int32_t ep,const struct GenericMemory * mem)1063 static int32_t UsbFnAdapterReleaseBuf(int32_t ep, const struct GenericMemory *mem)
1064 {
1065     if (ep <= 0 || mem == NULL) {
1066         return HDF_ERR_INVALID_PARAM;
1067     }
1068 
1069     return ioctl(ep, FUNCTIONFS_ENDPOINT_RELEASE_BUF, mem);
1070 }
1071 
UsbFnAdapterPipeIo(int32_t ep,struct IoData * ioData)1072 static int32_t UsbFnAdapterPipeIo(int32_t ep, struct IoData *ioData)
1073 {
1074     int32_t ret;
1075     if (ep <= 0 || ioData == NULL) {
1076         HDF_LOGE("%{public}s: invalid param", __func__);
1077         return HDF_ERR_INVALID_PARAM;
1078     }
1079 
1080     if (ioData->read) {
1081         ret = ioctl(ep, FUNCTIONFS_ENDPOINT_READ, ioData);
1082     } else {
1083         ret = ioctl(ep, FUNCTIONFS_ENDPOINT_WRITE, ioData);
1084     }
1085 
1086     if (ret < 0) {
1087         HDF_LOGE("%{public}s: handle endpoint failed errno:%{public}d", __func__, errno);
1088     }
1089 
1090     return ret;
1091 }
1092 
UsbFnAdapterCancelIo(int32_t ep,const struct IoData * const ioData)1093 static int32_t UsbFnAdapterCancelIo(int32_t ep, const struct IoData * const ioData)
1094 {
1095     if (ep <= 0 || ioData == NULL) {
1096         return HDF_ERR_INVALID_PARAM;
1097     }
1098     return ioctl(ep, FUNCTIONFS_ENDPOINT_RW_CANCEL, ioData);
1099 }
1100 
UsbFnAdapterMapAddr(int32_t ep,uint32_t len)1101 static uint8_t *UsbFnAdapterMapAddr(int32_t ep, uint32_t len)
1102 {
1103     if (ep <= 0) {
1104         return NULL;
1105     }
1106 
1107     return mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, ep, 0);
1108 }
1109 
UsbFnAdapterUnmapAddr(uint8_t * const mapAddr,uint32_t len)1110 static int32_t UsbFnAdapterUnmapAddr(uint8_t * const mapAddr, uint32_t len)
1111 {
1112     if (mapAddr == NULL) {
1113         return HDF_ERR_INVALID_PARAM;
1114     }
1115 
1116     return munmap(mapAddr, len);
1117 }
1118 
Ep0Event(struct UsbFnEventAll * event,struct pollfd * pfds)1119 static void Ep0Event(struct UsbFnEventAll *event, struct pollfd *pfds)
1120 {
1121     int32_t ret;
1122     uint8_t i;
1123     for (i = 0; i < event->ep0Num; i++) {
1124         if ((uint32_t)pfds[i].revents & POLLIN) {
1125             ret = read(event->ep0[i], &event->ep0Event[i].ctrlEvent, sizeof(struct UsbFnCtrlEvent));
1126             if (!ret) {
1127                 HDF_LOGE("unable to read event from ep0");
1128             }
1129             event->ep0Event[i].type = USB_EP0_CTRL_EVENT;
1130         } else if ((uint32_t)pfds[i].revents & POLLOUT) {
1131             ret = ioctl(event->ep0[i], FUNCTIONFS_ENDPOINT_GET_EP0_EVENT, &event->ep0Event[i].reqEvent);
1132             if (!ret) {
1133                 HDF_LOGE("unable to read reqEvent from ep0");
1134             }
1135             event->ep0Event[i].type = USB_EP0_IO_COMPLETED;
1136         }
1137     }
1138 }
1139 
EpEvent(struct UsbFnEventAll * event,struct pollfd * pfds)1140 static void EpEvent(struct UsbFnEventAll *event, struct pollfd *pfds)
1141 {
1142     uint8_t i;
1143     for (i = 0; i < event->epNum; i++) {
1144         if ((pfds[i + event->ep0Num].revents & POLLIN)) {
1145             event->numEvent[i] = read(event->epx[i], event->reqEvent[i], MAX_REQUEST * sizeof(struct UsbFnReqEvent)) /
1146                 sizeof(struct UsbFnReqEvent);
1147             if (!event->numEvent[i]) {
1148                 HDF_LOGE("unable to read indexBuf from ep#");
1149             }
1150         }
1151     }
1152 }
1153 
UsbFnAdapterPollEvent(struct UsbFnEventAll * event,int32_t timeout)1154 static int32_t UsbFnAdapterPollEvent(struct UsbFnEventAll *event, int32_t timeout)
1155 {
1156     int32_t ret;
1157     uint8_t i;
1158     struct pollfd *pfds = NULL;
1159 
1160     if (event == NULL) {
1161         return HDF_ERR_INVALID_PARAM;
1162     }
1163     if (event->ep0Num + event->epNum == 0) {
1164         return HDF_ERR_INVALID_PARAM;
1165     }
1166     pfds = UsbFnMemCalloc((event->ep0Num + event->epNum) * sizeof(struct pollfd));
1167     if (pfds == NULL) {
1168         return HDF_ERR_MALLOC_FAIL;
1169     }
1170     for (i = 0; i < event->ep0Num; i++) {
1171         if (event->ep0[i] <= 0) {
1172             UsbFnMemFree(pfds);
1173             HDF_LOGE("%{public}s: ep[%d] = %d", __func__, i, event->ep0[i]);
1174             return HDF_ERR_INVALID_PARAM;
1175         }
1176         pfds[i].fd = event->ep0[i];
1177         pfds[i].events = POLLIN | POLLOUT;
1178     }
1179     for (i = 0; i < event->epNum; i++) {
1180         if (event->epx[i] <= 0) {
1181             UsbFnMemFree(pfds);
1182             HDF_LOGE("%{public}s: ep[%d] = %d", __func__, i, event->epx[i]);
1183             return HDF_ERR_INVALID_PARAM;
1184         }
1185         pfds[i + event->ep0Num].fd = event->epx[i];
1186         pfds[i + event->ep0Num].events = POLLIN;
1187     }
1188     ret = poll(pfds, event->ep0Num + event->epNum, timeout);
1189     if (ret == 0) {
1190         UsbFnMemFree(pfds);
1191         return HDF_ERR_TIMEOUT;
1192     } else if (ret < 0) {
1193         HDF_LOGE("%{public}s: interrupt", __func__);
1194         UsbFnMemFree(pfds);
1195         return HDF_ERR_IO;
1196     }
1197     Ep0Event(event, pfds);
1198     EpEvent(event, pfds);
1199     UsbFnMemFree(pfds);
1200     return 0;
1201 }
1202 
UsbFnAdapterRequestGetStatus(int32_t ep,const struct IoData * ioData)1203 static int32_t UsbFnAdapterRequestGetStatus(int32_t ep, const struct IoData *ioData)
1204 {
1205     if (ep <= 0 || ioData == NULL) {
1206         return HDF_ERR_INVALID_PARAM;
1207     }
1208     return ioctl(ep, FUNCTIONFS_ENDPOINT_GET_REQ_STATUS, ioData);
1209 }
1210 
UsbFnMemAlloc(size_t size)1211 void *UsbFnMemAlloc(size_t size)
1212 {
1213     return UsbFnMemCalloc(size);
1214 }
1215 
UsbFnMemCalloc(size_t size)1216 void *UsbFnMemCalloc(size_t size)
1217 {
1218     void *buf = OsalMemCalloc(size);
1219     if (buf == NULL) {
1220         HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1221         return NULL;
1222     }
1223 
1224     if (g_usbRamTestFlag) {
1225         if (g_usbRamTestHead == NULL) {
1226             g_usbRamTestHead = OsalMemCalloc(sizeof(struct RawUsbRamTestList));
1227             if (g_usbRamTestHead == NULL) {
1228                 HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1229                 OsalMemFree(buf);
1230                 return NULL;
1231             }
1232             OsalMutexInit(&g_usbRamTestHead->lock);
1233             DListHeadInit(&g_usbRamTestHead->list);
1234         }
1235         struct RawUsbRamTestList *testEntry = OsalMemCalloc(sizeof(struct RawUsbRamTestList));
1236         if (testEntry == NULL) {
1237             HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1238             OsalMemFree(buf);
1239             return NULL;
1240         }
1241         testEntry->address = (uintptr_t)buf;
1242         testEntry->size = size;
1243 
1244         struct RawUsbRamTestList *pos = NULL;
1245         uint32_t totalSize = 0;
1246         OsalMutexLock(&g_usbRamTestHead->lock);
1247         DListInsertTail(&testEntry->list, &g_usbRamTestHead->list);
1248         DLIST_FOR_EACH_ENTRY(pos, &g_usbRamTestHead->list, struct RawUsbRamTestList, list) {
1249             totalSize += pos->size;
1250         }
1251         OsalMutexUnlock(&g_usbRamTestHead->lock);
1252         HDF_LOGI("%{public}s:add size = %{public}zu, totalSize = %{public}u", __func__, size, totalSize);
1253     }
1254     return buf;
1255 }
1256 
UsbFnMemFree(const void * mem)1257 void UsbFnMemFree(const void *mem)
1258 {
1259     if (mem == NULL) {
1260         HDF_LOGE("%{public}s:%{public}d invalid param mem.", __func__, __LINE__);
1261         return;
1262     }
1263 
1264     if (g_usbRamTestFlag && g_usbRamTestHead != NULL) {
1265         struct RawUsbRamTestList *pos = NULL;
1266         struct RawUsbRamTestList *tmp = NULL;
1267         uint32_t totalSize = 0;
1268         uint32_t size = 0;
1269         OsalMutexLock(&g_usbRamTestHead->lock);
1270         DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_usbRamTestHead->list, struct RawUsbRamTestList, list) {
1271             if (pos->address == (uintptr_t)mem) {
1272                 size = pos->size;
1273                 DListRemove(&pos->list);
1274                 OsalMemFree(pos);
1275                 continue;
1276             }
1277             totalSize += pos->size;
1278         }
1279         OsalMutexUnlock(&g_usbRamTestHead->lock);
1280         HDF_LOGI("%{public}s:rm size = %{public}u, totalSize = %{public}u", __func__, size, totalSize);
1281     }
1282 
1283     if (mem != NULL) {
1284         OsalMemFree((void *)mem);
1285     }
1286 }
1287 
UsbFnAdpMemTestTrigger(bool enable)1288 int32_t UsbFnAdpMemTestTrigger(bool enable)
1289 {
1290     g_usbRamTestFlag = enable;
1291     return HDF_SUCCESS;
1292 }
1293 
1294 static struct UsbFnAdapterOps g_usbFnAdapter = {
1295     .createDevice = UsbFnAdapterCreateDevice,
1296     .delDevice = UsbFnAdapterDelDevice,
1297 
1298     .openPipe = UsbFnAdapterOpenPipe,
1299     .closePipe = UsbFnAdapterClosePipe,
1300     .getPipeInfo = UsbFnAdapterGetPipeInfo,
1301 
1302     .queueInit = UsbFnAdapterQueueInit,
1303     .queueDel = UsbFnAdapterQueueDel,
1304     .releaseBuf = UsbFnAdapterReleaseBuf,
1305     .pipeIo = UsbFnAdapterPipeIo,
1306     .cancelIo = UsbFnAdapterCancelIo,
1307     .getReqStatus = UsbFnAdapterRequestGetStatus,
1308     .mapAddr = UsbFnAdapterMapAddr,
1309     .unmapAddr = UsbFnAdapterUnmapAddr,
1310     .pollEvent = UsbFnAdapterPollEvent,
1311     .writeUDC = UsbFnAdapterWriteUDC,
1312     .writeProp = UsbFnWriteProp,
1313     .writeDesString = UsbFnWriteDesString,
1314 };
1315 
UsbFnAdapterGetOps(void)1316 struct UsbFnAdapterOps *UsbFnAdapterGetOps(void)
1317 {
1318     return &g_usbFnAdapter;
1319 }
1320