/* zapper.c * Tom Trebisky * 5-29-2013 * * libusb host program to talk to firmware * in my Adafruit 32u4 breakout (HID Generic) */ #include #include #include int verbose = 1; /* 03eb:204f Atmel Corp. LUFA Generic HID Demo Application */ int vid = 0x03eb; int pid = 0x204f; #define INTERFACE 0 #define WRITE_ENDPOINT 2 #define READ_ENDPOINT 0x81 /* Prototypes */ libusb_device_handle * zap_open ( void ); void zap_write ( libusb_device_handle *, void *, int ); int zap_read ( libusb_device_handle *, void *, int ); void show_usb_error ( int e ) { if ( e == LIBUSB_ERROR_IO ) printf ("USB error: IO\n" ); else if ( e == LIBUSB_ERROR_BUSY ) printf ("USB error: BUSY\n" ); else if ( e == LIBUSB_ERROR_TIMEOUT ) printf ("USB error: TIMEOUT\n" ); else printf ("USB error: %d\n", e ); } void local_sleep ( int x ) { struct timespec ts; ts.tv_sec = x; ts.tv_nsec = 0; (void) nanosleep ( &ts, NULL ); } void dump ( char *buf, int n ) { int i; for ( i=0; i 0 ) buf[0] = **argv; libusb_init( NULL ); if ( verbose > 1 ) libusb_set_debug ( NULL, LIBUSB_LOG_LEVEL_DEBUG ); else libusb_set_debug ( NULL, LIBUSB_LOG_LEVEL_NONE ); uh = zap_open (); if ( ! uh ) { printf ( "Sorry\n" ); libusb_close ( uh ); libusb_exit ( NULL ); return 1; } if ( buf[0] != ' ' ) zap_write ( uh, buf, 8 ); for ( i=0; i<9999; i++ ) { /* n = zap_read ( uh, buf, 8 ); dump ( buf, n ); */ do_temp ( uh ); local_sleep ( 1 ); } libusb_release_interface ( uh, INTERFACE ); libusb_close ( uh ); libusb_exit ( NULL ); return 0; } libusb_device_handle * zap_open ( void ) { libusb_device_handle *devh; devh = libusb_open_device_with_vid_pid ( NULL, vid, pid ); if ( ! devh ) { printf("Device %04x:%04x not found on system\n", vid, pid ); return NULL; } if ( verbose ) printf("Found device %04x:%04x !\n", vid, pid ); /* Before we can actually do IO, we have to claim the interface. * As near as I can tell, this boils down to acquiring some kind * of operating system lock. However, on linux anyway, some driver * will usually have been attached when the device plugged in, and * that driver will be holding the lock. If you don't detach that * kernel driver, you will be unable to claim the interface. * The old libusb-0.1 interface allowed you to determine the name * of the driver. (In the case of my Generic HID device the name * of the driver was "dummy"). */ if ( libusb_kernel_driver_active ( devh, INTERFACE ) == 1 ) { printf("Found kernel driver active on device %04x:%04x\n", vid, pid ); if ( libusb_detach_kernel_driver ( devh, INTERFACE ) != 0 ) { printf ( "Unable to detach kernel driver\n" ); } } if ( libusb_claim_interface(devh, INTERFACE ) < 0 ) { libusb_close (devh ); printf("Unable to claim interface\n"); return NULL; } return devh; } void zap_write_ctl ( libusb_device_handle *h, void *buf, int len ) { int rv; rv = libusb_control_transfer ( h, 0x21, /* request type */ 9, /* request */ 0x0200, /* value */ 0, /* index */ (char *)buf, len, 20 /* timeout in milliseconds */ ); if ( verbose ) printf ( "Control transfer write returns %d\n", rv ); } void zap_write ( libusb_device_handle *h, void *buf, int len ) { int count; int rv; /* rv = libusb_bulk_transfer ( h, */ rv = libusb_interrupt_transfer ( h, WRITE_ENDPOINT, (char *)buf, len, &count, 20 /* timeout in milliseconds */ ); if ( verbose ) printf ( "Interrupt write to %02x returns %d, sent %d bytes\n", WRITE_ENDPOINT, rv, count ); // return count; } int zap_read ( libusb_device_handle *h, void *buf, int len ) { int count; int rv; /* rv = libusb_bulk_transfer ( h, */ rv = libusb_interrupt_transfer ( h, READ_ENDPOINT, (char *)buf, len, &count, 20 /* timeout in milliseconds */ ); if ( verbose > 1 ) { printf ( "Interrupt read to %02x returns %d, received %d bytes\n", READ_ENDPOINT, rv, count ); if ( rv < 0 ) show_usb_error ( rv ); } return count; } /* THE END */