Recent

Author Topic: FPC-HID-USB ReadFile ok on Windows, not working on Linux  (Read 5661 times)

BobT

  • New Member
  • *
  • Posts: 19
FPC-HID-USB ReadFile ok on Windows, not working on Linux
« on: November 30, 2021, 10:53:10 pm »
I am working on a Lazarus USB HID application ,which will eventually be a port of an existing Delphi 7 PIC bootloader application to be built in Linux (MX Linux 19.2 currently) for crossplatform use.

As a first step I am looking at the FPC-USB-HID demo application. I can build this ok and by adjusting the Vendor and product ID's my device checks out in both Linux and W7 builds. Manufacturer and Product description strings are read OK, but I'm running into problems when I try to use the HidReadWrite function to send a single byte command to my target device bootload firmware and read the response data. Building from the same source files and configuration on the same hardware, on Windows HidReadWrite works as expected, with the correct data being returned in Ctrl.LocalData. On Linux the read phase hangs for a few seconds, and eventually LocalData is populated with a few 1's rather than the expected values. (Using OnData gives a timeout error). Debugging of the target device shows it is receiving and responding to the Linux system command as normal, and Wireshark shows exactly the same expected data in the payload of the returned packet on both o/s.

Any suggestions gratefully received.


MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #2 on: December 01, 2021, 09:45:22 am »
I think the critical question here is 32- or 64-bit, and for that matter the kernel version and platform.

I can't help with FPC-USB-HID specifically, but I've got a vague recollection of looking at HID handling at some point using direct kernel access and finding that there were data structures etc. where alignment was absolutely critical. Hence the importance of being absolutely clear about your system, in case it's something that FPC-USB-HID hasn't been explicitly tested against.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #3 on: December 01, 2021, 09:57:27 am »
FPC-USB-HID is used (by me) on a daily base on RPi. So, it is tested. But not on ARM64.
However. Its not trivial stuff. So you might be correct.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #4 on: December 01, 2021, 10:09:00 am »
I'm having to scrape my memory here. I was probably tinkering on Debian x86_64, and eventually I concluded that the data structures were sufficiently tricky that the only safe way to do it was to use libraries supplied as one of the extension kernel projects i.e. compiled using the standard kernel tools.

I think that what I was doing was investigating whether an older program that controlled a Logitech gaming mouse (i.e. to customise buttons etc.) could be generalised.

But getting access properly defined in the udev rules is obviously critically important, particularly if a GUI program is involved.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BobT

  • New Member
  • *
  • Posts: 19
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #5 on: December 01, 2021, 12:39:51 pm »
Thank you all. System is 64bit x86_64, Lazarus 2.0.10, fpc 3.2.0, Kernel: 4.19.0-6-amd64 x86_64 (as I should have said before).

I do have appropriate udev rule in place, I think, and I can apparently access the device. 'WriteFile' is returning without error and the target device is responding as expected.

The target response in this test instance is fixed (4 bytes MCU ID code + 2 byte checksum) so I know what I should be getting every time, and can see the correct data appearing on the Wireshark trace. Some (not all) of those byte values are consistently appearing in the readBuffer content after calling ReadFile, though not as the 'value' fields but as the lowest byte of the 'HID' field. So alignment does seem to be a major part of the problem. 



MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #6 on: December 01, 2021, 01:29:23 pm »
I think the problem I was seeing was very close to the ioctl() calls themselves, but it might be reasonable to expect data alignment issues if what's being buffered has already been partially-processed by the kernel. I don't know to what extent that's fixable with explicit alignment directives etc.

There might possibly be something useful in my notes below... angst if nothing else :-)

Code: Pascal  [Select][+][-]
  1. (* This is a USB (and in particular HID) exerciser derived from this author's   *)
  2. (* lg600 program which has- or at least should have- the sole functionality of  *)
  3. (* setting up a Logitech G600 gamer's mouse. While the lg600 program put a      *)
  4. (* lot of effort into parsing information in the /sys tree it made very few     *)
  5. (* demands on the USB APIs and as such had no dependency on the development     *)
  6. (* versions of libusb or libhid, however once one starts looking at more        *)
  7. (* general functionality one gets very much bogged down in alignment and        *)
  8. (* anomalous structure size issues (specifically, at the binary API between     *)
  9. (* libhid and the kernel) with the result that using precompiled system         *)
  10. (* libraries is pretty much unavoidable.                MarkMLl, 2019-01-11     *)
  11.  
  12. (* NOTE: a lot of the structure of this program is derived from LG600, where it *)
  13. (* probably made more sense. Sorry.                                             *)
  14.  
  15. (* ASSUME THIS IS PERMANENTLY BROKEN, IT'S HERE ENTIRELY TO PREVENT ME FROM     *)
  16. (* TRYING TO TACK MORE AND MORE INAPPROPRIATE FUNCTIONALITY INTO THE LG600      *)
  17. (* MOUSE PROGRAM.                                                               *)
  18.  
  19. ...
  20.  
  21. (* Minimal testing of the above has been applied, and in practical terms even   *)
  22. (* during initial development I'm pretty sure that much of what I've done here  *)
  23. (* needs to be ripped out and replaced. One of the problems is that libhid      *)
  24. (* exists in two forms (one using the kernel's HID support, the other using     *)
  25. (* libusb) and both implementations are useful, and I suspect that the only way *)
  26. (* to sort out the naming conflicts will be to rely on dynamic linkage of the   *)
  27. (* appropriate .so files: see what I did for the K8055 test program.            *)
  28.  
  29. (* Discussion in the forum directed me to https://github.com/prof7bit/HIDAPI.pas *)
  30. (* which is an interface to hidapi-libusb, but by then I'd just about done my   *)
  31. (* own transcript. Also https://github.com/LongDirtyAnimAlf/FPC-USB-HID but     *)
  32. (* this is a JEDI interface and as such is not what I was looking for.          *)
  33.  

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #7 on: December 01, 2021, 02:05:35 pm »
What HID report length do you use.
And what HID report length does the device support ?

BobT

  • New Member
  • *
  • Posts: 19
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #8 on: December 01, 2021, 02:18:53 pm »
Hi Don Alfredo, report length and device support both 64 bytes.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #9 on: December 01, 2021, 03:12:57 pm »
I will try to look closer into this. I must confess: only used this library on 32 bit ARM systems.
As a first try:
Code: Pascal  [Select][+][-]
  1.   TReport = packed record
  2.     ReportID: byte;
  3.     Data:    array [0..15] of byte; // <-- this needs to be adapted to your report size; e.g 63 !
  4.   end;
  5.  


MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #11 on: December 01, 2021, 03:23:48 pm »
Is there a "Integer" size issue ? like a 64 bit Int over a 32 bit being used and sized calls etc?

Is there an endian issue ?

The impression I got was difficulty getting byte padding exactly right, leading to its being safer to have the same code generator on both sides of the ioctl(). There's also a subsidiary risk of there being something odd in the HID report parsing, and in the past I've found it frustrating that there's no way of getting at the raw HID data i.e. before "quirks" have been applied by the driver module.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BobT

  • New Member
  • *
  • Posts: 19
Re: FPC-HID-USB ReadFile ok on Windows, not working on Linux
« Reply #12 on: December 01, 2021, 03:36:54 pm »
Hi Don Alfredo, Treport.Data is [0..63].

MarkMLI
Quote
frustrating that there's no way of getting at the raw HID data i.e. before "quirks" have been applied by the driver module.
Agreed!

 

TinyPortal © 2005-2018