1b08c118cSDmitry Torokhov===============================
2e2ba5731SMauro Carvalho ChehabCreating an input device driver
3e2ba5731SMauro Carvalho Chehab===============================
4e2ba5731SMauro Carvalho Chehab
5e2ba5731SMauro Carvalho ChehabThe simplest example
6e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~
7e2ba5731SMauro Carvalho Chehab
8e2ba5731SMauro Carvalho ChehabHere comes a very simple example of an input device driver. The device has
9e2ba5731SMauro Carvalho Chehabjust one button and the button is accessible at i/o port BUTTON_PORT. When
10e2ba5731SMauro Carvalho Chehabpressed or released a BUTTON_IRQ happens. The driver could look like::
11e2ba5731SMauro Carvalho Chehab
12e2ba5731SMauro Carvalho Chehab    #include <linux/input.h>
13e2ba5731SMauro Carvalho Chehab    #include <linux/module.h>
14e2ba5731SMauro Carvalho Chehab    #include <linux/init.h>
15e2ba5731SMauro Carvalho Chehab
16e2ba5731SMauro Carvalho Chehab    #include <asm/irq.h>
17e2ba5731SMauro Carvalho Chehab    #include <asm/io.h>
18e2ba5731SMauro Carvalho Chehab
19e2ba5731SMauro Carvalho Chehab    static struct input_dev *button_dev;
20e2ba5731SMauro Carvalho Chehab
21e2ba5731SMauro Carvalho Chehab    static irqreturn_t button_interrupt(int irq, void *dummy)
22e2ba5731SMauro Carvalho Chehab    {
23e2ba5731SMauro Carvalho Chehab	    input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
24e2ba5731SMauro Carvalho Chehab	    input_sync(button_dev);
25e2ba5731SMauro Carvalho Chehab	    return IRQ_HANDLED;
26e2ba5731SMauro Carvalho Chehab    }
27e2ba5731SMauro Carvalho Chehab
28e2ba5731SMauro Carvalho Chehab    static int __init button_init(void)
29e2ba5731SMauro Carvalho Chehab    {
30e2ba5731SMauro Carvalho Chehab	    int error;
31e2ba5731SMauro Carvalho Chehab
32e2ba5731SMauro Carvalho Chehab	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
33e2ba5731SMauro Carvalho Chehab		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
34e2ba5731SMauro Carvalho Chehab		    return -EBUSY;
35e2ba5731SMauro Carvalho Chehab	    }
36e2ba5731SMauro Carvalho Chehab
37e2ba5731SMauro Carvalho Chehab	    button_dev = input_allocate_device();
38e2ba5731SMauro Carvalho Chehab	    if (!button_dev) {
39e2ba5731SMauro Carvalho Chehab		    printk(KERN_ERR "button.c: Not enough memory\n");
40e2ba5731SMauro Carvalho Chehab		    error = -ENOMEM;
41e2ba5731SMauro Carvalho Chehab		    goto err_free_irq;
42e2ba5731SMauro Carvalho Chehab	    }
43e2ba5731SMauro Carvalho Chehab
44e2ba5731SMauro Carvalho Chehab	    button_dev->evbit[0] = BIT_MASK(EV_KEY);
45e2ba5731SMauro Carvalho Chehab	    button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
46e2ba5731SMauro Carvalho Chehab
47e2ba5731SMauro Carvalho Chehab	    error = input_register_device(button_dev);
48e2ba5731SMauro Carvalho Chehab	    if (error) {
49e2ba5731SMauro Carvalho Chehab		    printk(KERN_ERR "button.c: Failed to register device\n");
50e2ba5731SMauro Carvalho Chehab		    goto err_free_dev;
51e2ba5731SMauro Carvalho Chehab	    }
52e2ba5731SMauro Carvalho Chehab
53e2ba5731SMauro Carvalho Chehab	    return 0;
54e2ba5731SMauro Carvalho Chehab
55e2ba5731SMauro Carvalho Chehab    err_free_dev:
56e2ba5731SMauro Carvalho Chehab	    input_free_device(button_dev);
57e2ba5731SMauro Carvalho Chehab    err_free_irq:
58e2ba5731SMauro Carvalho Chehab	    free_irq(BUTTON_IRQ, button_interrupt);
59e2ba5731SMauro Carvalho Chehab	    return error;
60e2ba5731SMauro Carvalho Chehab    }
61e2ba5731SMauro Carvalho Chehab
62e2ba5731SMauro Carvalho Chehab    static void __exit button_exit(void)
63e2ba5731SMauro Carvalho Chehab    {
64e2ba5731SMauro Carvalho Chehab	    input_unregister_device(button_dev);
65e2ba5731SMauro Carvalho Chehab	    free_irq(BUTTON_IRQ, button_interrupt);
66e2ba5731SMauro Carvalho Chehab    }
67e2ba5731SMauro Carvalho Chehab
68e2ba5731SMauro Carvalho Chehab    module_init(button_init);
69e2ba5731SMauro Carvalho Chehab    module_exit(button_exit);
70e2ba5731SMauro Carvalho Chehab
71e2ba5731SMauro Carvalho ChehabWhat the example does
72e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~
73e2ba5731SMauro Carvalho Chehab
74e2ba5731SMauro Carvalho ChehabFirst it has to include the <linux/input.h> file, which interfaces to the
75e2ba5731SMauro Carvalho Chehabinput subsystem. This provides all the definitions needed.
76e2ba5731SMauro Carvalho Chehab
77e2ba5731SMauro Carvalho ChehabIn the _init function, which is called either upon module load or when
78e2ba5731SMauro Carvalho Chehabbooting the kernel, it grabs the required resources (it should also check
79e2ba5731SMauro Carvalho Chehabfor the presence of the device).
80e2ba5731SMauro Carvalho Chehab
81e2ba5731SMauro Carvalho ChehabThen it allocates a new input device structure with input_allocate_device()
82e2ba5731SMauro Carvalho Chehaband sets up input bitfields. This way the device driver tells the other
83e2ba5731SMauro Carvalho Chehabparts of the input systems what it is - what events can be generated or
84e2ba5731SMauro Carvalho Chehabaccepted by this input device. Our example device can only generate EV_KEY
85e2ba5731SMauro Carvalho Chehabtype events, and from those only BTN_0 event code. Thus we only set these
86e2ba5731SMauro Carvalho Chehabtwo bits. We could have used::
87e2ba5731SMauro Carvalho Chehab
88a27e51b4SNelson Penn	set_bit(EV_KEY, button_dev->evbit);
89a27e51b4SNelson Penn	set_bit(BTN_0, button_dev->keybit);
90e2ba5731SMauro Carvalho Chehab
91e2ba5731SMauro Carvalho Chehabas well, but with more than single bits the first approach tends to be
92e2ba5731SMauro Carvalho Chehabshorter.
93e2ba5731SMauro Carvalho Chehab
94e2ba5731SMauro Carvalho ChehabThen the example driver registers the input device structure by calling::
95e2ba5731SMauro Carvalho Chehab
96a27e51b4SNelson Penn	input_register_device(button_dev);
97e2ba5731SMauro Carvalho Chehab
98e2ba5731SMauro Carvalho ChehabThis adds the button_dev structure to linked lists of the input driver and
99e2ba5731SMauro Carvalho Chehabcalls device handler modules _connect functions to tell them a new input
100e2ba5731SMauro Carvalho Chehabdevice has appeared. input_register_device() may sleep and therefore must
101e2ba5731SMauro Carvalho Chehabnot be called from an interrupt or with a spinlock held.
102e2ba5731SMauro Carvalho Chehab
103e2ba5731SMauro Carvalho ChehabWhile in use, the only used function of the driver is::
104e2ba5731SMauro Carvalho Chehab
105e2ba5731SMauro Carvalho Chehab	button_interrupt()
106e2ba5731SMauro Carvalho Chehab
107e2ba5731SMauro Carvalho Chehabwhich upon every interrupt from the button checks its state and reports it
108e2ba5731SMauro Carvalho Chehabvia the::
109e2ba5731SMauro Carvalho Chehab
110e2ba5731SMauro Carvalho Chehab	input_report_key()
111e2ba5731SMauro Carvalho Chehab
112e2ba5731SMauro Carvalho Chehabcall to the input system. There is no need to check whether the interrupt
113e2ba5731SMauro Carvalho Chehabroutine isn't reporting two same value events (press, press for example) to
114e2ba5731SMauro Carvalho Chehabthe input system, because the input_report_* functions check that
115e2ba5731SMauro Carvalho Chehabthemselves.
116e2ba5731SMauro Carvalho Chehab
117e2ba5731SMauro Carvalho ChehabThen there is the::
118e2ba5731SMauro Carvalho Chehab
119e2ba5731SMauro Carvalho Chehab	input_sync()
120e2ba5731SMauro Carvalho Chehab
121e2ba5731SMauro Carvalho Chehabcall to tell those who receive the events that we've sent a complete report.
122e2ba5731SMauro Carvalho ChehabThis doesn't seem important in the one button case, but is quite important
1235c184115SRandy Dunlapfor example for mouse movement, where you don't want the X and Y values
124e2ba5731SMauro Carvalho Chehabto be interpreted separately, because that'd result in a different movement.
125e2ba5731SMauro Carvalho Chehab
126e2ba5731SMauro Carvalho Chehabdev->open() and dev->close()
127e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128e2ba5731SMauro Carvalho Chehab
129e2ba5731SMauro Carvalho ChehabIn case the driver has to repeatedly poll the device, because it doesn't
130e2ba5731SMauro Carvalho Chehabhave an interrupt coming from it and the polling is too expensive to be done
1315c184115SRandy Dunlapall the time, or if the device uses a valuable resource (e.g. interrupt), it
132e2ba5731SMauro Carvalho Chehabcan use the open and close callback to know when it can stop polling or
133e2ba5731SMauro Carvalho Chehabrelease the interrupt and when it must resume polling or grab the interrupt
134e2ba5731SMauro Carvalho Chehabagain. To do that, we would add this to our example driver::
135e2ba5731SMauro Carvalho Chehab
136e2ba5731SMauro Carvalho Chehab    static int button_open(struct input_dev *dev)
137e2ba5731SMauro Carvalho Chehab    {
138e2ba5731SMauro Carvalho Chehab	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
139e2ba5731SMauro Carvalho Chehab		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
140e2ba5731SMauro Carvalho Chehab		    return -EBUSY;
141e2ba5731SMauro Carvalho Chehab	    }
142e2ba5731SMauro Carvalho Chehab
143e2ba5731SMauro Carvalho Chehab	    return 0;
144e2ba5731SMauro Carvalho Chehab    }
145e2ba5731SMauro Carvalho Chehab
146e2ba5731SMauro Carvalho Chehab    static void button_close(struct input_dev *dev)
147e2ba5731SMauro Carvalho Chehab    {
148e2ba5731SMauro Carvalho Chehab	    free_irq(IRQ_AMIGA_VERTB, button_interrupt);
149e2ba5731SMauro Carvalho Chehab    }
150e2ba5731SMauro Carvalho Chehab
151e2ba5731SMauro Carvalho Chehab    static int __init button_init(void)
152e2ba5731SMauro Carvalho Chehab    {
153e2ba5731SMauro Carvalho Chehab	    ...
154e2ba5731SMauro Carvalho Chehab	    button_dev->open = button_open;
155e2ba5731SMauro Carvalho Chehab	    button_dev->close = button_close;
156e2ba5731SMauro Carvalho Chehab	    ...
157e2ba5731SMauro Carvalho Chehab    }
158e2ba5731SMauro Carvalho Chehab
159e2ba5731SMauro Carvalho ChehabNote that input core keeps track of number of users for the device and
160e2ba5731SMauro Carvalho Chehabmakes sure that dev->open() is called only when the first user connects
161e2ba5731SMauro Carvalho Chehabto the device and that dev->close() is called when the very last user
162e2ba5731SMauro Carvalho Chehabdisconnects. Calls to both callbacks are serialized.
163e2ba5731SMauro Carvalho Chehab
1645c184115SRandy DunlapThe open() callback should return a 0 in case of success or any non-zero value
165e2ba5731SMauro Carvalho Chehabin case of failure. The close() callback (which is void) must always succeed.
166e2ba5731SMauro Carvalho Chehab
1676d59224fSAndrzej PietrasiewiczInhibiting input devices
1686d59224fSAndrzej Pietrasiewicz~~~~~~~~~~~~~~~~~~~~~~~~
1696d59224fSAndrzej Pietrasiewicz
1706d59224fSAndrzej PietrasiewiczInhibiting a device means ignoring input events from it. As such it is about
1716d59224fSAndrzej Pietrasiewiczmaintaining relationships with input handlers - either already existing
1726d59224fSAndrzej Pietrasiewiczrelationships, or relationships to be established while the device is in
1736d59224fSAndrzej Pietrasiewiczinhibited state.
1746d59224fSAndrzej Pietrasiewicz
1756d59224fSAndrzej PietrasiewiczIf a device is inhibited, no input handler will receive events from it.
1766d59224fSAndrzej Pietrasiewicz
1776d59224fSAndrzej PietrasiewiczThe fact that nobody wants events from the device is exploited further, by
1786d59224fSAndrzej Pietrasiewiczcalling device's close() (if there are users) and open() (if there are users) on
1796d59224fSAndrzej Pietrasiewiczinhibit and uninhibit operations, respectively. Indeed, the meaning of close()
1806d59224fSAndrzej Pietrasiewiczis to stop providing events to the input core and that of open() is to start
1816d59224fSAndrzej Pietrasiewiczproviding events to the input core.
1826d59224fSAndrzej Pietrasiewicz
1836d59224fSAndrzej PietrasiewiczCalling the device's close() method on inhibit (if there are users) allows the
1846d59224fSAndrzej Pietrasiewiczdriver to save power. Either by directly powering down the device or by
1855c184115SRandy Dunlapreleasing the runtime-PM reference it got in open() when the driver is using
1865c184115SRandy Dunlapruntime-PM.
1876d59224fSAndrzej Pietrasiewicz
1886d59224fSAndrzej PietrasiewiczInhibiting and uninhibiting are orthogonal to opening and closing the device by
1896d59224fSAndrzej Pietrasiewiczinput handlers. Userspace might want to inhibit a device in anticipation before
1906d59224fSAndrzej Pietrasiewiczany handler is positively matched against it.
1916d59224fSAndrzej Pietrasiewicz
1926d59224fSAndrzej PietrasiewiczInhibiting and uninhibiting are orthogonal to device's being a wakeup source,
1936d59224fSAndrzej Pietrasiewicztoo. Being a wakeup source plays a role when the system is sleeping, not when
1946d59224fSAndrzej Pietrasiewiczthe system is operating.  How drivers should program their interaction between
1956d59224fSAndrzej Pietrasiewiczinhibiting, sleeping and being a wakeup source is driver-specific.
1966d59224fSAndrzej Pietrasiewicz
1976d59224fSAndrzej PietrasiewiczTaking the analogy with the network devices - bringing a network interface down
1986d59224fSAndrzej Pietrasiewiczdoesn't mean that it should be impossible be wake the system up on LAN through
1996d59224fSAndrzej Pietrasiewiczthis interface. So, there may be input drivers which should be considered wakeup
2006d59224fSAndrzej Pietrasiewiczsources even when inhibited. Actually, in many I2C input devices their interrupt
2016d59224fSAndrzej Pietrasiewiczis declared a wakeup interrupt and its handling happens in driver's core, which
2026d59224fSAndrzej Pietrasiewiczis not aware of input-specific inhibit (nor should it be).  Composite devices
2036d59224fSAndrzej Pietrasiewiczcontaining several interfaces can be inhibited on a per-interface basis and e.g.
2046d59224fSAndrzej Pietrasiewiczinhibiting one interface shouldn't affect the device's capability of being a
2056d59224fSAndrzej Pietrasiewiczwakeup source.
2066d59224fSAndrzej Pietrasiewicz
2076d59224fSAndrzej PietrasiewiczIf a device is to be considered a wakeup source while inhibited, special care
2086d59224fSAndrzej Pietrasiewiczmust be taken when programming its suspend(), as it might need to call device's
2096d59224fSAndrzej Pietrasiewiczopen(). Depending on what close() means for the device in question, not
2106d59224fSAndrzej Pietrasiewiczopening() it before going to sleep might make it impossible to provide any
2116d59224fSAndrzej Pietrasiewiczwakeup events. The device is going to sleep anyway.
2126d59224fSAndrzej Pietrasiewicz
213e2ba5731SMauro Carvalho ChehabBasic event types
214e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~
215e2ba5731SMauro Carvalho Chehab
216e2ba5731SMauro Carvalho ChehabThe most simple event type is EV_KEY, which is used for keys and buttons.
217e2ba5731SMauro Carvalho ChehabIt's reported to the input system via::
218e2ba5731SMauro Carvalho Chehab
219e2ba5731SMauro Carvalho Chehab	input_report_key(struct input_dev *dev, int code, int value)
220e2ba5731SMauro Carvalho Chehab
22116a12fa9SLinus TorvaldsSee uapi/linux/input-event-codes.h for the allowable values of code (from 0 to
2225c184115SRandy DunlapKEY_MAX). Value is interpreted as a truth value, i.e. any non-zero value means
2235c184115SRandy Dunlapkey pressed, zero value means key released. The input code generates events only
224e2ba5731SMauro Carvalho Chehabin case the value is different from before.
225e2ba5731SMauro Carvalho Chehab
226e2ba5731SMauro Carvalho ChehabIn addition to EV_KEY, there are two more basic event types: EV_REL and
227e2ba5731SMauro Carvalho ChehabEV_ABS. They are used for relative and absolute values supplied by the
228e2ba5731SMauro Carvalho Chehabdevice. A relative value may be for example a mouse movement in the X axis.
229e2ba5731SMauro Carvalho ChehabThe mouse reports it as a relative difference from the last position,
230e2ba5731SMauro Carvalho Chehabbecause it doesn't have any absolute coordinate system to work in. Absolute
231e2ba5731SMauro Carvalho Chehabevents are namely for joysticks and digitizers - devices that do work in an
232e2ba5731SMauro Carvalho Chehababsolute coordinate systems.
233e2ba5731SMauro Carvalho Chehab
2345c184115SRandy DunlapHaving the device report EV_REL buttons is as simple as with EV_KEY; simply
235e2ba5731SMauro Carvalho Chehabset the corresponding bits and call the::
236e2ba5731SMauro Carvalho Chehab
237e2ba5731SMauro Carvalho Chehab	input_report_rel(struct input_dev *dev, int code, int value)
238e2ba5731SMauro Carvalho Chehab
2395c184115SRandy Dunlapfunction. Events are generated only for non-zero values.
240e2ba5731SMauro Carvalho Chehab
241e2ba5731SMauro Carvalho ChehabHowever EV_ABS requires a little special care. Before calling
242e2ba5731SMauro Carvalho Chehabinput_register_device, you have to fill additional fields in the input_dev
243e2ba5731SMauro Carvalho Chehabstruct for each absolute axis your device has. If our button device had also
244e2ba5731SMauro Carvalho Chehabthe ABS_X axis::
245e2ba5731SMauro Carvalho Chehab
246e2ba5731SMauro Carvalho Chehab	button_dev.absmin[ABS_X] = 0;
247e2ba5731SMauro Carvalho Chehab	button_dev.absmax[ABS_X] = 255;
248e2ba5731SMauro Carvalho Chehab	button_dev.absfuzz[ABS_X] = 4;
249e2ba5731SMauro Carvalho Chehab	button_dev.absflat[ABS_X] = 8;
250e2ba5731SMauro Carvalho Chehab
251e2ba5731SMauro Carvalho ChehabOr, you can just say::
252e2ba5731SMauro Carvalho Chehab
253e2ba5731SMauro Carvalho Chehab	input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
254e2ba5731SMauro Carvalho Chehab
255e2ba5731SMauro Carvalho ChehabThis setting would be appropriate for a joystick X axis, with the minimum of
256e2ba5731SMauro Carvalho Chehab0, maximum of 255 (which the joystick *must* be able to reach, no problem if
257e2ba5731SMauro Carvalho Chehabit sometimes reports more, but it must be able to always reach the min and
258e2ba5731SMauro Carvalho Chehabmax values), with noise in the data up to +- 4, and with a center flat
259e2ba5731SMauro Carvalho Chehabposition of size 8.
260e2ba5731SMauro Carvalho Chehab
261e2ba5731SMauro Carvalho ChehabIf you don't need absfuzz and absflat, you can set them to zero, which mean
262e2ba5731SMauro Carvalho Chehabthat the thing is precise and always returns to exactly the center position
263e2ba5731SMauro Carvalho Chehab(if it has any).
264e2ba5731SMauro Carvalho Chehab
265e2ba5731SMauro Carvalho ChehabBITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
266e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
267e2ba5731SMauro Carvalho Chehab
268e2ba5731SMauro Carvalho ChehabThese three macros from bitops.h help some bitfield computations::
269e2ba5731SMauro Carvalho Chehab
270e2ba5731SMauro Carvalho Chehab	BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
271e2ba5731SMauro Carvalho Chehab			   x bits
272e2ba5731SMauro Carvalho Chehab	BIT_WORD(x)	 - returns the index in the array in longs for bit x
273e2ba5731SMauro Carvalho Chehab	BIT_MASK(x)	 - returns the index in a long for bit x
274e2ba5731SMauro Carvalho Chehab
275e2ba5731SMauro Carvalho ChehabThe id* and name fields
276e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~
277e2ba5731SMauro Carvalho Chehab
278e2ba5731SMauro Carvalho ChehabThe dev->name should be set before registering the input device by the input
279e2ba5731SMauro Carvalho Chehabdevice driver. It's a string like 'Generic button device' containing a
280e2ba5731SMauro Carvalho Chehabuser friendly name of the device.
281e2ba5731SMauro Carvalho Chehab
282e2ba5731SMauro Carvalho ChehabThe id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
2835c184115SRandy Dunlapof the device. The bus IDs are defined in input.h. The vendor and device IDs
284e2ba5731SMauro Carvalho Chehabare defined in pci_ids.h, usb_ids.h and similar include files. These fields
285e2ba5731SMauro Carvalho Chehabshould be set by the input device driver before registering it.
286e2ba5731SMauro Carvalho Chehab
287e2ba5731SMauro Carvalho ChehabThe idtype field can be used for specific information for the input device
288e2ba5731SMauro Carvalho Chehabdriver.
289e2ba5731SMauro Carvalho Chehab
290e2ba5731SMauro Carvalho ChehabThe id and name fields can be passed to userland via the evdev interface.
291e2ba5731SMauro Carvalho Chehab
292e2ba5731SMauro Carvalho ChehabThe keycode, keycodemax, keycodesize fields
293e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
294e2ba5731SMauro Carvalho Chehab
295e2ba5731SMauro Carvalho ChehabThese three fields should be used by input devices that have dense keymaps.
296e2ba5731SMauro Carvalho ChehabThe keycode is an array used to map from scancodes to input system keycodes.
297e2ba5731SMauro Carvalho ChehabThe keycode max should contain the size of the array and keycodesize the
298e2ba5731SMauro Carvalho Chehabsize of each entry in it (in bytes).
299e2ba5731SMauro Carvalho Chehab
300e2ba5731SMauro Carvalho ChehabUserspace can query and alter current scancode to keycode mappings using
301e2ba5731SMauro Carvalho ChehabEVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
302e2ba5731SMauro Carvalho ChehabWhen a device has all 3 aforementioned fields filled in, the driver may
303e2ba5731SMauro Carvalho Chehabrely on kernel's default implementation of setting and querying keycode
304e2ba5731SMauro Carvalho Chehabmappings.
305e2ba5731SMauro Carvalho Chehab
306e2ba5731SMauro Carvalho Chehabdev->getkeycode() and dev->setkeycode()
307e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
308e2ba5731SMauro Carvalho Chehab
309e2ba5731SMauro Carvalho Chehabgetkeycode() and setkeycode() callbacks allow drivers to override default
310e2ba5731SMauro Carvalho Chehabkeycode/keycodesize/keycodemax mapping mechanism provided by input core
311e2ba5731SMauro Carvalho Chehaband implement sparse keycode maps.
312e2ba5731SMauro Carvalho Chehab
313e2ba5731SMauro Carvalho ChehabKey autorepeat
314e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~
315e2ba5731SMauro Carvalho Chehab
316e2ba5731SMauro Carvalho Chehab... is simple. It is handled by the input.c module. Hardware autorepeat is
317e2ba5731SMauro Carvalho Chehabnot used, because it's not present in many devices and even where it is
318e2ba5731SMauro Carvalho Chehabpresent, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
319e2ba5731SMauro Carvalho Chehabautorepeat for your device, just set EV_REP in dev->evbit. All will be
320e2ba5731SMauro Carvalho Chehabhandled by the input system.
321e2ba5731SMauro Carvalho Chehab
322e2ba5731SMauro Carvalho ChehabOther event types, handling output events
323e2ba5731SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
324e2ba5731SMauro Carvalho Chehab
325e2ba5731SMauro Carvalho ChehabThe other event types up to now are:
326e2ba5731SMauro Carvalho Chehab
327e2ba5731SMauro Carvalho Chehab- EV_LED - used for the keyboard LEDs.
328e2ba5731SMauro Carvalho Chehab- EV_SND - used for keyboard beeps.
329e2ba5731SMauro Carvalho Chehab
330e2ba5731SMauro Carvalho ChehabThey are very similar to for example key events, but they go in the other
331e2ba5731SMauro Carvalho Chehabdirection - from the system to the input device driver. If your input device
332e2ba5731SMauro Carvalho Chehabdriver can handle these events, it has to set the respective bits in evbit,
333e2ba5731SMauro Carvalho Chehab*and* also the callback routine::
334e2ba5731SMauro Carvalho Chehab
335e2ba5731SMauro Carvalho Chehab    button_dev->event = button_event;
336e2ba5731SMauro Carvalho Chehab
337e2ba5731SMauro Carvalho Chehab    int button_event(struct input_dev *dev, unsigned int type,
338e2ba5731SMauro Carvalho Chehab		     unsigned int code, int value)
339e2ba5731SMauro Carvalho Chehab    {
340e2ba5731SMauro Carvalho Chehab	    if (type == EV_SND && code == SND_BELL) {
341e2ba5731SMauro Carvalho Chehab		    outb(value, BUTTON_BELL);
342e2ba5731SMauro Carvalho Chehab		    return 0;
343e2ba5731SMauro Carvalho Chehab	    }
344e2ba5731SMauro Carvalho Chehab	    return -1;
345e2ba5731SMauro Carvalho Chehab    }
346e2ba5731SMauro Carvalho Chehab
347e2ba5731SMauro Carvalho ChehabThis callback routine can be called from an interrupt or a BH (although that
348e2ba5731SMauro Carvalho Chehabisn't a rule), and thus must not sleep, and must not take too long to finish.
349*76a67822SBrendan Connelly
350*76a67822SBrendan ConnellyPolled input devices
351*76a67822SBrendan Connelly~~~~~~~~~~~~~~~~~~~~
352*76a67822SBrendan Connelly
353*76a67822SBrendan ConnellyInput polling is set up by passing an input device struct and a callback to
354*76a67822SBrendan Connellythe function::
355*76a67822SBrendan Connelly
356*76a67822SBrendan Connelly    int input_setup_polling(struct input_dev *dev,
357*76a67822SBrendan Connelly        void (*poll_fn)(struct input_dev *dev))
358*76a67822SBrendan Connelly
359*76a67822SBrendan ConnellyWithin the callback, devices should use the regular input_report_* functions
360*76a67822SBrendan Connellyand input_sync as is used by other devices.
361*76a67822SBrendan Connelly
362*76a67822SBrendan ConnellyThere is also the function::
363*76a67822SBrendan Connelly
364*76a67822SBrendan Connelly    void input_set_poll_interval(struct input_dev *dev, unsigned int interval)
365*76a67822SBrendan Connelly
366*76a67822SBrendan Connellywhich is used to configure the interval, in milliseconds, that the device will
367*76a67822SBrendan Connellybe polled at.
368