Commit a1bbd840 authored by Jiri Kosina's avatar Jiri Kosina
Browse files

Merge branch 'for-7.1/hid-bpf' into for-linus

- sync of HID-BPF with udev-hid-bpf (Benjamin Tissoires, Muhammed Rishal)
parents b3793af3 30fb45cc
Loading
Loading
Loading
Loading
+90 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2025 Benjamin Tissoires
 */

#include "vmlinux.h"
#include "hid_bpf.h"
#include "hid_bpf_helpers.h"
#include "hid_report_helpers.h"
#include "hid_usages.h"
#include <bpf/bpf_tracing.h>

HID_BPF_CONFIG(
	HID_DEVICE(BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8, HID_VID_ANY, HID_PID_ANY),
);

EXPORT_UDEV_PROP(HID_DIGITIZER_PAD_TYPE, 32);

__u8 hw_req_buf[1024];

/* to be filled by udev-hid-bpf */
struct hid_rdesc_descriptor HID_REPORT_DESCRIPTOR;

SEC("syscall")
int probe(struct hid_bpf_probe_args *ctx)
{
	struct hid_rdesc_report *pad_type_feature = NULL;
	struct hid_rdesc_field *pad_type = NULL;
	struct hid_rdesc_report *feature;
	struct hid_bpf_ctx *hid_ctx;
	char *pad_type_str = "";
	int ret;

	hid_bpf_for_each_feature_report(&HID_REPORT_DESCRIPTOR, feature) {
		struct hid_rdesc_field *field;

		hid_bpf_for_each_field(feature, field) {
			if (field->usage_page == HidUsagePage_Digitizers &&
			    field->usage_id == HidUsage_Dig_PadType) {
				pad_type = field;
				pad_type_feature = feature;
				break;
			}
		}
		if (pad_type)
			break;
	}

	if (!pad_type || !pad_type_feature) {
		ctx->retval = -EINVAL;
		return 0;
	}

	hid_ctx = hid_bpf_allocate_context(ctx->hid);

	if (!hid_ctx)
		return -1; /* EPERM check */

	hw_req_buf[0] = pad_type_feature->report_id;

	ret = hid_bpf_hw_request(hid_ctx, hw_req_buf, sizeof(hw_req_buf),
					HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
	hid_bpf_release_context(hid_ctx);

	if (ret < 0) {
		ctx->retval = ret;
		return 0;
	}

	ctx->retval = 0;

	switch (EXTRACT_BITS(hw_req_buf, pad_type)) {
	case 0:
		pad_type_str = "Clickpad";
		break;
	case 1:
		pad_type_str = "Pressurepad";
		break;
	case 2:
		pad_type_str = "Discrete";
		break;
	default:
		pad_type_str = "Unknown";
	}

	UDEV_PROP_SPRINTF(HID_DIGITIZER_PAD_TYPE, "%s", pad_type_str);

	return 0;
}

char _license[] SEC("license") = "GPL";
+492 −0

File added.

Preview size limit exceeded, changes collapsed.

+2 −1
Original line number Diff line number Diff line
@@ -462,7 +462,8 @@ int BPF_PROG(k20_fix_events, struct hid_bpf_ctx *hctx)
			__u32 buttons;
			__u8 wheel;
		} __attribute__((packed)) *pad_report;
		int i, b;
		int i;
		size_t b;
		__u8 modifiers = data[1];
		__u32 buttons = 0;

+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ int BPF_PROG(hid_fix_rdesc, struct hid_bpf_ctx *hctx)
	if (data[3] != 0x06)
		return 0;

	for (int idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
	for (size_t idx = 0; idx < ARRAY_SIZE(offsets); idx++) {
		u8 offset = offsets[idx];

		/* if Input (Cnst,Var,Abs) , make it Input (Data,Var,Abs) */
+49 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
/* Fix for Trust Philips SPK6327 (145f:024b)
 * Modifier keys report as Array (0x00) instead of Variable (0x02)
 * causing LCtrl, LAlt, Super etc. to all act as LShift
 */
#include "vmlinux.h"
#include "hid_bpf.h"
#include "hid_bpf_helpers.h"
#include <bpf/bpf_tracing.h>

#define VID_TRUST 0x145F
#define PID_SPK6327 0x024B

HID_BPF_CONFIG(
	HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_TRUST, PID_SPK6327)
);

SEC(HID_BPF_RDESC_FIXUP)
int BPF_PROG(hid_fix_rdesc, struct hid_bpf_ctx *hctx)
{
	__u8 *data = hid_bpf_get_data(hctx, 0, 4096);

	if (!data)
		return 0;

	/* Fix modifier keys: Input Array (0x00) -> Input Variable (0x02) */
	if (data[101] == 0x00)
		data[101] = 0x02;

	return 0;
}

HID_BPF_OPS(trust_spk6327) = {
	.hid_rdesc_fixup = (void *)hid_fix_rdesc,
};

SEC("syscall")
int probe(struct hid_bpf_probe_args *ctx)
{
	/* Only apply to interface 1 (169 bytes) not interface 0 (62 bytes) */
	if (ctx->rdesc_size == 169)
		ctx->retval = 0;
	else
		ctx->retval = -EINVAL;

	return 0;
}

char _license[] SEC("license") = "GPL";
Loading