comment
[alphamote.git] / aperture_inc.c
blobd9208d74dafa255d707c6b3c63b4f83dcbba54f0
2 /* This file is generated with usbsnoop2libusb.pl from a usbsnoop log file. */
3 /* Latest version of the script should be in http://iki.fi/lindi/usb/usbsnoop2libusb.pl */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <signal.h>
9 #include <ctype.h>
10 #include <usb.h>
11 #if 0
12 #include <linux/usbdevice_fs.h>
13 #define LIBUSB_AUGMENT
14 #include "libusb_augment.h"
15 #endif
17 struct usb_dev_handle *devh;
19 void release_usb_device(int dummy) {
20 int ret;
21 ret = usb_release_interface(devh, 0);
22 if (!ret)
23 printf("failed to release interface: %d\n", ret);
24 usb_close(devh);
25 if (!ret)
26 printf("failed to close interface: %d\n", ret);
27 exit(1);
30 struct usb_device *find_device(int vendor, int product) {
31 struct usb_bus *bus;
33 for (bus = usb_get_busses(); bus; bus = bus->next) {
34 struct usb_device *dev;
36 for (dev = bus->devices; dev; dev = dev->next) {
37 if (dev->descriptor.idVendor == vendor
38 && dev->descriptor.idProduct == product)
39 return dev;
42 return NULL;
45 void print_bytes(char *bytes, int len) {
46 int i;
47 if (len > 0) {
48 for (i=0; i<len; i++) {
49 printf("%02x ", (int)((unsigned char)bytes[i]));
51 printf("\"");
52 for (i=0; i<len; i++) {
53 printf("%c", isprint(bytes[i]) ? bytes[i] : '.');
55 printf("\"");
60 int main(int argc, char **argv) {
61 int ret, vendor, product;
62 struct usb_device *dev;
63 char buf[65535], *endptr;
65 usb_init();
66 usb_find_busses();
67 usb_find_devices();
69 vendor = 0x054c;
70 product = 0x02e7;
71 dev = find_device(vendor, product);
72 assert(dev);
74 devh = usb_open(dev);
75 assert(devh);
77 signal(SIGTERM, release_usb_device);
79 ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
80 if (ret == 0) {
81 printf("interface 0 already claimed by driver \"%s\", attempting to detach it\n", buf);
82 ret = usb_detach_kernel_driver_np(devh, 0);
83 printf("usb_detach_kernel_driver_np returned %d\n", ret);
85 ret = usb_claim_interface(devh, 0);
86 if (ret != 0) {
87 printf("claim failed with error %d\n", ret);
88 exit(1);
91 ret = usb_set_altinterface(devh, 0);
92 assert(ret >= 0);
94 ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
95 usleep(4*1000);
96 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
97 usleep(3*1000);
98 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
99 usleep(4*1000);
100 ret = usb_release_interface(devh, 0);
101 if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
102 ret = usb_set_configuration(devh, 0x0000001);
103 ret = usb_claim_interface(devh, 0);
104 if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
105 ret = usb_set_altinterface(devh, 0);
107 // 9207: close down aperture
108 usleep(8*1000);
109 memcpy(buf, "\x10\x00\x00\x00\x01\x00\x07\x92\x10\x01\x00\x00\x07\x50\x00\x00", 0x0000010);
110 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000010, 1000);
111 usleep(4*1000);
112 memcpy(buf, "\x0d\x00\x00\x00\x02\x00\x07\x92\x10\x01\x00\x00\x01", 0x000000d);
113 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000d, 1000);
114 usleep(3*1000);
115 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
116 ret = usb_release_interface(devh, 0);
117 assert(ret == 0);
118 ret = usb_close(devh);
119 assert(ret == 0);
120 return 0;