blob: 26d461998e08974f1e47e54f33cfc630c0a9879d [file] [log] [blame]
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -07001========================
2Force feedback for Linux
3========================
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -07005:Author: Johann Deneux <johann.deneux@gmail.com> on 2001/04/22.
6:Updated: Anssi Hannula <anssi.hannula@gmail.com> on 2006/04/09.
7
Mauro Carvalho Chehabdeeb1e92017-04-04 21:42:54 -07008You may redistribute this file. Please remember to include shape.svg and
9interactive.svg as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070011Introduction
12~~~~~~~~~~~~
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014This document describes how to use force feedback devices under Linux. The
15goal is not to support these devices as if they were simple input-only devices
16(as it is already the case), but to really enable the rendering of force
17effects.
Anssi Hannula8b8277a2006-07-19 01:44:22 -040018This document only describes the force feedback part of the Linux input
19interface. Please read joystick.txt and input.txt before reading further this
20document.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070022Instructions to the user
23~~~~~~~~~~~~~~~~~~~~~~~~
24
Anssi Hannula8b8277a2006-07-19 01:44:22 -040025To enable force feedback, you have to:
26
271. have your kernel configured with evdev and a driver that supports your
28 device.
292. make sure evdev module is loaded and /dev/input/event* device files are
30 created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32Before you start, let me WARN you that some devices shake violently during the
33initialisation phase. This happens for example with my "AVB Top Shot Pegasus".
34To stop this annoying behaviour, move you joystick to its limits. Anyway, you
Anssi Hannula8b8277a2006-07-19 01:44:22 -040035should keep a hand on your device, in order to avoid it to break down if
Linus Torvalds1da177e2005-04-16 15:20:36 -070036something goes wrong.
37
Anssi Hannula8b8277a2006-07-19 01:44:22 -040038If you have a serial iforce device, you need to start inputattach. See
39joystick.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070041Does it work ?
42--------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070044There is an utility called fftest that will allow you to test the driver::
45
46 % fftest /dev/input/eventXX
47
48Instructions to the developer
49~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50
Anssi Hannula8b8277a2006-07-19 01:44:22 -040051All interactions are done using the event API. That is, you can use ioctl()
Linus Torvalds1da177e2005-04-16 15:20:36 -070052and write() on /dev/input/eventXX.
Anssi Hannula8b8277a2006-07-19 01:44:22 -040053This information is subject to change.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070055Querying device capabilities
56----------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070058::
59
60 #include <linux/input.h>
61 #include <sys/ioctl.h>
62
63 #define BITS_TO_LONGS(x) \
64 (((x) + 8 * sizeof (unsigned long) - 1) / (8 * sizeof (unsigned long)))
65 unsigned long features[BITS_TO_LONGS(FF_CNT)];
66 int ioctl(int file_descriptor, int request, unsigned long *features);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68"request" must be EVIOCGBIT(EV_FF, size of features array in bytes )
69
70Returns the features supported by the device. features is a bitfield with the
71following bits:
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073- FF_CONSTANT can render constant force effects
Anssi Hannula8b8277a2006-07-19 01:44:22 -040074- FF_PERIODIC can render periodic effects with the following waveforms:
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070075
Anssi Hannula8b8277a2006-07-19 01:44:22 -040076 - FF_SQUARE square waveform
77 - FF_TRIANGLE triangle waveform
78 - FF_SINE sine waveform
79 - FF_SAW_UP sawtooth up waveform
80 - FF_SAW_DOWN sawtooth down waveform
81 - FF_CUSTOM custom waveform
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083- FF_RAMP can render ramp effects
84- FF_SPRING can simulate the presence of a spring
Anssi Hannula8b8277a2006-07-19 01:44:22 -040085- FF_FRICTION can simulate friction
Linus Torvalds1da177e2005-04-16 15:20:36 -070086- FF_DAMPER can simulate damper effects
Anssi Hannula8b8277a2006-07-19 01:44:22 -040087- FF_RUMBLE rumble effects
Linus Torvalds1da177e2005-04-16 15:20:36 -070088- FF_INERTIA can simulate inertia
Anssi Hannula8b8277a2006-07-19 01:44:22 -040089- FF_GAIN gain is adjustable
90- FF_AUTOCENTER autocenter is adjustable
91
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070092.. note::
93
94 - In most cases you should use FF_PERIODIC instead of FF_RUMBLE. All
Anssi Hannula8b8277a2006-07-19 01:44:22 -040095 devices that support FF_RUMBLE support FF_PERIODIC (square, triangle,
96 sine) and the other way around.
97
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -070098 - The exact syntax FF_CUSTOM is undefined for the time being as no driver
Anssi Hannula8b8277a2006-07-19 01:44:22 -040099 supports it yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700101::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700103 int ioctl(int fd, EVIOCGEFFECTS, int *n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105Returns the number of effects the device can keep in its memory.
106
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700107Uploading effects to the device
108-------------------------------
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400109
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700110::
111
112 #include <linux/input.h>
113 #include <sys/ioctl.h>
114
115 int ioctl(int file_descriptor, int request, struct ff_effect *effect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117"request" must be EVIOCSFF.
118
119"effect" points to a structure describing the effect to upload. The effect is
120uploaded, but not played.
121The content of effect may be modified. In particular, its field "id" is set
122to the unique id assigned by the driver. This data is required for performing
123some operations (removing an effect, controlling the playback).
124This if field must be set to -1 by the user in order to tell the driver to
125allocate a new effect.
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400126
127Effects are file descriptor specific.
128
Linus Torvalds16a12fa2017-05-03 12:38:20 -0700129See <uapi/linux/input.h> for a description of the ff_effect struct. You
130should also find help in a few sketches, contained in files shape.svg
131and interactive.svg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Mauro Carvalho Chehabaeb899a2017-04-11 07:01:19 -0300133.. kernel-figure:: shape.svg
Mauro Carvalho Chehabdeeb1e92017-04-04 21:42:54 -0700134
135 Shape
136
Mauro Carvalho Chehabaeb899a2017-04-11 07:01:19 -0300137.. kernel-figure:: interactive.svg
Mauro Carvalho Chehabdeeb1e92017-04-04 21:42:54 -0700138
139 Interactive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700141
142Removing an effect from the device
143----------------------------------
144
145::
146
147 int ioctl(int fd, EVIOCRMFF, effect.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400149This makes room for new effects in the device's memory. Note that this also
150stops the effect if it was playing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700152Controlling the playback of effects
153-----------------------------------
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155Control of playing is done with write(). Below is an example:
156
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700157::
158
159 #include <linux/input.h>
160 #include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 struct input_event play;
163 struct input_event stop;
164 struct ff_effect effect;
165 int fd;
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700166 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 fd = open("/dev/input/eventXX", O_RDWR);
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700168 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Play three times */
170 play.type = EV_FF;
171 play.code = effect.id;
172 play.value = 3;
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 write(fd, (const void*) &play, sizeof(play));
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700175 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* Stop an effect */
177 stop.type = EV_FF;
178 stop.code = effect.id;
179 stop.value = 0;
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 write(fd, (const void*) &play, sizeof(stop));
182
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700183Setting the gain
184----------------
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186Not all devices have the same strength. Therefore, users should set a gain
187factor depending on how strong they want effects to be. This setting is
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400188persistent across access to the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700190::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700192 /* Set the gain of the device
193 int gain; /* between 0 and 100 */
194 struct input_event ie; /* structure used to communicate with the driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700196 ie.type = EV_FF;
197 ie.code = FF_GAIN;
198 ie.value = 0xFFFFUL * gain / 100;
199
200 if (write(fd, &ie, sizeof(ie)) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 perror("set gain");
202
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700203Enabling/Disabling autocenter
204-----------------------------
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206The autocenter feature quite disturbs the rendering of effects in my opinion,
207and I think it should be an effect, which computation depends on the game
208type. But you can enable it if you want.
209
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700210::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700212 int autocenter; /* between 0 and 100 */
213 struct input_event ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700215 ie.type = EV_FF;
216 ie.code = FF_AUTOCENTER;
217 ie.value = 0xFFFFUL * autocenter / 100;
218
219 if (write(fd, &ie, sizeof(ie)) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 perror("set auto-center");
221
222A value of 0 means "no auto-center".
223
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700224Dynamic update of an effect
225---------------------------
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227Proceed as if you wanted to upload a new effect, except that instead of
228setting the id field to -1, you set it to the wanted effect id.
229Normally, the effect is not stopped and restarted. However, depending on the
230type of device, not all parameters can be dynamically updated. For example,
231the direction of an effect cannot be updated with iforce devices. In this
232case, the driver stops the effect, up-load it, and restart it.
233
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400234Therefore it is recommended to dynamically change direction while the effect
235is playing only when it is ok to restart the effect with a replay count of 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700237Information about the status of effects
238---------------------------------------
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240Every time the status of an effect is changed, an event is sent. The values
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700241and meanings of the fields of the event are as follows::
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400242
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700243 struct input_event {
244 /* When the status of the effect changed */
245 struct timeval time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700247 /* Set to EV_FF_STATUS */
248 unsigned short type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700250 /* Contains the id of the effect */
251 unsigned short code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700253 /* Indicates the status */
254 unsigned int value;
255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700257 FF_STATUS_STOPPED The effect stopped playing
258 FF_STATUS_PLAYING The effect started to play
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400259
Mauro Carvalho Chehab3057d502017-04-04 17:40:14 -0700260.. note::
261
262 - Status feedback is only supported by iforce driver. If you have
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400263 a really good reason to use this, please contact
264 linux-joystick@atrey.karlin.mff.cuni.cz or anssi.hannula@gmail.com
265 so that support for it can be added to the rest of the drivers.