3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  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. 
  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  *  USB Host Mode management functions and variables. This file contains the LUFA code required to 
  34  *  manage the USB RNDIS host mode. 
  37 #include "USBHostMode.h" 
  39 /** LUFA RNDIS Class driver interface configuration and state information. This structure is 
  40  *  passed to all RNDIS Class driver functions, so that multiple instances of the same class 
  41  *  within a device can be differentiated from one another. 
  43 USB_ClassInfo_RNDIS_Host_t Ethernet_RNDIS_Interface_Host 
= 
  47                                 .DataINPipeNumber           
= 1, 
  48                                 .DataINPipeDoubleBank       
= false, 
  50                                 .DataOUTPipeNumber          
= 2, 
  51                                 .DataOUTPipeDoubleBank      
= false, 
  53                                 .NotificationPipeNumber     
= 3, 
  54                                 .NotificationPipeDoubleBank 
= false, 
  56                                 .HostMaxPacketSize          
= UIP_CONF_BUFFER_SIZE
, 
  61 /** USB host mode management task. This function manages the RNDIS Host class driver and uIP stack when the device is 
  62  *  initialized in USB host mode. 
  64 void USBHostMode_USBTask(void) 
  66         if (USB_CurrentMode 
!= USB_MODE_Host
) 
  69         switch (USB_HostState
) 
  71                 case HOST_STATE_Addressed
: 
  72                         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  74                         uint16_t ConfigDescriptorSize
; 
  75                         uint8_t  ConfigDescriptorData
[512]; 
  77                         if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
, 
  78                                                                                                    sizeof(ConfigDescriptorData
)) != HOST_GETCONFIG_Successful
) 
  80                                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
  81                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
  85                         if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface_Host
, 
  86                                                                                   ConfigDescriptorSize
, ConfigDescriptorData
) != RNDIS_ENUMERROR_NoError
) 
  88                                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
  89                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
  93                         if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful
) 
  95                                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
  96                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 100                         if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface_Host
) != HOST_SENDCONTROL_Successful
) 
 102                                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 103                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 107                         uint32_t PacketFilter 
= (REMOTE_NDIS_PACKET_DIRECTED 
| REMOTE_NDIS_PACKET_BROADCAST
); 
 108                         if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface_Host
, OID_GEN_CURRENT_PACKET_FILTER
, 
 109                                                                                         &PacketFilter
, sizeof(PacketFilter
)) != HOST_SENDCONTROL_Successful
) 
 111                                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 112                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 116                         if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface_Host
, OID_802_3_CURRENT_ADDRESS
, 
 117                                                                                           &MACAddress
, sizeof(MACAddress
)) != HOST_SENDCONTROL_Successful
) 
 119                                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 120                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 124                         /* Initialize uIP stack */ 
 125                         uIPManagement_Init(); 
 127                         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 128                         USB_HostState 
= HOST_STATE_Configured
; 
 130                 case HOST_STATE_Configured
: 
 131                         uIPManagement_ManageNetwork(); 
 136         RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface_Host
); 
 139 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and 
 140  *  starts the library USB task to begin the enumeration and USB management process. 
 142 void EVENT_USB_Host_DeviceAttached(void) 
 144         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 147 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and 
 148  *  stops the library USB task management process. 
 150 void EVENT_USB_Host_DeviceUnattached(void) 
 152         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 155 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully 
 156  *  enumerated by the host and is now ready to be used by the application. 
 158 void EVENT_USB_Host_DeviceEnumerationComplete(void) 
 160         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 163 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ 
 164 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) 
 168         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 172 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while 
 173  *  enumerating an attached USB device. 
 175 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
) 
 177         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);