blob: 81905e81585e2de04f9b80a658fb087013acd147 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001Programming input drivers
2~~~~~~~~~~~~~~~~~~~~~~~~~
3
41. Creating an input device driver
5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
71.0 The simplest example
8~~~~~~~~~~~~~~~~~~~~~~~~
9
10Here comes a very simple example of an input device driver. The device has
11just one button and the button is accessible at i/o port BUTTON_PORT. When
12pressed or released a BUTTON_IRQ happens. The driver could look like:
13
14#include <linux/input.h>
15#include <linux/module.h>
16#include <linux/init.h>
17
18#include <asm/irq.h>
19#include <asm/io.h>
20
Dmitry Torokhov85796e72007-04-29 23:42:08 -040021static struct input_dev *button_dev;
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static void button_interrupt(int irq, void *dummy, struct pt_regs *fp)
24{
Steven Whitehouse75570af2007-11-27 00:45:34 -050025 input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
Dmitry Torokhov85796e72007-04-29 23:42:08 -040026 input_sync(button_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
29static int __init button_init(void)
30{
Dmitry Torokhov85796e72007-04-29 23:42:08 -040031 int error;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
34 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
35 return -EBUSY;
36 }
Dmitry Torokhov85796e72007-04-29 23:42:08 -040037
38 button_dev = input_allocate_device();
39 if (!button_dev) {
40 printk(KERN_ERR "button.c: Not enough memory\n");
41 error = -ENOMEM;
42 goto err_free_irq;
43 }
44
Jiri Slaby7b19ada2007-10-18 23:40:32 -070045 button_dev->evbit[0] = BIT_MASK(EV_KEY);
46 button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
Dmitry Torokhov85796e72007-04-29 23:42:08 -040047
48 error = input_register_device(button_dev);
49 if (error) {
50 printk(KERN_ERR "button.c: Failed to register device\n");
51 goto err_free_dev;
52 }
53
54 return 0;
55
56 err_free_dev:
57 input_free_device(button_dev);
58 err_free_irq:
59 free_irq(BUTTON_IRQ, button_interrupt);
60 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63static void __exit button_exit(void)
64{
Dmitry Torokhov85796e72007-04-29 23:42:08 -040065 input_unregister_device(button_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 free_irq(BUTTON_IRQ, button_interrupt);
67}
68
69module_init(button_init);
70module_exit(button_exit);
71
721.1 What the example does
73~~~~~~~~~~~~~~~~~~~~~~~~~
74
75First it has to include the <linux/input.h> file, which interfaces to the
76input subsystem. This provides all the definitions needed.
77
78In the _init function, which is called either upon module load or when
79booting the kernel, it grabs the required resources (it should also check
80for the presence of the device).
81
Matt LaPlante01dd2fb2007-10-20 01:34:40 +020082Then it allocates a new input device structure with input_allocate_device()
Dmitry Torokhov85796e72007-04-29 23:42:08 -040083and sets up input bitfields. This way the device driver tells the other
Linus Torvalds1da177e2005-04-16 15:20:36 -070084parts of the input systems what it is - what events can be generated or
Dmitry Torokhov85796e72007-04-29 23:42:08 -040085accepted by this input device. Our example device can only generate EV_KEY
86type events, and from those only BTN_0 event code. Thus we only set these
87two bits. We could have used
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 set_bit(EV_KEY, button_dev.evbit);
90 set_bit(BTN_0, button_dev.keybit);
91
92as well, but with more than single bits the first approach tends to be
Dmitry Torokhov85796e72007-04-29 23:42:08 -040093shorter.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95Then the example driver registers the input device structure by calling
96
97 input_register_device(&button_dev);
98
99This adds the button_dev structure to linked lists of the input driver and
100calls device handler modules _connect functions to tell them a new input
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400101device has appeared. input_register_device() may sleep and therefore must
102not be called from an interrupt or with a spinlock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104While in use, the only used function of the driver is
105
106 button_interrupt()
107
108which upon every interrupt from the button checks its state and reports it
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400109via the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 input_report_key()
112
113call to the input system. There is no need to check whether the interrupt
114routine isn't reporting two same value events (press, press for example) to
115the input system, because the input_report_* functions check that
116themselves.
117
118Then there is the
119
120 input_sync()
121
122call to tell those who receive the events that we've sent a complete report.
123This doesn't seem important in the one button case, but is quite important
124for for example mouse movement, where you don't want the X and Y values
125to be interpreted separately, because that'd result in a different movement.
126
1271.2 dev->open() and dev->close()
128~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129
130In case the driver has to repeatedly poll the device, because it doesn't
131have an interrupt coming from it and the polling is too expensive to be done
132all the time, or if the device uses a valuable resource (eg. interrupt), it
133can use the open and close callback to know when it can stop polling or
134release the interrupt and when it must resume polling or grab the interrupt
135again. To do that, we would add this to our example driver:
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int button_open(struct input_dev *dev)
138{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
140 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EBUSY;
142 }
143
144 return 0;
145}
146
147static void button_close(struct input_dev *dev)
148{
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400149 free_irq(IRQ_AMIGA_VERTB, button_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static int __init button_init(void)
153{
154 ...
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400155 button_dev->open = button_open;
156 button_dev->close = button_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ...
158}
159
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400160Note that input core keeps track of number of users for the device and
161makes sure that dev->open() is called only when the first user connects
162to the device and that dev->close() is called when the very last user
163disconnects. Calls to both callbacks are serialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165The open() callback should return a 0 in case of success or any nonzero value
166in case of failure. The close() callback (which is void) must always succeed.
167
1681.3 Basic event types
169~~~~~~~~~~~~~~~~~~~~~
170
171The most simple event type is EV_KEY, which is used for keys and buttons.
172It's reported to the input system via:
173
174 input_report_key(struct input_dev *dev, int code, int value)
175
176See linux/input.h for the allowable values of code (from 0 to KEY_MAX).
177Value is interpreted as a truth value, ie any nonzero value means key
178pressed, zero value means key released. The input code generates events only
179in case the value is different from before.
180
181In addition to EV_KEY, there are two more basic event types: EV_REL and
182EV_ABS. They are used for relative and absolute values supplied by the
183device. A relative value may be for example a mouse movement in the X axis.
184The mouse reports it as a relative difference from the last position,
185because it doesn't have any absolute coordinate system to work in. Absolute
186events are namely for joysticks and digitizers - devices that do work in an
187absolute coordinate systems.
188
189Having the device report EV_REL buttons is as simple as with EV_KEY, simply
190set the corresponding bits and call the
191
192 input_report_rel(struct input_dev *dev, int code, int value)
193
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400194function. Events are generated only for nonzero value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196However EV_ABS requires a little special care. Before calling
197input_register_device, you have to fill additional fields in the input_dev
198struct for each absolute axis your device has. If our button device had also
199the ABS_X axis:
200
201 button_dev.absmin[ABS_X] = 0;
202 button_dev.absmax[ABS_X] = 255;
203 button_dev.absfuzz[ABS_X] = 4;
204 button_dev.absflat[ABS_X] = 8;
205
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400206Or, you can just say:
207
208 input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210This setting would be appropriate for a joystick X axis, with the minimum of
2110, maximum of 255 (which the joystick *must* be able to reach, no problem if
212it sometimes reports more, but it must be able to always reach the min and
213max values), with noise in the data up to +- 4, and with a center flat
214position of size 8.
215
216If you don't need absfuzz and absflat, you can set them to zero, which mean
217that the thing is precise and always returns to exactly the center position
218(if it has any).
219
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002201.4 BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221~~~~~~~~~~~~~~~~~~~~~~~~~~
222
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700223These three macros from bitops.h help some bitfield computations:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700225 BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
226 x bits
227 BIT_WORD(x) - returns the index in the array in longs for bit x
228 BIT_MASK(x) - returns the index in a long for bit x
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Dmitry Torokhov85796e72007-04-29 23:42:08 -04002301.5 The id* and name fields
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233The dev->name should be set before registering the input device by the input
234device driver. It's a string like 'Generic button device' containing a
235user friendly name of the device.
236
237The id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
238of the device. The bus IDs are defined in input.h. The vendor and device ids
239are defined in pci_ids.h, usb_ids.h and similar include files. These fields
240should be set by the input device driver before registering it.
241
242The idtype field can be used for specific information for the input device
243driver.
244
245The id and name fields can be passed to userland via the evdev interface.
246
Dmitry Torokhov85796e72007-04-29 23:42:08 -04002471.6 The keycode, keycodemax, keycodesize fields
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
249
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400250These three fields should be used by input devices that have dense keymaps.
251The keycode is an array used to map from scancodes to input system keycodes.
252The keycode max should contain the size of the array and keycodesize the
253size of each entry in it (in bytes).
254
255Userspace can query and alter current scancode to keycode mappings using
256EVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
257When a device has all 3 aforementioned fields filled in, the driver may
258rely on kernel's default implementation of setting and querying keycode
259mappings.
260
2611.7 dev->getkeycode() and dev->setkeycode()
262~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263getkeycode() and setkeycode() callbacks allow drivers to override default
264keycode/keycodesize/keycodemax mapping mechanism provided by input core
265and implement sparse keycode maps.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
2671.8 Key autorepeat
268~~~~~~~~~~~~~~~~~~
269
270... is simple. It is handled by the input.c module. Hardware autorepeat is
271not used, because it's not present in many devices and even where it is
272present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
273autorepeat for your device, just set EV_REP in dev->evbit. All will be
274handled by the input system.
275
2761.9 Other event types, handling output events
277~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278
279The other event types up to now are:
280
281EV_LED - used for the keyboard LEDs.
282EV_SND - used for keyboard beeps.
283
284They are very similar to for example key events, but they go in the other
285direction - from the system to the input device driver. If your input device
286driver can handle these events, it has to set the respective bits in evbit,
287*and* also the callback routine:
288
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400289 button_dev->event = button_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291int button_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
292{
293 if (type == EV_SND && code == SND_BELL) {
294 outb(value, BUTTON_BELL);
295 return 0;
296 }
297 return -1;
298}
299
300This callback routine can be called from an interrupt or a BH (although that
301isn't a rule), and thus must not sleep, and must not take too long to finish.