xref: /linux-6.15/include/linux/input.h (revision 3b51c44b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 1999-2002 Vojtech Pavlik
4  */
5 #ifndef _INPUT_H
6 #define _INPUT_H
7 
8 #include <linux/time.h>
9 #include <linux/list.h>
10 #include <uapi/linux/input.h>
11 /* Implementation details, userspace should not care about these */
12 #define ABS_MT_FIRST		ABS_MT_TOUCH_MAJOR
13 #define ABS_MT_LAST		ABS_MT_TOOL_Y
14 
15 /*
16  * In-kernel definitions.
17  */
18 
19 #include <linux/device.h>
20 #include <linux/fs.h>
21 #include <linux/timer.h>
22 #include <linux/mod_devicetable.h>
23 
24 /**
25  * struct input_value - input value representation
26  * @type: type of value (EV_KEY, EV_ABS, etc)
27  * @code: the value code
28  * @value: the value
29  */
30 struct input_value {
31 	__u16 type;
32 	__u16 code;
33 	__s32 value;
34 };
35 
36 enum input_clock_type {
37 	INPUT_CLK_REAL = 0,
38 	INPUT_CLK_MONO,
39 	INPUT_CLK_BOOT,
40 	INPUT_CLK_MAX
41 };
42 
43 /**
44  * struct input_dev - represents an input device
45  * @name: name of the device
46  * @phys: physical path to the device in the system hierarchy
47  * @uniq: unique identification code for the device (if device has it)
48  * @id: id of the device (struct input_id)
49  * @propbit: bitmap of device properties and quirks
50  * @evbit: bitmap of types of events supported by the device (EV_KEY,
51  *	EV_REL, etc.)
52  * @keybit: bitmap of keys/buttons this device has
53  * @relbit: bitmap of relative axes for the device
54  * @absbit: bitmap of absolute axes for the device
55  * @mscbit: bitmap of miscellaneous events supported by the device
56  * @ledbit: bitmap of leds present on the device
57  * @sndbit: bitmap of sound effects supported by the device
58  * @ffbit: bitmap of force feedback effects supported by the device
59  * @swbit: bitmap of switches present on the device
60  * @hint_events_per_packet: average number of events generated by the
61  *	device in a packet (between EV_SYN/SYN_REPORT events). Used by
62  *	event handlers to estimate size of the buffer needed to hold
63  *	events.
64  * @keycodemax: size of keycode table
65  * @keycodesize: size of elements in keycode table
66  * @keycode: map of scancodes to keycodes for this device
67  * @getkeycode: optional legacy method to retrieve current keymap.
68  * @setkeycode: optional method to alter current keymap, used to implement
69  *	sparse keymaps. If not supplied default mechanism will be used.
70  *	The method is being called while holding event_lock and thus must
71  *	not sleep
72  * @ff: force feedback structure associated with the device if device
73  *	supports force feedback effects
74  * @repeat_key: stores key code of the last key pressed; used to implement
75  *	software autorepeat
76  * @timer: timer for software autorepeat
77  * @rep: current values for autorepeat parameters (delay, rate)
78  * @mt: pointer to multitouch state
79  * @absinfo: array of &struct input_absinfo elements holding information
80  *	about absolute axes (current value, min, max, flat, fuzz,
81  *	resolution)
82  * @key: reflects current state of device's keys/buttons
83  * @led: reflects current state of device's LEDs
84  * @snd: reflects current state of sound effects
85  * @sw: reflects current state of device's switches
86  * @open: this method is called when the very first user calls
87  *	input_open_device(). The driver must prepare the device
88  *	to start generating events (start polling thread,
89  *	request an IRQ, submit URB, etc.)
90  * @close: this method is called when the very last user calls
91  *	input_close_device().
92  * @flush: purges the device. Most commonly used to get rid of force
93  *	feedback effects loaded into the device when disconnecting
94  *	from it
95  * @event: event handler for events sent _to_ the device, like EV_LED
96  *	or EV_SND. The device is expected to carry out the requested
97  *	action (turn on a LED, play sound, etc.) The call is protected
98  *	by @event_lock and must not sleep
99  * @grab: input handle that currently has the device grabbed (via
100  *	EVIOCGRAB ioctl). When a handle grabs a device it becomes sole
101  *	recipient for all input events coming from the device
102  * @event_lock: this spinlock is taken when input core receives
103  *	and processes a new event for the device (in input_event()).
104  *	Code that accesses and/or modifies parameters of a device
105  *	(such as keymap or absmin, absmax, absfuzz, etc.) after device
106  *	has been registered with input core must take this lock.
107  * @mutex: serializes calls to open(), close() and flush() methods
108  * @users: stores number of users (input handlers) that opened this
109  *	device. It is used by input_open_device() and input_close_device()
110  *	to make sure that dev->open() is only called when the first
111  *	user opens device and dev->close() is called when the very
112  *	last user closes the device
113  * @going_away: marks devices that are in a middle of unregistering and
114  *	causes input_open_device*() fail with -ENODEV.
115  * @dev: driver model's view of this device
116  * @h_list: list of input handles associated with the device. When
117  *	accessing the list dev->mutex must be held
118  * @node: used to place the device onto input_dev_list
119  * @num_vals: number of values queued in the current frame
120  * @max_vals: maximum number of values queued in a frame
121  * @vals: array of values queued in the current frame
122  * @devres_managed: indicates that devices is managed with devres framework
123  *	and needs not be explicitly unregistered or freed.
124  * @timestamp: storage for a timestamp set by input_set_timestamp called
125  *  by a driver
126  */
127 struct input_dev {
128 	const char *name;
129 	const char *phys;
130 	const char *uniq;
131 	struct input_id id;
132 
133 	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
134 
135 	unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
136 	unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
137 	unsigned long relbit[BITS_TO_LONGS(REL_CNT)];
138 	unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];
139 	unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];
140 	unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];
141 	unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
142 	unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
143 	unsigned long swbit[BITS_TO_LONGS(SW_CNT)];
144 
145 	unsigned int hint_events_per_packet;
146 
147 	unsigned int keycodemax;
148 	unsigned int keycodesize;
149 	void *keycode;
150 
151 	int (*setkeycode)(struct input_dev *dev,
152 			  const struct input_keymap_entry *ke,
153 			  unsigned int *old_keycode);
154 	int (*getkeycode)(struct input_dev *dev,
155 			  struct input_keymap_entry *ke);
156 
157 	struct ff_device *ff;
158 
159 	unsigned int repeat_key;
160 	struct timer_list timer;
161 
162 	int rep[REP_CNT];
163 
164 	struct input_mt *mt;
165 
166 	struct input_absinfo *absinfo;
167 
168 	unsigned long key[BITS_TO_LONGS(KEY_CNT)];
169 	unsigned long led[BITS_TO_LONGS(LED_CNT)];
170 	unsigned long snd[BITS_TO_LONGS(SND_CNT)];
171 	unsigned long sw[BITS_TO_LONGS(SW_CNT)];
172 
173 	int (*open)(struct input_dev *dev);
174 	void (*close)(struct input_dev *dev);
175 	int (*flush)(struct input_dev *dev, struct file *file);
176 	int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
177 
178 	struct input_handle __rcu *grab;
179 
180 	spinlock_t event_lock;
181 	struct mutex mutex;
182 
183 	unsigned int users;
184 	bool going_away;
185 
186 	struct device dev;
187 
188 	struct list_head	h_list;
189 	struct list_head	node;
190 
191 	unsigned int num_vals;
192 	unsigned int max_vals;
193 	struct input_value *vals;
194 
195 	bool devres_managed;
196 
197 	ktime_t timestamp[INPUT_CLK_MAX];
198 };
199 #define to_input_dev(d) container_of(d, struct input_dev, dev)
200 
201 /*
202  * Verify that we are in sync with input_device_id mod_devicetable.h #defines
203  */
204 
205 #if EV_MAX != INPUT_DEVICE_ID_EV_MAX
206 #error "EV_MAX and INPUT_DEVICE_ID_EV_MAX do not match"
207 #endif
208 
209 #if KEY_MIN_INTERESTING != INPUT_DEVICE_ID_KEY_MIN_INTERESTING
210 #error "KEY_MIN_INTERESTING and INPUT_DEVICE_ID_KEY_MIN_INTERESTING do not match"
211 #endif
212 
213 #if KEY_MAX != INPUT_DEVICE_ID_KEY_MAX
214 #error "KEY_MAX and INPUT_DEVICE_ID_KEY_MAX do not match"
215 #endif
216 
217 #if REL_MAX != INPUT_DEVICE_ID_REL_MAX
218 #error "REL_MAX and INPUT_DEVICE_ID_REL_MAX do not match"
219 #endif
220 
221 #if ABS_MAX != INPUT_DEVICE_ID_ABS_MAX
222 #error "ABS_MAX and INPUT_DEVICE_ID_ABS_MAX do not match"
223 #endif
224 
225 #if MSC_MAX != INPUT_DEVICE_ID_MSC_MAX
226 #error "MSC_MAX and INPUT_DEVICE_ID_MSC_MAX do not match"
227 #endif
228 
229 #if LED_MAX != INPUT_DEVICE_ID_LED_MAX
230 #error "LED_MAX and INPUT_DEVICE_ID_LED_MAX do not match"
231 #endif
232 
233 #if SND_MAX != INPUT_DEVICE_ID_SND_MAX
234 #error "SND_MAX and INPUT_DEVICE_ID_SND_MAX do not match"
235 #endif
236 
237 #if FF_MAX != INPUT_DEVICE_ID_FF_MAX
238 #error "FF_MAX and INPUT_DEVICE_ID_FF_MAX do not match"
239 #endif
240 
241 #if SW_MAX != INPUT_DEVICE_ID_SW_MAX
242 #error "SW_MAX and INPUT_DEVICE_ID_SW_MAX do not match"
243 #endif
244 
245 #if INPUT_PROP_MAX != INPUT_DEVICE_ID_PROP_MAX
246 #error "INPUT_PROP_MAX and INPUT_DEVICE_ID_PROP_MAX do not match"
247 #endif
248 
249 #define INPUT_DEVICE_ID_MATCH_DEVICE \
250 	(INPUT_DEVICE_ID_MATCH_BUS | INPUT_DEVICE_ID_MATCH_VENDOR | INPUT_DEVICE_ID_MATCH_PRODUCT)
251 #define INPUT_DEVICE_ID_MATCH_DEVICE_AND_VERSION \
252 	(INPUT_DEVICE_ID_MATCH_DEVICE | INPUT_DEVICE_ID_MATCH_VERSION)
253 
254 struct input_handle;
255 
256 /**
257  * struct input_handler - implements one of interfaces for input devices
258  * @private: driver-specific data
259  * @event: event handler. This method is being called by input core with
260  *	interrupts disabled and dev->event_lock spinlock held and so
261  *	it may not sleep
262  * @events: event sequence handler. This method is being called by
263  *	input core with interrupts disabled and dev->event_lock
264  *	spinlock held and so it may not sleep
265  * @filter: similar to @event; separates normal event handlers from
266  *	"filters".
267  * @match: called after comparing device's id with handler's id_table
268  *	to perform fine-grained matching between device and handler
269  * @connect: called when attaching a handler to an input device
270  * @disconnect: disconnects a handler from input device
271  * @start: starts handler for given handle. This function is called by
272  *	input core right after connect() method and also when a process
273  *	that "grabbed" a device releases it
274  * @legacy_minors: set to %true by drivers using legacy minor ranges
275  * @minor: beginning of range of 32 legacy minors for devices this driver
276  *	can provide
277  * @name: name of the handler, to be shown in /proc/bus/input/handlers
278  * @id_table: pointer to a table of input_device_ids this driver can
279  *	handle
280  * @h_list: list of input handles associated with the handler
281  * @node: for placing the driver onto input_handler_list
282  *
283  * Input handlers attach to input devices and create input handles. There
284  * are likely several handlers attached to any given input device at the
285  * same time. All of them will get their copy of input event generated by
286  * the device.
287  *
288  * The very same structure is used to implement input filters. Input core
289  * allows filters to run first and will not pass event to regular handlers
290  * if any of the filters indicate that the event should be filtered (by
291  * returning %true from their filter() method).
292  *
293  * Note that input core serializes calls to connect() and disconnect()
294  * methods.
295  */
296 struct input_handler {
297 
298 	void *private;
299 
300 	void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
301 	void (*events)(struct input_handle *handle,
302 		       const struct input_value *vals, unsigned int count);
303 	bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
304 	bool (*match)(struct input_handler *handler, struct input_dev *dev);
305 	int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
306 	void (*disconnect)(struct input_handle *handle);
307 	void (*start)(struct input_handle *handle);
308 
309 	bool legacy_minors;
310 	int minor;
311 	const char *name;
312 
313 	const struct input_device_id *id_table;
314 
315 	struct list_head	h_list;
316 	struct list_head	node;
317 };
318 
319 /**
320  * struct input_handle - links input device with an input handler
321  * @private: handler-specific data
322  * @open: counter showing whether the handle is 'open', i.e. should deliver
323  *	events from its device
324  * @name: name given to the handle by handler that created it
325  * @dev: input device the handle is attached to
326  * @handler: handler that works with the device through this handle
327  * @d_node: used to put the handle on device's list of attached handles
328  * @h_node: used to put the handle on handler's list of handles from which
329  *	it gets events
330  */
331 struct input_handle {
332 
333 	void *private;
334 
335 	int open;
336 	const char *name;
337 
338 	struct input_dev *dev;
339 	struct input_handler *handler;
340 
341 	struct list_head	d_node;
342 	struct list_head	h_node;
343 };
344 
345 struct input_dev __must_check *input_allocate_device(void);
346 struct input_dev __must_check *devm_input_allocate_device(struct device *);
347 void input_free_device(struct input_dev *dev);
348 
349 static inline struct input_dev *input_get_device(struct input_dev *dev)
350 {
351 	return dev ? to_input_dev(get_device(&dev->dev)) : NULL;
352 }
353 
354 static inline void input_put_device(struct input_dev *dev)
355 {
356 	if (dev)
357 		put_device(&dev->dev);
358 }
359 
360 static inline void *input_get_drvdata(struct input_dev *dev)
361 {
362 	return dev_get_drvdata(&dev->dev);
363 }
364 
365 static inline void input_set_drvdata(struct input_dev *dev, void *data)
366 {
367 	dev_set_drvdata(&dev->dev, data);
368 }
369 
370 int __must_check input_register_device(struct input_dev *);
371 void input_unregister_device(struct input_dev *);
372 
373 void input_reset_device(struct input_dev *);
374 
375 int __must_check input_register_handler(struct input_handler *);
376 void input_unregister_handler(struct input_handler *);
377 
378 int __must_check input_get_new_minor(int legacy_base, unsigned int legacy_num,
379 				     bool allow_dynamic);
380 void input_free_minor(unsigned int minor);
381 
382 int input_handler_for_each_handle(struct input_handler *, void *data,
383 				  int (*fn)(struct input_handle *, void *));
384 
385 int input_register_handle(struct input_handle *);
386 void input_unregister_handle(struct input_handle *);
387 
388 int input_grab_device(struct input_handle *);
389 void input_release_device(struct input_handle *);
390 
391 int input_open_device(struct input_handle *);
392 void input_close_device(struct input_handle *);
393 
394 int input_flush_device(struct input_handle *handle, struct file *file);
395 
396 void input_set_timestamp(struct input_dev *dev, ktime_t timestamp);
397 ktime_t *input_get_timestamp(struct input_dev *dev);
398 
399 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
400 void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value);
401 
402 static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
403 {
404 	input_event(dev, EV_KEY, code, !!value);
405 }
406 
407 static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value)
408 {
409 	input_event(dev, EV_REL, code, value);
410 }
411 
412 static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
413 {
414 	input_event(dev, EV_ABS, code, value);
415 }
416 
417 static inline void input_report_ff_status(struct input_dev *dev, unsigned int code, int value)
418 {
419 	input_event(dev, EV_FF_STATUS, code, value);
420 }
421 
422 static inline void input_report_switch(struct input_dev *dev, unsigned int code, int value)
423 {
424 	input_event(dev, EV_SW, code, !!value);
425 }
426 
427 static inline void input_sync(struct input_dev *dev)
428 {
429 	input_event(dev, EV_SYN, SYN_REPORT, 0);
430 }
431 
432 static inline void input_mt_sync(struct input_dev *dev)
433 {
434 	input_event(dev, EV_SYN, SYN_MT_REPORT, 0);
435 }
436 
437 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code);
438 
439 /**
440  * input_set_events_per_packet - tell handlers about the driver event rate
441  * @dev: the input device used by the driver
442  * @n_events: the average number of events between calls to input_sync()
443  *
444  * If the event rate sent from a device is unusually large, use this
445  * function to set the expected event rate. This will allow handlers
446  * to set up an appropriate buffer size for the event stream, in order
447  * to minimize information loss.
448  */
449 static inline void input_set_events_per_packet(struct input_dev *dev, int n_events)
450 {
451 	dev->hint_events_per_packet = n_events;
452 }
453 
454 void input_alloc_absinfo(struct input_dev *dev);
455 void input_set_abs_params(struct input_dev *dev, unsigned int axis,
456 			  int min, int max, int fuzz, int flat);
457 
458 #define INPUT_GENERATE_ABS_ACCESSORS(_suffix, _item)			\
459 static inline int input_abs_get_##_suffix(struct input_dev *dev,	\
460 					  unsigned int axis)		\
461 {									\
462 	return dev->absinfo ? dev->absinfo[axis]._item : 0;		\
463 }									\
464 									\
465 static inline void input_abs_set_##_suffix(struct input_dev *dev,	\
466 					   unsigned int axis, int val)	\
467 {									\
468 	input_alloc_absinfo(dev);					\
469 	if (dev->absinfo)						\
470 		dev->absinfo[axis]._item = val;				\
471 }
472 
473 INPUT_GENERATE_ABS_ACCESSORS(val, value)
474 INPUT_GENERATE_ABS_ACCESSORS(min, minimum)
475 INPUT_GENERATE_ABS_ACCESSORS(max, maximum)
476 INPUT_GENERATE_ABS_ACCESSORS(fuzz, fuzz)
477 INPUT_GENERATE_ABS_ACCESSORS(flat, flat)
478 INPUT_GENERATE_ABS_ACCESSORS(res, resolution)
479 
480 int input_scancode_to_scalar(const struct input_keymap_entry *ke,
481 			     unsigned int *scancode);
482 
483 int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke);
484 int input_set_keycode(struct input_dev *dev,
485 		      const struct input_keymap_entry *ke);
486 
487 bool input_match_device_id(const struct input_dev *dev,
488 			   const struct input_device_id *id);
489 
490 void input_enable_softrepeat(struct input_dev *dev, int delay, int period);
491 
492 extern struct class input_class;
493 
494 /**
495  * struct ff_device - force-feedback part of an input device
496  * @upload: Called to upload an new effect into device
497  * @erase: Called to erase an effect from device
498  * @playback: Called to request device to start playing specified effect
499  * @set_gain: Called to set specified gain
500  * @set_autocenter: Called to auto-center device
501  * @destroy: called by input core when parent input device is being
502  *	destroyed
503  * @private: driver-specific data, will be freed automatically
504  * @ffbit: bitmap of force feedback capabilities truly supported by
505  *	device (not emulated like ones in input_dev->ffbit)
506  * @mutex: mutex for serializing access to the device
507  * @max_effects: maximum number of effects supported by device
508  * @effects: pointer to an array of effects currently loaded into device
509  * @effect_owners: array of effect owners; when file handle owning
510  *	an effect gets closed the effect is automatically erased
511  *
512  * Every force-feedback device must implement upload() and playback()
513  * methods; erase() is optional. set_gain() and set_autocenter() need
514  * only be implemented if driver sets up FF_GAIN and FF_AUTOCENTER
515  * bits.
516  *
517  * Note that playback(), set_gain() and set_autocenter() are called with
518  * dev->event_lock spinlock held and interrupts off and thus may not
519  * sleep.
520  */
521 struct ff_device {
522 	int (*upload)(struct input_dev *dev, struct ff_effect *effect,
523 		      struct ff_effect *old);
524 	int (*erase)(struct input_dev *dev, int effect_id);
525 
526 	int (*playback)(struct input_dev *dev, int effect_id, int value);
527 	void (*set_gain)(struct input_dev *dev, u16 gain);
528 	void (*set_autocenter)(struct input_dev *dev, u16 magnitude);
529 
530 	void (*destroy)(struct ff_device *);
531 
532 	void *private;
533 
534 	unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
535 
536 	struct mutex mutex;
537 
538 	int max_effects;
539 	struct ff_effect *effects;
540 	struct file *effect_owners[];
541 };
542 
543 int input_ff_create(struct input_dev *dev, unsigned int max_effects);
544 void input_ff_destroy(struct input_dev *dev);
545 
546 int input_ff_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
547 
548 int input_ff_upload(struct input_dev *dev, struct ff_effect *effect, struct file *file);
549 int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file);
550 int input_ff_flush(struct input_dev *dev, struct file *file);
551 
552 int input_ff_create_memless(struct input_dev *dev, void *data,
553 		int (*play_effect)(struct input_dev *, void *, struct ff_effect *));
554 
555 #endif
556