• AVR Freaks

Jan Axelson's WinUSB example under Linux with libusb

Page: < 1234 > Showing page 3 of 4
Author
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/25 01:15:48 (permalink)
0
xiaofan i have tried with libusb non portable function but they return always -1.I have created a file in /etc/udev/rules.d with microchip setting as you told me but the program doesn't work...

here there is the code:
#include<usb.h>
#include<stdio.h>

struct usb_bus *temp_bus;
struct usb_device *comm_device;
struct usb_dev_handle *comm_handle;

const static int CONFIGURATION=1;
const static int INTERFACE=0;


void init_lib()
{
printf("\nInitializing lib_usb...\n");
usb_init();
printf("Searching usb_busses...\n");
int num_bus=usb_find_busses();
printf("Searching usb_devices...\n");
int num_dev=(usb_find_devices())-num_bus;
printf("BUSSES FOUND : ");
printf("%d",num_bus);
printf("\n");
printf("DEVICES FOUND : ");
printf("%d",num_dev);
printf("\n");
}

struct usb_device *find_device(int idVendor,int idProduct)
{
puts("\nSearching device...");
temp_bus=usb_get_busses();
struct usb_device *device;
while(temp_bus->next!=NULL)
{
struct usb_device *temp_device;
struct usb_device_descriptor temp_desc;

temp_device=temp_bus->devices;

while(temp_device->next!=NULL)
{
temp_desc=temp_device->descriptor;
if(temp_desc.idVendor==idVendor && temp_desc.idProduct==idProduct)
{
comm_device=temp_device;
puts("\nDevice found...");
}
temp_device=temp_device->next;
}
temp_bus=temp_bus->next;
}
return comm_device;
}

int main()
{
init_lib();
comm_device=find_device(0x1222,0x0007);
comm_handle=usb_open(comm_device);
char driver_buffer[64];
int num=usb_detach_kernel_driver_np(comm_handle,INTERFACE);
printf("%d",num);
num=usb_get_driver_np(comm_handle,INTERFACE,driver_buffer,64);
printf("%d",num);

return 0;
}
#41
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/25 02:36:22 (permalink)
0
xiaofan i tried with libusb non portable function but they return always -1...i have also created a rules file in /etc/udev/... but nothing is changed...
#42
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/25 03:40:22 (permalink)
0
I suggest you to test your code from root (at least for now) and test the code with a tested working firmware or test you firmware with a tested working software.
I don't know microc... but microchip (for C18) publish many working examples (software and firmware)... i suppose you could find working example for microc too.
I never used usb_detach_kernel_driver_np ... but the rest of the code seem to be ok.
#43
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/25 05:17:29 (permalink)
0
ok thank you...now i'm trying with libusb win32 and in this case function usb_set_configuration() , usb_claim_interface() and other function works (return 0)...but usb_bulk_write /read return < 0...i try again
#44
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/25 23:37:15 (permalink)
0
Under Linux, it is better you test with Root first. Then you need to write a correct udev rules to run as normal user.

Another example for you (for Jan Axelson's Generic HID example). You can see how the non-portable kernel driver detaching function is used.

http://www.microchip.com/forums/tm.aspx?m=340898


  USB_Links and libusb
#45
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/25 23:38:53 (permalink)
0
For udev rules, here is an archive copy of the Piklab wiki page.
http://mcuee.blogspot.com/2009/07/piklab-wiki-page-usb-port-permission.html

  USB_Links and libusb
#46
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/26 01:24:29 (permalink)
0
i tested my firware with a good work programin visual basic...and it works...thus my firmware hasn't problems...it works well...but my c program doesn't work with my firmware...before any i/o operation i must set_configuration claim interface (????? anything).I think that my Libusb win32 works well because when i reset my device the function usb_reset return 0 and the device is off...when i set configuration usb_set_configuration return 0 (no error)...but when i claim interface the function return -22 (error).Help me please...im going crazy!!!
#47
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/26 02:53:29 (permalink)
0
Now i'm using libusb win32 and i understand that the int to pass in function usb_set_configuration must be equals then the bConfigurationValue.it is right????
In my case bConfigurationValue = 1
if in my c code i do : " int k=usb_set_configuration(handle,1); " the program doesn't work...it is blocked on this instruction...but seems that no error occours...
if in my code i do for example : int k=usb_set_configuration(handle,0); " the function returns 0 (no error) but if after i claim the interface with its specific funtion it returs always <0 (error) any number i put in function (0,1,2....).I don't understand what is the problem...
#48
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/26 06:14:42 (permalink)
0
Please post your program codes again. Please use the [code] tag. Thanks.

And please post what is working and what is not, under Linux and under Windows. Under Windows, you have to use libusb-win32 device driver to replace the native HID driver. So it is actually not a very good choice for Windows. It is better to use native HID API for Windows.

Under Linux, you have to detach the kernel HID driver (like my example).


  USB_Links and libusb
#49
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/26 08:21:48 (permalink)
0
Here there is the code of my libusb win 32 program.For linux implementation i tried to do your suggestion but the libusb return always -1 .But i preffer first resolve win 32 implementation.
 

 
#include<usb.h>
#include<stdio.h>
 
void initLibUSB()
{
    usb_init();
    printf("%d\n",usb_find_busses());
    printf("%d\n",usb_find_devices());           
}
 
void printAllDevice()
{
    struct usb_device *device;
    device=(usb_get_busses())->devices;
    struct usb_device_descriptor desc;
    int cont=0;
    while(cont!=2)
    {
                             desc=device->descriptor;
                             printf("%d\n",desc.idVendor);
                             device=device->next;
                             cont++;                       
    }    
}
 
 
struct usb_device *find_device()
{
    struct usb_device *comm_device;
    struct usb_device *device;
    device=(usb_get_busses())->devices;
    struct usb_device_descriptor desc;
    int cont=0;
    while(cont!=2)
    {
                             desc=device->descriptor;
                             if(desc.idVendor==0x1111)
                             {
                                                      printf("your device found\n");
                                                      comm_device=device;
                                                      break;
                             }
                             device=device->next;
                             cont++;                       
    }    
    return comm_device;
}

 
int main()
{
    struct usb_device *gate;
    initLibUSB();
    printAllDevice();
    gate=find_device();
   
    struct usb_dev_handle *handle=usb_open(gate);
   
    int k=usb_set_configuration(handle,1);   //at this instruction the program will block if i put 1 If i put for example 0 this function returns 0 (no error) 
    printf("%d\n",k);                                //but the next function (usb_claim_interface() return -22 (if return <0 means error)
 
    k=usb_claim_interface(handle,0);   
    printf("%d\n",k);
    
    return 0;  
}
 

#50
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/27 07:08:46 (permalink)
0
Did you install the libusb-win32 device driver? You need to use the INF wizard to create the driver INF file, then you need to replace the original HID driver with this device driver. Then you can run the libusb-win32 test program to see if your driver installation is ok or not.

  USB_Links and libusb
#51
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/27 12:14:13 (permalink)
0
of course...i found the solution on linux libusb...i'm very happy...ahah...thank you xiaofan...i have another last question...now my program and my firware communicate well...but if i put the hid_input_report_bytes = 64 the pic isn't recognized by operating systems...the hid_input_report_bytes is the field in usb_descriptor (of course you know better then me :-P)
But if i put this field over 16 byte the operating system doesn't recognize the hardware...i don't know the reason...do you know its?
And instead if i put the hid_output_report_bytes for example 64 th operating system recognizes its...only input report is important...i'm waiting your suggestions...thank you...
#52
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/29 08:01:54 (permalink)
0
sorry...i try to show better my problem...I don't understand why the byte that the pic can read are equals then the HID_INPUT_REPORT_BYTES of the USB file descriptor....
for example if i write on port 10 byte and the HID_INPUT_REPORT_BYTES are equals to 8 the pic can read max 8 bytes
#53
Saetas
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2009/12/14 05:22:04
  • Location: 0
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2009/12/15 02:09:22 (permalink)
0
Hello to all... this post is really interesting and i'm interested to use openusb in my project. But i have a problem because set_configuration and claim_interface function return -16.

my program on libusb-0.1..12 :


#include <stdio.h>
#include <string.h>
#include <usb.h>

struct usb_device *search_device(int idVendor,int idProduct)
{
struct usb_bus *bus;
struct usb_device *dev;

for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {

if((dev->descriptor.idVendor == idVendor) && (dev->descriptor.idProduct == idProduct))
{
printf("Dispositivo encontrado \n");
return dev;
}
if(bus->next== NULL){
printf("Dispositivo no encontrado \n");
return NULL;
}
}
}
}


void init_bus()
{
struct usb_device *comm_device;
usb_dev_handle *udev;

usb_init();
usb_find_busses();
usb_find_devices();
int i;

comm_device = search_device(0x0403,0x6001);
udev = usb_open(comm_device);

if (usb_reset (udev) < 0)
{
printf ("Cannot reset usb connection.\n");
Close (udev);
return;
}
printf ("Resetting usb connection.\n");


i=usb_set_configuration(udev,1);
printf("%d",i);
Close (udev);

return;

}
void Close (usb_dev_handle *m_UsbDevice)
{
printf ("Closing LibUSB\n");
usb_release_interface (m_UsbDevice, 0);
usb_close (m_UsbDevice);
return;
}
#54
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2009/12/15 19:58:56 (permalink)
0
For HID device, you have to detach the Linux kernel HID driver first in order to use libusb. Please read the posts above. You can refer to the following libusb 0.1 documentation as well.
http://libusb.sourceforge.net/doc/ref.nonportable.html

  USB_Links and libusb
#55
Saetas
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2009/12/14 05:22:04
  • Location: 0
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2009/12/16 05:27:18 (permalink)
0
Hi xiaofan,

I don't know how i have to used the functions:

int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name, int namelen);
int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface);

Both return <0. Where do i have to write the function in my program? and what are the parameters that i must put?
thank you very much!!
#56
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2009/12/17 04:02:22 (permalink)
0
Try to run your program as root.

  USB_Links and libusb
#57
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2009/12/17 04:05:43 (permalink)
0
As for the kernel driver detaching, it is quite simple. Assuming interface 0 is the one to detach from the kernel HID driver, just add sb_detach_kernel_driver_np(udev, 0) before setting up the configuration 1 (usb_set_configuration(udev,1)).

  USB_Links and libusb
#58
Saetas
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2009/12/14 05:22:04
  • Location: 0
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2009/12/17 04:16:02 (permalink)
0
Thank you very much!! I'm a noob...my problem is solved.... grin
#59
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2010/02/03 06:01:53 (permalink)
0
Minor updates for the libusb 1.0 example for the released version (the old version is using libusb 1.0 beta)


#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <libusb-1.0/libusb.h>



#define VERSION "0.1.0"

#define VENDOR_ID 0x0925

#define PRODUCT_ID 0x1456
#define WINUSB_REQUEST_1     0x01

#define WINUSB_REQUEST_2     0x02
#define CTRL_IN            (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_INTERFACE)
#define CTRL_OUT        (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_INTERFACE)
const static int PACKET_CTRL_LEN=2;

const static int PACKET_INT_LEN=2;
const static int PACKET_BULK_LEN=64;
const static int INTERFACE=0;
const static int ENDPOINT_INT_IN=0x81; /* endpoint 0x81 address for IN */
const static int ENDPOINT_INT_OUT=0x01; /* endpoint 1 address for OUT */
const static int ENDPOINT_BULK_IN=0x82; /* endpoint 0x81 address for IN */
const static int ENDPOINT_BULK_OUT=0x02; /* endpoint 1 address for OUT */
const static int TIMEOUT=5000; /* timeout in ms */


void bad(const char *why) {
    fprintf(stderr,"Fatal error> %s\n",why);
    exit(17);
}


static struct libusb_device_handle *devh = NULL;



static int find_lvr_winusb(void)
{
    devh = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID);
    return devh ? 0 : -EIO;
}


static int test_control_transfer(void)
{
       int r,i;
       char answer[PACKET_CTRL_LEN];
       char question[PACKET_CTRL_LEN];
       for (i=0;i<PACKET_CTRL_LEN; i++) question[i]=0x20+i;

    r = libusb_control_transfer(devh,CTRL_OUT,WINUSB_REQUEST_1,2, 0,question, PACKET_CTRL_LEN,TIMEOUT);
    if (r < 0) {
        fprintf(stderr, "Control Out error %d\n", r);
        return r;
    }
    r = libusb_control_transfer(devh,CTRL_IN,WINUSB_REQUEST_2,2,0, answer,PACKET_CTRL_LEN, TIMEOUT);
    if (r < 0) {
        fprintf(stderr, "Control IN error %d\n", r);
        return r;
    }
    printf("Control Transfer Loop Test Result:\n");
//       for (i=0;i< PACKET_CTRL_LEN;i++) printf("%i, %i,\n ",question[i],answer[i]);
    for(i = 0;i < PACKET_CTRL_LEN; i++) {
        if(i%8 == 0)
            printf("\n");
        printf("%02x, %02x; ",question[i],answer[i]);
    }
    printf("\n");
    printf("\n");

    return 0;
}


static int test_interrupt_transfer(void)
{
       int r,i;
    int transferred;
       char answer[PACKET_INT_LEN];
       char question[PACKET_INT_LEN];
       for (i=0;i<PACKET_INT_LEN; i++) question[i]=0x40+i;

    r = libusb_interrupt_transfer(devh, ENDPOINT_INT_OUT, question, PACKET_INT_LEN,
        &transferred,TIMEOUT);
    if (r < 0) {
        fprintf(stderr, "Interrupt write error %d\n", r);
        return r;
    }
    r = libusb_interrupt_transfer(devh, ENDPOINT_INT_IN, answer,PACKET_INT_LEN,
        &transferred, TIMEOUT);
    if (r < 0) {
        fprintf(stderr, "Interrupt read error %d\n", r);
        return r;
    }
    if (transferred < PACKET_INT_LEN) {
        fprintf(stderr, "Interrupt transfer short read (%d)\n", r);
        return -1;
    }
       printf("Interrupt Transfer Loop Test Result:\n");
//       for (i=0;i< PACKET_INT_LEN;i++) printf("%i, %i,\n ",question[i],answer[i]);
    for(i = 0;i < PACKET_INT_LEN; i++) {
        if(i%8 == 0)
            printf("\n");
        printf("%02x, %02x; ",question[i],answer[i]);
    }
    printf("\n");
    printf("\n");

    return 0;
}



static int test_bulk_transfer(void)
{
       int r,i;
    int transferred;
       char answer[PACKET_BULK_LEN];
       char question[PACKET_BULK_LEN];
       for (i=0;i<PACKET_BULK_LEN; i++) question[i]=i;

    r = libusb_bulk_transfer(devh, ENDPOINT_BULK_OUT, question, PACKET_BULK_LEN,
        &transferred,TIMEOUT);
    if (r < 0) {
        fprintf(stderr, "Bulk write error %d\n", r);
        return r;
    }
    r = libusb_bulk_transfer(devh, ENDPOINT_BULK_IN, answer,PACKET_BULK_LEN,
        &transferred, TIMEOUT);
    if (r < 0) {
        fprintf(stderr, "Bulk read error %d\n", r);
        return r;
    }
    if (transferred < PACKET_BULK_LEN) {
        fprintf(stderr, "Bulk transfer short read (%d)\n", r);
        return -1;
    }
       printf("Bulk Transfer Loop Test Result:\n");
//       for (i=0;i< PACKET_BULK_LEN;i++) printf("%i, %i,\n ",question[i],answer[i]);
    for(i = 0;i < PACKET_BULK_LEN; i++) {
        if(i%8 == 0)
            printf("\n");
        printf("%02x, %02x; ",question[i],answer[i]);
    }
    printf("\n");
    printf("\n");

    return 0;
}


int main(void)
{
    int r = 1;

    r = libusb_init(NULL);
    if (r < 0) {
        fprintf(stderr, "failed to initialise libusb\n");
        exit(1);
    }

    r = find_lvr_winusb();
    if (r < 0) {
        fprintf(stderr, "Could not find/open device\n");
        goto out;
    }
    printf("Successfully find the LVR WINUSB device\n");
    r = libusb_set_configuration(devh, 1);
    if (r < 0) {
        fprintf(stderr, "libusb_set_configuration error %d\n", r);
        goto out;
    }
    printf("Successfully set usb configuration 1\n");
    r = libusb_claim_interface(devh, 0);
    if (r < 0) {
        fprintf(stderr, "libusb_claim_interface error %d\n", r);
        goto out;
    }
    printf("Successfully claimed interface\n");

    test_control_transfer();
    test_interrupt_transfer();
    test_bulk_transfer();


    libusb_release_interface(devh, 0);
out:
//This should not be necessary if the libusb_set_configuration call is changed in the future
//    libusb_reset_device(devh);
    libusb_close(devh);
    libusb_exit(NULL);
    return r >= 0 ? r : -r;

}


  USB_Links and libusb
#60
Page: < 1234 > Showing page 3 of 4
Jump to:
© 2019 APG vNext Commercial Version 4.5