Skip to content

Instantly share code, notes, and snippets.

@kurihei
Created May 1, 2014 15:56
Show Gist options
  • Select an option

  • Save kurihei/95c130a27b11ecfcf638 to your computer and use it in GitHub Desktop.

Select an option

Save kurihei/95c130a27b11ecfcf638 to your computer and use it in GitHub Desktop.
//gcc -o test_ez_read test_ez_read.c -lusb
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <usb.h>
//Define
#define USB_VENDOR 0x1f00
#define USB_PRODUCT 0x2012
#define TIMEOUT (5*1000)
#define EP6 0x86
/* Init USB */
struct usb_bus *USB_init()
{
usb_init();
usb_find_busses();
usb_find_devices();
return(usb_get_busses());
}
/* Find USB device */
struct usb_device *USB_find(struct usb_bus *busses, struct usb_device *dev)
{
struct usb_bus *bus;
for(bus=busses; bus; bus=bus->next){
for(dev=bus->devices; dev; dev=dev->next) {
if( (dev->descriptor.idVendor==USB_VENDOR) && (dev->descriptor.idProduct==USB_PRODUCT) ){
return( dev );
}
}
}
return( NULL );
}
/* USB Open */
struct usb_dev_handle *USB_open(struct usb_device *dev)
{
struct usb_dev_handle *udev = NULL;
udev=usb_open(dev);
if( (udev=usb_open(dev))==NULL ){
fprintf(stderr,"usb_open Error.(%s)\n",usb_strerror());
exit(1);
}
if( usb_set_configuration(udev,dev->config->bConfigurationValue)<0 ){
if( usb_detach_kernel_driver_np(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
fprintf(stderr,"usb_set_configuration Error.\n");
fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
}
}
if( usb_claim_interface(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
if( usb_detach_kernel_driver_np(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
fprintf(stderr,"usb_claim_interface Error.\n");
fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
}
}
if( usb_claim_interface(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
fprintf(stderr,"usb_claim_interface Error.(%s)\n",usb_strerror());
}
return(udev);
}
/* USB Close */
void USB_close(struct usb_dev_handle *dh)
{
if(usb_release_interface(dh, 0)){
fprintf(stderr,"usb_release_interface() failed. (%s)\n",usb_strerror());
usb_close(dh);
}
if( usb_close(dh)<0 ){
fprintf(stderr,"usb_close Error.(%s)\n",usb_strerror());
}
}
/* USB altinterface */
void USB_altinterface(struct usb_dev_handle *dh,int tyep)
{
if(usb_set_altinterface(dh,tyep)<0)
{
fprintf(stderr,"Failed to set altinterface %d: %s\n", 1,usb_strerror());
USB_close(dh);
}
}
main()
{
struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *dh;
unsigned char buf[512];
int ret;
/* Initialize */
printf("starting!\n");
bus=USB_init();
dev=USB_find(bus,dev);
if( dev==NULL ){
printf("device not found\n");
exit(1); }
printf("Initialize OK\n");
/*-------------*/
/* Device Open */
/*-------------*/
dh=USB_open(dev);
if( dh==NULL ){ exit(2); }
printf("Device Open OK\n");
/*
Put command here
*/
printf("Start Read data from EP6\n");
USB_altinterface(dh,1);
ret = usb_bulk_read(dh,EP6, buf, sizeof(buf), TIMEOUT);
if(ret<0){
fprintf(stderr,"usb_bulk_read() failed? buf(%d) usb(%d)\n",sizeof(buf), ret);
fprintf(stderr,"usb_bulk_read error.(%s)\n",usb_strerror());
USB_close(dh);
return 1;
}
printf("usb_bulk_read() finished\n");
fwrite(buf, 1, sizeof(buf), stdout);
USB_close(dh);
printf("USB End\n");
return 0;
}
@kurihei
Copy link
Copy Markdown
Author

kurihei commented May 1, 2014

Initialize OK
Device Open OK
Start Read data from EP6
Failed to set altinterface 1: could not set alt intf 0/1: Invalid argument
usb_bulk_read() failed? buf(512) usb(-25)
usb_bulk_read error.(error submitting URB: Inappropriate ioctl for device)
usb_release_interface() failed. (could not release intf 0: Inappropriate ioctl for device)
*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001d4d1a0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80996)[0x7f429a67e996]
/lib/x86_64-linux-gnu/libusb-0.1.so.4(usb_close+0x18)[0x7f429a9c7ac8]
./a.out[0x400f3a]
./a.out[0x401123]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7f429a61fde5]
./a.out[0x400bb9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:01 2097395 /home/kurimoto/Develop/libusb/a.out
00601000-00602000 r--p 00001000 08:01 2097395 /home/kurimoto/Develop/libusb/a.out
00602000-00603000 rw-p 00002000 08:01 2097395 /home/kurimoto/Develop/libusb/a.out
01d4c000-01d6d000 rw-p 00000000 00:00 0 [heap]
7f429a3e8000-7f429a3fd000 r-xp 00000000 08:01 786529 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f429a3fd000-7f429a5fc000 ---p 00015000 08:01 786529 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f429a5fc000-7f429a5fd000 r--p 00014000 08:01 786529 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f429a5fd000-7f429a5fe000 rw-p 00015000 08:01 786529 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f429a5fe000-7f429a7bb000 r-xp 00000000 08:01 790364 /lib/x86_64-linux-gnu/libc-2.17.so
7f429a7bb000-7f429a9bb000 ---p 001bd000 08:01 790364 /lib/x86_64-linux-gnu/libc-2.17.so
7f429a9bb000-7f429a9bf000 r--p 001bd000 08:01 790364 /lib/x86_64-linux-gnu/libc-2.17.so
7f429a9bf000-7f429a9c1000 rw-p 001c1000 08:01 790364 /lib/x86_64-linux-gnu/libc-2.17.so
7f429a9c1000-7f429a9c6000 rw-p 00000000 00:00 0
7f429a9c6000-7f429a9cd000 r-xp 00000000 08:01 790511 /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4
7f429a9cd000-7f429abcc000 ---p 00007000 08:01 790511 /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4
7f429abcc000-7f429abcd000 r--p 00006000 08:01 790511 /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4
7f429abcd000-7f429abce000 rw-p 00007000 08:01 790511 /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4
7f429abce000-7f429abcf000 rw-p 00000000 00:00 0
7f429abcf000-7f429abf2000 r-xp 00000000 08:01 790340 /lib/x86_64-linux-gnu/ld-2.17.so
7f429add8000-7f429addb000 rw-p 00000000 00:00 0
7f429aded000-7f429adf1000 rw-p 00000000 00:00 0
7f429adf1000-7f429adf2000 r--p 00022000 08:01 790340 /lib/x86_64-linux-gnu/ld-2.17.so
7f429adf2000-7f429adf4000 rw-p 00023000 08:01 790340 /lib/x86_64-linux-gnu/ld-2.17.so
7fffde629000-7fffde64a000 rw-p 00000000 00:00 0 [stack]
7fffde6ed000-7fffde6ef000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment