blob: b3867bf49f8f9ec0743e3cc9384d8ed45449a972 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001Force feedback for Linux.
Johann Deneux118e78d2007-10-20 00:47:32 +02002By Johann Deneux <johann.deneux@gmail.com> on 2001/04/22.
Anssi Hannula8b8277a2006-07-19 01:44:22 -04003Updated by Anssi Hannula <anssi.hannula@gmail.com> on 2006/04/09.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004You may redistribute this file. Please remember to include shape.fig and
5interactive.fig as well.
6----------------------------------------------------------------------------
7
Anssi Hannula8b8277a2006-07-19 01:44:22 -040081. Introduction
Linus Torvalds1da177e2005-04-16 15:20:36 -07009~~~~~~~~~~~~~~~
10This document describes how to use force feedback devices under Linux. The
11goal is not to support these devices as if they were simple input-only devices
12(as it is already the case), but to really enable the rendering of force
13effects.
Anssi Hannula8b8277a2006-07-19 01:44:22 -040014This document only describes the force feedback part of the Linux input
15interface. Please read joystick.txt and input.txt before reading further this
16document.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
182. Instructions to the user
19~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anssi Hannula8b8277a2006-07-19 01:44:22 -040020To enable force feedback, you have to:
21
221. have your kernel configured with evdev and a driver that supports your
23 device.
242. make sure evdev module is loaded and /dev/input/event* device files are
25 created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27Before you start, let me WARN you that some devices shake violently during the
28initialisation phase. This happens for example with my "AVB Top Shot Pegasus".
29To stop this annoying behaviour, move you joystick to its limits. Anyway, you
Anssi Hannula8b8277a2006-07-19 01:44:22 -040030should keep a hand on your device, in order to avoid it to break down if
Linus Torvalds1da177e2005-04-16 15:20:36 -070031something goes wrong.
32
Anssi Hannula8b8277a2006-07-19 01:44:22 -040033If you have a serial iforce device, you need to start inputattach. See
34joystick.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
362.1 Does it work ?
37~~~~~~~~~~~~~~~~~~
38There is an utility called fftest that will allow you to test the driver.
39% fftest /dev/input/eventXX
40
Matt LaPlantefff92892006-10-03 22:47:42 +0200413. Instructions to the developer
Linus Torvalds1da177e2005-04-16 15:20:36 -070042~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anssi Hannula8b8277a2006-07-19 01:44:22 -040043All interactions are done using the event API. That is, you can use ioctl()
Linus Torvalds1da177e2005-04-16 15:20:36 -070044and write() on /dev/input/eventXX.
Anssi Hannula8b8277a2006-07-19 01:44:22 -040045This information is subject to change.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
473.1 Querying device capabilities
48~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49#include <linux/input.h>
50#include <sys/ioctl.h>
51
Alexander Steine1336bf2011-01-11 01:07:09 -080052#define BITS_TO_LONGS(x) \
53 (((x) + 8 * sizeof (unsigned long) - 1) / (8 * sizeof (unsigned long)))
54unsigned long features[BITS_TO_LONGS(FF_CNT)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055int ioctl(int file_descriptor, int request, unsigned long *features);
56
57"request" must be EVIOCGBIT(EV_FF, size of features array in bytes )
58
59Returns the features supported by the device. features is a bitfield with the
60following bits:
Linus Torvalds1da177e2005-04-16 15:20:36 -070061- FF_CONSTANT can render constant force effects
Anssi Hannula8b8277a2006-07-19 01:44:22 -040062- FF_PERIODIC can render periodic effects with the following waveforms:
63 - FF_SQUARE square waveform
64 - FF_TRIANGLE triangle waveform
65 - FF_SINE sine waveform
66 - FF_SAW_UP sawtooth up waveform
67 - FF_SAW_DOWN sawtooth down waveform
68 - FF_CUSTOM custom waveform
Linus Torvalds1da177e2005-04-16 15:20:36 -070069- FF_RAMP can render ramp effects
70- FF_SPRING can simulate the presence of a spring
Anssi Hannula8b8277a2006-07-19 01:44:22 -040071- FF_FRICTION can simulate friction
Linus Torvalds1da177e2005-04-16 15:20:36 -070072- FF_DAMPER can simulate damper effects
Anssi Hannula8b8277a2006-07-19 01:44:22 -040073- FF_RUMBLE rumble effects
Linus Torvalds1da177e2005-04-16 15:20:36 -070074- FF_INERTIA can simulate inertia
Anssi Hannula8b8277a2006-07-19 01:44:22 -040075- FF_GAIN gain is adjustable
76- FF_AUTOCENTER autocenter is adjustable
77
78Note: In most cases you should use FF_PERIODIC instead of FF_RUMBLE. All
79 devices that support FF_RUMBLE support FF_PERIODIC (square, triangle,
80 sine) and the other way around.
81
82Note: The exact syntax FF_CUSTOM is undefined for the time being as no driver
83 supports it yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85
86int ioctl(int fd, EVIOCGEFFECTS, int *n);
87
88Returns the number of effects the device can keep in its memory.
89
903.2 Uploading effects to the device
91~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92#include <linux/input.h>
93#include <sys/ioctl.h>
Anssi Hannula8b8277a2006-07-19 01:44:22 -040094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095int ioctl(int file_descriptor, int request, struct ff_effect *effect);
96
97"request" must be EVIOCSFF.
98
99"effect" points to a structure describing the effect to upload. The effect is
100uploaded, but not played.
101The content of effect may be modified. In particular, its field "id" is set
102to the unique id assigned by the driver. This data is required for performing
103some operations (removing an effect, controlling the playback).
104This if field must be set to -1 by the user in order to tell the driver to
105allocate a new effect.
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400106
107Effects are file descriptor specific.
108
Alexey Dobriyan323579882006-01-15 02:12:54 +0100109See <linux/input.h> for a description of the ff_effect struct. You should also
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110find help in a few sketches, contained in files shape.fig and interactive.fig.
111You need xfig to visualize these files.
112
1133.3 Removing an effect from the device
114~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115int ioctl(int fd, EVIOCRMFF, effect.id);
116
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400117This makes room for new effects in the device's memory. Note that this also
118stops the effect if it was playing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
1203.4 Controlling the playback of effects
121~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
122Control of playing is done with write(). Below is an example:
123
124#include <linux/input.h>
125#include <unistd.h>
126
127 struct input_event play;
128 struct input_event stop;
129 struct ff_effect effect;
130 int fd;
131...
132 fd = open("/dev/input/eventXX", O_RDWR);
133...
134 /* Play three times */
135 play.type = EV_FF;
136 play.code = effect.id;
137 play.value = 3;
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 write(fd, (const void*) &play, sizeof(play));
140...
141 /* Stop an effect */
142 stop.type = EV_FF;
143 stop.code = effect.id;
144 stop.value = 0;
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 write(fd, (const void*) &play, sizeof(stop));
147
1483.5 Setting the gain
149~~~~~~~~~~~~~~~~~~~~
150Not all devices have the same strength. Therefore, users should set a gain
151factor depending on how strong they want effects to be. This setting is
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400152persistent across access to the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/* Set the gain of the device
155int gain; /* between 0 and 100 */
156struct input_event ie; /* structure used to communicate with the driver */
157
158ie.type = EV_FF;
159ie.code = FF_GAIN;
160ie.value = 0xFFFFUL * gain / 100;
161
162if (write(fd, &ie, sizeof(ie)) == -1)
163 perror("set gain");
164
1653.6 Enabling/Disabling autocenter
166~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167The autocenter feature quite disturbs the rendering of effects in my opinion,
168and I think it should be an effect, which computation depends on the game
169type. But you can enable it if you want.
170
171int autocenter; /* between 0 and 100 */
172struct input_event ie;
173
174ie.type = EV_FF;
175ie.code = FF_AUTOCENTER;
176ie.value = 0xFFFFUL * autocenter / 100;
177
178if (write(fd, &ie, sizeof(ie)) == -1)
179 perror("set auto-center");
180
181A value of 0 means "no auto-center".
182
1833.7 Dynamic update of an effect
184~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185Proceed as if you wanted to upload a new effect, except that instead of
186setting the id field to -1, you set it to the wanted effect id.
187Normally, the effect is not stopped and restarted. However, depending on the
188type of device, not all parameters can be dynamically updated. For example,
189the direction of an effect cannot be updated with iforce devices. In this
190case, the driver stops the effect, up-load it, and restart it.
191
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400192Therefore it is recommended to dynamically change direction while the effect
193is playing only when it is ok to restart the effect with a replay count of 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
1953.8 Information about the status of effects
196~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197Every time the status of an effect is changed, an event is sent. The values
198and meanings of the fields of the event are as follows:
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200struct input_event {
201/* When the status of the effect changed */
202 struct timeval time;
203
204/* Set to EV_FF_STATUS */
205 unsigned short type;
206
207/* Contains the id of the effect */
208 unsigned short code;
209
210/* Indicates the status */
211 unsigned int value;
212};
213
214FF_STATUS_STOPPED The effect stopped playing
215FF_STATUS_PLAYING The effect started to play
Anssi Hannula8b8277a2006-07-19 01:44:22 -0400216
217NOTE: Status feedback is only supported by iforce driver. If you have
218 a really good reason to use this, please contact
219 linux-joystick@atrey.karlin.mff.cuni.cz or anssi.hannula@gmail.com
220 so that support for it can be added to the rest of the drivers.
221