Upgrade doxygen configuration files to the latest version.
[pub/lufa.git] / Demos / Device / LowLevel / RNDISEthernet / RNDISEthernet.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2021.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2021 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaims all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Main source file for the RNDISEthernet demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "RNDISEthernet.h"
38
39 /** Global to store the incoming frame from the host before it is processed by the device. */
40 static Ethernet_Frame_Info_t FrameIN;
41
42 /** Global to store the outgoing frame created in the device before it is sent to the host. */
43 static Ethernet_Frame_Info_t FrameOUT;
44
45 /** Main program entry point. This routine configures the hardware required by the application, then
46 * enters a loop to run the application tasks in sequence.
47 */
48 int main(void)
49 {
50 SetupHardware();
51
52 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
53 GlobalInterruptEnable();
54
55 for (;;)
56 {
57 Ethernet_Task();
58 RNDIS_Task();
59 USB_USBTask();
60 }
61 }
62
63 /** Configures the board hardware and chip peripherals for the demo's functionality. */
64 void SetupHardware(void)
65 {
66 #if (ARCH == ARCH_AVR8)
67 /* Disable watchdog if enabled by bootloader/fuses */
68 MCUSR &= ~(1 << WDRF);
69 wdt_disable();
70
71 /* Disable clock division */
72 clock_prescale_set(clock_div_1);
73 #elif (ARCH == ARCH_XMEGA)
74 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
75 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
76 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
77
78 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
79 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
80 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
81
82 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
83 #endif
84
85 /* Hardware Initialization */
86 LEDs_Init();
87 Serial_Init(9600, false);
88 USB_Init();
89
90 /* Create a stdio stream for the serial port for stdin and stdout */
91 Serial_CreateStream(NULL);
92 }
93
94 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
95 * starts the library USB task to begin the enumeration and USB management process.
96 */
97 void EVENT_USB_Device_Connect(void)
98 {
99 /* Indicate USB enumerating */
100 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
101 }
102
103 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
104 * the status LEDs and stops all the relevant tasks.
105 */
106 void EVENT_USB_Device_Disconnect(void)
107 {
108 /* Indicate USB not ready */
109 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
110 }
111
112 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
113 * of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks.
114 */
115 void EVENT_USB_Device_ConfigurationChanged(void)
116 {
117 bool ConfigSuccess = true;
118
119 /* Setup RNDIS Data Endpoints */
120 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
121 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
122 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
123
124 /* Indicate endpoint configuration success or failure */
125 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
126 }
127
128 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
129 * the device from the USB host before passing along unhandled control requests to the library for processing
130 * internally.
131 */
132 void EVENT_USB_Device_ControlRequest(void)
133 {
134 /* Send MS OS Compatibility descriptor if requested by the host. */
135 CheckIfMSCompatibilityDescriptorRequest();
136
137 /* Process RNDIS class commands */
138 switch (USB_ControlRequest.bRequest)
139 {
140 case RNDIS_REQ_SendEncapsulatedCommand:
141 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
142 {
143 Endpoint_ClearSETUP();
144
145 /* Read in the RNDIS message into the message buffer */
146 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer, USB_ControlRequest.wLength);
147 Endpoint_ClearIN();
148
149 /* Process the RNDIS message */
150 ProcessRNDISControlMessage();
151 }
152
153 break;
154 case RNDIS_REQ_GetEncapsulatedResponse:
155 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
156 {
157 /* Check if a response to the last message is ready */
158 if (!(MessageHeader->MessageLength))
159 {
160 /* Set the response to a single 0x00 byte to indicate that no response is ready */
161 RNDISMessageBuffer[0] = 0;
162 MessageHeader->MessageLength = 1;
163 }
164
165 Endpoint_ClearSETUP();
166
167 /* Write the message response data to the endpoint */
168 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer, MessageHeader->MessageLength);
169 Endpoint_ClearOUT();
170
171 /* Reset the message header once again after transmission */
172 MessageHeader->MessageLength = 0;
173 }
174
175 break;
176 }
177 }
178
179 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS
180 * wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper
181 * to a frame in the FrameOUT global before sending the buffer contents to the host.
182 */
183 void RNDIS_Task(void)
184 {
185 /* Select the notification endpoint */
186 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPADDR);
187
188 /* Check if a message response is ready for the host */
189 if (Endpoint_IsINReady() && ResponseReady)
190 {
191 USB_Request_Header_t Notification = (USB_Request_Header_t)
192 {
193 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
194 .bRequest = RNDIS_NOTIF_ResponseAvailable,
195 .wValue = 0,
196 .wIndex = 0,
197 .wLength = 0,
198 };
199
200 /* Indicate that a message response is ready for the host */
201 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NULL);
202
203 /* Finalize the stream transfer to send the last packet */
204 Endpoint_ClearIN();
205
206 /* Indicate a response is no longer ready */
207 ResponseReady = false;
208 }
209
210 /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */
211 if ((CurrRNDISState == RNDIS_Data_Initialized) && !(MessageHeader->MessageLength))
212 {
213 /* Create a new packet header for reading/writing */
214 RNDIS_Packet_Message_t RNDISPacketHeader;
215
216 /* Select the data OUT endpoint */
217 Endpoint_SelectEndpoint(CDC_RX_EPADDR);
218
219 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */
220 if (Endpoint_IsOUTReceived() && !(FrameIN.FrameLength))
221 {
222 /* Read in the packet message header */
223 Endpoint_Read_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
224
225 /* Stall the request if the data is too large */
226 if (RNDISPacketHeader.DataLength > ETHERNET_FRAME_SIZE_MAX)
227 {
228 Endpoint_StallTransaction();
229 return;
230 }
231
232 /* Read in the Ethernet frame into the buffer */
233 Endpoint_Read_Stream_LE(FrameIN.FrameData, RNDISPacketHeader.DataLength, NULL);
234
235 /* Finalize the stream transfer to send the last packet */
236 Endpoint_ClearOUT();
237
238 /* Store the size of the Ethernet frame */
239 FrameIN.FrameLength = RNDISPacketHeader.DataLength;
240 }
241
242 /* Select the data IN endpoint */
243 Endpoint_SelectEndpoint(CDC_TX_EPADDR);
244
245 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */
246 if (Endpoint_IsINReady() && FrameOUT.FrameLength)
247 {
248 /* Clear the packet header with all 0s so that the relevant fields can be filled */
249 memset(&RNDISPacketHeader, 0, sizeof(RNDIS_Packet_Message_t));
250
251 /* Construct the required packet header fields in the buffer */
252 RNDISPacketHeader.MessageType = REMOTE_NDIS_PACKET_MSG;
253 RNDISPacketHeader.MessageLength = (sizeof(RNDIS_Packet_Message_t) + FrameOUT.FrameLength);
254 RNDISPacketHeader.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
255 RNDISPacketHeader.DataLength = FrameOUT.FrameLength;
256
257 /* Send the packet header to the host */
258 Endpoint_Write_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
259
260 /* Send the Ethernet frame data to the host */
261 Endpoint_Write_Stream_LE(FrameOUT.FrameData, RNDISPacketHeader.DataLength, NULL);
262
263 /* Finalize the stream transfer to send the last packet */
264 Endpoint_ClearIN();
265
266 /* Indicate Ethernet OUT buffer no longer full */
267 FrameOUT.FrameLength = 0;
268 }
269 }
270 }
271
272 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing
273 * of the frame to the Ethernet processing routines.
274 */
275 void Ethernet_Task(void)
276 {
277 /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and
278 outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
279 Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
280
281 /* Device must be connected and configured for the task to run */
282 if (USB_DeviceState != DEVICE_STATE_Configured)
283 return;
284
285 /* Check if a frame has been written to the IN frame buffer */
286 if (FrameIN.FrameLength)
287 {
288 /* Indicate packet processing started */
289 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
290
291 // TODO: Process FrameIN here, and optionally fill FrameOUT.
292
293 /* Indicate packet processing complete */
294 LEDs_SetAllLEDs(LEDMASK_USB_READY);
295 }
296 }
297