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