1.4
Input_dev
.
The last step to input the device driver's initialization function is to call
Input_register_device
Register a device. This function is shown in figure
Program list
1
. 9
.
Program list
1
.
9
Input_register_device
/* Driver/input. C */
Int input_register_device (struct input_dev * Dev)
{
Static
Atomic_t input_no = atomic_init (0 );
Struct
Input_handler * Handler;
Const
Char * path;
Int
Error;
_ Set_bit (ev_syn,
Dev-> evbit );
(1)
Init_timer (& Dev-> timer );
(2)
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;
}
If
(! Dev-> getkeycode)
(3)
Dev-> getkeycode
= Input_default_getkeycode;
If
(! Dev-> setkeycode)
Dev-> setkeycode
= Input_default_setkeycode;
Snprintf (Dev-> Dev. bus_id,
Sizeof (Dev-> Dev. bus_id ),
"Input % lD", (unsigned long)
Atomic_inc_return (& input_no)-1 );
Error =
Device_add (& Dev-> Dev );
If
(Error)
Return
Error;
Path =
Kobject_get_path (& Dev-> Dev. kobj, gfp_kernel );
Printk (kern_info
"Input: % s as % s/n ",
Dev-> name
? Dev-> name: "unspecified device", path? Path:
"N/");
Kfree (PATH );
Error =
Mutex_lock_interruptible (& input_mutex );
If
(Error ){
Device_del (& Dev-> Dev );
Return
Error;
}
List_add_tail (& Dev-> node,
& Input_dev_list );
(4)
List_for_each_entry (handler,
& Input_handler_list, node)
Bytes
Input_attach_handler (Dev,
Handler );
Input_wakeup_procfs_readers ();
Mutex_unlock (& input_mutex );
Return
0;
}
The following sections describe the marked items:
(1)
Register a synchronization event as a supported type, and any device supports synchronization events by default.
(2)
Initialize the device combo timer. If the driver does not enter the combo parameter, use the default value.
(3)
If the driver does not implement the ing modification and viewing functions, fill in the default functions.
(4)
Add the device to the device linked list
(
The linked list is global.
)
.
Bytes
Replace this device with an existing
Handler
Compare
ID
Matched
Handler
Establish a connection. It must be noted that the device may be connected to multiple
Handler
Connection. In this way, events generated by this device are distributed to all connected devices.
Handler
.
The fifth step is the most important step.
Input_attach_handler
This function. Its code is as follows:
Program list
1
. 10
.
Program list
1
.
10
Input_attach_handler
/* Driver/input. C */
Static int input_attach_handler (struct input_dev * Dev,
Struct input_handler * Handler)
{
Const
Struct input_device_id * ID;
Int
Error;
If
(Handler-> blacklist & input_match_device (Handler-> blacklist,
Dev ))
(1)
Return
-Enodev;
Id =
Input_match_device (Handler-> id_table, Dev );
(2)
If (! ID)
Return
-Enodev;
Error =
Handler-> connect (handler, Dev, ID );
(3)
If
(Error & error! =-Enodev)
Printk (kern_err
"Input:
Failed to attach handler % s to device % s ,"
"Error:
% D/N ",
Handler-> name,
Kobject_name (& Dev-> Dev. kobj), error );
Return
Error;
}
There are three points to note:
(1)
Handler-> blacklist
Is a device that is not connected
ID
First, check whether the device allows connection.
(2)
Handler-> id_table
Storage
Handler
Supported Devices
ID
. If the matched
ID
, Then create
Dev
And
Handler
.
(3)
Create
Dev
And
Handler
.
Connect
Next, let's talk about the function implementation in the following chapter.
Input_match_device
. Code such
Program list
1
. 11
.
Program list
1
.
11
Input_match_device
/* Driver/input. C */
# Define match_bit (bit, max )/
For
(I = 0; I <bits_to_longs (max); I ++ )/
If
(ID-> bit [I] & Dev-> bit [I])! = ID-> bit [I])/
(1)
Break;
/
If
(I! = Bits_to_longs (max ))/
Continue;
Static const struct input_device_id
* Input_match_device (const struct input_device_id * ID,
Struct
Input_dev * Dev)
{
Int I;
For (;
ID-> flags | ID-> driver_info; Id ++ ){
If
(ID-> flags & input_device_id_match_bus)
(2)
If
(ID-> bustype! = Dev-> ID. bustype)
Continue;
If
(ID-> flags & input_device_id_match_vendor)
If
(ID-> vendor! = Dev-> ID. Vendor)
Continue;
If
(ID-> flags & input_device_id_match_product)
If
(ID-> product! = Dev-> ID. Product)
Continue;
If
(ID-> flags & input_device_id_match_version)
If
(ID-> version! = Dev-> ID. Version)
Continue;
Match_bit (evbit,
Ev_max );
(3)
Match_bit (keybit,
Key_max );
Match_bit (relbit,
Rel_max );
Match_bit (absbit,
Abs_max );
Match_bit (mscbit,
Msc_max );
Match_bit (ledbit,
Led_max );
Match_bit (sndbit,
Snd_max );
Match_bit (ffbit,
Ff_max );
Match_bit (swbit,
Sw_max );
Return
ID;
}
Return
NULL;
}
The matching process mainly involves two tasks:
(2)
According
ID-> flag
Check
ID
Match or not.
ID-> flag
The domains that the record needs to match.
(3)
Check whether the supported event types are consistent.
Macro used for the second check
Match_bit
Defined in
(1)
. The most important sentence is"
If (ID-> bit [I] &
Dev-> bit [I])! = ID-> bit [I])
", Which means
ID
The supported event types are
Dev
The subset of the supported events is matched. If
Handler
Of
ID
Besides
ID-> driver_info
Other domains are
0
, Then this
Handler
And any
Dev
Match. Actually
<
Kernel
>/Driver/input/evdev. c
The initialization is like this.
ID
.
To sum up
Input_dev
Registration process: one
Input_dev
The registration process is mainly to add yourself
Input_dev_list
And then
Input_handler_list
.
ID
Which matches the Event Type
Handler
And establish a connection with it.
Input_dev
Events generated will be distributed to all connected
Handler
. Next we will continue to analyze the transfer of events.