initial checkin of usb_bulk_test code
authorhackbard <hackbard>
Tue, 13 Feb 2007 00:40:18 +0000 (00:40 +0000)
committerhackbard <hackbard>
Tue, 13 Feb 2007 00:40:18 +0000 (00:40 +0000)
fx2/usb_bulk_test.c [new file with mode: 0644]

diff --git a/fx2/usb_bulk_test.c b/fx2/usb_bulk_test.c
new file mode 100644 (file)
index 0000000..f68309a
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * usb_bulk_test.c - test fx2 bulktransfer
+ *
+ * usage: ./usb_bulk_test device iface alt_setting ep_out ep_in
+ * build: gcc -Wall -O3 usb_bulk_test.c -o usb_bulk_test
+ *
+ * author:hackbard@hackdaworld.org
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <linux/version.h>
+#include <linux/usb.h>
+#include <linux/usbdevice_fs.h>
+
+#define BUFLEN         (1024*10)
+#define BULKMAX                256
+#define TIMEOUT                1
+
+
+int main(int argc,char **argv) {
+
+       int usb_fd;
+       int random_fd;
+       int left;
+       int ret,rx,tmp;
+       unsigned char *tbuf;
+       struct usbdevfs_bulktransfer tbulk;
+       unsigned char *rbuf;
+       struct usbdevfs_bulktransfer rbulk;
+       struct usbdevfs_setinterface iface;
+
+
+       if(argc!=6) {
+               printf("usage: %s device iface alt_setting ep_out ep_in\n",
+                      argv[0]);
+               return -1;
+       }
+
+       tbuf=(unsigned char *)malloc(BUFLEN);
+       rbuf=(unsigned char *)malloc(BUFLEN);
+       if((rbuf==NULL)||(tbuf==NULL)) {
+               perror("malloc test buffer");
+               return -1;
+       }
+
+       usb_fd=open(argv[1],O_RDWR);
+       if(usb_fd<0) {
+               perror("open usb device");
+               return usb_fd;
+       }
+
+       random_fd=open("/dev/urandom",O_RDONLY);
+       if(random_fd<0) {
+               perror("open random device");
+               return random_fd;
+       }
+       printf("\npoke random data into buffer ...");
+       fflush(stdout);
+       left=BUFLEN;
+       while(left)
+               left-=read(random_fd,&(tbuf[BUFLEN-left]),left);
+       printf(" done\n");
+       close(random_fd);
+       
+       iface.interface=atoi(argv[2]);
+       iface.altsetting=atoi(argv[3]);
+
+       tbulk.ep=strtoul(argv[4],0,16);
+       tbulk.timeout=TIMEOUT;
+       tbulk.data=&tbuf;
+
+       rbulk.ep=strtoul(argv[5],0,16);
+       rbulk.timeout=TIMEOUT;
+       rbulk.data=&rbuf;
+
+       printf("\n");
+       printf("summary:\n");
+       printf("device %s\n",argv[1]);
+       printf("interface %d\n",iface.interface);
+       printf("alternate setting %d\n",iface.altsetting);
+       printf("bulk ep (out) %0x2\n",tbulk.ep);
+       printf("bulk ep (in) %0x2\n",rbulk.ep);
+       printf("\n");
+
+       printf("setting interface %d, alternate setting %d\n",
+              iface.interface,
+              iface.altsetting);
+
+       ret=ioctl(usb_fd,USBDEVFS_SETINTERFACE,&iface);
+       if(ret<0) {
+               perror("set interface");
+               return ret;
+       }
+
+       left=BUFLEN;
+       while(left) {
+               tbulk.data=&(tbuf[BUFLEN-left]);
+               rbulk.data=&(rbuf[BUFLEN-left]);
+               tbulk.len=BULKMAX;
+               printf("transmit ");
+               fflush(stdout);
+               ret=ioctl(usb_fd,USBDEVFS_BULK,&tbulk);
+               if(ret<0) {
+                       perror("transmitting data");
+                       return ret;
+               }
+               printf("done (%d) - receive ",ret);
+               fflush(stdout);
+
+               rx=0;
+               while(ret) {
+                       rbulk.len=ret;
+                       rbulk.data=&(rbuf[BUFLEN-left+rx]);
+                       tmp=ioctl(usb_fd,USBDEVFS_BULK,&rbulk);
+                               if(tmp<0) {
+                                       perror("receiving data");
+                                       return tmp;
+                               }
+                       printf("[%d] ",tmp);
+                       rx+=tmp;
+                       ret-=tmp;
+               }
+               printf("done (%d)",rx);
+
+               if(!memcmp(&tbuf[BUFLEN-left],&rbuf[BUFLEN-left],rx))
+                       printf(" <--- equal!\n");
+               else
+                       printf(" <--- differ! :(\n");
+
+               left-=rx;
+       }
+
+       close(usb_fd);
+
+       return 0;
+}