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:
- # Define ev_syn 0x00/* indicates that the device supports all events */
- # Define ev_key 0x01/* keyboard or button, indicating a key code */
- # Define ev_rel 0x02/* Indicates a relative cursor position result */
- # Define ev_abs 0x03/* the value generated by the tablet, which is an absolute integer */
- # Define ev_msc 0x04/* Other type */
- # Define ev_led 0x11/* LED Device */
- # Define ev_snd 0x12/* buzzer, input sound */
- # Define ev_rep 0x14/* repeat key types allowed */
- # 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.