xen-gntdev: integer overflow in gntdev_alloc_map()
[deliverable/linux.git] / drivers / input / input-polldev.c
CommitLineData
0dcd8073
DT
1/*
2 * Generic implementation of a polled input device
3
4 * Copyright (c) 2007 Dmitry Torokhov
5 *
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.
9 */
10
da0c4901
JP
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
0dcd8073 13#include <linux/jiffies.h>
5a0e3ad6 14#include <linux/slab.h>
0dcd8073 15#include <linux/mutex.h>
e490ebdc 16#include <linux/workqueue.h>
d2d8442d 17#include <linux/module.h>
0dcd8073
DT
18#include <linux/input-polldev.h>
19
36bd52a4
EP
20MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21MODULE_DESCRIPTION("Generic implementation of a polled input device");
22MODULE_LICENSE("GPL v2");
23MODULE_VERSION("0.1");
24
dad725d0 25static void input_polldev_queue_work(struct input_polled_dev *dev)
0dcd8073 26{
374766bc 27 unsigned long delay;
0dcd8073 28
374766bc
SH
29 delay = msecs_to_jiffies(dev->poll_interval);
30 if (delay >= HZ)
31 delay = round_jiffies_relative(delay);
32
e490ebdc 33 queue_delayed_work(system_freezable_wq, &dev->work, delay);
0dcd8073
DT
34}
35
dad725d0
SO
36static void input_polled_device_work(struct work_struct *work)
37{
38 struct input_polled_dev *dev =
39 container_of(work, struct input_polled_dev, work.work);
40
41 dev->poll(dev);
42 input_polldev_queue_work(dev);
43}
44
0dcd8073
DT
45static int input_open_polled_device(struct input_dev *input)
46{
3797fec1 47 struct input_polled_dev *dev = input_get_drvdata(input);
0dcd8073 48
b0aba1e6
SO
49 if (dev->open)
50 dev->open(dev);
0dcd8073 51
11bb4cc7 52 /* Only start polling if polling is enabled */
5e3e4eb1
DT
53 if (dev->poll_interval > 0) {
54 dev->poll(dev);
55 input_polldev_queue_work(dev);
56 }
0dcd8073
DT
57
58 return 0;
59}
60
61static void input_close_polled_device(struct input_dev *input)
62{
3797fec1 63 struct input_polled_dev *dev = input_get_drvdata(input);
0dcd8073 64
374766bc 65 cancel_delayed_work_sync(&dev->work);
b0aba1e6
SO
66
67 if (dev->close)
68 dev->close(dev);
0dcd8073
DT
69}
70
dad725d0
SO
71/* SYSFS interface */
72
73static ssize_t input_polldev_get_poll(struct device *dev,
74 struct device_attribute *attr, char *buf)
75{
76 struct input_polled_dev *polldev = dev_get_drvdata(dev);
77
78 return sprintf(buf, "%d\n", polldev->poll_interval);
79}
80
81static ssize_t input_polldev_set_poll(struct device *dev,
82 struct device_attribute *attr, const char *buf,
83 size_t count)
84{
85 struct input_polled_dev *polldev = dev_get_drvdata(dev);
86 struct input_dev *input = polldev->input;
87 unsigned long interval;
88
89 if (strict_strtoul(buf, 0, &interval))
90 return -EINVAL;
91
92 if (interval < polldev->poll_interval_min)
93 return -EINVAL;
94
95 if (interval > polldev->poll_interval_max)
96 return -EINVAL;
97
98 mutex_lock(&input->mutex);
99
100 polldev->poll_interval = interval;
101
102 if (input->users) {
103 cancel_delayed_work_sync(&polldev->work);
104 if (polldev->poll_interval > 0)
105 input_polldev_queue_work(polldev);
106 }
107
108 mutex_unlock(&input->mutex);
109
110 return count;
111}
112
113static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
114 input_polldev_set_poll);
115
116
117static ssize_t input_polldev_get_max(struct device *dev,
118 struct device_attribute *attr, char *buf)
119{
120 struct input_polled_dev *polldev = dev_get_drvdata(dev);
121
122 return sprintf(buf, "%d\n", polldev->poll_interval_max);
123}
124
125static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
126
127static ssize_t input_polldev_get_min(struct device *dev,
128 struct device_attribute *attr, char *buf)
129{
130 struct input_polled_dev *polldev = dev_get_drvdata(dev);
131
132 return sprintf(buf, "%d\n", polldev->poll_interval_min);
133}
134
135static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
136
137static struct attribute *sysfs_attrs[] = {
138 &dev_attr_poll.attr,
139 &dev_attr_max.attr,
140 &dev_attr_min.attr,
141 NULL
142};
143
144static struct attribute_group input_polldev_attribute_group = {
145 .attrs = sysfs_attrs
146};
147
0dcd8073 148/**
2546bcc2 149 * input_allocate_polled_device - allocate memory for polled device
0dcd8073
DT
150 *
151 * The function allocates memory for a polled device and also
152 * for an input device associated with this polled device.
153 */
154struct input_polled_dev *input_allocate_polled_device(void)
155{
156 struct input_polled_dev *dev;
157
158 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
159 if (!dev)
160 return NULL;
161
162 dev->input = input_allocate_device();
163 if (!dev->input) {
164 kfree(dev);
165 return NULL;
166 }
167
168 return dev;
169}
170EXPORT_SYMBOL(input_allocate_polled_device);
171
172/**
173 * input_free_polled_device - free memory allocated for polled device
174 * @dev: device to free
175 *
176 * The function frees memory allocated for polling device and drops
36203c4f 177 * reference to the associated input device.
0dcd8073
DT
178 */
179void input_free_polled_device(struct input_polled_dev *dev)
180{
181 if (dev) {
182 input_free_device(dev->input);
183 kfree(dev);
184 }
185}
186EXPORT_SYMBOL(input_free_polled_device);
187
188/**
189 * input_register_polled_device - register polled device
190 * @dev: device to register
191 *
192 * The function registers previously initialized polled input device
193 * with input layer. The device should be allocated with call to
194 * input_allocate_polled_device(). Callers should also set up poll()
195 * method and set up capabilities (id, name, phys, bits) of the
2546bcc2 196 * corresponding input_dev structure.
0dcd8073
DT
197 */
198int input_register_polled_device(struct input_polled_dev *dev)
199{
200 struct input_dev *input = dev->input;
dad725d0 201 int error;
0dcd8073 202
3797fec1 203 input_set_drvdata(input, dev);
0dcd8073
DT
204 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
205 if (!dev->poll_interval)
206 dev->poll_interval = 500;
dad725d0
SO
207 if (!dev->poll_interval_max)
208 dev->poll_interval_max = dev->poll_interval;
0dcd8073
DT
209 input->open = input_open_polled_device;
210 input->close = input_close_polled_device;
211
dad725d0
SO
212 error = input_register_device(input);
213 if (error)
214 return error;
215
216 error = sysfs_create_group(&input->dev.kobj,
217 &input_polldev_attribute_group);
218 if (error) {
219 input_unregister_device(input);
220 return error;
221 }
222
36203c4f
DT
223 /*
224 * Take extra reference to the underlying input device so
225 * that it survives call to input_unregister_polled_device()
226 * and is deleted only after input_free_polled_device()
227 * has been invoked. This is needed to ease task of freeing
228 * sparse keymaps.
229 */
230 input_get_device(input);
231
dad725d0 232 return 0;
0dcd8073
DT
233}
234EXPORT_SYMBOL(input_register_polled_device);
235
236/**
237 * input_unregister_polled_device - unregister polled device
238 * @dev: device to unregister
239 *
240 * The function unregisters previously registered polled input
241 * device from input layer. Polling is stopped and device is
242 * ready to be freed with call to input_free_polled_device().
0dcd8073
DT
243 */
244void input_unregister_polled_device(struct input_polled_dev *dev)
245{
dad725d0
SO
246 sysfs_remove_group(&dev->input->dev.kobj,
247 &input_polldev_attribute_group);
248
0dcd8073 249 input_unregister_device(dev->input);
0dcd8073
DT
250}
251EXPORT_SYMBOL(input_unregister_polled_device);
This page took 0.427795 seconds and 5 git commands to generate.