1 /*
2  * Copyright 2024 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _USB_DEVICE_MCUX_DRV_PORT_H_
9 #define _USB_DEVICE_MCUX_DRV_PORT_H_
10 
11 /*! @brief The setup packet size of USB control transfer. */
12 #define USB_SETUP_PACKET_SIZE (8U)
13 /*! @brief Control endpoint index */
14 #define USB_CONTROL_ENDPOINT (0U)
15 /*! @brief  USB endpoint mask */
16 #define USB_ENDPOINT_NUMBER_MASK (0x0FU)
17 /*! @brief Control endpoint maxPacketSize */
18 #define USB_CONTROL_MAX_PACKET_SIZE (64U)
19 /*! @brief the endpoint callback length of cancelled transfer */
20 #define USB_CANCELLED_TRANSFER_LENGTH (0xFFFFFFFFU)
21 /*! @brief Macro to define controller handle */
22 #define usb_device_controller_handle usb_device_handle
23 
24 /*! @brief Available notify types for device notification */
25 typedef enum _usb_device_notification
26 {
27     kUSB_DeviceNotifyBusReset = 0x10U, /*!< Reset signal detected */
28     kUSB_DeviceNotifySuspend,          /*!< Suspend signal detected */
29     kUSB_DeviceNotifyResume,           /*!< Resume signal detected */
30     kUSB_DeviceNotifyLPMSleep,         /*!< LPM signal detected */
31     kUSB_DeviceNotifyLPMResume,        /*!< Resume signal detected */
32     kUSB_DeviceNotifyError,            /*!< Errors happened in bus */
33     kUSB_DeviceNotifyDetach,           /*!< Device disconnected from a host */
34     kUSB_DeviceNotifyAttach,           /*!< Device connected to a host */
35     kUSB_DeviceNotifySOF,              /*!< Start of Frame received */
36 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
37     kUSB_DeviceNotifyDcdDetectFinished, /*!< Device charger detection finished */
38 #endif
39 } usb_device_notification_t;
40 
41 /*! @brief Control type for controller */
42 typedef enum _usb_device_control_type
43 {
44     kUSB_DeviceControlRun = 0U,          /*!< Enable the device functionality */
45     kUSB_DeviceControlStop,              /*!< Disable the device functionality */
46     kUSB_DeviceControlEndpointInit,      /*!< Initialize a specified endpoint */
47     kUSB_DeviceControlEndpointDeinit,    /*!< De-initialize a specified endpoint */
48     kUSB_DeviceControlEndpointStall,     /*!< Stall a specified endpoint */
49     kUSB_DeviceControlEndpointUnstall,   /*!< Un-stall a specified endpoint */
50     kUSB_DeviceControlGetDeviceStatus,   /*!< Get device status */
51     kUSB_DeviceControlGetEndpointStatus, /*!< Get endpoint status */
52     kUSB_DeviceControlSetDeviceAddress,  /*!< Set device address */
53     kUSB_DeviceControlGetSynchFrame,     /*!< Get current frame */
54     kUSB_DeviceControlResume,            /*!< Drive controller to generate a resume signal in USB bus */
55     kUSB_DeviceControlSleepResume,       /*!< Drive controller to generate a LPM resume signal in USB bus */
56     kUSB_DeviceControlSuspend,           /*!< Drive controller to enter into suspend mode */
57     kUSB_DeviceControlSleep,             /*!< Drive controller to enter into sleep mode */
58     kUSB_DeviceControlSetDefaultStatus,  /*!< Set controller to default status */
59     kUSB_DeviceControlGetSpeed,          /*!< Get current speed */
60     kUSB_DeviceControlGetOtgStatus,      /*!< Get OTG status */
61     kUSB_DeviceControlSetOtgStatus,      /*!< Set OTG status */
62     kUSB_DeviceControlSetTestMode,       /*!< Drive xCHI into test mode */
63     kUSB_DeviceControlGetRemoteWakeUp,   /*!< Get flag of LPM Remote Wake-up Enabled by USB host. */
64     kUSB_DeviceControlPreSetDeviceAddress, /*!< Pre set device address */
65 #if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
66     kUSB_DeviceControlGetCurrentFrameCount, /*!< Get current frame count */
67 #endif
68 } usb_device_control_type_t;
69 
70 /*! @brief Endpoint initialization structure */
71 typedef struct _usb_device_endpoint_init_struct
72 {
73     uint16_t maxPacketSize;  /*!< Endpoint maximum packet size */
74     uint8_t endpointAddress; /*!< Endpoint address*/
75     uint8_t transferType;    /*!< Endpoint transfer type*/
76     uint8_t zlt;             /*!< ZLT flag*/
77     uint8_t interval;        /*!< Endpoint interval*/
78 } usb_device_endpoint_init_struct_t;
79 
80 /*! @brief USB device controller initialization function typedef */
81 typedef usb_status_t (*usb_device_controller_init_t)(uint8_t controllerId,
82                                                      usb_device_handle handle,
83                                                      usb_device_controller_handle *controllerHandle);
84 
85 /*! @brief USB device controller de-initialization function typedef */
86 typedef usb_status_t (*usb_device_controller_deinit_t)(usb_device_controller_handle controllerHandle);
87 
88 /*! @brief USB device controller send data function typedef */
89 typedef usb_status_t (*usb_device_controller_send_t)(usb_device_controller_handle controllerHandle,
90                                                      uint8_t endpointAddress,
91                                                      uint8_t *buffer,
92                                                      uint32_t length);
93 
94 /*! @brief USB device controller receive data function typedef */
95 typedef usb_status_t (*usb_device_controller_recv_t)(usb_device_controller_handle controllerHandle,
96                                                      uint8_t endpointAddress,
97                                                      uint8_t *buffer,
98                                                      uint32_t length);
99 
100 /*! @brief USB device controller cancel transfer function in a specified endpoint typedef */
101 typedef usb_status_t (*usb_device_controller_cancel_t)(usb_device_controller_handle controllerHandle,
102                                                        uint8_t endpointAddress);
103 
104 /*! @brief USB device controller control function typedef */
105 typedef usb_status_t (*usb_device_controller_control_t)(usb_device_controller_handle controllerHandle,
106                                                         usb_device_control_type_t command,
107                                                         void *param);
108 
109 /*! @brief USB device controller interface structure */
110 typedef struct _usb_device_controller_interface_struct
111 {
112     usb_device_controller_init_t deviceInit;       /*!< Controller initialization */
113     usb_device_controller_deinit_t deviceDeinit;   /*!< Controller de-initialization */
114     usb_device_controller_send_t deviceSend;       /*!< Controller send data */
115     usb_device_controller_recv_t deviceRecv;       /*!< Controller receive data */
116     usb_device_controller_cancel_t deviceCancel;   /*!< Controller cancel transfer */
117     usb_device_controller_control_t deviceControl; /*!< Controller control */
118 } usb_device_controller_interface_struct_t;
119 
120 /*! @brief USB device status structure */
121 typedef struct _usb_device_struct
122 {
123     usb_device_controller_handle controllerHandle;     /*!< Controller handle */
124 } usb_device_struct_t;
125 
126 /*! @brief Device notification message structure */
127 typedef struct _usb_device_callback_message_struct
128 {
129     uint8_t *buffer; /*!< Transferred buffer */
130     uint32_t length; /*!< Transferred data length */
131     uint8_t code;    /*!< Notification code */
132     uint8_t isSetup; /*!< Is in a setup phase */
133 } usb_device_callback_message_struct_t;
134 
135 /*! @brief Endpoint status structure */
136 typedef struct _usb_device_endpoint_status_struct
137 {
138     uint8_t endpointAddress; /*!< Endpoint address */
139     uint16_t endpointStatus; /*!< Endpoint status : idle or stalled */
140 } usb_device_endpoint_status_struct_t;
141 
142 /*! @brief Defines endpoint state */
143 typedef enum _usb_endpoint_status
144 {
145     kUSB_DeviceEndpointStateIdle = 0U, /*!< Endpoint state, idle*/
146     kUSB_DeviceEndpointStateStalled,   /*!< Endpoint state, stalled*/
147 } usb_device_endpoint_status_t;
148 
149 /*!
150  * @brief Notify the device that the controller status changed.
151  *
152  * This function is used to notify the device that the controller status changed.
153  *
154  * @param handle                 The device handle. It equals the value returned from USB_DeviceInit.
155  * @param message                The device callback message handle.
156  *
157  * @return A USB error code or kStatus_USB_Success.
158  */
159 usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg);
160 
161 #if ((defined USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI))
162 /*!
163  * @brief Device EHCI ISR function.
164  *
165  * The function is the EHCI interrupt service routine.
166  *
167  * @param[in] deviceHandle The device handle got from #USB_DeviceInit.
168  */
169 extern void USB_DeviceEhciIsrFunction(void *deviceHandle);
170 #endif
171 
172 #if ((defined USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS)) ||\
173     ((defined USB_DEVICE_CONFIG_LPCIP3511FS) && (USB_DEVICE_CONFIG_LPCIP3511FS))
174 /*!
175  * @brief Device LPC USB ISR function.
176  *
177  * The function is the LPC USB interrupt service routine.
178  *
179  * @param[in] deviceHandle The device handle got from #USB_DeviceInit.
180  */
181 extern void USB_DeviceLpcIp3511IsrFunction(void *deviceHandle);
182 #endif
183 
184 #endif /* _USB_DEVICE_MCUX_DRV_PORT_H_ */
185