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