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