Recent

Author Topic: USB HID access on Linux  (Read 19685 times)

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
USB HID access on Linux
« on: February 22, 2015, 01:14:09 pm »
Hello to all,

Please find on GitHub (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.

wolfdreamer

  • New Member
  • *
  • Posts: 15
Re: USB HID access on Linux
« Reply #1 on: April 26, 2015, 12:09:40 pm »
i'm interest in USB but with win32. is there something like that?

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: USB HID access on Linux
« Reply #2 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 !

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: USB HID access on Linux
« Reply #3 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
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: USB HID access on Linux
« Reply #4 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
 
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: USB HID access on Linux
« Reply #5 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

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: USB HID access on Linux
« Reply #6 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.

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: USB HID access on Linux
« Reply #7 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:

Bus 001 Device 017: ID 1130:6801 Tenx Technology, Inc.
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0x1130 Tenx Technology, Inc.
  idProduct          0x6801
  bcdDevice            3.02
  iManufacturer           0
  iProduct                1 
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           34
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 No Subclass
      bInterfaceProtocol      0 None
      iInterface              0
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.10
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      34
          Report Descriptor: (length is 34)
            Item(Global): Usage Page, data= [ 0x00 0xff ] 65280
                            (null)
            Item(Local ): Usage, data= [ 0x01 ] 1
                            (null)
            Item(Main  ): Collection, data= [ 0x01 ] 1
                            Application
            Item(Local ): Usage, data= [ 0x01 ] 1
                            (null)
            Item(Global): Logical Minimum, data= [ 0x00 ] 0
            Item(Global): Logical Maximum, data= [ 0xff 0x00 ] 255
            Item(Global): Report Size, data= [ 0x08 ] 8
            Item(Global): Report Count, data= [ 0x08 ] 8
            Item(Main  ): Input, data= [ 0x00 ] 0
                            Data Array Absolute No_Wrap Linear
                            Preferred_State No_Null_Position Non_Volatile Bitfield
            Item(Local ): Usage, data= [ 0x02 ] 2
                            (null)
            Item(Global): Logical Minimum, data= [ 0x00 ] 0
            Item(Global): Logical Maximum, data= [ 0xff 0x00 ] 255
            Item(Global): Report Size, data= [ 0x08 ] 8
            Item(Global): Report Count, data= [ 0x08 ] 8
            Item(Main  ): Output, data= [ 0x02 ] 2
                            Data Variable Absolute No_Wrap Linear
                            Preferred_State No_Null_Position Non_Volatile Bitfield
            Item(Main  ): End Collection, data=none
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval              10
Device Status:     0x0000
  (Bus Powered)
It is simply meant to return 8 data bytes, which are its sensor values.

Thanks
Brian

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: USB HID access on Linux
« Reply #8 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 !

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: USB HID access on Linux
« Reply #9 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

mark

  • New Member
  • *
  • Posts: 11
Re: USB HID access on Linux
« Reply #10 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.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: USB HID access on Linux
« Reply #11 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:
Code: Pascal  [Select][+][-]
  1. const
  2.  Vendor                        = $045E; // = VID
  3.  Product                       = $0916; // = PID
  4.  
Please perform the above steps first. And report the results.

mark

  • New Member
  • *
  • Posts: 11
Re: USB HID access on Linux
« Reply #12 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 004: ID 046d:c058 Logitech, Inc. M115 Mouse
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0x046d Logitech, Inc.
  idProduct          0xc058 M115 Mouse
  bcdDevice           54.00
  iManufacturer           1 Logitech
  iProduct                2 USB Optical Mouse
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           34
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0xa0
      (Bus Powered)
      Remote Wakeup
    MaxPower               98mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      1 Boot Interface Subclass
      bInterfaceProtocol      2 Mouse
      iInterface              0
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.11
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      67
         Report Descriptors:
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0006  1x 6 bytes
        bInterval              10
Device Status:     0x0000
  (Bus Powered)

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?


DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: USB HID access on Linux
« Reply #13 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 !

mark

  • New Member
  • *
  • Posts: 11
Re: USB HID access on Linux
« Reply #14 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?

 

TinyPortal © 2005-2018