@@ -242,8 +242,31 @@ static ssize_t gain_store(struct device *dev,
242242 }
243243
244244 gain = value ;
245- if (tmff2 -> set_gain ) /* if we can, update gain immediately */
246- tmff2 -> set_gain (tmff2 -> data , (GAIN_MAX * gain ) / GAIN_MAX );
245+
246+ /* Rationale: two-level gain model
247+ * - The input API's set_gain (pg) is the in-game gain (0..GAIN_MAX).
248+ * - This driver also exposes a device/system gain via sysfs param `gain`.
249+ * - The device callback receives the product: (pg * gain) / GAIN_MAX.
250+ * See worker at tmff2->set_gain(... (pg * gain) / GAIN_MAX ).
251+ * When the sysfs `gain` changes, we trigger a recompute by pushing
252+ * pending_gain_value = GAIN_MAX here so the effective device gain becomes
253+ * exactly the sysfs value (GAIN_MAX * gain / GAIN_MAX == gain) and future
254+ * in-game set_gain calls continue to multiply in.
255+ *
256+ * References:
257+ * - docs/FFBEFFECTS.md: section "FF_GAIN" shows a dedicated device gain path.
258+ * - docs/FFB_T500RS.md: Report glossary mentions 0x43 (gain), i.e., device-side
259+ * gain separate from per-effect magnitudes; drivers should expose both levels.
260+ */
261+ if (tmff2 -> set_gain ) {
262+ unsigned long flags ;
263+ spin_lock_irqsave (& tmff2 -> lock , flags );
264+ tmff2 -> pending_gain_value = GAIN_MAX ;
265+ tmff2 -> gain_pending = 1 ;
266+ spin_unlock_irqrestore (& tmff2 -> lock , flags );
267+ if (!delayed_work_pending (& tmff2 -> work ) && tmff2 -> allow_scheduling )
268+ schedule_delayed_work (& tmff2 -> work , 0 );
269+ }
247270
248271 return count ;
249272}
@@ -258,6 +281,7 @@ static DEVICE_ATTR_RW(gain);
258281static void tmff2_set_gain (struct input_dev * dev , uint16_t value )
259282{
260283 struct tmff2_device_entry * tmff2 = tmff2_from_input (dev );
284+ unsigned long flags ;
261285
262286 if (!tmff2 )
263287 return ;
@@ -267,13 +291,20 @@ static void tmff2_set_gain(struct input_dev *dev, uint16_t value)
267291 return ;
268292 }
269293
270- if (tmff2 -> set_gain (tmff2 -> data , (value * gain ) / GAIN_MAX ))
271- hid_warn (tmff2 -> hdev , "unable to set gain\n" );
294+ /* Defer to workqueue: store pending gain and schedule */
295+ spin_lock_irqsave (& tmff2 -> lock , flags );
296+ tmff2 -> pending_gain_value = value ;
297+ tmff2 -> gain_pending = 1 ;
298+ spin_unlock_irqrestore (& tmff2 -> lock , flags );
299+
300+ if (!delayed_work_pending (& tmff2 -> work ) && tmff2 -> allow_scheduling )
301+ schedule_delayed_work (& tmff2 -> work , 0 );
272302}
273303
274304static void tmff2_set_autocenter (struct input_dev * dev , uint16_t value )
275305{
276306 struct tmff2_device_entry * tmff2 = tmff2_from_input (dev );
307+ unsigned long flags ;
277308
278309 if (!tmff2 )
279310 return ;
@@ -283,8 +314,14 @@ static void tmff2_set_autocenter(struct input_dev *dev, uint16_t value)
283314 return ;
284315 }
285316
286- if (tmff2 -> set_autocenter (tmff2 -> data , value ))
287- hid_warn (tmff2 -> hdev , "unable to set autocenter\n" );
317+ /* Defer to workqueue: store pending autocenter and schedule */
318+ spin_lock_irqsave (& tmff2 -> lock , flags );
319+ tmff2 -> pending_autocenter_value = value ;
320+ tmff2 -> autocenter_pending = 1 ;
321+ spin_unlock_irqrestore (& tmff2 -> lock , flags );
322+
323+ if (!delayed_work_pending (& tmff2 -> work ) && tmff2 -> allow_scheduling )
324+ schedule_delayed_work (& tmff2 -> work , 0 );
288325}
289326
290327static void tmff2_work_handler (struct work_struct * w )
@@ -301,6 +338,30 @@ static void tmff2_work_handler(struct work_struct *w)
301338 if (!tmff2 )
302339 return ;
303340
341+ /* Apply pending control changes (gain/autocenter) in process context */
342+ {
343+ unsigned long f2 ;
344+ uint16_t pg = 0 , pac = 0 ;
345+ int do_gain = 0 , do_ac = 0 ;
346+ spin_lock_irqsave (& tmff2 -> lock , f2 );
347+ if (tmff2 -> gain_pending ) {
348+ pg = tmff2 -> pending_gain_value ;
349+ tmff2 -> gain_pending = 0 ;
350+ do_gain = 1 ;
351+ }
352+ if (tmff2 -> autocenter_pending ) {
353+ pac = tmff2 -> pending_autocenter_value ;
354+ tmff2 -> autocenter_pending = 0 ;
355+ do_ac = 1 ;
356+ }
357+ spin_unlock_irqrestore (& tmff2 -> lock , f2 );
358+
359+ if (do_gain && tmff2 -> set_gain )
360+ tmff2 -> set_gain (tmff2 -> data , (pg * gain ) / GAIN_MAX );
361+ if (do_ac && tmff2 -> set_autocenter )
362+ tmff2 -> set_autocenter (tmff2 -> data , pac );
363+ }
364+
304365 for (effect_id = 0 ; effect_id < tmff2 -> max_effects ; ++ effect_id ) {
305366 unsigned long actions = 0 ;
306367 struct tmff2_effect_state effect ;
@@ -315,15 +376,26 @@ static void tmff2_work_handler(struct work_struct *w)
315376
316377 effect_delay = state -> effect .replay .delay ;
317378 effect_length = state -> effect .replay .length ;
379+ /* If playing with a finite length, stop when (delay + length) elapses */
318380 if (test_bit (FF_EFFECT_PLAYING , & state -> flags ) && effect_length ) {
319381 if ((time_now - state -> start_time ) >=
320382 (effect_delay + effect_length ) * state -> count ) {
321383 __clear_bit (FF_EFFECT_PLAYING , & state -> flags );
322384 __clear_bit (FF_EFFECT_QUEUE_UPDATE , & state -> flags );
323-
385+ /* Request a STOP in process context */
386+ __set_bit (FF_EFFECT_QUEUE_STOP , & actions );
324387 state -> count = 0 ;
325388 }
326389 }
390+ /* Delay handling for start: only trigger START after replay.delay */
391+ if (test_bit (FF_EFFECT_QUEUE_START , & state -> flags )) {
392+ if ((time_now - state -> start_time ) >= effect_delay ) {
393+ __set_bit (FF_EFFECT_QUEUE_START , & actions );
394+ __clear_bit (FF_EFFECT_QUEUE_START , & state -> flags );
395+ /* effect is playing since we're starting it now */
396+ __set_bit (FF_EFFECT_PLAYING , & state -> flags );
397+ } /* else: keep START pending until delay elapsed */
398+ }
327399
328400 if (test_bit (FF_EFFECT_QUEUE_UPLOAD , & state -> flags )) {
329401 __set_bit (FF_EFFECT_QUEUE_UPLOAD , & actions );
@@ -694,6 +766,11 @@ static int tmff2_probe(struct hid_device *hdev, const struct hid_device_id *id)
694766 goto wheel_err ;
695767 break ;
696768
769+ case TMT500RS_PC_ID :
770+ if ((ret = t500rs_populate_api (tmff2 )))
771+ goto wheel_err ;
772+ break ;
773+
697774 case TMT248_PC_ID :
698775 if ((ret = t248_populate_api (tmff2 )))
699776 goto wheel_err ;
@@ -802,6 +879,8 @@ static const struct hid_device_id tmff2_devices[] = {
802879 {HID_USB_DEVICE (USB_VENDOR_ID_THRUSTMASTER , TMT300RS_PS3_NORM_ID )},
803880 {HID_USB_DEVICE (USB_VENDOR_ID_THRUSTMASTER , TMT300RS_PS3_ADV_ID )},
804881 {HID_USB_DEVICE (USB_VENDOR_ID_THRUSTMASTER , TMT300RS_PS4_NORM_ID )},
882+ /* t500rs */
883+ {HID_USB_DEVICE (USB_VENDOR_ID_THRUSTMASTER , TMT500RS_PC_ID )},
805884 /* t248 PC*/
806885 {HID_USB_DEVICE (USB_VENDOR_ID_THRUSTMASTER , TMT248_PC_ID )},
807886 /* tx */
@@ -822,4 +901,10 @@ static struct hid_driver tmff2_driver = {
822901};
823902module_hid_driver (tmff2_driver );
824903
904+
905+ #ifndef TMFF2_DRIVER_VERSION
906+ #define TMFF2_DRIVER_VERSION "dev"
907+ #endif
908+ MODULE_VERSION (TMFF2_DRIVER_VERSION );
909+
825910MODULE_LICENSE ("GPL" );
0 commit comments