Chatting with the input device in Linux (4) She has been silently supporting you

Source: Internet
Author: User

Corresponding to the input core, I have always praised her for her kindness. For a guy that everyone doesn't know, I say this to her, isn't it a special character, to prove that she is not so imaginary, I have to show the two lines that appeared in section 2 above.CodeLet's take a look:

AKM-> input_dev = input_allocate_device ();

Err = input_register_device (AKM-> input_dev );

No error. This is exactly the two lines in the code that we need to implement AKM as an input device.

First, apply for an input device: allocate the corresponding memory space in the kernel and initialize it.

The second line registers the input device to the Linux kernel, from which the device takes root in the kernel, happy and happy handler lives a small life of her own (although handler does not necessarily belong to her, she doesn't care ).

As a man, I still have to be responsible for explaining to our input core that input_allocate_device () and input_register_device (); both come from our input core. Now I know her greatness. Let's take a look. We have written a few lines of code for the input device driver, and the only lines of code call functions from the kernel. I have to say that the input core is really awesome.

Now let's take a look at the first function input_allocate_device (). (Linux kernel source directory linux-2.6.29/Drivers/input. c file)

Struct input_dev * input_allocate_device (void)

{

1 struct input_dev * dev;

2 Dev = kzarloc (sizeof (struct input_dev), gfp_kernel );

3 if (Dev ){

4 Dev-> Dev. type = & input_dev_type;

5 Dev-> Dev. Class = & input_class;

6 device_initialize (& Dev-> Dev );

7 mutex_init (& Dev-> mutex );

8 spin_lock_init (& Dev-> event_lock );

9 init_list_head (& Dev-> h_list );

10 init_list_head (& Dev-> node );

 

12 _ module_get (this_module );

}

 

15 return dev;

}

Row 3 declares an input_dev struct variable;

Line 2: kzarloc () is a strange guy. It is actually equivalent to kmalloc + memset. I believe everyone is familiar with kmalloc. That's right. His younger brother is malloc, and kmalloc corresponds to the memory allocation function of the kernel space. Okay, the second line of code means that everyone understands: in the kernel space, open a memory zone of sizeof (struct input_dev) size and initialize it to 0. Gfp_kernel is the identifier of the allocation, that is, a regular memory allocation. Similarly, gfp_dma indicates that the allocated memory can be used by DMA. When gfp_atomic is allocated, sleep is not allowed, it is generally used in interrupt processing. Let's think about it.ProgramIn, the gfp_kernel flag is used to allocate memory. If the memory is insufficient, you will stay there and wait. Have you got it? (after hitting the keyboard, it took 2 minutes for the system to respond, I believe this seriously affects your interest in the beautiful mm online chat.) In the interrupt processing function, we cannot use the gfp_kernel flag to allocate memory.

After the memory is allocated, use Dev to point to the memory.

The second row determines whether the memory allocation is successful. If the allocation is successful, the dev initialization will be performed on lines 4-12. Otherwise, let's stop talking about it. If you do, quit and leave.

Lines 4th to 6 initialize the embedded Dev device of the input device.

Lines 7th to 8 initialize the dev mutex and lock to prevent concurrent access to Dev.

Lines 9th and 10 initialize the two linked list headers in the input device.

Well, this function has been analyzed. Once it goes smoothly, the input device has become a pretty elegant girl. Pay attention, buddy, she is still single. What we will do next, hey, I am sure you will know better than me. The brothers whose names are "undefeated in the Cave Room" opened their mouths: "Is it necessary to sell her?" the brothers are not doing anything elegantly and are not selling anything. It is called marrying, and she is expected to marry another house, listen to the next decomposition.

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: info-contact@alibabacloud.com 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.