2

I am currently porting code that uses a USB device from Windows to Linux.

I've thoroughly tested the original application and I'm pretty sure that the device works well. I implemented the USB interface on Linux using hidapi-libusb and there are times when the returned data from the device is missing at least a byte.

Once it happens, all the returned values are missing that much data. I more or less have to disconnect and reconnect the USB device in order to make the USB device read data correct. I'm starting to think that maybe the first byte is sometimes returned as 00 and Linux ignores it. It usual occurs on successive reads.

For example: I send get register state and I expect 10 data available for USB read. Byte 5 is the number of the data.

Expected:

00 00 01 02 00 08 42 (Data 8)

00 00 01 02 00 09 42 (Data 9)

Actual:

00 00 01 02 00 08 42 (Data 8)

00 00 02 00 09 42 ab (Data 9)

Data 9's packet number becomes wrong because it is missing a byte. I've tried changing to hidapi-hidraw, and it happens significantly less. I've checked the hexdump of the hidraw of the device (/dev/hidraw0), and it is consistent with the data I am getting in my application. I've tried using memory leak detection tools and no leaks/corruption is detected.

Is this a Linux problem (3.2.0-4-amd64) or is it possibly the device?

The pseudo code of my application is just:

  1. Initialize HIDAPI and device related

  2. Connect to device using HIDAPI

  3. Write USB command

  4. Read USB command (done multiple times if write expects multiple data)

  5. Parse data

  6. Repeat 3 and 4 until all commands are performed

  7. Free memory and close HIDAPI.

Things I've tried:

  1. Ensure no delay is between read and writes

  2. Add flushing of read data before writing (sometimes catches stray data)

  3. Add a really long timeout (five seconds) on flushing of read data - significantly reduces the problem at a big cost.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Detox
  • 71
  • 1
  • 5
  • I saw a very similar problem that was causing audio signal corruption transferred via USB audio device. Some suggestions to try: 1. find out the USB chip type on your device - Google for known problems. 2. Add a small delay (like 20ms) between read and writes to the device. 3. Try a different host (PC) - it can be caused by chip or by driver (ehci. xhci, ....) – L.R. Sep 10 '16 at 10:19
  • I am facing the same problem. Did u found fix for this? – md.jamal Nov 20 '20 at 00:24
  • Oh I found the fix and it was caused by the OS. I was using a virtual OS and it was causing lost packets. – Detox Nov 26 '20 at 05:38

0 Answers0