Lines Matching +full:linux +full:- +full:event +full:- +full:codes

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Event char devices, giving access to raw input device events.
5 * Copyright (c) 1999-2002 Vojtech Pavlik
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/input/mt.h>
23 #include <linux/major.h>
24 #include <linux/device.h>
25 #include <linux/cdev.h>
26 #include "input-compat.h"
82 /* EV_SYN and unknown codes are never filtered */ in __evdev_is_filtered()
87 mask = client->evmasks[0]; in __evdev_is_filtered()
96 mask = client->evmasks[type]; in __evdev_is_filtered()
100 /* flush queued events of type @type, caller must hold client->buffer_lock */
104 unsigned int mask = client->bufsize - 1; in __evdev_flush_queue()
110 head = client->tail; in __evdev_flush_queue()
111 client->packet_head = client->tail; in __evdev_flush_queue()
116 for (i = client->tail; i != client->head; i = (i + 1) & mask) { in __evdev_flush_queue()
117 ev = &client->buffer[i]; in __evdev_flush_queue()
118 is_report = ev->type == EV_SYN && ev->code == SYN_REPORT; in __evdev_flush_queue()
120 if (ev->type == type) { in __evdev_flush_queue()
128 client->buffer[head] = *ev; in __evdev_flush_queue()
136 client->packet_head = head; in __evdev_flush_queue()
140 client->head = head; in __evdev_flush_queue()
145 ktime_t *ev_time = input_get_timestamp(client->evdev->handle.dev); in __evdev_queue_syn_dropped()
146 struct timespec64 ts = ktime_to_timespec64(ev_time[client->clk_type]); in __evdev_queue_syn_dropped()
155 client->buffer[client->head++] = ev; in __evdev_queue_syn_dropped()
156 client->head &= client->bufsize - 1; in __evdev_queue_syn_dropped()
158 if (unlikely(client->head == client->tail)) { in __evdev_queue_syn_dropped()
159 /* drop queue but keep our SYN_DROPPED event */ in __evdev_queue_syn_dropped()
160 client->tail = (client->head - 1) & (client->bufsize - 1); in __evdev_queue_syn_dropped()
161 client->packet_head = client->tail; in __evdev_queue_syn_dropped()
169 spin_lock_irqsave(&client->buffer_lock, flags); in evdev_queue_syn_dropped()
171 spin_unlock_irqrestore(&client->buffer_lock, flags); in evdev_queue_syn_dropped()
191 return -EINVAL; in evdev_set_clk_type()
194 if (client->clk_type != clk_type) { in evdev_set_clk_type()
195 client->clk_type = clk_type; in evdev_set_clk_type()
198 * Flush pending events and queue SYN_DROPPED event, in evdev_set_clk_type()
201 spin_lock_irqsave(&client->buffer_lock, flags); in evdev_set_clk_type()
203 if (client->head != client->tail) { in evdev_set_clk_type()
204 client->packet_head = client->head = client->tail; in evdev_set_clk_type()
208 spin_unlock_irqrestore(&client->buffer_lock, flags); in evdev_set_clk_type()
215 const struct input_event *event) in __pass_event() argument
217 client->buffer[client->head++] = *event; in __pass_event()
218 client->head &= client->bufsize - 1; in __pass_event()
220 if (unlikely(client->head == client->tail)) { in __pass_event()
223 * EV_SYN/SYN_DROPPED plus the newest event in the queue. in __pass_event()
225 client->tail = (client->head - 2) & (client->bufsize - 1); in __pass_event()
227 client->buffer[client->tail] = (struct input_event) { in __pass_event()
228 .input_event_sec = event->input_event_sec, in __pass_event()
229 .input_event_usec = event->input_event_usec, in __pass_event()
235 client->packet_head = client->tail; in __pass_event()
238 if (event->type == EV_SYN && event->code == SYN_REPORT) { in __pass_event()
239 client->packet_head = client->head; in __pass_event()
240 kill_fasync(&client->fasync, SIGIO, POLL_IN); in __pass_event()
249 struct input_event event; in evdev_pass_values() local
253 if (client->revoked) in evdev_pass_values()
256 ts = ktime_to_timespec64(ev_time[client->clk_type]); in evdev_pass_values()
257 event.input_event_sec = ts.tv_sec; in evdev_pass_values()
258 event.input_event_usec = ts.tv_nsec / NSEC_PER_USEC; in evdev_pass_values()
261 spin_lock(&client->buffer_lock); in evdev_pass_values()
264 if (__evdev_is_filtered(client, v->type, v->code)) in evdev_pass_values()
267 if (v->type == EV_SYN && v->code == SYN_REPORT) { in evdev_pass_values()
269 if (client->packet_head == client->head) in evdev_pass_values()
275 event.type = v->type; in evdev_pass_values()
276 event.code = v->code; in evdev_pass_values()
277 event.value = v->value; in evdev_pass_values()
278 __pass_event(client, &event); in evdev_pass_values()
281 spin_unlock(&client->buffer_lock); in evdev_pass_values()
284 wake_up_interruptible_poll(&client->wait, in evdev_pass_values()
294 struct evdev *evdev = handle->private; in evdev_events()
296 ktime_t *ev_time = input_get_timestamp(handle->dev); in evdev_events()
300 client = rcu_dereference(evdev->grab); in evdev_events()
305 list_for_each_entry_rcu(client, &evdev->client_list, node) in evdev_events()
315 struct evdev_client *client = file->private_data; in evdev_fasync()
317 return fasync_helper(fd, file, on, &client->fasync); in evdev_fasync()
324 input_put_device(evdev->handle.dev); in evdev_free()
329 * Grabs an event device (along with underlying input device).
330 * This function is called with evdev->mutex taken.
336 if (evdev->grab) in evdev_grab()
337 return -EBUSY; in evdev_grab()
339 error = input_grab_device(&evdev->handle); in evdev_grab()
343 rcu_assign_pointer(evdev->grab, client); in evdev_grab()
350 struct evdev_client *grab = rcu_dereference_protected(evdev->grab, in evdev_ungrab()
351 lockdep_is_held(&evdev->mutex)); in evdev_ungrab()
354 return -EINVAL; in evdev_ungrab()
356 rcu_assign_pointer(evdev->grab, NULL); in evdev_ungrab()
358 input_release_device(&evdev->handle); in evdev_ungrab()
366 spin_lock(&evdev->client_lock); in evdev_attach_client()
367 list_add_tail_rcu(&client->node, &evdev->client_list); in evdev_attach_client()
368 spin_unlock(&evdev->client_lock); in evdev_attach_client()
374 spin_lock(&evdev->client_lock); in evdev_detach_client()
375 list_del_rcu(&client->node); in evdev_detach_client()
376 spin_unlock(&evdev->client_lock); in evdev_detach_client()
384 retval = mutex_lock_interruptible(&evdev->mutex); in evdev_open_device()
388 if (!evdev->exist) in evdev_open_device()
389 retval = -ENODEV; in evdev_open_device()
390 else if (!evdev->open++) { in evdev_open_device()
391 retval = input_open_device(&evdev->handle); in evdev_open_device()
393 evdev->open--; in evdev_open_device()
396 mutex_unlock(&evdev->mutex); in evdev_open_device()
402 mutex_lock(&evdev->mutex); in evdev_close_device()
404 if (evdev->exist && !--evdev->open) in evdev_close_device()
405 input_close_device(&evdev->handle); in evdev_close_device()
407 mutex_unlock(&evdev->mutex); in evdev_close_device()
418 spin_lock(&evdev->client_lock); in evdev_hangup()
419 list_for_each_entry(client, &evdev->client_list, node) { in evdev_hangup()
420 kill_fasync(&client->fasync, SIGIO, POLL_HUP); in evdev_hangup()
421 wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR); in evdev_hangup()
423 spin_unlock(&evdev->client_lock); in evdev_hangup()
428 struct evdev_client *client = file->private_data; in evdev_release()
429 struct evdev *evdev = client->evdev; in evdev_release()
432 mutex_lock(&evdev->mutex); in evdev_release()
434 if (evdev->exist && !client->revoked) in evdev_release()
435 input_flush_device(&evdev->handle, file); in evdev_release()
438 mutex_unlock(&evdev->mutex); in evdev_release()
443 bitmap_free(client->evmasks[i]); in evdev_release()
455 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS, in evdev_compute_buffer_size()
463 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev); in evdev_open()
464 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev); in evdev_open()
470 return -ENOMEM; in evdev_open()
472 init_waitqueue_head(&client->wait); in evdev_open()
473 client->bufsize = bufsize; in evdev_open()
474 spin_lock_init(&client->buffer_lock); in evdev_open()
475 client->evdev = evdev; in evdev_open()
482 file->private_data = client; in evdev_open()
496 struct evdev_client *client = file->private_data; in evdev_write()
497 struct evdev *evdev = client->evdev; in evdev_write()
498 struct input_event event; in evdev_write() local
503 * we do not hold evdev->mutex for too long. 4096 bytes corresponds in evdev_write()
509 return -EINVAL; in evdev_write()
511 retval = mutex_lock_interruptible(&evdev->mutex); in evdev_write()
515 if (!evdev->exist || client->revoked) { in evdev_write()
516 retval = -ENODEV; in evdev_write()
522 if (input_event_from_user(buffer + retval, &event)) { in evdev_write()
523 retval = -EFAULT; in evdev_write()
528 input_inject_event(&evdev->handle, in evdev_write()
529 event.type, event.code, event.value); in evdev_write()
534 mutex_unlock(&evdev->mutex); in evdev_write()
539 struct input_event *event) in evdev_fetch_next_event() argument
543 spin_lock_irq(&client->buffer_lock); in evdev_fetch_next_event()
545 have_event = client->packet_head != client->tail; in evdev_fetch_next_event()
547 *event = client->buffer[client->tail++]; in evdev_fetch_next_event()
548 client->tail &= client->bufsize - 1; in evdev_fetch_next_event()
551 spin_unlock_irq(&client->buffer_lock); in evdev_fetch_next_event()
559 struct evdev_client *client = file->private_data; in evdev_read()
560 struct evdev *evdev = client->evdev; in evdev_read()
561 struct input_event event; in evdev_read() local
566 return -EINVAL; in evdev_read()
569 if (!evdev->exist || client->revoked) in evdev_read()
570 return -ENODEV; in evdev_read()
572 if (client->packet_head == client->tail && in evdev_read()
573 (file->f_flags & O_NONBLOCK)) in evdev_read()
574 return -EAGAIN; in evdev_read()
577 * count == 0 is special - no IO is done but we check in evdev_read()
584 evdev_fetch_next_event(client, &event)) { in evdev_read()
586 if (input_event_to_user(buffer + read, &event)) in evdev_read()
587 return -EFAULT; in evdev_read()
595 if (!(file->f_flags & O_NONBLOCK)) { in evdev_read()
596 error = wait_event_interruptible(client->wait, in evdev_read()
597 client->packet_head != client->tail || in evdev_read()
598 !evdev->exist || client->revoked); in evdev_read()
607 /* No kernel lock - fine */
610 struct evdev_client *client = file->private_data; in evdev_poll()
611 struct evdev *evdev = client->evdev; in evdev_poll()
614 poll_wait(file, &client->wait, wait); in evdev_poll()
616 if (evdev->exist && !client->revoked) in evdev_poll()
621 if (client->packet_head != client->tail) in evdev_poll()
630 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
646 i + 1 - ((i % 2) << 1), in bits_to_user()
648 return -EFAULT; in bits_to_user()
655 return -EFAULT; in bits_to_user()
668 return -EINVAL; in bits_from_user()
676 i + 1 - ((i % 2) << 1), in bits_from_user()
679 return -EFAULT; in bits_from_user()
681 *((compat_long_t *) bits + i - 1) = 0; in bits_from_user()
685 return -EINVAL; in bits_from_user()
692 return -EFAULT; in bits_from_user()
710 return copy_to_user(p, bits, len) ? -EFAULT : len; in bits_to_user()
720 return -EINVAL; in bits_from_user()
727 return copy_from_user(bits, p, len) ? -EFAULT : len; in bits_from_user()
742 return copy_to_user(p, bits, len) ? -EFAULT : len; in bits_to_user()
751 return -EINVAL; in bits_from_user()
757 return copy_from_user(bits, p, len) ? -EFAULT : len; in bits_from_user()
767 return -ENOENT; in str_to_user()
773 return copy_to_user(p, str, len) ? -EFAULT : len; in str_to_user()
785 case 0: bits = dev->evbit; len = EV_MAX; break; in handle_eviocgbit()
786 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; in handle_eviocgbit()
787 case EV_REL: bits = dev->relbit; len = REL_MAX; break; in handle_eviocgbit()
788 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; in handle_eviocgbit()
789 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; in handle_eviocgbit()
790 case EV_LED: bits = dev->ledbit; len = LED_MAX; break; in handle_eviocgbit()
791 case EV_SND: bits = dev->sndbit; len = SND_MAX; break; in handle_eviocgbit()
792 case EV_FF: bits = dev->ffbit; len = FF_MAX; break; in handle_eviocgbit()
793 case EV_SW: bits = dev->swbit; len = SW_MAX; break; in handle_eviocgbit()
794 default: return -EINVAL; in handle_eviocgbit()
811 return -EFAULT; in evdev_handle_get_keycode()
818 return -EFAULT; in evdev_handle_get_keycode()
829 return -EFAULT; in evdev_handle_get_keycode_v2()
836 return -EFAULT; in evdev_handle_get_keycode_v2()
850 return -EFAULT; in evdev_handle_set_keycode()
853 return -EFAULT; in evdev_handle_set_keycode()
863 return -EFAULT; in evdev_handle_set_keycode_v2()
866 return -EINVAL; in evdev_handle_set_keycode_v2()
876 * event so user-space will notice missing events.
879 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
881 * it while flushing the queue. This allows input-core to handle filters while
895 return -ENOMEM; in evdev_handle_get_val()
897 spin_lock_irq(&dev->event_lock); in evdev_handle_get_val()
898 spin_lock(&client->buffer_lock); in evdev_handle_get_val()
902 spin_unlock(&dev->event_lock); in evdev_handle_get_val()
906 spin_unlock_irq(&client->buffer_lock); in evdev_handle_get_val()
921 const struct input_mt *mt = dev->mt; in evdev_handle_mt_request()
927 return -EFAULT; in evdev_handle_mt_request()
929 return -EINVAL; in evdev_handle_mt_request()
931 max_slots = (size - sizeof(__u32)) / sizeof(__s32); in evdev_handle_mt_request()
932 for (i = 0; i < mt->num_slots && i < max_slots; i++) { in evdev_handle_mt_request()
933 int value = input_mt_get_value(&mt->slots[i], code); in evdev_handle_mt_request()
935 return -EFAULT; in evdev_handle_mt_request()
944 client->revoked = true; in evdev_revoke()
946 input_flush_device(&evdev->handle, file); in evdev_revoke()
947 wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR); in evdev_revoke()
952 /* must be called with evdev-mutex held */
955 const void __user *codes, in evdev_set_mask() argument
963 /* we allow unknown types and 'codes_size > size' for forward-compat */ in evdev_set_mask()
970 return -ENOMEM; in evdev_set_mask()
972 error = bits_from_user(mask, cnt - 1, codes_size, codes, compat); in evdev_set_mask()
978 spin_lock_irqsave(&client->buffer_lock, flags); in evdev_set_mask()
979 oldmask = client->evmasks[type]; in evdev_set_mask()
980 client->evmasks[type] = mask; in evdev_set_mask()
981 spin_unlock_irqrestore(&client->buffer_lock, flags); in evdev_set_mask()
988 /* must be called with evdev-mutex held */
991 void __user *codes, in evdev_get_mask() argument
1000 /* we allow unknown types and 'codes_size > size' for forward-compat */ in evdev_get_mask()
1006 mask = client->evmasks[type]; in evdev_get_mask()
1008 error = bits_to_user(mask, cnt - 1, in evdev_get_mask()
1009 xfer_size, codes, compat); in evdev_get_mask()
1015 if (put_user(0xffU, (u8 __user *)codes + i)) in evdev_get_mask()
1016 return -EFAULT; in evdev_get_mask()
1021 if (clear_user(codes + xfer_size, codes_size - xfer_size)) in evdev_get_mask()
1022 return -EFAULT; in evdev_get_mask()
1030 struct evdev_client *client = file->private_data; in evdev_do_ioctl()
1031 struct evdev *evdev = client->evdev; in evdev_do_ioctl()
1032 struct input_dev *dev = evdev->handle.dev; in evdev_do_ioctl()
1041 /* First we check for fixed-length commands */ in evdev_do_ioctl()
1048 if (copy_to_user(p, &dev->id, sizeof(struct input_id))) in evdev_do_ioctl()
1049 return -EFAULT; in evdev_do_ioctl()
1053 if (!test_bit(EV_REP, dev->evbit)) in evdev_do_ioctl()
1054 return -ENOSYS; in evdev_do_ioctl()
1055 if (put_user(dev->rep[REP_DELAY], ip)) in evdev_do_ioctl()
1056 return -EFAULT; in evdev_do_ioctl()
1057 if (put_user(dev->rep[REP_PERIOD], ip + 1)) in evdev_do_ioctl()
1058 return -EFAULT; in evdev_do_ioctl()
1062 if (!test_bit(EV_REP, dev->evbit)) in evdev_do_ioctl()
1063 return -ENOSYS; in evdev_do_ioctl()
1065 return -EFAULT; in evdev_do_ioctl()
1067 return -EFAULT; in evdev_do_ioctl()
1069 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); in evdev_do_ioctl()
1070 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); in evdev_do_ioctl()
1078 i = test_bit(EV_FF, dev->evbit) ? in evdev_do_ioctl()
1079 dev->ff->max_effects : 0; in evdev_do_ioctl()
1081 return -EFAULT; in evdev_do_ioctl()
1092 return -EINVAL; in evdev_do_ioctl()
1100 return -EFAULT; in evdev_do_ioctl()
1112 return -EFAULT; in evdev_do_ioctl()
1122 return -EFAULT; in evdev_do_ioctl()
1141 /* Now check variable-length commands */ in evdev_do_ioctl()
1146 return bits_to_user(dev->propbit, INPUT_PROP_MAX, in evdev_do_ioctl()
1153 return evdev_handle_get_val(client, dev, EV_KEY, dev->key, in evdev_do_ioctl()
1157 return evdev_handle_get_val(client, dev, EV_LED, dev->led, in evdev_do_ioctl()
1161 return evdev_handle_get_val(client, dev, EV_SND, dev->snd, in evdev_do_ioctl()
1165 return evdev_handle_get_val(client, dev, EV_SW, dev->sw, in evdev_do_ioctl()
1169 return str_to_user(dev->name, size, p); in evdev_do_ioctl()
1172 return str_to_user(dev->phys, size, p); in evdev_do_ioctl()
1175 return str_to_user(dev->uniq, size, p); in evdev_do_ioctl()
1179 return -EFAULT; in evdev_do_ioctl()
1185 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) in evdev_do_ioctl()
1186 return -EFAULT; in evdev_do_ioctl()
1191 /* Multi-number variable-length handlers */ in evdev_do_ioctl()
1193 return -EINVAL; in evdev_do_ioctl()
1204 if (!dev->absinfo) in evdev_do_ioctl()
1205 return -EINVAL; in evdev_do_ioctl()
1208 abs = dev->absinfo[t]; in evdev_do_ioctl()
1212 return -EFAULT; in evdev_do_ioctl()
1222 if (!dev->absinfo) in evdev_do_ioctl()
1223 return -EINVAL; in evdev_do_ioctl()
1229 return -EFAULT; in evdev_do_ioctl()
1236 return -EINVAL; in evdev_do_ioctl()
1239 * Take event lock to ensure that we are not in evdev_do_ioctl()
1241 * of event. in evdev_do_ioctl()
1243 spin_lock_irq(&dev->event_lock); in evdev_do_ioctl()
1244 dev->absinfo[t] = abs; in evdev_do_ioctl()
1245 spin_unlock_irq(&dev->event_lock); in evdev_do_ioctl()
1251 return -EINVAL; in evdev_do_ioctl()
1257 struct evdev_client *client = file->private_data; in evdev_ioctl_handler()
1258 struct evdev *evdev = client->evdev; in evdev_ioctl_handler()
1261 retval = mutex_lock_interruptible(&evdev->mutex); in evdev_ioctl_handler()
1265 if (!evdev->exist || client->revoked) { in evdev_ioctl_handler()
1266 retval = -ENODEV; in evdev_ioctl_handler()
1273 mutex_unlock(&evdev->mutex); in evdev_ioctl_handler()
1305 * Mark device non-existent. This disables writes, ioctls and
1311 mutex_lock(&evdev->mutex); in evdev_mark_dead()
1312 evdev->exist = false; in evdev_mark_dead()
1313 mutex_unlock(&evdev->mutex); in evdev_mark_dead()
1318 struct input_handle *handle = &evdev->handle; in evdev_cleanup()
1323 /* evdev is marked dead so no one else accesses evdev->open */ in evdev_cleanup()
1324 if (evdev->open) { in evdev_cleanup()
1351 error = -ENOMEM; in evdev_connect()
1355 INIT_LIST_HEAD(&evdev->client_list); in evdev_connect()
1356 spin_lock_init(&evdev->client_lock); in evdev_connect()
1357 mutex_init(&evdev->mutex); in evdev_connect()
1358 evdev->exist = true; in evdev_connect()
1363 dev_no -= EVDEV_MINOR_BASE; in evdev_connect()
1364 dev_set_name(&evdev->dev, "event%d", dev_no); in evdev_connect()
1366 evdev->handle.dev = input_get_device(dev); in evdev_connect()
1367 evdev->handle.name = dev_name(&evdev->dev); in evdev_connect()
1368 evdev->handle.handler = handler; in evdev_connect()
1369 evdev->handle.private = evdev; in evdev_connect()
1371 evdev->dev.devt = MKDEV(INPUT_MAJOR, minor); in evdev_connect()
1372 evdev->dev.class = &input_class; in evdev_connect()
1373 evdev->dev.parent = &dev->dev; in evdev_connect()
1374 evdev->dev.release = evdev_free; in evdev_connect()
1375 device_initialize(&evdev->dev); in evdev_connect()
1377 error = input_register_handle(&evdev->handle); in evdev_connect()
1381 cdev_init(&evdev->cdev, &evdev_fops); in evdev_connect()
1383 error = cdev_device_add(&evdev->cdev, &evdev->dev); in evdev_connect()
1391 input_unregister_handle(&evdev->handle); in evdev_connect()
1393 put_device(&evdev->dev); in evdev_connect()
1401 struct evdev *evdev = handle->private; in evdev_disconnect()
1403 cdev_device_del(&evdev->cdev, &evdev->dev); in evdev_disconnect()
1405 input_free_minor(MINOR(evdev->dev.devt)); in evdev_disconnect()
1407 put_device(&evdev->dev); in evdev_disconnect()
1441 MODULE_DESCRIPTION("Input driver event char devices");