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
)
17 for (i
= 0; i
< s
; ++i
) {
18 printf("%02hhx ", (unsigned char)buf
[i
]);
19 if (i
% 16 == 15) printf("\n");
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;
40 static struct usb_device
*find_usb_flash_iface()
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
;
52 static int close_usb_flash_iface(void *handle
)
55 fprintf(stderr
, "iface destroyed\n");
56 usb_close((struct usb_dev_handle
*)handle
);
61 static int write_usb_flash_iface(void *handle
, uint8_t *buf
, int len
, const char **error_msg
)
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();
73 static int read_usb_flash_iface(void *handle
, uint8_t *buf
, int len
, const char **error_msg
)
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();
86 SCM
make_libusb_iface()
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");
97 dev
= find_usb_flash_iface();
99 fprintf(stderr
, "can't find dev\n");
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");
111 if (usb_claim_interface(handle
, 0) < 0) {
112 scm_gc_free(iface
, sizeof(struct flash_iface
), "flash-iface");
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");