1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 - 2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "usb_host_config.h"
10 #if (defined(USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS > 0U))
11 #include "usb_host.h"
12 #include "usb_host_hci.h"
13 #include "fsl_device_registers.h"
14 #include "usb_host_ip3516hs.h"
15 #include "usb_host_devices.h"
16 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
17 #include "usb_phy.h"
18 #endif
19 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
20     ((defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)))
21 #include "usb_hsdcd.h"
22 #endif
23 #if ((USB_HOST_CONFIG_IP3516HS_MAX_ISO >= 256) || (USB_HOST_CONFIG_IP3516HS_MAX_INT >= 256) || \
24      (USB_HOST_CONFIG_IP3516HS_MAX_ATL >= 256))
25 #error "This driver doesn't support same type pipe exceed 255."
26 #endif
27 /*******************************************************************************
28  * Definitions
29  ******************************************************************************/
30 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
31 #define USB_HOST_IP3516HS_TEST_DESCRIPTOR_LENGTH  (18U)
32 #define USB_HOST_IP3516HS_PORTSC_PTC_J_STATE      (0x01U)
33 #define USB_HOST_IP3516HS_PORTSC_PTC_K_STATE      (0x02U)
34 #define USB_HOST_IP3516HS_PORTSC_PTC_SE0_NAK      (0x03U)
35 #define USB_HOST_IP3516HS_PORTSC_PTC_PACKET       (0x04U)
36 #define USB_HOST_IP3516HS_PORTSC_PTC_FORCE_ENABLE (0x05U)
37 #endif
38 /* reset recovery time (ms) */
39 #define USB_HOST_IP3516HS_PORT_RESET_RECOVERY_DELAY (11U)
40 /*******************************************************************************
41  * Prototypes
42  ******************************************************************************/
43 static usb_status_t USB_HostIp3516HsGetBuffer(usb_host_ip3516hs_state_struct_t *usbHostState,
44                                               uint32_t length,
45                                               uint32_t MaxPacketSize,
46                                               uint32_t *index,
47                                               uint32_t *bufferLength);
48 static usb_status_t USB_HostIp3516HsFreeBuffer(usb_host_ip3516hs_state_struct_t *usbHostState,
49                                                uint32_t index,
50                                                uint32_t bufferLength);
51 
52 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
53 /*!
54  * @brief suspend bus.
55  *
56  * @param usbHostState      ip3516hs instance pointer.
57  */
58 static void USB_HostIp3516HsSuspendBus(usb_host_ip3516hs_state_struct_t *usbHostState);
59 
60 /*!
61  * @brief resume bus.
62  *
63  * @param usbHostState        ip3516hs instance pointer.
64  */
65 static void USB_HostIp3516HsResumeBus(usb_host_ip3516hs_state_struct_t *usbHostState);
66 
67 extern usb_status_t USB_HostStandardSetGetDescriptor(usb_host_device_instance_t *deviceInstance,
68                                                      usb_host_transfer_t *transfer,
69                                                      void *param);
70 
71 static void USB_HostIp3516HsDelay(usb_host_ip3516hs_state_struct_t *usbHostState, uint32_t ms);
72 static void USB_HostIp3516HsTestModeInit(usb_device_handle deviceHandle);
73 #endif
74 
75 /*******************************************************************************
76  * Variables
77  ******************************************************************************/
78 
79 USB_GLOBAL_DEDICATED_RAM USB_RAM_ADDRESS_ALIGNMENT(1024) static usb_host_ip3516hs_ptd_struct_t
80     s_UsbHostIp3516HsPtd[USB_HOST_CONFIG_IP3516HS];
81 static usb_host_ip3516hs_state_struct_t s_UsbHostIp3516HsState[USB_HOST_CONFIG_IP3516HS];
82 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U)) || \
83     (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
84 static uint8_t s_SlotMaxBandwidth[8] = {125, 125, 125, 125, 125, 125, 50, 0};
85 #endif
86 USB_GLOBAL_DEDICATED_RAM static uint8_t s_UsbHostIp3516HsBufferArray[80][64];
87 /*******************************************************************************
88  * Code
89  ******************************************************************************/
90 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
USB_HostIp3516HsTestSetMode(usb_host_ip3516hs_state_struct_t * usbHostState,uint32_t testMode)91 static usb_status_t USB_HostIp3516HsTestSetMode(usb_host_ip3516hs_state_struct_t *usbHostState, uint32_t testMode)
92 {
93     uint32_t Ip3516HsortSC;
94 
95     Ip3516HsortSC = usbHostState->usbRegBase->PORTSC1;
96     Ip3516HsortSC &= ~((uint32_t)USBHSH_PORTSC1_PTC_MASK);   /* clear test mode bits */
97     Ip3516HsortSC |= (testMode << USBHSH_PORTSC1_PTC_SHIFT); /* set test mode bits */
98     usbHostState->usbRegBase->PORTSC1 = Ip3516HsortSC;
99     return kStatus_USB_Success;
100 }
101 
USB_HostIp3516HsTestSuspendResume(usb_host_ip3516hs_state_struct_t * usbHostState)102 static void USB_HostIp3516HsTestSuspendResume(usb_host_ip3516hs_state_struct_t *usbHostState)
103 {
104     uint8_t timeCount;
105     timeCount = 15U * 8U; /* 15s */
106     while (0U != (timeCount--))
107     {
108         USB_HostIp3516HsDelay(usbHostState, 1000U);
109     }
110     USB_HostIp3516HsSuspendBus(usbHostState);
111     timeCount = 15U * 8U; /* 15s */
112     while (0U != (timeCount--))
113     {
114         USB_HostIp3516HsDelay(usbHostState, 1000U);
115     }
116 
117     USB_HostIp3516HsResumeBus(usbHostState);
118 }
119 
USB_HostIp3516HsTestCallback(void * param,usb_host_transfer_t * transfer,usb_status_t status)120 static void USB_HostIp3516HsTestCallback(void *param, usb_host_transfer_t *transfer, usb_status_t status)
121 {
122     (void)USB_HostFreeTransfer(param, transfer);
123     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)param;
124     if (1U == usbHostState->complianceTestStart)
125     {
126         uint8_t timeCount;
127         usbHostState->complianceTest      = 0U;
128         usbHostState->complianceTestStart = 0U;
129         timeCount                         = 15U * 8U; /* 15s */
130         while (0U != (timeCount--))
131         {
132             USB_HostIp3516HsDelay(usbHostState, 1000U);
133         }
134         (void)usb_echo("test_single_step_get_dev_desc_data finished\r\n");
135     }
136 }
137 
USB_HostIp3516HsTestSingleStepGetDeviceDesc(usb_host_ip3516hs_state_struct_t * usbHostState,usb_device_handle deviceHandle)138 static void USB_HostIp3516HsTestSingleStepGetDeviceDesc(usb_host_ip3516hs_state_struct_t *usbHostState,
139                                                         usb_device_handle deviceHandle)
140 {
141     usb_host_process_descriptor_param_t getDescriptorParam;
142     usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
143     usb_host_transfer_t *transfer;
144     uint8_t timeCount;
145 
146     /* disable periodic shedule */
147     usbHostState->usbRegBase->USBCMD &= ~(USB_HOST_IP3516HS_USBCMD_INT_EN_MASK | USB_HOST_IP3516HS_USBCMD_ISO_EN_MASK);
148 
149     timeCount = 15U * 8U; /* 15s */
150     while (0U != (timeCount--))
151     {
152         USB_HostIp3516HsDelay(usbHostState, 1000U);
153     }
154 
155     /* malloc one transfer */
156     if (USB_HostMallocTransfer(usbHostState->hostHandle, &transfer) != kStatus_USB_Success)
157     {
158 #ifdef HOST_ECHO
159         usb_echo("allocate transfer error\r\n");
160 #endif
161         return;
162     }
163 
164     getDescriptorParam.descriptorLength  = sizeof(usb_descriptor_device_t);
165     getDescriptorParam.descriptorLength  = 18;
166     getDescriptorParam.descriptorBuffer  = (uint8_t *)&deviceInstance->deviceDescriptor;
167     getDescriptorParam.descriptorType    = USB_DESCRIPTOR_TYPE_DEVICE;
168     getDescriptorParam.descriptorIndex   = 0;
169     getDescriptorParam.languageId        = 0;
170     transfer->callbackFn                 = USB_HostIp3516HsTestCallback;
171     transfer->callbackParam              = usbHostState->hostHandle;
172     transfer->setupPacket->bmRequestType = USB_REQUEST_TYPE_DIR_IN;
173     transfer->setupPacket->bRequest      = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
174     transfer->setupPacket->wIndex        = 0;
175     transfer->setupPacket->wLength       = 0;
176     transfer->setupPacket->wValue        = 0;
177     (void)USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
178 }
179 
USB_HostIp3516HsTestSingleStepGetDeviceDescData(usb_host_ip3516hs_state_struct_t * usbHostState,usb_device_handle deviceHandle)180 static void USB_HostIp3516HsTestSingleStepGetDeviceDescData(usb_host_ip3516hs_state_struct_t *usbHostState,
181                                                             usb_device_handle deviceHandle)
182 {
183     static uint8_t buffer[USB_HOST_IP3516HS_TEST_DESCRIPTOR_LENGTH];
184 
185     usb_host_process_descriptor_param_t getDescriptorParam;
186     usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
187     usb_host_transfer_t *transfer;
188 
189     /* disable periodic shedule */
190     usbHostState->usbRegBase->USBCMD &= ~(USB_HOST_IP3516HS_USBCMD_INT_EN_MASK | USB_HOST_IP3516HS_USBCMD_ISO_EN_MASK);
191 
192     /* malloc one transfer */
193     if (USB_HostMallocTransfer(usbHostState->hostHandle, &transfer) != kStatus_USB_Success)
194     {
195 #ifdef HOST_ECHO
196         usb_echo("allocate transfer error\r\n");
197 #endif
198         return;
199     }
200     usbHostState->complianceTestStart    = 1;
201     getDescriptorParam.descriptorLength  = sizeof(usb_descriptor_device_t);
202     getDescriptorParam.descriptorLength  = 18;
203     getDescriptorParam.descriptorBuffer  = (uint8_t *)buffer;
204     getDescriptorParam.descriptorType    = USB_DESCRIPTOR_TYPE_DEVICE;
205     getDescriptorParam.descriptorIndex   = 0;
206     getDescriptorParam.languageId        = 0;
207     transfer->callbackFn                 = USB_HostIp3516HsTestCallback;
208     transfer->callbackParam              = (void *)usbHostState;
209     transfer->setupPacket->bmRequestType = USB_REQUEST_TYPE_DIR_IN;
210     transfer->setupPacket->bRequest      = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
211     transfer->setupPacket->wIndex        = 0;
212     transfer->setupPacket->wLength       = 0;
213     transfer->setupPacket->wValue        = 0;
214     (void)USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
215 
216     return;
217 }
218 
USB_HostIp3516HsTestModeInit(usb_device_handle deviceHandle)219 static void USB_HostIp3516HsTestModeInit(usb_device_handle deviceHandle)
220 {
221     uint32_t productId;
222     usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
223     usb_host_ip3516hs_state_struct_t *usbHostState =
224         (usb_host_ip3516hs_state_struct_t *)(((usb_host_instance_t *)(deviceInstance->hostHandle))->controllerHandle);
225 
226     (void)USB_HostHelperGetPeripheralInformation(deviceHandle, (uint32_t)kUSB_HostGetDevicePID, &productId);
227 
228     (void)usb_echo("usb host Ip3516hs test mode init  product id:0x%x\r\n", productId);
229 
230     switch (productId)
231     {
232         case 0x0101U:
233             (void)USB_HostIp3516HsTestSetMode(usbHostState, USB_HOST_IP3516HS_PORTSC_PTC_SE0_NAK);
234             break;
235         case 0x0102U:
236             (void)USB_HostIp3516HsTestSetMode(usbHostState, USB_HOST_IP3516HS_PORTSC_PTC_J_STATE);
237             break;
238         case 0x0103U:
239             (void)USB_HostIp3516HsTestSetMode(usbHostState, USB_HOST_IP3516HS_PORTSC_PTC_K_STATE);
240             break;
241         case 0x0104U:
242             (void)USB_HostIp3516HsTestSetMode(usbHostState, USB_HOST_IP3516HS_PORTSC_PTC_PACKET);
243             break;
244         case 0x0105U:
245             (void)usb_echo("set test mode FORCE_ENALBE\r\n");
246             (void)USB_HostIp3516HsTestSetMode(usbHostState, USB_HOST_IP3516HS_PORTSC_PTC_FORCE_ENABLE);
247             break;
248         case 0x0106U:
249             USB_HostIp3516HsTestSuspendResume(usbHostState);
250             break;
251         case 0x0107U:
252             (void)usb_echo("start test SINGLE_STEP_GET_DEV_DESC\r\n");
253             USB_HostIp3516HsTestSingleStepGetDeviceDesc(usbHostState, deviceHandle);
254             break;
255         case 0x0108U:
256             (void)usb_echo("start test SINGLE_STEP_GET_DEV_DESC_DATA\r\n");
257             USB_HostIp3516HsTestSingleStepGetDeviceDescData(usbHostState, deviceHandle);
258             break;
259         default:
260             /*default*/
261             break;
262     }
263 
264     return;
265 }
266 
USB_HostIp3516HsSuspendBus(usb_host_ip3516hs_state_struct_t * usbHostState)267 static void USB_HostIp3516HsSuspendBus(usb_host_ip3516hs_state_struct_t *usbHostState)
268 {
269     uint32_t portStatus;
270     portStatus = usbHostState->usbRegBase->PORTSC1;
271     USB_HostIp3516HsLock();
272     if (0U != (portStatus & USB_HOST_IP3516HS_PORTSC1_PED_MASK))
273     {
274         portStatus = usbHostState->usbRegBase->PORTSC1;
275         portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_SUS_L1_MASK);
276         portStatus |= USB_HOST_IP3516HS_PORTSC1_SUSP_MASK;
277         usbHostState->usbRegBase->PORTSC1 = portStatus;
278     }
279 
280     USB_HostIp3516HsUnlock();
281 }
282 
USB_HostIp3516HsResumeBus(usb_host_ip3516hs_state_struct_t * usbHostState)283 static void USB_HostIp3516HsResumeBus(usb_host_ip3516hs_state_struct_t *usbHostState)
284 {
285     uint32_t portStatus;
286 
287     USB_HostIp3516HsLock();
288     /* Resume port */
289     portStatus = usbHostState->usbRegBase->PORTSC1;
290     if (portStatus & USB_HOST_IP3516HS_PORTSC1_PED_MASK)
291     {
292         portStatus = usbHostState->usbRegBase->PORTSC1;
293         portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC);
294         portStatus |= USB_HOST_IP3516HS_PORTSC1_FPR_MASK;
295         usbHostState->usbRegBase->PORTSC1 = portStatus;
296         /**resume 21ms = 168/8*/
297         USB_HostIp3516HsDelay(usbHostState, 168U);
298 
299         portStatus = usbHostState->usbRegBase->PORTSC1;
300         portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_FPR_MASK);
301         portStatus |= USB_HOST_IP3516HS_PORTSC1_PP_MASK | USB_HOST_IP3516HS_PORTSC1_PED_MASK;
302         usbHostState->usbRegBase->PORTSC1 = portStatus;
303     }
304     USB_HostIp3516HsUnlock();
305 }
306 #endif
307 
USB_HostOhciDisableIsr(usb_host_ip3516hs_state_struct_t * usbHostState)308 static void USB_HostOhciDisableIsr(usb_host_ip3516hs_state_struct_t *usbHostState)
309 {
310     OSA_SR_ALLOC();
311 
312     /* Enter critical */
313     OSA_ENTER_CRITICAL();
314     if (0U == usbHostState->isrLevel)
315     {
316         NVIC_DisableIRQ((IRQn_Type)usbHostState->isrNumber);
317     }
318     usbHostState->isrLevel++;
319     OSA_EXIT_CRITICAL();
320 }
321 
USB_HostOhciEnableIsr(usb_host_ip3516hs_state_struct_t * usbHostState)322 static void USB_HostOhciEnableIsr(usb_host_ip3516hs_state_struct_t *usbHostState)
323 {
324     OSA_SR_ALLOC();
325 
326     /* Enter critical */
327     OSA_ENTER_CRITICAL();
328     usbHostState->isrLevel--;
329     if (0U == usbHostState->isrLevel)
330     {
331         NVIC_EnableIRQ((IRQn_Type)usbHostState->isrNumber);
332     }
333     OSA_EXIT_CRITICAL();
334 }
335 
USB_HostIp3516HsDelay(usb_host_ip3516hs_state_struct_t * usbHostState,uint32_t ms)336 static void USB_HostIp3516HsDelay(usb_host_ip3516hs_state_struct_t *usbHostState, uint32_t ms)
337 {
338     /* note: the max delay time cannot exceed half of max value (0x4000) */
339     uint32_t sofStart;
340     uint32_t SofEnd;
341     uint32_t distance;
342 
343     sofStart = (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
344                           USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
345 
346     do
347     {
348         SofEnd   = (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
349                             USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
350         distance = (uint32_t)(
351             (SofEnd - sofStart + (USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK >> USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT) + 1U) &
352             (USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK >> USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT));
353     } while ((distance) < (ms)); /* compute the distance between sofStart and SofEnd */
354 }
355 /*seperate bus control to standlone alone function for misra Rule17.2*/
USB_HostIp3516HsControlBusReset(usb_host_ip3516hs_state_struct_t * usbHostState)356 static usb_status_t USB_HostIp3516HsControlBusReset(usb_host_ip3516hs_state_struct_t *usbHostState)
357 {
358     uint32_t portStatus = usbHostState->usbRegBase->PORTSC1;
359     usb_status_t status = kStatus_USB_Success;
360     portStatus &= ~USB_HOST_IP3516HS_PORTSC1_WIC;
361     usbHostState->usbRegBase->PORTSC1 = portStatus | USB_HOST_IP3516HS_PORTSC1_PR_MASK;
362 
363     USB_HostIp3516HsDelay(usbHostState, 200);
364 
365     portStatus = usbHostState->usbRegBase->PORTSC1;
366     portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_PR_MASK);
367     usbHostState->usbRegBase->PORTSC1 = portStatus;
368     while (0U != (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_PR_MASK))
369     {
370         __NOP();
371     }
372     return status;
373 }
374 
USB_HostIp3516HsControlBus(usb_host_ip3516hs_state_struct_t * usbHostState,uint8_t busControl)375 static usb_status_t USB_HostIp3516HsControlBus(usb_host_ip3516hs_state_struct_t *usbHostState, uint8_t busControl)
376 {
377 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
378     uint32_t portStatus = usbHostState->usbRegBase->PORTSC1;
379 #endif
380     usb_status_t status                = kStatus_USB_Success;
381     usb_host_bus_control_t controlCode = (usb_host_bus_control_t)busControl;
382     switch (controlCode)
383     {
384         case kUSB_HostBusReset:
385             (void)USB_HostIp3516HsControlBusReset(usbHostState);
386             break;
387 
388         case kUSB_HostBusRestart:
389             break;
390 
391 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
392         case kUSB_HostBusSuspend:
393             if (0U != (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_CCS_MASK))
394             {
395                 usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
396 
397                 portStatus = usbHostState->usbRegBase->PORTSC1;
398                 portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_SUS_L1_MASK);
399                 portStatus |= USB_HOST_IP3516HS_PORTSC1_SUSP_MASK;
400                 usbHostState->usbRegBase->PORTSC1 = portStatus;
401 
402                 usbHostState->matchTick        = hostPointer->hwTick;
403                 usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsStartSuspend;
404 
405                 while ((hostPointer->hwTick - usbHostState->matchTick) <= 5U)
406                 {
407                 }
408                 portStatus = usbHostState->usbRegBase->USBCMD;
409                 portStatus &= ~USB_HOST_IP3516HS_USBCMD_RS_MASK;
410                 usbHostState->usbRegBase->USBCMD = portStatus;
411                 /* call host callback function, function is initialized in USB_HostInit */
412                 (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
413                                                   kUSB_HostEventSuspended); /* call host callback function */
414                 usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsSuspended;
415             }
416             else
417             {
418                 status = kStatus_USB_Error;
419             }
420             break;
421 
422         case kUSB_HostBusResume:
423             if (0U != (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_CCS_MASK))
424             {
425                 usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
426 
427                 usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsStartResume;
428                 usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
429                 portStatus = usbHostState->usbRegBase->PORTSC1;
430                 portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC);
431                 portStatus |= USB_HOST_IP3516HS_PORTSC1_FPR_MASK;
432                 usbHostState->usbRegBase->PORTSC1 = portStatus;
433                 usbHostState->matchTick           = hostPointer->hwTick;
434                 while ((hostPointer->hwTick - usbHostState->matchTick) <= 20U)
435                 {
436                 }
437                 portStatus = usbHostState->usbRegBase->PORTSC1;
438                 portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_FPR_MASK);
439                 portStatus |= USB_HOST_IP3516HS_PORTSC1_PP_MASK | USB_HOST_IP3516HS_PORTSC1_PED_MASK;
440                 usbHostState->usbRegBase->PORTSC1 = portStatus;
441                 /* call host callback function, function is initialized in USB_HostInit */
442                 (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
443                                                   kUSB_HostEventResumed); /* call host callback function */
444                 hostPointer->suspendedDevice   = NULL;
445                 usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
446             }
447             else
448             {
449                 usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
450                 usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
451                 portStatus = usbHostState->usbRegBase->PORTSC1;
452                 portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_FPR_MASK);
453                 portStatus |= USB_HOST_IP3516HS_PORTSC1_PP_MASK | USB_HOST_IP3516HS_PORTSC1_PED_MASK;
454                 usbHostState->usbRegBase->PORTSC1 = portStatus;
455                 hostPointer->suspendedDevice      = NULL;
456                 usbHostState->busSuspendStatus    = (uint8_t)kBus_Ip3516HsIdle;
457 
458                 status = kStatus_USB_Error;
459             }
460             break;
461 #if ((defined(USB_HOST_CONFIG_LPM_L1)) && (USB_HOST_CONFIG_LPM_L1 > 0U))
462         case kUSB_HostBusL1Sleep:
463             if (0U != (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_CCS_MASK))
464             {
465                 usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
466 
467                 portStatus = usbHostState->usbRegBase->USBCMD;
468                 portStatus &= ~(USB_HOST_IP3516HS_USBCMD_HIRD_MASK | USB_HOST_IP3516HS_USBCMD_LPM_RWU_MASK);
469                 portStatus |= (uint32_t)(
470                     ((uint32_t)usbHostState->hirdValue << USB_HOST_IP3516HS_USBCMD_HIRD_SHIFT) |
471                     ((uint32_t)usbHostState->L1remoteWakeupEnable << USB_HOST_IP3516HS_USBCMD_LPM_RWU_SHIFT));
472                 usbHostState->usbRegBase->USBCMD = portStatus;
473 
474                 usb_host_device_instance_t *deviceInstance;
475 
476                 usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsL1StartSleep;
477 
478                 deviceInstance = (usb_host_device_instance_t *)hostPointer->suspendedDevice;
479                 usbHostState->usbRegBase->PORTSC1 |= (uint32_t)(
480                     (uint32_t)USB_HOST_IP3516HS_PORTSC1_SUSP_MASK | (uint32_t)USB_HOST_IP3516HS_PORTSC1_SUS_L1_MASK |
481                     (((uint32_t)deviceInstance->setAddress << USB_HOST_IP3516HS_PORTSC1_DEV_ADD_SHIFT) &
482                      (uint32_t)USB_HOST_IP3516HS_PORTSC1_DEV_ADD_MASK));
483 #if (defined(FSL_FEATURE_USBHSH_VERSION) && (FSL_FEATURE_USBHSH_VERSION >= 300U))
484 #else
485                 while (0U == (usbHostState->usbRegBase->PORTSC1 &
486                               (uint32_t)((uint32_t)USB_HOST_IP3516HS_PORTSC1_SUSP_MASK |
487                                          (uint32_t)USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK)))
488                 {
489                     __NOP();
490                 }
491 
492                 portStatus = usbHostState->usbRegBase->PORTSC1;
493 
494                 if ((0U != (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_SUSP_MASK)) &&
495                     (0x00U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
496                                USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT)))
497                 {
498                     usbHostState->matchTick = hostPointer->hwTick;
499                     while ((hostPointer->hwTick - usbHostState->matchTick) <= 4U)
500                     {
501                     }
502                     portStatus = usbHostState->usbRegBase->USBCMD;
503                     portStatus &= ~USB_HOST_IP3516HS_USBCMD_RS_MASK;
504                     usbHostState->usbRegBase->USBCMD = portStatus;
505 
506                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsL1Sleeped;
507                     /* call host callback function, function is initialized in USB_HostInit */
508                     (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
509                                                       kUSB_HostEventL1Sleeped); /* call host callback function */
510                 }
511                 else if (0x02U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
512                                    USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT))
513                 {
514                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
515                     /* call host callback function, function is initialized in USB_HostInit */
516                     (void)hostPointer->deviceCallback(
517                         hostPointer->suspendedDevice, NULL,
518                         kUSB_HostEventL1SleepNotSupport); /* call host callback function */
519                 }
520                 else if (0x01U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
521                                    USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT))
522                 {
523                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
524                     /* call host callback function, function is initialized in USB_HostInit */
525                     (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
526                                                       kUSB_HostEventL1SleepNYET); /* call host callback function */
527                 }
528                 else if (0x03U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
529                                    USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT))
530                 {
531                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
532                     /* call host callback function, function is initialized in USB_HostInit */
533                     (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
534                                                       kUSB_HostEventL1SleepError); /* call host callback function */
535                 }
536                 else
537                 {
538                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
539                 }
540 #endif
541             }
542             else
543             {
544                 status = kStatus_USB_Error;
545             }
546             break;
547 
548         case kUSB_HostBusL1Resume:
549             if (0U != (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_CCS_MASK))
550             {
551                 usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
552 
553                 usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsStartResume;
554                 usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
555                 portStatus = usbHostState->usbRegBase->PORTSC1;
556                 portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC);
557                 portStatus |= USB_HOST_IP3516HS_PORTSC1_FPR_MASK;
558                 usbHostState->usbRegBase->PORTSC1 = portStatus;
559                 while (0U != ((portStatus)&USB_HOST_IP3516HS_PORTSC1_FPR_MASK))
560                 {
561                     portStatus = (usbHostState->usbRegBase->PORTSC1);
562                     portStatus = portStatus & USB_HOST_IP3516HS_PORTSC1_FPR_MASK;
563                 }
564                 usbHostState->matchTick = hostPointer->hwTick;
565                 while ((hostPointer->hwTick - usbHostState->matchTick) <= 5U)
566                 {
567                 }
568                 /* call host callback function, function is initialized in USB_HostInit */
569                 (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
570                                                   kUSB_HostEventL1Resumed); /* call host callback function */
571                 hostPointer->suspendedDevice   = NULL;
572                 usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
573             }
574             else
575             {
576                 usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
577                 usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
578                 portStatus = usbHostState->usbRegBase->PORTSC1;
579                 portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_FPR_MASK);
580                 portStatus |= USB_HOST_IP3516HS_PORTSC1_PP_MASK | USB_HOST_IP3516HS_PORTSC1_PED_MASK;
581                 usbHostState->usbRegBase->PORTSC1 = portStatus;
582                 hostPointer->suspendedDevice      = NULL;
583                 usbHostState->busSuspendStatus    = (uint8_t)kBus_Ip3516HsIdle;
584 
585                 status = kStatus_USB_Error;
586             }
587             break;
588 #endif
589 #endif
590         case kUSB_HostBusEnableAttach:
591             if (0UL != (usbHostState->usbRegBase->HCSPARAMS &
592                         USB_HOST_IP3516HS_HCSPARAMS_PPC_MASK)) /* Ports have power port switches */
593             {
594                 /* only has one port */
595                 uint32_t tmp = usbHostState->usbRegBase->PORTSC1;
596                 tmp &= (~USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_PP_MASK);
597                 usbHostState->usbRegBase->PORTSC1 = (tmp); /* turn on port power */
598 
599                 tmp = usbHostState->usbRegBase->PORTSC1;
600                 tmp &= (~USB_HOST_IP3516HS_PORTSC1_WIC);
601                 usbHostState->usbRegBase->PORTSC1 = (tmp | USB_HOST_IP3516HS_PORTSC1_PP_MASK); /* turn on port power */
602             }
603             (void)USB_HostIp3516HsControlBusReset(usbHostState);
604             usbHostState->portState[0].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortDetached;
605             (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_PORT_CHANGE);
606             break;
607         case kUSB_HostBusDisableAttach:
608             break;
609         default:
610             status = kStatus_USB_Error;
611             break;
612     }
613     return status;
614 }
615 
616 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U)) || \
617     (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
USB_HostIp3516HsBusTime(uint8_t speed,uint8_t pipeType,uint8_t direction,uint32_t dataLength)618 static uint32_t USB_HostIp3516HsBusTime(uint8_t speed, uint8_t pipeType, uint8_t direction, uint32_t dataLength)
619 {
620     uint32_t result = (3167U + ((1000U * dataLength) * 7U * 8U / 6U)) / 1000U;
621 
622     if (pipeType == USB_ENDPOINT_ISOCHRONOUS) /* iso */
623     {
624         if (speed == USB_SPEED_HIGH)
625         {
626             result = 2083U * (38U * 8U + result) + USB_HOST_IP3516HS_TIME_DELAY;
627         }
628         else if (speed == USB_SPEED_FULL)
629         {
630             if (direction == USB_IN)
631             {
632                 result = 7268000U + 83540U * result + USB_HOST_IP3516HS_TIME_DELAY;
633             }
634             else
635             {
636                 result = 6265000U + 83540U * result + USB_HOST_IP3516HS_TIME_DELAY;
637             }
638         }
639         else
640         {
641             /*no action*/
642         }
643     }
644     else
645     {
646         if (speed == USB_SPEED_HIGH)
647         {
648             result = 2083U * (55U * 8U + result) + USB_HOST_IP3516HS_TIME_DELAY;
649         }
650         else if (speed == USB_SPEED_FULL)
651         {
652             result = 9107000U + 83540U * result + USB_HOST_IP3516HS_TIME_DELAY;
653         }
654         else if (speed == USB_SPEED_LOW)
655         {
656             if (direction == USB_IN)
657             {
658                 result = 64060000U + 2000U * USB_HOST_IP3516HS_HUB_LS_SETUP_TIME_DELAY + 676670U * result +
659                          USB_HOST_IP3516HS_TIME_DELAY;
660             }
661             else
662             {
663                 result = 64107000U + 2000U * USB_HOST_IP3516HS_HUB_LS_SETUP_TIME_DELAY + 667000U * result +
664                          USB_HOST_IP3516HS_TIME_DELAY;
665             }
666         }
667         else
668         {
669             /*no action*/
670         }
671     }
672 
673     result /= 1000000U;
674     if (result == 0U)
675     {
676         result = 1U;
677     }
678     else
679     {
680         /*no action*/
681     }
682 
683     return result;
684 }
685 #endif
686 
USB_HostIp3516HsGetNewPipe(usb_host_ip3516hs_pipe_struct_t ** pipeQueue,usb_host_ip3516hs_pipe_struct_t ** pipe)687 static usb_status_t USB_HostIp3516HsGetNewPipe(usb_host_ip3516hs_pipe_struct_t **pipeQueue,
688                                                usb_host_ip3516hs_pipe_struct_t **pipe)
689 {
690     usb_status_t error = kStatus_USB_Busy;
691     void *p;
692     OSA_SR_ALLOC();
693 
694     /* get a pipe instance */
695     /* Enter critical */
696     OSA_ENTER_CRITICAL();
697     if (NULL != (*pipeQueue))
698     {
699         *pipe      = *pipeQueue;
700         p          = (void *)((*pipe)->pipeCommon.next);
701         *pipeQueue = (usb_host_ip3516hs_pipe_struct_t *)p;
702         error      = kStatus_USB_Success;
703     }
704     /* Exit critical */
705     OSA_EXIT_CRITICAL();
706     return error;
707 }
708 
USB_HostIp3516HsRemovePipe(usb_host_ip3516hs_pipe_struct_t ** pipeQueue,usb_host_ip3516hs_pipe_struct_t * pipe)709 static usb_status_t USB_HostIp3516HsRemovePipe(usb_host_ip3516hs_pipe_struct_t **pipeQueue,
710                                                usb_host_ip3516hs_pipe_struct_t *pipe)
711 {
712     usb_host_ip3516hs_pipe_struct_t *p = *pipeQueue;
713     usb_host_ip3516hs_pipe_struct_t *pre;
714     void *temp;
715 
716     OSA_SR_ALLOC();
717 
718     /* get a pipe instance */
719     /* Enter critical */
720     OSA_ENTER_CRITICAL();
721     pre = NULL;
722     while (NULL != p)
723     {
724         if (p != pipe)
725         {
726             pre  = p;
727             temp = (void *)p->pipeCommon.next;
728             p    = (usb_host_ip3516hs_pipe_struct_t *)temp;
729         }
730         else
731         {
732             if (NULL != pre)
733             {
734                 pre->pipeCommon.next = p->pipeCommon.next;
735             }
736             else
737             {
738                 temp       = (void *)p->pipeCommon.next;
739                 *pipeQueue = (usb_host_ip3516hs_pipe_struct_t *)temp;
740             }
741             break;
742         }
743     }
744     OSA_EXIT_CRITICAL();
745     return kStatus_USB_Success;
746 }
747 
USB_HostIp3516HsInsertPipe(usb_host_ip3516hs_pipe_struct_t ** pipeQueue,usb_host_ip3516hs_pipe_struct_t * pipe)748 static usb_status_t USB_HostIp3516HsInsertPipe(usb_host_ip3516hs_pipe_struct_t **pipeQueue,
749                                                usb_host_ip3516hs_pipe_struct_t *pipe)
750 {
751     usb_host_ip3516hs_pipe_struct_t *p = *pipeQueue;
752     void *temp;
753 
754     OSA_SR_ALLOC();
755 
756     pipe->pipeCommon.next = NULL;
757     /* get a pipe instance */
758     /* Enter critical */
759     OSA_ENTER_CRITICAL();
760     while (NULL != p)
761     {
762         if (p != pipe)
763         {
764             temp = (void *)p->pipeCommon.next;
765             p    = (usb_host_ip3516hs_pipe_struct_t *)temp;
766         }
767         else
768         {
769             break;
770         }
771     }
772     if (NULL == p)
773     {
774         temp                  = (void *)(*pipeQueue);
775         pipe->pipeCommon.next = (usb_host_pipe_t *)temp;
776         *pipeQueue            = pipe;
777     }
778     OSA_EXIT_CRITICAL();
779     return kStatus_USB_Success;
780 }
781 
USB_HostIp3516HsPortChange(usb_host_ip3516hs_state_struct_t * usbHostState)782 static usb_status_t USB_HostIp3516HsPortChange(usb_host_ip3516hs_state_struct_t *usbHostState)
783 {
784     uint32_t portStatus;
785     uint8_t i = 0U;
786 
787     for (i = 0U; i < usbHostState->portNumber; i++)
788     {
789         portStatus = usbHostState->usbRegBase->PORTSC1;
790         if (0U != (portStatus & USB_HOST_IP3516HS_PORTSC1_CSC_MASK))
791         {
792 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
793             if ((uint8_t)kBus_Ip3516HsIdle != usbHostState->busSuspendStatus)
794             {
795                 usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
796                 usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
797                 portStatus = usbHostState->usbRegBase->PORTSC1;
798                 portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_FPR_MASK);
799                 portStatus |= USB_HOST_IP3516HS_PORTSC1_PP_MASK | USB_HOST_IP3516HS_PORTSC1_PED_MASK;
800                 usbHostState->usbRegBase->PORTSC1 = portStatus;
801                 hostPointer->suspendedDevice      = NULL;
802                 usbHostState->busSuspendStatus    = (uint8_t)kBus_Ip3516HsIdle;
803             }
804 #endif
805             uint32_t sofStart =
806                 (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
807                            USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
808             uint32_t sof;
809             while (1U == 1U)
810             {
811                 portStatus = usbHostState->usbRegBase->PORTSC1;
812                 if (0U != (portStatus & USB_HOST_IP3516HS_PORTSC1_CSC_MASK))
813                 {
814                     portStatus &= ~USB_HOST_IP3516HS_PORTSC1_WIC;
815                     usbHostState->usbRegBase->PORTSC1 = portStatus | USB_HOST_IP3516HS_PORTSC1_CSC_MASK;
816                 }
817                 sof = (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
818                                  USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
819                 if ((((sof + USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK + 1U) - sofStart) &
820                      USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) > 1U)
821                 {
822                     break;
823                 }
824             }
825         }
826 
827         portStatus = usbHostState->usbRegBase->PORTSC1;
828 
829         if (0U != (portStatus & USB_HOST_IP3516HS_PORTSC1_CCS_MASK))
830         {
831             uint32_t index;
832 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
833     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
834             /* set the charger type as SDP to disable the DCD function because the usb host start to works. */
835             uint8_t chargerType = (uint8_t)kUSB_DcdSDP;
836 #endif
837             if (kUSB_DeviceIp3516HsPortDetached !=
838                 (usb_host_ip3516hs_port_state_t)usbHostState->portState[i].portStatus)
839             {
840 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
841                 if (((uint8_t)kBus_Ip3516HsSuspended == usbHostState->busSuspendStatus))
842                 {
843                     usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
844 
845                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsStartResume;
846                     /* call host callback function, function is initialized in USB_HostInit */
847                     (void)hostPointer->deviceCallback(
848                         hostPointer->suspendedDevice, NULL,
849                         (uint32_t)kUSB_HostEventDetectResume); /* call host callback function */
850                     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
851                     portStatus = usbHostState->usbRegBase->PORTSC1;
852                     portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC);
853                     portStatus |= USB_HOST_IP3516HS_PORTSC1_FPR_MASK;
854                     usbHostState->usbRegBase->PORTSC1 = portStatus;
855                     usbHostState->matchTick           = hostPointer->hwTick;
856                     while ((hostPointer->hwTick - usbHostState->matchTick) <= 20U)
857                     {
858                     }
859                     portStatus = usbHostState->usbRegBase->PORTSC1;
860                     portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_FPR_MASK);
861                     portStatus |= USB_HOST_IP3516HS_PORTSC1_PP_MASK | USB_HOST_IP3516HS_PORTSC1_PED_MASK;
862                     usbHostState->usbRegBase->PORTSC1 = portStatus;
863                     /* call host callback function, function is initialized in USB_HostInit */
864                     (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
865                                                       kUSB_HostEventResumed); /* call host callback function */
866                     hostPointer->suspendedDevice   = NULL;
867                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
868                 }
869 #if ((defined(USB_HOST_CONFIG_LPM_L1)) && (USB_HOST_CONFIG_LPM_L1 > 0U))
870                 if (((uint8_t)kBus_Ip3516HsL1Sleeped == usbHostState->busSuspendStatus))
871                 {
872                     usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
873 
874                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsStartResume;
875                     /* call host callback function, function is initialized in USB_HostInit */
876                     (void)hostPointer->deviceCallback(
877                         hostPointer->suspendedDevice, NULL,
878                         (uint32_t)kUSB_HostEventDetectResume); /* call host callback function */
879                     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
880                     usbHostState->matchTick = hostPointer->hwTick;
881 
882                     while (0U != ((portStatus)&USB_HOST_IP3516HS_PORTSC1_FPR_MASK))
883                     {
884                         portStatus = (usbHostState->usbRegBase->PORTSC1);
885                         portStatus = portStatus & USB_HOST_IP3516HS_PORTSC1_FPR_MASK;
886                         if ((hostPointer->hwTick - usbHostState->matchTick) >= 1U)
887                         {
888                             break;
889                         }
890                     }
891 
892                     portStatus = usbHostState->usbRegBase->PORTSC1;
893                     portStatus &= ~(USB_HOST_IP3516HS_PORTSC1_WIC | USB_HOST_IP3516HS_PORTSC1_FPR_MASK);
894                     portStatus |= USB_HOST_IP3516HS_PORTSC1_PP_MASK | USB_HOST_IP3516HS_PORTSC1_PED_MASK;
895                     usbHostState->usbRegBase->PORTSC1 = portStatus;
896                     /* call host callback function, function is initialized in USB_HostInit */
897                     (void)hostPointer->deviceCallback(
898                         hostPointer->suspendedDevice, NULL,
899                         (uint32_t)kUSB_HostEventL1Resumed); /* call host callback function */
900                     hostPointer->suspendedDevice   = NULL;
901                     usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
902                 }
903 #if (defined(FSL_FEATURE_USBHSH_VERSION) && (FSL_FEATURE_USBHSH_VERSION >= 300U))
904                 else if (((uint8_t)kBus_Ip3516HsL1StartSleep == usbHostState->busSuspendStatus))
905                 {
906                     usb_host_instance_t *hostPointer = (usb_host_instance_t *)usbHostState->hostHandle;
907                     portStatus                       = usbHostState->usbRegBase->PORTSC1;
908 
909                     if ((0U != (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_SUSP_MASK)) &&
910                         (0x00U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
911                                    USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT)))
912                     {
913                         portStatus = usbHostState->usbRegBase->USBCMD;
914                         portStatus &= ~USB_HOST_IP3516HS_USBCMD_RS_MASK;
915                         usbHostState->usbRegBase->USBCMD = portStatus;
916 
917                         usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsL1Sleeped;
918                         /* call host callback function, function is initialized in USB_HostInit */
919                         (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
920                                                           kUSB_HostEventL1Sleeped); /* call host callback function */
921                     }
922                     else if (0x02U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
923                                        USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT))
924                     {
925                         usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
926                         /* call host callback function, function is initialized in USB_HostInit */
927                         (void)hostPointer->deviceCallback(
928                             hostPointer->suspendedDevice, NULL,
929                             kUSB_HostEventL1SleepNotSupport); /* call host callback function */
930                     }
931                     else if (0x01U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
932                                        USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT))
933                     {
934                         usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
935                         /* call host callback function, function is initialized in USB_HostInit */
936                         (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
937                                                           kUSB_HostEventL1SleepNYET); /* call host callback function */
938                     }
939                     else if (0x03U == ((portStatus & USB_HOST_IP3516HS_PORTSC1_SUS_STAT_MASK) >>
940                                        USB_HOST_IP3516HS_PORTSC1_SUS_STAT_SHIFT))
941                     {
942                         usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
943                         /* call host callback function, function is initialized in USB_HostInit */
944                         (void)hostPointer->deviceCallback(hostPointer->suspendedDevice, NULL,
945                                                           kUSB_HostEventL1SleepError); /* call host callback function */
946                     }
947                     else
948                     {
949                         usbHostState->busSuspendStatus = (uint8_t)kBus_Ip3516HsIdle;
950                     }
951                 }
952 #else
953 
954 #endif
955                 else
956                 {
957                     /* no action */
958                 }
959 #endif
960 #endif
961                 break;
962             }
963 
964             for (index = 0U; index < USB_HOST_IP3516HS_PORT_CONNECT_DEBOUNCE_DELAY; index++)
965             {
966                 USB_HostIp3516HsDelay(usbHostState, 8U);
967                 if (0U == (usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_CCS_MASK))
968                 {
969                     break;
970                 }
971             }
972 
973             portStatus = usbHostState->usbRegBase->PORTSC1;
974 
975             /* CCS is cleared ?*/
976             if ((0U == (portStatus & USB_HOST_IP3516HS_PORTSC1_CCS_MASK)) ||
977                 (index < USB_HOST_IP3516HS_PORT_CONNECT_DEBOUNCE_DELAY))
978             {
979                 usbHostState->portState[i].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortDetached;
980                 continue;
981             }
982 
983 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
984     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
985             USB_HSDCD_Control(usbHostState->dcdHandle, kUSB_HostHSDcdSetType, &(chargerType));
986 #endif
987             (void)USB_HostIp3516HsControlBus(usbHostState, (uint8_t)kUSB_HostBusReset);
988             USB_HostIp3516HsDelay(usbHostState, 81U);
989             usbHostState->portState[i].portSpeed =
990                 (uint8_t)((usbHostState->usbRegBase->PORTSC1 & USB_HOST_IP3516HS_PORTSC1_PSPD_MASK) >>
991                           USB_HOST_IP3516HS_PORTSC1_PSPD_SHIFT);
992             if (0x00U == usbHostState->portState[i].portSpeed)
993             {
994                 usbHostState->portState[i].portSpeed = USB_SPEED_LOW;
995             }
996             else if (0x01U == usbHostState->portState[i].portSpeed)
997             {
998                 usbHostState->portState[i].portSpeed = USB_SPEED_FULL;
999             }
1000             else
1001             {
1002 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
1003                 /* enable phy disconnection */
1004                 USB_EhcihostPhyDisconnectDetectCmd((uint8_t)kUSB_ControllerIp3516Hs0 + usbHostState->controllerId, 1U);
1005 #endif
1006             }
1007             /* do bus recovery delay */
1008             USB_HostIp3516HsDelay(usbHostState, USB_HOST_IP3516HS_PORT_RESET_RECOVERY_DELAY * 8U);
1009             usbHostState->portState[i].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortPhyAttached;
1010             (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_ATTACH);
1011         }
1012         else
1013         {
1014             if ((uint8_t)kUSB_DeviceIp3516HsPortDetached == usbHostState->portState[i].portStatus)
1015             {
1016                 continue;
1017             }
1018 
1019             if ((uint8_t)kUSB_DeviceIp3516HsPortAttached == usbHostState->portState[i].portStatus)
1020             {
1021 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
1022                 /* disable phy disconnection */
1023                 USB_EhcihostPhyDisconnectDetectCmd((uint8_t)kUSB_ControllerIp3516Hs0 + usbHostState->controllerId, 0U);
1024 #endif
1025                 usbHostState->portState[i].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortPhyDetached;
1026 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
1027     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1028                 USB_HSDCD_Control(usbHostState->dcdHandle, kUSB_HostHSDcdSetType, &(usbHostState->chargerType));
1029 #endif
1030                 (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_DETACH);
1031             }
1032             else
1033             {
1034                 usbHostState->portState[i].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortDetached;
1035             }
1036         }
1037     }
1038     return kStatus_USB_Success;
1039 }
1040 
1041 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U)) || \
1042     (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
USB_HostIp3516HsFillSlotBusTime(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe,uint32_t startUFrame,uint8_t * slotTime,uint8_t * ssSlot,uint8_t * csSlot)1043 static usb_status_t USB_HostIp3516HsFillSlotBusTime(usb_host_ip3516hs_state_struct_t *usbHostState,
1044                                                     usb_host_ip3516hs_pipe_struct_t *pipe,
1045                                                     uint32_t startUFrame,
1046                                                     uint8_t *slotTime,
1047                                                     uint8_t *ssSlot,
1048                                                     uint8_t *csSlot)
1049 {
1050     uint8_t slots     = (uint8_t)((pipe->pipeCommon.maxPacketSize + 187U) / 188U);
1051     uint8_t startSlot = (uint8_t)(startUFrame % 8UL);
1052 
1053     for (uint8_t i = 0; i < 8U; i++)
1054     {
1055         slotTime[i] = 0U;
1056     }
1057     *ssSlot = 0U;
1058     *csSlot = 0U;
1059 
1060     if (USB_ENDPOINT_ISOCHRONOUS == pipe->pipeCommon.pipeType)
1061     {
1062         if (USB_OUT == pipe->pipeCommon.direction)
1063         {
1064             if ((startSlot + slots) > 7U)
1065             {
1066                 return kStatus_USB_Error;
1067             }
1068             for (uint8_t i = 0; i < (slots); i++)
1069             {
1070                 slotTime[startSlot + i] = 125U;
1071                 *ssSlot |= (uint8_t)(1UL << (startSlot + i));
1072             }
1073             slotTime[startSlot + slots - 1U] = (uint8_t)(((pipe->pipeCommon.maxPacketSize % 188U) * 125U) / 188U);
1074         }
1075         else
1076         {
1077             if ((startSlot + slots + 1U) > 8U)
1078             {
1079                 return kStatus_USB_Error;
1080             }
1081             *ssSlot = 1U << startSlot;
1082             for (uint8_t i = 0U; i < (slots); i++)
1083             {
1084                 slotTime[startSlot + 1U + i] = 125U;
1085                 *csSlot |= (uint8_t)(1UL << (startSlot + 1U + i));
1086             }
1087             slotTime[startSlot + 1U + slots - 1U] = (uint8_t)(((pipe->pipeCommon.maxPacketSize % 188U) * 125U) / 188U);
1088         }
1089     }
1090     else if (USB_ENDPOINT_INTERRUPT == pipe->pipeCommon.pipeType)
1091     {
1092         if ((startSlot + 3U + 2U) > 8U)
1093         {
1094             return kStatus_USB_Error;
1095         }
1096         *ssSlot = 1U << startSlot;
1097         for (uint8_t i = 0U; i < 3U; i++)
1098         {
1099             *csSlot |= (uint8_t)(1UL << (startSlot + 2U + i));
1100         }
1101         if (USB_OUT == pipe->pipeCommon.direction)
1102         {
1103             slotTime[startSlot] = (uint8_t)pipe->busNoneHsTime;
1104         }
1105         else
1106         {
1107             for (uint8_t i = 0U; i < 3U; i++)
1108             {
1109                 slotTime[startSlot + 2U + i] = (uint8_t)pipe->busNoneHsTime;
1110             }
1111         }
1112     }
1113     else
1114     {
1115         /*no action*/
1116     }
1117     return kStatus_USB_Success;
1118 }
1119 
USB_HostIp3516HsFindStartFrame(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1120 static usb_status_t USB_HostIp3516HsFindStartFrame(usb_host_ip3516hs_state_struct_t *usbHostState,
1121                                                    usb_host_ip3516hs_pipe_struct_t *pipe)
1122 {
1123     usb_host_ip3516hs_pipe_struct_t *p;
1124     uint32_t startUFrame;
1125     uint32_t frame;
1126     uint32_t total = 0U;
1127     void *temp;
1128     uint8_t slotTime[8];
1129     uint8_t speed = ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed;
1130 
1131     pipe->startUFrame = 0U;
1132 
1133     if (NULL == usbHostState->pipeListInUsing)
1134     {
1135         return kStatus_USB_Success;
1136     }
1137 
1138     for (startUFrame = 0U; startUFrame < ((uint32_t)pipe->pipeCommon.interval); startUFrame++)
1139     {
1140         for (frame = startUFrame; frame < USB_HOST_IP3516HS_MAX_UFRAME; frame += pipe->pipeCommon.interval)
1141         {
1142             total = pipe->busHsTime;
1143             /* using temp for misra 14.2, 10.4*/
1144             temp = (void *)usbHostState->pipeListInUsing;
1145             while (NULL != temp)
1146             {
1147                 p    = (usb_host_ip3516hs_pipe_struct_t *)temp;
1148                 temp = (void *)p->pipeCommon.next;
1149                 if ((frame >= p->startUFrame) && ((p->pipeCommon.pipeType == USB_ENDPOINT_ISOCHRONOUS) ||
1150                                                   (p->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT)))
1151                 {
1152                     if (USB_SPEED_HIGH == speed)
1153                     {
1154                         if (0U == ((frame - p->startUFrame) % p->pipeCommon.interval))
1155                         {
1156                             total += p->busHsTime;
1157                         }
1158                         else
1159                         {
1160                         }
1161                     }
1162                     else
1163                     {
1164                         if (USB_OUT == p->pipeCommon.direction)
1165                         {
1166                             if (0U == ((frame - p->startUFrame) % p->pipeCommon.interval))
1167                             {
1168                                 total += p->busHsTime;
1169                             }
1170                             else
1171                             {
1172                             }
1173                         }
1174                         else
1175                         {
1176                             for (uint32_t cs = 0U; cs < 8U; cs++)
1177                             {
1178                                 if (0U != (p->csSlot & (1U << cs)))
1179                                 {
1180                                     if (0U == ((frame - (cs + (uint32_t)(((uint32_t)p->startUFrame) & 0xFFF8U))) %
1181                                                (uint32_t)(p->pipeCommon.interval)))
1182                                     {
1183                                         total += p->busHsTime;
1184                                     }
1185                                     else
1186                                     {
1187                                     }
1188                                 }
1189                             }
1190                         }
1191                     }
1192                 }
1193             }
1194             if ((float)total > USB_HOST_IP3516HS_PERIODIC_BANDWIDTH)
1195             {
1196                 break;
1197             }
1198         }
1199 
1200         do
1201         {
1202             if ((frame >= USB_HOST_IP3516HS_MAX_UFRAME) && (USB_SPEED_HIGH != speed))
1203             {
1204                 uint32_t hsHubNumber;
1205                 usb_status_t error;
1206                 uint8_t totalSlotTime[8];
1207                 uint8_t deviceSlotTime[8];
1208 
1209                 hsHubNumber = 0U;
1210                 frame       = startUFrame;
1211                 for (uint32_t i = 0; i < 8U; i++)
1212                 {
1213                     totalSlotTime[i]  = 0U;
1214                     deviceSlotTime[i] = 0U;
1215                     slotTime[i]       = 0U;
1216                 }
1217                 error = USB_HostIp3516HsFillSlotBusTime(usbHostState, pipe, startUFrame, slotTime, &pipe->ssSlot,
1218                                                         &pipe->csSlot);
1219                 if (kStatus_USB_Success != error)
1220                 {
1221                     break;
1222                 }
1223 
1224                 /* compute FS/LS bandwidth that blong to same high-speed hub, because FS/LS bandwidth is allocated from
1225                  * first parent high-speed hub */
1226                 (void)USB_HostHelperGetPeripheralInformation(pipe->pipeCommon.deviceHandle,
1227                                                              (uint32_t)kUSB_HostGetDeviceHSHubNumber, &hsHubNumber);
1228                 for (frame = startUFrame; frame < USB_HOST_IP3516HS_MAX_UFRAME; frame += pipe->pipeCommon.interval)
1229                 {
1230                     total = 0U;
1231                     temp  = (void *)usbHostState->pipeListInUsing;
1232                     while (NULL != temp)
1233                     {
1234                         p    = (usb_host_ip3516hs_pipe_struct_t *)temp;
1235                         temp = (void *)p->pipeCommon.next;
1236                         if ((frame >= p->startUFrame) && ((p->pipeCommon.pipeType == USB_ENDPOINT_ISOCHRONOUS) ||
1237                                                           (p->pipeCommon.pipeType == USB_ENDPOINT_INTERRUPT)))
1238                         {
1239                             uint32_t deviceHsHubNumber = 0U;
1240                             (void)USB_HostHelperGetPeripheralInformation(p->pipeCommon.deviceHandle,
1241                                                                          (uint32_t)kUSB_HostGetDeviceHSHubNumber,
1242                                                                          &deviceHsHubNumber);
1243                             if ((deviceHsHubNumber == hsHubNumber) &&
1244                                 (0U == (((frame & 0xFFFFFFF8U) - (p->startUFrame & 0xFFF8U)) % p->pipeCommon.interval)))
1245                             {
1246                                 uint8_t ss;
1247                                 uint8_t cs;
1248                                 (void)USB_HostIp3516HsFillSlotBusTime(usbHostState, pipe, startUFrame, deviceSlotTime,
1249                                                                       &ss, &cs);
1250                                 for (uint32_t i = 0; i < 8U; i++)
1251                                 {
1252                                     totalSlotTime[i] += deviceSlotTime[i];
1253                                 }
1254                             }
1255                         }
1256                     }
1257 
1258                     for (uint32_t i = 0; i < 8U; i++)
1259                     {
1260                         if ((totalSlotTime[i] + slotTime[i]) > s_SlotMaxBandwidth[i])
1261                         {
1262                             break;
1263                         }
1264                     }
1265                 }
1266             }
1267         } while (0U == 1U);
1268 
1269         if (frame >= USB_HOST_IP3516HS_MAX_UFRAME)
1270         {
1271             pipe->startUFrame = (uint16_t)startUFrame;
1272             return kStatus_USB_Success;
1273         }
1274     }
1275 
1276     return kStatus_USB_BandwidthFail;
1277 }
1278 
USB_HostIp3516BaudWidthCheck(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1279 static usb_status_t USB_HostIp3516BaudWidthCheck(usb_host_ip3516hs_state_struct_t *usbHostState,
1280                                                  usb_host_ip3516hs_pipe_struct_t *pipe)
1281 {
1282     usb_status_t error;
1283     uint8_t speed = ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed;
1284     OSA_SR_ALLOC();
1285 
1286     pipe->busHsTime = (uint16_t)USB_HostIp3516HsBusTime(
1287         USB_SPEED_HIGH, pipe->pipeCommon.pipeType, pipe->pipeCommon.direction,
1288         ((uint32_t)pipe->pipeCommon.maxPacketSize) * ((uint32_t)pipe->pipeCommon.numberPerUframe));
1289 
1290     if (USB_SPEED_HIGH != speed)
1291     {
1292         uint32_t thinkTime  = 0U;
1293         pipe->busNoneHsTime = (uint16_t)USB_HostIp3516HsBusTime(
1294             speed, pipe->pipeCommon.pipeType, pipe->pipeCommon.direction, pipe->pipeCommon.maxPacketSize);
1295         (void)USB_HostHelperGetPeripheralInformation(pipe->pipeCommon.deviceHandle, (uint32_t)kUSB_HostGetHubThinkTime,
1296                                                      &thinkTime); /* Get the hub think time */
1297         pipe->busNoneHsTime += (uint16_t)(thinkTime * 7U / (6U * 12U));
1298     }
1299     OSA_ENTER_CRITICAL();
1300     error = USB_HostIp3516HsFindStartFrame(usbHostState, pipe);
1301     OSA_EXIT_CRITICAL();
1302     return error;
1303 }
1304 #endif
1305 
USB_HostIp3516HsOpenControlBulkPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1306 static usb_status_t USB_HostIp3516HsOpenControlBulkPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1307                                                         usb_host_ip3516hs_pipe_struct_t *pipe)
1308 {
1309     usb_host_ip3516hs_atl_struct_t *atl;
1310     OSA_SR_ALLOC();
1311 
1312     /* Enter critical */
1313     OSA_ENTER_CRITICAL();
1314     for (uint8_t i = 0;
1315          i < (sizeof(s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl) / sizeof(usb_host_ip3516hs_atl_struct_t));
1316          i++)
1317     {
1318         if (0U == s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].control1Union.stateBitField.MaxPacketLength)
1319         {
1320             pipe->tdIndex = i;
1321             s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].control1Union.stateBitField.MaxPacketLength =
1322                 pipe->pipeCommon.maxPacketSize;
1323             break;
1324         }
1325     }
1326     /* Exit critical */
1327     OSA_EXIT_CRITICAL();
1328 
1329     if (0xFFU == pipe->tdIndex)
1330     {
1331         return kStatus_USB_Busy;
1332     }
1333     atl                                   = &s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex];
1334     atl->control1Union.stateBitField.Mult = pipe->pipeCommon.numberPerUframe;
1335     atl->control2Union.stateBitField.EP   = pipe->pipeCommon.endpointAddress;
1336     atl->control2Union.stateBitField.DeviceAddress =
1337         ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->setAddress;
1338     if (USB_SPEED_HIGH == usbHostState->portState[0].portSpeed)
1339     {
1340         atl->control2Union.stateBitField.S =
1341             (USB_SPEED_HIGH == ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed) ? 0U : 1U;
1342     }
1343     else
1344     {
1345         atl->control2Union.stateBitField.S = 0U;
1346     }
1347     atl->control2Union.stateBitField.RL  = 0xFU;
1348     atl->stateUnion.stateBitField.NakCnt = 0xFU;
1349     atl->control2Union.stateBitField.SE =
1350         (USB_SPEED_LOW == ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed) ? 2U : 0U;
1351     atl->control2Union.stateBitField.PortNumber =
1352 #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB > 0U))
1353         ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->hsHubPort;
1354 #else
1355         0U;
1356 #endif
1357     atl->control2Union.stateBitField.HubAddress =
1358 #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB > 0U))
1359         ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->hsHubNumber;
1360 #else
1361         0U;
1362 #endif
1363     atl->dataUnion.dataBitField.I        = 1U;
1364     atl->stateUnion.stateBitField.EpType = pipe->pipeCommon.pipeType;
1365     atl->stateUnion.stateBitField.DT     = 0U;
1366     atl->stateUnion.stateBitField.P      = 0U;
1367 
1368     return kStatus_USB_Success;
1369 }
1370 
1371 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
USB_HostIp3516HsOpenIsoPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1372 static usb_status_t USB_HostIp3516HsOpenIsoPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1373                                                 usb_host_ip3516hs_pipe_struct_t *pipe)
1374 {
1375     usb_host_ip3516hs_ptl_struct_t *ptl;
1376     usb_host_ip3516hs_sptl_struct_t *sptl;
1377     uint32_t count = 0;
1378     void *temp;
1379     usb_status_t error;
1380     OSA_SR_ALLOC();
1381 
1382     error = USB_HostIp3516BaudWidthCheck(usbHostState, pipe);
1383 
1384     if (kStatus_USB_Success != error)
1385     {
1386         return error;
1387     }
1388 
1389     /* Enter critical */
1390     OSA_ENTER_CRITICAL();
1391     for (uint32_t i = 0U; i < USB_HOST_CONFIG_IP3516HS_MAX_ISO; i++)
1392     {
1393         for (uint32_t j = i;
1394              (j < (USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER + i)) && (j < USB_HOST_CONFIG_IP3516HS_MAX_ISO); j++)
1395         {
1396             if (0U ==
1397                 s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[j].control1Union.stateBitField.MaxPacketLength)
1398             {
1399                 count++;
1400             }
1401         }
1402         if (count >= USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER)
1403         {
1404             pipe->tdIndex = (uint8_t)i;
1405             for (uint32_t j = i;
1406                  (j < (USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER + i)) && (j < USB_HOST_CONFIG_IP3516HS_MAX_ISO); j++)
1407             {
1408                 s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[j].control1Union.stateBitField.MaxPacketLength =
1409                     pipe->pipeCommon.maxPacketSize;
1410             }
1411             break;
1412         }
1413         else
1414         {
1415             count = 0;
1416         }
1417     }
1418     /* Exit critical */
1419     OSA_EXIT_CRITICAL();
1420 
1421     if (0xFFU == pipe->tdIndex)
1422     {
1423         return kStatus_USB_Busy;
1424     }
1425 
1426     for (uint32_t j = pipe->tdIndex; (j < ((uint32_t)USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER + pipe->tdIndex)) &&
1427                                      (j < USB_HOST_CONFIG_IP3516HS_MAX_ISO);
1428          j++)
1429     {
1430         ptl                                   = &s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[j];
1431         temp                                  = (void *)ptl;
1432         sptl                                  = (usb_host_ip3516hs_sptl_struct_t *)temp;
1433         ptl->control1Union.stateBitField.Mult = pipe->pipeCommon.numberPerUframe;
1434         ptl->control2Union.stateBitField.EP   = pipe->pipeCommon.endpointAddress;
1435         ptl->control2Union.stateBitField.DeviceAddress =
1436             ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->setAddress;
1437         if (USB_SPEED_HIGH == usbHostState->portState[0].portSpeed)
1438         {
1439             ptl->control2Union.stateBitField.S =
1440                 (USB_SPEED_HIGH == ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed) ? 0U : 1U;
1441         }
1442         else
1443         {
1444             ptl->control2Union.stateBitField.S = 0U;
1445         }
1446         ptl->control2Union.stateBitField.RL  = 0U;
1447         ptl->stateUnion.stateBitField.NakCnt = 0U;
1448         ptl->control2Union.stateBitField.SE =
1449             (USB_SPEED_LOW == ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed) ? 2U : 0U;
1450         if (0U != ptl->control2Union.stateBitField.S)
1451         {
1452             sptl->control2Union.stateBitField.PortNumber =
1453 #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB > 0U))
1454                 ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->hsHubPort;
1455 #else
1456                 0U;
1457 #endif
1458             sptl->control2Union.stateBitField.HubAddress =
1459 #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB > 0U))
1460                 ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->hsHubNumber;
1461 #else
1462                 0U;
1463 #endif
1464         }
1465         else
1466         {
1467             sptl->control2Union.stateBitField.PortNumber = 0U;
1468             sptl->control2Union.stateBitField.HubAddress = 0U;
1469         }
1470         ptl->dataUnion.dataBitField.NrBytesToTransfer = 0U;
1471         ptl->dataUnion.dataBitField.I                 = 1U;
1472         ptl->stateUnion.stateBitField.EpType          = pipe->pipeCommon.pipeType;
1473         ptl->stateUnion.stateBitField.DT              = 0U;
1474         ptl->stateUnion.stateBitField.P               = 0U;
1475     }
1476     return kStatus_USB_Success;
1477 }
1478 #endif
1479 
1480 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
USB_HostIp3516HsOpenInterruptPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1481 static usb_status_t USB_HostIp3516HsOpenInterruptPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1482                                                       usb_host_ip3516hs_pipe_struct_t *pipe)
1483 {
1484     usb_host_ip3516hs_ptl_struct_t *ptl;
1485     usb_host_ip3516hs_sptl_struct_t *sptl;
1486     usb_status_t error;
1487     void *temp;
1488     OSA_SR_ALLOC();
1489 
1490     error = USB_HostIp3516BaudWidthCheck(usbHostState, pipe);
1491 
1492     if (kStatus_USB_Success != error)
1493     {
1494         return error;
1495     }
1496 
1497     /* Enter critical */
1498     OSA_ENTER_CRITICAL();
1499     for (uint8_t i = 0; i < USB_HOST_CONFIG_IP3516HS_MAX_INT; i++)
1500     {
1501         if (0U ==
1502             s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].control1Union.stateBitField.MaxPacketLength)
1503         {
1504             pipe->tdIndex = i;
1505             s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].control1Union.stateBitField.MaxPacketLength =
1506                 pipe->pipeCommon.maxPacketSize;
1507             break;
1508         }
1509     }
1510     /* Exit critical */
1511     OSA_EXIT_CRITICAL();
1512 
1513     if (0xFFU == pipe->tdIndex)
1514     {
1515         pipe->pipeCommon.open = 0U;
1516         /* release the pipe */
1517         (void)USB_HostIp3516HsInsertPipe(&usbHostState->pipeList, pipe);
1518         return kStatus_USB_Busy;
1519     }
1520     ptl                                   = &s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[pipe->tdIndex];
1521     temp                                  = (void *)ptl;
1522     sptl                                  = (usb_host_ip3516hs_sptl_struct_t *)temp;
1523     ptl->control1Union.stateBitField.Mult = pipe->pipeCommon.numberPerUframe;
1524     ptl->control2Union.stateBitField.EP   = pipe->pipeCommon.endpointAddress;
1525     ptl->control2Union.stateBitField.DeviceAddress =
1526         ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->setAddress;
1527     if (USB_SPEED_HIGH == usbHostState->portState[0].portSpeed)
1528     {
1529         ptl->control2Union.stateBitField.S =
1530             (USB_SPEED_HIGH == ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed) ? 0U : 1U;
1531     }
1532     else
1533     {
1534         ptl->control2Union.stateBitField.S = 0U;
1535     }
1536     ptl->control2Union.stateBitField.RL  = 0xFU;
1537     ptl->stateUnion.stateBitField.NakCnt = 0xFU;
1538     ptl->control2Union.stateBitField.SE =
1539         (USB_SPEED_LOW == ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed) ? 2U : 0U;
1540     if (0U != ptl->control2Union.stateBitField.S)
1541     {
1542         sptl->control2Union.stateBitField.PortNumber =
1543 #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB > 0U))
1544             ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->hsHubPort;
1545 #else
1546             0U;
1547 #endif
1548         sptl->control2Union.stateBitField.HubAddress =
1549 #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB > 0U))
1550             ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->hsHubNumber;
1551 #else
1552             0U;
1553 #endif
1554     }
1555     else
1556     {
1557         sptl->control2Union.stateBitField.PortNumber = 0U;
1558         sptl->control2Union.stateBitField.HubAddress = 0U;
1559     }
1560     ptl->dataUnion.dataBitField.I        = 1U;
1561     ptl->stateUnion.stateBitField.EpType = pipe->pipeCommon.pipeType;
1562     ptl->stateUnion.stateBitField.DT     = 0U;
1563     ptl->stateUnion.stateBitField.P      = 0U;
1564 
1565     return kStatus_USB_Success;
1566 }
1567 #endif
1568 
USB_HostIp3516HsCancelPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe,usb_host_transfer_t * tr)1569 static usb_status_t USB_HostIp3516HsCancelPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1570                                                usb_host_ip3516hs_pipe_struct_t *pipe,
1571                                                usb_host_transfer_t *tr)
1572 {
1573     usb_host_transfer_t *trCurrent;
1574     usb_host_transfer_t *trPos;
1575     usb_host_transfer_t *trPre;
1576     uint32_t startUFrame;
1577     uint32_t currentUFrame;
1578 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
1579     indexLength_t indexLength;
1580 #endif
1581     usb_status_t trStatus = kStatus_USB_Success;
1582 
1583     switch (pipe->pipeCommon.pipeType)
1584     {
1585         case USB_ENDPOINT_BULK:
1586         case USB_ENDPOINT_CONTROL:
1587             usbHostState->usbRegBase->ATL_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
1588             break;
1589 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
1590         case USB_ENDPOINT_INTERRUPT:
1591             usbHostState->usbRegBase->INT_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
1592             break;
1593 #endif
1594 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
1595         case USB_ENDPOINT_ISOCHRONOUS:
1596             usbHostState->usbRegBase->ISO_PTD_SKIP_MAP |= ((1UL << USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER) - 1U)
1597                                                           << pipe->tdIndex;
1598             break;
1599 #endif
1600         default:
1601             /*no action*/
1602             break;
1603     }
1604     startUFrame   = (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
1605                              USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
1606     currentUFrame = startUFrame;
1607 
1608     while (currentUFrame == startUFrame)
1609     {
1610         currentUFrame = (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
1611                                    USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
1612     }
1613 
1614     USB_HostIp3516HsLock();
1615     trCurrent = pipe->trList;
1616     trPre     = NULL;
1617     while (NULL != trCurrent)
1618     {
1619         trPos = trCurrent->next;
1620         if ((NULL == tr) || (tr == trCurrent))
1621         {
1622             switch (pipe->pipeCommon.pipeType)
1623             {
1624                 case USB_ENDPOINT_BULK:
1625                 case USB_ENDPOINT_CONTROL:
1626                     trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1627                                                     .atl[pipe->tdIndex]
1628                                                     .stateUnion.stateBitField.NrBytesToTransfer;
1629                     if (1U ==
1630                         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.H)
1631                     {
1632                         trStatus = kStatus_USB_TransferStall;
1633                     }
1634                     else if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1635                                         .atl[pipe->tdIndex]
1636                                         .stateUnion.stateBitField.B) &&
1637                              (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1638                                         .atl[pipe->tdIndex]
1639                                         .stateUnion.stateBitField.X))
1640                     {
1641                         trStatus = kStatus_USB_TransferFailed;
1642                     }
1643                     else
1644                     {
1645                         if (((trCurrent->transferSofar != trCurrent->transferLength) ||
1646                              (pipe->pipeCommon.pipeType == USB_ENDPOINT_CONTROL)) &&
1647                             (kStatus_USB_Success == trStatus))
1648                         {
1649                             if (kStatus_USB_Success == (usb_status_t)trCurrent->union1.transferResult)
1650                             {
1651                                 trStatus = kStatus_USB_TransferCancel;
1652                             }
1653                             else
1654                             {
1655                                 trStatus                         = kStatus_USB_TransferFailed;
1656                                 trCurrent->union1.transferResult = (int32_t)kStatus_USB_Success;
1657                             }
1658                         }
1659                     }
1660                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.A = 0U;
1661                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].control1Union.stateBitField.V =
1662                         0U;
1663                     break;
1664 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
1665                 case USB_ENDPOINT_INTERRUPT:
1666                     trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1667                                                     .interrupt[pipe->tdIndex]
1668                                                     .stateUnion.stateBitField.NrBytesToTransfer;
1669                     if (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1670                                   .interrupt[pipe->tdIndex]
1671                                   .stateUnion.stateBitField.H)
1672                     {
1673                         trStatus = kStatus_USB_TransferStall;
1674                     }
1675                     else if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1676                                         .interrupt[pipe->tdIndex]
1677                                         .stateUnion.stateBitField.B) &&
1678                              (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1679                                         .interrupt[pipe->tdIndex]
1680                                         .stateUnion.stateBitField.X))
1681                     {
1682                         trStatus = kStatus_USB_TransferFailed;
1683                     }
1684                     else
1685                     {
1686                         if ((0U == trCurrent->transferSofar) && (kStatus_USB_Success == trStatus))
1687                         {
1688                             trStatus = kStatus_USB_TransferCancel;
1689                         }
1690                     }
1691                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1692                         .interrupt[pipe->tdIndex]
1693                         .stateUnion.stateBitField.A = 0U;
1694                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1695                         .interrupt[pipe->tdIndex]
1696                         .control1Union.stateBitField.V = 0U;
1697                     break;
1698 #endif
1699 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
1700                 case USB_ENDPOINT_ISOCHRONOUS:
1701                     indexLength.indexLength = trCurrent->union2.frame;
1702                     trCurrent->union2.frame = 0;
1703                     if (0U != indexLength.indexLength)
1704                     {
1705                         trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1706                                                         .iso[indexLength.state.tdIndex]
1707                                                         .stateUnion.stateBitField.NrBytesToTransfer;
1708                         if (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1709                                       .iso[indexLength.state.tdIndex]
1710                                       .stateUnion.stateBitField.H)
1711                         {
1712                             trStatus = kStatus_USB_TransferStall;
1713                         }
1714                         else if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1715                                             .iso[indexLength.state.tdIndex]
1716                                             .stateUnion.stateBitField.B) &&
1717                                  (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1718                                             .iso[indexLength.state.tdIndex]
1719                                             .stateUnion.stateBitField.X))
1720                         {
1721                             trStatus = kStatus_USB_TransferFailed;
1722                         }
1723                         else
1724                         {
1725                             if ((0U == trCurrent->transferSofar) && (kStatus_USB_Success == trStatus))
1726                             {
1727                                 trStatus = kStatus_USB_TransferCancel;
1728                             }
1729                         }
1730                         s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1731                             .iso[indexLength.state.tdIndex]
1732                             .stateUnion.stateBitField.A = 0U;
1733                         s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1734                             .iso[indexLength.state.tdIndex]
1735                             .control1Union.stateBitField.V = 0U;
1736                         s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1737                             .iso[indexLength.state.tdIndex]
1738                             .dataUnion.dataBitField.NrBytesToTransfer = 0U;
1739                     }
1740                     else
1741                     {
1742                         trStatus = kStatus_USB_TransferCancel;
1743                     }
1744                     pipe->bufferIndex  = indexLength.state.bufferIndex;
1745                     pipe->bufferLength = indexLength.state.bufferLength;
1746                     break;
1747 #endif
1748                 default:
1749                     /*no action*/
1750                     break;
1751             }
1752 
1753             if (0UL != pipe->bufferLength)
1754             {
1755                 (void)USB_HostIp3516HsFreeBuffer(usbHostState, pipe->bufferIndex, pipe->bufferLength);
1756                 pipe->bufferLength = 0U;
1757             }
1758             if (NULL != trPre)
1759             {
1760                 trPre->next = trPos;
1761             }
1762             else
1763             {
1764                 pipe->trList = trPos;
1765             }
1766             pipe->cutOffTime = USB_HOST_IP3516HS_TRANSFER_TIMEOUT_GAP;
1767             pipe->isBusy     = 0U;
1768             /* callback function is different from the current condition */
1769             trCurrent->callbackFn(trCurrent->callbackParam, trCurrent, trStatus); /* transfer callback */
1770         }
1771         else
1772         {
1773             trPre = trCurrent;
1774         }
1775         trCurrent = trPos;
1776     }
1777     USB_HostIp3516HsUnlock();
1778     switch (pipe->pipeCommon.pipeType)
1779     {
1780         case USB_ENDPOINT_BULK:
1781         case USB_ENDPOINT_CONTROL:
1782             usbHostState->usbRegBase->ATL_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
1783             break;
1784 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
1785         case USB_ENDPOINT_INTERRUPT:
1786             usbHostState->usbRegBase->INT_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
1787             break;
1788 #endif
1789 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
1790         case USB_ENDPOINT_ISOCHRONOUS:
1791             usbHostState->usbRegBase->ISO_PTD_SKIP_MAP &=
1792                 ~(((1UL << USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER) - 1U) << pipe->tdIndex);
1793             break;
1794 #endif
1795         default:
1796             /*no action*/
1797             break;
1798     }
1799     return kStatus_USB_Success;
1800 }
1801 
USB_HostIp3516HsCloseControlBulkPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1802 static usb_status_t USB_HostIp3516HsCloseControlBulkPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1803                                                          usb_host_ip3516hs_pipe_struct_t *pipe)
1804 {
1805     OSA_SR_ALLOC();
1806 
1807     (void)USB_HostIp3516HsCancelPipe(usbHostState, pipe, NULL);
1808 
1809     /* Enter critical */
1810     OSA_ENTER_CRITICAL();
1811     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].control1Union.stateBitField.V = 0U;
1812     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].control1Union.stateBitField.MaxPacketLength =
1813         0U;
1814     /* Exit critical */
1815     OSA_EXIT_CRITICAL();
1816 
1817     return kStatus_USB_Success;
1818 }
1819 
1820 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
USB_HostIp3516HsCloseIsoPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1821 static usb_status_t USB_HostIp3516HsCloseIsoPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1822                                                  usb_host_ip3516hs_pipe_struct_t *pipe)
1823 {
1824     OSA_SR_ALLOC();
1825 
1826     (void)USB_HostIp3516HsCancelPipe(usbHostState, pipe, NULL);
1827 
1828     /* Enter critical */
1829     OSA_ENTER_CRITICAL();
1830 
1831     for (uint32_t i = 0; i < USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER; i++)
1832     {
1833         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[pipe->tdIndex + i].control1Union.stateBitField.V = 0U;
1834         s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1835             .iso[pipe->tdIndex + i]
1836             .control1Union.stateBitField.MaxPacketLength = 0U;
1837         s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1838             .iso[pipe->tdIndex + i]
1839             .dataUnion.dataBitField.NrBytesToTransfer = 0U;
1840     }
1841     /* Exit critical */
1842     OSA_EXIT_CRITICAL();
1843 
1844     return kStatus_USB_Success;
1845 }
1846 #endif
1847 
1848 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
USB_HostIp3516HsCloseInterruptPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe)1849 static usb_status_t USB_HostIp3516HsCloseInterruptPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1850                                                        usb_host_ip3516hs_pipe_struct_t *pipe)
1851 {
1852     OSA_SR_ALLOC();
1853 
1854     (void)USB_HostIp3516HsCancelPipe(usbHostState, pipe, NULL);
1855 
1856     /* Enter critical */
1857     OSA_ENTER_CRITICAL();
1858     s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[pipe->tdIndex].control1Union.stateBitField.V = 0U;
1859     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
1860         .interrupt[pipe->tdIndex]
1861         .control1Union.stateBitField.MaxPacketLength = 0U;
1862     /* Exit critical */
1863     OSA_EXIT_CRITICAL();
1864 
1865     return kStatus_USB_Success;
1866 }
1867 #endif
1868 
USB_HostIp3516HsGetBuffer(usb_host_ip3516hs_state_struct_t * usbHostState,uint32_t length,uint32_t MaxPacketSize,uint32_t * index,uint32_t * bufferLength)1869 static usb_status_t USB_HostIp3516HsGetBuffer(usb_host_ip3516hs_state_struct_t *usbHostState,
1870                                               uint32_t length,
1871                                               uint32_t MaxPacketSize,
1872                                               uint32_t *index,
1873                                               uint32_t *bufferLength)
1874 {
1875     uint32_t i;
1876     uint32_t startIndex     = 0xFFFFFFFFU;
1877     uint32_t freeBufferSize = 0U;
1878     OSA_SR_ALLOC();
1879 
1880     OSA_ENTER_CRITICAL();
1881     for (i = 0U; i < (sizeof(s_UsbHostIp3516HsBufferArray) / (sizeof(uint8_t) * 64U)); i++)
1882     {
1883         if (0U != (usbHostState->bufferArrayBitMap[i >> 5U] & (1UL << (i % 32U))))
1884         {
1885             freeBufferSize = 0U;
1886             startIndex     = 0xFFFFFFFFU;
1887         }
1888         else
1889         {
1890             freeBufferSize += 64U;
1891             if (0xFFFFFFFFU == startIndex)
1892             {
1893                 startIndex = i;
1894             }
1895             if (length <= freeBufferSize)
1896             {
1897                 break;
1898             }
1899         }
1900     }
1901     if (0xFFFFFFFFU != startIndex)
1902     {
1903         *index        = startIndex;
1904         *bufferLength = freeBufferSize;
1905         for (; startIndex <= i; startIndex++)
1906         {
1907             usbHostState->bufferArrayBitMap[startIndex >> 5U] |= (1UL << (startIndex % 32U));
1908         }
1909         OSA_EXIT_CRITICAL();
1910         return kStatus_USB_Success;
1911     }
1912 
1913     if (MaxPacketSize >= length)
1914     {
1915         OSA_EXIT_CRITICAL();
1916         return kStatus_USB_Busy;
1917     }
1918 
1919     for (i = 0U; i < (sizeof(s_UsbHostIp3516HsBufferArray) / (sizeof(uint8_t) * 64U)); i++)
1920     {
1921         if (0U != (usbHostState->bufferArrayBitMap[i >> 5U] & (1UL << (i % 32U))))
1922         {
1923             freeBufferSize = 0U;
1924             startIndex     = 0xFFFFFFFFU;
1925         }
1926         else
1927         {
1928             freeBufferSize += 64U;
1929             if (0xFFFFFFFFU == startIndex)
1930             {
1931                 startIndex = i;
1932             }
1933             if (length <= MaxPacketSize)
1934             {
1935                 break;
1936             }
1937         }
1938     }
1939     if (0xFFFFFFFFU != startIndex)
1940     {
1941         *index        = startIndex;
1942         *bufferLength = freeBufferSize;
1943         for (; startIndex <= i; startIndex++)
1944         {
1945             usbHostState->bufferArrayBitMap[startIndex >> 5U] |= (1UL << (startIndex % 32U));
1946         }
1947         OSA_EXIT_CRITICAL();
1948         return kStatus_USB_Success;
1949     }
1950     OSA_EXIT_CRITICAL();
1951     return kStatus_USB_Busy;
1952 }
1953 
USB_HostIp3516HsFreeBuffer(usb_host_ip3516hs_state_struct_t * usbHostState,uint32_t index,uint32_t bufferLength)1954 static usb_status_t USB_HostIp3516HsFreeBuffer(usb_host_ip3516hs_state_struct_t *usbHostState,
1955                                                uint32_t index,
1956                                                uint32_t bufferLength)
1957 {
1958     uint32_t i;
1959     uint32_t indexCount = (bufferLength - 1U) / 64U + 1U;
1960     if ((indexCount + index) > (sizeof(s_UsbHostIp3516HsBufferArray) / (sizeof(uint8_t) * 64U)))
1961     {
1962         indexCount = (sizeof(s_UsbHostIp3516HsBufferArray) / (sizeof(uint8_t) * 64U)) - index;
1963     }
1964     for (i = index; i < (index + indexCount); i++)
1965     {
1966         usbHostState->bufferArrayBitMap[i >> 5U] &= ~(1UL << (i % 32U));
1967     }
1968     return kStatus_USB_Success;
1969 }
1970 
USB_HostIp3516HsWriteControlPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe,usb_host_transfer_t * tr)1971 static usb_status_t USB_HostIp3516HsWriteControlPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
1972                                                      usb_host_ip3516hs_pipe_struct_t *pipe,
1973                                                      usb_host_transfer_t *tr)
1974 {
1975     usb_host_ip3516hs_atl_struct_t *atl = &s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex];
1976     uint8_t *bufferAddress;
1977     uint32_t transferLength;
1978     usb_status_t status = kStatus_USB_Success;
1979     usb_host_ip3516hs_transfer_status_t transferStstus;
1980 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
1981     uint8_t timeCount;
1982 #endif
1983     OSA_SR_ALLOC();
1984     if (1U == pipe->isBusy)
1985     {
1986         return kStatus_USB_Success;
1987     }
1988     OSA_ENTER_CRITICAL();
1989     pipe->isBusy = 1U;
1990     OSA_EXIT_CRITICAL();
1991 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
1992     if (1U == usbHostState->complianceTest)
1993     {
1994         timeCount = 15 * 8; /* 15s */
1995         while (0U != (timeCount--))
1996         {
1997             USB_HostIp3516HsDelay(usbHostState, 1000U);
1998         }
1999     }
2000 #endif
2001     transferStstus = (usb_host_ip3516hs_transfer_status_t)tr->setupStatus;
2002     switch (transferStstus)
2003     {
2004         case kStatus_UsbHostIp3516Hs_Idle:
2005             if (0U != pipe->bufferLength)
2006             {
2007                 bufferAddress = (uint8_t *)&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0];
2008             }
2009             else
2010             {
2011                 transferLength = pipe->pipeCommon.maxPacketSize;
2012                 if (kStatus_USB_Success == USB_HostIp3516HsGetBuffer(usbHostState, transferLength,
2013                                                                      pipe->pipeCommon.maxPacketSize, &pipe->bufferIndex,
2014                                                                      &pipe->bufferLength))
2015                 {
2016                     bufferAddress = (uint8_t *)&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0];
2017                 }
2018                 else
2019                 {
2020                     OSA_ENTER_CRITICAL();
2021                     pipe->isBusy = 2U;
2022                     OSA_EXIT_CRITICAL();
2023                     return kStatus_USB_Success;
2024                 }
2025             }
2026             for (uint8_t i = 0; i < 8U; i++)
2027             {
2028                 bufferAddress[i] = ((uint8_t *)tr->setupPacket)[i];
2029             }
2030             atl->control2Union.stateBitField.RL             = 0U;
2031             atl->stateUnion.stateBitField.P                 = 0U;
2032             atl->stateUnion.stateBitField.NakCnt            = 0U;
2033             atl->stateUnion.stateBitField.Cerr              = 0x3U;
2034             atl->dataUnion.dataBitField.NrBytesToTransfer   = 8U;
2035             atl->stateUnion.stateBitField.NrBytesToTransfer = 0U;
2036             atl->dataUnion.dataBitField.DataStartAddress    = ((uint32_t)(bufferAddress)) & 0x0000FFFFU;
2037             atl->dataUnion.dataBitField.I                   = 1U;
2038             atl->stateUnion.stateBitField.Token             = USB_HOST_IP3516HS_PTD_TOKEN_SETUP;
2039             atl->stateUnion.stateBitField.DT                = 0x00U;
2040             atl->stateUnion.stateBitField.SC                = 0x00U;
2041             tr->setupStatus                                 = (uint8_t)kStatus_UsbHostIp3516Hs_Setup;
2042             atl->control1Union.stateBitField.V              = 0x01U;
2043             atl->stateUnion.stateBitField.A                 = 0x01U;
2044             break;
2045         case kStatus_UsbHostIp3516Hs_Data2:
2046         case kStatus_UsbHostIp3516Hs_Setup:
2047             transferLength = (tr->transferLength - tr->transferSofar);
2048             if (transferLength > pipe->bufferLength)
2049             {
2050                 transferLength = pipe->bufferLength;
2051             }
2052             bufferAddress = (uint8_t *)&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0];
2053             if (USB_OUT == tr->direction)
2054             {
2055                 for (uint32_t i = 0; i < transferLength; i++)
2056                 {
2057                     bufferAddress[i] = tr->transferBuffer[tr->transferSofar + i];
2058                 }
2059             }
2060             atl->control2Union.stateBitField.RL             = 0U;
2061             atl->stateUnion.stateBitField.NakCnt            = 0U;
2062             atl->stateUnion.stateBitField.Cerr              = 0x3U;
2063             atl->dataUnion.dataBitField.NrBytesToTransfer   = transferLength;
2064             atl->stateUnion.stateBitField.NrBytesToTransfer = 0U;
2065             atl->dataUnion.dataBitField.DataStartAddress    = ((uint32_t)(bufferAddress)) & 0x0000FFFFU;
2066             atl->dataUnion.dataBitField.I                   = 1U;
2067             atl->stateUnion.stateBitField.SC                = 0x00U;
2068             if (0U != transferLength)
2069             {
2070                 if (USB_IN == tr->direction)
2071                 {
2072                     atl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_IN;
2073                     atl->stateUnion.stateBitField.P     = 0x00U;
2074                 }
2075                 else
2076                 {
2077                     atl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_OUT;
2078                 }
2079                 if (transferLength < (tr->transferLength - tr->transferSofar))
2080                 {
2081                     tr->setupStatus = (uint8_t)kStatus_UsbHostIp3516Hs_Data2;
2082                 }
2083                 else
2084                 {
2085                     tr->setupStatus = (uint8_t)kStatus_UsbHostIp3516Hs_Data;
2086                 }
2087             }
2088             else
2089             {
2090                 atl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_IN;
2091                 tr->setupStatus                     = (uint8_t)kStatus_UsbHostIp3516Hs_State;
2092             }
2093             atl->control1Union.stateBitField.V = 0x01U;
2094             atl->stateUnion.stateBitField.A    = 0x01U;
2095             break;
2096         case kStatus_UsbHostIp3516Hs_Data:
2097             tr->setupStatus                      = (uint8_t)kStatus_UsbHostIp3516Hs_State;
2098             bufferAddress                        = (uint8_t *)&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0];
2099             atl->control2Union.stateBitField.RL  = 0U;
2100             atl->stateUnion.stateBitField.NakCnt = 0U;
2101             atl->stateUnion.stateBitField.Cerr   = 0x3U;
2102             atl->dataUnion.dataBitField.NrBytesToTransfer   = 0;
2103             atl->stateUnion.stateBitField.NrBytesToTransfer = 0U;
2104             atl->dataUnion.dataBitField.DataStartAddress    = ((uint32_t)(bufferAddress)) & 0x0000FFFFU;
2105             atl->dataUnion.dataBitField.I                   = 1U;
2106             atl->stateUnion.stateBitField.DT                = 1U;
2107             atl->stateUnion.stateBitField.P                 = 0x00U;
2108             atl->stateUnion.stateBitField.SC                = 0x00U;
2109             if (USB_OUT == tr->direction)
2110             {
2111                 atl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_IN;
2112             }
2113             else
2114             {
2115                 atl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_OUT;
2116             }
2117             atl->control1Union.stateBitField.V = 0x01U;
2118             atl->stateUnion.stateBitField.A    = 0x01U;
2119 
2120             break;
2121         default:
2122             status          = kStatus_USB_Error;
2123             tr->setupStatus = (uint8_t)kStatus_UsbHostIp3516Hs_Idle;
2124             OSA_ENTER_CRITICAL();
2125             pipe->isBusy = 0U;
2126             OSA_EXIT_CRITICAL();
2127             break;
2128     }
2129 
2130     return status;
2131 }
2132 
USB_HostIp3516HsWriteBulkPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe,usb_host_transfer_t * tr)2133 static usb_status_t USB_HostIp3516HsWriteBulkPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
2134                                                   usb_host_ip3516hs_pipe_struct_t *pipe,
2135                                                   usb_host_transfer_t *tr)
2136 {
2137     usb_host_ip3516hs_atl_struct_t *atl = &s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex];
2138     uint32_t transferLength;
2139     uint8_t *bufferAddress;
2140     OSA_SR_ALLOC();
2141 
2142     if (1U == pipe->isBusy)
2143     {
2144         return kStatus_USB_Success;
2145     }
2146 
2147     OSA_ENTER_CRITICAL();
2148     pipe->isBusy = 1U;
2149     OSA_EXIT_CRITICAL();
2150 
2151     transferLength = (tr->transferLength - tr->transferSofar);
2152     if (0U != pipe->bufferLength)
2153     {
2154         bufferAddress = (uint8_t *)&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0];
2155     }
2156     else
2157     {
2158         if (transferLength > 4096U)
2159         {
2160             transferLength = 4096U;
2161         }
2162         if (kStatus_USB_Success == USB_HostIp3516HsGetBuffer(usbHostState, transferLength,
2163                                                              pipe->pipeCommon.maxPacketSize, &pipe->bufferIndex,
2164                                                              &pipe->bufferLength))
2165         {
2166             bufferAddress = (uint8_t *)&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0];
2167         }
2168         else
2169         {
2170             OSA_ENTER_CRITICAL();
2171             pipe->isBusy = 2U;
2172             OSA_EXIT_CRITICAL();
2173             return kStatus_USB_Success;
2174         }
2175     }
2176     if (transferLength > pipe->bufferLength)
2177     {
2178         transferLength = pipe->bufferLength;
2179     }
2180     /*transferLength should be positive*/
2181     if ((USB_OUT == tr->direction) && (transferLength > 0U))
2182     {
2183         (void)memcpy((void *)bufferAddress, (void *)(&tr->transferBuffer[tr->transferSofar]), transferLength);
2184     }
2185 
2186     atl->control2Union.stateBitField.RL             = 0U;
2187     atl->stateUnion.stateBitField.NakCnt            = 0U;
2188     atl->stateUnion.stateBitField.Cerr              = 0x3U;
2189     atl->dataUnion.dataBitField.NrBytesToTransfer   = transferLength;
2190     atl->stateUnion.stateBitField.NrBytesToTransfer = 0U;
2191     atl->dataUnion.dataBitField.DataStartAddress    = ((uint32_t)(bufferAddress)) & 0x0000FFFFU;
2192     atl->dataUnion.dataBitField.I                   = 1U;
2193     atl->stateUnion.stateBitField.SC                = 0x00U;
2194     if (USB_IN == tr->direction)
2195     {
2196         atl->stateUnion.stateBitField.P     = 0x00U;
2197         atl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_IN;
2198     }
2199     else
2200     {
2201         atl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_OUT;
2202     }
2203     atl->control1Union.stateBitField.V = 0x01U;
2204     atl->stateUnion.stateBitField.A    = 0x01U;
2205     return kStatus_USB_Success;
2206 }
2207 
2208 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
USB_HostIp3516HsWriteIsoPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe,usb_host_transfer_t * tr)2209 static usb_status_t USB_HostIp3516HsWriteIsoPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
2210                                                  usb_host_ip3516hs_pipe_struct_t *pipe,
2211                                                  usb_host_transfer_t *tr)
2212 {
2213     usb_host_ip3516hs_ptl_struct_t *ptl;
2214     usb_host_ip3516hs_sptl_struct_t *sptl;
2215     usb_host_transfer_t *currentTr;
2216     uint32_t transferLength;
2217     uint32_t currentUFrame;
2218     uint32_t insertUFrame;
2219     uint32_t primedUFrame;
2220     uint32_t tdIndex;
2221     indexLength_t indexLength;
2222     uint8_t *bufferAddress;
2223     void *temp;
2224     uint8_t speed = ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed;
2225     OSA_SR_ALLOC();
2226 
2227     OSA_ENTER_CRITICAL();
2228 
2229     currentTr = tr;
2230     while (NULL != currentTr)
2231     {
2232         transferLength          = (currentTr->transferLength - currentTr->transferSofar);
2233         indexLength.indexLength = currentTr->union2.frame;
2234         if (0U == indexLength.indexLength)
2235         {
2236             uint32_t bufferIndex;
2237             uint32_t bufferLength;
2238 
2239             tdIndex = 0xFFU;
2240             for (uint32_t i = 0U; i < (USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER); i++)
2241             {
2242                 ptl = &s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[pipe->tdIndex + i];
2243                 if (0U == ptl->dataUnion.dataBitField.NrBytesToTransfer)
2244                 {
2245                     tdIndex = pipe->tdIndex + i;
2246                     temp    = (void *)ptl;
2247                     sptl    = (usb_host_ip3516hs_sptl_struct_t *)temp;
2248                     break;
2249                 }
2250             }
2251             if (0xFFUL == tdIndex)
2252             {
2253                 break;
2254             }
2255 
2256             if (kStatus_USB_Success == USB_HostIp3516HsGetBuffer(usbHostState, transferLength,
2257                                                                  pipe->pipeCommon.maxPacketSize, &bufferIndex,
2258                                                                  &bufferLength))
2259             {
2260                 indexLength.state.bufferIndex  = bufferIndex;
2261                 indexLength.state.bufferLength = bufferLength;
2262                 bufferAddress = (uint8_t *)&s_UsbHostIp3516HsBufferArray[indexLength.state.bufferIndex][0];
2263             }
2264             else
2265             {
2266                 break;
2267             }
2268             indexLength.state.tdIndex = tdIndex;
2269             currentTr->union2.frame   = indexLength.indexLength;
2270         }
2271         else
2272         {
2273             currentTr = currentTr->next;
2274             continue;
2275         }
2276 
2277         if (transferLength > indexLength.state.bufferLength)
2278         {
2279             transferLength = indexLength.state.bufferLength;
2280         }
2281 
2282         if (USB_OUT == currentTr->direction)
2283         {
2284             for (uint32_t i = 0; i < transferLength; i++)
2285             {
2286                 bufferAddress[i] = currentTr->transferBuffer[currentTr->transferSofar + i];
2287             }
2288         }
2289 
2290         if (USB_SPEED_HIGH != speed)
2291         {
2292             if (transferLength > pipe->pipeCommon.maxPacketSize)
2293             {
2294                 transferLength = pipe->pipeCommon.maxPacketSize;
2295             }
2296         }
2297         ptl->control2Union.stateBitField.RL             = 0U;
2298         ptl->stateUnion.stateBitField.NakCnt            = 0U;
2299         ptl->stateUnion.stateBitField.Cerr              = 0U;
2300         ptl->dataUnion.dataBitField.NrBytesToTransfer   = transferLength;
2301         ptl->stateUnion.stateBitField.NrBytesToTransfer = 0U;
2302         ptl->dataUnion.dataBitField.DataStartAddress    = ((uint32_t)(bufferAddress)) & 0x0000FFFFU;
2303         ptl->dataUnion.dataBitField.I                   = 1U;
2304         ptl->stateUnion.stateBitField.SC                = 0x00U;
2305 
2306         ptl->stateUnion.stateBitField.H = 0U;
2307         ptl->stateUnion.stateBitField.X = 0U;
2308         ptl->stateUnion.stateBitField.B = 0U;
2309         if (USB_IN == currentTr->direction)
2310         {
2311             ptl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_IN;
2312         }
2313         else
2314         {
2315             ptl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_OUT;
2316         }
2317 
2318         ptl->statusUnion.status = 0U;
2319         ptl->isoInUnion1.isoIn  = 0U;
2320         ptl->isoInUnion2.isoIn  = 0U;
2321         ptl->isoInUnion3.isoIn  = 0U;
2322 
2323         currentUFrame = (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
2324                                    USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
2325         currentUFrame = currentUFrame & 0xFFU;
2326 
2327         primedUFrame = ((uint32_t)pipe->lastPrimedIndex + pipe->pipeCommon.interval) & 0xFFU;
2328 
2329         if (primedUFrame < currentUFrame)
2330         {
2331             primedUFrame = primedUFrame + 0x100U;
2332         }
2333         if (primedUFrame >= (currentUFrame + USB_HOST_IP3516HS_PERIODIC_TRANSFER_GAP))
2334         {
2335             primedUFrame = primedUFrame & 0xFFU;
2336             insertUFrame = primedUFrame;
2337         }
2338         else
2339         {
2340             insertUFrame =
2341                 pipe->startUFrame + ((currentUFrame - pipe->startUFrame + USB_HOST_IP3516HS_PERIODIC_TRANSFER_GAP +
2342                                       pipe->pipeCommon.interval - 1U) &
2343                                      (~(pipe->pipeCommon.interval - 1U)));
2344         }
2345 
2346         pipe->lastPrimedIndex = (uint16_t)insertUFrame;
2347 
2348         if (USB_SPEED_HIGH == speed)
2349         {
2350             uint32_t primedLength                   = currentTr->transferSofar;
2351             ptl->control1Union.stateBitField.uFrame = insertUFrame & 0xF8U;
2352             ptl->statusUnion.statusBitField.uSA     = 0U;
2353 
2354             for (uint16_t i = (uint16_t)(insertUFrame & 0x07UL); (i < 8U); i += pipe->pipeCommon.interval)
2355             {
2356                 primedLength += (uint32_t)pipe->pipeCommon.maxPacketSize * pipe->pipeCommon.numberPerUframe;
2357                 ptl->statusUnion.statusBitField.uSA |= 1UL << i;
2358                 if (primedLength >= currentTr->transferLength)
2359                 {
2360                     break;
2361                 }
2362             }
2363         }
2364         else
2365         {
2366             if (0U != (ptl->control2Union.stateBitField.S))
2367             {
2368 #if (defined(FSL_FEATURE_USBHSH_VERSION) && (FSL_FEATURE_USBHSH_VERSION >= 300U))
2369                 if (USB_IN == currentTr->direction)
2370                 {
2371                     if (transferLength <= 192U)
2372                     {
2373                         sptl->control1Union.stateBitField.MaxPacketLength = transferLength;
2374                     }
2375                     else
2376                     {
2377                         sptl->control1Union.stateBitField.MaxPacketLength = 192U;
2378                     }
2379                 }
2380                 else
2381 #endif
2382                 {
2383                     if (transferLength <= 188U)
2384                     {
2385 #if (defined(FSL_FEATURE_USBHSH_VERSION) && (FSL_FEATURE_USBHSH_VERSION >= 300U))
2386                         sptl->control1Union.stateBitField.MaxPacketLength = transferLength;
2387 #else
2388                         sptl->control1Union.stateBitField.MaxPacketLength = transferLength + 1U;
2389 #endif
2390                     }
2391                     else
2392                     {
2393                         sptl->control1Union.stateBitField.MaxPacketLength = 188U;
2394                     }
2395                 }
2396             }
2397             else
2398             {
2399                 sptl->control1Union.stateBitField.MaxPacketLength = pipe->pipeCommon.maxPacketSize;
2400             }
2401             sptl->control1Union.stateBitField.uFrame = insertUFrame & 0xF8U;
2402             sptl->statusUnion.statusBitField.uSA     = pipe->ssSlot;
2403             sptl->isoInUnion1.bitField.uSCS          = pipe->csSlot;
2404         }
2405 
2406         ptl->control1Union.stateBitField.V = 0x01U;
2407         ptl->stateUnion.stateBitField.A    = 0x01U;
2408 
2409         currentTr = currentTr->next;
2410     }
2411     OSA_EXIT_CRITICAL();
2412     return kStatus_USB_Success;
2413 }
2414 #endif
2415 
2416 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
USB_HostIp3516HsWriteInterruptPipe(usb_host_ip3516hs_state_struct_t * usbHostState,usb_host_ip3516hs_pipe_struct_t * pipe,usb_host_transfer_t * tr)2417 static usb_status_t USB_HostIp3516HsWriteInterruptPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
2418                                                        usb_host_ip3516hs_pipe_struct_t *pipe,
2419                                                        usb_host_transfer_t *tr)
2420 {
2421     usb_host_ip3516hs_ptl_struct_t *ptl = &s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[pipe->tdIndex];
2422     usb_host_ip3516hs_sptl_struct_t *sptl;
2423     uint32_t transferLength;
2424     uint32_t currentUFrame;
2425     uint32_t insertUFrame;
2426     uint8_t *bufferAddress;
2427     void *temp;
2428     uint8_t speed = ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed;
2429     temp          = (void *)ptl;
2430     sptl          = (usb_host_ip3516hs_sptl_struct_t *)temp;
2431     OSA_SR_ALLOC();
2432 
2433     if (1U == pipe->isBusy)
2434     {
2435         return kStatus_USB_Success;
2436     }
2437 
2438     OSA_ENTER_CRITICAL();
2439     pipe->isBusy = 1U;
2440     OSA_EXIT_CRITICAL();
2441 
2442     transferLength = (tr->transferLength - tr->transferSofar);
2443     if (transferLength > pipe->pipeCommon.maxPacketSize)
2444     {
2445         transferLength = pipe->pipeCommon.maxPacketSize;
2446     }
2447     if (kStatus_USB_Success == USB_HostIp3516HsGetBuffer(usbHostState, transferLength, pipe->pipeCommon.maxPacketSize,
2448                                                          &pipe->bufferIndex, &pipe->bufferLength))
2449     {
2450         bufferAddress = (uint8_t *)&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0];
2451     }
2452     else
2453     {
2454         OSA_ENTER_CRITICAL();
2455         pipe->isBusy = 2U;
2456         OSA_EXIT_CRITICAL();
2457         return kStatus_USB_Success;
2458     }
2459 
2460     if (USB_OUT == tr->direction)
2461     {
2462         for (uint32_t i = 0; i < transferLength; i++)
2463         {
2464             bufferAddress[i] = tr->transferBuffer[tr->transferSofar + i];
2465         }
2466     }
2467 
2468     ptl->control2Union.stateBitField.RL             = 0U;
2469     ptl->stateUnion.stateBitField.NakCnt            = 0x0FU;
2470     ptl->stateUnion.stateBitField.Cerr              = 0x3U;
2471     ptl->dataUnion.dataBitField.NrBytesToTransfer   = transferLength;
2472     ptl->stateUnion.stateBitField.NrBytesToTransfer = 0U;
2473     ptl->dataUnion.dataBitField.DataStartAddress    = ((uint32_t)(bufferAddress)) & 0x0000FFFFU;
2474     ptl->dataUnion.dataBitField.I                   = 1U;
2475     ptl->stateUnion.stateBitField.P                 = 0x00U;
2476     ptl->stateUnion.stateBitField.SC                = 0x00U;
2477     if (USB_IN == tr->direction)
2478     {
2479         ptl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_IN;
2480     }
2481     else
2482     {
2483         ptl->stateUnion.stateBitField.Token = USB_HOST_IP3516HS_PTD_TOKEN_OUT;
2484     }
2485 
2486     ptl->statusUnion.status = 0U;
2487     ptl->isoInUnion1.isoIn  = 0U;
2488     ptl->isoInUnion2.isoIn  = 0U;
2489     ptl->isoInUnion3.isoIn  = 0U;
2490 
2491     currentUFrame = (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
2492                                USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
2493     currentUFrame = currentUFrame & 0xFFU;
2494 
2495     insertUFrame = pipe->startUFrame + ((currentUFrame - pipe->startUFrame + USB_HOST_IP3516HS_PERIODIC_TRANSFER_GAP +
2496                                          pipe->pipeCommon.interval - 1U) &
2497                                         (~(pipe->pipeCommon.interval - 1U)));
2498 
2499     ptl->control1Union.stateBitField.uFrame = (insertUFrame & 0xF8U);
2500     switch (pipe->pipeCommon.interval)
2501     {
2502         case 1:
2503         case 2:
2504         case 4:
2505         case 8:
2506             break;
2507         case 16:
2508             ptl->control1Union.stateBitField.uFrame |= 1U;
2509             break;
2510         case 32:
2511             ptl->control1Union.stateBitField.uFrame |= 2U;
2512             break;
2513         case 64:
2514             ptl->control1Union.stateBitField.uFrame |= 3U;
2515             break;
2516         case 128:
2517             ptl->control1Union.stateBitField.uFrame |= 4U;
2518             break;
2519         case 256:
2520             ptl->control1Union.stateBitField.uFrame |= 5U;
2521             break;
2522         default:
2523             /*no action*/
2524             break;
2525     }
2526 
2527     if (USB_SPEED_HIGH == speed)
2528     {
2529         for (uint32_t i = (insertUFrame & 0x07U); i < 8U; i += pipe->pipeCommon.interval)
2530         {
2531             ptl->statusUnion.statusBitField.uSA |= 1UL << i;
2532         }
2533     }
2534     else
2535     {
2536         sptl->statusUnion.statusBitField.uSA = pipe->ssSlot;
2537         sptl->isoInUnion1.bitField.uSCS      = pipe->csSlot;
2538     }
2539 
2540     ptl->control1Union.stateBitField.V = 0x01U;
2541     ptl->stateUnion.stateBitField.A    = 0x01U;
2542     return kStatus_USB_Success;
2543 }
2544 #endif
2545 
USB_HostIp3516HsGetPipe(usb_host_ip3516hs_state_struct_t * usbHostState,uint8_t pipeType,uint8_t tdIndex)2546 static usb_host_ip3516hs_pipe_struct_t *USB_HostIp3516HsGetPipe(usb_host_ip3516hs_state_struct_t *usbHostState,
2547                                                                 uint8_t pipeType,
2548                                                                 uint8_t tdIndex)
2549 {
2550     usb_host_ip3516hs_pipe_struct_t *p = usbHostState->pipeListInUsing;
2551     void *temp;
2552     OSA_SR_ALLOC();
2553 
2554     OSA_ENTER_CRITICAL();
2555     while (p != NULL)
2556     {
2557         if ((p->pipeCommon.pipeType == pipeType) && (p->tdIndex == tdIndex))
2558         {
2559             OSA_EXIT_CRITICAL();
2560             return p;
2561         }
2562         if ((p->pipeCommon.pipeType == pipeType) && (USB_ENDPOINT_ISOCHRONOUS == pipeType) && (p->tdIndex >= tdIndex) &&
2563             (tdIndex < (p->tdIndex + USB_HOST_IP3516HS_ISO_MULTIPLE_TRANSFER)))
2564         {
2565             OSA_EXIT_CRITICAL();
2566             return p;
2567         }
2568         temp = (void *)p->pipeCommon.next;
2569         p    = (usb_host_ip3516hs_pipe_struct_t *)temp;
2570     }
2571     OSA_EXIT_CRITICAL();
2572     return NULL;
2573 }
2574 
2575 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
USB_HostIp3516HsCheckIsoTransferSofar(usb_host_ip3516hs_state_struct_t * usbHostState)2576 static usb_status_t USB_HostIp3516HsCheckIsoTransferSofar(usb_host_ip3516hs_state_struct_t *usbHostState)
2577 {
2578     uint32_t tokenDoneBits = usbHostState->usbRegBase->ISO_PTD_DONE_MAP;
2579     usb_host_ip3516hs_pipe_struct_t *p;
2580 
2581     if (0U == tokenDoneBits)
2582     {
2583         return kStatus_USB_Success;
2584     }
2585     for (uint8_t i = 0; i < 32U; i++)
2586     {
2587         if (0U != (tokenDoneBits & (1UL << i)))
2588         {
2589             if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].stateUnion.stateBitField.A) &&
2590                 (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].control1Union.stateBitField.V))
2591             {
2592                 continue;
2593             }
2594             p = USB_HostIp3516HsGetPipe(usbHostState, USB_ENDPOINT_ISOCHRONOUS, i);
2595             if (NULL != p)
2596             {
2597                 p->isBusy = 0U;
2598 #if 0
2599                 if (NULL != p->trList)
2600                 {
2601                     usb_host_transfer_t *trCurrent = p->trList;
2602                     if ((s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[p->tdIndex].stateUnion.stateBitField.B) ||
2603                         (s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[p->tdIndex].stateUnion.stateBitField.X))
2604                     {
2605                         trCurrent->union1.transferResult = kStatus_USB_TransferFailed;
2606                     }
2607                     else
2608                     {
2609                         trCurrent->union1.transferResult = kStatus_USB_Success;
2610                     }
2611                     if (USB_IN == trCurrent->direction)
2612                     {
2613                         memcpy(&trCurrent->transferBuffer[trCurrent->transferSofar], &s_UsbHostIp3516HsBufferArray[p->bufferIndex][0], s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[p->tdIndex].stateUnion.stateBitField.NrBytesToTransfer);
2614                     }
2615                     trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2616                                                     .iso[p->tdIndex]
2617                                                     .stateUnion.stateBitField.NrBytesToTransfer;
2618                     if (p->trList)
2619                     {
2620                         p->currentTr = p->trList->next;
2621                     }
2622                     else
2623                     {
2624                         p->currentTr = NULL;
2625                     }
2626                     if (p->currentTr)
2627                     {
2628                         uint32_t trValue = (uint32_t)p->currentTr;
2629                         USB_HostIp3516HsWriteIsoPipe(usbHostState, p, (usb_host_transfer_t *)trValue);
2630                     }
2631                 }
2632 #endif
2633             }
2634         }
2635     }
2636     usbHostState->usbRegBase->ISO_PTD_DONE_MAP = tokenDoneBits;
2637     return kStatus_USB_Success;
2638 }
2639 #endif
2640 
2641 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
USB_HostIp3516HsCheckIntTransferSofar(usb_host_ip3516hs_state_struct_t * usbHostState)2642 static usb_status_t USB_HostIp3516HsCheckIntTransferSofar(usb_host_ip3516hs_state_struct_t *usbHostState)
2643 {
2644     uint32_t tokenDoneBits = usbHostState->usbRegBase->INT_PTD_DONE_MAP;
2645     usb_host_ip3516hs_pipe_struct_t *p;
2646 
2647     if (0U == tokenDoneBits)
2648     {
2649         return kStatus_USB_Success;
2650     }
2651     for (uint8_t i = 0; i < 32U; i++)
2652     {
2653         if (0U != (tokenDoneBits & (1UL << i)))
2654         {
2655             if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].stateUnion.stateBitField.A) &&
2656                 (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].control1Union.stateBitField.V))
2657             {
2658                 continue;
2659             }
2660             p = USB_HostIp3516HsGetPipe(usbHostState, USB_ENDPOINT_INTERRUPT, i);
2661             if (NULL != p)
2662             {
2663                 p->isBusy = 0U;
2664             }
2665         }
2666     }
2667     usbHostState->usbRegBase->INT_PTD_DONE_MAP = tokenDoneBits;
2668     return kStatus_USB_Success;
2669 }
2670 #endif
2671 
USB_HostIp3516HsCheckAtlTransferSofar(usb_host_ip3516hs_state_struct_t * usbHostState)2672 static usb_status_t USB_HostIp3516HsCheckAtlTransferSofar(usb_host_ip3516hs_state_struct_t *usbHostState)
2673 {
2674     uint32_t tokenDoneBits = usbHostState->usbRegBase->ATL_PTD_DONE_MAP;
2675     usb_host_ip3516hs_pipe_struct_t *p;
2676 
2677     if (0U == tokenDoneBits)
2678     {
2679         return kStatus_USB_Success;
2680     }
2681     for (uint8_t i = 0; i < 32U; i++)
2682     {
2683         if (0U != (tokenDoneBits & (1UL << i)))
2684         {
2685             p = USB_HostIp3516HsGetPipe(usbHostState, USB_ENDPOINT_CONTROL, i);
2686             if (NULL != p)
2687             {
2688                 p->isBusy = 0U;
2689             }
2690             p = USB_HostIp3516HsGetPipe(usbHostState, USB_ENDPOINT_BULK, i);
2691             if ((NULL != p))
2692             {
2693                 p->isBusy = 0U;
2694             }
2695         }
2696     }
2697     usbHostState->usbRegBase->ATL_PTD_DONE_MAP = tokenDoneBits;
2698     return kStatus_USB_Success;
2699 }
2700 
USB_HostIp3516HsCheckGetBufferFailedPipe(usb_host_ip3516hs_state_struct_t * usbHostState)2701 static void USB_HostIp3516HsCheckGetBufferFailedPipe(usb_host_ip3516hs_state_struct_t *usbHostState)
2702 {
2703     usb_host_ip3516hs_pipe_struct_t *pipe = usbHostState->pipeListInUsing;
2704     void *temp;
2705     while (NULL != pipe)
2706     {
2707         if (2U == pipe->isBusy)
2708         {
2709             if (NULL != pipe->trList)
2710             {
2711                 switch (pipe->pipeCommon.pipeType)
2712                 {
2713                     case USB_ENDPOINT_BULK:
2714                         (void)USB_HostIp3516HsWriteBulkPipe(usbHostState, pipe, pipe->trList);
2715                         break;
2716 
2717                     case USB_ENDPOINT_CONTROL:
2718                         (void)USB_HostIp3516HsWriteControlPipe(usbHostState, pipe, pipe->trList);
2719                         break;
2720 
2721 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
2722                     case USB_ENDPOINT_ISOCHRONOUS:
2723                         (void)USB_HostIp3516HsWriteIsoPipe(usbHostState, pipe, pipe->trList);
2724                         break;
2725 #endif
2726 
2727 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
2728                     case USB_ENDPOINT_INTERRUPT:
2729                         (void)USB_HostIp3516HsWriteInterruptPipe(usbHostState, pipe, pipe->trList);
2730                         break;
2731 #endif
2732 
2733                     default:
2734                         /*no action*/
2735                         break;
2736                 }
2737             }
2738         }
2739 
2740         temp = (void *)pipe->pipeCommon.next;
2741         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
2742     }
2743 }
2744 
USB_HostIp3516HsTokenDone(usb_host_ip3516hs_state_struct_t * usbHostState)2745 static usb_status_t USB_HostIp3516HsTokenDone(usb_host_ip3516hs_state_struct_t *usbHostState)
2746 {
2747     usb_host_ip3516hs_pipe_struct_t *pipe;
2748     usb_host_transfer_t *trCurrent;
2749     usb_host_ip3516hs_pipe_struct_t *p;
2750     uint32_t startUFrame;
2751     uint32_t currentUFrame;
2752     usb_host_ip3516hs_transfer_status_t transferStatus;
2753     void *temp;
2754 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
2755     indexLength_t indexLength;
2756 #endif
2757     usb_status_t trStatus;
2758     uint8_t trDone;
2759 
2760     /* Enter critical */
2761     USB_HostIp3516HsLock();
2762     USB_HostOhciDisableIsr(usbHostState);
2763     pipe = usbHostState->pipeListInUsing;
2764     while (NULL != pipe)
2765     {
2766         trStatus  = kStatus_USB_Success;
2767         trCurrent = pipe->trList;
2768         if (NULL == trCurrent)
2769         {
2770         }
2771         else
2772         {
2773             trDone = 0;
2774             switch (pipe->pipeCommon.pipeType)
2775             {
2776                 case USB_ENDPOINT_CONTROL:
2777                     if (0U != pipe->isBusy)
2778                     {
2779                         temp = (void *)pipe->pipeCommon.next;
2780                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
2781                         continue;
2782                     }
2783                     transferStatus = (usb_host_ip3516hs_transfer_status_t)trCurrent->setupStatus;
2784                     switch (transferStatus)
2785                     {
2786                         case kStatus_UsbHostIp3516Hs_Idle:
2787                             break;
2788                         case kStatus_UsbHostIp3516Hs_Setup:
2789                             break;
2790                         case kStatus_UsbHostIp3516Hs_Data2:
2791                             if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2792                                            .atl[pipe->tdIndex]
2793                                            .stateUnion.stateBitField.NrBytesToTransfer) &&
2794                                 (0U == (s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2795                                             .atl[pipe->tdIndex]
2796                                             .stateUnion.stateBitField.NrBytesToTransfer %
2797                                         pipe->pipeCommon.maxPacketSize)))
2798                             {
2799                             }
2800                             else
2801                             {
2802                                 trCurrent->setupStatus = (uint8_t)kStatus_UsbHostIp3516Hs_Data;
2803                             }
2804                             if (USB_IN == trCurrent->direction)
2805                             {
2806                                 (void)memcpy((void *)(&trCurrent->transferBuffer[trCurrent->transferSofar]),
2807                                              (void *)(&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0]),
2808                                              s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2809                                                  .atl[pipe->tdIndex]
2810                                                  .stateUnion.stateBitField.NrBytesToTransfer);
2811                             }
2812                             trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2813                                                             .atl[pipe->tdIndex]
2814                                                             .stateUnion.stateBitField.NrBytesToTransfer;
2815                             break;
2816                         case kStatus_UsbHostIp3516Hs_Data:
2817                             if (USB_IN == trCurrent->direction)
2818                             {
2819                                 (void)memcpy((void *)(&trCurrent->transferBuffer[trCurrent->transferSofar]),
2820                                              (void *)(&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0]),
2821                                              s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2822                                                  .atl[pipe->tdIndex]
2823                                                  .stateUnion.stateBitField.NrBytesToTransfer);
2824                             }
2825                             trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2826                                                             .atl[pipe->tdIndex]
2827                                                             .stateUnion.stateBitField.NrBytesToTransfer;
2828                             break;
2829                         case kStatus_UsbHostIp3516Hs_State:
2830                             break;
2831                         default:
2832                             /*no action*/
2833                             break;
2834                     }
2835 
2836                     if (!((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2837                                      .atl[pipe->tdIndex]
2838                                      .stateUnion.stateBitField.A) &&
2839                           (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2840                                      .atl[pipe->tdIndex]
2841                                      .control1Union.stateBitField.V)))
2842                     {
2843                         trDone = 1U;
2844                     }
2845 
2846                     if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2847                                    .atl[pipe->tdIndex]
2848                                    .stateUnion.stateBitField.B) ||
2849                         (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2850                                    .atl[pipe->tdIndex]
2851                                    .stateUnion.stateBitField.X))
2852                     {
2853                         trStatus = kStatus_USB_TransferFailed;
2854                     }
2855                     else if (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2856                                        .atl[pipe->tdIndex]
2857                                        .stateUnion.stateBitField.H)
2858                     {
2859                         trStatus = kStatus_USB_TransferStall;
2860                     }
2861                     else
2862                     {
2863                         if (0U != trDone)
2864                         {
2865 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
2866                             if (1U == usbHostState->complianceTestStart)
2867                             {
2868                                 usbHostState->complianceTest++;
2869                             }
2870 #endif
2871                             (void)USB_HostIp3516HsWriteControlPipe(usbHostState, pipe, pipe->trList);
2872                         }
2873                     }
2874 
2875                     if (0U != pipe->isBusy)
2876                     {
2877                         temp = (void *)pipe->pipeCommon.next;
2878                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
2879                         continue;
2880                     }
2881 
2882                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.H = 0U;
2883                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.X = 0U;
2884                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.B = 0U;
2885                     break;
2886                 case USB_ENDPOINT_BULK:
2887                     if (0U != pipe->isBusy)
2888                     {
2889                         temp = (void *)pipe->pipeCommon.next;
2890                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
2891                         continue;
2892                     }
2893 
2894                     if (USB_IN == trCurrent->direction)
2895                     {
2896                         uint32_t length = s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2897                                               .atl[pipe->tdIndex]
2898                                               .stateUnion.stateBitField.NrBytesToTransfer;
2899                         (void)memcpy((void *)(&trCurrent->transferBuffer[trCurrent->transferSofar]),
2900                                      (void *)(&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0]), length);
2901                     }
2902                     trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2903                                                     .atl[pipe->tdIndex]
2904                                                     .stateUnion.stateBitField.NrBytesToTransfer;
2905 
2906                     if (!((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2907                                      .atl[pipe->tdIndex]
2908                                      .stateUnion.stateBitField.A) &&
2909                           (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2910                                      .atl[pipe->tdIndex]
2911                                      .control1Union.stateBitField.V)))
2912                     {
2913                         trDone = 1U;
2914                     }
2915 
2916                     if (0U !=
2917                         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.H)
2918                     {
2919                         trStatus = kStatus_USB_TransferStall;
2920                     }
2921                     else if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2922                                         .atl[pipe->tdIndex]
2923                                         .stateUnion.stateBitField.B) ||
2924                              (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2925                                         .atl[pipe->tdIndex]
2926                                         .stateUnion.stateBitField.X))
2927                     {
2928                         trStatus = kStatus_USB_TransferFailed;
2929                     }
2930                     else
2931                     {
2932                         if (0U != trDone)
2933                         {
2934                             if ((0U == (pipe->trList->transferSofar % pipe->pipeCommon.maxPacketSize)) &&
2935                                 (pipe->trList->transferSofar < pipe->trList->transferLength))
2936                             {
2937                                 (void)USB_HostIp3516HsWriteBulkPipe(usbHostState, pipe, pipe->trList);
2938                             }
2939                         }
2940                     }
2941 
2942                     if (0U != pipe->isBusy)
2943                     {
2944                         temp = (void *)pipe->pipeCommon.next;
2945                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
2946                         continue;
2947                     }
2948 
2949                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.H = 0U;
2950                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.X = 0U;
2951                     s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[pipe->tdIndex].stateUnion.stateBitField.B = 0U;
2952 
2953                     break;
2954 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
2955                 case USB_ENDPOINT_INTERRUPT:
2956                     if (0U != pipe->isBusy)
2957                     {
2958                         temp = (void *)pipe->pipeCommon.next;
2959                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
2960                         continue;
2961                     }
2962 
2963                     if (!((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2964                                      .interrupt[pipe->tdIndex]
2965                                      .stateUnion.stateBitField.A) &&
2966                           (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2967                                      .interrupt[pipe->tdIndex]
2968                                      .control1Union.stateBitField.V)))
2969                     {
2970                         trDone = 1U;
2971                     }
2972 
2973                     if (USB_IN == trCurrent->direction)
2974                     {
2975                         (void)memcpy((void *)(&trCurrent->transferBuffer[trCurrent->transferSofar]),
2976                                      (void *)(&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0]),
2977                                      s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2978                                          .interrupt[pipe->tdIndex]
2979                                          .stateUnion.stateBitField.NrBytesToTransfer);
2980                     }
2981                     trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2982                                                     .interrupt[pipe->tdIndex]
2983                                                     .stateUnion.stateBitField.NrBytesToTransfer;
2984 
2985                     if (1U == s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2986                                   .interrupt[pipe->tdIndex]
2987                                   .stateUnion.stateBitField.H)
2988                     {
2989                         trStatus = kStatus_USB_TransferStall;
2990                     }
2991                     else if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2992                                         .interrupt[pipe->tdIndex]
2993                                         .stateUnion.stateBitField.B) ||
2994                              (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
2995                                         .interrupt[pipe->tdIndex]
2996                                         .stateUnion.stateBitField.X))
2997                     {
2998                         trStatus = kStatus_USB_TransferFailed;
2999                     }
3000                     else
3001                     {
3002                         /*no action*/
3003                     }
3004                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3005                         .interrupt[pipe->tdIndex]
3006                         .stateUnion.stateBitField.H = 0U;
3007                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3008                         .interrupt[pipe->tdIndex]
3009                         .stateUnion.stateBitField.X = 0U;
3010                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3011                         .interrupt[pipe->tdIndex]
3012                         .stateUnion.stateBitField.B = 0U;
3013                     break;
3014 #endif
3015 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3016                 case USB_ENDPOINT_ISOCHRONOUS:
3017 #if 1
3018                     indexLength.indexLength = trCurrent->union2.frame;
3019                     if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3020                                    .iso[indexLength.state.tdIndex]
3021                                    .stateUnion.stateBitField.A) &&
3022                         (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3023                                    .iso[indexLength.state.tdIndex]
3024                                    .control1Union.stateBitField.V))
3025                     {
3026                         temp = (void *)pipe->pipeCommon.next;
3027                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3028                         continue;
3029                     }
3030                     trCurrent->union2.frame = 0u;
3031                     pipe->bufferIndex  = indexLength.state.bufferIndex;
3032                     pipe->bufferLength = indexLength.state.bufferLength;
3033                     if (USB_IN == trCurrent->direction)
3034                     {
3035                         (void)memcpy((void *)(&trCurrent->transferBuffer[trCurrent->transferSofar]),
3036                                      (void *)(&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0]),
3037                                      s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3038                                          .iso[indexLength.state.tdIndex]
3039                                          .stateUnion.stateBitField.NrBytesToTransfer);
3040                     }
3041                     trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3042                                                     .iso[indexLength.state.tdIndex]
3043                                                     .stateUnion.stateBitField.NrBytesToTransfer;
3044 
3045                     if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3046                                    .iso[indexLength.state.tdIndex]
3047                                    .stateUnion.stateBitField.B) ||
3048                         (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3049                                    .iso[indexLength.state.tdIndex]
3050                                    .stateUnion.stateBitField.X))
3051                     {
3052                         trCurrent->union1.transferResult = (int32_t)kStatus_USB_TransferFailed;
3053                     }
3054                     else
3055                     {
3056                         trCurrent->union1.transferResult = (int32_t)kStatus_USB_Success;
3057                     }
3058 
3059                     if (NULL != pipe->trList)
3060                     {
3061                         pipe->currentTr = pipe->trList->next;
3062                     }
3063                     else
3064                     {
3065                         pipe->currentTr = NULL;
3066                     }
3067 
3068                     if (NULL != pipe->currentTr)
3069                     {
3070                         uint32_t trValue = (uint32_t)pipe->currentTr;
3071                         (void)USB_HostIp3516HsWriteIsoPipe(usbHostState, pipe, (usb_host_transfer_t *)trValue);
3072                     }
3073 #else
3074                     if (pipe->trList == pipe->currentTr)
3075                     {
3076                         temp = (void *)pipe->pipeCommon.next;
3077                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3078                         continue;
3079                     }
3080 #endif
3081                     trDone = 1U;
3082 
3083                     trStatus = (usb_status_t)trCurrent->union1.transferResult;
3084                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3085                         .iso[indexLength.state.tdIndex]
3086                         .dataUnion.dataBitField.NrBytesToTransfer = 0U;
3087 
3088                     break;
3089 #endif
3090                 default:
3091                     /*no action*/
3092                     break;
3093             }
3094             if (0U != trDone)
3095             {
3096                 pipe->trList = trCurrent->next;
3097 #if 0
3098 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3099                 if ((USB_ENDPOINT_ISOCHRONOUS != pipe->pipeCommon.pipeType) && (pipe->bufferLength))
3100 #else
3101                 if ((pipe->bufferLength)
3102 #endif
3103 
3104 #else
3105                 if ((0U != pipe->bufferLength))
3106 #endif
3107                 {
3108                     (void)USB_HostIp3516HsFreeBuffer(usbHostState, pipe->bufferIndex, pipe->bufferLength);
3109                     pipe->bufferLength = 0U;
3110                 }
3111                 pipe->cutOffTime = USB_HOST_IP3516HS_TRANSFER_TIMEOUT_GAP;
3112 
3113                 if ((kStatus_USB_Success == (trStatus)) && (USB_ENDPOINT_CONTROL == pipe->pipeCommon.pipeType) &&
3114                     (USB_REQUEST_STANDARD_CLEAR_FEATURE == trCurrent->setupPacket->bRequest) &&
3115                     (USB_REQUEST_TYPE_RECIPIENT_ENDPOINT == trCurrent->setupPacket->bmRequestType) &&
3116                     (USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT ==
3117                      (USB_SHORT_FROM_LITTLE_ENDIAN(trCurrent->setupPacket->wValue) & 0x00FFU)))
3118                 {
3119                     p = usbHostState->pipeListInUsing;
3120                     while (p != NULL)
3121                     {
3122                         /* only compute bulk and interrupt pipe */
3123                         if ((p->pipeCommon.deviceHandle == pipe->pipeCommon.deviceHandle) &&
3124                             ((p->pipeCommon.endpointAddress |
3125                               (p->pipeCommon.direction << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) ==
3126                              ((uint8_t)(USB_SHORT_FROM_LITTLE_ENDIAN(trCurrent->setupPacket->wIndex)))))
3127                         {
3128                             break;
3129                         }
3130                         temp = (void *)p->pipeCommon.next;
3131                         p    = (usb_host_ip3516hs_pipe_struct_t *)temp;
3132                     }
3133 
3134                     if ((NULL != p) && ((USB_ENDPOINT_BULK == p->pipeCommon.pipeType) ||
3135                                         (USB_ENDPOINT_INTERRUPT == p->pipeCommon.pipeType)))
3136                     {
3137                         if (USB_ENDPOINT_BULK == p->pipeCommon.pipeType)
3138                         {
3139                             usbHostState->usbRegBase->ATL_PTD_SKIP_MAP |= 1UL << p->tdIndex;
3140                         }
3141 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3142                         else if (USB_ENDPOINT_INTERRUPT == p->pipeCommon.pipeType)
3143                         {
3144                             usbHostState->usbRegBase->INT_PTD_SKIP_MAP |= 1UL << p->tdIndex;
3145                         }
3146 #endif
3147                         else
3148                         {
3149                         }
3150 
3151                         startUFrame = (uint32_t)(
3152                             (usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
3153                             USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
3154                         currentUFrame = startUFrame;
3155 
3156                         while (currentUFrame == startUFrame)
3157                         {
3158                             currentUFrame = (uint32_t)(
3159                                 (usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
3160                                 USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
3161                         }
3162 
3163                         if (USB_ENDPOINT_BULK == p->pipeCommon.pipeType)
3164                         {
3165                             s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3166                                 .atl[p->tdIndex]
3167                                 .stateUnion.stateBitField.DT = 0U;
3168                             usbHostState->usbRegBase->ATL_PTD_SKIP_MAP &= ~(1UL << p->tdIndex);
3169                         }
3170 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3171                         else if (USB_ENDPOINT_INTERRUPT == p->pipeCommon.pipeType)
3172                         {
3173                             s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3174                                 .interrupt[p->tdIndex]
3175                                 .stateUnion.stateBitField.DT = 0U;
3176                             usbHostState->usbRegBase->INT_PTD_SKIP_MAP &= ~(1UL << p->tdIndex);
3177                         }
3178 #endif
3179                         else
3180                         {
3181                         }
3182                     }
3183                 }
3184                 /* callback function is different from the current condition */
3185                 trCurrent->callbackFn(trCurrent->callbackParam, trCurrent, trStatus); /* transfer callback */
3186                 USB_HostIp3516HsCheckGetBufferFailedPipe(usbHostState);
3187                 if (NULL != pipe->trList)
3188                 {
3189                     switch (pipe->pipeCommon.pipeType)
3190                     {
3191                         case USB_ENDPOINT_BULK:
3192                             (void)USB_HostIp3516HsWriteBulkPipe(usbHostState, pipe, pipe->trList);
3193                             break;
3194 
3195                         case USB_ENDPOINT_CONTROL:
3196                             (void)USB_HostIp3516HsWriteControlPipe(usbHostState, pipe, pipe->trList);
3197                             break;
3198 
3199 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3200                         case USB_ENDPOINT_ISOCHRONOUS:
3201                             (void)USB_HostIp3516HsWriteIsoPipe(usbHostState, pipe, pipe->trList);
3202                             break;
3203 #endif
3204 
3205 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3206                         case USB_ENDPOINT_INTERRUPT:
3207                             (void)USB_HostIp3516HsWriteInterruptPipe(usbHostState, pipe, pipe->trList);
3208                             break;
3209 #endif
3210 
3211                         default:
3212                             /*no action*/
3213                             break;
3214                     }
3215                 }
3216             }
3217         }
3218         temp = (void *)pipe->pipeCommon.next;
3219         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3220     }
3221     USB_HostOhciEnableIsr(usbHostState);
3222     USB_HostIp3516HsUnlock();
3223     return kStatus_USB_Success;
3224 }
3225 
USB_HostIp3516HsSof(usb_host_ip3516hs_state_struct_t * usbHostState)3226 static usb_status_t USB_HostIp3516HsSof(usb_host_ip3516hs_state_struct_t *usbHostState)
3227 {
3228     usb_host_ip3516hs_pipe_struct_t *pipe;
3229     void *temp;
3230     /* Enter critical */
3231     USB_HostIp3516HsLock();
3232     pipe = usbHostState->pipeListInUsing;
3233     while (NULL != pipe)
3234     {
3235         if ((USB_ENDPOINT_CONTROL == pipe->pipeCommon.pipeType) || (USB_ENDPOINT_BULK == pipe->pipeCommon.pipeType))
3236         {
3237             if (NULL != pipe->trList)
3238             {
3239                 pipe->cutOffTime--;
3240                 if (0U == pipe->cutOffTime)
3241                 {
3242                     pipe->trList->union1.transferResult = (int)kStatus_USB_TransferFailed;
3243                     (void)USB_HostIp3516HsCancelPipe(usbHostState, pipe, pipe->trList);
3244                 }
3245             }
3246         }
3247         temp = (void *)pipe->pipeCommon.next;
3248         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3249     }
3250     /* Exit critical */
3251     USB_HostIp3516HsUnlock();
3252 
3253     return kStatus_USB_Success;
3254 }
3255 
USB_HostIp3516HsControllerReset(usb_host_ip3516hs_state_struct_t * usbHostState)3256 static usb_status_t USB_HostIp3516HsControllerReset(usb_host_ip3516hs_state_struct_t *usbHostState)
3257 {
3258     usbHostState->usbRegBase->USBCMD = USB_HOST_IP3516HS_USBCMD_HCRESET_MASK;
3259     while (0U != (usbHostState->usbRegBase->USBCMD & USB_HOST_IP3516HS_USBCMD_HCRESET_MASK))
3260     {
3261         __NOP();
3262     }
3263     return kStatus_USB_Success;
3264 }
3265 
USB_HostIp3516HsControllerInit(usb_host_ip3516hs_state_struct_t * usbHostState)3266 static usb_status_t USB_HostIp3516HsControllerInit(usb_host_ip3516hs_state_struct_t *usbHostState)
3267 {
3268     usb_host_ip3516hs_pipe_struct_t *pipe;
3269 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_GTD) && (USB_HOST_CONFIG_IP3516HS_MAX_GTD > 0U))
3270     usb_host_ip3516hs_atl_struct_t *gtd;
3271 #endif
3272 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ITD) && (USB_HOST_CONFIG_IP3516HS_MAX_ITD > 0U))
3273     usb_host_ip3516hs_isochronous_transfer_descritpor_struct_t *itd;
3274 #endif
3275     uint32_t i;
3276     uint32_t interruptState;
3277     void *temp;
3278     OSA_SR_ALLOC();
3279 
3280     /* Enter critical */
3281     OSA_ENTER_CRITICAL();
3282 
3283     for (i = 0U; i < (sizeof(usbHostState->bufferArrayBitMap) / (sizeof(uint32_t))); i++)
3284     {
3285         usbHostState->bufferArrayBitMap[i] = 0xFFFFFFFFU;
3286     }
3287 
3288     for (i = 0U; i < (sizeof(s_UsbHostIp3516HsBufferArray) / (sizeof(uint8_t) * 64U)); i++)
3289     {
3290         usbHostState->bufferArrayBitMap[i >> 5U] &= ~(1UL << (i % 32U));
3291     }
3292 
3293     for (i = 0U;
3294          i < (sizeof(s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl) / sizeof(usb_host_ip3516hs_atl_struct_t));
3295          i++)
3296     {
3297         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].control1Union.controlState = 0U;
3298         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].control2Union.controlState = 0U;
3299         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].dataUnion.data             = 0U;
3300         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].stateUnion.state           = 0U;
3301     }
3302     usbHostState->usbRegBase->ATL_PTD_BASE_ADDRESS =
3303         (((uint32_t)(&s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[0])) &
3304          USB_HOST_IP3516HS_ATL_PTD_ATL_BASE_MASK);
3305     usbHostState->usbRegBase->LAST_PTD_IN_USE = (i - 1U) & USB_HOST_IP3516HS_LAST_PTD_IN_USE_ATL_LAST_MASK;
3306     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_ATL_EN_MASK;
3307     interruptState = USB_HOST_IP3516HS_USBINTR_ATL_IRQ_E_MASK;
3308 
3309 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3310     for (i = 0U; i < USB_HOST_CONFIG_IP3516HS_MAX_ISO; i++)
3311     {
3312         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].control1Union.controlState = 0U;
3313         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].control2Union.controlState = 0U;
3314         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].dataUnion.data             = 0U;
3315         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].stateUnion.state           = 0U;
3316     }
3317     usbHostState->usbRegBase->ISO_PTD_BASE_ADDRESS =
3318         (((uint32_t)(&s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[0])) &
3319          USB_HOST_IP3516HS_ISO_PTD_ISO_BASE_MASK);
3320     usbHostState->usbRegBase->LAST_PTD_IN_USE |=
3321         ((USB_HOST_CONFIG_IP3516HS_MAX_ISO - 1UL) << USB_HOST_IP3516HS_LAST_PTD_IN_USE_ISO_LAST_SHIFT) &
3322         USB_HOST_IP3516HS_LAST_PTD_IN_USE_ISO_LAST_MASK;
3323     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_ISO_EN_MASK;
3324     interruptState |= USB_HOST_IP3516HS_USBINTR_ISO_IRQ_E_MASK;
3325 #endif
3326 
3327 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3328     for (i = 0U; i < USB_HOST_CONFIG_IP3516HS_MAX_INT; i++)
3329     {
3330         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].control1Union.controlState = 0U;
3331         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].control2Union.controlState = 0U;
3332         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].dataUnion.data             = 0U;
3333         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].stateUnion.state           = 0U;
3334     }
3335     usbHostState->usbRegBase->INT_PTD_BASE_ADDRESS =
3336         (((uint32_t)(&s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[0])) &
3337          USB_HOST_IP3516HS_INT_PTD_INT_BASE_MASK);
3338     usbHostState->usbRegBase->LAST_PTD_IN_USE |=
3339         (((uint32_t)USB_HOST_CONFIG_IP3516HS_MAX_INT - 1U) << USB_HOST_IP3516HS_LAST_PTD_IN_USE_INT_LAST_SHIFT) &
3340         USB_HOST_IP3516HS_LAST_PTD_IN_USE_INT_LAST_MASK;
3341     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_INT_EN_MASK;
3342     interruptState |= USB_HOST_IP3516HS_USBINTR_INT_IRQ_E_MASK;
3343 #endif
3344 
3345     usbHostState->pipeListInUsing = NULL;
3346     usbHostState->pipeList        = &usbHostState->pipePool[0];
3347     pipe                          = usbHostState->pipeList;
3348     for (i = 1U; i < USB_HOST_CONFIG_IP3516HS_MAX_PIPE; i++)
3349     {
3350         temp                  = (void *)(&usbHostState->pipePool[i]);
3351         pipe->pipeCommon.next = (usb_host_pipe_t *)temp;
3352         temp                  = (void *)pipe->pipeCommon.next;
3353         pipe                  = (usb_host_ip3516hs_pipe_struct_t *)temp;
3354     }
3355     pipe->pipeCommon.next = NULL;
3356 
3357     /* Exit critical */
3358     OSA_EXIT_CRITICAL();
3359 
3360     if (0U != (usbHostState->usbRegBase->HCSPARAMS &
3361                USB_HOST_IP3516HS_HCSPARAMS_PPC_MASK)) /* Ports have power port switches */
3362     {
3363         /* only has one port */
3364         uint32_t tmp = usbHostState->usbRegBase->PORTSC1;
3365         tmp &= (~USB_HOST_IP3516HS_PORTSC1_WIC);
3366         usbHostState->usbRegBase->PORTSC1 = (tmp | USB_HOST_IP3516HS_PORTSC1_PP_MASK); /* turn on port power */
3367     }
3368     usbHostState->usbRegBase->DATA_PAYLOAD_BASE_ADDRESS =
3369         (uint32_t)(&s_UsbHostIp3516HsBufferArray[0][0]) & USB_HOST_IP3516HS_DAT_BASE_MASK;
3370 
3371     usbHostState->usbRegBase->USBSTS = 0xFFFFFFFFU;
3372 
3373     usbHostState->usbRegBase->USBINTR =
3374         interruptState | USB_HOST_IP3516HS_USBINTR_PCDE_MASK | USB_HOST_IP3516HS_USBINTR_SOF_E_MASK;
3375 
3376 /* On RT600, there is no attach information when LS device is connected, the designer said it is due to the default
3377  * turnaround time is too small, the 0x654 is their suggested value. */
3378 #if (defined(FSL_FEATURE_USBHSH_HAS_TURNAROUND_TIMEOUT) && (FSL_FEATURE_USBHSH_HAS_TURNAROUND_TIMEOUT > 0U))
3379     usbHostState->usbRegBase->PACKET_TURNAROUND_TIMEOUT &= ~USBHSH_PACKET_TURNAROUND_TIME_HOST_TIMEOUT_LS_MASK;
3380     usbHostState->usbRegBase->PACKET_TURNAROUND_TIMEOUT |= 0x00000654;
3381 #endif
3382 
3383     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
3384 
3385     return kStatus_USB_Success;
3386 }
3387 
3388 /*!
3389  * @brief create the USB host ip3516hs instance.
3390  *
3391  * This function initializes the USB host ip3516hs controller driver.
3392  *
3393  * @param controllerId        The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
3394  * @param hostHandle         The host level handle.
3395  * @param controllerHandle  Return the controller instance handle.
3396  *
3397  * @retval kStatus_USB_Success              The host is initialized successfully.
3398  * @retval kStatus_USB_AllocFail             allocate memory fail.
3399  * @retval kStatus_USB_Error                 host mutex create fail, IP3516HS mutex or IP3516HS event create fail.
3400  *                                                         Or, IP3516HS IP initialize fail.
3401  */
USB_HostIp3516HsCreate(uint8_t controllerId,usb_host_handle hostHandle,usb_host_controller_handle * controllerHandle)3402 usb_status_t USB_HostIp3516HsCreate(uint8_t controllerId,
3403                                     usb_host_handle hostHandle,
3404                                     usb_host_controller_handle *controllerHandle)
3405 {
3406     usb_host_ip3516hs_state_struct_t *usbHostState;
3407     uint32_t usb_base_addrs[] = USBHSH_BASE_ADDRS;
3408     IRQn_Type usb_irq[]       = USBHSH_IRQS;
3409     usb_status_t status       = kStatus_USB_Success;
3410 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3411     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3412     uint32_t hsdcd_base[] = USBHSDCD_BASE_ADDRS;
3413     USBHSDCD_Type *base;
3414 #endif
3415 
3416     if (((controllerId - (uint8_t)kUSB_ControllerIp3516Hs0) >= (uint8_t)USB_HOST_CONFIG_IP3516HS)
3417 #if (defined(USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS > 1U))
3418         ||
3419         ((controllerId - (uint8_t)kUSB_ControllerIp3516Hs0) >= (uint8_t)((sizeof(usb_base_addrs) / sizeof(uint32_t))))
3420 #endif
3421     )
3422     {
3423         return kStatus_USB_ControllerNotFound;
3424     }
3425     usbHostState = &s_UsbHostIp3516HsState[controllerId - (uint8_t)kUSB_ControllerIp3516Hs0];
3426     /* Allocate the USB Host Pipe Descriptors */
3427     usbHostState->hostHandle   = hostHandle;
3428     usbHostState->controllerId = controllerId - (uint8_t)kUSB_ControllerIp3516Hs0;
3429 
3430     usbHostState->usbRegBase    = (usb_host_ip3516hs_register_struct_t *)usb_base_addrs[usbHostState->controllerId];
3431     usbHostState->isrNumber     = (uint8_t)usb_irq[usbHostState->controllerId];
3432     usbHostState->ip3516HsEvent = (osa_event_handle_t)&usbHostState->taskEventHandleBuffer[0];
3433     if (KOSA_StatusSuccess != OSA_EventCreate(usbHostState->ip3516HsEvent, 1U))
3434     {
3435 #ifdef HOST_ECHO
3436         usb_echo(" Event create failed in USB_HostIp3516HsCreate\n");
3437 #endif
3438         return kStatus_USB_AllocFail;
3439     } /* Endif */
3440     usbHostState->mutex = (osa_mutex_handle_t)(&usbHostState->mutexBuffer[0]);
3441     if (KOSA_StatusSuccess != OSA_MutexCreate(usbHostState->mutex))
3442     {
3443         (void)USB_HostIp3516HsDestory(usbHostState);
3444 #ifdef HOST_ECHO
3445         usb_echo("USB_HostIp3516HsCreate: create host mutex fail\r\n");
3446 #endif
3447         return kStatus_USB_AllocFail;
3448     }
3449 
3450     usbHostState->portNumber =
3451         (uint8_t)(usbHostState->usbRegBase->HCSPARAMS & USB_HOST_IP3516HS_HCSPARAMS_N_PORTS_MASK);
3452     if (0U == usbHostState->portNumber)
3453     {
3454 #ifdef HOST_ECHO
3455         usb_echo(" Port number is invalid in USB_HostIp3516HsCreate\n");
3456 #endif
3457         (void)USB_HostIp3516HsDestory(usbHostState);
3458         return kStatus_USB_Error;
3459     }
3460 
3461     usbHostState->portState = (usb_host_ip3516hs_port_state_struct_t *)OSA_MemoryAllocate(
3462         ((uint32_t)sizeof(usb_host_ip3516hs_port_state_struct_t)) *
3463         ((uint32_t)usbHostState->portNumber)); /* malloc host ip3516hs port state */
3464     if (NULL == usbHostState->portState)
3465     {
3466 #ifdef HOST_ECHO
3467         usb_echo(" Mem alloc failed in USB_HostIp3516HsCreate\n");
3468 #endif
3469         (void)USB_HostIp3516HsDestory(usbHostState);
3470         return kStatus_USB_Error;
3471     }
3472 
3473     (void)USB_HostIp3516HsControllerReset(usbHostState);
3474 
3475     (void)USB_HostIp3516HsControllerInit(usbHostState);
3476 
3477 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3478     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3479     base = (USBHSDCD_Type *)hsdcd_base[controllerId - kUSB_ControllerIp3516Hs0];
3480     if (kStatus_hsdcd_Success != USB_HSDCD_Init(base, NULL, &usbHostState->dcdHandle))
3481     {
3482         (void)USB_HostIp3516HsDestory(usbHostState);
3483         return kStatus_USB_Error;
3484     }
3485     usbHostState->chargerType = kUSB_DcdSDP;
3486 #endif
3487 
3488     *controllerHandle = (usb_host_handle)usbHostState;
3489     return status;
3490 }
3491 
3492 /*!
3493  * @brief destroy USB host ip3516hs instance.
3494  *
3495  * This function de-initialize the USB host ip3516hs controller driver.
3496  *
3497  * @param handle                                    the controller handle.
3498  *
3499  * @retval kStatus_USB_Success              The host is initialized successfully.
3500  */
USB_HostIp3516HsDestory(usb_host_controller_handle controllerHandle)3501 usb_status_t USB_HostIp3516HsDestory(usb_host_controller_handle controllerHandle)
3502 {
3503     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3504 
3505     usbHostState->usbRegBase->USBINTR = 0U;
3506     usbHostState->usbRegBase->USBSTS  = 0xFFFFFFFFU;
3507 
3508     usbHostState->usbRegBase->USBCMD &= ~USB_HOST_IP3516HS_USBCMD_RS_MASK;
3509 
3510     if (NULL != usbHostState->portState)
3511     {
3512         (void)OSA_MemoryFree(usbHostState->portState);
3513         usbHostState->portState = NULL;
3514     }
3515 
3516     if (NULL != usbHostState->mutex)
3517     {
3518         (void)OSA_MutexDestroy(usbHostState->mutex);
3519         usbHostState->mutex = NULL;
3520     }
3521 
3522     if (NULL != usbHostState->ip3516HsEvent)
3523     {
3524         (void)OSA_EventDestroy(usbHostState->ip3516HsEvent);
3525         usbHostState->ip3516HsEvent = NULL;
3526     }
3527 
3528 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3529     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3530     USB_HSDCD_Deinit(usbHostState->dcdHandle);
3531 #endif
3532 
3533     return kStatus_USB_Success;
3534 }
3535 
USB_HostIp3516HsOpenPipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle * pipeHandle,usb_host_pipe_init_t * pipeInit)3536 usb_status_t USB_HostIp3516HsOpenPipe(usb_host_controller_handle controllerHandle,
3537                                       usb_host_pipe_handle *pipeHandle,
3538                                       usb_host_pipe_init_t *pipeInit)
3539 {
3540     usb_host_ip3516hs_pipe_struct_t *pipe          = NULL;
3541     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3542     usb_status_t status;
3543 
3544     status = USB_HostIp3516HsGetNewPipe(&usbHostState->pipeList, &pipe);
3545     if ((kStatus_USB_Success != status) || (NULL == pipe))
3546     {
3547 #ifdef HOST_ECHO
3548         usb_echo("ip3516hs open pipe failed\r\n");
3549 #endif
3550         return kStatus_USB_Busy;
3551     }
3552 
3553     /* initialize pipe informations */
3554     pipe->pipeCommon.deviceHandle    = pipeInit->devInstance;
3555     pipe->pipeCommon.endpointAddress = pipeInit->endpointAddress;
3556     pipe->pipeCommon.direction       = pipeInit->direction;
3557     pipe->pipeCommon.interval        = pipeInit->interval;
3558     pipe->pipeCommon.maxPacketSize   = pipeInit->maxPacketSize;
3559     pipe->pipeCommon.pipeType        = pipeInit->pipeType;
3560     pipe->pipeCommon.numberPerUframe = (pipeInit->numberPerUframe + 1U) & 0x03U;
3561     pipe->pipeCommon.nakCount        = pipeInit->nakCount;
3562     pipe->pipeCommon.nextdata01      = 0U;
3563     pipe->pipeCommon.currentCount    = 0U;
3564     pipe->pipeCommon.open            = 1U;
3565     pipe->tdIndex                    = 0xFFU;
3566     pipe->cutOffTime                 = USB_HOST_IP3516HS_TRANSFER_TIMEOUT_GAP;
3567     pipe->startUFrame                = 0U;
3568     pipe->csSlot                     = 0U;
3569     pipe->isBusy                     = 0U;
3570     pipe->bufferLength               = 0U;
3571     pipe->currentTr                  = NULL;
3572     pipe->lastPrimedIndex            = 0U;
3573 
3574     if (USB_ENDPOINT_ISOCHRONOUS == pipe->pipeCommon.pipeType)
3575     {
3576 #if 1U
3577         if (0U == pipe->pipeCommon.interval)
3578         {
3579             pipe->pipeCommon.interval = 1U;
3580         }
3581         pipe->pipeCommon.interval =
3582             (uint16_t)(1UL << (pipe->pipeCommon.interval - 1U)); /* iso interval is the power of 2 */
3583 #else
3584         pipe->pipeCommon.interval = 1U;
3585 #endif
3586     }
3587     else if (USB_ENDPOINT_INTERRUPT == pipe->pipeCommon.pipeType)
3588     {
3589         if (USB_SPEED_HIGH != ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed)
3590         {
3591             uint32_t interval = 0U;
3592             /* FS/LS interrupt interval should be the power of 2, it is used for ip3516hs bandwidth */
3593             for (uint32_t p = 0U; p < 16U; p++)
3594             {
3595                 if (0U != (pipe->pipeCommon.interval & ((uint16_t)(1UL << p))))
3596                 {
3597                     if (p > interval)
3598                     {
3599                         interval = p;
3600                     }
3601                 }
3602             }
3603             pipe->pipeCommon.interval = (uint16_t)(1UL << interval);
3604         }
3605         else
3606         {
3607             pipe->pipeCommon.interval = ((uint16_t)(
3608                 1UL << ((uint32_t)pipe->pipeCommon.interval - 1UL))); /* HS interrupt interval is the power of 2 */
3609         }
3610     }
3611     else
3612     {
3613         pipe->pipeCommon.interval = 0U;
3614     }
3615 
3616     if (USB_SPEED_HIGH != ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed)
3617     {
3618         pipe->pipeCommon.interval = pipe->pipeCommon.interval << 3;
3619     }
3620 
3621     if (((uint16_t)USB_HOST_IP3516HS_MAX_FRAME) < pipe->pipeCommon.interval)
3622     {
3623         pipe->pipeCommon.interval = (uint16_t)USB_HOST_IP3516HS_MAX_FRAME;
3624     }
3625     /* open pipe */
3626     switch (pipe->pipeCommon.pipeType)
3627     {
3628         case USB_ENDPOINT_CONTROL:
3629         case USB_ENDPOINT_BULK:
3630             status = USB_HostIp3516HsOpenControlBulkPipe(usbHostState, pipe);
3631             break;
3632 
3633 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3634         case USB_ENDPOINT_ISOCHRONOUS:
3635             status = USB_HostIp3516HsOpenIsoPipe(usbHostState, pipe);
3636             break;
3637 #endif
3638 
3639 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3640         case USB_ENDPOINT_INTERRUPT:
3641             status = USB_HostIp3516HsOpenInterruptPipe(usbHostState, pipe);
3642             break;
3643 #endif
3644         default:
3645             status = kStatus_USB_Error;
3646             break;
3647     }
3648 
3649     if (status != kStatus_USB_Success)
3650     {
3651         pipe->pipeCommon.open = 0U;
3652         /* release the pipe */
3653         (void)USB_HostIp3516HsInsertPipe(&usbHostState->pipeList, pipe);
3654         return status;
3655     }
3656 
3657     (void)USB_HostIp3516HsInsertPipe(&usbHostState->pipeListInUsing, pipe);
3658 
3659     *pipeHandle = pipe;
3660     return status;
3661 }
3662 
USB_HostIp3516HsClosePipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle pipeHandle)3663 usb_status_t USB_HostIp3516HsClosePipe(usb_host_controller_handle controllerHandle, usb_host_pipe_handle pipeHandle)
3664 {
3665     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3666     usb_host_ip3516hs_pipe_struct_t *pipe          = (usb_host_ip3516hs_pipe_struct_t *)pipeHandle;
3667 
3668     switch (pipe->pipeCommon.pipeType)
3669     {
3670         case USB_ENDPOINT_BULK:
3671         case USB_ENDPOINT_CONTROL:
3672             (void)USB_HostIp3516HsCloseControlBulkPipe(usbHostState, pipe);
3673             break;
3674 
3675 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3676         case USB_ENDPOINT_ISOCHRONOUS:
3677             (void)USB_HostIp3516HsCloseIsoPipe(usbHostState, pipe);
3678             break;
3679 #endif
3680 
3681 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3682         case USB_ENDPOINT_INTERRUPT:
3683             (void)USB_HostIp3516HsCloseInterruptPipe(usbHostState, pipe);
3684             break;
3685 #endif
3686 
3687         default:
3688             /*no action*/
3689             break;
3690     }
3691     (void)USB_HostIp3516HsRemovePipe(&usbHostState->pipeListInUsing, pipe);
3692     pipe->pipeCommon.open = 0U;
3693     (void)USB_HostIp3516HsInsertPipe(&usbHostState->pipeList, pipe);
3694 
3695     return kStatus_USB_Success;
3696 }
3697 
USB_HostIp3516HsWritePipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle pipeHandle,usb_host_transfer_t * transfer)3698 usb_status_t USB_HostIp3516HsWritePipe(usb_host_controller_handle controllerHandle,
3699                                        usb_host_pipe_handle pipeHandle,
3700                                        usb_host_transfer_t *transfer)
3701 {
3702     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3703     usb_host_ip3516hs_pipe_struct_t *pipe          = (usb_host_ip3516hs_pipe_struct_t *)pipeHandle;
3704     usb_host_transfer_t *trPre;
3705     usb_status_t status = kStatus_USB_Success;
3706     uint8_t trListEmpty = 0U;
3707     OSA_SR_ALLOC();
3708 
3709     transfer->setupStatus           = (uint8_t)kStatus_UsbHostIp3516Hs_Idle;
3710     transfer->next                  = NULL;
3711     transfer->transferSofar         = 0U;
3712     transfer->union1.transferResult = (int32_t)kStatus_USB_Success;
3713 
3714     OSA_ENTER_CRITICAL();
3715     if (NULL == pipe->trList)
3716     {
3717         pipe->trList    = transfer;
3718         pipe->currentTr = transfer;
3719         trListEmpty     = 1U;
3720     }
3721     else
3722     {
3723         trPre = pipe->trList;
3724         while (NULL != trPre->next)
3725         {
3726             trPre = trPre->next;
3727         }
3728         trPre->next = transfer;
3729     }
3730     OSA_EXIT_CRITICAL();
3731 
3732 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3733     if ((0U != trListEmpty) || (USB_ENDPOINT_ISOCHRONOUS == pipe->pipeCommon.pipeType))
3734 #else
3735     if ((0U != trListEmpty))
3736 #endif
3737     {
3738         switch (pipe->pipeCommon.pipeType)
3739         {
3740             case USB_ENDPOINT_BULK:
3741                 status = USB_HostIp3516HsWriteBulkPipe(usbHostState, pipe, transfer);
3742                 break;
3743 
3744             case USB_ENDPOINT_CONTROL:
3745                 status = USB_HostIp3516HsWriteControlPipe(usbHostState, pipe, transfer);
3746                 break;
3747 
3748 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3749             case USB_ENDPOINT_ISOCHRONOUS:
3750                 status = USB_HostIp3516HsWriteIsoPipe(usbHostState, pipe, pipe->trList);
3751                 break;
3752 #endif
3753 
3754 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3755             case USB_ENDPOINT_INTERRUPT:
3756                 status = USB_HostIp3516HsWriteInterruptPipe(usbHostState, pipe, transfer);
3757                 break;
3758 #endif
3759 
3760             default:
3761                 /*no action*/
3762                 break;
3763         }
3764     }
3765     return status;
3766 }
3767 
USB_HostIp3516HsReadPipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle pipeHandle,usb_host_transfer_t * transfer)3768 usb_status_t USB_HostIp3516HsReadPipe(usb_host_controller_handle controllerHandle,
3769                                       usb_host_pipe_handle pipeHandle,
3770                                       usb_host_transfer_t *transfer)
3771 {
3772     return USB_HostIp3516HsWritePipe(controllerHandle, pipeHandle, transfer);
3773 }
3774 
USB_HostIp3516HsIoctl(usb_host_controller_handle controllerHandle,uint32_t ioctlEvent,void * ioctlParam)3775 usb_status_t USB_HostIp3516HsIoctl(usb_host_controller_handle controllerHandle, uint32_t ioctlEvent, void *ioctlParam)
3776 {
3777     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3778     usb_host_cancel_param_t *param;
3779     usb_host_ip3516hs_pipe_struct_t *pipe;
3780     uint32_t deviceAddress;
3781     usb_host_controller_control_t ioctl;
3782 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
3783 #if ((defined(USB_HOST_CONFIG_LPM_L1)) && (USB_HOST_CONFIG_LPM_L1 > 0U))
3784     uint8_t *lpmParam;
3785 #endif
3786 #endif
3787     deviceAddress       = 0U;
3788     usb_status_t status = kStatus_USB_Success;
3789 
3790     if (controllerHandle == NULL)
3791     {
3792         return kStatus_USB_InvalidHandle;
3793     }
3794     ioctl = (usb_host_controller_control_t)ioctlEvent;
3795     switch (ioctl)
3796     {
3797         case kUSB_HostCancelTransfer:
3798             /* cancel pipe or one transfer */
3799             param  = (usb_host_cancel_param_t *)ioctlParam;
3800             status = USB_HostIp3516HsCancelPipe(usbHostState, (usb_host_ip3516hs_pipe_struct_t *)param->pipeHandle,
3801                                                 param->transfer);
3802             break;
3803 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
3804 #if ((defined(USB_HOST_CONFIG_LPM_L1)) && (USB_HOST_CONFIG_LPM_L1 > 0U))
3805         case kUSB_HostL1Config:
3806             lpmParam                           = (uint8_t *)ioctlParam;
3807             usbHostState->hirdValue            = (*lpmParam) & 0xFU;
3808             usbHostState->L1remoteWakeupEnable = (((*lpmParam) & 0x80U) >> 7);
3809             break;
3810 #endif
3811 #endif
3812         case kUSB_HostBusControl:
3813             /* bus control */
3814             status = USB_HostIp3516HsControlBus(usbHostState, *((uint8_t *)ioctlParam));
3815             break;
3816 
3817         case kUSB_HostGetFrameNumber:
3818             /* get frame number */
3819             *((uint32_t *)ioctlParam) =
3820                 (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
3821                            USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
3822             break;
3823 
3824         case kUSB_HostUpdateControlEndpointAddress:
3825             pipe = (usb_host_ip3516hs_pipe_struct_t *)ioctlParam;
3826             /* update address */
3827             (void)USB_HostHelperGetPeripheralInformation(pipe->pipeCommon.deviceHandle,
3828                                                          (uint32_t)kUSB_HostGetDeviceAddress, &deviceAddress);
3829             switch (pipe->pipeCommon.pipeType)
3830             {
3831                 case USB_ENDPOINT_BULK:
3832                 case USB_ENDPOINT_CONTROL:
3833                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3834                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3835                         .atl[pipe->tdIndex]
3836                         .control2Union.stateBitField.DeviceAddress = deviceAddress;
3837                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3838                     break;
3839 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3840                 case USB_ENDPOINT_INTERRUPT:
3841                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3842                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3843                         .interrupt[pipe->tdIndex]
3844                         .control2Union.stateBitField.DeviceAddress = deviceAddress;
3845                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3846                     break;
3847 #endif
3848 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3849                 case USB_ENDPOINT_ISOCHRONOUS:
3850                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3851                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3852                         .iso[pipe->tdIndex]
3853                         .control2Union.stateBitField.DeviceAddress = deviceAddress;
3854                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3855                     break;
3856 #endif
3857                 default:
3858                     /*no action*/
3859                     break;
3860             }
3861             (void)USB_HostIp3516HsDelay(usbHostState, 16U);
3862             break;
3863 
3864         case kUSB_HostUpdateControlPacketSize:
3865             pipe = (usb_host_ip3516hs_pipe_struct_t *)ioctlParam;
3866             switch (pipe->pipeCommon.pipeType)
3867             {
3868                 case USB_ENDPOINT_BULK:
3869                 case USB_ENDPOINT_CONTROL:
3870                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3871                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3872                         .atl[pipe->tdIndex]
3873                         .control1Union.stateBitField.MaxPacketLength = pipe->pipeCommon.maxPacketSize;
3874                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3875                     break;
3876 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3877                 case USB_ENDPOINT_INTERRUPT:
3878                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3879                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3880                         .interrupt[pipe->tdIndex]
3881                         .control1Union.stateBitField.MaxPacketLength = pipe->pipeCommon.maxPacketSize;
3882                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3883                     break;
3884 #endif
3885 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3886                 case USB_ENDPOINT_ISOCHRONOUS:
3887                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3888                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3889                         .iso[pipe->tdIndex]
3890                         .control1Union.stateBitField.MaxPacketLength = pipe->pipeCommon.maxPacketSize;
3891                     if (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3892                                   .iso[pipe->tdIndex]
3893                                   .control2Union.stateBitField.S)
3894                     {
3895                         s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3896                             .iso[pipe->tdIndex]
3897                             .control1Union.stateBitField.MaxPacketLength++;
3898                     }
3899                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3900                     break;
3901 #endif
3902                 default:
3903                     /*no action*/
3904                     break;
3905             }
3906             break;
3907 
3908         case kUSB_HostPortAttachDisable:
3909             break;
3910 
3911         case kUSB_HostPortAttachEnable:
3912             if ((*(uint8_t *)ioctlParam) < usbHostState->portNumber)
3913             {
3914                 usbHostState->portState[*(uint8_t *)ioctlParam].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortDetached;
3915             }
3916             break;
3917 
3918 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3919     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3920         case kUSB_HostSetChargerType:
3921             usbHostState->chargerType = *((uint8_t *)ioctlParam);
3922             if ((usbHostState->portState[0].portStatus == kUSB_DeviceIp3516HsPortPhyDetached) ||
3923                 (usbHostState->portState[0].portStatus == kUSB_DeviceIp3516HsPortDetached))
3924             {
3925                 USB_HSDCD_Control(usbHostState->dcdHandle, kUSB_HostHSDcdSetType, &(usbHostState->chargerType));
3926             }
3927             break;
3928 #endif
3929 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
3930         case kUSB_HostTestModeInit: /* test mode control */
3931             USB_HostIp3516HsTestModeInit((usb_host_device_instance_t *)ioctlParam);
3932             break;
3933 #endif
3934         default:
3935             status = kStatus_USB_NotSupported;
3936             break;
3937     }
3938     return status;
3939 }
3940 
USB_HostIp3516HsTaskFunction(void * hostHandle)3941 void USB_HostIp3516HsTaskFunction(void *hostHandle)
3942 {
3943     usb_host_ip3516hs_state_struct_t *usbHostState;
3944     uint32_t bitSet;
3945 
3946     if (NULL == hostHandle)
3947     {
3948         return;
3949     }
3950     usbHostState = (usb_host_ip3516hs_state_struct_t *)((usb_host_instance_t *)hostHandle)->controllerHandle;
3951 
3952     /* wait all event */
3953     if (KOSA_StatusSuccess == OSA_EventWait(usbHostState->ip3516HsEvent, 0xFFU, 0, USB_OSA_WAIT_TIMEOUT, &bitSet))
3954     {
3955         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_PORT_CHANGE))
3956         {
3957             (void)USB_HostIp3516HsPortChange(usbHostState);
3958         }
3959         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_ISO_TOKEN_DONE))
3960         {
3961             (void)USB_HostIp3516HsTokenDone(usbHostState);
3962         }
3963         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_INT_TOKEN_DONE))
3964         {
3965             (void)USB_HostIp3516HsTokenDone(usbHostState);
3966         }
3967         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_ATL_TOKEN_DONE))
3968         {
3969             (void)USB_HostIp3516HsTokenDone(usbHostState);
3970         }
3971         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_SOF))
3972         {
3973             (void)USB_HostIp3516HsSof(usbHostState);
3974         }
3975         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_ATTACH))
3976         {
3977             for (uint8_t i = 0; i < usbHostState->portNumber; i++)
3978             {
3979                 if ((uint8_t)kUSB_DeviceIp3516HsPortPhyAttached == usbHostState->portState[i].portStatus)
3980                 {
3981                     void *deviceHandle;
3982                     if (kStatus_USB_Success == USB_HostAttachDevice(usbHostState->hostHandle,
3983                                                                     usbHostState->portState[i].portSpeed, 0U, i, 1U,
3984                                                                     &deviceHandle))
3985                     {
3986                         usbHostState->portState[i].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortAttached;
3987                     }
3988                 }
3989             }
3990         }
3991         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_DETACH))
3992         {
3993             for (uint8_t i = 0; i < usbHostState->portNumber; i++)
3994             {
3995                 if ((uint8_t)kUSB_DeviceIp3516HsPortPhyDetached == usbHostState->portState[i].portStatus)
3996                 {
3997                     (void)USB_HostDetachDevice(usbHostState->hostHandle, 0U, i);
3998                 }
3999             }
4000         }
4001     }
4002 }
4003 
USB_HostIp3516HsIsrFunction(void * hostHandle)4004 void USB_HostIp3516HsIsrFunction(void *hostHandle)
4005 {
4006     usb_host_ip3516hs_state_struct_t *usbHostState;
4007     static uint32_t interruptStatus = 0U;
4008     static uint32_t sofCount        = 0U;
4009 
4010     if (hostHandle == NULL)
4011     {
4012         return;
4013     }
4014 
4015     usbHostState = (usb_host_ip3516hs_state_struct_t *)((usb_host_instance_t *)hostHandle)->controllerHandle;
4016 
4017     interruptStatus = usbHostState->usbRegBase->USBSTS;
4018     interruptStatus &= usbHostState->usbRegBase->USBINTR;
4019     usbHostState->usbRegBase->USBSTS = interruptStatus; /* clear interrupt */
4020 
4021     if (0U != (interruptStatus & USB_HOST_IP3516HS_USBSTS_ISO_IRQ_MASK)) /* Write back done head */
4022     {
4023 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
4024         (void)USB_HostIp3516HsCheckIsoTransferSofar(usbHostState);
4025         (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_ISO_TOKEN_DONE);
4026 #endif
4027     }
4028 
4029     if (0U != (interruptStatus & USB_HOST_IP3516HS_USBSTS_INT_IRQ_MASK)) /* Write back done head */
4030     {
4031 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
4032         (void)USB_HostIp3516HsCheckIntTransferSofar(usbHostState);
4033         (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_INT_TOKEN_DONE);
4034 #endif
4035     }
4036 
4037     if (0U != (interruptStatus & USB_HOST_IP3516HS_USBSTS_ATL_IRQ_MASK)) /* Write back done head */
4038     {
4039         (void)USB_HostIp3516HsCheckAtlTransferSofar(usbHostState);
4040         (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_ATL_TOKEN_DONE);
4041     }
4042 
4043     if (0U != (interruptStatus & USB_HOST_IP3516HS_USBSTS_SOF_IRQ_MASK)) /* SOF interrupt */
4044     {
4045         if (USB_SPEED_HIGH == usbHostState->portState->portSpeed)
4046         {
4047             sofCount++;
4048         }
4049         else
4050         {
4051             sofCount += 8U;
4052         }
4053         if (sofCount >= USB_HOST_IP3516HS_TRANSFER_SCAN_INTERVAL)
4054         {
4055             sofCount = 0U;
4056             (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_SOF);
4057         }
4058     }
4059 
4060     if (0U != (interruptStatus & USB_HOST_IP3516HS_USBINTR_PCDE_MASK)) /* port change detect interrupt */
4061     {
4062         (void)OSA_EventSet(usbHostState->ip3516HsEvent, USB_HOST_IP3516HS_EVENT_PORT_CHANGE);
4063     }
4064 }
4065 
4066 #endif
4067