3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
37 #include "RNDISEthernet.h"
39 /* Project Tags, for reading out using the ButtLoad project */
40 BUTTLOADTAG(ProjName
, "LUFA RNDIS App");
41 BUTTLOADTAG(BuildTime
, __TIME__
);
42 BUTTLOADTAG(BuildDate
, __DATE__
);
43 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
);
45 /* Scheduler Task List */
48 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
49 { Task
: Ethernet_Task
, TaskStatus
: TASK_STOP
},
50 { Task
: TCP_Task
, TaskStatus
: TASK_STOP
},
51 { Task
: RNDIS_Task
, TaskStatus
: TASK_STOP
},
54 /** Main program entry point. This routine configures the hardware required by the application, then
55 * starts the scheduler to run the USB management task.
59 /* Disable watchdog if enabled by bootloader/fuses */
60 MCUSR
&= ~(1 << WDRF
);
63 /* Disable clock division */
64 clock_prescale_set(clock_div_1
);
66 /* Hardware Initialization */
68 SerialStream_Init(9600, false);
70 /* Webserver Initialization */
74 printf_P(PSTR("\r\n\r\n****** RNDIS Demo running. ******\r\n"));
76 /* Indicate USB not ready */
77 UpdateStatus(Status_USBNotReady
);
79 /* Initialize Scheduler so that it can be used */
82 /* Initialize USB Subsystem */
85 /* Scheduling - routine never returns, so put this last in the main function */
89 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
90 * starts the library USB task to begin the enumeration and USB management process.
92 EVENT_HANDLER(USB_Connect
)
94 /* Start USB management task */
95 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
97 /* Indicate USB enumerating */
98 UpdateStatus(Status_USBEnumerating
);
101 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
102 * the status LEDs and stops all the relevant tasks.
104 EVENT_HANDLER(USB_Disconnect
)
106 /* Stop running TCP/IP and USB management tasks */
107 Scheduler_SetTaskMode(RNDIS_Task
, TASK_STOP
);
108 Scheduler_SetTaskMode(Ethernet_Task
, TASK_STOP
);
109 Scheduler_SetTaskMode(TCP_Task
, TASK_STOP
);
110 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
112 /* Indicate USB not ready */
113 UpdateStatus(Status_USBNotReady
);
116 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
117 * of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks.
119 EVENT_HANDLER(USB_ConfigurationChanged
)
121 /* Setup CDC Notification, Rx and Tx Endpoints */
122 Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
123 ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
,
124 ENDPOINT_BANK_SINGLE
);
126 Endpoint_ConfigureEndpoint(CDC_TX_EPNUM
, EP_TYPE_BULK
,
127 ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
,
128 ENDPOINT_BANK_SINGLE
);
130 Endpoint_ConfigureEndpoint(CDC_RX_EPNUM
, EP_TYPE_BULK
,
131 ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
,
132 ENDPOINT_BANK_SINGLE
);
134 /* Indicate USB connected and ready */
135 UpdateStatus(Status_USBReady
);
137 /* Start TCP/IP tasks */
138 Scheduler_SetTaskMode(RNDIS_Task
, TASK_RUN
);
139 Scheduler_SetTaskMode(Ethernet_Task
, TASK_RUN
);
140 Scheduler_SetTaskMode(TCP_Task
, TASK_RUN
);
143 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
144 * control requests that are not handled internally by the USB library (including the RNDIS control commands,
145 * which set up the USB RNDIS network adapter), so that they can be handled appropriately for the application.
147 EVENT_HANDLER(USB_UnhandledControlPacket
)
149 /* Discard the unused wValue parameter */
150 Endpoint_Discard_Word();
152 /* Discard the unused wIndex parameter */
153 Endpoint_Discard_Word();
155 /* Read in the wLength parameter */
156 uint16_t wLength
= Endpoint_Read_Word_LE();
158 /* Process RNDIS class commands */
161 case REQ_SendEncapsulatedCommand
:
162 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
164 /* Clear the SETUP packet, ready for data transfer */
165 Endpoint_ClearSetupReceived();
167 /* Read in the RNDIS message into the message buffer */
168 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer
, wLength
);
170 /* Finalize the stream transfer to clear the last packet from the host */
171 Endpoint_ClearSetupIN();
173 /* Process the RNDIS message */
174 ProcessRNDISControlMessage();
178 case REQ_GetEncapsulatedResponse
:
179 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
181 /* Check if a response to the last message is ready */
182 if (!(MessageHeader
->MessageLength
))
184 /* Set the response to a single 0x00 byte to indicate that no response is ready */
185 RNDISMessageBuffer
[0] = 0;
186 MessageHeader
->MessageLength
= 1;
189 /* Check if less than the requested number of bytes to transfer */
190 if (MessageHeader
->MessageLength
< wLength
)
191 wLength
= MessageHeader
->MessageLength
;
193 /* Clear the SETUP packet, ready for data transfer */
194 Endpoint_ClearSetupReceived();
196 /* Write the message response data to the endpoint */
197 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer
, wLength
);
199 /* Finalize the stream transfer to send the last packet or clear the host abort */
200 Endpoint_ClearSetupOUT();
202 /* Reset the message header once again after transmission */
203 MessageHeader
->MessageLength
= 0;
210 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
211 * log to a serial port, or anything else that is suitable for status updates.
213 * \param CurrentStatus Current status of the system, from the RNDISEthernet_StatusCodes_t enum
215 void UpdateStatus(uint8_t CurrentStatus
)
217 uint8_t LEDMask
= LEDS_NO_LEDS
;
219 /* Set the LED mask to the appropriate LED mask based on the given status code */
220 switch (CurrentStatus
)
222 case Status_USBNotReady
:
223 LEDMask
= (LEDS_LED1
);
225 case Status_USBEnumerating
:
226 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
228 case Status_USBReady
:
229 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
231 case Status_ProcessingEthernetFrame
:
232 LEDMask
= (LEDS_LED2
| LEDS_LED3
);
236 /* Set the board LEDs to the new LED mask */
237 LEDs_SetAllLEDs(LEDMask
);
240 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS
241 * wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper
242 * to a frame in the FrameOUT global before sending the buffer contents to the host.
246 /* Select the notification endpoint */
247 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM
);
249 /* Check if a message response is ready for the host */
250 if (Endpoint_ReadWriteAllowed() && ResponseReady
)
252 USB_Notification_t Notification
= (USB_Notification_t
)
254 bmRequestType
: (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
255 bNotification
: NOTIF_RESPONSE_AVAILABLE
,
261 /* Indicate that a message response is ready for the host */
262 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
));
264 /* Finalize the stream transfer to send the last packet */
265 Endpoint_ClearCurrentBank();
267 /* Indicate a response is no longer ready */
268 ResponseReady
= false;
271 /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */
272 if ((CurrRNDISState
== RNDIS_Data_Initialized
) && !(MessageHeader
->MessageLength
))
274 /* Create a new packet header for reading/writing */
275 RNDIS_PACKET_MSG_t RNDISPacketHeader
;
277 /* Select the data OUT endpoint */
278 Endpoint_SelectEndpoint(CDC_RX_EPNUM
);
280 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */
281 if (Endpoint_ReadWriteAllowed() && !(FrameIN
.FrameInBuffer
))
283 /* Read in the packet message header */
284 Endpoint_Read_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_PACKET_MSG_t
));
286 /* Stall the request if the data is too large */
287 if (RNDISPacketHeader
.DataLength
> ETHERNET_FRAME_SIZE_MAX
)
289 Endpoint_StallTransaction();
293 /* Read in the Ethernet frame into the buffer */
294 Endpoint_Read_Stream_LE(FrameIN
.FrameData
, RNDISPacketHeader
.DataLength
);
296 /* Finalize the stream transfer to send the last packet */
297 Endpoint_ClearCurrentBank();
299 /* Store the size of the Ethernet frame */
300 FrameIN
.FrameLength
= RNDISPacketHeader
.DataLength
;
302 /* Indicate Ethernet IN buffer full */
303 FrameIN
.FrameInBuffer
= true;
306 /* Select the data IN endpoint */
307 Endpoint_SelectEndpoint(CDC_TX_EPNUM
);
309 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */
310 if (Endpoint_ReadWriteAllowed() && FrameOUT
.FrameInBuffer
)
312 /* Clear the packet header with all 0s so that the relevant fields can be filled */
313 memset(&RNDISPacketHeader
, 0, sizeof(RNDIS_PACKET_MSG_t
));
315 /* Construct the required packet header fields in the buffer */
316 RNDISPacketHeader
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
317 RNDISPacketHeader
.MessageLength
= (sizeof(RNDIS_PACKET_MSG_t
) + FrameOUT
.FrameLength
);
318 RNDISPacketHeader
.DataOffset
= (sizeof(RNDIS_PACKET_MSG_t
) - sizeof(RNDIS_Message_Header_t
));
319 RNDISPacketHeader
.DataLength
= FrameOUT
.FrameLength
;
321 /* Send the packet header to the host */
322 Endpoint_Write_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_PACKET_MSG_t
));
324 /* Send the Ethernet frame data to the host */
325 Endpoint_Write_Stream_LE(FrameOUT
.FrameData
, RNDISPacketHeader
.DataLength
);
327 /* Finalize the stream transfer to send the last packet */
328 Endpoint_ClearCurrentBank();
330 /* Indicate Ethernet OUT buffer no longer full */
331 FrameOUT
.FrameInBuffer
= false;
336 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing
337 * of the frame to the Ethernet processing routines.
341 /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and
342 outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
343 Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
345 /* Check if a frame has been written to the IN frame buffer */
346 if (FrameIN
.FrameInBuffer
)
348 /* Indicate packet processing started */
349 UpdateStatus(Status_ProcessingEthernetFrame
);
351 /* Process the ethernet frame - replace this with your own Ethernet handler code as desired */
352 Ethernet_ProcessPacket();
354 /* Indicate packet processing complete */
355 UpdateStatus(Status_USBReady
);