Discussion:
[Libusb-win32-devel] Identify the physical USB ports used by a driver handle
Patrik Thalin
2010-02-19 10:10:11 UTC
Permalink
Hi all,

I am looking for a solution to identify the physical USB ports used by a
device. I have looked at devcon in WDK to find the DeviceID eg.
USB\VID_xxxx&PID_yyyy\6&6B42B9C&0&4. This seem to be a unique for the
port used. I have successfully duplicated this to my application. By
calling SetupDiGetDeviceRegistryProperty and SetupDiGetDeviceInstanceId.
But I can't find a way to associate this to the driver handle. Can this
be done?


Any other suggestion on how identify the port is also welcome! Note that
I can not use a serial number in the device it has to be unique for the
USB port on the computer. Also note that I have several indentical units
connected.

Thanks in advance!

Regards,
Patrik Thalin
Xiaofan Chen
2010-02-19 14:47:13 UTC
Permalink
On Fri, Feb 19, 2010 at 6:10 PM, Patrik Thalin
Post by Patrik Thalin
Hi all,
I am looking for a solution to identify the physical USB ports used by a
device. I have looked at devcon in WDK to find the DeviceID eg.
USB\VID_xxxx&PID_yyyy\6&6B42B9C&0&4. This seem to be a unique for the
port used. I have successfully duplicated this to my application. By
calling SetupDiGetDeviceRegistryProperty and SetupDiGetDeviceInstanceId.
But I can't find a way to associate this to the driver handle. Can this
be done?
Any other suggestion on how identify the port is also welcome! Note that
I can not use a serial number in the device it has to be unique for the
USB port on the computer. Also note that I have several indentical units
connected.
I do not know the answer. You may have better luck trying out
some other lists. The best one may be this newsgroup.
microsoft.public.development.device.drivers
http://groups.google.com/group/microsoft.public.development.device.drivers/topics

But it is said to be non-trivial for any OS.
http://old.nabble.com/Re%3A-how-to-get-device-info-from-mount-point-p16593540.html

Some say it is not possible under Windows.
http://www.winvistatips.com/uniquely-identify-usb-hid-device-t809662.html

Might be possible under Linux or Mac OS X.
http://old.nabble.com/How-to-get-the-Location-ID-and-Current-Available-information-on-linux--td25588308.html
--
Xiaofan http://mcuee.blogspot.com
Pete Batard
2010-02-19 16:25:10 UTC
Permalink
Post by Patrik Thalin
Any other suggestion on how identify the port is also welcome!
I've been doing just that in the Windows backend for libusb 1.0.

First of all, for hub enumeration, you use the ConnectionIndex attribute
of the USB_NODE_CONNECTION_INFORMATION structure. This is pretty much a
port index.

Then, for the device themselves, you can use
SetupDiGetDeviceRegistryProperty with SPDRP_ADDRESS to get the port.
If you want to build the usb tree, you will then need to match the
parent as well using the SetupDi or CM functions.

This is pretty much what we use internally to build the USB tree in the
libusb 1.0 Windows backend.

If you have a look at
http://git.libusb.org/?p=libusb-pbatard.git;a=blob;f=libusb/os/windows_usb.c,
the usb_enumerate_hub() and set_device_paths() functions might be of
interest to you.

Regards,

/Pete
Patrik Thalin
2010-03-08 09:05:48 UTC
Permalink
Pete! Thanks for your help! I got it working now.

To match the hub node information to a device handle i had to make a few
additions in the driver to get the "DevicePropertyDriverKeyName" with
the function "IoGetDeviceProperty". Any comments on this solution is
welcome!

I have posted the patch here:

http://sourceforge.net/tracker/?func=detail&aid=2962463&group_id=78138&a
tid=552264


/Patrik


-----Original Message-----
From: Pete Batard [mailto:***@gmail.com]
Sent: den 19 februari 2010 17:25
To: libusb-win32-***@lists.sourceforge.net
Subject: Re: [Libusb-win32-devel] Identify the physical USB ports used
by a driver handle
Post by Patrik Thalin
Any other suggestion on how identify the port is also welcome!
I've been doing just that in the Windows backend for libusb 1.0.

First of all, for hub enumeration, you use the ConnectionIndex attribute
of the USB_NODE_CONNECTION_INFORMATION structure. This is pretty much a
port index.

Then, for the device themselves, you can use
SetupDiGetDeviceRegistryProperty with SPDRP_ADDRESS to get the port.
If you want to build the usb tree, you will then need to match the
parent as well using the SetupDi or CM functions.

This is pretty much what we use internally to build the USB tree in the
libusb 1.0 Windows backend.

If you have a look at
http://git.libusb.org/?p=libusb-pbatard.git;a=blob;f=libusb/os/windows_u
sb.c,
the usb_enumerate_hub() and set_device_paths() functions might be of
interest to you.

Regards,

/Pete

------------------------------------------------------------------------
------
Download Intel® Parallel Studio Eval Try the new software tools for
yourself. Speed compiling, find bugs proactively, and fine-tune
applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
Xiaofan Chen
2010-03-08 12:58:32 UTC
Permalink
On Mon, Mar 8, 2010 at 5:05 PM, Patrik Thalin
Post by Patrik Thalin
Pete! Thanks for your help! I got it working now.
To match the hub node information to a device handle i had to make a few
additions in the driver to get the "DevicePropertyDriverKeyName" with
the function "IoGetDeviceProperty". Any comments on this solution is
welcome!
http://sourceforge.net/tracker/?func=detail&aid=2962463&group_id=78138&a
tid=552264
Not so sure why you need to use libusb-win32 driver here. I am under
the impression that you do not need any driver for this based on the
approach of Pete. But Pete can comment better.
--
Xiaofan http://mcuee.blogspot.com
Pete Batard
2010-03-08 17:02:47 UTC
Permalink
Post by Patrik Thalin
To match the hub node information to a device handle i had to make a few
additions in the driver to get the "DevicePropertyDriverKeyName" with
the function "IoGetDeviceProperty".
Did you have to do that because SetupDiGetDeviceRegistryProperty with
SPDRP_ADDRESS did not work against devices driven by libusb0.sys?
Patrik Thalin
2010-03-09 07:16:43 UTC
Permalink
My application talks to the devices with the libusb driver and I need to
know the physical location of the port in my application. I have several
"usb_dev_handle" I need to map those to physical ports or an ID for that
port.

SetupDiGetDeviceRegistryProperty with SPDRP_ADDRESS does work but I need
to match that to one of the libusb open usb device handers (i have
several identical units). I also get SetupDiGetDeviceInstanceId to get
the Device Instance ID.

SPDRP_ADDRESS give me something like:
"{eb781aaf-9c70-4523-a5df-642a87eca567}\0009" and that has Device
Instance ID 5&3B8933DC&1&5
"{eb781aaf-9c70-4523-a5df-642a87eca567}\0010" and that has Device
Instance ID 5&3B8933DC&1&2
...

Then I get that same from the driver with IOCTL
DevicePropertyDriverKeyName:
Handler A has "{eb781aaf-9c70-4523-a5df-642a87eca567}\0009"
Handler B has "{eb781aaf-9c70-4523-a5df-642a87eca567}\0010"
...

Now I can tell that Handler A has Device Instance ID 5&3B8933DC&1&5.

Can I get that without a IOCTL?

/Patrik


-----Original Message-----
From: Pete Batard [mailto:***@gmail.com]
Sent: den 8 mars 2010 18:03
To: libusb-win32-***@lists.sourceforge.net
Subject: Re: [Libusb-win32-devel] Identify the physical USB ports used
by a driver handle
Post by Patrik Thalin
To match the hub node information to a device handle i had to make a
few additions in the driver to get the "DevicePropertyDriverKeyName"
with the function "IoGetDeviceProperty".
Did you have to do that because SetupDiGetDeviceRegistryProperty with
SPDRP_ADDRESS did not work against devices driven by libusb0.sys?
Pete Batard
2010-03-09 13:25:59 UTC
Permalink
Post by Patrik Thalin
My application talks to the devices with the libusb driver and I need to
know the physical location of the port in my application. I have several
"usb_dev_handle" I need to map those to physical ports or an ID for that
port.
OK. I don't think libusb (win32, 1.0) exposes any of the port
properties, although there might be plans to do that with libusb 1.1,
and we certainly have the port information available in the Windows
backend of 1.0, so it could be exposed at little cost if needed.
Post by Patrik Thalin
SetupDiGetDeviceRegistryProperty with SPDRP_ADDRESS does work but I need
to match that to one of the libusb open usb device handers (i have
several identical units). I also get SetupDiGetDeviceInstanceId to get
the Device Instance ID.
If you need to work with the libusb device handler, whose main purpose
is to abstract the device properties, then you would indeed need to
modify libusb to expose some of these properties.
Post by Patrik Thalin
Can I get that without a IOCTL?
You certainly can do the match you mentioned above without using an
IOCTL, but using the SetupDi and Configuration Manager (CM) functions,
like CM_Get_Parent, etc instead. This is what we do in libusb 1.0
Windows backend with set_device_paths().

It's a bit more complex than simply adding an IOCTL, but the big
advantage is that it works for all drivers, or even if no driver has
been installed for the device yet.

In fact, if you used libusb 1.0 on Windows, your problem could easily be
solved by adding a new libusb call to return the connection_index member
of the windows_device_priv structure, which is associated with any
libusb device on Windows, as this is really the USB port number.
You could also expose the parent device if needed.

Regards,

/Pete

Loading...