input-programming.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. Programming input drivers
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~
  3. 1. Creating an input device driver
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. 1.0 The simplest example
  6. ~~~~~~~~~~~~~~~~~~~~~~~~
  7. Here comes a very simple example of an input device driver. The device has
  8. just one button and the button is accessible at i/o port BUTTON_PORT. When
  9. pressed or released a BUTTON_IRQ happens. The driver could look like:
  10. #include <linux/input.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <asm/irq.h>
  14. #include <asm/io.h>
  15. static struct input_dev *button_dev;
  16. static irqreturn_t button_interrupt(int irq, void *dummy)
  17. {
  18. input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
  19. input_sync(button_dev);
  20. return IRQ_HANDLED;
  21. }
  22. static int __init button_init(void)
  23. {
  24. int error;
  25. if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
  26. printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
  27. return -EBUSY;
  28. }
  29. button_dev = input_allocate_device();
  30. if (!button_dev) {
  31. printk(KERN_ERR "button.c: Not enough memory\n");
  32. error = -ENOMEM;
  33. goto err_free_irq;
  34. }
  35. button_dev->evbit[0] = BIT_MASK(EV_KEY);
  36. button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
  37. error = input_register_device(button_dev);
  38. if (error) {
  39. printk(KERN_ERR "button.c: Failed to register device\n");
  40. goto err_free_dev;
  41. }
  42. return 0;
  43. err_free_dev:
  44. input_free_device(button_dev);
  45. err_free_irq:
  46. free_irq(BUTTON_IRQ, button_interrupt);
  47. return error;
  48. }
  49. static void __exit button_exit(void)
  50. {
  51. input_unregister_device(button_dev);
  52. free_irq(BUTTON_IRQ, button_interrupt);
  53. }
  54. module_init(button_init);
  55. module_exit(button_exit);
  56. 1.1 What the example does
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~
  58. First it has to include the <linux/input.h> file, which interfaces to the
  59. input subsystem. This provides all the definitions needed.
  60. In the _init function, which is called either upon module load or when
  61. booting the kernel, it grabs the required resources (it should also check
  62. for the presence of the device).
  63. Then it allocates a new input device structure with input_allocate_device()
  64. and sets up input bitfields. This way the device driver tells the other
  65. parts of the input systems what it is - what events can be generated or
  66. accepted by this input device. Our example device can only generate EV_KEY
  67. type events, and from those only BTN_0 event code. Thus we only set these
  68. two bits. We could have used
  69. set_bit(EV_KEY, button_dev.evbit);
  70. set_bit(BTN_0, button_dev.keybit);
  71. as well, but with more than single bits the first approach tends to be
  72. shorter.
  73. Then the example driver registers the input device structure by calling
  74. input_register_device(&button_dev);
  75. This adds the button_dev structure to linked lists of the input driver and
  76. calls device handler modules _connect functions to tell them a new input
  77. device has appeared. input_register_device() may sleep and therefore must
  78. not be called from an interrupt or with a spinlock held.
  79. While in use, the only used function of the driver is
  80. button_interrupt()
  81. which upon every interrupt from the button checks its state and reports it
  82. via the
  83. input_report_key()
  84. call to the input system. There is no need to check whether the interrupt
  85. routine isn't reporting two same value events (press, press for example) to
  86. the input system, because the input_report_* functions check that
  87. themselves.
  88. Then there is the
  89. input_sync()
  90. call to tell those who receive the events that we've sent a complete report.
  91. This doesn't seem important in the one button case, but is quite important
  92. for for example mouse movement, where you don't want the X and Y values
  93. to be interpreted separately, because that'd result in a different movement.
  94. 1.2 dev->open() and dev->close()
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. In case the driver has to repeatedly poll the device, because it doesn't
  97. have an interrupt coming from it and the polling is too expensive to be done
  98. all the time, or if the device uses a valuable resource (eg. interrupt), it
  99. can use the open and close callback to know when it can stop polling or
  100. release the interrupt and when it must resume polling or grab the interrupt
  101. again. To do that, we would add this to our example driver:
  102. static int button_open(struct input_dev *dev)
  103. {
  104. if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
  105. printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
  106. return -EBUSY;
  107. }
  108. return 0;
  109. }
  110. static void button_close(struct input_dev *dev)
  111. {
  112. free_irq(IRQ_AMIGA_VERTB, button_interrupt);
  113. }
  114. static int __init button_init(void)
  115. {
  116. ...
  117. button_dev->open = button_open;
  118. button_dev->close = button_close;
  119. ...
  120. }
  121. Note that input core keeps track of number of users for the device and
  122. makes sure that dev->open() is called only when the first user connects
  123. to the device and that dev->close() is called when the very last user
  124. disconnects. Calls to both callbacks are serialized.
  125. The open() callback should return a 0 in case of success or any nonzero value
  126. in case of failure. The close() callback (which is void) must always succeed.
  127. 1.3 Basic event types
  128. ~~~~~~~~~~~~~~~~~~~~~
  129. The most simple event type is EV_KEY, which is used for keys and buttons.
  130. It's reported to the input system via:
  131. input_report_key(struct input_dev *dev, int code, int value)
  132. See linux/input.h for the allowable values of code (from 0 to KEY_MAX).
  133. Value is interpreted as a truth value, ie any nonzero value means key
  134. pressed, zero value means key released. The input code generates events only
  135. in case the value is different from before.
  136. In addition to EV_KEY, there are two more basic event types: EV_REL and
  137. EV_ABS. They are used for relative and absolute values supplied by the
  138. device. A relative value may be for example a mouse movement in the X axis.
  139. The mouse reports it as a relative difference from the last position,
  140. because it doesn't have any absolute coordinate system to work in. Absolute
  141. events are namely for joysticks and digitizers - devices that do work in an
  142. absolute coordinate systems.
  143. Having the device report EV_REL buttons is as simple as with EV_KEY, simply
  144. set the corresponding bits and call the
  145. input_report_rel(struct input_dev *dev, int code, int value)
  146. function. Events are generated only for nonzero value.
  147. However EV_ABS requires a little special care. Before calling
  148. input_register_device, you have to fill additional fields in the input_dev
  149. struct for each absolute axis your device has. If our button device had also
  150. the ABS_X axis:
  151. button_dev.absmin[ABS_X] = 0;
  152. button_dev.absmax[ABS_X] = 255;
  153. button_dev.absfuzz[ABS_X] = 4;
  154. button_dev.absflat[ABS_X] = 8;
  155. Or, you can just say:
  156. input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
  157. This setting would be appropriate for a joystick X axis, with the minimum of
  158. 0, maximum of 255 (which the joystick *must* be able to reach, no problem if
  159. it sometimes reports more, but it must be able to always reach the min and
  160. max values), with noise in the data up to +- 4, and with a center flat
  161. position of size 8.
  162. If you don't need absfuzz and absflat, you can set them to zero, which mean
  163. that the thing is precise and always returns to exactly the center position
  164. (if it has any).
  165. 1.4 BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
  166. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  167. These three macros from bitops.h help some bitfield computations:
  168. BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
  169. x bits
  170. BIT_WORD(x) - returns the index in the array in longs for bit x
  171. BIT_MASK(x) - returns the index in a long for bit x
  172. 1.5 The id* and name fields
  173. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  174. The dev->name should be set before registering the input device by the input
  175. device driver. It's a string like 'Generic button device' containing a
  176. user friendly name of the device.
  177. The id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
  178. of the device. The bus IDs are defined in input.h. The vendor and device ids
  179. are defined in pci_ids.h, usb_ids.h and similar include files. These fields
  180. should be set by the input device driver before registering it.
  181. The idtype field can be used for specific information for the input device
  182. driver.
  183. The id and name fields can be passed to userland via the evdev interface.
  184. 1.6 The keycode, keycodemax, keycodesize fields
  185. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  186. These three fields should be used by input devices that have dense keymaps.
  187. The keycode is an array used to map from scancodes to input system keycodes.
  188. The keycode max should contain the size of the array and keycodesize the
  189. size of each entry in it (in bytes).
  190. Userspace can query and alter current scancode to keycode mappings using
  191. EVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
  192. When a device has all 3 aforementioned fields filled in, the driver may
  193. rely on kernel's default implementation of setting and querying keycode
  194. mappings.
  195. 1.7 dev->getkeycode() and dev->setkeycode()
  196. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  197. getkeycode() and setkeycode() callbacks allow drivers to override default
  198. keycode/keycodesize/keycodemax mapping mechanism provided by input core
  199. and implement sparse keycode maps.
  200. 1.8 Key autorepeat
  201. ~~~~~~~~~~~~~~~~~~
  202. ... is simple. It is handled by the input.c module. Hardware autorepeat is
  203. not used, because it's not present in many devices and even where it is
  204. present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
  205. autorepeat for your device, just set EV_REP in dev->evbit. All will be
  206. handled by the input system.
  207. 1.9 Other event types, handling output events
  208. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  209. The other event types up to now are:
  210. EV_LED - used for the keyboard LEDs.
  211. EV_SND - used for keyboard beeps.
  212. They are very similar to for example key events, but they go in the other
  213. direction - from the system to the input device driver. If your input device
  214. driver can handle these events, it has to set the respective bits in evbit,
  215. *and* also the callback routine:
  216. button_dev->event = button_event;
  217. int button_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  218. {
  219. if (type == EV_SND && code == SND_BELL) {
  220. outb(value, BUTTON_BELL);
  221. return 0;
  222. }
  223. return -1;
  224. }
  225. This callback routine can be called from an interrupt or a BH (although that
  226. isn't a rule), and thus must not sleep, and must not take too long to finish.