blob: 7f8b9d97bc47d32359150822eaa16b23acfc13be [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
Dmitri Vorobiev4f485442008-11-11 11:40:23 -050023static irqreturn_t button_interrupt(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
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);
Dmitri Vorobiev4f485442008-11-11 11:40:23 -050027 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
30static int __init button_init(void)
31{
Dmitry Torokhov85796e72007-04-29 23:42:08 -040032 int error;
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
35 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
36 return -EBUSY;
37 }
Dmitry Torokhov85796e72007-04-29 23:42:08 -040038
39 button_dev = input_allocate_device();
40 if (!button_dev) {
41 printk(KERN_ERR "button.c: Not enough memory\n");
42 error = -ENOMEM;
43 goto err_free_irq;
44 }
45
Jiri Slaby7b19ada2007-10-18 23:40:32 -070046 button_dev->evbit[0] = BIT_MASK(EV_KEY);
47 button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
Dmitry Torokhov85796e72007-04-29 23:42:08 -040048
49 error = input_register_device(button_dev);
50 if (error) {
51 printk(KERN_ERR "button.c: Failed to register device\n");
52 goto err_free_dev;
53 }
54
55 return 0;
56
57 err_free_dev:
58 input_free_device(button_dev);
59 err_free_irq:
60 free_irq(BUTTON_IRQ, button_interrupt);
61 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static void __exit button_exit(void)
65{
Dmitry Torokhov85796e72007-04-29 23:42:08 -040066 input_unregister_device(button_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 free_irq(BUTTON_IRQ, button_interrupt);
68}
69
70module_init(button_init);
71module_exit(button_exit);
72
731.1 What the example does
74~~~~~~~~~~~~~~~~~~~~~~~~~
75
76First it has to include the <linux/input.h> file, which interfaces to the
77input subsystem. This provides all the definitions needed.
78
79In the _init function, which is called either upon module load or when
80booting the kernel, it grabs the required resources (it should also check
81for the presence of the device).
82
Matt LaPlante01dd2fb2007-10-20 01:34:40 +020083Then it allocates a new input device structure with input_allocate_device()
Dmitry Torokhov85796e72007-04-29 23:42:08 -040084and sets up input bitfields. This way the device driver tells the other
Linus Torvalds1da177e2005-04-16 15:20:36 -070085parts of the input systems what it is - what events can be generated or
Dmitry Torokhov85796e72007-04-29 23:42:08 -040086accepted by this input device. Our example device can only generate EV_KEY
87type events, and from those only BTN_0 event code. Thus we only set these
88two bits. We could have used
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 set_bit(EV_KEY, button_dev.evbit);
91 set_bit(BTN_0, button_dev.keybit);
92
93as well, but with more than single bits the first approach tends to be
Dmitry Torokhov85796e72007-04-29 23:42:08 -040094shorter.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96Then the example driver registers the input device structure by calling
97
98 input_register_device(&button_dev);
99
100This adds the button_dev structure to linked lists of the input driver and
101calls device handler modules _connect functions to tell them a new input
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400102device has appeared. input_register_device() may sleep and therefore must
103not be called from an interrupt or with a spinlock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105While in use, the only used function of the driver is
106
107 button_interrupt()
108
109which upon every interrupt from the button checks its state and reports it
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400110via the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 input_report_key()
113
114call to the input system. There is no need to check whether the interrupt
115routine isn't reporting two same value events (press, press for example) to
116the input system, because the input_report_* functions check that
117themselves.
118
119Then there is the
120
121 input_sync()
122
123call to tell those who receive the events that we've sent a complete report.
124This doesn't seem important in the one button case, but is quite important
125for for example mouse movement, where you don't want the X and Y values
126to be interpreted separately, because that'd result in a different movement.
127
1281.2 dev->open() and dev->close()
129~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130
131In case the driver has to repeatedly poll the device, because it doesn't
132have an interrupt coming from it and the polling is too expensive to be done
133all the time, or if the device uses a valuable resource (eg. interrupt), it
134can use the open and close callback to know when it can stop polling or
135release the interrupt and when it must resume polling or grab the interrupt
136again. To do that, we would add this to our example driver:
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static int button_open(struct input_dev *dev)
139{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
141 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EBUSY;
143 }
144
145 return 0;
146}
147
148static void button_close(struct input_dev *dev)
149{
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400150 free_irq(IRQ_AMIGA_VERTB, button_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static int __init button_init(void)
154{
155 ...
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400156 button_dev->open = button_open;
157 button_dev->close = button_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 ...
159}
160
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400161Note that input core keeps track of number of users for the device and
162makes sure that dev->open() is called only when the first user connects
163to the device and that dev->close() is called when the very last user
164disconnects. Calls to both callbacks are serialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166The open() callback should return a 0 in case of success or any nonzero value
167in case of failure. The close() callback (which is void) must always succeed.
168
1691.3 Basic event types
170~~~~~~~~~~~~~~~~~~~~~
171
172The most simple event type is EV_KEY, which is used for keys and buttons.
173It's reported to the input system via:
174
175 input_report_key(struct input_dev *dev, int code, int value)
176
177See linux/input.h for the allowable values of code (from 0 to KEY_MAX).
178Value is interpreted as a truth value, ie any nonzero value means key
179pressed, zero value means key released. The input code generates events only
180in case the value is different from before.
181
182In addition to EV_KEY, there are two more basic event types: EV_REL and
183EV_ABS. They are used for relative and absolute values supplied by the
184device. A relative value may be for example a mouse movement in the X axis.
185The mouse reports it as a relative difference from the last position,
186because it doesn't have any absolute coordinate system to work in. Absolute
187events are namely for joysticks and digitizers - devices that do work in an
188absolute coordinate systems.
189
190Having the device report EV_REL buttons is as simple as with EV_KEY, simply
191set the corresponding bits and call the
192
193 input_report_rel(struct input_dev *dev, int code, int value)
194
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400195function. Events are generated only for nonzero value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197However EV_ABS requires a little special care. Before calling
198input_register_device, you have to fill additional fields in the input_dev
199struct for each absolute axis your device has. If our button device had also
200the ABS_X axis:
201
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;
206
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400207Or, you can just say:
208
209 input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211This setting would be appropriate for a joystick X axis, with the minimum of
2120, maximum of 255 (which the joystick *must* be able to reach, no problem if
213it sometimes reports more, but it must be able to always reach the min and
214max values), with noise in the data up to +- 4, and with a center flat
215position of size 8.
216
217If you don't need absfuzz and absflat, you can set them to zero, which mean
218that the thing is precise and always returns to exactly the center position
219(if it has any).
220
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002211.4 BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222~~~~~~~~~~~~~~~~~~~~~~~~~~
223
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700224These three macros from bitops.h help some bitfield computations:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700226 BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
227 x bits
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dmitry Torokhov85796e72007-04-29 23:42:08 -04002311.5 The id* and name fields
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234The dev->name should be set before registering the input device by the input
235device driver. It's a string like 'Generic button device' containing a
236user friendly name of the device.
237
238The id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
239of the device. The bus IDs are defined in input.h. The vendor and device ids
240are defined in pci_ids.h, usb_ids.h and similar include files. These fields
241should be set by the input device driver before registering it.
242
243The idtype field can be used for specific information for the input device
244driver.
245
246The id and name fields can be passed to userland via the evdev interface.
247
Dmitry Torokhov85796e72007-04-29 23:42:08 -04002481.6 The keycode, keycodemax, keycodesize fields
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400251These three fields should be used by input devices that have dense keymaps.
252The keycode is an array used to map from scancodes to input system keycodes.
253The keycode max should contain the size of the array and keycodesize the
254size of each entry in it (in bytes).
255
256Userspace can query and alter current scancode to keycode mappings using
257EVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
258When a device has all 3 aforementioned fields filled in, the driver may
259rely on kernel's default implementation of setting and querying keycode
260mappings.
261
2621.7 dev->getkeycode() and dev->setkeycode()
263~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
264getkeycode() and setkeycode() callbacks allow drivers to override default
265keycode/keycodesize/keycodemax mapping mechanism provided by input core
266and implement sparse keycode maps.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
2681.8 Key autorepeat
269~~~~~~~~~~~~~~~~~~
270
271... is simple. It is handled by the input.c module. Hardware autorepeat is
272not used, because it's not present in many devices and even where it is
273present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
274autorepeat for your device, just set EV_REP in dev->evbit. All will be
275handled by the input system.
276
2771.9 Other event types, handling output events
278~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
279
280The other event types up to now are:
281
282EV_LED - used for the keyboard LEDs.
283EV_SND - used for keyboard beeps.
284
285They are very similar to for example key events, but they go in the other
286direction - from the system to the input device driver. If your input device
287driver can handle these events, it has to set the respective bits in evbit,
288*and* also the callback routine:
289
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400290 button_dev->event = button_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292int button_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
293{
294 if (type == EV_SND && code == SND_BELL) {
295 outb(value, BUTTON_BELL);
296 return 0;
297 }
298 return -1;
299}
300
301This callback routine can be called from an interrupt or a BH (although that
302isn't a rule), and thus must not sleep, and must not take too long to finish.