xref: /openbmc/linux/Documentation/input/ff.rst (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
1e2ba5731SMauro Carvalho Chehab========================
2e2ba5731SMauro Carvalho ChehabForce feedback for Linux
3e2ba5731SMauro Carvalho Chehab========================
4e2ba5731SMauro Carvalho Chehab
5e2ba5731SMauro Carvalho Chehab:Author: Johann Deneux <johann.deneux@gmail.com> on 2001/04/22.
6e2ba5731SMauro Carvalho Chehab:Updated: Anssi Hannula <anssi.hannula@gmail.com> on 2006/04/09.
7e2ba5731SMauro Carvalho Chehab
8deeb1e90SMauro Carvalho ChehabYou may redistribute this file. Please remember to include shape.svg and
9deeb1e90SMauro Carvalho Chehabinteractive.svg as well.
10e2ba5731SMauro Carvalho Chehab
11e2ba5731SMauro Carvalho ChehabIntroduction
12e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~
13e2ba5731SMauro Carvalho Chehab
14e2ba5731SMauro Carvalho ChehabThis document describes how to use force feedback devices under Linux. The
15e2ba5731SMauro Carvalho Chehabgoal is not to support these devices as if they were simple input-only devices
16e2ba5731SMauro Carvalho Chehab(as it is already the case), but to really enable the rendering of force
17e2ba5731SMauro Carvalho Chehabeffects.
18e2ba5731SMauro Carvalho ChehabThis document only describes the force feedback part of the Linux input
19*a9600c98SRandy Dunlapinterface. Please read joydev/joystick.rst and input.rst before reading further
20*a9600c98SRandy Dunlapthis document.
21e2ba5731SMauro Carvalho Chehab
22e2ba5731SMauro Carvalho ChehabInstructions to the user
23e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~
24e2ba5731SMauro Carvalho Chehab
25e2ba5731SMauro Carvalho ChehabTo enable force feedback, you have to:
26e2ba5731SMauro Carvalho Chehab
27e2ba5731SMauro Carvalho Chehab1. have your kernel configured with evdev and a driver that supports your
28e2ba5731SMauro Carvalho Chehab   device.
29e2ba5731SMauro Carvalho Chehab2. make sure evdev module is loaded and /dev/input/event* device files are
30e2ba5731SMauro Carvalho Chehab   created.
31e2ba5731SMauro Carvalho Chehab
32e2ba5731SMauro Carvalho ChehabBefore you start, let me WARN you that some devices shake violently during the
33e2ba5731SMauro Carvalho Chehabinitialisation phase. This happens for example with my "AVB Top Shot Pegasus".
34a3f061bcSJean-François TêtuTo stop this annoying behaviour, move your joystick to its limits. Anyway, you
35e2ba5731SMauro Carvalho Chehabshould keep a hand on your device, in order to avoid it to break down if
36e2ba5731SMauro Carvalho Chehabsomething goes wrong.
37e2ba5731SMauro Carvalho Chehab
38e2ba5731SMauro Carvalho ChehabIf you have a serial iforce device, you need to start inputattach. See
39*a9600c98SRandy Dunlapjoydev/joystick.rst for details.
40e2ba5731SMauro Carvalho Chehab
41e2ba5731SMauro Carvalho ChehabDoes it work ?
42e2ba5731SMauro Carvalho Chehab--------------
43e2ba5731SMauro Carvalho Chehab
44e2ba5731SMauro Carvalho ChehabThere is an utility called fftest that will allow you to test the driver::
45e2ba5731SMauro Carvalho Chehab
46e2ba5731SMauro Carvalho Chehab    % fftest /dev/input/eventXX
47e2ba5731SMauro Carvalho Chehab
48e2ba5731SMauro Carvalho ChehabInstructions to the developer
49e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50e2ba5731SMauro Carvalho Chehab
51e2ba5731SMauro Carvalho ChehabAll interactions are done using the event API. That is, you can use ioctl()
52e2ba5731SMauro Carvalho Chehaband write() on /dev/input/eventXX.
53e2ba5731SMauro Carvalho ChehabThis information is subject to change.
54e2ba5731SMauro Carvalho Chehab
55e2ba5731SMauro Carvalho ChehabQuerying device capabilities
56e2ba5731SMauro Carvalho Chehab----------------------------
57e2ba5731SMauro Carvalho Chehab
58e2ba5731SMauro Carvalho Chehab::
59e2ba5731SMauro Carvalho Chehab
60e2ba5731SMauro Carvalho Chehab    #include <linux/input.h>
61e2ba5731SMauro Carvalho Chehab    #include <sys/ioctl.h>
62e2ba5731SMauro Carvalho Chehab
63e2ba5731SMauro Carvalho Chehab    #define BITS_TO_LONGS(x) \
64e2ba5731SMauro Carvalho Chehab	    (((x) + 8 * sizeof (unsigned long) - 1) / (8 * sizeof (unsigned long)))
65e2ba5731SMauro Carvalho Chehab    unsigned long features[BITS_TO_LONGS(FF_CNT)];
66e2ba5731SMauro Carvalho Chehab    int ioctl(int file_descriptor, int request, unsigned long *features);
67e2ba5731SMauro Carvalho Chehab
68e2ba5731SMauro Carvalho Chehab"request" must be EVIOCGBIT(EV_FF, size of features array in bytes )
69e2ba5731SMauro Carvalho Chehab
70e2ba5731SMauro Carvalho ChehabReturns the features supported by the device. features is a bitfield with the
71e2ba5731SMauro Carvalho Chehabfollowing bits:
72e2ba5731SMauro Carvalho Chehab
73e2ba5731SMauro Carvalho Chehab- FF_CONSTANT	can render constant force effects
74e2ba5731SMauro Carvalho Chehab- FF_PERIODIC	can render periodic effects with the following waveforms:
75e2ba5731SMauro Carvalho Chehab
76e2ba5731SMauro Carvalho Chehab  - FF_SQUARE	  square waveform
77e2ba5731SMauro Carvalho Chehab  - FF_TRIANGLE	  triangle waveform
78e2ba5731SMauro Carvalho Chehab  - FF_SINE	  sine waveform
79e2ba5731SMauro Carvalho Chehab  - FF_SAW_UP	  sawtooth up waveform
80e2ba5731SMauro Carvalho Chehab  - FF_SAW_DOWN	  sawtooth down waveform
81e2ba5731SMauro Carvalho Chehab  - FF_CUSTOM	  custom waveform
82e2ba5731SMauro Carvalho Chehab
83e2ba5731SMauro Carvalho Chehab- FF_RAMP       can render ramp effects
84e2ba5731SMauro Carvalho Chehab- FF_SPRING	can simulate the presence of a spring
85e2ba5731SMauro Carvalho Chehab- FF_FRICTION	can simulate friction
86e2ba5731SMauro Carvalho Chehab- FF_DAMPER	can simulate damper effects
87e2ba5731SMauro Carvalho Chehab- FF_RUMBLE	rumble effects
88e2ba5731SMauro Carvalho Chehab- FF_INERTIA    can simulate inertia
89e2ba5731SMauro Carvalho Chehab- FF_GAIN	gain is adjustable
90e2ba5731SMauro Carvalho Chehab- FF_AUTOCENTER	autocenter is adjustable
91e2ba5731SMauro Carvalho Chehab
92e2ba5731SMauro Carvalho Chehab.. note::
93e2ba5731SMauro Carvalho Chehab
94e2ba5731SMauro Carvalho Chehab    - In most cases you should use FF_PERIODIC instead of FF_RUMBLE. All
95e2ba5731SMauro Carvalho Chehab      devices that support FF_RUMBLE support FF_PERIODIC (square, triangle,
96e2ba5731SMauro Carvalho Chehab      sine) and the other way around.
97e2ba5731SMauro Carvalho Chehab
98e2ba5731SMauro Carvalho Chehab    - The exact syntax FF_CUSTOM is undefined for the time being as no driver
99e2ba5731SMauro Carvalho Chehab      supports it yet.
100e2ba5731SMauro Carvalho Chehab
101e2ba5731SMauro Carvalho Chehab::
102e2ba5731SMauro Carvalho Chehab
103e2ba5731SMauro Carvalho Chehab    int ioctl(int fd, EVIOCGEFFECTS, int *n);
104e2ba5731SMauro Carvalho Chehab
105e2ba5731SMauro Carvalho ChehabReturns the number of effects the device can keep in its memory.
106e2ba5731SMauro Carvalho Chehab
107e2ba5731SMauro Carvalho ChehabUploading effects to the device
108e2ba5731SMauro Carvalho Chehab-------------------------------
109e2ba5731SMauro Carvalho Chehab
110e2ba5731SMauro Carvalho Chehab::
111e2ba5731SMauro Carvalho Chehab
112e2ba5731SMauro Carvalho Chehab    #include <linux/input.h>
113e2ba5731SMauro Carvalho Chehab    #include <sys/ioctl.h>
114e2ba5731SMauro Carvalho Chehab
115e2ba5731SMauro Carvalho Chehab    int ioctl(int file_descriptor, int request, struct ff_effect *effect);
116e2ba5731SMauro Carvalho Chehab
117e2ba5731SMauro Carvalho Chehab"request" must be EVIOCSFF.
118e2ba5731SMauro Carvalho Chehab
119e2ba5731SMauro Carvalho Chehab"effect" points to a structure describing the effect to upload. The effect is
120e2ba5731SMauro Carvalho Chehabuploaded, but not played.
121e2ba5731SMauro Carvalho ChehabThe content of effect may be modified. In particular, its field "id" is set
122e2ba5731SMauro Carvalho Chehabto the unique id assigned by the driver. This data is required for performing
123e2ba5731SMauro Carvalho Chehabsome operations (removing an effect, controlling the playback).
124a3f061bcSJean-François TêtuThe "id" field must be set to -1 by the user in order to tell the driver to
125e2ba5731SMauro Carvalho Chehaballocate a new effect.
126e2ba5731SMauro Carvalho Chehab
127e2ba5731SMauro Carvalho ChehabEffects are file descriptor specific.
128e2ba5731SMauro Carvalho Chehab
12916a12fa9SLinus TorvaldsSee <uapi/linux/input.h> for a description of the ff_effect struct.  You
13016a12fa9SLinus Torvaldsshould also find help in a few sketches, contained in files shape.svg
13116a12fa9SLinus Torvaldsand interactive.svg:
132deeb1e90SMauro Carvalho Chehab
133aeb899afSMauro Carvalho Chehab.. kernel-figure:: shape.svg
134deeb1e90SMauro Carvalho Chehab
135deeb1e90SMauro Carvalho Chehab    Shape
136deeb1e90SMauro Carvalho Chehab
137aeb899afSMauro Carvalho Chehab.. kernel-figure:: interactive.svg
138deeb1e90SMauro Carvalho Chehab
139deeb1e90SMauro Carvalho Chehab    Interactive
140e2ba5731SMauro Carvalho Chehab
141e2ba5731SMauro Carvalho Chehab
142e2ba5731SMauro Carvalho ChehabRemoving an effect from the device
143e2ba5731SMauro Carvalho Chehab----------------------------------
144e2ba5731SMauro Carvalho Chehab
145e2ba5731SMauro Carvalho Chehab::
146e2ba5731SMauro Carvalho Chehab
147e2ba5731SMauro Carvalho Chehab    int ioctl(int fd, EVIOCRMFF, effect.id);
148e2ba5731SMauro Carvalho Chehab
149e2ba5731SMauro Carvalho ChehabThis makes room for new effects in the device's memory. Note that this also
150e2ba5731SMauro Carvalho Chehabstops the effect if it was playing.
151e2ba5731SMauro Carvalho Chehab
152e2ba5731SMauro Carvalho ChehabControlling the playback of effects
153e2ba5731SMauro Carvalho Chehab-----------------------------------
154e2ba5731SMauro Carvalho Chehab
155e2ba5731SMauro Carvalho ChehabControl of playing is done with write(). Below is an example:
156e2ba5731SMauro Carvalho Chehab
157e2ba5731SMauro Carvalho Chehab::
158e2ba5731SMauro Carvalho Chehab
159e2ba5731SMauro Carvalho Chehab    #include <linux/input.h>
160e2ba5731SMauro Carvalho Chehab    #include <unistd.h>
161e2ba5731SMauro Carvalho Chehab
162e2ba5731SMauro Carvalho Chehab	struct input_event play;
163e2ba5731SMauro Carvalho Chehab	struct input_event stop;
164e2ba5731SMauro Carvalho Chehab	struct ff_effect effect;
165e2ba5731SMauro Carvalho Chehab	int fd;
166e2ba5731SMauro Carvalho Chehab   ...
167e2ba5731SMauro Carvalho Chehab	fd = open("/dev/input/eventXX", O_RDWR);
168e2ba5731SMauro Carvalho Chehab   ...
169e2ba5731SMauro Carvalho Chehab	/* Play three times */
170e2ba5731SMauro Carvalho Chehab	play.type = EV_FF;
171e2ba5731SMauro Carvalho Chehab	play.code = effect.id;
172e2ba5731SMauro Carvalho Chehab	play.value = 3;
173e2ba5731SMauro Carvalho Chehab
174e2ba5731SMauro Carvalho Chehab	write(fd, (const void*) &play, sizeof(play));
175e2ba5731SMauro Carvalho Chehab   ...
176e2ba5731SMauro Carvalho Chehab	/* Stop an effect */
177e2ba5731SMauro Carvalho Chehab	stop.type = EV_FF;
178e2ba5731SMauro Carvalho Chehab	stop.code = effect.id;
179e2ba5731SMauro Carvalho Chehab	stop.value = 0;
180e2ba5731SMauro Carvalho Chehab
181a3f061bcSJean-François Têtu	write(fd, (const void*) &stop, sizeof(stop));
182e2ba5731SMauro Carvalho Chehab
183e2ba5731SMauro Carvalho ChehabSetting the gain
184e2ba5731SMauro Carvalho Chehab----------------
185e2ba5731SMauro Carvalho Chehab
186e2ba5731SMauro Carvalho ChehabNot all devices have the same strength. Therefore, users should set a gain
187e2ba5731SMauro Carvalho Chehabfactor depending on how strong they want effects to be. This setting is
188e2ba5731SMauro Carvalho Chehabpersistent across access to the driver.
189e2ba5731SMauro Carvalho Chehab
190e2ba5731SMauro Carvalho Chehab::
191e2ba5731SMauro Carvalho Chehab
192e2ba5731SMauro Carvalho Chehab    /* Set the gain of the device
193e2ba5731SMauro Carvalho Chehab    int gain;		/* between 0 and 100 */
194e2ba5731SMauro Carvalho Chehab    struct input_event ie;	/* structure used to communicate with the driver */
195e2ba5731SMauro Carvalho Chehab
196e2ba5731SMauro Carvalho Chehab    ie.type = EV_FF;
197e2ba5731SMauro Carvalho Chehab    ie.code = FF_GAIN;
198e2ba5731SMauro Carvalho Chehab    ie.value = 0xFFFFUL * gain / 100;
199e2ba5731SMauro Carvalho Chehab
200e2ba5731SMauro Carvalho Chehab    if (write(fd, &ie, sizeof(ie)) == -1)
201e2ba5731SMauro Carvalho Chehab	perror("set gain");
202e2ba5731SMauro Carvalho Chehab
203e2ba5731SMauro Carvalho ChehabEnabling/Disabling autocenter
204e2ba5731SMauro Carvalho Chehab-----------------------------
205e2ba5731SMauro Carvalho Chehab
206e2ba5731SMauro Carvalho ChehabThe autocenter feature quite disturbs the rendering of effects in my opinion,
207e2ba5731SMauro Carvalho Chehaband I think it should be an effect, which computation depends on the game
208e2ba5731SMauro Carvalho Chehabtype. But you can enable it if you want.
209e2ba5731SMauro Carvalho Chehab
210e2ba5731SMauro Carvalho Chehab::
211e2ba5731SMauro Carvalho Chehab
212e2ba5731SMauro Carvalho Chehab    int autocenter;		/* between 0 and 100 */
213e2ba5731SMauro Carvalho Chehab    struct input_event ie;
214e2ba5731SMauro Carvalho Chehab
215e2ba5731SMauro Carvalho Chehab    ie.type = EV_FF;
216e2ba5731SMauro Carvalho Chehab    ie.code = FF_AUTOCENTER;
217e2ba5731SMauro Carvalho Chehab    ie.value = 0xFFFFUL * autocenter / 100;
218e2ba5731SMauro Carvalho Chehab
219e2ba5731SMauro Carvalho Chehab    if (write(fd, &ie, sizeof(ie)) == -1)
220e2ba5731SMauro Carvalho Chehab	perror("set auto-center");
221e2ba5731SMauro Carvalho Chehab
222e2ba5731SMauro Carvalho ChehabA value of 0 means "no auto-center".
223e2ba5731SMauro Carvalho Chehab
224e2ba5731SMauro Carvalho ChehabDynamic update of an effect
225e2ba5731SMauro Carvalho Chehab---------------------------
226e2ba5731SMauro Carvalho Chehab
227e2ba5731SMauro Carvalho ChehabProceed as if you wanted to upload a new effect, except that instead of
228e2ba5731SMauro Carvalho Chehabsetting the id field to -1, you set it to the wanted effect id.
229e2ba5731SMauro Carvalho ChehabNormally, the effect is not stopped and restarted. However, depending on the
230e2ba5731SMauro Carvalho Chehabtype of device, not all parameters can be dynamically updated. For example,
231e2ba5731SMauro Carvalho Chehabthe direction of an effect cannot be updated with iforce devices. In this
232e2ba5731SMauro Carvalho Chehabcase, the driver stops the effect, up-load it, and restart it.
233e2ba5731SMauro Carvalho Chehab
234e2ba5731SMauro Carvalho ChehabTherefore it is recommended to dynamically change direction while the effect
235e2ba5731SMauro Carvalho Chehabis playing only when it is ok to restart the effect with a replay count of 1.
236e2ba5731SMauro Carvalho Chehab
237e2ba5731SMauro Carvalho ChehabInformation about the status of effects
238e2ba5731SMauro Carvalho Chehab---------------------------------------
239e2ba5731SMauro Carvalho Chehab
240e2ba5731SMauro Carvalho ChehabEvery time the status of an effect is changed, an event is sent. The values
241e2ba5731SMauro Carvalho Chehaband meanings of the fields of the event are as follows::
242e2ba5731SMauro Carvalho Chehab
243e2ba5731SMauro Carvalho Chehab    struct input_event {
244e2ba5731SMauro Carvalho Chehab    /* When the status of the effect changed */
245e2ba5731SMauro Carvalho Chehab	    struct timeval time;
246e2ba5731SMauro Carvalho Chehab
247e2ba5731SMauro Carvalho Chehab    /* Set to EV_FF_STATUS */
248e2ba5731SMauro Carvalho Chehab	    unsigned short type;
249e2ba5731SMauro Carvalho Chehab
250e2ba5731SMauro Carvalho Chehab    /* Contains the id of the effect */
251e2ba5731SMauro Carvalho Chehab	    unsigned short code;
252e2ba5731SMauro Carvalho Chehab
253e2ba5731SMauro Carvalho Chehab    /* Indicates the status */
254e2ba5731SMauro Carvalho Chehab	    unsigned int value;
255e2ba5731SMauro Carvalho Chehab    };
256e2ba5731SMauro Carvalho Chehab
257e2ba5731SMauro Carvalho Chehab    FF_STATUS_STOPPED	The effect stopped playing
258e2ba5731SMauro Carvalho Chehab    FF_STATUS_PLAYING	The effect started to play
259e2ba5731SMauro Carvalho Chehab
260e2ba5731SMauro Carvalho Chehab.. note::
261e2ba5731SMauro Carvalho Chehab
262e2ba5731SMauro Carvalho Chehab    - Status feedback is only supported by iforce driver. If you have
263e2ba5731SMauro Carvalho Chehab      a really good reason to use this, please contact
264e2ba5731SMauro Carvalho Chehab      linux-joystick@atrey.karlin.mff.cuni.cz or anssi.hannula@gmail.com
265e2ba5731SMauro Carvalho Chehab      so that support for it can be added to the rest of the drivers.
266