Why does WdfUsbTargetDeviceCreate fail in non-WDF driver?

Hi.

I am creating a WDM driver which uses WDF in order to access the WDF usb
functionality. I have successfully added the WDF coinstaller and am able to
load/unload my device. I am also able to create the WDF device using
WdfDeviceMiniportCreate(). This is based on msvad.

However, my call to WdfUsbTargetDeviceCreate() always fails with 0xC0000010.
I read and followed everything I could find regarding this subject. I don’t
know what more to try so I’m hoping someone can help me.

Thanks a lot,
Philip

I am posting the debug log and source code separately to this thread because I had problems posting it together…

Here is the debug output I am getting:

****** -> DriverEntry
****** <- DriverEntry
****** -> AddDevice
WdfDeviceMiniportCreate WdfDriver: 0x7bb25910 WdfDevice: 0x7bb4f7a8
UsbDevice: 0x0
****** <- AddDevice
****** -> StartDevice
****** -> CAdapterCommon::Init
InstallSubDevice T
****** -> CMiniportTopologyUSBT::CMiniportTopologyUSBT
****** -> CMiniportTopology::Init
****** -> CMiniportTopologyUSBT::Init
****** -> CMiniportTopologyUSBT::GetDescription
****** <- InstallSubdevice
InstallSubDevice W
****** -> CMiniportWaveCyclicUSBT::CMiniportWaveCyclicUSBT
****** -> CMiniportWaveCyclic::Init
****** -> CMiniportWaveCyclicUSBT::Init
****** -> CAdapterCommon::SetWaveServiceGroup
****** -> CMiniportWaveCyclicUSBT::GetDescription
****** <- InstallSubdevice
StartDevice1 WdfDriver: 0x7bb25910 WdfDevice: 0x7bb4f7a8 UsbDevice: 0x0
StartDevice2 WdfDriver: 0x7bb25910 WdfDevice: 0x7bb4f7a8 UsbDevice: 0x0
*****************************WdfUsbTargetDeviceCreate failed c0000010!
****** -> CMiniportTopology::~CMiniportTopology
****** -> CMiniportTopologyUSBT::~CMiniportTopologyUSBT
****** -> CMiniportWaveCyclic::~CMiniportWaveCyclic
****** -> CMiniportWaveCyclicUSBT::~CMiniportWaveCyclicUSBT
****** -> CAdapterCommon::~CAdapterCommon
****** -> DriverUnload
****** <- DriverUnload

Here is the relevant source code based on msvad

#define MAX_MINIPORTS 3 // Number of maximum miniports.

#pragma code_seg(“INIT”)
extern “C” NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPathName)
{
WDF_DRIVER_CONFIG config;
WDFDRIVER hDriver;
NTSTATUS ntStatus;

usbtDebugPrintEnter();

WDF_DRIVER_CONFIG_INIT(&config, WDF_NO_EVENT_CALLBACK);
config.DriverInitFlags |= WdfDriverInitNoDispatchOverride;

ntStatus = WdfDriverCreate(DriverObject,
RegistryPathName,
WDF_NO_OBJECT_ATTRIBUTES,
&config,
&hDriver);
if (!NT_SUCCESS(ntStatus))
{
usbtDebugPrint((DBGERROR, “WdfDriverCreate failed\n”));
return ntStatus;
}

// Tell the class driver to initialize the driver.
//
ntStatus = PcInitializeAdapterDriver(DriverObject, RegistryPathName, (PDRIVER_ADD_DEVICE)AddDevice);

usbtDebugPrintExit();

return ntStatus;
} // DriverEntry
#pragma code_seg()

#pragma code_seg(“PAGE”)

NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT PhysicalDeviceObject)
{
PAGED_CODE();

NTSTATUS ntStatus;

usbtDebugPrintEnter();

// disable prefast warning 28152 because
// DO_DEVICE_INITIALIZING is cleared in PcAddAdapterDevice
#pragma warning(disable:28152)

FunctionalDeviceObject = IoGetAttachedDevice(PhysicalDeviceObject);
// Tell the class driver to add the device.
ntStatus = PcAddAdapterDevice(DriverObject, PhysicalDeviceObject, PCPFNSTARTDEVICE(StartDevice),
MAX_MINIPORTS, 128 + MY_EXTENSION_SIZE + PORT_CLASS_DEVICE_EXTENSION_SIZE);

if (!NT_SUCCESS (ntStatus))
{
usbtDebugPrint((DBGERROR, “PcAddAdapterDevice failed (0x%x)\n”, ntStatus));
return ntStatus;
}

FunctionalDeviceObject = PhysicalDeviceObject->AttachedDevice;
FunctionalDeviceObject = IoGetAttachedDevice(PhysicalDeviceObject);

GetMyExtension()->WdfDriver = WdfGetDriver();
GetMyExtension()->WdfDevice = 0;
GetMyExtension()->UsbDevice = 0;

WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, WDF_DEVICE_INFO);
ntStatus = WdfDeviceMiniportCreate(GetMyExtension()->WdfDriver,
&attributes,
FunctionalDeviceObject,
PhysicalDeviceObject->AttachedDevice,
PhysicalDeviceObject,
&(GetMyExtension()->WdfDevice));
if (!NT_SUCCESS (ntStatus))
usbtDebugPrint((DBGERROR, “WdfDeviceMiniportCreate failed (0x%x)\n”, ntStatus));

usbtDebugPrint((DBGERROR, “WdfDeviceMiniportCreate WdfDriver: 0x%x WdfDevice: 0x%x UsbDevice: 0x%x\n”,
GetMyExtension()->WdfDriver, GetMyExtension()->WdfDevice, GetMyExtension()->UsbDevice));

usbtDebugPrintExit();

return ntStatus;
} // AddDevice

//=============================================================================
NTSTATUS InstallSubdevice(__in PDEVICE_OBJECT DeviceObject, __in PIRP Irp, __in PWSTR Name, __in REFGUID PortClassId, __in REFGUID MiniportClassId,
__in_opt PFNCREATEINSTANCE MiniportCreate, __in_opt PUNKNOWN UnknownAdapter, __in_opt PRESOURCELIST ResourceList, __in REFGUID PortInterfaceId,
__out_opt PUNKNOWN *OutPortInterface, __out_opt PUNKNOWN *OutPortUnknown)
{
**** same as msvad
} // InstallSubDevice

//=============================================================================
NTSTATUS StartDevice(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PRESOURCELIST ResourceList)
{
**** this part same as msvad

// Release the adapter common object. It either has other references,
// or we need to delete it anyway.
//
if (pAdapterCommon)
pAdapterCommon->Release();

if (pUnknownCommon)
pUnknownCommon->Release();

if (unknownTopology)
unknownTopology->Release();

if (unknownWave)
unknownWave->Release();

usbtDebugPrint((DBGERROR, “StartDevice1 WdfDriver: 0x%x WdfDevice: 0x%x UsbDevice: 0x%x\n”
, GetMyExtension()->WdfDriver, GetMyExtension()->WdfDevice, GetMyExtension()->UsbDevice));

// allocate Usb device
WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, USB_DEVICE_CONTEXT);
ntStatus = WdfUsbTargetDeviceCreate(GetMyExtension()->WdfDevice,
WDF_NO_OBJECT_ATTRIBUTES,//&attributes,
&GetMyExtension()->UsbDevice);

usbtDebugPrint((DBGERROR, “StartDevice2 WdfDriver: 0x%x WdfDevice: 0x%x UsbDevice: 0x%x\n”,
GetMyExtension()->WdfDriver, GetMyExtension()->WdfDevice, GetMyExtension()->UsbDevice));

if (!NT_SUCCESS(ntStatus))
{
usbtDebugPrint((DBGERROR, “*********WdfUsbTargetDeviceCreate failed %x!\n”, ntStatus));
return ntStatus;
}

usbtDebugPrintExit();

return ntStatus;
} // StartDevice
#pragma code_seg()

!wdflogdump should give more context as to exact reasons why this call failed.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@cox.net
Sent: Friday, August 28, 2009 12:53 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Why does WdfUsbTargetDeviceCreate fail in non-WDF driver?

Here is the relevant source code based on msvad

#define MAX_MINIPORTS 3 // Number of maximum miniports.

#pragma code_seg(“INIT”)
extern “C” NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPathName)
{
WDF_DRIVER_CONFIG config;
WDFDRIVER hDriver;
NTSTATUS ntStatus;

usbtDebugPrintEnter();

WDF_DRIVER_CONFIG_INIT(&config, WDF_NO_EVENT_CALLBACK);
config.DriverInitFlags |= WdfDriverInitNoDispatchOverride;

ntStatus = WdfDriverCreate(DriverObject,
RegistryPathName,
WDF_NO_OBJECT_ATTRIBUTES,
&config,
&hDriver);
if (!NT_SUCCESS(ntStatus))
{
usbtDebugPrint((DBGERROR, “WdfDriverCreate failed\n”));
return ntStatus;
}

// Tell the class driver to initialize the driver.
//
ntStatus = PcInitializeAdapterDriver(DriverObject, RegistryPathName, (PDRIVER_ADD_DEVICE)AddDevice);

usbtDebugPrintExit();

return ntStatus;
} // DriverEntry
#pragma code_seg()

#pragma code_seg(“PAGE”)

NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT PhysicalDeviceObject)
{
PAGED_CODE();

NTSTATUS ntStatus;

usbtDebugPrintEnter();

// disable prefast warning 28152 because
// DO_DEVICE_INITIALIZING is cleared in PcAddAdapterDevice
#pragma warning(disable:28152)

FunctionalDeviceObject = IoGetAttachedDevice(PhysicalDeviceObject);
// Tell the class driver to add the device.
ntStatus = PcAddAdapterDevice(DriverObject, PhysicalDeviceObject, PCPFNSTARTDEVICE(StartDevice),
MAX_MINIPORTS, 128 + MY_EXTENSION_SIZE + PORT_CLASS_DEVICE_EXTENSION_SIZE);

if (!NT_SUCCESS (ntStatus))
{
usbtDebugPrint((DBGERROR, “PcAddAdapterDevice failed (0x%x)\n”, ntStatus));
return ntStatus;
}

FunctionalDeviceObject = PhysicalDeviceObject->AttachedDevice;
FunctionalDeviceObject = IoGetAttachedDevice(PhysicalDeviceObject);

GetMyExtension()->WdfDriver = WdfGetDriver();
GetMyExtension()->WdfDevice = 0;
GetMyExtension()->UsbDevice = 0;

WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, WDF_DEVICE_INFO);
ntStatus = WdfDeviceMiniportCreate(GetMyExtension()->WdfDriver,
&attributes,
FunctionalDeviceObject,
PhysicalDeviceObject->AttachedDevice,
PhysicalDeviceObject,
&(GetMyExtension()->WdfDevice));
if (!NT_SUCCESS (ntStatus))
usbtDebugPrint((DBGERROR, “WdfDeviceMiniportCreate failed (0x%x)\n”, ntStatus));

usbtDebugPrint((DBGERROR, “WdfDeviceMiniportCreate WdfDriver: 0x%x WdfDevice: 0x%x UsbDevice: 0x%x\n”,
GetMyExtension()->WdfDriver, GetMyExtension()->WdfDevice, GetMyExtension()->UsbDevice));

usbtDebugPrintExit();

return ntStatus;
} // AddDevice

//=============================================================================
NTSTATUS InstallSubdevice(__in PDEVICE_OBJECT DeviceObject, __in PIRP Irp, __in PWSTR Name, __in REFGUID PortClassId, __in REFGUID MiniportClassId,
__in_opt PFNCREATEINSTANCE MiniportCreate, __in_opt PUNKNOWN UnknownAdapter, __in_opt PRESOURCELIST ResourceList, __in REFGUID PortInterfaceId,
__out_opt PUNKNOWN *OutPortInterface, __out_opt PUNKNOWN *OutPortUnknown)
{
**** same as msvad
} // InstallSubDevice

//=============================================================================
NTSTATUS StartDevice(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PRESOURCELIST ResourceList)
{
**** this part same as msvad

// Release the adapter common object. It either has other references,
// or we need to delete it anyway.
//
if (pAdapterCommon)
pAdapterCommon->Release();

if (pUnknownCommon)
pUnknownCommon->Release();

if (unknownTopology)
unknownTopology->Release();

if (unknownWave)
unknownWave->Release();

usbtDebugPrint((DBGERROR, “StartDevice1 WdfDriver: 0x%x WdfDevice: 0x%x UsbDevice: 0x%x\n”
, GetMyExtension()->WdfDriver, GetMyExtension()->WdfDevice, GetMyExtension()->UsbDevice));

// allocate Usb device
WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, USB_DEVICE_CONTEXT);
ntStatus = WdfUsbTargetDeviceCreate(GetMyExtension()->WdfDevice,
WDF_NO_OBJECT_ATTRIBUTES,//&attributes,
&GetMyExtension()->UsbDevice);

usbtDebugPrint((DBGERROR, “StartDevice2 WdfDriver: 0x%x WdfDevice: 0x%x UsbDevice: 0x%x\n”,
GetMyExtension()->WdfDriver, GetMyExtension()->WdfDevice, GetMyExtension()->UsbDevice));

if (!NT_SUCCESS(ntStatus))
{
usbtDebugPrint((DBGERROR, “*********WdfUsbTargetDeviceCreate failed %x!\n”, ntStatus));
return ntStatus;
}

usbtDebugPrintExit();

return ntStatus;
} // StartDevice
#pragma code_seg()


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Thanks Brandon.

I just fixed the problem with the following change to the sources (in case
anyone else is interested):

WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes,
WDF_DEVICE_INFO);
ntStatus = WdfDeviceMiniportCreate(GetMyExtension()->WdfDriver,
&attributes,
FunctionalDeviceObject,
PhysicalDeviceObject->NextDevice,
PhysicalDeviceObject,
&(GetMyExtension()->WdfDevice));

Cheers

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Brandon Wilson
Sent: Friday, August 28, 2009 5:25 PM
To: Windows System Software Devs Interest List
Subject: RE: RE:[ntdev] Why does WdfUsbTargetDeviceCreate fail in non-WDF
driver?

!wdflogdump should give more context as to exact reasons why this call
failed.

This

PhysicalDeviceObject->NextDevice,

Is incorrect, this is not the next device below you in the pnp stack. The value you are specifying here is the next devobj created by the bus driver after it created this pdo. At worst you could specify PhysicalDeviceObject again and skip any potential lower filters. At best you should specify the real attached device

d

Sent from my phone with no t9, all spilling mistakes are not intentional.

-----Original Message-----
From: pgruebele
Sent: Friday, August 28, 2009 2:30 PM
To: Windows System Software Devs Interest List
Subject: RE: RE:[ntdev] Why does WdfUsbTargetDeviceCreate fail in non-WDF driver?

Thanks Brandon.

I just fixed the problem with the following change to the sources (in case
anyone else is interested):

WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes,
WDF_DEVICE_INFO);
ntStatus = WdfDeviceMiniportCreate(GetMyExtension()->WdfDriver,
&attributes,
FunctionalDeviceObject,
PhysicalDeviceObject->NextDevice,
PhysicalDeviceObject,
&(GetMyExtension()->WdfDevice));

Cheers

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Brandon Wilson
Sent: Friday, August 28, 2009 5:25 PM
To: Windows System Software Devs Interest List
Subject: RE: RE:[ntdev] Why does WdfUsbTargetDeviceCreate fail in non-WDF
driver?

!wdflogdump should give more context as to exact reasons why this call
failed.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer