• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "usb_raw_api_library.h"
17 #include "linux_adapter.h"
18 
19 #define HDF_LOG_TAG USB_RAW_API_LIBRARY
20 
21 struct UsbSession *g_usbRawDefaultSession = NULL;
22 static struct RawUsbRamTestList *g_usbRamTestHead = NULL;
23 static bool g_usbRamTestFlag = false;
24 
SyncRequestCallback(const void * requestArg)25 static void SyncRequestCallback(const void *requestArg)
26 {
27     struct UsbHostRequest *request = (struct UsbHostRequest *)requestArg;
28     if (request == NULL || request->userData == NULL) {
29         HDF_LOGE("%{public}s:%d invalid param requestArg.", __func__, __LINE__);
30         return;
31     }
32 
33     int32_t *completed = request->userData;
34     *completed = 1;
35     OsalSemPost(&request->sem);
36 }
37 
ControlRequestGetData(const struct UsbHostRequest * request)38 static inline unsigned char *ControlRequestGetData(const struct UsbHostRequest *request)
39 {
40     return request->buffer + USB_RAW_CONTROL_SETUP_SIZE;
41 }
42 
HandleSyncRequestCompletion(const struct UsbHostRequest * request,struct UsbRequestData * requestData)43 static int32_t HandleSyncRequestCompletion(const struct UsbHostRequest *request, struct UsbRequestData *requestData)
44 {
45     int32_t ret;
46     uint32_t waitTime;
47 
48     if (request->timeout == USB_RAW_REQUEST_TIME_ZERO_MS) {
49         waitTime = HDF_WAIT_FOREVER;
50     } else {
51         waitTime = request->timeout;
52     }
53 
54     ret = OsalSemWait((struct OsalSem *)&request->sem, waitTime);
55     if (ret == HDF_ERR_TIMEOUT) {
56         RawCancelRequest(request);
57         RawHandleRequestCompletion((struct UsbHostRequest *)request, USB_REQUEST_TIMEOUT);
58     } else if (ret != HDF_SUCCESS) {
59         HDF_LOGE("%{public}s:%d OsalSemWait failed, ret = %d ", __func__, __LINE__, ret);
60         goto OUT;
61     }
62 
63     if (requestData->requested) {
64         *(requestData->requested) = request->actualLength;
65     }
66 
67     switch (request->status) {
68         case USB_REQUEST_COMPLETED:
69             ret = HDF_SUCCESS;
70             break;
71         case USB_REQUEST_TIMEOUT:
72             ret = HDF_ERR_TIMEOUT;
73             break;
74         case USB_REQUEST_NO_DEVICE:
75             ret = HDF_DEV_ERR_NO_DEVICE;
76             break;
77         case USB_REQUEST_STALL:
78         case USB_REQUEST_OVERFLOW:
79         case USB_REQUEST_ERROR:
80         case USB_REQUEST_CANCELLED:
81             ret = HDF_ERR_IO;
82             break;
83         default:
84             HDF_LOGW("%{public}s: unrecognised status code %d", __func__, request->status);
85             ret = HDF_FAILURE;
86             break;
87     }
88 
89 OUT:
90     OsalSemDestroy((struct OsalSem *)&request->sem);
91     return ret;
92 }
93 
HandleSyncRequest(struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbRequestData * requestData,unsigned char type)94 static int32_t HandleSyncRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
95     const struct UsbRequestData *requestData, unsigned char type)
96 {
97     int32_t ret;
98     static int32_t completed = 0;
99 
100     if (UsbEndpointDirOut(requestData->endPoint)) {
101         ret = memcpy_s(request->buffer, request->bufLen, requestData->data, requestData->length);
102         if (ret != EOK) {
103             HDF_LOGE("%{public}s:%d memcpy_s fail!", __func__, __LINE__);
104             return ret;
105         }
106     }
107 
108     request->devHandle = (struct UsbDeviceHandle *)devHandle;
109     request->endPoint = requestData->endPoint;
110     request->requestType = type;
111     request->timeout = requestData->timeout;
112     request->length = requestData->length;
113     request->userData = &completed;
114     request->callback = SyncRequestCallback;
115     request->userCallback = NULL;
116 
117     ret = OsalSemInit(&request->sem, 0);
118     if (ret != HDF_SUCCESS) {
119         HDF_LOGE("%{public}s:%d OsalSemInit failed, ret=%d ", __func__, __LINE__, ret);
120         return ret;
121     }
122 
123     ret = RawSubmitRequest(request);
124     if (ret < 0) {
125         OsalSemDestroy(&request->sem);
126         return ret;
127     }
128 
129     return HandleSyncRequestCompletion(request, (struct UsbRequestData *)requestData);
130 }
131 
GetInterfaceNumberDes(const struct UsbDescriptorHeader * header,uint8_t nIntf[],uint8_t nAlts[],int32_t * num)132 static void GetInterfaceNumberDes(
133     const struct UsbDescriptorHeader *header, uint8_t nIntf[], uint8_t nAlts[], int32_t *num)
134 {
135     uint8_t inum;
136     int32_t i;
137     struct UsbInterfaceDescriptor *desc = NULL;
138 
139     desc = (struct UsbInterfaceDescriptor *)header;
140     if (desc->bLength < USB_DDK_DT_INTERFACE_SIZE) {
141         HDF_LOGW("%{public}s: invalid interface descriptor length %d, skipping", __func__, desc->bLength);
142         return;
143     }
144 
145     inum = desc->bInterfaceNumber;
146     for (i = 0; i < *num; ++i) {
147         if (nIntf[i] == inum) {
148             break;
149         }
150     }
151     if (i < *num) {
152         if (nAlts[i] < USB_MAXALTSETTING) {
153             ++nAlts[i];
154         }
155     } else if (*num < USB_MAXINTERFACES) {
156         nIntf[*num] = inum;
157         nAlts[*num] = 1;
158         ++*num;
159     }
160 }
161 
GetInterfaceNumber(const uint8_t * buffer,size_t size,uint8_t nIntf[],uint8_t nAlts[])162 static int32_t GetInterfaceNumber(const uint8_t *buffer, size_t size, uint8_t nIntf[], uint8_t nAlts[])
163 {
164     struct UsbDescriptorHeader *header = NULL;
165     const uint8_t *buffer2;
166     size_t size2;
167     int32_t num = 0;
168 
169     for ((buffer2 = buffer, size2 = size); size2 > 0; (buffer2 += header->bLength, size2 -= header->bLength)) {
170         if (size2 < sizeof(struct UsbDescriptorHeader)) {
171             HDF_LOGW("%{public}s: descriptor has %zu excess bytes", __func__, size2);
172             break;
173         }
174         header = (struct UsbDescriptorHeader *)buffer2;
175         if ((header->bLength > size2) || (header->bLength < sizeof(struct UsbDescriptorHeader))) {
176             HDF_LOGW("%{public}s: invalid descriptor length %hhu, skipping remainder", __func__, header->bLength);
177             break;
178         }
179 
180         if (header->bDescriptorType == USB_DDK_DT_INTERFACE) {
181             GetInterfaceNumberDes(header, nIntf, nAlts, &num);
182         }
183     }
184 
185     return num;
186 }
187 
FindNextDescriptor(const uint8_t * buffer,size_t size)188 static int32_t FindNextDescriptor(const uint8_t *buffer, size_t size)
189 {
190     struct UsbDescriptorHeader *h = NULL;
191     const uint8_t *buffer0 = buffer;
192 
193     while (size > 0) {
194         h = (struct UsbDescriptorHeader *)buffer;
195         if (h->bDescriptorType == USB_DDK_DT_INTERFACE || h->bDescriptorType == USB_DDK_DT_ENDPOINT) {
196             break;
197         }
198         buffer += h->bLength;
199         size -= h->bLength;
200     }
201 
202     return buffer - buffer0;
203 }
GetConfigDescriptor(const struct UsbDevice * dev,uint8_t configIdx,uint8_t * buffer,size_t size)204 static int32_t GetConfigDescriptor(const struct UsbDevice *dev, uint8_t configIdx, uint8_t *buffer, size_t size)
205 {
206     int32_t ret;
207     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
208 
209     if (dev == NULL) {
210         HDF_LOGE("%{public}s: invalid param", __func__);
211         return HDF_ERR_INVALID_PARAM;
212     }
213 
214     if (!osAdapterOps->getConfigDescriptor) {
215         HDF_LOGE("%{public}s: getConfigDescriptor is null", __func__);
216         return HDF_ERR_NOT_SUPPORT;
217     }
218 
219     ret = osAdapterOps->getConfigDescriptor(dev, configIdx, buffer, size);
220     if (ret < 0) {
221         HDF_LOGE("%{public}s: getConfigDescriptor error = %d", __func__, ret);
222         return ret;
223     }
224 
225     if (ret < USB_DDK_DT_CONFIG_SIZE) {
226         HDF_LOGE("%{public}s: short config descriptor read error = %d", __func__, ret);
227         return HDF_ERR_IO;
228     } else if (ret != (int)size) {
229         HDF_LOGE("%{public}s: short config descriptor read size = %zu, ret = %d", __func__, size, ret);
230     }
231 
232     return ret;
233 }
234 
GetRawConfigDescriptor(const UsbRawHandle * rawHandle,uint8_t configIndex,uint8_t * configDesc,uint32_t configDescLen)235 int32_t GetRawConfigDescriptor(
236     const UsbRawHandle *rawHandle, uint8_t configIndex, uint8_t *configDesc, uint32_t configDescLen)
237 {
238     if (rawHandle == NULL || configDesc == NULL) {
239         HDF_LOGE("%{public}s: invalid param", __func__);
240         return HDF_ERR_INVALID_PARAM;
241     }
242 
243     struct UsbDeviceHandle *devHandle = (struct UsbDeviceHandle *)rawHandle;
244     return GetConfigDescriptor(devHandle->dev, configIndex, configDesc, configDescLen);
245 }
246 
ParseDescriptor(const void * source,enum UsbRawDescriptorType bDescriptorType,void * dest)247 static void ParseDescriptor(const void *source, enum UsbRawDescriptorType bDescriptorType, void *dest)
248 {
249     int32_t ret;
250 
251     if (source == NULL) {
252         HDF_LOGE("%{public}s: invalid param", __func__);
253         return;
254     }
255 
256     switch (bDescriptorType) {
257         case USB_RAW_CONFIG_DESCRIPTOR_TYPE: {
258             struct UsbConfigDescriptor *desc = (struct UsbConfigDescriptor *)dest;
259             ret = memcpy_s(dest, sizeof(struct UsbConfigDescriptor), source, USB_DDK_DT_CONFIG_SIZE);
260             if (ret != EOK) {
261                 HDF_LOGE("%{public}s:%d memcpy_s failed, ret = %d", __func__, __LINE__, ret);
262                 break;
263             }
264             desc->wTotalLength = LE16_TO_CPU(desc->wTotalLength);
265             break;
266         }
267         case USB_RAW_INTERFACE_DESCRIPTOR_TYPE: {
268             ret = memcpy_s(dest, sizeof(struct UsbInterfaceDescriptor), source, USB_DDK_DT_INTERFACE_SIZE);
269             if (ret != EOK) {
270                 HDF_LOGE("%{public}s:%d memcpy_s failed, ret = %d", __func__, __LINE__, ret);
271             }
272             break;
273         }
274         case USB_RAW_ENDPOINT_DESCRIPTOR_TYPE: {
275             struct UsbEndpointDescriptor *desc = (struct UsbEndpointDescriptor *)dest;
276             ret = memcpy_s(dest, sizeof(struct UsbEndpointDescriptor), source, USB_DDK_DT_ENDPOINT_SIZE);
277             if (ret != EOK) {
278                 HDF_LOGE("%{public}s:%d memcpy_s failed, ret = %d", __func__, __LINE__, ret);
279                 break;
280             }
281             desc->wMaxPacketSize = LE16_TO_CPU(desc->wMaxPacketSize);
282             break;
283         }
284         case USB_RAW_AUDIO_ENDPOINT_DESCRIPTOR_TYPE: {
285             struct UsbEndpointDescriptor *desc = (struct UsbEndpointDescriptor *)dest;
286             ret = memcpy_s(dest, sizeof(struct UsbEndpointDescriptor), source, USB_DDK_DT_ENDPOINT_AUDIO_SIZE);
287             if (ret != EOK) {
288                 HDF_LOGE("%{public}s:%d memcpy_s failed, ret = %d", __func__, __LINE__, ret);
289                 break;
290             }
291             desc->wMaxPacketSize = LE16_TO_CPU(desc->wMaxPacketSize);
292             break;
293         }
294         default:
295             HDF_LOGE("%{public}s: error bDescriptorType = %d", __func__, bDescriptorType);
296             break;
297     }
298 }
299 
ClearEndpoint(struct UsbRawEndpointDescriptor * endPoint)300 static void ClearEndpoint(struct UsbRawEndpointDescriptor *endPoint)
301 {
302     if ((endPoint != NULL) && (endPoint->extra != NULL)) {
303         RawUsbMemFree((void *)endPoint->extra);
304         endPoint->extra = NULL;
305     }
306 }
307 
ParseEndpoint(struct UsbRawEndpointDescriptor * endPoint,const uint8_t * buffer,int32_t size)308 static int32_t ParseEndpoint(struct UsbRawEndpointDescriptor *endPoint, const uint8_t *buffer, int32_t size)
309 {
310     const uint8_t *buffer0 = buffer;
311     const struct UsbDescriptorHeader *header = NULL;
312     void *extra = NULL;
313     int32_t len;
314     int32_t ret;
315 
316     if (size < DESC_HEADER_LENGTH) {
317         HDF_LOGE("%{public}s:size = %d is short endPoint descriptor ", __func__, size);
318         return HDF_ERR_IO;
319     }
320 
321     header = (const struct UsbDescriptorHeader *)buffer;
322     if ((header->bDescriptorType != USB_DDK_DT_ENDPOINT) || (header->bLength > size)) {
323         HDF_LOGE("%{public}s:%d unexpected descriptor, type = 0x%x, length = %hhu", __func__, __LINE__,
324             header->bDescriptorType, header->bLength);
325         return buffer - buffer0;
326     } else if (header->bLength < USB_DDK_DT_ENDPOINT_SIZE) {
327         HDF_LOGE("%{public}s:%d invalid endpoint length = %hhu", __func__, __LINE__, header->bLength);
328         return HDF_ERR_IO;
329     }
330 
331     if (header->bLength >= USB_DDK_DT_ENDPOINT_AUDIO_SIZE) {
332         ParseDescriptor(buffer, USB_RAW_AUDIO_ENDPOINT_DESCRIPTOR_TYPE, endPoint);
333     } else {
334         ParseDescriptor(buffer, USB_RAW_ENDPOINT_DESCRIPTOR_TYPE, endPoint);
335     }
336 
337     buffer += header->bLength;
338     size -= header->bLength;
339 
340     len = FindNextDescriptor(buffer, size);
341     if (!len) {
342         return buffer - buffer0;
343     }
344 
345     extra = RawUsbMemAlloc((size_t)len);
346     if (extra == NULL) {
347         return HDF_ERR_MALLOC_FAIL;
348     }
349 
350     ret = memcpy_s(extra, len + endPoint->extraLength, buffer, len);
351     if (ret != EOK) {
352         HDF_LOGE("%{public}s:%d memcpy_s failed!", __func__, __LINE__);
353         return HDF_ERR_IO;
354     }
355     endPoint->extra = extra;
356     endPoint->extraLength = len;
357 
358     return buffer + len - buffer0;
359 }
360 
ClearInterface(const struct UsbRawInterface * usbInterface)361 static void ClearInterface(const struct UsbRawInterface *usbInterface)
362 {
363     struct UsbRawInterfaceDescriptor *infPtr = NULL;
364     uint8_t i;
365     uint8_t j;
366 
367     if (usbInterface == NULL) {
368         HDF_LOGE("%{public}s:%d usbInterface is null", __func__, __LINE__);
369         return;
370     }
371 
372     if (usbInterface->numAltsetting > USB_MAXALTSETTING) {
373         HDF_LOGE("%{public}s:%d numAltsetting = %hhu is error", __func__, __LINE__, usbInterface->numAltsetting);
374         return;
375     }
376 
377     for (i = 0; i < usbInterface->numAltsetting; i++) {
378         infPtr = (struct UsbRawInterfaceDescriptor *)(usbInterface->altsetting + i);
379         if (infPtr == NULL) {
380             HDF_LOGE("%{public}s:%d altsetting is null", __func__, __LINE__);
381             continue;
382         }
383 
384         if (infPtr->extra != NULL) {
385             RawUsbMemFree((void *)infPtr->extra);
386             infPtr->extra = NULL;
387         }
388 
389         if (infPtr->endPoint != NULL) {
390             for (j = 0; j < infPtr->interfaceDescriptor.bNumEndpoints; j++) {
391                 ClearEndpoint((struct UsbRawEndpointDescriptor *)infPtr->endPoint + j);
392             }
393 
394             RawUsbMemFree((void *)infPtr->endPoint);
395             infPtr->endPoint = NULL;
396         }
397     }
398 
399     RawUsbMemFree((void *)usbInterface);
400 }
401 
RawParseDescriptor(int32_t size,const uint8_t * buffer,enum UsbRawDescriptorType bDescriptorType,const struct UsbRawInterfaceDescriptor * ifp)402 static int32_t RawParseDescriptor(int32_t size, const uint8_t *buffer, enum UsbRawDescriptorType bDescriptorType,
403     const struct UsbRawInterfaceDescriptor *ifp)
404 {
405     int32_t ret = HDF_SUCCESS;
406 
407     ParseDescriptor(buffer, bDescriptorType, (void *)ifp);
408     if ((ifp->interfaceDescriptor.bDescriptorType != USB_DDK_DT_INTERFACE) ||
409         (ifp->interfaceDescriptor.bLength > size)) {
410         HDF_LOGE("%{public}s: unexpected descriptor: type = 0x%x, size = %d", __func__,
411             ifp->interfaceDescriptor.bDescriptorType, size);
412         ret = HDF_FAILURE;
413     } else if ((ifp->interfaceDescriptor.bLength < USB_DDK_DT_INTERFACE_SIZE) ||
414         (ifp->interfaceDescriptor.bNumEndpoints > USB_MAXENDPOINTS)) {
415         HDF_LOGE("%{public}s: invalid descriptor: length = %u, numEndpoints = %u ", __func__,
416             ifp->interfaceDescriptor.bLength, ifp->interfaceDescriptor.bNumEndpoints);
417         ret = HDF_ERR_IO;
418     }
419 
420     return ret;
421 }
422 
ParseInterfaceCopy(struct UsbRawInterfaceDescriptor * const ifp,int32_t len,const uint8_t * buffer)423 static int32_t ParseInterfaceCopy(struct UsbRawInterfaceDescriptor * const ifp, int32_t len, const uint8_t *buffer)
424 {
425     int32_t ret;
426 
427     ifp->extra = RawUsbMemAlloc((size_t)len);
428     if (!ifp->extra) {
429         ret = HDF_ERR_MALLOC_FAIL;
430         return ret;
431     }
432 
433     ret = memcpy_s((void *)ifp->extra, len + ifp->extraLength, buffer, len);
434     if (ret != EOK) {
435         HDF_LOGE("%{public}s:%d memcpy_s failed, ret = %d", __func__, __LINE__, ret);
436         return ret;
437     }
438     ifp->extraLength = len;
439     return ret;
440 }
441 
ParseInterfaceEndpoint(struct UsbRawInterfaceDescriptor * ifp,const uint8_t ** buffer,int32_t * size)442 static int32_t ParseInterfaceEndpoint(struct UsbRawInterfaceDescriptor *ifp, const uint8_t **buffer, int32_t *size)
443 {
444     struct UsbRawEndpointDescriptor *endPoint = NULL;
445     int32_t ret = HDF_SUCCESS;
446 
447     if (ifp->interfaceDescriptor.bNumEndpoints > 0) {
448         endPoint = RawUsbMemCalloc(ifp->interfaceDescriptor.bNumEndpoints * sizeof(*endPoint));
449         if (endPoint == NULL) {
450             ret = HDF_ERR_MALLOC_FAIL;
451             return ret;
452         }
453 
454         ifp->endPoint = endPoint;
455         for (uint8_t i = 0; i < ifp->interfaceDescriptor.bNumEndpoints; i++) {
456             ret = ParseEndpoint(endPoint + i, *buffer, *size);
457             if (ret == 0) {
458                 ifp->interfaceDescriptor.bNumEndpoints = i;
459                 break;
460             } else if (ret < 0) {
461                 return ret;
462             }
463 
464             *buffer += ret;
465             *size -= ret;
466         }
467     }
468     return ret;
469 }
470 
ParseInterface(struct UsbRawInterface * usbInterface,const uint8_t * buffer,int32_t size)471 static int32_t ParseInterface(struct UsbRawInterface *usbInterface, const uint8_t *buffer, int32_t size)
472 {
473     const uint8_t *buffer0 = buffer;
474     int32_t interfaceNumber = -1; // initial value of interfaceNumber is -1
475     const struct UsbInterfaceDescriptor *ifDesc = NULL;
476     struct UsbRawInterfaceDescriptor *ifp = NULL;
477 
478     if (usbInterface == NULL || usbInterface->numAltsetting > USB_MAXALTSETTING) {
479         HDF_LOGE("%{public}s: usbInterface is null or numAltsetting is invalid", __func__);
480         return HDF_DEV_ERR_NORANGE;
481     }
482 
483     while (size >= USB_DDK_DT_INTERFACE_SIZE) {
484         ifp = (struct UsbRawInterfaceDescriptor *)(usbInterface->altsetting + usbInterface->numAltsetting);
485         int32_t ret = RawParseDescriptor(size, buffer, USB_RAW_INTERFACE_DESCRIPTOR_TYPE, ifp);
486         if (ret == HDF_FAILURE) {
487             return buffer - buffer0;
488         } else if (ret == HDF_ERR_IO) {
489             HDF_LOGE("%{public}s: RawParseDescriptor failed", __func__);
490             return ret;
491         }
492 
493         usbInterface->numAltsetting++;
494         ifp->extra = NULL;
495         ifp->extraLength = 0;
496         ifp->endPoint = NULL;
497         if (interfaceNumber == -1) {
498             interfaceNumber = ifp->interfaceDescriptor.bInterfaceNumber;
499         }
500 
501         buffer += ifp->interfaceDescriptor.bLength;
502         size -= (int)ifp->interfaceDescriptor.bLength;
503         int32_t len = FindNextDescriptor(buffer, size);
504         if (len != 0) {
505             if (ParseInterfaceCopy(ifp, len, buffer) != EOK) {
506                 HDF_LOGE("%{public}s: ParseInterfaceCopy failed", __func__);
507                 return HDF_FAILURE;
508             }
509             buffer += len;
510             size -= len;
511         }
512 
513         ret = ParseInterfaceEndpoint(ifp, &buffer, &size);
514         if (ret < HDF_SUCCESS) {
515             HDF_LOGE("%{public}s: ParseInterfaceEndpoint, ret less than zero", __func__);
516             return ret;
517         }
518 
519         ifDesc = (const struct UsbInterfaceDescriptor *)buffer;
520         bool tempFlag = (size < USB_DDK_DT_INTERFACE_SIZE) || (ifDesc->bDescriptorType != USB_DDK_DT_INTERFACE) ||
521             (ifDesc->bInterfaceNumber != interfaceNumber);
522         if (tempFlag == true) {
523             return buffer - buffer0;
524         }
525     }
526 
527     return buffer - buffer0;
528 }
529 
ParseConfigurationDes(struct UsbRawConfigDescriptor * config,const uint8_t * buffer,int32_t size,struct UsbRawInterface * usbInterface,const uint8_t * nIntf)530 static int32_t ParseConfigurationDes(struct UsbRawConfigDescriptor *config, const uint8_t *buffer, int32_t size,
531     struct UsbRawInterface *usbInterface, const uint8_t *nIntf)
532 {
533     int32_t ret, len;
534     uint8_t i;
535 
536     len = FindNextDescriptor(buffer, size);
537     if (len != 0) {
538         config->extra = RawUsbMemAlloc(len);
539         if (config->extra == NULL) {
540             ret = HDF_ERR_MALLOC_FAIL;
541             RawClearConfiguration(config);
542             return ret;
543         }
544 
545         ret = memcpy_s((void *)config->extra, len + config->extraLength, buffer, len);
546         if (ret != EOK) {
547             HDF_LOGE("%{public}s:%d memcpy_s failed! ret = %d", __func__, __LINE__, ret);
548             RawClearConfiguration(config);
549             return ret;
550         }
551         config->extraLength = len;
552         buffer += len;
553         size -= len;
554     }
555 
556     while (size > 0) {
557         struct UsbInterfaceDescriptor *ifDesc = (struct UsbInterfaceDescriptor *)buffer;
558         if (config->configDescriptor.bNumInterfaces >= USB_MAXINTERFACES) {
559             HDF_LOGE("%{public}d: bNumInterfaces overlong.", config->configDescriptor.bNumInterfaces);
560             return HDF_FAILURE;
561         }
562         for (i = 0; i < config->configDescriptor.bNumInterfaces; ++i) {
563             if (nIntf[i] == ifDesc->bInterfaceNumber) {
564                 usbInterface = (struct UsbRawInterface *)config->interface[i];
565                 break;
566             }
567         }
568         ret = ParseInterface(usbInterface, buffer, size);
569         if (ret < 0) {
570             RawClearConfiguration(config);
571             return ret;
572         }
573         buffer += ret;
574         size -= ret;
575     }
576 
577     return size;
578 }
579 
ParseConfiguration(struct UsbRawConfigDescriptor * config,const uint8_t * buffer,int32_t size)580 static int32_t ParseConfiguration(struct UsbRawConfigDescriptor *config, const uint8_t *buffer, int32_t size)
581 {
582     struct UsbRawInterface *usbInterface = NULL;
583     uint8_t nIntf[USB_MAXINTERFACES] = {0};
584     uint8_t nAlts[USB_MAXINTERFACES] = {0};
585     int32_t intfNum;
586 
587     if (size < USB_DDK_DT_CONFIG_SIZE || config == NULL) {
588         HDF_LOGE("%{public}s:%d size = %d is short, or config is null!", __func__, __LINE__, size);
589         return HDF_ERR_IO;
590     }
591 
592     ParseDescriptor(buffer, USB_RAW_CONFIG_DESCRIPTOR_TYPE, config);
593     if ((config->configDescriptor.bDescriptorType != USB_DDK_DT_CONFIG) ||
594         (config->configDescriptor.bLength < USB_DDK_DT_CONFIG_SIZE) ||
595         (config->configDescriptor.bLength > (uint8_t)size) ||
596         (config->configDescriptor.bNumInterfaces > USB_MAXINTERFACES)) {
597         HDF_LOGE("%{public}s:%d invalid descriptor: type = 0x%x, length = %u", __func__, __LINE__,
598             config->configDescriptor.bDescriptorType, config->configDescriptor.bLength);
599         return HDF_ERR_IO;
600     }
601 
602     intfNum = GetInterfaceNumber(buffer, size, nIntf, nAlts);
603     config->configDescriptor.bNumInterfaces = (uint8_t)intfNum;
604 
605     for (int32_t i = 0; i < intfNum; ++i) {
606         uint8_t j = nAlts[i];
607         if (j > USB_MAXALTSETTING) {
608             HDF_LOGW("%{public}s: too many alternate settings: %hhu", __func__, j);
609             nAlts[i] = USB_MAXALTSETTING;
610             j = USB_MAXALTSETTING;
611         }
612         usbInterface = RawUsbMemCalloc(sizeof(struct UsbRawInterface) + sizeof(struct UsbRawInterfaceDescriptor) * j);
613         config->interface[i] = usbInterface;
614         if (usbInterface == NULL) {
615             return HDF_ERR_MALLOC_FAIL;
616         }
617     }
618 
619     buffer += config->configDescriptor.bLength;
620     size -= (int32_t)config->configDescriptor.bLength;
621 
622     return ParseConfigurationDes(config, buffer, size, usbInterface, nIntf);
623 }
624 
DescToConfig(const uint8_t * buf,int32_t size,struct UsbRawConfigDescriptor ** const config)625 static int32_t DescToConfig(const uint8_t *buf, int32_t size, struct UsbRawConfigDescriptor ** const config)
626 {
627     struct UsbRawConfigDescriptor *tmpConfig = RawUsbMemCalloc(sizeof(struct UsbRawConfigDescriptor));
628     int32_t ret;
629 
630     if (tmpConfig == NULL) {
631         HDF_LOGE("%{public}s: RawUsbMemCalloc failed", __func__);
632         return HDF_ERR_MALLOC_FAIL;
633     }
634 
635     ret = ParseConfiguration(tmpConfig, buf, size);
636     if (ret < 0) {
637         HDF_LOGE("%{public}s: ParseConfiguration failed with error = %d", __func__, ret);
638         RawUsbMemFree(tmpConfig);
639         tmpConfig = NULL;
640         return ret;
641     } else if (ret > 0) {
642         HDF_LOGW("%{public}s: still %d bytes of descriptor data left", __func__, ret);
643     }
644 
645     *config = tmpConfig;
646 
647     return ret;
648 }
649 
ControlRequestCompletion(const struct UsbHostRequest * request,struct UsbControlRequestData * requestData)650 static int32_t ControlRequestCompletion(const struct UsbHostRequest *request, struct UsbControlRequestData *requestData)
651 {
652     int32_t ret;
653     uint32_t waitTime;
654 
655     if (request->timeout == USB_RAW_REQUEST_TIME_ZERO_MS) {
656         waitTime = HDF_WAIT_FOREVER;
657     } else {
658         waitTime = request->timeout;
659     }
660 
661     ret = OsalSemWait((struct OsalSem *)&request->sem, waitTime);
662     if (ret == HDF_ERR_TIMEOUT) {
663         RawCancelRequest(request);
664         RawHandleRequestCompletion((struct UsbHostRequest *)request, USB_REQUEST_TIMEOUT);
665     } else if (ret != HDF_SUCCESS) {
666         HDF_LOGE("%{public}s:%d OsalSemWait failed, ret=%d ", __func__, __LINE__, ret);
667         goto OUT;
668     }
669 
670     if ((requestData->requestType & USB_DDK_ENDPOINT_DIR_MASK) == USB_PIPE_DIRECTION_IN) {
671         ret = memcpy_s(requestData->data, request->actualLength + requestData->length, ControlRequestGetData(request),
672             request->actualLength);
673         if (ret != EOK) {
674             HDF_LOGE("%{public}s:%d memcpy_s failed! ret = %d", __func__, __LINE__, ret);
675             goto OUT;
676         }
677     }
678 
679     switch (request->status) {
680         case USB_REQUEST_COMPLETED:
681             ret = request->actualLength;
682             break;
683         case USB_REQUEST_TIMEOUT:
684             ret = HDF_ERR_TIMEOUT;
685             break;
686         case USB_REQUEST_NO_DEVICE:
687             ret = HDF_DEV_ERR_NO_DEVICE;
688             break;
689         case USB_REQUEST_STALL:
690         case USB_REQUEST_OVERFLOW:
691         case USB_REQUEST_ERROR:
692         case USB_REQUEST_CANCELLED:
693             ret = HDF_ERR_IO;
694             break;
695         default:
696             HDF_LOGW("%{public}s: status = %d is unrecognised", __func__, request->status);
697             ret = HDF_FAILURE;
698     }
699 
700 OUT:
701     OsalSemDestroy((struct OsalSem *)&request->sem);
702     return ret;
703 }
704 
RawGetSession(const struct UsbSession * session)705 struct UsbSession *RawGetSession(const struct UsbSession *session)
706 {
707     return (struct UsbSession *)(session ? session : g_usbRawDefaultSession);
708 }
709 
RawInit(struct UsbSession ** session)710 int32_t RawInit(struct UsbSession **session)
711 {
712     int32_t ret;
713     struct UsbSession *tempSession = NULL;
714     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
715 
716     if (session == NULL && g_usbRawDefaultSession != NULL) {
717         AdapterAtomicInc(&g_usbRawDefaultSession->refCount);
718         return HDF_SUCCESS;
719     }
720 
721     tempSession = (struct UsbSession *)RawUsbMemCalloc(sizeof(*tempSession));
722     if (tempSession == NULL) {
723         return HDF_ERR_MALLOC_FAIL;
724     }
725     OsalAtomicSet(&tempSession->refCount, 1);
726     HdfSListInit(&tempSession->usbDevs);
727     DListHeadInit(&tempSession->ifacePoolList);
728     OsalMutexInit(&tempSession->lock);
729     if (session == NULL && g_usbRawDefaultSession == NULL) {
730         g_usbRawDefaultSession = tempSession;
731         HDF_LOGI("%{public}s: created default context", __func__);
732     }
733 
734     if (osAdapterOps->init) {
735         ret = osAdapterOps->init(tempSession);
736         if (ret < 0) {
737             HDF_LOGE("%{public}s: init error, return %d", __func__, ret);
738             goto ERR_FREE_SESSION;
739         }
740     } else {
741         ret = HDF_ERR_NOT_SUPPORT;
742         goto ERR_FREE_SESSION;
743     }
744 
745     if (session != NULL) {
746         *session = tempSession;
747     }
748 
749     return HDF_SUCCESS;
750 
751 ERR_FREE_SESSION:
752     if (tempSession == g_usbRawDefaultSession) {
753         g_usbRawDefaultSession = NULL;
754     }
755 
756     RawUsbMemFree(tempSession);
757     return ret;
758 }
759 
RawExit(const struct UsbSession * session)760 int32_t RawExit(const struct UsbSession *session)
761 {
762     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
763     struct UsbSession *realSession = RawGetSession(session);
764     if (realSession == NULL || AdapterAtomicDec(&realSession->refCount) > 0) {
765         return HDF_SUCCESS;
766     }
767 
768     if (osAdapterOps->exit) {
769         osAdapterOps->exit(realSession);
770     }
771     if (realSession == g_usbRawDefaultSession) {
772         g_usbRawDefaultSession = NULL;
773     }
774 
775     OsalMutexDestroy(&realSession->lock);
776     RawUsbMemFree(realSession);
777 
778     return HDF_SUCCESS;
779 }
780 
RawOpenDevice(const struct UsbSession * session,uint8_t busNum,uint8_t usbAddr)781 struct UsbDeviceHandle *RawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr)
782 {
783     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
784     struct UsbSession *realSession = NULL;
785 
786     if (osAdapterOps->openDevice == NULL) {
787         HDF_LOGE("%{public}s: openDevice is null", __func__);
788         return NULL;
789     }
790 
791     realSession = RawGetSession(session);
792     if (realSession == NULL) {
793         return NULL;
794     }
795 
796     return osAdapterOps->openDevice(realSession, busNum, usbAddr);
797 }
798 
RawCloseDevice(const struct UsbDeviceHandle * devHandle)799 int32_t RawCloseDevice(const struct UsbDeviceHandle *devHandle)
800 {
801     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
802 
803     if (devHandle == NULL) {
804         HDF_LOGE("%{public}s devHandle is null", __func__);
805         return HDF_ERR_INVALID_PARAM;
806     }
807 
808     if (osAdapterOps->closeDevice) {
809         osAdapterOps->closeDevice((struct UsbDeviceHandle *)devHandle);
810     } else {
811         return HDF_ERR_NOT_SUPPORT;
812     }
813 
814     return HDF_SUCCESS;
815 }
816 
RawClaimInterface(struct UsbDeviceHandle * devHandle,int32_t interfaceNumber)817 int32_t RawClaimInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber)
818 {
819     int32_t ret;
820     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
821 
822     if (devHandle == NULL || interfaceNumber < 0 || interfaceNumber >= USB_MAXINTERFACES) {
823         HDF_LOGE("%{public}s:%d HDF_ERR_INVALID_PARAM", __func__, __LINE__);
824         return HDF_ERR_INVALID_PARAM;
825     }
826 
827     OsalMutexLock(&devHandle->lock);
828     if ((devHandle->claimedInterfaces) & (1U << (uint32_t)interfaceNumber)) {
829         ret = HDF_SUCCESS;
830         goto OUT;
831     }
832 
833     if (!osAdapterOps->claimInterface) {
834         ret = HDF_ERR_NOT_SUPPORT;
835         goto OUT;
836     }
837 
838     ret = osAdapterOps->claimInterface(devHandle, (unsigned int)interfaceNumber);
839     if (ret == HDF_SUCCESS) {
840         devHandle->claimedInterfaces |= 1U << (uint32_t)interfaceNumber;
841     }
842 
843 OUT:
844     OsalMutexUnlock(&devHandle->lock);
845 
846     return ret;
847 }
848 
RawClaimInterfaceForce(struct UsbDeviceHandle * devHandle,uint32_t interfaceNumber)849 int32_t RawClaimInterfaceForce(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber)
850 {
851     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
852 
853     if (devHandle == NULL || interfaceNumber >= USB_MAXINTERFACES || osAdapterOps->claimInterface == NULL) {
854         HDF_LOGE("%{public}s:%d HDF_ERR_INVALID_PARAM", __func__, __LINE__);
855         return HDF_ERR_INVALID_PARAM;
856     }
857     if (((devHandle->claimedInterfaces) & (1U << interfaceNumber)) != 0) {
858         return HDF_SUCCESS;
859     }
860 
861     OsalMutexLock(&devHandle->lock);
862     int32_t ret = osAdapterOps->detachKernelDriverAndClaim(devHandle, interfaceNumber);
863     if (ret == HDF_SUCCESS) {
864         devHandle->claimedInterfaces |= 1U << interfaceNumber;
865     }
866     OsalMutexUnlock(&devHandle->lock);
867     return ret;
868 }
869 
AllocRequest(const struct UsbDeviceHandle * devHandle,int32_t isoPackets,size_t length)870 struct UsbHostRequest *AllocRequest(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, size_t length)
871 {
872     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
873 
874     if (osAdapterOps->allocRequest == NULL) {
875         return NULL;
876     }
877 
878     return osAdapterOps->allocRequest(devHandle, isoPackets, length);
879 }
880 
AllocRequestByMmap(const struct UsbDeviceHandle * devHandle,int32_t isoPackets,size_t length)881 struct UsbHostRequest *AllocRequestByMmap(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, size_t length)
882 {
883     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
884 
885     if (osAdapterOps->allocRequestByMmap == NULL) {
886         return NULL;
887     }
888 
889     return osAdapterOps->allocRequestByMmap(devHandle, isoPackets, length);
890 }
891 
FreeRequest(const struct UsbHostRequest * request)892 int32_t FreeRequest(const struct UsbHostRequest *request)
893 {
894     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
895     if (osAdapterOps->freeRequest == NULL) {
896         return HDF_ERR_NOT_SUPPORT;
897     }
898 
899     return osAdapterOps->freeRequest((struct UsbHostRequest *)request);
900 }
901 
FreeRequestByMmap(const struct UsbHostRequest * request)902 int32_t FreeRequestByMmap(const struct UsbHostRequest *request)
903 {
904     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
905     if (osAdapterOps->freeRequestByMmap == NULL) {
906         return HDF_ERR_NOT_SUPPORT;
907     }
908 
909     return osAdapterOps->freeRequestByMmap((struct UsbHostRequest *)request);
910 }
911 
RawFillBulkRequest(struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbFillRequestData * fillRequestData)912 int32_t RawFillBulkRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
913     const struct UsbFillRequestData *fillRequestData)
914 {
915     if (request == NULL || request->buffer == NULL || devHandle == NULL || fillRequestData == NULL) {
916         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
917         return HDF_ERR_INVALID_PARAM;
918     }
919 
920     if (UsbEndpointDirOut(fillRequestData->endPoint)) {
921         if (fillRequestData->buffer == NULL) {
922             HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
923             return HDF_ERR_INVALID_PARAM;
924         }
925         if (memcpy_s(request->buffer, request->bufLen, fillRequestData->buffer, fillRequestData->length) != EOK) {
926             HDF_LOGE("%{public}s:%d memcpy_s failed!", __func__, __LINE__);
927             return HDF_FAILURE;
928         }
929     }
930     request->devHandle = (struct UsbDeviceHandle *)devHandle;
931     request->endPoint = fillRequestData->endPoint;
932     request->requestType = USB_PIPE_TYPE_BULK;
933     request->timeout = fillRequestData->timeout;
934     request->length = fillRequestData->length;
935     request->userData = fillRequestData->userData;
936     request->callback = fillRequestData->callback;
937     request->userCallback = fillRequestData->userCallback;
938 
939     return HDF_SUCCESS;
940 }
941 
RawFillControlSetup(const unsigned char * setup,const struct UsbControlRequestData * requestData)942 int32_t RawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData)
943 {
944     struct UsbRawControlSetup *setupData = (struct UsbRawControlSetup *)setup;
945 
946     if (setup == NULL || requestData == NULL) {
947         HDF_LOGE("%{public}s: invalid param", __func__);
948         return HDF_ERR_INVALID_PARAM;
949     }
950 
951     setupData->requestType = requestData->requestType;
952     setupData->request = requestData->requestCmd;
953     setupData->value = CPU_TO_LE16(requestData->value);
954     setupData->index = CPU_TO_LE16(requestData->index);
955     setupData->length = CPU_TO_LE16(requestData->length);
956 
957     return HDF_SUCCESS;
958 }
959 
RawFillControlRequest(struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbFillRequestData * fillRequestData)960 int32_t RawFillControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
961     const struct UsbFillRequestData *fillRequestData)
962 {
963     if (request == NULL || devHandle == NULL || fillRequestData == NULL) {
964         HDF_LOGE("%{public}s: invalid param", __func__);
965         return HDF_ERR_INVALID_PARAM;
966     }
967 
968     request->devHandle = (struct UsbDeviceHandle *)devHandle;
969     request->endPoint = fillRequestData->endPoint;
970     request->requestType = USB_PIPE_TYPE_CONTROL;
971     request->timeout = fillRequestData->timeout;
972     request->userData = fillRequestData->userData;
973     request->callback = fillRequestData->callback;
974     request->userCallback = fillRequestData->userCallback;
975     request->length = fillRequestData->length;
976 
977     return HDF_SUCCESS;
978 }
979 
RawFillInterruptRequest(struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbFillRequestData * fillRequestData)980 int32_t RawFillInterruptRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
981     const struct UsbFillRequestData *fillRequestData)
982 {
983     if (request == NULL || devHandle == NULL || fillRequestData == NULL) {
984         HDF_LOGE("%{public}s:%d param is null!", __func__, __LINE__);
985         return HDF_ERR_INVALID_PARAM;
986     }
987 
988     if (UsbEndpointDirOut(fillRequestData->endPoint)) {
989         if (memcpy_s(request->buffer, request->bufLen, fillRequestData->buffer, fillRequestData->length) != EOK) {
990             HDF_LOGE("%{public}s:%d memcpy_s failed!", __func__, __LINE__);
991             return HDF_FAILURE;
992         }
993     }
994     request->devHandle = (struct UsbDeviceHandle *)devHandle;
995     request->endPoint = fillRequestData->endPoint;
996     request->requestType = USB_PIPE_TYPE_INTERRUPT;
997     request->timeout = fillRequestData->timeout;
998     request->length = fillRequestData->length;
999     request->userData = fillRequestData->userData;
1000     request->callback = fillRequestData->callback;
1001     request->userCallback = fillRequestData->userCallback;
1002 
1003     return HDF_SUCCESS;
1004 }
1005 
RawFillInterruptRequestByMmap(struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbFillRequestData * fillRequestData)1006 int32_t RawFillInterruptRequestByMmap(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
1007     const struct UsbFillRequestData *fillRequestData)
1008 {
1009     if (request == NULL || devHandle == NULL || fillRequestData == NULL) {
1010         HDF_LOGE("%{public}s:%d param is null!", __func__, __LINE__);
1011         return HDF_ERR_INVALID_PARAM;
1012     }
1013 
1014     request->devHandle = (struct UsbDeviceHandle *)devHandle;
1015     request->endPoint = fillRequestData->endPoint;
1016     request->requestType = USB_PIPE_TYPE_INTERRUPT;
1017     request->timeout = fillRequestData->timeout;
1018     request->length = fillRequestData->length;
1019     request->userData = fillRequestData->userData;
1020     request->callback = fillRequestData->callback;
1021     request->userCallback = fillRequestData->userCallback;
1022 
1023     return HDF_SUCCESS;
1024 }
1025 
RawFillIsoRequest(struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbFillRequestData * fillRequestData)1026 int32_t RawFillIsoRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
1027     const struct UsbFillRequestData *fillRequestData)
1028 {
1029     if (request == NULL || devHandle == NULL || fillRequestData == NULL) {
1030         HDF_LOGE("%{public}s:%d param is NULL!", __func__, __LINE__);
1031         return HDF_ERR_INVALID_PARAM;
1032     }
1033 
1034     if (UsbEndpointDirOut(fillRequestData->endPoint)) {
1035         int32_t ret = memcpy_s(request->buffer, request->bufLen, fillRequestData->buffer, fillRequestData->length);
1036         if (ret != EOK) {
1037             HDF_LOGE("%{public}s:%d memcpy_s fail!", __func__, __LINE__);
1038             return ret;
1039         }
1040     }
1041     request->devHandle = (struct UsbDeviceHandle *)devHandle;
1042     request->endPoint = fillRequestData->endPoint;
1043     request->requestType = USB_PIPE_TYPE_ISOCHRONOUS;
1044     request->timeout = fillRequestData->timeout;
1045     request->length = fillRequestData->length;
1046     request->numIsoPackets = fillRequestData->numIsoPackets;
1047     request->userData = fillRequestData->userData;
1048     request->callback = fillRequestData->callback;
1049     request->userCallback = fillRequestData->userCallback;
1050 
1051     return HDF_SUCCESS;
1052 }
1053 
RawSendControlRequest(struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbControlRequestData * requestData)1054 int32_t RawSendControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
1055     const struct UsbControlRequestData *requestData)
1056 {
1057     struct UsbFillRequestData fillRequestData = {0};
1058     unsigned char *setup = NULL;
1059     int32_t completed = 0;
1060     int32_t ret;
1061 
1062     if (request == NULL || request->buffer == NULL || devHandle == NULL || requestData == NULL) {
1063         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
1064         return HDF_ERR_INVALID_PARAM;
1065     }
1066 
1067     setup = request->buffer;
1068     RawFillControlSetup(setup, requestData);
1069     if ((requestData->requestType & USB_DDK_ENDPOINT_DIR_MASK) == USB_PIPE_DIRECTION_OUT) {
1070         fillRequestData.endPoint = 0;
1071         fillRequestData.length = requestData->length;
1072         if (requestData->length > 0) {
1073             ret = memcpy_s(request->buffer + USB_RAW_CONTROL_SETUP_SIZE, fillRequestData.length, requestData->data,
1074                 requestData->length);
1075             if (ret != EOK) {
1076                 HDF_LOGE("%{public}s:%d memcpy_s fail, requestData.length=%d", __func__, __LINE__, requestData->length);
1077                 return ret;
1078             }
1079         }
1080         fillRequestData.length = USB_RAW_CONTROL_SETUP_SIZE + requestData->length;
1081     } else {
1082         fillRequestData.endPoint = (0x1 << USB_DIR_OFFSET);
1083     }
1084     fillRequestData.userCallback = NULL;
1085     fillRequestData.callback = SyncRequestCallback;
1086     fillRequestData.userData = &completed;
1087     fillRequestData.timeout = requestData->timeout;
1088     RawFillControlRequest(request, devHandle, &fillRequestData);
1089 
1090     ret = OsalSemInit(&request->sem, 0);
1091     if (ret != HDF_SUCCESS) {
1092         HDF_LOGE("%{public}s:%d OsalSemInit failed, ret = %d ", __func__, __LINE__, ret);
1093         return ret;
1094     }
1095 
1096     ret = RawSubmitRequest(request);
1097     if (ret < 0) {
1098         OsalSemDestroy(&request->sem);
1099         return ret;
1100     }
1101 
1102     return ControlRequestCompletion(request, (struct UsbControlRequestData *)requestData);
1103 }
1104 
RawSendBulkRequest(const struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbRequestData * requestData)1105 int32_t RawSendBulkRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
1106     const struct UsbRequestData *requestData)
1107 {
1108     if (request == NULL || devHandle == NULL || requestData == NULL) {
1109         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
1110         return HDF_ERR_INVALID_PARAM;
1111     }
1112 
1113     return HandleSyncRequest((struct UsbHostRequest *)request, devHandle, requestData, USB_PIPE_TYPE_BULK);
1114 }
1115 
RawSendInterruptRequest(const struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbRequestData * requestData)1116 int32_t RawSendInterruptRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
1117     const struct UsbRequestData *requestData)
1118 {
1119     if (request == NULL || devHandle == NULL || requestData == NULL) {
1120         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
1121         return HDF_ERR_INVALID_PARAM;
1122     }
1123 
1124     return HandleSyncRequest((struct UsbHostRequest *)request, devHandle, requestData, USB_PIPE_TYPE_INTERRUPT);
1125 }
1126 
RawAllocRequest(const struct UsbDeviceHandle * devHandle,int32_t isoPackets,int32_t length)1127 struct UsbHostRequest *RawAllocRequest(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length)
1128 {
1129     struct UsbHostRequest *request = NULL;
1130     request = (struct UsbHostRequest *)AllocRequest(devHandle, isoPackets, length);
1131     if (request == NULL) {
1132         HDF_LOGE("%{public}s:%d RawMemAlloc failed", __func__, __LINE__);
1133         return NULL;
1134     }
1135     return request;
1136 }
1137 
RawAllocRequestByMmap(const struct UsbDeviceHandle * devHandle,int32_t isoPackets,int32_t length)1138 struct UsbHostRequest *RawAllocRequestByMmap(
1139     const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length)
1140 {
1141     struct UsbHostRequest *request = NULL;
1142     request = (struct UsbHostRequest *)AllocRequestByMmap(devHandle, isoPackets, length);
1143     if (request == NULL) {
1144         HDF_LOGE("%{public}s:%d RawMemAlloc failed", __func__, __LINE__);
1145         return NULL;
1146     }
1147     return request;
1148 }
1149 
RawFreeRequest(const struct UsbHostRequest * request)1150 int32_t RawFreeRequest(const struct UsbHostRequest *request)
1151 {
1152     if (request == NULL) {
1153         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
1154         return HDF_ERR_INVALID_PARAM;
1155     }
1156     return FreeRequest(request);
1157 }
1158 
RawFreeRequestByMmap(const struct UsbHostRequest * request)1159 int32_t RawFreeRequestByMmap(const struct UsbHostRequest *request)
1160 {
1161     if (request == NULL) {
1162         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
1163         return HDF_ERR_INVALID_PARAM;
1164     }
1165     return FreeRequestByMmap(request);
1166 }
1167 
RawGetConfigDescriptor(const struct UsbDevice * dev,uint8_t configIndex,struct UsbRawConfigDescriptor ** const config)1168 int32_t RawGetConfigDescriptor(
1169     const struct UsbDevice *dev, uint8_t configIndex, struct UsbRawConfigDescriptor ** const config)
1170 {
1171     int32_t ret;
1172     union UsbiConfigDescBuf tmpConfig;
1173     uint16_t configLen;
1174     uint8_t *buf = NULL;
1175 
1176     if (dev == NULL) {
1177         HDF_LOGE("%{public}s: invalid param", __func__);
1178         return HDF_ERR_INVALID_PARAM;
1179     }
1180 
1181     if (configIndex > dev->deviceDescriptor.bNumConfigurations) {
1182         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
1183         return HDF_ERR_BAD_FD;
1184     }
1185 
1186     ret = GetConfigDescriptor(dev, configIndex, tmpConfig.buf, sizeof(tmpConfig.buf));
1187     if (ret < HDF_SUCCESS) {
1188         HDF_LOGE("%{public}s:%d ret=%d", __func__, __LINE__, ret);
1189         return ret;
1190     }
1191     configLen = LE16_TO_CPU(tmpConfig.desc.wTotalLength);
1192     buf = RawUsbMemAlloc(configLen);
1193     if (buf == NULL) {
1194         HDF_LOGE("%{public}s:%d RawUsbMemAlloc failed", __func__, __LINE__);
1195         return HDF_ERR_MALLOC_FAIL;
1196     }
1197 
1198     ret = GetConfigDescriptor(dev, configIndex, buf, configLen);
1199     if (ret >= HDF_SUCCESS) {
1200         ret = DescToConfig(buf, ret, config);
1201     }
1202 
1203     RawUsbMemFree(buf);
1204     buf = NULL;
1205 
1206     return ret;
1207 }
1208 
RawClearConfiguration(struct UsbRawConfigDescriptor * config)1209 void RawClearConfiguration(struct UsbRawConfigDescriptor *config)
1210 {
1211     uint8_t i;
1212 
1213     if (config == NULL) {
1214         HDF_LOGE("%{public}s:%d config is NULL", __func__, __LINE__);
1215         return;
1216     }
1217 
1218     for (i = 0; i < config->configDescriptor.bNumInterfaces; i++) {
1219         ClearInterface((const struct UsbRawInterface *)(config->interface[i]));
1220         config->interface[i] = NULL;
1221     }
1222 
1223     if (config->extra != NULL) {
1224         RawUsbMemFree((void *)config->extra);
1225         config->extra = NULL;
1226     }
1227 }
1228 
RawGetConfiguration(const struct UsbDeviceHandle * devHandle,int32_t * config)1229 int32_t RawGetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t *config)
1230 {
1231     int32_t ret;
1232     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1233     uint8_t tmp = 0;
1234 
1235     if (devHandle == NULL || config == NULL) {
1236         HDF_LOGE("%{public}s:%d param is null", __func__, __LINE__);
1237         return HDF_ERR_INVALID_PARAM;
1238     }
1239 
1240     if (!osAdapterOps->getConfiguration) {
1241         HDF_LOGE("%{public}s:%d adapter don't support getConfiguration", __func__, __LINE__);
1242         return HDF_ERR_NOT_SUPPORT;
1243     }
1244 
1245     ret = osAdapterOps->getConfiguration(devHandle, &tmp);
1246     *config = tmp;
1247     return ret;
1248 }
1249 
RawSetConfiguration(const struct UsbDeviceHandle * devHandle,int32_t configuration)1250 int32_t RawSetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t configuration)
1251 {
1252     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1253 
1254     if (configuration < -1 || configuration > (int)0xFF) {
1255         HDF_LOGE("%{public}s: invalid param", __func__);
1256         return HDF_ERR_INVALID_PARAM;
1257     }
1258     if (!osAdapterOps->setConfiguration) {
1259         return HDF_ERR_NOT_SUPPORT;
1260     }
1261 
1262     return osAdapterOps->setConfiguration((struct UsbDeviceHandle *)devHandle, configuration);
1263 }
1264 
RawGetDescriptor(const struct UsbHostRequest * request,const struct UsbDeviceHandle * devHandle,const struct UsbRawDescriptorParam * param,const unsigned char * data)1265 int32_t RawGetDescriptor(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
1266     const struct UsbRawDescriptorParam *param, const unsigned char *data)
1267 {
1268     int32_t ret;
1269     struct UsbControlRequestData requestData;
1270 
1271     if (request == NULL || devHandle == NULL || param == NULL || data == NULL) {
1272         HDF_LOGE("%{public}s:%d param is NULL!", __func__, __LINE__);
1273         return HDF_ERR_INVALID_PARAM;
1274     }
1275 
1276     requestData.requestType = USB_PIPE_DIRECTION_IN;
1277     requestData.requestCmd = USB_REQUEST_GET_DESCRIPTOR;
1278     requestData.value = (uint16_t)((param->descType << BYTE_LENGTH) | param->descIndex);
1279     requestData.index = 0;
1280     requestData.data = (unsigned char *)data;
1281     requestData.length = (uint16_t)param->length;
1282     requestData.timeout = USB_RAW_REQUEST_DEFAULT_TIMEOUT;
1283     ret = RawSendControlRequest((struct UsbHostRequest *)request, devHandle, &requestData);
1284 
1285     return ret;
1286 }
1287 
RawGetDevice(const struct UsbDeviceHandle * devHandle)1288 struct UsbDevice *RawGetDevice(const struct UsbDeviceHandle *devHandle)
1289 {
1290     if (devHandle == NULL) {
1291         HDF_LOGE("%{public}s: invalid param", __func__);
1292         return NULL;
1293     }
1294 
1295     return devHandle->dev;
1296 }
1297 
RawGetDeviceDescriptor(const struct UsbDevice * dev,struct UsbDeviceDescriptor * desc)1298 int32_t RawGetDeviceDescriptor(const struct UsbDevice *dev, struct UsbDeviceDescriptor *desc)
1299 {
1300     if (dev == NULL || desc == NULL || sizeof(dev->deviceDescriptor) != USB_DDK_DT_DEVICE_SIZE) {
1301         HDF_LOGE("%{public}s: struct UsbDeviceDescriptor is not expected size", __func__);
1302         return HDF_ERR_INVALID_PARAM;
1303     }
1304 
1305     *desc = dev->deviceDescriptor;
1306     return HDF_SUCCESS;
1307 }
1308 
RawAttachKernelDriver(struct UsbDeviceHandle * devHandle,uint8_t interfaceNumber)1309 void RawAttachKernelDriver(struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber)
1310 {
1311     if (devHandle == NULL || interfaceNumber >= USB_MAXINTERFACES) {
1312         HDF_LOGE("%{public}s param is NULL or interfaceNumber = %d is out of range", __func__, interfaceNumber);
1313         return;
1314     }
1315 
1316     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1317     if (osAdapterOps->attachKernelDriver == NULL) {
1318         HDF_LOGE("%{public}s: releaseInterface not support", __func__);
1319         return;
1320     }
1321 
1322     OsalMutexLock(&devHandle->lock);
1323     osAdapterOps->attachKernelDriver(devHandle, interfaceNumber);
1324     OsalMutexUnlock(&devHandle->lock);
1325     return;
1326 }
1327 
RawReleaseInterface(struct UsbDeviceHandle * devHandle,int32_t interfaceNumber)1328 int32_t RawReleaseInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber)
1329 {
1330     int32_t ret;
1331     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1332 
1333     if (devHandle == NULL || interfaceNumber < 0 || interfaceNumber >= USB_MAXINTERFACES) {
1334         HDF_LOGE(
1335             "%{public}s:%d param is NULL or interfaceNumber = %d is out of range", __func__, __LINE__, interfaceNumber);
1336         return HDF_ERR_INVALID_PARAM;
1337     }
1338 
1339     OsalMutexLock(&devHandle->lock);
1340     if (!(devHandle->claimedInterfaces & (1U << (uint32_t)interfaceNumber))) {
1341         ret = HDF_ERR_BAD_FD;
1342         goto OUT;
1343     }
1344 
1345     if (!osAdapterOps->releaseInterface) {
1346         ret = HDF_ERR_NOT_SUPPORT;
1347         goto OUT;
1348     }
1349 
1350     ret = osAdapterOps->releaseInterface(devHandle, (unsigned int)interfaceNumber);
1351     if (ret == HDF_SUCCESS) {
1352         devHandle->claimedInterfaces &= ~(1U << (uint32_t)interfaceNumber);
1353     }
1354 
1355 OUT:
1356     OsalMutexUnlock(&devHandle->lock);
1357 
1358     return ret;
1359 }
1360 
RawResetDevice(const struct UsbDeviceHandle * devHandle)1361 int32_t RawResetDevice(const struct UsbDeviceHandle *devHandle)
1362 {
1363     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1364 
1365     if (!osAdapterOps->resetDevice) {
1366         return HDF_ERR_NOT_SUPPORT;
1367     }
1368 
1369     return osAdapterOps->resetDevice(devHandle);
1370 }
1371 
RawSubmitRequest(const struct UsbHostRequest * request)1372 int32_t RawSubmitRequest(const struct UsbHostRequest *request)
1373 {
1374     int32_t ret;
1375     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1376 
1377     if (request == NULL) {
1378         HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
1379         return HDF_ERR_INVALID_PARAM;
1380     }
1381 
1382     if (!osAdapterOps->submitRequest) {
1383         return HDF_ERR_NOT_SUPPORT;
1384     }
1385 
1386     ret = osAdapterOps->submitRequest((struct UsbHostRequest *)request);
1387     if (ret != HDF_SUCCESS) {
1388         HDF_LOGE("%{public}s:%d ret = %d", __func__, __LINE__, ret);
1389     }
1390 
1391     return ret;
1392 }
1393 
RawCancelRequest(const struct UsbHostRequest * request)1394 int32_t RawCancelRequest(const struct UsbHostRequest *request)
1395 {
1396     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1397 
1398     if (!osAdapterOps->cancelRequest) {
1399         return HDF_ERR_NOT_SUPPORT;
1400     }
1401 
1402     return osAdapterOps->cancelRequest((struct UsbHostRequest *)request);
1403 }
1404 
RawHandleRequest(const struct UsbDeviceHandle * devHandle)1405 int32_t RawHandleRequest(const struct UsbDeviceHandle *devHandle)
1406 {
1407     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1408     int32_t ret;
1409 
1410     if (!osAdapterOps->urbCompleteHandle) {
1411         return HDF_ERR_NOT_SUPPORT;
1412     }
1413 
1414     ret = osAdapterOps->urbCompleteHandle(devHandle);
1415     if (ret < 0) {}
1416 
1417     return ret;
1418 }
1419 
RawClearHalt(const struct UsbDeviceHandle * devHandle,uint8_t pipeAddress)1420 int32_t RawClearHalt(const struct UsbDeviceHandle *devHandle, uint8_t pipeAddress)
1421 {
1422     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1423     unsigned int endPoint = pipeAddress;
1424 
1425     if (osAdapterOps->clearHalt == NULL) {
1426         return HDF_ERR_NOT_SUPPORT;
1427     }
1428 
1429     return osAdapterOps->clearHalt(devHandle, endPoint);
1430 }
1431 
RawHandleRequestCompletion(struct UsbHostRequest * request,UsbRequestStatus status)1432 int32_t RawHandleRequestCompletion(struct UsbHostRequest *request, UsbRequestStatus status)
1433 {
1434     if (request == NULL) {
1435         HDF_LOGE("%{public}s:%d request is NULL!", __func__, __LINE__);
1436         return HDF_ERR_INVALID_PARAM;
1437     }
1438 
1439     request->status = status;
1440     if (request->callback) {
1441         request->callback((void *)request);
1442     }
1443 
1444     return HDF_SUCCESS;
1445 }
1446 
RawSetInterfaceAltsetting(const struct UsbDeviceHandle * devHandle,uint8_t interfaceNumber,uint8_t settingIndex)1447 int32_t RawSetInterfaceAltsetting(
1448     const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber, uint8_t settingIndex)
1449 {
1450     struct UsbOsAdapterOps *osAdapterOps = UsbAdapterGetOps();
1451 
1452     if (osAdapterOps->setInterfaceAltsetting == NULL) {
1453         return HDF_ERR_NOT_SUPPORT;
1454     }
1455 
1456     return osAdapterOps->setInterfaceAltsetting(devHandle, interfaceNumber, settingIndex);
1457 }
1458 
RawGetTid(void)1459 UsbRawTidType RawGetTid(void)
1460 {
1461     return UsbAdapterGetTid();
1462 }
1463 
RawRegisterSignal(void)1464 int32_t RawRegisterSignal(void)
1465 {
1466     return UsbAdapterRegisterSignal();
1467 }
1468 
RawKillSignal(struct UsbDeviceHandle * devHandle,UsbRawTidType tid)1469 int32_t RawKillSignal(struct UsbDeviceHandle *devHandle, UsbRawTidType tid)
1470 {
1471     return UsbAdapterKillSignal(devHandle, tid);
1472 }
1473 
RawInitPnpService(enum UsbPnpNotifyServiceCmd cmdType,struct UsbPnpAddRemoveInfo infoData)1474 int32_t RawInitPnpService(enum UsbPnpNotifyServiceCmd cmdType, struct UsbPnpAddRemoveInfo infoData)
1475 {
1476     if (cmdType != USB_PNP_NOTIFY_ADD_INTERFACE && cmdType != USB_PNP_NOTIFY_REMOVE_INTERFACE) {
1477         HDF_LOGE("%{public}s:%d invalid param cmdType", __func__, __LINE__);
1478         return HDF_ERR_INVALID_PARAM;
1479     }
1480 
1481     int32_t ret;
1482     struct HdfIoService *serv = HdfIoServiceBind(USB_HOST_PNP_SERVICE_NAME);
1483     if (serv == NULL || serv->dispatcher == NULL || serv->dispatcher->Dispatch == NULL) {
1484         ret = HDF_FAILURE;
1485         HDF_LOGE("%{public}s:%d failed to get service %s", __func__, __LINE__, USB_HOST_PNP_SERVICE_NAME);
1486         return ret;
1487     }
1488 
1489     struct HdfSBuf *pnpData = HdfSbufObtainDefaultSize();
1490     struct HdfSBuf *pnpReply = HdfSbufObtainDefaultSize();
1491     if (pnpData == NULL || pnpReply == NULL) {
1492         ret = HDF_FAILURE;
1493         HDF_LOGE("%{public}s:%d GetService err", __func__, __LINE__);
1494         goto ERR_SBUF;
1495     }
1496 
1497     if (!HdfSbufWriteBuffer(pnpData, (const void *)(&infoData), sizeof(struct UsbPnpAddRemoveInfo))) {
1498         HDF_LOGE("%{public}s:%d sbuf write infoData failed", __func__, __LINE__);
1499         ret = HDF_FAILURE;
1500         goto OUT;
1501     }
1502 
1503     ret = serv->dispatcher->Dispatch(&serv->object, cmdType, pnpData, pnpReply);
1504     if (ret != HDF_SUCCESS) {
1505         HDF_LOGE("%{public}s:%d Dispatch USB_PNP_NOTIFY_REMOVE_TEST failed ret = %d", __func__, __LINE__, ret);
1506         goto OUT;
1507     }
1508 
1509     int32_t replyData = 0;
1510     if (!HdfSbufReadInt32(pnpReply, &replyData)) {
1511         HDF_LOGE("%{public}s:HdfSbufReadInt32 failed", __func__);
1512         ret = HDF_FAILURE;
1513         goto OUT;
1514     }
1515     if (replyData != INT32_MAX) {
1516         HDF_LOGE("%{public}s:%d cmdType = %d reply failed", __func__, __LINE__, cmdType);
1517         ret = HDF_FAILURE;
1518         goto OUT;
1519     }
1520     ret = HDF_SUCCESS;
1521     HDF_LOGI("%{public}s:%d cmdType = %d reply success", __func__, __LINE__, cmdType);
1522 
1523 OUT:
1524     HdfSbufRecycle(pnpData);
1525     HdfSbufRecycle(pnpReply);
1526 ERR_SBUF:
1527     HdfIoServiceRecycle(serv);
1528 
1529     return ret;
1530 }
1531 
RawRequestListInit(struct UsbDevice * deviceObj)1532 void RawRequestListInit(struct UsbDevice *deviceObj)
1533 {
1534     if (deviceObj == NULL) {
1535         HDF_LOGE("%{public}s:%d deviceObj is NULL!", __func__, __LINE__);
1536         return;
1537     }
1538 
1539     OsalMutexInit(&deviceObj->requestLock);
1540     HdfSListInit(&deviceObj->requestList);
1541 }
1542 
RawUsbMemTestTrigger(bool enable)1543 int32_t RawUsbMemTestTrigger(bool enable)
1544 {
1545     g_usbRamTestFlag = enable;
1546     return HDF_SUCCESS;
1547 }
1548 
RawUsbMemAlloc(size_t size)1549 void *RawUsbMemAlloc(size_t size)
1550 {
1551     return RawUsbMemCalloc(size);
1552 }
1553 
RawUsbMemCalloc(size_t size)1554 void *RawUsbMemCalloc(size_t size)
1555 {
1556     if (size == 0) {
1557         HDF_LOGE("%{public}s:%d size is 0", __func__, __LINE__);
1558         return NULL;
1559     }
1560 
1561     void *buf = OsalMemCalloc(size);
1562     if (buf == NULL) {
1563         HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1564         return NULL;
1565     }
1566 
1567     if (g_usbRamTestFlag) {
1568         if (g_usbRamTestHead == NULL) {
1569             g_usbRamTestHead = OsalMemCalloc(sizeof(struct RawUsbRamTestList));
1570             if (g_usbRamTestHead == NULL) {
1571                 HDF_LOGE("%{public}s: %{public}d, OsalMemCalloc failed", __func__, __LINE__);
1572                 OsalMemFree(buf);
1573                 return NULL;
1574             }
1575             OsalMutexInit(&g_usbRamTestHead->lock);
1576             DListHeadInit(&g_usbRamTestHead->list);
1577         }
1578         struct RawUsbRamTestList *testEntry = OsalMemCalloc(sizeof(struct RawUsbRamTestList));
1579         if (testEntry == NULL) {
1580             HDF_LOGE("%{public}s:%d testEntry is NULL", __func__, __LINE__);
1581             OsalMemFree(buf);
1582             return buf;
1583         }
1584         testEntry->address = (uintptr_t)buf;
1585         testEntry->size = size;
1586 
1587         struct RawUsbRamTestList *pos = NULL;
1588         uint32_t totalSize = 0;
1589         OsalMutexLock(&g_usbRamTestHead->lock);
1590         DListInsertTail(&testEntry->list, &g_usbRamTestHead->list);
1591         DLIST_FOR_EACH_ENTRY(pos, &g_usbRamTestHead->list, struct RawUsbRamTestList, list) {
1592             totalSize += pos->size;
1593         }
1594         OsalMutexUnlock(&g_usbRamTestHead->lock);
1595 
1596         HDF_LOGI("%{public}s add size=%{public}d totalSize=%{public}d", __func__, (uint32_t)size, totalSize);
1597     }
1598     return buf;
1599 }
1600 
RawUsbMemFree(void * mem)1601 void RawUsbMemFree(void *mem)
1602 {
1603     if (mem == NULL) {
1604         HDF_LOGE("%{public}s:%{public}d mem is null.", __func__, __LINE__);
1605         return;
1606     }
1607 
1608     if (g_usbRamTestFlag && g_usbRamTestHead != NULL) {
1609         struct RawUsbRamTestList *pos = NULL;
1610         struct RawUsbRamTestList *tmp = NULL;
1611         uint32_t totalSize = 0;
1612         uint32_t size = 0;
1613         OsalMutexLock(&g_usbRamTestHead->lock);
1614         DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_usbRamTestHead->list, struct RawUsbRamTestList, list) {
1615             if (pos->address == (uintptr_t)mem) {
1616                 size = pos->size;
1617                 DListRemove(&pos->list);
1618                 OsalMemFree(pos);
1619                 continue;
1620             }
1621             totalSize += pos->size;
1622         }
1623         OsalMutexUnlock(&g_usbRamTestHead->lock);
1624         HDF_LOGI("%{public}s rm size=%{public}d totalSize=%{public}d", __func__, size, totalSize);
1625     }
1626 
1627     if (mem != NULL) {
1628         OsalMemFree(mem);
1629     }
1630 }
1631