Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

WIP remove GD32 recvControl workaround #92

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions src/BootKeyboard/BootKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,37 +179,12 @@ bool BootKeyboard_::setup(USBSetup& setup) {
// Check if data has the correct length afterwards
int length = setup.wLength;


// ------------------------------------------------------------
// Workaround for a bug in the GD32 core:
//
// On GD32, when we call `USV_RecvControl`, it casts the (void*) pointer
// we give it to `uint16_t*`, which means that it will alway write an even
// number of bytes to that pointer. Because we don't want to overwrite
// the next byte in memory past `leds`, we use a temporary array that is
// guaranteed to be big enough, and copy the data from that:
if (setup.wValueH == HID_REPORT_TYPE_OUTPUT) {
if (length == sizeof(leds)) {
uint8_t raw_report_data[2];
USB_RecvControl(&raw_report_data, length);
leds = raw_report_data[0];
USB_RecvControl(&leds, length);
return true;
// } else {
// char tmp[8];
// USB_RecvControl(&tmp, length);
// return true;
}
}
}
// Once the GD32 core bug is fixed, we can replace the above code with the
// original code below:
// ------------------------------------------------------------
// if (setup.wValueH == HID_REPORT_TYPE_OUTPUT) {
// if (length == sizeof(leds)) {
// USB_RecvControl(&leds, length);
// return true;
// }
// }
// ------------------------------------------------------------

// Input (set HID report)
else if (setup.wValueH == HID_REPORT_TYPE_INPUT) {
Expand Down Expand Up @@ -415,4 +390,7 @@ void BootKeyboard_::checkReset() {
}

__attribute__((weak))
BootKeyboard_ BootKeyboard;
BootKeyboard_& BootKeyboard() {
static BootKeyboard_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/BootKeyboard/BootKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ class BootKeyboard_ : public PluggableUSBModule {

uint8_t leds;
};
extern BootKeyboard_ BootKeyboard;
extern BootKeyboard_& BootKeyboard();
29 changes: 2 additions & 27 deletions src/HID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,37 +151,12 @@ bool HID_::setup(USBSetup& setup) {
if (request == HID_SET_REPORT) {
uint16_t length = setup.wLength;

// ------------------------------------------------------------
// Workaround for a bug in the GD32 core:
//
// On GD32, when we call `USV_RecvControl`, it casts the (void*) pointer
// we give it to `uint16_t*`, which means that it will alway write an even
// number of bytes to that pointer. Because we might be trying to just
// read the `leds` byte, and we don't want to overwrite the next byte in
// memory, instead of giving it the pointer to the `setReportData` member
// variable directly, we have it write into to temporary `raw_report_data`
// array that's guaranteed to be big enough, then copy the data from that
// array into `setReportData`:
uint8_t raw_report_data[sizeof(setReportData) + 1];
if (length == sizeof(setReportData)) {
USB_RecvControl(&raw_report_data, length);
setReportData.reportId = raw_report_data[0];
setReportData.leds = raw_report_data[1];
USB_RecvControl(&setReportData, length);
} else if (length == sizeof(setReportData.leds)) {
USB_RecvControl(&raw_report_data, length);
USB_RecvControl(&setReportData.leds, length);
setReportData.reportId = 0;
setReportData.leds = raw_report_data[0];
}
// Once the GD32 core bug is fixed, we can replace the above code with the
// original code below:
// ------------------------------------------------------------
// if (length == sizeof(setReportData)) {
// USB_RecvControl(&setReportData, length);
// } else if (length == sizeof(setReportData.leds)) {
// USB_RecvControl(&setReportData.leds, length);
// setReportData.reportId = 0;
// }
// ------------------------------------------------------------
return true;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/MultiReport/AbsoluteMouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ void AbsoluteMouse_::sendReport(void* data, int length) {
HID().SendReport(HID_REPORTID_MOUSE_ABSOLUTE, data, length);
}

AbsoluteMouse_ AbsoluteMouse;
AbsoluteMouse_& AbsoluteMouse() {
static AbsoluteMouse_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/MultiReport/AbsoluteMouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ class AbsoluteMouse_ : public AbsoluteMouseAPI {
virtual void sendReport(void* data, int length);
};

extern AbsoluteMouse_ AbsoluteMouse;
extern AbsoluteMouse_& AbsoluteMouse();
5 changes: 4 additions & 1 deletion src/MultiReport/ConsumerControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,7 @@ void ConsumerControl_::sendReport() {
memcpy(&last_report_, &report_, sizeof(report_));
}

ConsumerControl_ ConsumerControl;
ConsumerControl_& ConsumerControl() {
static ConsumerControl_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/MultiReport/ConsumerControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ class ConsumerControl_ {
private:
void sendReportUnchecked();
};
extern ConsumerControl_ ConsumerControl;
extern ConsumerControl_& ConsumerControl();
5 changes: 4 additions & 1 deletion src/MultiReport/Gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@ void Gamepad_::sendReport(void* data, int length) {
HID().SendReport(HID_REPORTID_GAMEPAD, data, length);
}

Gamepad_ Gamepad;
Gamepad_& Gamepad() {
static Gamepad_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/MultiReport/Gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ class Gamepad_ {
protected:
HID_GamepadReport_Data_t report_;
};
extern Gamepad_ Gamepad;
extern Gamepad_& Gamepad();
5 changes: 4 additions & 1 deletion src/MultiReport/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,7 @@ void Keyboard_::releaseAll() {
memset(&report_.allkeys, 0x00, sizeof(report_.allkeys));
}

Keyboard_ Keyboard;
Keyboard_& Keyboard() {
static Keyboard_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/MultiReport/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ class Keyboard_ {

int sendReportUnchecked();
};
extern Keyboard_ Keyboard;
extern Keyboard_& Keyboard();
5 changes: 4 additions & 1 deletion src/MultiReport/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,7 @@ void Mouse_::sendReport() {
}
}

Mouse_ Mouse;
Mouse_& Mouse() {
static Mouse_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/MultiReport/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ class Mouse_ {
private:
void sendReportUnchecked();
};
extern Mouse_ Mouse;
extern Mouse_& Mouse();
5 changes: 4 additions & 1 deletion src/MultiReport/SystemControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ void SystemControl_::sendReport(void* data, int length) {
HID().SendReport(HID_REPORTID_SYSTEMCONTROL, data, length);
}

SystemControl_ SystemControl;
SystemControl_& SystemControl() {
static SystemControl_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/MultiReport/SystemControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ class SystemControl_ {



extern SystemControl_ SystemControl;
extern SystemControl_& SystemControl();
5 changes: 4 additions & 1 deletion src/SingleReport/SingleAbsoluteMouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,7 @@ void SingleAbsoluteMouse_::sendReport(void* data, int length) {
HIDReportObserver::observeReport(HID_REPORTID_MOUSE_ABSOLUTE, data, length, result);
}

SingleAbsoluteMouse_ SingleAbsoluteMouse;
SingleAbsoluteMouse_& SingleAbsoluteMouse() {
static SingleAbsoluteMouse_ obj;
return obj;
};
2 changes: 1 addition & 1 deletion src/SingleReport/SingleAbsoluteMouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ class SingleAbsoluteMouse_ : public PluggableUSBModule, public AbsoluteMouseAPI

virtual inline void sendReport(void* data, int length) override;
};
extern SingleAbsoluteMouse_ SingleAbsoluteMouse;
extern SingleAbsoluteMouse_& SingleAbsoluteMouse();