Skip to content

Commit

Permalink
Various tidyups for USB descriptor code (#9005)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored and noroadsleft committed Jul 31, 2020
1 parent 49da1ed commit ac46e0a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 91 deletions.
8 changes: 4 additions & 4 deletions tmk_core/protocol/chibios/usb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extern keymap_config_t keymap_config;

uint8_t keyboard_idle __attribute__((aligned(2))) = 0;
uint8_t keyboard_protocol __attribute__((aligned(2))) = 1;
uint8_t keyboard_led_stats = 0;
uint8_t keyboard_led_state = 0;
volatile uint16_t keyboard_idle_count = 0;
static virtual_timer_t keyboard_idle_timer;
static void keyboard_idle_timer_cb(void *arg);
Expand Down Expand Up @@ -386,10 +386,10 @@ static void set_led_transfer_cb(USBDriver *usbp) {
if (usbp->setup[6] == 2) { /* LSB(wLength) */
uint8_t report_id = set_report_buf[0];
if ((report_id == REPORT_ID_KEYBOARD) || (report_id == REPORT_ID_NKRO)) {
keyboard_led_stats = set_report_buf[1];
keyboard_led_state = set_report_buf[1];
}
} else {
keyboard_led_stats = set_report_buf[0];
keyboard_led_state = set_report_buf[0];
}
}

Expand Down Expand Up @@ -610,7 +610,7 @@ static void keyboard_idle_timer_cb(void *arg) {
}

/* LED status */
uint8_t keyboard_leds(void) { return keyboard_led_stats; }
uint8_t keyboard_leds(void) { return keyboard_led_state; }

/* prepare and start sending a report IN
* not callable from ISR or locked state */
Expand Down
120 changes: 61 additions & 59 deletions tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern keymap_config_t keymap_config;
uint8_t keyboard_idle = 0;
/* 0: Boot Protocol, 1: Report Protocol(default) */
uint8_t keyboard_protocol = 1;
static uint8_t keyboard_led_stats = 0;
static uint8_t keyboard_led_state = 0;

static report_keyboard_t keyboard_report_sent;

Expand All @@ -103,30 +103,30 @@ host_driver_t lufa_driver = {
};

#ifdef VIRTSER_ENABLE
// clang-format off

USB_ClassInfo_CDC_Device_t cdc_device = {
.Config =
{
.ControlInterfaceNumber = CCI_INTERFACE,
.DataINEndpoint =
{
.Address = CDC_IN_EPADDR,
.Size = CDC_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = CDC_OUT_EPADDR,
.Size = CDC_EPSIZE,
.Banks = 1,
},
.NotificationEndpoint =
{
.Address = CDC_NOTIFICATION_EPADDR,
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1,
},
.Config = {
.ControlInterfaceNumber = CCI_INTERFACE,
.DataINEndpoint = {
.Address = (CDC_IN_EPNUM | ENDPOINT_DIR_IN),
.Size = CDC_EPSIZE,
.Banks = 1
},
.DataOUTEndpoint = {
.Address = (CDC_OUT_EPNUM | ENDPOINT_DIR_OUT),
.Size = CDC_EPSIZE,
.Banks = 1
},
.NotificationEndpoint = {
.Address = (CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN),
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1
}
}
};

// clang-format on
#endif

#ifdef RAW_ENABLE
Expand Down Expand Up @@ -254,7 +254,7 @@ static void Console_Task(void) {
// fill empty bank
while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0);

// flash senchar packet
// flush sendchar packet
if (Endpoint_IsINReady()) {
Endpoint_ClearIN();
}
Expand Down Expand Up @@ -370,45 +370,46 @@ void EVENT_USB_Device_StartOfFrame(void) {
void EVENT_USB_Device_ConfigurationChanged(void) {
bool ConfigSuccess = true;

/* Setup Keyboard HID Report Endpoints */
#ifndef KEYBOARD_SHARED_EP
ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
/* Setup keyboard report endpoint */
ConfigSuccess &= Endpoint_ConfigureEndpoint((KEYBOARD_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1);
#endif

#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
/* Setup Mouse HID Report Endpoint */
ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
/* Setup mouse report endpoint */
ConfigSuccess &= Endpoint_ConfigureEndpoint((MOUSE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, MOUSE_EPSIZE, 1);
#endif

#ifdef SHARED_EP_ENABLE
/* Setup Shared HID Report Endpoint */
ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, SHARED_EPSIZE, ENDPOINT_BANK_SINGLE);
/* Setup shared report endpoint */
ConfigSuccess &= Endpoint_ConfigureEndpoint((SHARED_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, SHARED_EPSIZE, 1);
#endif

#ifdef RAW_ENABLE
/* Setup Raw HID Report Endpoints */
ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
/* Setup raw HID endpoints */
ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1);
ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1);
#endif

#ifdef CONSOLE_ENABLE
/* Setup Console HID Report Endpoints */
ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
/* Setup console endpoint */
ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1);
# if 0
ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1);
# endif
#endif

#ifdef MIDI_ENABLE
ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
/* Setup MIDI stream endpoints */
ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
#endif

#ifdef VIRTSER_ENABLE
ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
/* Setup virtual serial endpoints */
ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_BULK, CDC_EPSIZE, 1);
ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, CDC_EPSIZE, 1);
#endif
}

Expand Down Expand Up @@ -472,10 +473,10 @@ void EVENT_USB_Device_ControlRequest(void) {
uint8_t report_id = Endpoint_Read_8();

if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) {
keyboard_led_stats = Endpoint_Read_8();
keyboard_led_state = Endpoint_Read_8();
}
} else {
keyboard_led_stats = Endpoint_Read_8();
keyboard_led_state = Endpoint_Read_8();
}

Endpoint_ClearOUT();
Expand Down Expand Up @@ -545,7 +546,7 @@ void EVENT_USB_Device_ControlRequest(void) {
*
* FIXME: Needs doc
*/
static uint8_t keyboard_leds(void) { return keyboard_led_stats; }
static uint8_t keyboard_leds(void) { return keyboard_led_state; }

/** \brief Send Keyboard
*
Expand Down Expand Up @@ -808,25 +809,26 @@ int8_t sendchar(uint8_t c) {
******************************************************************************/

#ifdef MIDI_ENABLE
// clang-format off

USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = {
.Config =
{
.StreamingInterfaceNumber = AS_INTERFACE,
.DataINEndpoint =
{
.Address = MIDI_STREAM_IN_EPADDR,
.Size = MIDI_STREAM_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = MIDI_STREAM_OUT_EPADDR,
.Size = MIDI_STREAM_EPSIZE,
.Banks = 1,
},
.Config = {
.StreamingInterfaceNumber = AS_INTERFACE,
.DataINEndpoint = {
.Address = (MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN),
.Size = MIDI_STREAM_EPSIZE,
.Banks = 1
},
.DataOUTEndpoint = {
.Address = (MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_OUT),
.Size = MIDI_STREAM_EPSIZE,
.Banks = 1
}
}
};

// clang-format on

void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); }

bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); }
Expand Down
4 changes: 0 additions & 4 deletions tmk_core/protocol/lufa/lufa.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,4 @@ extern host_driver_t lufa_driver;
# define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0))
#endif

#define ENDPOINT_BANK_SINGLE 1
#define ENDPOINT_BANK_DOUBLE 2
#define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum), eptype, epsize, epbank)

#endif
10 changes: 5 additions & 5 deletions tmk_core/protocol/usb_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t),
.Type = DTYPE_Endpoint
},
.EndpointAddress = MIDI_STREAM_OUT_EPADDR,
.EndpointAddress = (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM),
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = MIDI_STREAM_EPSIZE,
.PollingIntervalMS = 0x05
Expand All @@ -696,7 +696,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t),
.Type = DTYPE_Endpoint
},
.EndpointAddress = MIDI_STREAM_IN_EPADDR,
.EndpointAddress = (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM),
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = MIDI_STREAM_EPSIZE,
.PollingIntervalMS = 0x05
Expand Down Expand Up @@ -774,7 +774,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
.Size = sizeof(USB_Descriptor_Endpoint_t),
.Type = DTYPE_Endpoint
},
.EndpointAddress = CDC_NOTIFICATION_EPADDR,
.EndpointAddress = (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM),
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_NOTIFICATION_EPSIZE,
.PollingIntervalMS = 0xFF
Expand All @@ -797,7 +797,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
.Size = sizeof(USB_Descriptor_Endpoint_t),
.Type = DTYPE_Endpoint
},
.EndpointAddress = CDC_OUT_EPADDR,
.EndpointAddress = (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM),
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_EPSIZE,
.PollingIntervalMS = 0x05
Expand All @@ -807,7 +807,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
.Size = sizeof(USB_Descriptor_Endpoint_t),
.Type = DTYPE_Endpoint
},
.EndpointAddress = CDC_IN_EPADDR,
.EndpointAddress = (ENDPOINT_DIR_IN | CDC_IN_EPNUM),
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_EPSIZE,
.PollingIntervalMS = 0x05
Expand Down
5 changes: 0 additions & 5 deletions tmk_core/protocol/usb_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,12 @@ enum usb_endpoints {
#ifdef MIDI_ENABLE
MIDI_STREAM_IN_EPNUM = NEXT_EPNUM,
MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM,
# define MIDI_STREAM_IN_EPADDR (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM)
# define MIDI_STREAM_OUT_EPADDR (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM)
#endif

#ifdef VIRTSER_ENABLE
CDC_NOTIFICATION_EPNUM = NEXT_EPNUM,
CDC_IN_EPNUM = NEXT_EPNUM,
CDC_OUT_EPNUM = NEXT_EPNUM,
# define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM)
# define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM)
# define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM)
#endif
};

Expand Down
24 changes: 10 additions & 14 deletions tmk_core/protocol/vusb/vusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ enum usb_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;
static uint8_t keyboard_led_state = 0;
static uint8_t vusb_idle_rate = 0;

/* Keyboard report send buffer */
Expand All @@ -74,13 +74,7 @@ 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; // sent to PC
static report_keyboard_t keyboard_report_sent;

#define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10

Expand Down Expand Up @@ -218,7 +212,7 @@ static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_sy

host_driver_t *vusb_driver(void) { return &driver; }

static uint8_t keyboard_leds(void) { return vusb_keyboard_leds; }
static uint8_t keyboard_leds(void) { return keyboard_led_state; }

static void send_keyboard(report_keyboard_t *report) {
uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
Expand All @@ -232,6 +226,7 @@ static void send_keyboard(report_keyboard_t *report) {
// NOTE: send key strokes of Macro
usbPoll();
vusb_transfer_keyboard();
keyboard_report_sent = *report;
}

typedef struct {
Expand Down Expand Up @@ -289,9 +284,10 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
if (rq->bRequest == USBRQ_HID_GET_REPORT) {
debug("GET_REPORT:");
/* we only have one report type, so don't look at wValue */
usbMsgPtr = (usbMsgPtr_t)&keyboard_report;
return sizeof(keyboard_report);
if (rq->wIndex.word == KEYBOARD_INTERFACE) {
usbMsgPtr = (usbMsgPtr_t)&keyboard_report_sent;
return sizeof(keyboard_report_sent);
}
} else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
debug("GET_IDLE: ");
// debug_hex(vusb_idle_rate);
Expand All @@ -304,7 +300,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
} else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
debug("SET_REPORT: ");
// Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
if (rq->wValue.word == 0x0200 && rq->wIndex.word == KEYBOARD_INTERFACE) {
debug("SET_LED: ");
last_req.kind = SET_LED;
last_req.len = rq->wLength.word;
Expand All @@ -330,7 +326,7 @@ uchar usbFunctionWrite(uchar *data, uchar len) {
debug("SET_LED: ");
debug_hex(data[0]);
debug("\n");
vusb_keyboard_leds = data[0];
keyboard_led_state = data[0];
last_req.len = 0;
return 1;
break;
Expand Down

0 comments on commit ac46e0a

Please sign in to comment.