1 /**
2  * Copyright 2024 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __USB_HOST_CDC_ECM_H__
9 #define __USB_HOST_CDC_ECM_H__
10 
11 /*!
12  * @addtogroup usb_host_cdc_ecm_drv
13  * @{
14  */
15 
16 /*******************************************************************************
17  * Includes
18  ******************************************************************************/
19 
20 /*******************************************************************************
21  * Definitions
22  ******************************************************************************/
23 /*! @brief Class-Specific Codes */
24 #define USB_HOST_CDC_ECM_CLASS_CODE    (0x02U)
25 #define USB_HOST_CDC_ECM_SUBCLASS_CODE (0x06U)
26 #define USB_HOST_CDC_ECM_PROTOCOL_CODE (0x00U)
27 
28 /*! @brief Class-Specific Request Codes for Ethernet subclass */
29 #define CDC_ECM_SET_ETHERNET_MULTICAST_FILTER                (0x40U)
30 #define CDC_ECM_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER (0x41U)
31 #define CDC_ECM_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER (0x42U)
32 #define CDC_ECM_SET_ETHERNET_PACKET_FILTER                   (0x43U)
33 #define CDC_ECM_GET_ETHERNET_STATISTIC                       (0x44U)
34 
35 /*! @brief Ethernet Packet Filter Bitmap */
36 #define CDC_ECM_ETH_PACKET_FILTER_PROMISCUOUS   (0x01U)
37 #define CDC_ECM_ETH_PACKET_FILTER_ALL_MULTICAST (0x02U)
38 #define CDC_ECM_ETH_PACKET_FILTER_DIRECTED      (0x04U)
39 #define CDC_ECM_ETH_PACKET_FILTER_BROADCAST     (0x08U)
40 #define CDC_ECM_ETH_PACKET_FILTER_MULTICAST     (0x10U)
41 
42 /*! @brief Ethernet Statistics Capabilities */
43 typedef enum _usb_host_cdc_ecm_eth_stats_cap_enum
44 {
45     XMIT_OK = 0,           /*!< Frames transmitted without errors */
46     RVC_OK,                /*!< Frames received without errors */
47     XMIT_ERROR,            /*!< Frames not transmitted, or transmitted with errors */
48     RCV_ERROR,             /*!< Frames received with errors that are not delivered to the USB host. */
49     RCV_NO_BUFFER,         /*!< Frame missed, no buffers */
50     DIRECTED_BYTES_XMIT,   /*!< Directed bytes transmitted without errors */
51     DIRECTED_FRAMES_XMIT,  /*!< Directed frames transmitted without errors */
52     MULTICAST_BYTES_XMIT,  /*!< Multicast bytes transmitted without errors */
53     MULTICAST_FRAMES_XMIT, /*!< Multicast frames transmitted without errors */
54     BROADCAST_BYTES_XMIT,  /*!< Broadcast bytes transmitted without errors */
55     BROADCAST_FRAMES_XMIT, /*!< Broadcast frames transmitted without errors */
56     DIRECTED_BYTES_RCV,    /*!< Directed bytes received without errors */
57     DIRECTED_FRAMES_RCV,   /*!< Directed frames received without errors */
58     MULTICAST_BYTES_RCV,   /*!< Multicast bytes received without errors */
59     MULTICAST_FRAMES_RCV,  /*!< Multicast frames received without errors */
60     BROADCAST_BYTES_RCV,   /*!< Broadcast bytes received without errors */
61     BROADCAST_FRAMES_RCV,  /*!< Broadcast frames received without errors */
62     RCV_CRC_ERROR, /*!< Frames received with circular redundancy check (CRC) or frame check sequence (FCS) error */
63     TRANSMIT_QUEUE_LENGTH,  /*!< Length of transmit queue */
64     RCV_ERROR_ALIGNMENT,    /*!< Frames received with alignment error */
65     XMIT_ONE_COLLISION,     /*!< Frames transmitted with one collision */
66     XMIT_MORE_COLLISIONS,   /*!< Frames transmitted with more than one collision */
67     XMIT_DEFERRED,          /*!< Frames transmitted after deferral */
68     XMIT_MAX_COLLISIONS,    /*!< Frames not transmitted due to collisions */
69     RCV_OVERRUN,            /*!< Frames not received due to overrun */
70     XMIT_UNDERRUN,          /*!< Frames not transmitted due to underrun */
71     XMIT_HEARTBEAT_FAILURE, /*!< Frames transmitted with heartbeat failure */
72     XMIT_TIMES_CRS_LOST,    /*!< Times carrier sense signal lost during transmission */
73     XMIT_LATE_COLLISIONS,   /*!< Late collisions detected */
74     ETH_STATS_CAP_COUNT     /*!< The count of Ethernet Statistics Capabilities */
75 } USB_HostCdcEcmEthStatsCap_t;
76 
77 /*! @brief Ethernet Statistics Capabilities Bitmap */
78 #define CDC_ECM_ETH_STATS_BITMAP(type) ((uint32_t)(1 << type))
79 
80 /*! @brief Ethernet Statistics Feature Selector Codes */
81 #define CDC_ECM_ETH_STATS_FEATURE_SEL_CODE(type) ((uint8_t)(type + 1))
82 
83 /*! @brief Default maximum Ethernet frame size */
84 #define CDC_ECM_FRAME_MAX_FRAMELEN (1514U)
85 
86 /*! @brief Power Management Pattern Filter Structure */
87 typedef struct
88 {
89     uint16_t MaskSize;
90     uint8_t *Mask;
91     uint8_t *Pattern;
92 } USB_HostCdcEcmPowerManagementPatternFilter_t;
93 
94 /*! @brief Parameters of Request SetEthernetMulticastFilters */
95 typedef struct
96 {
97     uint16_t filterNumSum;
98     uint8_t (*multicastAddressList)[6];
99 } USB_HostCdcEcmSetEthernetMulticastFiltersParam_t;
100 
101 /*! @brief Parameters of Request SetEthernetPowerManagementPatternFilter */
102 typedef struct
103 {
104     uint16_t filternum;
105     USB_HostCdcEcmPowerManagementPatternFilter_t *filterData;
106 } USB_HostCdcEcmSetEthernetPowerManagementPatternFilterParam_t;
107 
108 /*! @brief Parameters of Request GetEthernetPowerManagementPatternFilter */
109 typedef struct
110 {
111     uint16_t filternum;
112     uint16_t patternActive;
113 } USB_HostCdcEcmGetEthernetPowerManagementPatternFilterParam_t;
114 
115 /*! @brief Parameters of Request SetEthernetPacketFilter */
116 typedef uint16_t USB_HostCdcEcmSetEthernetPacketFilterParam_t;
117 
118 /*! @brief Parameters of Request GetEthernetStatistic */
119 typedef struct
120 {
121     uint16_t featureSelector;
122     uint32_t statistics;
123 } USB_HostCdcEcmGetEthernetStatisticParam_t;
124 
125 /*! @brief Parameters of CDC-ECM Class-Specific Request */
126 typedef union
127 {
128     USB_HostCdcEcmSetEthernetMulticastFiltersParam_t SetEthernetMulticastFilters;
129     USB_HostCdcEcmSetEthernetPowerManagementPatternFilterParam_t SetEthernetPowerManagementPatternFilter;
130     USB_HostCdcEcmGetEthernetPowerManagementPatternFilterParam_t GetEthernetPowerManagementPatternFilter;
131     USB_HostCdcEcmSetEthernetPacketFilterParam_t SetEthernetPacketFilter;
132     USB_HostCdcEcmGetEthernetStatisticParam_t GetEthernetStatistic;
133 } USB_HostCdcEcmRequestParam_t;
134 
135 /*******************************************************************************
136  * Prototypes
137  ******************************************************************************/
138 /*!
139  * @name USB CDC-ECM host class driver
140  * @{
141  */
142 
143 /*!
144  * @brief Set the Ethernet device multicast filters as specified in the sequential list of 48 bit Ethernet multicast
145  * addresses.
146  * @param classHandle The class handle.
147  * @param filterNumSum The sum of filters to be set.
148  * @param filterData The data of filters.
149  * @param callbackFn This callback is called after this function completes.
150  * @param callbackParam The parameter in the callback function.
151  * @return An error code or kStatus_USB_Success.
152  */
153 usb_status_t USB_HostCdcEcmSetEthernetMulticastFilters(usb_host_class_handle classHandle,
154                                                        uint16_t filterNumSum,
155                                                        uint8_t (*filterData)[6],
156                                                        transfer_callback_t callbackFn,
157                                                        void *callbackParam);
158 
159 /*!
160  * @brief Set up the specified Ethernet power management pattern filter as described in the data structure.
161  * @param classHandle The class handle.
162  * @param filterNum The number of the specific pattern filter to be set.
163  * @param buffer The data of pattern filters.
164  * @param bufferLength The length of pattern filters.
165  * @param callbackFn This callback is called after this function completes.
166  * @param callbackParam The parameter in the callback function.
167  * @return An error code or kStatus_USB_Success.
168  */
169 usb_status_t USB_HostCdcEcmSetEthernetPowerManagementPatternFilter(usb_host_class_handle classHandle,
170                                                                    uint16_t filterNum,
171                                                                    uint8_t *buffer,
172                                                                    uint16_t bufferLength,
173                                                                    transfer_callback_t callbackFn,
174                                                                    void *callbackParam);
175 
176 /*!
177  * @brief Retrieves the status of the specified Ethernet power management pattern filter from the device.
178  * @param classHandle The class handle.
179  * @param filterNum The number of the specific pattern filter to be set.
180  * @param buffer The pattern filter active.
181  * @param callbackFn This callback is called after this function completes.
182  * @param callbackParam The parameter in the callback function.
183  * @returns An error code or kStatus_USB_Success.
184  */
185 usb_status_t USB_HostCdcEcmGetEthernetPowerManagementPatternFilter(usb_host_class_handle classHandle,
186                                                                    uint16_t filterNum,
187                                                                    uint8_t (*buffer)[2],
188                                                                    transfer_callback_t callbackFn,
189                                                                    void *callbackParam);
190 
191 /*!
192  * @brief Configure device Ethernet packet filter settings.
193  * @param classHandle The class handle.
194  * @param packetFilterBitmap The config of the packet filter to be set.
195  * @param callbackFn This callback is called after this function completes.
196  * @param callbackParam The parameter in the callback function.
197  * @returns An error code or kStatus_USB_Success.
198  */
199 usb_status_t USB_HostCdcEcmSetEthernetPacketFilter(usb_host_class_handle classHandle,
200                                                    uint16_t packetFilterBitmap,
201                                                    transfer_callback_t callbackFn,
202                                                    void *callbackParam);
203 
204 /*!
205  * @brief Retrieve a statistic based on the feature selector.
206  * @param classHandle The class handle.
207  * @param featureSelector The ethernet statistics feature selector to be set.
208  * @param buffer The receiving address for Ethernet statistics.
209  * @param callbackFn This callback is called after this function completes.
210  * @param callbackParam The parameter in the callback function.
211  * @returns An error code or kStatus_USB_Success.
212  */
213 usb_status_t USB_HostCdcEcmGetEthernetStatistic(usb_host_class_handle classHandle,
214                                                 uint16_t featureSelector,
215                                                 uint8_t (*buffer)[4],
216                                                 transfer_callback_t callbackFn,
217                                                 void *callbackParam);
218 
219 /*!
220  * @brief Receive data.
221  * @param classHandle The class handle.
222  * @param buffer Received data.
223  * @param bufferLength The length of data.
224  * @param callbackFn This callback is called after this function completes.
225  * @param callbackParam The parameter in the callback function.
226  * @returns An error code or kStatus_USB_Success.
227  */
228 usb_status_t USB_HostCdcEcmDataRecv(usb_host_class_handle classHandle,
229                                     uint8_t *buffer,
230                                     uint32_t bufferLength,
231                                     transfer_callback_t callbackFn,
232                                     void *callbackParam);
233 
234 /*!
235  * @brief Send data.
236  * @param classHandle The class handle.
237  * @param buffer Data to be sent.
238  * @param bufferLength The length of data.
239  * @param maxSegmentSize The max segment size of the data to be sent.
240  * @param callbackFn This callback is called after this function completes.
241  * @param callbackParam The parameter in the callback function.
242  * @returns An error code or kStatus_USB_Success.
243  */
244 usb_status_t USB_HostCdcEcmDataSend(usb_host_class_handle classHandle,
245                                     uint8_t *buffer,
246                                     uint32_t bufferLength,
247                                     uint16_t maxSegmentSize,
248                                     transfer_callback_t callbackFn,
249                                     void *callbackParam);
250 
251 /*!
252  * @brief Get CDC-ECM MAC string descriptor.
253  * @param classHandle The class handle.
254  * @param index The index of string descriptor where MAC is in it.
255  * @param langID Language ID of string descriptor.
256  * @param buffer The string to be received.
257  * @param bufferLength The length of string.
258  * @param callbackFn This callback is called after this function completes.
259  * @param callbackParam The parameter in the callback function.
260  * @returns An error code or kStatus_USB_Success.
261  */
262 usb_status_t USB_HostCdcEcmGetMacStringDescriptor(usb_host_class_handle classHandle,
263                                                   uint16_t index,
264                                                   uint16_t langID,
265                                                   uint8_t *buffer,
266                                                   uint16_t bufferLength,
267                                                   transfer_callback_t callbackFn,
268                                                   void *callbackParam);
269 
270 /*!
271  * @brief Encapsulating a Power Management Pattern Filter.
272  * @param param Information of Power Management Pattern Filter.
273  * @param dataBuf The buffer to store Encapsulated Power Management Pattern Filter.
274  * @returns The length of Power Management Pattern Filter.
275  */
276 uint32_t USB_HostCdcEcmPowerManagementPatternFilterData(USB_HostCdcEcmPowerManagementPatternFilter_t *param,
277                                                         uint8_t *dataBuf);
278 
279 /*! @} */
280 /*! @} */
281 #endif
282