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 #define  __INCLUDE_FROM_USB_DRIVER 
  32 #include "../../Core/USBMode.h" 
  34 #if defined(USB_CAN_BE_DEVICE) 
  36 #define  __INCLUDE_FROM_HID_DRIVER 
  37 #define  __INCLUDE_FROM_HID_DEVICE_C 
  38 #include "HIDClassDevice.h" 
  40 void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
  42         if (!(Endpoint_IsSETUPReceived())) 
  45         if (USB_ControlRequest
.wIndex 
!= HIDInterfaceInfo
->Config
.InterfaceNumber
) 
  48         switch (USB_ControlRequest
.bRequest
) 
  50                 case HID_REQ_GetReport
: 
  51                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  53                                 uint16_t ReportSize 
= 0; 
  54                                 uint8_t  ReportID   
= (USB_ControlRequest
.wValue 
& 0xFF); 
  55                                 uint8_t  ReportType 
= (USB_ControlRequest
.wValue 
>> 8) - 1; 
  56                                 uint8_t  ReportData
[HIDInterfaceInfo
->Config
.PrevReportINBufferSize
]; 
  58                                 memset(ReportData
, 0, sizeof(ReportData
)); 
  60                                 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, ReportType
, ReportData
, &ReportSize
); 
  62                                 if (HIDInterfaceInfo
->Config
.PrevReportINBuffer 
!= NULL
) 
  64                                         memcpy(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportData
, 
  65                                                HIDInterfaceInfo
->Config
.PrevReportINBufferSize
); 
  68                                 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
); 
  70                                 Endpoint_ClearSETUP(); 
  71                                 Endpoint_Write_Control_Stream_LE(ReportData
, ReportSize
); 
  76                 case HID_REQ_SetReport
: 
  77                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  79                                 uint16_t ReportSize 
= USB_ControlRequest
.wLength
; 
  80                                 uint8_t  ReportID   
= (USB_ControlRequest
.wValue 
& 0xFF); 
  81                                 uint8_t  ReportType 
= (USB_ControlRequest
.wValue 
>> 8) - 1; 
  82                                 uint8_t  ReportData
[ReportSize
]; 
  84                                 Endpoint_ClearSETUP(); 
  85                                 Endpoint_Read_Control_Stream_LE(ReportData
, ReportSize
); 
  88                                 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo
, ReportID
, ReportType
, 
  89                                                                      &ReportData
[ReportID ? 
1 : 0], ReportSize 
- (ReportID ? 
1 : 0)); 
  93                 case HID_REQ_GetProtocol
: 
  94                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  96                                 Endpoint_ClearSETUP(); 
  97                                 while (!(Endpoint_IsINReady())); 
  98                                 Endpoint_Write_8(HIDInterfaceInfo
->State
.UsingReportProtocol
); 
 100                                 Endpoint_ClearStatusStage(); 
 104                 case HID_REQ_SetProtocol
: 
 105                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 107                                 Endpoint_ClearSETUP(); 
 108                                 Endpoint_ClearStatusStage(); 
 110                                 HIDInterfaceInfo
->State
.UsingReportProtocol 
= ((USB_ControlRequest
.wValue 
& 0xFF) != 0x00); 
 114                 case HID_REQ_SetIdle
: 
 115                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 117                                 Endpoint_ClearSETUP(); 
 118                                 Endpoint_ClearStatusStage(); 
 120                                 HIDInterfaceInfo
->State
.IdleCount 
= ((USB_ControlRequest
.wValue 
& 0xFF00) >> 6); 
 124                 case HID_REQ_GetIdle
: 
 125                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 127                                 Endpoint_ClearSETUP(); 
 128                                 while (!(Endpoint_IsINReady())); 
 129                                 Endpoint_Write_8(HIDInterfaceInfo
->State
.IdleCount 
>> 2); 
 131                                 Endpoint_ClearStatusStage(); 
 138 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
 140         memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
)); 
 141         HIDInterfaceInfo
->State
.UsingReportProtocol 
= true; 
 142         HIDInterfaceInfo
->State
.IdleCount           
= 500; 
 144         HIDInterfaceInfo
->Config
.ReportINEndpoint
.Type 
= EP_TYPE_INTERRUPT
; 
 146         if (!(Endpoint_ConfigureEndpointTable(&HIDInterfaceInfo
->Config
.ReportINEndpoint
, 1))) 
 152 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
 154         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 157         if (HIDInterfaceInfo
->State
.PrevFrameNum 
== USB_Device_GetFrameNumber()) 
 159                 #if defined(USB_DEVICE_OPT_LOWSPEED) 
 160                 if (!(USB_Options 
& USB_DEVICE_OPT_LOWSPEED
)) 
 167         Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpoint
.Address
); 
 169         if (Endpoint_IsReadWriteAllowed()) 
 171                 uint8_t  ReportINData
[HIDInterfaceInfo
->Config
.PrevReportINBufferSize
]; 
 172                 uint8_t  ReportID     
= 0; 
 173                 uint16_t ReportINSize 
= 0; 
 175                 memset(ReportINData
, 0, sizeof(ReportINData
)); 
 177                 bool ForceSend         
= CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, HID_REPORT_ITEM_In
, 
 178                                                                              ReportINData
, &ReportINSize
); 
 179                 bool StatesChanged     
= false; 
 180                 bool IdlePeriodElapsed 
= (HIDInterfaceInfo
->State
.IdleCount 
&& !(HIDInterfaceInfo
->State
.IdleMSRemaining
)); 
 182                 if (HIDInterfaceInfo
->Config
.PrevReportINBuffer 
!= NULL
) 
 184                         StatesChanged 
= (memcmp(ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINSize
) != 0); 
 185                         memcpy(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBufferSize
); 
 188                 if (ReportINSize 
&& (ForceSend 
|| StatesChanged 
|| IdlePeriodElapsed
)) 
 190                         HIDInterfaceInfo
->State
.IdleMSRemaining 
= HIDInterfaceInfo
->State
.IdleCount
; 
 192                         Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpoint
.Address
); 
 195                           Endpoint_Write_8(ReportID
); 
 197                         Endpoint_Write_Stream_LE(ReportINData
, ReportINSize
, NULL
); 
 202                 HIDInterfaceInfo
->State
.PrevFrameNum 
= USB_Device_GetFrameNumber();