2 * usb_bulk_test.c - test fx2 bulktransfer
4 * usage: ./usb_bulk_test device iface alt_setting ep_out ep_in
5 * build: gcc -Wall -O3 usb_bulk_test.c -o usb_bulk_test
7 * author:hackbard@hackdaworld.org
14 #include <sys/types.h>
16 #include <sys/ioctl.h>
20 #include <linux/version.h>
21 #include <linux/usb.h>
22 #include <linux/usbdevice_fs.h>
24 #define BUFLEN (1024*10)
29 int main(int argc,char **argv) {
36 struct usbdevfs_bulktransfer tbulk;
38 struct usbdevfs_bulktransfer rbulk;
39 struct usbdevfs_setinterface iface;
43 printf("usage: %s device iface alt_setting ep_out ep_in\n",
48 tbuf=(unsigned char *)malloc(BUFLEN);
49 rbuf=(unsigned char *)malloc(BUFLEN);
50 if((rbuf==NULL)||(tbuf==NULL)) {
51 perror("malloc test buffer");
55 usb_fd=open(argv[1],O_RDWR);
57 perror("open usb device");
61 random_fd=open("/dev/urandom",O_RDONLY);
63 perror("open random device");
66 printf("\npoke random data into buffer ...");
70 left-=read(random_fd,&(tbuf[BUFLEN-left]),left);
74 iface.interface=atoi(argv[2]);
75 iface.altsetting=atoi(argv[3]);
77 tbulk.ep=strtoul(argv[4],0,16);
78 tbulk.timeout=TIMEOUT;
81 rbulk.ep=strtoul(argv[5],0,16);
82 rbulk.timeout=TIMEOUT;
87 printf("device %s\n",argv[1]);
88 printf("interface %d\n",iface.interface);
89 printf("alternate setting %d\n",iface.altsetting);
90 printf("bulk ep (out) %0x2\n",tbulk.ep);
91 printf("bulk ep (in) %0x2\n",rbulk.ep);
94 printf("setting interface %d, alternate setting %d\n",
98 ret=ioctl(usb_fd,USBDEVFS_SETINTERFACE,&iface);
100 perror("set interface");
106 tbulk.data=&(tbuf[BUFLEN-left]);
107 rbulk.data=&(rbuf[BUFLEN-left]);
111 ret=ioctl(usb_fd,USBDEVFS_BULK,&tbulk);
113 perror("transmitting data");
116 printf("done (%d) - receive ",ret);
122 rbulk.data=&(rbuf[BUFLEN-left+rx]);
123 tmp=ioctl(usb_fd,USBDEVFS_BULK,&rbulk);
125 perror("receiving data");
132 printf("done (%d)",rx);
134 if(!memcmp(&tbuf[BUFLEN-left],&rbuf[BUFLEN-left],rx))
135 printf(" <--- equal!\n");
137 printf(" <--- differ! :(\n");