aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/evdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/evdev.c')
-rw-r--r--drivers/input/evdev.c708
1 files changed, 470 insertions, 238 deletions
diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
index f1c3d6cebd58..1d62c8b88e12 100644
--- a/drivers/input/evdev.c
+++ b/drivers/input/evdev.c
@@ -30,6 +30,8 @@ struct evdev {
30 wait_queue_head_t wait; 30 wait_queue_head_t wait;
31 struct evdev_client *grab; 31 struct evdev_client *grab;
32 struct list_head client_list; 32 struct list_head client_list;
33 spinlock_t client_lock; /* protects client_list */
34 struct mutex mutex;
33 struct device dev; 35 struct device dev;
34}; 36};
35 37
@@ -37,39 +39,54 @@ struct evdev_client {
37 struct input_event buffer[EVDEV_BUFFER_SIZE]; 39 struct input_event buffer[EVDEV_BUFFER_SIZE];
38 int head; 40 int head;
39 int tail; 41 int tail;
42 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
40 struct fasync_struct *fasync; 43 struct fasync_struct *fasync;
41 struct evdev *evdev; 44 struct evdev *evdev;
42 struct list_head node; 45 struct list_head node;
43}; 46};
44 47
45static struct evdev *evdev_table[EVDEV_MINORS]; 48static struct evdev *evdev_table[EVDEV_MINORS];
49static DEFINE_MUTEX(evdev_table_mutex);
46 50
47static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) 51static void evdev_pass_event(struct evdev_client *client,
52 struct input_event *event)
53{
54 /*
55 * Interrupts are disabled, just acquire the lock
56 */
57 spin_lock(&client->buffer_lock);
58 client->buffer[client->head++] = *event;
59 client->head &= EVDEV_BUFFER_SIZE - 1;
60 spin_unlock(&client->buffer_lock);
61
62 kill_fasync(&client->fasync, SIGIO, POLL_IN);
63}
64
65/*
66 * Pass incoming event to all connected clients.
67 */
68static void evdev_event(struct input_handle *handle,
69 unsigned int type, unsigned int code, int value)
48{ 70{
49 struct evdev *evdev = handle->private; 71 struct evdev *evdev = handle->private;
50 struct evdev_client *client; 72 struct evdev_client *client;
73 struct input_event event;
51 74
52 if (evdev->grab) { 75 do_gettimeofday(&event.time);
53 client = evdev->grab; 76 event.type = type;
77 event.code = code;
78 event.value = value;
54 79
55 do_gettimeofday(&client->buffer[client->head].time); 80 rcu_read_lock();
56 client->buffer[client->head].type = type;
57 client->buffer[client->head].code = code;
58 client->buffer[client->head].value = value;
59 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
60 81
61 kill_fasync(&client->fasync, SIGIO, POLL_IN); 82 client = rcu_dereference(evdev->grab);
62 } else 83 if (client)
63 list_for_each_entry(client, &evdev->client_list, node) { 84 evdev_pass_event(client, &event);
85 else
86 list_for_each_entry_rcu(client, &evdev->client_list, node)
87 evdev_pass_event(client, &event);
64 88
65 do_gettimeofday(&client->buffer[client->head].time); 89 rcu_read_unlock();
66 client->buffer[client->head].type = type;
67 client->buffer[client->head].code = code;
68 client->buffer[client->head].value = value;
69 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
70
71 kill_fasync(&client->fasync, SIGIO, POLL_IN);
72 }
73 90
74 wake_up_interruptible(&evdev->wait); 91 wake_up_interruptible(&evdev->wait);
75} 92}
@@ -88,38 +105,140 @@ static int evdev_flush(struct file *file, fl_owner_t id)
88{ 105{
89 struct evdev_client *client = file->private_data; 106 struct evdev_client *client = file->private_data;
90 struct evdev *evdev = client->evdev; 107 struct evdev *evdev = client->evdev;
108 int retval;
109
110 retval = mutex_lock_interruptible(&evdev->mutex);
111 if (retval)
112 return retval;
91 113
92 if (!evdev->exist) 114 if (!evdev->exist)
93 return -ENODEV; 115 retval = -ENODEV;
116 else
117 retval = input_flush_device(&evdev->handle, file);
94 118
95 return input_flush_device(&evdev->handle, file); 119 mutex_unlock(&evdev->mutex);
120 return retval;
96} 121}
97 122
98static void evdev_free(struct device *dev) 123static void evdev_free(struct device *dev)
99{ 124{
100 struct evdev *evdev = container_of(dev, struct evdev, dev); 125 struct evdev *evdev = container_of(dev, struct evdev, dev);
101 126
102 evdev_table[evdev->minor] = NULL;
103 kfree(evdev); 127 kfree(evdev);
104} 128}
105 129
130/*
131 * Grabs an event device (along with underlying input device).
132 * This function is called with evdev->mutex taken.
133 */
134static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
135{
136 int error;
137
138 if (evdev->grab)
139 return -EBUSY;
140
141 error = input_grab_device(&evdev->handle);
142 if (error)
143 return error;
144
145 rcu_assign_pointer(evdev->grab, client);
146 synchronize_rcu();
147
148 return 0;
149}
150
151static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
152{
153 if (evdev->grab != client)
154 return -EINVAL;
155
156 rcu_assign_pointer(evdev->grab, NULL);
157 synchronize_rcu();
158 input_release_device(&evdev->handle);
159
160 return 0;
161}
162
163static void evdev_attach_client(struct evdev *evdev,
164 struct evdev_client *client)
165{
166 spin_lock(&evdev->client_lock);
167 list_add_tail_rcu(&client->node, &evdev->client_list);
168 spin_unlock(&evdev->client_lock);
169 synchronize_rcu();
170}
171
172static void evdev_detach_client(struct evdev *evdev,
173 struct evdev_client *client)
174{
175 spin_lock(&evdev->client_lock);
176 list_del_rcu(&client->node);
177 spin_unlock(&evdev->client_lock);
178 synchronize_rcu();
179}
180
181static int evdev_open_device(struct evdev *evdev)
182{
183 int retval;
184
185 retval = mutex_lock_interruptible(&evdev->mutex);
186 if (retval)
187 return retval;
188
189 if (!evdev->exist)
190 retval = -ENODEV;
191 else if (!evdev->open++) {
192 retval = input_open_device(&evdev->handle);
193 if (retval)
194 evdev->open--;
195 }
196
197 mutex_unlock(&evdev->mutex);
198 return retval;
199}
200
201static void evdev_close_device(struct evdev *evdev)
202{
203 mutex_lock(&evdev->mutex);
204
205 if (evdev->exist && !--evdev->open)
206 input_close_device(&evdev->handle);
207
208 mutex_unlock(&evdev->mutex);
209}
210
211/*
212 * Wake up users waiting for IO so they can disconnect from
213 * dead device.
214 */
215static void evdev_hangup(struct evdev *evdev)
216{
217 struct evdev_client *client;
218
219 spin_lock(&evdev->client_lock);
220 list_for_each_entry(client, &evdev->client_list, node)
221 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
222 spin_unlock(&evdev->client_lock);
223
224 wake_up_interruptible(&evdev->wait);
225}
226
106static int evdev_release(struct inode *inode, struct file *file) 227static int evdev_release(struct inode *inode, struct file *file)
107{ 228{
108 struct evdev_client *client = file->private_data; 229 struct evdev_client *client = file->private_data;
109 struct evdev *evdev = client->evdev; 230 struct evdev *evdev = client->evdev;
110 231
111 if (evdev->grab == client) { 232 mutex_lock(&evdev->mutex);
112 input_release_device(&evdev->handle); 233 if (evdev->grab == client)
113 evdev->grab = NULL; 234 evdev_ungrab(evdev, client);
114 } 235 mutex_unlock(&evdev->mutex);
115 236
116 evdev_fasync(-1, file, 0); 237 evdev_fasync(-1, file, 0);
117 list_del(&client->node); 238 evdev_detach_client(evdev, client);
118 kfree(client); 239 kfree(client);
119 240
120 if (!--evdev->open && evdev->exist) 241 evdev_close_device(evdev);
121 input_close_device(&evdev->handle);
122
123 put_device(&evdev->dev); 242 put_device(&evdev->dev);
124 243
125 return 0; 244 return 0;
@@ -127,41 +246,44 @@ static int evdev_release(struct inode *inode, struct file *file)
127 246
128static int evdev_open(struct inode *inode, struct file *file) 247static int evdev_open(struct inode *inode, struct file *file)
129{ 248{
130 struct evdev_client *client;
131 struct evdev *evdev; 249 struct evdev *evdev;
250 struct evdev_client *client;
132 int i = iminor(inode) - EVDEV_MINOR_BASE; 251 int i = iminor(inode) - EVDEV_MINOR_BASE;
133 int error; 252 int error;
134 253
135 if (i >= EVDEV_MINORS) 254 if (i >= EVDEV_MINORS)
136 return -ENODEV; 255 return -ENODEV;
137 256
257 error = mutex_lock_interruptible(&evdev_table_mutex);
258 if (error)
259 return error;
138 evdev = evdev_table[i]; 260 evdev = evdev_table[i];
261 if (evdev)
262 get_device(&evdev->dev);
263 mutex_unlock(&evdev_table_mutex);
139 264
140 if (!evdev || !evdev->exist) 265 if (!evdev)
141 return -ENODEV; 266 return -ENODEV;
142 267
143 get_device(&evdev->dev);
144
145 client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); 268 client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);
146 if (!client) { 269 if (!client) {
147 error = -ENOMEM; 270 error = -ENOMEM;
148 goto err_put_evdev; 271 goto err_put_evdev;
149 } 272 }
150 273
274 spin_lock_init(&client->buffer_lock);
151 client->evdev = evdev; 275 client->evdev = evdev;
152 list_add_tail(&client->node, &evdev->client_list); 276 evdev_attach_client(evdev, client);
153 277
154 if (!evdev->open++ && evdev->exist) { 278 error = evdev_open_device(evdev);
155 error = input_open_device(&evdev->handle); 279 if (error)
156 if (error) 280 goto err_free_client;
157 goto err_free_client;
158 }
159 281
160 file->private_data = client; 282 file->private_data = client;
161 return 0; 283 return 0;
162 284
163 err_free_client: 285 err_free_client:
164 list_del(&client->node); 286 evdev_detach_client(evdev, client);
165 kfree(client); 287 kfree(client);
166 err_put_evdev: 288 err_put_evdev:
167 put_device(&evdev->dev); 289 put_device(&evdev->dev);
@@ -197,12 +319,14 @@ static inline size_t evdev_event_size(void)
197 sizeof(struct input_event_compat) : sizeof(struct input_event); 319 sizeof(struct input_event_compat) : sizeof(struct input_event);
198} 320}
199 321
200static int evdev_event_from_user(const char __user *buffer, struct input_event *event) 322static int evdev_event_from_user(const char __user *buffer,
323 struct input_event *event)
201{ 324{
202 if (COMPAT_TEST) { 325 if (COMPAT_TEST) {
203 struct input_event_compat compat_event; 326 struct input_event_compat compat_event;
204 327
205 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat))) 328 if (copy_from_user(&compat_event, buffer,
329 sizeof(struct input_event_compat)))
206 return -EFAULT; 330 return -EFAULT;
207 331
208 event->time.tv_sec = compat_event.time.tv_sec; 332 event->time.tv_sec = compat_event.time.tv_sec;
@@ -219,7 +343,8 @@ static int evdev_event_from_user(const char __user *buffer, struct input_event *
219 return 0; 343 return 0;
220} 344}
221 345
222static int evdev_event_to_user(char __user *buffer, const struct input_event *event) 346static int evdev_event_to_user(char __user *buffer,
347 const struct input_event *event)
223{ 348{
224 if (COMPAT_TEST) { 349 if (COMPAT_TEST) {
225 struct input_event_compat compat_event; 350 struct input_event_compat compat_event;
@@ -230,7 +355,8 @@ static int evdev_event_to_user(char __user *buffer, const struct input_event *ev
230 compat_event.code = event->code; 355 compat_event.code = event->code;
231 compat_event.value = event->value; 356 compat_event.value = event->value;
232 357
233 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat))) 358 if (copy_to_user(buffer, &compat_event,
359 sizeof(struct input_event_compat)))
234 return -EFAULT; 360 return -EFAULT;
235 361
236 } else { 362 } else {
@@ -248,7 +374,8 @@ static inline size_t evdev_event_size(void)
248 return sizeof(struct input_event); 374 return sizeof(struct input_event);
249} 375}
250 376
251static int evdev_event_from_user(const char __user *buffer, struct input_event *event) 377static int evdev_event_from_user(const char __user *buffer,
378 struct input_event *event)
252{ 379{
253 if (copy_from_user(event, buffer, sizeof(struct input_event))) 380 if (copy_from_user(event, buffer, sizeof(struct input_event)))
254 return -EFAULT; 381 return -EFAULT;
@@ -256,7 +383,8 @@ static int evdev_event_from_user(const char __user *buffer, struct input_event *
256 return 0; 383 return 0;
257} 384}
258 385
259static int evdev_event_to_user(char __user *buffer, const struct input_event *event) 386static int evdev_event_to_user(char __user *buffer,
387 const struct input_event *event)
260{ 388{
261 if (copy_to_user(buffer, event, sizeof(struct input_event))) 389 if (copy_to_user(buffer, event, sizeof(struct input_event)))
262 return -EFAULT; 390 return -EFAULT;
@@ -266,37 +394,71 @@ static int evdev_event_to_user(char __user *buffer, const struct input_event *ev
266 394
267#endif /* CONFIG_COMPAT */ 395#endif /* CONFIG_COMPAT */
268 396
269static ssize_t evdev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 397static ssize_t evdev_write(struct file *file, const char __user *buffer,
398 size_t count, loff_t *ppos)
270{ 399{
271 struct evdev_client *client = file->private_data; 400 struct evdev_client *client = file->private_data;
272 struct evdev *evdev = client->evdev; 401 struct evdev *evdev = client->evdev;
273 struct input_event event; 402 struct input_event event;
274 int retval = 0; 403 int retval;
275 404
276 if (!evdev->exist) 405 retval = mutex_lock_interruptible(&evdev->mutex);
277 return -ENODEV; 406 if (retval)
407 return retval;
408
409 if (!evdev->exist) {
410 retval = -ENODEV;
411 goto out;
412 }
278 413
279 while (retval < count) { 414 while (retval < count) {
280 415
281 if (evdev_event_from_user(buffer + retval, &event)) 416 if (evdev_event_from_user(buffer + retval, &event)) {
282 return -EFAULT; 417 retval = -EFAULT;
283 input_inject_event(&evdev->handle, event.type, event.code, event.value); 418 goto out;
419 }
420
421 input_inject_event(&evdev->handle,
422 event.type, event.code, event.value);
284 retval += evdev_event_size(); 423 retval += evdev_event_size();
285 } 424 }
286 425
426 out:
427 mutex_unlock(&evdev->mutex);
287 return retval; 428 return retval;
288} 429}
289 430
290static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) 431static int evdev_fetch_next_event(struct evdev_client *client,
432 struct input_event *event)
433{
434 int have_event;
435
436 spin_lock_irq(&client->buffer_lock);
437
438 have_event = client->head != client->tail;
439 if (have_event) {
440 *event = client->buffer[client->tail++];
441 client->tail &= EVDEV_BUFFER_SIZE - 1;
442 }
443
444 spin_unlock_irq(&client->buffer_lock);
445
446 return have_event;
447}
448
449static ssize_t evdev_read(struct file *file, char __user *buffer,
450 size_t count, loff_t *ppos)
291{ 451{
292 struct evdev_client *client = file->private_data; 452 struct evdev_client *client = file->private_data;
293 struct evdev *evdev = client->evdev; 453 struct evdev *evdev = client->evdev;
454 struct input_event event;
294 int retval; 455 int retval;
295 456
296 if (count < evdev_event_size()) 457 if (count < evdev_event_size())
297 return -EINVAL; 458 return -EINVAL;
298 459
299 if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK)) 460 if (client->head == client->tail && evdev->exist &&
461 (file->f_flags & O_NONBLOCK))
300 return -EAGAIN; 462 return -EAGAIN;
301 463
302 retval = wait_event_interruptible(evdev->wait, 464 retval = wait_event_interruptible(evdev->wait,
@@ -307,14 +469,12 @@ static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count,
307 if (!evdev->exist) 469 if (!evdev->exist)
308 return -ENODEV; 470 return -ENODEV;
309 471
310 while (client->head != client->tail && retval + evdev_event_size() <= count) { 472 while (retval + evdev_event_size() <= count &&
311 473 evdev_fetch_next_event(client, &event)) {
312 struct input_event *event = (struct input_event *) client->buffer + client->tail;
313 474
314 if (evdev_event_to_user(buffer + retval, event)) 475 if (evdev_event_to_user(buffer + retval, &event))
315 return -EFAULT; 476 return -EFAULT;
316 477
317 client->tail = (client->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
318 retval += evdev_event_size(); 478 retval += evdev_event_size();
319 } 479 }
320 480
@@ -409,8 +569,8 @@ static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
409 return copy_to_user(p, str, len) ? -EFAULT : len; 569 return copy_to_user(p, str, len) ? -EFAULT : len;
410} 570}
411 571
412static long evdev_ioctl_handler(struct file *file, unsigned int cmd, 572static long evdev_do_ioctl(struct file *file, unsigned int cmd,
413 void __user *p, int compat_mode) 573 void __user *p, int compat_mode)
414{ 574{
415 struct evdev_client *client = file->private_data; 575 struct evdev_client *client = file->private_data;
416 struct evdev *evdev = client->evdev; 576 struct evdev *evdev = client->evdev;
@@ -421,215 +581,289 @@ static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
421 int i, t, u, v; 581 int i, t, u, v;
422 int error; 582 int error;
423 583
424 if (!evdev->exist)
425 return -ENODEV;
426
427 switch (cmd) { 584 switch (cmd) {
428 585
429 case EVIOCGVERSION: 586 case EVIOCGVERSION:
430 return put_user(EV_VERSION, ip); 587 return put_user(EV_VERSION, ip);
431 588
432 case EVIOCGID: 589 case EVIOCGID:
433 if (copy_to_user(p, &dev->id, sizeof(struct input_id))) 590 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
434 return -EFAULT; 591 return -EFAULT;
435 return 0; 592 return 0;
436 593
437 case EVIOCGREP: 594 case EVIOCGREP:
438 if (!test_bit(EV_REP, dev->evbit)) 595 if (!test_bit(EV_REP, dev->evbit))
439 return -ENOSYS; 596 return -ENOSYS;
440 if (put_user(dev->rep[REP_DELAY], ip)) 597 if (put_user(dev->rep[REP_DELAY], ip))
441 return -EFAULT; 598 return -EFAULT;
442 if (put_user(dev->rep[REP_PERIOD], ip + 1)) 599 if (put_user(dev->rep[REP_PERIOD], ip + 1))
443 return -EFAULT; 600 return -EFAULT;
444 return 0; 601 return 0;
445 602
446 case EVIOCSREP: 603 case EVIOCSREP:
447 if (!test_bit(EV_REP, dev->evbit)) 604 if (!test_bit(EV_REP, dev->evbit))
448 return -ENOSYS; 605 return -ENOSYS;
449 if (get_user(u, ip)) 606 if (get_user(u, ip))
450 return -EFAULT; 607 return -EFAULT;
451 if (get_user(v, ip + 1)) 608 if (get_user(v, ip + 1))
452 return -EFAULT; 609 return -EFAULT;
453 610
454 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); 611 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
455 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); 612 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
456 613
457 return 0; 614 return 0;
458 615
459 case EVIOCGKEYCODE: 616 case EVIOCGKEYCODE:
460 if (get_user(t, ip)) 617 if (get_user(t, ip))
461 return -EFAULT; 618 return -EFAULT;
462 619
463 error = dev->getkeycode(dev, t, &v); 620 error = dev->getkeycode(dev, t, &v);
464 if (error) 621 if (error)
465 return error; 622 return error;
466 623
467 if (put_user(v, ip + 1)) 624 if (put_user(v, ip + 1))
468 return -EFAULT; 625 return -EFAULT;
469 626
470 return 0; 627 return 0;
471 628
472 case EVIOCSKEYCODE: 629 case EVIOCSKEYCODE:
473 if (get_user(t, ip) || get_user(v, ip + 1)) 630 if (get_user(t, ip) || get_user(v, ip + 1))
474 return -EFAULT; 631 return -EFAULT;
475 632
476 return dev->setkeycode(dev, t, v); 633 return dev->setkeycode(dev, t, v);
477 634
478 case EVIOCSFF: 635 case EVIOCSFF:
479 if (copy_from_user(&effect, p, sizeof(effect))) 636 if (copy_from_user(&effect, p, sizeof(effect)))
480 return -EFAULT; 637 return -EFAULT;
481 638
482 error = input_ff_upload(dev, &effect, file); 639 error = input_ff_upload(dev, &effect, file);
483 640
484 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) 641 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
485 return -EFAULT; 642 return -EFAULT;
486 643
487 return error; 644 return error;
488 645
489 case EVIOCRMFF: 646 case EVIOCRMFF:
490 return input_ff_erase(dev, (int)(unsigned long) p, file); 647 return input_ff_erase(dev, (int)(unsigned long) p, file);
491 648
492 case EVIOCGEFFECTS: 649 case EVIOCGEFFECTS:
493 i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0; 650 i = test_bit(EV_FF, dev->evbit) ?
494 if (put_user(i, ip)) 651 dev->ff->max_effects : 0;
495 return -EFAULT; 652 if (put_user(i, ip))
496 return 0; 653 return -EFAULT;
497 654 return 0;
498 case EVIOCGRAB: 655
499 if (p) { 656 case EVIOCGRAB:
500 if (evdev->grab) 657 if (p)
501 return -EBUSY; 658 return evdev_grab(evdev, client);
502 if (input_grab_device(&evdev->handle)) 659 else
503 return -EBUSY; 660 return evdev_ungrab(evdev, client);
504 evdev->grab = client;
505 return 0;
506 } else {
507 if (evdev->grab != client)
508 return -EINVAL;
509 input_release_device(&evdev->handle);
510 evdev->grab = NULL;
511 return 0;
512 }
513 661
514 default: 662 default:
515 663
516 if (_IOC_TYPE(cmd) != 'E') 664 if (_IOC_TYPE(cmd) != 'E')
517 return -EINVAL; 665 return -EINVAL;
518 666
519 if (_IOC_DIR(cmd) == _IOC_READ) { 667 if (_IOC_DIR(cmd) == _IOC_READ) {
520 668
521 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) { 669 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0))) {
522 670
523 unsigned long *bits; 671 unsigned long *bits;
524 int len; 672 int len;
525 673
526 switch (_IOC_NR(cmd) & EV_MAX) { 674 switch (_IOC_NR(cmd) & EV_MAX) {
527 case 0: bits = dev->evbit; len = EV_MAX; break;
528 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
529 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
530 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
531 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
532 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
533 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
534 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
535 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
536 default: return -EINVAL;
537 }
538 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
539 }
540 675
541 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) 676 case 0: bits = dev->evbit; len = EV_MAX; break;
542 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd), 677 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
543 p, compat_mode); 678 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
679 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
680 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
681 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
682 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
683 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
684 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
685 default: return -EINVAL;
686 }
687 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
688 }
544 689
545 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) 690 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
546 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd), 691 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
547 p, compat_mode); 692 p, compat_mode);
548 693
549 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) 694 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
550 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd), 695 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
551 p, compat_mode); 696 p, compat_mode);
552 697
553 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) 698 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
554 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd), 699 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
555 p, compat_mode); 700 p, compat_mode);
556 701
557 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) 702 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
558 return str_to_user(dev->name, _IOC_SIZE(cmd), p); 703 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
704 p, compat_mode);
559 705
560 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) 706 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
561 return str_to_user(dev->phys, _IOC_SIZE(cmd), p); 707 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
562 708
563 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) 709 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
564 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p); 710 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
565 711
566 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { 712 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
713 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
567 714
568 t = _IOC_NR(cmd) & ABS_MAX; 715 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
569 716
570 abs.value = dev->abs[t]; 717 t = _IOC_NR(cmd) & ABS_MAX;
571 abs.minimum = dev->absmin[t];
572 abs.maximum = dev->absmax[t];
573 abs.fuzz = dev->absfuzz[t];
574 abs.flat = dev->absflat[t];
575 718
576 if (copy_to_user(p, &abs, sizeof(struct input_absinfo))) 719 abs.value = dev->abs[t];
577 return -EFAULT; 720 abs.minimum = dev->absmin[t];
721 abs.maximum = dev->absmax[t];
722 abs.fuzz = dev->absfuzz[t];
723 abs.flat = dev->absflat[t];
578 724
579 return 0; 725 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
580 } 726 return -EFAULT;
581 727
728 return 0;
582 } 729 }
583 730
584 if (_IOC_DIR(cmd) == _IOC_WRITE) { 731 }
732
733 if (_IOC_DIR(cmd) == _IOC_WRITE) {
585 734
586 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { 735 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
587 736
588 t = _IOC_NR(cmd) & ABS_MAX; 737 t = _IOC_NR(cmd) & ABS_MAX;
589 738
590 if (copy_from_user(&abs, p, sizeof(struct input_absinfo))) 739 if (copy_from_user(&abs, p,
591 return -EFAULT; 740 sizeof(struct input_absinfo)))
741 return -EFAULT;
592 742
593 dev->abs[t] = abs.value; 743 /*
594 dev->absmin[t] = abs.minimum; 744 * Take event lock to ensure that we are not
595 dev->absmax[t] = abs.maximum; 745 * changing device parameters in the middle
596 dev->absfuzz[t] = abs.fuzz; 746 * of event.
597 dev->absflat[t] = abs.flat; 747 */
748 spin_lock_irq(&dev->event_lock);
598 749
599 return 0; 750 dev->abs[t] = abs.value;
600 } 751 dev->absmin[t] = abs.minimum;
752 dev->absmax[t] = abs.maximum;
753 dev->absfuzz[t] = abs.fuzz;
754 dev->absflat[t] = abs.flat;
755
756 spin_unlock_irq(&dev->event_lock);
757
758 return 0;
601 } 759 }
760 }
602 } 761 }
603 return -EINVAL; 762 return -EINVAL;
604} 763}
605 764
765static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
766 void __user *p, int compat_mode)
767{
768 struct evdev_client *client = file->private_data;
769 struct evdev *evdev = client->evdev;
770 int retval;
771
772 retval = mutex_lock_interruptible(&evdev->mutex);
773 if (retval)
774 return retval;
775
776 if (!evdev->exist) {
777 retval = -ENODEV;
778 goto out;
779 }
780
781 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
782
783 out:
784 mutex_unlock(&evdev->mutex);
785 return retval;
786}
787
606static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 788static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
607{ 789{
608 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); 790 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
609} 791}
610 792
611#ifdef CONFIG_COMPAT 793#ifdef CONFIG_COMPAT
612static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) 794static long evdev_ioctl_compat(struct file *file,
795 unsigned int cmd, unsigned long arg)
613{ 796{
614 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); 797 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
615} 798}
616#endif 799#endif
617 800
618static const struct file_operations evdev_fops = { 801static const struct file_operations evdev_fops = {
619 .owner = THIS_MODULE, 802 .owner = THIS_MODULE,
620 .read = evdev_read, 803 .read = evdev_read,
621 .write = evdev_write, 804 .write = evdev_write,
622 .poll = evdev_poll, 805 .poll = evdev_poll,
623 .open = evdev_open, 806 .open = evdev_open,
624 .release = evdev_release, 807 .release = evdev_release,
625 .unlocked_ioctl = evdev_ioctl, 808 .unlocked_ioctl = evdev_ioctl,
626#ifdef CONFIG_COMPAT 809#ifdef CONFIG_COMPAT
627 .compat_ioctl = evdev_ioctl_compat, 810 .compat_ioctl = evdev_ioctl_compat,
628#endif 811#endif
629 .fasync = evdev_fasync, 812 .fasync = evdev_fasync,
630 .flush = evdev_flush 813 .flush = evdev_flush
631}; 814};
632 815
816static int evdev_install_chrdev(struct evdev *evdev)
817{
818 /*
819 * No need to do any locking here as calls to connect and
820 * disconnect are serialized by the input core
821 */
822 evdev_table[evdev->minor] = evdev;
823 return 0;
824}
825
826static void evdev_remove_chrdev(struct evdev *evdev)
827{
828 /*
829 * Lock evdev table to prevent race with evdev_open()
830 */
831 mutex_lock(&evdev_table_mutex);
832 evdev_table[evdev->minor] = NULL;
833 mutex_unlock(&evdev_table_mutex);
834}
835
836/*
837 * Mark device non-existent. This disables writes, ioctls and
838 * prevents new users from opening the device. Already posted
839 * blocking reads will stay, however new ones will fail.
840 */
841static void evdev_mark_dead(struct evdev *evdev)
842{
843 mutex_lock(&evdev->mutex);
844 evdev->exist = 0;
845 mutex_unlock(&evdev->mutex);
846}
847
848static void evdev_cleanup(struct evdev *evdev)
849{
850 struct input_handle *handle = &evdev->handle;
851
852 evdev_mark_dead(evdev);
853 evdev_hangup(evdev);
854 evdev_remove_chrdev(evdev);
855
856 /* evdev is marked dead so no one else accesses evdev->open */
857 if (evdev->open) {
858 input_flush_device(handle, NULL);
859 input_close_device(handle);
860 }
861}
862
863/*
864 * Create new evdev device. Note that input core serializes calls
865 * to connect and disconnect so we don't need to lock evdev_table here.
866 */
633static int evdev_connect(struct input_handler *handler, struct input_dev *dev, 867static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
634 const struct input_device_id *id) 868 const struct input_device_id *id)
635{ 869{
@@ -637,7 +871,10 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
637 int minor; 871 int minor;
638 int error; 872 int error;
639 873
640 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); 874 for (minor = 0; minor < EVDEV_MINORS; minor++)
875 if (!evdev_table[minor])
876 break;
877
641 if (minor == EVDEV_MINORS) { 878 if (minor == EVDEV_MINORS) {
642 printk(KERN_ERR "evdev: no more free evdev devices\n"); 879 printk(KERN_ERR "evdev: no more free evdev devices\n");
643 return -ENFILE; 880 return -ENFILE;
@@ -648,38 +885,44 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
648 return -ENOMEM; 885 return -ENOMEM;
649 886
650 INIT_LIST_HEAD(&evdev->client_list); 887 INIT_LIST_HEAD(&evdev->client_list);
888 spin_lock_init(&evdev->client_lock);
889 mutex_init(&evdev->mutex);
651 init_waitqueue_head(&evdev->wait); 890 init_waitqueue_head(&evdev->wait);
652 891
892 snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
653 evdev->exist = 1; 893 evdev->exist = 1;
654 evdev->minor = minor; 894 evdev->minor = minor;
895
655 evdev->handle.dev = dev; 896 evdev->handle.dev = dev;
656 evdev->handle.name = evdev->name; 897 evdev->handle.name = evdev->name;
657 evdev->handle.handler = handler; 898 evdev->handle.handler = handler;
658 evdev->handle.private = evdev; 899 evdev->handle.private = evdev;
659 snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
660 900
661 snprintf(evdev->dev.bus_id, sizeof(evdev->dev.bus_id), 901 strlcpy(evdev->dev.bus_id, evdev->name, sizeof(evdev->dev.bus_id));
662 "event%d", minor); 902 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
663 evdev->dev.class = &input_class; 903 evdev->dev.class = &input_class;
664 evdev->dev.parent = &dev->dev; 904 evdev->dev.parent = &dev->dev;
665 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
666 evdev->dev.release = evdev_free; 905 evdev->dev.release = evdev_free;
667 device_initialize(&evdev->dev); 906 device_initialize(&evdev->dev);
668 907
669 evdev_table[minor] = evdev; 908 error = input_register_handle(&evdev->handle);
670
671 error = device_add(&evdev->dev);
672 if (error) 909 if (error)
673 goto err_free_evdev; 910 goto err_free_evdev;
674 911
675 error = input_register_handle(&evdev->handle); 912 error = evdev_install_chrdev(evdev);
913 if (error)
914 goto err_unregister_handle;
915
916 error = device_add(&evdev->dev);
676 if (error) 917 if (error)
677 goto err_delete_evdev; 918 goto err_cleanup_evdev;
678 919
679 return 0; 920 return 0;
680 921
681 err_delete_evdev: 922 err_cleanup_evdev:
682 device_del(&evdev->dev); 923 evdev_cleanup(evdev);
924 err_unregister_handle:
925 input_unregister_handle(&evdev->handle);
683 err_free_evdev: 926 err_free_evdev:
684 put_device(&evdev->dev); 927 put_device(&evdev->dev);
685 return error; 928 return error;
@@ -688,21 +931,10 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
688static void evdev_disconnect(struct input_handle *handle) 931static void evdev_disconnect(struct input_handle *handle)
689{ 932{
690 struct evdev *evdev = handle->private; 933 struct evdev *evdev = handle->private;
691 struct evdev_client *client;
692 934
693 input_unregister_handle(handle);
694 device_del(&evdev->dev); 935 device_del(&evdev->dev);
695 936 evdev_cleanup(evdev);
696 evdev->exist = 0; 937 input_unregister_handle(handle);
697
698 if (evdev->open) {
699 input_flush_device(handle, NULL);
700 input_close_device(handle);
701 list_for_each_entry(client, &evdev->client_list, node)
702 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
703 wake_up_interruptible(&evdev->wait);
704 }
705
706 put_device(&evdev->dev); 938 put_device(&evdev->dev);
707} 939}
708 940
@@ -714,13 +946,13 @@ static const struct input_device_id evdev_ids[] = {
714MODULE_DEVICE_TABLE(input, evdev_ids); 946MODULE_DEVICE_TABLE(input, evdev_ids);
715 947
716static struct input_handler evdev_handler = { 948static struct input_handler evdev_handler = {
717 .event = evdev_event, 949 .event = evdev_event,
718 .connect = evdev_connect, 950 .connect = evdev_connect,
719 .disconnect = evdev_disconnect, 951 .disconnect = evdev_disconnect,
720 .fops = &evdev_fops, 952 .fops = &evdev_fops,
721 .minor = EVDEV_MINOR_BASE, 953 .minor = EVDEV_MINOR_BASE,
722 .name = "evdev", 954 .name = "evdev",
723 .id_table = evdev_ids, 955 .id_table = evdev_ids,
724}; 956};
725 957
726static int __init evdev_init(void) 958static int __init evdev_init(void)