UsbDbgPdd_EventHandler (Compact 2013)
10/16/2014
Implement this function to handle USB events, such as attach, detach, suspend, and resume.
Syntax
extern "C" DWORD UsbDbgPdd_EventHandler(
USBDBG_MSG* msg,
BYTE* msgBuf
)
Parameters
- msg
[out] Pointer to the appropriate USBDBG_MSG element defined in UsbDbgDdsi.h.
- msgBuf
[out] Pointer to the received SETUP packet or endpoint number.
Return Value
Implement this function so that it returns ERROR_SUCCESS if successful; otherwise, return the appropriate error code from the values defined in WinError.h.
Remarks
The model device driver (MDD) calls this API repeatedly to receive updates on USB bus states, receive and transmit completed events on endpoints, and process SETUP packets on endpoint 0.
Example
The following example shows a typical implementation of the UsbDbgPdd_EventHandler function.
Important
For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.
extern "C" DWORD UsbDbgPdd_EventHandler(
USBDBG_MSG* msg,
BYTE* msgBuf
)
{
USBFN_PDD *pPdd = (USBFN_PDD *)&usbfn_pdd;
CSP_USB_REGS* pUSBDRegs = pPdd->pUSBDRegs;
USB_USBSTS_T source;
USB_CTRL_T ctrl;
DWORD *temp;
int i;
USB_ENDPTCOMPLETE_T edptcomp;
DWORD *pEpc;
pEpc = (DWORD*)&edptcomp;
USBDBGMSG(USBDBG_ZONE_FUNC, (L"usbdbgpdd: +UsbDbgPdd_EventHandler\r\n"));
//Process the previous pending requests first.
if (g_dwLastPktStatus)
{
*pEpc = g_dwLastPktStatus;
//Loop through all the initialized endpoints
for (i=0;i<=g_NumEPsUsed;i++)
{
//OUT Endpoints
if (edptcomp.ERCE&(1<<i))
{
ProcessOUTPacket(pPdd, msg, msgBuf, i);
goto clean;
}
//IN EndPoints
if (edptcomp.ETCE&(1<<i))
{
ProcessINPacket(pPdd, msg, msgBuf, i);
goto clean;
}
}
goto clean;
}
*pEpc = INREG32(&pUSBDRegs->OTG.ENDPTCOMPLETE);
// Clear the reg
temp=(DWORD *)&source;
*temp = INREG32(&pUSBDRegs->OTG.USBSTS);
OUTREG32(&pUSBDRegs->OTG.USBSTS, *temp);
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"UsbDbgPdd_EventHandler:: source 0x%x, endptcomplete 0x%x\r\n", *temp, *pEpc));
if (source.UEI)
{
USBDBGMSG(USBDBG_ZONE_ERROR, (L"UsbDbgPdd_EventHandler::USB Error Interrupt Occured\r\n"));
g_dwLastPktStatus = 0;
}
//USB Reset Interrupt
if (source.URI)
{
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"UsbDbgPdd_EventHandler::USB Reset Interrupt\r\n"));
g_dwLastPktStatus = 0;
HandleUSBReset(pPdd);
*msg = USBDBG_MSG_BUS_EVENT_RESET;
goto clean;
}
//USB Device State change
if (source.SLI)
{
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"UsbDbgPdd_EventHandler::State change Interrupt\r\n"));
USB_PORTSC_T state;
DWORD * t1;
// Get device state & change
t1=(DWORD *)&state;
*t1 = INREG32(&pUSBDRegs->OTG.PORTSC[USBD_PORT_NUM]);
//Deattach
if (pPdd->devState&&state.SUSP)
{
// Don't process other changes (we are disconnected)
pPdd->devState=0;
*msg = USBDBG_MSG_BUS_EVENT_DETACH;
OUTREG32(&pPdd->pUSBDRegs->OTG.T_154H.USBADR, 0);
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"UsbDbgPdd_EventHandler::Device detached\r\n"));
goto clean;
}
g_dwLastPktStatus = 0;
}
// USB Interrupt Transfer complete interrupt
if (source.UI)
{
USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"UsbDbgPdd_EventHandler::USB Interrupt Transfer complete interrupt\r\n"));
if (INREG32(&pUSBDRegs->OTG.ENDPTSETUPSTAT) || INREG32(&pUSBDRegs->OTG.ENDPTCOMPLETE))
{
if (INREG32(&pUSBDRegs->OTG.ENDPTSETUPSTAT))
{
ProcessSetupPacket(pPdd, msg, msgBuf);
goto clean;
}
//Loop through all the endpoints
for (i=0;i<=g_NumEPsUsed;i++)
{
//OUT Endpoints
if (edptcomp.ERCE&(1<<i))
{
ProcessOUTPacket(pPdd, msg, msgBuf, i);
goto clean;
}
//IN EndPoints
if (edptcomp.ETCE&(1<<i))
{
ProcessINPacket(pPdd, msg, msgBuf, i);
goto clean;
}
}
}
}
clean:
g_dwLastPktStatus = INREG32(&pUSBDRegs->OTG.ENDPTCOMPLETE);
USBDBGMSG(USBDBG_ZONE_FUNC, (L"usbdbgpdd: -UsbDbgPdd_EventHandler\r\n"));
return ERROR_SUCCESS;
}
Requirements
Header |
UsbDbgDdsi.h |