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