aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/misc
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-10-02 20:16:10 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-10-02 20:16:10 -0400
commitfc47912d9cda50ae6bd9ca30e97e8c03de5b7b60 (patch)
treed7da6ce3b23c1524e681ee33aa6e21d3c0586108 /drivers/input/misc
parent06fe918e9f177dc2a0592b0ad40a6ce4920b2033 (diff)
parentdde3ada3d0069855eeb353707b2b0f946191cfd6 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
Pull input updates from Dmitry Torokhov: "A few drivers were updated with device tree bindings and others got a few small cleanups and fixes." Fix trivial conflict in drivers/input/keyboard/omap-keypad.c due to changes clashing with a whitespace cleanup. * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input: (28 commits) Input: wacom - mark Intuos5 pad as in-prox when touching buttons Input: synaptics - adjust threshold for treating position values as negative Input: hgpk - use %*ph to dump small buffer Input: gpio_keys_polled - fix dt pdata->nbuttons Input: Add KD[GS]KBDIACRUC ioctls to the compatible list Input: omap-keypad - fixed formatting Input: tegra - move platform data header Input: wacom - add support for EMR on Cintiq 24HD touch Input: s3c2410_ts - make s3c_ts_pmops const Input: samsung-keypad - use of_get_child_count() helper Input: samsung-keypad - use of_match_ptr() Input: uinput - fix formatting Input: uinput - specify exact bit sizes on userspace APIs Input: uinput - mark failed submission requests as free Input: uinput - fix race that can block nonblocking read Input: uinput - return -EINVAL when read buffer size is too small Input: uinput - take event lock when fetching events from buffer Input: get rid of MATCH_BIT() macro Input: rotary-encoder - add DT bindings Input: rotary-encoder - constify platform data pointers ...
Diffstat (limited to 'drivers/input/misc')
-rw-r--r--drivers/input/misc/rotary_encoder.c140
-rw-r--r--drivers/input/misc/twl4030-pwrbutton.c1
-rw-r--r--drivers/input/misc/uinput.c197
3 files changed, 218 insertions, 120 deletions
diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index f07f784198b9..99a49e4968d2 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -24,12 +24,14 @@
24#include <linux/gpio.h> 24#include <linux/gpio.h>
25#include <linux/rotary_encoder.h> 25#include <linux/rotary_encoder.h>
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/of_platform.h>
28#include <linux/of_gpio.h>
27 29
28#define DRV_NAME "rotary-encoder" 30#define DRV_NAME "rotary-encoder"
29 31
30struct rotary_encoder { 32struct rotary_encoder {
31 struct input_dev *input; 33 struct input_dev *input;
32 struct rotary_encoder_platform_data *pdata; 34 const struct rotary_encoder_platform_data *pdata;
33 35
34 unsigned int axis; 36 unsigned int axis;
35 unsigned int pos; 37 unsigned int pos;
@@ -43,7 +45,7 @@ struct rotary_encoder {
43 char last_stable; 45 char last_stable;
44}; 46};
45 47
46static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata) 48static int rotary_encoder_get_state(const struct rotary_encoder_platform_data *pdata)
47{ 49{
48 int a = !!gpio_get_value(pdata->gpio_a); 50 int a = !!gpio_get_value(pdata->gpio_a);
49 int b = !!gpio_get_value(pdata->gpio_b); 51 int b = !!gpio_get_value(pdata->gpio_b);
@@ -56,7 +58,7 @@ static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata)
56 58
57static void rotary_encoder_report_event(struct rotary_encoder *encoder) 59static void rotary_encoder_report_event(struct rotary_encoder *encoder)
58{ 60{
59 struct rotary_encoder_platform_data *pdata = encoder->pdata; 61 const struct rotary_encoder_platform_data *pdata = encoder->pdata;
60 62
61 if (pdata->relative_axis) { 63 if (pdata->relative_axis) {
62 input_report_rel(encoder->input, 64 input_report_rel(encoder->input,
@@ -140,36 +142,89 @@ static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
140 return IRQ_HANDLED; 142 return IRQ_HANDLED;
141} 143}
142 144
145#ifdef CONFIG_OF
146static struct of_device_id rotary_encoder_of_match[] = {
147 { .compatible = "rotary-encoder", },
148 { },
149};
150MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
151
152static struct rotary_encoder_platform_data * __devinit
153rotary_encoder_parse_dt(struct device *dev)
154{
155 const struct of_device_id *of_id =
156 of_match_device(rotary_encoder_of_match, dev);
157 struct device_node *np = dev->of_node;
158 struct rotary_encoder_platform_data *pdata;
159 enum of_gpio_flags flags;
160
161 if (!of_id || !np)
162 return NULL;
163
164 pdata = kzalloc(sizeof(struct rotary_encoder_platform_data),
165 GFP_KERNEL);
166 if (!pdata)
167 return ERR_PTR(-ENOMEM);
168
169 of_property_read_u32(np, "rotary-encoder,steps", &pdata->steps);
170 of_property_read_u32(np, "linux,axis", &pdata->axis);
171
172 pdata->gpio_a = of_get_gpio_flags(np, 0, &flags);
173 pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW;
174
175 pdata->gpio_b = of_get_gpio_flags(np, 1, &flags);
176 pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW;
177
178 pdata->relative_axis = !!of_get_property(np,
179 "rotary-encoder,relative-axis", NULL);
180 pdata->rollover = !!of_get_property(np,
181 "rotary-encoder,rollover", NULL);
182 pdata->half_period = !!of_get_property(np,
183 "rotary-encoder,half-period", NULL);
184
185 return pdata;
186}
187#else
188static inline struct rotary_encoder_platform_data *
189rotary_encoder_parse_dt(struct device *dev)
190{
191 return NULL;
192}
193#endif
194
143static int __devinit rotary_encoder_probe(struct platform_device *pdev) 195static int __devinit rotary_encoder_probe(struct platform_device *pdev)
144{ 196{
145 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data; 197 struct device *dev = &pdev->dev;
198 const struct rotary_encoder_platform_data *pdata = dev_get_platdata(dev);
146 struct rotary_encoder *encoder; 199 struct rotary_encoder *encoder;
147 struct input_dev *input; 200 struct input_dev *input;
148 irq_handler_t handler; 201 irq_handler_t handler;
149 int err; 202 int err;
150 203
151 if (!pdata) { 204 if (!pdata) {
152 dev_err(&pdev->dev, "missing platform data\n"); 205 pdata = rotary_encoder_parse_dt(dev);
153 return -ENOENT; 206 if (IS_ERR(pdata))
207 return PTR_ERR(pdata);
208
209 if (!pdata) {
210 dev_err(dev, "missing platform data\n");
211 return -EINVAL;
212 }
154 } 213 }
155 214
156 encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL); 215 encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL);
157 input = input_allocate_device(); 216 input = input_allocate_device();
158 if (!encoder || !input) { 217 if (!encoder || !input) {
159 dev_err(&pdev->dev, "failed to allocate memory for device\n");
160 err = -ENOMEM; 218 err = -ENOMEM;
161 goto exit_free_mem; 219 goto exit_free_mem;
162 } 220 }
163 221
164 encoder->input = input; 222 encoder->input = input;
165 encoder->pdata = pdata; 223 encoder->pdata = pdata;
166 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
167 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
168 224
169 /* create and register the input driver */
170 input->name = pdev->name; 225 input->name = pdev->name;
171 input->id.bustype = BUS_HOST; 226 input->id.bustype = BUS_HOST;
172 input->dev.parent = &pdev->dev; 227 input->dev.parent = dev;
173 228
174 if (pdata->relative_axis) { 229 if (pdata->relative_axis) {
175 input->evbit[0] = BIT_MASK(EV_REL); 230 input->evbit[0] = BIT_MASK(EV_REL);
@@ -180,40 +235,21 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
180 pdata->axis, 0, pdata->steps, 0, 1); 235 pdata->axis, 0, pdata->steps, 0, 1);
181 } 236 }
182 237
183 err = input_register_device(input);
184 if (err) {
185 dev_err(&pdev->dev, "failed to register input device\n");
186 goto exit_free_mem;
187 }
188
189 /* request the GPIOs */ 238 /* request the GPIOs */
190 err = gpio_request(pdata->gpio_a, DRV_NAME); 239 err = gpio_request_one(pdata->gpio_a, GPIOF_IN, dev_name(dev));
191 if (err) {
192 dev_err(&pdev->dev, "unable to request GPIO %d\n",
193 pdata->gpio_a);
194 goto exit_unregister_input;
195 }
196
197 err = gpio_direction_input(pdata->gpio_a);
198 if (err) { 240 if (err) {
199 dev_err(&pdev->dev, "unable to set GPIO %d for input\n", 241 dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a);
200 pdata->gpio_a); 242 goto exit_free_mem;
201 goto exit_unregister_input;
202 } 243 }
203 244
204 err = gpio_request(pdata->gpio_b, DRV_NAME); 245 err = gpio_request_one(pdata->gpio_b, GPIOF_IN, dev_name(dev));
205 if (err) { 246 if (err) {
206 dev_err(&pdev->dev, "unable to request GPIO %d\n", 247 dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b);
207 pdata->gpio_b);
208 goto exit_free_gpio_a; 248 goto exit_free_gpio_a;
209 } 249 }
210 250
211 err = gpio_direction_input(pdata->gpio_b); 251 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
212 if (err) { 252 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
213 dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
214 pdata->gpio_b);
215 goto exit_free_gpio_a;
216 }
217 253
218 /* request the IRQs */ 254 /* request the IRQs */
219 if (pdata->half_period) { 255 if (pdata->half_period) {
@@ -227,8 +263,7 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
227 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 263 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
228 DRV_NAME, encoder); 264 DRV_NAME, encoder);
229 if (err) { 265 if (err) {
230 dev_err(&pdev->dev, "unable to request IRQ %d\n", 266 dev_err(dev, "unable to request IRQ %d\n", encoder->irq_a);
231 encoder->irq_a);
232 goto exit_free_gpio_b; 267 goto exit_free_gpio_b;
233 } 268 }
234 269
@@ -236,43 +271,55 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
236 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 271 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
237 DRV_NAME, encoder); 272 DRV_NAME, encoder);
238 if (err) { 273 if (err) {
239 dev_err(&pdev->dev, "unable to request IRQ %d\n", 274 dev_err(dev, "unable to request IRQ %d\n", encoder->irq_b);
240 encoder->irq_b);
241 goto exit_free_irq_a; 275 goto exit_free_irq_a;
242 } 276 }
243 277
278 err = input_register_device(input);
279 if (err) {
280 dev_err(dev, "failed to register input device\n");
281 goto exit_free_irq_b;
282 }
283
244 platform_set_drvdata(pdev, encoder); 284 platform_set_drvdata(pdev, encoder);
245 285
246 return 0; 286 return 0;
247 287
288exit_free_irq_b:
289 free_irq(encoder->irq_b, encoder);
248exit_free_irq_a: 290exit_free_irq_a:
249 free_irq(encoder->irq_a, encoder); 291 free_irq(encoder->irq_a, encoder);
250exit_free_gpio_b: 292exit_free_gpio_b:
251 gpio_free(pdata->gpio_b); 293 gpio_free(pdata->gpio_b);
252exit_free_gpio_a: 294exit_free_gpio_a:
253 gpio_free(pdata->gpio_a); 295 gpio_free(pdata->gpio_a);
254exit_unregister_input:
255 input_unregister_device(input);
256 input = NULL; /* so we don't try to free it */
257exit_free_mem: 296exit_free_mem:
258 input_free_device(input); 297 input_free_device(input);
259 kfree(encoder); 298 kfree(encoder);
299 if (!dev_get_platdata(&pdev->dev))
300 kfree(pdata);
301
260 return err; 302 return err;
261} 303}
262 304
263static int __devexit rotary_encoder_remove(struct platform_device *pdev) 305static int __devexit rotary_encoder_remove(struct platform_device *pdev)
264{ 306{
265 struct rotary_encoder *encoder = platform_get_drvdata(pdev); 307 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
266 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data; 308 const struct rotary_encoder_platform_data *pdata = encoder->pdata;
267 309
268 free_irq(encoder->irq_a, encoder); 310 free_irq(encoder->irq_a, encoder);
269 free_irq(encoder->irq_b, encoder); 311 free_irq(encoder->irq_b, encoder);
270 gpio_free(pdata->gpio_a); 312 gpio_free(pdata->gpio_a);
271 gpio_free(pdata->gpio_b); 313 gpio_free(pdata->gpio_b);
314
272 input_unregister_device(encoder->input); 315 input_unregister_device(encoder->input);
273 platform_set_drvdata(pdev, NULL);
274 kfree(encoder); 316 kfree(encoder);
275 317
318 if (!dev_get_platdata(&pdev->dev))
319 kfree(pdata);
320
321 platform_set_drvdata(pdev, NULL);
322
276 return 0; 323 return 0;
277} 324}
278 325
@@ -282,6 +329,7 @@ static struct platform_driver rotary_encoder_driver = {
282 .driver = { 329 .driver = {
283 .name = DRV_NAME, 330 .name = DRV_NAME,
284 .owner = THIS_MODULE, 331 .owner = THIS_MODULE,
332 .of_match_table = of_match_ptr(rotary_encoder_of_match),
285 } 333 }
286}; 334};
287module_platform_driver(rotary_encoder_driver); 335module_platform_driver(rotary_encoder_driver);
diff --git a/drivers/input/misc/twl4030-pwrbutton.c b/drivers/input/misc/twl4030-pwrbutton.c
index 38e4b507b94c..b3dd96d6448b 100644
--- a/drivers/input/misc/twl4030-pwrbutton.c
+++ b/drivers/input/misc/twl4030-pwrbutton.c
@@ -42,6 +42,7 @@ static irqreturn_t powerbutton_irq(int irq, void *_pwr)
42 err = twl_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &value, 42 err = twl_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &value,
43 STS_HW_CONDITIONS); 43 STS_HW_CONDITIONS);
44 if (!err) { 44 if (!err) {
45 pm_wakeup_event(pwr->dev.parent, 0);
45 input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ); 46 input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ);
46 input_sync(pwr); 47 input_sync(pwr);
47 } else { 48 } else {
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
index 6b1797503e34..a0a4bbaef02c 100644
--- a/drivers/input/misc/uinput.c
+++ b/drivers/input/misc/uinput.c
@@ -40,7 +40,8 @@
40#include <linux/input/mt.h> 40#include <linux/input/mt.h>
41#include "../input-compat.h" 41#include "../input-compat.h"
42 42
43static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) 43static int uinput_dev_event(struct input_dev *dev,
44 unsigned int type, unsigned int code, int value)
44{ 45{
45 struct uinput_device *udev = input_get_drvdata(dev); 46 struct uinput_device *udev = input_get_drvdata(dev);
46 47
@@ -56,10 +57,11 @@ static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned i
56} 57}
57 58
58/* Atomically allocate an ID for the given request. Returns 0 on success. */ 59/* Atomically allocate an ID for the given request. Returns 0 on success. */
59static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request) 60static bool uinput_request_alloc_id(struct uinput_device *udev,
61 struct uinput_request *request)
60{ 62{
61 int id; 63 unsigned int id;
62 int err = -1; 64 bool reserved = false;
63 65
64 spin_lock(&udev->requests_lock); 66 spin_lock(&udev->requests_lock);
65 67
@@ -67,32 +69,35 @@ static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_req
67 if (!udev->requests[id]) { 69 if (!udev->requests[id]) {
68 request->id = id; 70 request->id = id;
69 udev->requests[id] = request; 71 udev->requests[id] = request;
70 err = 0; 72 reserved = true;
71 break; 73 break;
72 } 74 }
73 } 75 }
74 76
75 spin_unlock(&udev->requests_lock); 77 spin_unlock(&udev->requests_lock);
76 return err; 78 return reserved;
77} 79}
78 80
79static struct uinput_request *uinput_request_find(struct uinput_device *udev, int id) 81static struct uinput_request *uinput_request_find(struct uinput_device *udev,
82 unsigned int id)
80{ 83{
81 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */ 84 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
82 if (id >= UINPUT_NUM_REQUESTS || id < 0) 85 if (id >= UINPUT_NUM_REQUESTS)
83 return NULL; 86 return NULL;
84 87
85 return udev->requests[id]; 88 return udev->requests[id];
86} 89}
87 90
88static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request) 91static int uinput_request_reserve_slot(struct uinput_device *udev,
92 struct uinput_request *request)
89{ 93{
90 /* Allocate slot. If none are available right away, wait. */ 94 /* Allocate slot. If none are available right away, wait. */
91 return wait_event_interruptible(udev->requests_waitq, 95 return wait_event_interruptible(udev->requests_waitq,
92 !uinput_request_alloc_id(udev, request)); 96 uinput_request_alloc_id(udev, request));
93} 97}
94 98
95static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request) 99static void uinput_request_done(struct uinput_device *udev,
100 struct uinput_request *request)
96{ 101{
97 /* Mark slot as available */ 102 /* Mark slot as available */
98 udev->requests[request->id] = NULL; 103 udev->requests[request->id] = NULL;
@@ -101,14 +106,11 @@ static void uinput_request_done(struct uinput_device *udev, struct uinput_reques
101 complete(&request->done); 106 complete(&request->done);
102} 107}
103 108
104static int uinput_request_submit(struct uinput_device *udev, struct uinput_request *request) 109static int uinput_request_send(struct uinput_device *udev,
110 struct uinput_request *request)
105{ 111{
106 int retval; 112 int retval;
107 113
108 retval = uinput_request_reserve_slot(udev, request);
109 if (retval)
110 return retval;
111
112 retval = mutex_lock_interruptible(&udev->mutex); 114 retval = mutex_lock_interruptible(&udev->mutex);
113 if (retval) 115 if (retval)
114 return retval; 116 return retval;
@@ -118,7 +120,12 @@ static int uinput_request_submit(struct uinput_device *udev, struct uinput_reque
118 goto out; 120 goto out;
119 } 121 }
120 122
121 /* Tell our userspace app about this new request by queueing an input event */ 123 init_completion(&request->done);
124
125 /*
126 * Tell our userspace application about this new request
127 * by queueing an input event.
128 */
122 uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id); 129 uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
123 130
124 out: 131 out:
@@ -126,8 +133,27 @@ static int uinput_request_submit(struct uinput_device *udev, struct uinput_reque
126 return retval; 133 return retval;
127} 134}
128 135
136static int uinput_request_submit(struct uinput_device *udev,
137 struct uinput_request *request)
138{
139 int error;
140
141 error = uinput_request_reserve_slot(udev, request);
142 if (error)
143 return error;
144
145 error = uinput_request_send(udev, request);
146 if (error) {
147 uinput_request_done(udev, request);
148 return error;
149 }
150
151 wait_for_completion(&request->done);
152 return request->retval;
153}
154
129/* 155/*
130 * Fail all ouitstanding requests so handlers don't wait for the userspace 156 * Fail all outstanding requests so handlers don't wait for the userspace
131 * to finish processing them. 157 * to finish processing them.
132 */ 158 */
133static void uinput_flush_requests(struct uinput_device *udev) 159static void uinput_flush_requests(struct uinput_device *udev)
@@ -163,11 +189,12 @@ static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
163 return uinput_dev_event(dev, EV_FF, effect_id, value); 189 return uinput_dev_event(dev, EV_FF, effect_id, value);
164} 190}
165 191
166static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old) 192static int uinput_dev_upload_effect(struct input_dev *dev,
193 struct ff_effect *effect,
194 struct ff_effect *old)
167{ 195{
168 struct uinput_device *udev = input_get_drvdata(dev); 196 struct uinput_device *udev = input_get_drvdata(dev);
169 struct uinput_request request; 197 struct uinput_request request;
170 int retval;
171 198
172 /* 199 /*
173 * uinput driver does not currently support periodic effects with 200 * uinput driver does not currently support periodic effects with
@@ -180,42 +207,25 @@ static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *eff
180 effect->u.periodic.waveform == FF_CUSTOM) 207 effect->u.periodic.waveform == FF_CUSTOM)
181 return -EINVAL; 208 return -EINVAL;
182 209
183 request.id = -1;
184 init_completion(&request.done);
185 request.code = UI_FF_UPLOAD; 210 request.code = UI_FF_UPLOAD;
186 request.u.upload.effect = effect; 211 request.u.upload.effect = effect;
187 request.u.upload.old = old; 212 request.u.upload.old = old;
188 213
189 retval = uinput_request_submit(udev, &request); 214 return uinput_request_submit(udev, &request);
190 if (!retval) {
191 wait_for_completion(&request.done);
192 retval = request.retval;
193 }
194
195 return retval;
196} 215}
197 216
198static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id) 217static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
199{ 218{
200 struct uinput_device *udev = input_get_drvdata(dev); 219 struct uinput_device *udev = input_get_drvdata(dev);
201 struct uinput_request request; 220 struct uinput_request request;
202 int retval;
203 221
204 if (!test_bit(EV_FF, dev->evbit)) 222 if (!test_bit(EV_FF, dev->evbit))
205 return -ENOSYS; 223 return -ENOSYS;
206 224
207 request.id = -1;
208 init_completion(&request.done);
209 request.code = UI_FF_ERASE; 225 request.code = UI_FF_ERASE;
210 request.u.effect_id = effect_id; 226 request.u.effect_id = effect_id;
211 227
212 retval = uinput_request_submit(udev, &request); 228 return uinput_request_submit(udev, &request);
213 if (!retval) {
214 wait_for_completion(&request.done);
215 retval = request.retval;
216 }
217
218 return retval;
219} 229}
220 230
221static void uinput_destroy_device(struct uinput_device *udev) 231static void uinput_destroy_device(struct uinput_device *udev)
@@ -347,7 +357,8 @@ static int uinput_allocate_device(struct uinput_device *udev)
347 return 0; 357 return 0;
348} 358}
349 359
350static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count) 360static int uinput_setup_device(struct uinput_device *udev,
361 const char __user *buffer, size_t count)
351{ 362{
352 struct uinput_user_dev *user_dev; 363 struct uinput_user_dev *user_dev;
353 struct input_dev *dev; 364 struct input_dev *dev;
@@ -419,7 +430,8 @@ static int uinput_setup_device(struct uinput_device *udev, const char __user *bu
419 return retval; 430 return retval;
420} 431}
421 432
422static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count) 433static ssize_t uinput_inject_event(struct uinput_device *udev,
434 const char __user *buffer, size_t count)
423{ 435{
424 struct input_event ev; 436 struct input_event ev;
425 437
@@ -434,11 +446,15 @@ static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char
434 return input_event_size(); 446 return input_event_size();
435} 447}
436 448
437static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 449static ssize_t uinput_write(struct file *file, const char __user *buffer,
450 size_t count, loff_t *ppos)
438{ 451{
439 struct uinput_device *udev = file->private_data; 452 struct uinput_device *udev = file->private_data;
440 int retval; 453 int retval;
441 454
455 if (count == 0)
456 return 0;
457
442 retval = mutex_lock_interruptible(&udev->mutex); 458 retval = mutex_lock_interruptible(&udev->mutex);
443 if (retval) 459 if (retval)
444 return retval; 460 return retval;
@@ -452,42 +468,74 @@ static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t
452 return retval; 468 return retval;
453} 469}
454 470
455static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) 471static bool uinput_fetch_next_event(struct uinput_device *udev,
472 struct input_event *event)
456{ 473{
457 struct uinput_device *udev = file->private_data; 474 bool have_event;
458 int retval = 0;
459 475
460 if (udev->state != UIST_CREATED) 476 spin_lock_irq(&udev->dev->event_lock);
461 return -ENODEV;
462 477
463 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK)) 478 have_event = udev->head != udev->tail;
464 return -EAGAIN; 479 if (have_event) {
480 *event = udev->buff[udev->tail];
481 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
482 }
465 483
466 retval = wait_event_interruptible(udev->waitq, 484 spin_unlock_irq(&udev->dev->event_lock);
467 udev->head != udev->tail || udev->state != UIST_CREATED);
468 if (retval)
469 return retval;
470 485
471 retval = mutex_lock_interruptible(&udev->mutex); 486 return have_event;
472 if (retval) 487}
473 return retval;
474 488
475 if (udev->state != UIST_CREATED) { 489static ssize_t uinput_events_to_user(struct uinput_device *udev,
476 retval = -ENODEV; 490 char __user *buffer, size_t count)
477 goto out; 491{
478 } 492 struct input_event event;
493 size_t read = 0;
479 494
480 while (udev->head != udev->tail && retval + input_event_size() <= count) { 495 while (read + input_event_size() <= count &&
481 if (input_event_to_user(buffer + retval, &udev->buff[udev->tail])) { 496 uinput_fetch_next_event(udev, &event)) {
482 retval = -EFAULT; 497
483 goto out; 498 if (input_event_to_user(buffer + read, &event))
484 } 499 return -EFAULT;
485 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE; 500
486 retval += input_event_size(); 501 read += input_event_size();
487 } 502 }
488 503
489 out: 504 return read;
490 mutex_unlock(&udev->mutex); 505}
506
507static ssize_t uinput_read(struct file *file, char __user *buffer,
508 size_t count, loff_t *ppos)
509{
510 struct uinput_device *udev = file->private_data;
511 ssize_t retval;
512
513 if (count != 0 && count < input_event_size())
514 return -EINVAL;
515
516 do {
517 retval = mutex_lock_interruptible(&udev->mutex);
518 if (retval)
519 return retval;
520
521 if (udev->state != UIST_CREATED)
522 retval = -ENODEV;
523 else if (udev->head == udev->tail &&
524 (file->f_flags & O_NONBLOCK))
525 retval = -EAGAIN;
526 else
527 retval = uinput_events_to_user(udev, buffer, count);
528
529 mutex_unlock(&udev->mutex);
530
531 if (retval || count == 0)
532 break;
533
534 if (!(file->f_flags & O_NONBLOCK))
535 retval = wait_event_interruptible(udev->waitq,
536 udev->head != udev->tail ||
537 udev->state != UIST_CREATED);
538 } while (retval == 0);
491 539
492 return retval; 540 return retval;
493} 541}
@@ -516,8 +564,8 @@ static int uinput_release(struct inode *inode, struct file *file)
516 564
517#ifdef CONFIG_COMPAT 565#ifdef CONFIG_COMPAT
518struct uinput_ff_upload_compat { 566struct uinput_ff_upload_compat {
519 int request_id; 567 __u32 request_id;
520 int retval; 568 __s32 retval;
521 struct ff_effect_compat effect; 569 struct ff_effect_compat effect;
522 struct ff_effect_compat old; 570 struct ff_effect_compat old;
523}; 571};
@@ -703,7 +751,8 @@ static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
703 break; 751 break;
704 752
705 req = uinput_request_find(udev, ff_up.request_id); 753 req = uinput_request_find(udev, ff_up.request_id);
706 if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) { 754 if (!req || req->code != UI_FF_UPLOAD ||
755 !req->u.upload.effect) {
707 retval = -EINVAL; 756 retval = -EINVAL;
708 break; 757 break;
709 } 758 }
@@ -786,7 +835,8 @@ static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
786} 835}
787 836
788#ifdef CONFIG_COMPAT 837#ifdef CONFIG_COMPAT
789static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 838static long uinput_compat_ioctl(struct file *file,
839 unsigned int cmd, unsigned long arg)
790{ 840{
791 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg)); 841 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
792} 842}
@@ -831,4 +881,3 @@ MODULE_VERSION("0.3");
831 881
832module_init(uinput_init); 882module_init(uinput_init);
833module_exit(uinput_exit); 883module_exit(uinput_exit);
834