Announcements => Third party => Topic started by: DonAlfredo on February 22, 2015, 01:14:09 pm
Title: USB HID access on Linux
Post by: DonAlfredo on February 22, 2015, 01:14:09 pm
Hello to all,
Please find on GitHub (https://github.com/LongDirtyAnimAlf/FPC-USB-HID (https://github.com/LongDirtyAnimAlf/FPC-USB-HID)) , a (my) library to access USB HID devices on Linux with FPC. Its a (limited) clone (port) from the jedi jvcl JvHidControllerClass. Very usefull for accessing small MCU's with buildin HID-firmware.
Alfred.
Title: Re: USB HID access on Linux
Post by: wolfdreamer on April 26, 2015, 12:09:40 pm
i'm interest in USB but with win32. is there something like that?
Title: Re: USB HID access on Linux
Post by: DonAlfredo on April 26, 2015, 04:28:33 pm
Yes, there is ! It's the original jedi jvcl JvHidControllerClass. You have to adapt it to work with FPC, but it can (easily, but with some effort) be done !
Title: Re: USB HID access on Linux
Post by: Fred vS on April 26, 2015, 10:44:05 pm
Quote
Please find on GitHub (https://github.com/LongDirtyAnimAlf/FPC-USB-HID) ,
EXCELLENT => IT IS A MUST ;D
Many thanks.
Fre;D
Title: Re: USB HID access on Linux
Post by: Fred vS on April 26, 2015, 11:07:46 pm
Ok, perfect, have tested it on Linux 64 Mint => It works ;D
Hum, little detail, in your demo FpcUsbLinuxHid.lpi you have set CPU to "arm". Maybe you could set it as "Default" so it can be compiled if CPU <> arm . :-X
Many thanks for this great utility.
Fre;D
Title: Re: USB HID access on Linux
Post by: BLL on December 20, 2015, 12:53:43 pm
H, I have run your code on my Raspberry Pi and it seems to work. I have changed the Vendor and Product values for my Ventus weather station. What do I need to do to see any data from it please? I am new to all this!
Brian
fpc 3.1.1, lazarus 1.5
Title: Re: USB HID access on Linux
Post by: DonAlfredo on December 20, 2015, 02:48:27 pm
As I do not know the details of your system, I have to guess.
It is important to add the udev-rules !! Otherwise, you do not have hardware access (as ordinary user). Also adapt these rules and fill them with your Vendor/Device.
Title: Re: USB HID access on Linux
Post by: BLL on December 29, 2015, 04:22:01 pm
Hi, Thanks for the reply. I already have the udev.rules as I can read the weather station data in a C program called te923con but it locks up the RasPi randomly. I would prefer to access the data via my lazarus program. In your demo program, the first two buttons work, but the 3rd gives no serial. I got info about the weather station from lsusb -vvv as follows:
Title: Re: USB HID access on Linux
Post by: DonAlfredo on December 29, 2015, 04:38:50 pm
Do you have to query your device to get data. Or does it send by itself ?
You could also give me some insight in the datalogger you use ....
ps: the getserial button is meant for my own hardware (it will not work for you) ... inclusion in the general demo is therefor a mistake by me !
Title: Re: USB HID access on Linux
Post by: BLL on December 30, 2015, 10:21:42 pm
Hi, Thanks for the reply. The weather station is a Ventus W831 (aka Honeywell TE923). I have been trying to use a C program te923con from http://te923.fukz.org/, but it is unreliable and intermittently seizes the RasPi! It also has to be called as an external program (I have used runCommand), which is not very elegant!. The W831 returns a series of data values when asked to do so. I then process these in my program and display them. I have looked at his C code but I don't follow it. I need to be able to get the W831 to give me its data once every 60 seconds, but I don't know how to proceed, despite having read all I can about HIDs! Ihope this provides you with what you asked, in conjunction with my previous post with the lsusb -vvv output. Brian
Title: Re: USB HID access on Linux
Post by: mark on January 31, 2017, 01:54:54 pm
@DonAlfredo : Thank you for the conversion. To all : i am new with Lazarus. But with some help i got it going on a PI 2. The sample compiles but i do not see any device pulled or plugged. I assume i have to click the create and enable buttons first, so i did.
The rules file i copied and altered. Unlike the tjvhid windows which shows all usb devices, i understand that i can only list 1 device in the sample. So i altered the vid & pid. The rules are confusing to me. I spend a whole day without anything going. I read/googled and tried various things. But the more i read/study, the more confused i get. The sample rules file : # HIDAPI/libusb SUBSYSTEM=="usbmisc", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="003f", MODE="0666"
the usbmisc i can not find a reference too. I tried usbmisc, usb and isb_device. I also made a file that starts with 98 but that did not work either.
i monitored the usb devices with udevadm . it shows devices plugged and pulled. I also tried hidraw. But no luck. What i try to do is to see a mouse plugged in. That is a HID device. Both the rules file and the sample are altered to use the vid/pid of the mouse(an addiitonal mouse). I feel really stupid because the instructions are clear, and i seem to be the only one not getting this going on a pi :'( I hope some one has an idea what else i can test.
Title: Re: USB HID access on Linux
Post by: DonAlfredo on January 31, 2017, 03:16:58 pm
No worries; keep the faith; eventually we must be able to get it to work !
First, do: lsusb -vvv This will list the connected devices.
Besides, your USB devices should be listed under /dev/usb or /dev or /dev/bus/usb
About the rules. SUBSYSTEM=="usbmisc", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="003f", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="28de", MODE="0666" KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="2833", MODE="0666" These rules are here to give a normal, non-root user access to these devices. And these rules should be added into eg: /etc/udev/rules.d/20-usb-rules-by-mark.rules
If you have the correct VID and PID, you have to add them ito USB2.pas:
Please perform the above steps first. And report the results.
Title: Re: USB HID access on Linux
Post by: mark on January 31, 2017, 03:41:56 pm
Thank you for your reply :)
This is with no plugged devices : Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Then with the device (mouse) plugged : Bus 001 Device 004: ID 046d:c058 Logitech, Inc. M115 Mouse Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Then the -vvv output : (using sudo) pi@raspberrypi:~ $ sudo lsusb -vvv
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 255 Vendor Specific Class bDeviceSubClass 0 bDeviceProtocol 1 bMaxPacketSize0 64 idVendor 0x0424 Standard Microsystems Corp. idProduct 0xec00 SMSC9512/9514 Fast Ethernet Adapter bcdDevice 2.00 iManufacturer 0 iProduct 0 iSerial 0 bNumConfigurations 1 Configuration Descriptor: bLength 9 bDescriptorType 2 wTotalLength 39 bNumInterfaces 1 bConfigurationValue 1 iConfiguration 0 bmAttributes 0xe0 Self Powered Remote Wakeup MaxPower 2mA Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 0 bNumEndpoints 3 bInterfaceClass 255 Vendor Specific Class bInterfaceSubClass 0 bInterfaceProtocol 255 iInterface 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x81 EP 1 IN bmAttributes 2 Transfer Type Bulk Synch Type None Usage Type Data wMaxPacketSize 0x0200 1x 512 bytes bInterval 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x02 EP 2 OUT bmAttributes 2 Transfer Type Bulk Synch Type None Usage Type Data wMaxPacketSize 0x0200 1x 512 bytes bInterval 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x83 EP 3 IN bmAttributes 3 Transfer Type Interrupt Synch Type None Usage Type Data wMaxPacketSize 0x0010 1x 16 bytes bInterval 4 Device Qualifier (for other device speed): bLength 10 bDescriptorType 6 bcdUSB 2.00 bDeviceClass 255 Vendor Specific Class bDeviceSubClass 0 bDeviceProtocol 1 bMaxPacketSize0 64 bNumConfigurations 1 Device Status: 0x0001 Self Powered
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 9 Hub bDeviceSubClass 0 Unused bDeviceProtocol 2 TT per port bMaxPacketSize0 64 idVendor 0x0424 Standard Microsystems Corp. idProduct 0x9514 bcdDevice 2.00 iManufacturer 0 iProduct 0 iSerial 0 bNumConfigurations 1 Configuration Descriptor: bLength 9 bDescriptorType 2 wTotalLength 41 bNumInterfaces 1 bConfigurationValue 1 iConfiguration 0 bmAttributes 0xe0 Self Powered Remote Wakeup MaxPower 2mA Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 0 bNumEndpoints 1 bInterfaceClass 9 Hub bInterfaceSubClass 0 Unused bInterfaceProtocol 1 Single TT iInterface 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x81 EP 1 IN bmAttributes 3 Transfer Type Interrupt Synch Type None Usage Type Data wMaxPacketSize 0x0001 1x 1 bytes bInterval 12 Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 1 bNumEndpoints 1 bInterfaceClass 9 Hub bInterfaceSubClass 0 Unused bInterfaceProtocol 2 TT per port iInterface 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x81 EP 1 IN bmAttributes 3 Transfer Type Interrupt Synch Type None Usage Type Data wMaxPacketSize 0x0001 1x 1 bytes bInterval 12 Hub Descriptor: bLength 9 bDescriptorType 41 nNbrPorts 5 wHubCharacteristic 0x000d Per-port power switching Compound device Per-port overcurrent protection TT think time 8 FS bits bPwrOn2PwrGood 50 * 2 milli seconds bHubContrCurrent 1 milli Ampere DeviceRemovable 0x02 PortPwrCtrlMask 0xff Hub Port Status: Port 1: 0000.0503 highspeed power enable connect Port 2: 0000.0303 lowspeed power enable connect Port 3: 0000.0100 power Port 4: 0000.0100 power Port 5: 0000.0100 power Device Qualifier (for other device speed): bLength 10 bDescriptorType 6 bcdUSB 2.00 bDeviceClass 9 Hub bDeviceSubClass 0 Unused bDeviceProtocol 0 Full speed (or root) hub bMaxPacketSize0 64 bNumConfigurations 1 Device Status: 0x0001 Self Powered
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 9 Hub bDeviceSubClass 0 Unused bDeviceProtocol 1 Single TT bMaxPacketSize0 64 idVendor 0x1d6b Linux Foundation idProduct 0x0002 2.0 root hub bcdDevice 4.04 iManufacturer 3 Linux 4.4.34-v7+ dwc_otg_hcd iProduct 2 DWC OTG Controller iSerial 1 3f980000.usb bNumConfigurations 1 Configuration Descriptor: bLength 9 bDescriptorType 2 wTotalLength 25 bNumInterfaces 1 bConfigurationValue 1 iConfiguration 0 bmAttributes 0xe0 Self Powered Remote Wakeup MaxPower 0mA Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 0 bNumEndpoints 1 bInterfaceClass 9 Hub bInterfaceSubClass 0 Unused bInterfaceProtocol 0 Full speed (or root) hub iInterface 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x81 EP 1 IN bmAttributes 3 Transfer Type Interrupt Synch Type None Usage Type Data wMaxPacketSize 0x0004 1x 4 bytes bInterval 12 Hub Descriptor: bLength 9 bDescriptorType 41 nNbrPorts 1 wHubCharacteristic 0x0008 Ganged power switching Per-port overcurrent protection TT think time 8 FS bits bPwrOn2PwrGood 1 * 2 milli seconds bHubContrCurrent 0 milli Ampere DeviceRemovable 0x00 PortPwrCtrlMask 0xff Hub Port Status: Port 1: 0000.0503 highspeed power enable connect Device Status: 0x0001 Self Powered
It learns me : idVendor 0x046d Logitech, Inc. idProduct 0xc058 M115 Mouse
Then i made the rules file and using cat : SUBSYSTEM=="usbmisc", ATTRS{idVendor}=="046d" ,ATTRS{idProduct}=="c058", MODE="0666" SUBSYSTEM=="usb",ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c058", MODE="0666" KERNEL=="hidraw", SUBSYSTEM=="hidraw", ATTRS{idVendor}="c058" , MODE="0666"
finally i changed the consts:
const Vendor = $046d; Product = $c058;
compile and run. then press HIDcreate button, then HIDenable button Then i pull and plug the mouse, but nothing happens. btnINfo says : no new usb info.
Are the rules ok? Or do you see any mistake?
Title: Re: USB HID access on Linux
Post by: DonAlfredo on January 31, 2017, 04:30:07 pm
I have made some small changes into the GitHub repo. Should give you better user-feedback. Please try and report back !
Title: Re: USB HID access on Linux
Post by: mark on January 31, 2017, 04:52:48 pm
Thanks. I downloaded the files. Put them on my PI. I got compile error in windows unit, so i changed build target to linuxarmhf I also changed my vid and pid and run the app. I see the serial button is removed. I click HID create, hid enable. then pull and insert usb. But nothing. This works for you on a PI ? Must the program have some rights?
Title: Re: USB HID access on Linux
Post by: DonAlfredo on January 31, 2017, 05:12:41 pm
Will dive into it. Please stay tuned.
Title: Re: USB HID access on Linux
Post by: DonAlfredo on January 31, 2017, 09:38:00 pm
I think your mouse is not detected as a real hiddev on Linux. I guess it shows up as a hidraw device.
Therefor, I have updated the GitRepo to show hidraw. Please test as previous. I hope that you will succeed this time.
Title: Re: USB HID access on Linux
Post by: mark on January 31, 2017, 10:56:26 pm
ok, thanks, i will test and let you know. because i also thought it would be a good idea to use something else, i also test with a different hid device. But that also did not work. It appears that the rule is not executed. But because of being new it is hard to debug/test. So i first try your update.
Title: Re: USB HID access on Linux
Post by: mark on January 31, 2017, 11:03:56 pm
Hello DonAlfredo
Now it works :) :) :) And the thing : i only had to change the pid/vid in the code. What did you change that fixed it? Well i can check that of course. I appreciate the help. Now i can continue.
Title: Re: USB HID access on Linux
Post by: DonAlfredo on February 01, 2017, 08:15:55 am
These defines allow you to choose ! As you can see, hidraw is defined in your use-case.
Title: Re: USB HID access on Linux
Post by: mark on February 01, 2017, 10:43:49 am
thanks. In the mean time I tried a different HID device, made all the changes, but i seem to have the old problem : nothing listed again. It appears that using usb on PI with lazarus is not a walk in the park. This time i tried an usb programmer : Bus 001 Device 010: ID 03eb:2104 Atmel Corp. AVR ISP mkII
and here is the lsb -vvv Bus 001 Device 010: ID 03eb:2104 Atmel Corp. AVR ISP mkII Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 1.10 bDeviceClass 255 Vendor Specific Class bDeviceSubClass 0 bDeviceProtocol 0 bMaxPacketSize0 16 idVendor 0x03eb Atmel Corp. idProduct 0x2104 AVR ISP mkII bcdDevice 2.00 iManufacturer 1 ATMEL iProduct 2 AVRISP mkII iSerial 3 0000A0005627 bNumConfigurations 1 Configuration Descriptor: bLength 9 bDescriptorType 2 wTotalLength 32 bNumInterfaces 1 bConfigurationValue 1 iConfiguration 0 bmAttributes 0xc0 Self Powered MaxPower 100mA Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 0 bNumEndpoints 2 bInterfaceClass 255 Vendor Specific Class bInterfaceSubClass 0 bInterfaceProtocol 0 iInterface 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x82 EP 2 IN bmAttributes 2 Transfer Type Bulk Synch Type None Usage Type Data wMaxPacketSize 0x0040 1x 64 bytes bInterval 10 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x02 EP 2 OUT bmAttributes 2 Transfer Type Bulk Synch Type None Usage Type Data wMaxPacketSize 0x0040 1x 64 bytes bInterval 10 Device Status: 0x0001 Self Powered
the new rules : pi@raspberrypi:~ $ cat /etc/udev/rules.d/20-usb-rules-by-mark.rules SUBSYSTEM=="usbmisc", ATTRS{idVendor}=="03eb" ,ATTRS{idProduct}=="2104", MODE="0666" SUBSYSTEM=="usb",ATTRS{idVendor}=="03eb", ATTRS{idVendor}=="2104", MODE="0666"
and i changed the code : Vendor = $03eb; Product = $2104;
when it did not work, i also tried with : {.$DEFINE hidraw} {$DEFINE hiddev}
But nothing. I was under assumption that all HID devices are the same with respect to jvHidcontroller. Under windows Delphi i use the Tjvhidcontroller for some years and it lists all HID devices. I hope you have some idea since this is a great component when using a PI.
Title: Re: USB HID access on Linux
Post by: DonAlfredo on February 01, 2017, 10:50:36 am
So, its no HID, and will not be detected by this component !
However, for FPC on Linux, you can also use libusb ! That will work with any USB device AFAIK.
Title: Re: USB HID access on Linux
Post by: mark on February 01, 2017, 10:54:03 am
Aha, you are right. I tested with the wrong device. I will dig up a home made hid device and test again. Thanks for your help.
Title: Re: USB HID access on Linux
Post by: mark on February 01, 2017, 11:19:25 am
I retested with a proper HID device and your latest code works perfect :) thanks again!
Title: Re: USB HID access on Linux
Post by: Punkertjenv on November 24, 2017, 04:16:28 pm
Hi DonAlfredo,
I want to thank you for you contribution. The basics seem to work very well. However I have a device were it would be feasible from to get/set the hid feature reports. I altered the code and added a function to do so, but It does not seem te be working. I'v created a record based on an array of cuint32 that I pass to the fpioctl function, but i don't notice any change in the record. Did you ever get any luck getting/setting features?
Kind Regards,
N!
Title: Re: USB HID access on Linux
Post by: DonAlfredo on November 24, 2017, 04:40:54 pm
To be honest, I only used feature reports on Windows. And have never tested this on Linux.
If time permits, I will dig into this to get it working on Linux too !