blob: 910220c127cb97755abcce575cb2680f0a621e9b [file] [log] [blame]
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -04001/*
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
11#include <linux/jiffies.h>
12#include <linux/mutex.h>
13#include <linux/input-polldev.h>
14
Eric Piel36bd52a2007-05-22 23:28:03 -040015MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
16MODULE_DESCRIPTION("Generic implementation of a polled input device");
17MODULE_LICENSE("GPL v2");
18MODULE_VERSION("0.1");
19
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040020static DEFINE_MUTEX(polldev_mutex);
21static int polldev_users;
22static struct workqueue_struct *polldev_wq;
23
24static int input_polldev_start_workqueue(void)
25{
26 int retval;
27
28 retval = mutex_lock_interruptible(&polldev_mutex);
29 if (retval)
30 return retval;
31
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;
39 }
40 }
41
42 polldev_users++;
43
44 out:
45 mutex_unlock(&polldev_mutex);
46 return retval;
47}
48
49static void input_polldev_stop_workqueue(void)
50{
51 mutex_lock(&polldev_mutex);
52
53 if (!--polldev_users)
54 destroy_workqueue(polldev_wq);
55
56 mutex_unlock(&polldev_mutex);
57}
58
59static void input_polled_device_work(struct work_struct *work)
60{
61 struct input_polled_dev *dev =
62 container_of(work, struct input_polled_dev, work.work);
Stephen Hemminger374766b2007-11-21 14:03:37 -050063 unsigned long delay;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040064
65 dev->poll(dev);
Stephen Hemminger374766b2007-11-21 14:03:37 -050066
67 delay = msecs_to_jiffies(dev->poll_interval);
68 if (delay >= HZ)
69 delay = round_jiffies_relative(delay);
70
71 queue_delayed_work(polldev_wq, &dev->work, delay);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040072}
73
74static int input_open_polled_device(struct input_dev *input)
75{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040076 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040077 int error;
78
79 error = input_polldev_start_workqueue();
80 if (error)
81 return error;
82
Samu Onkalob0aba1e2009-10-18 00:38:57 -070083 if (dev->open)
84 dev->open(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040085
Stephen Hemminger374766b2007-11-21 14:03:37 -050086 queue_delayed_work(polldev_wq, &dev->work,
87 msecs_to_jiffies(dev->poll_interval));
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040088
89 return 0;
90}
91
92static void input_close_polled_device(struct input_dev *input)
93{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040094 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040095
Stephen Hemminger374766b2007-11-21 14:03:37 -050096 cancel_delayed_work_sync(&dev->work);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040097 input_polldev_stop_workqueue();
Samu Onkalob0aba1e2009-10-18 00:38:57 -070098
99 if (dev->close)
100 dev->close(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400101}
102
103/**
104 * input_allocate_polled_device - allocated memory polled device
105 *
106 * The function allocates memory for a polled device and also
107 * for an input device associated with this polled device.
108 */
109struct input_polled_dev *input_allocate_polled_device(void)
110{
111 struct input_polled_dev *dev;
112
113 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
114 if (!dev)
115 return NULL;
116
117 dev->input = input_allocate_device();
118 if (!dev->input) {
119 kfree(dev);
120 return NULL;
121 }
122
123 return dev;
124}
125EXPORT_SYMBOL(input_allocate_polled_device);
126
127/**
128 * input_free_polled_device - free memory allocated for polled device
129 * @dev: device to free
130 *
131 * The function frees memory allocated for polling device and drops
132 * reference to the associated input device (if present).
133 */
134void input_free_polled_device(struct input_polled_dev *dev)
135{
136 if (dev) {
137 input_free_device(dev->input);
138 kfree(dev);
139 }
140}
141EXPORT_SYMBOL(input_free_polled_device);
142
143/**
144 * input_register_polled_device - register polled device
145 * @dev: device to register
146 *
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.
152 */
153int input_register_polled_device(struct input_polled_dev *dev)
154{
155 struct input_dev *input = dev->input;
156
Dmitry Torokhov3797fec2008-04-02 00:41:00 -0400157 input_set_drvdata(input, dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400158 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
159 if (!dev->poll_interval)
160 dev->poll_interval = 500;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400161 input->open = input_open_polled_device;
162 input->close = input_close_polled_device;
163
164 return input_register_device(input);
165}
166EXPORT_SYMBOL(input_register_polled_device);
167
168/**
169 * input_unregister_polled_device - unregister polled device
170 * @dev: device to unregister
171 *
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.
177 */
178void input_unregister_polled_device(struct input_polled_dev *dev)
179{
180 input_unregister_device(dev->input);
181 dev->input = NULL;
182}
183EXPORT_SYMBOL(input_unregister_polled_device);
184