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