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