With libusb-0.1..12 which comes with Ubuntu 8.04.
Edit: fixed a bug, not to release the interface inside the test.
#include <usb.h>
#include <stdio.h>
#define VERSION "0.1.0"
#define VENDOR_ID 0x0925
#define PRODUCT_ID 0x1456
#define INTERFACE 0
#define WINUSB_REQUEST_1 0x01
#define WINUSB_REQUEST_2 0x02
#define CTRL_IN (USB_TYPE_VENDOR | USB_ENDPOINT_IN | USB_RECIP_INTERFACE)
#define CTRL_OUT (USB_TYPE_VENDOR | USB_ENDPOINT_OUT | USB_RECIP_INTERFACE)
const static int reqCTRLLen=2;
const static int reqIntLen=2;
const static int reqBulkLen=64;
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);
}
usb_dev_handle *find_lvr_winusb();
usb_dev_handle* setup_libusb_access() {
usb_dev_handle *lvr_winusb;
usb_set_debug(255);
usb_init();
usb_find_busses();
usb_find_devices();
if(!(lvr_winusb = find_lvr_winusb())) {
printf("Couldn't find the USB device, Exiting\n");
return NULL;
}
if (usb_set_configuration(lvr_winusb, 1) < 0) {
printf("Could not set configuration 1 : %s\n");
return NULL;
}
if (usb_claim_interface(lvr_winusb, INTERFACE) < 0) {
printf("Could not claim interface: %s\n");
return NULL;
}
return lvr_winusb;
}
usb_dev_handle *find_lvr_winusb()
{
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 == VENDOR_ID &&
dev->descriptor.idProduct == PRODUCT_ID ) {
usb_dev_handle *handle;
printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID);
if (!(handle = usb_open(dev))) {
printf("Could not open USB device\n");
return NULL;
}
return handle;
}
}
}
return NULL;
}
void test_control_transfer(usb_dev_handle *dev)
{
int r,i;
char answer[reqCTRLLen];
char question[reqCTRLLen];
for (i=0;i<reqCTRLLen; i++) question[i]=0x20+i;
r = usb_control_msg(dev,CTRL_OUT,WINUSB_REQUEST_1,0, 0,question, reqCTRLLen,timeout);
if (r < 0) {
perror("USB control transfer out"); bad("USB write failed");
}
r = usb_control_msg(dev,CTRL_IN,WINUSB_REQUEST_2,0,0, answer,reqCTRLLen, timeout);
if (r < 0) {
perror("USB control transfer in"); bad("USB read failed");
}
printf("Control Transfer Loop Test Result:\n");
// for (i=0;i<reqIntLen; i++) printf("%i, %i, \n",question[i],answer[i]);
for(i = 0;i < reqIntLen; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");
printf("\n");
}
void test_interrupt_transfer(usb_dev_handle *dev)
{
int r,i;
char answer[reqIntLen];
char question[reqIntLen];
for (i=0;i<reqIntLen; i++) question[i]=0x40+i;
r = usb_interrupt_write(dev, endpoint_Int_out, question, reqIntLen, timeout);
if( r < 0 )
{
perror("USB interrupt write"); bad("USB write failed");
}
r = usb_interrupt_read(dev, endpoint_Int_in, answer, reqIntLen, timeout);
if( r != reqIntLen )
{
perror("USB interrupt read"); bad("USB read failed");
}
printf("Interrupt Transfer Loop Test Result:\n");
// for (i=0;i<reqIntLen; i++) printf("%i, %i, \n",question[i],answer[i]);
for(i = 0;i < reqIntLen; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");
printf("\n");
}
void test_bulk_transfer(usb_dev_handle *dev)
{
int r,i;
char answer[reqBulkLen];
char question[reqBulkLen];
for (i=0;i<reqBulkLen; i++) question[i]=i;
r = usb_bulk_write(dev, endpoint_Bulk_out, question, reqBulkLen, timeout);
if( r < 0 )
{
perror("USB bulk write"); bad("USB write failed");
}
r = usb_bulk_read(dev, endpoint_Bulk_in, answer, reqBulkLen, timeout);
if( r != reqBulkLen )
{
perror("USB bulk read"); bad("USB read failed");
}
printf("Bulk Transfer Loop Test Result:\n");
// for (i=0;i<reqBulkLen;i++) printf("%i, %i, \n",question[i],answer[i]);
for(i = 0;i < reqBulkLen; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");
printf("\n");
}
int main( int argc, char **argv)
{
usb_dev_handle *lvr_winusb;
if ((lvr_winusb = setup_libusb_access()) == NULL) {
exit(-1);
}
test_control_transfer(lvr_winusb);
test_interrupt_transfer(lvr_winusb);
test_bulk_transfer(lvr_winusb);
usb_release_interface(lvr_winusb, 0);
usb_close(lvr_winusb);
return 0;
}
mcuee@ubuntu804:~/Desktop/build/winusb/libusb01$ ./libusb_winusb2
usb_set_debug: Setting debugging level to 255 (on)
usb_os_init: Found USB VFS at /dev/bus/usb
usb_os_find_busses: Found 003
usb_os_find_busses: Found 002
usb_os_find_busses: Found 001
usb_os_find_devices: couldn't get connect info
usb_os_find_devices: Found 001 on 003
error obtaining child information: Operation not permitted
usb_os_find_devices: couldn't get connect info
usb_os_find_devices: Found 001 on 002
error obtaining child information: Operation not permitted
usb_os_find_devices: Found 004 on 001
usb_os_find_devices: Found 003 on 001
skipped 1 class/vendor specific interface descriptors
usb_os_find_devices: couldn't get connect info
usb_os_find_devices: Found 001 on 001
error obtaining child information: Inappropriate ioctl for device
error obtaining child information: Inappropriate ioctl for device
error obtaining child information: Operation not permitted
lvr_winusb with Vendor Id: 925 and Product Id: 1456 found.
Control Transfer Loop Test Result:
20, 20; 21, 21;
Interrupt Transfer Loop Test Result:
40, 40; 41, 41;
Bulk Transfer Loop Test Result:
00, 00; 01, 01; 02, 02; 03, 03; 04, 04; 05, 05; 06, 06; 07, 07;
08, 08; 09, 09; 0a, 0a; 0b, 0b; 0c, 0c; 0d, 0d; 0e, 0e; 0f, 0f;
10, 10; 11, 11; 12, 12; 13, 13; 14, 14; 15, 15; 16, 16; 17, 17;
18, 18; 19, 19; 1a, 1a; 1b, 1b; 1c, 1c; 1d, 1d; 1e, 1e; 1f, 1f;
20, 20; 21, 21; 22, 22; 23, 23; 24, 24; 25, 25; 26, 26; 27, 27;
28, 28; 29, 29; 2a, 2a; 2b, 2b; 2c, 2c; 2d, 2d; 2e, 2e; 2f, 2f;
30, 30; 31, 31; 32, 32; 33, 33; 34, 34; 35, 35; 36, 36; 37, 37;
38, 38; 39, 39; 3a, 3a; 3b, 3b; 3c, 3c; 3d, 3d; 3e, 3e; 3f, 3f;
<message edited by xiaofan on Tuesday, June 10, 2008 4:24 AM>