usleep is apparently not needed during bulk download - and makes it much faster!
[alphamote.git] / init.c
blobd028f800e88c8faa96a87d36042c9e346ee3cf33
1 /* This file is generated with usbsnoop2libusb.pl from a usbsnoop log file. */
2 /* Latest version of the script should be in http://iki.fi/lindi/usb/usbsnoop2libusb.pl */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <signal.h>
8 #include <ctype.h>
9 #include <usb.h>
10 #if 0
11 #include <linux/usbdevice_fs.h>
12 #define LIBUSB_AUGMENT
13 #include "libusb_augment.h"
14 #endif
16 struct usb_dev_handle *devh;
18 void release_usb_device(int dummy) {
19 int ret;
20 ret = usb_release_interface(devh, 0);
21 if (!ret)
22 printf("failed to release interface: %d\n", ret);
23 usb_close(devh);
24 if (!ret)
25 printf("failed to close interface: %d\n", ret);
26 exit(1);
29 struct usb_device *find_device(int vendor, int product) {
30 struct usb_bus *bus;
32 for (bus = usb_get_busses(); bus; bus = bus->next) {
33 struct usb_device *dev;
35 for (dev = bus->devices; dev; dev = dev->next) {
36 if (dev->descriptor.idVendor == vendor
37 && dev->descriptor.idProduct == product)
38 return dev;
41 return NULL;
44 int main(int argc, char **argv) {
45 int ret, vendor, product;
46 struct usb_device *dev;
47 char buf[65535], *endptr;
49 usb_init();
50 usb_find_busses();
51 usb_find_devices();
53 // vendor and product code for Sony alpha 700
54 vendor = 0x054c;
55 product = 0x02e7;
56 dev = find_device(vendor, product);
57 assert(dev);
59 devh = usb_open(dev);
60 assert(devh);
62 signal(SIGTERM, release_usb_device);
64 ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
65 ret = usb_claim_interface(devh, 0);
66 if (ret != 0) {
67 printf("claim failed with error %d\n", ret);
68 exit(1);
71 ret = usb_set_altinterface(devh, 0);
72 assert(ret >= 0);
74 ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
75 usleep(4*1000);
76 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
77 usleep(3*1000);
78 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
79 usleep(4*1000);
80 ret = usb_release_interface(devh, 0);
81 if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
82 ret = usb_set_configuration(devh, 0x0000001);
83 ret = usb_claim_interface(devh, 0);
84 if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
85 ret = usb_set_altinterface(devh, 0);
87 memcpy(buf, "\x10\x00\x00\x00\x01\x00\x02\x10\x00\x00\x00\x00\x01\x00\x00\x00", 0x0000010);
88 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000010, 1000);
89 usleep(4*1000);
90 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
91 if (buf[6] == 1) {
92 printf("not yet initialized, initializing ...\n");
94 else {
95 printf("already initialized, no need to do it again\n");
96 exit(0);
98 printf("\n");
99 usleep(5*1000);
100 memcpy(buf, "\x0c\x00\x00\x00\x01\x00\x01\x10\x01\x00\x00\x00", 0x000000c);
101 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000c, 1000);
102 usleep(1*1000);
103 ret = usb_interrupt_read(devh, 0x00000083, buf, 0x0000008, 1000);
104 usleep(4*1000);
105 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
106 usleep(3*1000);
107 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
108 usleep(5*1000);
109 memcpy(buf, "\x0c\x00\x00\x00\x01\x00\x04\x10\x02\x00\x00\x00", 0x000000c);
110 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000c, 1000);
111 usleep(4*1000);
112 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
113 usleep(4*1000);
114 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
115 memcpy(buf, "\x0c\x00\x00\x00\x01\x00\x03\x10\x03\x00\x00\x00", 0x000000c);
116 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000c, 1000);
117 usleep(4*1000);
118 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
119 memcpy(buf, "\x10\x00\x00\x00\x01\x00\x02\x10\x00\x00\x00\x00\x01\x00\x00\x00", 0x0000010);
120 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000010, 1000);
121 usleep(6*1000);
122 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
123 usleep(5*1000);
124 memcpy(buf, "\x0c\x00\x00\x00\x01\x00\x01\x10\x01\x00\x00\x00", 0x000000c);
125 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000c, 1000);
126 usleep(3*1000);
127 ret = usb_interrupt_read(devh, 0x00000083, buf, 0x0000008, 1000);
128 usleep(3*1000);
129 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
130 usleep(4*1000);
131 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
132 usleep(4*1000);
133 memcpy(buf, "\x0c\x00\x00\x00\x01\x00\x04\x10\x02\x00\x00\x00", 0x000000c);
134 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000c, 1000);
135 usleep(8*1000);
136 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
137 usleep(4*1000);
138 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
139 usleep(17*1000);
140 memcpy(buf, "\x18\x00\x00\x00\x01\x00\x01\x92\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x0000018);
141 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000018, 1000);
142 usleep(10*1000);
143 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
144 usleep(9*1000);
145 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
146 usleep(10*1000);
147 memcpy(buf, "\x18\x00\x00\x00\x01\x00\x01\x92\x04\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x0000018);
148 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000018, 1000);
149 usleep(4*1000);
150 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
151 usleep(4*1000);
152 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
153 usleep(4*1000);
154 memcpy(buf, "\x0c\x00\x00\x00\x01\x00\x02\x92\x05\x00\x00\x00", 0x000000c);
155 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000c, 1000);
156 usleep(6*1000);
157 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
158 usleep(7*1000);
159 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
160 usleep(6*1000);
161 memcpy(buf, "\x18\x00\x00\x00\x01\x00\x01\x92\x06\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x0000018);
162 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000018, 1000);
163 usleep(4*1000);
164 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
165 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
166 ret = usb_release_interface(devh, 0);
167 assert(ret == 0);
168 ret = usb_close(devh);
169 assert(ret == 0);
170 return 0;