Merge pull request #5 from Lawstorant/universal-quirks

Add envelope fix quirk and value clamping functions
This commit is contained in:
Makarenko Oleg
2024-07-14 23:08:21 +03:00
committed by GitHub
6 changed files with 121 additions and 45 deletions

2
Kbuild
View File

@@ -1,3 +1,3 @@
obj-m := hid-new-pidff.o obj-m := hid-new-pidff.o
hid-new-pidff-y := hid-newpidff.o hid-pidff.o hid-new-pidff-y := hid-universal-pidff.o hid-pidff.o
ccflags-y := -Idrivers/hid ccflags-y := -Idrivers/hid

View File

@@ -10,10 +10,7 @@ And that's basically it
## What devices are supported? ## What devices are supported?
### Bases: ### Bases:
1. MOZA R3, R5, R9, R12, R16, R21 1. MOZA R3, R5, R9, R12, R16, R21
1. ... 2. ...
### Wheel rims (others yet untested):
1. MOZA RS V2 (with Moza wheelbases)
## What works? ## What works?
1. FFB (all effects from device descriptor) 1. FFB (all effects from device descriptor)
@@ -51,13 +48,13 @@ To unload module:
`sudo rmmod hid_moza_ff` `sudo rmmod hid_moza_ff`
## How to set up a base parameters? ## How to set up a base parameters?
### MOZA ### MOZA
For now, please, use [Android App](https://play.google.com/store/apps/details?id=com.gudsen.mozapithouse) **[Boxflat](https://github.com/Lawstorant/boxflat)** is a Linux Pit House alternative made by [@Lawstorant](https://github.com/Lawstorant)
**[Android App](https://play.google.com/store/apps/details?id=com.gudsen.mozapithouse)**
## Known issues with the driver ## Known issues with the driver
1. Firmware update does not work. Please use Windows machine or Windows VM for any firmware updates - Buttons above 80 simply don't show up. This is a Linux limitation and we're trying to fix that in the upstream
## Known issues with the firmware ## Known issues with the firmware
You tell me please You tell me please

View File

@@ -1,8 +1,8 @@
PACKAGE_NAME="new-pidff" PACKAGE_NAME="universal-pidff"
PACKAGE_VERSION="0.0.2" PACKAGE_VERSION="0.0.3"
MAKE[0]="make KVERSION=$kernelver" MAKE[0]="make KVERSION=$kernelver"
CLEAN="make clean" CLEAN="make clean"
BUILT_MODULE_NAME[0]="hid-new-pidff" BUILT_MODULE_NAME[0]="hid-new-pidff"
DEST_MODULE_NAME[0]="hid-new-pidff" DEST_MODULE_NAME[0]="hid-universal-ff"
DEST_MODULE_LOCATION[0]="/kernel/drivers/hid" DEST_MODULE_LOCATION[0]="/kernel/drivers/hid"
AUTOINSTALL="yes" AUTOINSTALL="yes"

View File

@@ -25,6 +25,14 @@
#define PID_EFFECTS_MAX 64 #define PID_EFFECTS_MAX 64
/*
* This is 16384 or 90 degrees in polar coordinates (up)
* Racing games exclusively use polar coordinates but sometimes
* SDL/Proton mess with this value as they try to do
* some conversions to fix FFB for flight sticks
*/
#define PIDFF_FIXED_DIRECTION 0x4000
/* Report usage table used to put reports into an array */ /* Report usage table used to put reports into an array */
#define PID_SET_EFFECT 0 #define PID_SET_EFFECT 0
@@ -213,6 +221,36 @@ static int pidff_rescale_signed(int i, struct hid_field *field)
field->logical_minimum / -0x8000; field->logical_minimum / -0x8000;
} }
/*
* Clamp minimum value for the given field
*/
static int pidff_clamp_min(int i, struct hid_field *field)
{
int ret = i < field->logical_minimum ? field->logical_minimum : i;
pr_debug("clamped min value from %d to %d\n", i, ret);
return ret;
}
/*
* Clamp maximum value for the given field
*/
static int pidff_clamp_max(int i, struct hid_field *field)
{
int ret = i > field->logical_maximum ? field->logical_maximum : i;
pr_debug("clamped max value from %d to %d\n", i, ret);
return ret;
}
/*
* Clamp value for the given field
*/
static int pidff_clamp(int i, struct hid_field *field)
{
i = pidff_clamp_min(i, field);
i = pidff_clamp_max(i, field);
return i;
}
static void pidff_set(struct pidff_usage *usage, u16 value) static void pidff_set(struct pidff_usage *usage, u16 value)
{ {
usage->value[0] = pidff_rescale(value, 0xffff, usage->field); usage->value[0] = pidff_rescale(value, 0xffff, usage->field);
@@ -255,7 +293,11 @@ static void pidff_set_envelope_report(struct pidff_device *pidff,
pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length; pidff->set_envelope[PID_ATTACK_TIME].value[0] = envelope->attack_length;
pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length; pidff->set_envelope[PID_FADE_TIME].value[0] = envelope->fade_length;
hid_dbg(pidff->hid, "attack %u => %d\n", hid_dbg(pidff->hid, "attack level %u => %d\n",
envelope->attack_level,
pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
hid_dbg(pidff->hid, "fade level %u => %d\n",
envelope->attack_level, envelope->attack_level,
pidff->set_envelope[PID_ATTACK_LEVEL].value[0]); pidff->set_envelope[PID_ATTACK_LEVEL].value[0]);
@@ -308,12 +350,8 @@ static void pidff_set_effect_report(struct pidff_device *pidff,
/* check for device quirks */ /* check for device quirks */
unsigned short direction = effect->direction; unsigned short direction = effect->direction;
if ((effect->type == FF_DAMPER || if (pidff->quirks & PIDFF_QUIRK_FIX_WHEEL_DIRECTION)
effect->type == FF_FRICTION || direction = PIDFF_FIXED_DIRECTION;
effect->type == FF_SPRING ||
effect->type == FF_INERTIA) &&
pidff->quirks & PIDFF_QUIRK_FIX_WHEEL_DIRECTION)
direction = 0x3FFF;
pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] = pidff->set_effect[PID_EFFECT_BLOCK_INDEX].value[0] =
pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0]; pidff->block_load[PID_EFFECT_BLOCK_INDEX].value[0];
@@ -363,7 +401,7 @@ static void pidff_set_periodic_report(struct pidff_device *pidff,
pidff_set_signed(&pidff->set_periodic[PID_OFFSET], pidff_set_signed(&pidff->set_periodic[PID_OFFSET],
effect->u.periodic.offset); effect->u.periodic.offset);
pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase); pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase);
pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period; pidff->set_periodic[PID_PERIOD].value[0] = pidff_clamp(effect->u.periodic.period, pidff->set_periodic[PID_PERIOD].field);
hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC], hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC],
HID_REQ_SET_REPORT); HID_REQ_SET_REPORT);
@@ -637,6 +675,17 @@ static int pidff_upload_effect(struct input_dev *dev, struct ff_effect *effect,
pidff_set_effect_report(pidff, effect); pidff_set_effect_report(pidff, effect);
if (!old || pidff_needs_set_periodic(effect, old)) if (!old || pidff_needs_set_periodic(effect, old))
pidff_set_periodic_report(pidff, effect); pidff_set_periodic_report(pidff, effect);
if (pidff->quirks & PIDFF_QUIRK_FIX_PERIODIC_ENVELOPE)
{
effect->u.periodic.envelope.attack_level =
effect->u.periodic.envelope.attack_level == 0
? 0x7fff : effect->u.periodic.envelope.attack_level;
effect->u.periodic.envelope.fade_level =
effect->u.periodic.envelope.fade_level == 0
? 0x7fff : effect->u.periodic.envelope.fade_level;
}
if (!old || if (!old ||
pidff_needs_set_envelope(&effect->u.periodic.envelope, pidff_needs_set_envelope(&effect->u.periodic.envelope,
&old->u.periodic.envelope)) &old->u.periodic.envelope))
@@ -1264,7 +1313,7 @@ static int pidff_check_autocenter(struct pidff_device *pidff,
/* /*
* Check if the device is PID and initialize it * Check if the device is PID and initialize it
*/ */
int hid_new_pidff_init(struct hid_device *hid, const struct hid_device_id *id) int hid_pidff_init(struct hid_device *hid)
{ {
struct pidff_device *pidff; struct pidff_device *pidff;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input *hidinput = list_entry(hid->inputs.next,
@@ -1286,7 +1335,6 @@ int hid_new_pidff_init(struct hid_device *hid, const struct hid_device_id *id)
return -ENOMEM; return -ENOMEM;
pidff->hid = hid; pidff->hid = hid;
pidff->quirks |= id->driver_data;
hid_device_io_start(hid); hid_device_io_start(hid);
@@ -1364,3 +1412,29 @@ int hid_new_pidff_init(struct hid_device *hid, const struct hid_device_id *id)
kfree(pidff); kfree(pidff);
return error; return error;
} }
/*
* Check if the device is PID and initialize it
* Add quirks after initialisation
*/
int hid_pidff_init_with_quirks(struct hid_device *hid, const struct hid_device_id *id)
{
int error = hid_pidff_init(hid);
if (error)
return error;
struct hid_input *hidinput = list_entry(hid->inputs.next,
struct hid_input, list);
struct input_dev *dev = hidinput->input;
struct pidff_device *pidff = dev->ff->private;
/* set quirks on the pidff device */
hid_device_io_start(hid);
pidff->quirks |= id->driver_data;
hid_device_io_stop(hid);
hid_dbg(dev, "Device quirks: %d\n", pidff->quirks);
return 0;
}

View File

@@ -5,8 +5,12 @@
/* PIDFF Quirks to solve issues with certain devices */ /* PIDFF Quirks to solve issues with certain devices */
/* /*
* Ignore direction for spring/damping/friction/inertia effects * Always set a value > 0 for PERIODIC envelope attack and fade level
* and always set 16384 */
#define PIDFF_QUIRK_FIX_PERIODIC_ENVELOPE BIT(0)
/*
* Ignore direction and always set 16384 (0x4000)
*/ */
#define PIDFF_QUIRK_FIX_WHEEL_DIRECTION BIT(1) #define PIDFF_QUIRK_FIX_WHEEL_DIRECTION BIT(1)
@@ -16,6 +20,7 @@
#define PIDFF_QUIRK_NO_DELAY_EFFECT BIT(2) #define PIDFF_QUIRK_NO_DELAY_EFFECT BIT(2)
int hid_new_pidff_init(struct hid_device *hid, const struct hid_device_id *id); int hid_pidff_init(struct hid_device *hid);
int hid_pidff_init_with_quirks(struct hid_device *hid, const struct hid_device_id *id);
#endif #endif

View File

@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
/* /*
* Improved HID PIDFF driver * HID PIDFF wrapper
* First of all targeting steering wheels and wheelbases * First of all targeting steering wheels and wheelbases
* *
* Copyright (c) 2024 Makarenko Oleg * Copyright (c) 2024 Makarenko Oleg
@@ -14,15 +14,15 @@
static const struct hid_device_id pidff_wheel_devices[] = { static const struct hid_device_id pidff_wheel_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R3), { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R3),
.driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_0_INFINITE_LENGTH }, .driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_PERIODIC_ENVELOPE },
{ HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R5), { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R5),
.driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_0_INFINITE_LENGTH }, .driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_PERIODIC_ENVELOPE },
{ HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R9), { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R9),
.driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_0_INFINITE_LENGTH }, .driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_PERIODIC_ENVELOPE },
{ HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R12), { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R12),
.driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_0_INFINITE_LENGTH }, .driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_PERIODIC_ENVELOPE },
{ HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R16_R21), { HID_USB_DEVICE(USB_VENDOR_ID_MOZA, USB_DEVICE_ID_MOZA_R16_R21),
.driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_0_INFINITE_LENGTH }, .driver_data = PIDFF_QUIRK_FIX_WHEEL_DIRECTION | PIDFF_QUIRK_FIX_PERIODIC_ENVELOPE },
{ } { }
}; };
MODULE_DEVICE_TABLE(hid, pidff_wheel_devices); MODULE_DEVICE_TABLE(hid, pidff_wheel_devices);
@@ -39,7 +39,7 @@ static u8 *moza_report_fixup(struct hid_device *hdev, __u8 *rdesc,
} }
static u8 *new_pidff_report_fixup(struct hid_device *hdev, __u8 *rdesc, static u8 *universal_pidff_report_fixup(struct hid_device *hdev, __u8 *rdesc,
unsigned int *rsize) unsigned int *rsize)
{ {
if (hdev->vendor == USB_VENDOR_ID_MOZA) { if (hdev->vendor == USB_VENDOR_ID_MOZA) {
@@ -53,7 +53,7 @@ static u8 *new_pidff_report_fixup(struct hid_device *hdev, __u8 *rdesc,
* Check if the device is PID and initialize it * Check if the device is PID and initialize it
* Add quirks after initialisation * Add quirks after initialisation
*/ */
static int new_pidff_probe(struct hid_device *hdev, static int universal_pidff_probe(struct hid_device *hdev,
const struct hid_device_id *id) const struct hid_device_id *id)
{ {
int ret; int ret;
@@ -69,7 +69,7 @@ static int new_pidff_probe(struct hid_device *hdev,
goto err; goto err;
} }
ret = hid_new_pidff_init(hdev, id); ret = hid_pidff_init_with_quirks(hdev, id);
if (ret) { if (ret) {
hid_warn(hdev, "Force feedback not supported\n"); hid_warn(hdev, "Force feedback not supported\n");
goto err; goto err;
@@ -80,7 +80,7 @@ err:
return ret; return ret;
} }
static int new_pidff_input_configured(struct hid_device *hdev, static int universal_pidff_input_configured(struct hid_device *hdev,
struct hid_input *hidinput) struct hid_input *hidinput)
{ {
struct input_dev *input = hidinput->input; struct input_dev *input = hidinput->input;
@@ -90,15 +90,15 @@ static int new_pidff_input_configured(struct hid_device *hdev,
return 0; return 0;
} }
static struct hid_driver new_pidff = { static struct hid_driver universal_pidff = {
.name = "new-pidff", .name = "hid-universal-pidff",
.id_table = pidff_wheel_devices, .id_table = pidff_wheel_devices,
.probe = new_pidff_probe, .probe = universal_pidff_probe,
.input_configured = new_pidff_input_configured, .input_configured = universal_pidff_input_configured,
.report_fixup = new_pidff_report_fixup .report_fixup = universal_pidff_report_fixup
}; };
module_hid_driver(new_pidff); module_hid_driver(universal_pidff);
MODULE_AUTHOR("Oleg Makarenko <oleg@makarenk.ooo>"); MODULE_AUTHOR("Oleg Makarenko <oleg@makarenk.ooo>");
MODULE_DESCRIPTION("Improved HID PIDFF Driver"); MODULE_DESCRIPTION("Universal HID PIDFF Driver");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");