I'm converting an usb.h include file.
struct usb_device
{
struct usb_device *next, *prev;
char filename[LIBUSB_PATH_MAX];
struct usb_bus *bus;
struct usb_device_descriptor descriptor;
struct usb_config_descriptor *config;
void *dev; /* Darwin support */
unsigned char devnum;
unsigned char num_children;
struct usb_device **children;
};
Is this the correct conversion to ASM ?
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 --> I assume this is a pointer to a pointer ?
usb_device ends
are you using libusb ?
http://www.libusb.org/ (http://www.libusb.org/)
if not, the only MSDN definition i find is here
https://msdn.microsoft.com/en-us/library/ms923207.aspx (https://msdn.microsoft.com/en-us/library/ms923207.aspx)
I'm no good in C, the line struct usb_device *next, *prev; looks odd.
If you are unsure, and you can use it in C to compile a fake program, use e.g.
_asm {
int 3
lea eax, somestruct
lea ecx, somestruct.next
}
... and run it with Olly.
Hi Dave,
I use libusb-win32 http://sourceforge.net/projects/libusb-win32/files/latest/download?source=files
This lib is also used to communicate with the usb dongles used for Software Defined Radio.
Hi Jochen,
I don't have a C/C++ compiler installed.
I have converted a lot of C/C++ include files but now I'm also confused by these lines:
struct usb_device *next, *prev;
And is, "struct usb_config_descriptor *config;" a pointer to a struct or is it an included struct ???
Edit: corrected link to libusb-win32.
the structure translation is OK.
**x means pointer to pointer, struct foo *p is pointer to structure foo. In C you can have several declarators (next,prev) in one declaration - the asterisk * belongs to the declarator.
I'm no expert but a couple points,
struct usb_device *next, *prev
Evidently this implements a linked list, pointing at the next and prev structs in the list. So you ought to be able to represent them as two DWORDS, or, "ptr whatever". That reserves two DWORDS also, of course. A peculiar fact about JWasm (maybe ML also): once it sees that "ptr" it assumes a DWORD (or QWORD, in 64-bit). So you can write "ptr it_doesnt_matter_what_you_put_here" and it still works!
struct usb_config_descriptor *config
- I think it's a pointer to a struct, like the two above
Quote**children --> I assume this is a pointer to a pointer ?
- yes; which as far as asm struct cares, is just another pointer
[edit] wrote this b4 seeing qWord's post, guess it's still relevant
Quote from: rrr314159 on October 12, 2015, 10:33:24 AMSo you can write "ptr it_doesnt_matter_what_you_put_here" and it still works!
If
T is not declared for
"ptr T", MASM does a structure forward declaration. If
T is then used as anything else than a structure (or declaration of it), MASM throws an error.
qWord, thanks for the confirmation. :t
Quote from: qWordIf T is not declared for "ptr T", MASM does a structure forward declaration. If T is then used as anything else than a structure (or declaration of it), MASM throws an error.
- makes sense. But, interestingly, if T isn't declared at all, JWasm throws no error, reserves a DWORD or QWORD, no problem. You can then go ahead and use that pointer as usual
Quote from: rrr314159 on October 12, 2015, 11:17:47 AMBut, interestingly, if T isn't declared at all, JWasm throws no error, reserves a DWORD or QWORD, no problem.
indeed - I also recognized that MASM allows that (intended?). However an quick test also showed that using T as PROC name cause an internal assembler error (v6 and 8 tested).
regards
this include is tested with ml 6.15 on win 10,it work
Thanks ToutEnMasm,
I'll have a look at this one, is it from the win sdk ?
SIEKMANSKI,
Where the heck is usb.h ??? I downloaded the source and looked all through the files and could not find it,...although I found some very similar files,...
(I downloaded version 1.0.20)
try libusb.h
Could it be: struct libusb_device ???
...declared but not defined in libusb.h,...
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:
: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:
: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
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
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.
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.
cool :biggrin:
that one could make you a little money :t
added you on FB - i know a lot of hams in there
What USB Device Tree Viewer (http://www.uwe-sieber.de/usbtreeview_e.html) shows from that device?
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...
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;
}
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.
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 (https://msdn.microsoft.com/en-us/library/windows/hardware/ff550897(v=vs.85).aspx) 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 (https://msdn.microsoft.com/en-us/library/windows/hardware/ff549791(v=vs.85).aspx)
...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,...
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.
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 (https://github.com/libusb/libusb), 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 (https://github.com/libusb/libusb/wiki/Windows)
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)
...Interesting,...
Thanks for the information. And, thanks for alerting us to the possibilities,...:bgrin:
I'll spend some more time reading through all the USDLIB source and info.
...As you are well aware,...this is an excellent concept for a MASM project,...
libusb-win32 setup example source code.
So without proper driver libusb-win32 doesn't find anything ?
You have to install the libusb-win32 drivers for the usb-device you want to communicate with.
With these drivers it only finds those usb-devices.
This is what it shows on my PC,
libusb-win32 setup example.
usb busses count : 1
usb devices count : 2
bus-0 VID: 16C0, PID: 05DC
DeviceNumber: 1
Manufacturer: 1
Product : 2
SerialNumber: 0
bus-0 VID: 0BDA, PID: 2838
DeviceNumber: 2
Manufacturer: 1
Product : 2
SerialNumber: 3
Press any key to continue...
Those 2 usb devices are my USBasp avr programmer and my just bought RTL-SDR dongle. :biggrin:
Hi guys,
How do I translate this piece of C code to asm ?
a = 5;
b = 7;
c = (a & ~b);
mov eax, a
mov edx, b
not edx ; ~b
and eax, edx ; a & b
mov c, eax
it depends on whether you want "a" and "b" and "c" to be globals or locals
in the case of "a" and "b", they might even be constants (EQU or =)
they might also be arguments of a PROC...
SomeProc PROC a:DWORD,b:DWORD
mov eax,b
not eax
and eax,a
ret
SomeProc ENDP
something similar would be a macro :P
a = 5;
b = 7;
c = (a & ~b);
anwered by cl /Fa
; Line 12
mov DWORD PTR a$[rbp], 5
; Line 13
mov DWORD PTR b$[rbp], 7
; Line 14
mov eax, DWORD PTR b$[rbp]
not eax
mov ecx, DWORD PTR a$[rbp]
and ecx, eax
mov eax, ecx
mov DWORD PTR c$[rbp], eax
:biggrin:
Thank you very much guys. :t