···5252 To compile this driver as a module, choose M here: the5353 module will be called ff-memless.54545555-config INPUT_POLLDEV5656- tristate "Polled input device skeleton"5757- help5858- Say Y here if you are using a driver for an input5959- device that periodically polls hardware state. This6060- option is only useful for out-of-tree drivers since6161- in-tree drivers select it automatically.6262-6363- If unsure, say N.6464-6565- To compile this driver as a module, choose M here: the6666- module will be called input-polldev.6767-6855config INPUT_SPARSEKMAP6956 tristate "Sparse keymap support library"7057 help
···11-// SPDX-License-Identifier: GPL-2.0-only22-/*33- * Generic implementation of a polled input device44-55- * Copyright (c) 2007 Dmitry Torokhov66- */77-88-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt99-1010-#include <linux/jiffies.h>1111-#include <linux/slab.h>1212-#include <linux/mutex.h>1313-#include <linux/workqueue.h>1414-#include <linux/module.h>1515-#include <linux/input-polldev.h>1616-1717-MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");1818-MODULE_DESCRIPTION("Generic implementation of a polled input device");1919-MODULE_LICENSE("GPL v2");2020-2121-static void input_polldev_queue_work(struct input_polled_dev *dev)2222-{2323- unsigned long delay;2424-2525- delay = msecs_to_jiffies(dev->poll_interval);2626- if (delay >= HZ)2727- delay = round_jiffies_relative(delay);2828-2929- queue_delayed_work(system_freezable_wq, &dev->work, delay);3030-}3131-3232-static void input_polled_device_work(struct work_struct *work)3333-{3434- struct input_polled_dev *dev =3535- container_of(work, struct input_polled_dev, work.work);3636-3737- dev->poll(dev);3838- input_polldev_queue_work(dev);3939-}4040-4141-static int input_open_polled_device(struct input_dev *input)4242-{4343- struct input_polled_dev *dev = input_get_drvdata(input);4444-4545- if (dev->open)4646- dev->open(dev);4747-4848- /* Only start polling if polling is enabled */4949- if (dev->poll_interval > 0) {5050- dev->poll(dev);5151- input_polldev_queue_work(dev);5252- }5353-5454- return 0;5555-}5656-5757-static void input_close_polled_device(struct input_dev *input)5858-{5959- struct input_polled_dev *dev = input_get_drvdata(input);6060-6161- cancel_delayed_work_sync(&dev->work);6262-6363- if (dev->close)6464- dev->close(dev);6565-}6666-6767-/* SYSFS interface */6868-6969-static ssize_t input_polldev_get_poll(struct device *dev,7070- struct device_attribute *attr, char *buf)7171-{7272- struct input_polled_dev *polldev = dev_get_drvdata(dev);7373-7474- return sprintf(buf, "%d\n", polldev->poll_interval);7575-}7676-7777-static ssize_t input_polldev_set_poll(struct device *dev,7878- struct device_attribute *attr, const char *buf,7979- size_t count)8080-{8181- struct input_polled_dev *polldev = dev_get_drvdata(dev);8282- struct input_dev *input = polldev->input;8383- unsigned int interval;8484- int err;8585-8686- err = kstrtouint(buf, 0, &interval);8787- if (err)8888- return err;8989-9090- if (interval < polldev->poll_interval_min)9191- return -EINVAL;9292-9393- if (interval > polldev->poll_interval_max)9494- return -EINVAL;9595-9696- mutex_lock(&input->mutex);9797-9898- polldev->poll_interval = interval;9999-100100- if (input->users) {101101- cancel_delayed_work_sync(&polldev->work);102102- if (polldev->poll_interval > 0)103103- input_polldev_queue_work(polldev);104104- }105105-106106- mutex_unlock(&input->mutex);107107-108108- return count;109109-}110110-111111-static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,112112- input_polldev_set_poll);113113-114114-115115-static ssize_t input_polldev_get_max(struct device *dev,116116- struct device_attribute *attr, char *buf)117117-{118118- struct input_polled_dev *polldev = dev_get_drvdata(dev);119119-120120- return sprintf(buf, "%d\n", polldev->poll_interval_max);121121-}122122-123123-static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);124124-125125-static ssize_t input_polldev_get_min(struct device *dev,126126- struct device_attribute *attr, char *buf)127127-{128128- struct input_polled_dev *polldev = dev_get_drvdata(dev);129129-130130- return sprintf(buf, "%d\n", polldev->poll_interval_min);131131-}132132-133133-static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);134134-135135-static struct attribute *sysfs_attrs[] = {136136- &dev_attr_poll.attr,137137- &dev_attr_max.attr,138138- &dev_attr_min.attr,139139- NULL140140-};141141-142142-static struct attribute_group input_polldev_attribute_group = {143143- .attrs = sysfs_attrs144144-};145145-146146-static const struct attribute_group *input_polldev_attribute_groups[] = {147147- &input_polldev_attribute_group,148148- NULL149149-};150150-151151-/**152152- * input_allocate_polled_device - allocate memory for polled device153153- *154154- * The function allocates memory for a polled device and also155155- * for an input device associated with this polled device.156156- */157157-struct input_polled_dev *input_allocate_polled_device(void)158158-{159159- struct input_polled_dev *dev;160160-161161- dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);162162- if (!dev)163163- return NULL;164164-165165- dev->input = input_allocate_device();166166- if (!dev->input) {167167- kfree(dev);168168- return NULL;169169- }170170-171171- return dev;172172-}173173-EXPORT_SYMBOL(input_allocate_polled_device);174174-175175-struct input_polled_devres {176176- struct input_polled_dev *polldev;177177-};178178-179179-static int devm_input_polldev_match(struct device *dev, void *res, void *data)180180-{181181- struct input_polled_devres *devres = res;182182-183183- return devres->polldev == data;184184-}185185-186186-static void devm_input_polldev_release(struct device *dev, void *res)187187-{188188- struct input_polled_devres *devres = res;189189- struct input_polled_dev *polldev = devres->polldev;190190-191191- dev_dbg(dev, "%s: dropping reference/freeing %s\n",192192- __func__, dev_name(&polldev->input->dev));193193-194194- input_put_device(polldev->input);195195- kfree(polldev);196196-}197197-198198-static void devm_input_polldev_unregister(struct device *dev, void *res)199199-{200200- struct input_polled_devres *devres = res;201201- struct input_polled_dev *polldev = devres->polldev;202202-203203- dev_dbg(dev, "%s: unregistering device %s\n",204204- __func__, dev_name(&polldev->input->dev));205205- input_unregister_device(polldev->input);206206-207207- /*208208- * Note that we are still holding extra reference to the input209209- * device so it will stick around until devm_input_polldev_release()210210- * is called.211211- */212212-}213213-214214-/**215215- * devm_input_allocate_polled_device - allocate managed polled device216216- * @dev: device owning the polled device being created217217- *218218- * Returns prepared &struct input_polled_dev or %NULL.219219- *220220- * Managed polled input devices do not need to be explicitly unregistered221221- * or freed as it will be done automatically when owner device unbinds222222- * from * its driver (or binding fails). Once such managed polled device223223- * is allocated, it is ready to be set up and registered in the same224224- * fashion as regular polled input devices (using225225- * input_register_polled_device() function).226226- *227227- * If you want to manually unregister and free such managed polled devices,228228- * it can be still done by calling input_unregister_polled_device() and229229- * input_free_polled_device(), although it is rarely needed.230230- *231231- * NOTE: the owner device is set up as parent of input device and users232232- * should not override it.233233- */234234-struct input_polled_dev *devm_input_allocate_polled_device(struct device *dev)235235-{236236- struct input_polled_dev *polldev;237237- struct input_polled_devres *devres;238238-239239- devres = devres_alloc(devm_input_polldev_release, sizeof(*devres),240240- GFP_KERNEL);241241- if (!devres)242242- return NULL;243243-244244- polldev = input_allocate_polled_device();245245- if (!polldev) {246246- devres_free(devres);247247- return NULL;248248- }249249-250250- polldev->input->dev.parent = dev;251251- polldev->devres_managed = true;252252-253253- devres->polldev = polldev;254254- devres_add(dev, devres);255255-256256- return polldev;257257-}258258-EXPORT_SYMBOL(devm_input_allocate_polled_device);259259-260260-/**261261- * input_free_polled_device - free memory allocated for polled device262262- * @dev: device to free263263- *264264- * The function frees memory allocated for polling device and drops265265- * reference to the associated input device.266266- */267267-void input_free_polled_device(struct input_polled_dev *dev)268268-{269269- if (dev) {270270- if (dev->devres_managed)271271- WARN_ON(devres_destroy(dev->input->dev.parent,272272- devm_input_polldev_release,273273- devm_input_polldev_match,274274- dev));275275- input_put_device(dev->input);276276- kfree(dev);277277- }278278-}279279-EXPORT_SYMBOL(input_free_polled_device);280280-281281-/**282282- * input_register_polled_device - register polled device283283- * @dev: device to register284284- *285285- * The function registers previously initialized polled input device286286- * with input layer. The device should be allocated with call to287287- * input_allocate_polled_device(). Callers should also set up poll()288288- * method and set up capabilities (id, name, phys, bits) of the289289- * corresponding input_dev structure.290290- */291291-int input_register_polled_device(struct input_polled_dev *dev)292292-{293293- struct input_polled_devres *devres = NULL;294294- struct input_dev *input = dev->input;295295- int error;296296-297297- if (dev->devres_managed) {298298- devres = devres_alloc(devm_input_polldev_unregister,299299- sizeof(*devres), GFP_KERNEL);300300- if (!devres)301301- return -ENOMEM;302302-303303- devres->polldev = dev;304304- }305305-306306- input_set_drvdata(input, dev);307307- INIT_DELAYED_WORK(&dev->work, input_polled_device_work);308308-309309- if (!dev->poll_interval)310310- dev->poll_interval = 500;311311- if (!dev->poll_interval_max)312312- dev->poll_interval_max = dev->poll_interval;313313-314314- input->open = input_open_polled_device;315315- input->close = input_close_polled_device;316316-317317- input->dev.groups = input_polldev_attribute_groups;318318-319319- error = input_register_device(input);320320- if (error) {321321- devres_free(devres);322322- return error;323323- }324324-325325- /*326326- * Take extra reference to the underlying input device so327327- * that it survives call to input_unregister_polled_device()328328- * and is deleted only after input_free_polled_device()329329- * has been invoked. This is needed to ease task of freeing330330- * sparse keymaps.331331- */332332- input_get_device(input);333333-334334- if (dev->devres_managed) {335335- dev_dbg(input->dev.parent, "%s: registering %s with devres.\n",336336- __func__, dev_name(&input->dev));337337- devres_add(input->dev.parent, devres);338338- }339339-340340- return 0;341341-}342342-EXPORT_SYMBOL(input_register_polled_device);343343-344344-/**345345- * input_unregister_polled_device - unregister polled device346346- * @dev: device to unregister347347- *348348- * The function unregisters previously registered polled input349349- * device from input layer. Polling is stopped and device is350350- * ready to be freed with call to input_free_polled_device().351351- */352352-void input_unregister_polled_device(struct input_polled_dev *dev)353353-{354354- if (dev->devres_managed)355355- WARN_ON(devres_destroy(dev->input->dev.parent,356356- devm_input_polldev_unregister,357357- devm_input_polldev_match,358358- dev));359359-360360- input_unregister_device(dev->input);361361-}362362-EXPORT_SYMBOL(input_unregister_polled_device);
-58
include/linux/input-polldev.h
···11-/* SPDX-License-Identifier: GPL-2.0-only */22-#ifndef _INPUT_POLLDEV_H33-#define _INPUT_POLLDEV_H44-55-/*66- * Copyright (c) 2007 Dmitry Torokhov77- */88-99-#include <linux/input.h>1010-#include <linux/workqueue.h>1111-1212-/**1313- * struct input_polled_dev - simple polled input device1414- * @private: private driver data.1515- * @open: driver-supplied method that prepares device for polling1616- * (enabled the device and maybe flushes device state).1717- * @close: driver-supplied method that is called when device is no1818- * longer being polled. Used to put device into low power mode.1919- * @poll: driver-supplied method that polls the device and posts2020- * input events (mandatory).2121- * @poll_interval: specifies how often the poll() method should be called.2222- * Defaults to 500 msec unless overridden when registering the device.2323- * @poll_interval_max: specifies upper bound for the poll interval.2424- * Defaults to the initial value of @poll_interval.2525- * @poll_interval_min: specifies lower bound for the poll interval.2626- * Defaults to 0.2727- * @input: input device structure associated with the polled device.2828- * Must be properly initialized by the driver (id, name, phys, bits).2929- *3030- * Polled input device provides a skeleton for supporting simple input3131- * devices that do not raise interrupts but have to be periodically3232- * scanned or polled to detect changes in their state.3333- */3434-struct input_polled_dev {3535- void *private;3636-3737- void (*open)(struct input_polled_dev *dev);3838- void (*close)(struct input_polled_dev *dev);3939- void (*poll)(struct input_polled_dev *dev);4040- unsigned int poll_interval; /* msec */4141- unsigned int poll_interval_max; /* msec */4242- unsigned int poll_interval_min; /* msec */4343-4444- struct input_dev *input;4545-4646-/* private: */4747- struct delayed_work work;4848-4949- bool devres_managed;5050-};5151-5252-struct input_polled_dev *input_allocate_polled_device(void);5353-struct input_polled_dev *devm_input_allocate_polled_device(struct device *dev);5454-void input_free_polled_device(struct input_polled_dev *dev);5555-int input_register_polled_device(struct input_polled_dev *dev);5656-void input_unregister_polled_device(struct input_polled_dev *dev);5757-5858-#endif