Recent

Author Topic: [Solved] Working with USB  (Read 5556 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Working with USB
« Reply #15 on: January 09, 2023, 02:25:53 pm »
I've written minimal application and at least it shows "Devices found: 1" on Linux, but unfortunately my device is complicated one and requires some warm up process before reading any data. And this part needs some debugging and experimenting. I have problems with debugging now, because I have to compile project on virtual machine and run it on real computer, because this computer doesn't have access to Internet. So, all I have now - is access violation at line, where data is read. But I'll solve this problem in nearest future.

I've done rather more of this stuff than you have and am *telling* *you*: you'll do yourself a big favour if you run lsusb etc., and investigate what the internal chip is.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Working with USB
« Reply #16 on: January 09, 2023, 08:02:42 pm »
I've done rather more of this stuff than you have and am *telling* *you*: you'll do yourself a big favour if you run lsusb etc., and investigate what the internal chip is.

MarkMLl
I've already done it. And I already know, that it's worst case scenario. Because every device model has it's own control sequence, but device with my VID+PID combination requires runtime model detection. So first thing, I need to do - at least detect, which one of 3 model variants I have. Before that I can't even hardcode control sequence just to test things.

Bad thing - Java is more complicated language. Code I have is complicated too. It has threads, static class members, calling constructors without assigning result to anything. For example connection object is created from endpoint one and at the same time connection object periodically calls endpoint constructor, i.e. recreates itself.
« Last Edit: January 09, 2023, 08:08:36 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Working with USB
« Reply #17 on: January 10, 2023, 02:47:21 pm »
Ehhh. My program works. It just needs root privileges. But next problem -  LIBUSB_ERROR_IO when performing libusb_control_transfer.

And another problem. I'm new to Linux console applications. My application doesn't create new console window, so I don't see it's output. How to solve this problem?
« Last Edit: January 10, 2023, 02:49:55 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Working with USB
« Reply #18 on: January 10, 2023, 02:50:45 pm »
I've already told you what to look for.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12866
  • FPC developer.
Re: Working with USB
« Reply #19 on: January 10, 2023, 03:08:11 pm »
For Windows you can enumerate devices over the setup api, some Jedi project has a header for that, from which I got some parts.

I mainly use it to identify USB serial COM ports, and to detect the insertion and removal of devices (inserting an USB stick for the first time with lots of media files in the root can clog storage to all drives for seconds. Removal is usually painless but also logged).

(this is detection only)
« Last Edit: January 11, 2023, 10:28:46 am by marcov »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Working with USB
« Reply #20 on: January 10, 2023, 04:30:50 pm »
My biggest problem for now - is difference between Java and libusb code logic. Java code obtains control endpoint from interface. Yeah, lsusb shows me, that device has one interface with one endpoint, that is interrupt endpoint and uses exactly 8 byte packets, that is right. But at the same time Java uses exactly control endpoint, not interrupt one. And libusb uses completely different logic. Interrupt endpoint doesn't support methods, I need - control endpoint does. And control endpoint isn't even bound to any interface.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

cai

  • New Member
  • *
  • Posts: 44
Re: Working with USB
« Reply #21 on: January 11, 2023, 04:14:09 am »
My biggest problem for now - is difference between Java and libusb code logic. Java code obtains control endpoint from interface. Yeah, lsusb shows me, that device has one interface with one endpoint, that is interrupt endpoint and uses exactly 8 byte packets, that is right. But at the same time Java uses exactly control endpoint, not interrupt one. And libusb uses completely different logic. Interrupt endpoint doesn't support methods, I need - control endpoint does. And control endpoint isn't even bound to any interface.

endpoint is not required in control transfer, only data-direction in request_type, request code, value ....

Code: Pascal  [Select][+][-]
  1. libusb_control_transfer: function (dev_handle: Plibusb_device_handle; request_type: uint8_t; request: uint8_t; value: uint16_t; index: uint16_t; data: PByte; length:uint16_t;timeout: uint32_t):int32_t;
  2.  

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Working with USB
« Reply #22 on: January 11, 2023, 04:33:24 am »
endpoint is not required in control transfer, only data-direction in request_type, request code, value ....

Code: Pascal  [Select][+][-]
  1. libusb_control_transfer: function (dev_handle: Plibusb_device_handle; request_type: uint8_t; request: uint8_t; value: uint16_t; index: uint16_t; data: PByte; length:uint16_t;timeout: uint32_t):int32_t;
  2.  
I'm new to USB programming - that's why I ask questions. I guess, my device is something like HID one. This code has something called TUSBPseudoHIDInterface, that works according to desired logic. Problem is - Java code has different logic. It obtains interface, then pipe from interface and then so called IRP from pipe. So, I'm not sure, if I port this code correctly. Because it seems like Java code actually reads from interrupt interface. Because it claims it prior to that. But at the same time it looks like control messages are used to do it. Because they have the same parameters and they're bidirectional, while interrupt endpoint is read-only.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

cai

  • New Member
  • *
  • Posts: 44
Re: Working with USB
« Reply #23 on: January 11, 2023, 05:10:39 am »
endpoint is required in interrupt transfer.
usb communication protocol is a serial-stuffs,
so maybe in your device:
control-trans is declare an intention,  interrupt-trans send or recv data, it depends on the device firmware logic.
========================
if you not sure, I suggest you can use wireshark to monitor the USB communication data with [java code] first
wireshark can be found on www.wireshark.org
it would be helpful, maybe usbmon is also fine.
=========================
on windows:
usb bushound is a necessary tool in usb-dev work!
« Last Edit: January 11, 2023, 07:00:52 am by cai »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Working with USB
« Reply #24 on: January 11, 2023, 10:30:14 am »
I guess, there is ton of code hidden behind these Java's IRPs. I guess, request is sent via control message, while response is read from interrupt endpoint behind the scene. May be waiting for response is also needed there.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

avra

  • Hero Member
  • *****
  • Posts: 2586
    • Additional info
Re: Working with USB
« Reply #25 on: January 11, 2023, 01:42:35 pm »
I guess, my device is something like HID one.
If you have a HID device then you can completely avoid java code translation and do it much simpler with HID library mentioned here: https://forum.lazarus.freepascal.org/index.php?topic=49233.0.
There is also a small Lazarus project which lists attached HID devices, or you can open Device Manager and look if anything new is shown under Human Interface Devices tree when you attach your device to confirm it is a HID one.
« Last Edit: January 11, 2023, 01:50:09 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Working with USB
« Reply #26 on: January 11, 2023, 03:41:46 pm »
If you have a HID device then you can completely avoid java code translation and do it much simpler with HID library mentioned here: https://forum.lazarus.freepascal.org/index.php?topic=49233.0.
There is also a small Lazarus project which lists attached HID devices, or you can open Device Manager and look if anything new is shown under Human Interface Devices tree when you attach your device to confirm it is a HID one.
Ok, but first I want to try TLibUsbControlTransfer from pl_usb. The most important achievement for today - I've managed to forward my USB device to my computer via USBIP. And it works! Bad thing - Windows client is buggy. Even with all fixes it still causes BSOD when I try to detach device. But at least now I can work with my project on my computer. I just need to use Linux VM. But immediate problem - device is no longer listed by libusb, so, I guess, at the end libusb doesn't list devices recursively.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Working with USB
« Reply #27 on: January 12, 2023, 02:06:15 pm »
Ok. At the end I didn't need TLibUsbControlTransfer, because it's just async variant of ControlMsg. What I needed - to simply claim interface. Everything worked after that.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

 

TinyPortal © 2005-2018