1 /*
2  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __USB_DEVICE_DCI_H__
10 #define __USB_DEVICE_DCI_H__
11 
12 /*!
13  * @addtogroup usb_device_controller_driver
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 
21 /*! @brief Macro to define controller handle */
22 #define usb_device_controller_handle usb_device_handle
23 #define USB_DEVICE_MESSAGES_SIZE \
24     (sizeof(uint32_t) * (1U + (sizeof(usb_device_callback_message_struct_t) - 1U) / sizeof(uint32_t)))
25 /*! @brief Available notify types for device notification */
26 typedef enum _usb_device_notification
27 {
28     kUSB_DeviceNotifyBusReset = 0x10U, /*!< Reset signal detected */
29     kUSB_DeviceNotifySuspend,          /*!< Suspend signal detected */
30     kUSB_DeviceNotifyResume,           /*!< Resume signal detected */
31     kUSB_DeviceNotifyLPMSleep,         /*!< LPM signal detected */
32     kUSB_DeviceNotifyLPMResume,        /*!< Resume signal detected */
33     kUSB_DeviceNotifyError,            /*!< Errors happened in bus */
34     kUSB_DeviceNotifyDetach,           /*!< Device disconnected from a host */
35     kUSB_DeviceNotifyAttach,           /*!< Device connected to a host */
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 Device notification message structure */
42 typedef struct _usb_device_callback_message_struct
43 {
44     uint8_t *buffer; /*!< Transferred buffer */
45     uint32_t length; /*!< Transferred data length */
46     uint8_t code;    /*!< Notification code */
47     uint8_t isSetup; /*!< Is in a setup phase */
48 } usb_device_callback_message_struct_t;
49 
50 /*! @brief Control type for controller */
51 typedef enum _usb_device_control_type
52 {
53     kUSB_DeviceControlRun = 0U,          /*!< Enable the device functionality */
54     kUSB_DeviceControlStop,              /*!< Disable the device functionality */
55     kUSB_DeviceControlEndpointInit,      /*!< Initialize a specified endpoint */
56     kUSB_DeviceControlEndpointDeinit,    /*!< De-initialize a specified endpoint */
57     kUSB_DeviceControlEndpointStall,     /*!< Stall a specified endpoint */
58     kUSB_DeviceControlEndpointUnstall,   /*!< Un-stall a specified endpoint */
59     kUSB_DeviceControlGetDeviceStatus,   /*!< Get device status */
60     kUSB_DeviceControlGetEndpointStatus, /*!< Get endpoint status */
61     kUSB_DeviceControlSetDeviceAddress,  /*!< Set device address */
62     kUSB_DeviceControlGetSynchFrame,     /*!< Get current frame */
63     kUSB_DeviceControlResume,            /*!< Drive controller to generate a resume signal in USB bus */
64     kUSB_DeviceControlSleepResume,       /*!< Drive controller to generate a LPM resume signal in USB bus */
65     kUSB_DeviceControlSuspend,           /*!< Drive controller to enter into suspend mode */
66     kUSB_DeviceControlSleep,             /*!< Drive controller to enter into sleep mode */
67     kUSB_DeviceControlSetDefaultStatus,  /*!< Set controller to default status */
68     kUSB_DeviceControlGetSpeed,          /*!< Get current speed */
69     kUSB_DeviceControlGetOtgStatus,      /*!< Get OTG status */
70     kUSB_DeviceControlSetOtgStatus,      /*!< Set OTG status */
71     kUSB_DeviceControlSetTestMode,       /*!< Drive xCHI into test mode */
72     kUSB_DeviceControlGetRemoteWakeUp,   /*!< Get flag of LPM Remote Wake-up Enabled by USB host. */
73 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
74     kUSB_DeviceControlDcdDisable, /*!< disable dcd module function. */
75     kUSB_DeviceControlDcdEnable,  /*!< enable dcd module function. */
76 #endif
77     kUSB_DeviceControlPreSetDeviceAddress, /*!< Pre set device address */
78     kUSB_DeviceControlUpdateHwTick,        /*!< update hardware tick */
79 #if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
80     kUSB_DeviceControlGetCurrentFrameCount, /*!< Get current frame count */
81 #endif
82 } usb_device_control_type_t;
83 
84 /*! @brief USB device controller initialization function typedef */
85 typedef usb_status_t (*usb_device_controller_init_t)(uint8_t controllerId,
86                                                      usb_device_handle handle,
87                                                      usb_device_controller_handle *controllerHandle);
88 
89 /*! @brief USB device controller de-initialization function typedef */
90 typedef usb_status_t (*usb_device_controller_deinit_t)(usb_device_controller_handle controllerHandle);
91 
92 /*! @brief USB device controller send data function typedef */
93 typedef usb_status_t (*usb_device_controller_send_t)(usb_device_controller_handle controllerHandle,
94                                                      uint8_t endpointAddress,
95                                                      uint8_t *buffer,
96                                                      uint32_t length);
97 
98 /*! @brief USB device controller receive data function typedef */
99 typedef usb_status_t (*usb_device_controller_recv_t)(usb_device_controller_handle controllerHandle,
100                                                      uint8_t endpointAddress,
101                                                      uint8_t *buffer,
102                                                      uint32_t length);
103 
104 /*! @brief USB device controller cancel transfer function in a specified endpoint typedef */
105 typedef usb_status_t (*usb_device_controller_cancel_t)(usb_device_controller_handle controllerHandle,
106                                                        uint8_t endpointAddress);
107 
108 /*! @brief USB device controller control function typedef */
109 typedef usb_status_t (*usb_device_controller_control_t)(usb_device_controller_handle controllerHandle,
110                                                         usb_device_control_type_t command,
111                                                         void *param);
112 
113 /*! @brief USB device controller interface structure */
114 typedef struct _usb_device_controller_interface_struct
115 {
116     usb_device_controller_init_t deviceInit;       /*!< Controller initialization */
117     usb_device_controller_deinit_t deviceDeinit;   /*!< Controller de-initialization */
118     usb_device_controller_send_t deviceSend;       /*!< Controller send data */
119     usb_device_controller_recv_t deviceRecv;       /*!< Controller receive data */
120     usb_device_controller_cancel_t deviceCancel;   /*!< Controller cancel transfer */
121     usb_device_controller_control_t deviceControl; /*!< Controller control */
122 } usb_device_controller_interface_struct_t;
123 
124 /*! @brief USB device status structure */
125 typedef struct _usb_device_struct
126 {
127 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)) || \
128     (defined(FSL_FEATURE_SOC_USB_ANALOG_COUNT) && (FSL_FEATURE_SOC_USB_ANALOG_COUNT > 0U))
129     volatile uint64_t hwTick; /*!< Current hw tick(ms)*/
130 #endif
131     usb_device_controller_handle controllerHandle;                       /*!< Controller handle */
132     const usb_device_controller_interface_struct_t *controllerInterface; /*!< Controller interface handle */
133 #if USB_DEVICE_CONFIG_USE_TASK
134     OSA_MSGQ_HANDLE_DEFINE(notificationQueueBuffer,
135                            USB_DEVICE_CONFIG_MAX_MESSAGES,
136                            USB_DEVICE_MESSAGES_SIZE); /*!< Message queue buffer*/
137     osa_msgq_handle_t notificationQueue;              /*!< Message queue*/
138 #endif
139     usb_device_callback_t deviceCallback; /*!< Device callback function pointer */
140     usb_device_endpoint_callback_struct_t
141         epCallback[USB_DEVICE_CONFIG_ENDPOINTS << 1U]; /*!< Endpoint callback function structure */
142     uint8_t deviceAddress;                             /*!< Current device address */
143     uint8_t controllerId;                              /*!< Controller ID */
144     uint8_t state;                                     /*!< Current device state */
145 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
146     uint8_t remotewakeup; /*!< Remote wakeup is enabled or not */
147 #endif
148     uint8_t isResetting; /*!< Is doing device reset or not */
149 #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
150     uint8_t epCallbackDirectly; /*!< Whether call ep callback directly when the task is enabled */
151 #endif
152 } usb_device_struct_t;
153 
154 /*******************************************************************************
155  * API
156  ******************************************************************************/
157 /*!
158  * @brief Notify the device that the controller status changed.
159  *
160  * This function is used to notify the device that the controller status changed.
161  *
162  * @param handle                 The device handle. It equals the value returned from USB_DeviceInit.
163  * @param message                The device callback message handle.
164  *
165  * @return A USB error code or kStatus_USB_Success.
166  */
167 usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg);
168 /*! @}*/
169 
170 #endif /* __USB_DEVICE_DCI_H__ */
171