3      Copyright (C) Dean Camera, 2018. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2018  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 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 
  33  *  USB Device Mode management functions and variables. This file contains the LUFA code required to 
  34  *  manage the USB Mass Storage device mode. 
  37 #include "USBDeviceMode.h" 
  39 /** Message buffer for RNDIS messages processed by the RNDIS device class driver. */ 
  40 static uint8_t RNDIS_Message_Buffer
[192]; 
  42 /** LUFA RNDIS Class driver interface configuration and state information. This structure is 
  43  *  passed to all RNDIS Class driver functions, so that multiple instances of the same class 
  44  *  within a device can be differentiated from one another. 
  46 USB_ClassInfo_RNDIS_Device_t Ethernet_RNDIS_Interface_Device 
= 
  50                                 .ControlInterfaceNumber         
= INTERFACE_ID_CDC_CCI
, 
  53                                                 .Address                
= CDC_TX_EPADDR
, 
  54                                                 .Size                   
= CDC_TXRX_EPSIZE
, 
  59                                                 .Address                
= CDC_RX_EPADDR
, 
  60                                                 .Size                   
= CDC_TXRX_EPSIZE
, 
  63                                 .NotificationEndpoint           
= 
  65                                                 .Address                
= CDC_NOTIFICATION_EPADDR
, 
  66                                                 .Size                   
= CDC_NOTIFICATION_EPSIZE
, 
  69                                 .AdapterVendorDescription       
= "LUFA RNDIS Adapter", 
  70                                 .AdapterMACAddress              
= {{0x02, 0x00, 0x02, 0x00, 0x02, 0x00}}, 
  71                                 .MessageBuffer                  
= RNDIS_Message_Buffer
, 
  72                                 .MessageBufferLength            
= sizeof(RNDIS_Message_Buffer
), 
  76 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is 
  77  *  passed to all Mass Storage Class driver functions, so that multiple instances of the same class 
  78  *  within a device can be differentiated from one another. 
  80 USB_ClassInfo_MS_Device_t Disk_MS_Interface 
= 
  84                                 .InterfaceNumber                
= INTERFACE_ID_MassStorage
, 
  87                                                 .Address                
= MASS_STORAGE_IN_EPADDR
, 
  88                                                 .Size                   
= MASS_STORAGE_IO_EPSIZE
, 
  93                                                 .Address                
= MASS_STORAGE_OUT_EPADDR
, 
  94                                                 .Size                   
= MASS_STORAGE_IO_EPSIZE
, 
 102 /** USB device mode management task. This function manages the Mass Storage Device class driver when the device is 
 103  *  initialized in USB device mode. 
 105 void USBDeviceMode_USBTask(void) 
 107         if (USB_CurrentMode 
!= USB_MODE_Device
) 
 110         uIPManagement_ManageNetwork(); 
 112         RNDIS_Device_USBTask(&Ethernet_RNDIS_Interface_Device
); 
 113         MS_Device_USBTask(&Disk_MS_Interface
); 
 116 /** Event handler for the library USB Connection event. */ 
 117 void EVENT_USB_Device_Connect(void) 
 119         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 121         uIPManagement_Init(); 
 124 /** Event handler for the library USB Disconnection event. */ 
 125 void EVENT_USB_Device_Disconnect(void) 
 127         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 130 /** Event handler for the library USB Configuration Changed event. */ 
 131 void EVENT_USB_Device_ConfigurationChanged(void) 
 133         bool ConfigSuccess 
= true; 
 135         ConfigSuccess 
&= RNDIS_Device_ConfigureEndpoints(&Ethernet_RNDIS_Interface_Device
); 
 136         ConfigSuccess 
&= MS_Device_ConfigureEndpoints(&Disk_MS_Interface
); 
 138         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 141 /** Event handler for the library USB Control Request reception event. */ 
 142 void EVENT_USB_Device_ControlRequest(void) 
 144         /* Send MS OS Compatibility descriptor if requested by the host. */ 
 145         CheckIfMSCompatibilityDescriptorRequest(); 
 147         RNDIS_Device_ProcessControlRequest(&Ethernet_RNDIS_Interface_Device
); 
 148         MS_Device_ProcessControlRequest(&Disk_MS_Interface
); 
 151 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed. 
 153  *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced 
 155 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 159         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 160         CommandSuccess 
= SCSI_DecodeSCSICommand(MSInterfaceInfo
); 
 161         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 163         return CommandSuccess
;