How to get the whole device tree

I wrote a virtual USB bus driver, it can load a driver stack, and build a device tree. My bus driver can get the device object of the driver right upon itselft, like usbstor.sys. But I can not get the device object of disk.sys, which is upon usbstor.sys.

I don’t understand. In the DEVICE_OBJECT struct which constitute the device tree, there’s no member for recording upper level device object. The member “AttachDevice” is available only when a upper FDO attaches to a PDO. But the FDO can not get the PDO which is created by the same driver. Maybe I should get the driver object by the FDO, and get the PDO by the driver object. It’s too complex! There must be some commonly used ways that I don’t know. How does the system manage the device trees?

If you want to enum all devices create by same driver, using the NextDevice
pointer of DEVICE_OBJECT.

2011/2/22 <disney_cheng>

> I wrote a virtual USB bus driver, it can load a driver stack, and build a
> device tree. My bus driver can get the device object of the driver right
> upon itselft, like usbstor.sys. But I can not get the device object of
> disk.sys, which is upon usbstor.sys.
>
> I don’t understand. In the DEVICE_OBJECT struct which constitute the device
> tree, there’s no member for recording upper level device object. The member
> “AttachDevice” is available only when a upper FDO attaches to a PDO. But the
> FDO can not get the PDO which is created by the same driver. Maybe I should
> get the driver object by the FDO, and get the PDO by the driver object. It’s
> too complex! There must be some commonly used ways that I don’t know. How
> does the system manage the device trees?
>
> —
> 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
></disney_cheng>

>upper FDO attaches to a PDO. But the FDO can not get the PDO which is created by the same

driver

Just use your own child PDO list. BTW - KMDF does this for you.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

But sometimes the NextDevice is NULL, because another device object which I want to get is the previous one. I had to get it by the driver object. And if there’re more than tow device object on one driver, I don’t know which one I should get.

If you want to enum all devices create by same driver, using the NextDevice pointer of DEVICE_OBJECT.

You should be able to send yourself a
QUERY_DEVICE_RELATIONS/BusRelations IRP which will hand you the PDO
list.

Paul

On 22 February 2011 09:02, <disney_cheng> wrote:
> I wrote a virtual USB bus driver, it can load a driver stack, and build a device tree. My bus driver can get the device object of the driver right upon itselft, like usbstor.sys. But I can not get the device object of disk.sys, which is upon usbstor.sys.
>
> I don’t understand. In the DEVICE_OBJECT struct which constitute the device tree, there’s no member for recording upper level device object. The member “AttachDevice” is available only when a upper FDO attaches to a PDO. But the FDO can not get the PDO which is created by the same driver. Maybe I should get the driver object by the FDO, and get the PDO by the driver object. It’s too complex! There must be some commonly used ways that I don’t know. How does the system manage the device trees?
>
> —
> 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
>


Paul Durrant
http://www.linkedin.com/in/pdurrant</disney_cheng>

The pointer DeviceObject in DRIVER_OBJECT is the 1st device object created
by the driver.
You of course can enum all child devices following this way.

2011/2/22 <disney_cheng>

> But sometimes the NextDevice is NULL, because another device object which I
> want to get is the previous one. I had to get it by the driver object. And
> if there’re more than tow device object on one driver, I don’t know which
> one I should get.
>
>
> >If you want to enum all devices create by same driver, using the
> NextDevice pointer of DEVICE_OBJECT.
>
> —
> 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
></disney_cheng>

You can get a large pnp device tree with starting your enumerating from
\Driver\PnpManager which has many child device named as \Device\0000XXX.

2011/2/22 ???

> The pointer DeviceObject in DRIVER_OBJECT is the 1st device object created
> by the driver.
> You of course can enum all child devices following this way.
>
> 2011/2/22 <disney_cheng>
>
>> But sometimes the NextDevice is NULL, because another device object which
>> I want to get is the previous one. I had to get it by the driver object.
>> And if there’re more than tow device object on one driver, I don’t know
>> which one I should get.
>>
>>
>>
>> >If you want to enum all devices create by same driver, using the
>> NextDevice pointer of DEVICE_OBJECT.
>>
>> —
>> 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
>>
>
></disney_cheng>

Actually that was rather bad advice. Leave the pointers in the
DeviceObject alone. Leave the pointers in the DriverObject alone.

There is no good reason for you to be enumerating the device objects
of any driver other than your own, and then only if you are a bus
driver, as you probably need a list of the device objects you provide
as child devices.

What is it that you are trying to do that requires the enumeration of
device objects?

Mark Roddy

On Tue, Feb 22, 2011 at 4:15 AM, <disney_cheng> wrote:
> But sometimes the NextDevice is NULL, because another device object which I want to get is the previous one. I had to get it ?by the driver object. And if there’re more than tow device object on one driver, I don’t know which one I should get.
>
>
>>If you want to enum all devices create by same driver, using the NextDevice pointer of DEVICE_OBJECT.
>
> —
> 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
></disney_cheng>

My virtual bus driver want to know the status of the upper drivers.

If the bus fail to load the usbstor.sys which is right upon it, it will know that. But sometimes it load the usbstor.sys successfully, and the usbstor.sys load the disk.sys unsuccessfully. It doesn’t know this, and treat it as a succeed.

I think the bus should know the situation about the whole device tree. But I can not get that informations.

Actually that was rather bad advice. Leave the pointers in the
DeviceObject alone. Leave the pointers in the DriverObject alone.

There is no good reason for you to be enumerating the device objects
of any driver other than your own, and then only if you are a bus
driver, as you probably need a list of the device objects you provide
as child devices.

What is it that you are trying to do that requires the enumeration of
device objects?

Mark Roddy

Please explain what you mean by “device tree” here. I *think* you mean BRANCH of the Device Tree. The whole Device Tree is comprised of every device object in the entire system and I don’t think you’re seeking to enumerate that.

Peter
OSR

Your bus driver knows only about the PDOs it enumerates. Any subsequent enumeration by those children us not visible to the (now) grandparent (your bus driver). This is by design. Please do not fight the system’s design to get this type if info. If you want to walk the pnp tree, do it in user mode.

Btw, a driver may *never* send query device relations/bus relations on its own. This is a pnp state changing irp and the driver you are sending it to will expect that state change to take place and be processed by the pnp manager, but it won’t be because it was your driver who sent it and now that change is lost. for instance, if the driver reported as pdo as missing gor the war you sent, no surprise remove will be sent. When the stack is turn down later, the bus driver will delete the pdo because it was reported as missing, but the pno manager will bugcheck because a pdo was deleted and not actually reported as missing from its pic.

d

dent from a phine with no keynoard

-----Original Message-----
From: disney_cheng@qq.com
Sent: Tuesday, February 22, 2011 6:22 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to get the whole device tree

My virtual bus driver want to know the status of the upper drivers.

If the bus fail to load the usbstor.sys which is right upon it, it will know that. But sometimes it load the usbstor.sys successfully, and the usbstor.sys load the disk.sys unsuccessfully. It doesn’t know this, and treat it as a succeed.

I think the bus should know the situation about the whole device tree. But I can not get that informations.

Actually that was rather bad advice. Leave the pointers in the
DeviceObject alone. Leave the pointers in the DriverObject alone.

There is no good reason for you to be enumerating the device objects
of any driver other than your own, and then only if you are a bus
driver, as you probably need a list of the device objects you provide
as child devices.

What is it that you are trying to do that requires the enumeration of
device objects?

Mark Roddy


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

> wrote in message news:xxxxx@ntdev…
>The pointer DeviceObject in DRIVER_OBJECT is the 1st device object created
>by the driver.
>You of course can enum all child devices following this way.

That field is reserved for system use, you can’t just follow the pointer.
The problem is that you don’t have access to the lock necessary to protect
the list from changing (i.e. if a device object gets deleted while you’re
walking the list you can crash the system). If you really need all of the
device objects created by a particular driver you need to call
IoEnumerateDeviceObjectList.

-scott


Scott Noone
Consulting Associate and Chief System Problem Analyst
OSR Open Systems Resources, Inc.
http://www.osronline.com

Your bus driver knows only about the PDOs it enumerates. Any subsequent enumeration by those children us not visible to the (now) grandparent (your bus driver). This is by design. Please do not fight the system’s design to get this type if info. If you want to walk the pnp tree, do it in user mode.

Btw, a driver may *never* send query device relations/bus relations on its own. This is a pnp state changing irp and the driver you are sending it to will expect that state change to take place and be processed by the pnp manager, but it won’t be because it was your driver who sent it and now that change is lost. for instance, if the driver reported as pdo as missing gor the war you sent, no surprise remove will be sent. When the stack is turn down later, the bus driver will delete the pdo because it was reported as missing, but the pno manager will bugcheck because a pdo was deleted and not actually reported as missing from its POV.

d

You’re right. What I want is the whole branch of my virtual bus. Sorry for this.

Please explain what you mean by “device tree” here. I *think* you mean BRANCH
of the Device Tree. The whole Device Tree is comprised of every device object
in the entire system and I don’t think you’re seeking to enumerate that.

Peter
OSR

Thank you. Would you tell me how to do that in user mode please?

Your bus driver knows only about the PDOs it enumerates. Any subsequent
enumeration by those children us not visible to the (now) grandparent (your bus
driver). This is by design. Please do not fight the system’s design to get this
type if info. If you want to walk the pnp tree, do it in user mode.

Btw, a driver may *never* send query device relations/bus relations on its own.
This is a pnp state changing irp and the driver you are sending it to will
expect that state change to take place and be processed by the pnp manager, but
it won’t be because it was your driver who sent it and now that change is lost.
for instance, if the driver reported as pdo as missing gor the war you sent, no
surprise remove will be sent. When the stack is turn down later, the bus driver
will delete the pdo because it was reported as missing, but the pno manager will
bugcheck because a pdo was deleted and not actually reported as missing from its
POV.

d

Thank you. I’ll try this!

That field is reserved for system use, you can’t just follow the pointer.
The problem is that you don’t have access to the lock necessary to protect
the list from changing (i.e. if a device object gets deleted while you’re
walking the list you can crash the system). If you really need all of the
device objects created by a particular driver you need to call
IoEnumerateDeviceObjectList.

-scott


Scott Noone
Consulting Associate and Chief System Problem Analyst
OSR Open Systems Resources, Inc.
http://www.osronline.com

No, don’t try this. IoEnumerateDeviceObjectList will give you a flat list of device objects and you have no idea if they are a pdo, fdo, filter or control device. Furthermore, let’s say you get a PDO of a grandchild magically, how are you going to poke at it to know that it has failed to start or know its state? To enumerate the tree in user mode, use the CM_Xxxx APIs. Call CM_Locate_DevNode to get the root or start with your devnode if you have the devinst and then walk the tree. There is even a CM_Xxx API to get the status of the state of a DEVINST

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of disney_cheng@qq.com
Sent: Tuesday, February 22, 2011 10:06 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to get the whole device tree

Thank you. I’ll try this!

That field is reserved for system use, you can’t just follow the pointer.
The problem is that you don’t have access to the lock necessary to protect the list from changing (i.e. if a device object gets deleted while you’re walking the list you can crash the system). If you really need all of the device objects created by a particular driver you need to call IoEnumerateDeviceObjectList.

-scott


Scott Noone
Consulting Associate and Chief System Problem Analyst OSR Open Systems Resources, Inc.
http://www.osronline.com


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

> You’re right. What I want is the whole branch of my virtual bus. Sorry for this.

Your bus driver surely knows its immediate children PDOs, this is trivial.

As about levels below - they depends on FDs loaded to these devnodes. If the FD is not yours - then sorry, you don’t know at all what is its children. And, if the FD is also yours, you can create the private IOCTLs between the FD and the bus driver, just to report the whole subtree (and not only immediate children) to the topmost bus driver.

I think this is what MS’s USB stack does with its “get root hub PDO” request and stuff around it.


Maxim S. Shatskih
Windows DDK MVP

xxxxx@storagecraft.com

http://www.storagecraft.com