The input subsystem is to present the functions of the input device to the application. It supports devices that need to constantly report data, such as mouse, keyboard, buzzer, touch screen, sensor and so on. Simple example: the device in this example has only one key. When the key is pressed, an interrupt will be generated. The kernel detects the interrupt and processes it# include#includestaTIcstrucTInput_ dev*button_ dev;/* Input device structure * / / * interrupt handling function * / staticirqreturn_ tbutton_ Interrupt (intirq, void * dummy) {/ * report the key generation event to the input subsystem * / input_ report_ key(button_ dev,BTN_ 0,inb(BUTTON_ PORT)&1);/* Notify the receiver that a report has been sent * / input_ sync(button_ dev); returnIRQ_ HANDLED;//?}/* Load function * / staticant__ initbutton_ init(void){interror;/* The request interrupt processing function * / / / returns 0 to indicate success, and - inval to indicate invalid if (request_ irq(BUTTON_ IRQ,button_ Interrupt, 0, "button", null)) {/ * if the application fails, the error message * / printk (Kern) will be printed_ ERR"button.c:Can'tallocateirq%d\n",button_ irq); return-EBUSY;}/* Assign a device structure * / / / the device attribute file button will be created under sys / class / input / input-n_ dev=input_ allocate_ device(); if(! button_ Dev) / * judge whether the allocation is successful * / {printk (Kern)_ ERR"button.c:Notenoughmemory\n"); error=-ENOMEM; gotoerr_ free_ irq;} button_ dev->evbit[0]=BIT_ MASK(EV_ KEY);/* Set key information * / button_ dev->keybit[BIT_ WORD(BTN_ 0)]=BIT_ MASK(BTN_ 0); error=input_ register_ device(button_ dev);/* Register an input device * / if (error) {printk (Kern)_ ERR"button.c:Failedtoregisterdevice\n"); gotoerr_ free_ dev;} return0; err_ free_ Dev: / * the following is error handling * / input_ free_ device(button_ dev); err_ free_ irq:free_ irq(BUTTON_ IRQ,button_ interrupt); returnerror;} staticvoid__ exitbutton_ Exit (void) / * unload function * / {input_ unregister_ device(button_ dev);/* Logoff key device * / free_ irq(BUTTON_ IRQ,button_ interrupt);/* Release the interrupt line occupied by the key * /}module_ init(button_ init); module_ exit(button_ exit); As you can see from this simple example. In the initialization function button_ An interrupt handling function is registered in init (), and then input_ is called. allocate_ The device() function allocates an input_ Dev structure and call input_ register_ Device () registers it. In the interrupt handling function button_ In interrupt (), the instance reports the received key information to the input subsystem, which provides key input information to the user state program through the input subsystem. Key function of input subsystem_ allocate_ device()input_ register_ device()-》input_ attach_ handler()-》input_ match_ device()input_ allocate_ The device () function allocates a space in memory for the input device structure and initializes its main members. The code is as follows: structinput_ dev*input_ allocate_ device(void){structinput_ dev*dev; dev=kzalloc(sizeof(structinput_ dev),GFP_ KERNEL);/* Assign an input_ The dev structure is initialized to 0 * / if (DEV) {dev - > dev.type = & input_ dev_ type;/* Initialization device type * / dev - > dev.class = & input_ class;/* Set as input device class * / device_ initialize(&dev->dev);/* Initialize device structure * / mutex_ init(&dev->mutex);/* Initialize mutex * / spin_ lock_ init(&dev->event_ lock);/* Initialization event spin lock * / init_ LIST_ HEAD(&dev->h_ list);/* Initialize linked list * / init_ LIST_ HEAD(&dev->node);/* Initialize linked list*/__ module_ get(THIS_ MODULE);/* Module reference technology plus 1 * /} returnev;}--------------------- It returns a pointer to input_ Dev type pointer. This structure is an input device structure, which contains the relevant information of the input device (key code, device name, supported events). input_ register_ The function device () is provided by the input core of the input subsystem. It will input_ The dev structure is registered in the input subsystem core (input_ The dev structure must be defined by input_ allocate_ Device () function to allocate). If the function registration fails, you must call input_ free_ The device () function to free up the allocated space. If the function registration is successful, input should be called in the unload function_ unregister_ The device () function to unregister the input device structure. Let's take a look at the function prototype: int input_ register_ device(structinput_ Dev * DEV) {/ / define the local variable static to be used in some functions_ tinput_ no=ATOMIC_ INIT(0); structinput_ handler*handler; constchar*path; interror;// Set input_ The event types supported by dev are represented by evbit members. The specific types are summarized later__ set_ bit(EV_ SYN,dev->evbit);// Initialize the timer timer to handle repeated clicks( (de buffeting) init_ timer(&dev->timer);// If rep [rep]_ Delay] and [Rep_ Period] if no value is set, the default value is assigned. To shake. if(! dev->rep[REP_ DELAY]&&! dev->rep[REP_ PERIOD]){dev->timer.data=(long)dev; dev->timer.function=input_ repeat_ key; dev->rep[REP_ DELAY]=250; dev->rep[REP_ PERIOD]=33;}// Check whether the following two functions are defined. If not, assign default values. if(! dev->getkeycode)dev->getkeycode=input_ default_ getkeycode;// Get the key value of the specified position if (! dev->setkeycode)dev->setkeycode=input_ default_ setkeycode;// Set the key value at the specified location. / / set input_ The name of the device in dev is inputn / / it will appear in the sysfs file system as input0input1input2_ set_ name(&dev->dev,"input%ld",(unsignedlong)atomic_ inc_ return(&input_ no)-1);// Register the device structure contained in input - > dev into the Linux device model// And show error = device in the file system_ add(&dev->dev); if(error)returnerror;// Print the path of the device and output the debugging information path = kobject_ get_ path(&dev->dev.kobj,GFP_ KERNEL); printk(KERN_ INFO"input:%sas%s\n",dev->name?dev->name:"Unspecifieddevice",path?:" N/A"); kfree(path); error=mutex_ lock_ interruptible(&input_ mutex); if(error){device_ del(&dev->dev); returnerror;}// Set input_ Dev add input_ dev_ List (this list contains all input devices)_ add_ tail(&dev->node,&input_ dev_ list); list_ for_ each_ entry(handler,&input_ handler_ list,node);// Call input_ attatch_ The handler () function matches handler and input_ dev。// This function is very important and will be analyzed separately later. input_ attach_ handler(dev,handler); input_ wakeup_ procfs_ readers(); mutex_ unlock(&input_ mutex); return0;} Input set for evbit_ Event types supported by dev: #defineev_ Syn0x00 / * indicates that the device supports all events * / #defineev_ Key0x01 / * keyboard or key, indicating a key code * / #defineev_ Rel0x02 / * mouse device, indicating a relative cursor position result * / #defineev_ The value generated by abs0x03 / * tablet, which is an absolute integer value * / #defineev_ Msc0x04 / * other types * / #defineev_ Led0x11 / * LED light equipment * / #defineev_ Snd0x12 / * buzzer, input sound * / #defineev_ Rep0x14 / * allow repeated key types * / #defineev_ Pwr0x16 / * power management event * / input_ attatch_ The handler () function is used to match the input_ Dev and handler are associated only after successful matching. The function code is as follows: staticinput_ attach_ handler(structinput_ dev*dev,structinput_ handler*handler){//input_ device_ The ID structure represents the identification of the device and stores the device information. conststructinput_ device_ id*id;/* Pointer of input device * / Interror// First judge whether the handler's blacklist has an assignment, and then judge whether it matches / / blacklist is an input_ device_ ID * type, pointing to a table that stores the device if (handler - > blacklist & & input) that the driver should ignore_ match_ device(handler->blacklist,dev))return-ENODEV;/*** Matching between device and handler function * * * / / / handler - > ID_ The device and dev - > ID data in the list pointed to by table id = input_ match_ device(handler->id_ table,dev); if(! id)return-ENODEV;// If the matching is successful, call handler - > connect to connect the handler and input_ deverror=handler->connect(handler,dev,id);/* Connect device and processing function * / if (error & & error=- ENODEV)printk(KERN_ ERR"input:failedtoattachhandler%stodevice%s,""error:
Our other product: