Chatting with the input device in Linux (11) slowly opens her mind

Source: Internet
Author: User

That's right. So far, the AKM input device has been registered, and the full picture of Uncle evdev handler is also clear, and their daughter is also slim and elegant ...... Is it time to do the right thing?

Okay, let's take a look at how to do this:

Or list the operation set function (Uncle handler carefully prepared for the friends who laughed at us, but there is no such father in real life ):

Static const struct file_operations evdev_fops = {

. Owner = this_module,

. Read = evdev_read,

. Write = evdev_write,

. Poll = evdev_poll,

. Open = evdev_open,

. Release = evdev_release,

. Unlocked_ioctl = evdev_ioctl,

# Ifdef config_compat

. Compat_ioctl = evdev_ioctl_compat,

# Endif

. Fasync = evdev_fasync,

. Flush = evdev_flush


Remember? This is the ushield we obtained after carefully filling out the form.

To learn about evdev, you must first open up her heart and understand her. I remember a saying that the first thing I want to do to get the favor of a beautiful girl is to open her words and talk about things she is interested in, open her heart completely, so that she will have a sense of trust in you .... Next? Then I will not talk about it. If you are interested, you can talk to me (the second floor of the R & D Building will show you a long thin guy who is not very cool ).

Well, at the application layer, we can call the OPEN function and then go to. Open = evdev_open in our operation set function ,. Is it easy. Well, it's actually a simple B. Continue to see the specific opening process:


Static int evdev_open (struct inode * inode, struct file * file)


1 struct evdev * evdev;

2 struct evdev_client * client;

3 int I = iminor (inode)-evdev_minor_base;

4 int error;


5 if (I> = evdev_minors)

6 Return-enodev;


7 Error = mutex_lock_interruptible (& evdev_table_mutex );

8 If (error)

9 return error;

10 evdev = evdev_table [I];

11 if (evdev)

12 get_device (& evdev-> Dev );

13 mutex_unlock (& evdev_table_mutex );


14 if (! Evdev)

15 return-enodev;


16 client = kzarloc (sizeof (struct evdev_client), gfp_kernel );

17 if (! Client ){

18 error =-enomem;

19 goto err_put_evdev;



21 spin_lock_init (& client-> buffer_lock );

22 wake_lock_init (& client-> wake_lock, wake_lock_suspend, "evdev ");

23 client-> evdev = evdev;

24 evdev_attach_client (evdev, client );


25 error = evdev_open_device (evdev );

26 if (error)

27 goto err_free_client;


28 file-> private_data = client;

29 return 0;


30 err_free_client:

31 evdev_detach_client (evdev, client );

32 kfree (client );

33 err_put_evdev:

34 put_device (& evdev-> Dev );

35 return error;


As the saying goes: three times are cool and three times are cool. I believe that the buddy who has read the previous chapter will read this function, which is simply a piece of cake. However, there are several lines of code that need to be mentioned:

3. Lines 10 and 10, row 3rd: Find the cradle location number through the device node number; line 10th: Find the child through the cradle number. By the way, it is no longer a child, it is a beautiful girl.

Line 16: Create struct evdev_client. Do you still remember that she is a brand machine transformed Based on struct evdev. I have repeatedly stressed that it is not a shanzhai.

Bind 24 rows.

25 rows, 25 key rows, and how to trace them:

Static int evdev_open_device (struct evdev * evdev)


Int retval;


Retval = mutex_lock_interruptible (& evdev-> mutex );

If (retval)

Return retval;


If (! Evdev-> exist)

Retval =-enodev;

Else if (! Evdev-> open ++ ){

Retval = input_open_device (& evdev-> handle );

If (retval)

Evdev-> open --;



Mutex_unlock (& evdev-> mutex );

Return retval;


The guy with a bad eye will immediately see that a line of code is bold and he is the core of our function. Just like when I was a child, the teacher often asked us to write a paragraph to make a general idea and find a central sentence. If I couldn't find it, I would go to the playground to get a penalty for running. There was a natural lack of language cells. At that time, I didn't have to run much. I even met my aunt who picked up garbage on the playground. Occasionally, I was lucky enough to pass the test and didn't run, the next day, my mom would say, "I didn't come to class yesterday ". One major cause of the estimation is that the number of sentences in the Chinese class is missing when I was a child. As soon as I saw a bunch of code, I wanted to find the center.

Now let's keep track of what input_open_device (& evdev-> handle) has done in this center:

Int input_open_device (struct input_handle * handle)


Struct input_dev * Dev = handle-> dev;

Int retval;


Retval = mutex_lock_interruptible (& Dev-> mutex );

If (retval)

Return retval;


If (Dev-> going_away ){

Retval =-enodev;

Goto out;



Handle-> open ++;


If (! Dev-> Users ++ & Dev-> open)

Retval = Dev-> open (Dev );


If (retval ){

Dev-> Users --;

If (! -- Handle-> open ){


* Make sure we are not delivering any more events

* Through this handle


Synchronize_rcu ();





Mutex_unlock (& Dev-> mutex );

Return retval;

I can only say: you are too talented, and you will immediately see the center sentence again; right here, this bold part:

Handle-> open ++ ;. Do you have any impression on it? If you don't remember it, open section 10th and see the bold part of the first function. It is because of it that our data can be passed on ......

28 lines of file-> private_data = client; the reason why the client is assigned to the private data of the file is as follows.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.