Quantcast

Problem with libusb_bulk_transfer while reading

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problem with libusb_bulk_transfer while reading

vijaykri
Hi All,

We have developed a USB device and we are trying to communicate with the host using libusb.
 
With the endpoint 0x01, I am able to write to device but I could not read back from endpoint 0x81.
 
I tried writing and reading --> writing performed and read not happended

also

I tried only reading, reading failed with return -99.

The debug message shows as:
libusb:Warning [do_sync_bulk_transfer] unrecognised status code 1; with return value -99.
 
Can anyone let me know if you have encountered a same problem.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

Xiaofan Chen
On Sat, Oct 1, 2011 at 4:15 AM, vijaykri <[hidden email]> wrote:

> Hi All,
>
> We have developed a USB device and we are trying to communicate with the
> host using libusb.
>
> With the endpoint 0x01, I am able to write to device but I could not read
> back from endpoint 0x81.
>
> I tried writing and reading --> writing performed and read not happended
>
> also
>
> I tried only reading, reading failed with return -99.
>
> The debug message shows as:
> libusb:Warning [do_sync_bulk_transfer] unrecognised status code 1; with
> return value -99.
>
> Can anyone let me know if you have encountered a same problem

More information is needed.

1. Since you developed the device, so you do have access to the firmware
and know the communication protocol, right? Are you sure your codes
correspond to the communication protocol?
2. What is the OS and what is the libusb version?
3. Code snippets


--
Xiaofan

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

vijaykri
Hi  Xiaofan,

I have a device on FPGA and we have developed based on the USB protocol.
We do have many simulation results but now we wanted to try integrating with the host.

This is the first time I am using libusb libraries, I might be wrong in the flow. can you let me know the series of steps to be carried out for read;

Below is the code snippet for the read i am doing, I am running on centOS does it have problems with the linux distro we are using

         #include <iostream>
        #include <stdio.h>
        #include </libusb-1.0.8/libusb/libusb.h>
         
        using namespace std;
         
        int main() {
            libusb_device **devs;
            libusb_device_handle *dev_handle;
            libusb_context *ctx = NULL;
            libusb_config_descriptor *config;
           
               
            uint8_t config_index;

            int r; //for return values
            ssize_t cnt; //holding number of devices in list
            r = libusb_init(&ctx);
            if(r < 0) {
                cout<<"Init Error "<<r<<endl; //there was an error
                return 1;
            }
            libusb_set_debug(ctx, 3);
         
            cnt = libusb_get_device_list(ctx, &devs);

            cout<<"No. of devices = "<<cnt<<endl;

            if(cnt < 0) {
                cout<<"Get Device Error"<<endl; //there was an error
                return 1;
            }
            cout<<cnt<<" Devices in list."<<endl;
         
            dev_handle = libusb_open_device_with_vid_pid(ctx,  65278 , 273 ); //these are vendorID and productID I found for my usb device
            if(dev_handle == NULL)
                cout<<"Cannot open device"<<endl;
            else
                cout<<"Device Opened"<<endl;
            libusb_free_device_list(devs, 1); //free the list, unref the devices in it

       
         
            int actual; //used to find out how many bytes were written


            if(libusb_kernel_driver_active(dev_handle, 1) == 1) { //find out if kernel driver is attached
                cout<<"Kernel Driver Active"<<endl;
                if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
                    cout<<"Kernel Driver Detached!"<<endl;
            }
            r = libusb_claim_interface(dev_handle, 1); //claim interface 1 (the first) of device (mine had jsut 1)
            if(r < 0) {
                cout<<"Cannot Claim Interface with return value = "<< r <<endl;
                return 1;
            }


        <b>//Reading the data from USB
         
     r = libusb_bulk_transfer(dev_handle, 81 , data, 3, &actual, 0); //port 81 is the input port
            if(r == 0 && actual == 1)
                {cout<<"read Successful!"<<endl;
                 cout<<"Data-REceived ->"<<data<<"<-"<<endl;
                 }
            else
                cout<<"read Error with r = "<<r<<endl;</b>
               
 
            r = libusb_release_interface(dev_handle, 1); //release the claimed interface
            if(r!=0) {
                cout<<"Cannot Release Interface"<<endl;
                return 1;
            }
            cout<<"Released Interface"<<endl;
         

            libusb_close(dev_handle); //close the device we opened
            libusb_exit(ctx); //needs to be called to end the
         
            delete[] data; //delete the allocated memory for data
            return 0;
        }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

Peter Stuge
I recommend to use a real email software for participating on the
mailing list. Messages posted through Nabble are severely delayed
and seem to be very badly changed by the web-based posting process.

vijaykri wrote:
>      r = libusb_bulk_transfer(dev_handle, 81 , data, 3, &actual, 0); //port 81 is the input port

81 is only the correct endpoint address in hexadecimal notation, so
change the code to either 0x81 or 129.


>    if(r == 0 && actual == 1)

Also, you are trying to transfer 3 bytes, but you are checking if 1
byte was transfered to determine a successful transfer. This can
still work if your firmware will transfer only 1 byte followed by a
packet of 0 bytes, when asked for 3 bytes, but this is somewhat
unusual communications protocol design. I would expect that you try
to transfer only 1 bytes, when 1 bytes being transferred is the
indication of successful transfer.


Also, make sure that the interface number that you are using (1) is
indeed the corresponding bInterfaceNumber in your interface
descriptor.


//Peter

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

vijaykri
Peter,
I did a mistake while sending the file. I am actually using:

 r = libusb_bulk_transfer(dev_handle, 0x81 , data, 512 , &actual, 0); //port 81 is the input port
if(r == 0 && actual == 512)
{
cout << "read Successful !"<<endl;
}
else
cout << "read Error with r = "<<r<<endl;

Since the device is configured as high speed, I am giving 512 bytes as one packet.

And total number of interfaces we are using in 1 and interface number is 0:

please update the program as:

        #include <iostream>
        #include <stdio.h>
        #include </libusb-1.0.8/libusb/libusb.h>
         
        using namespace std;
         
        int main() {
            libusb_device **devs;
            libusb_device_handle *dev_handle; //a device handle
            libusb_context *ctx = NULL; //a libusb session
            libusb_config_descriptor *config;
           
               
            uint8_t config_index;

            int r; //for return values
            ssize_t cnt; //holding number of devices in list
            r = libusb_init(&ctx); //initialize the library for the session we just declared
            if(r < 0) {
                cout<<"Init Error "<<r<<endl; //there was an error
                return 1;
            }
            libusb_set_debug(ctx, 3);
         
            cnt = libusb_get_device_list(ctx, &devs); //get the list of devices

            cout<<"No. of devices = "<<cnt<<endl;

            if(cnt < 0) {
                cout<<"Get Device Error"<<endl; //there was an error
                return 1;
            }
            cout<<cnt<<" Devices in list."<<endl;
         
            dev_handle = libusb_open_device_with_vid_pid(ctx,  65278 , 273 ); //these are vendorID and productID I found for my usb device
            if(dev_handle == NULL)
                cout<<"Cannot open device"<<endl;
            else
                cout<<"Device Opened"<<endl;
            libusb_free_device_list(devs, 1); //free the list, unref the devices in it

               
         
            unsigned char *data = new unsigned char[512]; //data to read
           
         
            int actual; //used to find out how many bytes were written



            if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached
                cout<<"Kernel Driver Active"<<endl;
                if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
                    cout<<"Kernel Driver Detached!"<<endl;
            }
            r = libusb_claim_interface(dev_handle, 0); //claim interface 1 (the first) of device (mine had jsut 1)
            if(r < 0) {
                cout<<"Cannot Claim Interface with return value = "<< r <<endl;
                return 1;
            }

            cout<<"Claimed Interface"<<endl;
       

        //Reading the data from USB
            cout<<"Now reading the data from the USB, please enter a value \n"<<endl;
            //cin>>data[0];
     r = libusb_bulk_transfer(dev_handle, 0x81 , data, 512, &actual, 0); //port 81 is the input port
            if(r == 0 && actual == 512 )
                {cout<<"read Successful!"<<endl;
                 cout<<"Data-REceived ->"<<data <<"<-"<<endl;
                 }
            else
                cout<<"read Error with r = "<<r<<endl;
               

             
            r = libusb_release_interface(dev_handle, 0); //release the claimed interface
            if(r!=0) {
                cout<<"Cannot Release Interface"<<endl;
                return 1;
            }
            cout<<"Released Interface"<<endl;
         

            libusb_close(dev_handle); //close the device we opened
            libusb_exit(ctx); //needs to be called to end the
         
            delete[] data; //delete the allocated memory for data
            return 0;
        }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

Peter Stuge
vijaykri wrote:
> Peter,
> I did a mistake while sending the file. I am actually using:
>
>  r = libusb_bulk_transfer(dev_handle, 0x81 , data, 512 , &actual, 0); //port 81 is the input port
> if(r == 0 && actual == 512)
..

> Since the device is configured as high speed, I am giving 512 bytes
> as one packet.

This must also be expressed in the descriptors, otherwise the host
will not accept 512 bytes from the device.


> And total number of interfaces we are using in 1 and interface number
> is 0:

Then your host program looks good. You do not need to call
libusb_get_device_list() and libusb_free_device_list() when using
libusb_open_device_with_vid_pid(), and usually vid and pid are
written in hexadecimal form, but these things do not matter for the
end result.

I believe the problem lies in your firmware.


//Peter

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

vijaykri
Can I know whats the meaning of return value -99 when performing

r= libusb_bulk_transfer(dev_handle, 0x81, data, 64, &actual, 0)

r = -99 when executed and a warning message as [do_sync_bulk_transfer] unrecognised status code 1.
 

Thanks,
Vijay K
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

Xiaofan Chen
On Wed, Oct 5, 2011 at 8:14 AM, vijaykri <[hidden email]> wrote:
> Can I know whats the meaning of return value -99 when performing
>
> r= libusb_bulk_transfer(dev_handle, 0x81, data, 64, &actual, 0)
>
> r = -99 when executed and a warning message as [do_sync_bulk_transfer]
> unrecognised status code 1.
>

If you read libusb.h and you will notice this one.

        /** Other error */
        LIBUSB_ERROR_OTHER = -99

Are you sure your device works this way? Do you need to send something
first in order for it to return back the data?

What is the output of "lsusb -vvv" about your device?


--
Xiaofan

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

vijaykri
Below are the details related to my device after doing lsusb -vvv

Bus 001 Device 017: ID fefe:0111  
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0xfefe
  idProduct          0x0111
  bcdDevice            2.22
  iManufacturer           0
  iProduct                0
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           46
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0xc0
      Self Powered
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           4
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass    255 Vendor Specific Subclass
      bInterfaceProtocol    255 Vendor Specific Protocol
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               1
can't get debug descriptor: Connection timed out
cannot read device status, Connection timed out (110)

Can it infer any meaning message to perform read..

Thanks and Regards,
Vijay K
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with libusb_bulk_transfer while reading

Xiaofan Chen
In reply to this post by Xiaofan Chen
On Wed, Oct 5, 2011 at 11:23 AM, Xiaofan Chen <[hidden email]> wrote:
> On Wed, Oct 5, 2011 at 8:14 AM, vijaykri <[hidden email]> wrote:
>> r= libusb_bulk_transfer(dev_handle, 0x81, data, 64, &actual, 0)
>>
> Are you sure your device works this way? Do you need to send something
> first in order for it to return back the data?

I think this is still the key question you need to ask yourself. Only
you know how the device works.

> What is the output of "lsusb -vvv" about your device?
>

Now you have the "lsusb" result, maybe you can try to change 64 as 512
as the wMaxPacketSize is 512.


--
Xiaofan

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Loading...