From 60e90e0a96039c58968de551aadca0fab490d35d Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 12 Mar 2022 18:00:40 +0200 Subject: allow for wheel 'backends' --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 1b4e1f4..9a7b851 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -obj-m += hid-tmt300rs.o +obj-m += hid-tmff2.o KDIR ?= /lib/modules/$(shell uname -r)/build all: hid-tminit -- cgit v1.3 From 0dcf31ff3fe8eb9bc079d94242253290a30eb97e Mon Sep 17 00:00:00 2001 From: Kimplul Date: Mon, 14 Mar 2022 00:23:40 +0200 Subject: don't include .c directly Arguably more hygienic. --- Kbuild | 2 ++ Makefile | 1 - hid-tmff2.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++--------- hid-tmff2.h | 32 ++++++++++++++----- hid-tmt300rs.c | 74 +++++++------------------------------------- hid-tmt300rs.h | 9 ++++++ 6 files changed, 131 insertions(+), 85 deletions(-) create mode 100644 Kbuild create mode 100644 hid-tmt300rs.h (limited to 'Makefile') diff --git a/Kbuild b/Kbuild new file mode 100644 index 0000000..697f3e1 --- /dev/null +++ b/Kbuild @@ -0,0 +1,2 @@ +obj-m := hid-tmff-new.o +hid-tmff-new-y := hid-tmff2.o hid-tmt300rs.o diff --git a/Makefile b/Makefile index 9a7b851..2d88264 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ -obj-m += hid-tmff2.o KDIR ?= /lib/modules/$(shell uname -r)/build all: hid-tminit diff --git a/hid-tmff2.c b/hid-tmff2.c index 6a112fa..811d75b 100644 --- a/hid-tmff2.c +++ b/hid-tmff2.c @@ -1,38 +1,40 @@ #include #include #include +#define TMFF2_MAIN #include "hid-tmff2.h" +#include "hid-tmt300rs.h" -static int timer_msecs = DEFAULT_TIMER_PERIOD; +int timer_msecs = DEFAULT_TIMER_PERIOD; module_param(timer_msecs, int, 0660); MODULE_PARM_DESC(timer_msecs, "Timer resolution in msecs"); /* should these be removed and just rely on /sys? */ -static int spring_level = 30; +int spring_level = 30; module_param(spring_level, int, 0); MODULE_PARM_DESC(spring_level, "Level of spring force (0-100), as per Oversteer standards"); -static int damper_level = 30; +int damper_level = 30; module_param(damper_level, int, 0); MODULE_PARM_DESC(damper_level, "Level of damper force (0-100), as per Oversteer standards"); -static int friction_level = 30; +int friction_level = 30; module_param(friction_level, int, 0); MODULE_PARM_DESC(friction_level, "Level of friction force (0-100), as per Oversteer standards"); -static int range = 900; +int range = 900; module_param(range, int, 0); MODULE_PARM_DESC(range, "Range of wheel, depends on the wheel. Invalid values are ignored."); -static int alt_mode = 0; +int alt_mode = 0; module_param(alt_mode, int, 0); MODULE_PARM_DESC(alt_mode, - "Alternate mode, eg. T300RS F1 mode."); + "Alternate mode, eg. F1 mode."); static spinlock_t lock; static unsigned long lock_flags; @@ -217,9 +219,6 @@ static ssize_t alt_mode_show(struct device *dev, } static DEVICE_ATTR_RW(alt_mode); -/* include each wheel */ -#include "hid-tmt300rs.c" - static void tmff2_set_gain(struct input_dev *dev, uint16_t gain) { struct tmff2_device_entry *tmff2 = tmff2_from_input(dev); @@ -416,6 +415,61 @@ static void tmff2_close(struct input_dev *dev) hid_err(tmff2->hdev, "no close callback set\n"); } +static int tmff2_create_files(struct tmff2_device_entry *tmff2) +{ + struct device *dev = &tmff2->hdev->dev; + int ret; + + /* could use short circuiting but this is more explicit */ + if (tmff2->params & HAS_ALT_MODE) { + if ((ret = device_create_file(dev, &dev_attr_alt_mode))) { + hid_err(tmff2->hdev, "unable to create sysfs for alt_mode\n"); + goto alt_err; + } + } + + if (tmff2->params & HAS_RANGE) { + if ((ret = device_create_file(dev, &dev_attr_range))) { + hid_warn(tmff2->hdev, "unable to create sysfs for range\n"); + goto range_err; + } + } + + if (tmff2->params & HAS_SPRING_LEVEL) { + if ((ret = device_create_file(dev, &dev_attr_spring_level))) { + hid_warn(tmff2->hdev, "unable to create sysfs for spring_level\n"); + goto spring_err; + } + } + + if (tmff2->params & HAS_DAMPER_LEVEL) { + if ((ret = device_create_file(dev, &dev_attr_damper_level))) { + hid_warn(tmff2->hdev, "unable to create sysfs for damper_level\n"); + goto damper_err; + } + } + + if (tmff2->params & HAS_FRICTION_LEVEL) { + if ((ret = device_create_file(dev, &dev_attr_friction_level))) { + hid_warn(tmff2->hdev, "unable to create sysfs for friction_level\n"); + goto friction_err; + } + } + + return 0; + +friction_err: + device_remove_file(dev, &dev_attr_damper_level); +damper_err: + device_remove_file(dev, &dev_attr_spring_level); +spring_err: + device_remove_file(dev, &dev_attr_range); +range_err: + device_remove_file(dev, &dev_attr_alt_mode); +alt_err: + return ret; +} + static int tmff2_wheel_init(struct tmff2_device_entry *tmff2) { int ret, i; @@ -464,6 +518,10 @@ static int tmff2_wheel_init(struct tmff2_device_entry *tmff2) if (tmff2->set_autocenter) ff->set_autocenter = tmff2_set_autocenter; + /* create files */ + if ((ret = tmff2_create_files(tmff2))) + goto err; + return 0; err: @@ -519,13 +577,10 @@ static int tmff2_probe(struct hid_device *hdev, const struct hid_device_id *id) init_err: hid_hw_stop(hdev); - hid_err: tmff2->wheel_destroy(tmff2->data); - wheel_err: kfree(tmff2); - oom_err: return ret; } @@ -546,12 +601,27 @@ static __u8 *tmff2_report_fixup(struct hid_device *hdev, __u8 *rdesc, static void tmff2_remove(struct hid_device *hdev) { struct tmff2_device_entry *tmff2 = tmff2_from_hdev(hdev); + struct device *dev; if (!tmff2) return; + cancel_delayed_work_sync(&tmff2->work); - tmff2->wheel_destroy(tmff2->data); + dev = &tmff2->hdev->dev; + if (tmff2->params & HAS_DAMPER_LEVEL) + device_remove_file(dev, &dev_attr_damper_level); + + if (tmff2->params & HAS_SPRING_LEVEL) + device_remove_file(dev, &dev_attr_spring_level); + + if (tmff2->params & HAS_RANGE) + device_remove_file(dev, &dev_attr_range); + + if (tmff2->params & HAS_ALT_MODE) + device_remove_file(dev, &dev_attr_alt_mode); + + tmff2->wheel_destroy(tmff2->data); hid_hw_stop(hdev); kfree(tmff2->states); diff --git a/hid-tmff2.h b/hid-tmff2.h index b2a46c9..1585649 100644 --- a/hid-tmff2.h +++ b/hid-tmff2.h @@ -1,11 +1,20 @@ /* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _HID_TMFF2 -#define _HID_TMFF2 +#ifndef __HID_TMFF2_H +#define __HID_TMFF2_H #include #include #include +#ifndef TMFF2_MAIN +extern int timer_msecs; +extern int spring_level; +extern int damper_level; +extern int friction_level; +extern int range; +extern int alt_mode; +#endif + #define USB_VENDOR_ID_THRUSTMASTER 0x044f /* the wheel seems to only be capable of processing a certain number of @@ -16,11 +25,17 @@ */ #define DEFAULT_TIMER_PERIOD 8 -#define FF_EFFECT_QUEUE_UPLOAD 0 -#define FF_EFFECT_QUEUE_START 1 -#define FF_EFFECT_QUEUE_STOP 2 -#define FF_EFFECT_QUEUE_UPDATE 3 -#define FF_EFFECT_PLAYING 4 +#define FF_EFFECT_QUEUE_UPLOAD 0 +#define FF_EFFECT_QUEUE_START 1 +#define FF_EFFECT_QUEUE_STOP 2 +#define FF_EFFECT_QUEUE_UPDATE 3 +#define FF_EFFECT_PLAYING 4 + +#define HAS_SPRING_LEVEL (1 << 0) +#define HAS_DAMPER_LEVEL (1 << 1) +#define HAS_FRICTION_LEVEL (1 << 2) +#define HAS_RANGE (1 << 3) +#define HAS_ALT_MODE (1 << 4) #undef fixp_sin16 #define fixp_sin16(v) (((v % 360) > 180) ?\ @@ -51,6 +66,7 @@ struct tmff2_device_entry { /* fields relevant to each actual device (T300, T150...) */ void *data; + unsigned long params; unsigned long max_effects; signed short supported_effects[FF_CNT]; @@ -75,4 +91,4 @@ struct tmff2_device_entry { /* void pointers are dangerous, I know, but in this case likely the best option... */ }; -#endif /* _HID_TMFF2 */ +#endif /* __HID_TMFF2_H */ diff --git a/hid-tmt300rs.c b/hid-tmt300rs.c index 8171578..b253562 100644 --- a/hid-tmt300rs.c +++ b/hid-tmt300rs.c @@ -1,10 +1,20 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include +#include "hid-tmff2.h" +#include "hid-tmt300rs.h" #define T300RS_MAX_EFFECTS 16 #define T300RS_NORM_BUFFER_LENGTH 63 #define T300RS_PS4_BUFFER_LENGTH 31 +static const unsigned long t300rs_params = + HAS_SPRING_LEVEL + | HAS_DAMPER_LEVEL + | HAS_FRICTION_LEVEL + | HAS_RANGE + | HAS_ALT_MODE; + static const signed short t300rs_effects[] = { FF_CONSTANT, FF_RAMP, @@ -1329,50 +1339,6 @@ static int t300rs_close(void *data) return ret; } -static int t300rs_create_files(struct t300rs_device_entry *t300rs) -{ - int ret; - if ((ret = device_create_file(&t300rs->hdev->dev, &dev_attr_alt_mode))) { - hid_err(t300rs->hdev, "unable to create sysfs interface for alt_mode\n"); - goto alt_err; - } - - if ((ret = device_create_file(&t300rs->hdev->dev, &dev_attr_range))) { - hid_warn(t300rs->hdev, "unable to create sysfs interface for range\n"); - goto range_err; - } - - if ((ret = device_create_file(&t300rs->hdev->dev, &dev_attr_spring_level))) { - hid_warn(t300rs->hdev, "unable to create sysfs interface for spring_level\n"); - goto spring_err; - } - - if ((ret = device_create_file(&t300rs->hdev->dev, &dev_attr_damper_level))) { - hid_warn(t300rs->hdev, "unable to create sysfs interface for damper_level\n"); - goto damper_err; - } - - if ((ret = device_create_file(&t300rs->hdev->dev, &dev_attr_friction_level))) { - hid_warn(t300rs->hdev, "unable to create sysfs interface for friction_level\n"); - goto friction_err; - } - - return ret; - - /* if the creation of dev_attr_friction fails, we don't need to remove it */ - /* device_remove_file(&hdev->dev, &dev_attr_friction_level); */ -friction_err: - device_remove_file(&t300rs->hdev->dev, &dev_attr_damper_level); -damper_err: - device_remove_file(&t300rs->hdev->dev, &dev_attr_spring_level); -spring_err: - device_remove_file(&t300rs->hdev->dev, &dev_attr_range); -range_err: - device_remove_file(&t300rs->hdev->dev, &dev_attr_alt_mode); -alt_err: - return ret; -} - static int t300rs_check_firmware(struct t300rs_device_entry *t300rs) { int ret; @@ -1460,28 +1426,20 @@ static int t300rs_wheel_init(struct tmff2_device_entry *tmff2) t300rs->open = t300rs->input_dev->open; t300rs->close = t300rs->input_dev->close; - if ((ret = t300rs_create_files(t300rs))) { - /* probably not a massive issue, but could affect programs like - * Oversteer, best play it safe */ - hid_err(t300rs->hdev, "could not create sysfs files\n"); - goto sysfs_err; - } - /* TODO: PS4 advanced mode? */ alt_mode = (t300rs->hdev->product == 0xb66f); /* everythin went OK */ tmff2->data = t300rs; + tmff2->params = t300rs_params; tmff2->max_effects = T300RS_MAX_EFFECTS; memcpy(tmff2->supported_effects, t300rs_effects, sizeof(t300rs_effects)); hid_info(t300rs->hdev, "force feedback for T300RS\n"); return 0; -sysfs_err: firmware_err: kfree(t300rs->send_buffer); - t300rs_err: kfree(t300rs); send_err: @@ -1495,14 +1453,6 @@ static int t300rs_wheel_destroy(void *data) if (!t300rs) return -ENODEV; - /* apparently should be safe to call these even without the files - * existing */ - device_remove_file(&t300rs->hdev->dev, &dev_attr_range); - device_remove_file(&t300rs->hdev->dev, &dev_attr_alt_mode); - device_remove_file(&t300rs->hdev->dev, &dev_attr_spring_level); - device_remove_file(&t300rs->hdev->dev, &dev_attr_damper_level); - device_remove_file(&t300rs->hdev->dev, &dev_attr_friction_level); - kfree(t300rs->send_buffer); kfree(t300rs); return 0; @@ -1534,7 +1484,7 @@ static __u8 *t300rs_wheel_fixup(struct hid_device *hdev, __u8 *rdesc, return rdesc; } -static int t300rs_populate_api(struct tmff2_device_entry *tmff2) +int t300rs_populate_api(struct tmff2_device_entry *tmff2) { /* set callbacks */ tmff2->play_effect = t300rs_play_effect; diff --git a/hid-tmt300rs.h b/hid-tmt300rs.h new file mode 100644 index 0000000..9357ce1 --- /dev/null +++ b/hid-tmt300rs.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __HID_TMT300RS_H +#define __HID_TMT300RS_H + +#include "hid-tmff2.h" + +int t300rs_populate_api(struct tmff2_device_entry *tmff2); + +#endif /* __HID_TM300RS_H */ -- cgit v1.3