diff options
Diffstat (limited to 'tmk_core/protocol/vusb')
-rw-r--r-- | tmk_core/protocol/vusb/main.c | 47 | ||||
-rw-r--r-- | tmk_core/protocol/vusb/usbconfig.h | 16 | ||||
-rw-r--r-- | tmk_core/protocol/vusb/vusb.c | 279 | ||||
-rw-r--r-- | tmk_core/protocol/vusb/vusb.h | 23 |
4 files changed, 295 insertions, 70 deletions
diff --git a/tmk_core/protocol/vusb/main.c b/tmk_core/protocol/vusb/main.c index 610638e7d4..7dc16926d2 100644 --- a/tmk_core/protocol/vusb/main.c +++ b/tmk_core/protocol/vusb/main.c @@ -21,12 +21,23 @@ #include "uart.h" #include "debug.h" #include "suspend.h" +#include "wait.h" +#include "sendchar.h" + #ifdef SLEEP_LED_ENABLE # include "sleep_led.h" #endif #define UART_BAUD_RATE 115200 +#ifdef CONSOLE_ENABLE +void console_task(void); +#endif + +#ifdef RAW_ENABLE +void raw_hid_task(void); +#endif + /* This is from main.c of USBaspLoader */ static void initForUsbConnectivity(void) { uint8_t i = 0; @@ -39,10 +50,9 @@ static void initForUsbConnectivity(void) { _delay_ms(1); } usbDeviceConnect(); - sei(); } -void usb_remote_wakeup(void) { +static void usb_remote_wakeup(void) { cli(); int8_t ddr_orig = USBDDR; @@ -59,6 +69,23 @@ void usb_remote_wakeup(void) { sei(); } +/** \brief Setup USB + * + * FIXME: Needs doc + */ +static void setup_usb(void) { + // debug("initForUsbConnectivity()\n"); + initForUsbConnectivity(); + + // for Console_Task + print_set_sendchar(sendchar); +} + +/** \brief Main + * + * FIXME: Needs doc + */ +int main(void) __attribute__((weak)); int main(void) { bool suspended = false; #if USB_COUNT_SOF @@ -76,8 +103,10 @@ int main(void) { keyboard_setup(); host_set_driver(vusb_driver()); - debug("initForUsbConnectivity()\n"); - initForUsbConnectivity(); + setup_usb(); + sei(); + + wait_ms(50); keyboard_init(); #ifdef SLEEP_LED_ENABLE @@ -120,12 +149,13 @@ int main(void) { if (!suspended) { usbPoll(); - // TODO: configuration process is incosistent. it sometime fails. + // TODO: configuration process is inconsistent. it sometime fails. // To prevent failing to configure NOT scan keyboard during configuration if (usbConfiguration && usbInterruptIsReady()) { keyboard_task(); } vusb_transfer_keyboard(); + #ifdef RAW_ENABLE usbPoll(); @@ -133,6 +163,13 @@ int main(void) { raw_hid_task(); } #endif +#ifdef CONSOLE_ENABLE + usbPoll(); + + if (usbConfiguration && usbInterruptIsReady3()) { + console_task(); + } +#endif } else if (suspend_wakeup_condition()) { usb_remote_wakeup(); } diff --git a/tmk_core/protocol/vusb/usbconfig.h b/tmk_core/protocol/vusb/usbconfig.h index f118dc8ede..dcef7584c4 100644 --- a/tmk_core/protocol/vusb/usbconfig.h +++ b/tmk_core/protocol/vusb/usbconfig.h @@ -24,15 +24,21 @@ section at the end of this file). /* ---------------------------- Hardware Config ---------------------------- */ +#ifndef USB_CFG_IOPORTNAME #define USB_CFG_IOPORTNAME D +#endif /* This is the port where the USB bus is connected. When you configure it to * "B", the registers PORTB, PINB and DDRB will be used. */ +#ifndef USB_CFG_DMINUS_BIT #define USB_CFG_DMINUS_BIT 3 +#endif /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected. * This may be any bit in the port. */ +#ifndef USB_CFG_DPLUS_BIT #define USB_CFG_DPLUS_BIT 2 +#endif /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected. * This may be any bit in the port. Please note that D+ must also be connected * to interrupt pin INT0! [You can also use other interrupts, see section @@ -151,7 +157,9 @@ section at the end of this file). /* This macro (if defined) is executed when a USB SET_ADDRESS request was * received. */ +#ifndef USB_COUNT_SOF #define USB_COUNT_SOF 1 +#endif /* define this macro to 1 if you need the global variable "usbSofCount" which * counts SOF packets. This feature requires that the hardware interrupt is * connected to D- instead of D+. @@ -321,10 +329,18 @@ section at the end of this file). /* Set INT1 for D- falling edge to count SOF */ /* #define USB_INTR_CFG EICRA */ +#ifndef USB_INTR_CFG_SET #define USB_INTR_CFG_SET ((1 << ISC11) | (0 << ISC10)) +#endif /* #define USB_INTR_CFG_CLR 0 */ /* #define USB_INTR_ENABLE EIMSK */ +#ifndef USB_INTR_ENABLE_BIT #define USB_INTR_ENABLE_BIT INT1 +#endif /* #define USB_INTR_PENDING EIFR */ +#ifndef USB_INTR_PENDING_BIT #define USB_INTR_PENDING_BIT INTF1 +#endif +#ifndef USB_INTR_VECTOR #define USB_INTR_VECTOR INT1_vect +#endif diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index c4baf5ab04..a9f37c61b0 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c @@ -15,25 +15,53 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <avr/eeprom.h> #include <avr/wdt.h> +#include <util/delay.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 <util/delay.h> +#include "print.h" +#include "debug.h" #ifdef RAW_ENABLE # include "raw_hid.h" #endif -#if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)) && defined(RAW_ENABLE) -# error "Enabling Mousekeys/Extrakeys and Raw HID at the same time is not currently supported on V-USB." +#if defined(CONSOLE_ENABLE) +# define RBUF_SIZE 128 +# include "ring_buffer.h" +#endif + +#define NEXT_INTERFACE __COUNTER__ + +/* + * Interface indexes + */ +enum usb_interfaces { + KEYBOARD_INTERFACE = NEXT_INTERFACE, +// It is important that the Raw HID interface is at a constant +// interface number, to support Linux/OSX platforms and chrome.hid +// If Raw HID is enabled, let it be always 1. +#ifdef RAW_ENABLE + RAW_INTERFACE = NEXT_INTERFACE, +#endif +#if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)) + MOUSE_EXTRA_INTERFACE = NEXT_INTERFACE, +#endif +#ifdef CONSOLE_ENABLE + CONSOLE_INTERFACE = NEXT_INTERFACE, +#endif + TOTAL_INTERFACES = NEXT_INTERFACE, +}; + +#define MAX_INTERFACES 2 + +#if (NEXT_INTERFACE - 1) > MAX_INTERFACES +# error There are not enough available interfaces to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Raw HID, Console #endif static uint8_t vusb_keyboard_leds = 0; @@ -120,7 +148,60 @@ void raw_hid_task(void) { raw_output_received_bytes = 0; } } +#endif + +/*------------------------------------------------------------------* + * Console + *------------------------------------------------------------------*/ +#ifdef CONSOLE_ENABLE +# define CONSOLE_BUFFER_SIZE 32 +# define CONSOLE_EPSIZE 8 + +int8_t sendchar(uint8_t c) { + rbuf_enqueue(c); + return 0; +} + +static inline bool usbSendData3(char *data, uint8_t len) { + uint8_t retries = 5; + while (!usbInterruptIsReady3()) { + if (!(retries--)) { + return false; + } + usbPoll(); + } + + usbSetInterrupt3((unsigned char *)data, len); + return true; +} +void console_task(void) { + if (!usbConfiguration) { + return; + } + + if (!rbuf_has_data()) { + return; + } + + // Send in chunks of 8 padded to 32 + char send_buf[CONSOLE_BUFFER_SIZE] = {0}; + uint8_t send_buf_count = 0; + while (rbuf_has_data() && send_buf_count < CONSOLE_EPSIZE) { + send_buf[send_buf_count++] = rbuf_dequeue(); + } + + char *temp = send_buf; + for (uint8_t i = 0; i < 4; i++) { + if (!usbSendData3(temp, 8)) { + break; + } + temp += 8; + } + + usbSendData3(0, 0); + usbPoll(); +} #endif /*------------------------------------------------------------------* @@ -326,6 +407,29 @@ const PROGMEM uchar keyboard_hid_report[] = { 0xC0 // End Collection }; +#ifdef RAW_ENABLE +const PROGMEM uchar raw_hid_report[] = { + 0x06, 0x60, 0xFF, // Usage Page (Vendor Defined) + 0x09, 0x61, // Usage (Vendor Defined) + 0xA1, 0x01, // Collection (Application) + // Data to host + 0x09, 0x62, // Usage (Vendor Defined) + 0x15, 0x00, // Logical Minimum (0) + 0x26, 0xFF, 0x00, // Logical Maximum (255) + 0x95, RAW_BUFFER_SIZE, // Report Count + 0x75, 0x08, // Report Size (8) + 0x81, 0x02, // Input (Data, Variable, Absolute) + // Data from host + 0x09, 0x63, // Usage (Vendor Defined) + 0x15, 0x00, // Logical Minimum (0) + 0x26, 0xFF, 0x00, // Logical Maximum (255) + 0x95, RAW_BUFFER_SIZE, // Report Count + 0x75, 0x08, // Report Size (8) + 0x91, 0x02, // Output (Data, Variable, Absolute) + 0xC0 // End Collection +}; +#endif + #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) const PROGMEM uchar mouse_extra_hid_report[] = { # ifdef MOUSE_ENABLE @@ -410,26 +514,26 @@ const PROGMEM uchar mouse_extra_hid_report[] = { }; #endif -#ifdef RAW_ENABLE -const PROGMEM uchar raw_hid_report[] = { - 0x06, 0x60, 0xFF, // Usage Page (Vendor Defined) - 0x09, 0x61, // Usage (Vendor Defined) +#if defined(CONSOLE_ENABLE) +const PROGMEM uchar console_hid_report[] = { + 0x06, 0x31, 0xFF, // Usage Page (Vendor Defined - PJRC Teensy compatible) + 0x09, 0x74, // Usage (Vendor Defined - PJRC Teensy compatible) 0xA1, 0x01, // Collection (Application) // Data to host - 0x09, 0x62, // Usage (Vendor Defined) - 0x15, 0x00, // Logical Minimum (0) - 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x95, RAW_BUFFER_SIZE, // Report Count - 0x75, 0x08, // Report Size (8) - 0x81, 0x02, // Input (Data, Variable, Absolute) + 0x09, 0x75, // Usage (Vendor Defined) + 0x15, 0x00, // Logical Minimum (0x00) + 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF) + 0x95, CONSOLE_BUFFER_SIZE, // Report Count + 0x75, 0x08, // Report Size (8) + 0x81, 0x02, // Input (Data, Variable, Absolute) // Data from host - 0x09, 0x63, // Usage (Vendor Defined) - 0x15, 0x00, // Logical Minimum (0) - 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x95, RAW_BUFFER_SIZE, // Report Count - 0x75, 0x08, // Report Size (8) - 0x91, 0x02, // Output (Data, Variable, Absolute) - 0xC0, // End Collection + 0x09, 0x76, // Usage (Vendor Defined) + 0x15, 0x00, // Logical Minimum (0x00) + 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF) + 0x95, CONSOLE_BUFFER_SIZE, // Report Count + 0x75, 0x08, // Report Size (8) + 0x91, 0x02, // Output (Data) + 0xC0 // End Collection }; #endif @@ -511,11 +615,7 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = { .bDescriptorType = USBDESCR_CONFIG }, .wTotalLength = sizeof(usbConfigurationDescriptor_t), -# if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) || defined(RAW_ENABLE) - .bNumInterfaces = 2, -# else - .bNumInterfaces = 1, -# endif + .bNumInterfaces = TOTAL_INTERFACES, .bConfigurationValue = 0x01, .iConfiguration = 0x00, .bmAttributes = (1 << 7) | USBATTR_REMOTEWAKE, @@ -530,7 +630,7 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = { .bLength = sizeof(usbInterfaceDescriptor_t), .bDescriptorType = USBDESCR_INTERFACE }, - .bInterfaceNumber = 0, + .bInterfaceNumber = KEYBOARD_INTERFACE, .bAlternateSetting = 0x00, .bNumEndpoints = 1, .bInterfaceClass = 0x03, @@ -560,6 +660,55 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = { .bInterval = USB_POLLING_INTERVAL_MS }, +# if defined(RAW_ENABLE) + /* + * RAW HID + */ + .rawInterface = { + .header = { + .bLength = sizeof(usbInterfaceDescriptor_t), + .bDescriptorType = USBDESCR_INTERFACE + }, + .bInterfaceNumber = RAW_INTERFACE, + .bAlternateSetting = 0x00, + .bNumEndpoints = 2, + .bInterfaceClass = 0x03, + .bInterfaceSubClass = 0x00, + .bInterfaceProtocol = 0x00, + .iInterface = 0x00 + }, + .rawHID = { + .header = { + .bLength = sizeof(usbHIDDescriptor_t), + .bDescriptorType = USBDESCR_HID + }, + .bcdHID = 0x0101, + .bCountryCode = 0x00, + .bNumDescriptors = 2, + .bDescriptorType = USBDESCR_HID_REPORT, + .wDescriptorLength = sizeof(raw_hid_report) + }, + .rawINEndpoint = { + .header = { + .bLength = sizeof(usbEndpointDescriptor_t), + .bDescriptorType = USBDESCR_ENDPOINT + }, + .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER), + .bmAttributes = 0x03, + .wMaxPacketSize = RAW_EPSIZE, + .bInterval = USB_POLLING_INTERVAL_MS + }, + .rawOUTEndpoint = { + .header = { + .bLength = sizeof(usbEndpointDescriptor_t), + .bDescriptorType = USBDESCR_ENDPOINT + }, + .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP3_NUMBER), + .bmAttributes = 0x03, + .wMaxPacketSize = RAW_EPSIZE, + .bInterval = USB_POLLING_INTERVAL_MS + }, +# endif # if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) /* * Mouse/Extrakeys @@ -569,7 +718,7 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = { .bLength = sizeof(usbInterfaceDescriptor_t), .bDescriptorType = USBDESCR_INTERFACE }, - .bInterfaceNumber = 1, + .bInterfaceNumber = MOUSE_EXTRA_INTERFACE, .bAlternateSetting = 0x00, .bNumEndpoints = 1, .bInterfaceClass = 0x03, @@ -597,14 +746,18 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = { .bmAttributes = 0x03, .wMaxPacketSize = 8, .bInterval = USB_POLLING_INTERVAL_MS - } -# elif defined(RAW_ENABLE) - .rawInterface = { + }, +# endif +# if defined(CONSOLE_ENABLE) + /* + * Console + */ + .consoleInterface = { .header = { .bLength = sizeof(usbInterfaceDescriptor_t), .bDescriptorType = USBDESCR_INTERFACE }, - .bInterfaceNumber = 1, + .bInterfaceNumber = CONSOLE_INTERFACE, .bAlternateSetting = 0x00, .bNumEndpoints = 2, .bInterfaceClass = 0x03, @@ -612,37 +765,37 @@ const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = { .bInterfaceProtocol = 0x00, .iInterface = 0x00 }, - .rawHID = { + .consoleHID = { .header = { .bLength = sizeof(usbHIDDescriptor_t), .bDescriptorType = USBDESCR_HID }, - .bcdHID = 0x0101, + .bcdHID = 0x0111, .bCountryCode = 0x00, - .bNumDescriptors = 2, + .bNumDescriptors = 1, .bDescriptorType = USBDESCR_HID_REPORT, - .wDescriptorLength = sizeof(raw_hid_report) + .wDescriptorLength = sizeof(console_hid_report) }, - .rawINEndpoint = { + .consoleINEndpoint = { .header = { .bLength = sizeof(usbEndpointDescriptor_t), .bDescriptorType = USBDESCR_ENDPOINT }, .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER), .bmAttributes = 0x03, - .wMaxPacketSize = RAW_EPSIZE, - .bInterval = USB_POLLING_INTERVAL_MS + .wMaxPacketSize = CONSOLE_EPSIZE, + .bInterval = 0x01 }, - .rawOUTEndpoint = { + .consoleOUTEndpoint = { .header = { .bLength = sizeof(usbEndpointDescriptor_t), .bDescriptorType = USBDESCR_ENDPOINT }, .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP3_NUMBER), .bmAttributes = 0x03, - .wMaxPacketSize = RAW_EPSIZE, - .bInterval = USB_POLLING_INTERVAL_MS - } + .wMaxPacketSize = CONSOLE_EPSIZE, + .bInterval = 0x01 + }, # endif }; @@ -690,18 +843,25 @@ USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) { break; case USBDESCR_HID: switch (rq->wValue.bytes[0]) { - case 0: + case KEYBOARD_INTERFACE: usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.keyboardHID; len = sizeof(usbHIDDescriptor_t); break; +#if defined(RAW_ENABLE) + case RAW_INTERFACE: + usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.rawHID; + len = sizeof(usbHIDDescriptor_t); + break; +#endif #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) - case 1: + case MOUSE_EXTRA_INTERFACE: usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.mouseExtraHID; len = sizeof(usbHIDDescriptor_t); break; -#elif defined(RAW_ENABLE) - case 1: - usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.rawHID; +#endif +#if defined(CONSOLE_ENABLE) + case CONSOLE_INTERFACE: + usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.consoleHID; len = sizeof(usbHIDDescriptor_t); break; #endif @@ -710,19 +870,26 @@ USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) { case USBDESCR_HID_REPORT: /* interface index */ switch (rq->wIndex.word) { - case 0: + case KEYBOARD_INTERFACE: usbMsgPtr = (unsigned char *)keyboard_hid_report; len = sizeof(keyboard_hid_report); break; +#if defined(RAW_ENABLE) + case RAW_INTERFACE: + usbMsgPtr = (unsigned char *)raw_hid_report; + len = sizeof(raw_hid_report); + break; +#endif #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) - case 1: + case MOUSE_EXTRA_INTERFACE: usbMsgPtr = (unsigned char *)mouse_extra_hid_report; len = sizeof(mouse_extra_hid_report); break; -#elif defined(RAW_ENABLE) - case 1: - usbMsgPtr = (unsigned char *)raw_hid_report; - len = sizeof(raw_hid_report); +#endif +#if defined(CONSOLE_ENABLE) + case CONSOLE_INTERFACE: + usbMsgPtr = (unsigned char *)console_hid_report; + len = sizeof(console_hid_report); break; #endif } diff --git a/tmk_core/protocol/vusb/vusb.h b/tmk_core/protocol/vusb/vusb.h index e097323fd0..828960fc35 100644 --- a/tmk_core/protocol/vusb/vusb.h +++ b/tmk_core/protocol/vusb/vusb.h @@ -89,23 +89,28 @@ typedef struct usbConfigurationDescriptor { usbHIDDescriptor_t keyboardHID; usbEndpointDescriptor_t keyboardINEndpoint; -#if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) - usbInterfaceDescriptor_t mouseExtraInterface; - usbHIDDescriptor_t mouseExtraHID; - usbEndpointDescriptor_t mouseExtraINEndpoint; -#elif defined(RAW_ENABLE) +#if defined(RAW_ENABLE) usbInterfaceDescriptor_t rawInterface; usbHIDDescriptor_t rawHID; usbEndpointDescriptor_t rawINEndpoint; usbEndpointDescriptor_t rawOUTEndpoint; #endif + +#if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) + usbInterfaceDescriptor_t mouseExtraInterface; + usbHIDDescriptor_t mouseExtraHID; + usbEndpointDescriptor_t mouseExtraINEndpoint; +#endif + +#if defined(CONSOLE_ENABLE) + usbInterfaceDescriptor_t consoleInterface; + usbHIDDescriptor_t consoleHID; + usbEndpointDescriptor_t consoleINEndpoint; + usbEndpointDescriptor_t consoleOUTEndpoint; +#endif } __attribute__((packed)) usbConfigurationDescriptor_t; #define USB_STRING_LEN(s) (sizeof(usbDescriptorHeader_t) + ((s) << 1)) host_driver_t *vusb_driver(void); void vusb_transfer_keyboard(void); - -#ifdef RAW_ENABLE -void raw_hid_task(void); -#endif |