Skip to content

Commit

Permalink
* Added support for tracing usb bus resets and pipe resets
Browse files Browse the repository at this point in the history
  • Loading branch information
djpnewton committed Oct 30, 2009
1 parent 1d55388 commit 0e81e66
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 94 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2009-10-30 Daniel Newton <djpnewton@gmail.com>

* filter/BusDogMain.c:
* filter/BusDogTraceList.c:
* filter/inc/BusDogUserCommon.h
* gui/busdog/MainForm.c:
* gui/busdog/Native.c: Added support for tracing usb bus resets and pipe resets

2009-10-29 Daniel Newton <djpnewton@gmail.com>

* gui/busdog/MainForm.cs:
Expand Down
202 changes: 142 additions & 60 deletions filter/BusDogMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,15 +1280,79 @@ BusDogIoInternalDeviceControlComplete(
WdfRequestComplete(Request, Params->IoStatus.Status);
}

VOID
__BusDogProcessUrbTransfer(
IN WDFDEVICE Device,
IN PBUSDOG_CONTEXT Context,
IN PURB pUrb,
IN PVOID pTransferBuffer,
IN PMDL pTransferBufferMDL,
IN ULONG TransferBufferLength,
IN BOOLEAN bCompletion,
IN BOOLEAN bRead
)
{
//
// If we have an urb we want with a transfer buffer (bulk, interupt and control) then lets process it
//

BusDogPrint(BUSDOG_DEBUG_INFO, " TransferBufferLength: %d\n", TransferBufferLength);
BusDogPrint(BUSDOG_DEBUG_INFO, " In/Out: %s, MDL: %d\n", bRead ? "in" : "out", pTransferBufferMDL != NULL);

if (bCompletion && bRead || !bCompletion && !bRead)
{
BUSDOG_REQUEST_PARAMS params;
BUSDOG_REQUEST_PARAMS_INIT(&params);

params.p1 = BusDogUSBSubmitURB;
params.p2 = bRead ? BusDogUsbIn : BusDogUsbOut;
params.p3 = pUrb->UrbHeader.Function;

BusDogPrint(BUSDOG_DEBUG_INFO, " Data: ");

if (pTransferBuffer != NULL)
{
PrintChars((PCHAR)pTransferBuffer, TransferBufferLength);

BusDogAddTraceToFifo(Device,
Context->DeviceId,
BusDogInternalDeviceControlRequest,
params,
pTransferBuffer,
TransferBufferLength);
}
else if (pTransferBufferMDL != NULL)
{
PCHAR pMDLBuf = (PCHAR)MmGetSystemAddressForMdlSafe(pTransferBufferMDL, NormalPagePriority);

PrintChars(pMDLBuf, TransferBufferLength);

BusDogAddTraceToFifo(Device,
Context->DeviceId,
BusDogInternalDeviceControlRequest,
params,
pMDLBuf,
TransferBufferLength);
}
else
{
BusDogPrint(BUSDOG_DEBUG_ERROR, "Buffer error!\n");
}
}
}

VOID
BusDogProcessInternalDeviceControl(
IN WDFDEVICE Device,
IN PBUSDOG_CONTEXT Context,
IN WDFREQUEST Request,
IN ULONG IoControlCode,
IN BOOLEAN bCompletion,
OUT BOOLEAN* bRead)
OUT BOOLEAN* bRead
)
{
*bRead = FALSE;

if (BusDogFiltering && Context->FilterEnabled)
{
if (bCompletion)
Expand All @@ -1305,10 +1369,6 @@ BusDogProcessInternalDeviceControl(
case IOCTL_INTERNAL_USB_SUBMIT_URB:
{
PURB pUrb;
PVOID pTransferBuffer = NULL;
PMDL pTransferBufferMDL = NULL;
ULONG TransferBufferLength = 0;
BOOLEAN urbHandled = TRUE;

BusDogPrint(BUSDOG_DEBUG_INFO, " IOCTL_INTERNAL_USB_SUBMIT_URB\n");

Expand All @@ -1327,9 +1387,15 @@ BusDogProcessInternalDeviceControl(

BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER\n");

pTransferBuffer = pTransfer->TransferBuffer;
pTransferBufferMDL = pTransfer->TransferBufferMDL;
TransferBufferLength = pTransfer->TransferBufferLength;
__BusDogProcessUrbTransfer(
Device,
Context,
pUrb,
pTransfer->TransferBuffer,
pTransfer->TransferBufferMDL,
pTransfer->TransferBufferLength,
bCompletion,
*bRead);

break;
}
Expand All @@ -1340,9 +1406,15 @@ BusDogProcessInternalDeviceControl(

BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION_CONTROL_TRANSFER\n");

pTransferBuffer = pTransfer->TransferBuffer;
pTransferBufferMDL = pTransfer->TransferBufferMDL;
TransferBufferLength = pTransfer->TransferBufferLength;
__BusDogProcessUrbTransfer(
Device,
Context,
pUrb,
pTransfer->TransferBuffer,
pTransfer->TransferBufferMDL,
pTransfer->TransferBufferLength,
bCompletion,
*bRead);

break;
}
Expand Down Expand Up @@ -1386,68 +1458,78 @@ BusDogProcessInternalDeviceControl(
break;
}

pTransferBuffer = pTransfer->TransferBuffer;
pTransferBufferMDL = pTransfer->TransferBufferMDL;
TransferBufferLength = pTransfer->TransferBufferLength;
__BusDogProcessUrbTransfer(
Device,
Context,
pUrb,
pTransfer->TransferBuffer,
pTransfer->TransferBufferMDL,
pTransfer->TransferBufferLength,
bCompletion,
*bRead);

break;
}
default:
BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION: %d\n", pUrb->UrbHeader.Function);
urbHandled = FALSE;
break;
}

//
// If we have an urb we want (bulk, interupt and control) then lets process it
//

if (urbHandled)
{
BusDogPrint(BUSDOG_DEBUG_INFO, " TransferBufferLength: %d\n", TransferBufferLength);
BusDogPrint(BUSDOG_DEBUG_INFO, " R/W: %s, MDL: %d\n", *bRead ? "read" : "write", pTransferBufferMDL != NULL);

if (bCompletion && *bRead || !bCompletion && !*bRead)
case URB_FUNCTION_ABORT_PIPE:
case URB_FUNCTION_RESET_PIPE:
case URB_FUNCTION_SYNC_RESET_PIPE:
case URB_FUNCTION_SYNC_CLEAR_STALL:
{
BUSDOG_REQUEST_PARAMS params;
BUSDOG_REQUEST_PARAMS_INIT(&params);

params.p1 = BusDogUSB;
params.p2 = *bRead ? BusDogUsbIn : BusDogUsbOut;
params.p3 = pUrb->UrbHeader.Function;

BusDogPrint(BUSDOG_DEBUG_INFO, " Data: ");

if (pTransferBuffer != NULL)
switch (pUrb->UrbHeader.Function)
{
PrintChars((PCHAR)pTransferBuffer, TransferBufferLength);

BusDogAddTraceToFifo(Device,
Context->DeviceId,
BusDogInternalDeviceControlRequest,
params,
pTransferBuffer,
TransferBufferLength);
case URB_FUNCTION_ABORT_PIPE:
BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION_ABORT_PIPE\n");
break;
case URB_FUNCTION_RESET_PIPE:
BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION_RESET_PIPE\n");
break;
case URB_FUNCTION_SYNC_RESET_PIPE:
BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION_SYNC_RESET_PIPE\n");
break;
case URB_FUNCTION_SYNC_CLEAR_STALL:
BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION_SYNC_CLEAR_STALL\n");
break;
}
else if (pTransferBufferMDL != NULL)
{
PCHAR pMDLBuf = (PCHAR)MmGetSystemAddressForMdlSafe(pTransferBufferMDL, NormalPagePriority);

PrintChars(pMDLBuf, TransferBufferLength);
params.p1 = BusDogUSBSubmitURB;
params.p3 = pUrb->UrbHeader.Function;

BusDogAddTraceToFifo(Device,
Context->DeviceId,
BusDogInternalDeviceControlRequest,
params,
pMDLBuf,
TransferBufferLength);
}
else
{
BusDogPrint(BUSDOG_DEBUG_ERROR, "Buffer error!\n");
}
BusDogAddTraceToFifo(Device,
Context->DeviceId,
BusDogInternalDeviceControlRequest,
params,
NULL,
0);

break;
}
default:
BusDogPrint(BUSDOG_DEBUG_INFO, " URB_FUNCTION: %d\n", pUrb->UrbHeader.Function);
break;
}

break;
}

case IOCTL_INTERNAL_USB_RESET_PORT:
{
BUSDOG_REQUEST_PARAMS params;
BUSDOG_REQUEST_PARAMS_INIT(&params);

params.p1 = BusDogUSBResetPort;

BusDogPrint(BUSDOG_DEBUG_INFO, " IOCTL_INTERNAL_USB_RESET_PORT\n");

BusDogAddTraceToFifo(Device,
Context->DeviceId,
BusDogInternalDeviceControlRequest,
params,
NULL,
0);

break;
}
}
Expand Down
10 changes: 7 additions & 3 deletions filter/BusDogTraceList.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ __BusDogCreateTrace(

pTrace->BufferSize = BufferLength;

RtlCopyMemory((PCHAR)pTrace + sizeof(BUSDOG_FILTER_TRACE),
TraceBuffer,
BufferLength);
// Some traces might have no buffer (eg usb bus reset event)
if (BufferLength > 0)
{
RtlCopyMemory((PCHAR)pTrace + sizeof(BUSDOG_FILTER_TRACE),
TraceBuffer,
BufferLength);
}

return pTraceItem;
}
Expand Down
3 changes: 2 additions & 1 deletion filter/inc/BusDogUserCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ typedef struct _REQUEST_PARAMS

typedef enum _REQUEST_INTERNAL_DEVICE_CONTROL_TYPE
{
BusDogUSB = 0x2000
BusDogUSBSubmitURB = 0x2000,
BusDogUSBResetPort
} BUSDOG_REQUEST_INTERNAL_DEVICE_CONTROL_TYPE;

typedef enum _REQUEST_USB_DIRECTION
Expand Down
5 changes: 4 additions & 1 deletion gui/busdog/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ private void AddFilterTrace(FilterTrace filterTrace)
item.SubItems.Add(filterTrace.GetTimestampDelta(prevTrace).ToString());
break;
case 3:
item.SubItems.Add(filterTrace.Buffer.Length.ToString());
if (filterTrace.Buffer != null)
item.SubItems.Add(filterTrace.Buffer.Length.ToString());
else
item.SubItems.Add(Convert.ToString(0));
break;
case 4:
item.SubItems.Add(filterTrace.BufToHex());
Expand Down
Loading

0 comments on commit 0e81e66

Please sign in to comment.