Skip to content

Commit eff11aa

Browse files
middleware: usb: update mcux usb host controller driver to support zephyr
- add one porting file to use SDK usb host driver files in Zephyr - EHCI and KHCI driver include Zephyr USB port file if it is Zephyr environment Signed-off-by: Mark Wang <yichang.wang@nxp.com>
1 parent eba0880 commit eff11aa

File tree

3 files changed

+321
-1
lines changed

3 files changed

+321
-1
lines changed

mcux/middleware/mcux-sdk-middleware-usb/host/usb_host_ehci.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
*/
88
#include "usb_host_config.h"
99
#if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI > 0U))
10+
#if (defined CONFIG_UHC_DRIVER)
11+
#include "usb_host_mcux_drv_port.h"
12+
#include "fsl_device_registers.h"
13+
#include "usb_host_ehci.h"
14+
#if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT))
15+
#include "usb_phy.h"
16+
#endif
17+
#else
1018
#include "usb_host.h"
1119
#include "usb_host_hci.h"
1220
#include "usb_host_devices.h"
@@ -21,6 +29,7 @@
2129
#if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
2230
#include "usb_host.h"
2331
#endif
32+
#endif
2433
#if (defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && (FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET > 0U))
2534
#include "fsl_memory.h"
2635
#endif

mcux/middleware/mcux-sdk-middleware-usb/host/usb_host_khci.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88

99
#include "usb_host_config.h"
1010
#if ((defined USB_HOST_CONFIG_KHCI) && (USB_HOST_CONFIG_KHCI))
11+
#if (defined CONFIG_UHC_DRIVER)
12+
#include "usb_host_mcux_drv_port.h"
13+
#include "fsl_device_registers.h"
14+
#include "usb_host_khci.h"
15+
#else
1116
#include "usb_host.h"
1217
#include "usb_host_hci.h"
1318
#include "fsl_device_registers.h"
1419
#include "usb_host_khci.h"
1520
#include "usb_host_devices.h"
1621
#include "usb_host_framework.h"
22+
#endif
1723
/*******************************************************************************
1824
* Variables
1925
******************************************************************************/
@@ -1703,7 +1709,7 @@ usb_status_t USB_HostKhciOpenPipe(usb_host_controller_handle controllerHandle,
17031709
{
17041710
usb_khci_host_state_struct_t *usbHostPointer = (usb_khci_host_state_struct_t *)controllerHandle;
17051711
usb_host_pipe_t *pipePointer;
1706-
usb_host_pipe_t *prePipePointer;
1712+
usb_host_pipe_t *prePipePointer = NULL;
17071713
usb_host_pipe_t *tempPipePointer;
17081714

17091715
OSA_SR_ALLOC();
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
/*
2+
* Copyright 2024 NXP
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: BSD-3-Clause
6+
*/
7+
8+
/* This file is used in Zephyr */
9+
10+
#ifndef _USB_HOST_MCUX_DRV_PORT_H_
11+
#define _USB_HOST_MCUX_DRV_PORT_H_
12+
13+
/*******************************************************************************
14+
* Definitions
15+
******************************************************************************/
16+
17+
/*! @brief USB host controller handle type define */
18+
typedef void *usb_host_controller_handle;
19+
20+
/*! @brief USB host pipe handle type define */
21+
typedef void *usb_host_pipe_handle;
22+
23+
/*! @brief USB host configuration handle type define */
24+
typedef void *usb_host_configuration_handle;
25+
26+
/*! @brief USB host controller control code */
27+
typedef enum _usb_host_controller_control {
28+
kUSB_HostCancelTransfer = 1U, /*!< Cancel transfer code */
29+
kUSB_HostBusControl, /*!< Bus control code */
30+
kUSB_HostGetFrameNumber, /*!< Get frame number code */
31+
kUSB_HostUpdateControlEndpointAddress, /*!< Update control endpoint address */
32+
kUSB_HostUpdateControlPacketSize, /*!< Update control endpoint maximum packet size */
33+
kUSB_HostPortAttachDisable, /*!< Disable the port attach event */
34+
kUSB_HostPortAttachEnable, /*!< Enable the port attach event */
35+
kUSB_HostL1Config, /*!< L1 suspend Bus control code */
36+
kUSB_HostSetChargerType, /*!< set charger type */
37+
#if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
38+
kUSB_HostTestModeInit, /*!< initialize charger type */
39+
#endif
40+
} usb_host_controller_control_t;
41+
42+
/*! @brief USB host controller bus control code */
43+
typedef enum _usb_host_bus_control {
44+
kUSB_HostBusReset = 1U, /*!< Reset bus */
45+
kUSB_HostBusRestart, /*!< Restart bus */
46+
kUSB_HostBusEnableAttach, /*!< Enable attach */
47+
kUSB_HostBusDisableAttach, /*!< Disable attach */
48+
kUSB_HostBusSuspend, /*!< Suspend BUS */
49+
kUSB_HostBusResume, /*!< Resume BUS */
50+
kUSB_HostBusL1SuspendInit, /*!< L1 Suspend BUS */
51+
kUSB_HostBusL1Sleep, /*!< L1 Suspend BUS */
52+
kUSB_HostBusL1Resume, /*!< L1 Resume BUS */
53+
} usb_host_bus_control_t;
54+
55+
/*! @brief USB host device information code */
56+
typedef enum _usb_host_dev_info {
57+
kUSB_HostGetDeviceAddress = 1U, /*!< Device's address */
58+
kUSB_HostGetDeviceHubNumber, /*!< Device's first hub address */
59+
kUSB_HostGetDevicePortNumber, /*!< Device's first hub port number */
60+
kUSB_HostGetDeviceSpeed, /*!< Device's speed */
61+
kUSB_HostGetDeviceHSHubNumber, /*!< Device's first high-speed hub address */
62+
kUSB_HostGetDeviceHSHubPort, /*!< Device's first high-speed hub number */
63+
kUSB_HostGetDeviceLevel, /*!< Device's hub level */
64+
kUSB_HostGetHostHandle, /*!< Device's host handle */
65+
kUSB_HostGetDeviceControlPipe, /*!< Device's control pipe handle */
66+
kUSB_HostGetDevicePID, /*!< Device's PID */
67+
kUSB_HostGetDeviceVID, /*!< Device's VID */
68+
kUSB_HostGetHubThinkTime, /*!< Device's hub total think time */
69+
kUSB_HostGetDeviceConfigIndex, /*!< Device's running zero-based config index */
70+
kUSB_HostGetConfigurationDes, /*!< Device's configuration descriptor pointer */
71+
kUSB_HostGetConfigurationLength,/*!< Device's configuration descriptor pointer */
72+
} usb_host_dev_info_t;
73+
74+
struct _usb_host_transfer;
75+
/*!
76+
* @brief Host stack inner transfer callback function typedef.
77+
*
78+
* This callback function is used to notify the upper layer the result of a transfer.
79+
* This callback pointer is passed when initializing the structure usb_host_transfer_t.
80+
*
81+
* @param param The parameter pointer, which is passed when calling the send/receive APIs.
82+
* @param transfer The transfer information; See the structure usb_host_transfer_t.
83+
* @param status A USB error code or kStatus_USB_Success.
84+
*/
85+
typedef void (*host_inner_transfer_callback_t)(void *param,
86+
struct _usb_host_transfer *transfer, usb_status_t status);
87+
88+
/*!
89+
* @brief Host callback function typedef.
90+
*
91+
* This callback function is used to notify application device attach/detach event.
92+
* This callback pointer is passed when initializing the host.
93+
*
94+
* @param deviceHandle The device handle, which indicates the attached device.
95+
* @param configurationHandle The configuration handle contains the attached device's
96+
* configuration information.
97+
* @param event_code The callback event code; See the enumeration host_event_t.
98+
*
99+
* @return A USB error code or kStatus_USB_Success.
100+
* @retval kStatus_USB_Success Application handles the attached device successfully.
101+
* @retval kStatus_USB_NotSupported Application don't support the attached device.
102+
* @retval kStatus_USB_Error Application handles the attached device falsely.
103+
*/
104+
typedef usb_status_t (*host_callback_t)(usb_device_handle deviceHandle,
105+
usb_host_configuration_handle configurationHandle,
106+
uint32_t eventCode);
107+
108+
/*! @brief USB host pipe information structure for opening pipe */
109+
typedef struct _usb_host_pipe_init {
110+
void *devInstance; /*!< Device instance handle*/
111+
uint16_t nakCount; /*!< Maximum NAK retry count. MUST be zero for interrupt*/
112+
uint16_t maxPacketSize; /*!< Pipe's maximum packet size*/
113+
uint8_t interval; /*!< Pipe's interval*/
114+
uint8_t endpointAddress;/*!< Endpoint address*/
115+
uint8_t direction; /*!< Endpoint direction*/
116+
uint8_t pipeType; /*!< Endpoint type, the value is USB_ENDPOINT_INTERRUPT,
117+
* USB_ENDPOINT_CONTROL, USB_ENDPOINT_ISOCHRONOUS,
118+
* USB_ENDPOINT_BULK
119+
*/
120+
uint8_t numberPerUframe;/*!< Transaction number for each micro-frame*/
121+
} usb_host_pipe_init_t;
122+
123+
/*! @brief USB host pipe common structure */
124+
typedef struct _usb_host_pipe {
125+
struct _usb_host_pipe *next; /*!< Link the idle pipes*/
126+
usb_device_handle deviceHandle; /*!< This pipe's device's handle*/
127+
uint16_t currentCount; /*!< For KHCI transfer*/
128+
uint16_t nakCount; /*!< Maximum NAK count*/
129+
uint16_t maxPacketSize; /*!< Maximum packet size*/
130+
uint16_t interval; /*!< FS/LS: frame unit; HS: micro-frame unit*/
131+
uint8_t open; /*!< 0 - closed, 1 - open*/
132+
uint8_t nextdata01; /*!< Data toggle*/
133+
uint8_t endpointAddress; /*!< Endpoint address*/
134+
uint8_t direction; /*!< Pipe direction*/
135+
uint8_t pipeType; /*!< Pipe type, for example USB_ENDPOINT_BULK*/
136+
uint8_t numberPerUframe; /*!< Transaction number per micro-frame*/
137+
} usb_host_pipe_t;
138+
139+
/*! @brief USB host transfer structure */
140+
typedef struct _usb_host_transfer {
141+
struct _usb_host_transfer *next; /*!< The next transfer structure*/
142+
uint8_t *transferBuffer; /*!< Transfer data buffer*/
143+
uint32_t transferLength; /*!< Transfer data length*/
144+
uint32_t transferSofar; /*!< Length transferred so far*/
145+
host_inner_transfer_callback_t callbackFn; /*!< Transfer callback function*/
146+
void *callbackParam; /*!< Transfer callback parameter*/
147+
usb_host_pipe_t *transferPipe; /*!< Transfer pipe pointer*/
148+
usb_setup_struct_t *setupPacket; /*!< Set up packet buffer*/
149+
/*! Transfer direction; it's values are USB_OUT or USB_IN */
150+
uint8_t direction;
151+
uint8_t setupStatus; /*!< Set up the transfer status*/
152+
union {
153+
uint32_t unitHead; /*!< xTD head for this transfer*/
154+
int32_t transferResult; /*!< KHCI transfer result */
155+
} union1;
156+
157+
union {
158+
uint32_t unitTail; /*!<xTD tail for this transfer*/
159+
uint32_t frame; /*!< KHCI transfer frame number */
160+
} union2;
161+
162+
#if USB_HOST_CONFIG_KHCI
163+
uint16_t nakTimeout; /*!< KHCI transfer NAK timeout */
164+
uint16_t retry; /*!< KHCI transfer retry */
165+
#endif
166+
void *uhc_xfer;
167+
} usb_host_transfer_t;
168+
169+
/*! @brief Cancel transfer parameter structure */
170+
typedef struct _usb_host_cancel_param {
171+
usb_host_pipe_handle pipeHandle; /*!< Canceling pipe handle*/
172+
usb_host_transfer_t *transfer; /*!< Canceling transfer*/
173+
} usb_host_cancel_param_t;
174+
175+
/*! @brief USB host controller interface structure */
176+
typedef struct _usb_host_controller_interface {
177+
/*! Create a controller instance function prototype*/
178+
usb_status_t (*controllerCreate)(
179+
uint8_t controllerId,
180+
usb_host_handle upperLayerHandle,
181+
usb_host_controller_handle *controllerHandle);
182+
/*! Destroy a controller instance function prototype*/
183+
usb_status_t (*controllerDestory)(
184+
usb_host_controller_handle controllerHandle);
185+
/*! Open a controller pipe function prototype*/
186+
usb_status_t (*controllerOpenPipe)(usb_host_controller_handle controllerHandle,
187+
usb_host_pipe_handle *pipeHandle,
188+
usb_host_pipe_init_t *pipeInit);
189+
/*! Close a controller pipe function prototype*/
190+
usb_status_t (*controllerClosePipe)(
191+
usb_host_controller_handle controllerHandle,
192+
usb_host_pipe_handle pipeHandle);
193+
/*! Write data to a pipe function prototype*/
194+
usb_status_t (*controllerWritePipe)(usb_host_controller_handle controllerHandle,
195+
usb_host_pipe_handle pipeHandle,
196+
usb_host_transfer_t *transfer);
197+
/*! Read data from a pipe function prototype*/
198+
usb_status_t (*controllerReadPipe)(usb_host_controller_handle controllerHandle,
199+
usb_host_pipe_handle pipeHandle,
200+
usb_host_transfer_t *transfer);
201+
/*! Control a controller function prototype*/
202+
usb_status_t (*controllerIoctl)(usb_host_controller_handle controllerHandle,
203+
uint32_t ioctlEvent,
204+
void *ioctlParam);
205+
} usb_host_controller_interface_t;
206+
207+
/*! @brief USB host instance structure */
208+
typedef struct _usb_host_instance {
209+
void *controllerHandle; /*!< The low level controller handle*/
210+
host_callback_t deviceCallback; /*!< Device attach/detach callback*/
211+
} usb_host_instance_t;
212+
213+
/*!
214+
* @brief Controller driver calls this function when device detaches.
215+
*
216+
* @param hostHandle Host instance handle.
217+
* @param hubNumber Device hub no. root device's hub no. is 0.
218+
* @param portNumber Device port no. root device's port no. is 0.
219+
*
220+
* @return kStatus_USB_Success or error codes.
221+
*/
222+
usb_status_t USB_HostDetachDevice(usb_host_handle hostHandle, uint8_t hubNumber,
223+
uint8_t portNumber);
224+
225+
/*!
226+
* @brief Controller driver calls this function when device attach.
227+
*
228+
* @param hostHandle Host instance handle.
229+
* @param speed Device speed.
230+
* @param hubNumber Device hub no. root device's hub no. is 0.
231+
* @param portNumber Device port no. root device's port no. is 0.
232+
* @param level Device level. root device's level is 1.
233+
* @param deviceHandle Return device handle.
234+
*
235+
* @return kStatus_USB_Success or error codes.
236+
*/
237+
usb_status_t USB_HostAttachDevice(usb_host_handle hostHandle,
238+
uint8_t speed,
239+
uint8_t hubNumber,
240+
uint8_t portNumber,
241+
uint8_t level,
242+
usb_device_handle *deviceHandle);
243+
244+
/*!
245+
* @brief Controller driver calls this function to get the device information.
246+
*
247+
* This function gets the device information.
248+
*
249+
* @param[in] deviceHandle Removing device handle.
250+
* @param[in] infoCode See the enumeration host_dev_info_t.
251+
* @param[out] infoValue Return the information value.
252+
*
253+
* @retval kStatus_USB_Success Close successfully.
254+
* @retval kStatus_USB_InvalidParameter The deviceHandle or info_value is a NULL pointer.
255+
* @retval kStatus_USB_Error The info_code is not the host_dev_info_t value.
256+
*/
257+
usb_status_t USB_HostHelperGetPeripheralInformation(usb_device_handle deviceHandle,
258+
uint32_t infoCode,
259+
uint32_t *infoValue);
260+
261+
#ifdef CONFIG_USB_UHC_NXP_EHCI
262+
/*!
263+
* @brief Device EHCI ISR function.
264+
*
265+
* The function is the EHCI interrupt service routine.
266+
*
267+
* @param[in] hostHandle The host handle.
268+
*/
269+
void USB_HostEhciIsrFunction(void *hostHandle);
270+
271+
/*!
272+
* @brief EHCI task function.
273+
*
274+
* The function is used to handle the EHCI controller message.
275+
* In the bare metal environment, this function should be called periodically in the main function.
276+
* In the RTOS environment, this function should be used as a function entry to create a task.
277+
*
278+
* @param[in] hostHandle The host handle.
279+
*/
280+
void USB_HostEhciTaskFunction(void *hostHandle);
281+
#endif
282+
283+
#ifdef CONFIG_USB_UHC_NXP_KHCI
284+
/*!
285+
* @brief Device KHCI ISR function.
286+
*
287+
* The function is the KHCI interrupt service routine.
288+
*
289+
* @param[in] hostHandle The host handle.
290+
*/
291+
void USB_HostKhciIsrFunction(void *hostHandle);
292+
293+
/*!
294+
* @brief KHCI task function.
295+
*
296+
* The function is used to handle the KHCI controller message.
297+
* In the bare metal environment, this function should be called periodically in the main function.
298+
* In the RTOS environment, this function should be used as a function entry to create a task.
299+
*
300+
* @param[in] hostHandle The host handle.
301+
*/
302+
void USB_HostKhciTaskFunction(void *hostHandle);
303+
#endif
304+
305+
#endif /* _USB_HOST_MCUX_DRV_PORT_H_ */

0 commit comments

Comments
 (0)