aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/vusb/vusb.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/protocol/vusb/vusb.c')
-rw-r--r--tmk_core/protocol/vusb/vusb.c123
1 files changed, 90 insertions, 33 deletions
diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c
index 9ce75334a..a422903cc 100644
--- a/tmk_core/protocol/vusb/vusb.c
+++ b/tmk_core/protocol/vusb/vusb.c
@@ -46,19 +46,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
46 * Interface indexes 46 * Interface indexes
47 */ 47 */
48enum usb_interfaces { 48enum usb_interfaces {
49#ifndef KEYBOARD_SHARED_EP
49 KEYBOARD_INTERFACE = NEXT_INTERFACE, 50 KEYBOARD_INTERFACE = NEXT_INTERFACE,
51#else
52 SHARED_INTERFACE = NEXT_INTERFACE,
53# define KEYBOARD_INTERFACE SHARED_INTERFACE
54#endif
55
50// It is important that the Raw HID interface is at a constant 56// It is important that the Raw HID interface is at a constant
51// interface number, to support Linux/OSX platforms and chrome.hid 57// interface number, to support Linux/OSX platforms and chrome.hid
52// If Raw HID is enabled, let it be always 1. 58// If Raw HID is enabled, let it be always 1.
53#ifdef RAW_ENABLE 59#ifdef RAW_ENABLE
54 RAW_INTERFACE = NEXT_INTERFACE, 60 RAW_INTERFACE = NEXT_INTERFACE,
55#endif 61#endif
56#if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)) 62
57 MOUSE_EXTRA_INTERFACE = NEXT_INTERFACE, 63#if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
64 SHARED_INTERFACE = NEXT_INTERFACE,
58#endif 65#endif
66
59#ifdef CONSOLE_ENABLE 67#ifdef CONSOLE_ENABLE
60 CONSOLE_INTERFACE = NEXT_INTERFACE, 68 CONSOLE_INTERFACE = NEXT_INTERFACE,
61#endif 69#endif
70
62 TOTAL_INTERFACES = NEXT_INTERFACE 71 TOTAL_INTERFACES = NEXT_INTERFACE
63}; 72};
64 73
@@ -90,7 +99,16 @@ void vusb_transfer_keyboard(void) {
90 for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) { 99 for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
91 if (usbInterruptIsReady()) { 100 if (usbInterruptIsReady()) {
92 if (kbuf_head != kbuf_tail) { 101 if (kbuf_head != kbuf_tail) {
102#ifndef KEYBOARD_SHARED_EP
93 usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t)); 103 usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
104#else
105 // Ugly hack! :(
106 usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t) - 1);
107 while (!usbInterruptIsReady()) {
108 usbPoll();
109 }
110 usbSetInterrupt((void *)(&(kbuf[kbuf_tail].keys[5])), 1);
111#endif
94 kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE; 112 kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
95 if (debug_keyboard) { 113 if (debug_keyboard) {
96 dprintf("V-USB: kbuf[%d->%d](%02X)\n", kbuf_tail, kbuf_head, (kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail)); 114 dprintf("V-USB: kbuf[%d->%d](%02X)\n", kbuf_tail, kbuf_head, (kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
@@ -230,16 +248,18 @@ static void send_keyboard(report_keyboard_t *report) {
230 keyboard_report_sent = *report; 248 keyboard_report_sent = *report;
231} 249}
232 250
233typedef struct { 251#ifndef KEYBOARD_SHARED_EP
234 uint8_t report_id; 252# define usbInterruptIsReadyShared usbInterruptIsReady3
235 report_mouse_t report; 253# define usbSetInterruptShared usbSetInterrupt3
236} __attribute__((packed)) vusb_mouse_report_t; 254#else
255# define usbInterruptIsReadyShared usbInterruptIsReady
256# define usbSetInterruptShared usbSetInterrupt
257#endif
237 258
238static void send_mouse(report_mouse_t *report) { 259static void send_mouse(report_mouse_t *report) {
239#ifdef MOUSE_ENABLE 260#ifdef MOUSE_ENABLE
240 vusb_mouse_report_t r = {.report_id = REPORT_ID_MOUSE, .report = *report}; 261 if (usbInterruptIsReadyShared()) {
241 if (usbInterruptIsReady3()) { 262 usbSetInterruptShared((void *)report, sizeof(report_mouse_t));
242 usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
243 } 263 }
244#endif 264#endif
245} 265}
@@ -253,8 +273,8 @@ static void send_extra(uint8_t report_id, uint16_t data) {
253 last_data = data; 273 last_data = data;
254 274
255 report_extra_t report = {.report_id = report_id, .usage = data}; 275 report_extra_t report = {.report_id = report_id, .usage = data};
256 if (usbInterruptIsReady3()) { 276 if (usbInterruptIsReadyShared()) {
257 usbSetInterrupt3((void *)&report, sizeof(report)); 277 usbSetInterruptShared((void *)&report, sizeof(report_extra_t));
258 } 278 }
259} 279}
260#endif 280#endif
@@ -360,10 +380,18 @@ void usbFunctionWriteOut(uchar *data, uchar len) {
360 * Descriptors * 380 * Descriptors *
361 *------------------------------------------------------------------*/ 381 *------------------------------------------------------------------*/
362 382
383#ifdef KEYBOARD_SHARED_EP
384const PROGMEM uchar shared_hid_report[] = {
385# define SHARED_REPORT_STARTED
386#else
363const PROGMEM uchar keyboard_hid_report[] = { 387const PROGMEM uchar keyboard_hid_report[] = {
388#endif
364 0x05, 0x01, // Usage Page (Generic Desktop) 389 0x05, 0x01, // Usage Page (Generic Desktop)
365 0x09, 0x06, // Usage (Keyboard) 390 0x09, 0x06, // Usage (Keyboard)
366 0xA1, 0x01, // Collection (Application) 391 0xA1, 0x01, // Collection (Application)
392#ifdef KEYBOARD_SHARED_EP
393 0x85, REPORT_ID_KEYBOARD, // Report ID
394#endif
367 // Modifiers (8 bits) 395 // Modifiers (8 bits)
368 0x05, 0x07, // Usage Page (Keyboard/Keypad) 396 0x05, 0x07, // Usage Page (Keyboard/Keypad)
369 0x19, 0xE0, // Usage Minimum (Keyboard Left Control) 397 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
@@ -398,12 +426,17 @@ const PROGMEM uchar keyboard_hid_report[] = {
398 0x95, 0x01, // Report Count (1) 426 0x95, 0x01, // Report Count (1)
399 0x75, 0x03, // Report Size (3) 427 0x75, 0x03, // Report Size (3)
400 0x91, 0x03, // Output (Constant) 428 0x91, 0x03, // Output (Constant)
401 0xC0 // End Collection 429 0xC0, // End Collection
430#ifndef KEYBOARD_SHARED_EP
402}; 431};
432#endif
403 433
404#if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) 434#if defined(SHARED_EP_ENABLE) && !defined(SHARED_REPORT_STARTED)
405const PROGMEM uchar mouse_extra_hid_report[] = { 435const PROGMEM uchar shared_hid_report[] = {
406# ifdef MOUSE_ENABLE 436# define SHARED_REPORT_STARTED
437#endif
438
439#ifdef MOUSE_ENABLE
407 // Mouse report descriptor 440 // Mouse report descriptor
408 0x05, 0x01, // Usage Page (Generic Desktop) 441 0x05, 0x01, // Usage Page (Generic Desktop)
409 0x09, 0x02, // Usage (Mouse) 442 0x09, 0x02, // Usage (Mouse)
@@ -452,9 +485,9 @@ const PROGMEM uchar mouse_extra_hid_report[] = {
452 0x81, 0x06, // Input (Data, Variable, Relative) 485 0x81, 0x06, // Input (Data, Variable, Relative)
453 0xC0, // End Collection 486 0xC0, // End Collection
454 0xC0, // End Collection 487 0xC0, // End Collection
455# endif 488#endif
456 489
457# ifdef EXTRAKEY_ENABLE 490#ifdef EXTRAKEY_ENABLE
458 // Extrakeys report descriptor 491 // Extrakeys report descriptor
459 0x05, 0x01, // Usage Page (Generic Desktop) 492 0x05, 0x01, // Usage Page (Generic Desktop)
460 0x09, 0x80, // Usage (System Control) 493 0x09, 0x80, // Usage (System Control)
@@ -481,7 +514,8 @@ const PROGMEM uchar mouse_extra_hid_report[] = {
481 0x75, 0x10, // Report Size (16) 514 0x75, 0x10, // Report Size (16)
482 0x81, 0x00, // Input (Data, Array, Absolute) 515 0x81, 0x00, // Input (Data, Array, Absolute)
483 0xC0 // End Collection 516 0xC0 // End Collection
484# endif 517#endif
518#ifdef SHARED_EP_ENABLE
485}; 519};
486#endif 520#endif
487 521
@@ -618,6 +652,7 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
618 .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2 652 .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
619 }, 653 },
620 654
655# ifndef KEYBOARD_SHARED_EP
621 /* 656 /*
622 * Keyboard 657 * Keyboard
623 */ 658 */
@@ -655,6 +690,7 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
655 .wMaxPacketSize = 8, 690 .wMaxPacketSize = 8,
656 .bInterval = USB_POLLING_INTERVAL_MS 691 .bInterval = USB_POLLING_INTERVAL_MS
657 }, 692 },
693# endif
658 694
659# if defined(RAW_ENABLE) 695# if defined(RAW_ENABLE)
660 /* 696 /*
@@ -705,24 +741,30 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
705 .bInterval = USB_POLLING_INTERVAL_MS 741 .bInterval = USB_POLLING_INTERVAL_MS
706 }, 742 },
707# endif 743# endif
708# if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) 744
745# ifdef SHARED_EP_ENABLE
709 /* 746 /*
710 * Mouse/Extrakeys 747 * Shared
711 */ 748 */
712 .mouseExtraInterface = { 749 .sharedInterface = {
713 .header = { 750 .header = {
714 .bLength = sizeof(usbInterfaceDescriptor_t), 751 .bLength = sizeof(usbInterfaceDescriptor_t),
715 .bDescriptorType = USBDESCR_INTERFACE 752 .bDescriptorType = USBDESCR_INTERFACE
716 }, 753 },
717 .bInterfaceNumber = MOUSE_EXTRA_INTERFACE, 754 .bInterfaceNumber = SHARED_INTERFACE,
718 .bAlternateSetting = 0x00, 755 .bAlternateSetting = 0x00,
719 .bNumEndpoints = 1, 756 .bNumEndpoints = 1,
720 .bInterfaceClass = 0x03, 757 .bInterfaceClass = 0x03,
758# ifdef KEYBOARD_SHARED_EP
759 .bInterfaceSubClass = 0x01,
760 .bInterfaceProtocol = 0x01,
761# else
721 .bInterfaceSubClass = 0x00, 762 .bInterfaceSubClass = 0x00,
722 .bInterfaceProtocol = 0x00, 763 .bInterfaceProtocol = 0x00,
764# endif
723 .iInterface = 0x00 765 .iInterface = 0x00
724 }, 766 },
725 .mouseExtraHID = { 767 .sharedHID = {
726 .header = { 768 .header = {
727 .bLength = sizeof(usbHIDDescriptor_t), 769 .bLength = sizeof(usbHIDDescriptor_t),
728 .bDescriptorType = USBDESCR_HID 770 .bDescriptorType = USBDESCR_HID
@@ -731,19 +773,24 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
731 .bCountryCode = 0x00, 773 .bCountryCode = 0x00,
732 .bNumDescriptors = 1, 774 .bNumDescriptors = 1,
733 .bDescriptorType = USBDESCR_HID_REPORT, 775 .bDescriptorType = USBDESCR_HID_REPORT,
734 .wDescriptorLength = sizeof(mouse_extra_hid_report) 776 .wDescriptorLength = sizeof(shared_hid_report)
735 }, 777 },
736 .mouseExtraINEndpoint = { 778 .sharedINEndpoint = {
737 .header = { 779 .header = {
738 .bLength = sizeof(usbEndpointDescriptor_t), 780 .bLength = sizeof(usbEndpointDescriptor_t),
739 .bDescriptorType = USBDESCR_ENDPOINT 781 .bDescriptorType = USBDESCR_ENDPOINT
740 }, 782 },
783# ifdef KEYBOARD_SHARED_EP
784 .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
785# else
741 .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER), 786 .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
787# endif
742 .bmAttributes = 0x03, 788 .bmAttributes = 0x03,
743 .wMaxPacketSize = 8, 789 .wMaxPacketSize = 8,
744 .bInterval = USB_POLLING_INTERVAL_MS 790 .bInterval = USB_POLLING_INTERVAL_MS
745 }, 791 },
746# endif 792# endif
793
747# if defined(CONSOLE_ENABLE) 794# if defined(CONSOLE_ENABLE)
748 /* 795 /*
749 * Console 796 * Console
@@ -791,7 +838,7 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
791 .bmAttributes = 0x03, 838 .bmAttributes = 0x03,
792 .wMaxPacketSize = CONSOLE_EPSIZE, 839 .wMaxPacketSize = CONSOLE_EPSIZE,
793 .bInterval = 0x01 840 .bInterval = 0x01
794 }, 841 }
795# endif 842# endif
796}; 843};
797 844
@@ -833,22 +880,27 @@ USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
833 break; 880 break;
834 case USBDESCR_HID: 881 case USBDESCR_HID:
835 switch (rq->wValue.bytes[0]) { 882 switch (rq->wValue.bytes[0]) {
883#ifndef KEYBOARD_SHARED_EP
836 case KEYBOARD_INTERFACE: 884 case KEYBOARD_INTERFACE:
837 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID; 885 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID;
838 len = sizeof(usbHIDDescriptor_t); 886 len = sizeof(usbHIDDescriptor_t);
839 break; 887 break;
888#endif
889
840#if defined(RAW_ENABLE) 890#if defined(RAW_ENABLE)
841 case RAW_INTERFACE: 891 case RAW_INTERFACE:
842 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID; 892 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID;
843 len = sizeof(usbHIDDescriptor_t); 893 len = sizeof(usbHIDDescriptor_t);
844 break; 894 break;
845#endif 895#endif
846#if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) 896
847 case MOUSE_EXTRA_INTERFACE: 897#ifdef SHARED_EP_ENABLE
848 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.mouseExtraHID; 898 case SHARED_INTERFACE:
899 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.sharedHID;
849 len = sizeof(usbHIDDescriptor_t); 900 len = sizeof(usbHIDDescriptor_t);
850 break; 901 break;
851#endif 902#endif
903
852#if defined(CONSOLE_ENABLE) 904#if defined(CONSOLE_ENABLE)
853 case CONSOLE_INTERFACE: 905 case CONSOLE_INTERFACE:
854 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID; 906 usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID;
@@ -860,22 +912,27 @@ USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
860 case USBDESCR_HID_REPORT: 912 case USBDESCR_HID_REPORT:
861 /* interface index */ 913 /* interface index */
862 switch (rq->wIndex.word) { 914 switch (rq->wIndex.word) {
915#ifndef KEYBOARD_SHARED_EP
863 case KEYBOARD_INTERFACE: 916 case KEYBOARD_INTERFACE:
864 usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report; 917 usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report;
865 len = sizeof(keyboard_hid_report); 918 len = sizeof(keyboard_hid_report);
866 break; 919 break;
920#endif
921
867#if defined(RAW_ENABLE) 922#if defined(RAW_ENABLE)
868 case RAW_INTERFACE: 923 case RAW_INTERFACE:
869 usbMsgPtr = (usbMsgPtr_t)raw_hid_report; 924 usbMsgPtr = (usbMsgPtr_t)raw_hid_report;
870 len = sizeof(raw_hid_report); 925 len = sizeof(raw_hid_report);
871 break; 926 break;
872#endif 927#endif
873#if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) 928
874 case MOUSE_EXTRA_INTERFACE: 929#ifdef SHARED_EP_ENABLE
875 usbMsgPtr = (usbMsgPtr_t)mouse_extra_hid_report; 930 case SHARED_INTERFACE:
876 len = sizeof(mouse_extra_hid_report); 931 usbMsgPtr = (usbMsgPtr_t)shared_hid_report;
932 len = sizeof(shared_hid_report);
877 break; 933 break;
878#endif 934#endif
935
879#if defined(CONSOLE_ENABLE) 936#if defined(CONSOLE_ENABLE)
880 case CONSOLE_INTERFACE: 937 case CONSOLE_INTERFACE:
881 usbMsgPtr = (usbMsgPtr_t)console_hid_report; 938 usbMsgPtr = (usbMsgPtr_t)console_hid_report;