3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  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 
  38 void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
  40         if (!(Endpoint_IsSETUPReceived())) 
  43         if (USB_ControlRequest
.wIndex 
!= HIDInterfaceInfo
->Config
.InterfaceNumber
) 
  46         switch (USB_ControlRequest
.bRequest
) 
  49                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  51                                 Endpoint_ClearSETUP();   
  53                                 uint16_t ReportINSize 
= 0; 
  54                                 uint8_t  ReportID     
= (USB_ControlRequest
.wValue 
& 0xFF); 
  55                                 uint8_t  ReportType   
= (USB_ControlRequest
.wValue 
>> 8) - 1; 
  56                                 uint8_t  ReportINData
[HIDInterfaceInfo
->Config
.PrevReportINBufferSize
]; 
  58                                 memset(ReportINData
, 0, sizeof(ReportINData
)); 
  60                                 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, ReportType
, 
  61                                                                     HIDInterfaceInfo
->Config
.PrevReportINBuffer
, &ReportINSize
); 
  63                                 if (HIDInterfaceInfo
->Config
.PrevReportINBuffer 
!= NULL
) 
  64                                   memcpy(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBufferSize
); 
  66                                 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
); 
  67                                 Endpoint_Write_Control_Stream_LE(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINSize
); 
  73                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  75                                 Endpoint_ClearSETUP(); 
  77                                 uint16_t ReportOUTSize 
= USB_ControlRequest
.wLength
; 
  78                                 uint8_t  ReportOUTData
[ReportOUTSize
]; 
  79                                 uint8_t  ReportID 
= (USB_ControlRequest
.wValue 
& 0xFF); 
  81                                 Endpoint_Read_Control_Stream_LE(ReportOUTData
, ReportOUTSize
); 
  84                                 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo
, ReportID
, ReportOUTData
, ReportOUTSize
); 
  89                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  91                                 Endpoint_ClearSETUP(); 
  93                                 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.UsingReportProtocol
); 
  96                                 Endpoint_ClearStatusStage(); 
 100                 case REQ_SetProtocol
: 
 101                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 103                                 Endpoint_ClearSETUP(); 
 105                                 HIDInterfaceInfo
->State
.UsingReportProtocol 
= ((USB_ControlRequest
.wValue 
& 0xFF) != 0x00); 
 107                                 Endpoint_ClearStatusStage(); 
 112                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 114                                 Endpoint_ClearSETUP(); 
 116                                 HIDInterfaceInfo
->State
.IdleCount 
= ((USB_ControlRequest
.wValue 
& 0xFF00) >> 6); 
 118                                 Endpoint_ClearStatusStage(); 
 123                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 125                                 Endpoint_ClearSETUP(); 
 127                                 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.IdleCount 
>> 2); 
 130                                 Endpoint_ClearStatusStage(); 
 137 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
 139         memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
)); 
 140         HIDInterfaceInfo
->State
.UsingReportProtocol 
= true; 
 141         HIDInterfaceInfo
->State
.IdleCount 
= 500; 
 143         if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
, EP_TYPE_INTERRUPT
, 
 144                                                                          ENDPOINT_DIR_IN
, HIDInterfaceInfo
->Config
.ReportINEndpointSize
, 
 145                                                                          HIDInterfaceInfo
->Config
.ReportINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE 
: ENDPOINT_BANK_SINGLE
))) 
 153 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
) 
 155         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 158         Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
); 
 160         if (Endpoint_IsReadWriteAllowed()) 
 162                 uint8_t  ReportINData
[HIDInterfaceInfo
->Config
.PrevReportINBufferSize
]; 
 163                 uint8_t  ReportID     
= 0; 
 164                 uint16_t ReportINSize 
= 0; 
 166                 memset(ReportINData
, 0, sizeof(ReportINData
)); 
 168                 bool ForceSend         
= CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, REPORT_ITEM_TYPE_In
, 
 169                                                                              ReportINData
, &ReportINSize
); 
 170                 bool StatesChanged     
= false; 
 171                 bool IdlePeriodElapsed 
= (HIDInterfaceInfo
->State
.IdleCount 
&& !(HIDInterfaceInfo
->State
.IdleMSRemaining
)); 
 173                 if (HIDInterfaceInfo
->Config
.PrevReportINBuffer 
!= NULL
) 
 175                         StatesChanged 
= (memcmp(ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINSize
) != 0); 
 176                         memcpy(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBufferSize
); 
 179                 if (ReportINSize 
&& (ForceSend 
|| StatesChanged 
|| IdlePeriodElapsed
)) 
 181                         HIDInterfaceInfo
->State
.IdleMSRemaining 
= HIDInterfaceInfo
->State
.IdleCount
; 
 183                         Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
); 
 186                           Endpoint_Write_Byte(ReportID
); 
 188                         Endpoint_Write_Stream_LE(ReportINData
, ReportINSize
, NO_STREAM_CALLBACK
);