]> www.hackdaworld.org Git - my-code/fpga.git/blob - fx2/usb_bulk_test.c
forgot to free tbuf and rbuf since they were static during first tries
[my-code/fpga.git] / fx2 / usb_bulk_test.c
1 /*
2  * usb_bulk_test.c - test fx2 bulktransfer
3  *
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
6  *
7  * author:hackbard@hackdaworld.org
8  *
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/ioctl.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 #include <linux/version.h>
21 #include <linux/usb.h>
22 #include <linux/usbdevice_fs.h>
23
24 #define BUFLEN          (1024*10)
25 #define BULKMAX         256
26 #define TIMEOUT         1
27
28
29 int main(int argc,char **argv) {
30
31         int usb_fd;
32         int random_fd;
33         int left;
34         int ret,rx,tmp;
35         unsigned char *tbuf;
36         struct usbdevfs_bulktransfer tbulk;
37         unsigned char *rbuf;
38         struct usbdevfs_bulktransfer rbulk;
39         struct usbdevfs_setinterface iface;
40
41
42         if(argc!=6) {
43                 printf("usage: %s device iface alt_setting ep_out ep_in\n",
44                        argv[0]);
45                 return -1;
46         }
47
48         tbuf=(unsigned char *)malloc(BUFLEN);
49         rbuf=(unsigned char *)malloc(BUFLEN);
50         if((rbuf==NULL)||(tbuf==NULL)) {
51                 perror("malloc test buffer");
52                 return -1;
53         }
54
55         usb_fd=open(argv[1],O_RDWR);
56         if(usb_fd<0) {
57                 perror("open usb device");
58                 return usb_fd;
59         }
60
61         random_fd=open("/dev/urandom",O_RDONLY);
62         if(random_fd<0) {
63                 perror("open random device");
64                 return random_fd;
65         }
66         printf("\npoke random data into buffer ...");
67         fflush(stdout);
68         left=BUFLEN;
69         while(left)
70                 left-=read(random_fd,&(tbuf[BUFLEN-left]),left);
71         printf(" done\n");
72         close(random_fd);
73         
74         iface.interface=atoi(argv[2]);
75         iface.altsetting=atoi(argv[3]);
76
77         tbulk.ep=strtoul(argv[4],0,16);
78         tbulk.timeout=TIMEOUT;
79         tbulk.data=&tbuf;
80
81         rbulk.ep=strtoul(argv[5],0,16);
82         rbulk.timeout=TIMEOUT;
83         rbulk.data=&rbuf;
84
85         printf("\n");
86         printf("summary:\n");
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);
92         printf("\n");
93
94         printf("setting interface %d, alternate setting %d\n",
95                iface.interface,
96                iface.altsetting);
97
98         ret=ioctl(usb_fd,USBDEVFS_SETINTERFACE,&iface);
99         if(ret<0) {
100                 perror("set interface");
101                 return ret;
102         }
103
104         left=BUFLEN;
105         while(left) {
106                 tbulk.data=&(tbuf[BUFLEN-left]);
107                 rbulk.data=&(rbuf[BUFLEN-left]);
108                 tbulk.len=BULKMAX;
109                 printf("transmit ");
110                 fflush(stdout);
111                 ret=ioctl(usb_fd,USBDEVFS_BULK,&tbulk);
112                 if(ret<0) {
113                         perror("transmitting data");
114                         return ret;
115                 }
116                 printf("done (%d) - receive ",ret);
117                 fflush(stdout);
118
119                 rx=0;
120                 while(ret) {
121                         rbulk.len=ret;
122                         rbulk.data=&(rbuf[BUFLEN-left+rx]);
123                         tmp=ioctl(usb_fd,USBDEVFS_BULK,&rbulk);
124                                 if(tmp<0) {
125                                         perror("receiving data");
126                                         return tmp;
127                                 }
128                         printf("[%d] ",tmp);
129                         rx+=tmp;
130                         ret-=tmp;
131                 }
132                 printf("done (%d)",rx);
133
134                 if(!memcmp(&tbuf[BUFLEN-left],&rbuf[BUFLEN-left],rx))
135                         printf(" <--- equal!\n");
136                 else
137                         printf(" <--- differ! :(\n");
138
139                 left-=rx;
140         }
141
142         free(tbuf);
143         free(rbuf);
144         close(usb_fd);
145
146         return 0;
147 }