1 |
1275 |
phoenix |
$Id: input-programming.txt,v 1.1.1.1 2004-04-15 02:32:41 phoenix Exp $
|
2 |
|
|
|
3 |
|
|
Programming input drivers
|
4 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
5 |
|
|
|
6 |
|
|
1. Creating an input device driver
|
7 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
8 |
|
|
|
9 |
|
|
1.0 The simplest example
|
10 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
11 |
|
|
|
12 |
|
|
Here comes a very simple example of an input device driver. The device has
|
13 |
|
|
just one button and the button is accessible at i/o port BUTTON_PORT. When
|
14 |
|
|
pressed or released a BUTTON_IRQ happens. The driver could look like:
|
15 |
|
|
|
16 |
|
|
#include
|
17 |
|
|
#include
|
18 |
|
|
#include
|
19 |
|
|
|
20 |
|
|
#include
|
21 |
|
|
#include
|
22 |
|
|
|
23 |
|
|
static void button_interrupt(int irq, void *dummy, struct pt_regs *fp)
|
24 |
|
|
{
|
25 |
|
|
input_report_key(&button_dev, BTN_1, inb(BUTTON_PORT) & 1);
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
static int __init button_init(void)
|
29 |
|
|
{
|
30 |
|
|
if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
|
31 |
|
|
printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
|
32 |
|
|
return -EBUSY;
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
button_dev.evbit[0] = BIT(EV_KEY);
|
36 |
|
|
button_dev.keybit[LONG(BTN_0)] = BIT(BTN_0);
|
37 |
|
|
|
38 |
|
|
input_register_device(&button_dev);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
static void __exit button_exit(void)
|
42 |
|
|
{
|
43 |
|
|
input_unregister_device(&button_dev);
|
44 |
|
|
free_irq(BUTTON_IRQ, button_interrupt);
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
module_init(button_init);
|
48 |
|
|
module_exit(button_exit);
|
49 |
|
|
|
50 |
|
|
1.1 What the example does
|
51 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
52 |
|
|
|
53 |
|
|
First it has to include the file, which interfaces to the
|
54 |
|
|
input subsystem. This provides all the definitions needed.
|
55 |
|
|
|
56 |
|
|
In the _init function, which is called either upon module load or when
|
57 |
|
|
booting the kernel, it grabs the required resources (it should also check
|
58 |
|
|
for the presence of the device).
|
59 |
|
|
|
60 |
|
|
Then it sets the input bitfields. This way the device driver tells the other
|
61 |
|
|
parts of the input systems what it is - what events can be generated or
|
62 |
|
|
accepted by this input device. Our example device can only generate EV_KEY type
|
63 |
|
|
events, and from those only BTN_0 event code. Thus we only set these two
|
64 |
|
|
bits. We could have used
|
65 |
|
|
|
66 |
|
|
set_bit(EV_KEY, button_dev.evbit);
|
67 |
|
|
set_bit(BTN_0, button_dev.keybit);
|
68 |
|
|
|
69 |
|
|
as well, but with more than single bits the first approach tends to be
|
70 |
|
|
shorter.
|
71 |
|
|
|
72 |
|
|
Then the example driver registers the input device structure by calling
|
73 |
|
|
|
74 |
|
|
input_register_device(&button_dev);
|
75 |
|
|
|
76 |
|
|
This adds the button_dev structure to linked lists of the input driver and
|
77 |
|
|
calls device handler modules _connect functions to tell them a new input
|
78 |
|
|
device has appeared. Because the _connect functions may call kmalloc(,
|
79 |
|
|
GFP_KERNEL), which can sleep, input_register_device() must not be called
|
80 |
|
|
from an interrupt or with a spinlock held.
|
81 |
|
|
|
82 |
|
|
While in use, the only used function of the driver is
|
83 |
|
|
|
84 |
|
|
button_interrupt()
|
85 |
|
|
|
86 |
|
|
which upon every interrupt from the button checks its state and reports it
|
87 |
|
|
via the
|
88 |
|
|
|
89 |
|
|
input_report_btn()
|
90 |
|
|
|
91 |
|
|
call to the input system. There is no need to check whether the interrupt
|
92 |
|
|
routine isn't reporting two same value events (press, press for example) to
|
93 |
|
|
the input system, because the input_report_* functions check that
|
94 |
|
|
themselves.
|
95 |
|
|
|
96 |
|
|
1.2 dev->open() and dev->close()
|
97 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
98 |
|
|
|
99 |
|
|
In case the driver has to repeatedly poll the device, because it doesn't
|
100 |
|
|
have an interrupt coming from it and the polling is too expensive to be done
|
101 |
|
|
all the time, or if the device uses a valuable resource (eg. interrupt), it
|
102 |
|
|
can use the open and close callback to know when it can stop polling or
|
103 |
|
|
release the interrupt and when it must resume polling or grab the interrupt
|
104 |
|
|
again. To do that, we would add this to our example driver:
|
105 |
|
|
|
106 |
|
|
int button_used = 0;
|
107 |
|
|
|
108 |
|
|
static int button_open(struct input_dev *dev)
|
109 |
|
|
{
|
110 |
|
|
if (button_used++)
|
111 |
|
|
return 0;
|
112 |
|
|
|
113 |
|
|
if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
|
114 |
|
|
printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
|
115 |
|
|
button_used--;
|
116 |
|
|
return -EBUSY;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
return 0;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
static void button_close(struct input_dev *dev)
|
123 |
|
|
{
|
124 |
|
|
if (!--button_used)
|
125 |
|
|
free_irq(IRQ_AMIGA_VERTB, button_interrupt);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
static int __init button_init(void)
|
129 |
|
|
{
|
130 |
|
|
...
|
131 |
|
|
button_dev.open = button_open;
|
132 |
|
|
button_dev.close = button_close;
|
133 |
|
|
...
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
Note the button_used variable - we have to track how many times the open
|
137 |
|
|
function was called to know when exactly our device stops being used.
|
138 |
|
|
|
139 |
|
|
The open() callback should return a 0 in case of succes or any nonzero value
|
140 |
|
|
in case of failure. The close() callback (which is void) must always succeed.
|
141 |
|
|
|
142 |
|
|
1.3 Basic event types
|
143 |
|
|
~~~~~~~~~~~~~~~~~~~~~
|
144 |
|
|
|
145 |
|
|
The most simple event type is EV_KEY, which is used for keys and buttons.
|
146 |
|
|
It's reported to the input system via:
|
147 |
|
|
|
148 |
|
|
input_report_key(struct input_dev *dev, int code, int value)
|
149 |
|
|
|
150 |
|
|
See linux/input.h for the allowable values of code (from 0 to KEY_MAX).
|
151 |
|
|
Value is interpreted as a truth value, ie any nonzero value means key
|
152 |
|
|
pressed, zero value means key released. The input code generates events only
|
153 |
|
|
in case the value is different from before.
|
154 |
|
|
|
155 |
|
|
In addition to EV_KEY, there are two more basic event types: EV_REL and
|
156 |
|
|
EV_ABS. They are used for relative and absolute values supplied by the
|
157 |
|
|
device. A relative value may be for example a mouse movement in the X axis.
|
158 |
|
|
The mouse reports it as a relative difference from the last position,
|
159 |
|
|
because it doesn't have any absolute coordinate system to work in. Absolute
|
160 |
|
|
events are namely for joysticks and digitizers - devices that do work in an
|
161 |
|
|
absolute coordinate systems.
|
162 |
|
|
|
163 |
|
|
Having the device report EV_REL buttons is as simple as with EV_KEY, simply
|
164 |
|
|
set the corresponding bits and call the
|
165 |
|
|
|
166 |
|
|
input_report_rel(struct input_dev *dev, int code, int value)
|
167 |
|
|
|
168 |
|
|
function. Events are generated only for nonzero value.
|
169 |
|
|
|
170 |
|
|
However EV_ABS requires a little special care. Before calling
|
171 |
|
|
input_register_devices, you have to fill additional fields in the input_dev
|
172 |
|
|
struct for each absolute axis your device has. If our button device had also
|
173 |
|
|
the ABS_X axis:
|
174 |
|
|
|
175 |
|
|
button_dev.absmin[ABS_X] = 0;
|
176 |
|
|
button_dev.absmax[ABS_X] = 255;
|
177 |
|
|
button_dev.absfuzz[ABS_X] = 4;
|
178 |
|
|
button_dev.absflat[ABS_X] = 8;
|
179 |
|
|
|
180 |
|
|
This setting would be appropriate for a joystick X axis, with the minimum of
|
181 |
|
|
0, maximum of 255 (which the joystick *must* be able to reach, no problem if
|
182 |
|
|
it sometimes reports more, but it must be able to always reach the min and
|
183 |
|
|
max values), with noise in the data up to +- 4, and with a center flat
|
184 |
|
|
position of size 8.
|
185 |
|
|
|
186 |
|
|
If you don't need absfuzz and absflat, you can set them to zero, which mean
|
187 |
|
|
that the thing is precise and always returns to exactly the center position
|
188 |
|
|
(if it has any).
|
189 |
|
|
|
190 |
|
|
1.4 The void *private field
|
191 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
192 |
|
|
|
193 |
|
|
This field in the input structure can be used to point to any private data
|
194 |
|
|
structures in the input device driver, in case the driver handles more than
|
195 |
|
|
one device. You'll need it in the open and close callbacks.
|
196 |
|
|
|
197 |
|
|
1.5 NBITS(), LONG(), BIT()
|
198 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
199 |
|
|
|
200 |
|
|
These three macros frin input.h help some bitfield computations:
|
201 |
|
|
|
202 |
|
|
NBITS(x) - returns the length of a bitfield array in longs for x bits
|
203 |
|
|
LONG(x) - returns the index in the array in longs for bit x
|
204 |
|
|
BIT(x) - returns the indes in a long for bit x
|
205 |
|
|
|
206 |
|
|
1.6 The number, id* and name fields
|
207 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
208 |
|
|
|
209 |
|
|
The dev->number is assigned by the input system to the input device when it
|
210 |
|
|
is registered. It has no use except for identifying the device to the user
|
211 |
|
|
in system messages.
|
212 |
|
|
|
213 |
|
|
The dev->name should be set before registering the input device by the input
|
214 |
|
|
device driver. It's a string like 'Generic button device' containing an
|
215 |
|
|
user friendly name of the device.
|
216 |
|
|
|
217 |
|
|
The id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
|
218 |
|
|
of the device. The bus IDs are defined in input.h. The vendor and device ids
|
219 |
|
|
are defined in pci_ids.h, usb_ids.h and similar include files. These fields
|
220 |
|
|
should be set by the input device driver before registering it.
|
221 |
|
|
|
222 |
|
|
The idtype field can be used for specific information for the input device
|
223 |
|
|
driver.
|
224 |
|
|
|
225 |
|
|
The id and name fields can be passed to userland via the evdev interface.
|
226 |
|
|
|
227 |
|
|
1.7 The keycode, keycodemax, keycodesize fields
|
228 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
229 |
|
|
|
230 |
|
|
These two fields will be used for any inpur devices that report their data
|
231 |
|
|
as scancodes. If not all scancodes can be known by autodetection, they may
|
232 |
|
|
need to be set by userland utilities. The keycode array then is an array
|
233 |
|
|
used to map from scancodes to input system keycodes. The keycode max will
|
234 |
|
|
contain the size of the array and keycodesize the size of each entry in it
|
235 |
|
|
(in bytes).
|
236 |
|
|
|
237 |
|
|
1.8 Key autorepeat
|
238 |
|
|
~~~~~~~~~~~~~~~~~~
|
239 |
|
|
|
240 |
|
|
... is simple. It is handled by the input.c module. Hardware autorepeat is
|
241 |
|
|
not used, because it's not present in many devices and even where it is
|
242 |
|
|
present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
|
243 |
|
|
autorepeat for your device, just set EV_REP in dev->evbit. All will be
|
244 |
|
|
handled by the input system.
|
245 |
|
|
|
246 |
|
|
1.9 Other event types, handling output events
|
247 |
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
248 |
|
|
|
249 |
|
|
The other event types up to now are:
|
250 |
|
|
|
251 |
|
|
EV_LED - used for the keyboad LEDs.
|
252 |
|
|
EV_SND - used for keyboard beeps.
|
253 |
|
|
|
254 |
|
|
They are very similar to for example key events, but they go in the other
|
255 |
|
|
direction - from the system to the input device driver. If your input device
|
256 |
|
|
driver can handle these events, it has to set the respective bits in evbit,
|
257 |
|
|
*and* also the callback routine:
|
258 |
|
|
|
259 |
|
|
button_dev.event = button_event;
|
260 |
|
|
|
261 |
|
|
int button_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
|
262 |
|
|
{
|
263 |
|
|
if (type == EV_SND && code == EV_BELL) {
|
264 |
|
|
outb(value, BUTTON_BELL);
|
265 |
|
|
return 0;
|
266 |
|
|
}
|
267 |
|
|
return -1;
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
This callback routine can be called from an interrupt or a BH (although that
|
271 |
|
|
isn't a rule), and thus must not sleep, and must not take too long to finish.
|