x86: arch/x86/mm/init_32.c cleanup
[wrt350n-kernel.git] / drivers / input / input-polldev.c
blob92b359894e81ed89d7608c90e4f52a2173daa8c3
1 /*
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.
9 */
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)
26 int retval;
28 retval = mutex_lock_interruptible(&polldev_mutex);
29 if (retval)
30 return retval;
32 if (!polldev_users) {
33 polldev_wq = create_singlethread_workqueue("ipolldevd");
34 if (!polldev_wq) {
35 printk(KERN_ERR "input-polldev: failed to create "
36 "ipolldevd workqueue\n");
37 retval = -ENOMEM;
38 goto out;
42 polldev_users++;
44 out:
45 mutex_unlock(&polldev_mutex);
46 return retval;
49 static void input_polldev_stop_workqueue(void)
51 mutex_lock(&polldev_mutex);
53 if (!--polldev_users)
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);
64 dev->poll(dev);
65 queue_delayed_work(polldev_wq, &dev->work,
66 msecs_to_jiffies(dev->poll_interval));
69 static int input_open_polled_device(struct input_dev *input)
71 struct input_polled_dev *dev = input->private;
72 int error;
73 unsigned long ticks;
75 error = input_polldev_start_workqueue();
76 if (error)
77 return error;
79 if (dev->flush)
80 dev->flush(dev);
82 ticks = msecs_to_jiffies(dev->poll_interval);
83 if (ticks >= HZ)
84 ticks = round_jiffies(ticks);
85 queue_delayed_work(polldev_wq, &dev->work, ticks);
87 return 0;
90 static void input_close_polled_device(struct input_dev *input)
92 struct input_polled_dev *dev = input->private;
94 cancel_rearming_delayed_workqueue(polldev_wq, &dev->work);
95 input_polldev_stop_workqueue();
98 /**
99 * input_allocate_polled_device - allocated memory polled device
101 * The function allocates memory for a polled device and also
102 * for an input device associated with this polled device.
104 struct input_polled_dev *input_allocate_polled_device(void)
106 struct input_polled_dev *dev;
108 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
109 if (!dev)
110 return NULL;
112 dev->input = input_allocate_device();
113 if (!dev->input) {
114 kfree(dev);
115 return NULL;
118 return dev;
120 EXPORT_SYMBOL(input_allocate_polled_device);
123 * input_free_polled_device - free memory allocated for polled device
124 * @dev: device to free
126 * The function frees memory allocated for polling device and drops
127 * reference to the associated input device (if present).
129 void input_free_polled_device(struct input_polled_dev *dev)
131 if (dev) {
132 input_free_device(dev->input);
133 kfree(dev);
136 EXPORT_SYMBOL(input_free_polled_device);
139 * input_register_polled_device - register polled device
140 * @dev: device to register
142 * The function registers previously initialized polled input device
143 * with input layer. The device should be allocated with call to
144 * input_allocate_polled_device(). Callers should also set up poll()
145 * method and set up capabilities (id, name, phys, bits) of the
146 * corresponing input_dev structure.
148 int input_register_polled_device(struct input_polled_dev *dev)
150 struct input_dev *input = dev->input;
152 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
153 if (!dev->poll_interval)
154 dev->poll_interval = 500;
155 input->private = dev;
156 input->open = input_open_polled_device;
157 input->close = input_close_polled_device;
159 return input_register_device(input);
161 EXPORT_SYMBOL(input_register_polled_device);
164 * input_unregister_polled_device - unregister polled device
165 * @dev: device to unregister
167 * The function unregisters previously registered polled input
168 * device from input layer. Polling is stopped and device is
169 * ready to be freed with call to input_free_polled_device().
170 * Callers should not attempt to access dev->input pointer
171 * after calling this function.
173 void input_unregister_polled_device(struct input_polled_dev *dev)
175 input_unregister_device(dev->input);
176 dev->input = NULL;
178 EXPORT_SYMBOL(input_unregister_polled_device);