1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * Copyright (c) 1999-2002 Vojtech Pavlik
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9 #ifndef _INPUT_H
10 #define _INPUT_H
11 
12 
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include "standard-headers/linux/types.h"
16 
17 #include "standard-headers/linux/input-event-codes.h"
18 
19 /*
20  * The event structure itself
21  * Note that __USE_TIME_BITS64 is defined by libc based on
22  * application's request to use 64 bit time_t.
23  */
24 
25 struct input_event {
26 #if (HOST_LONG_BITS != 32 || !defined(__USE_TIME_BITS64)) && !defined(__KERNEL__)
27 	struct timeval time;
28 #define input_event_sec time.tv_sec
29 #define input_event_usec time.tv_usec
30 #else
31 	unsigned long __sec;
32 #if defined(__sparc__) && defined(__arch64__)
33 	unsigned int __usec;
34 	unsigned int __pad;
35 #else
36 	unsigned long __usec;
37 #endif
38 #define input_event_sec  __sec
39 #define input_event_usec __usec
40 #endif
41 	uint16_t type;
42 	uint16_t code;
43 	int32_t value;
44 };
45 
46 /*
47  * Protocol version.
48  */
49 
50 #define EV_VERSION		0x010001
51 
52 /*
53  * IOCTLs (0x00 - 0x7f)
54  */
55 
56 struct input_id {
57 	uint16_t bustype;
58 	uint16_t vendor;
59 	uint16_t product;
60 	uint16_t version;
61 };
62 
63 /**
64  * struct input_absinfo - used by EVIOCGABS/EVIOCSABS ioctls
65  * @value: latest reported value for the axis.
66  * @minimum: specifies minimum value for the axis.
67  * @maximum: specifies maximum value for the axis.
68  * @fuzz: specifies fuzz value that is used to filter noise from
69  *	the event stream.
70  * @flat: values that are within this value will be discarded by
71  *	joydev interface and reported as 0 instead.
72  * @resolution: specifies resolution for the values reported for
73  *	the axis.
74  *
75  * Note that input core does not clamp reported values to the
76  * [minimum, maximum] limits, such task is left to userspace.
77  *
78  * The default resolution for main axes (ABS_X, ABS_Y, ABS_Z,
79  * ABS_MT_POSITION_X, ABS_MT_POSITION_Y) is reported in units
80  * per millimeter (units/mm), resolution for rotational axes
81  * (ABS_RX, ABS_RY, ABS_RZ) is reported in units per radian.
82  * The resolution for the size axes (ABS_MT_TOUCH_MAJOR,
83  * ABS_MT_TOUCH_MINOR, ABS_MT_WIDTH_MAJOR, ABS_MT_WIDTH_MINOR)
84  * is reported in units per millimeter (units/mm).
85  * When INPUT_PROP_ACCELEROMETER is set the resolution changes.
86  * The main axes (ABS_X, ABS_Y, ABS_Z) are then reported in
87  * units per g (units/g) and in units per degree per second
88  * (units/deg/s) for rotational axes (ABS_RX, ABS_RY, ABS_RZ).
89  */
90 struct input_absinfo {
91 	int32_t value;
92 	int32_t minimum;
93 	int32_t maximum;
94 	int32_t fuzz;
95 	int32_t flat;
96 	int32_t resolution;
97 };
98 
99 /**
100  * struct input_keymap_entry - used by EVIOCGKEYCODE/EVIOCSKEYCODE ioctls
101  * @scancode: scancode represented in machine-endian form.
102  * @len: length of the scancode that resides in @scancode buffer.
103  * @index: index in the keymap, may be used instead of scancode
104  * @flags: allows to specify how kernel should handle the request. For
105  *	example, setting INPUT_KEYMAP_BY_INDEX flag indicates that kernel
106  *	should perform lookup in keymap by @index instead of @scancode
107  * @keycode: key code assigned to this scancode
108  *
109  * The structure is used to retrieve and modify keymap data. Users have
110  * option of performing lookup either by @scancode itself or by @index
111  * in keymap entry. EVIOCGKEYCODE will also return scancode or index
112  * (depending on which element was used to perform lookup).
113  */
114 struct input_keymap_entry {
115 #define INPUT_KEYMAP_BY_INDEX	(1 << 0)
116 	uint8_t  flags;
117 	uint8_t  len;
118 	uint16_t index;
119 	uint32_t keycode;
120 	uint8_t  scancode[32];
121 };
122 
123 struct input_mask {
124 	uint32_t type;
125 	uint32_t codes_size;
126 	uint64_t codes_ptr;
127 };
128 
129 #define EVIOCGVERSION		_IOR('E', 0x01, int)			/* get driver version */
130 #define EVIOCGID		_IOR('E', 0x02, struct input_id)	/* get device ID */
131 #define EVIOCGREP		_IOR('E', 0x03, unsigned int[2])	/* get repeat settings */
132 #define EVIOCSREP		_IOW('E', 0x03, unsigned int[2])	/* set repeat settings */
133 
134 #define EVIOCGKEYCODE		_IOR('E', 0x04, unsigned int[2])        /* get keycode */
135 #define EVIOCGKEYCODE_V2	_IOR('E', 0x04, struct input_keymap_entry)
136 #define EVIOCSKEYCODE		_IOW('E', 0x04, unsigned int[2])        /* set keycode */
137 #define EVIOCSKEYCODE_V2	_IOW('E', 0x04, struct input_keymap_entry)
138 
139 #define EVIOCGNAME(len)		_IOC(_IOC_READ, 'E', 0x06, len)		/* get device name */
140 #define EVIOCGPHYS(len)		_IOC(_IOC_READ, 'E', 0x07, len)		/* get physical location */
141 #define EVIOCGUNIQ(len)		_IOC(_IOC_READ, 'E', 0x08, len)		/* get unique identifier */
142 #define EVIOCGPROP(len)		_IOC(_IOC_READ, 'E', 0x09, len)		/* get device properties */
143 
144 /**
145  * EVIOCGMTSLOTS(len) - get MT slot values
146  * @len: size of the data buffer in bytes
147  *
148  * The ioctl buffer argument should be binary equivalent to
149  *
150  * struct input_mt_request_layout {
151  *	uint32_t code;
152  *	int32_t values[num_slots];
153  * };
154  *
155  * where num_slots is the (arbitrary) number of MT slots to extract.
156  *
157  * The ioctl size argument (len) is the size of the buffer, which
158  * should satisfy len = (num_slots + 1) * sizeof(int32_t).  If len is
159  * too small to fit all available slots, the first num_slots are
160  * returned.
161  *
162  * Before the call, code is set to the wanted ABS_MT event type. On
163  * return, values[] is filled with the slot values for the specified
164  * ABS_MT code.
165  *
166  * If the request code is not an ABS_MT value, -EINVAL is returned.
167  */
168 #define EVIOCGMTSLOTS(len)	_IOC(_IOC_READ, 'E', 0x0a, len)
169 
170 #define EVIOCGKEY(len)		_IOC(_IOC_READ, 'E', 0x18, len)		/* get global key state */
171 #define EVIOCGLED(len)		_IOC(_IOC_READ, 'E', 0x19, len)		/* get all LEDs */
172 #define EVIOCGSND(len)		_IOC(_IOC_READ, 'E', 0x1a, len)		/* get all sounds status */
173 #define EVIOCGSW(len)		_IOC(_IOC_READ, 'E', 0x1b, len)		/* get all switch states */
174 
175 #define EVIOCGBIT(ev,len)	_IOC(_IOC_READ, 'E', 0x20 + (ev), len)	/* get event bits */
176 #define EVIOCGABS(abs)		_IOR('E', 0x40 + (abs), struct input_absinfo)	/* get abs value/limits */
177 #define EVIOCSABS(abs)		_IOW('E', 0xc0 + (abs), struct input_absinfo)	/* set abs value/limits */
178 
179 #define EVIOCSFF		_IOW('E', 0x80, struct ff_effect)	/* send a force effect to a force feedback device */
180 #define EVIOCRMFF		_IOW('E', 0x81, int)			/* Erase a force effect */
181 #define EVIOCGEFFECTS		_IOR('E', 0x84, int)			/* Report number of effects playable at the same time */
182 
183 #define EVIOCGRAB		_IOW('E', 0x90, int)			/* Grab/Release device */
184 #define EVIOCREVOKE		_IOW('E', 0x91, int)			/* Revoke device access */
185 
186 /**
187  * EVIOCGMASK - Retrieve current event mask
188  *
189  * This ioctl allows user to retrieve the current event mask for specific
190  * event type. The argument must be of type "struct input_mask" and
191  * specifies the event type to query, the address of the receive buffer and
192  * the size of the receive buffer.
193  *
194  * The event mask is a per-client mask that specifies which events are
195  * forwarded to the client. Each event code is represented by a single bit
196  * in the event mask. If the bit is set, the event is passed to the client
197  * normally. Otherwise, the event is filtered and will never be queued on
198  * the client's receive buffer.
199  *
200  * Event masks do not affect global state of the input device. They only
201  * affect the file descriptor they are applied to.
202  *
203  * The default event mask for a client has all bits set, i.e. all events
204  * are forwarded to the client. If the kernel is queried for an unknown
205  * event type or if the receive buffer is larger than the number of
206  * event codes known to the kernel, the kernel returns all zeroes for those
207  * codes.
208  *
209  * At maximum, codes_size bytes are copied.
210  *
211  * This ioctl may fail with ENODEV in case the file is revoked, EFAULT
212  * if the receive-buffer points to invalid memory, or EINVAL if the kernel
213  * does not implement the ioctl.
214  */
215 #define EVIOCGMASK		_IOR('E', 0x92, struct input_mask)	/* Get event-masks */
216 
217 /**
218  * EVIOCSMASK - Set event mask
219  *
220  * This ioctl is the counterpart to EVIOCGMASK. Instead of receiving the
221  * current event mask, this changes the client's event mask for a specific
222  * type.  See EVIOCGMASK for a description of event-masks and the
223  * argument-type.
224  *
225  * This ioctl provides full forward compatibility. If the passed event type
226  * is unknown to the kernel, or if the number of event codes specified in
227  * the mask is bigger than what is known to the kernel, the ioctl is still
228  * accepted and applied. However, any unknown codes are left untouched and
229  * stay cleared. That means, the kernel always filters unknown codes
230  * regardless of what the client requests.  If the new mask doesn't cover
231  * all known event-codes, all remaining codes are automatically cleared and
232  * thus filtered.
233  *
234  * This ioctl may fail with ENODEV in case the file is revoked. EFAULT is
235  * returned if the receive-buffer points to invalid memory. EINVAL is returned
236  * if the kernel does not implement the ioctl.
237  */
238 #define EVIOCSMASK		_IOW('E', 0x93, struct input_mask)	/* Set event-masks */
239 
240 #define EVIOCSCLOCKID		_IOW('E', 0xa0, int)			/* Set clockid to be used for timestamps */
241 
242 /*
243  * IDs.
244  */
245 
246 #define ID_BUS			0
247 #define ID_VENDOR		1
248 #define ID_PRODUCT		2
249 #define ID_VERSION		3
250 
251 #define BUS_PCI			0x01
252 #define BUS_ISAPNP		0x02
253 #define BUS_USB			0x03
254 #define BUS_HIL			0x04
255 #define BUS_BLUETOOTH		0x05
256 #define BUS_VIRTUAL		0x06
257 
258 #define BUS_ISA			0x10
259 #define BUS_I8042		0x11
260 #define BUS_XTKBD		0x12
261 #define BUS_RS232		0x13
262 #define BUS_GAMEPORT		0x14
263 #define BUS_PARPORT		0x15
264 #define BUS_AMIGA		0x16
265 #define BUS_ADB			0x17
266 #define BUS_I2C			0x18
267 #define BUS_HOST		0x19
268 #define BUS_GSC			0x1A
269 #define BUS_ATARI		0x1B
270 #define BUS_SPI			0x1C
271 #define BUS_RMI			0x1D
272 #define BUS_CEC			0x1E
273 #define BUS_INTEL_ISHTP		0x1F
274 #define BUS_AMD_SFH		0x20
275 
276 /*
277  * MT_TOOL types
278  */
279 #define MT_TOOL_FINGER		0x00
280 #define MT_TOOL_PEN		0x01
281 #define MT_TOOL_PALM		0x02
282 #define MT_TOOL_DIAL		0x0a
283 #define MT_TOOL_MAX		0x0f
284 
285 /*
286  * Values describing the status of a force-feedback effect
287  */
288 #define FF_STATUS_STOPPED	0x00
289 #define FF_STATUS_PLAYING	0x01
290 #define FF_STATUS_MAX		0x01
291 
292 /*
293  * Structures used in ioctls to upload effects to a device
294  * They are pieces of a bigger structure (called ff_effect)
295  */
296 
297 /*
298  * All duration values are expressed in ms. Values above 32767 ms (0x7fff)
299  * should not be used and have unspecified results.
300  */
301 
302 /**
303  * struct ff_replay - defines scheduling of the force-feedback effect
304  * @length: duration of the effect
305  * @delay: delay before effect should start playing
306  */
307 struct ff_replay {
308 	uint16_t length;
309 	uint16_t delay;
310 };
311 
312 /**
313  * struct ff_trigger - defines what triggers the force-feedback effect
314  * @button: number of the button triggering the effect
315  * @interval: controls how soon the effect can be re-triggered
316  */
317 struct ff_trigger {
318 	uint16_t button;
319 	uint16_t interval;
320 };
321 
322 /**
323  * struct ff_envelope - generic force-feedback effect envelope
324  * @attack_length: duration of the attack (ms)
325  * @attack_level: level at the beginning of the attack
326  * @fade_length: duration of fade (ms)
327  * @fade_level: level at the end of fade
328  *
329  * The @attack_level and @fade_level are absolute values; when applying
330  * envelope force-feedback core will convert to positive/negative
331  * value based on polarity of the default level of the effect.
332  * Valid range for the attack and fade levels is 0x0000 - 0x7fff
333  */
334 struct ff_envelope {
335 	uint16_t attack_length;
336 	uint16_t attack_level;
337 	uint16_t fade_length;
338 	uint16_t fade_level;
339 };
340 
341 /**
342  * struct ff_constant_effect - defines parameters of a constant force-feedback effect
343  * @level: strength of the effect; may be negative
344  * @envelope: envelope data
345  */
346 struct ff_constant_effect {
347 	int16_t level;
348 	struct ff_envelope envelope;
349 };
350 
351 /**
352  * struct ff_ramp_effect - defines parameters of a ramp force-feedback effect
353  * @start_level: beginning strength of the effect; may be negative
354  * @end_level: final strength of the effect; may be negative
355  * @envelope: envelope data
356  */
357 struct ff_ramp_effect {
358 	int16_t start_level;
359 	int16_t end_level;
360 	struct ff_envelope envelope;
361 };
362 
363 /**
364  * struct ff_condition_effect - defines a spring or friction force-feedback effect
365  * @right_saturation: maximum level when joystick moved all way to the right
366  * @left_saturation: same for the left side
367  * @right_coeff: controls how fast the force grows when the joystick moves
368  *	to the right
369  * @left_coeff: same for the left side
370  * @deadband: size of the dead zone, where no force is produced
371  * @center: position of the dead zone
372  */
373 struct ff_condition_effect {
374 	uint16_t right_saturation;
375 	uint16_t left_saturation;
376 
377 	int16_t right_coeff;
378 	int16_t left_coeff;
379 
380 	uint16_t deadband;
381 	int16_t center;
382 };
383 
384 /**
385  * struct ff_periodic_effect - defines parameters of a periodic force-feedback effect
386  * @waveform: kind of the effect (wave)
387  * @period: period of the wave (ms)
388  * @magnitude: peak value
389  * @offset: mean value of the wave (roughly)
390  * @phase: 'horizontal' shift
391  * @envelope: envelope data
392  * @custom_len: number of samples (FF_CUSTOM only)
393  * @custom_data: buffer of samples (FF_CUSTOM only)
394  *
395  * Known waveforms - FF_SQUARE, FF_TRIANGLE, FF_SINE, FF_SAW_UP,
396  * FF_SAW_DOWN, FF_CUSTOM. The exact syntax FF_CUSTOM is undefined
397  * for the time being as no driver supports it yet.
398  *
399  * Note: the data pointed by custom_data is copied by the driver.
400  * You can therefore dispose of the memory after the upload/update.
401  */
402 struct ff_periodic_effect {
403 	uint16_t waveform;
404 	uint16_t period;
405 	int16_t magnitude;
406 	int16_t offset;
407 	uint16_t phase;
408 
409 	struct ff_envelope envelope;
410 
411 	uint32_t custom_len;
412 	int16_t *custom_data;
413 };
414 
415 /**
416  * struct ff_rumble_effect - defines parameters of a periodic force-feedback effect
417  * @strong_magnitude: magnitude of the heavy motor
418  * @weak_magnitude: magnitude of the light one
419  *
420  * Some rumble pads have two motors of different weight. Strong_magnitude
421  * represents the magnitude of the vibration generated by the heavy one.
422  */
423 struct ff_rumble_effect {
424 	uint16_t strong_magnitude;
425 	uint16_t weak_magnitude;
426 };
427 
428 /**
429  * struct ff_effect - defines force feedback effect
430  * @type: type of the effect (FF_CONSTANT, FF_PERIODIC, FF_RAMP, FF_SPRING,
431  *	FF_FRICTION, FF_DAMPER, FF_RUMBLE, FF_INERTIA, or FF_CUSTOM)
432  * @id: an unique id assigned to an effect
433  * @direction: direction of the effect
434  * @trigger: trigger conditions (struct ff_trigger)
435  * @replay: scheduling of the effect (struct ff_replay)
436  * @u: effect-specific structure (one of ff_constant_effect, ff_ramp_effect,
437  *	ff_periodic_effect, ff_condition_effect, ff_rumble_effect) further
438  *	defining effect parameters
439  *
440  * This structure is sent through ioctl from the application to the driver.
441  * To create a new effect application should set its @id to -1; the kernel
442  * will return assigned @id which can later be used to update or delete
443  * this effect.
444  *
445  * Direction of the effect is encoded as follows:
446  *	0 deg -> 0x0000 (down)
447  *	90 deg -> 0x4000 (left)
448  *	180 deg -> 0x8000 (up)
449  *	270 deg -> 0xC000 (right)
450  */
451 struct ff_effect {
452 	uint16_t type;
453 	int16_t id;
454 	uint16_t direction;
455 	struct ff_trigger trigger;
456 	struct ff_replay replay;
457 
458 	union {
459 		struct ff_constant_effect constant;
460 		struct ff_ramp_effect ramp;
461 		struct ff_periodic_effect periodic;
462 		struct ff_condition_effect condition[2]; /* One for each axis */
463 		struct ff_rumble_effect rumble;
464 	} u;
465 };
466 
467 /*
468  * Force feedback effect types
469  */
470 
471 #define FF_RUMBLE	0x50
472 #define FF_PERIODIC	0x51
473 #define FF_CONSTANT	0x52
474 #define FF_SPRING	0x53
475 #define FF_FRICTION	0x54
476 #define FF_DAMPER	0x55
477 #define FF_INERTIA	0x56
478 #define FF_RAMP		0x57
479 
480 #define FF_EFFECT_MIN	FF_RUMBLE
481 #define FF_EFFECT_MAX	FF_RAMP
482 
483 /*
484  * Force feedback periodic effect types
485  */
486 
487 #define FF_SQUARE	0x58
488 #define FF_TRIANGLE	0x59
489 #define FF_SINE		0x5a
490 #define FF_SAW_UP	0x5b
491 #define FF_SAW_DOWN	0x5c
492 #define FF_CUSTOM	0x5d
493 
494 #define FF_WAVEFORM_MIN	FF_SQUARE
495 #define FF_WAVEFORM_MAX	FF_CUSTOM
496 
497 /*
498  * Set ff device properties
499  */
500 
501 #define FF_GAIN		0x60
502 #define FF_AUTOCENTER	0x61
503 
504 /*
505  * ff->playback(effect_id = FF_GAIN) is the first effect_id to
506  * cause a collision with another ff method, in this case ff->set_gain().
507  * Therefore the greatest safe value for effect_id is FF_GAIN - 1,
508  * and thus the total number of effects should never exceed FF_GAIN.
509  */
510 #define FF_MAX_EFFECTS	FF_GAIN
511 
512 #define FF_MAX		0x7f
513 #define FF_CNT		(FF_MAX+1)
514 
515 #endif /* _INPUT_H */
516