possibly all files needed to make things work

This commit is contained in:
Jack Humbert 2021-02-07 19:53:50 -05:00
parent 708bb4f55d
commit 09ff4b0c99
15 changed files with 783 additions and 16 deletions

View file

@ -30,7 +30,8 @@ enum hid_report_ids {
REPORT_ID_SYSTEM,
REPORT_ID_CONSUMER,
REPORT_ID_NKRO,
REPORT_ID_JOYSTICK
REPORT_ID_JOYSTICK,
REPORT_ID_GAMEPAD
};
/* Mouse buttons */
@ -197,6 +198,29 @@ typedef struct {
#endif
} __attribute__((packed)) joystick_report_t;
#ifdef GAMEPAD_ENABLE
typedef struct {
#ifdef SWITCH_CONTROLLER_ENABLE
uint16_t Button; // 16 buttons; see JoystickButtons_t for bit mapping
uint8_t HAT; // HAT switch; one nibble w/ unused nibble
uint8_t LX; // Left Stick X
uint8_t LY; // Left Stick Y
uint8_t RX; // Right Stick X
uint8_t RY; // Right Stick Y
uint8_t VendorSpec;
#else
// TODO add generic gamepad report
uint16_t Button; // 16 buttons; see JoystickButtons_t for bit mapping
uint8_t HAT; // HAT switch; one nibble w/ unused nibble
uint8_t LX; // Left Stick X
uint8_t LY; // Left Stick Y
uint8_t RX; // Right Stick X
uint8_t RY; // Right Stick Y
uint8_t VendorSpec;
#endif
} report_gamepad_t;
#endif
/* keycode to system usage */
static inline uint16_t KEYCODE2SYSTEM(uint8_t key) {
switch (key) {

View file

@ -82,10 +82,15 @@ void raw_hid_task(void);
#ifdef CONSOLE_ENABLE
void console_task(void);
#endif
#ifdef MIDI_ENABLE
void midi_ep_task(void);
#endif
#ifdef GAMEPAD_ENABLE
void gamepad_ep_task(void);
#endif
/* TESTING
* Amber LED blinker thread, times are in milliseconds.
*/
@ -253,6 +258,9 @@ int main(void) {
#endif
#ifdef MIDI_ENABLE
midi_ep_task();
#endif
#ifdef GAMEPAD_ENABLE
gamepad_ep_task();
#endif
#ifdef VIRTSER_ENABLE
virtser_task();

View file

@ -51,6 +51,10 @@ extern keymap_config_t keymap_config;
# include "joystick.h"
#endif
#ifdef GAMEPAD_ENABLE
# include "gamepad.h"
#endif
/* ---------------------------------------------------------
* Global interface variables and declarations
* ---------------------------------------------------------
@ -109,7 +113,7 @@ static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype
return &desc;
}
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
/* keyboard endpoint state structure */
static USBInEndpointState kbd_ep_state;
/* keyboard endpoint initialization structure (IN) - see USBEndpointConfig comment at top of file */
@ -253,6 +257,9 @@ typedef struct {
#endif
#ifdef JOYSTICK_ENABLE
usb_driver_config_t joystick_driver;
#endif
#ifdef GAMEPAD_ENABLE
usb_driver_config_t gamepad_driver;
#endif
};
usb_driver_config_t array[0];
@ -298,6 +305,14 @@ static usb_driver_configs_t drivers = {
# define JOYSTICK_OUT_MODE USB_EP_MODE_TYPE_BULK
.joystick_driver = QMK_USB_DRIVER_CONFIG(JOYSTICK, 0, false),
#endif
#ifdef GAMEPAD_ENABLE
# define GAMEPAD_IN_CAPACITY 4
# define GAMEPAD_OUT_CAPACITY 4
# define GAMEPAD_IN_MODE USB_EP_MODE_TYPE_BULK
# define GAMEPAD_OUT_MODE USB_EP_MODE_TYPE_BULK
.gamepad_driver = QMK_USB_DRIVER_CONFIG(GAMEPAD, 0, false),
#endif
};
#define NUM_USB_DRIVERS (sizeof(drivers) / sizeof(usb_driver_config_t))
@ -317,7 +332,7 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
case USB_EVENT_CONFIGURED:
osalSysLockFromISR();
/* Enable the endpoints specified into the configuration. */
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
usbInitEndpointI(usbp, KEYBOARD_IN_EPNUM, &kbd_ep_config);
#endif
#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
@ -426,11 +441,12 @@ static bool usb_request_hook_cb(USBDriver *usbp) {
switch (usbp->setup[1]) { /* bRequest */
case HID_GET_REPORT:
switch (usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */
#ifdef KEYBOARD_ENABLE
case KEYBOARD_INTERFACE:
usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL);
return TRUE;
break;
#endif
#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
case MOUSE_INTERFACE:
usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL);
@ -446,7 +462,7 @@ static bool usb_request_hook_cb(USBDriver *usbp) {
break;
case HID_GET_PROTOCOL:
if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
if ((usbp->setup[4] == 0) && (usbp->setup[5] == 0)) { /* wIndex */
usbSetupTransfer(usbp, &keyboard_protocol, 1, NULL);
return TRUE;
}
@ -463,7 +479,9 @@ static bool usb_request_hook_cb(USBDriver *usbp) {
switch (usbp->setup[1]) { /* bRequest */
case HID_SET_REPORT:
switch (usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */
#ifdef KEYBOARD_ENABLE
case KEYBOARD_INTERFACE:
#endif
#if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
case SHARED_INTERFACE:
#endif
@ -474,7 +492,7 @@ static bool usb_request_hook_cb(USBDriver *usbp) {
break;
case HID_SET_PROTOCOL:
if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
if ((usbp->setup[4] == 0) && (usbp->setup[5] == 0)) { /* wIndex */
keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */
#ifdef NKRO_ENABLE
keymap_config.nkro = !!keyboard_protocol;
@ -579,7 +597,7 @@ void init_usb_driver(USBDriver *usbp) {
* ---------------------------------------------------------
*/
/* keyboard IN callback hander (a kbd report has made it IN) */
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
void kbd_in_cb(USBDriver *usbp, usbep_t ep) {
/* STUB */
(void)usbp;
@ -612,9 +630,11 @@ static void keyboard_idle_timer_cb(void *arg) {
if (keyboard_idle && keyboard_protocol) {
#endif /* NKRO_ENABLE */
/* TODO: are we sure we want the KBD_ENDPOINT? */
#ifdef KEYBOARD_ENABLE
if (!usbGetTransmitStatusI(usbp, KEYBOARD_IN_EPNUM)) {
usbStartTransmitI(usbp, KEYBOARD_IN_EPNUM, (uint8_t *)&keyboard_report_sent, KEYBOARD_EPSIZE);
}
#endif
/* rearm the timer */
chVTSetI(&keyboard_idle_timer, 4 * TIME_MS2I(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
}
@ -630,6 +650,7 @@ uint8_t keyboard_leds(void) { return keyboard_led_stats; }
/* prepare and start sending a report IN
* not callable from ISR or locked state */
void send_keyboard(report_keyboard_t *report) {
#ifdef KEYBOARD_ENABLE
osalSysLock();
if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
goto unlock;
@ -686,6 +707,7 @@ void send_keyboard(report_keyboard_t *report) {
unlock:
osalSysUnlock();
#endif
}
/* ---------------------------------------------------------
@ -939,3 +961,35 @@ void send_joystick_packet(joystick_t *joystick) {
}
#endif
#ifdef GAMEPAD_ENABLE
void send_gamepad(report_gamepad_t *report) {
osalSysLock();
if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
osalSysUnlock();
return;
}
usbStartTransmitI(&USB_DRIVER, GAMEPAD_IN_EPNUM, (uint8_t *)report, sizeof(report_gamepad_t));
osalSysUnlock();
}
bool recv_gamepad_packet(report_gamepad_t *const report) {
size_t size = chnReadTimeout(&drivers.gamepad_driver.driver, (uint8_t *)report, sizeof(report_gamepad_t), TIME_IMMEDIATE);
return size == sizeof(report_gamepad_t);
}
void gamepad_ep_task(void) {
uint8_t buffer[GAMEPAD_EPSIZE];
size_t size = 0;
do {
size_t size = chnReadTimeout(&drivers.gamepad_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
if (size > 0) {
report_gamepad_t report;
recv_gamepad_packet(&report);
}
} while (size > 0);
}
#endif

View file

@ -45,6 +45,7 @@
/*
* HID report descriptors
*/
#ifdef KEYBOARD_ENABLE
#ifdef KEYBOARD_SHARED_EP
const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = {
# define SHARED_REPORT_STARTED
@ -95,6 +96,7 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] = {
#ifndef KEYBOARD_SHARED_EP
};
#endif
#endif
#ifdef MOUSE_ENABLE
# ifndef MOUSE_SHARED_EP
@ -335,6 +337,64 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM JoystickReport[] = {
};
#endif
#ifdef GAMEPAD_ENABLE
const USB_Descriptor_HIDReport_Datatype_t PROGMEM GamepadReport[] = {
HID_RI_USAGE_PAGE(8,1), /* Generic Desktop */
HID_RI_USAGE(8,5), /* Joystick */
HID_RI_COLLECTION(8,1), /* Application */
// Buttons (2 bytes)
HID_RI_LOGICAL_MINIMUM(8,0),
HID_RI_LOGICAL_MAXIMUM(8,1),
HID_RI_PHYSICAL_MINIMUM(8,0),
HID_RI_PHYSICAL_MAXIMUM(8,1),
// The Switch will allow us to expand the original HORI descriptors to a full 16 buttons.
// The Switch will make use of 14 of those buttons.
HID_RI_REPORT_SIZE(8,1),
HID_RI_REPORT_COUNT(8,16),
HID_RI_USAGE_PAGE(8,9),
HID_RI_USAGE_MINIMUM(8,1),
HID_RI_USAGE_MAXIMUM(8,16),
HID_RI_INPUT(8,2),
// HAT Switch (1 nibble)
HID_RI_USAGE_PAGE(8,1),
HID_RI_LOGICAL_MAXIMUM(8,7),
HID_RI_PHYSICAL_MAXIMUM(16,315),
HID_RI_REPORT_SIZE(8,4),
HID_RI_REPORT_COUNT(8,1),
HID_RI_UNIT(8,20),
HID_RI_USAGE(8,57),
HID_RI_INPUT(8,66),
// There's an additional nibble here that's utilized as part of the Switch Pro Controller.
// I believe this -might- be separate U/D/L/R bits on the Switch Pro Controller, as they're utilized as four button descriptors on the Switch Pro Controller.
HID_RI_UNIT(8,0),
HID_RI_REPORT_COUNT(8,1),
HID_RI_INPUT(8,1),
// Joystick (4 bytes)
HID_RI_LOGICAL_MAXIMUM(16,255),
HID_RI_PHYSICAL_MAXIMUM(16,255),
HID_RI_USAGE(8,48),
HID_RI_USAGE(8,49),
HID_RI_USAGE(8,50),
HID_RI_USAGE(8,53),
HID_RI_REPORT_SIZE(8,8),
HID_RI_REPORT_COUNT(8,4),
HID_RI_INPUT(8,2),
// ??? Vendor Specific (1 byte)
// This byte requires additional investigation.
HID_RI_USAGE_PAGE(16,65280),
HID_RI_USAGE(8,32),
HID_RI_REPORT_COUNT(8,1),
HID_RI_INPUT(8,2),
// Output (8 bytes)
// Original observation of this suggests it to be a mirror of the inputs that we sent.
// The Switch requires us to have these descriptors available.
HID_RI_USAGE(16,9761),
HID_RI_REPORT_COUNT(8,8),
HID_RI_OUTPUT(8,2),
HID_RI_END_COLLECTION(0),
};
#endif
/*
* Device descriptor
*/
@ -390,7 +450,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
.ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_REMOTEWAKEUP),
.MaxPowerConsumption = USB_CONFIG_POWER_MA(USB_MAX_POWER_CONSUMPTION)
},
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
/*
* Keyboard
*/
@ -909,6 +969,56 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
.PollingIntervalMS = USB_POLLING_INTERVAL_MS
}
#endif
#ifdef GAMEPAD_ENABLE
/*
* Gamepad
*/
.Gamepad_Interface = {
.Header = {
.Size = sizeof(USB_Descriptor_Interface_t),
.Type = DTYPE_Interface
},
.InterfaceNumber = GAMEPAD_INTERFACE,
.AlternateSetting = 0x00,
.TotalEndpoints = 2,
.Class = HID_CSCP_HIDClass,
.SubClass = HID_CSCP_NonBootSubclass,
.Protocol = HID_CSCP_NonBootProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.Gamepad_HID = {
.Header = {
.Size = sizeof(USB_HID_Descriptor_HID_t),
.Type = HID_DTYPE_HID
},
.HIDSpec = VERSION_BCD(1, 1, 1),
.CountryCode = 0x00,
.TotalReportDescriptors = 1,
.HIDReportType = HID_DTYPE_Report,
.HIDReportLength = sizeof(GamepadReport)
},
.Gamepad_INEndpoint = {
.Header = {
.Size = sizeof(USB_Descriptor_Endpoint_t),
.Type = DTYPE_Endpoint
},
.EndpointAddress = (ENDPOINT_DIR_IN | GAMEPAD_IN_EPNUM),
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = GAMEPAD_EPSIZE,
.PollingIntervalMS = USB_POLLING_INTERVAL_MS
},
.Gamepad_OUTEndpoint = {
.Header = {
.Size = sizeof(USB_Descriptor_Endpoint_t),
.Type = DTYPE_Endpoint
},
.EndpointAddress = (ENDPOINT_DIR_OUT | GAMEPAD_OUT_EPNUM),
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = GAMEPAD_EPSIZE,
.PollingIntervalMS = USB_POLLING_INTERVAL_MS
}
#endif
};
/*
@ -1003,7 +1113,7 @@ uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const
break;
case HID_DTYPE_HID:
switch (wIndex) {
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
case KEYBOARD_INTERFACE:
Address = &ConfigurationDescriptor.Keyboard_HID;
Size = sizeof(USB_HID_Descriptor_HID_t);
@ -1046,13 +1156,19 @@ uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const
Address = &ConfigurationDescriptor.Joystick_HID;
Size = sizeof(USB_HID_Descriptor_HID_t);
break;
#endif
#ifdef GAMEPAD_ENABLE
case GAMEPAD_INTERFACE:
Address = &ConfigurationDescriptor.Gamepad_HID;
Size = sizeof(USB_HID_Descriptor_HID_t);
break;
#endif
}
break;
case HID_DTYPE_Report:
switch (wIndex) {
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
case KEYBOARD_INTERFACE:
Address = &KeyboardReport;
Size = sizeof(KeyboardReport);
@ -1096,6 +1212,12 @@ uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const
Address = &JoystickReport;
Size = sizeof(JoystickReport);
break;
#endif
#ifdef GAMEPAD_ENABLE
case GAMEPAD_INTERFACE:
Address = &GamepadReport;
Size = sizeof(GamepadReport);
break;
#endif
}

View file

@ -55,7 +55,7 @@
typedef struct {
USB_Descriptor_Configuration_Header_t Config;
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
// Keyboard HID Interface
USB_Descriptor_Interface_t Keyboard_Interface;
USB_HID_Descriptor_HID_t Keyboard_HID;
@ -129,16 +129,30 @@ typedef struct {
USB_Descriptor_Interface_t Joystick_Interface;
USB_HID_Descriptor_HID_t Joystick_HID;
USB_Descriptor_Endpoint_t Joystick_INEndpoint;
#ifdef SWITCH_CONTROLLER_ENABLE
USB_Descriptor_Endpoint_t Joystick_OUTEndpoint;
#endif
#endif
#ifdef GAMEPAD_ENABLE
// Gamepad HID Interface
USB_Descriptor_Interface_t Gamepad_Interface;
USB_HID_Descriptor_HID_t Gamepad_HID;
USB_Descriptor_Endpoint_t Gamepad_INEndpoint;
USB_Descriptor_Endpoint_t Gamepad_OUTEndpoint;
#endif
} USB_Descriptor_Configuration_t;
/*
* Interface indexes
*/
enum usb_interfaces {
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
KEYBOARD_INTERFACE,
#else
#endif
#ifdef KEYBOARD_SHARED_EP
# define KEYBOARD_INTERFACE SHARED_INTERFACE
#endif
@ -171,9 +185,13 @@ enum usb_interfaces {
CDI_INTERFACE,
#endif
#if defined(JOYSTICK_ENABLE)
#ifdef JOYSTICK_ENABLE
JOYSTICK_INTERFACE,
#endif
#ifdef GAMEPAD_ENABLE
GAMEPAD_INTERFACE,
#endif
TOTAL_INTERFACES
};
@ -185,9 +203,10 @@ enum usb_interfaces {
enum usb_endpoints {
__unused_epnum__ = NEXT_EPNUM, // Endpoint numbering starts at 1
#ifndef KEYBOARD_SHARED_EP
#if !defined(KEYBOARD_SHARED_EP) && defined(KEYBOARD_ENABLE)
KEYBOARD_IN_EPNUM = NEXT_EPNUM,
#else
#endif
#ifdef KEYBOARD_SHARED_EP
# define KEYBOARD_IN_EPNUM SHARED_IN_EPNUM
#endif
@ -234,10 +253,16 @@ enum usb_endpoints {
# define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM)
# define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM)
#endif
#ifdef JOYSTICK_ENABLE
JOYSTICK_IN_EPNUM = NEXT_EPNUM,
JOYSTICK_OUT_EPNUM = NEXT_EPNUM,
#endif
#ifdef GAMEPAD_ENABLE
GAMEPAD_IN_EPNUM = NEXT_EPNUM,
GAMEPAD_OUT_EPNUM = NEXT_EPNUM,
#endif
};
#ifdef PROTOCOL_LUFA
@ -262,6 +287,7 @@ enum usb_endpoints {
#define MIDI_STREAM_EPSIZE 64
#define CDC_NOTIFICATION_EPSIZE 8
#define CDC_EPSIZE 16
#define GAMEPAD_EPSIZE 64
#define JOYSTICK_EPSIZE 8
uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const void** const DescriptorAddress);