Path: blob/master/drivers/hid/bpf/progs/FR-TEC__Raptor-Mach-2.bpf.c
26285 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright (c) 2024 Benjamin Tissoires2*/34#include "vmlinux.h"5#include "hid_bpf.h"6#include "hid_bpf_helpers.h"7#include <bpf/bpf_tracing.h>89#define VID_BETOP_2185PC 0x11C010#define PID_RAPTOR_MACH_2 0x56061112HID_BPF_CONFIG(13HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, VID_BETOP_2185PC, PID_RAPTOR_MACH_2),14);1516/*17* For reference, this is the fixed report descriptor18*19* static const __u8 fixed_rdesc[] = {20* 0x05, 0x01, // Usage Page (Generic Desktop) 021* 0x09, 0x04, // Usage (Joystick) 222* 0xa1, 0x01, // Collection (Application) 423* 0x05, 0x01, // Usage Page (Generic Desktop) 624* 0x85, 0x01, // Report ID (1) 825* 0x05, 0x01, // Usage Page (Generic Desktop) 1026* 0x09, 0x30, // Usage (X) 1227* 0x75, 0x10, // Report Size (16) 1428* 0x95, 0x01, // Report Count (1) 1629* 0x15, 0x00, // Logical Minimum (0) 1830* 0x26, 0xff, 0x07, // Logical Maximum (2047) 2031* 0x46, 0xff, 0x07, // Physical Maximum (2047) 2332* 0x81, 0x02, // Input (Data,Var,Abs) 2633* 0x05, 0x01, // Usage Page (Generic Desktop) 2834* 0x09, 0x31, // Usage (Y) 3035* 0x75, 0x10, // Report Size (16) 3236* 0x95, 0x01, // Report Count (1) 3437* 0x15, 0x00, // Logical Minimum (0) 3638* 0x26, 0xff, 0x07, // Logical Maximum (2047) 3839* 0x46, 0xff, 0x07, // Physical Maximum (2047) 4140* 0x81, 0x02, // Input (Data,Var,Abs) 4441* 0x05, 0x01, // Usage Page (Generic Desktop) 4642* 0x09, 0x33, // Usage (Rx) 4843* 0x75, 0x10, // Report Size (16) 5044* 0x95, 0x01, // Report Count (1) 5245* 0x15, 0x00, // Logical Minimum (0) 5446* 0x26, 0xff, 0x03, // Logical Maximum (1023) 5647* 0x46, 0xff, 0x03, // Physical Maximum (1023) 5948* 0x81, 0x02, // Input (Data,Var,Abs) 6249* 0x05, 0x00, // Usage Page (Undefined) 6450* 0x09, 0x00, // Usage (Undefined) 6651* 0x75, 0x10, // Report Size (16) 6852* 0x95, 0x01, // Report Count (1) 7053* 0x15, 0x00, // Logical Minimum (0) 7254* 0x26, 0xff, 0x03, // Logical Maximum (1023) 7455* 0x46, 0xff, 0x03, // Physical Maximum (1023) 7756* 0x81, 0x02, // Input (Data,Var,Abs) 8057* 0x05, 0x01, // Usage Page (Generic Desktop) 8258* 0x09, 0x32, // Usage (Z) 8459* 0x75, 0x10, // Report Size (16) 8660* 0x95, 0x01, // Report Count (1) 8861* 0x15, 0x00, // Logical Minimum (0) 9062* 0x26, 0xff, 0x03, // Logical Maximum (1023) 9263* 0x46, 0xff, 0x03, // Physical Maximum (1023) 9564* 0x81, 0x02, // Input (Data,Var,Abs) 9865* 0x05, 0x01, // Usage Page (Generic Desktop) 10066* 0x09, 0x35, // Usage (Rz) 10267* 0x75, 0x10, // Report Size (16) 10468* 0x95, 0x01, // Report Count (1) 10669* 0x15, 0x00, // Logical Minimum (0) 10870* 0x26, 0xff, 0x03, // Logical Maximum (1023) 11071* 0x46, 0xff, 0x03, // Physical Maximum (1023) 11372* 0x81, 0x02, // Input (Data,Var,Abs) 11673* 0x05, 0x01, // Usage Page (Generic Desktop) 11874* 0x09, 0x34, // Usage (Ry) 12075* 0x75, 0x10, // Report Size (16) 12276* 0x95, 0x01, // Report Count (1) 12477* 0x15, 0x00, // Logical Minimum (0) 12678* 0x26, 0xff, 0x07, // Logical Maximum (2047) 12879* 0x46, 0xff, 0x07, // Physical Maximum (2047) 13180* 0x81, 0x02, // Input (Data,Var,Abs) 13481* 0x05, 0x01, // Usage Page (Generic Desktop) 13682* 0x09, 0x36, // Usage (Slider) 13883* 0x75, 0x10, // Report Size (16) 14084* 0x95, 0x01, // Report Count (1) 14285* 0x15, 0x00, // Logical Minimum (0) 14486* 0x26, 0xff, 0x03, // Logical Maximum (1023) 14687* 0x46, 0xff, 0x03, // Physical Maximum (1023) 14988* 0x81, 0x02, // Input (Data,Var,Abs) 15289* 0x05, 0x09, // Usage Page (Button) 15490* 0x19, 0x01, // Usage Minimum (1) 15691* 0x2a, 0x1d, 0x00, // Usage Maximum (29) 15892* 0x15, 0x00, // Logical Minimum (0) 16193* 0x25, 0x01, // Logical Maximum (1) 16394* 0x75, 0x01, // Report Size (1) 16595* 0x96, 0x80, 0x00, // Report Count (128) 16796* 0x81, 0x02, // Input (Data,Var,Abs) 17097* 0x05, 0x01, // Usage Page (Generic Desktop) 17298* 0x09, 0x39, // Usage (Hat switch) 17499* 0x26, 0x07, 0x00, // Logical Maximum (7) 176 // changed (was 239)100* 0x46, 0x68, 0x01, // Physical Maximum (360) 179101* 0x65, 0x14, // Unit (EnglishRotation: deg) 182102* 0x75, 0x10, // Report Size (16) 184103* 0x95, 0x01, // Report Count (1) 186104* 0x81, 0x42, // Input (Data,Var,Abs,Null) 188105* 0x05, 0x01, // Usage Page (Generic Desktop) 190106* 0x09, 0x00, // Usage (Undefined) 192107* 0x75, 0x08, // Report Size (8) 194108* 0x95, 0x1d, // Report Count (29) 196109* 0x81, 0x01, // Input (Cnst,Arr,Abs) 198110* 0x15, 0x00, // Logical Minimum (0) 200111* 0x26, 0xef, 0x00, // Logical Maximum (239) 202112* 0x85, 0x58, // Report ID (88) 205113* 0x26, 0xff, 0x00, // Logical Maximum (255) 207114* 0x46, 0xff, 0x00, // Physical Maximum (255) 210115* 0x75, 0x08, // Report Size (8) 213116* 0x95, 0x3f, // Report Count (63) 215117* 0x09, 0x00, // Usage (Undefined) 217118* 0x91, 0x02, // Output (Data,Var,Abs) 219119* 0x85, 0x59, // Report ID (89) 221120* 0x75, 0x08, // Report Size (8) 223121* 0x95, 0x80, // Report Count (128) 225122* 0x09, 0x00, // Usage (Undefined) 227123* 0xb1, 0x02, // Feature (Data,Var,Abs) 229124* 0xc0, // End Collection 231125* };126*/127128/*129* We need to amend the report descriptor for the following:130* - the joystick sends its hat_switch data between 0 and 239 but131* the kernel expects the logical max to stick into a signed 8 bits132* integer. We thus divide it by 30 to match what other joysticks are133* doing134*/135SEC(HID_BPF_RDESC_FIXUP)136int BPF_PROG(hid_fix_rdesc_raptor_mach_2, struct hid_bpf_ctx *hctx)137{138__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, HID_MAX_DESCRIPTOR_SIZE /* size */);139140if (!data)141return 0; /* EPERM check */142143data[177] = 0x07;144145return 0;146}147148/*149* The hat_switch value at offsets 33 and 34 (16 bits) needs150* to be reduced to a single 8 bit signed integer. So we151* divide it by 30.152* Byte 34 is always null, so it is ignored.153*/154SEC(HID_BPF_DEVICE_EVENT)155int BPF_PROG(raptor_mach_2_fix_hat_switch, struct hid_bpf_ctx *hctx)156{157__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 64 /* size */);158159if (!data)160return 0; /* EPERM check */161162if (data[0] != 0x01) /* not the joystick report ID */163return 0;164165data[33] /= 30;166167return 0;168}169170HID_BPF_OPS(raptor_mach_2) = {171.hid_rdesc_fixup = (void *)hid_fix_rdesc_raptor_mach_2,172.hid_device_event = (void *)raptor_mach_2_fix_hat_switch,173};174175SEC("syscall")176int probe(struct hid_bpf_probe_args *ctx)177{178ctx->retval = ctx->rdesc_size != 232;179if (ctx->retval)180ctx->retval = -EINVAL;181182/* ensure the kernel isn't fixed already */183if (ctx->rdesc[177] != 0xef) /* Logical Max of 239 */184ctx->retval = -EINVAL;185186return 0;187}188189char _license[] SEC("license") = "GPL";190191192