]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Device/LowLevel/RNDISEthernet/RNDISEthernet.c
Squashed 'tmk_core/' changes from 8da1898..e5f9940
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Device / LowLevel / RNDISEthernet / RNDISEthernet.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 /** Main program entry point. This routine configures the hardware required by the application, then
40 * enters a loop to run the application tasks in sequence.
41 */
42 int main(void)
43 {
44 SetupHardware();
45
46 /* Webserver Initialization */
47 TCP_Init();
48 Webserver_Init();
49
50 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
51 GlobalInterruptEnable();
52
53 for (;;)
54 {
55 Ethernet_Task();
56 TCP_Task();
57 RNDIS_Task();
58 USB_USBTask();
59 }
60 }
61
62 /** Configures the board hardware and chip peripherals for the demo's functionality. */
63 void SetupHardware(void)
64 {
65 #if (ARCH == ARCH_AVR8)
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 #elif (ARCH == ARCH_XMEGA)
73 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
74 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
75 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
76
77 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
78 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
79 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
80
81 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
82 #endif
83
84 /* Hardware Initialization */
85 LEDs_Init();
86 Serial_Init(9600, false);
87 USB_Init();
88
89 /* Create a stdio stream for the serial port for stdin and stdout */
90 Serial_CreateStream(NULL);
91 }
92
93 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
94 * starts the library USB task to begin the enumeration and USB management process.
95 */
96 void EVENT_USB_Device_Connect(void)
97 {
98 /* Indicate USB enumerating */
99 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
100 }
101
102 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
103 * the status LEDs and stops all the relevant tasks.
104 */
105 void EVENT_USB_Device_Disconnect(void)
106 {
107 /* Indicate USB not ready */
108 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
109 }
110
111 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
112 * of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks.
113 */
114 void EVENT_USB_Device_ConfigurationChanged(void)
115 {
116 bool ConfigSuccess = true;
117
118 /* Setup RNDIS Data Endpoints */
119 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
120 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
121 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
122
123 /* Indicate endpoint configuration success or failure */
124 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
125 }
126
127 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
128 * the device from the USB host before passing along unhandled control requests to the library for processing
129 * internally.
130 */
131 void EVENT_USB_Device_ControlRequest(void)
132 {
133 /* Process RNDIS class commands */
134 switch (USB_ControlRequest.bRequest)
135 {
136 case RNDIS_REQ_SendEncapsulatedCommand:
137 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
138 {
139 Endpoint_ClearSETUP();
140
141 /* Read in the RNDIS message into the message buffer */
142 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer, USB_ControlRequest.wLength);
143 Endpoint_ClearIN();
144
145 /* Process the RNDIS message */
146 ProcessRNDISControlMessage();
147 }
148
149 break;
150 case RNDIS_REQ_GetEncapsulatedResponse:
151 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
152 {
153 /* Check if a response to the last message is ready */
154 if (!(MessageHeader->MessageLength))
155 {
156 /* Set the response to a single 0x00 byte to indicate that no response is ready */
157 RNDISMessageBuffer[0] = 0;
158 MessageHeader->MessageLength = 1;
159 }
160
161 Endpoint_ClearSETUP();
162
163 /* Write the message response data to the endpoint */
164 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer, MessageHeader->MessageLength);
165 Endpoint_ClearOUT();
166
167 /* Reset the message header once again after transmission */
168 MessageHeader->MessageLength = 0;
169 }
170
171 break;
172 }
173 }
174
175 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS
176 * wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper
177 * to a frame in the FrameOUT global before sending the buffer contents to the host.
178 */
179 void RNDIS_Task(void)
180 {
181 /* Select the notification endpoint */
182 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPADDR);
183
184 /* Check if a message response is ready for the host */
185 if (Endpoint_IsINReady() && ResponseReady)
186 {
187 USB_Request_Header_t Notification = (USB_Request_Header_t)
188 {
189 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
190 .bRequest = RNDIS_NOTIF_ResponseAvailable,
191 .wValue = 0,
192 .wIndex = 0,
193 .wLength = 0,
194 };
195
196 /* Indicate that a message response is ready for the host */
197 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NULL);
198
199 /* Finalize the stream transfer to send the last packet */
200 Endpoint_ClearIN();
201
202 /* Indicate a response is no longer ready */
203 ResponseReady = false;
204 }
205
206 /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */
207 if ((CurrRNDISState == RNDIS_Data_Initialized) && !(MessageHeader->MessageLength))
208 {
209 /* Create a new packet header for reading/writing */
210 RNDIS_Packet_Message_t RNDISPacketHeader;
211
212 /* Select the data OUT endpoint */
213 Endpoint_SelectEndpoint(CDC_RX_EPADDR);
214
215 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */
216 if (Endpoint_IsOUTReceived() && !(FrameIN.FrameLength))
217 {
218 /* Read in the packet message header */
219 Endpoint_Read_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
220
221 /* Stall the request if the data is too large */
222 if (RNDISPacketHeader.DataLength > ETHERNET_FRAME_SIZE_MAX)
223 {
224 Endpoint_StallTransaction();
225 return;
226 }
227
228 /* Read in the Ethernet frame into the buffer */
229 Endpoint_Read_Stream_LE(FrameIN.FrameData, RNDISPacketHeader.DataLength, NULL);
230
231 /* Finalize the stream transfer to send the last packet */
232 Endpoint_ClearOUT();
233
234 /* Store the size of the Ethernet frame */
235 FrameIN.FrameLength = RNDISPacketHeader.DataLength;
236 }
237
238 /* Select the data IN endpoint */
239 Endpoint_SelectEndpoint(CDC_TX_EPADDR);
240
241 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */
242 if (Endpoint_IsINReady() && FrameOUT.FrameLength)
243 {
244 /* Clear the packet header with all 0s so that the relevant fields can be filled */
245 memset(&RNDISPacketHeader, 0, sizeof(RNDIS_Packet_Message_t));
246
247 /* Construct the required packet header fields in the buffer */
248 RNDISPacketHeader.MessageType = REMOTE_NDIS_PACKET_MSG;
249 RNDISPacketHeader.MessageLength = (sizeof(RNDIS_Packet_Message_t) + FrameOUT.FrameLength);
250 RNDISPacketHeader.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
251 RNDISPacketHeader.DataLength = FrameOUT.FrameLength;
252
253 /* Send the packet header to the host */
254 Endpoint_Write_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
255
256 /* Send the Ethernet frame data to the host */
257 Endpoint_Write_Stream_LE(FrameOUT.FrameData, RNDISPacketHeader.DataLength, NULL);
258
259 /* Finalize the stream transfer to send the last packet */
260 Endpoint_ClearIN();
261
262 /* Indicate Ethernet OUT buffer no longer full */
263 FrameOUT.FrameLength = 0;
264 }
265 }
266 }
267
268 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing
269 * of the frame to the Ethernet processing routines.
270 */
271 void Ethernet_Task(void)
272 {
273 /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and
274 outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
275 Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
276
277 /* Device must be connected and configured for the task to run */
278 if (USB_DeviceState != DEVICE_STATE_Configured)
279 return;
280
281 /* Check if a frame has been written to the IN frame buffer */
282 if (FrameIN.FrameLength)
283 {
284 /* Indicate packet processing started */
285 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
286
287 /* Process the ethernet frame - replace this with your own Ethernet handler code as desired */
288 Ethernet_ProcessPacket();
289
290 /* Indicate packet processing complete */
291 LEDs_SetAllLEDs(LEDMASK_USB_READY);
292 }
293 }
294
Imprint / Impressum