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