ControlTransfer instruction not sending value parameter from setup packetr in LibUsbDotNet

StackOverflow https://stackoverflow.com/questions/8862740

  •  28-10-2019
  •  | 
  •  

Question

I have a USB device that I need to be able to talk to from a .net application. The device is not a standard HID device and in order to initilise it I've been given a trace of packets from a USB Protocol Analyser / Sniffer for the packets used when intialising it on another type of machine. I need to replicate this packet sequence from my .net application to initialise the device.

Everything is working well until I get to a particular control transfer packet / class type request.

The trace I've been given states I should issue:

Control Transfer Class Type Request 
21 0A 00 00 00 00 00 00 
Result stall (intentional)

Control Transfer Class Tyoe Request
A1 01 01 03 00 00 40 00 
Result will initiate a 64 byte transfer of data from the device to the host.

This is the code I'm using to do this:

                // Transcation 6
                UsbSetupPacket setup = new UsbSetupPacket(0x21, 0x0A, 0, 0, 0);
                bool result = MyUsbDevice.ControlTransfer(ref setup, buffer, 0, out transferred);
                Console.WriteLine("Result = {0}", result);

                // Transcation 7
                setup = new UsbSetupPacket(0xA1, 0x01, 0x0301, 0x0000, 0x0040);
                result = MyUsbDevice.ControlTransfer(ref setup, buffer, 64, out transferred);

                Console.WriteLine("Result = {0}, {1}", result, transferred);

And this is the trace I'm receiving from BusHound which is sniffing the USB data traffic for this device:

Device  Phase  Data                      Description       Cmd.Phase.Ofs(rep)
------  -----  ------------------------  ----------------  ------------------
  46.0  CTL    21 0a 00 00  00 00 00 00  SET IDLE                20.1.0        
  46.0  USTS   c0000004                  stall pid               20.2.0        
  46.0  CTL    a1 01 01 03  00 00 00 00  GET REPORT              21.1.0        
  46.1  USTS   c0000004                  stall pid               22.1.0        

As you can see the 0x0040 value parameter in the setup packet is not making it out even though I'm setting it. I'm relatively new to USB and to .net / LibUsbDotNet and I'm not quite sure what I'm doing wrong. I wonder if anyone can suggest anything for me to try?

Note, I'm developing on a Windows 7 64bit machine using Visual Studio 2008.

Thanks, Rich

Was it helpful?

Solution

OK, after much investigation I found the source of the problem and it was really my lack of understanding of how LibUSBDotNet works, which isn't helped by the poor documentation for the otherwise excellent library.

The problem is that the 0x0040 should not be manually specified in the setup packet - this value appears to be irrelevant. Instead simply specify the bytes to transfer in the ControlTransfer method and also ensure that buffer is a pre-allocated suitably large byte array eg:

byte[] buffer = new byte[256];
setup = new UsbSetupPacket(0xA1, 0x01, 0x0301, 0x0000, 0x0000); 
result = MyUsbDevice.ControlTransfer(ref setup, buffer, 0x0040, out transferred); 

This will generate the correct control transfer packet sent to the USB device

Control Transfer Class Tyoe Request   
A1 01 01 03 00 00 40 00   

It appears the LibUsbDotNet does some validation on the various parameters and in the case where the buffer array isn't large enought it simply just sends something else instead (in my case 0x0000) rather than throwing an appropriate exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top