comparison playusb.c @ 0:71b9540bdd23

starting, importing Woo's code and adding minimon
author Peter Meerwald <pmeerw@pmeerw.net>
date Sat, 07 May 2011 17:29:58 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:71b9540bdd23
1 /**
2 * based on usbreplay.c
3 * -- takes a list of jpeg filenames (on stdin) and sends them to the device index X
4 *
5 */
6
7 #include <usb.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <assert.h>
13
14 struct usb_device *find_pvr(int index) {
15 struct usb_bus *bus;
16 struct usb_device *dev;
17
18 usb_init();
19 //usb_set_debug(999999);
20 usb_find_busses();
21 usb_find_devices();
22
23 while(index >= 0) {
24 for (bus = usb_busses; bus; bus = bus->next) {
25 for (dev = bus->devices; dev; dev = dev->next) {
26 if (dev->descriptor.idVendor == 0x04e8 &&
27 dev->descriptor.idProduct == 0x2028) {
28 if (index == 0)
29 return dev;
30 --index;
31 }
32 }
33 }
34 }
35 return NULL;
36 }
37
38 usb_dev_handle *dev_open(struct usb_device *dev) {
39 int res = -1;
40 char buf[256];
41 usb_dev_handle *udev;
42 int numeps = 0;
43
44 udev = usb_open(dev);
45
46 setuid(getuid());
47
48 res = usb_set_configuration(udev, 1);
49
50 usb_claim_interface(udev, 0);
51 numeps = dev->config[0].interface[0].altsetting[0].bNumEndpoints;
52 if (numeps == 0) {
53 fprintf(stderr, "** Did you forget to initialize the FX2 firmware with usbreplay -i?\n");
54 exit(1);
55 }
56
57 strcpy(buf, "** no string **");
58 res = usb_get_string_simple(udev, dev->descriptor.iManufacturer, buf, sizeof(buf));
59 fprintf(stderr, "usb_get_string_simple => %d, %s\n", res, buf);
60
61 {
62 int eplist[] = { 0x2, 0x81, 0x83 };
63 int eplength = sizeof(eplist)/sizeof(eplist[0]);
64 int *endpoint = eplist;
65 int i;
66 for (i=0; i<eplength; i++) {
67 res = usb_resetep(udev, *endpoint);
68 res = usb_clear_halt(udev, *endpoint);
69 endpoint++;
70 }
71 }
72
73 return udev;
74 }
75
76
77 void send_jpeg(FILE *f, usb_dev_handle *udev) {
78 fseek(f, 0, SEEK_END);
79 int sz = ftell(f);
80 fseek(f, 0, SEEK_SET);
81
82 #define URBBUF_MAX 0x20000
83 char buf[URBBUF_MAX];
84
85 #define HDR_LEN 12
86 char hdr[HDR_LEN] = {0xa5, 0x5a, 0x18, 0x04, 0xff, 0xff, 0xff, 0xff, 0x48, 0x00, 0x00, 0x00};
87 *(int *)(hdr+4) = sz;
88
89 memcpy(buf, hdr, HDR_LEN);
90 int off = HDR_LEN;
91
92 //printf("new file of size %d\n", sz);
93 while(!feof(f)) {
94 int nr = fread(buf+off, 1, URBBUF_MAX - off, f);
95 if (nr < 0) break;
96 // pad
97 memset(buf + off + nr, 0, URBBUF_MAX - off - nr);
98
99 // write it out chunk by chunk
100 int timeout = 1000;
101 int endpoint = 0x2;
102 int res = usb_bulk_write(udev, endpoint, buf, URBBUF_MAX, timeout);
103 //printf("writing chunk\n");
104 assert(res >= 0);
105 off = 0; // no header on subsequent chunks
106 }
107 }
108
109 int main(int argc, char *argv[]) {
110 int index = 0;
111 if (argc > 1) {
112 if (sscanf(argv[1], "%d", &index) != 1) {
113 fprintf(stderr, "Usage: %s [device index] < filelist", argv[0]);
114 return -1;
115 }
116 }
117
118 struct usb_device *dev = find_pvr(index);
119 assert (dev != NULL);
120
121 usb_dev_handle *udev = dev_open(dev);
122
123 while(1) {
124 // for each file in the list
125 while(!feof(stdin)) {
126 char fn[256];
127 fgets(fn, 256, stdin);
128 int nn = strlen(fn);
129 if (nn > 0) {
130 if (fn[nn-1] == '\n')
131 fn[nn-1] = 0;
132 FILE *f = fopen(fn, "r");
133 if (f == NULL) {
134 fprintf(stderr, "file %s not found\n", fn);
135 return 2;
136 }
137 send_jpeg(f, udev);
138 fclose(f);
139 }
140 }
141 fseek(stdin, 0, SEEK_SET);
142 }
143 return 0;
144 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.