0001 ===============================
0002 Creating an input device driver
0003 ===============================
0004
0005 The simplest example
0006 ~~~~~~~~~~~~~~~~~~~~
0007
0008 Here comes a very simple example of an input device driver. The device has
0009 just one button and the button is accessible at i/o port BUTTON_PORT. When
0010 pressed or released a BUTTON_IRQ happens. The driver could look like::
0011
0012 #include <linux/input.h>
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015
0016 #include <asm/irq.h>
0017 #include <asm/io.h>
0018
0019 static struct input_dev *button_dev;
0020
0021 static irqreturn_t button_interrupt(int irq, void *dummy)
0022 {
0023 input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
0024 input_sync(button_dev);
0025 return IRQ_HANDLED;
0026 }
0027
0028 static int __init button_init(void)
0029 {
0030 int error;
0031
0032 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
0033 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
0034 return -EBUSY;
0035 }
0036
0037 button_dev = input_allocate_device();
0038 if (!button_dev) {
0039 printk(KERN_ERR "button.c: Not enough memory\n");
0040 error = -ENOMEM;
0041 goto err_free_irq;
0042 }
0043
0044 button_dev->evbit[0] = BIT_MASK(EV_KEY);
0045 button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
0046
0047 error = input_register_device(button_dev);
0048 if (error) {
0049 printk(KERN_ERR "button.c: Failed to register device\n");
0050 goto err_free_dev;
0051 }
0052
0053 return 0;
0054
0055 err_free_dev:
0056 input_free_device(button_dev);
0057 err_free_irq:
0058 free_irq(BUTTON_IRQ, button_interrupt);
0059 return error;
0060 }
0061
0062 static void __exit button_exit(void)
0063 {
0064 input_unregister_device(button_dev);
0065 free_irq(BUTTON_IRQ, button_interrupt);
0066 }
0067
0068 module_init(button_init);
0069 module_exit(button_exit);
0070
0071 What the example does
0072 ~~~~~~~~~~~~~~~~~~~~~
0073
0074 First it has to include the <linux/input.h> file, which interfaces to the
0075 input subsystem. This provides all the definitions needed.
0076
0077 In the _init function, which is called either upon module load or when
0078 booting the kernel, it grabs the required resources (it should also check
0079 for the presence of the device).
0080
0081 Then it allocates a new input device structure with input_allocate_device()
0082 and sets up input bitfields. This way the device driver tells the other
0083 parts of the input systems what it is - what events can be generated or
0084 accepted by this input device. Our example device can only generate EV_KEY
0085 type events, and from those only BTN_0 event code. Thus we only set these
0086 two bits. We could have used::
0087
0088 set_bit(EV_KEY, button_dev->evbit);
0089 set_bit(BTN_0, button_dev->keybit);
0090
0091 as well, but with more than single bits the first approach tends to be
0092 shorter.
0093
0094 Then the example driver registers the input device structure by calling::
0095
0096 input_register_device(button_dev);
0097
0098 This adds the button_dev structure to linked lists of the input driver and
0099 calls device handler modules _connect functions to tell them a new input
0100 device has appeared. input_register_device() may sleep and therefore must
0101 not be called from an interrupt or with a spinlock held.
0102
0103 While in use, the only used function of the driver is::
0104
0105 button_interrupt()
0106
0107 which upon every interrupt from the button checks its state and reports it
0108 via the::
0109
0110 input_report_key()
0111
0112 call to the input system. There is no need to check whether the interrupt
0113 routine isn't reporting two same value events (press, press for example) to
0114 the input system, because the input_report_* functions check that
0115 themselves.
0116
0117 Then there is the::
0118
0119 input_sync()
0120
0121 call to tell those who receive the events that we've sent a complete report.
0122 This doesn't seem important in the one button case, but is quite important
0123 for example for mouse movement, where you don't want the X and Y values
0124 to be interpreted separately, because that'd result in a different movement.
0125
0126 dev->open() and dev->close()
0127 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0128
0129 In case the driver has to repeatedly poll the device, because it doesn't
0130 have an interrupt coming from it and the polling is too expensive to be done
0131 all the time, or if the device uses a valuable resource (e.g. interrupt), it
0132 can use the open and close callback to know when it can stop polling or
0133 release the interrupt and when it must resume polling or grab the interrupt
0134 again. To do that, we would add this to our example driver::
0135
0136 static int button_open(struct input_dev *dev)
0137 {
0138 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
0139 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
0140 return -EBUSY;
0141 }
0142
0143 return 0;
0144 }
0145
0146 static void button_close(struct input_dev *dev)
0147 {
0148 free_irq(IRQ_AMIGA_VERTB, button_interrupt);
0149 }
0150
0151 static int __init button_init(void)
0152 {
0153 ...
0154 button_dev->open = button_open;
0155 button_dev->close = button_close;
0156 ...
0157 }
0158
0159 Note that input core keeps track of number of users for the device and
0160 makes sure that dev->open() is called only when the first user connects
0161 to the device and that dev->close() is called when the very last user
0162 disconnects. Calls to both callbacks are serialized.
0163
0164 The open() callback should return a 0 in case of success or any non-zero value
0165 in case of failure. The close() callback (which is void) must always succeed.
0166
0167 Inhibiting input devices
0168 ~~~~~~~~~~~~~~~~~~~~~~~~
0169
0170 Inhibiting a device means ignoring input events from it. As such it is about
0171 maintaining relationships with input handlers - either already existing
0172 relationships, or relationships to be established while the device is in
0173 inhibited state.
0174
0175 If a device is inhibited, no input handler will receive events from it.
0176
0177 The fact that nobody wants events from the device is exploited further, by
0178 calling device's close() (if there are users) and open() (if there are users) on
0179 inhibit and uninhibit operations, respectively. Indeed, the meaning of close()
0180 is to stop providing events to the input core and that of open() is to start
0181 providing events to the input core.
0182
0183 Calling the device's close() method on inhibit (if there are users) allows the
0184 driver to save power. Either by directly powering down the device or by
0185 releasing the runtime-PM reference it got in open() when the driver is using
0186 runtime-PM.
0187
0188 Inhibiting and uninhibiting are orthogonal to opening and closing the device by
0189 input handlers. Userspace might want to inhibit a device in anticipation before
0190 any handler is positively matched against it.
0191
0192 Inhibiting and uninhibiting are orthogonal to device's being a wakeup source,
0193 too. Being a wakeup source plays a role when the system is sleeping, not when
0194 the system is operating. How drivers should program their interaction between
0195 inhibiting, sleeping and being a wakeup source is driver-specific.
0196
0197 Taking the analogy with the network devices - bringing a network interface down
0198 doesn't mean that it should be impossible be wake the system up on LAN through
0199 this interface. So, there may be input drivers which should be considered wakeup
0200 sources even when inhibited. Actually, in many I2C input devices their interrupt
0201 is declared a wakeup interrupt and its handling happens in driver's core, which
0202 is not aware of input-specific inhibit (nor should it be). Composite devices
0203 containing several interfaces can be inhibited on a per-interface basis and e.g.
0204 inhibiting one interface shouldn't affect the device's capability of being a
0205 wakeup source.
0206
0207 If a device is to be considered a wakeup source while inhibited, special care
0208 must be taken when programming its suspend(), as it might need to call device's
0209 open(). Depending on what close() means for the device in question, not
0210 opening() it before going to sleep might make it impossible to provide any
0211 wakeup events. The device is going to sleep anyway.
0212
0213 Basic event types
0214 ~~~~~~~~~~~~~~~~~
0215
0216 The most simple event type is EV_KEY, which is used for keys and buttons.
0217 It's reported to the input system via::
0218
0219 input_report_key(struct input_dev *dev, int code, int value)
0220
0221 See uapi/linux/input-event-codes.h for the allowable values of code (from 0 to
0222 KEY_MAX). Value is interpreted as a truth value, i.e. any non-zero value means
0223 key pressed, zero value means key released. The input code generates events only
0224 in case the value is different from before.
0225
0226 In addition to EV_KEY, there are two more basic event types: EV_REL and
0227 EV_ABS. They are used for relative and absolute values supplied by the
0228 device. A relative value may be for example a mouse movement in the X axis.
0229 The mouse reports it as a relative difference from the last position,
0230 because it doesn't have any absolute coordinate system to work in. Absolute
0231 events are namely for joysticks and digitizers - devices that do work in an
0232 absolute coordinate systems.
0233
0234 Having the device report EV_REL buttons is as simple as with EV_KEY; simply
0235 set the corresponding bits and call the::
0236
0237 input_report_rel(struct input_dev *dev, int code, int value)
0238
0239 function. Events are generated only for non-zero values.
0240
0241 However EV_ABS requires a little special care. Before calling
0242 input_register_device, you have to fill additional fields in the input_dev
0243 struct for each absolute axis your device has. If our button device had also
0244 the ABS_X axis::
0245
0246 button_dev.absmin[ABS_X] = 0;
0247 button_dev.absmax[ABS_X] = 255;
0248 button_dev.absfuzz[ABS_X] = 4;
0249 button_dev.absflat[ABS_X] = 8;
0250
0251 Or, you can just say::
0252
0253 input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
0254
0255 This setting would be appropriate for a joystick X axis, with the minimum of
0256 0, maximum of 255 (which the joystick *must* be able to reach, no problem if
0257 it sometimes reports more, but it must be able to always reach the min and
0258 max values), with noise in the data up to +- 4, and with a center flat
0259 position of size 8.
0260
0261 If you don't need absfuzz and absflat, you can set them to zero, which mean
0262 that the thing is precise and always returns to exactly the center position
0263 (if it has any).
0264
0265 BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
0266 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0267
0268 These three macros from bitops.h help some bitfield computations::
0269
0270 BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
0271 x bits
0272 BIT_WORD(x) - returns the index in the array in longs for bit x
0273 BIT_MASK(x) - returns the index in a long for bit x
0274
0275 The id* and name fields
0276 ~~~~~~~~~~~~~~~~~~~~~~~
0277
0278 The dev->name should be set before registering the input device by the input
0279 device driver. It's a string like 'Generic button device' containing a
0280 user friendly name of the device.
0281
0282 The id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
0283 of the device. The bus IDs are defined in input.h. The vendor and device IDs
0284 are defined in pci_ids.h, usb_ids.h and similar include files. These fields
0285 should be set by the input device driver before registering it.
0286
0287 The idtype field can be used for specific information for the input device
0288 driver.
0289
0290 The id and name fields can be passed to userland via the evdev interface.
0291
0292 The keycode, keycodemax, keycodesize fields
0293 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0294
0295 These three fields should be used by input devices that have dense keymaps.
0296 The keycode is an array used to map from scancodes to input system keycodes.
0297 The keycode max should contain the size of the array and keycodesize the
0298 size of each entry in it (in bytes).
0299
0300 Userspace can query and alter current scancode to keycode mappings using
0301 EVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
0302 When a device has all 3 aforementioned fields filled in, the driver may
0303 rely on kernel's default implementation of setting and querying keycode
0304 mappings.
0305
0306 dev->getkeycode() and dev->setkeycode()
0307 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0308
0309 getkeycode() and setkeycode() callbacks allow drivers to override default
0310 keycode/keycodesize/keycodemax mapping mechanism provided by input core
0311 and implement sparse keycode maps.
0312
0313 Key autorepeat
0314 ~~~~~~~~~~~~~~
0315
0316 ... is simple. It is handled by the input.c module. Hardware autorepeat is
0317 not used, because it's not present in many devices and even where it is
0318 present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
0319 autorepeat for your device, just set EV_REP in dev->evbit. All will be
0320 handled by the input system.
0321
0322 Other event types, handling output events
0323 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0324
0325 The other event types up to now are:
0326
0327 - EV_LED - used for the keyboard LEDs.
0328 - EV_SND - used for keyboard beeps.
0329
0330 They are very similar to for example key events, but they go in the other
0331 direction - from the system to the input device driver. If your input device
0332 driver can handle these events, it has to set the respective bits in evbit,
0333 *and* also the callback routine::
0334
0335 button_dev->event = button_event;
0336
0337 int button_event(struct input_dev *dev, unsigned int type,
0338 unsigned int code, int value)
0339 {
0340 if (type == EV_SND && code == SND_BELL) {
0341 outb(value, BUTTON_BELL);
0342 return 0;
0343 }
0344 return -1;
0345 }
0346
0347 This callback routine can be called from an interrupt or a BH (although that
0348 isn't a rule), and thus must not sleep, and must not take too long to finish.