News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

converting an usb.h include file

Started by Siekmanski, October 12, 2015, 09:22:31 AM

Previous topic - Next topic

dedndave

right - i couldn't find the structure definition, either - lol

maybe Marinus could show us the original C definition, we don't have to look for it   :biggrin:

Zen

:icon_eek:...I suspect that extraterrestrials have hacked SIEKMANSKI's account,...:icon_eek:

We've all noticed that SIEKMANSKI always has great ideas for projects,...he is no slacker,...
:bgrin:...I'm just trying to hijack his USB project before he posts the source,...:bgrin:

Siekmanski

 :biggrin:

I noticed that my previous link was not the right one, sorry my mistake.

Here you can download the latest version: http://sourceforge.net/projects/libusb-win32/files/latest/download?source=files

libusb-win32-bin-1.2.6.0/include/lusb0_usb.h
Creative coders use backward thinking techniques as a strategy.

Siekmanski

Here is the include file, I didn't test it yet ...

LIBUSB_PATH_MAX             EQU 512

; * USB spec information
; *
; * This is all stuff grabbed from various USB specs and is pretty much
; * not subject to change

; * Device and/or Interface Class codes
USB_CLASS_PER_INTERFACE     EQU 0   ; * for DeviceClass
USB_CLASS_AUDIO             EQU 1
USB_CLASS_COMM              EQU 2
USB_CLASS_HID               EQU 3
USB_CLASS_PRINTER           EQU 7
USB_CLASS_MASS_STORAGE      EQU 8
USB_CLASS_HUB               EQU 9
USB_CLASS_DATA              EQU 10
USB_CLASS_VENDOR_SPEC       EQU 0ffh

; * Descriptor types
USB_DT_DEVICE               EQU 01h
USB_DT_CONFIG               EQU 02h
USB_DT_STRING               EQU 03h
USB_DT_INTERFACE            EQU 04h
USB_DT_ENDPOINT             EQU 05h
USB_DT_HID                  EQU 21h
USB_DT_REPORT               EQU 22h
USB_DT_PHYSICAL             EQU 23h
USB_DT_HUB                  EQU 29h

; * Descriptor sizes per descriptor type
USB_DT_DEVICE_SIZE          EQU 18
USB_DT_CONFIG_SIZE          EQU 9
USB_DT_INTERFACE_SIZE       EQU 9
USB_DT_ENDPOINT_SIZE        EQU 7
USB_DT_ENDPOINT_AUDIO_SIZE  EQU 9   ; * Audio extension
USB_DT_HUB_NONVAR_SIZE      EQU 7

; * All standard descriptors have these 2 fields in common
usb_descriptor_header   struct
    bLength             BYTE ?
    bDescriptorType     BYTE ?
usb_descriptor_header   ends

usb_string_descriptor   struct
    bLength             BYTE ?
    bDescriptorType     BYTE ?
    wData               WORD ?
usb_string_descriptor   ends

usb_hid_descriptor      struct
    bLength             BYTE ?
    bDescriptorType     BYTE ?
    bcdHID              WORD ?
    bCountryCode        BYTE ?
    bNumDescriptors     BYTE ?
usb_hid_descriptor      ends

USB_MAXENDPOINTS            EQU 32

usb_endpoint_descriptor struct
    bLength             BYTE ?
    bDescriptorType     BYTE ?
    bEndpointAddress    BYTE ?
    bmAttributes        BYTE ?
    wMaxPacketSize      WORD ?
    bInterval           BYTE ?
    bRefresh            BYTE ?
    bSynchAddress       BYTE ?
    extra               DWORD ? ;* Extra descriptors
    extralen            DWORD ?
usb_endpoint_descriptor ends

USB_ENDPOINT_ADDRESS_MASK       EQU 0fh ;* in bEndpointAddress
USB_ENDPOINT_DIR_MASK           EQU 80h

USB_ENDPOINT_TYPE_MASK          EQU 03h ;* in bmAttributes
USB_ENDPOINT_TYPE_CONTROL       EQU 0
USB_ENDPOINT_TYPE_ISOCHRONOUS   EQU 1
USB_ENDPOINT_TYPE_BULK          EQU 2
USB_ENDPOINT_TYPE_INTERRUPT     EQU 3
USB_MAXINTERFACES               EQU 32

usb_interface_descriptor struct
    bLength             BYTE ?
    bDescriptorType     BYTE ?
    bInterfaceNumber    BYTE ?
    bAlternateSetting   BYTE ?
    bNumEndpoints       BYTE ?
    bInterfaceClass     BYTE ?
    bInterfaceSubClass  BYTE ?
    bInterfaceProtocol  BYTE ?
    iInterface          BYTE ?
    endpoint            DWORD ?
    extra               DWORD ? ;* Extra descriptors
    extralen            DWORD ?
usb_interface_descriptor ends

USB_MAXALTSETTING               EQU 128 ;* Hard limit

usb_interface           struct
    altsetting          usb_interface_descriptor <>
    num_altsetting      DWORD ?
usb_interface           ends

;* Configuration descriptor information..
USB_MAXCONFIG                   EQU 8

usb_config_descriptor   struct
    bLength             BYTE ?
    bDescriptorType     BYTE ?
    wTotalLength        WORD ?
    bNumInterfaces      BYTE ?
    bConfigurationValue BYTE ?
    iConfiguration      BYTE ?
    bmAttributes        BYTE ?
    MaxPower            BYTE ?
    interface           DWORD ?
    extra               DWORD ? ;* Extra descriptors
    extralen            DWORD ?
usb_config_descriptor   ends

usb_device_descriptor   struct
    bLength             BYTE ?
    bDescriptorType     BYTE ?
    bcdUSB              WORD ?
    bDeviceClass        BYTE ?
    bDeviceSubClass     BYTE ?
    bDeviceProtocol     BYTE ?
    bMaxPacketSize0     BYTE ?
    idVendor            WORD ?
    idProduct           WORD ?
    bcdDevice           WORD ?
    iManufacturer       BYTE ?
    iProduct            BYTE ?
    iSerialNumber       BYTE ?
    bNumConfigurations  BYTE ?
usb_device_descriptor   ends

usb_ctrl_setup          struct
    bRequestType        BYTE ?
    bRequest            BYTE ?
    wValue              WORD ?
    wIndex              WORD ?
    wLength             WORD ?
usb_ctrl_setup          ends

; * Standard requests
USB_REQ_GET_STATUS              EQU 00h
USB_REQ_CLEAR_FEATURE           EQU 01h
;* 02h is reserved
USB_REQ_SET_FEATURE             EQU 03h
;* 04h is reserved
USB_REQ_SET_ADDRESS             EQU 05h
USB_REQ_GET_DESCRIPTOR          EQU 06h
USB_REQ_SET_DESCRIPTOR          EQU 07h
USB_REQ_GET_CONFIGURATION       EQU 08h
USB_REQ_SET_CONFIGURATION       EQU 09h
USB_REQ_GET_INTERFACE           EQU 0Ah
USB_REQ_SET_INTERFACE           EQU 0Bh
USB_REQ_SYNCH_FRAME             EQU 0Ch

USB_TYPE_STANDARD               EQU ( 00h shl 5 )
USB_TYPE_CLASS                  EQU ( 01h shl 5 )
USB_TYPE_VENDOR                 EQU ( 02h shl 5 )
USB_TYPE_RESERVED               EQU ( 03h shl 5 )

USB_RECIP_DEVICE                EQU 00h
USB_RECIP_INTERFACE             EQU 01h
USB_RECIP_ENDPOINT              EQU 02h
USB_RECIP_OTHER                 EQU 03h

; * Various libusb API related stuff
USB_ENDPOINT_IN                 EQU 80h
USB_ENDPOINT_OUT                EQU 00h
; * Error codes
USB_ERROR_BEGIN                 EQU 500000

USB_LE16_TO_CPU macro x
    exitm <>
    endm

; * Device reset types for usb_reset_ex.
; * http://msdn.microsoft.com/en-us/library/ff537269%28VS.85%29.aspx
; * http://msdn.microsoft.com/en-us/library/ff537243%28v=vs.85%29.aspx
USB_RESET_TYPE_RESET_PORT       EQU ( 1 shl 0 )
USB_RESET_TYPE_CYCLE_PORT       EQU ( 1 shl 1 )
USB_RESET_TYPE_FULL_RESET       EQU ( USB_RESET_TYPE_CYCLE_PORT or USB_RESET_TYPE_RESET_PORT )

; * Data types
; * struct usb_device
; * struct usb_bus

usb_device              struct
    next                DWORD ?
    prev                DWORD ?
    filename            BYTE LIBUSB_PATH_MAX dup (?)
    bus                 DWORD ?
    descriptor          usb_device_descriptor <>
    config              DWORD ?
    dev                 DWORD ? ; * Darwin support
    devnum              BYTE ?
    num_children        BYTE ?
    children            DWORD ? ; **children
usb_device              ends

usb_bus                 struct
    next                DWORD ?
    prev                DWORD ?
    dirname             BYTE LIBUSB_PATH_MAX dup (?)
    devices             DWORD ?
    location            DWORD ?
    root_dev            DWORD ?
usb_bus                 ends

usb_version_info        struct
    major               DWORD   ?
    minor               DWORD   ?
    micro               DWORD   ?
    nano                DWORD   ?
usb_version_info        ends

usb_version             struct
    dll                 usb_version_info <>
    driver              usb_version_info <>
usb_version             ends

Creative coders use backward thinking techniques as a strategy.

TouEnMasm

More easy with a good link
Here the full translate of the original lusb0_usb.h,headinc translate with some manual corrections.
The usb.inc is a mixed win 10 defines with your posted structure.
Proto can be C prototype ,just change "PROTO" by "PROTO C" with an editor (Notepad).

Take care of this,from BenchmarkHelp.txt
Quote
WARNING:
          This program should only be used with USB devices which implement
          one more more "Benchmark" interface(s).  Using this application
          with a USB device it was not designed for can result in permanent
          damage to the device.

Fa is a musical note to play with CL

Siekmanski

Friday I get my DVB-T USB DONGLE with the R820T2 tuner chip € 24.50
Than my journey can start to communicate with this dongle and try to create a Software Defined Radio app.

For testing purposes I installed the libusb-win32-bin-1.2.6.0 drivers for an USBasp atmel programmer.
First test to get the VID and PID is a succes.  :biggrin:

Hi ToutEnMasm,
The drivers use c calling conventions.
Creative coders use backward thinking techniques as a strategy.

dedndave

cool   :biggrin:
that one could make you a little money   :t

added you on FB - i know a lot of hams in there

TWell


Siekmanski

Hi Dave,
I have just started and must see how far I will come.
But, I can always use some money hahaha..  :biggrin:
I'll have a look on FB although I'm not really active on it.

Hi TWell,
For now I have only written a simple usb_bus/device enumerator where I only search for a certain VID PID pair.
This is the output,


LibUsb routines 2015.

return usb_init: 1
return usb_find_busses: 1
return usb_find_devices: 1
return usb_get_busses: 8659856
bus-0 VID: 16C0, PID: 05DC

Press any key to continue...

Creative coders use backward thinking techniques as a strategy.

TWell

#24
EDIT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

TCHAR* UsbRegDevPath(TCHAR *szName, int nLen, int nIdx)
{
HKEY hKey;
DWORD dwIdx;
TCHAR szUsbDev[] = TEXT("SYSTEM\\CurrentControlSet\\Control\\DeviceClasses\\{a5dcbf10-6530-11d2-901f-00c04fb951ed}");
TCHAR szSub[260];

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szUsbDev, 0, KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS) {
dwIdx = 0;
if (RegEnumKey(hKey, nIdx, szSub, sizeof(szSub)) == ERROR_SUCCESS) {
if (szSub[0] == '#') szSub[0] = '\\';
if (szSub[1] == '#') szSub[1] = '\\';
if (szSub[3] == '#') szSub[3] = '\\';
lstrcpyn(szName, szSub,nLen);
} else return NULL;
RegCloseKey(hKey);
return szName;
} else return NULL;
}

int main(int argc, char **argv)
{
TCHAR szPath[260];
for (int i=0; i<100; i++) {
if (!UsbRegDevPath(szPath, 260, i)) break;
printf("%2i %s\n", i, szPath);
}
return 0;
}
EDIT#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ole2.h>
#include <setupapi.h>
#include <stdio.h>

#pragma comment(lib, "setupapi.lib")
// GUID_DEVINTERFACE_USB_DEVICE
static GUID GUID_DEVINTERFACE_USB_DEVICE =
{0xA5DCBF10L, 0x6530, 0x11D2,{0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}};

int main(void)
{
HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (INVALID_HANDLE_VALUE != hDevInfo)
{
SP_DEVICE_INTERFACE_DATA did = { sizeof(did) };
int iDev = 0;
while (SetupDiEnumDeviceInterfaces(hDevInfo, 0, &GUID_DEVINTERFACE_USB_DEVICE, iDev++, &did))
{
DWORD cbRequired = 0;

SetupDiGetDeviceInterfaceDetail(hDevInfo, &did, 0, 0, &cbRequired, 0);
if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR,cbRequired);
if (pdidd)
{
pdidd->cbSize = sizeof(*pdidd);
if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &did, pdidd, cbRequired, &cbRequired, 0))
{
printf("%s\n", pdidd->DevicePath);
}
LocalFree(pdidd);
}
}

}
}
return 0;
}

Siekmanski

Thanks TWell,

I was also thinking to check the registry and see if an usb device that's hooked up but not installed yet showed up in it.
I don't know how it works to check out uninstalled usb devices.
I'll test it with your code example.
This could be handy to make an automatic libusb installer for the tuner device from the SDR app.

In my little test app I get all the device info which I need from the libusb driver, to find the wanted device and to open it.
Creative coders use backward thinking techniques as a strategy.

Zen

SIEKMANSKI, I'm just posting this to DESTROY YOUR MIND (Ha, Ha) :bgrin:

...But,...(and, I suspect you already know this,...) you can obtain limited information about installed or previously installed hardware devices (in user mode) by using: SetupAPI Reference, MSDN methods.
Those functions do make registry entries, and you can conveniently check a manufacturer device identifier against an existing registry value to determine if any device has been previously installed and then disconnected.
Here are the really FUN ones: Public Device Installation Functions, MSDN

...But,...DON'T DO THIS,...IT'S FUNKY,...:icon_eek:
...You'll end up with a Zen-like UBER-BLOATED app,...and everyone here will make derisive remarks about your 'unconventional' coding style,...

Siekmanski

Zen, no chance you can destroy my mind. ( hahahaha.. ).  :bgrin:
All information is welcome, thanks.  :t

It's not really necessary but it would be a nice feature to get the drivers installed for the user.
Creative coders use backward thinking techniques as a strategy.

Zen

SIEKMANSKI,
Great concept for a project,...and, clearly quite useful,...
...But, I noticed upon downloading the correct version that you mentioned above,...that there is no source code for the device driver or the USBLIB library file,...kind of disappointing. There are, however, a number of source code versions available on the GitHub, libusb libusb website, and I assume that they are almost compatible,...(but, they have different version numbers),...
...So,...the obvious question is: does the driver work as advertised ???
Don't windows drivers have to be verified (signed) by Microsoft in operating system versions later than Windows XP ???
How to use libusb on Windows

Siekmanski

Hi Zen,
The library source code you can find in libusb-win32-bin-1.2.6.0\lib\dynamic\libusb_dyn.c
The functions from the libusb are different then those from libusb-win32.
This is all very confusing because, inside libusb-win32 they also speak of libusb.
I have decided to use libusb-win32 to communicate with the devices and it works great until now...
I'm now testing the "usb_control_msg" function to send and receive I2C commands.
This protocol is used by the RTL sdr dongles, can't wait till friday to buy the dongle.  :biggrin:

I have used the "inf-wizard.exe" to install the usb drivers to test it on my usb avr programmer. ( located in libusb-win32-bin-1.2.6.0\bin\inf-wizard.exe )
You have to plug in the usb device and run inf-wizard.exe as administrator, choose your device from the list and install it.
No complaining from windows about unverified/unsigned drivers. ( tested on XP and Win 8.1)
Creative coders use backward thinking techniques as a strategy.