diff --git a/Makefile b/Makefile index 113861b..544413f 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ obj-m += hanvon.o all: - gcc -L/usr/local/lib -lusb-1.0 hanvon-libusb.c -g - + gcc hanvon-libusb.c -L/usr/local/lib -lusb-1.0 -g + + archive: tar f - --exclude=.git -C ../ -c hanvon | gzip -c9 > ../hanvon-`date +%Y%m%d`.tgz diff --git a/hanvon-libusb.c b/hanvon-libusb.c index 912e45d..13cadb1 100644 --- a/hanvon-libusb.c +++ b/hanvon-libusb.c @@ -73,53 +73,109 @@ void callback(struct libusb_transfer *transfer) { printf("placeholder\n"); } +int find_device(libusb_device **list, unsigned int count) { + if (count < 0) { + return -1; + } + int found = -1; + struct libusb_device_descriptor desc; + for (unsigned int i = 0; i < count; i++) { + libusb_device *t = list[i]; + libusb_get_device_descriptor(list[i], &desc); + + printf( "Dev%u ID %04x:%04x\n", (i), desc.idVendor, desc.idProduct ); + + if (desc.idVendor == VENDOR_ID_HANVON) { + switch(desc.idProduct) { + default: + break; + case PRODUCT_ID_AM0806: + case PRODUCT_ID_AM0605: + case PRODUCT_ID_AM1107: + case PRODUCT_ID_AM1209: + case PRODUCT_ID_RL0604: + case PRODUCT_ID_RL0504: + case PRODUCT_ID_GP0806: + case PRODUCT_ID_GP0806B: + case PRODUCT_ID_GP0605: + case PRODUCT_ID_GP0605A: + case PRODUCT_ID_GP0504: + case PRODUCT_ID_NXS1513: + case PRODUCT_ID_GP0906: + case PRODUCT_ID_APPIV0906: + return i; + } // end switch + } // end if + } // end for + return found; +} + + + libusb_device *FindHanvon( libusb_context **context); int HandleData( void ); int main() { - libusb_context *context; - libusb_device *device; - libusb_device_handle *handle; +#define UNREF_DEVICE 1 +#define KEEP_DEVICE_REF 0 - int state = libusb_init( &context ); - if( state != STATE_SUCCESS ) + libusb_device ** devs; + int r = libusb_init(NULL); + if (r < 0) { + return r; + } + int count = libusb_get_device_list(NULL, &devs); + if (count < 0) { + libusb_exit(NULL); + return count; + } + + int index = find_device(devs, count); + if (index < 0) { + printf("Device not plugged in\n"); + return 0; + } + + printf("Device #%i\n", index); + + libusb_device_handle *h; + int state = libusb_open(devs[index], &h); + libusb_free_device_list (devs, UNREF_DEVICE); + + if (state < 0 || h == NULL) { + printf(" Something happened, %i.\n", state); + return 0; + } + + printf("Got to this point\n"); + + + // Wait and handle interrupts? + struct libusb_transfer tx; + + const int ENDPOINT_ADDR = 0x81; // bEndpointAddress from lsusb -v + const unsigned int LEN = 10; // wMaxPacketSize from lsusb -v + unsigned char buffer[LEN]; + // assign timeout of 1s, for any action is too much as is + libusb_fill_interrupt_transfer( &tx, + h, + ENDPOINT_ADDR, + buffer, + LEN, + callback, + NULL, + 130); // milliseconds + + state = libusb_submit_transfer(&tx); + if (state < 0 ) { + return state; + } + for(;;) { + libusb_handle_events(NULL); + } return state; - - device = FindHanvon(& context ); - if( device == NULL ) - return STATE_NOT_FOUND; - - state = libusb_open( device, &handle ); - if( state != STATE_SUCCESS ) - return state; - - - // Wait and handle interrupts? - struct libusb_transfer tx; - - const int ENDPOINT_ADDR = 0x81; // bEndpointAddress from lsusb -v - const unsigned int LEN = 10; // wMaxPacketSize from lsusb -v - unsigned char buffer[LEN]; - // assign timeout of 1s, for any action is too much as is - libusb_fill_interrupt_transfer(&tx, - handle, - ENDPOINT_ADDR, - buffer, - LEN, - callback, - NULL, - 130); // milliseconds - - state = libusb_submit_transfer(&tx); - if( state != STATE_SUCCESS ) - return state; - - for(;;) { - libusb_handle_events(context); - } - return state; }