Author Topic: usb gamepad order always random.  (Read 12557 times)

ulao

  • Frequent Contributor
  • ****
  • Posts: 172
usb gamepad order always random.
« on: June 09, 2017, 11:51:53 am »
I have had a question I was never able to get a definitive answer to that thus for the only work around is to use separate ID's.

The issues is where you have a gamepad USB HID with an ID of say 0x0d55, you add this to your system and it's player one in the list of enumerated controllers. Now you add another device of the same. Is will be play 1 or 2, there is no control over that order. Thus far the answer is, USB is random. In xp and win98/95 there was a reg hack but since win7 +, there is no fix. What some people do is hack the firmware flashed to the device and find ID and change it.

It's an issue because some devices just make sense being player 1,2,3,4 from left to right.

I believe there is some sort of GUID request that could be used but I never could get that to work. I tried messing with USB values to get a delay when enumerating but again, even if the second device is added last it can jump to player 1. Also tried messing with a serial number but it also didn't help.

Anyone have a thought?
« Last Edit: June 09, 2017, 01:35:01 pm by ulao »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: usb gamepad order always random.
« Reply #1 on: June 09, 2017, 08:37:49 pm »
This might relate to it, from USB Complete Fifth Edition:

A device without a USB serial number string descriptor gets a new hardware key
every time the device attaches to a port the device hasn’t been attached to previously.
If you physically remove the device from the bus and attach a different device
with identical descriptors to the same port, the operating system doesn’t know the
difference and thus doesn’t create a new hardware key. Devices with USB serial numbers
have one hardware key per physical device without regard to what port the
device attaches to.

Each hardware key has a Device Parameters subheading with additional information
about the device instance such as a device interface GUID and whether selective suspend
is enabled.


ulao

  • Frequent Contributor
  • ****
  • Posts: 172
Re: usb gamepad order always random.
« Reply #2 on: June 12, 2017, 12:15:53 pm »
 Would this not imply the serial number would force an order? - My serial number default length is 4 charters. What is a normal serial number length, maybe I need to use the full SN.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: usb gamepad order always random.
« Reply #3 on: June 13, 2017, 11:43:48 am »
I don't think serial number length matters except where the class spec specifies it (not HID).

A unique serial number should give you the same device interface GUID every time.

Where are you viewing the enumerated list? In a host application?

ulao

  • Frequent Contributor
  • ****
  • Posts: 172
Re: usb gamepad order always random.
« Reply #4 on: June 13, 2017, 03:05:45 pm »
Jan,having troubles with that statement?

"A unique serial number should give you the same device interface GUID every time."
- this is the issue though. I get the same GUID everytime, thus causing a random order. So serials will not fix this?
--- Oh wait you mean unique to that serial. If so , yes this is what I'd like to see.

There are a lot of ways to pull the order.
1) just bring up the game-pad screen in Microsoft. This list shows consistently random. 
2) Enumerate in directX, same order as above.
3) Using USB.dll, again, same order.
4) using linux, in dev input you get j0 j1 j2 j3 each again follows  this  consistently random order.

by consistently random I mean this.

plug in a 4 port hub to a new usb port. the order will be random says 2,4,1,3 for example. Now use the examples above they are always 2,4,1,3

now try another usb port. say this time its 2,1,3,4 again, all examples above show this order.

You can actually try usb after usb until you get 1,2,3,4 this is good even on a reboot. But the minute you change usb ports or removed the hid drivers and reinstall them, they will pick another order. My goals is a way to force 1,2,3,4.








« Last Edit: June 13, 2017, 03:18:53 pm by ulao »

Bret

  • Frequent Contributor
  • ****
  • Posts: 68
Re: usb gamepad order always random.
« Reply #5 on: June 13, 2017, 06:55:40 pm »
What you're asking isn't really a USB question, but a Windows question:  essentially, "How do make make Windows order or name things the way I want it to?"  All OS's have the same issue, at least when there are multiple devices that look the same to the OS.  With USB, the only way Windows can tell the difference between similar devices is either with a serial number or which port they get plugged into.  Windows just assumes that if a device gets plugged into a port and it is similar to a device that got plugged into that same port previously, it must be the same device.  Sometimes that's true and sometimes it's not, but Windows also assumes that it is.

Windows also always assumes that you automatically want things setup the same way it automatically decided to do it last time, which is why it makes up and stores the functional equivalent of cookies (GUIDs and Registry entries) so it can remember what it did last time.  This is quite useful most of the time, but really annoying at other times, especially since Windows won't ever automatically delete any of it.  And, Windows insists on doing it all automatically and really doesn't like you telling it what to do, at least when it comes to hardware.

Maybe somebody out there knows a good way around this, but keep in mind that this really isn't a USB question.  If there is a good answer, it would probably be applicable to lots of other types of removable/wireless mechanisms in Windows besides USB, also.

bpaddock

  • Frequent Contributor
  • ****
  • Posts: 66
Re: usb gamepad order always random.
« Reply #6 on: June 14, 2017, 08:14:29 am »
I have found Ghost Buster Portable an easy way to clean out the accumulated dead devices from Windows.
This is particularly useful on a development machine or one on a production floor where such devices are tested and manufactured.

https://ghostbuster.codeplex.com/

Microsoft's DevCon utility can be used too "Add, delete, and reorder the hardware IDs of root-enumerated devices".
I don't expect it to help with reordering devices on hubs.  A lot of the "Random" is the device's power up timing that Windows then commits to it's memory.

https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon

This BAT file will do the same as GhostBuster using DevCon:

Code: [Select]
@ECHO OFF
:: Check DOS/Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax

:: Localize variables
SETLOCAL

:: Check command line arguments
IF NOT "%~1"=="" IF /I NOT "%~1"=="/D" IF /I NOT "%~1"=="/Y" GOTO Syntax

:: Check if debug mode is requested
IF /I NOT "%Debug%"=="ECHO" SET Debug=
ECHO.%* | FIND /I "/D" >NUL && SET Debug=ECHO

:: Check if confirmation is required
SET Confirmed=N
ECHO.%* | FIND /I "/Y" >NUL && SET Confirmed=Y

:: Check if DEVCON.EXE is available:
set DEVCON3264=devcon
if "%PROCESSOR_ARCHITECTURE%" == "AMD64" set DEVCON3264=devcon64

SET DevconAvailable=
%DEVCON3264% /? >NUL 2>&1
:: Abort if DEVCON.EXE is not available yet
IF "%DevconAvailable%"=="No" GOTO End

:: Ask for confirmation, unless overruled by command line switch
IF NOT "%Confirmed%"=="Y" (
ECHO You are about to remove all inactive PnP devices from this computer.
SET /P Confirmed=Are you sure you want to continue? [yN]
)
IF /I NOT "%Confirmed%"=="Y" GOTO Syntax

:: Save a list of all active devices in a temporary file
%DEVCON3264% Find * | FIND /I /V "matching device(s)" > "%Temp%\DevconFind.txt"
IF NOT EXIST "%Temp%\DevconFind.txt" GOTO Syntax

:: List all devices, both hidden/inactive and active ones, and remove
:: all devices that are not listed as active in the temporary file
FOR /F "tokens=1 delims=: " %%A IN ('%DEVCON3264% FindAll * ^| FIND /I /V "matching device(s)"') DO (
TYPE "%Temp%\DevconFind.txt" | FIND "%%~A" >NUL
IF ERRORLEVEL 1 %Debug% %DEVCON3264% Remove "@%%~A"
)

:: Remove the temporary file
DEL "%Temp%\DevconFind.txt"

:: Done
ENDLOCAL
GOTO:EOF


:Syntax
ECHO.
ECHO RmHidDev.bat,  Version 1.00 for Windows 2000 / XP
ECHO Use DEVCON to remove all hidden/inactive PnP devices.
ECHO.
ECHO Usage:  RMHIDDEV  [ /D ]  [ /Y ]
ECHO.
ECHO Where:  /D  Will only ECHO the removal commands instead of executing them
ECHO         /Y  Will remove devices without confirmation
ECHO.
ECHO Notes:  [1] This batch file requires Microsoft's DEVCON.EXE, available at
ECHO             http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272
ECHO             You will be prompted for download if it isn't found.
ECHO         [2] This batch file was developed with "ghost devices" on Ghost images
ECHO             in mind. It has been tested on Windows XP Ghost images only.
ECHO             I cannot guarantee flawless operation on any other PC.
ECHO             Use this batch file entirely at your own risk. Make sure you have
ECHO             a recent full backup available in case something might go wrong.
ECHO             And remember to test, test, test and test before using it in a
ECHO             production environment.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

IF "%OS%"=="Windows_NT" ENDLOCAL
pause

This batch file, via DevCon, will delete only USB devices with a VID of 0403 (FTDI):

Code: [Select]
@echo off
set PGM=devcon
if "%PROCESSOR_ARCHITECTURE%" == "AMD64" set PGM=devcon64
@echo Using %PGM% to remove any FTDI USB devices (If none listed, none were removed):
FOR /F "tokens=1 delims=: " %%A IN ('%PGM% FindAll * ^| FIND /I "VID_0403"') DO (
 %PGM% Remove "@%%~A"
)
pause

Using DevCon to remove all USB Devices.
BE CAREFUL IF YOU ARE USING A USB KEYBOARD AND MOUSE.
May have to unplug it and plug back in.

Code: [Select]
:: List and remove all USB devices
>> RenewUSB.dat ECHO.%Date%, %Time%
DEVCON FindAll =USB | FIND ":" >> RenewUSB.dat
FOR /F "tokens=1 delims=: " %%A IN ('DEVCON FindAll ^=USB ^| FIND ":"') DO DEVCON Remove "@%%A"
DEVCON FindAll USB* | FIND ":" >> RenewUSB.dat
FOR /F "tokens=1 delims=: " %%A IN ('DEVCON FindAll USB*  ^| FIND ":"') DO DEVCON Remove "@%%A"

:: Rescan for new hardware
DEVCON ReScan

That is from a larger BAT file written by Rob van der Woude
at  http://www.robvanderwoude.com .


ulao

  • Frequent Contributor
  • ****
  • Posts: 172
Re: usb gamepad order always random.
« Reply #7 on: June 14, 2017, 12:51:47 pm »
Sure Bret, I get that but I see the same behavior on linux. Granted its not usb, maybe more of an os thing. But I was not suggesting a fix for the usb, but a way to control the issue via usb.

devcon I believe I tried messing with but didnt have any luck. I make look in to that. Though using 4 ID's is still the best approach I have found. With the ID's I can change the usb names. player1,2,3,4 and they may not be in order but at lest they are specific.

C'est la vie