input.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. */
  111. struct input_dev {
  112. const char *name;
  113. const char *phys;
  114. const char *uniq;
  115. struct input_id id;
  116. unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
  117. unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
  118. unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
  119. unsigned long relbit[BITS_TO_LONGS(REL_CNT)];
  120. unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];
  121. unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];
  122. unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];
  123. unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
  124. unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
  125. unsigned long swbit[BITS_TO_LONGS(SW_CNT)];
  126. unsigned int hint_events_per_packet;
  127. unsigned int keycodemax;
  128. unsigned int keycodesize;
  129. void *keycode;
  130. int (*setkeycode)(struct input_dev *dev,
  131. const struct input_keymap_entry *ke,
  132. unsigned int *old_keycode);
  133. int (*getkeycode)(struct input_dev *dev,
  134. struct input_keymap_entry *ke);
  135. struct ff_device *ff;
  136. unsigned int repeat_key;
  137. struct timer_list timer;
  138. int rep[REP_CNT];
  139. struct input_mt *mt;
  140. struct input_absinfo *absinfo;
  141. unsigned long key[BITS_TO_LONGS(KEY_CNT)];
  142. unsigned long led[BITS_TO_LONGS(LED_CNT)];
  143. unsigned long snd[BITS_TO_LONGS(SND_CNT)];
  144. unsigned long sw[BITS_TO_LONGS(SW_CNT)];
  145. int (*open)(struct input_dev *dev);
  146. void (*close)(struct input_dev *dev);
  147. int (*flush)(struct input_dev *dev, struct file *file);
  148. int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  149. struct input_handle __rcu *grab;
  150. spinlock_t event_lock;
  151. struct mutex mutex;
  152. unsigned int users;
  153. bool going_away;
  154. struct device dev;
  155. struct list_head h_list;
  156. struct list_head node;
  157. unsigned int num_vals;
  158. unsigned int max_vals;
  159. struct input_value *vals;
  160. };
  161. #define to_input_dev(d) container_of(d, struct input_dev, dev)
  162. /*
  163. * Verify that we are in sync with input_device_id mod_devicetable.h #defines
  164. */
  165. #if EV_MAX != INPUT_DEVICE_ID_EV_MAX
  166. #error "EV_MAX and INPUT_DEVICE_ID_EV_MAX do not match"
  167. #endif
  168. #if KEY_MIN_INTERESTING != INPUT_DEVICE_ID_KEY_MIN_INTERESTING
  169. #error "KEY_MIN_INTERESTING and INPUT_DEVICE_ID_KEY_MIN_INTERESTING do not match"
  170. #endif
  171. #if KEY_MAX != INPUT_DEVICE_ID_KEY_MAX
  172. #error "KEY_MAX and INPUT_DEVICE_ID_KEY_MAX do not match"
  173. #endif
  174. #if REL_MAX != INPUT_DEVICE_ID_REL_MAX
  175. #error "REL_MAX and INPUT_DEVICE_ID_REL_MAX do not match"
  176. #endif
  177. #if ABS_MAX != INPUT_DEVICE_ID_ABS_MAX
  178. #error "ABS_MAX and INPUT_DEVICE_ID_ABS_MAX do not match"
  179. #endif
  180. #if MSC_MAX != INPUT_DEVICE_ID_MSC_MAX
  181. #error "MSC_MAX and INPUT_DEVICE_ID_MSC_MAX do not match"
  182. #endif
  183. #if LED_MAX != INPUT_DEVICE_ID_LED_MAX
  184. #error "LED_MAX and INPUT_DEVICE_ID_LED_MAX do not match"
  185. #endif
  186. #if SND_MAX != INPUT_DEVICE_ID_SND_MAX
  187. #error "SND_MAX and INPUT_DEVICE_ID_SND_MAX do not match"
  188. #endif
  189. #if FF_MAX != INPUT_DEVICE_ID_FF_MAX
  190. #error "FF_MAX and INPUT_DEVICE_ID_FF_MAX do not match"
  191. #endif
  192. #if SW_MAX != INPUT_DEVICE_ID_SW_MAX
  193. #error "SW_MAX and INPUT_DEVICE_ID_SW_MAX do not match"
  194. #endif
  195. #define INPUT_DEVICE_ID_MATCH_DEVICE \
  196. (INPUT_DEVICE_ID_MATCH_BUS | INPUT_DEVICE_ID_MATCH_VENDOR | INPUT_DEVICE_ID_MATCH_PRODUCT)
  197. #define INPUT_DEVICE_ID_MATCH_DEVICE_AND_VERSION \
  198. (INPUT_DEVICE_ID_MATCH_DEVICE | INPUT_DEVICE_ID_MATCH_VERSION)
  199. struct input_handle;
  200. /**
  201. * struct input_handler - implements one of interfaces for input devices
  202. * @private: driver-specific data
  203. * @event: event handler. This method is being called by input core with
  204. * interrupts disabled and dev->event_lock spinlock held and so
  205. * it may not sleep
  206. * @events: event sequence handler. This method is being called by
  207. * input core with interrupts disabled and dev->event_lock
  208. * spinlock held and so it may not sleep
  209. * @filter: similar to @event; separates normal event handlers from
  210. * "filters".
  211. * @match: called after comparing device's id with handler's id_table
  212. * to perform fine-grained matching between device and handler
  213. * @connect: called when attaching a handler to an input device
  214. * @disconnect: disconnects a handler from input device
  215. * @start: starts handler for given handle. This function is called by
  216. * input core right after connect() method and also when a process
  217. * that "grabbed" a device releases it
  218. * @legacy_minors: set to %true by drivers using legacy minor ranges
  219. * @minor: beginning of range of 32 legacy minors for devices this driver
  220. * can provide
  221. * @name: name of the handler, to be shown in /proc/bus/input/handlers
  222. * @id_table: pointer to a table of input_device_ids this driver can
  223. * handle
  224. * @h_list: list of input handles associated with the handler
  225. * @node: for placing the driver onto input_handler_list
  226. *
  227. * Input handlers attach to input devices and create input handles. There
  228. * are likely several handlers attached to any given input device at the
  229. * same time. All of them will get their copy of input event generated by
  230. * the device.
  231. *
  232. * The very same structure is used to implement input filters. Input core
  233. * allows filters to run first and will not pass event to regular handlers
  234. * if any of the filters indicate that the event should be filtered (by
  235. * returning %true from their filter() method).
  236. *
  237. * Note that input core serializes calls to connect() and disconnect()
  238. * methods.
  239. */
  240. struct input_handler {
  241. void *private;
  242. void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
  243. void (*events)(struct input_handle *handle,
  244. const struct input_value *vals, unsigned int count);
  245. bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
  246. bool (*match)(struct input_handler *handler, struct input_dev *dev);
  247. int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
  248. void (*disconnect)(struct input_handle *handle);
  249. void (*start)(struct input_handle *handle);
  250. bool legacy_minors;
  251. int minor;
  252. const char *name;
  253. const struct input_device_id *id_table;
  254. struct list_head h_list;
  255. struct list_head node;
  256. };
  257. /**
  258. * struct input_handle - links input device with an input handler
  259. * @private: handler-specific data
  260. * @open: counter showing whether the handle is 'open', i.e. should deliver
  261. * events from its device
  262. * @name: name given to the handle by handler that created it
  263. * @dev: input device the handle is attached to
  264. * @handler: handler that works with the device through this handle
  265. * @d_node: used to put the handle on device's list of attached handles
  266. * @h_node: used to put the handle on handler's list of handles from which
  267. * it gets events
  268. */
  269. struct input_handle {
  270. void *private;
  271. int open;
  272. const char *name;
  273. struct input_dev *dev;
  274. struct input_handler *handler;
  275. struct list_head d_node;
  276. struct list_head h_node;
  277. };
  278. struct input_dev *input_allocate_device(void);
  279. void input_free_device(struct input_dev *dev);
  280. static inline struct input_dev *input_get_device(struct input_dev *dev)
  281. {
  282. return dev ? to_input_dev(get_device(&dev->dev)) : NULL;
  283. }
  284. static inline void input_put_device(struct input_dev *dev)
  285. {
  286. if (dev)
  287. put_device(&dev->dev);
  288. }
  289. static inline void *input_get_drvdata(struct input_dev *dev)
  290. {
  291. return dev_get_drvdata(&dev->dev);
  292. }
  293. static inline void input_set_drvdata(struct input_dev *dev, void *data)
  294. {
  295. dev_set_drvdata(&dev->dev, data);
  296. }
  297. int __must_check input_register_device(struct input_dev *);
  298. void input_unregister_device(struct input_dev *);
  299. void input_reset_device(struct input_dev *);
  300. int __must_check input_register_handler(struct input_handler *);
  301. void input_unregister_handler(struct input_handler *);
  302. int __must_check input_get_new_minor(int legacy_base, unsigned int legacy_num,
  303. bool allow_dynamic);
  304. void input_free_minor(unsigned int minor);
  305. int input_handler_for_each_handle(struct input_handler *, void *data,
  306. int (*fn)(struct input_handle *, void *));
  307. int input_register_handle(struct input_handle *);
  308. void input_unregister_handle(struct input_handle *);
  309. int input_grab_device(struct input_handle *);
  310. void input_release_device(struct input_handle *);
  311. int input_open_device(struct input_handle *);
  312. void input_close_device(struct input_handle *);
  313. int input_flush_device(struct input_handle *handle, struct file *file);
  314. void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  315. void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value);
  316. static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
  317. {
  318. input_event(dev, EV_KEY, code, !!value);
  319. }
  320. static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value)
  321. {
  322. input_event(dev, EV_REL, code, value);
  323. }
  324. static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
  325. {
  326. input_event(dev, EV_ABS, code, value);
  327. }
  328. static inline void input_report_ff_status(struct input_dev *dev, unsigned int code, int value)
  329. {
  330. input_event(dev, EV_FF_STATUS, code, value);
  331. }
  332. static inline void input_report_switch(struct input_dev *dev, unsigned int code, int value)
  333. {
  334. input_event(dev, EV_SW, code, !!value);
  335. }
  336. static inline void input_sync(struct input_dev *dev)
  337. {
  338. input_event(dev, EV_SYN, SYN_REPORT, 0);
  339. }
  340. static inline void input_mt_sync(struct input_dev *dev)
  341. {
  342. input_event(dev, EV_SYN, SYN_MT_REPORT, 0);
  343. }
  344. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code);
  345. /**
  346. * input_set_events_per_packet - tell handlers about the driver event rate
  347. * @dev: the input device used by the driver
  348. * @n_events: the average number of events between calls to input_sync()
  349. *
  350. * If the event rate sent from a device is unusually large, use this
  351. * function to set the expected event rate. This will allow handlers
  352. * to set up an appropriate buffer size for the event stream, in order
  353. * to minimize information loss.
  354. */
  355. static inline void input_set_events_per_packet(struct input_dev *dev, int n_events)
  356. {
  357. dev->hint_events_per_packet = n_events;
  358. }
  359. void input_alloc_absinfo(struct input_dev *dev);
  360. void input_set_abs_params(struct input_dev *dev, unsigned int axis,
  361. int min, int max, int fuzz, int flat);
  362. #define INPUT_GENERATE_ABS_ACCESSORS(_suffix, _item) \
  363. static inline int input_abs_get_##_suffix(struct input_dev *dev, \
  364. unsigned int axis) \
  365. { \
  366. return dev->absinfo ? dev->absinfo[axis]._item : 0; \
  367. } \
  368. \
  369. static inline void input_abs_set_##_suffix(struct input_dev *dev, \
  370. unsigned int axis, int val) \
  371. { \
  372. input_alloc_absinfo(dev); \
  373. if (dev->absinfo) \
  374. dev->absinfo[axis]._item = val; \
  375. }
  376. INPUT_GENERATE_ABS_ACCESSORS(val, value)
  377. INPUT_GENERATE_ABS_ACCESSORS(min, minimum)
  378. INPUT_GENERATE_ABS_ACCESSORS(max, maximum)
  379. INPUT_GENERATE_ABS_ACCESSORS(fuzz, fuzz)
  380. INPUT_GENERATE_ABS_ACCESSORS(flat, flat)
  381. INPUT_GENERATE_ABS_ACCESSORS(res, resolution)
  382. int input_scancode_to_scalar(const struct input_keymap_entry *ke,
  383. unsigned int *scancode);
  384. int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke);
  385. int input_set_keycode(struct input_dev *dev,
  386. const struct input_keymap_entry *ke);
  387. extern struct class input_class;
  388. /**
  389. * struct ff_device - force-feedback part of an input device
  390. * @upload: Called to upload an new effect into device
  391. * @erase: Called to erase an effect from device
  392. * @playback: Called to request device to start playing specified effect
  393. * @set_gain: Called to set specified gain
  394. * @set_autocenter: Called to auto-center device
  395. * @destroy: called by input core when parent input device is being
  396. * destroyed
  397. * @private: driver-specific data, will be freed automatically
  398. * @ffbit: bitmap of force feedback capabilities truly supported by
  399. * device (not emulated like ones in input_dev->ffbit)
  400. * @mutex: mutex for serializing access to the device
  401. * @max_effects: maximum number of effects supported by device
  402. * @effects: pointer to an array of effects currently loaded into device
  403. * @effect_owners: array of effect owners; when file handle owning
  404. * an effect gets closed the effect is automatically erased
  405. *
  406. * Every force-feedback device must implement upload() and playback()
  407. * methods; erase() is optional. set_gain() and set_autocenter() need
  408. * only be implemented if driver sets up FF_GAIN and FF_AUTOCENTER
  409. * bits.
  410. *
  411. * Note that playback(), set_gain() and set_autocenter() are called with
  412. * dev->event_lock spinlock held and interrupts off and thus may not
  413. * sleep.
  414. */
  415. struct ff_device {
  416. int (*upload)(struct input_dev *dev, struct ff_effect *effect,
  417. struct ff_effect *old);
  418. int (*erase)(struct input_dev *dev, int effect_id);
  419. int (*playback)(struct input_dev *dev, int effect_id, int value);
  420. void (*set_gain)(struct input_dev *dev, u16 gain);
  421. void (*set_autocenter)(struct input_dev *dev, u16 magnitude);
  422. void (*destroy)(struct ff_device *);
  423. void *private;
  424. unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
  425. struct mutex mutex;
  426. int max_effects;
  427. struct ff_effect *effects;
  428. struct file *effect_owners[];
  429. };
  430. int input_ff_create(struct input_dev *dev, unsigned int max_effects);
  431. void input_ff_destroy(struct input_dev *dev);
  432. int input_ff_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  433. int input_ff_upload(struct input_dev *dev, struct ff_effect *effect, struct file *file);
  434. int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file);
  435. int input_ff_create_memless(struct input_dev *dev, void *data,
  436. int (*play_effect)(struct input_dev *, void *, struct ff_effect *));
  437. #endif