usleep is apparently not needed during bulk download - and makes it much faster!
[alphamote.git] / aperture_dec.c
blob396b56844e0030ce3575c7f418f184aeb876dcdd
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 void print_bytes(char *bytes, int len) {
45 int i;
46 if (len > 0) {
47 for (i=0; i<len; i++) {
48 printf("%02x ", (int)((unsigned char)bytes[i]));
50 printf("\"");
51 for (i=0; i<len; i++) {
52 printf("%c", isprint(bytes[i]) ? bytes[i] : '.');
54 printf("\"");
59 int main(int argc, char **argv) {
60 int ret, vendor, product;
61 struct usb_device *dev;
62 char buf[65535], *endptr;
64 usb_init();
65 usb_find_busses();
66 usb_find_devices();
68 vendor = 0x054c;
69 product = 0x02e7;
70 dev = find_device(vendor, product);
71 assert(dev);
73 devh = usb_open(dev);
74 assert(devh);
76 signal(SIGTERM, release_usb_device);
78 ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
79 if (ret == 0) {
80 printf("interface 0 already claimed by driver \"%s\", attempting to detach it\n", buf);
81 ret = usb_detach_kernel_driver_np(devh, 0);
82 printf("usb_detach_kernel_driver_np returned %d\n", ret);
84 ret = usb_claim_interface(devh, 0);
85 if (ret != 0) {
86 printf("claim failed with error %d\n", ret);
87 exit(1);
90 ret = usb_set_altinterface(devh, 0);
91 assert(ret >= 0);
93 ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
94 usleep(4*1000);
95 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
96 usleep(3*1000);
97 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
98 usleep(4*1000);
99 ret = usb_release_interface(devh, 0);
100 if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
101 ret = usb_set_configuration(devh, 0x0000001);
102 ret = usb_claim_interface(devh, 0);
103 if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
104 ret = usb_set_altinterface(devh, 0);
106 // 9207: open up aperture
107 usleep(6*1000);
108 memcpy(buf, "\x10\x00\x00\x00\x01\x00\x07\x92\x78\x00\x00\x00\x07\x50\x00\x00", 0x0000010);
109 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000010, 1000);
110 usleep(4*1000);
111 memcpy(buf, "\x0d\x00\x00\x00\x02\x00\x07\x92\x78\x00\x00\x00\xff", 0x000000d);
112 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000d, 1000);
113 ret = usb_release_interface(devh, 0);
114 assert(ret == 0);
115 ret = usb_close(devh);
116 assert(ret == 0);
117 return 0;