2 * Generic implementation of a polled input device
4 * Copyright (c) 2007 Dmitry Torokhov
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #include <linux/jiffies.h>
12 #include <linux/mutex.h>
13 #include <linux/input-polldev.h>
15 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
16 MODULE_DESCRIPTION("Generic implementation of a polled input device");
17 MODULE_LICENSE("GPL v2");
18 MODULE_VERSION("0.1");
20 static DEFINE_MUTEX(polldev_mutex);
21 static int polldev_users;
22 static struct workqueue_struct *polldev_wq;
24 static int input_polldev_start_workqueue(void)
28 retval = mutex_lock_interruptible(&polldev_mutex);
33 polldev_wq = create_singlethread_workqueue("ipolldevd");
35 printk(KERN_ERR "input-polldev: failed to create "
36 "ipolldevd workqueue\n");
45 mutex_unlock(&polldev_mutex);
49 static void input_polldev_stop_workqueue(void)
51 mutex_lock(&polldev_mutex);
54 destroy_workqueue(polldev_wq);
56 mutex_unlock(&polldev_mutex);
59 static void input_polled_device_work(struct work_struct *work)
61 struct input_polled_dev *dev =
62 container_of(work, struct input_polled_dev, work.work);
67 delay = msecs_to_jiffies(dev->poll_interval);
69 delay = round_jiffies_relative(delay);
71 queue_delayed_work(polldev_wq, &dev->work, delay);
74 static int input_open_polled_device(struct input_dev *input)
76 struct input_polled_dev *dev = input_get_drvdata(input);
79 error = input_polldev_start_workqueue();
86 queue_delayed_work(polldev_wq, &dev->work,
87 msecs_to_jiffies(dev->poll_interval));
92 static void input_close_polled_device(struct input_dev *input)
94 struct input_polled_dev *dev = input_get_drvdata(input);
96 cancel_delayed_work_sync(&dev->work);
97 input_polldev_stop_workqueue();
104 * input_allocate_polled_device - allocated memory polled device
106 * The function allocates memory for a polled device and also
107 * for an input device associated with this polled device.
109 struct input_polled_dev *input_allocate_polled_device(void)
111 struct input_polled_dev *dev;
113 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
117 dev->input = input_allocate_device();
125 EXPORT_SYMBOL(input_allocate_polled_device);
128 * input_free_polled_device - free memory allocated for polled device
129 * @dev: device to free
131 * The function frees memory allocated for polling device and drops
132 * reference to the associated input device (if present).
134 void input_free_polled_device(struct input_polled_dev *dev)
137 input_free_device(dev->input);
141 EXPORT_SYMBOL(input_free_polled_device);
144 * input_register_polled_device - register polled device
145 * @dev: device to register
147 * The function registers previously initialized polled input device
148 * with input layer. The device should be allocated with call to
149 * input_allocate_polled_device(). Callers should also set up poll()
150 * method and set up capabilities (id, name, phys, bits) of the
151 * corresponing input_dev structure.
153 int input_register_polled_device(struct input_polled_dev *dev)
155 struct input_dev *input = dev->input;
157 input_set_drvdata(input, dev);
158 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
159 if (!dev->poll_interval)
160 dev->poll_interval = 500;
161 input->open = input_open_polled_device;
162 input->close = input_close_polled_device;
164 return input_register_device(input);
166 EXPORT_SYMBOL(input_register_polled_device);
169 * input_unregister_polled_device - unregister polled device
170 * @dev: device to unregister
172 * The function unregisters previously registered polled input
173 * device from input layer. Polling is stopped and device is
174 * ready to be freed with call to input_free_polled_device().
175 * Callers should not attempt to access dev->input pointer
176 * after calling this function.
178 void input_unregister_polled_device(struct input_polled_dev *dev)
180 input_unregister_device(dev->input);
183 EXPORT_SYMBOL(input_unregister_polled_device);