162306a36Sopenharmony_ci===============================
262306a36Sopenharmony_ciCreating an input device driver
362306a36Sopenharmony_ci===============================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciThe simplest example
662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciHere comes a very simple example of an input device driver. The device has
962306a36Sopenharmony_cijust one button and the button is accessible at i/o port BUTTON_PORT. When
1062306a36Sopenharmony_cipressed or released a BUTTON_IRQ happens. The driver could look like::
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci    #include <linux/input.h>
1362306a36Sopenharmony_ci    #include <linux/module.h>
1462306a36Sopenharmony_ci    #include <linux/init.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci    #include <asm/irq.h>
1762306a36Sopenharmony_ci    #include <asm/io.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci    static struct input_dev *button_dev;
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci    static irqreturn_t button_interrupt(int irq, void *dummy)
2262306a36Sopenharmony_ci    {
2362306a36Sopenharmony_ci	    input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
2462306a36Sopenharmony_ci	    input_sync(button_dev);
2562306a36Sopenharmony_ci	    return IRQ_HANDLED;
2662306a36Sopenharmony_ci    }
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci    static int __init button_init(void)
2962306a36Sopenharmony_ci    {
3062306a36Sopenharmony_ci	    int error;
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
3362306a36Sopenharmony_ci		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
3462306a36Sopenharmony_ci		    return -EBUSY;
3562306a36Sopenharmony_ci	    }
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	    button_dev = input_allocate_device();
3862306a36Sopenharmony_ci	    if (!button_dev) {
3962306a36Sopenharmony_ci		    printk(KERN_ERR "button.c: Not enough memory\n");
4062306a36Sopenharmony_ci		    error = -ENOMEM;
4162306a36Sopenharmony_ci		    goto err_free_irq;
4262306a36Sopenharmony_ci	    }
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	    button_dev->evbit[0] = BIT_MASK(EV_KEY);
4562306a36Sopenharmony_ci	    button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	    error = input_register_device(button_dev);
4862306a36Sopenharmony_ci	    if (error) {
4962306a36Sopenharmony_ci		    printk(KERN_ERR "button.c: Failed to register device\n");
5062306a36Sopenharmony_ci		    goto err_free_dev;
5162306a36Sopenharmony_ci	    }
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	    return 0;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci    err_free_dev:
5662306a36Sopenharmony_ci	    input_free_device(button_dev);
5762306a36Sopenharmony_ci    err_free_irq:
5862306a36Sopenharmony_ci	    free_irq(BUTTON_IRQ, button_interrupt);
5962306a36Sopenharmony_ci	    return error;
6062306a36Sopenharmony_ci    }
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci    static void __exit button_exit(void)
6362306a36Sopenharmony_ci    {
6462306a36Sopenharmony_ci	    input_unregister_device(button_dev);
6562306a36Sopenharmony_ci	    free_irq(BUTTON_IRQ, button_interrupt);
6662306a36Sopenharmony_ci    }
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci    module_init(button_init);
6962306a36Sopenharmony_ci    module_exit(button_exit);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciWhat the example does
7262306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciFirst it has to include the <linux/input.h> file, which interfaces to the
7562306a36Sopenharmony_ciinput subsystem. This provides all the definitions needed.
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciIn the _init function, which is called either upon module load or when
7862306a36Sopenharmony_cibooting the kernel, it grabs the required resources (it should also check
7962306a36Sopenharmony_cifor the presence of the device).
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciThen it allocates a new input device structure with input_allocate_device()
8262306a36Sopenharmony_ciand sets up input bitfields. This way the device driver tells the other
8362306a36Sopenharmony_ciparts of the input systems what it is - what events can be generated or
8462306a36Sopenharmony_ciaccepted by this input device. Our example device can only generate EV_KEY
8562306a36Sopenharmony_citype events, and from those only BTN_0 event code. Thus we only set these
8662306a36Sopenharmony_citwo bits. We could have used::
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	set_bit(EV_KEY, button_dev->evbit);
8962306a36Sopenharmony_ci	set_bit(BTN_0, button_dev->keybit);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cias well, but with more than single bits the first approach tends to be
9262306a36Sopenharmony_cishorter.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciThen the example driver registers the input device structure by calling::
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	input_register_device(button_dev);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciThis adds the button_dev structure to linked lists of the input driver and
9962306a36Sopenharmony_cicalls device handler modules _connect functions to tell them a new input
10062306a36Sopenharmony_cidevice has appeared. input_register_device() may sleep and therefore must
10162306a36Sopenharmony_cinot be called from an interrupt or with a spinlock held.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciWhile in use, the only used function of the driver is::
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	button_interrupt()
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciwhich upon every interrupt from the button checks its state and reports it
10862306a36Sopenharmony_civia the::
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	input_report_key()
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_cicall to the input system. There is no need to check whether the interrupt
11362306a36Sopenharmony_ciroutine isn't reporting two same value events (press, press for example) to
11462306a36Sopenharmony_cithe input system, because the input_report_* functions check that
11562306a36Sopenharmony_cithemselves.
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciThen there is the::
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	input_sync()
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cicall to tell those who receive the events that we've sent a complete report.
12262306a36Sopenharmony_ciThis doesn't seem important in the one button case, but is quite important
12362306a36Sopenharmony_cifor example for mouse movement, where you don't want the X and Y values
12462306a36Sopenharmony_cito be interpreted separately, because that'd result in a different movement.
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cidev->open() and dev->close()
12762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ciIn case the driver has to repeatedly poll the device, because it doesn't
13062306a36Sopenharmony_cihave an interrupt coming from it and the polling is too expensive to be done
13162306a36Sopenharmony_ciall the time, or if the device uses a valuable resource (e.g. interrupt), it
13262306a36Sopenharmony_cican use the open and close callback to know when it can stop polling or
13362306a36Sopenharmony_cirelease the interrupt and when it must resume polling or grab the interrupt
13462306a36Sopenharmony_ciagain. To do that, we would add this to our example driver::
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci    static int button_open(struct input_dev *dev)
13762306a36Sopenharmony_ci    {
13862306a36Sopenharmony_ci	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
13962306a36Sopenharmony_ci		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
14062306a36Sopenharmony_ci		    return -EBUSY;
14162306a36Sopenharmony_ci	    }
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	    return 0;
14462306a36Sopenharmony_ci    }
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci    static void button_close(struct input_dev *dev)
14762306a36Sopenharmony_ci    {
14862306a36Sopenharmony_ci	    free_irq(IRQ_AMIGA_VERTB, button_interrupt);
14962306a36Sopenharmony_ci    }
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci    static int __init button_init(void)
15262306a36Sopenharmony_ci    {
15362306a36Sopenharmony_ci	    ...
15462306a36Sopenharmony_ci	    button_dev->open = button_open;
15562306a36Sopenharmony_ci	    button_dev->close = button_close;
15662306a36Sopenharmony_ci	    ...
15762306a36Sopenharmony_ci    }
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciNote that input core keeps track of number of users for the device and
16062306a36Sopenharmony_cimakes sure that dev->open() is called only when the first user connects
16162306a36Sopenharmony_cito the device and that dev->close() is called when the very last user
16262306a36Sopenharmony_cidisconnects. Calls to both callbacks are serialized.
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ciThe open() callback should return a 0 in case of success or any non-zero value
16562306a36Sopenharmony_ciin case of failure. The close() callback (which is void) must always succeed.
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciInhibiting input devices
16862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ciInhibiting a device means ignoring input events from it. As such it is about
17162306a36Sopenharmony_cimaintaining relationships with input handlers - either already existing
17262306a36Sopenharmony_cirelationships, or relationships to be established while the device is in
17362306a36Sopenharmony_ciinhibited state.
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciIf a device is inhibited, no input handler will receive events from it.
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciThe fact that nobody wants events from the device is exploited further, by
17862306a36Sopenharmony_cicalling device's close() (if there are users) and open() (if there are users) on
17962306a36Sopenharmony_ciinhibit and uninhibit operations, respectively. Indeed, the meaning of close()
18062306a36Sopenharmony_ciis to stop providing events to the input core and that of open() is to start
18162306a36Sopenharmony_ciproviding events to the input core.
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ciCalling the device's close() method on inhibit (if there are users) allows the
18462306a36Sopenharmony_cidriver to save power. Either by directly powering down the device or by
18562306a36Sopenharmony_cireleasing the runtime-PM reference it got in open() when the driver is using
18662306a36Sopenharmony_ciruntime-PM.
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciInhibiting and uninhibiting are orthogonal to opening and closing the device by
18962306a36Sopenharmony_ciinput handlers. Userspace might want to inhibit a device in anticipation before
19062306a36Sopenharmony_ciany handler is positively matched against it.
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciInhibiting and uninhibiting are orthogonal to device's being a wakeup source,
19362306a36Sopenharmony_citoo. Being a wakeup source plays a role when the system is sleeping, not when
19462306a36Sopenharmony_cithe system is operating.  How drivers should program their interaction between
19562306a36Sopenharmony_ciinhibiting, sleeping and being a wakeup source is driver-specific.
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ciTaking the analogy with the network devices - bringing a network interface down
19862306a36Sopenharmony_cidoesn't mean that it should be impossible be wake the system up on LAN through
19962306a36Sopenharmony_cithis interface. So, there may be input drivers which should be considered wakeup
20062306a36Sopenharmony_cisources even when inhibited. Actually, in many I2C input devices their interrupt
20162306a36Sopenharmony_ciis declared a wakeup interrupt and its handling happens in driver's core, which
20262306a36Sopenharmony_ciis not aware of input-specific inhibit (nor should it be).  Composite devices
20362306a36Sopenharmony_cicontaining several interfaces can be inhibited on a per-interface basis and e.g.
20462306a36Sopenharmony_ciinhibiting one interface shouldn't affect the device's capability of being a
20562306a36Sopenharmony_ciwakeup source.
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ciIf a device is to be considered a wakeup source while inhibited, special care
20862306a36Sopenharmony_cimust be taken when programming its suspend(), as it might need to call device's
20962306a36Sopenharmony_ciopen(). Depending on what close() means for the device in question, not
21062306a36Sopenharmony_ciopening() it before going to sleep might make it impossible to provide any
21162306a36Sopenharmony_ciwakeup events. The device is going to sleep anyway.
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciBasic event types
21462306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciThe most simple event type is EV_KEY, which is used for keys and buttons.
21762306a36Sopenharmony_ciIt's reported to the input system via::
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	input_report_key(struct input_dev *dev, int code, int value)
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ciSee uapi/linux/input-event-codes.h for the allowable values of code (from 0 to
22262306a36Sopenharmony_ciKEY_MAX). Value is interpreted as a truth value, i.e. any non-zero value means
22362306a36Sopenharmony_cikey pressed, zero value means key released. The input code generates events only
22462306a36Sopenharmony_ciin case the value is different from before.
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ciIn addition to EV_KEY, there are two more basic event types: EV_REL and
22762306a36Sopenharmony_ciEV_ABS. They are used for relative and absolute values supplied by the
22862306a36Sopenharmony_cidevice. A relative value may be for example a mouse movement in the X axis.
22962306a36Sopenharmony_ciThe mouse reports it as a relative difference from the last position,
23062306a36Sopenharmony_cibecause it doesn't have any absolute coordinate system to work in. Absolute
23162306a36Sopenharmony_cievents are namely for joysticks and digitizers - devices that do work in an
23262306a36Sopenharmony_ciabsolute coordinate systems.
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciHaving the device report EV_REL buttons is as simple as with EV_KEY; simply
23562306a36Sopenharmony_ciset the corresponding bits and call the::
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	input_report_rel(struct input_dev *dev, int code, int value)
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_cifunction. Events are generated only for non-zero values.
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ciHowever EV_ABS requires a little special care. Before calling
24262306a36Sopenharmony_ciinput_register_device, you have to fill additional fields in the input_dev
24362306a36Sopenharmony_cistruct for each absolute axis your device has. If our button device had also
24462306a36Sopenharmony_cithe ABS_X axis::
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	button_dev.absmin[ABS_X] = 0;
24762306a36Sopenharmony_ci	button_dev.absmax[ABS_X] = 255;
24862306a36Sopenharmony_ci	button_dev.absfuzz[ABS_X] = 4;
24962306a36Sopenharmony_ci	button_dev.absflat[ABS_X] = 8;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ciOr, you can just say::
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ciThis setting would be appropriate for a joystick X axis, with the minimum of
25662306a36Sopenharmony_ci0, maximum of 255 (which the joystick *must* be able to reach, no problem if
25762306a36Sopenharmony_ciit sometimes reports more, but it must be able to always reach the min and
25862306a36Sopenharmony_cimax values), with noise in the data up to +- 4, and with a center flat
25962306a36Sopenharmony_ciposition of size 8.
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ciIf you don't need absfuzz and absflat, you can set them to zero, which mean
26262306a36Sopenharmony_cithat the thing is precise and always returns to exactly the center position
26362306a36Sopenharmony_ci(if it has any).
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ciBITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
26662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ciThese three macros from bitops.h help some bitfield computations::
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
27162306a36Sopenharmony_ci			   x bits
27262306a36Sopenharmony_ci	BIT_WORD(x)	 - returns the index in the array in longs for bit x
27362306a36Sopenharmony_ci	BIT_MASK(x)	 - returns the index in a long for bit x
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ciThe id* and name fields
27662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ciThe dev->name should be set before registering the input device by the input
27962306a36Sopenharmony_cidevice driver. It's a string like 'Generic button device' containing a
28062306a36Sopenharmony_ciuser friendly name of the device.
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ciThe id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
28362306a36Sopenharmony_ciof the device. The bus IDs are defined in input.h. The vendor and device IDs
28462306a36Sopenharmony_ciare defined in pci_ids.h, usb_ids.h and similar include files. These fields
28562306a36Sopenharmony_cishould be set by the input device driver before registering it.
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ciThe idtype field can be used for specific information for the input device
28862306a36Sopenharmony_cidriver.
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ciThe id and name fields can be passed to userland via the evdev interface.
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ciThe keycode, keycodemax, keycodesize fields
29362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ciThese three fields should be used by input devices that have dense keymaps.
29662306a36Sopenharmony_ciThe keycode is an array used to map from scancodes to input system keycodes.
29762306a36Sopenharmony_ciThe keycode max should contain the size of the array and keycodesize the
29862306a36Sopenharmony_cisize of each entry in it (in bytes).
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ciUserspace can query and alter current scancode to keycode mappings using
30162306a36Sopenharmony_ciEVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
30262306a36Sopenharmony_ciWhen a device has all 3 aforementioned fields filled in, the driver may
30362306a36Sopenharmony_cirely on kernel's default implementation of setting and querying keycode
30462306a36Sopenharmony_cimappings.
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cidev->getkeycode() and dev->setkeycode()
30762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_cigetkeycode() and setkeycode() callbacks allow drivers to override default
31062306a36Sopenharmony_cikeycode/keycodesize/keycodemax mapping mechanism provided by input core
31162306a36Sopenharmony_ciand implement sparse keycode maps.
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ciKey autorepeat
31462306a36Sopenharmony_ci~~~~~~~~~~~~~~
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci... is simple. It is handled by the input.c module. Hardware autorepeat is
31762306a36Sopenharmony_cinot used, because it's not present in many devices and even where it is
31862306a36Sopenharmony_cipresent, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
31962306a36Sopenharmony_ciautorepeat for your device, just set EV_REP in dev->evbit. All will be
32062306a36Sopenharmony_cihandled by the input system.
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ciOther event types, handling output events
32362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ciThe other event types up to now are:
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci- EV_LED - used for the keyboard LEDs.
32862306a36Sopenharmony_ci- EV_SND - used for keyboard beeps.
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ciThey are very similar to for example key events, but they go in the other
33162306a36Sopenharmony_cidirection - from the system to the input device driver. If your input device
33262306a36Sopenharmony_cidriver can handle these events, it has to set the respective bits in evbit,
33362306a36Sopenharmony_ci*and* also the callback routine::
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci    button_dev->event = button_event;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci    int button_event(struct input_dev *dev, unsigned int type,
33862306a36Sopenharmony_ci		     unsigned int code, int value)
33962306a36Sopenharmony_ci    {
34062306a36Sopenharmony_ci	    if (type == EV_SND && code == SND_BELL) {
34162306a36Sopenharmony_ci		    outb(value, BUTTON_BELL);
34262306a36Sopenharmony_ci		    return 0;
34362306a36Sopenharmony_ci	    }
34462306a36Sopenharmony_ci	    return -1;
34562306a36Sopenharmony_ci    }
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ciThis callback routine can be called from an interrupt or a BH (although that
34862306a36Sopenharmony_ciisn't a rule), and thus must not sleep, and must not take too long to finish.
349