1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "liteos_adapter.h"
17 #include "los_spinlock.h"
18 #ifdef LOSCFG_DRIVERS_HDF_USB_PNP_NOTIFY
19 #include "usb_pnp_notify.h"
20 #endif
21 #include "usbd_wrapper.h"
22
23 #define HDF_LOG_TAG USB_LITEOS_ADAPTER
24
25 #define PATH_LEN 24
26 #define DESC_READ_LEN 256
27 #define EP_NUM_MAX 30
28
29 static bool g_CompleteExit;
30
31 enum UrbState {
32 URB_INIT_STATE,
33 URB_SUBMIT_STATE,
34 URB_REAP_STATE,
35 };
36
37 struct Async {
38 struct DListHead asynclist;
39 enum UrbState state;
40 struct UsbDevice *dev;
41 UsbAdapterUrb urb;
42 };
43
44 struct OsDev {
45 struct DListHead asyncCompleted;
46 UsbAdapterDevice *adapterDevice;
47 SPIN_LOCK_S completeLock;
48 struct OsalMutex completeMux;
49 struct OsalSem cvWait;
50 };
51
OsAdapterRealloc(void * ptr,size_t oldSize,size_t newSize)52 static void *OsAdapterRealloc(void *ptr, size_t oldSize, size_t newSize)
53 {
54 void *mem;
55
56 mem = RawUsbMemAlloc(newSize);
57 if (mem == NULL) {
58 HDF_LOGE("%{public}s:%{public}d", __func__, __LINE__);
59 goto OUT;
60 }
61
62 if (oldSize > 0) {
63 if (memmove_s(mem, newSize, ptr, oldSize) != HDF_SUCCESS) {
64 HDF_LOGE("%{public}s:%{public}d", __func__, __LINE__);
65 RawUsbMemFree(mem);
66 mem = NULL;
67 goto OUT;
68 }
69 }
70
71 RawUsbMemFree(ptr);
72 ptr = NULL;
73 OUT:
74 return mem;
75 }
76
UsbFindUrb(const struct Async * urb,struct DListHead * list)77 static bool UsbFindUrb(const struct Async *urb, struct DListHead *list)
78 {
79 bool findFlag = false;
80 struct OsDev *osDev = CONTAINER_OF(list, struct OsDev, asyncCompleted);
81 struct Async *asPos = NULL;
82 struct Async *asTemp = NULL;
83 if (DListIsEmpty(&osDev->asyncCompleted)) {
84 return false;
85 }
86 DLIST_FOR_EACH_ENTRY_SAFE(asPos, asTemp, list, struct Async, asynclist) {
87 if (asPos == urb) {
88 findFlag = true;
89 break;
90 }
91 }
92 return findFlag;
93 }
94
OsUrbComplete(UsbAdapterUrb * urb)95 static void OsUrbComplete(UsbAdapterUrb *urb)
96 {
97 uint32_t save;
98 struct Async *as = CONTAINER_OF(urb, struct Async, urb);
99 struct UsbDevice *dev = as->dev;
100 if (dev == NULL) {
101 DPRINTFN(0, "dev is null\n");
102 return;
103 }
104 struct OsDev *osDev = (struct OsDev *)dev->privateData;
105 if (osDev == NULL) {
106 DPRINTFN(0, "osDev is null\n");
107 return;
108 }
109 as->state = URB_REAP_STATE;
110 LOS_SpinLockSave(&osDev->completeLock, &save);
111 if (UsbFindUrb(as, &osDev->asyncCompleted) == false) {
112 DListInsertTail(&as->asynclist, &osDev->asyncCompleted);
113 LOS_SpinUnlockRestore(&osDev->completeLock, save);
114 OsalSemPost(&osDev->cvWait);
115 } else {
116 LOS_SpinUnlockRestore(&osDev->completeLock, save);
117 }
118 }
119
OsSubmitUrb(UsbAdapterUrb * urb,UsbAdapterDevice * adapterDevice,UsbAdapterHostEndpoint * uhe)120 static int32_t OsSubmitUrb(UsbAdapterUrb *urb, UsbAdapterDevice *adapterDevice, UsbAdapterHostEndpoint *uhe)
121 {
122 int32_t err;
123
124 if ((uhe == NULL) || (urb == NULL) || (adapterDevice == NULL)) {
125 DPRINTFN(0, "invalid param\n");
126 return (-EINVAL);
127 }
128
129 err = usb_setup_endpoint(adapterDevice, uhe, 1024);
130 if (err < 0) {
131 DPRINTFN(0, "setup failed err:%d\n", err);
132 return (err);
133 }
134 err = usb_submit_urb(urb, 0);
135 return (err);
136 }
137
OsControlMsg(UsbAdapterUrb * urb)138 static int32_t OsControlMsg(UsbAdapterUrb *urb)
139 {
140 uint16_t actLen = 0;
141 UsbAdapterDevice *adapterDevice = urb->dev;
142 void *buffer = urb->transfer_buffer;
143 UsbAdapterDeviceRequest req;
144
145 int32_t err = memcpy_s(&req, sizeof(UsbAdapterDeviceRequest), buffer, sizeof(UsbAdapterDeviceRequest));
146 if (err != EOK) {
147 DPRINTFN(0, "%s:%d err=%d\n", __func__, __LINE__, err);
148 err = HDF_ERR_IO;
149 return err;
150 }
151 err = usbd_do_request_flags(
152 adapterDevice, NULL, &req, (char *)buffer + sizeof(req), USB_SHORT_XFER_OK, &actLen, urb->timeout);
153 if (err) {
154 DPRINTFN(1, "OsControlMsg!err:%d\n", err);
155 }
156 urb->actual_length = actLen;
157 if (urb->complete) {
158 (urb->complete)(urb);
159 }
160 return err;
161 }
162
OsWaitUrb(struct OsDev * osDev)163 static int32_t OsWaitUrb(struct OsDev *osDev)
164 {
165 if (osDev == NULL) {
166 return HDF_FAILURE;
167 }
168 do {
169 OsalSemWait(&osDev->cvWait, HDF_WAIT_FOREVER);
170 } while (!g_CompleteExit && DListIsEmpty(&osDev->asyncCompleted));
171 return HDF_SUCCESS;
172 }
173
OsReapUrb(const struct UsbDeviceHandle * handle,struct Async ** urb)174 static int32_t OsReapUrb(const struct UsbDeviceHandle *handle, struct Async **urb)
175 {
176 if ((handle == NULL) || (handle->dev == NULL) || (handle->dev->privateData == NULL)) {
177 PRINTK("invalid parmater\n");
178 return HDF_ERR_INVALID_PARAM;
179 }
180 int32_t err = 0;
181 uint32_t save;
182 struct Async *as = NULL;
183 struct UsbDevice *dev = handle->dev;
184 if (dev->privateData == NULL) {
185 PRINTK("invalid parmater\n");
186 return HDF_ERR_INVALID_PARAM;
187 }
188 struct OsDev *osDev = (struct OsDev *)dev->privateData;
189 err = OsWaitUrb(osDev);
190 LOS_SpinLockSave(&osDev->completeLock, &save);
191 if (!DListIsEmpty(&osDev->asyncCompleted)) {
192 as = DLIST_FIRST_ENTRY(&osDev->asyncCompleted, struct Async, asynclist);
193 if (as->asynclist.prev != NULL && as->asynclist.next != NULL) {
194 DListRemove(&as->asynclist);
195 } else {
196 HDF_LOGE("%{public}s: The node prev or next is NULL", __func__);
197 }
198 }
199 LOS_SpinUnlockRestore(&osDev->completeLock, save);
200 *urb = as;
201 return err;
202 }
203
OsDeviceCompare(struct HdfSListNode * listEntry,uint32_t searchKey)204 static bool OsDeviceCompare(struct HdfSListNode *listEntry, uint32_t searchKey)
205 {
206 struct UsbDevice *dev = (struct UsbDevice *)listEntry;
207 if (dev == NULL) {
208 return false;
209 }
210
211 if ((dev->busNum == (searchKey >> BUS_OFFSET)) && (dev->devAddr == (searchKey & 0xFF))) {
212 return true;
213 }
214
215 return false;
216 }
217
OsGetDeviceHandle(struct UsbSession * session,uint8_t busNum,uint8_t usbAddr)218 static struct UsbDeviceHandle *OsGetDeviceHandle(struct UsbSession *session, uint8_t busNum, uint8_t usbAddr)
219 {
220 struct UsbDevice *dev = NULL;
221 struct UsbDeviceHandle *handle = NULL;
222
223 if (session == NULL) {
224 DPRINTFN(0, "%s:invalid param\n", __func__);
225 return NULL;
226 }
227
228 OsalMutexLock(&session->lock);
229 dev = (struct UsbDevice *)HdfSListSearch(&session->usbDevs, (busNum << BUS_OFFSET) | usbAddr, OsDeviceCompare);
230 if (dev != NULL) {
231 handle = dev->devHandle;
232 AdapterAtomicInc(&dev->refcnt);
233 }
234 OsalMutexUnlock(&session->lock);
235
236 return handle;
237 }
238
OsCallocDeviceHandle(void)239 static struct UsbDeviceHandle *OsCallocDeviceHandle(void)
240 {
241 struct UsbDeviceHandle *handle = NULL;
242
243 handle = RawUsbMemCalloc(sizeof(*handle));
244 if (handle == NULL) {
245 DPRINTFN(0, "%s: allocate handle failed", __func__);
246 return NULL;
247 }
248
249 OsalMutexInit(&handle->lock);
250
251 return handle;
252 }
253
OsAllocDevice(struct UsbSession * session,struct UsbDeviceHandle * handle)254 static struct UsbDevice *OsAllocDevice(struct UsbSession *session, struct UsbDeviceHandle *handle)
255 {
256 struct UsbDevice *dev = RawUsbMemCalloc(sizeof(*dev));
257 if (dev == NULL) {
258 DPRINTFN(0, "%s:%d no memory", __func__, __LINE__);
259 return NULL;
260 }
261
262 dev->session = session;
263 dev->devHandle = handle;
264
265 RawRequestListInit(dev);
266
267 handle->dev = dev;
268 return dev;
269 }
270
OsReadDescriptors(struct UsbDevice * dev)271 static int32_t OsReadDescriptors(struct UsbDevice *dev)
272 {
273 size_t allocLen = 0;
274 int32_t ret;
275 if (dev == NULL) {
276 HDF_LOGE("%{public}s:%{public}d dev is NULL!", __func__, __LINE__);
277 return HDF_ERR_INVALID_PARAM;
278 }
279 struct OsDev *osDev = (struct OsDev *)dev->privateData;
280 if ((osDev == NULL) || (osDev->adapterDevice == NULL) || (osDev->adapterDevice->cdesc == NULL)) {
281 HDF_LOGE("%{public}s:%{public}d is NULL!", __func__, __LINE__);
282 return HDF_ERR_INVALID_PARAM;
283 }
284
285 do {
286 size_t oldLen = allocLen;
287 allocLen += DESC_READ_LEN;
288 dev->descriptors = OsAdapterRealloc(dev->descriptors, oldLen, allocLen);
289 if ((dev->descriptors == NULL) || (dev->descriptorsLength > allocLen)) {
290 DPRINTFN(0, "%s:%d\n", __func__, __LINE__);
291 return HDF_ERR_MALLOC_FAIL;
292 }
293 uint8_t *ptr = (uint8_t *)dev->descriptors + dev->descriptorsLength;
294 if (ptr == NULL) {
295 DPRINTFN(0, "%s:%d ptr is NULL\n", __func__, __LINE__);
296 ret = HDF_ERR_INVALID_PARAM;
297 return ret;
298 }
299 ret = memset_s(ptr, DESC_READ_LEN, 0, DESC_READ_LEN);
300 if (ret != HDF_SUCCESS) {
301 HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
302 return HDF_FAILURE;
303 }
304
305 size_t count = UGETW(osDev->adapterDevice->cdesc->wTotalLength);
306 if (count > DESC_READ_LEN) {
307 ret = HDF_ERR_IO;
308 return ret;
309 }
310 ret = memcpy_s(ptr, DESC_READ_LEN, osDev->adapterDevice->cdesc, count);
311 if (ret != EOK) {
312 DPRINTFN(0, "%s:%d ret=%d\n", __func__, __LINE__, ret);
313 ret = HDF_ERR_IO;
314 return ret;
315 }
316 DPRINTFN(0, "%s:+configdes_size:%d+type:%d\n", __func__, UGETW(osDev->adapterDevice->cdesc->wTotalLength),
317 osDev->adapterDevice->cdesc->bDescriptorType);
318 dev->descriptorsLength += count;
319 } while (dev->descriptorsLength == allocLen);
320
321 return HDF_SUCCESS;
322 }
323
OsParseConfigDescriptors(struct UsbDevice * dev)324 static int32_t OsParseConfigDescriptors(struct UsbDevice *dev)
325 {
326 uint8_t i;
327 uint8_t *buffer = NULL;
328 size_t descLen;
329 UsbAdapterDeviceDescriptor *deviceDesc = NULL;
330 struct OsDev *osDev = (struct OsDev *)dev->privateData;
331 deviceDesc = &osDev->adapterDevice->ddesc;
332 uint8_t numConfigs = deviceDesc->bNumConfigurations;
333 if (numConfigs == 0) {
334 return HDF_SUCCESS;
335 }
336 dev->configDescriptors = RawUsbMemAlloc(numConfigs * sizeof(struct UsbDeviceConfigDescriptor));
337 if (dev->configDescriptors == NULL) {
338 DPRINTFN(0, "%s:%d\n", __func__, __LINE__);
339 return HDF_ERR_MALLOC_FAIL;
340 }
341 buffer = (uint8_t *)dev->descriptors;
342 descLen = dev->descriptorsLength;
343
344 for (i = 0; i < numConfigs; i++) {
345 struct UsbConfigDescriptor *configDesc = NULL;
346 uint16_t configLen;
347
348 if (descLen < USB_DDK_DT_CONFIG_SIZE) {
349 DPRINTFN(0, "%s:%d read %zu", __func__, __LINE__, descLen);
350 RawUsbMemFree(dev->configDescriptors);
351 return HDF_ERR_IO;
352 }
353 configDesc = (struct UsbConfigDescriptor *)buffer;
354 if ((configDesc->bDescriptorType != USB_DDK_DT_CONFIG) || (configDesc->bLength < USB_DDK_DT_CONFIG_SIZE)) {
355 DPRINTFN(0, "%s:%d config desc error: type 0x%02x, length %u\n", __func__, __LINE__,
356 configDesc->bDescriptorType, configDesc->bLength);
357 RawUsbMemFree(dev->configDescriptors);
358 return HDF_ERR_IO;
359 }
360 configLen = LE16_TO_CPU(configDesc->wTotalLength);
361 if (configLen < USB_DDK_DT_CONFIG_SIZE) {
362 DPRINTFN(0, "invalid wTotalLength value %u\n", configLen);
363 RawUsbMemFree(dev->configDescriptors);
364 return HDF_ERR_IO;
365 }
366 if (configLen > descLen) {
367 DPRINTFN(0, "%s:%d read %zu/%u\n", __func__, __LINE__, descLen, configLen);
368 configLen = (uint16_t)descLen;
369 }
370 dev->configDescriptors[i].desc = configDesc;
371 dev->configDescriptors[i].actualLen = configLen;
372 buffer += configLen;
373 descLen -= configLen;
374 }
375 return HDF_SUCCESS;
376 }
377
OsDevAllocInit(void)378 static struct OsDev *OsDevAllocInit(void)
379 {
380 struct OsDev *osDev = NULL;
381 osDev = RawUsbMemCalloc(sizeof(*osDev));
382 if (osDev == NULL) {
383 return NULL;
384 }
385 DListHeadInit(&osDev->asyncCompleted);
386 OsalSemInit(&osDev->cvWait, 0);
387 LOS_SpinInit(&osDev->completeLock);
388 OsalMutexInit(&osDev->completeMux);
389 return osDev;
390 }
391
OsDevDestory(struct OsDev * osDev)392 static void OsDevDestory(struct OsDev *osDev)
393 {
394 if (osDev == NULL) {
395 DPRINTFN(0, "invalid parma\n");
396 return;
397 }
398 OsalSemDestroy(&osDev->cvWait);
399 OsalMutexDestroy(&osDev->completeMux);
400 RawUsbMemFree(osDev);
401 }
402
OsInitDevice(struct UsbDevice * dev,uint8_t busNum,uint8_t devAddr)403 static int32_t OsInitDevice(struct UsbDevice *dev, uint8_t busNum, uint8_t devAddr)
404 {
405 struct OsDev *osDev = NULL;
406 int32_t ret;
407
408 dev->busNum = busNum;
409 dev->devAddr = devAddr;
410 osDev = OsDevAllocInit();
411 if (osDev == NULL) {
412 DPRINTFN(0, "%s:%d osDev is NULL\n", __func__, __LINE__);
413 return HDF_DEV_ERR_NO_DEVICE;
414 }
415 g_CompleteExit = false;
416 #ifdef LOSCFG_DRIVERS_HDF_USB_PNP_NOTIFY
417 struct UsbGetDevicePara paraData;
418 paraData.type = USB_PNP_DEVICE_ADDRESS_TYPE;
419 paraData.busNum = dev->busNum;
420 paraData.devNum = dev->devAddr;
421 osDev->adapterDevice = UsbPnpNotifyGetUsbDevice(paraData);
422 #else
423 osDev->adapterDevice = NULL;
424 #endif
425 if (osDev->adapterDevice == NULL) {
426 DPRINTFN(0, "%s:%d osDev->adapterDevice is NULL\n", __func__, __LINE__);
427 return HDF_DEV_ERR_NO_DEVICE;
428 }
429
430 dev->privateData = (void *)osDev;
431 dev->descriptorsLength = 0;
432 ret = OsReadDescriptors(dev);
433 if (ret != HDF_SUCCESS) {
434 DPRINTFN(0, "%s:%d ret=%d\n", __func__, __LINE__, ret);
435 return ret;
436 }
437 ret = OsParseConfigDescriptors(dev);
438 if (ret != HDF_SUCCESS) {
439 DPRINTFN(0, "%s:%d ret=%d\n", __func__, __LINE__, ret);
440 return ret;
441 }
442 ret = memcpy_s(&dev->deviceDescriptor, sizeof(struct UsbDeviceDescriptor), &osDev->adapterDevice->ddesc,
443 USB_DDK_DT_DEVICE_SIZE);
444 if (ret != HDF_SUCCESS) {
445 DPRINTFN(0, "%s:%d ret=%d\n", __func__, __LINE__, ret);
446 ret = HDF_ERR_IO;
447 }
448 return ret;
449 }
450
OsFreeIsoUrbs(struct UsbHostRequest * request)451 static void OsFreeIsoUrbs(struct UsbHostRequest *request)
452 {
453 struct Async *urb = NULL;
454
455 for (int32_t i = 0; i < request->numUrbs; i++) {
456 urb = request->isoUrbs[i];
457 if (urb == NULL) {
458 break;
459 }
460 RawUsbMemFree(urb);
461 request->isoUrbs[i] = NULL;
462 }
463
464 RawUsbMemFree(request->isoUrbs);
465 request->isoUrbs = NULL;
466 }
467
OsDiscardUrbs(const struct UsbHostRequest * request,int32_t first,int32_t last)468 static void OsDiscardUrbs(const struct UsbHostRequest *request, int32_t first, int32_t last)
469 {
470 UsbAdapterUrb *urb = NULL;
471 struct Async *as = NULL;
472
473 if ((request == NULL) || (request->devHandle == NULL) || (first > URBS_PER_REQUEST) || (first > last)) {
474 DPRINTFN(0, "%s:%d invalid param", __func__, __LINE__);
475 return;
476 }
477
478 for (int32_t i = last - 1; i >= first; i--) {
479 if (request->requestType == USB_REQUEST_TYPE_ISOCHRONOUS) {
480 as = (struct Async *)request->isoUrbs[i];
481 } else {
482 as = &(((struct Async *)request->urbs)[i]);
483 }
484 if (as == NULL) {
485 DPRINTFN(0, "discard as null\n");
486 return;
487 }
488 urb = &as->urb;
489 if (as->state == URB_SUBMIT_STATE) {
490 DPRINTFN(0, "usb kill urb\n");
491 usb_kill_urb(urb);
492 DPRINTFN(0, "%s:%d discard request\n", __func__, __LINE__);
493 }
494 }
495 }
496
OsSubmitControlMsg(struct UsbHostRequest * const request,const UsbAdapterDevice * adapterDevice,const struct UsbDevice * dev)497 static int32_t OsSubmitControlMsg(
498 struct UsbHostRequest * const request, const UsbAdapterDevice *adapterDevice, const struct UsbDevice *dev)
499 {
500 int32_t ret;
501 struct Async *as = NULL;
502 UsbAdapterUrb *urb = NULL;
503 UsbAdapterHostEndpoint *uhe = NULL;
504 if ((request == NULL) || (request->length > MAX_BULK_DATA_BUFFER_LENGTH) || (request->buffer == NULL) ||
505 (adapterDevice == NULL) || (dev == NULL)) {
506 return HDF_ERR_INVALID_PARAM;
507 }
508 uhe = usb_find_host_endpoint((UsbAdapterDevice *)adapterDevice, request->requestType, request->endPoint);
509 if (uhe == NULL) {
510 DPRINTFN(0, "no found endpoint\n");
511 return -1;
512 }
513 if (request->urbs == NULL) {
514 as = RawUsbMemCalloc(sizeof(*as));
515 request->urbs = (void *)as;
516 request->numUrbs = 1;
517 }
518
519 if (request->urbs == NULL) {
520 DPRINTFN(0, "%s:%d no mem", __func__, __LINE__);
521 return HDF_ERR_MALLOC_FAIL;
522 }
523 as = (struct Async *)request->urbs;
524 DListHeadInit(&as->asynclist);
525 as->dev = (struct UsbDevice *)dev;
526 as->state = URB_SUBMIT_STATE;
527 urb = &as->urb;
528 ret = memset_s(urb, sizeof(*urb), 0, sizeof(*urb));
529 if (ret != HDF_SUCCESS) {
530 HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
531 return HDF_FAILURE;
532 }
533
534 urb->dev = (UsbAdapterDevice *)adapterDevice;
535 urb->endpoint = uhe;
536 urb->timeout = 500;
537 urb->transfer_buffer = request->buffer;
538 urb->context = (void *)request;
539 urb->complete = OsUrbComplete;
540 ret = OsControlMsg(urb);
541 DPRINTFN(0, "OsSubmitControlRequest:ret:%d\n", ret);
542 if (ret) {
543 DPRINTFN(0, "submiturb failed, errno=%d\n", errno);
544 return HDF_ERR_IO;
545 }
546
547 return HDF_SUCCESS;
548 }
549
OsSubmitControlRequest(struct UsbHostRequest * request)550 static int32_t OsSubmitControlRequest(struct UsbHostRequest *request)
551 {
552 struct OsDev *osDev = NULL;
553 UsbAdapterDevice *adapterDevice = NULL;
554
555 if ((request == NULL) || (request->length > MAX_BULK_DATA_BUFFER_LENGTH) || (request->devHandle == NULL) ||
556 (request->buffer == NULL)) {
557 return HDF_ERR_INVALID_PARAM;
558 }
559 struct UsbDeviceHandle *handle = request->devHandle;
560 struct UsbDevice *dev = handle->dev;
561 if (dev) {
562 osDev = (struct OsDev *)dev->privateData;
563 }
564 if (osDev) {
565 adapterDevice = osDev->adapterDevice;
566 }
567
568 return OsSubmitControlMsg(request, adapterDevice, dev);
569 }
570
OsSubmitBulkRequestHandleUrb(struct Async * pas,struct UsbHostRequest * request,int32_t bulkBufferLen,int32_t number)571 static int32_t OsSubmitBulkRequestHandleUrb(
572 struct Async *pas, struct UsbHostRequest *request, int32_t bulkBufferLen, int32_t number)
573 {
574 UsbAdapterUrb *urb = NULL;
575
576 if (bulkBufferLen == 0) {
577 HDF_LOGE("%{public}s:%{public}d bulkBufferLen can not be zero", __func__, __LINE__);
578 return HDF_ERR_INVALID_PARAM;
579 }
580
581 urb = &pas->urb;
582 urb->context = (void *)request;
583 switch (request->requestType) {
584 case USB_REQUEST_TYPE_BULK:
585 break;
586 case USB_REQUEST_TYPE_INTERRUPT:
587 urb->interval = 50;
588 break;
589 default:
590 DPRINTFN(0, "%s:%d unknown requestType=%u\n", __func__, __LINE__, request->requestType);
591 return HDF_ERR_INVALID_PARAM;
592 }
593 urb->transfer_buffer = request->buffer + (number * bulkBufferLen);
594 urb->complete = OsUrbComplete;
595 if (number == request->numUrbs - 1) {
596 uint32_t len = (uint32_t)(request->length % bulkBufferLen);
597 urb->transfer_buffer_length = (len == 0) ? (uint32_t)bulkBufferLen : len;
598 } else {
599 urb->transfer_buffer_length = (uint32_t)bulkBufferLen;
600 }
601
602 return HDF_SUCCESS;
603 }
604
OsSubmitBulkRequestHandle(struct UsbHostRequest * const request,struct Async * const as,int32_t bulkBufferLen)605 static int32_t OsSubmitBulkRequestHandle(
606 struct UsbHostRequest * const request, struct Async * const as, int32_t bulkBufferLen)
607 {
608 struct Async *pas = NULL;
609 int32_t numUrbs = request->numUrbs;
610 struct UsbDevice *dev = request->devHandle->dev;
611 struct OsDev *osDev = (struct OsDev *)dev->privateData;
612 UsbAdapterDevice *adapterDevice = osDev->adapterDevice;
613 UsbAdapterUrb *urb = NULL;
614
615 UsbAdapterHostEndpoint *uhe = usb_find_host_endpoint(adapterDevice, request->requestType, request->endPoint);
616 if (uhe == NULL) {
617 DPRINTFN(0, "no found endpoint\n");
618 return HDF_DEV_ERR_NO_DEVICE;
619 }
620
621 int32_t i;
622 for (i = 0, pas = as; i < numUrbs; i++, pas++) {
623 urb = &pas->urb;
624 int32_t ret = memset_s(urb, sizeof(*urb), 0, sizeof(*urb));
625 if (ret != HDF_SUCCESS) {
626 HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
627 return HDF_FAILURE;
628 }
629
630 ret = OsSubmitBulkRequestHandleUrb(pas, request, bulkBufferLen, i);
631 if (ret != HDF_SUCCESS) {
632 return ret;
633 }
634 pas->state = URB_SUBMIT_STATE;
635 DListHeadInit(&pas->asynclist);
636 pas->dev = dev;
637 urb->dev = adapterDevice;
638 urb->endpoint = uhe;
639
640 ret = OsSubmitUrb(urb, urb->dev, urb->endpoint);
641 if (ret == 0) {
642 continue;
643 }
644 if (i == 0) {
645 DPRINTFN(0, "the first urb failed\n");
646 return HDF_ERR_IO;
647 }
648 OsalMutexLock(&request->lock);
649 request->numRetired += numUrbs - i;
650 if (errno != EREMOTEIO) {
651 request->reqStatus = USB_REQUEST_ERROR;
652 }
653 OsDiscardUrbs(request, 0, i);
654 OsalMutexUnlock(&request->lock);
655 return HDF_SUCCESS;
656 }
657
658 return HDF_SUCCESS;
659 }
660
OsSubmitBulkRequest(struct UsbHostRequest * const request)661 static int32_t OsSubmitBulkRequest(struct UsbHostRequest * const request)
662 {
663 struct Async *as = NULL;
664 uint32_t bulkBufferLen;
665 int32_t numUrbs;
666
667 if ((request == NULL) || (request->devHandle == NULL)) {
668 DPRINTFN(0, "%s: invalid param", __func__);
669 return HDF_ERR_INVALID_PARAM;
670 }
671
672 if (request->length > MAX_BULK_DATA_BUFFER_LENGTH || request->length <= 0) {
673 DPRINTFN(0, "Bulk request size err\n");
674 return HDF_FAILURE;
675 }
676
677 if (request->devHandle->caps & USB_ADAPTER_CAP_BULK_SCATTER_GATHER) {
678 bulkBufferLen = request->length ? request->length : 1;
679 } else {
680 bulkBufferLen = MAX_BULK_DATA_BUFFER_LENGTH;
681 }
682 if (request->length < bulkBufferLen) {
683 numUrbs = 1;
684 } else {
685 numUrbs = (request->length + bulkBufferLen - 1) / bulkBufferLen;
686 }
687 if (request->urbs == NULL) {
688 as = RawUsbMemCalloc(sizeof(*as));
689 request->urbs = (void *)as;
690 } else if (numUrbs > 1) {
691 RawUsbMemFree(request->urbs);
692 as = RawUsbMemCalloc(numUrbs * sizeof(*as));
693 request->urbs = (void *)as;
694 }
695 if (request->urbs == NULL) {
696 DPRINTFN(0, "%s:%d no mem", __func__, __LINE__);
697 return HDF_ERR_MALLOC_FAIL;
698 }
699 as = (struct Async *)request->urbs;
700 request->numUrbs = numUrbs;
701 request->numRetired = 0;
702 request->reqStatus = USB_REQUEST_COMPLETED;
703
704 return OsSubmitBulkRequestHandle(request, as, bulkBufferLen);
705 }
706
OsAllocIsoUrbs(struct UsbHostRequest * request,int32_t numUrbs,struct Async ** ass)707 static int32_t OsAllocIsoUrbs(struct UsbHostRequest *request, int32_t numUrbs, struct Async **ass)
708 {
709 struct Async *as = NULL;
710 unsigned char *urbBuffer = request->buffer;
711 int32_t numPacketsLeft = request->numIsoPackets;
712 int32_t packetIdx = 0;
713 int32_t i, j;
714
715 UsbPipeType pipeType = request->requestType;
716 unsigned char endPoint = request->endPoint;
717 struct UsbDeviceHandle *handle = request->devHandle;
718 struct UsbDevice *dev = handle->dev;
719 struct OsDev *osDev = (struct OsDev *)dev->privateData;
720 UsbAdapterDevice *adapterDevice = osDev->adapterDevice;
721 UsbAdapterHostEndpoint *uhe = NULL;
722 uhe = usb_find_host_endpoint(adapterDevice, pipeType, endPoint);
723 if (uhe == NULL) {
724 DPRINTFN(0, "no found endpoint\n");
725 return HDF_DEV_ERR_NO_DEVICE;
726 }
727
728 for (i = 0; i < numUrbs; i++) {
729 UsbAdapterUrb *urb = NULL;
730 int32_t numPackets = MIN(numPacketsLeft, MAX_ISO_PACKETS_PER_URB);
731 as = RawUsbMemCalloc(sizeof(struct Async));
732 if (as == NULL) {
733 OsFreeIsoUrbs(request);
734 return HDF_ERR_MALLOC_FAIL;
735 }
736 ass[i] = as;
737 urb = &as->urb;
738 for (j = 0; j < numPackets; j++) {
739 unsigned int packetLen = request->isoPacketDesc[packetIdx++].length;
740 urb->transfer_buffer_length += packetLen;
741 urb->iso_frame_desc[j].length = packetLen;
742 }
743 urb->endpoint = uhe;
744 urb->number_of_packets = (unsigned int)numPackets;
745 urb->transfer_buffer = urbBuffer;
746 urb->context = request;
747 urbBuffer += urb->transfer_buffer_length;
748 numPacketsLeft -= numPackets;
749 }
750
751 return HDF_SUCCESS;
752 }
753
OsSubmitIsoUrbs(struct UsbHostRequest * request,int32_t numUrbs,struct Async ** pUrbs)754 static int32_t OsSubmitIsoUrbs(struct UsbHostRequest *request, int32_t numUrbs, struct Async **pUrbs)
755 {
756 for (int32_t i = 0; i < numUrbs; i++) {
757 UsbAdapterUrb *urb = &(pUrbs[i]->urb);
758 int32_t ret = OsSubmitUrb(urb, urb->dev, urb->endpoint);
759 DPRINTFN(0, "submitUrb:%d errno=%d\n", ret, errno);
760 if (ret == 0) {
761 continue;
762 }
763
764 if (errno == ENODEV) {
765 ret = HDF_DEV_ERR_NO_DEVICE;
766 } else {
767 DPRINTFN(0, "%s:%d submit iso urb failed errno=%d\n", __func__, __LINE__, errno);
768 ret = HDF_ERR_IO;
769 }
770
771 if (i == 0) {
772 DPRINTFN(0, "first URB failed");
773 OsFreeIsoUrbs(request);
774 return ret;
775 }
776
777 OsalMutexLock(&request->lock);
778 request->reqStatus = USB_REQUEST_ERROR;
779 request->numRetired += numUrbs - i;
780 if (request->numRetired == numUrbs) {
781 RawUsbMemFree(pUrbs);
782 request->isoUrbs = NULL;
783 }
784 OsalMutexUnlock(&request->lock);
785
786 break;
787 }
788
789 return HDF_SUCCESS;
790 }
791
OsSubmitIsoRequest(struct UsbHostRequest * request)792 static int32_t OsSubmitIsoRequest(struct UsbHostRequest *request)
793 {
794 unsigned int totalLen = 0;
795
796 if ((request == NULL) || (request->devHandle == NULL) || (request->numIsoPackets < 1)) {
797 DPRINTFN(0, "%s:%d invalid param", __func__, __LINE__);
798 return HDF_ERR_INVALID_PARAM;
799 }
800
801 if (request->length > MAX_ISO_DATA_BUFFER_LEN) {
802 DPRINTFN(0, "%s:%d request length exceed the maximum", __func__, __LINE__);
803 return HDF_ERR_NOT_SUPPORT;
804 }
805
806 for (int32_t i = 0; i < request->numIsoPackets; i++) {
807 unsigned int packetLen = request->isoPacketDesc[i].length;
808 if (packetLen > MAX_ISO_DATA_BUFFER_LEN) {
809 DPRINTFN(0, "%s:%d packet length: %u exceeds maximum: %u\n", __func__, __LINE__, packetLen,
810 MAX_ISO_DATA_BUFFER_LEN);
811 return HDF_ERR_INVALID_PARAM;
812 }
813 totalLen += packetLen;
814 }
815 if (request->length < totalLen) {
816 DPRINTFN(0, "%s:%d invalid param", __func__, __LINE__);
817 return HDF_ERR_INVALID_PARAM;
818 }
819 int32_t numUrbs = (request->numIsoPackets + (MAX_ISO_PACKETS_PER_URB - 1)) / MAX_ISO_PACKETS_PER_URB;
820 struct Async **pUrbs = RawUsbMemCalloc(numUrbs * sizeof(struct Async *));
821 if (pUrbs == NULL) {
822 DPRINTFN(0, "%s:%d RawUsbMemCalloc pUrbs failed", __func__, __LINE__);
823 return HDF_ERR_MALLOC_FAIL;
824 }
825 request->isoUrbs = (void **)pUrbs;
826 request->numUrbs = numUrbs;
827 request->numRetired = 0;
828 request->isoPacketOffset = 0;
829 int32_t ret = OsAllocIsoUrbs(request, numUrbs, pUrbs);
830 if (ret != HDF_SUCCESS) {
831 DPRINTFN(0, "%s:%d alloc iso urbs failed", __func__, __LINE__);
832 return ret;
833 }
834
835 return OsSubmitIsoUrbs(request, numUrbs, pUrbs);
836 }
837
OsControlCompletion(struct UsbHostRequest * request,struct Async * as)838 static int32_t OsControlCompletion(struct UsbHostRequest *request, struct Async *as)
839 {
840 int32_t status;
841 UsbAdapterUrb *urb = &as->urb;
842
843 OsalMutexLock(&request->lock);
844 request->numRetired++;
845 request->actualLength += (int)urb->actual_length;
846 if (request->reqStatus == USB_REQUEST_CANCELLED) {
847 OsalMutexUnlock(&request->lock);
848 as->state = URB_INIT_STATE;
849 return RawHandleRequestCompletion(request, USB_REQUEST_CANCELLED);
850 }
851
852 switch (urb->status) {
853 case 0:
854 status = USB_REQUEST_COMPLETED;
855 break;
856 case -ENOENT:
857 status = USB_REQUEST_CANCELLED;
858 break;
859 case -EPIPE:
860 DPRINTFN(0, "%s:%d unsupported control request", __func__, __LINE__);
861 status = USB_REQUEST_STALL;
862 break;
863 case -EOVERFLOW:
864 DPRINTFN(0, "%s:%d overflow actualLength=%d\n", __func__, __LINE__, urb->actual_length);
865 status = USB_REQUEST_OVERFLOW;
866 break;
867 case -ENODEV:
868 case -ESHUTDOWN:
869 DPRINTFN(0, "device removed");
870 status = USB_REQUEST_NO_DEVICE;
871 break;
872 default:
873 DPRINTFN(0, "%s:%d urb status=%d\n", __func__, __LINE__, urb->status);
874 status = USB_REQUEST_ERROR;
875 break;
876 }
877 OsalMutexUnlock(&request->lock);
878 as->state = URB_INIT_STATE;
879 return RawHandleRequestCompletion(request, status);
880 }
881
OsIsoRequestDesStatus(struct UsbHostRequest * request,UsbAdapterUrb * urb)882 static void OsIsoRequestDesStatus(struct UsbHostRequest *request, UsbAdapterUrb *urb)
883 {
884 uint32_t i;
885 UsbAdapterIsoPacketDescriptor *urbDesc = NULL;
886 struct UsbIsoPacketDesc *requestDesc = NULL;
887
888 for (i = 0; i < urb->number_of_packets; i++) {
889 urbDesc = &urb->iso_frame_desc[i];
890 requestDesc = &request->isoPacketDesc[request->isoPacketOffset++];
891
892 switch (urbDesc->status) {
893 case HDF_SUCCESS:
894 requestDesc->status = USB_REQUEST_COMPLETED;
895 break;
896 case -ENODEV:
897 case -ESHUTDOWN:
898 requestDesc->status = USB_REQUEST_NO_DEVICE;
899 break;
900 case -EPIPE:
901 requestDesc->status = USB_REQUEST_STALL;
902 break;
903 case -EOVERFLOW:
904 requestDesc->status = USB_REQUEST_OVERFLOW;
905 break;
906 default:
907 requestDesc->status = USB_REQUEST_ERROR;
908 break;
909 }
910 DPRINTFN(0, "%s:%d urb status=%d-%d\n", __func__, __LINE__, i, urbDesc->status);
911
912 requestDesc->actualLength = urbDesc->actual_length;
913 }
914 }
915
OsIsoCompletion(struct UsbHostRequest * request,struct Async * as)916 static int32_t OsIsoCompletion(struct UsbHostRequest *request, struct Async *as)
917 {
918 UsbRequestStatus status;
919 int32_t urbIndex = 0;
920 int32_t numUrbs;
921 UsbAdapterUrb *urb = &as->urb;
922
923 if (request == NULL) {
924 DPRINTFN(0, "%s:%d invalid param", __func__, __LINE__);
925 return HDF_ERR_INVALID_PARAM;
926 }
927
928 numUrbs = request->numUrbs;
929 OsalMutexLock(&request->lock);
930 for (int32_t i = 0; i < numUrbs; i++) {
931 if (urb == request->isoUrbs[i]) {
932 urbIndex = i + 1;
933 break;
934 }
935 }
936 if (urbIndex == 0) {
937 DPRINTFN(0, "%s:%d urbIndex is zero", __func__, __LINE__);
938 OsalMutexUnlock(&request->lock);
939 return HDF_ERR_BAD_FD;
940 }
941
942 OsIsoRequestDesStatus(request, urb);
943 request->numRetired++;
944 if (request->reqStatus != USB_REQUEST_COMPLETED) {
945 if (request->numRetired == numUrbs) {
946 OsFreeIsoUrbs(request);
947 OsalMutexUnlock(&request->lock);
948 return RawHandleRequestCompletion(request, USB_REQUEST_ERROR);
949 }
950 goto OUT;
951 }
952
953 status = USB_REQUEST_COMPLETED;
954 if (urb->status == -ESHUTDOWN) {
955 status = USB_REQUEST_NO_DEVICE;
956 } else if (!((urb->status == HDF_SUCCESS) || (urb->status == -ENOENT) || (urb->status == -ECONNRESET))) {
957 status = USB_REQUEST_ERROR;
958 }
959
960 if (request->numRetired == numUrbs) {
961 DPRINTFN(0, "%s:%d all URBs reaped for complete", __func__, __LINE__);
962 OsFreeIsoUrbs(request);
963 OsalMutexUnlock(&request->lock);
964 return RawHandleRequestCompletion(request, status);
965 }
966 OUT:
967 OsalMutexUnlock(&request->lock);
968 return HDF_SUCCESS;
969 }
970
OsProcessAbnormalReap(struct UsbHostRequest * request,const UsbAdapterUrb * urb)971 static int32_t OsProcessAbnormalReap(struct UsbHostRequest *request, const UsbAdapterUrb *urb)
972 {
973 if (urb->actual_length > 0) {
974 unsigned char *target = request->buffer + request->actualLength;
975 if (urb->transfer_buffer != target) {
976 if (memmove_s(target, urb->actual_length, urb->transfer_buffer, urb->actual_length) != EOK) {
977 DPRINTFN(0, "%s: memmove_s failed, ret=%d", __func__, ret);
978 }
979 }
980 request->actualLength += urb->actual_length;
981 }
982 if (request->numRetired == request->numUrbs) {
983 return HDF_SUCCESS;
984 }
985
986 return HDF_ERR_IO;
987 }
988
OsUrbStatusToRequestStatus(struct UsbHostRequest * request,const UsbAdapterUrb * urb)989 static int32_t OsUrbStatusToRequestStatus(struct UsbHostRequest *request, const UsbAdapterUrb *urb)
990 {
991 int32_t ret;
992
993 switch (urb->status) {
994 case 0:
995 ret = HDF_SUCCESS;
996 break;
997 case -ESHUTDOWN:
998 DPRINTFN(0, "%s:%d device is removed", __func__, __LINE__);
999 request->reqStatus = USB_REQUEST_NO_DEVICE;
1000 ret = HDF_DEV_ERR_NO_DEVICE;
1001 break;
1002 case -EPIPE:
1003 if (request->reqStatus == USB_REQUEST_COMPLETED) {
1004 request->reqStatus = USB_REQUEST_STALL;
1005 }
1006 ret = HDF_DEV_ERR_NO_DEVICE;
1007 break;
1008 case -EOVERFLOW:
1009 DPRINTFN(0, "%s:%d overflow error, actualLength=%d\n", __func__, __LINE__, urb->actual_length);
1010 if (request->reqStatus == USB_REQUEST_COMPLETED) {
1011 request->reqStatus = USB_REQUEST_OVERFLOW;
1012 }
1013 ret = HDF_FAILURE;
1014 break;
1015 case -ECONNRESET:
1016 ret = HDF_DEV_ERR_OP;
1017 if (request->reqStatus == USB_REQUEST_COMPLETED) {
1018 request->reqStatus = USB_REQUEST_CANCELLED;
1019 }
1020 break;
1021 default:
1022 DPRINTFN(0, "unknown urb status %d\n", urb->status);
1023 if (request->reqStatus == USB_REQUEST_COMPLETED) {
1024 request->reqStatus = USB_REQUEST_ERROR;
1025 }
1026 ret = HDF_FAILURE;
1027 break;
1028 }
1029
1030 return ret;
1031 }
1032
OsBulkCompletion(struct UsbHostRequest * const request,struct Async * const as)1033 static int32_t OsBulkCompletion(struct UsbHostRequest * const request, struct Async * const as)
1034 {
1035 int32_t ret;
1036 int32_t urbIdx = as - (struct Async *)request->urbs;
1037 const UsbAdapterUrb *urb = &as->urb;
1038
1039 OsalMutexLock(&request->lock);
1040 request->numRetired++;
1041 if (request->reqStatus != USB_REQUEST_COMPLETED) {
1042 if (OsProcessAbnormalReap(request, urb) == HDF_SUCCESS) {
1043 goto COMPLETED;
1044 } else {
1045 goto OUT;
1046 }
1047 }
1048 request->actualLength += urb->actual_length;
1049 ret = OsUrbStatusToRequestStatus(request, urb);
1050 if (ret == HDF_DEV_ERR_NO_DEVICE) {
1051 goto CANCEL;
1052 } else if (ret == HDF_FAILURE) {
1053 goto COMPLETED;
1054 }
1055
1056 if (request->numRetired == request->numUrbs) {
1057 goto COMPLETED;
1058 } else if (urb->actual_length < urb->transfer_buffer_length) {
1059 if (request->reqStatus == USB_REQUEST_COMPLETED) {
1060 request->reqStatus = USB_REQUEST_COMPLETED_SHORT;
1061 }
1062 } else {
1063 goto OUT;
1064 }
1065
1066 CANCEL:
1067 if (request->numRetired == request->numUrbs) {
1068 goto COMPLETED;
1069 }
1070 OsDiscardUrbs(request, urbIdx + 1, request->numUrbs);
1071
1072 OUT:
1073 OsalMutexUnlock(&request->lock);
1074 return HDF_SUCCESS;
1075
1076 COMPLETED:
1077 OsalMutexUnlock(&request->lock);
1078 as->state = URB_INIT_STATE;
1079 return RawHandleRequestCompletion(request, request->reqStatus);
1080 }
1081
OsFreeRequest(const struct UsbHostRequest * request)1082 static int32_t OsFreeRequest(const struct UsbHostRequest *request)
1083 {
1084 int32_t retry = 0;
1085 if (request == NULL) {
1086 DPRINTFN(0, "%s:%d invalid param", __func__, __LINE__);
1087 return HDF_ERR_INVALID_PARAM;
1088 }
1089
1090 while (true) {
1091 OsalMSleep(20);
1092 if (request->numUrbs != request->numRetired) {
1093 if (++retry > 10) {
1094 DPRINTFN(0, "request busy numUrbs:%d+numretired:%d\n", request->numUrbs, request->numRetired);
1095 return HDF_ERR_DEVICE_BUSY;
1096 }
1097 continue;
1098 } else {
1099 break;
1100 }
1101 }
1102
1103 return HDF_SUCCESS;
1104 }
1105
AdapterInit(const struct UsbSession * session)1106 static int32_t AdapterInit(const struct UsbSession *session)
1107 {
1108 (void)session;
1109 return HDF_SUCCESS;
1110 }
1111
AdapterExit(const struct UsbSession * session)1112 static void AdapterExit(const struct UsbSession *session)
1113 {
1114 (void)session;
1115 return;
1116 }
1117
AdapterOpenDevice(struct UsbSession * session,uint8_t busNum,uint8_t usbAddr)1118 static struct UsbDeviceHandle *AdapterOpenDevice(struct UsbSession *session, uint8_t busNum, uint8_t usbAddr)
1119 {
1120 int32_t ret;
1121 struct UsbDevice *dev = NULL;
1122 struct UsbDeviceHandle *handle = NULL;
1123
1124 handle = OsGetDeviceHandle(session, busNum, usbAddr);
1125 if (handle != NULL) {
1126 return handle;
1127 }
1128 handle = OsCallocDeviceHandle();
1129 if (handle == NULL) {
1130 DPRINTFN(0, "%s: Calloc Device Handle failed", __func__);
1131 return NULL;
1132 }
1133 dev = OsAllocDevice(session, handle);
1134 if (dev == NULL) {
1135 DPRINTFN(0, "%s: OsAllocDevice failed\n", __func__);
1136 goto ERR;
1137 }
1138 ret = OsInitDevice(dev, busNum, usbAddr);
1139 if (ret) {
1140 DPRINTFN(0, "%s: OsInitDevice failed ret=%d\n", __func__, ret);
1141 RawUsbMemFree(dev->privateData);
1142 RawUsbMemFree(dev);
1143 goto ERR;
1144 }
1145 OsalAtomicSet(&dev->refcnt, 1);
1146 OsalMutexLock(&session->lock);
1147 HdfSListAdd(&session->usbDevs, &dev->list);
1148 OsalMutexUnlock(&session->lock);
1149 return handle;
1150 ERR:
1151 OsalMutexDestroy(&handle->lock);
1152 RawUsbMemFree(handle);
1153 return NULL;
1154 }
1155
AdapterCloseDevice(struct UsbDeviceHandle * handle)1156 static void AdapterCloseDevice(struct UsbDeviceHandle *handle)
1157 {
1158 struct UsbDevice *dev = NULL;
1159
1160 if ((handle == NULL) || (handle->dev == NULL)) {
1161 DPRINTFN(0, "%s:%d invalid param", __func__, __LINE__);
1162 return;
1163 }
1164 if (RawKillSignal(handle, 0) != HDF_SUCCESS) {
1165 HDF_LOGE("%{public}s:%{public}d RawKillSignal failed", __func__, __LINE__);
1166 }
1167 dev = handle->dev;
1168 if (AdapterAtomicDec(&dev->refcnt)) {
1169 return;
1170 }
1171 if (dev->session == NULL) {
1172 return;
1173 }
1174 OsalMutexLock(&dev->session->lock);
1175 HdfSListRemove(&dev->session->usbDevs, &dev->list);
1176 OsalMutexUnlock(&dev->session->lock);
1177
1178 if (dev->configDescriptors) {
1179 RawUsbMemFree(dev->configDescriptors);
1180 }
1181 if (dev->descriptors) {
1182 RawUsbMemFree(dev->descriptors);
1183 }
1184 if (dev->privateData) {
1185 OsDevDestory(dev->privateData);
1186 dev->privateData = NULL;
1187 }
1188 RawUsbMemFree(dev);
1189
1190 OsalMutexDestroy(&handle->lock);
1191 RawUsbMemFree(handle);
1192 handle = NULL;
1193 }
1194
AdapterGetConfigDescriptor(const struct UsbDevice * dev,uint8_t configIndex,void * buffer,size_t len)1195 static int32_t AdapterGetConfigDescriptor(const struct UsbDevice *dev, uint8_t configIndex, void *buffer, size_t len)
1196 {
1197 struct OsDev *osDev = (struct OsDev *)dev->privateData;
1198 UsbAdapterDevice *adapterDevice = osDev->adapterDevice;
1199 size_t count;
1200 if ((buffer == NULL) || (len == 0) || (adapterDevice == NULL) || (adapterDevice->cdesc == NULL)) {
1201 DPRINTFN(0, "invalid param is NULL");
1202 return HDF_ERR_INVALID_PARAM;
1203 }
1204 count = UGETW(adapterDevice->cdesc->wTotalLength);
1205 if (count > len) {
1206 DPRINTFN(0, "count length is error");
1207 return HDF_ERR_IO;
1208 }
1209 if (memcpy_s(buffer, len, adapterDevice->cdesc, count) != EOK) {
1210 DPRINTFN(0, "memcpy_s fail");
1211 return HDF_ERR_IO;
1212 }
1213 return (int32_t)len;
1214 }
1215
AdapterGetConfiguration(const struct UsbDeviceHandle * handle,uint8_t * activeConfig)1216 static int32_t AdapterGetConfiguration(const struct UsbDeviceHandle *handle, uint8_t *activeConfig)
1217 {
1218 struct UsbDevice *dev = handle->dev;
1219 struct OsDev *osDev = (struct OsDev *)dev->privateData;
1220 UsbAdapterDevice *adapterDevice = osDev->adapterDevice;
1221
1222 if (adapterDevice != NULL) {
1223 *activeConfig = adapterDevice->curr_config_index;
1224 }
1225
1226 return HDF_SUCCESS;
1227 }
1228
AdapterSetConfiguration(struct UsbDeviceHandle * handle,int32_t activeConfig)1229 static int32_t AdapterSetConfiguration(struct UsbDeviceHandle *handle, int32_t activeConfig)
1230 {
1231 struct UsbDevice *dev = handle->dev;
1232 struct OsDev *osDev = (struct OsDev *)dev->privateData;
1233 UsbAdapterDevice *adapterDevice = osDev->adapterDevice;
1234
1235 if (adapterDevice->flags.usb_mode != USB_MODE_HOST) {
1236 return HDF_DEV_ERR_DEV_INIT_FAIL;
1237 }
1238 if ((activeConfig < 0) || (activeConfig >= (int)adapterDevice->curr_config_no)) {
1239 return HDF_ERR_INVALID_PARAM;
1240 }
1241 if (activeConfig == adapterDevice->curr_config_index) {
1242 return HDF_SUCCESS;
1243 }
1244 if (usbd_set_config_index(adapterDevice, activeConfig) != 0) {
1245 return HDF_ERR_IO;
1246 }
1247
1248 return HDF_SUCCESS;
1249 }
1250
AdapterClaimInterface(const struct UsbDeviceHandle * handle,unsigned int interfaceNumber)1251 static int32_t AdapterClaimInterface(const struct UsbDeviceHandle *handle, unsigned int interfaceNumber)
1252 {
1253 (void)handle;
1254 (void)interfaceNumber;
1255 return HDF_SUCCESS;
1256 }
1257
AdapterReleaseInterface(const struct UsbDeviceHandle * handle,unsigned int interfaceNumber)1258 static int32_t AdapterReleaseInterface(const struct UsbDeviceHandle *handle, unsigned int interfaceNumber)
1259 {
1260 (void)handle;
1261 (void)interfaceNumber;
1262 return HDF_SUCCESS;
1263 }
1264
AdapterSetInterface(const struct UsbDeviceHandle * handle,uint8_t interface,uint8_t altSetting)1265 static int32_t AdapterSetInterface(const struct UsbDeviceHandle *handle, uint8_t interface, uint8_t altSetting)
1266 {
1267 struct UsbDevice *dev = handle->dev;
1268 struct OsDev *osDev = (struct OsDev *)dev->privateData;
1269 UsbAdapterDevice *adapterDevice = osDev->adapterDevice;
1270
1271 if (adapterDevice->flags.usb_mode != USB_MODE_HOST) {
1272 return HDF_DEV_ERR_DEV_INIT_FAIL;
1273 }
1274 DPRINTFN(0, "altSetting interfaceId:%d+altSetting:%d\n", interface, altSetting);
1275 if (usb_set_interface(adapterDevice, interface, altSetting)) {
1276 return HDF_ERR_IO;
1277 }
1278
1279 return HDF_SUCCESS;
1280 }
1281
AdapterClearHalt(const struct UsbDeviceHandle * handle,unsigned int endPoint)1282 static int32_t AdapterClearHalt(const struct UsbDeviceHandle *handle, unsigned int endPoint)
1283 {
1284 struct UsbDevice *dev = handle->dev;
1285 struct OsDev *osDev = (struct OsDev *)dev->privateData;
1286 UsbAdapterDevice *adapterDevice = osDev->adapterDevice;
1287 int32_t ret;
1288 UsbAdapterHostEndpoint *uhe = usb_find_host_endpoint(adapterDevice, PIPE_BULK, endPoint);
1289 if (uhe == NULL) {
1290 printf("no found uhe\n");
1291 return -1;
1292 }
1293 ret = usb_clear_halt(adapterDevice, uhe);
1294 return ret;
1295 }
1296
AdapterResetDevice(const struct UsbDeviceHandle * handle)1297 static int32_t AdapterResetDevice(const struct UsbDeviceHandle *handle)
1298 {
1299 (void)handle;
1300 return HDF_SUCCESS;
1301 }
1302
AdapterAllocRequest(const struct UsbDeviceHandle * handle,int32_t isoPackets,size_t length)1303 static struct UsbHostRequest *AdapterAllocRequest(
1304 const struct UsbDeviceHandle *handle, int32_t isoPackets, size_t length)
1305 {
1306 (void)handle;
1307 size_t allocSize;
1308 struct UsbHostRequest *request = NULL;
1309
1310 allocSize = sizeof(struct UsbHostRequest) + (sizeof(struct UsbIsoPacketDesc) * (size_t)isoPackets) +
1311 (sizeof(unsigned char) * length);
1312 request = RawUsbMemCalloc(allocSize);
1313 if (request == NULL) {
1314 HDF_LOGE("%{public}s RawMemAlloc fail", __func__);
1315 return NULL;
1316 }
1317 request->numIsoPackets = isoPackets;
1318 request->buffer = (unsigned char *)request + allocSize - length;
1319 request->bufLen = (int)length;
1320 return request;
1321 }
1322
AdapterFreeRequest(struct UsbHostRequest * request)1323 static int32_t AdapterFreeRequest(struct UsbHostRequest *request)
1324 {
1325 if (request == NULL) {
1326 DPRINTFN(0, "%s:%d invalid param", __func__, __LINE__);
1327 return HDF_ERR_INVALID_PARAM;
1328 }
1329 if (request->numUrbs > request->numRetired) {
1330 OsDiscardUrbs(request, request->numRetired, request->numUrbs);
1331 int32_t ret = OsFreeRequest(request);
1332 if (ret != HDF_SUCCESS) {
1333 return ret;
1334 }
1335 }
1336 if (request->urbs) {
1337 RawUsbMemFree(request->urbs);
1338 request->urbs = NULL;
1339 }
1340 RawUsbMemFree((void *)request);
1341 return HDF_SUCCESS;
1342 }
1343
AdapterSubmitRequest(struct UsbHostRequest * request)1344 static int32_t AdapterSubmitRequest(struct UsbHostRequest *request)
1345 {
1346 int32_t ret;
1347
1348 if (request == NULL) {
1349 DPRINTFN(0, "%s:%d request is NULL", __func__, __LINE__);
1350 return HDF_FAILURE;
1351 }
1352 OsalMutexInit(&(request->lock));
1353 request->actualLength = 0;
1354 request->numRetired = 0;
1355 request->numUrbs = 0;
1356 switch (request->requestType) {
1357 case USB_REQUEST_TYPE_CONTROL:
1358 ret = OsSubmitControlRequest(request);
1359 break;
1360 case USB_REQUEST_TYPE_ISOCHRONOUS:
1361 ret = OsSubmitIsoRequest(request);
1362 break;
1363 case USB_REQUEST_TYPE_BULK:
1364 case USB_REQUEST_TYPE_INTERRUPT:
1365 ret = OsSubmitBulkRequest(request);
1366 break;
1367 default:
1368 DPRINTFN(0, "%s:%d unknown requestType=%u\n", __func__, __LINE__, request->requestType);
1369 ret = HDF_ERR_INVALID_PARAM;
1370 break;
1371 }
1372
1373 return ret;
1374 }
1375
AdapterCancelRequest(const struct UsbHostRequest * request)1376 static int32_t AdapterCancelRequest(const struct UsbHostRequest *request)
1377 {
1378 if (!request->urbs) {
1379 DPRINTFN(0, "adapter cancel urb null\n");
1380 goto END;
1381 }
1382
1383 OsDiscardUrbs(request, 0, request->numUrbs);
1384 END:
1385 return HDF_SUCCESS;
1386 }
1387
AdapterUrbCompleteHandle(const struct UsbDeviceHandle * devHandle)1388 static int32_t AdapterUrbCompleteHandle(const struct UsbDeviceHandle *devHandle)
1389 {
1390 int32_t ret;
1391 struct UsbDevice *dev = NULL;
1392 struct OsDev *osDev = NULL;
1393 UsbAdapterUrb *urb = NULL;
1394 struct Async *as = NULL;
1395 struct UsbHostRequest *request = NULL;
1396 if ((devHandle == NULL) || (devHandle->dev == NULL) || (devHandle->dev->privateData == NULL)) {
1397 HDF_LOGE("%{public}s:%{public}d invalid parameter", __func__, __LINE__);
1398 return HDF_ERR_INVALID_PARAM;
1399 }
1400 dev = devHandle->dev;
1401 osDev = (struct OsDev *)dev->privateData;
1402 ret = OsReapUrb(devHandle, &as);
1403 if (as == NULL) {
1404 HDF_LOGE("as is null\n");
1405 return HDF_FAILURE;
1406 }
1407 urb = &as->urb;
1408 request = urb->context;
1409 switch (request->requestType) {
1410 case USB_REQUEST_TYPE_CONTROL:
1411 ret = OsControlCompletion(request, as);
1412 break;
1413 case USB_REQUEST_TYPE_ISOCHRONOUS:
1414 ret = OsIsoCompletion(request, as);
1415 break;
1416 case USB_REQUEST_TYPE_BULK:
1417 case USB_REQUEST_TYPE_INTERRUPT:
1418 ret = OsBulkCompletion(request, as);
1419 break;
1420 default:
1421 DPRINTFN(0, "%s:%d unrecognised requestType %u\n", __func__, __LINE__, request->requestType);
1422 ret = HDF_FAILURE;
1423 break;
1424 }
1425 return ret;
1426 }
1427
1428 static struct UsbOsAdapterOps g_usbAdapter = {
1429 .init = AdapterInit,
1430 .exit = AdapterExit,
1431 .openDevice = AdapterOpenDevice,
1432 .closeDevice = AdapterCloseDevice,
1433 .getConfigDescriptor = AdapterGetConfigDescriptor,
1434 .getConfiguration = AdapterGetConfiguration,
1435 .setConfiguration = AdapterSetConfiguration,
1436 .claimInterface = AdapterClaimInterface,
1437 .releaseInterface = AdapterReleaseInterface,
1438 .setInterfaceAltsetting = AdapterSetInterface,
1439 .clearHalt = AdapterClearHalt,
1440 .resetDevice = AdapterResetDevice,
1441 .allocRequest = AdapterAllocRequest,
1442 .freeRequest = AdapterFreeRequest,
1443 .submitRequest = AdapterSubmitRequest,
1444 .cancelRequest = AdapterCancelRequest,
1445 .urbCompleteHandle = AdapterUrbCompleteHandle,
1446 };
1447
UsbAdapterGetOps(void)1448 struct UsbOsAdapterOps *UsbAdapterGetOps(void)
1449 {
1450 return &g_usbAdapter;
1451 }
1452
UsbAdapterGetTid(void)1453 UsbRawTidType UsbAdapterGetTid(void)
1454 {
1455 return HDF_FAILURE;
1456 }
1457
UsbAdapterRegisterSignal(void)1458 int32_t UsbAdapterRegisterSignal(void)
1459 {
1460 return HDF_SUCCESS;
1461 }
1462
UsbAdapterKillSignal(struct UsbDeviceHandle * handle,UsbRawTidType tid)1463 int32_t UsbAdapterKillSignal(struct UsbDeviceHandle *handle, UsbRawTidType tid)
1464 {
1465 if ((handle != NULL) && (handle->dev != NULL)) {
1466 struct UsbDevice *dev = handle->dev;
1467 struct OsDev *osDev = (struct OsDev *)dev->privateData;
1468 if (osDev != NULL) {
1469 g_CompleteExit = true;
1470 OsalSemPost(&osDev->cvWait);
1471 HDF_LOGD("%{public}s:%{public}d signal post", __func__, __LINE__);
1472 return HDF_SUCCESS;
1473 } else {
1474 return HDF_FAILURE;
1475 }
1476 } else {
1477 return HDF_FAILURE;
1478 }
1479 }
1480
AdapterAtomicInc(OsalAtomic * v)1481 int32_t AdapterAtomicInc(OsalAtomic *v)
1482 {
1483 int32_t valOld;
1484 int32_t val;
1485 uint32_t status = 0;
1486 Atomic *p = NULL;
1487 if (v) {
1488 p = (Atomic *)&(v)->counter;
1489 } else {
1490 return HDF_FAILURE;
1491 }
1492 do {
1493 __asm__ __volatile__("ldrex %1, [%4]\n"
1494 "mov %0, %1\n"
1495 "add %1, %1, #1\n"
1496 "strex %2, %1, [%4]"
1497 : "=&r"(valOld), "=&r"(val), "=&r"(status), "+m"(*p)
1498 : "r"(p)
1499 : "cc");
1500 } while (__builtin_expect(status != 0, 0));
1501
1502 return valOld;
1503 }
1504
AdapterAtomicDec(OsalAtomic * v)1505 int32_t AdapterAtomicDec(OsalAtomic *v)
1506 {
1507 if (v) {
1508 return LOS_AtomicDecRet((Atomic *)&(v)->counter);
1509 } else {
1510 return HDF_FAILURE;
1511 }
1512 }
1513