~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/input/input-programming.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/input/input-programming.rst (Version linux-6.12-rc7) and /Documentation/input/input-programming.rst (Version linux-2.6.32.71)


  1 ===============================                   
  2 Creating an input device driver                   
  3 ===============================                   
  4                                                   
  5 The simplest example                              
  6 ~~~~~~~~~~~~~~~~~~~~                              
  7                                                   
  8 Here comes a very simple example of an input d    
  9 just one button and the button is accessible a    
 10 pressed or released a BUTTON_IRQ happens. The     
 11                                                   
 12     #include <linux/input.h>                      
 13     #include <linux/module.h>                     
 14     #include <linux/init.h>                       
 15                                                   
 16     #include <asm/irq.h>                          
 17     #include <asm/io.h>                           
 18                                                   
 19     static struct input_dev *button_dev;          
 20                                                   
 21     static irqreturn_t button_interrupt(int ir    
 22     {                                             
 23             input_report_key(button_dev, BTN_0    
 24             input_sync(button_dev);               
 25             return IRQ_HANDLED;                   
 26     }                                             
 27                                                   
 28     static int __init button_init(void)           
 29     {                                             
 30             int error;                            
 31                                                   
 32             if (request_irq(BUTTON_IRQ, button    
 33                     printk(KERN_ERR "button.c:    
 34                     return -EBUSY;                
 35             }                                     
 36                                                   
 37             button_dev = input_allocate_device    
 38             if (!button_dev) {                    
 39                     printk(KERN_ERR "button.c:    
 40                     error = -ENOMEM;              
 41                     goto err_free_irq;            
 42             }                                     
 43                                                   
 44             button_dev->evbit[0] = BIT_MASK(EV    
 45             button_dev->keybit[BIT_WORD(BTN_0)    
 46                                                   
 47             error = input_register_device(butt    
 48             if (error) {                          
 49                     printk(KERN_ERR "button.c:    
 50                     goto err_free_dev;            
 51             }                                     
 52                                                   
 53             return 0;                             
 54                                                   
 55     err_free_dev:                                 
 56             input_free_device(button_dev);        
 57     err_free_irq:                                 
 58             free_irq(BUTTON_IRQ, button_interr    
 59             return error;                         
 60     }                                             
 61                                                   
 62     static void __exit button_exit(void)          
 63     {                                             
 64             input_unregister_device(button_dev    
 65             free_irq(BUTTON_IRQ, button_interr    
 66     }                                             
 67                                                   
 68     module_init(button_init);                     
 69     module_exit(button_exit);                     
 70                                                   
 71 What the example does                             
 72 ~~~~~~~~~~~~~~~~~~~~~                             
 73                                                   
 74 First it has to include the <linux/input.h> fi    
 75 input subsystem. This provides all the definit    
 76                                                   
 77 In the _init function, which is called either     
 78 booting the kernel, it grabs the required reso    
 79 for the presence of the device).                  
 80                                                   
 81 Then it allocates a new input device structure    
 82 and sets up input bitfields. This way the devi    
 83 parts of the input systems what it is - what e    
 84 accepted by this input device. Our example dev    
 85 type events, and from those only BTN_0 event c    
 86 two bits. We could have used::                    
 87                                                   
 88         set_bit(EV_KEY, button_dev->evbit);       
 89         set_bit(BTN_0, button_dev->keybit);       
 90                                                   
 91 as well, but with more than single bits the fi    
 92 shorter.                                          
 93                                                   
 94 Then the example driver registers the input de    
 95                                                   
 96         input_register_device(button_dev);        
 97                                                   
 98 This adds the button_dev structure to linked l    
 99 calls device handler modules _connect function    
100 device has appeared. input_register_device() m    
101 not be called from an interrupt or with a spin    
102                                                   
103 While in use, the only used function of the dr    
104                                                   
105         button_interrupt()                        
106                                                   
107 which upon every interrupt from the button che    
108 via the::                                         
109                                                   
110         input_report_key()                        
111                                                   
112 call to the input system. There is no need to     
113 routine isn't reporting two same value events     
114 the input system, because the input_report_* f    
115 themselves.                                       
116                                                   
117 Then there is the::                               
118                                                   
119         input_sync()                              
120                                                   
121 call to tell those who receive the events that    
122 This doesn't seem important in the one button     
123 for example for mouse movement, where you don'    
124 to be interpreted separately, because that'd r    
125                                                   
126 dev->open() and dev->close()                      
127 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~                      
128                                                   
129 In case the driver has to repeatedly poll the     
130 have an interrupt coming from it and the polli    
131 all the time, or if the device uses a valuable    
132 can use the open and close callback to know wh    
133 release the interrupt and when it must resume     
134 again. To do that, we would add this to our ex    
135                                                   
136     static int button_open(struct input_dev *d    
137     {                                             
138             if (request_irq(BUTTON_IRQ, button    
139                     printk(KERN_ERR "button.c:    
140                     return -EBUSY;                
141             }                                     
142                                                   
143             return 0;                             
144     }                                             
145                                                   
146     static void button_close(struct input_dev     
147     {                                             
148             free_irq(IRQ_AMIGA_VERTB, button_i    
149     }                                             
150                                                   
151     static int __init button_init(void)           
152     {                                             
153             ...                                   
154             button_dev->open = button_open;       
155             button_dev->close = button_close;     
156             ...                                   
157     }                                             
158                                                   
159 Note that input core keeps track of number of     
160 makes sure that dev->open() is called only whe    
161 to the device and that dev->close() is called     
162 disconnects. Calls to both callbacks are seria    
163                                                   
164 The open() callback should return a 0 in case     
165 in case of failure. The close() callback (whic    
166                                                   
167 Inhibiting input devices                          
168 ~~~~~~~~~~~~~~~~~~~~~~~~                          
169                                                   
170 Inhibiting a device means ignoring input event    
171 maintaining relationships with input handlers     
172 relationships, or relationships to be establis    
173 inhibited state.                                  
174                                                   
175 If a device is inhibited, no input handler wil    
176                                                   
177 The fact that nobody wants events from the dev    
178 calling device's close() (if there are users)     
179 inhibit and uninhibit operations, respectively    
180 is to stop providing events to the input core     
181 providing events to the input core.               
182                                                   
183 Calling the device's close() method on inhibit    
184 driver to save power. Either by directly power    
185 releasing the runtime-PM reference it got in o    
186 runtime-PM.                                       
187                                                   
188 Inhibiting and uninhibiting are orthogonal to     
189 input handlers. Userspace might want to inhibi    
190 any handler is positively matched against it.     
191                                                   
192 Inhibiting and uninhibiting are orthogonal to     
193 too. Being a wakeup source plays a role when t    
194 the system is operating.  How drivers should p    
195 inhibiting, sleeping and being a wakeup source    
196                                                   
197 Taking the analogy with the network devices -     
198 doesn't mean that it should be impossible be w    
199 this interface. So, there may be input drivers    
200 sources even when inhibited. Actually, in many    
201 is declared a wakeup interrupt and its handlin    
202 is not aware of input-specific inhibit (nor sh    
203 containing several interfaces can be inhibited    
204 inhibiting one interface shouldn't affect the     
205 wakeup source.                                    
206                                                   
207 If a device is to be considered a wakeup sourc    
208 must be taken when programming its suspend(),     
209 open(). Depending on what close() means for th    
210 opening() it before going to sleep might make     
211 wakeup events. The device is going to sleep an    
212                                                   
213 Basic event types                                 
214 ~~~~~~~~~~~~~~~~~                                 
215                                                   
216 The most simple event type is EV_KEY, which is    
217 It's reported to the input system via::           
218                                                   
219         input_report_key(struct input_dev *dev    
220                                                   
221 See uapi/linux/input-event-codes.h for the all    
222 KEY_MAX). Value is interpreted as a truth valu    
223 key pressed, zero value means key released. Th    
224 in case the value is different from before.       
225                                                   
226 In addition to EV_KEY, there are two more basi    
227 EV_ABS. They are used for relative and absolut    
228 device. A relative value may be for example a     
229 The mouse reports it as a relative difference     
230 because it doesn't have any absolute coordinat    
231 events are namely for joysticks and digitizers    
232 absolute coordinate systems.                      
233                                                   
234 Having the device report EV_REL buttons is as     
235 set the corresponding bits and call the::         
236                                                   
237         input_report_rel(struct input_dev *dev    
238                                                   
239 function. Events are generated only for non-ze    
240                                                   
241 However EV_ABS requires a little special care.    
242 input_register_device, you have to fill additi    
243 struct for each absolute axis your device has.    
244 the ABS_X axis::                                  
245                                                   
246         button_dev.absmin[ABS_X] = 0;             
247         button_dev.absmax[ABS_X] = 255;           
248         button_dev.absfuzz[ABS_X] = 4;            
249         button_dev.absflat[ABS_X] = 8;            
250                                                   
251 Or, you can just say::                            
252                                                   
253         input_set_abs_params(button_dev, ABS_X    
254                                                   
255 This setting would be appropriate for a joysti    
256 0, maximum of 255 (which the joystick *must* b    
257 it sometimes reports more, but it must be able    
258 max values), with noise in the data up to +- 4    
259 position of size 8.                               
260                                                   
261 If you don't need absfuzz and absflat, you can    
262 that the thing is precise and always returns t    
263 (if it has any).                                  
264                                                   
265 BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()           
266 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~           
267                                                   
268 These three macros from bitops.h help some bit    
269                                                   
270         BITS_TO_LONGS(x) - returns the length     
271                            x bits                 
272         BIT_WORD(x)      - returns the index i    
273         BIT_MASK(x)      - returns the index i    
274                                                   
275 The id* and name fields                           
276 ~~~~~~~~~~~~~~~~~~~~~~~                           
277                                                   
278 The dev->name should be set before registering    
279 device driver. It's a string like 'Generic but    
280 user friendly name of the device.                 
281                                                   
282 The id* fields contain the bus ID (PCI, USB, .    
283 of the device. The bus IDs are defined in inpu    
284 are defined in pci_ids.h, usb_ids.h and simila    
285 should be set by the input device driver befor    
286                                                   
287 The idtype field can be used for specific info    
288 driver.                                           
289                                                   
290 The id and name fields can be passed to userla    
291                                                   
292 The keycode, keycodemax, keycodesize fields       
293 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       
294                                                   
295 These three fields should be used by input dev    
296 The keycode is an array used to map from scanc    
297 The keycode max should contain the size of the    
298 size of each entry in it (in bytes).              
299                                                   
300 Userspace can query and alter current scancode    
301 EVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corr    
302 When a device has all 3 aforementioned fields     
303 rely on kernel's default implementation of set    
304 mappings.                                         
305                                                   
306 dev->getkeycode() and dev->setkeycode()           
307 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~           
308                                                   
309 getkeycode() and setkeycode() callbacks allow     
310 keycode/keycodesize/keycodemax mapping mechani    
311 and implement sparse keycode maps.                
312                                                   
313 Key autorepeat                                    
314 ~~~~~~~~~~~~~~                                    
315                                                   
316 ... is simple. It is handled by the input.c mo    
317 not used, because it's not present in many dev    
318 present, it is broken sometimes (at keyboards:    
319 autorepeat for your device, just set EV_REP in    
320 handled by the input system.                      
321                                                   
322 Other event types, handling output events         
323 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~         
324                                                   
325 The other event types up to now are:              
326                                                   
327 - EV_LED - used for the keyboard LEDs.            
328 - EV_SND - used for keyboard beeps.               
329                                                   
330 They are very similar to for example key event    
331 direction - from the system to the input devic    
332 driver can handle these events, it has to set     
333 *and* also the callback routine::                 
334                                                   
335     button_dev->event = button_event;             
336                                                   
337     int button_event(struct input_dev *dev, un    
338                      unsigned int code, int va    
339     {                                             
340             if (type == EV_SND && code == SND_    
341                     outb(value, BUTTON_BELL);     
342                     return 0;                     
343             }                                     
344             return -1;                            
345     }                                             
346                                                   
347 This callback routine can be called from an in    
348 isn't a rule), and thus must not sleep, and mu    
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php