diff options
Diffstat (limited to 'keyboard')
| -rw-r--r-- | keyboard/.gitignore | 1 | ||||
| -rw-r--r-- | keyboard/Makefile | 7 | ||||
| -rw-r--r-- | keyboard/usbtest.c | 168 |
3 files changed, 174 insertions, 2 deletions
diff --git a/keyboard/.gitignore b/keyboard/.gitignore new file mode 100644 index 0000000..c9f901b --- /dev/null +++ b/keyboard/.gitignore @@ -0,0 +1 @@ +usbtest diff --git a/keyboard/Makefile b/keyboard/Makefile index 10c831f..081ebed 100644 --- a/keyboard/Makefile +++ b/keyboard/Makefile @@ -6,10 +6,10 @@ OBJFLAGS = -j .text -j .data -O ihex OBJECTS = usbdrv/usbdrv.o usbdrv/oddebug.o usbdrv/usbdrvasm.o keyboard.o -all: keyboard.hex +all: keyboard.hex usbtest clean: - $(RM) *.o *.hex *.elf usbdrv/*.o + $(RM) *.o *.hex *.elf usbdrv/*.o usbtest %.hex: %.elf $(OBJCOPY) $(OBJFLAGS) $< $@ @@ -24,3 +24,6 @@ $(OBJECTS): usbdrv/usbconfig.h %.o: %.S $(CC) $(CFLAGS) -x assembler-with-cpp -c $< -o $@ + +usbtest: + gcc $(pkg-config --libs libusb) -O -Wall usbtest.c -o usbtest -lusb diff --git a/keyboard/usbtest.c b/keyboard/usbtest.c new file mode 100644 index 0000000..a86cb5f --- /dev/null +++ b/keyboard/usbtest.c @@ -0,0 +1,168 @@ +/** + * Project: AVR ATtiny USB Tutorial at http://codeandlife.com/ + * Author: Joonas Pihlajamaa, joonas.pihlajamaa@iki.fi + * Based on V-USB example code by Christian Starkjohann + * Copyright: (C) 2012 by Joonas Pihlajamaa + * License: GNU GPL v3 (see License.txt) + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// this is libusb, see http://libusb.sourceforge.net/ +#include <usb.h> + +// same as in main.c +#define USB_LED_OFF 0 +#define USB_LED_ON 1 +#define USB_DATA_OUT 2 +#define USB_DATA_WRITE 3 +#define USB_DATA_IN 4 + +// used to get descriptor strings for device identification +static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, + char *buf, int buflen) { + char buffer[256]; + int rval, i; + + // make standard request GET_DESCRIPTOR, type string and given index + // (e.g. dev->iProduct) + rval = usb_control_msg(dev, + USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, + buffer, sizeof(buffer), 1000); + + if(rval < 0) // error + return rval; + + // rval should be bytes read, but buffer[0] contains the actual response size + if((unsigned char)buffer[0] < rval) + rval = (unsigned char)buffer[0]; // string is shorter than bytes read + + if(buffer[1] != USB_DT_STRING) // second byte is the data type + return 0; // invalid return type + + // we're dealing with UTF-16LE here so actual chars is half of rval, + // and index 0 doesn't count + rval /= 2; + + // lossy conversion to ISO Latin1 + for(i = 1; i < rval && i < buflen; i++) { + if(buffer[2 * i + 1] == 0) + buf[i-1] = buffer[2 * i]; + else + buf[i-1] = '?'; // outside of ISO Latin1 range + } + buf[i-1] = 0; + + return i-1; +} + +static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, + int product, char *productName) { + struct usb_bus *bus; + struct usb_device *dev; + char devVendor[256], devProduct[256]; + + usb_dev_handle * handle = NULL; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + + for(bus=usb_get_busses(); bus; bus=bus->next) { + for(dev=bus->devices; dev; dev=dev->next) { + if(dev->descriptor.idVendor != vendor || + dev->descriptor.idProduct != product) + continue; + + // we need to open the device in order to query strings + if(!(handle = usb_open(dev))) { + fprintf(stderr, "Warning: cannot open USB device: %s\n", + usb_strerror()); + continue; + } + + // get vendor name + if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0) { + fprintf(stderr, + "Warning: cannot query manufacturer for device: %s\n", + usb_strerror()); + usb_close(handle); + continue; + } + + // get product name + if(usbGetDescriptorString(handle, dev->descriptor.iProduct, + 0x0409, devProduct, sizeof(devVendor)) < 0) { + fprintf(stderr, + "Warning: cannot query product for device: %s\n", + usb_strerror()); + usb_close(handle); + continue; + } + + if(strcmp(devVendor, vendorName) == 0 && + strcmp(devProduct, productName) == 0) + return handle; + else + usb_close(handle); + } + } + + return NULL; +} + +int main(int argc, char **argv) { + usb_dev_handle *handle = NULL; + int nBytes = 0; + char buffer[256]; + + if(argc < 2) { + printf("Usage:\n"); + printf("usbtext.exe on\n"); + printf("usbtext.exe off\n"); + printf("usbtext.exe out\n"); + printf("usbtext.exe write\n"); + printf("usbtext.exe in <string>\n"); + exit(1); + } + + handle = usbOpenDevice(0x16C0, "y3xz.com", 0x05DC, "keyboard"); + + if(handle == NULL) { + fprintf(stderr, "Could not find USB device!\n"); + exit(1); + } + + if(strcmp(argv[1], "on") == 0) { + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_LED_ON, 0, 0, (char *)buffer, sizeof(buffer), 5000); + } else if(strcmp(argv[1], "off") == 0) { + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_LED_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000); + } else if(strcmp(argv[1], "out") == 0) { + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000); + printf("Got %d bytes: %s\n", nBytes, buffer); + } else if(strcmp(argv[1], "write") == 0) { + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_DATA_WRITE, 'T' + ('E' << 8), 'S' + ('T' << 8), + (char *)buffer, sizeof(buffer), 5000); + } else if(strcmp(argv[1], "in") == 0 && argc > 2) { + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, + USB_DATA_IN, 0, 0, argv[2], strlen(argv[2])+1, 5000); + } + + if(nBytes < 0) + fprintf(stderr, "USB error: %s\n", usb_strerror()); + + usb_close(handle); + + return 0; +} |
