Page 1 of 1

Libusb or not?

Posted: Sun Dec 19, 2010 11:05 pm
by eslavko
Hello...

I just have few question...
If I use HID device can I send the data thru control endpoint 0?
Do I must need the libusb or is that possible with standard vin API calls?

What size of data can be send in control endpoint 0?
I need to send data packet as short as 5 bytes but can be long 250 bytes too. Is that possible? Or I need to always send max size packet?

Same is for receive. the packets are of different lenghts can I send back only 1 byte or I should use MAX size of packet?

Thanks.....

Re: Libusb or not?

Posted: Mon Dec 20, 2010 1:07 am
by maxi
eslavko wrote:If I use HID device can I send the data thru control endpoint 0?

Sure, by using HID feature reports. Try out the HID-data example, hopefully it will answer the rest of your questions and does pretty much what you're asking already. 8)

Re: Libusb or not?

Posted: Mon Dec 20, 2010 9:42 am
by eslavko
As far I understand that If I use API call instead libusb then I have only fixed data len (of 8). Is that true?

Re: Libusb or not?

Posted: Tue Dec 21, 2010 12:22 am
by maxi
As far as I am aware, the driver splits all control transfers (function read/write) into 8 byte packets regardless of the payload. Maybe you need to use an interrupt endpoint?

Sorry, i'm really not sure what you are trying to do.

Re: Libusb or not?

Posted: Tue Dec 21, 2010 10:37 am
by eslavko
Just want simplest interface (if it's possible without DLL's on WinXP) and to be able to send data of variable length in both ways.

Re: Libusb or not?

Posted: Tue Mar 01, 2011 4:56 pm
by choleski
I too am trying to do the hid_data example without libusb .
can you please tell more about how to use control endpoint 0 to do it ?

Re: Libusb or not?

Posted: Wed Mar 02, 2011 11:15 am
by eslavko
It possible to send data with windows API HidD_SetFeature and to receive it with HidD_GetFeature.
The only problem is that the the length is predefined in descriptor's. So If you have descriptor for 8 bytes then you must send 8 bytes. So I had two (or more) descriptors with different length's. For example I had one capture device. I had two descriptors. One is for 8 bytes data (intended to control parameters) and other is 128 bytes long for receiving data. And work's well.

Slavko

Re: Libusb or not?

Posted: Wed Mar 02, 2011 11:19 am
by eslavko
There is my function to open HID device written in FreeBasic language.

Code: Select all

function OpenHID(vid as integer,pid as integer,byval manufacturerp as byte ptr,byval productp as byte ptr,debug as integer=0) as HANDLE export
  dim as string manufacturer, product
  manufacturer = *manufacturerp
  product = *productp

  Type HIDD_ATTRIBUTES
    size As Dword
    VendorID As Word
    ProductID As Word
    VersionNumber As Word
  End Type
  Type DEVICE_INTERFACE_DETAIL_DATA
    cbSize As Integer
    txt As ZString*250
  End Type
  Dim As guid hidGuid
  dim as HDEVINFO deviceInfoList
  dim as SP_DEVICE_INTERFACE_DATA deviceInfo
  dim as HANDLE handle = cast(any ptr,INVALID_HANDLE_VALUE)
  dim as DWORD size
  dim as integer OpenFlag=0
  dim as HIDD_ATTRIBUTES deviceAttributes
  Dim As WString*128 WS
  Dim As DEVICE_INTERFACE_DETAIL_DATA DetailData
  HidD_GetHidGuid (@hidGuid)
  deviceInfoList = SetupDiGetClassDevs(@hidGuid,0,0,DIGCF_PRESENT or DIGCF_INTERFACEDEVICE)
  If deviceInfoList = INVALID_HANDLE_VALUE Then return cast(any ptr,INVALID_HANDLE_VALUE)
  for i as integer=0 to 100000
    if handle <> INVALID_HANDLE_VALUE then CloseHandle(handle):handle = cast(any ptr,INVALID_HANDLE_VALUE)
    deviceInfo.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA)
    if SetupDiEnumDeviceInterfaces(deviceInfoList, 0, @hidGuid, i, @deviceInfo)=0 then return cast(any ptr,INVALID_HANDLE_VALUE)
    SetupDiGetDeviceInterfaceDetail(deviceInfoList,@deviceInfo,0,0,@size,0) 'samo da dobim size!
    DetailData.cbSize=5
    SetupDiGetDeviceInterfaceDetail(deviceInfoList,@deviceInfo,cast(any ptr,@DetailData),size,@size,0)
    handle = CreateFile(detailData.txt, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, openFlag, NULL)
    if handle = INVALID_HANDLE_VALUE then return cast(any ptr,INVALID_HANDLE_VALUE)
    deviceAttributes.Size = sizeof(deviceAttributes)
    HidD_GetAttributes(handle, @deviceAttributes)
    if debug then
      print"VID:"; hex(deviceAttributes.VendorID,4);
      print" PID:"; hex(deviceAttributes.ProductID,4);
      print" Ver:"; hex(deviceAttributes.VersionNumber,4);
      If HidD_GetManufacturerString(handle,StrPtr(WS), 128)<>0 Then Print " Mfg:" ; WS;
      If HidD_GetProductString(handle,StrPtr(WS), 128)<>0 Then  Print " Product:" ; WS;
      print
    end if
    if  vid=deviceAttributes.VendorID then
      if  pid=deviceAttributes.ProductID then
        If HidD_GetManufacturerString(handle,StrPtr(WS), 128)<>0 Then
          if manufacturer=Ws then
            If HidD_GetProductString(handle,StrPtr(WS), 128)<>0 Then
              if Product=Ws then
                exit for 'just my device
              end if
            end if
          end if
        end if
      end if
    end if
  next
  SetupDiDestroyDeviceInfoList(deviceInfoList)
  OpenHID=handle
end function

Re: Libusb or not?

Posted: Tue Mar 08, 2011 7:52 pm
by Bob
Just use different feature number for your different lengths and check the feature number in usbFunctionSetup

Code: Select all

if (rq->bRequest == USBRQ_HID_SET_REPORT) //(0x09)
{
    if (rq->wValue.bytes[0] == 2)
    {
        DataRemaining = DATA_FTSIZE;
        return USB_NO_MSG;
    }
    else if (rq->wValue.bytes[0] == 1)  // only if the report ID is 1
    {
        DataRemaining = 0;
        return USB_NO_MSG;          // tell driver to call usbFunctionWrite with data
    }
}

Re: Libusb or not?

Posted: Tue Mar 08, 2011 9:18 pm
by eslavko
...just already do that...