Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  Force feedback support for memoryless devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) /* #define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/fixp-arith.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) MODULE_AUTHOR("Anssi Hannula <anssi.hannula@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) MODULE_DESCRIPTION("Force feedback support for memoryless devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /* Number of effects handled with memoryless devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define FF_MEMLESS_EFFECTS	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /* Envelope update interval in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define FF_ENVELOPE_INTERVAL	50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define FF_EFFECT_STARTED	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define FF_EFFECT_PLAYING	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define FF_EFFECT_ABORTING	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) struct ml_effect_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct ff_effect *effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	unsigned long flags;	/* effect state (STARTED, PLAYING, etc) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int count;		/* loop count of the effect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	unsigned long play_at;	/* start time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	unsigned long stop_at;	/* stop time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	unsigned long adj_at;	/* last time the effect was sent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct ml_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	void *private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct ml_effect_state states[FF_MEMLESS_EFFECTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int (*play_effect)(struct input_dev *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			   struct ff_effect *effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static const struct ff_envelope *get_envelope(const struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	static const struct ff_envelope empty_envelope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	switch (effect->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	case FF_PERIODIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return &effect->u.periodic.envelope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	case FF_CONSTANT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return &effect->u.constant.envelope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return &empty_envelope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * Check for the next time envelope requires an update on memoryless devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static unsigned long calculate_next_time(struct ml_effect_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	const struct ff_envelope *envelope = get_envelope(state->effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	unsigned long attack_stop, fade_start, next_fade;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (envelope->attack_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		attack_stop = state->play_at +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			msecs_to_jiffies(envelope->attack_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		if (time_before(state->adj_at, attack_stop))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			return state->adj_at +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 					msecs_to_jiffies(FF_ENVELOPE_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (state->effect->replay.length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (envelope->fade_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			/* check when fading should start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			fade_start = state->stop_at -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 					msecs_to_jiffies(envelope->fade_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			if (time_before(state->adj_at, fade_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 				return fade_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			/* already fading, advance to next checkpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			next_fade = state->adj_at +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 					msecs_to_jiffies(FF_ENVELOPE_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			if (time_before(next_fade, state->stop_at))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				return next_fade;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return state->stop_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return state->play_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void ml_schedule_timer(struct ml_device *ml)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct ml_effect_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	unsigned long now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	unsigned long earliest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	unsigned long next_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	pr_debug("calculating next timer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		state = &ml->states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (!test_bit(FF_EFFECT_STARTED, &state->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (test_bit(FF_EFFECT_PLAYING, &state->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			next_at = calculate_next_time(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			next_at = state->play_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (time_before_eq(now, next_at) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		    (++events == 1 || time_before(next_at, earliest)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			earliest = next_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (!events) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		pr_debug("no actions\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		del_timer(&ml->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		pr_debug("timer set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		mod_timer(&ml->timer, earliest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * Apply an envelope to a value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int apply_envelope(struct ml_effect_state *state, int value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			  struct ff_envelope *envelope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct ff_effect *effect = state->effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	unsigned long now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	int time_from_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int time_of_envelope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int envelope_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int difference;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (envelope->attack_length &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	    time_before(now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			state->play_at + msecs_to_jiffies(envelope->attack_length))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		pr_debug("value = 0x%x, attack_level = 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			 value, envelope->attack_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		time_from_level = jiffies_to_msecs(now - state->play_at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		time_of_envelope = envelope->attack_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		envelope_level = min_t(u16, envelope->attack_level, 0x7fff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	} else if (envelope->fade_length && effect->replay.length &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		   time_after(now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			      state->stop_at - msecs_to_jiffies(envelope->fade_length)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		   time_before(now, state->stop_at)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		time_from_level = jiffies_to_msecs(state->stop_at - now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		time_of_envelope = envelope->fade_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		envelope_level = min_t(u16, envelope->fade_level, 0x7fff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	difference = abs(value) - envelope_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	pr_debug("difference = %d\n", difference);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	pr_debug("time_from_level = 0x%x\n", time_from_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	pr_debug("time_of_envelope = 0x%x\n", time_of_envelope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	difference = difference * time_from_level / time_of_envelope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	pr_debug("difference = %d\n", difference);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return value < 0 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		-(difference + envelope_level) : (difference + envelope_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * Return the type the effect has to be converted into (memless devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int get_compatible_type(struct ff_device *ff, int effect_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (test_bit(effect_type, ff->ffbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return effect_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (effect_type == FF_PERIODIC && test_bit(FF_RUMBLE, ff->ffbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		return FF_RUMBLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	pr_err("invalid type in get_compatible_type()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * Only left/right direction should be used (under/over 0x8000) for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * forward/reverse motor direction (to keep calculation fast & simple).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static u16 ml_calculate_direction(u16 direction, u16 force,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				  u16 new_direction, u16 new_force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (!force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		return new_direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!new_force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	return (((u32)(direction >> 1) * force +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		 (new_direction >> 1) * new_force) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		(force + new_force)) << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) #define FRAC_N 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static inline s16 fixp_new16(s16 a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return ((s32)a) >> (16 - FRAC_N);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static inline s16 fixp_mult(s16 a, s16 b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	a = ((s32)a * 0x100) / 0x7fff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return ((s32)(a * b)) >> FRAC_N;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * Combine two effects and apply gain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static void ml_combine_effects(struct ff_effect *effect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			       struct ml_effect_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			       int gain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct ff_effect *new = state->effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	unsigned int strong, weak, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	int x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	s16 level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	switch (new->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	case FF_CONSTANT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		i = new->direction * 360 / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		level = fixp_new16(apply_envelope(state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 					new->u.constant.level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 					&new->u.constant.envelope));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		x = fixp_mult(fixp_sin16(i), level) * gain / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		y = fixp_mult(-fixp_cos16(i), level) * gain / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 * here we abuse ff_ramp to hold x and y of constant force
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 * If in future any driver wants something else than x and y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		 * in s8, this should be changed to something more generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		effect->u.ramp.start_level =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			clamp_val(effect->u.ramp.start_level + x, -0x80, 0x7f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		effect->u.ramp.end_level =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			clamp_val(effect->u.ramp.end_level + y, -0x80, 0x7f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	case FF_RUMBLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		strong = (u32)new->u.rumble.strong_magnitude * gain / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		weak = (u32)new->u.rumble.weak_magnitude * gain / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		if (effect->u.rumble.strong_magnitude + strong)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			effect->direction = ml_calculate_direction(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				effect->direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				effect->u.rumble.strong_magnitude,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				new->direction, strong);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		else if (effect->u.rumble.weak_magnitude + weak)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			effect->direction = ml_calculate_direction(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				effect->direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				effect->u.rumble.weak_magnitude,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				new->direction, weak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			effect->direction = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		effect->u.rumble.strong_magnitude =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			min(strong + effect->u.rumble.strong_magnitude,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			    0xffffU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		effect->u.rumble.weak_magnitude =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			min(weak + effect->u.rumble.weak_magnitude, 0xffffU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	case FF_PERIODIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		i = apply_envelope(state, abs(new->u.periodic.magnitude),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				   &new->u.periodic.envelope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		/* here we also scale it 0x7fff => 0xffff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		i = i * gain / 0x7fff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		if (effect->u.rumble.strong_magnitude + i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			effect->direction = ml_calculate_direction(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				effect->direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 				effect->u.rumble.strong_magnitude,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 				new->direction, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			effect->direction = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		effect->u.rumble.strong_magnitude =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			min(i + effect->u.rumble.strong_magnitude, 0xffffU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		effect->u.rumble.weak_magnitude =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			min(i + effect->u.rumble.weak_magnitude, 0xffffU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		pr_err("invalid type in ml_combine_effects()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * Because memoryless devices have only one effect per effect type active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  * at one time we have to combine multiple effects into one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int ml_get_combo_effect(struct ml_device *ml,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			       unsigned long *effect_handled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			       struct ff_effect *combo_effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	struct ff_effect *effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct ml_effect_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	int effect_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	memset(combo_effect, 0, sizeof(struct ff_effect));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	for (i = 0; i < FF_MEMLESS_EFFECTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (__test_and_set_bit(i, effect_handled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		state = &ml->states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		effect = state->effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (!test_bit(FF_EFFECT_STARTED, &state->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		if (time_before(jiffies, state->play_at))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		 * here we have started effects that are either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		 * currently playing (and may need be aborted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		 * or need to start playing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		effect_type = get_compatible_type(ml->dev->ff, effect->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		if (combo_effect->type != effect_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			if (combo_effect->type != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				__clear_bit(i, effect_handled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			combo_effect->type = effect_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		if (__test_and_clear_bit(FF_EFFECT_ABORTING, &state->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			__clear_bit(FF_EFFECT_PLAYING, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			__clear_bit(FF_EFFECT_STARTED, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		} else if (effect->replay.length &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			   time_after_eq(jiffies, state->stop_at)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			__clear_bit(FF_EFFECT_PLAYING, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			if (--state->count <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				__clear_bit(FF_EFFECT_STARTED, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 				state->play_at = jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 					msecs_to_jiffies(effect->replay.delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 				state->stop_at = state->play_at +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 					msecs_to_jiffies(effect->replay.length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			__set_bit(FF_EFFECT_PLAYING, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			state->adj_at = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			ml_combine_effects(combo_effect, state, ml->gain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	return combo_effect->type != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static void ml_play_effects(struct ml_device *ml)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct ff_effect effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	DECLARE_BITMAP(handled_bm, FF_MEMLESS_EFFECTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	memset(handled_bm, 0, sizeof(handled_bm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	while (ml_get_combo_effect(ml, handled_bm, &effect))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		ml->play_effect(ml->dev, ml->private, &effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	ml_schedule_timer(ml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static void ml_effect_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	struct ml_device *ml = from_timer(ml, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	struct input_dev *dev = ml->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	pr_debug("timer: updating effects\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	spin_lock_irqsave(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	ml_play_effects(ml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	spin_unlock_irqrestore(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * Sets requested gain for FF effects. Called with dev->event_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static void ml_ff_set_gain(struct input_dev *dev, u16 gain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	struct ml_device *ml = dev->ff->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	ml->gain = gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		__clear_bit(FF_EFFECT_PLAYING, &ml->states[i].flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	ml_play_effects(ml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  * Start/stop specified FF effect. Called with dev->event_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static int ml_ff_playback(struct input_dev *dev, int effect_id, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct ml_device *ml = dev->ff->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	struct ml_effect_state *state = &ml->states[effect_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (value > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		pr_debug("initiated play\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		__set_bit(FF_EFFECT_STARTED, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		state->count = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		state->play_at = jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 				 msecs_to_jiffies(state->effect->replay.delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		state->stop_at = state->play_at +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 				 msecs_to_jiffies(state->effect->replay.length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		state->adj_at = state->play_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		pr_debug("initiated stop\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		if (test_bit(FF_EFFECT_PLAYING, &state->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			__set_bit(FF_EFFECT_ABORTING, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			__clear_bit(FF_EFFECT_STARTED, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	ml_play_effects(ml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static int ml_ff_upload(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			struct ff_effect *effect, struct ff_effect *old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	struct ml_device *ml = dev->ff->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	struct ml_effect_state *state = &ml->states[effect->id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	spin_lock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	if (test_bit(FF_EFFECT_STARTED, &state->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		__clear_bit(FF_EFFECT_PLAYING, &state->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		state->play_at = jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 				 msecs_to_jiffies(state->effect->replay.delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		state->stop_at = state->play_at +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 				 msecs_to_jiffies(state->effect->replay.length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		state->adj_at = state->play_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		ml_schedule_timer(ml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static void ml_ff_destroy(struct ff_device *ff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	struct ml_device *ml = ff->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	 * Even though we stop all playing effects when tearing down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	 * an input device (via input_device_flush() that calls into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	 * input_ff_flush() that stops and erases all effects), we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	 * do not actually stop the timer, and therefore we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	 * do it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	del_timer_sync(&ml->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	kfree(ml->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)  * input_ff_create_memless() - create memoryless force-feedback device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)  * @dev: input device supporting force-feedback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)  * @data: driver-specific data to be passed into @play_effect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  * @play_effect: driver-specific method for playing FF effect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int input_ff_create_memless(struct input_dev *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	struct ml_device *ml;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	struct ff_device *ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	ml = kzalloc(sizeof(struct ml_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (!ml)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	ml->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	ml->private = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	ml->play_effect = play_effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	ml->gain = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	timer_setup(&ml->timer, ml_effect_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	set_bit(FF_GAIN, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	error = input_ff_create(dev, FF_MEMLESS_EFFECTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		kfree(ml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	ff = dev->ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	ff->private = ml;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	ff->upload = ml_ff_upload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	ff->playback = ml_ff_playback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	ff->set_gain = ml_ff_set_gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	ff->destroy = ml_ff_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	/* we can emulate periodic effects with RUMBLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	if (test_bit(FF_RUMBLE, ff->ffbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		set_bit(FF_PERIODIC, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		set_bit(FF_SINE, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		set_bit(FF_TRIANGLE, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		set_bit(FF_SQUARE, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	for (i = 0; i < FF_MEMLESS_EFFECTS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		ml->states[i].effect = &ff->effects[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) EXPORT_SYMBOL_GPL(input_ff_create_memless);