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->transferLength % 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                     trCurrent->union2.frame = 0u;
3020                     if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3021                                    .iso[indexLength.state.tdIndex]
3022                                    .stateUnion.stateBitField.A) &&
3023                         (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3024                                    .iso[indexLength.state.tdIndex]
3025                                    .control1Union.stateBitField.V))
3026                     {
3027                         temp = (void *)pipe->pipeCommon.next;
3028                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3029                         continue;
3030                     }
3031 
3032                     pipe->bufferIndex  = indexLength.state.bufferIndex;
3033                     pipe->bufferLength = indexLength.state.bufferLength;
3034                     if (USB_IN == trCurrent->direction)
3035                     {
3036                         (void)memcpy((void *)(&trCurrent->transferBuffer[trCurrent->transferSofar]),
3037                                      (void *)(&s_UsbHostIp3516HsBufferArray[pipe->bufferIndex][0]),
3038                                      s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3039                                          .iso[indexLength.state.tdIndex]
3040                                          .stateUnion.stateBitField.NrBytesToTransfer);
3041                     }
3042                     trCurrent->transferSofar += s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3043                                                     .iso[indexLength.state.tdIndex]
3044                                                     .stateUnion.stateBitField.NrBytesToTransfer;
3045 
3046                     if ((0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3047                                    .iso[indexLength.state.tdIndex]
3048                                    .stateUnion.stateBitField.B) ||
3049                         (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3050                                    .iso[indexLength.state.tdIndex]
3051                                    .stateUnion.stateBitField.X))
3052                     {
3053                         trCurrent->union1.transferResult = (int32_t)kStatus_USB_TransferFailed;
3054                     }
3055                     else
3056                     {
3057                         trCurrent->union1.transferResult = (int32_t)kStatus_USB_Success;
3058                     }
3059 
3060                     if (NULL != pipe->trList)
3061                     {
3062                         pipe->currentTr = pipe->trList->next;
3063                     }
3064                     else
3065                     {
3066                         pipe->currentTr = NULL;
3067                     }
3068 
3069                     if (NULL != pipe->currentTr)
3070                     {
3071                         uint32_t trValue = (uint32_t)pipe->currentTr;
3072                         (void)USB_HostIp3516HsWriteIsoPipe(usbHostState, pipe, (usb_host_transfer_t *)trValue);
3073                     }
3074 #else
3075                     if (pipe->trList == pipe->currentTr)
3076                     {
3077                         temp = (void *)pipe->pipeCommon.next;
3078                         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3079                         continue;
3080                     }
3081 #endif
3082                     trDone = 1U;
3083 
3084                     trStatus = (usb_status_t)trCurrent->union1.transferResult;
3085                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3086                         .iso[indexLength.state.tdIndex]
3087                         .dataUnion.dataBitField.NrBytesToTransfer = 0U;
3088 
3089                     break;
3090 #endif
3091                 default:
3092                     /*no action*/
3093                     break;
3094             }
3095             if (0U != trDone)
3096             {
3097                 pipe->trList = trCurrent->next;
3098 #if 0
3099 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3100                 if ((USB_ENDPOINT_ISOCHRONOUS != pipe->pipeCommon.pipeType) && (pipe->bufferLength))
3101 #else
3102                 if ((pipe->bufferLength)
3103 #endif
3104 
3105 #else
3106                 if ((0U != pipe->bufferLength))
3107 #endif
3108                 {
3109                     (void)USB_HostIp3516HsFreeBuffer(usbHostState, pipe->bufferIndex, pipe->bufferLength);
3110                     pipe->bufferLength = 0U;
3111                 }
3112                 pipe->cutOffTime = USB_HOST_IP3516HS_TRANSFER_TIMEOUT_GAP;
3113 
3114                 if ((kStatus_USB_Success == (trStatus)) && (USB_ENDPOINT_CONTROL == pipe->pipeCommon.pipeType) &&
3115                     (USB_REQUEST_STANDARD_CLEAR_FEATURE == trCurrent->setupPacket->bRequest) &&
3116                     (USB_REQUEST_TYPE_RECIPIENT_ENDPOINT == trCurrent->setupPacket->bmRequestType) &&
3117                     (USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT ==
3118                      (USB_SHORT_FROM_LITTLE_ENDIAN(trCurrent->setupPacket->wValue) & 0x00FFU)))
3119                 {
3120                     p = usbHostState->pipeListInUsing;
3121                     while (p != NULL)
3122                     {
3123                         /* only compute bulk and interrupt pipe */
3124                         if ((p->pipeCommon.deviceHandle == pipe->pipeCommon.deviceHandle) &&
3125                             ((p->pipeCommon.endpointAddress |
3126                               (p->pipeCommon.direction << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) ==
3127                              ((uint8_t)(USB_SHORT_FROM_LITTLE_ENDIAN(trCurrent->setupPacket->wIndex)))))
3128                         {
3129                             break;
3130                         }
3131                         temp = (void *)p->pipeCommon.next;
3132                         p    = (usb_host_ip3516hs_pipe_struct_t *)temp;
3133                     }
3134 
3135                     if ((NULL != p) && ((USB_ENDPOINT_BULK == p->pipeCommon.pipeType) ||
3136                                         (USB_ENDPOINT_INTERRUPT == p->pipeCommon.pipeType)))
3137                     {
3138                         if (USB_ENDPOINT_BULK == p->pipeCommon.pipeType)
3139                         {
3140                             usbHostState->usbRegBase->ATL_PTD_SKIP_MAP |= 1UL << p->tdIndex;
3141                         }
3142 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3143                         else if (USB_ENDPOINT_INTERRUPT == p->pipeCommon.pipeType)
3144                         {
3145                             usbHostState->usbRegBase->INT_PTD_SKIP_MAP |= 1UL << p->tdIndex;
3146                         }
3147 #endif
3148                         else
3149                         {
3150                         }
3151 
3152                         startUFrame = (uint32_t)(
3153                             (usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
3154                             USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
3155                         currentUFrame = startUFrame;
3156 
3157                         while (currentUFrame == startUFrame)
3158                         {
3159                             currentUFrame = (uint32_t)(
3160                                 (usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
3161                                 USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
3162                         }
3163 
3164                         if (USB_ENDPOINT_BULK == p->pipeCommon.pipeType)
3165                         {
3166                             s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3167                                 .atl[p->tdIndex]
3168                                 .stateUnion.stateBitField.DT = 0U;
3169                             usbHostState->usbRegBase->ATL_PTD_SKIP_MAP &= ~(1UL << p->tdIndex);
3170                         }
3171 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3172                         else if (USB_ENDPOINT_INTERRUPT == p->pipeCommon.pipeType)
3173                         {
3174                             s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3175                                 .interrupt[p->tdIndex]
3176                                 .stateUnion.stateBitField.DT = 0U;
3177                             usbHostState->usbRegBase->INT_PTD_SKIP_MAP &= ~(1UL << p->tdIndex);
3178                         }
3179 #endif
3180                         else
3181                         {
3182                         }
3183                     }
3184                 }
3185                 /* callback function is different from the current condition */
3186                 trCurrent->callbackFn(trCurrent->callbackParam, trCurrent, trStatus); /* transfer callback */
3187                 USB_HostIp3516HsCheckGetBufferFailedPipe(usbHostState);
3188                 if (NULL != pipe->trList)
3189                 {
3190                     switch (pipe->pipeCommon.pipeType)
3191                     {
3192                         case USB_ENDPOINT_BULK:
3193                             (void)USB_HostIp3516HsWriteBulkPipe(usbHostState, pipe, pipe->trList);
3194                             break;
3195 
3196                         case USB_ENDPOINT_CONTROL:
3197                             (void)USB_HostIp3516HsWriteControlPipe(usbHostState, pipe, pipe->trList);
3198                             break;
3199 
3200 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3201                         case USB_ENDPOINT_ISOCHRONOUS:
3202                             (void)USB_HostIp3516HsWriteIsoPipe(usbHostState, pipe, pipe->trList);
3203                             break;
3204 #endif
3205 
3206 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3207                         case USB_ENDPOINT_INTERRUPT:
3208                             (void)USB_HostIp3516HsWriteInterruptPipe(usbHostState, pipe, pipe->trList);
3209                             break;
3210 #endif
3211 
3212                         default:
3213                             /*no action*/
3214                             break;
3215                     }
3216                 }
3217             }
3218         }
3219         temp = (void *)pipe->pipeCommon.next;
3220         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3221     }
3222     USB_HostOhciEnableIsr(usbHostState);
3223     USB_HostIp3516HsUnlock();
3224     return kStatus_USB_Success;
3225 }
3226 
USB_HostIp3516HsSof(usb_host_ip3516hs_state_struct_t * usbHostState)3227 static usb_status_t USB_HostIp3516HsSof(usb_host_ip3516hs_state_struct_t *usbHostState)
3228 {
3229     usb_host_ip3516hs_pipe_struct_t *pipe;
3230     void *temp;
3231     /* Enter critical */
3232     USB_HostIp3516HsLock();
3233     pipe = usbHostState->pipeListInUsing;
3234     while (NULL != pipe)
3235     {
3236         if ((USB_ENDPOINT_CONTROL == pipe->pipeCommon.pipeType) || (USB_ENDPOINT_BULK == pipe->pipeCommon.pipeType))
3237         {
3238             if (NULL != pipe->trList)
3239             {
3240                 pipe->cutOffTime--;
3241                 if (0U == pipe->cutOffTime)
3242                 {
3243                     pipe->trList->union1.transferResult = (int)kStatus_USB_TransferFailed;
3244                     (void)USB_HostIp3516HsCancelPipe(usbHostState, pipe, pipe->trList);
3245                 }
3246             }
3247         }
3248         temp = (void *)pipe->pipeCommon.next;
3249         pipe = (usb_host_ip3516hs_pipe_struct_t *)temp;
3250     }
3251     /* Exit critical */
3252     USB_HostIp3516HsUnlock();
3253 
3254     return kStatus_USB_Success;
3255 }
3256 
USB_HostIp3516HsControllerReset(usb_host_ip3516hs_state_struct_t * usbHostState)3257 static usb_status_t USB_HostIp3516HsControllerReset(usb_host_ip3516hs_state_struct_t *usbHostState)
3258 {
3259     usbHostState->usbRegBase->USBCMD = USB_HOST_IP3516HS_USBCMD_HCRESET_MASK;
3260     while (0U != (usbHostState->usbRegBase->USBCMD & USB_HOST_IP3516HS_USBCMD_HCRESET_MASK))
3261     {
3262         __NOP();
3263     }
3264     return kStatus_USB_Success;
3265 }
3266 
USB_HostIp3516HsControllerInit(usb_host_ip3516hs_state_struct_t * usbHostState)3267 static usb_status_t USB_HostIp3516HsControllerInit(usb_host_ip3516hs_state_struct_t *usbHostState)
3268 {
3269     usb_host_ip3516hs_pipe_struct_t *pipe;
3270 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_GTD) && (USB_HOST_CONFIG_IP3516HS_MAX_GTD > 0U))
3271     usb_host_ip3516hs_atl_struct_t *gtd;
3272 #endif
3273 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ITD) && (USB_HOST_CONFIG_IP3516HS_MAX_ITD > 0U))
3274     usb_host_ip3516hs_isochronous_transfer_descritpor_struct_t *itd;
3275 #endif
3276     uint32_t i;
3277     uint32_t interruptState;
3278     void *temp;
3279     OSA_SR_ALLOC();
3280 
3281     /* Enter critical */
3282     OSA_ENTER_CRITICAL();
3283 
3284     for (i = 0U; i < (sizeof(usbHostState->bufferArrayBitMap) / (sizeof(uint32_t))); i++)
3285     {
3286         usbHostState->bufferArrayBitMap[i] = 0xFFFFFFFFU;
3287     }
3288 
3289     for (i = 0U; i < (sizeof(s_UsbHostIp3516HsBufferArray) / (sizeof(uint8_t) * 64U)); i++)
3290     {
3291         usbHostState->bufferArrayBitMap[i >> 5U] &= ~(1UL << (i % 32U));
3292     }
3293 
3294     for (i = 0U;
3295          i < (sizeof(s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl) / sizeof(usb_host_ip3516hs_atl_struct_t));
3296          i++)
3297     {
3298         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].control1Union.controlState = 0U;
3299         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].control2Union.controlState = 0U;
3300         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].dataUnion.data             = 0U;
3301         s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[i].stateUnion.state           = 0U;
3302     }
3303     usbHostState->usbRegBase->ATL_PTD_BASE_ADDRESS =
3304         (((uint32_t)(&s_UsbHostIp3516HsPtd[usbHostState->controllerId].atl[0])) &
3305          USB_HOST_IP3516HS_ATL_PTD_ATL_BASE_MASK);
3306     usbHostState->usbRegBase->LAST_PTD_IN_USE = (i - 1U) & USB_HOST_IP3516HS_LAST_PTD_IN_USE_ATL_LAST_MASK;
3307     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_ATL_EN_MASK;
3308     interruptState = USB_HOST_IP3516HS_USBINTR_ATL_IRQ_E_MASK;
3309 
3310 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3311     for (i = 0U; i < USB_HOST_CONFIG_IP3516HS_MAX_ISO; i++)
3312     {
3313         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].control1Union.controlState = 0U;
3314         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].control2Union.controlState = 0U;
3315         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].dataUnion.data             = 0U;
3316         s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[i].stateUnion.state           = 0U;
3317     }
3318     usbHostState->usbRegBase->ISO_PTD_BASE_ADDRESS =
3319         (((uint32_t)(&s_UsbHostIp3516HsPtd[usbHostState->controllerId].iso[0])) &
3320          USB_HOST_IP3516HS_ISO_PTD_ISO_BASE_MASK);
3321     usbHostState->usbRegBase->LAST_PTD_IN_USE |=
3322         ((USB_HOST_CONFIG_IP3516HS_MAX_ISO - 1UL) << USB_HOST_IP3516HS_LAST_PTD_IN_USE_ISO_LAST_SHIFT) &
3323         USB_HOST_IP3516HS_LAST_PTD_IN_USE_ISO_LAST_MASK;
3324     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_ISO_EN_MASK;
3325     interruptState |= USB_HOST_IP3516HS_USBINTR_ISO_IRQ_E_MASK;
3326 #endif
3327 
3328 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3329     for (i = 0U; i < USB_HOST_CONFIG_IP3516HS_MAX_INT; i++)
3330     {
3331         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].control1Union.controlState = 0U;
3332         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].control2Union.controlState = 0U;
3333         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].dataUnion.data             = 0U;
3334         s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[i].stateUnion.state           = 0U;
3335     }
3336     usbHostState->usbRegBase->INT_PTD_BASE_ADDRESS =
3337         (((uint32_t)(&s_UsbHostIp3516HsPtd[usbHostState->controllerId].interrupt[0])) &
3338          USB_HOST_IP3516HS_INT_PTD_INT_BASE_MASK);
3339     usbHostState->usbRegBase->LAST_PTD_IN_USE |=
3340         (((uint32_t)USB_HOST_CONFIG_IP3516HS_MAX_INT - 1U) << USB_HOST_IP3516HS_LAST_PTD_IN_USE_INT_LAST_SHIFT) &
3341         USB_HOST_IP3516HS_LAST_PTD_IN_USE_INT_LAST_MASK;
3342     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_INT_EN_MASK;
3343     interruptState |= USB_HOST_IP3516HS_USBINTR_INT_IRQ_E_MASK;
3344 #endif
3345 
3346     usbHostState->pipeListInUsing = NULL;
3347     usbHostState->pipeList        = &usbHostState->pipePool[0];
3348     pipe                          = usbHostState->pipeList;
3349     for (i = 1U; i < USB_HOST_CONFIG_IP3516HS_MAX_PIPE; i++)
3350     {
3351         temp                  = (void *)(&usbHostState->pipePool[i]);
3352         pipe->pipeCommon.next = (usb_host_pipe_t *)temp;
3353         temp                  = (void *)pipe->pipeCommon.next;
3354         pipe                  = (usb_host_ip3516hs_pipe_struct_t *)temp;
3355     }
3356     pipe->pipeCommon.next = NULL;
3357 
3358     /* Exit critical */
3359     OSA_EXIT_CRITICAL();
3360 
3361     if (0U != (usbHostState->usbRegBase->HCSPARAMS &
3362                USB_HOST_IP3516HS_HCSPARAMS_PPC_MASK)) /* Ports have power port switches */
3363     {
3364         /* only has one port */
3365         uint32_t tmp = usbHostState->usbRegBase->PORTSC1;
3366         tmp &= (~USB_HOST_IP3516HS_PORTSC1_WIC);
3367         usbHostState->usbRegBase->PORTSC1 = (tmp | USB_HOST_IP3516HS_PORTSC1_PP_MASK); /* turn on port power */
3368     }
3369     usbHostState->usbRegBase->DATA_PAYLOAD_BASE_ADDRESS =
3370         (uint32_t)(&s_UsbHostIp3516HsBufferArray[0][0]) & USB_HOST_IP3516HS_DAT_BASE_MASK;
3371 
3372     usbHostState->usbRegBase->USBSTS = 0xFFFFFFFFU;
3373 
3374     usbHostState->usbRegBase->USBINTR =
3375         interruptState | USB_HOST_IP3516HS_USBINTR_PCDE_MASK | USB_HOST_IP3516HS_USBINTR_SOF_E_MASK;
3376 
3377 /* On RT600, there is no attach information when LS device is connected, the designer said it is due to the default
3378  * turnaround time is too small, the 0x654 is their suggested value. */
3379 #if (defined(FSL_FEATURE_USBHSH_HAS_TURNAROUND_TIMEOUT) && (FSL_FEATURE_USBHSH_HAS_TURNAROUND_TIMEOUT > 0U))
3380     usbHostState->usbRegBase->PACKET_TURNAROUND_TIMEOUT &= ~USBHSH_PACKET_TURNAROUND_TIME_HOST_TIMEOUT_LS_MASK;
3381     usbHostState->usbRegBase->PACKET_TURNAROUND_TIMEOUT |= 0x00000654;
3382 #endif
3383 
3384     usbHostState->usbRegBase->USBCMD |= USB_HOST_IP3516HS_USBCMD_RS_MASK;
3385 
3386     return kStatus_USB_Success;
3387 }
3388 
3389 /*!
3390  * @brief create the USB host ip3516hs instance.
3391  *
3392  * This function initializes the USB host ip3516hs controller driver.
3393  *
3394  * @param controllerId        The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
3395  * @param hostHandle         The host level handle.
3396  * @param controllerHandle  Return the controller instance handle.
3397  *
3398  * @retval kStatus_USB_Success              The host is initialized successfully.
3399  * @retval kStatus_USB_AllocFail             allocate memory fail.
3400  * @retval kStatus_USB_Error                 host mutex create fail, IP3516HS mutex or IP3516HS event create fail.
3401  *                                                         Or, IP3516HS IP initialize fail.
3402  */
USB_HostIp3516HsCreate(uint8_t controllerId,usb_host_handle hostHandle,usb_host_controller_handle * controllerHandle)3403 usb_status_t USB_HostIp3516HsCreate(uint8_t controllerId,
3404                                     usb_host_handle hostHandle,
3405                                     usb_host_controller_handle *controllerHandle)
3406 {
3407     usb_host_ip3516hs_state_struct_t *usbHostState;
3408     uint32_t usb_base_addrs[] = USBHSH_BASE_ADDRS;
3409     IRQn_Type usb_irq[]       = USBHSH_IRQS;
3410     usb_status_t status       = kStatus_USB_Success;
3411 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3412     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3413     uint32_t hsdcd_base[] = USBHSDCD_BASE_ADDRS;
3414     USBHSDCD_Type *base;
3415 #endif
3416 
3417     if (((controllerId - (uint8_t)kUSB_ControllerIp3516Hs0) >= (uint8_t)USB_HOST_CONFIG_IP3516HS)
3418 #if (defined(USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS > 1U))
3419         ||
3420         ((controllerId - (uint8_t)kUSB_ControllerIp3516Hs0) >= (uint8_t)((sizeof(usb_base_addrs) / sizeof(uint32_t))))
3421 #endif
3422     )
3423     {
3424         return kStatus_USB_ControllerNotFound;
3425     }
3426     usbHostState = &s_UsbHostIp3516HsState[controllerId - (uint8_t)kUSB_ControllerIp3516Hs0];
3427     /* Allocate the USB Host Pipe Descriptors */
3428     usbHostState->hostHandle   = hostHandle;
3429     usbHostState->controllerId = controllerId - (uint8_t)kUSB_ControllerIp3516Hs0;
3430 
3431     usbHostState->usbRegBase    = (usb_host_ip3516hs_register_struct_t *)usb_base_addrs[usbHostState->controllerId];
3432     usbHostState->isrNumber     = (uint8_t)usb_irq[usbHostState->controllerId];
3433     usbHostState->ip3516HsEvent = (osa_event_handle_t)&usbHostState->taskEventHandleBuffer[0];
3434     if (KOSA_StatusSuccess != OSA_EventCreate(usbHostState->ip3516HsEvent, 1U))
3435     {
3436 #ifdef HOST_ECHO
3437         usb_echo(" Event create failed in USB_HostIp3516HsCreate\n");
3438 #endif
3439         return kStatus_USB_AllocFail;
3440     } /* Endif */
3441     usbHostState->mutex = (osa_mutex_handle_t)(&usbHostState->mutexBuffer[0]);
3442     if (KOSA_StatusSuccess != OSA_MutexCreate(usbHostState->mutex))
3443     {
3444         (void)USB_HostIp3516HsDestory(usbHostState);
3445 #ifdef HOST_ECHO
3446         usb_echo("USB_HostIp3516HsCreate: create host mutex fail\r\n");
3447 #endif
3448         return kStatus_USB_AllocFail;
3449     }
3450 
3451     usbHostState->portNumber =
3452         (uint8_t)(usbHostState->usbRegBase->HCSPARAMS & USB_HOST_IP3516HS_HCSPARAMS_N_PORTS_MASK);
3453     if (0U == usbHostState->portNumber)
3454     {
3455 #ifdef HOST_ECHO
3456         usb_echo(" Port number is invalid in USB_HostIp3516HsCreate\n");
3457 #endif
3458         (void)USB_HostIp3516HsDestory(usbHostState);
3459         return kStatus_USB_Error;
3460     }
3461 
3462     usbHostState->portState = (usb_host_ip3516hs_port_state_struct_t *)OSA_MemoryAllocate(
3463         ((uint32_t)sizeof(usb_host_ip3516hs_port_state_struct_t)) *
3464         ((uint32_t)usbHostState->portNumber)); /* malloc host ip3516hs port state */
3465     if (NULL == usbHostState->portState)
3466     {
3467 #ifdef HOST_ECHO
3468         usb_echo(" Mem alloc failed in USB_HostIp3516HsCreate\n");
3469 #endif
3470         (void)USB_HostIp3516HsDestory(usbHostState);
3471         return kStatus_USB_Error;
3472     }
3473 
3474     (void)USB_HostIp3516HsControllerReset(usbHostState);
3475 
3476     (void)USB_HostIp3516HsControllerInit(usbHostState);
3477 
3478 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3479     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3480     base = (USBHSDCD_Type *)hsdcd_base[controllerId - kUSB_ControllerIp3516Hs0];
3481     if (kStatus_hsdcd_Success != USB_HSDCD_Init(base, NULL, &usbHostState->dcdHandle))
3482     {
3483         (void)USB_HostIp3516HsDestory(usbHostState);
3484         return kStatus_USB_Error;
3485     }
3486     usbHostState->chargerType = kUSB_DcdSDP;
3487 #endif
3488 
3489     *controllerHandle = (usb_host_handle)usbHostState;
3490     return status;
3491 }
3492 
3493 /*!
3494  * @brief destroy USB host ip3516hs instance.
3495  *
3496  * This function de-initialize the USB host ip3516hs controller driver.
3497  *
3498  * @param handle                                    the controller handle.
3499  *
3500  * @retval kStatus_USB_Success              The host is initialized successfully.
3501  */
USB_HostIp3516HsDestory(usb_host_controller_handle controllerHandle)3502 usb_status_t USB_HostIp3516HsDestory(usb_host_controller_handle controllerHandle)
3503 {
3504     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3505 
3506     usbHostState->usbRegBase->USBINTR = 0U;
3507     usbHostState->usbRegBase->USBSTS  = 0xFFFFFFFFU;
3508 
3509     usbHostState->usbRegBase->USBCMD &= ~USB_HOST_IP3516HS_USBCMD_RS_MASK;
3510 
3511     if (NULL != usbHostState->portState)
3512     {
3513         (void)OSA_MemoryFree(usbHostState->portState);
3514         usbHostState->portState = NULL;
3515     }
3516 
3517     if (NULL != usbHostState->mutex)
3518     {
3519         (void)OSA_MutexDestroy(usbHostState->mutex);
3520         usbHostState->mutex = NULL;
3521     }
3522 
3523     if (NULL != usbHostState->ip3516HsEvent)
3524     {
3525         (void)OSA_EventDestroy(usbHostState->ip3516HsEvent);
3526         usbHostState->ip3516HsEvent = NULL;
3527     }
3528 
3529 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3530     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3531     USB_HSDCD_Deinit(usbHostState->dcdHandle);
3532 #endif
3533 
3534     return kStatus_USB_Success;
3535 }
3536 
USB_HostIp3516HsOpenPipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle * pipeHandle,usb_host_pipe_init_t * pipeInit)3537 usb_status_t USB_HostIp3516HsOpenPipe(usb_host_controller_handle controllerHandle,
3538                                       usb_host_pipe_handle *pipeHandle,
3539                                       usb_host_pipe_init_t *pipeInit)
3540 {
3541     usb_host_ip3516hs_pipe_struct_t *pipe          = NULL;
3542     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3543     usb_status_t status;
3544 
3545     status = USB_HostIp3516HsGetNewPipe(&usbHostState->pipeList, &pipe);
3546     if ((kStatus_USB_Success != status) || (NULL == pipe))
3547     {
3548 #ifdef HOST_ECHO
3549         usb_echo("ip3516hs open pipe failed\r\n");
3550 #endif
3551         return kStatus_USB_Busy;
3552     }
3553 
3554     /* initialize pipe informations */
3555     pipe->pipeCommon.deviceHandle    = pipeInit->devInstance;
3556     pipe->pipeCommon.endpointAddress = pipeInit->endpointAddress;
3557     pipe->pipeCommon.direction       = pipeInit->direction;
3558     pipe->pipeCommon.interval        = pipeInit->interval;
3559     pipe->pipeCommon.maxPacketSize   = pipeInit->maxPacketSize;
3560     pipe->pipeCommon.pipeType        = pipeInit->pipeType;
3561     pipe->pipeCommon.numberPerUframe = (pipeInit->numberPerUframe + 1U) & 0x03U;
3562     pipe->pipeCommon.nakCount        = pipeInit->nakCount;
3563     pipe->pipeCommon.nextdata01      = 0U;
3564     pipe->pipeCommon.currentCount    = 0U;
3565     pipe->pipeCommon.open            = 1U;
3566     pipe->tdIndex                    = 0xFFU;
3567     pipe->cutOffTime                 = USB_HOST_IP3516HS_TRANSFER_TIMEOUT_GAP;
3568     pipe->startUFrame                = 0U;
3569     pipe->csSlot                     = 0U;
3570     pipe->isBusy                     = 0U;
3571     pipe->bufferLength               = 0U;
3572     pipe->currentTr                  = NULL;
3573     pipe->lastPrimedIndex            = 0U;
3574 
3575     if (USB_ENDPOINT_ISOCHRONOUS == pipe->pipeCommon.pipeType)
3576     {
3577 #if 1U
3578         if (0U == pipe->pipeCommon.interval)
3579         {
3580             pipe->pipeCommon.interval = 1U;
3581         }
3582         pipe->pipeCommon.interval =
3583             (uint16_t)(1UL << (pipe->pipeCommon.interval - 1U)); /* iso interval is the power of 2 */
3584 #else
3585         pipe->pipeCommon.interval = 1U;
3586 #endif
3587     }
3588     else if (USB_ENDPOINT_INTERRUPT == pipe->pipeCommon.pipeType)
3589     {
3590         if (USB_SPEED_HIGH != ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed)
3591         {
3592             uint32_t interval = 0U;
3593             /* FS/LS interrupt interval should be the power of 2, it is used for ip3516hs bandwidth */
3594             for (uint32_t p = 0U; p < 16U; p++)
3595             {
3596                 if (0U != (pipe->pipeCommon.interval & ((uint16_t)(1UL << p))))
3597                 {
3598                     if (p > interval)
3599                     {
3600                         interval = p;
3601                     }
3602                 }
3603             }
3604             pipe->pipeCommon.interval = (uint16_t)(1UL << interval);
3605         }
3606         else
3607         {
3608             pipe->pipeCommon.interval = ((uint16_t)(
3609                 1UL << ((uint32_t)pipe->pipeCommon.interval - 1UL))); /* HS interrupt interval is the power of 2 */
3610         }
3611     }
3612     else
3613     {
3614         pipe->pipeCommon.interval = 0U;
3615     }
3616 
3617     if (USB_SPEED_HIGH != ((usb_host_device_instance_t *)pipe->pipeCommon.deviceHandle)->speed)
3618     {
3619         pipe->pipeCommon.interval = pipe->pipeCommon.interval << 3;
3620     }
3621 
3622     if (((uint16_t)USB_HOST_IP3516HS_MAX_FRAME) < pipe->pipeCommon.interval)
3623     {
3624         pipe->pipeCommon.interval = (uint16_t)USB_HOST_IP3516HS_MAX_FRAME;
3625     }
3626     /* open pipe */
3627     switch (pipe->pipeCommon.pipeType)
3628     {
3629         case USB_ENDPOINT_CONTROL:
3630         case USB_ENDPOINT_BULK:
3631             status = USB_HostIp3516HsOpenControlBulkPipe(usbHostState, pipe);
3632             break;
3633 
3634 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3635         case USB_ENDPOINT_ISOCHRONOUS:
3636             status = USB_HostIp3516HsOpenIsoPipe(usbHostState, pipe);
3637             break;
3638 #endif
3639 
3640 #if (defined(USB_HOST_CONFIG_IP3516HS_MAX_INT) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3641         case USB_ENDPOINT_INTERRUPT:
3642             status = USB_HostIp3516HsOpenInterruptPipe(usbHostState, pipe);
3643             break;
3644 #endif
3645         default:
3646             status = kStatus_USB_Error;
3647             break;
3648     }
3649 
3650     if (status != kStatus_USB_Success)
3651     {
3652         pipe->pipeCommon.open = 0U;
3653         /* release the pipe */
3654         (void)USB_HostIp3516HsInsertPipe(&usbHostState->pipeList, pipe);
3655         return status;
3656     }
3657 
3658     (void)USB_HostIp3516HsInsertPipe(&usbHostState->pipeListInUsing, pipe);
3659 
3660     *pipeHandle = pipe;
3661     return status;
3662 }
3663 
USB_HostIp3516HsClosePipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle pipeHandle)3664 usb_status_t USB_HostIp3516HsClosePipe(usb_host_controller_handle controllerHandle, usb_host_pipe_handle pipeHandle)
3665 {
3666     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3667     usb_host_ip3516hs_pipe_struct_t *pipe          = (usb_host_ip3516hs_pipe_struct_t *)pipeHandle;
3668 
3669     switch (pipe->pipeCommon.pipeType)
3670     {
3671         case USB_ENDPOINT_BULK:
3672         case USB_ENDPOINT_CONTROL:
3673             (void)USB_HostIp3516HsCloseControlBulkPipe(usbHostState, pipe);
3674             break;
3675 
3676 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3677         case USB_ENDPOINT_ISOCHRONOUS:
3678             (void)USB_HostIp3516HsCloseIsoPipe(usbHostState, pipe);
3679             break;
3680 #endif
3681 
3682 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3683         case USB_ENDPOINT_INTERRUPT:
3684             (void)USB_HostIp3516HsCloseInterruptPipe(usbHostState, pipe);
3685             break;
3686 #endif
3687 
3688         default:
3689             /*no action*/
3690             break;
3691     }
3692     (void)USB_HostIp3516HsRemovePipe(&usbHostState->pipeListInUsing, pipe);
3693     pipe->pipeCommon.open = 0U;
3694     (void)USB_HostIp3516HsInsertPipe(&usbHostState->pipeList, pipe);
3695 
3696     return kStatus_USB_Success;
3697 }
3698 
USB_HostIp3516HsWritePipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle pipeHandle,usb_host_transfer_t * transfer)3699 usb_status_t USB_HostIp3516HsWritePipe(usb_host_controller_handle controllerHandle,
3700                                        usb_host_pipe_handle pipeHandle,
3701                                        usb_host_transfer_t *transfer)
3702 {
3703     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3704     usb_host_ip3516hs_pipe_struct_t *pipe          = (usb_host_ip3516hs_pipe_struct_t *)pipeHandle;
3705     usb_host_transfer_t *trPre;
3706     usb_status_t status = kStatus_USB_Success;
3707     uint8_t trListEmpty = 0U;
3708     OSA_SR_ALLOC();
3709 
3710     transfer->setupStatus           = (uint8_t)kStatus_UsbHostIp3516Hs_Idle;
3711     transfer->next                  = NULL;
3712     transfer->transferSofar         = 0U;
3713     transfer->union1.transferResult = (int32_t)kStatus_USB_Success;
3714 
3715     OSA_ENTER_CRITICAL();
3716     if (NULL == pipe->trList)
3717     {
3718         pipe->trList    = transfer;
3719         pipe->currentTr = transfer;
3720         trListEmpty     = 1U;
3721     }
3722     else
3723     {
3724         trPre = pipe->trList;
3725         while (NULL != trPre->next)
3726         {
3727             trPre = trPre->next;
3728         }
3729         trPre->next = transfer;
3730     }
3731     OSA_EXIT_CRITICAL();
3732 
3733 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3734     if ((0U != trListEmpty) || (USB_ENDPOINT_ISOCHRONOUS == pipe->pipeCommon.pipeType))
3735 #else
3736     if ((0U != trListEmpty))
3737 #endif
3738     {
3739         switch (pipe->pipeCommon.pipeType)
3740         {
3741             case USB_ENDPOINT_BULK:
3742                 status = USB_HostIp3516HsWriteBulkPipe(usbHostState, pipe, transfer);
3743                 break;
3744 
3745             case USB_ENDPOINT_CONTROL:
3746                 status = USB_HostIp3516HsWriteControlPipe(usbHostState, pipe, transfer);
3747                 break;
3748 
3749 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3750             case USB_ENDPOINT_ISOCHRONOUS:
3751                 status = USB_HostIp3516HsWriteIsoPipe(usbHostState, pipe, pipe->trList);
3752                 break;
3753 #endif
3754 
3755 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3756             case USB_ENDPOINT_INTERRUPT:
3757                 status = USB_HostIp3516HsWriteInterruptPipe(usbHostState, pipe, transfer);
3758                 break;
3759 #endif
3760 
3761             default:
3762                 /*no action*/
3763                 break;
3764         }
3765     }
3766     return status;
3767 }
3768 
USB_HostIp3516HsReadPipe(usb_host_controller_handle controllerHandle,usb_host_pipe_handle pipeHandle,usb_host_transfer_t * transfer)3769 usb_status_t USB_HostIp3516HsReadPipe(usb_host_controller_handle controllerHandle,
3770                                       usb_host_pipe_handle pipeHandle,
3771                                       usb_host_transfer_t *transfer)
3772 {
3773     return USB_HostIp3516HsWritePipe(controllerHandle, pipeHandle, transfer);
3774 }
3775 
USB_HostIp3516HsIoctl(usb_host_controller_handle controllerHandle,uint32_t ioctlEvent,void * ioctlParam)3776 usb_status_t USB_HostIp3516HsIoctl(usb_host_controller_handle controllerHandle, uint32_t ioctlEvent, void *ioctlParam)
3777 {
3778     usb_host_ip3516hs_state_struct_t *usbHostState = (usb_host_ip3516hs_state_struct_t *)controllerHandle;
3779     usb_host_cancel_param_t *param;
3780     usb_host_ip3516hs_pipe_struct_t *pipe;
3781     uint32_t deviceAddress;
3782     usb_host_controller_control_t ioctl;
3783 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
3784 #if ((defined(USB_HOST_CONFIG_LPM_L1)) && (USB_HOST_CONFIG_LPM_L1 > 0U))
3785     uint8_t *lpmParam;
3786 #endif
3787 #endif
3788     deviceAddress       = 0U;
3789     usb_status_t status = kStatus_USB_Success;
3790 
3791     if (controllerHandle == NULL)
3792     {
3793         return kStatus_USB_InvalidHandle;
3794     }
3795     ioctl = (usb_host_controller_control_t)ioctlEvent;
3796     switch (ioctl)
3797     {
3798         case kUSB_HostCancelTransfer:
3799             /* cancel pipe or one transfer */
3800             param  = (usb_host_cancel_param_t *)ioctlParam;
3801             status = USB_HostIp3516HsCancelPipe(usbHostState, (usb_host_ip3516hs_pipe_struct_t *)param->pipeHandle,
3802                                                 param->transfer);
3803             break;
3804 #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
3805 #if ((defined(USB_HOST_CONFIG_LPM_L1)) && (USB_HOST_CONFIG_LPM_L1 > 0U))
3806         case kUSB_HostL1Config:
3807             lpmParam                           = (uint8_t *)ioctlParam;
3808             usbHostState->hirdValue            = (*lpmParam) & 0xFU;
3809             usbHostState->L1remoteWakeupEnable = (((*lpmParam) & 0x80U) >> 7);
3810             break;
3811 #endif
3812 #endif
3813         case kUSB_HostBusControl:
3814             /* bus control */
3815             status = USB_HostIp3516HsControlBus(usbHostState, *((uint8_t *)ioctlParam));
3816             break;
3817 
3818         case kUSB_HostGetFrameNumber:
3819             /* get frame number */
3820             *((uint32_t *)ioctlParam) =
3821                 (uint32_t)((usbHostState->usbRegBase->FLADJ_FRINDEX & USB_HOST_IP3516HS_FLADJ_FRINDEX_MASK) >>
3822                            USB_HOST_IP3516HS_FLADJ_FRINDEX_SHIFT);
3823             break;
3824 
3825         case kUSB_HostUpdateControlEndpointAddress:
3826             pipe = (usb_host_ip3516hs_pipe_struct_t *)ioctlParam;
3827             /* update address */
3828             (void)USB_HostHelperGetPeripheralInformation(pipe->pipeCommon.deviceHandle,
3829                                                          (uint32_t)kUSB_HostGetDeviceAddress, &deviceAddress);
3830             switch (pipe->pipeCommon.pipeType)
3831             {
3832                 case USB_ENDPOINT_BULK:
3833                 case USB_ENDPOINT_CONTROL:
3834                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3835                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3836                         .atl[pipe->tdIndex]
3837                         .control2Union.stateBitField.DeviceAddress = deviceAddress;
3838                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3839                     break;
3840 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3841                 case USB_ENDPOINT_INTERRUPT:
3842                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3843                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3844                         .interrupt[pipe->tdIndex]
3845                         .control2Union.stateBitField.DeviceAddress = deviceAddress;
3846                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3847                     break;
3848 #endif
3849 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3850                 case USB_ENDPOINT_ISOCHRONOUS:
3851                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3852                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3853                         .iso[pipe->tdIndex]
3854                         .control2Union.stateBitField.DeviceAddress = deviceAddress;
3855                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3856                     break;
3857 #endif
3858                 default:
3859                     /*no action*/
3860                     break;
3861             }
3862             (void)USB_HostIp3516HsDelay(usbHostState, 16U);
3863             break;
3864 
3865         case kUSB_HostUpdateControlPacketSize:
3866             pipe = (usb_host_ip3516hs_pipe_struct_t *)ioctlParam;
3867             switch (pipe->pipeCommon.pipeType)
3868             {
3869                 case USB_ENDPOINT_BULK:
3870                 case USB_ENDPOINT_CONTROL:
3871                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3872                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3873                         .atl[pipe->tdIndex]
3874                         .control1Union.stateBitField.MaxPacketLength = pipe->pipeCommon.maxPacketSize;
3875                     usbHostState->usbRegBase->ATL_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3876                     break;
3877 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_INT)) && (USB_HOST_CONFIG_IP3516HS_MAX_INT > 0U))
3878                 case USB_ENDPOINT_INTERRUPT:
3879                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3880                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3881                         .interrupt[pipe->tdIndex]
3882                         .control1Union.stateBitField.MaxPacketLength = pipe->pipeCommon.maxPacketSize;
3883                     usbHostState->usbRegBase->INT_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3884                     break;
3885 #endif
3886 #if ((defined(USB_HOST_CONFIG_IP3516HS_MAX_ISO)) && (USB_HOST_CONFIG_IP3516HS_MAX_ISO > 0U))
3887                 case USB_ENDPOINT_ISOCHRONOUS:
3888                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP |= 1UL << pipe->tdIndex;
3889                     s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3890                         .iso[pipe->tdIndex]
3891                         .control1Union.stateBitField.MaxPacketLength = pipe->pipeCommon.maxPacketSize;
3892                     if (0U != s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3893                                   .iso[pipe->tdIndex]
3894                                   .control2Union.stateBitField.S)
3895                     {
3896                         s_UsbHostIp3516HsPtd[usbHostState->controllerId]
3897                             .iso[pipe->tdIndex]
3898                             .control1Union.stateBitField.MaxPacketLength++;
3899                     }
3900                     usbHostState->usbRegBase->ISO_PTD_SKIP_MAP &= ~(1UL << pipe->tdIndex);
3901                     break;
3902 #endif
3903                 default:
3904                     /*no action*/
3905                     break;
3906             }
3907             break;
3908 
3909         case kUSB_HostPortAttachDisable:
3910             break;
3911 
3912         case kUSB_HostPortAttachEnable:
3913             if ((*(uint8_t *)ioctlParam) < usbHostState->portNumber)
3914             {
3915                 usbHostState->portState[*(uint8_t *)ioctlParam].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortDetached;
3916             }
3917             break;
3918 
3919 #if (defined(USB_HOST_CONFIG_BATTERY_CHARGER) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U)) && \
3920     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
3921         case kUSB_HostSetChargerType:
3922             usbHostState->chargerType = *((uint8_t *)ioctlParam);
3923             if ((usbHostState->portState[0].portStatus == kUSB_DeviceIp3516HsPortPhyDetached) ||
3924                 (usbHostState->portState[0].portStatus == kUSB_DeviceIp3516HsPortDetached))
3925             {
3926                 USB_HSDCD_Control(usbHostState->dcdHandle, kUSB_HostHSDcdSetType, &(usbHostState->chargerType));
3927             }
3928             break;
3929 #endif
3930 #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
3931         case kUSB_HostTestModeInit: /* test mode control */
3932             USB_HostIp3516HsTestModeInit((usb_host_device_instance_t *)ioctlParam);
3933             break;
3934 #endif
3935         default:
3936             status = kStatus_USB_NotSupported;
3937             break;
3938     }
3939     return status;
3940 }
3941 
USB_HostIp3516HsTaskFunction(void * hostHandle)3942 void USB_HostIp3516HsTaskFunction(void *hostHandle)
3943 {
3944     usb_host_ip3516hs_state_struct_t *usbHostState;
3945     uint32_t bitSet;
3946 
3947     if (NULL == hostHandle)
3948     {
3949         return;
3950     }
3951     usbHostState = (usb_host_ip3516hs_state_struct_t *)((usb_host_instance_t *)hostHandle)->controllerHandle;
3952 
3953     /* wait all event */
3954     if (KOSA_StatusSuccess == OSA_EventWait(usbHostState->ip3516HsEvent, 0xFFU, 0, USB_OSA_WAIT_TIMEOUT, &bitSet))
3955     {
3956         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_PORT_CHANGE))
3957         {
3958             (void)USB_HostIp3516HsPortChange(usbHostState);
3959         }
3960         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_ISO_TOKEN_DONE))
3961         {
3962             (void)USB_HostIp3516HsTokenDone(usbHostState);
3963         }
3964         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_INT_TOKEN_DONE))
3965         {
3966             (void)USB_HostIp3516HsTokenDone(usbHostState);
3967         }
3968         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_ATL_TOKEN_DONE))
3969         {
3970             (void)USB_HostIp3516HsTokenDone(usbHostState);
3971         }
3972         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_SOF))
3973         {
3974             (void)USB_HostIp3516HsSof(usbHostState);
3975         }
3976         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_ATTACH))
3977         {
3978             for (uint8_t i = 0; i < usbHostState->portNumber; i++)
3979             {
3980                 if ((uint8_t)kUSB_DeviceIp3516HsPortPhyAttached == usbHostState->portState[i].portStatus)
3981                 {
3982                     void *deviceHandle;
3983                     if (kStatus_USB_Success == USB_HostAttachDevice(usbHostState->hostHandle,
3984                                                                     usbHostState->portState[i].portSpeed, 0U, i, 1U,
3985                                                                     &deviceHandle))
3986                     {
3987                         usbHostState->portState[i].portStatus = (uint8_t)kUSB_DeviceIp3516HsPortAttached;
3988                     }
3989                 }
3990             }
3991         }
3992         if (0U != (bitSet & USB_HOST_IP3516HS_EVENT_DETACH))
3993         {
3994             for (uint8_t i = 0; i < usbHostState->portNumber; i++)
3995             {
3996                 if ((uint8_t)kUSB_DeviceIp3516HsPortPhyDetached == usbHostState->portState[i].portStatus)
3997                 {
3998                     (void)USB_HostDetachDevice(usbHostState->hostHandle, 0U, i);
3999                 }
4000             }
4001         }
4002     }
4003 }
4004 
USB_HostIp3516HsIsrFunction(void * hostHandle)4005 void USB_HostIp3516HsIsrFunction(void *hostHandle)
4006 {
4007     usb_host_ip3516hs_state_struct_t *usbHostState;
4008     static uint32_t interruptStatus = 0U;
4009     static uint32_t sofCount        = 0U;
4010 
4011     if (hostHandle == NULL)
4012     {
4013         return;
4014     }
4015 
4016     usbHostState = (usb_host_ip3516hs_state_struct_t *)((usb_host_instance_t *)hostHandle)->controllerHandle;
4017 
4018     interruptStatus = usbHostState->usbRegBase->USBSTS;
4019     interruptStatus &= usbHostState->usbRegBase->USBINTR;
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     usbHostState->usbRegBase->USBSTS = interruptStatus; /* clear interrupt */
4066 }
4067 
4068 #endif
4069