Author Topic: WinUsb_Initialize fails  (Read 29236 times)

juliang

  • Member
  • ***
  • Posts: 13
WinUsb_Initialize fails
« on: January 10, 2011, 07:10:54 pm »
Hi,

Does anybody know why WinUsb_Initialize fails with error 22, ERROR_BAD_COMMAND ?

The application I am writing always return this when I call WinUsb_Initialize.

I really appreciate if someone can point me to the right direction, thanks in advance.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WinUsb_Initialize fails
« Reply #1 on: January 10, 2011, 08:49:59 pm »
Post your code.

Jan

juliang

  • Member
  • ***
  • Posts: 13
Re: WinUsb_Initialize fails
« Reply #2 on: January 11, 2011, 11:47:55 am »
bool GetDeviceHandle (GUID guidDeviceInterface, PHANDLE deviceHandle)
{
    if (guidDeviceInterface == GUID_NULL)
    {
        return false;
    }

    bool res = true;
    HDEVINFO deviceInfo;   

    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData = NULL;

    unsigned long requiredLength=0;

    char devicePath[1024];

    unsigned long index = 0;

    // Get information about all the installed devices for the specified
    // device interface class.
    deviceInfo = SetupDiGetClassDevs(
        &guidDeviceInterface,
        NULL,
        NULL,
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    if (deviceInfo == INVALID_HANDLE_VALUE)
    {
        // ERROR
        printf("Error SetupDiGetClassDevs: %d.\n", GetLastError());
        return false;
    }

    for (index = 0; index < 8; index++)
    {
        deviceInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

        //Get information about the device interface.
        res = SetupDiEnumDeviceInterfaces(
           deviceInfo,
           NULL,
           &guidDeviceInterface,
           index,
           &deviceInterfaceData);

        // Check if last item
        if (GetLastError () == ERROR_NO_MORE_ITEMS)
        {
            break;
        }

        //Check for some other error
        if (!res)
        {
            printf("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
   return false;
        }

        //Interface data is returned in SP_DEVICE_INTERFACE_DETAIL_DATA
        //which we need to allocate, so we have to call this function twice.
        //First to get the size so that we know how much to allocate
        //Second, the actual call with the allocated buffer
       
        res = SetupDiGetDeviceInterfaceDetail(
            deviceInfo,
            &deviceInterfaceData,
            NULL, // probing so no output buffer yet
         0, // probing so output buffer length of zero
            &requiredLength,
            NULL); // not interested in the specific dev-node


        //Check for some other error
        if (!res)
        {
            if ((ERROR_INSUFFICIENT_BUFFER == GetLastError()) && (requiredLength>0))
            {
                //we got the size, allocate buffer
                interfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, requiredLength);
               
                if (!interfaceDetailData)
                {
                    // ERROR
                    printf("Error allocating memory for the device detail buffer.\n");
                   return false;
                }
            }
            else
            {
                printf("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
                return false;
            }
        }

        //get the interface detailed data
        interfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        //Now call it with the correct size and allocated buffer
        res = SetupDiGetDeviceInterfaceDetail(
                deviceInfo,
                &deviceInterfaceData,
                interfaceDetailData,
                requiredLength,
                NULL,
                &deviceInfoData);
       
        //Check for some other error
        if (!res)
        {
            printf("Error SetupDiGetDeviceInterfaceDetail: %d.\n", GetLastError());
            return false;
        }

        //copy device path
        strcpy(devicePath, interfaceDetailData->DevicePath);                                       
        printf("Device path:  %s\n", devicePath);
      if (!devicePath)
      {
         //Error.
         printf("Error %d.", GetLastError());
         return false;
      }
      //Open the device
   *deviceHandle = CreateFile (
            devicePath,
                  GENERIC_READ | GENERIC_WRITE,
                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                  NULL,
                  OPEN_EXISTING,
                  FILE_FLAG_OVERLAPPED,
                  NULL);

       if (*deviceHandle == INVALID_HANDLE_VALUE)
      {
         //Error.
         printf("Error %d.", GetLastError());
         goto done;
      }
      else
      {

      return true;
      }

    }
       
    return false;
}


bool GetWinUSBHandle(HANDLE deviceHandle, PWINUSB_INTERFACE_HANDLE winUSBHandle)
{   

   if (deviceHandle == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    bool res = WinUsb_Initialize(deviceHandle, winUSBHandle);
    if (!res)
    {
        //Error.
        printf("WinUsb_Initialize Error %d.", GetLastError());
        return false;
    }
   

    return res;
}

int main(int argc,  char* argv[])
{

    GUID guidDeviceInterface = OMAPFLASH_USB; //in the INF file

    bool res = true;

    PIPE_ID pipeId;

    HANDLE deviceHandle = INVALID_HANDLE_VALUE;
    WINUSB_INTERFACE_HANDLE winUSBHandle = INVALID_HANDLE_VALUE;
   
    unsigned char deviceSpeed;
    unsigned int size = 0;

    res = false;
   do
   {
      res = GetDeviceHandle(guidDeviceInterface, &deviceHandle);
   }while(!res);

    res = GetWinUSBHandle(deviceHandle, &winUSBHandle);
}

Output:
WinUsb_Initialize Error 22.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WinUsb_Initialize fails
« Reply #3 on: January 11, 2011, 12:38:44 pm »
Are you sure deviceHandle is valid? This shows different code for copy device path before CreateFile:

http://msdn.microsoft.com/en-us/library/ff540174%28VS.85%29.aspx

Jan

juliang

  • Member
  • ***
  • Posts: 13
Re: WinUsb_Initialize fails
« Reply #4 on: January 11, 2011, 01:03:27 pm »
Thanks for you reply,

Even though I changed my code to match the one in the microsoft article, I still got the same error. So what else could be?

I also use your sample code winusb_cs and get an error when calling WinUsb_Initialize, I dont know what else to do, any suggestions?


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WinUsb_Initialize fails
« Reply #5 on: January 11, 2011, 01:10:05 pm »
Does Windows Device Manager show the winusb driver assigned to the device?

Jan

juliang

  • Member
  • ***
  • Posts: 13
Re: WinUsb_Initialize fails
« Reply #6 on: January 11, 2011, 02:39:34 pm »
No, actually I do not see my device in the device manager, however when I installed my device windows informed that the device was successfully installed.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WinUsb_Initialize fails
« Reply #7 on: January 11, 2011, 03:54:07 pm »
You should see it in Device Manager. Also take a look at the setupapi log file contents after attaching the device.

Jan

juliang

  • Member
  • ***
  • Posts: 13
Re: WinUsb_Initialize fails
« Reply #8 on: January 11, 2011, 06:08:18 pm »
This what my setutapi.log file contains, it looks like there is an error but only because it could not find a default driver, so I think that is ok right?

[SetupAPI Log]
OS Version = 5.1.2600 Service Pack 3
Platform ID = 2 (NT)
Service Pack = 3.0
Suite = 0x0100
Product Type = 1
Architecture = x86
[2011/01/11 17:01:58 732.3 Driver Install]
#-019 Searching for hardware ID(s): usb\vid_0451&pid_d00e&rev_0000,usb\vid_0451&pid_d00e
#-018 Searching for compatible ID(s): usb\class_ff&subclass_ff&prot_ff,usb\class_ff&subclass_ff,usb\class_ff
#-198 Command line processed: C:\WINDOWS\system32\services.exe
#-166 Device install function: DIF_SELECTBESTCOMPATDRV.
#W059 Selecting best compatible driver failed. Error 0xe0000228: There are no compatible drivers for this device.
#W157 Default installer failed. Error 0xe0000228: There are no compatible drivers for this device.
[2011/01/11 17:02:00 2100.2]
#-199 Executing "C:\WINDOWS\system32\rundll32.exe" with command line: rundll32.exe newdev.dll,ClientSideInstall \\.\pipe\PNP_Device_Install_Pipe_0.{409A2A4E-C2F3-4CA9-904B-7345FB3D3C25}
#I060 Set selected driver.
#-019 Searching for hardware ID(s): usb\vid_0451&pid_d00e&rev_0000,usb\vid_0451&pid_d00e
#-018 Searching for compatible ID(s): usb\class_ff&subclass_ff&prot_ff,usb\class_ff&subclass_ff,usb\class_ff
#-166 Device install function: DIF_SELECTBESTCOMPATDRV.
#W059 Selecting best compatible driver failed. Error 0xe0000228: There are no compatible drivers for this device.
#W157 Default installer failed. Error 0xe0000228: There are no compatible drivers for this device.
#I060 Set selected driver.
#-019 Searching for hardware ID(s): usb\vid_0451&pid_d00e&rev_0000,usb\vid_0451&pid_d00e
#-018 Searching for compatible ID(s): usb\class_ff&subclass_ff&prot_ff,usb\class_ff&subclass_ff,usb\class_ff
#-019 Searching for hardware ID(s): usb\vid_0451&pid_d00e&rev_0000,usb\vid_0451&pid_d00e
#-018 Searching for compatible ID(s): usb\class_ff&subclass_ff&prot_ff,usb\class_ff&subclass_ff,usb\class_ff
#I022 Found "USB\VID_0451&PID_d00e" in c:\ti\usbdevice\omap_winusb.inf; Device: "WinUSB Demo"; Driver: "WinUSB Demo"; Provider: "Texas Instruments"; Mfg: "Texas Instruments"; Section name: "USB_Install".
#I087 Driver node not trusted, rank changed from 0x00000001 to 0x00008001.
#I023 Actual install section: [USB_Install]. Rank: 0x00008001. Effective driver date: 01/05/2010.
#-166 Device install function: DIF_SELECTBESTCOMPATDRV.
#I063 Selected driver installs from section [USB_Install] in "c:\ti\usbdevice\omap_winusb.inf".
#I320 Class GUID of device remains: {36FC9E60-C465-11CF-8056-444553540000}.
#I060 Set selected driver.
#I058 Selected best compatible driver.
#-166 Device install function: DIF_SELECTBESTCOMPATDRV.
#I063 Selected driver installs from section [USB_Install] in "c:\ti\usbdevice\omap_winusb.inf".
#I320 Class GUID of device remains: {36FC9E60-C465-11CF-8056-444553540000}.
#I060 Set selected driver.
#I058 Selected best compatible driver.
#-124 Doing copy-only install of "USB\VID_0451&PID_D00E\5&425E0F2&0&8".
#E366 An unsigned or incorrectly signed file "c:\ti\usbdevice\omap_winusb.inf" for driver "WinUSB Demo" will be installed (Policy=Warn, user said ok). Error 0xe000022f: The third-party INF does not contain digital signature information.
#W187 Install failed, attempting to restore original files.
#E362 An unsigned or incorrectly signed file "c:\ti\usbdevice\omap_winusb.inf" for driver "WinUSB Demo" will be installed (Policy=Warn). Error 0xe000022f: The third-party INF does not contain digital signature information.
#-024 Copying file "c:\ti\usbdevice\i386\WinUSBCoInstaller2.dll" to "C:\WINDOWS\system32\WinUSBCoInstaller2.dll".
#E362 An unsigned or incorrectly signed file "c:\ti\usbdevice\omap_winusb.inf" for driver "WinUSB Demo" will be installed (Policy=Warn). Error 0xe000022f: The third-party INF does not contain digital signature information.
#-336 Copying file "c:\ti\usbdevice\i386\WdfCoInstaller01009.dll" to "C:\WINDOWS\system32\WdfCoInstaller01009.dll" via temporary file "C:\WINDOWS\system32\SET3E.tmp".
#E362 An unsigned or incorrectly signed file "c:\ti\usbdevice\omap_winusb.inf" for driver "WinUSB Demo" will be installed (Policy=Warn). Error 0xe000022f: The third-party INF does not contain digital signature information.
#-336 Copying file "c:\ti\usbdevice\i386\WUDFUpdate_01009.dll" to "C:\WINDOWS\system32\WUDFUpdate_01009.dll" via temporary file "C:\WINDOWS\system32\SET41.tmp".
#E362 An unsigned or incorrectly signed file "c:\ti\usbdevice\omap_winusb.inf" for driver "WinUSB Demo" will be installed (Policy=Warn). Error 0xe000022f: The third-party INF does not contain digital signature information.
#-166 Device install function: DIF_REGISTER_COINSTALLERS.
#I056 Coinstallers registered.
#-166 Device install function: DIF_INSTALLINTERFACES.
#-011 Installing section [USB_Install.Interfaces] from "c:\ti\usbdevice\omap_winusb.inf".
#I054 Interfaces installed.
#-166 Device install function: DIF_INSTALLDEVICE.
#I123 Doing full install of "USB\VID_0451&PID_D00E\5&425E0F2&0&8".
#E362 An unsigned or incorrectly signed file "c:\ti\usbdevice\omap_winusb.inf" for driver "WinUSB Demo" will be installed (Policy=Warn). Error 0xe000022f: The third-party INF does not contain digital signature information.
#I121 Device install of "USB\VID_0451&PID_D00E\5&425E0F2&0&8" finished successfully.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WinUsb_Initialize fails
« Reply #9 on: January 11, 2011, 07:58:16 pm »
Yes, it looks like it installed the driver from your unsigned INF file.

Jan

juliang

  • Member
  • ***
  • Posts: 13
Re: WinUsb_Initialize fails
« Reply #10 on: January 12, 2011, 11:18:12 am »
What do you reckon is the problem then?

Is there any chance that it could be a hw issue? I mean that my hw were not compatible with winusb?

Sorry to bother you with so many questions, but I have tried almost everything and I have been looking in the web for a similar problem and I could not find anything.

Thanks

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WinUsb_Initialize fails
« Reply #11 on: January 12, 2011, 01:06:28 pm »
It's the first WinUSB function called. Does your application know where to look? See How to Use the WinUSB API in the howto doc here:

http://www.microsoft.com/whdc/connect/usb/winusb_howto.mspx

Jan

juliang

  • Member
  • ***
  • Posts: 13
Re: WinUsb_Initialize fails
« Reply #12 on: January 21, 2011, 01:03:52 pm »
I have read the document you sent me and still I don't get why I am receiving that error, I successfully created the file and got the file handle, I used that file handle to call WinUsb_Initialize and I received error 22. I got the same code as in the sample Microsoft provide in the white paper. What else can I do?

Thanks in advance

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WinUsb_Initialize fails
« Reply #13 on: January 21, 2011, 02:12:24 pm »
Did you do all of these (from the Microsoft doc):

   Include WinUsb.h. It is included with the WDK, under WINDDK\BuildNumber\inc\ddk.
   Add WinUsb.lib to the list of libraries that are linked to your application. WinUsb.lib is included with the WDK. The version for Windows XP is located under WINDDK\BuildNumber\lib\wxp\i386. There are separate versions of WinUsb.lib for Windows Vista for each supported CPU architecture. They are located under the WINDDK\BuildNumber\lib\wlh folder.
   Include Usb100.h, which is also under WINDDK\BuildNumber\inc\ddk. This header is not required, but it contains declarations for some useful macros.


Jan

juliang

  • Member
  • ***
  • Posts: 13
Re: WinUsb_Initialize fails
« Reply #14 on: January 21, 2011, 02:38:18 pm »
Yes, I did all of them ... and still do know what is going on