summaryrefslogtreecommitdiffstats
path: root/Documentation/input
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@s-opensource.com>2017-04-04 20:44:04 -0400
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2017-04-05 18:44:56 -0400
commit1c4ada609d6a0b9ddd4621cef6ac57790289ff4f (patch)
tree7eb22fd2d6d46bb6c74548dcdf5afd171d84361c /Documentation/input
parentf863995224da8e63e8703ca9eb0b2e29d93f5d66 (diff)
Input: convert input-programming doc into ReST format
This file require minimum adjustments to be a valid ReST file. Do it, in order to be able to parse it with Sphinx. Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Diffstat (limited to 'Documentation/input')
-rw-r--r--Documentation/input/input-programming.txt265
1 files changed, 134 insertions, 131 deletions
diff --git a/Documentation/input/input-programming.txt b/Documentation/input/input-programming.txt
index 7f8b9d97bc47..4d3b22222e93 100644
--- a/Documentation/input/input-programming.txt
+++ b/Documentation/input/input-programming.txt
@@ -1,77 +1,78 @@
1~~~~~~~~~~~~~~~~~~~~~~~~~
1Programming input drivers 2Programming input drivers
2~~~~~~~~~~~~~~~~~~~~~~~~~ 3~~~~~~~~~~~~~~~~~~~~~~~~~
3 4
41. Creating an input device driver 5Creating an input device driver
5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 6===============================
6 7
71.0 The simplest example 8The simplest example
8~~~~~~~~~~~~~~~~~~~~~~~~ 9~~~~~~~~~~~~~~~~~~~~
9 10
10Here comes a very simple example of an input device driver. The device has 11Here comes a very simple example of an input device driver. The device has
11just one button and the button is accessible at i/o port BUTTON_PORT. When 12just one button and the button is accessible at i/o port BUTTON_PORT. When
12pressed or released a BUTTON_IRQ happens. The driver could look like: 13pressed or released a BUTTON_IRQ happens. The driver could look like::
13 14
14#include <linux/input.h> 15 #include <linux/input.h>
15#include <linux/module.h> 16 #include <linux/module.h>
16#include <linux/init.h> 17 #include <linux/init.h>
17 18
18#include <asm/irq.h> 19 #include <asm/irq.h>
19#include <asm/io.h> 20 #include <asm/io.h>
20 21
21static struct input_dev *button_dev; 22 static struct input_dev *button_dev;
22 23
23static irqreturn_t button_interrupt(int irq, void *dummy) 24 static irqreturn_t button_interrupt(int irq, void *dummy)
24{ 25 {
25 input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1); 26 input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
26 input_sync(button_dev); 27 input_sync(button_dev);
27 return IRQ_HANDLED; 28 return IRQ_HANDLED;
28} 29 }
29 30
30static int __init button_init(void) 31 static int __init button_init(void)
31{ 32 {
32 int error; 33 int error;
33 34
34 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) { 35 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
35 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq); 36 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
36 return -EBUSY; 37 return -EBUSY;
37 } 38 }
38 39
39 button_dev = input_allocate_device(); 40 button_dev = input_allocate_device();
40 if (!button_dev) { 41 if (!button_dev) {
41 printk(KERN_ERR "button.c: Not enough memory\n"); 42 printk(KERN_ERR "button.c: Not enough memory\n");
42 error = -ENOMEM; 43 error = -ENOMEM;
43 goto err_free_irq; 44 goto err_free_irq;
44 } 45 }
45 46
46 button_dev->evbit[0] = BIT_MASK(EV_KEY); 47 button_dev->evbit[0] = BIT_MASK(EV_KEY);
47 button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0); 48 button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
48 49
49 error = input_register_device(button_dev); 50 error = input_register_device(button_dev);
50 if (error) { 51 if (error) {
51 printk(KERN_ERR "button.c: Failed to register device\n"); 52 printk(KERN_ERR "button.c: Failed to register device\n");
52 goto err_free_dev; 53 goto err_free_dev;
53 } 54 }
54 55
55 return 0; 56 return 0;
56 57
57 err_free_dev: 58 err_free_dev:
58 input_free_device(button_dev); 59 input_free_device(button_dev);
59 err_free_irq: 60 err_free_irq:
60 free_irq(BUTTON_IRQ, button_interrupt); 61 free_irq(BUTTON_IRQ, button_interrupt);
61 return error; 62 return error;
62} 63 }
63 64
64static void __exit button_exit(void) 65 static void __exit button_exit(void)
65{ 66 {
66 input_unregister_device(button_dev); 67 input_unregister_device(button_dev);
67 free_irq(BUTTON_IRQ, button_interrupt); 68 free_irq(BUTTON_IRQ, button_interrupt);
68} 69 }
69 70
70module_init(button_init); 71 module_init(button_init);
71module_exit(button_exit); 72 module_exit(button_exit);
72 73
731.1 What the example does 74What the example does
74~~~~~~~~~~~~~~~~~~~~~~~~~ 75~~~~~~~~~~~~~~~~~~~~~
75 76
76First it has to include the <linux/input.h> file, which interfaces to the 77First it has to include the <linux/input.h> file, which interfaces to the
77input subsystem. This provides all the definitions needed. 78input subsystem. This provides all the definitions needed.
@@ -85,7 +86,7 @@ and sets up input bitfields. This way the device driver tells the other
85parts of the input systems what it is - what events can be generated or 86parts of the input systems what it is - what events can be generated or
86accepted by this input device. Our example device can only generate EV_KEY 87accepted by this input device. Our example device can only generate EV_KEY
87type events, and from those only BTN_0 event code. Thus we only set these 88type events, and from those only BTN_0 event code. Thus we only set these
88two bits. We could have used 89two bits. We could have used::
89 90
90 set_bit(EV_KEY, button_dev.evbit); 91 set_bit(EV_KEY, button_dev.evbit);
91 set_bit(BTN_0, button_dev.keybit); 92 set_bit(BTN_0, button_dev.keybit);
@@ -93,7 +94,7 @@ two bits. We could have used
93as well, but with more than single bits the first approach tends to be 94as well, but with more than single bits the first approach tends to be
94shorter. 95shorter.
95 96
96Then the example driver registers the input device structure by calling 97Then the example driver registers the input device structure by calling::
97 98
98 input_register_device(&button_dev); 99 input_register_device(&button_dev);
99 100
@@ -102,12 +103,12 @@ calls device handler modules _connect functions to tell them a new input
102device has appeared. input_register_device() may sleep and therefore must 103device has appeared. input_register_device() may sleep and therefore must
103not be called from an interrupt or with a spinlock held. 104not be called from an interrupt or with a spinlock held.
104 105
105While in use, the only used function of the driver is 106While in use, the only used function of the driver is::
106 107
107 button_interrupt() 108 button_interrupt()
108 109
109which upon every interrupt from the button checks its state and reports it 110which upon every interrupt from the button checks its state and reports it
110via the 111via the::
111 112
112 input_report_key() 113 input_report_key()
113 114
@@ -116,7 +117,7 @@ routine isn't reporting two same value events (press, press for example) to
116the input system, because the input_report_* functions check that 117the input system, because the input_report_* functions check that
117themselves. 118themselves.
118 119
119Then there is the 120Then there is the::
120 121
121 input_sync() 122 input_sync()
122 123
@@ -125,38 +126,38 @@ This doesn't seem important in the one button case, but is quite important
125for for example mouse movement, where you don't want the X and Y values 126for for example mouse movement, where you don't want the X and Y values
126to be interpreted separately, because that'd result in a different movement. 127to be interpreted separately, because that'd result in a different movement.
127 128
1281.2 dev->open() and dev->close() 129dev->open() and dev->close()
129~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 130~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130 131
131In case the driver has to repeatedly poll the device, because it doesn't 132In case the driver has to repeatedly poll the device, because it doesn't
132have an interrupt coming from it and the polling is too expensive to be done 133have an interrupt coming from it and the polling is too expensive to be done
133all the time, or if the device uses a valuable resource (eg. interrupt), it 134all the time, or if the device uses a valuable resource (eg. interrupt), it
134can use the open and close callback to know when it can stop polling or 135can use the open and close callback to know when it can stop polling or
135release the interrupt and when it must resume polling or grab the interrupt 136release the interrupt and when it must resume polling or grab the interrupt
136again. To do that, we would add this to our example driver: 137again. To do that, we would add this to our example driver::
137 138
138static int button_open(struct input_dev *dev) 139 static int button_open(struct input_dev *dev)
139{ 140 {
140 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) { 141 if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
141 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq); 142 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
142 return -EBUSY; 143 return -EBUSY;
143 } 144 }
144 145
145 return 0; 146 return 0;
146} 147 }
147 148
148static void button_close(struct input_dev *dev) 149 static void button_close(struct input_dev *dev)
149{ 150 {
150 free_irq(IRQ_AMIGA_VERTB, button_interrupt); 151 free_irq(IRQ_AMIGA_VERTB, button_interrupt);
151} 152 }
152 153
153static int __init button_init(void) 154 static int __init button_init(void)
154{ 155 {
155 ... 156 ...
156 button_dev->open = button_open; 157 button_dev->open = button_open;
157 button_dev->close = button_close; 158 button_dev->close = button_close;
158 ... 159 ...
159} 160 }
160 161
161Note that input core keeps track of number of users for the device and 162Note that input core keeps track of number of users for the device and
162makes sure that dev->open() is called only when the first user connects 163makes sure that dev->open() is called only when the first user connects
@@ -166,11 +167,11 @@ disconnects. Calls to both callbacks are serialized.
166The open() callback should return a 0 in case of success or any nonzero value 167The open() callback should return a 0 in case of success or any nonzero value
167in case of failure. The close() callback (which is void) must always succeed. 168in case of failure. The close() callback (which is void) must always succeed.
168 169
1691.3 Basic event types 170Basic event types
170~~~~~~~~~~~~~~~~~~~~~ 171~~~~~~~~~~~~~~~~~
171 172
172The most simple event type is EV_KEY, which is used for keys and buttons. 173The most simple event type is EV_KEY, which is used for keys and buttons.
173It's reported to the input system via: 174It's reported to the input system via::
174 175
175 input_report_key(struct input_dev *dev, int code, int value) 176 input_report_key(struct input_dev *dev, int code, int value)
176 177
@@ -188,7 +189,7 @@ events are namely for joysticks and digitizers - devices that do work in an
188absolute coordinate systems. 189absolute coordinate systems.
189 190
190Having the device report EV_REL buttons is as simple as with EV_KEY, simply 191Having the device report EV_REL buttons is as simple as with EV_KEY, simply
191set the corresponding bits and call the 192set the corresponding bits and call the::
192 193
193 input_report_rel(struct input_dev *dev, int code, int value) 194 input_report_rel(struct input_dev *dev, int code, int value)
194 195
@@ -197,14 +198,14 @@ function. Events are generated only for nonzero value.
197However EV_ABS requires a little special care. Before calling 198However EV_ABS requires a little special care. Before calling
198input_register_device, you have to fill additional fields in the input_dev 199input_register_device, you have to fill additional fields in the input_dev
199struct for each absolute axis your device has. If our button device had also 200struct for each absolute axis your device has. If our button device had also
200the ABS_X axis: 201the ABS_X axis::
201 202
202 button_dev.absmin[ABS_X] = 0; 203 button_dev.absmin[ABS_X] = 0;
203 button_dev.absmax[ABS_X] = 255; 204 button_dev.absmax[ABS_X] = 255;
204 button_dev.absfuzz[ABS_X] = 4; 205 button_dev.absfuzz[ABS_X] = 4;
205 button_dev.absflat[ABS_X] = 8; 206 button_dev.absflat[ABS_X] = 8;
206 207
207Or, you can just say: 208Or, you can just say::
208 209
209 input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8); 210 input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);
210 211
@@ -218,18 +219,18 @@ If you don't need absfuzz and absflat, you can set them to zero, which mean
218that the thing is precise and always returns to exactly the center position 219that the thing is precise and always returns to exactly the center position
219(if it has any). 220(if it has any).
220 221
2211.4 BITS_TO_LONGS(), BIT_WORD(), BIT_MASK() 222BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
222~~~~~~~~~~~~~~~~~~~~~~~~~~ 223~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223 224
224These three macros from bitops.h help some bitfield computations: 225These three macros from bitops.h help some bitfield computations::
225 226
226 BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for 227 BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
227 x bits 228 x bits
228 BIT_WORD(x) - returns the index in the array in longs for bit x 229 BIT_WORD(x) - returns the index in the array in longs for bit x
229 BIT_MASK(x) - returns the index in a long for bit x 230 BIT_MASK(x) - returns the index in a long for bit x
230 231
2311.5 The id* and name fields 232The id* and name fields
232~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 233~~~~~~~~~~~~~~~~~~~~~~~
233 234
234The dev->name should be set before registering the input device by the input 235The dev->name should be set before registering the input device by the input
235device driver. It's a string like 'Generic button device' containing a 236device driver. It's a string like 'Generic button device' containing a
@@ -245,8 +246,8 @@ driver.
245 246
246The id and name fields can be passed to userland via the evdev interface. 247The id and name fields can be passed to userland via the evdev interface.
247 248
2481.6 The keycode, keycodemax, keycodesize fields 249The keycode, keycodemax, keycodesize fields
249~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 250~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250 251
251These three fields should be used by input devices that have dense keymaps. 252These three fields should be used by input devices that have dense keymaps.
252The keycode is an array used to map from scancodes to input system keycodes. 253The keycode is an array used to map from scancodes to input system keycodes.
@@ -259,14 +260,15 @@ When a device has all 3 aforementioned fields filled in, the driver may
259rely on kernel's default implementation of setting and querying keycode 260rely on kernel's default implementation of setting and querying keycode
260mappings. 261mappings.
261 262
2621.7 dev->getkeycode() and dev->setkeycode() 263dev->getkeycode() and dev->setkeycode()
263~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 264~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
265
264getkeycode() and setkeycode() callbacks allow drivers to override default 266getkeycode() and setkeycode() callbacks allow drivers to override default
265keycode/keycodesize/keycodemax mapping mechanism provided by input core 267keycode/keycodesize/keycodemax mapping mechanism provided by input core
266and implement sparse keycode maps. 268and implement sparse keycode maps.
267 269
2681.8 Key autorepeat 270Key autorepeat
269~~~~~~~~~~~~~~~~~~ 271~~~~~~~~~~~~~~
270 272
271... is simple. It is handled by the input.c module. Hardware autorepeat is 273... is simple. It is handled by the input.c module. Hardware autorepeat is
272not used, because it's not present in many devices and even where it is 274not used, because it's not present in many devices and even where it is
@@ -274,29 +276,30 @@ present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
274autorepeat for your device, just set EV_REP in dev->evbit. All will be 276autorepeat for your device, just set EV_REP in dev->evbit. All will be
275handled by the input system. 277handled by the input system.
276 278
2771.9 Other event types, handling output events 279Other event types, handling output events
278~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 280~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
279 281
280The other event types up to now are: 282The other event types up to now are:
281 283
282EV_LED - used for the keyboard LEDs. 284- EV_LED - used for the keyboard LEDs.
283EV_SND - used for keyboard beeps. 285- EV_SND - used for keyboard beeps.
284 286
285They are very similar to for example key events, but they go in the other 287They are very similar to for example key events, but they go in the other
286direction - from the system to the input device driver. If your input device 288direction - from the system to the input device driver. If your input device
287driver can handle these events, it has to set the respective bits in evbit, 289driver can handle these events, it has to set the respective bits in evbit,
288*and* also the callback routine: 290*and* also the callback routine::
289 291
290 button_dev->event = button_event; 292 button_dev->event = button_event;
291 293
292int button_event(struct input_dev *dev, unsigned int type, unsigned int code, int value); 294 int button_event(struct input_dev *dev, unsigned int type,
293{ 295 unsigned int code, int value)
294 if (type == EV_SND && code == SND_BELL) { 296 {
295 outb(value, BUTTON_BELL); 297 if (type == EV_SND && code == SND_BELL) {
296 return 0; 298 outb(value, BUTTON_BELL);
297 } 299 return 0;
298 return -1; 300 }
299} 301 return -1;
302 }
300 303
301This callback routine can be called from an interrupt or a BH (although that 304This callback routine can be called from an interrupt or a BH (although that
302isn't a rule), and thus must not sleep, and must not take too long to finish. 305isn't a rule), and thus must not sleep, and must not take too long to finish.