diff options
-rw-r--r-- | kvmd/apps/otg/__init__.py | 56 | ||||
-rw-r--r-- | kvmd/apps/otg/hid/__init__.py | 32 | ||||
-rw-r--r-- | kvmd/apps/otg/hid/keyboard.py | 81 | ||||
-rw-r--r-- | kvmd/apps/otg/hid/mouse.py | 77 | ||||
-rwxr-xr-x | setup.py | 1 |
5 files changed, 206 insertions, 41 deletions
diff --git a/kvmd/apps/otg/__init__.py b/kvmd/apps/otg/__init__.py index ed7666b4..825069cc 100644 --- a/kvmd/apps/otg/__init__.py +++ b/kvmd/apps/otg/__init__.py @@ -20,13 +20,12 @@ # ========================================================================== # +import os import re import time import argparse -import os - -from os.path import join +from os.path import join # pylint: disable=ungrouped-imports from typing import List from typing import Optional @@ -39,6 +38,10 @@ from ...validators import ValidatorError from .. import init +from .hid import Hid +from .hid.keyboard import KEYBOARD_HID +from .hid.mouse import MOUSE_HID + # ===== def _mkdir(path: str) -> None: @@ -101,43 +104,14 @@ def _create_acm(gadget_path: str, config_path: str) -> None: _symlink(func_path, join(config_path, "acm.usb0")) -def _create_keyboard(gadget_path: str, config_path: str) -> None: - func_path = join(gadget_path, "functions/hid.usb0") # Keyboard +def _create_hid(gadget_path: str, config_path: str, hid: Hid, instance: int) -> None: + func_path = join(gadget_path, f"functions/hid.usb{instance}") _mkdir(func_path) - _write(join(func_path, "protocol"), "1") - _write(join(func_path, "subclass"), "1") - _write(join(func_path, "report_length"), "8") - _write_bytes( - join(func_path, "report_desc"), - b"\x05\x01\x09\x06\xa1\x01\x05\x07\x19\xe0\x29\xe7\x15\x00" - b"\x25\x01\x75\x01\x95\x08\x81\x02\x95\x01\x75\x08\x81\x03" - b"\x95\x05\x75\x01\x05\x08\x19\x01\x29\x05\x91\x02\x95\x01" - b"\x75\x03\x91\x03\x95\x06\x75\x08\x15\x00\x25\x65\x05\x07" - b"\x19\x00\x29\x65\x81\x00\xc0" - ) - _symlink(func_path, join(config_path, "hid.usb0")) - - -def _create_mouse(gadget_path: str, config_path: str) -> None: - # https://github.com/NicoHood/HID/blob/0835e6a/src/SingleReport/SingleAbsoluteMouse.cpp - # Репорт взят отсюда ^^^, но изменен диапазон значений координат перемещений. - # Автор предлагает использовать -32768...32767, но семерка почему-то не хочет работать - # с отрицательными значениями координат, как не хочет хавать 65536 и 32768. - # Так что мы ей скармливаем диапазон 0...32767, и передаем рукожопам из микрософта привет, - # потому что линуксы прекрасно работают с любыми двухбайтовыми диапазонами. - func_path = join(gadget_path, "functions/hid.usb1") # Mouse - _mkdir(func_path) - _write(join(func_path, "protocol"), "0") - _write(join(func_path, "subclass"), "0") - _write(join(func_path, "report_length"), "6") - _write_bytes( - join(func_path, "report_desc"), - b"\x05\x01\x09\x02\xA1\x01\x05\x09\x19\x01\x29\x08\x15\x00" - b"\x25\x01\x95\x08\x75\x01\x81\x02\x05\x01\x09\x30\x09\x31" - b"\x16\x00\x00\x26\xFF\x7F\x75\x10\x95\x02\x81\x02\x09\x38" - b"\x15\x81\x25\x7f\x75\x08\x95\x01\x81\x06\xc0" - ) - _symlink(func_path, join(config_path, "hid.usb1")) + _write(join(func_path, "protocol"), str(hid.protocol)) + _write(join(func_path, "subclass"), str(hid.subclass)) + _write(join(func_path, "report_length"), str(hid.report_length)) + _write_bytes(join(func_path, "report_desc"), hid.report_descriptor) + _symlink(func_path, join(config_path, f"hid.usb{instance}")) def _create_msd(gadget_path: str, config_path: str) -> None: @@ -188,8 +162,8 @@ def _cmd_start(config: Section) -> None: if config.kvmd.hid.type == "otg": logger.info("Required HID") - _create_keyboard(gadget_path, config_path) - _create_mouse(gadget_path, config_path) + _create_hid(gadget_path, config_path, KEYBOARD_HID, 0) + _create_hid(gadget_path, config_path, MOUSE_HID, 1) if config.kvmd.msd.type == "otg": logger.info("Required MSD") diff --git a/kvmd/apps/otg/hid/__init__.py b/kvmd/apps/otg/hid/__init__.py new file mode 100644 index 00000000..e5f34641 --- /dev/null +++ b/kvmd/apps/otg/hid/__init__.py @@ -0,0 +1,32 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# 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 3 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 <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +import dataclasses + + +# ===== [email protected](frozen=True) +class Hid: + protocol: int + subclass: int + report_length: int + report_descriptor: bytes diff --git a/kvmd/apps/otg/hid/keyboard.py b/kvmd/apps/otg/hid/keyboard.py new file mode 100644 index 00000000..274406fd --- /dev/null +++ b/kvmd/apps/otg/hid/keyboard.py @@ -0,0 +1,81 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# 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 3 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 <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from . import Hid + + +# ===== +KEYBOARD_HID = Hid( + protocol=1, + subclass=1, + + report_length=8, + + report_descriptor=bytes([ + # https://www.kernel.org/doc/Documentation/usb/gadget_hid.txt + + # Keyboard + 0x05, 0x01, # USAGE_PAGE (Generic Desktop) + 0x09, 0x06, # USAGE (Keyboard) + 0xA1, 0x01, # COLLECTION (Application) + + # Modifiers + 0x05, 0x07, # USAGE_PAGE (Keyboard) + 0x19, 0xE0, # USAGE_MINIMUM (Keyboard LeftControl) + 0x29, 0xE7, # USAGE_MAXIMUM (Keyboard Right GUI) + 0x15, 0x00, # LOGICAL_MINIMUM (0) + 0x25, 0x01, # LOGICAL_MAXIMUM (1) + 0x75, 0x01, # REPORT_SIZE (1) + 0x95, 0x08, # REPORT_COUNT (8) + 0x81, 0x02, # INPUT (Data,Var,Abs) + + # Reserved byte + 0x95, 0x01, # REPORT_COUNT (1) + 0x75, 0x08, # REPORT_SIZE (8) + 0x81, 0x03, # INPUT (Cnst,Var,Abs) + + # LEDs output + 0x95, 0x05, # REPORT_COUNT (5) + 0x75, 0x01, # REPORT_SIZE (1) + 0x05, 0x08, # USAGE_PAGE (LEDs) + 0x19, 0x01, # USAGE_MINIMUM (Num Lock) + 0x29, 0x05, # USAGE_MAXIMUM (Kana) + 0x91, 0x02, # OUTPUT (Data,Var,Abs) + + # Reserved 3 bits in output + 0x95, 0x01, # REPORT_COUNT (1) + 0x75, 0x03, # REPORT_SIZE (3) + 0x91, 0x03, # OUTPUT (Cnst,Var,Abs) + + # 6 keys + 0x95, 0x06, # REPORT_COUNT (6) + 0x75, 0x08, # REPORT_SIZE (8) + 0x15, 0x00, # LOGICAL_MINIMUM (0) + 0x25, 0x65, # LOGICAL_MAXIMUM (101) + 0x05, 0x07, # USAGE_PAGE (Keyboard) + 0x19, 0x00, # USAGE_MINIMUM (Reserved) + 0x29, 0x65, # USAGE_MAXIMUM (Keyboard Application) + 0x81, 0x00, # INPUT (Data,Ary,Abs) + + 0xC0, # END_COLLECTION + ]), +) diff --git a/kvmd/apps/otg/hid/mouse.py b/kvmd/apps/otg/hid/mouse.py new file mode 100644 index 00000000..325d27ef --- /dev/null +++ b/kvmd/apps/otg/hid/mouse.py @@ -0,0 +1,77 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# 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 3 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 <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from . import Hid + + +# ===== +MOUSE_HID = Hid( + protocol=0, + subclass=0, + + report_length=6, + + report_descriptor=bytes([ + # https://github.com/NicoHood/HID/blob/0835e6a/src/SingleReport/SingleAbsoluteMouse.cpp + # Репорт взят отсюда ^^^, но изменен диапазон значений координат перемещений. + # Автор предлагает использовать -32768...32767, но семерка почему-то не хочет работать + # с отрицательными значениями координат, как не хочет хавать 65536 и 32768. + # Так что мы ей скармливаем диапазон 0...32767, и передаем рукожопам из микрософта привет, + # потому что линуксы прекрасно работают с любыми двухбайтовыми диапазонами. + + # Absolute mouse + 0x05, 0x01, # USAGE_PAGE (Generic Desktop) + 0x09, 0x02, # USAGE (Mouse) + 0xA1, 0x01, # COLLECTION (Application) + + # 8 Buttons + 0x05, 0x09, # USAGE_PAGE (Button) + 0x19, 0x01, # USAGE_MINIMUM (Button 1) + 0x29, 0x08, # USAGE_MAXIMUM (Button 8) + 0x15, 0x00, # LOGICAL_MINIMUM (0) + 0x25, 0x01, # LOGICAL_MAXIMUM (1) + 0x95, 0x08, # REPORT_COUNT (8) + 0x75, 0x01, # REPORT_SIZE (1) + 0x81, 0x02, # INPUT (Data,Var,Abs) + + # X, Y + 0x05, 0x01, # USAGE_PAGE (Generic Desktop) + 0x09, 0x30, # USAGE (X) + 0x09, 0x31, # USAGE (Y) + 0x16, 0x00, 0x00, # LOGICAL_MINIMUM (0) + 0x26, 0xFF, 0x7F, # LOGICAL_MAXIMUM (32767) + 0x75, 0x10, # REPORT_SIZE (16) + 0x95, 0x02, # REPORT_COUNT (2) + 0x81, 0x02, # INPUT (Data,Var,Abs) + + # Wheel + 0x09, 0x38, # USAGE (Wheel) + 0x15, 0x81, # LOGICAL_MINIMUM (-127) + 0x25, 0x7F, # LOGICAL_MAXIMUM (127) + 0x75, 0x08, # REPORT_SIZE (8) + 0x95, 0x01, # REPORT_COUNT (1) + 0x81, 0x06, # INPUT (Data,Var,Rel) + + # End + 0xC0, # END_COLLECTION + ]), +) @@ -88,6 +88,7 @@ def main() -> None: "kvmd.apps", "kvmd.apps.kvmd", "kvmd.apps.otg", + "kvmd.apps.otg.hid", "kvmd.apps.otgmsd", "kvmd.apps.htpasswd", "kvmd.apps.cleanup", |