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_SOF_NOTIFICATIONS) && (USB_DEVICE_CONFIG_SOF_NOTIFICATIONS > 0U))
37     kUSB_DeviceNotifySOF,              /*!< Start of Frame received */
38 #endif
39 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
40     kUSB_DeviceNotifyDcdDetectFinished, /*!< Device charger detection finished */
41 #endif
42 } usb_device_notification_t;
43 
44 /*! @brief Device notification message structure */
45 typedef struct _usb_device_callback_message_struct
46 {
47     uint8_t *buffer; /*!< Transferred buffer */
48     uint32_t length; /*!< Transferred data length */
49     uint8_t code;    /*!< Notification code */
50     uint8_t isSetup; /*!< Is in a setup phase */
51 } usb_device_callback_message_struct_t;
52 
53 /*! @brief Control type for controller */
54 typedef enum _usb_device_control_type
55 {
56     kUSB_DeviceControlRun = 0U,          /*!< Enable the device functionality */
57     kUSB_DeviceControlStop,              /*!< Disable the device functionality */
58     kUSB_DeviceControlEndpointInit,      /*!< Initialize a specified endpoint */
59     kUSB_DeviceControlEndpointDeinit,    /*!< De-initialize a specified endpoint */
60     kUSB_DeviceControlEndpointStall,     /*!< Stall a specified endpoint */
61     kUSB_DeviceControlEndpointUnstall,   /*!< Un-stall a specified endpoint */
62     kUSB_DeviceControlGetDeviceStatus,   /*!< Get device status */
63     kUSB_DeviceControlGetEndpointStatus, /*!< Get endpoint status */
64     kUSB_DeviceControlSetDeviceAddress,  /*!< Set device address */
65     kUSB_DeviceControlGetSynchFrame,     /*!< Get current frame */
66     kUSB_DeviceControlResume,            /*!< Drive controller to generate a resume signal in USB bus */
67     kUSB_DeviceControlSleepResume,       /*!< Drive controller to generate a LPM resume signal in USB bus */
68     kUSB_DeviceControlSuspend,           /*!< Drive controller to enter into suspend mode */
69     kUSB_DeviceControlSleep,             /*!< Drive controller to enter into sleep mode */
70     kUSB_DeviceControlSetDefaultStatus,  /*!< Set controller to default status */
71     kUSB_DeviceControlGetSpeed,          /*!< Get current speed */
72     kUSB_DeviceControlGetOtgStatus,      /*!< Get OTG status */
73     kUSB_DeviceControlSetOtgStatus,      /*!< Set OTG status */
74     kUSB_DeviceControlSetTestMode,       /*!< Drive xCHI into test mode */
75     kUSB_DeviceControlGetRemoteWakeUp,   /*!< Get flag of LPM Remote Wake-up Enabled by USB host. */
76 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
77     kUSB_DeviceControlDcdDisable, /*!< disable dcd module function. */
78     kUSB_DeviceControlDcdEnable,  /*!< enable dcd module function. */
79 #endif
80     kUSB_DeviceControlPreSetDeviceAddress, /*!< Pre set device address */
81     kUSB_DeviceControlUpdateHwTick,        /*!< update hardware tick */
82 #if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
83     kUSB_DeviceControlGetCurrentFrameCount, /*!< Get current frame count */
84 #endif
85 } usb_device_control_type_t;
86 
87 /*! @brief USB device controller initialization function typedef */
88 typedef usb_status_t (*usb_device_controller_init_t)(uint8_t controllerId,
89                                                      usb_device_handle handle,
90                                                      usb_device_controller_handle *controllerHandle);
91 
92 /*! @brief USB device controller de-initialization function typedef */
93 typedef usb_status_t (*usb_device_controller_deinit_t)(usb_device_controller_handle controllerHandle);
94 
95 /*! @brief USB device controller send data function typedef */
96 typedef usb_status_t (*usb_device_controller_send_t)(usb_device_controller_handle controllerHandle,
97                                                      uint8_t endpointAddress,
98                                                      uint8_t *buffer,
99                                                      uint32_t length);
100 
101 /*! @brief USB device controller receive data function typedef */
102 typedef usb_status_t (*usb_device_controller_recv_t)(usb_device_controller_handle controllerHandle,
103                                                      uint8_t endpointAddress,
104                                                      uint8_t *buffer,
105                                                      uint32_t length);
106 
107 /*! @brief USB device controller cancel transfer function in a specified endpoint typedef */
108 typedef usb_status_t (*usb_device_controller_cancel_t)(usb_device_controller_handle controllerHandle,
109                                                        uint8_t endpointAddress);
110 
111 /*! @brief USB device controller control function typedef */
112 typedef usb_status_t (*usb_device_controller_control_t)(usb_device_controller_handle controllerHandle,
113                                                         usb_device_control_type_t command,
114                                                         void *param);
115 
116 /*! @brief USB device controller interface structure */
117 typedef struct _usb_device_controller_interface_struct
118 {
119     usb_device_controller_init_t deviceInit;       /*!< Controller initialization */
120     usb_device_controller_deinit_t deviceDeinit;   /*!< Controller de-initialization */
121     usb_device_controller_send_t deviceSend;       /*!< Controller send data */
122     usb_device_controller_recv_t deviceRecv;       /*!< Controller receive data */
123     usb_device_controller_cancel_t deviceCancel;   /*!< Controller cancel transfer */
124     usb_device_controller_control_t deviceControl; /*!< Controller control */
125 } usb_device_controller_interface_struct_t;
126 
127 /*! @brief USB device status structure */
128 typedef struct _usb_device_struct
129 {
130 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)) || \
131     (defined(FSL_FEATURE_SOC_USB_ANALOG_COUNT) && (FSL_FEATURE_SOC_USB_ANALOG_COUNT > 0U))
132     volatile uint64_t hwTick; /*!< Current hw tick(ms)*/
133 #endif
134     usb_device_controller_handle controllerHandle;                       /*!< Controller handle */
135     const usb_device_controller_interface_struct_t *controllerInterface; /*!< Controller interface handle */
136 #if USB_DEVICE_CONFIG_USE_TASK
137     OSA_MSGQ_HANDLE_DEFINE(notificationQueueBuffer,
138                            USB_DEVICE_CONFIG_MAX_MESSAGES,
139                            USB_DEVICE_MESSAGES_SIZE); /*!< Message queue buffer*/
140     osa_msgq_handle_t notificationQueue;              /*!< Message queue*/
141 #endif
142     usb_device_callback_t deviceCallback; /*!< Device callback function pointer */
143     usb_device_endpoint_callback_struct_t
144         epCallback[USB_DEVICE_CONFIG_ENDPOINTS << 1U]; /*!< Endpoint callback function structure */
145     uint8_t deviceAddress;                             /*!< Current device address */
146     uint8_t controllerId;                              /*!< Controller ID */
147     uint8_t state;                                     /*!< Current device state */
148 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
149     uint8_t remotewakeup; /*!< Remote wakeup is enabled or not */
150 #endif
151     uint8_t isResetting; /*!< Is doing device reset or not */
152 #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
153     uint8_t epCallbackDirectly; /*!< Whether call ep callback directly when the task is enabled */
154 #endif
155 } usb_device_struct_t;
156 
157 /*******************************************************************************
158  * API
159  ******************************************************************************/
160 /*!
161  * @brief Notify the device that the controller status changed.
162  *
163  * This function is used to notify the device that the controller status changed.
164  *
165  * @param handle                 The device handle. It equals the value returned from USB_DeviceInit.
166  * @param message                The device callback message handle.
167  *
168  * @return A USB error code or kStatus_USB_Success.
169  */
170 usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg);
171 /*! @}*/
172 
173 #endif /* __USB_DEVICE_DCI_H__ */
174