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 
  31 #define  __INCLUDE_FROM_USB_DRIVER 
  32 #include "../../HighLevel/USBMode.h" 
  33 #if defined(USB_CAN_BE_DEVICE) 
  35 #define  __INCLUDE_FROM_HID_DRIVER 
  36 #define  __INCLUDE_FROM_HID_DEVICE_C 
  39 void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
  41         if (!(Endpoint_IsSETUPReceived())) 
  44         if (USB_ControlRequest
.wIndex 
!= HIDInterfaceInfo
->Config
.InterfaceNumber
) 
  47         switch (USB_ControlRequest
.bRequest
) 
  49                 case HID_REQ_GetReport
: 
  50                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  52                                 uint16_t ReportSize 
= 0; 
  53                                 uint8_t  ReportID   
= (USB_ControlRequest
.wValue 
& 0xFF); 
  54                                 uint8_t  ReportType 
= (USB_ControlRequest
.wValue 
>> 8) - 1; 
  55                                 uint8_t  ReportData
[HIDInterfaceInfo
->Config
.PrevReportINBufferSize
]; 
  57                                 memset(ReportData
, 0, sizeof(ReportData
)); 
  59                                 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, ReportType
, ReportData
, &ReportSize
); 
  61                                 if (HIDInterfaceInfo
->Config
.PrevReportINBuffer 
!= NULL
) 
  63                                         memcpy(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportData
, 
  64                                                HIDInterfaceInfo
->Config
.PrevReportINBufferSize
); 
  67                                 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
); 
  69                                 Endpoint_ClearSETUP(); 
  70                                 Endpoint_Write_Control_Stream_LE(ReportData
, ReportSize
); 
  75                 case HID_REQ_SetReport
: 
  76                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  78                                 uint16_t ReportSize 
= USB_ControlRequest
.wLength
; 
  79                                 uint8_t  ReportID   
= (USB_ControlRequest
.wValue 
& 0xFF); 
  80                                 uint8_t  ReportType 
= (USB_ControlRequest
.wValue 
>> 8) - 1; 
  81                                 uint8_t  ReportData
[ReportSize
]; 
  83                                 Endpoint_ClearSETUP(); 
  84                                 Endpoint_Read_Control_Stream_LE(ReportData
, ReportSize
); 
  87                                 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo
, ReportID
, ReportType
, 
  88                                                                      &ReportData
[ReportID ? 
1 : 0], ReportSize 
- (ReportID ? 
1 : 0)); 
  92                 case HID_REQ_GetProtocol
: 
  93                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  95                                 Endpoint_ClearSETUP(); 
  96                                 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.UsingReportProtocol
); 
  98                                 Endpoint_ClearStatusStage(); 
 102                 case HID_REQ_SetProtocol
: 
 103                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 105                                 Endpoint_ClearSETUP(); 
 106                                 Endpoint_ClearStatusStage(); 
 108                                 HIDInterfaceInfo
->State
.UsingReportProtocol 
= ((USB_ControlRequest
.wValue 
& 0xFF) != 0x00); 
 112                 case HID_REQ_SetIdle
: 
 113                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 115                                 Endpoint_ClearSETUP(); 
 116                                 Endpoint_ClearStatusStage(); 
 118                                 HIDInterfaceInfo
->State
.IdleCount 
= ((USB_ControlRequest
.wValue 
& 0xFF00) >> 6); 
 122                 case HID_REQ_GetIdle
: 
 123                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 125                                 Endpoint_ClearSETUP(); 
 126                                 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.IdleCount 
>> 2); 
 128                                 Endpoint_ClearStatusStage(); 
 135 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
 137         memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
)); 
 138         HIDInterfaceInfo
->State
.UsingReportProtocol 
= true; 
 139         HIDInterfaceInfo
->State
.IdleCount 
= 500; 
 141         if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
, EP_TYPE_INTERRUPT
, 
 142                                                                          ENDPOINT_DIR_IN
, HIDInterfaceInfo
->Config
.ReportINEndpointSize
, 
 143                                                                          HIDInterfaceInfo
->Config
.ReportINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE 
: ENDPOINT_BANK_SINGLE
))) 
 151 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
 153         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 156         Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
); 
 158         if (Endpoint_IsReadWriteAllowed()) 
 160                 uint8_t  ReportINData
[HIDInterfaceInfo
->Config
.PrevReportINBufferSize
]; 
 161                 uint8_t  ReportID     
= 0; 
 162                 uint16_t ReportINSize 
= 0; 
 164                 memset(ReportINData
, 0, sizeof(ReportINData
)); 
 166                 bool ForceSend         
= CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, HID_REPORT_ITEM_In
, 
 167                                                                              ReportINData
, &ReportINSize
); 
 168                 bool StatesChanged     
= false; 
 169                 bool IdlePeriodElapsed 
= (HIDInterfaceInfo
->State
.IdleCount 
&& !(HIDInterfaceInfo
->State
.IdleMSRemaining
)); 
 171                 if (HIDInterfaceInfo
->Config
.PrevReportINBuffer 
!= NULL
) 
 173                         StatesChanged 
= (memcmp(ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINSize
) != 0); 
 174                         memcpy(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBufferSize
); 
 177                 if (ReportINSize 
&& (ForceSend 
|| StatesChanged 
|| IdlePeriodElapsed
)) 
 179                         HIDInterfaceInfo
->State
.IdleMSRemaining 
= HIDInterfaceInfo
->State
.IdleCount
; 
 181                         Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
); 
 184                           Endpoint_Write_Byte(ReportID
); 
 186                         Endpoint_Write_Stream_LE(ReportINData
, ReportINSize
, NULL
);