input.h 18 KB

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