Lines Matching defs:ecm

40 static void EcmAllocWriteReq(struct EcmDevice * const ecm);
41 static void EcmFreeWriteReq(struct EcmDevice * const ecm);
42 static int32_t EcmAllocIntReq(struct EcmDevice *ecm);
43 static void EcmAllocReadReq(struct EcmDevice *ecm);
44 static void EcmFreeReadReq(struct EcmDevice *ecm);
45 static int32_t EcmInit(struct EcmDevice *ecm);
46 static void EcmRelease(struct EcmDevice *ecm);
47 static struct UsbInterface *EcmGetUsbInterfaceById(struct EcmDevice *ecm, uint8_t interfaceIndex);
49 static int32_t EcmWbAlloc(struct EcmDevice *ecm)
55 OsalMutexLock(&ecm->writeLock);
57 wb = &ecm->wb[wbn];
61 OsalMutexUnlock(&ecm->writeLock);
66 OsalMutexUnlock(&ecm->writeLock);
70 OsalMutexUnlock(&ecm->writeLock);
73 static UsbInterfaceHandle *InterfaceIdToHandle(const struct EcmDevice *ecm, uint8_t id)
78 devHandle = ecm->ctrDevHandle;
80 for (int32_t i = 0; i < ecm->interfaceCnt; i++) {
81 if (ecm->iface[i]->info.interfaceIndex == id) {
82 devHandle = ecm->devHandle[i];
110 static int32_t EcmWbIsAvail(struct EcmDevice *ecm)
114 OsalMutexLock(&ecm->writeLock);
116 n -= ecm->wb[i].use;
118 OsalMutexUnlock(&ecm->writeLock);
122 static int32_t EcmStartWb(struct EcmDevice *ecm, struct EcmWb *wb)
126 ecm->transmitting++;
127 parmas.interfaceId = ecm->dataOutPipe->interfaceId;
128 parmas.pipeAddress = ecm->dataOutPipe->pipeAddress;
129 parmas.pipeId = ecm->dataOutPipe->pipeId;
137 rc = UsbFillRequest(wb->request, InterfaceIdToHandle(ecm, ecm->dataOutPipe->interfaceId), &parmas);
142 ecm->writeReq = wb->request;
146 OsalMutexLock(&ecm->writeLock);
148 OsalMutexUnlock(&ecm->writeLock);
149 ecm->transmitting--;
151 ecm->writeReqNum++;
156 static int32_t EcmWriteBufAlloc(struct EcmDevice *ecm)
160 for (wb = &ecm->wb[0], i = 0; i < ECM_NW; i++, wb++) {
161 wb->buf = OsalMemCalloc(ecm->writeSize);
175 static int32_t EcmWriteBufFree(struct EcmDevice *ecm)
179 for (wb = &ecm->wb[0], i = 0; i < ECM_NW; i++, wb++) {
192 struct EcmDevice *ecm = wb->ecm;
193 ecm->writeReqNum--;
197 OsalMutexLock(&ecm->writeLock);
199 OsalMutexUnlock(&ecm->writeLock);
226 static int32_t EcmCtrlMsg(struct EcmDevice *ecm, uint8_t request, uint16_t value, void *buf, uint16_t len)
233 if (ecm == NULL) {
237 usbRequest = UsbAllocRequest(ecm->ctrDevHandle, 0, len);
242 ecm->ctrlReq = usbRequest;
252 parmas.pipeAddress = ecm->ctrPipe->pipeAddress;
253 parmas.pipeId = ecm->ctrPipe->pipeId;
258 ret = UsbFillRequest(ecm->ctrlReq, ecm->ctrDevHandle, &parmas);
263 ret = UsbSubmitRequestAsync(ecm->ctrlReq);
268 if (!ecm->ctrlReq->compInfo.status) {
269 HDF_LOGE("%{public}s status=%{public}d ", __func__, ecm->ctrlReq->compInfo.status);
274 static int32_t EcmRead(struct EcmDevice *ecm, struct HdfSBuf *reply)
279 if (ecm == NULL) {
283 if (!(ecm->openFlag)) {
288 if (ecm->readReq[i]->compInfo.status != USB_REQUEST_COMPLETED) {
290 __func__, __LINE__, i, ecm->readReq[i]->compInfo.status);
294 OsalMutexLock(&ecm->readLock);
295 if (DataFifoIsEmpty(&ecm->readFifo)) {
296 OsalMutexUnlock(&ecm->readLock);
299 OsalMutexUnlock(&ecm->readLock);
300 buf = (uint8_t *)OsalMemCalloc(DataFifoLen(&ecm->readFifo) + sizeof(uint32_t));
305 OsalMutexLock(&ecm->readLock);
306 len = DataFifoRead(&ecm->readFifo, buf, DataFifoLen(&ecm->readFifo));
310 OsalMutexUnlock(&ecm->readLock);
313 OsalMutexUnlock(&ecm->readLock);
324 static int32_t EcmOpen(struct EcmDevice *ecm, struct HdfSBuf *data)
329 if ((ecm == NULL) || (data == NULL)) {
339 ret = EcmInit(ecm);
350 ret = EcmAllocFifo(&ecm->readFifo, READ_BUF_SIZE);
355 ecm->openFlag = true;
356 ecm->readReqNum = 0;
357 ecm->writeReqNum = 0;
358 EcmAllocWriteReq(ecm);
359 EcmAllocReadReq(ecm);
361 ret = UsbSubmitRequestAsync(ecm->readReq[i]);
366 ecm->readReqNum++;
371 EcmFreeFifo(&ecm->readFifo);
375 static void EcmClostRelease(struct EcmDevice *ecm)
381 if (!(ecm->openFlag)) {
387 ret = UsbCancelRequest(ecm->readReq[i]);
393 struct EcmWb *snd = &(ecm->wb[i]);
400 while ((cnt < temp) && ((ecm->readReqNum != 0) || (ecm->writeReqNum != 0))) {
404 EcmFreeWriteReq(ecm);
405 EcmFreeReadReq(ecm);
406 EcmFreeFifo(&ecm->readFifo);
407 ecm->openFlag = false;
410 static int32_t EcmClose(struct EcmDevice *ecm, struct HdfSBuf *data)
414 if ((ecm == NULL) || (data == NULL)) {
429 EcmClostRelease(ecm);
430 EcmRelease(ecm);
434 static int32_t EcmWrite(struct EcmDevice *ecm, struct HdfSBuf *data)
444 if (ecm == NULL || ecm->openFlag == false) {
452 if (EcmWbIsAvail(ecm)) {
453 wbn = EcmWbAlloc(ecm);
458 wb = &ecm->wb[wbn];
463 if (size > ecm->writeSize) {
464 len = ecm->writeSize;
465 size -= ecm->writeSize;
471 ret = memcpy_s(wb->buf, ecm->writeSize, tmp, len);
477 wb->ecm = ecm;
478 ret = EcmStartWb(ecm, wb);
487 static int32_t EcmGetMac(struct EcmDevice *ecm, struct HdfSBuf *reply)
489 (void)ecm;
494 static int32_t EcmAddOrRemoveInterface(int32_t cmd, struct EcmDevice *ecm, struct HdfSBuf *data)
498 if (ecm == NULL) {
517 return UsbAddOrRemoveInterface(ecm->session, ecm->busNum, ecm->devAddr, index, status);
523 struct EcmDevice *ecm = NULL;
544 ecm = (struct EcmDevice *)client->device->service;
548 return EcmOpen(ecm, data);
550 return EcmClose(ecm, data);
552 return EcmRead(ecm, reply);
554 return EcmWrite(ecm, data);
556 return EcmGetMac(ecm, reply);
559 return EcmAddOrRemoveInterface(cmd, ecm, data);
567 static struct UsbInterface *EcmGetUsbInterfaceById(struct EcmDevice *ecm, uint8_t interfaceIndex)
569 return UsbClaimInterface(ecm->session, ecm->busNum, ecm->devAddr, interfaceIndex);
572 static void EcmFreePipes(struct EcmDevice *ecm)
574 if (ecm == NULL) {
577 if (ecm->ctrPipe) {
578 OsalMemFree(ecm->ctrPipe);
579 ecm->ctrPipe = NULL;
581 if (ecm->intPipe) {
582 OsalMemFree(ecm->intPipe);
583 ecm->intPipe = NULL;
585 if (ecm->dataInPipe) {
586 OsalMemFree(ecm->dataInPipe);
587 ecm->dataInPipe = NULL;
589 if (ecm->dataOutPipe) {
590 OsalMemFree(ecm->dataOutPipe);
591 ecm->dataOutPipe = NULL;
596 struct EcmDevice *ecm, uint8_t interfaceIndex, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
601 info = &ecm->ctrIface->info;
602 interfaceHandle = ecm->ctrDevHandle;
604 info = &ecm->iface[interfaceIndex]->info;
605 interfaceHandle = ecm->devHandle[interfaceIndex];
628 static struct UsbPipeInfo *EcmGetPipe(struct EcmDevice *ecm, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
631 if (ecm == NULL) {
635 for (i = 0; i < ecm->interfaceCnt; i++) {
637 if (ecm->iface[i] == NULL) {
640 p = EcmEnumePipe(ecm, i, pipeType, pipeDirection);
649 static int32_t EcmGetPipes(struct EcmDevice *ecm)
651 ecm->dataInPipe = EcmGetPipe(ecm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN);
652 if (ecm->dataInPipe == NULL) {
656 ecm->dataOutPipe = EcmGetPipe(ecm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_OUT);
657 if (ecm->dataOutPipe == NULL) {
661 ecm->ctrPipe = EcmEnumePipe(ecm, ecm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT);
662 if (ecm->ctrPipe == NULL) {
666 ecm->intPipe = EcmGetPipe(ecm, USB_PIPE_TYPE_INTERRUPT, USB_PIPE_DIRECTION_IN);
667 if (ecm->intPipe == NULL) {
672 ecm->readSize = ecm->dataInPipe->maxPacketSize;
673 ecm->writeSize = ecm->dataOutPipe->maxPacketSize;
674 ecm->ctrlSize = ecm->ctrPipe->maxPacketSize;
675 ecm->intSize = ecm->intPipe->maxPacketSize;
680 EcmFreePipes(ecm);
688 struct EcmDevice *ecm = NULL;
693 ecm = (struct EcmDevice *)OsalMemCalloc(sizeof(*ecm));
694 if (ecm == NULL) {
703 ecm->busNum = (uint8_t)info->busNum;
704 ecm->devAddr = (uint8_t)info->devNum;
705 ecm->interfaceCnt = info->interfaceLength;
706 err = memcpy_s((void *)(ecm->interfaceIndex), USB_MAX_INTERFACES, (const void *)info->interfaceNumber,
717 ecm->device = device;
718 device->service = &(ecm->service);
719 ecm->device->service->Dispatch = EcmDeviceDispatch;
724 OsalMemFree(ecm);
725 ecm = NULL;
729 static void EcmProcessNotification(struct EcmDevice *ecm, unsigned char *buf)
731 (void)ecm;
747 static void EcmNotificationAndRequest(struct UsbRequest *req, struct EcmDevice *ecm, struct UsbCdcNotification *dr,
751 EcmProcessNotification(ecm, (unsigned char *)dr);
752 ecm->nbIndex = 0;
762 struct EcmDevice *ecm = (struct EcmDevice *)req->compInfo.userData;
776 if (ecm->nbIndex) {
777 dr = (struct UsbCdcNotification *)ecm->notificationBuffer;
781 if (ecm->nbSize < expectedSize) {
782 if (ecm->nbSize) {
783 OsalMemFree(ecm->notificationBuffer);
784 ecm->nbSize = 0;
787 ecm->notificationBuffer = OsalMemCalloc(allocSize);
788 if (!ecm->notificationBuffer) {
791 ecm->nbSize = allocSize;
793 uint32_t copySize = MIN(currentSize, expectedSize - ecm->nbIndex);
795 &ecm->notificationBuffer[ecm->nbIndex], ecm->nbSize - ecm->nbIndex, req->compInfo.buffer, copySize);
798 OsalMemFree(ecm->notificationBuffer);
799 ecm->notificationBuffer = NULL;
802 ecm->nbIndex += copySize;
803 currentSize = ecm->nbIndex;
805 EcmNotificationAndRequest(req, ecm, dr, currentSize, expectedSize);
814 struct EcmDevice *ecm = (struct EcmDevice *)req->compInfo.userData;
815 ecm->readReqNum--;
818 OsalMutexLock(&ecm->readLock);
821 if (DataFifoIsFull(&ecm->readFifo)) {
823 DataFifoSkip(&ecm->readFifo, size);
825 uint32_t count = DataFifoWrite(&ecm->readFifo, data, size);
830 OsalMutexUnlock(&ecm->readLock);
837 if (ecm->openFlag) {
842 ecm->readReqNum++;
847 static void EcmAllocWriteReq(struct EcmDevice * const ecm)
849 if (EcmWriteBufAlloc(ecm) < 0) {
855 struct EcmWb *snd = &(ecm->wb[i]);
856 snd->request = UsbAllocRequest(InterfaceIdToHandle(ecm, ecm->dataOutPipe->interfaceId), 0, ecm->writeSize);
857 snd->instance = ecm;
866 EcmWriteBufFree(ecm);
870 static void EcmFreeWriteReq(struct EcmDevice * const ecm)
872 OsalMutexLock(&ecm->writeLock);
874 struct EcmWb *snd = &(ecm->wb[i]);
878 OsalMutexUnlock(&ecm->writeLock);
879 EcmWriteBufFree(ecm);
882 static int32_t EcmAllocIntReq(struct EcmDevice *ecm)
886 if (ecm == NULL || ecm->intPipe == NULL) {
889 ecm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(ecm, ecm->intPipe->interfaceId), 0, ecm->intSize);
890 if (!ecm->notifyReq) {
894 intParmas.userData = (void *)ecm;
895 intParmas.pipeAddress = ecm->intPipe->pipeAddress;
896 intParmas.pipeId = ecm->intPipe->pipeId;
897 intParmas.interfaceId = ecm->intPipe->interfaceId;
902 intParmas.dataReq.directon = (((uint32_t)(ecm->intPipe->pipeDirection)) >> USB_DIR_OFFSET) & 0x1;
903 intParmas.dataReq.length = ecm->intSize;
904 ret = UsbFillRequest(ecm->notifyReq, InterfaceIdToHandle(ecm, ecm->intPipe->interfaceId), &intParmas);
912 static void EcmAllocReadReq(struct EcmDevice *ecm)
916 ecm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(ecm, ecm->dataInPipe->interfaceId), 0, ecm->readSize);
917 if (!ecm->readReq[i]) {
921 readParmas.userData = (void *)ecm;
922 readParmas.pipeAddress = ecm->dataInPipe->pipeAddress;
923 readParmas.pipeId = ecm->dataInPipe->pipeId;
924 readParmas.interfaceId = ecm->dataInPipe->interfaceId;
929 readParmas.dataReq.directon = (((uint32_t)(ecm->dataInPipe->pipeDirection)) >> USB_DIR_OFFSET) & 0x1;
930 readParmas.dataReq.length = ecm->readSize;
932 UsbFillRequest(ecm->readReq[i], InterfaceIdToHandle(ecm, ecm->dataInPipe->interfaceId), &readParmas);
940 static void EcmFreeReadReq(struct EcmDevice *ecm)
944 ret = UsbFreeRequest(ecm->readReq[i]);
953 static void UsbFreeNotifyReqeust(struct EcmDevice *ecm)
957 if ((ecm == NULL) || (ecm->notifyReq == NULL)) {
958 HDF_LOGE("%{public}s: ecm or notifyReq is NULL", __func__);
961 ret = UsbCancelRequest(ecm->notifyReq);
965 ret = UsbFreeRequest(ecm->notifyReq);
967 ecm->notifyReq = NULL;
973 static void EcmReleaseInterfaces(struct EcmDevice *ecm)
975 for (uint8_t i = 0; i < ecm->interfaceCnt; i++) {
976 if (ecm->iface[i]) {
977 UsbReleaseInterface(ecm->iface[i]);
978 ecm->iface[i] = NULL;
981 if (ecm->ctrIface) {
982 UsbReleaseInterface(ecm->ctrIface);
983 ecm->ctrIface = NULL;
987 static int32_t EcmClaimInterfaces(struct EcmDevice *ecm)
989 for (uint8_t i = 0; i < ecm->interfaceCnt; i++) {
990 ecm->iface[i] = EcmGetUsbInterfaceById(ecm, ecm->interfaceIndex[i]);
991 if (ecm->iface[i] == NULL) {
992 HDF_LOGE("interface%{public}d is null", ecm->interfaceIndex[i]);
997 ecm->ctrIface = EcmGetUsbInterfaceById(ecm, USB_CTRL_INTERFACE_ID);
998 if (ecm->ctrIface == NULL) {
1006 EcmReleaseInterfaces(ecm);
1010 static void EcmCloseInterfaces(struct EcmDevice *ecm)
1012 for (uint8_t i = 0; i < ecm->interfaceCnt; i++) {
1013 if (ecm->devHandle[i]) {
1014 UsbCloseInterface(ecm->devHandle[i], false);
1015 ecm->devHandle[i] = NULL;
1019 if (ecm->ctrDevHandle) {
1020 UsbCloseInterface(ecm->ctrDevHandle, false);
1021 ecm->ctrDevHandle = NULL;
1025 static int32_t EcmOpenInterfaces(struct EcmDevice *ecm)
1027 for (uint8_t i = 0; i < ecm->interfaceCnt; i++) {
1028 if (ecm->iface[i]) {
1029 ecm->devHandle[i] = UsbOpenInterface(ecm->iface[i]);
1030 if (ecm->devHandle[i] == NULL) {
1037 ecm->ctrDevHandle = UsbOpenInterface(ecm->ctrIface);
1038 if (ecm->ctrDevHandle == NULL) {
1046 EcmCloseInterfaces(ecm);
1050 static int32_t EcmGetPipesAndRequest(struct EcmDevice *ecm, int32_t ret)
1052 ret = EcmGetPipes(ecm);
1058 ret = EcmAllocIntReq(ecm);
1064 EcmCtrlMsg(ecm, USB_DDK_CDC_SET_ETHERNET_PACKET_FILTER,
1066 ret = UsbSubmitRequestAsync(ecm->notifyReq);
1071 ecm->initFlag = true;
1074 EcmFreePipes(ecm);
1076 UsbFreeNotifyReqeust(ecm);
1080 static int32_t EcmInit(struct EcmDevice *ecm)
1085 if (ecm->initFlag == true) {
1094 ecm->session = NULL;
1096 ret = EcmClaimInterfaces(ecm);
1102 ret = EcmOpenInterfaces(ecm);
1108 if (ecm->interfaceCnt > USB_MAX_INTERFACES) {
1109 HDF_LOGE("interfaceCnt invalid : %{public}u", ecm->interfaceCnt);
1114 ecm->devHandle[ecm->interfaceCnt - 1], altsetting, &ecm->iface[ecm->interfaceCnt - 1]); // set altsetting
1119 EcmGetPipesAndRequest(ecm, ret);
1121 EcmCloseInterfaces(ecm);
1123 EcmReleaseInterfaces(ecm);
1125 UsbExitHostSdk(ecm->session);
1126 ecm->session = NULL;
1130 static void EcmRelease(struct EcmDevice *ecm)
1132 if (!(ecm->initFlag)) {
1137 EcmCloseInterfaces(ecm);
1138 EcmReleaseInterfaces(ecm);
1139 UsbFreeNotifyReqeust(ecm);
1140 EcmFreePipes(ecm);
1141 UsbExitHostSdk(ecm->session);
1143 ecm->initFlag = false;
1148 struct EcmDevice *ecm = NULL;
1154 ecm = (struct EcmDevice *)device->service;
1155 if (ecm == NULL) {
1156 HDF_LOGE("%{public}s: ecm is null", __func__);
1160 OsalMutexInit(&ecm->readLock);
1161 OsalMutexInit(&ecm->writeLock);
1162 ecm->openFlag = false;
1163 ecm->initFlag = false;
1172 struct EcmDevice *ecm = NULL;
1177 ecm = (struct EcmDevice *)device->service;
1178 if (ecm == NULL) {
1179 HDF_LOGE("%{public}s: ecm is null", __func__);
1185 if (ecm->openFlag) {
1187 EcmClostRelease(ecm);
1189 if (ecm->initFlag) {
1191 EcmRelease(ecm);
1193 OsalMutexDestroy(&ecm->writeLock);
1194 OsalMutexDestroy(&ecm->readLock);
1195 OsalMemFree(ecm);
1196 ecm = NULL;