18c2ecf20Sopenharmony_ci===============================
28c2ecf20Sopenharmony_ciCreating an input device driver
38c2ecf20Sopenharmony_ci===============================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciThe simplest example
68c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciHere comes a very simple example of an input device driver. The device has
98c2ecf20Sopenharmony_cijust one button and the button is accessible at i/o port BUTTON_PORT. When
108c2ecf20Sopenharmony_cipressed or released a BUTTON_IRQ happens. The driver could look like::
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci    #include <linux/input.h>
138c2ecf20Sopenharmony_ci    #include <linux/module.h>
148c2ecf20Sopenharmony_ci    #include <linux/init.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci    #include <asm/irq.h>
178c2ecf20Sopenharmony_ci    #include <asm/io.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci    static struct input_dev *button_dev;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci    static irqreturn_t button_interrupt(int irq, void *dummy)
228c2ecf20Sopenharmony_ci    {
238c2ecf20Sopenharmony_ci	    input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
248c2ecf20Sopenharmony_ci	    input_sync(button_dev);
258c2ecf20Sopenharmony_ci	    return IRQ_HANDLED;
268c2ecf20Sopenharmony_ci    }
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci    static int __init button_init(void)
298c2ecf20Sopenharmony_ci    {
308c2ecf20Sopenharmony_ci	    int error;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
338c2ecf20Sopenharmony_ci		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
348c2ecf20Sopenharmony_ci		    return -EBUSY;
358c2ecf20Sopenharmony_ci	    }
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	    button_dev = input_allocate_device();
388c2ecf20Sopenharmony_ci	    if (!button_dev) {
398c2ecf20Sopenharmony_ci		    printk(KERN_ERR "button.c: Not enough memory\n");
408c2ecf20Sopenharmony_ci		    error = -ENOMEM;
418c2ecf20Sopenharmony_ci		    goto err_free_irq;
428c2ecf20Sopenharmony_ci	    }
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	    button_dev->evbit[0] = BIT_MASK(EV_KEY);
458c2ecf20Sopenharmony_ci	    button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	    error = input_register_device(button_dev);
488c2ecf20Sopenharmony_ci	    if (error) {
498c2ecf20Sopenharmony_ci		    printk(KERN_ERR "button.c: Failed to register device\n");
508c2ecf20Sopenharmony_ci		    goto err_free_dev;
518c2ecf20Sopenharmony_ci	    }
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	    return 0;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci    err_free_dev:
568c2ecf20Sopenharmony_ci	    input_free_device(button_dev);
578c2ecf20Sopenharmony_ci    err_free_irq:
588c2ecf20Sopenharmony_ci	    free_irq(BUTTON_IRQ, button_interrupt);
598c2ecf20Sopenharmony_ci	    return error;
608c2ecf20Sopenharmony_ci    }
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci    static void __exit button_exit(void)
638c2ecf20Sopenharmony_ci    {
648c2ecf20Sopenharmony_ci	    input_unregister_device(button_dev);
658c2ecf20Sopenharmony_ci	    free_irq(BUTTON_IRQ, button_interrupt);
668c2ecf20Sopenharmony_ci    }
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci    module_init(button_init);
698c2ecf20Sopenharmony_ci    module_exit(button_exit);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ciWhat the example does
728c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciFirst it has to include the <linux/input.h> file, which interfaces to the
758c2ecf20Sopenharmony_ciinput subsystem. This provides all the definitions needed.
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciIn the _init function, which is called either upon module load or when
788c2ecf20Sopenharmony_cibooting the kernel, it grabs the required resources (it should also check
798c2ecf20Sopenharmony_cifor the presence of the device).
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciThen it allocates a new input device structure with input_allocate_device()
828c2ecf20Sopenharmony_ciand sets up input bitfields. This way the device driver tells the other
838c2ecf20Sopenharmony_ciparts of the input systems what it is - what events can be generated or
848c2ecf20Sopenharmony_ciaccepted by this input device. Our example device can only generate EV_KEY
858c2ecf20Sopenharmony_citype events, and from those only BTN_0 event code. Thus we only set these
868c2ecf20Sopenharmony_citwo bits. We could have used::
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	set_bit(EV_KEY, button_dev.evbit);
898c2ecf20Sopenharmony_ci	set_bit(BTN_0, button_dev.keybit);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cias well, but with more than single bits the first approach tends to be
928c2ecf20Sopenharmony_cishorter.
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciThen the example driver registers the input device structure by calling::
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	input_register_device(&button_dev);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciThis adds the button_dev structure to linked lists of the input driver and
998c2ecf20Sopenharmony_cicalls device handler modules _connect functions to tell them a new input
1008c2ecf20Sopenharmony_cidevice has appeared. input_register_device() may sleep and therefore must
1018c2ecf20Sopenharmony_cinot be called from an interrupt or with a spinlock held.
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciWhile in use, the only used function of the driver is::
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	button_interrupt()
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciwhich upon every interrupt from the button checks its state and reports it
1088c2ecf20Sopenharmony_civia the::
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	input_report_key()
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cicall to the input system. There is no need to check whether the interrupt
1138c2ecf20Sopenharmony_ciroutine isn't reporting two same value events (press, press for example) to
1148c2ecf20Sopenharmony_cithe input system, because the input_report_* functions check that
1158c2ecf20Sopenharmony_cithemselves.
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciThen there is the::
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	input_sync()
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cicall to tell those who receive the events that we've sent a complete report.
1228c2ecf20Sopenharmony_ciThis doesn't seem important in the one button case, but is quite important
1238c2ecf20Sopenharmony_cifor for example mouse movement, where you don't want the X and Y values
1248c2ecf20Sopenharmony_cito be interpreted separately, because that'd result in a different movement.
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cidev->open() and dev->close()
1278c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciIn case the driver has to repeatedly poll the device, because it doesn't
1308c2ecf20Sopenharmony_cihave an interrupt coming from it and the polling is too expensive to be done
1318c2ecf20Sopenharmony_ciall the time, or if the device uses a valuable resource (eg. interrupt), it
1328c2ecf20Sopenharmony_cican use the open and close callback to know when it can stop polling or
1338c2ecf20Sopenharmony_cirelease the interrupt and when it must resume polling or grab the interrupt
1348c2ecf20Sopenharmony_ciagain. To do that, we would add this to our example driver::
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci    static int button_open(struct input_dev *dev)
1378c2ecf20Sopenharmony_ci    {
1388c2ecf20Sopenharmony_ci	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
1398c2ecf20Sopenharmony_ci		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
1408c2ecf20Sopenharmony_ci		    return -EBUSY;
1418c2ecf20Sopenharmony_ci	    }
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	    return 0;
1448c2ecf20Sopenharmony_ci    }
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci    static void button_close(struct input_dev *dev)
1478c2ecf20Sopenharmony_ci    {
1488c2ecf20Sopenharmony_ci	    free_irq(IRQ_AMIGA_VERTB, button_interrupt);
1498c2ecf20Sopenharmony_ci    }
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci    static int __init button_init(void)
1528c2ecf20Sopenharmony_ci    {
1538c2ecf20Sopenharmony_ci	    ...
1548c2ecf20Sopenharmony_ci	    button_dev->open = button_open;
1558c2ecf20Sopenharmony_ci	    button_dev->close = button_close;
1568c2ecf20Sopenharmony_ci	    ...
1578c2ecf20Sopenharmony_ci    }
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciNote that input core keeps track of number of users for the device and
1608c2ecf20Sopenharmony_cimakes sure that dev->open() is called only when the first user connects
1618c2ecf20Sopenharmony_cito the device and that dev->close() is called when the very last user
1628c2ecf20Sopenharmony_cidisconnects. Calls to both callbacks are serialized.
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciThe open() callback should return a 0 in case of success or any nonzero value
1658c2ecf20Sopenharmony_ciin case of failure. The close() callback (which is void) must always succeed.
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciBasic event types
1688c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ciThe most simple event type is EV_KEY, which is used for keys and buttons.
1718c2ecf20Sopenharmony_ciIt's reported to the input system via::
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	input_report_key(struct input_dev *dev, int code, int value)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciSee uapi/linux/input-event-codes.h for the allowable values of code (from 0 to
1768c2ecf20Sopenharmony_ciKEY_MAX). Value is interpreted as a truth value, ie any nonzero value means key
1778c2ecf20Sopenharmony_cipressed, zero value means key released. The input code generates events only
1788c2ecf20Sopenharmony_ciin case the value is different from before.
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciIn addition to EV_KEY, there are two more basic event types: EV_REL and
1818c2ecf20Sopenharmony_ciEV_ABS. They are used for relative and absolute values supplied by the
1828c2ecf20Sopenharmony_cidevice. A relative value may be for example a mouse movement in the X axis.
1838c2ecf20Sopenharmony_ciThe mouse reports it as a relative difference from the last position,
1848c2ecf20Sopenharmony_cibecause it doesn't have any absolute coordinate system to work in. Absolute
1858c2ecf20Sopenharmony_cievents are namely for joysticks and digitizers - devices that do work in an
1868c2ecf20Sopenharmony_ciabsolute coordinate systems.
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciHaving the device report EV_REL buttons is as simple as with EV_KEY, simply
1898c2ecf20Sopenharmony_ciset the corresponding bits and call the::
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	input_report_rel(struct input_dev *dev, int code, int value)
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cifunction. Events are generated only for nonzero value.
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ciHowever EV_ABS requires a little special care. Before calling
1968c2ecf20Sopenharmony_ciinput_register_device, you have to fill additional fields in the input_dev
1978c2ecf20Sopenharmony_cistruct for each absolute axis your device has. If our button device had also
1988c2ecf20Sopenharmony_cithe ABS_X axis::
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	button_dev.absmin[ABS_X] = 0;
2018c2ecf20Sopenharmony_ci	button_dev.absmax[ABS_X] = 255;
2028c2ecf20Sopenharmony_ci	button_dev.absfuzz[ABS_X] = 4;
2038c2ecf20Sopenharmony_ci	button_dev.absflat[ABS_X] = 8;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ciOr, you can just say::
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ciThis setting would be appropriate for a joystick X axis, with the minimum of
2108c2ecf20Sopenharmony_ci0, maximum of 255 (which the joystick *must* be able to reach, no problem if
2118c2ecf20Sopenharmony_ciit sometimes reports more, but it must be able to always reach the min and
2128c2ecf20Sopenharmony_cimax values), with noise in the data up to +- 4, and with a center flat
2138c2ecf20Sopenharmony_ciposition of size 8.
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciIf you don't need absfuzz and absflat, you can set them to zero, which mean
2168c2ecf20Sopenharmony_cithat the thing is precise and always returns to exactly the center position
2178c2ecf20Sopenharmony_ci(if it has any).
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ciBITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
2208c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ciThese three macros from bitops.h help some bitfield computations::
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
2258c2ecf20Sopenharmony_ci			   x bits
2268c2ecf20Sopenharmony_ci	BIT_WORD(x)	 - returns the index in the array in longs for bit x
2278c2ecf20Sopenharmony_ci	BIT_MASK(x)	 - returns the index in a long for bit x
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ciThe id* and name fields
2308c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciThe dev->name should be set before registering the input device by the input
2338c2ecf20Sopenharmony_cidevice driver. It's a string like 'Generic button device' containing a
2348c2ecf20Sopenharmony_ciuser friendly name of the device.
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ciThe id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
2378c2ecf20Sopenharmony_ciof the device. The bus IDs are defined in input.h. The vendor and device ids
2388c2ecf20Sopenharmony_ciare defined in pci_ids.h, usb_ids.h and similar include files. These fields
2398c2ecf20Sopenharmony_cishould be set by the input device driver before registering it.
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciThe idtype field can be used for specific information for the input device
2428c2ecf20Sopenharmony_cidriver.
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ciThe id and name fields can be passed to userland via the evdev interface.
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ciThe keycode, keycodemax, keycodesize fields
2478c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ciThese three fields should be used by input devices that have dense keymaps.
2508c2ecf20Sopenharmony_ciThe keycode is an array used to map from scancodes to input system keycodes.
2518c2ecf20Sopenharmony_ciThe keycode max should contain the size of the array and keycodesize the
2528c2ecf20Sopenharmony_cisize of each entry in it (in bytes).
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciUserspace can query and alter current scancode to keycode mappings using
2558c2ecf20Sopenharmony_ciEVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
2568c2ecf20Sopenharmony_ciWhen a device has all 3 aforementioned fields filled in, the driver may
2578c2ecf20Sopenharmony_cirely on kernel's default implementation of setting and querying keycode
2588c2ecf20Sopenharmony_cimappings.
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cidev->getkeycode() and dev->setkeycode()
2618c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cigetkeycode() and setkeycode() callbacks allow drivers to override default
2648c2ecf20Sopenharmony_cikeycode/keycodesize/keycodemax mapping mechanism provided by input core
2658c2ecf20Sopenharmony_ciand implement sparse keycode maps.
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ciKey autorepeat
2688c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci... is simple. It is handled by the input.c module. Hardware autorepeat is
2718c2ecf20Sopenharmony_cinot used, because it's not present in many devices and even where it is
2728c2ecf20Sopenharmony_cipresent, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
2738c2ecf20Sopenharmony_ciautorepeat for your device, just set EV_REP in dev->evbit. All will be
2748c2ecf20Sopenharmony_cihandled by the input system.
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ciOther event types, handling output events
2778c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ciThe other event types up to now are:
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci- EV_LED - used for the keyboard LEDs.
2828c2ecf20Sopenharmony_ci- EV_SND - used for keyboard beeps.
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ciThey are very similar to for example key events, but they go in the other
2858c2ecf20Sopenharmony_cidirection - from the system to the input device driver. If your input device
2868c2ecf20Sopenharmony_cidriver can handle these events, it has to set the respective bits in evbit,
2878c2ecf20Sopenharmony_ci*and* also the callback routine::
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci    button_dev->event = button_event;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci    int button_event(struct input_dev *dev, unsigned int type,
2928c2ecf20Sopenharmony_ci		     unsigned int code, int value)
2938c2ecf20Sopenharmony_ci    {
2948c2ecf20Sopenharmony_ci	    if (type == EV_SND && code == SND_BELL) {
2958c2ecf20Sopenharmony_ci		    outb(value, BUTTON_BELL);
2968c2ecf20Sopenharmony_ci		    return 0;
2978c2ecf20Sopenharmony_ci	    }
2988c2ecf20Sopenharmony_ci	    return -1;
2998c2ecf20Sopenharmony_ci    }
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ciThis callback routine can be called from an interrupt or a BH (although that
3028c2ecf20Sopenharmony_ciisn't a rule), and thus must not sleep, and must not take too long to finish.
303