123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- #include <avr/eeprom.h>
- #include <avr/wdt.h>
- #include <stdint.h>
- #include "usbdrv.h"
- #include "usbconfig.h"
- #include "host.h"
- #include "report.h"
- #include "print.h"
- #include "debug.h"
- #include "host_driver.h"
- #include "vusb.h"
- #include "bootloader.h"
- #include <util/delay.h>
- static uint8_t vusb_keyboard_leds = 0;
- static uint8_t vusb_idle_rate = 0;
- #define KBUF_SIZE 16
- static report_keyboard_t kbuf[KBUF_SIZE];
- static uint8_t kbuf_head = 0;
- static uint8_t kbuf_tail = 0;
- typedef struct {
- uint8_t modifier;
- uint8_t reserved;
- uint8_t keycode[6];
- } keyboard_report_t;
- static keyboard_report_t keyboard_report;
- #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
- void vusb_transfer_keyboard(void)
- {
- for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
- if (usbInterruptIsReady()) {
- if (kbuf_head != kbuf_tail) {
- usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
- kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
- if (debug_keyboard) {
- print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("](");
- phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
- print(")\n");
- }
- }
- break;
- }
- usbPoll();
- _delay_ms(1);
- }
- }
- static uint8_t keyboard_leds(void);
- static void send_keyboard(report_keyboard_t *report);
- static void send_mouse(report_mouse_t *report);
- static void send_system(uint16_t data);
- static void send_consumer(uint16_t data);
- static host_driver_t driver = {
- keyboard_leds,
- send_keyboard,
- send_mouse,
- send_system,
- send_consumer
- };
- host_driver_t *vusb_driver(void)
- {
- return &driver;
- }
- static uint8_t keyboard_leds(void) {
- return vusb_keyboard_leds;
- }
- static void send_keyboard(report_keyboard_t *report)
- {
- uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
- if (next != kbuf_tail) {
- kbuf[kbuf_head] = *report;
- kbuf_head = next;
- } else {
- debug("kbuf: full\n");
- }
-
- usbPoll();
- vusb_transfer_keyboard();
- }
- typedef struct {
- uint8_t report_id;
- report_mouse_t report;
- } __attribute__ ((packed)) vusb_mouse_report_t;
- static void send_mouse(report_mouse_t *report)
- {
- vusb_mouse_report_t r = {
- .report_id = REPORT_ID_MOUSE,
- .report = *report
- };
- if (usbInterruptIsReady3()) {
- usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
- }
- }
- typedef struct {
- uint8_t report_id;
- uint16_t usage;
- } __attribute__ ((packed)) report_extra_t;
- static void send_system(uint16_t data)
- {
- static uint16_t last_data = 0;
- if (data == last_data) return;
- last_data = data;
- report_extra_t report = {
- .report_id = REPORT_ID_SYSTEM,
- .usage = data
- };
- if (usbInterruptIsReady3()) {
- usbSetInterrupt3((void *)&report, sizeof(report));
- }
- }
- static void send_consumer(uint16_t data)
- {
- static uint16_t last_data = 0;
- if (data == last_data) return;
- last_data = data;
- report_extra_t report = {
- .report_id = REPORT_ID_CONSUMER,
- .usage = data
- };
- if (usbInterruptIsReady3()) {
- usbSetInterrupt3((void *)&report, sizeof(report));
- }
- }
- static struct {
- uint16_t len;
- enum {
- NONE,
- BOOTLOADER,
- SET_LED
- } kind;
- } last_req;
- usbMsgLen_t usbFunctionSetup(uchar data[8])
- {
- usbRequest_t *rq = (void *)data;
- if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){
- if(rq->bRequest == USBRQ_HID_GET_REPORT){
- debug("GET_REPORT:");
-
- usbMsgPtr = (void *)&keyboard_report;
- return sizeof(keyboard_report);
- }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
- debug("GET_IDLE: ");
-
- usbMsgPtr = &vusb_idle_rate;
- return 1;
- }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
- vusb_idle_rate = rq->wValue.bytes[1];
- debug("SET_IDLE: ");
- debug_hex(vusb_idle_rate);
- }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
- debug("SET_REPORT: ");
-
- if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
- debug("SET_LED: ");
- last_req.kind = SET_LED;
- last_req.len = rq->wLength.word;
- #ifdef BOOTLOADER_SIZE
- } else if(rq->wValue.word == 0x0301) {
- last_req.kind = BOOTLOADER;
- last_req.len = rq->wLength.word;
- #endif
- }
- return USB_NO_MSG;
- } else {
- debug("UNKNOWN:");
- }
- }else{
- debug("VENDOR:");
-
- }
- debug("\n");
- return 0;
- }
- uchar usbFunctionWrite(uchar *data, uchar len)
- {
- if (last_req.len == 0) {
- return -1;
- }
- switch (last_req.kind) {
- case SET_LED:
- debug("SET_LED: ");
- debug_hex(data[0]);
- debug("\n");
- vusb_keyboard_leds = data[0];
- last_req.len = 0;
- return 1;
- break;
- case BOOTLOADER:
- usbDeviceDisconnect();
- bootloader_jump();
- return 1;
- break;
- case NONE:
- default:
- return -1;
- break;
- }
- return 1;
- }
- const PROGMEM uchar keyboard_hid_report[] = {
- 0x05, 0x01,
- 0x09, 0x06,
- 0xA1, 0x01,
- 0x75, 0x01,
- 0x95, 0x08,
- 0x05, 0x07,
- 0x19, 0xE0,
- 0x29, 0xE7,
- 0x15, 0x00,
- 0x25, 0x01,
- 0x81, 0x02,
- 0x95, 0x01,
- 0x75, 0x08,
- 0x81, 0x03,
- 0x95, 0x05,
- 0x75, 0x01,
- 0x05, 0x08,
- 0x19, 0x01,
- 0x29, 0x05,
- 0x91, 0x02,
- 0x95, 0x01,
- 0x75, 0x03,
- 0x91, 0x03,
- 0x95, 0x06,
- 0x75, 0x08,
- 0x15, 0x00,
- 0x26, 0xFF, 0x00,
- 0x05, 0x07,
- 0x19, 0x00,
- 0x29, 0xFF,
- 0x81, 0x00,
- 0xc0
- };
- const PROGMEM uchar mouse_hid_report[] = {
-
- 0x05, 0x01,
- 0x09, 0x02,
- 0xa1, 0x01,
- 0x85, REPORT_ID_MOUSE,
- 0x09, 0x01,
- 0xa1, 0x00,
-
- 0x05, 0x09,
- 0x19, 0x01,
- 0x29, 0x05,
- 0x15, 0x00,
- 0x25, 0x01,
- 0x75, 0x01,
- 0x95, 0x05,
- 0x81, 0x02,
- 0x75, 0x03,
- 0x95, 0x01,
- 0x81, 0x03,
-
- 0x05, 0x01,
- 0x09, 0x30,
- 0x09, 0x31,
- 0x15, 0x81,
- 0x25, 0x7f,
- 0x75, 0x08,
- 0x95, 0x02,
- 0x81, 0x06,
-
- 0x09, 0x38,
- 0x15, 0x81,
- 0x25, 0x7f,
- 0x35, 0x00,
- 0x45, 0x00,
- 0x75, 0x08,
- 0x95, 0x01,
- 0x81, 0x06,
-
- 0x05, 0x0c,
- 0x0a, 0x38, 0x02,
- 0x15, 0x81,
- 0x25, 0x7f,
- 0x75, 0x08,
- 0x95, 0x01,
- 0x81, 0x06,
- 0xc0,
- 0xc0,
-
- 0x05, 0x01,
- 0x09, 0x80,
- 0xa1, 0x01,
- 0x85, REPORT_ID_SYSTEM,
- 0x15, 0x01,
- 0x26, 0xb7, 0x00,
- 0x19, 0x01,
- 0x29, 0xb7,
- 0x75, 0x10,
- 0x95, 0x01,
- 0x81, 0x00,
- 0xc0,
-
- 0x05, 0x0c,
- 0x09, 0x01,
- 0xa1, 0x01,
- 0x85, REPORT_ID_CONSUMER,
- 0x15, 0x01,
- 0x26, 0x9c, 0x02,
- 0x19, 0x01,
- 0x2a, 0x9c, 0x02,
- 0x75, 0x10,
- 0x95, 0x01,
- 0x81, 0x00,
- 0xc0,
- };
- #if USB_CFG_DESCR_PROPS_CONFIGURATION
- const PROGMEM char usbDescriptorConfiguration[] = {
- 9,
- USBDESCR_CONFIG,
- 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
-
-
- 2,
- 1,
- 0,
- #if USB_CFG_IS_SELF_POWERED
- (1 << 7) | USBATTR_SELFPOWER,
- #else
- (1 << 7),
- #endif
- USB_CFG_MAX_BUS_POWER/2,
-
-
- 9,
- USBDESCR_INTERFACE,
- 0,
- 0,
- USB_CFG_HAVE_INTRIN_ENDPOINT,
- USB_CFG_INTERFACE_CLASS,
- USB_CFG_INTERFACE_SUBCLASS,
- USB_CFG_INTERFACE_PROTOCOL,
- 0,
-
- 9,
- USBDESCR_HID,
- 0x01, 0x01,
- 0x00,
- 0x01,
- 0x22,
- sizeof(keyboard_hid_report), 0,
-
- #if USB_CFG_HAVE_INTRIN_ENDPOINT
- 7,
- USBDESCR_ENDPOINT,
- (char)0x81,
- 0x03,
- 8, 0,
- USB_CFG_INTR_POLL_INTERVAL,
- #endif
-
-
- 9,
- USBDESCR_INTERFACE,
- 1,
- 0,
- USB_CFG_HAVE_INTRIN_ENDPOINT3,
- 0x03,
- 0,
- 0,
- 0,
-
- 9,
- USBDESCR_HID,
- 0x01, 0x01,
- 0x00,
- 0x01,
- 0x22,
- sizeof(mouse_hid_report), 0,
- #if USB_CFG_HAVE_INTRIN_ENDPOINT3
-
- 7,
- USBDESCR_ENDPOINT,
- (char)(0x80 | USB_CFG_EP3_NUMBER),
- 0x03,
- 8, 0,
- USB_CFG_INTR_POLL_INTERVAL,
- #endif
- };
- #endif
- USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
- {
- usbMsgLen_t len = 0;
- switch (rq->wValue.bytes[1]) {
- #if USB_CFG_DESCR_PROPS_CONFIGURATION
- case USBDESCR_CONFIG:
- usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
- len = sizeof(usbDescriptorConfiguration);
- break;
- #endif
- case USBDESCR_HID:
- switch (rq->wValue.bytes[0]) {
- case 0:
- usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
- len = 9;
- break;
- case 1:
- usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
- len = 9;
- break;
- }
- break;
- case USBDESCR_HID_REPORT:
-
- switch (rq->wIndex.word) {
- case 0:
- usbMsgPtr = (unsigned char *)keyboard_hid_report;
- len = sizeof(keyboard_hid_report);
- break;
- case 1:
- usbMsgPtr = (unsigned char *)mouse_hid_report;
- len = sizeof(mouse_hid_report);
- break;
- }
- break;
- }
-
- return len;
- }
|