@ -22,10 +22,13 @@ UpstreamHidGetReportCallback GetReportCallback = NULL;
KeyboardOutStateTypeDef KeyboardOutDataState = KEYBOARD_OUT_STATE_IDLE ;
KeyboardOutStateTypeDef KeyboardOutDataState = KEYBOARD_OUT_STATE_IDLE ;
uint8_t KeyboardOutData [ HID_KEYBOARD_OUTPUT_DATA_LEN ] ;
uint8_t KeyboardOutData [ HID_KEYBOARD_OUTPUT_DATA_LEN ] ;
uint8_t GetReportLoopIsRunning = 0 ;
void Upstream_HID_GetNextInterruptReportReceiveCallback ( UpstreamPacketTypeDef * receivedPacket ) ;
static void Upstream_HID_ReceiveInterruptReport ( void ) ;
void Upstream_HID_ReallySendControlReportReceiveCallback ( UpstreamPacketTypeDef * receivedPacket ) ;
static void Upstream_HID_ReceiveInterruptReportCallback ( UpstreamPacketTypeDef * receivedPacket ) ;
static void Upstream_HID_SendControlReport ( void ) ;
static void Upstream_HID_SendControlReportCallback ( UpstreamPacketTypeDef * receivedPacket ) ;
@ -42,51 +45,43 @@ void Upstream_HID_DeInit(void)
void Upstream_HID_GetNextInterruptReport ( UpstreamHidGetReportCallback callback )
//Called by usbd_hid to request our next interrupt report
void Upstream_HID_GetInterruptReport ( UpstreamHidGetReportCallback callback )
{
{
UpstreamPacketTypeDef * freePacket ;
GetReportCallback = callback ;
InterfaceCommandClassTypeDef activeClass ;
activeClass = Upstream_StateMachine_CheckActiveClass ( ) ;
if ( ( activeClass ! = COMMAND_CLASS_HID_MOUSE ) & &
( activeClass ! = COMMAND_CLASS_HID_KEYBOARD ) ) //add classes here
{
UPSTREAM_STATEMACHINE_FREAKOUT ;
return ;
}
if ( callback ! = NULL )
{
//This means we were called by the host
//Release packet used for last transaction (if any)
if ( UpstreamHidPacket ! = NULL )
if ( UpstreamHidPacket ! = NULL )
{
{
Upstream_ReleasePacket ( UpstreamHidPacket ) ;
Upstream_ReleasePacket ( UpstreamHidPacket ) ;
UpstreamHidPacket = NULL ;
UpstreamHidPacket = NULL ;
}
}
if ( KeyboardOutDataState = = KEYBOARD_OUT_STATE_BUSY )
//Wakeup interrupts are apparently broken.
{
//So we check for resume when the host sends us something here.
//Just save the callback, because we are still waiting for the OUT report to complete
Upstream_StateMachine_CheckResume ( ) ;
GetReportCallback = callback ;
return ;
//Start our internal loop?
}
if ( ! GetReportLoopIsRunning )
if ( GetReportCallback ! = NULL )
{
{
//Just return if we already have an outstanding request
GetReportLoopIsRunning = 1 ;
return ;
Upstream_HID_ReceiveInterruptReport ( ) ;
}
}
GetReportCallback = callback ;
}
}
else
//Our internal report request loop
static void Upstream_HID_ReceiveInterruptReport ( void )
{
{
//This means were called on OUT report completion, or retrying after a downstream NAK
UpstreamPacketTypeDef * freePacket ;
if ( GetReportCallback = = NULL )
InterfaceCommandClassTypeDef activeClass ;
activeClass = Upstream_StateMachine_CheckActiveClass ( ) ;
if ( ( activeClass ! = COMMAND_CLASS_HID_MOUSE ) & &
( activeClass ! = COMMAND_CLASS_HID_KEYBOARD ) ) //add classes here
{
{
//The host has not given us the callback yet, so we give up
UPSTREAM_STATEMACHINE_FREAKOUT ;
return ;
return ;
}
}
}
freePacket = Upstream_GetFreePacketImmediately ( ) ;
freePacket = Upstream_GetFreePacketImmediately ( ) ;
if ( freePacket = = NULL )
if ( freePacket = = NULL )
@ -100,17 +95,18 @@ void Upstream_HID_GetNextInterruptReport(UpstreamHidGetReportCallback callback)
if ( Upstream_TransmitPacket ( freePacket ) = = HAL_OK )
if ( Upstream_TransmitPacket ( freePacket ) = = HAL_OK )
{
{
Upstream_ReceivePacket ( Upstream_HID_GetNextInterruptReportReceive Callback) ;
Upstream_ReceivePacket ( Upstream_HID_ReceiveInterruptReport Callback) ;
}
}
else
else
{
{
Upstream_ReleasePacket ( freePacket ) ;
Upstream_ReleasePacket ( freePacket ) ;
}
}
}
}
void Upstream_HID_GetNextInterruptReportReceive Callback( UpstreamPacketTypeDef * receivedPacket )
static void Upstream_HID_ReceiveInterruptReport Callback( UpstreamPacketTypeDef * receivedPacket )
{
{
UpstreamHidGetReportCallback tempReportCallback ;
UpstreamHidGetReportCallback tempReportCallback ;
InterfaceCommandClassTypeDef activeClass ;
InterfaceCommandClassTypeDef activeClass ;
@ -126,41 +122,18 @@ void Upstream_HID_GetNextInterruptReportReceiveCallback(UpstreamPacketTypeDef* r
return ;
return ;
}
}
if ( UpstreamHidPacket ! = NULL )
{
while ( 1 ) ;
}
if ( receivedPacket = = NULL ) //Error receiving packet
if ( receivedPacket = = NULL ) //Error receiving packet
{
{
return ; //Just give up...
return ; //Just give up...
}
}
if ( GetReportCallback = = NULL ) //HID class may have been DeInitted while we were waiting for our reply
{
Upstream_ReleasePacket ( receivedPacket ) ;
return ;
}
if ( receivedPacket - > Length16 = = UPSTREAM_PACKET_HEADER_LEN_16 )
if ( receivedPacket - > Length16 = = UPSTREAM_PACKET_HEADER_LEN_16 )
{
{
//Zero-length reply indicates no data from downstream device
//Zero-length reply indicates no data from downstream device
Upstream_ReleasePacket ( receivedPacket ) ;
Upstream_ReleasePacket ( receivedPacket ) ;
//Check if we need to send OUT data to the keyboard before requesting next Interrupt IN data
if ( KeyboardOutDataState = = KEYBOARD_OUT_STATE_DATA_READY )
{
Upstream_HID_ReallySendControlReport ( ) ;
}
}
else
else
{
{
//Otherwise poll downstream again
Upstream_HID_GetNextInterruptReport ( NULL ) ;
}
return ;
}
if ( activeClass = = COMMAND_CLASS_HID_MOUSE )
if ( activeClass = = COMMAND_CLASS_HID_MOUSE )
{
{
if ( receivedPacket - > Length16 ! = ( UPSTREAM_PACKET_HEADER_LEN_16 + ( ( HID_MOUSE_INPUT_DATA_LEN + 1 ) / 2 ) ) )
if ( receivedPacket - > Length16 ! = ( UPSTREAM_PACKET_HEADER_LEN_16 + ( ( HID_MOUSE_INPUT_DATA_LEN + 1 ) / 2 ) ) )
@ -174,6 +147,18 @@ void Upstream_HID_GetNextInterruptReportReceiveCallback(UpstreamPacketTypeDef* r
UPSTREAM_STATEMACHINE_FREAKOUT ;
UPSTREAM_STATEMACHINE_FREAKOUT ;
return ;
return ;
}
}
//Mouse wakeup is triggered by button clicks only
if ( receivedPacket - > Data [ 0 ] ! = 0 )
{
if ( Upstream_StateMachine_GetSuspendState ( ) )
{
//Send wakeup signal to host instead of returning data packet
GetReportCallback = NULL ;
Upstream_StateMachine_Wakeup ( ) ;
}
}
//Other mouse sanity checks & stuff go here...
//Other mouse sanity checks & stuff go here...
}
}
@ -199,32 +184,53 @@ void Upstream_HID_GetNextInterruptReportReceiveCallback(UpstreamPacketTypeDef* r
return ;
return ;
}
}
}
}
if ( Upstream_StateMachine_GetSuspendState ( ) )
{
//Send wakeup signal to host instead of returning data packet
GetReportCallback = NULL ;
Upstream_StateMachine_Wakeup ( ) ;
}
//Other keyboard sanity checks here...
//Other keyboard sanity checks here...
}
}
//Other HID classes go here...
//Other HID classes go here...
else
else
{
{
UPSTREAM_STATEMACHINE_FREAKOUT ;
UPSTREAM_STATEMACHINE_FREAKOUT ;
return ;
return ;
}
}
if ( ( GetReportCallback = = NULL ) | |
( UpstreamHidPacket ! = NULL ) )
{
Upstream_ReleasePacket ( receivedPacket ) ;
}
else
{
//Send resulting data to host
UpstreamHidPacket = receivedPacket ; //Save packet so we can free it when upstream USB transaction is done
UpstreamHidPacket = receivedPacket ; //Save packet so we can free it when upstream USB transaction is done
tempReportCallback = GetReportCallback ;
tempReportCallback = GetReportCallback ;
GetReportCallback = NULL ;
GetReportCallback = NULL ;
tempReportCallback ( receivedPacket - > Data , dataLength ) ;
tempReportCallback ( receivedPacket - > Data , dataLength ) ;
}
}
//Check if we need to send OUT data to the keyboard before requesting next Interrupt IN data
//Check if we need to send OUT data to the keyboard before requesting next Interrupt IN data
if ( KeyboardOutDataState = = KEYBOARD_OUT_STATE_DATA_READY )
if ( KeyboardOutDataState = = KEYBOARD_OUT_STATE_DATA_READY )
{
{
Upstream_HID_ReallySendControlReport ( ) ;
Upstream_HID_SendControlReport ( ) ;
}
else
{
Upstream_HID_ReceiveInterruptReport ( ) ; //Otherwise poll downstream again
}
}
}
}
void Upstream_HID_ SendControlReport( UpstreamPacketTypeDef * packetToSend , uint8_t dataLength )
void Upstream_HID_ Request SendControlReport( UpstreamPacketTypeDef * packetToSend , uint8_t dataLength )
{
{
InterfaceCommandClassTypeDef activeClass ;
InterfaceCommandClassTypeDef activeClass ;
uint32_t i ;
uint32_t i ;
@ -248,7 +254,7 @@ void Upstream_HID_SendControlReport(UpstreamPacketTypeDef* packetToSend, uint8_t
void Upstream_HID_ Really SendControlReport( void )
static void Upstream_HID_ SendControlReport( void )
{
{
UpstreamPacketTypeDef * freePacket ;
UpstreamPacketTypeDef * freePacket ;
uint32_t i ;
uint32_t i ;
@ -270,7 +276,7 @@ void Upstream_HID_ReallySendControlReport(void)
if ( Upstream_TransmitPacket ( freePacket ) = = HAL_OK )
if ( Upstream_TransmitPacket ( freePacket ) = = HAL_OK )
{
{
Upstream_ReceivePacket ( Upstream_HID_ Really SendControlReportReceive Callback) ;
Upstream_ReceivePacket ( Upstream_HID_ SendControlReportCallback) ;
}
}
else
else
{
{
@ -280,7 +286,7 @@ void Upstream_HID_ReallySendControlReport(void)
void Upstream_HID_ Really SendControlReportReceive Callback( UpstreamPacketTypeDef * receivedPacket )
static void Upstream_HID_ SendControlReportCallback( UpstreamPacketTypeDef * receivedPacket )
{
{
InterfaceCommandClassTypeDef activeClass ;
InterfaceCommandClassTypeDef activeClass ;
@ -291,6 +297,12 @@ void Upstream_HID_ReallySendControlReportReceiveCallback(UpstreamPacketTypeDef*
return ;
return ;
}
}
if ( KeyboardOutDataState ! = KEYBOARD_OUT_STATE_BUSY )
{
UPSTREAM_STATEMACHINE_FREAKOUT ;
return ;
}
if ( receivedPacket = = NULL )
if ( receivedPacket = = NULL )
{
{
return ; //Just give up...
return ; //Just give up...
@ -298,9 +310,8 @@ void Upstream_HID_ReallySendControlReportReceiveCallback(UpstreamPacketTypeDef*
Upstream_ReleasePacket ( receivedPacket ) ;
Upstream_ReleasePacket ( receivedPacket ) ;
KeyboardOutDataState = KEYBOARD_OUT_STATE_IDLE ;
KeyboardOutDataState = KEYBOARD_OUT_STATE_IDLE ;
Upstream_HID_ReceiveInterruptReport ( ) ;
//If upstream host has already requested the next IN report data,
//this will send the request downstream.
Upstream_HID_GetNextInterruptReport ( NULL ) ;
}
}