3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  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 
  31 #include "../../../../Common/Common.h" 
  32 #if (ARCH == ARCH_AVR8) 
  34 #define  __INCLUDE_FROM_USB_DRIVER 
  35 #include "../USBMode.h" 
  37 #if defined(USB_CAN_BE_DEVICE) 
  39 #include "../Endpoint.h" 
  41 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE) 
  42 uint8_t USB_Device_ControlEndpointSize 
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
; 
  45 bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t
* const Table
, 
  46                                      const uint8_t Entries
) 
  48         for (uint8_t i 
= 0; i 
< Entries
; i
++) 
  50                 if (!(Table
[i
].Address
)) 
  53                 if (!(Endpoint_ConfigureEndpoint(Table
[i
].Address
, Table
[i
].Type
, Table
[i
].Size
, Table
[i
].Banks
))) 
  60 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, 
  61                                     const uint8_t UECFG0XData
, 
  62                                     const uint8_t UECFG1XData
) 
  64 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG) 
  65         Endpoint_SelectEndpoint(Number
); 
  66         Endpoint_EnableEndpoint(); 
  69         UECFG0X 
= UECFG0XData
; 
  70         UECFG1X 
= UECFG1XData
; 
  72         return Endpoint_IsConfigured(); 
  74         for (uint8_t EPNum 
= Number
; EPNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++) 
  80                 Endpoint_SelectEndpoint(EPNum
); 
  84                         UECFG0XTemp 
= UECFG0XData
; 
  85                         UECFG1XTemp 
= UECFG1XData
; 
  90                         UECFG0XTemp 
= UECFG0X
; 
  91                         UECFG1XTemp 
= UECFG1X
; 
  95                 if (!(UECFG1XTemp 
& (1 << ALLOC
))) 
  98                 Endpoint_DisableEndpoint(); 
  99                 UECFG1X 
&= ~(1 << ALLOC
); 
 101                 Endpoint_EnableEndpoint(); 
 102                 UECFG0X 
= UECFG0XTemp
; 
 103                 UECFG1X 
= UECFG1XTemp
; 
 106                 if (!(Endpoint_IsConfigured())) 
 110         Endpoint_SelectEndpoint(Number
); 
 115 void Endpoint_ClearEndpoints(void) 
 119         for (uint8_t EPNum 
= 0; EPNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++) 
 121                 Endpoint_SelectEndpoint(EPNum
); 
 125                 Endpoint_DisableEndpoint(); 
 129 void Endpoint_ClearStatusStage(void) 
 131         if (USB_ControlRequest
.bmRequestType 
& REQDIR_DEVICETOHOST
) 
 133                 while (!(Endpoint_IsOUTReceived())) 
 135                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 143                 while (!(Endpoint_IsINReady())) 
 145                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 153 #if !defined(CONTROL_ONLY_DEVICE) 
 154 uint8_t Endpoint_WaitUntilReady(void) 
 156         #if (USB_STREAM_TIMEOUT_MS < 0xFF) 
 157         uint8_t  TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 159         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 162         uint16_t PreviousFrameNumber 
= USB_Device_GetFrameNumber(); 
 166                 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
) 
 168                         if (Endpoint_IsINReady()) 
 169                           return ENDPOINT_READYWAIT_NoError
; 
 173                         if (Endpoint_IsOUTReceived()) 
 174                           return ENDPOINT_READYWAIT_NoError
; 
 177                 uint8_t USB_DeviceState_LCL 
= USB_DeviceState
; 
 179                 if (USB_DeviceState_LCL 
== DEVICE_STATE_Unattached
) 
 180                   return ENDPOINT_READYWAIT_DeviceDisconnected
; 
 181                 else if (USB_DeviceState_LCL 
== DEVICE_STATE_Suspended
) 
 182                   return ENDPOINT_READYWAIT_BusSuspended
; 
 183                 else if (Endpoint_IsStalled()) 
 184                   return ENDPOINT_READYWAIT_EndpointStalled
; 
 186                 uint16_t CurrentFrameNumber 
= USB_Device_GetFrameNumber(); 
 188                 if (CurrentFrameNumber 
!= PreviousFrameNumber
) 
 190                         PreviousFrameNumber 
= CurrentFrameNumber
; 
 192                         if (!(TimeoutMSRem
--)) 
 193                           return ENDPOINT_READYWAIT_Timeout
;