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 #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY)) 
  34 volatile uint8_t USB_CurrentMode 
= USB_MODE_NONE
; 
  37 #if !defined(USE_STATIC_OPTIONS) 
  38 volatile uint8_t USB_Options
; 
  42                #if defined(USB_CAN_BE_BOTH) 
  46                #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS)) 
  48                #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS)) 
  52                #if !defined(USE_STATIC_OPTIONS) 
  57         #if defined(USB_CAN_BE_BOTH) 
  58         USB_CurrentMode 
= Mode
; 
  61         #if !defined(USE_STATIC_OPTIONS) 
  62         USB_Options 
= Options
; 
  65         #if defined(USB_CAN_BE_HOST) 
  66         USB_ControlPipeSize 
= PIPE_CONTROLPIPE_DEFAULT_SIZE
; 
  69         #if defined(USB_DEVICE_ONLY) && (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)) 
  70         UHWCON 
|= (1 << UIMOD
); 
  71         #elif defined(USB_HOST_ONLY) 
  72         UHWCON 
&= ~(1 << UIMOD
); 
  73         #elif defined(USB_CAN_BE_BOTH) 
  74         if (Mode 
== USB_MODE_UID
) 
  76                 UHWCON 
|=  (1 << UIDE
); 
  78                 USB_INT_Clear(USB_INT_IDTI
); 
  79                 USB_INT_Enable(USB_INT_IDTI
); 
  81                 USB_CurrentMode 
= USB_GetUSBModeFromUID(); 
  83         else if (Mode 
== USB_MODE_DEVICE
) 
  85                 UHWCON 
|=  (1 << UIMOD
); 
  87         else if (Mode 
== USB_MODE_HOST
)                  
  89                 UHWCON 
&= ~(1 << UIMOD
); 
  93                 EVENT_USB_InitFailure(USB_INITERROR_NoUSBModeSpecified
); 
 100         #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) 
 104         USB_IsInitialized 
= true; 
 109 void USB_ShutDown(void) 
 111         #if defined(USB_CAN_BE_DEVICE) 
 112         if (USB_DeviceState 
!= DEVICE_STATE_Unattached
) 
 113           EVENT_USB_Disconnect(); 
 116         #if defined(USB_CAN_BE_HOST) 
 117         if (USB_HostState 
!= HOST_STATE_Unattached
) 
 118           EVENT_USB_Disconnect(); 
 121         USB_ResetInterface(); 
 123         USB_Controller_Disable(); 
 125         if (!(USB_Options 
& USB_OPT_MANUAL_PLL
)) 
 128         #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) 
 132         #if defined(USB_CAN_BE_BOTH) 
 133         UHWCON 
&= ~(1 << UIDE
); 
 136         USB_IsInitialized 
= false; 
 138         #if defined(CAN_BE_BOTH) 
 139         USB_CurrentMode 
= USB_MODE_NONE
; 
 143 void USB_ResetInterface(void) 
 145         USB_INT_DisableAllInterrupts(); 
 146         USB_INT_ClearAllInterrupts(); 
 148         #if defined(USB_CAN_BE_HOST) 
 149         USB_HostState   
= HOST_STATE_Unattached
; 
 152         #if defined(USB_CAN_BE_DEVICE) 
 153         USB_DeviceState 
= DEVICE_STATE_Unattached
; 
 154         USB_ConfigurationNumber  
= 0; 
 155         USB_RemoteWakeupEnabled  
= false; 
 156         USB_CurrentlySelfPowered 
= false; 
 159         if (!(USB_Options 
& USB_OPT_MANUAL_PLL
)) 
 161                 #if defined(USB_SERIES_4_AVR) 
 162                 PLLFRQ 
= ((1 << PLLUSB
) | (1 << PDIV3
) | (1 << PDIV1
)); 
 166                 while (!(USB_PLL_IsReady())); 
 169         USB_Controller_Reset(); 
 171         #if defined(USB_CAN_BE_BOTH) 
 172         if (UHWCON 
& (1 << UIDE
)) 
 174                 USB_INT_Clear(USB_INT_IDTI
); 
 175                 USB_INT_Enable(USB_INT_IDTI
); 
 176                 USB_CurrentMode 
= USB_GetUSBModeFromUID(); 
 180         if (!(USB_Options 
& USB_OPT_REG_DISABLED
)) 
 187         #if (defined(USB_CAN_BE_DEVICE) && (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))) 
 188         if (USB_CurrentMode 
== USB_MODE_DEVICE
) 
 190                 if (USB_Options 
& USB_DEVICE_OPT_LOWSPEED
) 
 191                   USB_Device_SetLowSpeed(); 
 193                   USB_Device_SetFullSpeed(); 
 195                 USB_INT_Enable(USB_INT_VBUS
); 
 199         #if (defined(USB_CAN_BE_DEVICE) && !defined(FIXED_CONTROL_ENDPOINT_SIZE)) 
 200         if (USB_CurrentMode 
== USB_MODE_DEVICE
) 
 202                 USB_Descriptor_Device_t
* DeviceDescriptorPtr
; 
 204                 if (CALLBACK_USB_GetDescriptor((DTYPE_Device 
<< 8), 0, (void*)&DeviceDescriptorPtr
) != NO_DESCRIPTOR
) 
 206                         #if defined(USE_RAM_DESCRIPTORS) 
 207                                 USB_ControlEndpointSize 
= DeviceDescriptorPtr
->Endpoint0Size
; 
 208                         #elif defined(USE_EEPROM_DESCRIPTORS) 
 209                                 USB_ControlEndpointSize 
= eeprom_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
); 
 211                                 USB_ControlEndpointSize 
= pgm_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
); 
 219         #if defined(USB_DEVICE_ONLY)     
 220         USB_INT_Enable(USB_INT_SUSPEND
); 
 221         USB_INT_Enable(USB_INT_EORSTI
); 
 222         #if defined(CONTROL_ONLY_DEVICE) 
 223         UENUM 
= ENDPOINT_CONTROLEP
; 
 226         #elif defined(USB_HOST_ONLY) 
 227         USB_Host_HostMode_On(); 
 229         USB_Host_VBUS_Auto_Off(); 
 232         USB_Host_VBUS_Manual_Enable(); 
 233         USB_Host_VBUS_Manual_On(); 
 235         USB_INT_Enable(USB_INT_SRPI
); 
 236         USB_INT_Enable(USB_INT_BCERRI
); 
 238         if (USB_CurrentMode 
== USB_MODE_DEVICE
) 
 240                 USB_INT_Enable(USB_INT_SUSPEND
); 
 241                 USB_INT_Enable(USB_INT_EORSTI
); 
 243                 #if defined(CONTROL_ONLY_DEVICE) 
 244                 UENUM 
= ENDPOINT_CONTROLEP
; 
 247         else if (USB_CurrentMode 
== USB_MODE_HOST
) 
 249                 USB_Host_HostMode_On(); 
 251                 USB_Host_VBUS_Auto_Off(); 
 254                 USB_Host_VBUS_Manual_Enable(); 
 255                 USB_Host_VBUS_Manual_On(); 
 257                 USB_INT_Enable(USB_INT_SRPI
); 
 258                 USB_INT_Enable(USB_INT_BCERRI
);