diff --git a/device/husky/seat/README.md b/device/husky/seat/README.md new file mode 100644 index 000000000..25e68b1de --- /dev/null +++ b/device/husky/seat/README.md @@ -0,0 +1,6 @@ + + +insmod /tmp/omamobile-seat-uinput/uinput.ko +evtest /dev/input/event0 + +`build-uinput.sh` prints `/tmp/omamobile-seat-uinput/uinput.ko`. The bridge binary is `bridge`, from `device/husky/seat/bridge.c`. One byte on stdin writes one `EV_KEY`. diff --git a/device/husky/seat/bridge.c b/device/husky/seat/bridge.c new file mode 100644 index 000000000..8d1a61056 --- /dev/null +++ b/device/husky/seat/bridge.c @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: MIT +/* + * One keyboard on /dev/uinput. One byte on stdin writes one EV_KEY, + * then SYN_REPORT, because evdev holds the key until the sync. + * The process stays up until stdin closes, so the event node stays. + * SEAT_DRY_RUN prints that event and does not open a device. + */ +#include +#include +#include + +#ifdef SEAT_DRY_RUN + +int main(void) +{ + printf("struct input_event { type = EV_KEY, code = KEY_A, value = 1 }\n"); + return 0; +} + +#else + +#include +#include +#include + +/* Linux keycodes follow the keyboard, not the alphabet. KEY_A is 30. */ +static const unsigned short letter_code[26] = { + KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, + KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, + KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, + KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z +}; + +static const unsigned short digit_code[10] = { + KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, + KEY_5, KEY_6, KEY_7, KEY_8, KEY_9 +}; + +static int code_for_byte(unsigned char b, unsigned short *code) +{ + if (b >= 'a' && b <= 'z') { + *code = letter_code[b - 'a']; + return 0; + } + if (b >= 'A' && b <= 'Z') { + *code = letter_code[b - 'A']; + return 0; + } + if (b >= '0' && b <= '9') { + *code = digit_code[b - '0']; + return 0; + } + return -1; +} + +static int enable(int fd, unsigned long req, unsigned int bit) +{ + if (ioctl(fd, req, bit) < 0) { + perror("ioctl"); + return -1; + } + return 0; +} + +static int write_event(int fd, unsigned short type, unsigned short code, int value) +{ + struct input_event ev; + + memset(&ev, 0, sizeof(ev)); + ev.type = type; + ev.code = code; + ev.value = value; + if (write(fd, &ev, sizeof(ev)) != (ssize_t)sizeof(ev)) { + perror("write"); + return -1; + } + return 0; +} + +int main(void) +{ + int fd; + int i; + unsigned char byte; + unsigned short code; + struct uinput_setup setup; + ssize_t n; + + fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); + if (fd < 0) { + perror("/dev/uinput"); + return 1; + } + + if (enable(fd, UI_SET_EVBIT, EV_KEY) < 0) + return 1; + for (i = 0; i < 26; i++) { + if (enable(fd, UI_SET_KEYBIT, letter_code[i]) < 0) + return 1; + } + for (i = 0; i < 10; i++) { + if (enable(fd, UI_SET_KEYBIT, digit_code[i]) < 0) + return 1; + } + + memset(&setup, 0, sizeof(setup)); + setup.id.bustype = BUS_VIRTUAL; + snprintf(setup.name, sizeof(setup.name), "husky-seat"); + if (ioctl(fd, UI_DEV_SETUP, &setup) < 0) { + perror("UI_DEV_SETUP"); + return 1; + } + if (ioctl(fd, UI_DEV_CREATE) < 0) { + perror("UI_DEV_CREATE"); + return 1; + } + + n = read(STDIN_FILENO, &byte, 1); + if (n != 1) { + fprintf(stderr, "bridge: expected one byte on stdin\n"); + return 1; + } + if (code_for_byte(byte, &code) < 0) { + fprintf(stderr, "bridge: byte is not a letter or a digit\n"); + return 1; + } + if (write_event(fd, EV_KEY, code, 1) < 0) + return 1; + if (write_event(fd, EV_SYN, SYN_REPORT, 0) < 0) + return 1; + + for (;;) { + n = read(STDIN_FILENO, &byte, 1); + if (n <= 0) + break; + } + return 0; +} + +#endif diff --git a/device/husky/seat/build-uinput.sh b/device/husky/seat/build-uinput.sh new file mode 100755 index 000000000..8457425a3 --- /dev/null +++ b/device/husky/seat/build-uinput.sh @@ -0,0 +1,76 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# Build uinput.ko from drivers/input/misc/uinput.c. Do not commit, checkout, +# or edit the linux tree, and do not build Image. Prints the module path. +# +# Vermagic is UTS_RELEASE from include/generated/utsrelease.h. The phone +# uname has no -dirty suffix, so the build writes the phone string into +# that header and into include/config/kernel.release, then restores both. +set -euo pipefail + +KERNEL_SRC=${KERNEL_SRC:-/home/cryptokid/android/mainline/linux} +KERNEL_OUT=${KERNEL_OUT:-/home/cryptokid/android/mainline/out-zuma-ufs2} +PHONE_RELEASE=7.3.0-rc6-omamobile-push1-ge446d69d2ab2 +STAGING=/tmp/omamobile-seat-uinput + +rel_file=$KERNEL_OUT/include/config/kernel.release +uts_file=$KERNEL_OUT/include/generated/utsrelease.h +src_c=$KERNEL_SRC/drivers/input/misc/uinput.c +src_h=$KERNEL_SRC/drivers/input/input-compat.h + +[[ -f $src_c && -f $src_h ]] || { + echo "build-uinput.sh: missing uinput sources under $KERNEL_SRC" >&2 + exit 1 +} +[[ -f $rel_file && -f $uts_file ]] || { + echo "build-uinput.sh: missing release files under $KERNEL_OUT" >&2 + exit 1 +} +[[ $STAGING == /tmp/omamobile-seat-uinput && ! -L $STAGING ]] || { + echo "build-uinput.sh: refusing to stage at $STAGING" >&2 + exit 1 +} + +current=$(<"$rel_file") +case $current in +"$PHONE_RELEASE"|"$PHONE_RELEASE-dirty") ;; +*) + echo "build-uinput.sh: kernel.release is '$current', want $PHONE_RELEASE or $PHONE_RELEASE-dirty" >&2 + exit 1 + ;; +esac + +saved_rel=$(mktemp) +saved_uts=$(mktemp) +cp -a "$rel_file" "$saved_rel" +cp -a "$uts_file" "$saved_uts" + +restore_release() { + local st=$? + cp -a "$saved_rel" "$rel_file" || echo "build-uinput.sh: failed to restore kernel.release" >&2 + cp -a "$saved_uts" "$uts_file" || echo "build-uinput.sh: failed to restore utsrelease.h" >&2 + rm -f "$saved_rel" "$saved_uts" + exit "$st" +} +trap restore_release EXIT + +printf '%s\n' "$PHONE_RELEASE" >"$rel_file" +printf '#define UTS_RELEASE "%s"\n' "$PHONE_RELEASE" >"$uts_file" + +rm -rf "$STAGING" +mkdir -p "$STAGING" +cp "$src_c" "$STAGING/uinput.c" +cp "$src_h" "$STAGING/input-compat.h" +sed -i 's|#include "../input-compat.h"|#include "input-compat.h"|' "$STAGING/uinput.c" +if grep -q '#include "../input-compat.h"' "$STAGING/uinput.c"; then + echo "build-uinput.sh: staged uinput.c still includes ../input-compat.h" >&2 + exit 1 +fi +printf 'obj-m += uinput.o\n' >"$STAGING/Makefile" + +make -C "$KERNEL_SRC" O="$KERNEL_OUT" ARCH=arm64 LLVM=1 M="$STAGING" modules +[[ -f $STAGING/uinput.ko ]] || { + echo "build-uinput.sh: make did not write $STAGING/uinput.ko" >&2 + exit 1 +} +echo "$STAGING/uinput.ko"