How to get the usb root hub device and determine the type of an usb device ?

I am a beginner on windows driver programming, I am now working on a USB
Monitor program, the first thing I want to do is: when a USB device is
plugged into the USB hub, I can detect it and determine the its type (USB
mouse/USB flash disk/USB joystick/etc). My idea is: when a USB device is
plugged into the USB hub, PnP Manager will send an IRP with type
RP_MN_QUERY_DEVICE_RELATIONS to the USB Root Hub bus driver. So if I create
a filter driver and then attach it to the USB Root Hub device object I can
know when a USB device is plugged into the hub. But I don’t know how to do
the next:
1, How to get the USB Root Hub device object that I want attach my filter
driver to ?
2, How to determine a USB device’s type and how to get the information
needed ? (Hardware ID ?)

You have to attach your filter driver to each pdo enumerated by each hub
device.This is known as a “bus filter driver” and is undocumented and
unsupported by microsoft, but if you search this list you will find plenty
of discussion and documentation.

If you just want to monitor usb devices and do not need to capture usb data
traffic you can do that from user mode, see the usbview app in the WDK.

Mark Roddy

On Mon, Aug 31, 2015 at 3:25 AM, Li Ych wrote:

> I am a beginner on windows driver programming, I am now working on a USB
> Monitor program, the first thing I want to do is: when a USB device is
> plugged into the USB hub, I can detect it and determine the its type (USB
> mouse/USB flash disk/USB joystick/etc). My idea is: when a USB device is
> plugged into the USB hub, PnP Manager will send an IRP with type
> RP_MN_QUERY_DEVICE_RELATIONS to the USB Root Hub bus driver. So if I create
> a filter driver and then attach it to the USB Root Hub device object I can
> know when a USB device is plugged into the hub. But I don’t know how to do
> the next:
> 1, How to get the USB Root Hub device object that I want attach my filter
> driver to ?
> 2, How to determine a USB device’s type and how to get the information
> needed ? (Hardware ID ?)
> — NTDEV is sponsored by OSR Visit the list at:
> http://www.osronline.com/showlists.cfm?list=ntdev OSR is HIRING!! See
> http://www.osr.com/careers 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

Li Ych wrote:

I am a beginner on windows driver programming, I am now working on a
USB Monitor program, the first thing I want to do is: when a USB
device is plugged into the USB hub, I can detect it and determine the
its type (USB mouse/USB flash disk/USB joystick/etc). My idea is: when
a USB device is plugged into the USB hub, PnP Manager will send an
IRP with type |RP_MN_QUERY_DEVICE_RELATIONS| to the USB Root Hub bus
driver. So if I create a filter driver and then attach it to the USB
Root Hub device object I can know when a USB device is plugged into
the hub. But I don’t know how to do the next:
1, How to get the USB Root Hub device object that I want attach my
filter driver to ?
2, How to determine a USB device’s type and how to get the information
needed ? (Hardware ID ?)

You don’t need a driver for this. You can use
RegisterDeviceNotification to get notified any time the device tree
changes. When you see that some device has come or gone, you can
enumerate the whole USB tree, using code similar to UsbView, and note
which ports have changed since the last time you looked. If you don’t
have the UsbView source code, you need to look through it.

You need to understand a lot more about USB before you embark on this
kind of a project, just so you can understand what you can learn and
what you cannot learn from a device. A device does not not identify
itself as a USB flash disk or a USB joystick. The descriptors will tell
you how many interfaces there are, and each interface is part of some
USB device class. Keyboards, mice, joysticks, etc. are all HID (human
interface device) class. Some USB serial ports and many custom devices
also have HID interfaces.

However, a web camera, for example, might have 2 USB Video Class
interfaces, 2 USB Audio Class interfaces, and a HID interface for
controlling a flash. You can get that from the descriptors, but it’s up
to you to classify it further.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.