while'd version of u8vector-append replaced back with tail recursion version
[mot-flash.git] / libusb.c
blob3221de33903e54499aa040eaffd0078886d97070
1 #include <stdio.h>
2 #include <libguile.h>
3 #include <usb.h>
5 #include "iface_type.h"
7 // TODO: rewrite this with phone *detection*
8 #define IFACE_VENDOR 0x22b8
9 #define IFACE_PRODUCT 0x4903
11 #define TIMEOUT 1*1000
13 void dump(char *buf, size_t s)
15 int i;
16 if (s == 0) return;
17 for (i = 0; i < s; ++i) {
18 printf("%02hhx ", (unsigned char)buf[i]);
19 if (i % 16 == 15) printf("\n");
21 printf("\n");
24 static int init_usb()
26 usb_init();
27 usb_find_busses();
28 usb_find_devices();
29 return 0;
32 static int is_usb_flash_iface(struct usb_device *device)
34 struct usb_device_descriptor *desc = &device->descriptor;
35 if (desc->idVendor != IFACE_VENDOR || desc->idProduct != IFACE_PRODUCT) return 0;
36 if (desc->bNumConfigurations != 1) return 0;
37 return 1;
40 static struct usb_device *find_usb_flash_iface()
42 struct usb_bus *bus;
43 for (bus = usb_get_busses(); bus != NULL; bus = bus->next) {
44 struct usb_device *device;
45 for (device = bus->devices; device != NULL; device = device->next) {
46 if (is_usb_flash_iface(device)) return device;
49 return NULL;
52 static int close_usb_flash_iface(void *handle)
54 if (handle) {
55 fprintf(stderr, "iface destroyed\n");
56 usb_close((struct usb_dev_handle*)handle);
58 return 0;
61 static int write_usb_flash_iface(void *handle, uint8_t *buf, int len, const char **error_msg)
63 int ret;
64 printf("WRITE: \n");
65 dump(buf, len);
66 ret = usb_bulk_write((struct usb_dev_handle*)handle, 0x1, (char*)buf, len, TIMEOUT);
67 if (ret < 0 && error_msg) {
68 *error_msg = usb_strerror();
70 return ret;
73 static int read_usb_flash_iface(void *handle, uint8_t *buf, int len, const char **error_msg)
75 int ret;
76 ret = usb_bulk_read((struct usb_dev_handle*)handle, 0x82, (char*)buf, len, TIMEOUT);
77 printf("READ: %d\n", ret);
78 if (ret < 0 && error_msg) {
79 *error_msg = usb_strerror();
80 } else {
81 dump(buf, ret);
83 return ret;
86 SCM make_libusb_iface()
88 SCM smob;
89 struct flash_iface *iface;
90 struct usb_device *dev;
91 struct usb_dev_handle *handle;
93 if (init_usb() != 0) {
94 fprintf(stderr, "can't init usb\n");
95 return SCM_BOOL_F;
97 dev = find_usb_flash_iface();
98 if (dev == NULL) {
99 fprintf(stderr, "can't find dev\n");
100 return SCM_BOOL_F;
103 iface = (struct flash_iface*)scm_gc_malloc(sizeof(struct flash_iface), "flash-iface");
105 handle = usb_open(dev);
106 if (handle == NULL) {
107 scm_gc_free(iface, sizeof(struct flash_iface), "flash-iface");
108 return SCM_BOOL_F;
111 if (usb_claim_interface(handle, 0) < 0) {
112 scm_gc_free(iface, sizeof(struct flash_iface), "flash-iface");
113 usb_close(handle);
114 return SCM_BOOL_F;
117 iface->handle = (void*)handle;
118 iface->iface_close = &close_usb_flash_iface;
119 iface->iface_write = &write_usb_flash_iface;
120 iface->iface_read = &read_usb_flash_iface;
122 SCM_NEWSMOB(smob, flash_iface_tag, iface);
124 fprintf(stderr, "iface created\n");
125 return smob;