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

