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 static DEFINE_MUTEX(polldev_mutex);
16 static int polldev_users;
17 static struct workqueue_struct *polldev_wq;
19 static int input_polldev_start_workqueue(void)
23 retval = mutex_lock_interruptible(&polldev_mutex);
28 polldev_wq = create_singlethread_workqueue("ipolldevd");
30 printk(KERN_ERR "input-polldev: failed to create "
31 "ipolldevd workqueue\n");
40 mutex_unlock(&polldev_mutex);
44 static void input_polldev_stop_workqueue(void)
46 mutex_lock(&polldev_mutex);
49 destroy_workqueue(polldev_wq);
51 mutex_unlock(&polldev_mutex);
54 static void input_polled_device_work(struct work_struct *work)
56 struct input_polled_dev *dev =
57 container_of(work, struct input_polled_dev, work.work);
60 queue_delayed_work(polldev_wq, &dev->work,
61 msecs_to_jiffies(dev->poll_interval));
64 static int input_open_polled_device(struct input_dev *input)
66 struct input_polled_dev *dev = input->private;
69 error = input_polldev_start_workqueue();
76 queue_delayed_work(polldev_wq, &dev->work,
77 msecs_to_jiffies(dev->poll_interval));
82 static void input_close_polled_device(struct input_dev *input)
84 struct input_polled_dev *dev = input->private;
86 cancel_rearming_delayed_workqueue(polldev_wq, &dev->work);
87 input_polldev_stop_workqueue();
91 * input_allocate_polled_device - allocated memory polled device
93 * The function allocates memory for a polled device and also
94 * for an input device associated with this polled device.
96 struct input_polled_dev *input_allocate_polled_device(void)
98 struct input_polled_dev *dev;
100 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
104 dev->input = input_allocate_device();
112 EXPORT_SYMBOL(input_allocate_polled_device);
115 * input_free_polled_device - free memory allocated for polled device
116 * @dev: device to free
118 * The function frees memory allocated for polling device and drops
119 * reference to the associated input device (if present).
121 void input_free_polled_device(struct input_polled_dev *dev)
124 input_free_device(dev->input);
128 EXPORT_SYMBOL(input_free_polled_device);
131 * input_register_polled_device - register polled device
132 * @dev: device to register
134 * The function registers previously initialized polled input device
135 * with input layer. The device should be allocated with call to
136 * input_allocate_polled_device(). Callers should also set up poll()
137 * method and set up capabilities (id, name, phys, bits) of the
138 * corresponing input_dev structure.
140 int input_register_polled_device(struct input_polled_dev *dev)
142 struct input_dev *input = dev->input;
144 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
145 if (!dev->poll_interval)
146 dev->poll_interval = 500;
147 input->private = dev;
148 input->open = input_open_polled_device;
149 input->close = input_close_polled_device;
151 return input_register_device(input);
153 EXPORT_SYMBOL(input_register_polled_device);
156 * input_unregister_polled_device - unregister polled device
157 * @dev: device to unregister
159 * The function unregisters previously registered polled input
160 * device from input layer. Polling is stopped and device is
161 * ready to be freed with call to input_free_polled_device().
162 * Callers should not attempt to access dev->input pointer
163 * after calling this function.
165 void input_unregister_polled_device(struct input_polled_dev *dev)
167 input_unregister_device(dev->input);
170 EXPORT_SYMBOL(input_unregister_polled_device);