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