qmk_firmware/tmk_core/protocol/host.c

271 lines
6.6 KiB
C
Raw Permalink Normal View History

/*
Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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 <stdint.h>
#include "keyboard.h"
#include "keycode.h"
#include "host.h"
#include "util.h"
#include "debug.h"
#ifdef DIGITIZER_ENABLE
# include "digitizer.h"
#endif
#ifdef JOYSTICK_ENABLE
# include "joystick.h"
#endif
#ifdef BLUETOOTH_ENABLE
2022-09-29 10:38:09 -07:00
# include "bluetooth.h"
# include "outputselect.h"
#endif
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
2018-11-15 22:22:05 -08:00
#ifdef NKRO_ENABLE
2019-08-30 11:19:03 -07:00
# include "keycode_config.h"
extern keymap_config_t keymap_config;
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
2018-11-15 22:22:05 -08:00
#endif
static host_driver_t *driver;
2022-10-06 19:35:01 -07:00
static uint16_t last_system_usage = 0;
static uint16_t last_consumer_usage = 0;
void host_set_driver(host_driver_t *d) {
driver = d;
}
host_driver_t *host_get_driver(void) {
return driver;
}
#ifdef SPLIT_KEYBOARD
uint8_t split_led_state = 0;
void set_split_host_keyboard_leds(uint8_t led_state) {
split_led_state = led_state;
}
#endif
2019-08-30 11:19:03 -07:00
uint8_t host_keyboard_leds(void) {
#ifdef SPLIT_KEYBOARD
if (!is_keyboard_master()) return split_led_state;
#endif
if (!driver) return 0;
return (*driver->keyboard_leds)();
}
led_t host_keyboard_led_state(void) {
return (led_t)host_keyboard_leds();
}
/* send report */
2019-08-30 11:19:03 -07:00
void host_keyboard_send(report_keyboard_t *report) {
#ifdef BLUETOOTH_ENABLE
if (where_to_send() == OUTPUT_BLUETOOTH) {
2022-09-29 10:38:09 -07:00
bluetooth_send_keyboard(report);
return;
}
#endif
if (!driver) return;
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
2018-11-15 22:22:05 -08:00
#if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP)
if (keyboard_protocol && keymap_config.nkro) {
/* The callers of this function assume that report->mods is where mods go in.
* But report->nkro.mods can be at a different offset if core keyboard does not have a report ID.
*/
2019-08-30 11:19:03 -07:00
report->nkro.mods = report->mods;
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
2018-11-15 22:22:05 -08:00
report->nkro.report_id = REPORT_ID_NKRO;
} else
#endif
{
#ifdef KEYBOARD_SHARED_EP
report->report_id = REPORT_ID_KEYBOARD;
#endif
}
(*driver->send_keyboard)(report);
if (debug_keyboard) {
dprint("keyboard_report: ");
for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
dprintf("%02X ", report->raw[i]);
}
dprint("\n");
}
}
2019-08-30 11:19:03 -07:00
void host_mouse_send(report_mouse_t *report) {
#ifdef BLUETOOTH_ENABLE
if (where_to_send() == OUTPUT_BLUETOOTH) {
2022-09-29 10:38:09 -07:00
bluetooth_send_mouse(report);
return;
}
#endif
if (!driver) return;
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
2018-11-15 22:22:05 -08:00
#ifdef MOUSE_SHARED_EP
report->report_id = REPORT_ID_MOUSE;
#endif
#ifdef MOUSE_EXTENDED_REPORT
// clip and copy to Boot protocol XY
report->boot_x = (report->x > 127) ? 127 : ((report->x < -127) ? -127 : report->x);
report->boot_y = (report->y > 127) ? 127 : ((report->y < -127) ? -127 : report->y);
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
2018-11-15 22:22:05 -08:00
#endif
(*driver->send_mouse)(report);
}
2022-10-06 19:35:01 -07:00
void host_system_send(uint16_t usage) {
if (usage == last_system_usage) return;
last_system_usage = usage;
if (!driver) return;
2022-10-06 19:35:01 -07:00
report_extra_t report = {
.report_id = REPORT_ID_SYSTEM,
.usage = usage,
};
(*driver->send_extra)(&report);
}
2022-10-06 19:35:01 -07:00
void host_consumer_send(uint16_t usage) {
if (usage == last_consumer_usage) return;
last_consumer_usage = usage;
#ifdef BLUETOOTH_ENABLE
if (where_to_send() == OUTPUT_BLUETOOTH) {
2022-10-06 19:35:01 -07:00
bluetooth_send_consumer(usage);
return;
}
#endif
if (!driver) return;
2022-10-06 19:35:01 -07:00
report_extra_t report = {
.report_id = REPORT_ID_CONSUMER,
.usage = usage,
};
(*driver->send_extra)(&report);
}
void host_radio_send(bool state) {
if (!driver) return;
/* It's a toggle button, state==true means to toggle it.
* !state means there's no change. */
if (!state) return;
report_radio_t report = {
.report_id = REPORT_ID_RADIO,
.state = (uint8_t)state,
};
send_radio(&report);
}
__attribute__((weak)) void send_radio(report_radio_t *report) {}
#ifdef JOYSTICK_ENABLE
void host_joystick_send(joystick_t *joystick) {
if (!driver) return;
report_joystick_t report = {
2022-11-26 08:14:45 -08:00
# ifdef JOYSTICK_SHARED_EP
.report_id = REPORT_ID_JOYSTICK,
# endif
# if JOYSTICK_AXIS_COUNT > 0
.axes =
{
joystick->axes[0],
2022-11-26 08:14:45 -08:00
# if JOYSTICK_AXIS_COUNT >= 2
joystick->axes[1],
# endif
2022-11-26 08:14:45 -08:00
# if JOYSTICK_AXIS_COUNT >= 3
joystick->axes[2],
# endif
2022-11-26 08:14:45 -08:00
# if JOYSTICK_AXIS_COUNT >= 4
joystick->axes[3],
# endif
2022-11-26 08:14:45 -08:00
# if JOYSTICK_AXIS_COUNT >= 5
joystick->axes[4],
# endif
2022-11-26 08:14:45 -08:00
# if JOYSTICK_AXIS_COUNT >= 6
joystick->axes[5],
# endif
},
# endif
# if JOYSTICK_BUTTON_COUNT > 0
.buttons =
{
joystick->buttons[0],
# if JOYSTICK_BUTTON_COUNT > 8
joystick->buttons[1],
# endif
# if JOYSTICK_BUTTON_COUNT > 16
joystick->buttons[2],
# endif
# if JOYSTICK_BUTTON_COUNT > 24
joystick->buttons[3],
# endif
},
# endif
};
send_joystick(&report);
}
#endif
__attribute__((weak)) void send_joystick(report_joystick_t *report) {}
#ifdef DIGITIZER_ENABLE
void host_digitizer_send(digitizer_t *digitizer) {
report_digitizer_t report = {
# ifdef DIGITIZER_SHARED_EP
.report_id = REPORT_ID_DIGITIZER,
# endif
.in_range = digitizer->in_range,
.tip = digitizer->tip,
.barrel = digitizer->barrel,
.x = (uint16_t)(digitizer->x * 0x7FFF),
.y = (uint16_t)(digitizer->y * 0x7FFF),
};
send_digitizer(&report);
}
#endif
__attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
#ifdef PROGRAMMABLE_BUTTON_ENABLE
void host_programmable_button_send(uint32_t data) {
report_programmable_button_t report = {
.report_id = REPORT_ID_PROGRAMMABLE_BUTTON,
.usage = data,
};
send_programmable_button(&report);
}
#endif
__attribute__((weak)) void send_programmable_button(report_programmable_button_t *report) {}
2022-10-06 19:35:01 -07:00
uint16_t host_last_system_usage(void) {
return last_system_usage;
}
2022-10-06 19:35:01 -07:00
uint16_t host_last_consumer_usage(void) {
return last_consumer_usage;
}