Author Topic: Some fundamental questions..  (Read 9423 times)

DavidClarke

  • Member
  • ***
  • Posts: 24
Some fundamental questions..
« on: May 03, 2019, 05:28:23 pm »
Hi

I am very new to USB.

At the moment I am only developing for Windows while I am learning.

I have already called:
SetupDiEnumDeviceInterfaces
SetupDiGetDeviceInterfaceDetail
SetupDiGetDeviceInterfaceDetail

For the sake of this question assume I have a File_Handle from  calling:


hReadFile = CreateFile(FILE_NAME, %GENERIC_READ OR %GENERIC_WRITE, %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL %NULL, %OPEN_EXISTING, %FILE_FLAG_OVERLAPPED, %NULL)


Question is, Can I get 100% of the information from the device by using ReadFile / WriteFile?  Assuming I am able to parse the data.
Or is there a reason why one should use -
HidD_GetManufacturerString,
HidD_GetProductString,
HidD_GetSerialNumberString,
HidP_GetCaps etc?
Those are just "helper" functions, right?  I can see they make life easy!
I am still not exactly why I need to know the descriptor stuff as my code is for one specific device.  It sends a bit array to tell me which buttons are pressed.  Then I am supposed to be able to send BMP data to the device to change the button colors, text, logo etc...

I don't mind using those functions just wanted to clarify that I could get raw data and derive the same information as the "helper" functions.
All my years in serial and tcp ip tend to make me think in terms of transmit and receive, at least while I am building understand of what is going on.

All the examples I have seen show opening two different file handles - one not overlapped and one overlapped.  Why do I need two?  Couldn't the overlapped do the job.

I am working on integrating an HID USB device into one of our existing products without using an external library or dll.  I have the device "events", button presses, working fine with WaitForSingleObject(MY_OVERLAPPED.hEvent, 3000).         

I am stuck on sending to the device to control its display.

When I try to send a command from my code to the device using WriteFile the device ignores it.
However I am not sure what the SetupPacket is doing...

Any pointers would help, Thanks!  Yes, I have your book, but I am missing some of the simple fundamental concepts.
Do you have any examples using Win32Api ReadFile and WriteFile?  What is the difference between Read/WriteFile and HidD_GetInputReport / HidDSetOutputReport?
Page 305 talks about FileStream and HidD_ but no ReadFile/WriteFile overlapped.
I suspect this is a case of "there are many ways to do it"  between read write, functions etc.  I am looking for the smallest most reliable and easy to understand way of sending commands/ data to the device - an output report, right?

Thank you!  Can we hire you to help us as we get deeper into all this?


The following is a trace from a command to adjust the display brightness:


--------- Request # 8 ----[IN]----

URB_FUNCTION_CONTROL_TRANSFER

Length: 0x88
USBD Status: USBD_STATUS_SUCCESS (0x0)
EndpointAddress: 0x0
PipeHandle: 0xFFFFC907DC967E20
TransferFlags: 0xB ( USBD_TRANSFER_DIRECTION_IN USBD_SHORT_TRANSFER_OK )
TransferBufferLength: 0x1A
TransferBuffer: 0xFFFFC907E2AD1A70
TransferBufferMDL: 0x0
UrbLink: 0x0
SetupPacket: 0x80 0x6 0x3 0x3 0x9 0x4 0x6 0x1
RequestType: 0x80 (Direction: Device-to-host, Type: Standard, Recipient: Device)
Request: 0x6 (GET_DESCRIPTOR)
Value: 0x303 (USB_STRING_DESCRIPTOR_TYPE)
Index: 0x409
Length: 0x106

String Descriptor
bLength: 0x1A
bString: BL19H1A06957

** Data **
1A 03 42 00 4C 00 31 00 39 00 48 00 31 00 41 00 30 00 36 00 39 00 35 00 37 00

--------- Request # 9 ----[OUT]----

URB_FUNCTION_CLASS_INTERFACE

Length: 0x88
USBD Status: USBD_STATUS_SUCCESS (0x0)
TransferFlags: 0x0 ( USBD_TRANSFER_DIRECTION_OUT )
TransferBufferLength: 0x11
TransferBuffer: 0xFFFFC907E13855C0
TransferBufferMDL: 0x0
UrbLink: 0x0
RequestTypeReservedBits: 0x22
Request: 0x9
Value: 0x305
Index: 0x0

** Data **
05 55 AA D1 01 10 00 00 00 00 00 00 00 00 00 00 00

--------- Request # 10 ----[OUT]----

URB_FUNCTION_CLASS_INTERFACE

Length: 0x88
USBD Status: USBD_STATUS_SUCCESS (0x0)
TransferFlags: 0x0 ( USBD_TRANSFER_DIRECTION_OUT )
TransferBufferLength: 0x11
TransferBuffer: 0xFFFFC907E13855C0
TransferBufferMDL: 0x0
UrbLink: 0x0
RequestTypeReservedBits: 0x22
Request: 0x9
Value: 0x305
Index: 0x0

** Data **
05 55 AA D1 01 10 00 00 00 00 00 00 00 00 00 00 00

--------- Request # 11 ----[IN]----

URB_FUNCTION_CONTROL_TRANSFER

Length: 0x88
USBD Status: USBD_STATUS_PENDING (0x40000000)
EndpointAddress: 0x0
PipeHandle: 0xFFFFC907DC967E20
TransferFlags: 0xA ( USBD_TRANSFER_DIRECTION_OUT USBD_SHORT_TRANSFER_OK )
TransferBufferLength: 0x0
TransferBuffer: 0xFFFFC907E13855C0
TransferBufferMDL: 0xFFFFC907E1521A40
UrbLink: 0x0
SetupPacket: 0x21 0x9 0x5 0x3 0x0 0x0 0x11 0x0
RequestType: 0x21 (Direction: Host-to-device, Type: Class, Recipient: Interface)
Request: 0x9
Value: 0x305
Index: 0x0
Length: 0x11

SET_REPORT

Report Type: Feature
Report ID: 0x5
Interface: 0x0




 

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Some fundamental questions..
« Reply #1 on: May 03, 2019, 08:48:18 pm »
If you have a handle, ReadFile and WriteFile will exchange Input and Output reports.

If the host application knows what to do with received data, it doesn't have to ask the device for specifics but can work with the raw data.

Two handles - can't answer without seeing what they're used for.

I have ReadFile / WriteFile HID code here:

http://www.janaxelson.com/hidpage.htm#MyExampleCode

WriteFile uses interrupt transfers if the HID has an interrupt OUT endpoint. Otherwise, it uses control transfers with HidD_SetOutputReport.

Feature reports use control transfers and HidD_SetFeature.

Assuming it's an Output report, check if WriteFile returns successfully.

Use a protocol analyzer or whatever debugging tools you have to find out if the host application is sending data on the bus and if so, if the device is detecting the received data.

I don't do consulting but will respond to questions posted here.

DavidClarke

  • Member
  • ***
  • Posts: 24
Re: Some fundamental questions..
« Reply #2 on: May 04, 2019, 07:42:20 am »
Thanks!  Protocol analyzer is a fine idea.