Added hardware locks

This commit is contained in:
Angel Cascarino
2022-01-04 12:37:05 +00:00
parent 4f35551547
commit 1ae9c55477

View File

@@ -9,6 +9,7 @@
#include "descriptor_defs.h"
#include "xua_hid_report.h"
#include "hid_report_descriptor.h"
#include "hwlock.h"
#define HID_REPORT_ITEM_LOCATION_SIZE ( 1 )
@@ -18,6 +19,9 @@
/*
* Each element in s_hidChangePending corresponds to an element in hidReports.
*/
hwlock_t hidStaticVarLock = hwlock_alloc();
static unsigned s_hidChangePending[ HID_REPORT_COUNT ];
static unsigned char s_hidReportDescriptor[ HID_REPORT_DESCRIPTOR_MAX_LENGTH ];
static size_t s_hidReportDescriptorLength;
@@ -134,30 +138,36 @@ static size_t hidTranslateItem( const USB_HID_Short_Item_t* inPtr, unsigned char
void hidCalcNextReportTime( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) {
if( id == hidGetElementReportId( hidReports[ idx ]->location )) {
s_hidNextReportTime[ idx ] = s_hidReportTime[ idx ] + s_hidCurrentPeriod[ idx ];
}
}
hwlock_release(hidStaticVarLock);
}
void hidCaptureReportTime( const unsigned id, const unsigned time )
{
hwlock_acquire(hidStaticVarLock);
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) {
if( id == hidGetElementReportId( hidReports[ idx ]->location )) {
s_hidReportTime[ idx ] = time;
}
}
hwlock_release(hidStaticVarLock);
}
void hidClearChangePending( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) {
if(( id == 0U ) || ( id == hidGetElementReportId( hidReports[ idx ]->location ))) {
s_hidChangePending[ idx ] = 0U;
break;
}
}
hwlock_release(hidStaticVarLock);
}
static unsigned hidGetElementBitLocation( const unsigned short location )
@@ -202,7 +212,9 @@ static unsigned hidGetItemType( const unsigned char header )
return bType;
}
unsigned hidGetNextReportTime( const unsigned id ) {
unsigned hidGetNextReportTime( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
unsigned retVal = 0U;
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) {
@@ -210,28 +222,35 @@ unsigned hidGetNextReportTime( const unsigned id ) {
retVal = s_hidNextReportTime[ idx ];
}
}
hwlock_release(hidStaticVarLock);
return retVal;
}
unsigned hidIsReportDescriptorPrepared( void )
{
hwlock_acquire(hidStaticVarLock);
return s_hidReportDescriptorPrepared;
hwlock_release(hidStaticVarLock);
}
unsigned char* hidGetReportDescriptor( void )
{
unsigned char* retVal = NULL;
hwlock_acquire(hidStaticVarLock);
if( s_hidReportDescriptorPrepared ) {
retVal = s_hidReportDescriptor;
}
hwlock_release(hidStaticVarLock);
return retVal;
}
size_t hidGetReportDescriptorLength( void )
{
hwlock_acquire(hidStaticVarLock);
size_t retVal = ( s_hidReportDescriptorPrepared ) ? s_hidReportDescriptorLength : 0U;
hwlock_release(hidStaticVarLock);
return retVal;
}
@@ -320,6 +339,7 @@ unsigned hidGetReportItem(
size_t hidGetReportLength( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
size_t retVal = 0U;
if( s_hidReportDescriptorPrepared ) {
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) {
@@ -328,11 +348,13 @@ size_t hidGetReportLength( const unsigned id )
}
}
}
hwlock_release(hidStaticVarLock);
return retVal;
}
unsigned hidGetReportPeriod( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
unsigned retVal = 0U;
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) {
if( id == hidGetElementReportId( hidReports[ idx ]->location )) {
@@ -340,10 +362,13 @@ unsigned hidGetReportPeriod( const unsigned id )
break;
}
}
hwlock_release(hidStaticVarLock);
return retVal;
}
unsigned hidGetReportTime( const unsigned id ) {
unsigned hidGetReportTime( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
unsigned retVal = 0U;
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) {
@@ -351,6 +376,7 @@ unsigned hidGetReportTime( const unsigned id ) {
retVal = s_hidReportTime[ idx ];
}
}
hwlock_release(hidStaticVarLock);
return retVal;
}
@@ -368,6 +394,7 @@ static unsigned hidGetUsagePage( const unsigned id )
unsigned hidIsChangePending( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
unsigned retVal = 0U;
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) {
if( id == 0U && s_hidChangePending[ idx ] != 0U ) {
@@ -377,11 +404,13 @@ unsigned hidIsChangePending( const unsigned id )
break;
}
}
hwlock_release(hidStaticVarLock);
return retVal;
}
unsigned hidIsIdleActive( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
unsigned retVal = 0U;
if( 0U == id ) {
retVal = 1U;
@@ -395,11 +424,13 @@ unsigned hidIsIdleActive( const unsigned id )
break;
}
}
return retVal;
hwlock_release(hidStaticVarLock);
return retVal;
}
void hidPrepareReportDescriptor( void )
{
hwlock_acquire(hidStaticVarLock);
if( !s_hidReportDescriptorPrepared ) {
s_hidReportDescriptorLength = 0U;
unsigned char* ptr = s_hidReportDescriptor;
@@ -410,49 +441,60 @@ void hidPrepareReportDescriptor( void )
s_hidReportDescriptorPrepared = 1U;
}
hwlock_release(hidStaticVarLock);
}
void hidReportInit( void )
{
hwlock_acquire(hidStaticVarLock);
for( unsigned idx = 0U; idx < HID_REPORT_COUNT; ++idx ) {
s_hidCurrentPeriod[ idx ] = ENDPOINT_INT_INTERVAL_IN_HID * MS_IN_TICKS;
}
memset( s_hidIdleActive, 0, sizeof( s_hidIdleActive ) );
memset( s_hidChangePending, 0, sizeof( s_hidChangePending ) );
hwlock_release(hidStaticVarLock);
}
void hidResetReportDescriptor( void )
{
hwlock_acquire(hidStaticVarLock);
s_hidReportDescriptorPrepared = 0U;
hwlock_release(hidStaticVarLock);
}
void hidSetChangePending( const unsigned id )
{
hwlock_acquire(hidStaticVarLock);
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) {
if( id == hidGetElementReportId( hidReports[ idx ]->location )) {
s_hidChangePending[ idx ] = 1U;
break;
}
}
hwlock_release(hidStaticVarLock);
}
void hidSetIdle( const unsigned id, const unsigned state )
{
hwlock_acquire(hidStaticVarLock);
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) {
if( id == hidGetElementReportId( hidReports[ idx ]->location )) {
s_hidIdleActive[ idx ] = ( state != 0U );
break;
}
}
hwlock_release(hidStaticVarLock);
}
void hidSetNextReportTime( const unsigned id, const unsigned time )
{
hwlock_acquire(hidStaticVarLock);
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx ) {
if( id == hidGetElementReportId( hidReports[ idx ]->location )) {
s_hidNextReportTime[ idx ] = time;
}
}
hwlock_release(hidStaticVarLock);
}
unsigned hidSetReportItem(
@@ -464,6 +506,7 @@ unsigned hidSetReportItem(
const unsigned char data[]
)
{
hwlock_acquire(hidStaticVarLock);
unsigned retVal = HID_STATUS_IN_USE;
if( !s_hidReportDescriptorPrepared ) {
@@ -511,17 +554,20 @@ unsigned hidSetReportItem(
}
}
hwlock_release(hidStaticVarLock);
return retVal;
}
void hidSetReportPeriod( const unsigned id, const unsigned period )
{
hwlock_acquire(hidStaticVarLock);
for( size_t idx = 0U; idx < HID_REPORT_COUNT; ++idx) {
if( id == hidGetElementReportId( hidReports[ idx ]->location )) {
s_hidCurrentPeriod[ idx ] = period;
break;
}
}
hwlock_release(hidStaticVarLock);
}
static size_t hidTranslateItem( const USB_HID_Short_Item_t* inPtr, unsigned char** outPtrPtr )