xref: /OK3568_Linux_fs/kernel/Documentation/input/input-programming.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun===============================
2*4882a593SmuzhiyunCreating an input device driver
3*4882a593Smuzhiyun===============================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThe simplest example
6*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunHere comes a very simple example of an input device driver. The device has
9*4882a593Smuzhiyunjust one button and the button is accessible at i/o port BUTTON_PORT. When
10*4882a593Smuzhiyunpressed or released a BUTTON_IRQ happens. The driver could look like::
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun    #include <linux/input.h>
13*4882a593Smuzhiyun    #include <linux/module.h>
14*4882a593Smuzhiyun    #include <linux/init.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun    #include <asm/irq.h>
17*4882a593Smuzhiyun    #include <asm/io.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun    static struct input_dev *button_dev;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun    static irqreturn_t button_interrupt(int irq, void *dummy)
22*4882a593Smuzhiyun    {
23*4882a593Smuzhiyun	    input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
24*4882a593Smuzhiyun	    input_sync(button_dev);
25*4882a593Smuzhiyun	    return IRQ_HANDLED;
26*4882a593Smuzhiyun    }
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun    static int __init button_init(void)
29*4882a593Smuzhiyun    {
30*4882a593Smuzhiyun	    int error;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
33*4882a593Smuzhiyun		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
34*4882a593Smuzhiyun		    return -EBUSY;
35*4882a593Smuzhiyun	    }
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun	    button_dev = input_allocate_device();
38*4882a593Smuzhiyun	    if (!button_dev) {
39*4882a593Smuzhiyun		    printk(KERN_ERR "button.c: Not enough memory\n");
40*4882a593Smuzhiyun		    error = -ENOMEM;
41*4882a593Smuzhiyun		    goto err_free_irq;
42*4882a593Smuzhiyun	    }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun	    button_dev->evbit[0] = BIT_MASK(EV_KEY);
45*4882a593Smuzhiyun	    button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun	    error = input_register_device(button_dev);
48*4882a593Smuzhiyun	    if (error) {
49*4882a593Smuzhiyun		    printk(KERN_ERR "button.c: Failed to register device\n");
50*4882a593Smuzhiyun		    goto err_free_dev;
51*4882a593Smuzhiyun	    }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun	    return 0;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun    err_free_dev:
56*4882a593Smuzhiyun	    input_free_device(button_dev);
57*4882a593Smuzhiyun    err_free_irq:
58*4882a593Smuzhiyun	    free_irq(BUTTON_IRQ, button_interrupt);
59*4882a593Smuzhiyun	    return error;
60*4882a593Smuzhiyun    }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun    static void __exit button_exit(void)
63*4882a593Smuzhiyun    {
64*4882a593Smuzhiyun	    input_unregister_device(button_dev);
65*4882a593Smuzhiyun	    free_irq(BUTTON_IRQ, button_interrupt);
66*4882a593Smuzhiyun    }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun    module_init(button_init);
69*4882a593Smuzhiyun    module_exit(button_exit);
70*4882a593Smuzhiyun
71*4882a593SmuzhiyunWhat the example does
72*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~
73*4882a593Smuzhiyun
74*4882a593SmuzhiyunFirst it has to include the <linux/input.h> file, which interfaces to the
75*4882a593Smuzhiyuninput subsystem. This provides all the definitions needed.
76*4882a593Smuzhiyun
77*4882a593SmuzhiyunIn the _init function, which is called either upon module load or when
78*4882a593Smuzhiyunbooting the kernel, it grabs the required resources (it should also check
79*4882a593Smuzhiyunfor the presence of the device).
80*4882a593Smuzhiyun
81*4882a593SmuzhiyunThen it allocates a new input device structure with input_allocate_device()
82*4882a593Smuzhiyunand sets up input bitfields. This way the device driver tells the other
83*4882a593Smuzhiyunparts of the input systems what it is - what events can be generated or
84*4882a593Smuzhiyunaccepted by this input device. Our example device can only generate EV_KEY
85*4882a593Smuzhiyuntype events, and from those only BTN_0 event code. Thus we only set these
86*4882a593Smuzhiyuntwo bits. We could have used::
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun	set_bit(EV_KEY, button_dev.evbit);
89*4882a593Smuzhiyun	set_bit(BTN_0, button_dev.keybit);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyunas well, but with more than single bits the first approach tends to be
92*4882a593Smuzhiyunshorter.
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunThen the example driver registers the input device structure by calling::
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun	input_register_device(&button_dev);
97*4882a593Smuzhiyun
98*4882a593SmuzhiyunThis adds the button_dev structure to linked lists of the input driver and
99*4882a593Smuzhiyuncalls device handler modules _connect functions to tell them a new input
100*4882a593Smuzhiyundevice has appeared. input_register_device() may sleep and therefore must
101*4882a593Smuzhiyunnot be called from an interrupt or with a spinlock held.
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunWhile in use, the only used function of the driver is::
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun	button_interrupt()
106*4882a593Smuzhiyun
107*4882a593Smuzhiyunwhich upon every interrupt from the button checks its state and reports it
108*4882a593Smuzhiyunvia the::
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun	input_report_key()
111*4882a593Smuzhiyun
112*4882a593Smuzhiyuncall to the input system. There is no need to check whether the interrupt
113*4882a593Smuzhiyunroutine isn't reporting two same value events (press, press for example) to
114*4882a593Smuzhiyunthe input system, because the input_report_* functions check that
115*4882a593Smuzhiyunthemselves.
116*4882a593Smuzhiyun
117*4882a593SmuzhiyunThen there is the::
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun	input_sync()
120*4882a593Smuzhiyun
121*4882a593Smuzhiyuncall to tell those who receive the events that we've sent a complete report.
122*4882a593SmuzhiyunThis doesn't seem important in the one button case, but is quite important
123*4882a593Smuzhiyunfor for example mouse movement, where you don't want the X and Y values
124*4882a593Smuzhiyunto be interpreted separately, because that'd result in a different movement.
125*4882a593Smuzhiyun
126*4882a593Smuzhiyundev->open() and dev->close()
127*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128*4882a593Smuzhiyun
129*4882a593SmuzhiyunIn case the driver has to repeatedly poll the device, because it doesn't
130*4882a593Smuzhiyunhave an interrupt coming from it and the polling is too expensive to be done
131*4882a593Smuzhiyunall the time, or if the device uses a valuable resource (eg. interrupt), it
132*4882a593Smuzhiyuncan use the open and close callback to know when it can stop polling or
133*4882a593Smuzhiyunrelease the interrupt and when it must resume polling or grab the interrupt
134*4882a593Smuzhiyunagain. To do that, we would add this to our example driver::
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun    static int button_open(struct input_dev *dev)
137*4882a593Smuzhiyun    {
138*4882a593Smuzhiyun	    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
139*4882a593Smuzhiyun		    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
140*4882a593Smuzhiyun		    return -EBUSY;
141*4882a593Smuzhiyun	    }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun	    return 0;
144*4882a593Smuzhiyun    }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun    static void button_close(struct input_dev *dev)
147*4882a593Smuzhiyun    {
148*4882a593Smuzhiyun	    free_irq(IRQ_AMIGA_VERTB, button_interrupt);
149*4882a593Smuzhiyun    }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun    static int __init button_init(void)
152*4882a593Smuzhiyun    {
153*4882a593Smuzhiyun	    ...
154*4882a593Smuzhiyun	    button_dev->open = button_open;
155*4882a593Smuzhiyun	    button_dev->close = button_close;
156*4882a593Smuzhiyun	    ...
157*4882a593Smuzhiyun    }
158*4882a593Smuzhiyun
159*4882a593SmuzhiyunNote that input core keeps track of number of users for the device and
160*4882a593Smuzhiyunmakes sure that dev->open() is called only when the first user connects
161*4882a593Smuzhiyunto the device and that dev->close() is called when the very last user
162*4882a593Smuzhiyundisconnects. Calls to both callbacks are serialized.
163*4882a593Smuzhiyun
164*4882a593SmuzhiyunThe open() callback should return a 0 in case of success or any nonzero value
165*4882a593Smuzhiyunin case of failure. The close() callback (which is void) must always succeed.
166*4882a593Smuzhiyun
167*4882a593SmuzhiyunBasic event types
168*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunThe most simple event type is EV_KEY, which is used for keys and buttons.
171*4882a593SmuzhiyunIt's reported to the input system via::
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun	input_report_key(struct input_dev *dev, int code, int value)
174*4882a593Smuzhiyun
175*4882a593SmuzhiyunSee uapi/linux/input-event-codes.h for the allowable values of code (from 0 to
176*4882a593SmuzhiyunKEY_MAX). Value is interpreted as a truth value, ie any nonzero value means key
177*4882a593Smuzhiyunpressed, zero value means key released. The input code generates events only
178*4882a593Smuzhiyunin case the value is different from before.
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunIn addition to EV_KEY, there are two more basic event types: EV_REL and
181*4882a593SmuzhiyunEV_ABS. They are used for relative and absolute values supplied by the
182*4882a593Smuzhiyundevice. A relative value may be for example a mouse movement in the X axis.
183*4882a593SmuzhiyunThe mouse reports it as a relative difference from the last position,
184*4882a593Smuzhiyunbecause it doesn't have any absolute coordinate system to work in. Absolute
185*4882a593Smuzhiyunevents are namely for joysticks and digitizers - devices that do work in an
186*4882a593Smuzhiyunabsolute coordinate systems.
187*4882a593Smuzhiyun
188*4882a593SmuzhiyunHaving the device report EV_REL buttons is as simple as with EV_KEY, simply
189*4882a593Smuzhiyunset the corresponding bits and call the::
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun	input_report_rel(struct input_dev *dev, int code, int value)
192*4882a593Smuzhiyun
193*4882a593Smuzhiyunfunction. Events are generated only for nonzero value.
194*4882a593Smuzhiyun
195*4882a593SmuzhiyunHowever EV_ABS requires a little special care. Before calling
196*4882a593Smuzhiyuninput_register_device, you have to fill additional fields in the input_dev
197*4882a593Smuzhiyunstruct for each absolute axis your device has. If our button device had also
198*4882a593Smuzhiyunthe ABS_X axis::
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun	button_dev.absmin[ABS_X] = 0;
201*4882a593Smuzhiyun	button_dev.absmax[ABS_X] = 255;
202*4882a593Smuzhiyun	button_dev.absfuzz[ABS_X] = 4;
203*4882a593Smuzhiyun	button_dev.absflat[ABS_X] = 8;
204*4882a593Smuzhiyun
205*4882a593SmuzhiyunOr, you can just say::
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun	input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
208*4882a593Smuzhiyun
209*4882a593SmuzhiyunThis setting would be appropriate for a joystick X axis, with the minimum of
210*4882a593Smuzhiyun0, maximum of 255 (which the joystick *must* be able to reach, no problem if
211*4882a593Smuzhiyunit sometimes reports more, but it must be able to always reach the min and
212*4882a593Smuzhiyunmax values), with noise in the data up to +- 4, and with a center flat
213*4882a593Smuzhiyunposition of size 8.
214*4882a593Smuzhiyun
215*4882a593SmuzhiyunIf you don't need absfuzz and absflat, you can set them to zero, which mean
216*4882a593Smuzhiyunthat the thing is precise and always returns to exactly the center position
217*4882a593Smuzhiyun(if it has any).
218*4882a593Smuzhiyun
219*4882a593SmuzhiyunBITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
220*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221*4882a593Smuzhiyun
222*4882a593SmuzhiyunThese three macros from bitops.h help some bitfield computations::
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun	BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
225*4882a593Smuzhiyun			   x bits
226*4882a593Smuzhiyun	BIT_WORD(x)	 - returns the index in the array in longs for bit x
227*4882a593Smuzhiyun	BIT_MASK(x)	 - returns the index in a long for bit x
228*4882a593Smuzhiyun
229*4882a593SmuzhiyunThe id* and name fields
230*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~
231*4882a593Smuzhiyun
232*4882a593SmuzhiyunThe dev->name should be set before registering the input device by the input
233*4882a593Smuzhiyundevice driver. It's a string like 'Generic button device' containing a
234*4882a593Smuzhiyunuser friendly name of the device.
235*4882a593Smuzhiyun
236*4882a593SmuzhiyunThe id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
237*4882a593Smuzhiyunof the device. The bus IDs are defined in input.h. The vendor and device ids
238*4882a593Smuzhiyunare defined in pci_ids.h, usb_ids.h and similar include files. These fields
239*4882a593Smuzhiyunshould be set by the input device driver before registering it.
240*4882a593Smuzhiyun
241*4882a593SmuzhiyunThe idtype field can be used for specific information for the input device
242*4882a593Smuzhiyundriver.
243*4882a593Smuzhiyun
244*4882a593SmuzhiyunThe id and name fields can be passed to userland via the evdev interface.
245*4882a593Smuzhiyun
246*4882a593SmuzhiyunThe keycode, keycodemax, keycodesize fields
247*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
248*4882a593Smuzhiyun
249*4882a593SmuzhiyunThese three fields should be used by input devices that have dense keymaps.
250*4882a593SmuzhiyunThe keycode is an array used to map from scancodes to input system keycodes.
251*4882a593SmuzhiyunThe keycode max should contain the size of the array and keycodesize the
252*4882a593Smuzhiyunsize of each entry in it (in bytes).
253*4882a593Smuzhiyun
254*4882a593SmuzhiyunUserspace can query and alter current scancode to keycode mappings using
255*4882a593SmuzhiyunEVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
256*4882a593SmuzhiyunWhen a device has all 3 aforementioned fields filled in, the driver may
257*4882a593Smuzhiyunrely on kernel's default implementation of setting and querying keycode
258*4882a593Smuzhiyunmappings.
259*4882a593Smuzhiyun
260*4882a593Smuzhiyundev->getkeycode() and dev->setkeycode()
261*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262*4882a593Smuzhiyun
263*4882a593Smuzhiyungetkeycode() and setkeycode() callbacks allow drivers to override default
264*4882a593Smuzhiyunkeycode/keycodesize/keycodemax mapping mechanism provided by input core
265*4882a593Smuzhiyunand implement sparse keycode maps.
266*4882a593Smuzhiyun
267*4882a593SmuzhiyunKey autorepeat
268*4882a593Smuzhiyun~~~~~~~~~~~~~~
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun... is simple. It is handled by the input.c module. Hardware autorepeat is
271*4882a593Smuzhiyunnot used, because it's not present in many devices and even where it is
272*4882a593Smuzhiyunpresent, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
273*4882a593Smuzhiyunautorepeat for your device, just set EV_REP in dev->evbit. All will be
274*4882a593Smuzhiyunhandled by the input system.
275*4882a593Smuzhiyun
276*4882a593SmuzhiyunOther event types, handling output events
277*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278*4882a593Smuzhiyun
279*4882a593SmuzhiyunThe other event types up to now are:
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun- EV_LED - used for the keyboard LEDs.
282*4882a593Smuzhiyun- EV_SND - used for keyboard beeps.
283*4882a593Smuzhiyun
284*4882a593SmuzhiyunThey are very similar to for example key events, but they go in the other
285*4882a593Smuzhiyundirection - from the system to the input device driver. If your input device
286*4882a593Smuzhiyundriver can handle these events, it has to set the respective bits in evbit,
287*4882a593Smuzhiyun*and* also the callback routine::
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun    button_dev->event = button_event;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun    int button_event(struct input_dev *dev, unsigned int type,
292*4882a593Smuzhiyun		     unsigned int code, int value)
293*4882a593Smuzhiyun    {
294*4882a593Smuzhiyun	    if (type == EV_SND && code == SND_BELL) {
295*4882a593Smuzhiyun		    outb(value, BUTTON_BELL);
296*4882a593Smuzhiyun		    return 0;
297*4882a593Smuzhiyun	    }
298*4882a593Smuzhiyun	    return -1;
299*4882a593Smuzhiyun    }
300*4882a593Smuzhiyun
301*4882a593SmuzhiyunThis callback routine can be called from an interrupt or a BH (although that
302*4882a593Smuzhiyunisn't a rule), and thus must not sleep, and must not take too long to finish.
303