• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_impl.h"
17 
18 #include <cerrno>
19 #include <hdf_base.h>
20 #include <hdf_log.h>
21 #include <sys/mman.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include "ddk_pnp_listener_mgr.h"
26 #include "device_resource_if.h"
27 #include "hdf_slist.h"
28 #include "hisysevent.h"
29 #include "hitrace_meter.h"
30 #include "osal_mutex.h"
31 #include "usb_ddk_interface.h"
32 #include "usb_ddk_pnp_loader.h"
33 #include "usb_interface_pool.h"
34 #include "usbd_dispatcher.h"
35 #include "usbd_function.h"
36 #include "usbd_port.h"
37 using namespace OHOS::HiviewDFX;
38 constexpr double USB_RECOGNITION_FAIL_RATE_BASE = 100.00;
39 
40 namespace OHOS {
41 namespace HDI {
42 namespace Usb {
43 namespace V1_0 {
44 HdfDevEventlistener UsbImpl::listenerForLoadService_ = {nullptr};
45 UsbdLoadService UsbImpl::loadUsbService_ = {USB_SYSTEM_ABILITY_ID};
46 UsbdLoadService UsbImpl::loadHdfEdm_ = {HDF_EXTERNAL_DEVICE_MANAGER_SA_ID};
47 UsbdSubscriber UsbImpl::subscribers_[MAX_SUBSCRIBER] = {{0}};
48 bool UsbImpl::isGadgetConnected_ = false;
49 uint32_t UsbImpl::attachCount_ = 0;
50 uint32_t UsbImpl::attachFailedCount_ = 0;
51 
UsbInterfaceImplGetInstance(void)52 extern "C" IUsbInterface *UsbInterfaceImplGetInstance(void)
53 {
54     using OHOS::HDI::Usb::V1_0::UsbImpl;
55     UsbImpl *service = new (std::nothrow) UsbImpl();
56     if (service == nullptr) {
57         return nullptr;
58     }
59     return service;
60 }
61 
UsbImpl()62 UsbImpl::UsbImpl() : session_(nullptr), device_(nullptr)
63 {
64     HdfSListInit(&devList_);
65     OsalMutexInit(&lock_);
66 }
67 
~UsbImpl()68 UsbImpl::~UsbImpl()
69 {
70     UsbdReleaseDevices();
71 }
72 
FindDevFromService(uint8_t busNum,uint8_t devAddr)73 HostDevice *UsbImpl::FindDevFromService(uint8_t busNum, uint8_t devAddr)
74 {
75     HdfSListIterator it;
76     HostDevice *port = nullptr;
77     bool flag = false;
78 
79     OsalMutexLock(&lock_);
80     HdfSListIteratorInit(&it, &devList_);
81     while (HdfSListIteratorHasNext(&it)) {
82         port = reinterpret_cast<HostDevice *>(HdfSListIteratorNext(&it));
83         if (port == nullptr) {
84             continue;
85         }
86         if (port->busNum == busNum && port->devAddr == devAddr) {
87             flag = true;
88             break;
89         }
90     }
91     OsalMutexUnlock(&lock_);
92     if (!flag) {
93         return nullptr;
94     }
95     return port;
96 }
97 
MakeUsbControlParams(UsbControlParams * controlParams,uint8_t * buffer,uint16_t length,uint16_t value,uint16_t index)98 void UsbImpl::MakeUsbControlParams(
99     UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index)
100 {
101     if (controlParams == nullptr || buffer == nullptr) {
102         HDF_LOGE("%{public}s: %{public}d controlParams or buffer is nullptr", __func__, __LINE__);
103         return;
104     }
105 
106     controlParams->request = USB_DDK_REQ_GET_DESCRIPTOR;
107     controlParams->target = USB_REQUEST_TARGET_DEVICE;
108     controlParams->reqType = USB_REQUEST_TYPE_STANDARD;
109     controlParams->directon = USB_REQUEST_DIR_FROM_DEVICE;
110     controlParams->value = value;
111     controlParams->index = index;
112     controlParams->data = buffer;
113     controlParams->size = length;
114 }
115 
MakeGetActiveUsbControlParams(UsbControlParams * controlParams,uint8_t * buffer,uint16_t length,uint16_t value,uint16_t index)116 void UsbImpl::MakeGetActiveUsbControlParams(
117     UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index)
118 {
119     if (controlParams == nullptr || buffer == nullptr) {
120         HDF_LOGE("%{public}s: %{public}d controlParams or buffer is nullptr", __func__, __LINE__);
121         return;
122     }
123 
124     controlParams->request = USB_DDK_REQ_GET_CONFIGURATION;
125     controlParams->target = USB_REQUEST_TARGET_DEVICE;
126     controlParams->reqType = USB_REQUEST_TYPE_STANDARD;
127     controlParams->directon = USB_REQUEST_DIR_FROM_DEVICE;
128     controlParams->value = value;
129     controlParams->index = index;
130     controlParams->data = buffer;
131     controlParams->size = length;
132 }
133 
UsbControlTransferEx(HostDevice * dev,UsbControlParams * ctrParams,int32_t timeout)134 int32_t UsbImpl::UsbControlTransferEx(HostDevice *dev, UsbControlParams *ctrParams, int32_t timeout)
135 {
136     UsbRequestParams params;
137     if (memset_s(&params, sizeof(params), 0, sizeof(params)) != EOK) {
138         HDF_LOGE("%{public}s:memset_s params failed", __func__);
139         return HDF_FAILURE;
140     }
141 
142     if (dev == nullptr || dev->ctrDevHandle == nullptr || ctrParams == nullptr || ctrParams->data == nullptr) {
143         HDF_LOGE("%{public}s:invalid params", __func__);
144         return HDF_ERR_INVALID_PARAM;
145     }
146 
147     UsbRequest *request = nullptr;
148     if (dev->ctrlReq == nullptr) {
149         request = UsbAllocRequest(dev->ctrDevHandle, 0, MAX_CONTROL_BUFF_SIZE);
150         if (request == nullptr) {
151             return HDF_ERR_MALLOC_FAIL;
152         }
153         dev->ctrlReq = request;
154     } else {
155         request = dev->ctrlReq;
156     }
157 
158     UsbdDispatcher::UsbRequestParamsInit(&params, timeout);
159     int32_t ret = UsbdDispatcher::UsbControlSetUp(ctrParams, &params.ctrlReq);
160     if (ret != HDF_SUCCESS) {
161         return ret;
162     }
163     OsalMutexLock(&dev->lock);
164     do {
165         ret = UsbFillRequest(request, dev->ctrDevHandle, &params);
166         if (ret != HDF_SUCCESS) {
167             HDF_LOGE("%{public}s:UsbFillRequest failed, ret = %{public}d", __func__, ret);
168             break;
169         }
170 
171         ret = UsbSubmitRequestSync(request);
172         if (ret != HDF_SUCCESS) {
173             HDF_LOGE("%{public}s:UsbSubmitRequestSync failed, ret=%{public}d", __func__, ret);
174             break;
175         }
176 
177         if (ctrParams->directon == USB_REQUEST_DIR_FROM_DEVICE) {
178             ret = memcpy_s(ctrParams->data, ctrParams->size, request->compInfo.buffer, request->compInfo.actualLength);
179             if (ret != EOK) {
180                 HDF_LOGE("%{public}s: memcpy_s failed, ret  = %{public}d", __func__, ret);
181                 break;
182             }
183             ctrParams->size =
184                 ctrParams->size < request->compInfo.actualLength ? ctrParams->size : request->compInfo.actualLength;
185         }
186     } while (0);
187 
188     OsalMutexUnlock(&dev->lock);
189     return ret;
190 }
191 
MakeSetActiveUsbControlParams(UsbControlParams * controlParams,uint8_t * buffer,uint16_t length,uint16_t value,uint16_t index)192 void UsbImpl::MakeSetActiveUsbControlParams(
193     UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index)
194 {
195     if (controlParams == nullptr || buffer == nullptr) {
196         HDF_LOGE("%{public}s: %{public}d controlParams or buffer is nullptr", __func__, __LINE__);
197         return;
198     }
199 
200     controlParams->request = USB_DDK_REQ_SET_CONFIGURATION;
201     controlParams->target = USB_REQUEST_TARGET_DEVICE;
202     controlParams->reqType = USB_REQUEST_TYPE_STANDARD;
203     controlParams->directon = USB_REQUEST_DIR_FROM_DEVICE;
204     controlParams->value = value;
205     controlParams->index = index;
206     controlParams->data = buffer;
207     controlParams->size = length;
208 }
209 
ReOpenDevice(HostDevice * port)210 int32_t UsbImpl::ReOpenDevice(HostDevice *port)
211 {
212     if (port == nullptr) {
213         HDF_LOGE("%{public}s:invalid param port", __func__);
214         return HDF_FAILURE;
215     }
216 
217     uint8_t busNum = port->busNum;
218     uint8_t devAddr = port->devAddr;
219     UsbdDispatcher::UsbdRelease(port);
220     port->busNum = busNum;
221     port->devAddr = devAddr;
222     OsalMutexInit(&port->writeLock);
223     OsalMutexInit(&port->readLock);
224     OsalMutexInit(&port->lock);
225     OsalMutexInit(&port->requestLock);
226     OsalMutexInit(&port->reqSyncLock);
227     OsalMutexInit(&port->reqASyncLock);
228     HdfSListInit(&port->requestQueue);
229     HdfSListInit(&port->reqSyncList);
230     HdfSListInit(&port->reqASyncList);
231     int32_t ret = UsbdDispatcher::UsbdInit(port);
232     if (ret != HDF_SUCCESS) {
233         HDF_LOGE("%{public}s:UsbInit failed ret:%{public}d", __func__, ret);
234         UsbdDispatcher::UsbdRelease(port);
235         UsbdDispatcher::RemoveDevFromService(port->service, port);
236         OsalMemFree(port);
237         return ret;
238     }
239 
240     DataFifoReset(&port->readFifo);
241     port->initFlag = true;
242     return HDF_SUCCESS;
243 }
244 
InterfaceIdToHandle(const HostDevice * dev,uint8_t id)245 UsbInterfaceHandle *UsbImpl::InterfaceIdToHandle(const HostDevice *dev, uint8_t id)
246 {
247     if (dev == nullptr) {
248         HDF_LOGE("%{public}s: invalid param dev is nullptr", __func__);
249         return nullptr;
250     }
251 
252     if (id == MAX_INTERFACEID) {
253         return dev->ctrDevHandle;
254     } else if (id < USB_MAX_INTERFACES) {
255         return dev->devHandle[id];
256     }
257     return nullptr;
258 }
259 
UsbdRequestSyncReleaseList(HostDevice * port)260 int32_t UsbImpl::UsbdRequestSyncReleaseList(HostDevice *port)
261 {
262     if (port == nullptr) {
263         HDF_LOGE("%{public}s:invalid param port is nullptr", __func__);
264         return HDF_ERR_INVALID_PARAM;
265     }
266 
267     HdfSListIterator it;
268     OsalMutexLock(&port->reqSyncLock);
269     HdfSListIteratorInit(&it, &port->reqSyncList);
270     while (HdfSListIteratorHasNext(&it)) {
271         UsbdRequestSync *req = reinterpret_cast<UsbdRequestSync *>(HdfSListIteratorNext(&it));
272         if (req == nullptr) {
273             continue;
274         }
275         HdfSListIteratorRemove(&it);
276         int32_t ret = UsbdDispatcher::UsbdRequestSyncRelease(req);
277         if (ret != HDF_SUCCESS) {
278             HDF_LOGE("%{public}s:UsbdRequestSyncRelease failed", __func__);
279         }
280     }
281     OsalMutexUnlock(&port->reqSyncLock);
282 
283     return HDF_SUCCESS;
284 }
285 
UsbdRequestASyncReleaseList(HostDevice * port)286 int32_t UsbImpl::UsbdRequestASyncReleaseList(HostDevice *port)
287 {
288     if (port == nullptr) {
289         HDF_LOGE("%{public}s:invalid param port", __func__);
290         return HDF_ERR_INVALID_PARAM;
291     }
292 
293     HdfSListIterator it;
294     OsalMutexLock(&port->reqASyncLock);
295     HdfSListIteratorInit(&it, &port->reqASyncList);
296     while (HdfSListIteratorHasNext(&it)) {
297         UsbdRequestASync *req = reinterpret_cast<UsbdRequestASync *>(HdfSListIteratorNext(&it));
298         if (req == nullptr) {
299             continue;
300         }
301         HdfSListIteratorRemove(&it);
302         int32_t ret = UsbdDispatcher::UsbdRequestASyncRelease(req);
303         if (ret != HDF_SUCCESS) {
304             HDF_LOGW("%{public}s:UsbdRequestASyncRelease failed", __func__);
305         }
306     }
307     OsalMutexUnlock(&port->reqASyncLock);
308 
309     return HDF_SUCCESS;
310 }
311 
UsbdBulkASyncListReleasePort(HostDevice * port)312 int32_t UsbImpl::UsbdBulkASyncListReleasePort(HostDevice *port)
313 {
314     if (port == nullptr || port->bulkASyncList == nullptr) {
315         HDF_LOGE("%{public}s: port or port->bulkASyncList is nullptr", __func__);
316         return HDF_SUCCESS;
317     }
318 
319     UsbdBulkASyncList *list = nullptr;
320     while (port->bulkASyncList) {
321         list = port->bulkASyncList;
322         port->bulkASyncList = list->next;
323         UsbdDispatcher::UsbdBulkASyncListRelease(list);
324         list = nullptr;
325     }
326     return HDF_SUCCESS;
327 }
328 
UsbdFindRequestSyncAndCreat(HostDevice * port,uint8_t interfaceId,uint8_t pipeAddr,UsbdRequestSync ** request)329 int32_t UsbImpl::UsbdFindRequestSyncAndCreat(
330     HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr, UsbdRequestSync **request)
331 {
332     if (port == nullptr || request == nullptr) {
333         HDF_LOGE("%{public}s:ifId:%{public}u, epId:%{public}u", __func__, interfaceId, pipeAddr);
334         return HDF_ERR_INVALID_PARAM;
335     }
336 
337     *request = nullptr;
338     UsbdRequestSync *requestSync = UsbdDispatcher::UsbdFindRequestSync(port, interfaceId, pipeAddr);
339     if (requestSync != nullptr) {
340         *request = requestSync;
341         return HDF_SUCCESS;
342     }
343 
344     UsbPipeInfo pipe;
345     if (memset_s(&pipe, sizeof(UsbPipeInfo), 0, sizeof(UsbPipeInfo)) != EOK) {
346         HDF_LOGE("%{public}s:memset_s pipe failed ", __func__);
347         return HDF_FAILURE;
348     }
349 
350     int32_t ret = UsbdDispatcher::GetPipe(port, interfaceId, pipeAddr, &pipe);
351     if (ret != HDF_SUCCESS) {
352         HDF_LOGE("%{public}s: get pipe failed ifId:%{public}u, epId:%{public}u", __func__, interfaceId, pipeAddr);
353         return HDF_FAILURE;
354     }
355 
356     UsbInterfaceHandle *ifHandle = InterfaceIdToHandle(port, interfaceId);
357     if (ifHandle == nullptr) {
358         HDF_LOGE("%{public}s:get interface handle failed", __func__);
359         return HDF_ERR_INVALID_PARAM;
360     }
361 
362     requestSync = UsbdDispatcher::UsbdRequestSyncAlloc();
363     if (requestSync == nullptr) {
364         HDF_LOGE("%{public}s:fail ifId:%{public}u pipeId:%{public}u", __func__, interfaceId, pipeAddr);
365         return HDF_ERR_MALLOC_FAIL;
366     }
367 
368     ret = UsbdDispatcher::UsbdRequestSyncInit(port, ifHandle, &pipe, requestSync);
369     if (ret != HDF_SUCCESS) {
370         HDF_LOGE("%{public}s:UsbdRequestSyncInit failed ret:%{public}d", __func__, ret);
371         int32_t tRet = UsbdDispatcher::UsbdRequestSyncRelease(requestSync);
372         requestSync = nullptr;
373         if (tRet != HDF_SUCCESS) {
374             HDF_LOGE("%{public}s:Release failed ifId:%{public}u pipeId:%{public}u", __func__, interfaceId, pipeAddr);
375         }
376         return ret;
377     }
378     *request = requestSync;
379     return ret;
380 }
381 
UsbdBulkReadSyncBase(int32_t timeout,uint8_t * buffer,uint32_t size,uint32_t * actlength,UsbdRequestSync * requestSync)382 int32_t UsbImpl::UsbdBulkReadSyncBase(
383     int32_t timeout, uint8_t *buffer, uint32_t size, uint32_t *actlength, UsbdRequestSync *requestSync)
384 {
385     if (requestSync == nullptr || requestSync->request == nullptr || buffer == nullptr || actlength == nullptr ||
386         size < 1) {
387         HDF_LOGE("%{public}s:invalid params", __func__);
388         return HDF_ERR_INVALID_PARAM;
389     }
390 
391     HITRACE_METER_NAME(HITRACE_TAG_HDF, "UsbdBulkReadSyncBase");
392     int32_t ret = HDF_FAILURE;
393     uint64_t intTimeout = timeout < 0 ? 0 : static_cast<uint64_t>(timeout);
394     uint64_t stime = OsalGetSysTimeMs();
395     uint32_t tcur = 0;
396     OsalMutexLock(&requestSync->lock);
397     requestSync->params.timeout = 500; // 500 is timeout
398     uint32_t msize = requestSync->pipe.maxPacketSize;
399     while (tcur + msize < size) {
400         ret = UsbFillRequest(requestSync->request, requestSync->ifHandle, &requestSync->params);
401         if (ret != HDF_SUCCESS) {
402             HDF_LOGE("%{public}s: UsbFillRequest failed, ret=%{public}d \n", __func__, ret);
403             break;
404         }
405 
406         ret = UsbSubmitRequestSync(requestSync->request);
407         if (ret == HDF_SUCCESS) {
408             ret = memcpy_s(buffer + tcur, size - tcur, requestSync->request->compInfo.buffer,
409                 requestSync->request->compInfo.actualLength);
410             if (ret != EOK) {
411                 HDF_LOGE("%{public}s: memcpy_s failed, ret = %{public}d", __func__, ret);
412                 break;
413             }
414             tcur += requestSync->request->compInfo.actualLength;
415         } else if (ret == HDF_ERR_TIMEOUT) {
416             if (tcur > 0) {
417                 ret = HDF_SUCCESS;
418                 break;
419             }
420             uint64_t ntime = OsalGetSysTimeMs();
421             if (intTimeout == 0 || (ntime < stime + intTimeout)) {
422                 continue;
423             } else {
424                 HDF_LOGE("%{public}s:tcur:%{public}u ret:%{public}d", __func__, tcur, ret);
425                 break;
426             }
427         } else {
428             HDF_LOGE("%{public}s:tcur:%{public}u ret:%{public}d", __func__, tcur, ret);
429             break;
430         }
431     }
432     OsalMutexUnlock(&requestSync->lock);
433 
434     *actlength = tcur;
435     return ret;
436 }
437 
UsbdBulkWriteSyncBase(HostDevice * port,UsbdRequestSync * requestSync,const uint8_t * buffer,uint32_t length,int32_t timeout)438 int32_t UsbImpl::UsbdBulkWriteSyncBase(
439     HostDevice *port, UsbdRequestSync *requestSync, const uint8_t *buffer, uint32_t length, int32_t timeout)
440 {
441     if (port == nullptr || requestSync == nullptr || buffer == nullptr || length < 1) {
442         HDF_LOGE("%{public}s:invalid param", __func__);
443         return HDF_ERR_INVALID_PARAM;
444     }
445 
446     HITRACE_METER_NAME(HITRACE_TAG_HDF, "UsbdBulkWriteSyncBase");
447     int32_t ret = HDF_FAILURE;
448     OsalMutexLock(&requestSync->lock);
449     uint32_t initTimeout = timeout < 0 ? 0 : static_cast<uint32_t>(timeout);
450     requestSync->params.timeout = initTimeout;
451     requestSync->params.userData = port;
452     uint32_t tcur = 0;
453     uint32_t msize = static_cast<uint32_t>(requestSync->pipe.maxPacketSize);
454     while (tcur < length) {
455         uint32_t tsize = (length - tcur) < msize ? (length - tcur) : msize;
456         requestSync->params.dataReq.buffer = static_cast<unsigned char *>(const_cast<uint8_t *>(buffer) + tcur);
457         requestSync->params.dataReq.length = tsize;
458         tcur += tsize;
459         ret = UsbFillRequest(requestSync->request, requestSync->ifHandle, &requestSync->params);
460         if (ret != HDF_SUCCESS) {
461             HDF_LOGE("%{public}s:UsbFillRequest failed, ret:%{public}d, tcur:%{public}u, length:%{public}u", __func__,
462                 ret, tcur, length);
463             break;
464         }
465         ret = UsbSubmitRequestSync(requestSync->request);
466         if (ret != HDF_SUCCESS) {
467             HDF_LOGE("%{public}s:UsbSubmitRequestSync failed, ret:%{public}d tcur:%{public}u length:%{public}u",
468                 __func__, ret, tcur, length);
469             break;
470         }
471     }
472     OsalMutexUnlock(&requestSync->lock);
473     return ret;
474 }
475 
UsbdFindRequestASync(HostDevice * port,uint8_t interfaceId,uint8_t pipeAddr)476 UsbdRequestASync *UsbImpl::UsbdFindRequestASync(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr)
477 {
478     if (port == nullptr) {
479         HDF_LOGE("%{public}s:invalid param port", __func__);
480         return nullptr;
481     }
482 
483     UsbdRequestASync *req = nullptr;
484     HdfSListIterator it;
485     uint8_t reqnum = 0;
486     uint8_t usenum = 0;
487     bool flag = false;
488     OsalMutexLock(&port->reqASyncLock);
489     HdfSListIteratorInit(&it, &port->reqASyncList);
490     while (HdfSListIteratorHasNext(&it)) {
491         req = reinterpret_cast<UsbdRequestASync *>(HdfSListIteratorNext(&it));
492         if (req == nullptr) {
493             continue;
494         }
495         if (req->pipe.interfaceId == interfaceId && ((req->pipe.pipeAddress | req->pipe.pipeDirection) == pipeAddr)) {
496             reqnum++;
497             if (req->status) {
498                 usenum++;
499             } else {
500                 flag = true;
501                 break;
502             }
503         }
504     }
505     OsalMutexUnlock(&port->reqASyncLock);
506 
507     if (flag) {
508         if (req) {
509             OsalMutexLock(&req->lock);
510             req->status = 1;
511             OsalMutexUnlock(&req->lock);
512         }
513         return req;
514     } else {
515         if (reqnum < MAX_REQUESTASYNC_NUM) {
516             req = UsbdDispatcher::UsbdRequestASyncCreatAndInsert(port, interfaceId, pipeAddr);
517             if (req) {
518                 OsalMutexLock(&req->lock);
519                 req->status = 1;
520                 OsalMutexUnlock(&req->lock);
521             }
522             return req;
523         }
524     }
525     return nullptr;
526 }
527 
FunRequestQueueFillAndSubmit(HostDevice * port,UsbdRequestASync * reqAsync,const uint8_t * buffer,uint32_t length)528 int32_t UsbImpl::FunRequestQueueFillAndSubmit(
529     HostDevice *port, UsbdRequestASync *reqAsync, const uint8_t *buffer, uint32_t length)
530 {
531     if (port == nullptr || reqAsync == nullptr) {
532         HDF_LOGE("%{public}s:invalid param port or reqAsync", __func__);
533         return HDF_ERR_INVALID_PARAM;
534     }
535 
536     UsbdDispatcher::FillReqAyncParams(reqAsync, &reqAsync->pipe, &reqAsync->params, buffer, length);
537     int32_t ret = UsbFillRequest(reqAsync->reqMsg.request, reqAsync->ifHandle, &reqAsync->params);
538     if (ret != HDF_SUCCESS) {
539         HDF_LOGE("%{public}s:UsbFillRequest failed, ret:%{public}d", __func__, ret);
540         OsalMutexLock(&reqAsync->lock);
541         reqAsync->reqMsg.clientData = nullptr;
542         reqAsync->reqMsg.clientLength = 0;
543         reqAsync->params.dataReq.buffer = nullptr;
544         reqAsync->params.dataReq.length = 0;
545         reqAsync->status = 0;
546         OsalMutexUnlock(&reqAsync->lock);
547         return ret;
548     }
549 
550     UsbIfRequest *requestConvertVal = reinterpret_cast<UsbIfRequest *>(reqAsync->reqMsg.request);
551     OsalSemInit(&requestConvertVal->hostRequest->sem, 0);
552     ret = UsbSubmitRequestAsync(reqAsync->reqMsg.request);
553     if (ret == HDF_SUCCESS) {
554         OsalMutexLock(&port->requestLock);
555         HdfSListAddTail(&port->requestQueue, &reqAsync->qNode);
556         OsalMutexUnlock(&port->requestLock);
557     } else {
558         HDF_LOGE("%{public}s:UsbSubmitRequestAsync failed, ret:%{public}d", __func__, ret);
559         OsalMutexLock(&reqAsync->lock);
560         reqAsync->status = 0;
561         reqAsync->reqMsg.clientData = nullptr;
562         reqAsync->reqMsg.clientLength = 0;
563         reqAsync->params.dataReq.buffer = nullptr;
564         reqAsync->params.dataReq.length = 0;
565         OsalMutexUnlock(&reqAsync->lock);
566     }
567     return ret;
568 }
569 
GetRequestMsgFromQueue(HostDevice * port,UsbdRequestASync ** reqMsg)570 int32_t UsbImpl::GetRequestMsgFromQueue(HostDevice *port, UsbdRequestASync **reqMsg)
571 {
572     if (port == nullptr || reqMsg == nullptr) {
573         HDF_LOGE("%{public}s: port or reqMsg is nullptr", __func__);
574         return HDF_ERR_INVALID_PARAM;
575     }
576 
577     HdfSListNode *requestNode = nullptr;
578     OsalMutexLock(&port->requestLock);
579     if (!HdfSListIsEmpty(&port->requestQueue)) {
580         requestNode = HdfSListPop(&port->requestQueue);
581     }
582     OsalMutexUnlock(&port->requestLock);
583     if (requestNode == nullptr) {
584         HDF_LOGE("%{public}s:requestNode is nullptr", __func__);
585         return HDF_FAILURE;
586     }
587     *reqMsg = HDF_SLIST_CONTAINER_OF(HdfSListNode, requestNode, UsbdRequestASync, qNode);
588     if (*reqMsg == nullptr) {
589         HDF_LOGE("%{public}s:request msg is nullptr", __func__);
590         return HDF_FAILURE;
591     }
592     return HDF_SUCCESS;
593 }
594 
GetRequestMsgData(HostDevice * port,UsbdRequestASync * reqMsg,int32_t timeout,uint8_t ** buffer,uint32_t * length)595 int32_t UsbImpl::GetRequestMsgData(
596     HostDevice *port, UsbdRequestASync *reqMsg, int32_t timeout, uint8_t **buffer, uint32_t *length)
597 {
598     if (port == nullptr || reqMsg == nullptr || reqMsg->reqMsg.request == nullptr) {
599         HDF_LOGE("%{public}s: invalid param", __func__);
600         return HDF_ERR_INVALID_PARAM;
601     }
602 
603     int32_t ret = HDF_SUCCESS;
604     UsbIfRequest *reqValue = reinterpret_cast<UsbIfRequest *>(reqMsg->reqMsg.request);
605     if (static_cast<int32_t>(reqMsg->reqMsg.request->compInfo.status) == -1) {
606         ret = OsalSemWait(&reqValue->hostRequest->sem, timeout);
607         if (ret != HDF_SUCCESS) {
608             HDF_LOGE("%{public}s:OsalSemWait failed, ret:%{public}d", __func__, ret);
609             OsalMutexLock(&port->requestLock);
610             HdfSListAdd(&port->requestQueue, &reqMsg->qNode);
611             OsalMutexUnlock(&port->requestLock);
612             return ret;
613         }
614     }
615 
616     OsalMutexLock(&reqMsg->lock);
617     *length = reqMsg->reqMsg.length;
618     *buffer = static_cast<uint8_t *>(reqMsg->reqMsg.buffer);
619     reqMsg->reqMsg.buffer = nullptr;
620     reqMsg->reqMsg.length = 0;
621     reqMsg->status = 0;
622     OsalMutexUnlock(&reqMsg->lock);
623     return ret;
624 }
625 
UsbdRequestASyncReleaseData(UsbdRequestASync * request)626 int32_t UsbImpl::UsbdRequestASyncReleaseData(UsbdRequestASync *request)
627 {
628     if (request == nullptr) {
629         HDF_LOGE("%{public}s: invalid param request is nullptr", __func__);
630         return HDF_ERR_INVALID_PARAM;
631     }
632 
633     if (request->reqMsg.buffer != nullptr) {
634         HDF_LOGI("%{public}s:free buffer\n", __func__);
635         OsalMemFree(request->reqMsg.buffer);
636         request->reqMsg.buffer = nullptr;
637     }
638     request->reqMsg.length = 0;
639     if (request->reqMsg.clientData) {
640         HDF_LOGI("%{public}s:free clientData\n", __func__);
641         OsalMemFree(request->reqMsg.clientData);
642         request->reqMsg.clientData = nullptr;
643     }
644     request->reqMsg.clientLength = 0;
645     request->status = 0;
646     return HDF_SUCCESS;
647 }
648 
UsbdBulkASyncListFind(HostDevice * port,uint8_t ifId,uint8_t epId)649 UsbdBulkASyncList *UsbImpl::UsbdBulkASyncListFind(HostDevice *port, uint8_t ifId, uint8_t epId)
650 {
651     if (port == nullptr) {
652         HDF_LOGE("%{public}s:invalid param port", __func__);
653         return nullptr;
654     }
655 
656     UsbdBulkASyncList *list = port->bulkASyncList;
657     while (list) {
658         if (list->epId == epId && list->ifId == ifId) {
659             return list;
660         }
661         list = list->next;
662     }
663     return nullptr;
664 }
665 
UsbdBulkASyncListInit(HostDevice * port,uint8_t ifId,uint8_t epId)666 UsbdBulkASyncList *UsbImpl::UsbdBulkASyncListInit(HostDevice *port, uint8_t ifId, uint8_t epId)
667 {
668     UsbdBulkASyncList *list = UsbdBulkASyncListFind(port, ifId, epId);
669     if (list == nullptr) {
670         list = UsbdDispatcher::UsbdBulkASyncListAlloc(port, ifId, epId);
671         if (list != nullptr) {
672             list->next = port->bulkASyncList;
673             port->bulkASyncList = list;
674         }
675     }
676     return list;
677 }
678 
InitAsmBufferHandle(UsbdBufferHandle * handle,int32_t fd,int32_t size)679 int32_t UsbImpl::InitAsmBufferHandle(UsbdBufferHandle *handle, int32_t fd, int32_t size)
680 {
681     if (handle == nullptr) {
682         HDF_LOGE("%{public}s:invalid param handle", __func__);
683         return HDF_ERR_INVALID_PARAM;
684     }
685     ReleaseAsmBufferHandle(handle);
686 
687     if (fd < 0 || size < 0) {
688         HDF_LOGE("%{public}s:invalid param fd or size", __func__);
689         return HDF_ERR_INVALID_PARAM;
690     }
691     handle->fd = fd;
692     handle->size = static_cast<uint32_t>(size);
693     handle->cur = 0;
694     handle->rcur = 0;
695     handle->cbflg = 0;
696     lseek(fd, 0, SEEK_SET);
697     handle->starAddr = static_cast<uint8_t *>(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
698     close(fd);
699     if (handle->starAddr == nullptr) {
700         handle->fd = -1;
701         handle->size = 0;
702         HDF_LOGE("%{public}s:mmap error:%{public}s", __func__, strerror(errno));
703         return HDF_ERR_MALLOC_FAIL;
704     }
705     return HDF_SUCCESS;
706 }
707 
UsbdBulkReadASyncSubmitStart(UsbdBulkASyncList * list)708 int32_t UsbImpl::UsbdBulkReadASyncSubmitStart(UsbdBulkASyncList *list)
709 {
710     if (list == nullptr) {
711         HDF_LOGE("%{public}s:invalid param list", __func__);
712         return HDF_ERR_INVALID_PARAM;
713     }
714 
715     uint32_t i = 0;
716     int32_t ret = HDF_SUCCESS;
717     do {
718         UsbdBulkASyncReqNode *req = UsbdDispatcher::UsbdBulkASyncReqGetENode(&list->rList);
719         if (req == nullptr) {
720             HDF_LOGE("%{public}s:UsbdBulkASyncReqGetENode failed", __func__);
721             ret = HDF_ERR_DEVICE_BUSY;
722             break;
723         } else {
724             ret = UsbdDispatcher::UsbdBulkASyncReqReadSubmit(req);
725             if (ret != HDF_SUCCESS) {
726                 HDF_LOGE("%{public}s:UsbdBulkASyncReqReadSubmit failed", __func__);
727                 break;
728             }
729         }
730         if (++i > USBD_BULKASYNCREQ_NUM_MAX) {
731             break;
732         }
733     } while (!DListIsEmpty(&list->rList.eList));
734     return ret;
735 }
736 
UsbdBulkASyncWriteSubmitStart(UsbdBulkASyncList * list)737 int32_t UsbImpl::UsbdBulkASyncWriteSubmitStart(UsbdBulkASyncList *list)
738 {
739     if (list == nullptr) {
740         HDF_LOGE("%{public}s:invalid param list", __func__);
741         return HDF_ERR_INVALID_PARAM;
742     }
743 
744     uint32_t i = 0;
745     int32_t ret = HDF_SUCCESS;
746     do {
747         UsbdBulkASyncReqNode *req = UsbdDispatcher::UsbdBulkASyncReqGetENode(&list->rList);
748         if (req == nullptr) {
749             HDF_LOGE("%{public}s:UsbdBulkASyncReqGetENode failed", __func__);
750             ret = HDF_ERR_DEVICE_BUSY;
751             break;
752         } else {
753             ret = UsbdDispatcher::UsbdBulkASyncReqWriteSubmit(req);
754             if (ret != HDF_SUCCESS) {
755                 HDF_LOGE("%{public}s:UsbdBulkASyncReqWriteSubmit failed", __func__);
756                 break;
757             }
758         }
759         if (++i > USBD_BULKASYNCREQ_NUM_MAX) {
760             break;
761         }
762     } while (!DListIsEmpty(&list->rList.eList));
763     return ret;
764 }
765 
ReleaseAsmBufferHandle(UsbdBufferHandle * handle)766 void UsbImpl::ReleaseAsmBufferHandle(UsbdBufferHandle *handle)
767 {
768     if (handle == nullptr) {
769         HDF_LOGE("%{public}s:invalid param handle", __func__);
770         return;
771     }
772 
773     if (handle->size > 0) {
774         OsalMutexLock(&handle->lock);
775         handle->cur = 0;
776         handle->rcur = 0;
777         handle->fd = -1;
778         handle->cbflg = 0;
779         munmap(handle->starAddr, handle->size);
780         OsalMutexUnlock(&handle->lock);
781     }
782 
783     handle->fd = -1;
784     handle->cur = 0;
785     handle->rcur = 0;
786     handle->size = 0;
787     handle->cbflg = 0;
788     handle->starAddr = nullptr;
789 }
790 
BulkRequestCancel(UsbdBulkASyncList * list)791 int32_t UsbImpl::BulkRequestCancel(UsbdBulkASyncList *list)
792 {
793     if (list == nullptr) {
794         HDF_LOGE("%{public}s:invalid param list", __func__);
795         return HDF_ERR_INVALID_PARAM;
796     }
797 
798     for (int32_t i = 0; i < USBD_BULKASYNCREQ_NUM_MAX; ++i) {
799         if (list->rList.node[i].use == USBD_REQNODE_USE || list->rList.node[i].use == USBD_REQNODE_OTHER) {
800             int32_t ret = UsbCancelRequest(list->rList.node[i].request);
801             if (ret != HDF_SUCCESS) {
802                 HDF_LOGE("%{public}s: UsbCancelRequest failed, ret=%{public}d ", __func__, ret);
803                 return ret;
804             }
805         }
806     }
807     OsalMSleep(USB_BULK_CANCEL_SLEEP_TIME);
808 
809     for (int32_t i = 0; i < USBD_BULKASYNCREQ_NUM_MAX; ++i) {
810         list->rList.node[i].request->compInfo.status = UsbRequestStatus::USB_REQUEST_COMPLETED;
811         if (list->rList.node[i].use == USBD_REQNODE_USE || list->rList.node[i].use == USBD_REQNODE_OTHER) {
812             list->rList.node[i].request->compInfo.status = UsbRequestStatus::USB_REQUEST_COMPLETED;
813             UsbdDispatcher::UsbdBulkASyncReqNodeSetNoUse(&list->rList.node[i]);
814         }
815     }
816     return HDF_SUCCESS;
817 }
818 
ReportUsbdSysEvent(int32_t code,UsbPnpNotifyMatchInfoTable * infoTable)819 void UsbImpl::ReportUsbdSysEvent(int32_t code, UsbPnpNotifyMatchInfoTable *infoTable)
820 {
821     if (code == HDF_SUCCESS) {
822         HDF_LOGI("%{public}s: UsbdDeviceCreateAndAttach successed", __func__);
823         attachCount_++;
824     } else if (code != HDF_ERR_DEVICE_BUSY) {
825         attachFailedCount_++;
826         attachCount_++;
827         HDF_LOGI("%{public}s: UsbdDeviceCreateAndAttach failed", __func__);
828         HiSysEventWrite(HiSysEvent::Domain::HDF_USB, "RECOGNITION_FAIL", HiSysEvent::EventType::FAULT, "DEVICE_NAME",
829             std::to_string(infoTable->busNum) + "-" + std::to_string(infoTable->devNum), "DEVICE_PROTOCOL",
830             infoTable->deviceInfo.deviceProtocol, "DEVICE_CLASS", infoTable->deviceInfo.deviceClass, "VENDOR_ID",
831             infoTable->deviceInfo.vendorId, "PRODUCT_ID", infoTable->deviceInfo.productId, "VERSION", "1.0.0",
832             "FAIL_REASON", 0, "FAIL_INFO", "USB device recognition failed");
833         HiSysEventWrite(HiSysEvent::Domain::HDF_USB, "RECOGNITION_FAIL_STATISTICS", HiSysEvent::EventType::FAULT,
834             "EXCEPTION_CNT", attachFailedCount_, "TOTAL_CNT", attachCount_, "FAIL_RATE",
835             (static_cast<double>(attachFailedCount_)) /
836                 (static_cast<double>(attachCount_) * USB_RECOGNITION_FAIL_RATE_BASE),
837             "QUALITY_STATISTICAL", "Failure rate statistics");
838     } else {
839         HDF_LOGI("%{public}s:device already add", __func__);
840     }
841 }
842 
UsbdPnpNotifyAddAndRemoveDevice(HdfSBuf * data,UsbdSubscriber * usbdSubscriber,uint32_t id)843 int32_t UsbImpl::UsbdPnpNotifyAddAndRemoveDevice(HdfSBuf *data, UsbdSubscriber *usbdSubscriber, uint32_t id)
844 {
845     if (data == nullptr) {
846         HDF_LOGE("%{public}s: data is nullptr", __func__);
847         return HDF_ERR_INVALID_PARAM;
848     }
849     sptr<UsbImpl> super = static_cast<UsbImpl *>(usbdSubscriber->impl);
850     if (super == nullptr) {
851         HDF_LOGE("%{public}s super is nullptr", __func__);
852         return HDF_ERR_INVALID_PARAM;
853     }
854     const sptr<IUsbdSubscriber> subscriber = usbdSubscriber->subscriber;
855 
856     uint32_t infoSize;
857     UsbPnpNotifyMatchInfoTable *infoTable = nullptr;
858     bool flag = HdfSbufReadBuffer(data, (const void **)(&infoTable), &infoSize);
859     if (!flag || infoTable == nullptr) {
860         HDF_LOGE("%{public}s: HdfSbufReadBuffer failed, flag=%{public}d", __func__, flag);
861         return HDF_ERR_INVALID_PARAM;
862     }
863 
864     if (infoTable->deviceInfo.deviceClass == BASE_CLASS_HUB) {
865         HDF_LOGI("%{public}s:hub device", __func__);
866         if (id == USB_PNP_NOTIFY_REMOVE_DEVICE) {
867             HDF_LOGI("%{public}s:UsbdRemoveBusDev busNum:%{public}d", __func__, infoTable->busNum);
868             UsbdDispatcher::UsbdRemoveBusDev(super, infoTable->busNum, subscriber);
869         }
870         return HDF_SUCCESS;
871     }
872 
873     int32_t ret = HDF_SUCCESS;
874     if (id == USB_PNP_NOTIFY_ADD_DEVICE) {
875         ret = UsbdDispatcher::UsbdDeviceCreateAndAttach(super, infoTable->busNum, infoTable->devNum);
876         ReportUsbdSysEvent(ret, infoTable);
877         USBDeviceInfo info = {ACT_DEVUP, infoTable->busNum, infoTable->devNum};
878         if (subscriber == nullptr) {
879             HDF_LOGE("%{public}s: subscriber is nullptr, %{public}d", __func__, __LINE__);
880             return HDF_FAILURE;
881         }
882         ret = subscriber->DeviceEvent(info);
883     } else if (id == USB_PNP_NOTIFY_REMOVE_DEVICE) {
884         UsbdDispatcher::UsbdDeviceDettach(super, infoTable->busNum, infoTable->devNum);
885         USBDeviceInfo info = {ACT_DEVDOWN, infoTable->busNum, infoTable->devNum};
886         if (subscriber == nullptr) {
887             HDF_LOGE("%{public}s: subscriber is nullptr, %{public}d", __func__, __LINE__);
888             return HDF_FAILURE;
889         }
890         ret = subscriber->DeviceEvent(info);
891     }
892     return ret;
893 }
894 
UsbdPnpLoaderEventReceived(void * priv,uint32_t id,HdfSBuf * data)895 int32_t UsbImpl::UsbdPnpLoaderEventReceived(void *priv, uint32_t id, HdfSBuf *data)
896 {
897     UsbdSubscriber *usbdSubscriber = static_cast<UsbdSubscriber *>(priv);
898     const sptr<IUsbdSubscriber> subscriber = usbdSubscriber->subscriber;
899 
900     int32_t ret = HDF_SUCCESS;
901     if (id == USB_PNP_DRIVER_GADGET_ADD) {
902         isGadgetConnected_ = true;
903         USBDeviceInfo info = {ACT_UPDEVICE, 0, 0};
904         if (subscriber == nullptr) {
905             HDF_LOGE("%{public}s: subscriber is nullptr, %{public}d", __func__, __LINE__);
906             return HDF_FAILURE;
907         }
908         ret = subscriber->DeviceEvent(info);
909         return ret;
910     } else if (id == USB_PNP_DRIVER_GADGET_REMOVE) {
911         isGadgetConnected_ = false;
912         USBDeviceInfo info = {ACT_DOWNDEVICE, 0, 0};
913         if (subscriber == nullptr) {
914             HDF_LOGE("%{public}s: subscriber is nullptr, %{public}d", __func__, __LINE__);
915             return HDF_FAILURE;
916         }
917         ret = subscriber->DeviceEvent(info);
918         return ret;
919     } else if (id == USB_PNP_DRIVER_PORT_HOST) {
920         return UsbdPort::GetInstance().UpdatePort(PORT_MODE_HOST, subscriber);
921     } else if (id == USB_PNP_DRIVER_PORT_DEVICE) {
922         return UsbdPort::GetInstance().UpdatePort(PORT_MODE_DEVICE, subscriber);
923     }
924 
925     ret = UsbdPnpNotifyAddAndRemoveDevice(data, usbdSubscriber, id);
926     return ret;
927 }
928 
UsbdLoadServiceCallback(void * priv,uint32_t id,HdfSBuf * data)929 int32_t UsbImpl::UsbdLoadServiceCallback(void *priv, uint32_t id, HdfSBuf *data)
930 {
931     (void)priv;
932     (void)data;
933     if (id == USB_PNP_DRIVER_GADGET_ADD || id == USB_PNP_NOTIFY_ADD_DEVICE) {
934         if (loadUsbService_.LoadService() != 0) {
935             HDF_LOGE("usbdev LoadUsbServer error");
936             return HDF_FAILURE;
937         }
938         if (id == USB_PNP_NOTIFY_ADD_DEVICE) {
939             if (loadHdfEdm_.LoadService() != 0) {
940                 HDF_LOGE("usbdev LoadUsbServer error");
941                 return HDF_FAILURE;
942             }
943         }
944     }
945     return HDF_SUCCESS;
946 }
947 
UsbdEventHandle(const sptr<UsbImpl> & inst)948 int32_t UsbImpl::UsbdEventHandle(const sptr<UsbImpl> &inst)
949 {
950     inst->parsePortPath();
951     listenerForLoadService_.callBack = UsbdLoadServiceCallback;
952     if (DdkListenerMgrAdd(&listenerForLoadService_) != HDF_SUCCESS) {
953         HDF_LOGE("%{public}s: register listerer failed", __func__);
954         return HDF_FAILURE;
955     }
956     return HDF_SUCCESS;
957 }
958 
UsbdEventHandleRelease(void)959 int32_t UsbImpl::UsbdEventHandleRelease(void)
960 {
961     int32_t ret = DdkListenerMgrRemove(&listenerForLoadService_);
962     if (ret != HDF_SUCCESS) {
963         HDF_LOGE("%{public}s: DdkListenerMgrRemove failed", __func__);
964     }
965     listenerForLoadService_.callBack = nullptr;
966     listenerForLoadService_.priv = nullptr;
967     return ret;
968 }
969 
UsbdReleaseDevices()970 int32_t UsbImpl::UsbdReleaseDevices()
971 {
972     OsalMutexLock(&lock_);
973     while (!HdfSListIsEmpty(&devList_)) {
974         HostDevice *port = reinterpret_cast<HostDevice *>(HdfSListPop(&devList_));
975         if (port != nullptr) {
976             UsbdDispatcher::UsbdRelease(port);
977             OsalMemFree(port);
978         }
979     }
980     OsalMutexUnlock(&lock_);
981     return HDF_SUCCESS;
982 }
983 
OpenDevice(const UsbDev & dev)984 int32_t UsbImpl::OpenDevice(const UsbDev &dev)
985 {
986     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
987     if (port == nullptr) {
988         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
989         return HDF_DEV_ERR_NO_DEVICE;
990     }
991     port->initFlag = true;
992     return HDF_SUCCESS;
993 }
994 
CloseDevice(const UsbDev & dev)995 int32_t UsbImpl::CloseDevice(const UsbDev &dev)
996 {
997     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
998     if (port == nullptr) {
999         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1000         return HDF_DEV_ERR_NO_DEVICE;
1001     }
1002     port->initFlag = false;
1003     return HDF_SUCCESS;
1004 }
1005 
GetDeviceDescriptor(const UsbDev & dev,std::vector<uint8_t> & descriptor)1006 int32_t UsbImpl::GetDeviceDescriptor(const UsbDev &dev, std::vector<uint8_t> &descriptor)
1007 {
1008     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1009     if (port == nullptr) {
1010         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1011         return HDF_DEV_ERR_NO_DEVICE;
1012     }
1013 
1014     uint16_t length = MAX_CONTROL_BUFF_SIZE;
1015     uint8_t buffer[USB_MAX_DESCRIPTOR_SIZE] = {0};
1016     UsbControlParams controlParams = {0};
1017     MakeUsbControlParams(&controlParams, buffer, length, static_cast<int32_t>(USB_DDK_DT_DEVICE) << TYPE_OFFSET_8, 0);
1018     int32_t ret = UsbControlTransferEx(port, &controlParams, USB_CTRL_SET_TIMEOUT);
1019     if (ret != HDF_SUCCESS) {
1020         HDF_LOGE("%{public}s:UsbControlTransferEx failed, ret:%{public}d", __func__, ret);
1021         return ret;
1022     }
1023 
1024     descriptor.assign(buffer, buffer + USB_MAX_DESCRIPTOR_SIZE);
1025     return HDF_SUCCESS;
1026 }
1027 
GetStringDescriptor(const UsbDev & dev,uint8_t descId,std::vector<uint8_t> & descriptor)1028 int32_t UsbImpl::GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &descriptor)
1029 {
1030     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1031     if (port == nullptr) {
1032         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1033         return HDF_DEV_ERR_NO_DEVICE;
1034     }
1035 
1036     uint16_t length = MAX_CONTROL_BUFF_SIZE;
1037     uint8_t buffer[USB_MAX_DESCRIPTOR_SIZE] = {0};
1038     UsbControlParams controlParams = {0};
1039     MakeUsbControlParams(
1040         &controlParams, buffer, length, (static_cast<int32_t>(USB_DDK_DT_STRING) << TYPE_OFFSET_8) + descId, 0);
1041     int32_t ret = UsbControlTransferEx(port, &controlParams, GET_STRING_SET_TIMEOUT);
1042     if (ret != HDF_SUCCESS) {
1043         HDF_LOGE("%{public}s:UsbControlTransferEx failed ret=%{public}d", __func__, ret);
1044         return ret;
1045     }
1046 
1047     descriptor.assign(buffer, buffer + USB_MAX_DESCRIPTOR_SIZE);
1048     return HDF_SUCCESS;
1049 }
1050 
GetConfigDescriptor(const UsbDev & dev,uint8_t descId,std::vector<uint8_t> & descriptor)1051 int32_t UsbImpl::GetConfigDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &descriptor)
1052 {
1053     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1054     if (port == nullptr) {
1055         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1056         return HDF_DEV_ERR_NO_DEVICE;
1057     }
1058 
1059     uint16_t length = MAX_CONTROL_BUFF_SIZE;
1060     uint8_t buffer[USB_MAX_DESCRIPTOR_SIZE] = {0};
1061     UsbControlParams controlParams = {0};
1062     MakeUsbControlParams(
1063         &controlParams, buffer, length, (static_cast<int32_t>(USB_DDK_DT_CONFIG) << TYPE_OFFSET_8) + descId, 0);
1064     int32_t ret = UsbControlTransferEx(port, &controlParams, USB_CTRL_SET_TIMEOUT);
1065     if (ret != HDF_SUCCESS) {
1066         HDF_LOGE("%{public}s:UsbControlTransferEx failed ret=%{public}d", __func__, ret);
1067         return ret;
1068     }
1069 
1070     descriptor.assign(buffer, buffer + USB_MAX_DESCRIPTOR_SIZE);
1071     return HDF_SUCCESS;
1072 }
1073 
GetRawDescriptor(const UsbDev & dev,std::vector<uint8_t> & descriptor)1074 int32_t UsbImpl::GetRawDescriptor(const UsbDev &dev, std::vector<uint8_t> &descriptor)
1075 {
1076     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1077     if (port == nullptr || port->ctrDevHandle == nullptr) {
1078         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1079         return HDF_DEV_ERR_NO_DEVICE;
1080     }
1081 
1082     UsbInterfaceHandleEntity *handle = reinterpret_cast<UsbInterfaceHandleEntity *>(port->ctrDevHandle);
1083     OsalMutexLock(&handle->devHandle->lock);
1084     uint8_t *ptr = static_cast<uint8_t *>(handle->devHandle->dev->descriptors);
1085     uint32_t length = handle->devHandle->dev->descriptorsLength;
1086     descriptor.assign(ptr, ptr + length);
1087     OsalMutexUnlock(&handle->devHandle->lock);
1088     return HDF_SUCCESS;
1089 }
1090 
GetFileDescriptor(const UsbDev & dev,int32_t & fd)1091 int32_t UsbImpl::GetFileDescriptor(const UsbDev &dev, int32_t &fd)
1092 {
1093     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1094     if (port == nullptr || port->ctrDevHandle == nullptr) {
1095         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1096         return HDF_DEV_ERR_NO_DEVICE;
1097     }
1098 
1099     UsbInterfaceHandleEntity *handle = reinterpret_cast<UsbInterfaceHandleEntity *>(port->ctrDevHandle);
1100     OsalMutexLock(&handle->devHandle->lock);
1101     fd = handle->devHandle->fd;
1102     OsalMutexUnlock(&handle->devHandle->lock);
1103     return HDF_SUCCESS;
1104 }
1105 
SetConfig(const UsbDev & dev,uint8_t configIndex)1106 int32_t UsbImpl::SetConfig(const UsbDev &dev, uint8_t configIndex)
1107 {
1108     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1109     if (port == nullptr) {
1110         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1111         return HDF_DEV_ERR_NO_DEVICE;
1112     }
1113 
1114     uint8_t configIdOld = 0;
1115     uint8_t configIdNew = 0;
1116     uint16_t length = 1;
1117     UsbControlParams controlParams;
1118     MakeGetActiveUsbControlParams(&controlParams, &configIdOld, length, 0, 0);
1119     int32_t ret = UsbControlTransferEx(port, &controlParams, USB_CTRL_SET_TIMEOUT);
1120     if (ret != HDF_SUCCESS) {
1121         HDF_LOGE("%{public}s:setConfiguration failed ret:%{public}d", __func__, ret);
1122         return HDF_ERR_INVALID_PARAM;
1123     }
1124     if (configIndex == configIdOld) {
1125         HDF_LOGE("%{public}s:setConfiguration success, configIndex:%{public}d configIdOld:%{public}d", __func__,
1126             configIndex, configIdOld);
1127         return HDF_SUCCESS;
1128     }
1129 
1130     length = 0;
1131     MakeSetActiveUsbControlParams(&controlParams, &configIndex, length, static_cast<int32_t>(configIndex), 0);
1132     ret = UsbControlTransferEx(port, &controlParams, USB_CTRL_SET_TIMEOUT);
1133     if (ret != HDF_SUCCESS) {
1134         HDF_LOGE("%{public}s:setConfiguration failed ret:%{public}d", __func__, ret);
1135         return HDF_ERR_IO;
1136     }
1137 
1138     length = 1;
1139     MakeGetActiveUsbControlParams(&controlParams, &configIdNew, length, 0, 0);
1140     ret = UsbControlTransferEx(port, &controlParams, USB_CTRL_SET_TIMEOUT);
1141     if (ret != HDF_SUCCESS || configIndex != configIdNew) {
1142         HDF_LOGE("%{public}s:getConfiguration failed ret:%{public}d", __func__, ret);
1143         return HDF_ERR_IO;
1144     }
1145     if (configIndex != 0) {
1146         ret = ReOpenDevice(port);
1147         if (ret != HDF_SUCCESS) {
1148             HDF_LOGE("%{public}s:ReOpenDevice failed ret:%{public}d", __func__, ret);
1149             return ret;
1150         }
1151     }
1152     return ret;
1153 }
1154 
GetConfig(const UsbDev & dev,uint8_t & configIndex)1155 int32_t UsbImpl::GetConfig(const UsbDev &dev, uint8_t &configIndex)
1156 {
1157     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1158     if (port == nullptr) {
1159         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1160         return HDF_DEV_ERR_NO_DEVICE;
1161     }
1162 
1163     uint16_t length = 1;
1164     UsbControlParams controlParams;
1165     MakeGetActiveUsbControlParams(&controlParams, &configIndex, length, 0, 0);
1166     int32_t ret = UsbControlTransferEx(port, &controlParams, USB_CTRL_SET_TIMEOUT);
1167     if (ret != HDF_SUCCESS) {
1168         HDF_LOGE("%{public}s:UsbControlTransferEx failed", __func__);
1169         return HDF_ERR_IO;
1170     }
1171     return HDF_SUCCESS;
1172 }
1173 
ClaimInterface(const UsbDev & dev,uint8_t interfaceId,uint8_t force)1174 int32_t UsbImpl::ClaimInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t force)
1175 {
1176     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1177     if (port == nullptr) {
1178         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1179         return HDF_DEV_ERR_NO_DEVICE;
1180     }
1181     if (interfaceId >= USB_MAX_INTERFACES) {
1182         HDF_LOGE("%{public}s:interfaceId larger then max num", __func__);
1183         return HDF_ERR_INVALID_PARAM;
1184     }
1185 
1186     if (port->iface[interfaceId] == nullptr) {
1187         if (force) {
1188             port->iface[interfaceId] =
1189                 UsbClaimInterface(port->service->session_, port->busNum, port->devAddr, interfaceId);
1190         } else {
1191             port->iface[interfaceId] =
1192                 UsbClaimInterfaceUnforce(port->service->session_, port->busNum, port->devAddr, interfaceId);
1193         }
1194         if (port->iface[interfaceId] == nullptr) {
1195             HDF_LOGE("%{public}s:UsbClaimInterface failed, busNum=%{public}u, devAddr=%{public}u", __func__,
1196                 port->busNum, port->devAddr);
1197             return HDF_FAILURE;
1198         }
1199     }
1200     if (port->devHandle[interfaceId] == nullptr) {
1201         port->devHandle[interfaceId] = UsbOpenInterface(port->iface[interfaceId]);
1202         if (port->devHandle[interfaceId] == nullptr) {
1203             HDF_LOGE("%{public}s:UsbOpenInterface failed.", __func__);
1204             return HDF_FAILURE;
1205         }
1206     }
1207     return HDF_SUCCESS;
1208 }
1209 
ReleaseInterface(const UsbDev & dev,uint8_t interfaceId)1210 int32_t UsbImpl::ReleaseInterface(const UsbDev &dev, uint8_t interfaceId)
1211 {
1212     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1213     if (port == nullptr) {
1214         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1215         return HDF_DEV_ERR_NO_DEVICE;
1216     }
1217 
1218     if (interfaceId >= USB_MAX_INTERFACES) {
1219         HDF_LOGE("%{public}s:ReleaseInterface failed.", __func__);
1220         return HDF_ERR_INVALID_PARAM;
1221     }
1222     return HDF_SUCCESS;
1223 }
1224 
SetInterface(const UsbDev & dev,uint8_t interfaceId,uint8_t altIndex)1225 int32_t UsbImpl::SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex)
1226 {
1227     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1228     if (port == nullptr) {
1229         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1230         return HDF_DEV_ERR_NO_DEVICE;
1231     }
1232 
1233     UsbInterfaceHandle *interfaceHandle = InterfaceIdToHandle(port, interfaceId);
1234     if (interfaceHandle == nullptr || interfaceId >= USB_MAX_INTERFACES) {
1235         HDF_LOGE("%{public}s:interfaceId failed busNum:%{public}u devAddr:%{public}u interfaceId:%{public}u", __func__,
1236             port->busNum, port->devAddr, interfaceId);
1237         return HDF_FAILURE;
1238     }
1239     if (port->iface[interfaceId] && port->iface[interfaceId]->info.curAltSetting == altIndex) {
1240         HDF_LOGE("%{public}s:bus:%{public}d devAddr:%{public}d interfaceId:%{public}d cur:%{public}d", __func__,
1241             port->busNum, port->devAddr, interfaceId, altIndex);
1242         return HDF_SUCCESS;
1243     }
1244 
1245     int32_t ret = UsbSelectInterfaceSetting(interfaceHandle, altIndex, &port->iface[interfaceId]);
1246     if (ret == HDF_SUCCESS) {
1247         UsbdRequestSyncReleaseList(port);
1248         UsbdRequestASyncReleaseList(port);
1249         UsbdBulkASyncListReleasePort(port);
1250     }
1251     return ret;
1252 }
1253 
BulkTransferRead(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & data)1254 int32_t UsbImpl::BulkTransferRead(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data)
1255 {
1256     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1257     if (port == nullptr) {
1258         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1259         return HDF_DEV_ERR_NO_DEVICE;
1260     }
1261 
1262     uint8_t tbuf[READ_BUF_SIZE] = {0};
1263     uint32_t tsize = READ_BUF_SIZE;
1264     uint32_t actlength = 0;
1265     UsbdRequestSync *requestSync = nullptr;
1266     int32_t ret = UsbdFindRequestSyncAndCreat(port, pipe.intfId, pipe.endpointId, &requestSync);
1267     if (ret != HDF_SUCCESS || requestSync == nullptr) {
1268         HDF_LOGE("%{public}s:UsbdFindRequestSyncAndCreat failed.", __func__);
1269         return ret;
1270     }
1271     if (requestSync->pipe.pipeDirection != USB_PIPE_DIRECTION_IN || requestSync->pipe.pipeType != USB_PIPE_TYPE_BULK) {
1272         HDF_LOGE("%{public}s:invalid param", __func__);
1273         return HDF_ERR_INVALID_PARAM;
1274     }
1275     ret = UsbdBulkReadSyncBase(timeout, tbuf, tsize, &actlength, requestSync);
1276     if (ret != HDF_SUCCESS) {
1277         HDF_LOGW("%{public}s:UsbdBulkReadSyncBase ret:%{public}d, actlength:%{public}u", __func__, ret, actlength);
1278     }
1279 
1280     if (actlength > 0) {
1281         data.assign(tbuf, tbuf + actlength);
1282         ret = HDF_SUCCESS;
1283     }
1284     return ret;
1285 }
1286 
BulkTransferWrite(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,const std::vector<uint8_t> & data)1287 int32_t UsbImpl::BulkTransferWrite(
1288     const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data)
1289 {
1290     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1291     if (port == nullptr) {
1292         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1293         return HDF_DEV_ERR_NO_DEVICE;
1294     }
1295 
1296     UsbdRequestSync *requestSync = nullptr;
1297     int32_t ret = UsbdFindRequestSyncAndCreat(port, pipe.intfId, pipe.endpointId, &requestSync);
1298     if (ret != HDF_SUCCESS || requestSync == nullptr) {
1299         HDF_LOGE("%{public}s:read timeout error", __func__);
1300         return ret;
1301     }
1302     if (requestSync->pipe.pipeDirection != USB_PIPE_DIRECTION_OUT || requestSync->pipe.pipeType != USB_PIPE_TYPE_BULK) {
1303         HDF_LOGE("%{public}s:invalid param", __func__);
1304         return HDF_ERR_INVALID_PARAM;
1305     }
1306     ret = UsbdBulkWriteSyncBase(port, requestSync, data.data(), data.size(), timeout);
1307     return ret;
1308 }
1309 
ControlTransferRead(const UsbDev & dev,const UsbCtrlTransfer & ctrl,std::vector<uint8_t> & data)1310 int32_t UsbImpl::ControlTransferRead(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &data)
1311 {
1312     if ((static_cast<uint32_t>(ctrl.requestType) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
1313         HDF_LOGE("%{public}s: this function is read, not write", __func__);
1314         return HDF_FAILURE;
1315     }
1316 
1317     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1318     if (port == nullptr || port->ctrDevHandle == nullptr) {
1319         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1320         return HDF_DEV_ERR_NO_DEVICE;
1321     }
1322 
1323     UsbControlParams controlParams;
1324     if (memset_s(&controlParams, sizeof(controlParams), 0, sizeof(controlParams)) != EOK) {
1325         HDF_LOGE("%{public}s:memset_s failed ", __func__);
1326         return HDF_FAILURE;
1327     }
1328     controlParams.request = static_cast<uint8_t>(ctrl.requestCmd);
1329     controlParams.value = ctrl.value;
1330     controlParams.index = ctrl.index;
1331     controlParams.target = (UsbRequestTargetType)(static_cast<uint32_t>(ctrl.requestType) & USB_RECIP_MASK);
1332     controlParams.directon = (UsbRequestDirection)(((static_cast<uint32_t>(ctrl.requestType)) >> DIRECTION_OFFSET_7) &
1333         ENDPOINT_DIRECTION_MASK);
1334     controlParams.reqType =
1335         (UsbControlRequestType)((static_cast<uint32_t>(ctrl.requestType) >> CMD_OFFSET_5) & CMD_TYPE_MASK);
1336     controlParams.size = MAX_CONTROL_BUFF_SIZE;
1337     controlParams.data = static_cast<void *>(OsalMemCalloc(controlParams.size));
1338     if (controlParams.data == nullptr) {
1339         HDF_LOGE("%{public}s:OsalMemCalloc failed", __func__);
1340         return HDF_ERR_MALLOC_FAIL;
1341     }
1342     int32_t ret = UsbControlTransferEx(port, &controlParams, ctrl.timeout);
1343     if (ret != HDF_SUCCESS) {
1344         HDF_LOGE("%{public}s:%{public}d UsbControlTransfer failed ret:%{public}d\n", __func__, __LINE__, ret);
1345     }
1346 
1347     uint8_t *dataValue = static_cast<uint8_t *>(controlParams.data);
1348     data.assign(dataValue, dataValue + controlParams.size);
1349     OsalMemFree(controlParams.data);
1350     return ret;
1351 }
1352 
ControlTransferWrite(const UsbDev & dev,const UsbCtrlTransfer & ctrl,const std::vector<uint8_t> & data)1353 int32_t UsbImpl::ControlTransferWrite(const UsbDev &dev, const UsbCtrlTransfer &ctrl, const std::vector<uint8_t> &data)
1354 {
1355     if ((static_cast<uint32_t>(ctrl.requestType) & USB_ENDPOINT_DIR_MASK) != USB_ENDPOINT_DIR_OUT) {
1356         HDF_LOGE("%{public}s: this function is write, not read", __func__);
1357         return HDF_FAILURE;
1358     }
1359 
1360     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1361     if (port == nullptr || port->ctrDevHandle == nullptr) {
1362         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1363         return HDF_DEV_ERR_NO_DEVICE;
1364     }
1365 
1366     UsbControlParams controlParams;
1367     if (memset_s(&controlParams, sizeof(controlParams), 0, sizeof(controlParams)) != EOK) {
1368         HDF_LOGE("%{public}s:memset_s failed ", __func__);
1369         return HDF_FAILURE;
1370     }
1371     controlParams.request = static_cast<uint8_t>(ctrl.requestCmd);
1372     controlParams.value = ctrl.value;
1373     controlParams.index = ctrl.index;
1374     controlParams.target = (UsbRequestTargetType)(static_cast<uint32_t>(ctrl.requestType) & USB_RECIP_MASK);
1375     controlParams.directon = (UsbRequestDirection)(((static_cast<uint32_t>(ctrl.requestType)) >> DIRECTION_OFFSET_7) &
1376         ENDPOINT_DIRECTION_MASK);
1377     controlParams.reqType =
1378         (UsbControlRequestType)((static_cast<uint32_t>(ctrl.requestType) >> CMD_OFFSET_5) & CMD_TYPE_MASK);
1379     controlParams.size = data.size();
1380     controlParams.data = static_cast<void *>(const_cast<uint8_t *>(data.data()));
1381     int32_t ret = UsbControlTransferEx(port, &controlParams, ctrl.timeout);
1382     if (ret != HDF_SUCCESS) {
1383         HDF_LOGE("%{public}s:%{public}d UsbControlTransfer failed ret:%{public}d\n", __func__, __LINE__, ret);
1384     }
1385     return ret;
1386 }
1387 
InterruptTransferRead(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & data)1388 int32_t UsbImpl::InterruptTransferRead(
1389     const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data)
1390 {
1391     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1392     if (port == nullptr) {
1393         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1394         return HDF_DEV_ERR_NO_DEVICE;
1395     }
1396 
1397     uint8_t tbuf[READ_BUF_SIZE] = {0};
1398     uint32_t tsize = READ_BUF_SIZE;
1399     uint32_t actlength = 0;
1400     UsbdRequestSync *requestSync = nullptr;
1401     int32_t ret = UsbdFindRequestSyncAndCreat(port, pipe.intfId, pipe.endpointId, &requestSync);
1402     if (ret != HDF_SUCCESS || requestSync == nullptr) {
1403         HDF_LOGE("%{public}s:read timeout error", __func__);
1404         return ret;
1405     }
1406     if (requestSync->pipe.pipeDirection != USB_PIPE_DIRECTION_IN || requestSync->pipe.pipeType != USB_PIPE_TYPE_BULK) {
1407         HDF_LOGE("%{public}s:invalid param", __func__);
1408         return HDF_ERR_INVALID_PARAM;
1409     }
1410     ret = UsbdBulkReadSyncBase(timeout, tbuf, tsize, &actlength, requestSync);
1411     if (ret != HDF_SUCCESS) {
1412         HDF_LOGW("%{public}s:UsbdBulkReadSyncBase ret:%{public}d, actlength:%{public}u", __func__, ret, actlength);
1413     }
1414 
1415     if (actlength > 0) {
1416         data.assign(tbuf, tbuf + actlength);
1417         ret = HDF_SUCCESS;
1418     }
1419     return ret;
1420 }
1421 
InterruptTransferWrite(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,const std::vector<uint8_t> & data)1422 int32_t UsbImpl::InterruptTransferWrite(
1423     const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data)
1424 {
1425     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1426     if (port == nullptr) {
1427         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1428         return HDF_DEV_ERR_NO_DEVICE;
1429     }
1430 
1431     UsbdRequestSync *requestSync = nullptr;
1432     int32_t ret = UsbdFindRequestSyncAndCreat(port, pipe.intfId, pipe.endpointId, &requestSync);
1433     if (ret != HDF_SUCCESS || requestSync == nullptr) {
1434         HDF_LOGE("%{public}s:read timeout error", __func__);
1435         return ret;
1436     }
1437     if (requestSync->pipe.pipeDirection != USB_PIPE_DIRECTION_OUT || requestSync->pipe.pipeType != USB_PIPE_TYPE_BULK) {
1438         HDF_LOGE("%{public}s:invalid param", __func__);
1439         return HDF_ERR_INVALID_PARAM;
1440     }
1441     ret = UsbdBulkWriteSyncBase(port, requestSync, data.data(), data.size(), timeout);
1442     return ret;
1443 }
1444 
IsoTransferRead(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & data)1445 int32_t UsbImpl::IsoTransferRead(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data)
1446 {
1447     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1448     if (port == nullptr) {
1449         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1450         return HDF_DEV_ERR_NO_DEVICE;
1451     }
1452 
1453     uint8_t tbuf[READ_BUF_SIZE] = {0};
1454     uint32_t tsize = READ_BUF_SIZE;
1455     uint32_t actlength = 0;
1456     UsbdRequestSync *requestSync = nullptr;
1457     int32_t ret = UsbdFindRequestSyncAndCreat(port, pipe.intfId, pipe.endpointId, &requestSync);
1458     if (ret != HDF_SUCCESS || requestSync == nullptr) {
1459         HDF_LOGE("%{public}s:UsbdFindRequestSyncAndCreat failed of requestSync is nullptr", __func__);
1460         return ret;
1461     }
1462 
1463     if (requestSync->pipe.pipeDirection != USB_PIPE_DIRECTION_IN || requestSync->pipe.pipeType != USB_PIPE_TYPE_BULK) {
1464         HDF_LOGE("%{public}s:invalid param", __func__);
1465         return HDF_ERR_INVALID_PARAM;
1466     }
1467 
1468     ret = UsbdBulkReadSyncBase(timeout, tbuf, tsize, &actlength, requestSync);
1469     if (ret != HDF_SUCCESS) {
1470         HDF_LOGW("%{public}s:UsbdBulkReadSyncBase failed, ret:%{public}d, len:%{public}u", __func__, ret, actlength);
1471     }
1472 
1473     if (actlength > 0) {
1474         data.assign(tbuf, tbuf + actlength);
1475         ret = HDF_SUCCESS;
1476     }
1477     return ret;
1478 }
1479 
IsoTransferWrite(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,const std::vector<uint8_t> & data)1480 int32_t UsbImpl::IsoTransferWrite(
1481     const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data)
1482 {
1483     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1484     if (port == nullptr) {
1485         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1486         return HDF_DEV_ERR_NO_DEVICE;
1487     }
1488 
1489     UsbdRequestSync *requestSync = nullptr;
1490     int32_t ret = UsbdFindRequestSyncAndCreat(port, pipe.intfId, pipe.endpointId, &requestSync);
1491     if (ret != HDF_SUCCESS || requestSync == nullptr) {
1492         HDF_LOGE("%{public}s:read timeout error", __func__);
1493         return ret;
1494     }
1495 
1496     if (requestSync->pipe.pipeDirection != USB_PIPE_DIRECTION_OUT || requestSync->pipe.pipeType != USB_PIPE_TYPE_BULK) {
1497         HDF_LOGE("%{public}s:invalid param", __func__);
1498         return HDF_ERR_INVALID_PARAM;
1499     }
1500 
1501     ret = UsbdBulkWriteSyncBase(port, requestSync, data.data(), data.size(), timeout);
1502     return ret;
1503 }
1504 
RequestQueue(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & clientData,const std::vector<uint8_t> & buffer)1505 int32_t UsbImpl::RequestQueue(
1506     const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData, const std::vector<uint8_t> &buffer)
1507 {
1508     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1509     if (port == nullptr) {
1510         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1511         return HDF_DEV_ERR_NO_DEVICE;
1512     }
1513 
1514     UsbdRequestASync *reqAsync = UsbdFindRequestASync(port, pipe.intfId, pipe.endpointId);
1515     if (reqAsync == nullptr) {
1516         HDF_LOGE("%{public}s:UsbdFindRequestASync failed", __func__);
1517         return HDF_ERR_DEVICE_BUSY;
1518     }
1519 
1520     uint8_t *clientDataAddr = nullptr;
1521     int32_t ret = UsbdDispatcher::UsbdMallocAndFill(clientDataAddr, clientData);
1522     if (ret != HDF_SUCCESS) {
1523         HDF_LOGE("%{public}s:clientDataAddr UsbdMallocAndFill failed", __func__);
1524         return HDF_FAILURE;
1525     }
1526 
1527     uint8_t *bufferAddr = nullptr;
1528     ret = UsbdDispatcher::UsbdMallocAndFill(bufferAddr, buffer);
1529     if (ret != HDF_SUCCESS) {
1530         OsalMemFree(clientDataAddr);
1531         clientDataAddr = nullptr;
1532         HDF_LOGE("%{public}s:bufferAddr UsbdMallocAndFill failed", __func__);
1533         return HDF_FAILURE;
1534     }
1535 
1536     reqAsync->reqMsg.clientData = static_cast<void *>(clientDataAddr);
1537     reqAsync->reqMsg.clientLength = sizeof(uint8_t) * clientData.size();
1538     ret = FunRequestQueueFillAndSubmit(port, reqAsync, bufferAddr, sizeof(uint8_t) * buffer.size());
1539     if (ret != HDF_SUCCESS) {
1540         HDF_LOGE("%{public}s:FunRequestQueueFillAndSubmit failed:%{public}d", __func__, ret);
1541         OsalMemFree(clientDataAddr);
1542         clientDataAddr = nullptr;
1543     }
1544 
1545     OsalMemFree(bufferAddr);
1546     bufferAddr = nullptr;
1547     return ret;
1548 }
1549 
RequestWait(const UsbDev & dev,std::vector<uint8_t> & clientData,std::vector<uint8_t> & buffer,int32_t timeout)1550 int32_t UsbImpl::RequestWait(
1551     const UsbDev &dev, std::vector<uint8_t> &clientData, std::vector<uint8_t> &buffer, int32_t timeout)
1552 {
1553     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1554     if (port == nullptr) {
1555         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1556         return HDF_DEV_ERR_NO_DEVICE;
1557     }
1558 
1559     UsbdRequestASync *reqMsg = nullptr;
1560     int32_t ret = GetRequestMsgFromQueue(port, &reqMsg);
1561     if (ret != HDF_SUCCESS || reqMsg == nullptr || reqMsg->reqMsg.request == nullptr) {
1562         HDF_LOGE("%{public}s:GetRequestMsgFromQueue error:%{public}d", __func__, ret);
1563         return ret;
1564     }
1565 
1566     UsbIfRequest *reqValue = reinterpret_cast<UsbIfRequest *>(reqMsg->reqMsg.request);
1567     if (static_cast<int32_t>(reqMsg->reqMsg.request->compInfo.status) == -1) {
1568         ret = OsalSemWait(&reqValue->hostRequest->sem, timeout);
1569         if (ret != HDF_SUCCESS) {
1570             HDF_LOGE("%{public}s:OsalSemWait failed, ret=%{public}d", __func__, ret);
1571             OsalMutexLock(&port->requestLock);
1572             HdfSListAdd(&port->requestQueue, &reqMsg->qNode);
1573             OsalMutexUnlock(&port->requestLock);
1574             return ret;
1575         }
1576     }
1577 
1578     uint32_t length = 0;
1579     uint8_t *buf = nullptr;
1580     ret = GetRequestMsgData(port, reqMsg, timeout, &buf, &length);
1581     if (ret != HDF_SUCCESS) {
1582         HDF_LOGE("%{public}s:GetRequestMsgData failed:%{public}d", __func__, ret);
1583         return ret;
1584     }
1585 
1586     uint8_t *clientDataAddr = static_cast<uint8_t *>(reqMsg->reqMsg.clientData);
1587     uint8_t *bufferAddr = static_cast<uint8_t *>(reqMsg->reqMsg.request->compInfo.buffer);
1588     clientData.assign(clientDataAddr, clientDataAddr + reqMsg->reqMsg.clientLength);
1589     buffer.assign(bufferAddr, bufferAddr + reqMsg->reqMsg.request->compInfo.length);
1590     UsbdRequestASyncReleaseData(reqMsg);
1591     return ret;
1592 }
1593 
RequestCancel(const UsbDev & dev,const UsbPipe & pipe)1594 int32_t UsbImpl::RequestCancel(const UsbDev &dev, const UsbPipe &pipe)
1595 {
1596     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1597     if (port == nullptr) {
1598         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1599         return HDF_DEV_ERR_NO_DEVICE;
1600     }
1601 
1602     UsbdRequestASync *reqMsg = nullptr;
1603     while (GetRequestMsgFromQueue(port, &reqMsg) == HDF_SUCCESS) {
1604         if (reqMsg) {
1605             int32_t ret = UsbCancelRequest(reqMsg->reqMsg.request);
1606             if (ret != HDF_SUCCESS) {
1607                 HDF_LOGW("%{public}s:request cancel failed = %{public}d", __func__, ret);
1608             }
1609             OsalMutexLock(&reqMsg->lock);
1610             UsbdRequestASyncReleaseData(reqMsg);
1611             reqMsg->status = 0;
1612             OsalMutexUnlock(&reqMsg->lock);
1613         }
1614     }
1615     return HDF_SUCCESS;
1616 }
1617 
GetCurrentFunctions(int32_t & funcs)1618 int32_t UsbImpl::GetCurrentFunctions(int32_t &funcs)
1619 {
1620     funcs = UsbdFunction::UsbdGetFunction();
1621     return HDF_SUCCESS;
1622 }
1623 
SetCurrentFunctions(int32_t funcs)1624 int32_t UsbImpl::SetCurrentFunctions(int32_t funcs)
1625 {
1626     if (!isGadgetConnected_) {
1627         HDF_LOGE("%{public}s:gadget is not connected", __func__);
1628         return HDF_DEV_ERR_NO_DEVICE;
1629     }
1630     OsalMutexLock(&lock_);
1631     int32_t ret = UsbdFunction::UsbdSetFunction(funcs);
1632     if (ret != HDF_SUCCESS) {
1633         HDF_LOGE("%{public}s:UsbdSetFunction failed, ret:%{public}d", __func__, ret);
1634         OsalMutexUnlock(&lock_);
1635         return ret;
1636     }
1637     OsalMutexUnlock(&lock_);
1638     return HDF_SUCCESS;
1639 }
1640 
parsePortPath()1641 void UsbImpl::parsePortPath()
1642 {
1643     const char *path_ = nullptr;
1644     const char *pathDef_ = nullptr;
1645     struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
1646     if (iface == nullptr) {
1647         HDF_LOGE("%{public}s: DeviceResourceGetIfaceInstance failed\n", __func__);
1648         return;
1649     }
1650 
1651     if (device_ == nullptr) {
1652         HDF_LOGE("%{public}s: device_ is empty\n", __func__);
1653         return;
1654     }
1655     if (iface->GetString(device_->property, "port_file_path", &path_, pathDef_) != HDF_SUCCESS) {
1656         HDF_LOGE("%{public}s: read port_file_path failed", __func__);
1657         return;
1658     }
1659     UsbdPort::GetInstance().setPortPath(path_);
1660     return;
1661 }
1662 
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)1663 int32_t UsbImpl::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
1664 {
1665     int32_t ret = UsbdPort::GetInstance().SetPort(portId, powerRole, dataRole, subscribers_, MAX_SUBSCRIBER);
1666     if (ret != HDF_SUCCESS) {
1667         HDF_LOGE("%{public}s:FunSetRole failed, ret:%{public}d", __func__, ret);
1668         return ret;
1669     }
1670 
1671     return HDF_SUCCESS;
1672 }
1673 
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)1674 int32_t UsbImpl::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
1675 {
1676     int32_t ret = UsbdPort::GetInstance().QueryPort(portId, powerRole, dataRole, mode);
1677     if (ret != HDF_SUCCESS) {
1678         HDF_LOGE("%{public}s:QueryPort failed, ret:%{public}d", __func__, ret);
1679         return ret;
1680     }
1681 
1682     return HDF_SUCCESS;
1683 }
1684 
BindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)1685 int32_t UsbImpl::BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
1686 {
1687     int32_t i;
1688     if (subscriber == nullptr) {
1689         HDF_LOGE("%{public}s:subscriber is  null", __func__);
1690         return HDF_ERR_INVALID_PARAM;
1691     }
1692     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<IUsbdSubscriber>(subscriber);
1693     for (i = 0; i < MAX_SUBSCRIBER; i++) {
1694         if (subscribers_[i].remote == remote) {
1695             break;
1696         }
1697     }
1698     if (i < MAX_SUBSCRIBER) {
1699         HDF_LOGI("%{public}s: current subscriber was bind", __func__);
1700         return HDF_SUCCESS;
1701     }
1702     for (i = 0; i < MAX_SUBSCRIBER; i++) {
1703         if (subscribers_[i].subscriber == nullptr) {
1704             subscribers_[i].subscriber = subscriber;
1705             subscribers_[i].impl = this;
1706             subscribers_[i].usbPnpListener.callBack = UsbdPnpLoaderEventReceived;
1707             subscribers_[i].usbPnpListener.priv = &subscribers_[i];
1708             subscribers_[i].remote = remote;
1709             subscribers_[i].deathRecipient = new UsbImpl::UsbDeathRecipient(subscriber);
1710             if (subscribers_[i].deathRecipient == nullptr) {
1711                 HDF_LOGE("%{public}s: new deathRecipient failed", __func__);
1712                 return HDF_FAILURE;
1713             }
1714             bool result = subscribers_[i].remote->AddDeathRecipient(
1715                 static_cast<UsbDeathRecipient *>(subscribers_[i].deathRecipient));
1716             if (!result) {
1717                 HDF_LOGE("%{public}s:AddUsbDeathRecipient failed", __func__);
1718                 return HDF_FAILURE;
1719             }
1720 
1721             HDF_LOGI("%{public}s: index = %{public}d", __func__, i);
1722             break;
1723         }
1724     }
1725     if (i == MAX_SUBSCRIBER) {
1726         HDF_LOGE("%{public}s: too many listeners", __func__);
1727         return HDF_ERR_OUT_OF_RANGE;
1728     }
1729 
1730     if (DdkListenerMgrAdd(&subscribers_[i].usbPnpListener) != HDF_SUCCESS) {
1731         HDF_LOGE("%{public}s: register listerer failed", __func__);
1732         return HDF_FAILURE;
1733     }
1734     return HDF_SUCCESS;
1735 }
1736 
UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)1737 int32_t UsbImpl::UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
1738 {
1739     if (subscriber == nullptr) {
1740         HDF_LOGE("%{public}s:subscriber is  null", __func__);
1741         return HDF_ERR_INVALID_PARAM;
1742     }
1743     int32_t i;
1744     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<IUsbdSubscriber>(subscriber);
1745     for (i = 0; i < MAX_SUBSCRIBER; i++) {
1746         if (subscribers_[i].remote == remote) {
1747             break;
1748         }
1749     }
1750     if (i == MAX_SUBSCRIBER) {
1751         HDF_LOGE("%{public}s: current subscriber not bind", __func__);
1752         return HDF_DEV_ERR_NO_DEVICE;
1753     }
1754     bool result = remote->RemoveDeathRecipient(static_cast<UsbDeathRecipient *>(subscribers_[i].deathRecipient));
1755     if (!result) {
1756         HDF_LOGE("%{public}s:RemoveUsbDeathRecipient failed", __func__);
1757         return HDF_FAILURE;
1758     }
1759 
1760     subscribers_[i].subscriber = nullptr;
1761     subscribers_[i].remote = nullptr;
1762     subscribers_[i].deathRecipient = nullptr;
1763     if (DdkListenerMgrRemove(&subscribers_[i].usbPnpListener) != HDF_SUCCESS) {
1764         HDF_LOGE("%{public}s: remove listerer failed", __func__);
1765         return HDF_FAILURE;
1766     }
1767     return HDF_SUCCESS;
1768 }
1769 
OnRemoteDied(const wptr<IRemoteObject> & object)1770 void UsbImpl::UsbDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
1771 {
1772     int32_t i;
1773     for (i = 0; i < MAX_SUBSCRIBER; i++) {
1774         if (UsbImpl::subscribers_[i].subscriber == deathSubscriber_) {
1775             break;
1776         }
1777     }
1778     if (i == MAX_SUBSCRIBER) {
1779         HDF_LOGE("%{public}s: current subscriber not bind", __func__);
1780         return;
1781     }
1782     UsbImpl::subscribers_[i].subscriber = nullptr;
1783     subscribers_[i].remote = nullptr;
1784     subscribers_[i].deathRecipient = nullptr;
1785     if (DdkListenerMgrRemove(&UsbImpl::subscribers_[i].usbPnpListener) != HDF_SUCCESS) {
1786         HDF_LOGE("%{public}s: remove listerer failed", __func__);
1787     }
1788 }
1789 
RegBulkCallback(const UsbDev & dev,const UsbPipe & pipe,const sptr<IUsbdBulkCallback> & cb)1790 int32_t UsbImpl::RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IUsbdBulkCallback> &cb)
1791 {
1792     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1793     if (port == nullptr) {
1794         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1795         return HDF_DEV_ERR_NO_DEVICE;
1796     }
1797 
1798     UsbdBulkASyncList *list = UsbdBulkASyncListInit(port, pipe.intfId, pipe.endpointId);
1799     if (list == nullptr) {
1800         HDF_LOGE("%{public}s:UsbdBulkASyncListFind failed", __func__);
1801         return HDF_ERR_MALLOC_FAIL;
1802     }
1803     list->cb = cb;
1804     if (list->cb == nullptr) {
1805         HDF_LOGE("%{public}s:get callback error", __func__);
1806         return HDF_ERR_IO;
1807     }
1808 
1809     return HDF_SUCCESS;
1810 }
1811 
UnRegBulkCallback(const UsbDev & dev,const UsbPipe & pipe)1812 int32_t UsbImpl::UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe)
1813 {
1814     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1815     if (port == nullptr) {
1816         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1817         return HDF_DEV_ERR_NO_DEVICE;
1818     }
1819 
1820     UsbdBulkASyncList *list = UsbdBulkASyncListFind(port, pipe.intfId, pipe.endpointId);
1821     if (list == nullptr) {
1822         HDF_LOGE("%{public}s:UsbdBulkASyncListFind failed", __func__);
1823         return HDF_FAILURE;
1824     }
1825     list->cb = nullptr;
1826     return HDF_SUCCESS;
1827 }
1828 
BulkRead(const UsbDev & dev,const UsbPipe & pipe,const sptr<Ashmem> & ashmem)1829 int32_t UsbImpl::BulkRead(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem)
1830 {
1831     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1832     if (port == nullptr) {
1833         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1834         return HDF_DEV_ERR_NO_DEVICE;
1835     }
1836 
1837     UsbdBulkASyncList *list = UsbdBulkASyncListInit(port, pipe.intfId, pipe.endpointId);
1838     if (list == nullptr) {
1839         HDF_LOGE("%{public}s:UsbdBulkASyncListInit failed.", __func__);
1840         return HDF_ERR_MALLOC_FAIL;
1841     }
1842 
1843     int32_t ret = InitAsmBufferHandle(&list->asmHandle, ashmem->GetAshmemFd(), ashmem->GetAshmemSize());
1844     if (ret != HDF_SUCCESS) {
1845         HDF_LOGE("%{public}s:InitAsmBufferHandle failed ret:%{public}d ashmem fd:%{public}d size:%{public}d", __func__,
1846             ret, ashmem->GetAshmemFd(), ashmem->GetAshmemSize());
1847         return ret;
1848     }
1849 
1850     ret = UsbdBulkReadASyncSubmitStart(list);
1851     if (ret == HDF_DEV_ERR_NODATA || ret == HDF_DEV_ERR_NO_MEMORY || ret == HDF_ERR_DEVICE_BUSY) {
1852         ret = HDF_SUCCESS;
1853     }
1854     if (ret != HDF_SUCCESS) {
1855         HDF_LOGE("%{public}s:UsbdBulkReadASyncSubmitStart error ret:%{public}d", __func__, ret);
1856         return ret;
1857     }
1858 
1859     return ret;
1860 }
1861 
BulkWrite(const UsbDev & dev,const UsbPipe & pipe,const sptr<Ashmem> & ashmem)1862 int32_t UsbImpl::BulkWrite(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem)
1863 {
1864     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1865     if (port == nullptr) {
1866         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1867         return HDF_DEV_ERR_NO_DEVICE;
1868     }
1869 
1870     UsbdBulkASyncList *list = UsbdBulkASyncListInit(port, pipe.intfId, pipe.endpointId);
1871     if (list == nullptr) {
1872         HDF_LOGE("%{public}s:UsbdBulkASyncListInit failed.", __func__);
1873         return HDF_ERR_MALLOC_FAIL;
1874     }
1875 
1876     int32_t ret = InitAsmBufferHandle(&list->asmHandle, ashmem->GetAshmemFd(), ashmem->GetAshmemSize());
1877     if (ret != HDF_SUCCESS) {
1878         HDF_LOGE("%{public}s:InitAsmBufferHandle failed ret:%{public}d ashmem fd:%{public}d size:%{public}d", __func__,
1879             ret, ashmem->GetAshmemFd(), ashmem->GetAshmemSize());
1880         return ret;
1881     }
1882 
1883     ret = UsbdBulkASyncWriteSubmitStart(list);
1884     if (ret == HDF_DEV_ERR_NODATA || ret == HDF_DEV_ERR_NO_MEMORY || ret == HDF_ERR_DEVICE_BUSY) {
1885         ret = HDF_SUCCESS;
1886     }
1887     if (ret != HDF_SUCCESS) {
1888         HDF_LOGE("%{public}s:UsbdBulkASyncWriteSubmitStart error ret:%{public}d", __func__, ret);
1889         return ret;
1890     }
1891 
1892     return ret;
1893 }
1894 
BulkCancel(const UsbDev & dev,const UsbPipe & pipe)1895 int32_t UsbImpl::BulkCancel(const UsbDev &dev, const UsbPipe &pipe)
1896 {
1897     HostDevice *port = FindDevFromService(dev.busNum, dev.devAddr);
1898     if (port == nullptr) {
1899         HDF_LOGE("%{public}s:FindDevFromService failed", __func__);
1900         return HDF_DEV_ERR_NO_DEVICE;
1901     }
1902 
1903     UsbdBulkASyncList *list = UsbdBulkASyncListFind(port, pipe.intfId, pipe.endpointId);
1904     if (list == nullptr) {
1905         HDF_LOGW("%{public}s:UsbdBulkASyncListFind failed interfaceId:%{public}u endpointId:%{public}u", __func__,
1906             pipe.intfId, pipe.endpointId);
1907         return HDF_FAILURE;
1908     }
1909     sptr<IUsbdBulkCallback> tcb = list->cb;
1910     list->cb = nullptr;
1911     ReleaseAsmBufferHandle(&list->asmHandle);
1912     BulkRequestCancel(list);
1913     list->cb = tcb;
1914     return HDF_SUCCESS;
1915 }
1916 } // namespace V1_0
1917 } // namespace Usb
1918 } // namespace HDI
1919 } // namespace OHOS
1920