Input_register_device registration function analysis in Linux

Source: Internet
Author: User

 

17.1.2 registration function input_register_device () (1)

The 28 lines in the button_init () function call the input_register_device () function to register the input device struct. The input_register_device () function is a function provided by the input core. This function registers the input_dev struct to the input subsystem core. The input_dev struct must be allocated by the input_allocate_device () function mentioned earlier. If the input_register_device () function fails to be registered, you must call the input_free_device () function to release the allocated space. If the function is successfully registered, call the input_unregister_device () function in the uninstall function to deregister the input device struct.

1. input_register_device () function

The code for the input_register_device () function is as follows:

/** * input_register_device - register device with input core * @dev: device to be registered * * This function registers device with input core. The device must be * allocated with input_allocate_device() and all it's capabilities * set up before registering. * If function fails the device must be freed with input_free_device(). * Once device has been successfully registered it can be unregistered * with input_unregister_device(); input_free_device() should not be * called in this case. */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;/* Every input device generates EV_SYN/SYN_REPORT events. */__set_bit(EV_SYN, dev->evbit);/* KEY_RESERVED is not supposed to be transmitted to userspace. */__clear_bit(KEY_RESERVED, dev->keybit);/* Make sure that bitmasks not mentioned in dev->evbit are clean. */input_cleanse_bitmasks(dev);/* * If delay and period are pre-set by the driver, then autorepeating * is handled by the driver itself and we don't do it in input.c. */init_timer(&dev->timer);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)dev->getkeycode = input_default_getkeycode;if (!dev->setkeycode)dev->setkeycode = input_default_setkeycode;dev_set_name(&dev->dev, "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/A");kfree(path);error = mutex_lock_interruptible(&input_mutex);if (error) {device_del(&dev->dev);return error;}list_add_tail(&dev->node, &input_dev_list);list_for_each_entry(handler, &input_handler_list, node)input_attach_handler(dev, handler);input_wakeup_procfs_readers();mutex_unlock(&input_mutex);return 0;}


 

 

The main code of the function is analyzed.

03rd ~ Row 06 defines the local variables that will be used in some functions.

In row 07th, call the _ set_bit () function to set the event types supported by input_dev. The event type is represented by the evbit member of input_dev. Here, the ev_syn is set to support all events. Note that one device supports one or more event types. Common event types are as follows:

 
 
  1. # Define ev_syn 0x00/* indicates that the device supports all events */
  2. # Define ev_key 0x01/* keyboard or button, indicating a key code */
  3. # Define ev_rel 0x02/* Indicates a relative cursor position result */
  4. # Define ev_abs 0x03/* the value generated by the tablet, which is an absolute integer */
  5. # Define ev_msc 0x04/* Other type */
  6. # Define ev_led 0x11/* LED Device */
  7. # Define ev_snd 0x12/* buzzer, input sound */
  8. # Define ev_rep 0x14/* repeat key types allowed */
  9. # Define ev_pwr 0x16/* power management event */

Dev_set_name: set the device name in input_dev. The name appears in sysfs file system in the form of input0, input1, input2, input3, and input4.

Use the device_add () function to register the device structure contained in input_dev to the Linux Device Model and display it in the sysfs file system.

Print the device path and output debugging information.

Call the list_add_tail () function to add input_dev to the input_dev_list linked list. The input_dev_list linked list contains all the input_dev devices in the system.

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.