USB receiving data from the device

I am trying to read data from a HID device. I have a USB sniffer capture that basically does:

Get Device Descriptor Get Device Descriptor Set Address Get Configuration Descriptor Get Configuration Descriptor Set Configuration Set Idle Get Input Report Get Input Report Get Input Report Get Input Report Set Feature Report Get Input Report Set Feature Report Get Input Report Get Input Report Set Output Report Get Input Report Set Feature Report Input Report Input Report 

It seems that everything before the Input Report configured and that the Input Report is a regular collection of data from the device.

In libusb , I do the following:

 usb_init(); usb_find_busses(); usb_find_devices(); loop through busses loop through devices if correct vendor and correct product handle = usb_open(device) break usb_set_configuration(dev_handle, 1) // Endpoint 0 is a 'write endpoint', endpoint 1 is a 'read endpoint'. endpoint = &device->config[0].interface[0].altsetting[0].endpoint[1] usb_claim_interface(dev_handle, 0) usb_set_altinterface(dev_handle, 0) usb_bulk_read(dev_handle, endpoint->bEndpointAddress, buffer, endpoint->wMaxPacketSize, 1); 

I assume that the driver and code before usb_set_configuration consistent with sniffer analysis before Set Configuration .

Everything in the code succeeds until there is usb_bulk_read .

  • How to Set Idle , Get Input Report , Set Feature Report , Set Output Report ?
  • Why usb_bulk_read crash?
  • What else do I need to do to set up a connection with my device?
+4
source share
2 answers

I'm new to libusb and USB in general, so I'm not sure if this is correct, but by looking at the output of a USB analyzer such as USBlyzer and setting up a few things, I come up with the following protocol elements:

usb_claim_interface

When I used the usb_claim_interface interface ( usb_claim_interface ) and then canceled my application, I was inoperable on subsequent launches. I tried various usb_reset ( usb_reset and usb_resetep ), but I still could not get the correct use from usb_control_msg .

SetReport / GetReport

USBlyzer showed that the corresponding packages include Get Descriptor , Select Configuration , Set Report and Get Report . Get Descriptor and Select Configuration explicitly associated with usb_get_descriptor and usb_set_configuration respectively.

Some Get Report packages contained Feature Id and others Input Id . I was able to map them to usb_control_msg with the following parameters ( libusb.c helped me figure this out):

 requesttype = USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE value = 0x01 (for GetReport) index = id | (0x03 << 8) (for FeatureId) 

Set Report packages also used Feature Id but Output Id . From consideration of the details, it became clear that Input Id same (0x01 << 8) and Output Id same (0x02 << 8). Therefore, to get the Set Report I called usb_control_msg with these adjusted parameters:

 requesttype = USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE value = 0x09 (for SetReport) index = id | (0x03 << 8) (for FeatureId) 

This may not be the β€œright” way to do all this, and I would certainly appreciate a deeper understanding of what happens with the various API functions. But this was able to make my host capture all the relevant data from the device.

+2
source

HID device [...] usb_bulk_read

Uch. USB Bulk read is used only for mass endpoints, but HID is not.

HID endpoints are interruption endpoints and therefore need usb_interrupt_transfer() . You looked at the endpoint descriptor, right? It must declare the endpoint type as an interrupt.

+3
source

Source: https://habr.com/ru/post/1399067/


All Articles