blob: 2638dce69764cff9495204f3d0b536bfc46b71eb [file] [log] [blame]
Dmitry Torokhovb08c1182017-04-06 18:08:42 -07001===============================
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -07002Creating an input device driver
3===============================
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -07005The simplest example
6~~~~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8Here comes a very simple example of an input device driver. The device has
9just one button and the button is accessible at i/o port BUTTON_PORT. When
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070010pressed or released a BUTTON_IRQ happens. The driver could look like::
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070012 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070016 #include <asm/irq.h>
17 #include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070019 static struct input_dev *button_dev;
Dmitry Torokhov85796e72007-04-29 23:42:08 -040020
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070021 static irqreturn_t button_interrupt(int irq, void *dummy)
22 {
23 input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
24 input_sync(button_dev);
25 return IRQ_HANDLED;
26 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070028 static int __init button_init(void)
29 {
30 int error;
Dmitry Torokhov85796e72007-04-29 23:42:08 -040031
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070032 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
33 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
34 return -EBUSY;
35 }
Dmitry Torokhov85796e72007-04-29 23:42:08 -040036
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070037 button_dev = input_allocate_device();
38 if (!button_dev) {
39 printk(KERN_ERR "button.c: Not enough memory\n");
40 error = -ENOMEM;
41 goto err_free_irq;
42 }
Dmitry Torokhov85796e72007-04-29 23:42:08 -040043
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070044 button_dev->evbit[0] = BIT_MASK(EV_KEY);
45 button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
Dmitry Torokhov85796e72007-04-29 23:42:08 -040046
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070047 error = input_register_device(button_dev);
48 if (error) {
49 printk(KERN_ERR "button.c: Failed to register device\n");
50 goto err_free_dev;
51 }
Dmitry Torokhov85796e72007-04-29 23:42:08 -040052
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070053 return 0;
Dmitry Torokhov85796e72007-04-29 23:42:08 -040054
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070055 err_free_dev:
56 input_free_device(button_dev);
57 err_free_irq:
58 free_irq(BUTTON_IRQ, button_interrupt);
59 return error;
60 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070062 static void __exit button_exit(void)
63 {
64 input_unregister_device(button_dev);
65 free_irq(BUTTON_IRQ, button_interrupt);
66 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070068 module_init(button_init);
69 module_exit(button_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070071What the example does
72~~~~~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74First it has to include the <linux/input.h> file, which interfaces to the
75input subsystem. This provides all the definitions needed.
76
77In the _init function, which is called either upon module load or when
78booting the kernel, it grabs the required resources (it should also check
79for the presence of the device).
80
Matt LaPlante01dd2fb2007-10-20 01:34:40 +020081Then it allocates a new input device structure with input_allocate_device()
Dmitry Torokhov85796e72007-04-29 23:42:08 -040082and sets up input bitfields. This way the device driver tells the other
Linus Torvalds1da177e2005-04-16 15:20:36 -070083parts of the input systems what it is - what events can be generated or
Dmitry Torokhov85796e72007-04-29 23:42:08 -040084accepted by this input device. Our example device can only generate EV_KEY
85type events, and from those only BTN_0 event code. Thus we only set these
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070086two bits. We could have used::
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 set_bit(EV_KEY, button_dev.evbit);
89 set_bit(BTN_0, button_dev.keybit);
90
91as well, but with more than single bits the first approach tends to be
Dmitry Torokhov85796e72007-04-29 23:42:08 -040092shorter.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -070094Then the example driver registers the input device structure by calling::
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 input_register_device(&button_dev);
97
98This adds the button_dev structure to linked lists of the input driver and
99calls device handler modules _connect functions to tell them a new input
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400100device has appeared. input_register_device() may sleep and therefore must
101not be called from an interrupt or with a spinlock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700103While in use, the only used function of the driver is::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 button_interrupt()
106
107which upon every interrupt from the button checks its state and reports it
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700108via the::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 input_report_key()
111
112call to the input system. There is no need to check whether the interrupt
113routine isn't reporting two same value events (press, press for example) to
114the input system, because the input_report_* functions check that
115themselves.
116
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700117Then there is the::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 input_sync()
120
121call to tell those who receive the events that we've sent a complete report.
122This doesn't seem important in the one button case, but is quite important
Randy Dunlap5c184112021-03-02 14:35:22 -0800123for example for mouse movement, where you don't want the X and Y values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124to be interpreted separately, because that'd result in a different movement.
125
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700126dev->open() and dev->close()
127~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129In case the driver has to repeatedly poll the device, because it doesn't
130have an interrupt coming from it and the polling is too expensive to be done
Randy Dunlap5c184112021-03-02 14:35:22 -0800131all the time, or if the device uses a valuable resource (e.g. interrupt), it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132can use the open and close callback to know when it can stop polling or
133release the interrupt and when it must resume polling or grab the interrupt
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700134again. To do that, we would add this to our example driver::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700136 static int button_open(struct input_dev *dev)
137 {
138 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
139 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
140 return -EBUSY;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700143 return 0;
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700146 static void button_close(struct input_dev *dev)
147 {
148 free_irq(IRQ_AMIGA_VERTB, button_interrupt);
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700151 static int __init button_init(void)
152 {
153 ...
154 button_dev->open = button_open;
155 button_dev->close = button_close;
156 ...
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400159Note that input core keeps track of number of users for the device and
160makes sure that dev->open() is called only when the first user connects
161to the device and that dev->close() is called when the very last user
162disconnects. Calls to both callbacks are serialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Randy Dunlap5c184112021-03-02 14:35:22 -0800164The open() callback should return a 0 in case of success or any non-zero value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165in case of failure. The close() callback (which is void) must always succeed.
166
Andrzej Pietrasiewicz6d592242020-12-02 14:44:26 -0800167Inhibiting input devices
168~~~~~~~~~~~~~~~~~~~~~~~~
169
170Inhibiting a device means ignoring input events from it. As such it is about
171maintaining relationships with input handlers - either already existing
172relationships, or relationships to be established while the device is in
173inhibited state.
174
175If a device is inhibited, no input handler will receive events from it.
176
177The fact that nobody wants events from the device is exploited further, by
178calling device's close() (if there are users) and open() (if there are users) on
179inhibit and uninhibit operations, respectively. Indeed, the meaning of close()
180is to stop providing events to the input core and that of open() is to start
181providing events to the input core.
182
183Calling the device's close() method on inhibit (if there are users) allows the
184driver to save power. Either by directly powering down the device or by
Randy Dunlap5c184112021-03-02 14:35:22 -0800185releasing the runtime-PM reference it got in open() when the driver is using
186runtime-PM.
Andrzej Pietrasiewicz6d592242020-12-02 14:44:26 -0800187
188Inhibiting and uninhibiting are orthogonal to opening and closing the device by
189input handlers. Userspace might want to inhibit a device in anticipation before
190any handler is positively matched against it.
191
192Inhibiting and uninhibiting are orthogonal to device's being a wakeup source,
193too. Being a wakeup source plays a role when the system is sleeping, not when
194the system is operating. How drivers should program their interaction between
195inhibiting, sleeping and being a wakeup source is driver-specific.
196
197Taking the analogy with the network devices - bringing a network interface down
198doesn't mean that it should be impossible be wake the system up on LAN through
199this interface. So, there may be input drivers which should be considered wakeup
200sources even when inhibited. Actually, in many I2C input devices their interrupt
201is declared a wakeup interrupt and its handling happens in driver's core, which
202is not aware of input-specific inhibit (nor should it be). Composite devices
203containing several interfaces can be inhibited on a per-interface basis and e.g.
204inhibiting one interface shouldn't affect the device's capability of being a
205wakeup source.
206
207If a device is to be considered a wakeup source while inhibited, special care
208must be taken when programming its suspend(), as it might need to call device's
209open(). Depending on what close() means for the device in question, not
210opening() it before going to sleep might make it impossible to provide any
211wakeup events. The device is going to sleep anyway.
212
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700213Basic event types
214~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216The most simple event type is EV_KEY, which is used for keys and buttons.
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700217It's reported to the input system via::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 input_report_key(struct input_dev *dev, int code, int value)
220
Martin Kepplinger28a5c962017-03-12 12:54:22 +0100221See uapi/linux/input-event-codes.h for the allowable values of code (from 0 to
Randy Dunlap5c184112021-03-02 14:35:22 -0800222KEY_MAX). Value is interpreted as a truth value, i.e. any non-zero value means
223key pressed, zero value means key released. The input code generates events only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224in case the value is different from before.
225
226In addition to EV_KEY, there are two more basic event types: EV_REL and
227EV_ABS. They are used for relative and absolute values supplied by the
228device. A relative value may be for example a mouse movement in the X axis.
229The mouse reports it as a relative difference from the last position,
230because it doesn't have any absolute coordinate system to work in. Absolute
231events are namely for joysticks and digitizers - devices that do work in an
232absolute coordinate systems.
233
Randy Dunlap5c184112021-03-02 14:35:22 -0800234Having the device report EV_REL buttons is as simple as with EV_KEY; simply
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700235set the corresponding bits and call the::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 input_report_rel(struct input_dev *dev, int code, int value)
238
Randy Dunlap5c184112021-03-02 14:35:22 -0800239function. Events are generated only for non-zero values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241However EV_ABS requires a little special care. Before calling
242input_register_device, you have to fill additional fields in the input_dev
243struct for each absolute axis your device has. If our button device had also
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700244the ABS_X axis::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 button_dev.absmin[ABS_X] = 0;
247 button_dev.absmax[ABS_X] = 255;
248 button_dev.absfuzz[ABS_X] = 4;
249 button_dev.absflat[ABS_X] = 8;
250
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700251Or, you can just say::
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400252
253 input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255This setting would be appropriate for a joystick X axis, with the minimum of
2560, maximum of 255 (which the joystick *must* be able to reach, no problem if
257it sometimes reports more, but it must be able to always reach the min and
258max values), with noise in the data up to +- 4, and with a center flat
259position of size 8.
260
261If you don't need absfuzz and absflat, you can set them to zero, which mean
262that the thing is precise and always returns to exactly the center position
263(if it has any).
264
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700265BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
266~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700268These three macros from bitops.h help some bitfield computations::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700270 BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
271 x bits
272 BIT_WORD(x) - returns the index in the array in longs for bit x
273 BIT_MASK(x) - returns the index in a long for bit x
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700275The id* and name fields
276~~~~~~~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278The dev->name should be set before registering the input device by the input
279device driver. It's a string like 'Generic button device' containing a
280user friendly name of the device.
281
282The id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
Randy Dunlap5c184112021-03-02 14:35:22 -0800283of the device. The bus IDs are defined in input.h. The vendor and device IDs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284are defined in pci_ids.h, usb_ids.h and similar include files. These fields
285should be set by the input device driver before registering it.
286
287The idtype field can be used for specific information for the input device
288driver.
289
290The id and name fields can be passed to userland via the evdev interface.
291
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700292The keycode, keycodemax, keycodesize fields
293~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400295These three fields should be used by input devices that have dense keymaps.
296The keycode is an array used to map from scancodes to input system keycodes.
297The keycode max should contain the size of the array and keycodesize the
298size of each entry in it (in bytes).
299
300Userspace can query and alter current scancode to keycode mappings using
301EVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
302When a device has all 3 aforementioned fields filled in, the driver may
303rely on kernel's default implementation of setting and querying keycode
304mappings.
305
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700306dev->getkeycode() and dev->setkeycode()
307~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
308
Dmitry Torokhov85796e72007-04-29 23:42:08 -0400309getkeycode() and setkeycode() callbacks allow drivers to override default
310keycode/keycodesize/keycodemax mapping mechanism provided by input core
311and implement sparse keycode maps.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700313Key autorepeat
314~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316... is simple. It is handled by the input.c module. Hardware autorepeat is
317not used, because it's not present in many devices and even where it is
318present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
319autorepeat for your device, just set EV_REP in dev->evbit. All will be
320handled by the input system.
321
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700322Other event types, handling output events
323~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325The other event types up to now are:
326
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700327- EV_LED - used for the keyboard LEDs.
328- EV_SND - used for keyboard beeps.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330They are very similar to for example key events, but they go in the other
331direction - from the system to the input device driver. If your input device
332driver can handle these events, it has to set the respective bits in evbit,
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700333*and* also the callback routine::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700335 button_dev->event = button_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Mauro Carvalho Chehab1c4ada62017-04-04 17:44:04 -0700337 int button_event(struct input_dev *dev, unsigned int type,
338 unsigned int code, int value)
339 {
340 if (type == EV_SND && code == SND_BELL) {
341 outb(value, BUTTON_BELL);
342 return 0;
343 }
344 return -1;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347This callback routine can be called from an interrupt or a BH (although that
348isn't a rule), and thus must not sleep, and must not take too long to finish.