input-programming.txt 9.7 KB

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