Converted device mode low-level demos to schedulerless.
[pub/USBasp.git] / Demos / Device / LowLevel / RNDISEthernet / RNDISEthernet.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 disclaim 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 /** Main program entry point. This routine configures the hardware required by the application, then
40 * starts the scheduler to run the USB management task.
41 */
42 int main(void)
43 {
44 SetupHardware();
45
46 /* Webserver Initialization */
47 TCP_Init();
48 Webserver_Init();
49
50 printf_P(PSTR("\r\n\r\n****** RNDIS Demo running. ******\r\n"));
51
52 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
53
54 for (;;)
55 {
56 Ethernet_Task();
57 TCP_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 /* Disable watchdog if enabled by bootloader/fuses */
67 MCUSR &= ~(1 << WDRF);
68 wdt_disable();
69
70 /* Disable clock division */
71 clock_prescale_set(clock_div_1);
72
73 /* Hardware Initialization */
74 LEDs_Init();
75 SerialStream_Init(9600, false);
76 USB_Init();
77 }
78
79 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
80 * starts the library USB task to begin the enumeration and USB management process.
81 */
82 void EVENT_USB_Connect(void)
83 {
84 /* Indicate USB enumerating */
85 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
86 }
87
88 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
89 * the status LEDs and stops all the relevant tasks.
90 */
91 void EVENT_USB_Disconnect(void)
92 {
93 /* Indicate USB not ready */
94 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
95 }
96
97 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
98 * of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks.
99 */
100 void EVENT_USB_ConfigurationChanged(void)
101 {
102 /* Setup CDC Notification, Rx and Tx Endpoints */
103 Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK,
104 ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
105 ENDPOINT_BANK_SINGLE);
106
107 Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK,
108 ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
109 ENDPOINT_BANK_SINGLE);
110
111 Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
112 ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
113 ENDPOINT_BANK_SINGLE);
114
115 /* Indicate USB connected and ready */
116 LEDs_SetAllLEDs(LEDMASK_USB_READY);
117 }
118
119 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
120 * control requests that are not handled internally by the USB library (including the RNDIS control commands,
121 * which set up the USB RNDIS network adapter), so that they can be handled appropriately for the application.
122 */
123 void EVENT_USB_UnhandledControlPacket(void)
124 {
125 /* Process RNDIS class commands */
126 switch (USB_ControlRequest.bRequest)
127 {
128 case REQ_SendEncapsulatedCommand:
129 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
130 {
131 /* Clear the SETUP packet, ready for data transfer */
132 Endpoint_ClearSETUP();
133
134 /* Read in the RNDIS message into the message buffer */
135 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer, USB_ControlRequest.wLength);
136
137 /* Finalize the stream transfer to clear the last packet from the host */
138 Endpoint_ClearIN();
139
140 /* Process the RNDIS message */
141 ProcessRNDISControlMessage();
142 }
143
144 break;
145 case REQ_GetEncapsulatedResponse:
146 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
147 {
148 /* Clear the SETUP packet, ready for data transfer */
149 Endpoint_ClearSETUP();
150
151 /* Check if a response to the last message is ready */
152 if (!(MessageHeader->MessageLength))
153 {
154 /* Set the response to a single 0x00 byte to indicate that no response is ready */
155 RNDISMessageBuffer[0] = 0;
156 MessageHeader->MessageLength = 1;
157 }
158
159 /* Write the message response data to the endpoint */
160 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer, MessageHeader->MessageLength);
161
162 /* Finalize the stream transfer to send the last packet or clear the host abort */
163 Endpoint_ClearOUT();
164
165 /* Reset the message header once again after transmission */
166 MessageHeader->MessageLength = 0;
167 }
168
169 break;
170 }
171 }
172
173 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS
174 * wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper
175 * to a frame in the FrameOUT global before sending the buffer contents to the host.
176 */
177 void RNDIS_Task(void)
178 {
179 /* Select the notification endpoint */
180 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM);
181
182 /* Check if a message response is ready for the host */
183 if (Endpoint_IsINReady() && ResponseReady)
184 {
185 USB_Notification_t Notification = (USB_Notification_t)
186 {
187 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
188 .bNotification = NOTIF_RESPONSE_AVAILABLE,
189 .wValue = 0,
190 .wIndex = 0,
191 .wLength = 0,
192 };
193
194 /* Indicate that a message response is ready for the host */
195 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification));
196
197 /* Finalize the stream transfer to send the last packet */
198 Endpoint_ClearIN();
199
200 /* Indicate a response is no longer ready */
201 ResponseReady = false;
202 }
203
204 /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */
205 if ((CurrRNDISState == RNDIS_Data_Initialized) && !(MessageHeader->MessageLength))
206 {
207 /* Create a new packet header for reading/writing */
208 RNDIS_PACKET_MSG_t RNDISPacketHeader;
209
210 /* Select the data OUT endpoint */
211 Endpoint_SelectEndpoint(CDC_RX_EPNUM);
212
213 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */
214 if (Endpoint_IsOUTReceived() && !(FrameIN.FrameInBuffer))
215 {
216 /* Read in the packet message header */
217 Endpoint_Read_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_PACKET_MSG_t));
218
219 /* Stall the request if the data is too large */
220 if (RNDISPacketHeader.DataLength > ETHERNET_FRAME_SIZE_MAX)
221 {
222 Endpoint_StallTransaction();
223 return;
224 }
225
226 /* Read in the Ethernet frame into the buffer */
227 Endpoint_Read_Stream_LE(FrameIN.FrameData, RNDISPacketHeader.DataLength);
228
229 /* Finalize the stream transfer to send the last packet */
230 Endpoint_ClearOUT();
231
232 /* Store the size of the Ethernet frame */
233 FrameIN.FrameLength = RNDISPacketHeader.DataLength;
234
235 /* Indicate Ethernet IN buffer full */
236 FrameIN.FrameInBuffer = true;
237 }
238
239 /* Select the data IN endpoint */
240 Endpoint_SelectEndpoint(CDC_TX_EPNUM);
241
242 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */
243 if (Endpoint_IsINReady() && FrameOUT.FrameInBuffer)
244 {
245 /* Clear the packet header with all 0s so that the relevant fields can be filled */
246 memset(&RNDISPacketHeader, 0, sizeof(RNDIS_PACKET_MSG_t));
247
248 /* Construct the required packet header fields in the buffer */
249 RNDISPacketHeader.MessageType = REMOTE_NDIS_PACKET_MSG;
250 RNDISPacketHeader.MessageLength = (sizeof(RNDIS_PACKET_MSG_t) + FrameOUT.FrameLength);
251 RNDISPacketHeader.DataOffset = (sizeof(RNDIS_PACKET_MSG_t) - sizeof(RNDIS_Message_Header_t));
252 RNDISPacketHeader.DataLength = FrameOUT.FrameLength;
253
254 /* Send the packet header to the host */
255 Endpoint_Write_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_PACKET_MSG_t));
256
257 /* Send the Ethernet frame data to the host */
258 Endpoint_Write_Stream_LE(FrameOUT.FrameData, RNDISPacketHeader.DataLength);
259
260 /* Finalize the stream transfer to send the last packet */
261 Endpoint_ClearIN();
262
263 /* Indicate Ethernet OUT buffer no longer full */
264 FrameOUT.FrameInBuffer = false;
265 }
266 }
267 }
268
269 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing
270 * of the frame to the Ethernet processing routines.
271 */
272 void Ethernet_Task(void)
273 {
274 /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and
275 outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
276 Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
277
278 /* Check if a frame has been written to the IN frame buffer */
279 if (FrameIN.FrameInBuffer)
280 {
281 /* Indicate packet processing started */
282 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
283
284 /* Process the ethernet frame - replace this with your own Ethernet handler code as desired */
285 Ethernet_ProcessPacket();
286
287 /* Indicate packet processing complete */
288 LEDs_SetAllLEDs(LEDMASK_USB_READY);
289 }
290 }