diff options
Diffstat (limited to 'drivers/input/input.c')
-rw-r--r-- | drivers/input/input.c | 181 |
1 files changed, 153 insertions, 28 deletions
diff --git a/drivers/input/input.c b/drivers/input/input.c index 53a0ddee787..ce01332f7b3 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c | |||
@@ -534,8 +534,11 @@ EXPORT_SYMBOL(input_grab_device); | |||
534 | static void __input_release_device(struct input_handle *handle) | 534 | static void __input_release_device(struct input_handle *handle) |
535 | { | 535 | { |
536 | struct input_dev *dev = handle->dev; | 536 | struct input_dev *dev = handle->dev; |
537 | struct input_handle *grabber; | ||
537 | 538 | ||
538 | if (dev->grab == handle) { | 539 | grabber = rcu_dereference_protected(dev->grab, |
540 | lockdep_is_held(&dev->mutex)); | ||
541 | if (grabber == handle) { | ||
539 | rcu_assign_pointer(dev->grab, NULL); | 542 | rcu_assign_pointer(dev->grab, NULL); |
540 | /* Make sure input_pass_event() notices that grab is gone */ | 543 | /* Make sure input_pass_event() notices that grab is gone */ |
541 | synchronize_rcu(); | 544 | synchronize_rcu(); |
@@ -1723,7 +1726,7 @@ EXPORT_SYMBOL_GPL(input_class); | |||
1723 | /** | 1726 | /** |
1724 | * input_allocate_device - allocate memory for new input device | 1727 | * input_allocate_device - allocate memory for new input device |
1725 | * | 1728 | * |
1726 | * Returns prepared struct input_dev or NULL. | 1729 | * Returns prepared struct input_dev or %NULL. |
1727 | * | 1730 | * |
1728 | * NOTE: Use input_free_device() to free devices that have not been | 1731 | * NOTE: Use input_free_device() to free devices that have not been |
1729 | * registered; input_unregister_device() should be used for already | 1732 | * registered; input_unregister_device() should be used for already |
@@ -1750,6 +1753,70 @@ struct input_dev *input_allocate_device(void) | |||
1750 | } | 1753 | } |
1751 | EXPORT_SYMBOL(input_allocate_device); | 1754 | EXPORT_SYMBOL(input_allocate_device); |
1752 | 1755 | ||
1756 | struct input_devres { | ||
1757 | struct input_dev *input; | ||
1758 | }; | ||
1759 | |||
1760 | static int devm_input_device_match(struct device *dev, void *res, void *data) | ||
1761 | { | ||
1762 | struct input_devres *devres = res; | ||
1763 | |||
1764 | return devres->input == data; | ||
1765 | } | ||
1766 | |||
1767 | static void devm_input_device_release(struct device *dev, void *res) | ||
1768 | { | ||
1769 | struct input_devres *devres = res; | ||
1770 | struct input_dev *input = devres->input; | ||
1771 | |||
1772 | dev_dbg(dev, "%s: dropping reference to %s\n", | ||
1773 | __func__, dev_name(&input->dev)); | ||
1774 | input_put_device(input); | ||
1775 | } | ||
1776 | |||
1777 | /** | ||
1778 | * devm_input_allocate_device - allocate managed input device | ||
1779 | * @dev: device owning the input device being created | ||
1780 | * | ||
1781 | * Returns prepared struct input_dev or %NULL. | ||
1782 | * | ||
1783 | * Managed input devices do not need to be explicitly unregistered or | ||
1784 | * freed as it will be done automatically when owner device unbinds from | ||
1785 | * its driver (or binding fails). Once managed input device is allocated, | ||
1786 | * it is ready to be set up and registered in the same fashion as regular | ||
1787 | * input device. There are no special devm_input_device_[un]register() | ||
1788 | * variants, regular ones work with both managed and unmanaged devices. | ||
1789 | * | ||
1790 | * NOTE: the owner device is set up as parent of input device and users | ||
1791 | * should not override it. | ||
1792 | */ | ||
1793 | |||
1794 | struct input_dev *devm_input_allocate_device(struct device *dev) | ||
1795 | { | ||
1796 | struct input_dev *input; | ||
1797 | struct input_devres *devres; | ||
1798 | |||
1799 | devres = devres_alloc(devm_input_device_release, | ||
1800 | sizeof(struct input_devres), GFP_KERNEL); | ||
1801 | if (!devres) | ||
1802 | return NULL; | ||
1803 | |||
1804 | input = input_allocate_device(); | ||
1805 | if (!input) { | ||
1806 | devres_free(devres); | ||
1807 | return NULL; | ||
1808 | } | ||
1809 | |||
1810 | input->dev.parent = dev; | ||
1811 | input->devres_managed = true; | ||
1812 | |||
1813 | devres->input = input; | ||
1814 | devres_add(dev, devres); | ||
1815 | |||
1816 | return input; | ||
1817 | } | ||
1818 | EXPORT_SYMBOL(devm_input_allocate_device); | ||
1819 | |||
1753 | /** | 1820 | /** |
1754 | * input_free_device - free memory occupied by input_dev structure | 1821 | * input_free_device - free memory occupied by input_dev structure |
1755 | * @dev: input device to free | 1822 | * @dev: input device to free |
@@ -1766,8 +1833,14 @@ EXPORT_SYMBOL(input_allocate_device); | |||
1766 | */ | 1833 | */ |
1767 | void input_free_device(struct input_dev *dev) | 1834 | void input_free_device(struct input_dev *dev) |
1768 | { | 1835 | { |
1769 | if (dev) | 1836 | if (dev) { |
1837 | if (dev->devres_managed) | ||
1838 | WARN_ON(devres_destroy(dev->dev.parent, | ||
1839 | devm_input_device_release, | ||
1840 | devm_input_device_match, | ||
1841 | dev)); | ||
1770 | input_put_device(dev); | 1842 | input_put_device(dev); |
1843 | } | ||
1771 | } | 1844 | } |
1772 | EXPORT_SYMBOL(input_free_device); | 1845 | EXPORT_SYMBOL(input_free_device); |
1773 | 1846 | ||
@@ -1888,6 +1961,38 @@ static void input_cleanse_bitmasks(struct input_dev *dev) | |||
1888 | INPUT_CLEANSE_BITMASK(dev, SW, sw); | 1961 | INPUT_CLEANSE_BITMASK(dev, SW, sw); |
1889 | } | 1962 | } |
1890 | 1963 | ||
1964 | static void __input_unregister_device(struct input_dev *dev) | ||
1965 | { | ||
1966 | struct input_handle *handle, *next; | ||
1967 | |||
1968 | input_disconnect_device(dev); | ||
1969 | |||
1970 | mutex_lock(&input_mutex); | ||
1971 | |||
1972 | list_for_each_entry_safe(handle, next, &dev->h_list, d_node) | ||
1973 | handle->handler->disconnect(handle); | ||
1974 | WARN_ON(!list_empty(&dev->h_list)); | ||
1975 | |||
1976 | del_timer_sync(&dev->timer); | ||
1977 | list_del_init(&dev->node); | ||
1978 | |||
1979 | input_wakeup_procfs_readers(); | ||
1980 | |||
1981 | mutex_unlock(&input_mutex); | ||
1982 | |||
1983 | device_del(&dev->dev); | ||
1984 | } | ||
1985 | |||
1986 | static void devm_input_device_unregister(struct device *dev, void *res) | ||
1987 | { | ||
1988 | struct input_devres *devres = res; | ||
1989 | struct input_dev *input = devres->input; | ||
1990 | |||
1991 | dev_dbg(dev, "%s: unregistering device %s\n", | ||
1992 | __func__, dev_name(&input->dev)); | ||
1993 | __input_unregister_device(input); | ||
1994 | } | ||
1995 | |||
1891 | /** | 1996 | /** |
1892 | * input_register_device - register device with input core | 1997 | * input_register_device - register device with input core |
1893 | * @dev: device to be registered | 1998 | * @dev: device to be registered |
@@ -1903,11 +2008,21 @@ static void input_cleanse_bitmasks(struct input_dev *dev) | |||
1903 | int input_register_device(struct input_dev *dev) | 2008 | int input_register_device(struct input_dev *dev) |
1904 | { | 2009 | { |
1905 | static atomic_t input_no = ATOMIC_INIT(0); | 2010 | static atomic_t input_no = ATOMIC_INIT(0); |
2011 | struct input_devres *devres = NULL; | ||
1906 | struct input_handler *handler; | 2012 | struct input_handler *handler; |
1907 | unsigned int packet_size; | 2013 | unsigned int packet_size; |
1908 | const char *path; | 2014 | const char *path; |
1909 | int error; | 2015 | int error; |
1910 | 2016 | ||
2017 | if (dev->devres_managed) { | ||
2018 | devres = devres_alloc(devm_input_device_unregister, | ||
2019 | sizeof(struct input_devres), GFP_KERNEL); | ||
2020 | if (!devres) | ||
2021 | return -ENOMEM; | ||
2022 | |||
2023 | devres->input = dev; | ||
2024 | } | ||
2025 | |||
1911 | /* Every input device generates EV_SYN/SYN_REPORT events. */ | 2026 | /* Every input device generates EV_SYN/SYN_REPORT events. */ |
1912 | __set_bit(EV_SYN, dev->evbit); | 2027 | __set_bit(EV_SYN, dev->evbit); |
1913 | 2028 | ||
@@ -1923,8 +2038,10 @@ int input_register_device(struct input_dev *dev) | |||
1923 | 2038 | ||
1924 | dev->max_vals = max(dev->hint_events_per_packet, packet_size) + 2; | 2039 | dev->max_vals = max(dev->hint_events_per_packet, packet_size) + 2; |
1925 | dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL); | 2040 | dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL); |
1926 | if (!dev->vals) | 2041 | if (!dev->vals) { |
1927 | return -ENOMEM; | 2042 | error = -ENOMEM; |
2043 | goto err_devres_free; | ||
2044 | } | ||
1928 | 2045 | ||
1929 | /* | 2046 | /* |
1930 | * If delay and period are pre-set by the driver, then autorepeating | 2047 | * If delay and period are pre-set by the driver, then autorepeating |
@@ -1949,7 +2066,7 @@ int input_register_device(struct input_dev *dev) | |||
1949 | 2066 | ||
1950 | error = device_add(&dev->dev); | 2067 | error = device_add(&dev->dev); |
1951 | if (error) | 2068 | if (error) |
1952 | return error; | 2069 | goto err_free_vals; |
1953 | 2070 | ||
1954 | path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); | 2071 | path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); |
1955 | pr_info("%s as %s\n", | 2072 | pr_info("%s as %s\n", |
@@ -1958,10 +2075,8 @@ int input_register_device(struct input_dev *dev) | |||
1958 | kfree(path); | 2075 | kfree(path); |
1959 | 2076 | ||
1960 | error = mutex_lock_interruptible(&input_mutex); | 2077 | error = mutex_lock_interruptible(&input_mutex); |
1961 | if (error) { | 2078 | if (error) |
1962 | device_del(&dev->dev); | 2079 | goto err_device_del; |
1963 | return error; | ||
1964 | } | ||
1965 | 2080 | ||
1966 | list_add_tail(&dev->node, &input_dev_list); | 2081 | list_add_tail(&dev->node, &input_dev_list); |
1967 | 2082 | ||
@@ -1972,7 +2087,21 @@ int input_register_device(struct input_dev *dev) | |||
1972 | 2087 | ||
1973 | mutex_unlock(&input_mutex); | 2088 | mutex_unlock(&input_mutex); |
1974 | 2089 | ||
2090 | if (dev->devres_managed) { | ||
2091 | dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n", | ||
2092 | __func__, dev_name(&dev->dev)); | ||
2093 | devres_add(dev->dev.parent, devres); | ||
2094 | } | ||
1975 | return 0; | 2095 | return 0; |
2096 | |||
2097 | err_device_del: | ||
2098 | device_del(&dev->dev); | ||
2099 | err_free_vals: | ||
2100 | kfree(dev->vals); | ||
2101 | dev->vals = NULL; | ||
2102 | err_devres_free: | ||
2103 | devres_free(devres); | ||
2104 | return error; | ||
1976 | } | 2105 | } |
1977 | EXPORT_SYMBOL(input_register_device); | 2106 | EXPORT_SYMBOL(input_register_device); |
1978 | 2107 | ||
@@ -1985,24 +2114,20 @@ EXPORT_SYMBOL(input_register_device); | |||
1985 | */ | 2114 | */ |
1986 | void input_unregister_device(struct input_dev *dev) | 2115 | void input_unregister_device(struct input_dev *dev) |
1987 | { | 2116 | { |
1988 | struct input_handle *handle, *next; | 2117 | if (dev->devres_managed) { |
1989 | 2118 | WARN_ON(devres_destroy(dev->dev.parent, | |
1990 | input_disconnect_device(dev); | 2119 | devm_input_device_unregister, |
1991 | 2120 | devm_input_device_match, | |
1992 | mutex_lock(&input_mutex); | 2121 | dev)); |
1993 | 2122 | __input_unregister_device(dev); | |
1994 | list_for_each_entry_safe(handle, next, &dev->h_list, d_node) | 2123 | /* |
1995 | handle->handler->disconnect(handle); | 2124 | * We do not do input_put_device() here because it will be done |
1996 | WARN_ON(!list_empty(&dev->h_list)); | 2125 | * when 2nd devres fires up. |
1997 | 2126 | */ | |
1998 | del_timer_sync(&dev->timer); | 2127 | } else { |
1999 | list_del_init(&dev->node); | 2128 | __input_unregister_device(dev); |
2000 | 2129 | input_put_device(dev); | |
2001 | input_wakeup_procfs_readers(); | 2130 | } |
2002 | |||
2003 | mutex_unlock(&input_mutex); | ||
2004 | |||
2005 | device_unregister(&dev->dev); | ||
2006 | } | 2131 | } |
2007 | EXPORT_SYMBOL(input_unregister_device); | 2132 | EXPORT_SYMBOL(input_unregister_device); |
2008 | 2133 | ||