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 /** 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. 
  46         /* Webserver Initialization */ 
  50         printf_P(PSTR("\r\n\r\n****** RNDIS Demo running. ******\r\n")); 
  52         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  63 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  64 void SetupHardware(void) 
  66         /* Disable watchdog if enabled by bootloader/fuses */ 
  67         MCUSR 
&= ~(1 << WDRF
); 
  70         /* Disable clock division */ 
  71         clock_prescale_set(clock_div_1
); 
  73         /* Hardware Initialization */ 
  75         SerialStream_Init(9600, false); 
  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. 
  82 void EVENT_USB_Device_Connect(void) 
  84         /* Indicate USB enumerating */ 
  85         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  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. 
  91 void EVENT_USB_Device_Disconnect(void) 
  93         /* Indicate USB not ready */ 
  94         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  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. 
 100 void EVENT_USB_Device_ConfigurationChanged(void) 
 102         /* Indicate USB connected and ready */ 
 103         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 105         /* Setup CDC Notification, Rx and Tx Endpoints */ 
 106         if (!(Endpoint_ConfigureEndpoint(CDC_TX_EPNUM
, EP_TYPE_BULK
, 
 107                                              ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
, 
 108                                          ENDPOINT_BANK_SINGLE
))) 
 110                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 113         if (!(Endpoint_ConfigureEndpoint(CDC_RX_EPNUM
, EP_TYPE_BULK
, 
 114                                              ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
, 
 115                                          ENDPOINT_BANK_SINGLE
))) 
 117                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 120         if (!(Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
, 
 121                                              ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
, 
 122                                          ENDPOINT_BANK_SINGLE
))) 
 124                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 128 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific 
 129  *  control requests that are not handled internally by the USB library (including the RNDIS control commands, 
 130  *  which set up the USB RNDIS network adapter), so that they can be handled appropriately for the application. 
 132 void EVENT_USB_Device_UnhandledControlRequest(void) 
 134         /* Process RNDIS class commands */ 
 135         switch (USB_ControlRequest
.bRequest
) 
 137                 case REQ_SendEncapsulatedCommand
: 
 138                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 140                                 /* Clear the SETUP packet, ready for data transfer */ 
 141                                 Endpoint_ClearSETUP(); 
 143                                 /* Read in the RNDIS message into the message buffer */ 
 144                                 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer
, USB_ControlRequest
.wLength
); 
 146                                 /* Finalize the stream transfer to clear the last packet from the host */ 
 149                                 /* Process the RNDIS message */ 
 150                                 ProcessRNDISControlMessage(); 
 154                 case REQ_GetEncapsulatedResponse
: 
 155                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 157                                 /* Clear the SETUP packet, ready for data transfer */ 
 158                                 Endpoint_ClearSETUP(); 
 160                                 /* Check if a response to the last message is ready */ 
 161                                 if (!(MessageHeader
->MessageLength
)) 
 163                                         /* Set the response to a single 0x00 byte to indicate that no response is ready */ 
 164                                         RNDISMessageBuffer
[0] = 0; 
 165                                         MessageHeader
->MessageLength 
= 1; 
 168                                 /* Write the message response data to the endpoint */ 
 169                                 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer
, MessageHeader
->MessageLength
); 
 171                                 /* Finalize the stream transfer to send the last packet or clear the host abort */ 
 174                                 /* Reset the message header once again after transmission */ 
 175                                 MessageHeader
->MessageLength 
= 0; 
 182 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS 
 183  *  wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper 
 184  *  to a frame in the FrameOUT global before sending the buffer contents to the host. 
 186 void RNDIS_Task(void) 
 188         /* Select the notification endpoint */ 
 189         Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM
); 
 191         /* Check if a message response is ready for the host */ 
 192         if (Endpoint_IsINReady() && ResponseReady
) 
 194                 USB_Notification_t Notification 
= (USB_Notification_t
) 
 196                                 .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 197                                 .bNotification 
= NOTIF_RESPONSE_AVAILABLE
, 
 203                 /* Indicate that a message response is ready for the host */ 
 204                 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
)); 
 206                 /* Finalize the stream transfer to send the last packet */ 
 209                 /* Indicate a response is no longer ready */ 
 210                 ResponseReady 
= false; 
 213         /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */ 
 214         if ((CurrRNDISState 
== RNDIS_Data_Initialized
) && !(MessageHeader
->MessageLength
)) 
 216                 /* Create a new packet header for reading/writing */ 
 217                 RNDIS_Packet_Message_t RNDISPacketHeader
; 
 219                 /* Select the data OUT endpoint */ 
 220                 Endpoint_SelectEndpoint(CDC_RX_EPNUM
); 
 222                 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */ 
 223                 if (Endpoint_IsOUTReceived() && !(FrameIN
.FrameInBuffer
)) 
 225                         /* Read in the packet message header */ 
 226                         Endpoint_Read_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_Packet_Message_t
)); 
 228                         /* Stall the request if the data is too large */ 
 229                         if (RNDISPacketHeader
.DataLength 
> ETHERNET_FRAME_SIZE_MAX
) 
 231                                 Endpoint_StallTransaction(); 
 235                         /* Read in the Ethernet frame into the buffer */ 
 236                         Endpoint_Read_Stream_LE(FrameIN
.FrameData
, RNDISPacketHeader
.DataLength
); 
 238                         /* Finalize the stream transfer to send the last packet */ 
 241                         /* Store the size of the Ethernet frame */ 
 242                         FrameIN
.FrameLength 
= RNDISPacketHeader
.DataLength
; 
 244                         /* Indicate Ethernet IN buffer full */ 
 245                         FrameIN
.FrameInBuffer 
= true; 
 248                 /* Select the data IN endpoint */ 
 249                 Endpoint_SelectEndpoint(CDC_TX_EPNUM
); 
 251                 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */ 
 252                 if (Endpoint_IsINReady() && FrameOUT
.FrameInBuffer
) 
 254                         /* Clear the packet header with all 0s so that the relevant fields can be filled */ 
 255                         memset(&RNDISPacketHeader
, 0, sizeof(RNDIS_Packet_Message_t
)); 
 257                         /* Construct the required packet header fields in the buffer */ 
 258                         RNDISPacketHeader
.MessageType   
= REMOTE_NDIS_PACKET_MSG
; 
 259                         RNDISPacketHeader
.MessageLength 
= (sizeof(RNDIS_Packet_Message_t
) + FrameOUT
.FrameLength
); 
 260                         RNDISPacketHeader
.DataOffset    
= (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
)); 
 261                         RNDISPacketHeader
.DataLength    
= FrameOUT
.FrameLength
; 
 263                         /* Send the packet header to the host */ 
 264                         Endpoint_Write_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_Packet_Message_t
)); 
 266                         /* Send the Ethernet frame data to the host */ 
 267                         Endpoint_Write_Stream_LE(FrameOUT
.FrameData
, RNDISPacketHeader
.DataLength
); 
 269                         /* Finalize the stream transfer to send the last packet */ 
 272                         /* Indicate Ethernet OUT buffer no longer full */ 
 273                         FrameOUT
.FrameInBuffer 
= false; 
 278 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing 
 279  *  of the frame to the Ethernet processing routines. 
 281 void Ethernet_Task(void) 
 283         /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and 
 284            outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single 
 285            Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */ 
 287         /* Device must be connected and configured for the task to run */ 
 288         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 291         /* Check if a frame has been written to the IN frame buffer */ 
 292         if (FrameIN
.FrameInBuffer
) 
 294                 /* Indicate packet processing started */ 
 295                 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 297                 /* Process the ethernet frame - replace this with your own Ethernet handler code as desired */ 
 298                 Ethernet_ProcessPacket(); 
 300                 /* Indicate packet processing complete */ 
 301                 LEDs_SetAllLEDs(LEDMASK_USB_READY
);