cregit-Linux how code gets into the kernel

Release 4.11 drivers/input/evdev.c

Directory: drivers/input
/*
 * Event char devices, giving access to raw input device events.
 *
 * Copyright (c) 1999-2002 Vojtech Pavlik
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */


#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt


#define EVDEV_MINOR_BASE	64

#define EVDEV_MINORS		32

#define EVDEV_MIN_BUFFER_SIZE	64U

#define EVDEV_BUF_PACKETS	8

#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/input/mt.h>
#include <linux/major.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include "input-compat.h"


enum evdev_clock_type {
	
EV_CLK_REAL = 0,
	
EV_CLK_MONO,
	
EV_CLK_BOOT,
	
EV_CLK_MAX
};


struct evdev {
	
int open;
	
struct input_handle handle;
	
wait_queue_head_t wait;
	
struct evdev_client __rcu *grab;
	
struct list_head client_list;
	
spinlock_t client_lock; /* protects client_list */
	
struct mutex mutex;
	
struct device dev;
	
struct cdev cdev;
	
bool exist;
};


struct evdev_client {
	
unsigned int head;
	
unsigned int tail;
	
unsigned int packet_head; /* [future] position of the first element of next packet */
	
spinlock_t buffer_lock; /* protects access to buffer, head and tail */
	
struct fasync_struct *fasync;
	
struct evdev *evdev;
	
struct list_head node;
	
unsigned int clk_type;
	
bool revoked;
	
unsigned long *evmasks[EV_CNT];
	
unsigned int bufsize;
	
struct input_event buffer[];
};


static size_t evdev_get_mask_cnt(unsigned int type) { static const size_t counts[EV_CNT] = { /* EV_SYN==0 is EV_CNT, _not_ SYN_CNT, see EVIOCGBIT */ [EV_SYN] = EV_CNT, [EV_KEY] = KEY_CNT, [EV_REL] = REL_CNT, [EV_ABS] = ABS_CNT, [EV_MSC] = MSC_CNT, [EV_SW] = SW_CNT, [EV_LED] = LED_CNT, [EV_SND] = SND_CNT, [EV_FF] = FF_CNT, }; return (type < EV_CNT) ? counts[type] : 0; }

Contributors

PersonTokensPropCommitsCommitProp
David Herrmann89100.00%1100.00%
Total89100.00%1100.00%

/* requires the buffer lock to be held */
static bool __evdev_is_filtered(struct evdev_client *client, unsigned int type, unsigned int code) { unsigned long *mask; size_t cnt; /* EV_SYN and unknown codes are never filtered */ if (type == EV_SYN || type >= EV_CNT) return false; /* first test whether the type is filtered */ mask = client->evmasks[0]; if (mask && !test_bit(type, mask)) return true; /* unknown values are never filtered */ cnt = evdev_get_mask_cnt(type); if (!cnt || code >= cnt) return false; mask = client->evmasks[type]; return mask && !test_bit(code, mask); }

Contributors

PersonTokensPropCommitsCommitProp
David Herrmann106100.00%1100.00%
Total106100.00%1100.00%

/* flush queued events of type @type, caller must hold client->buffer_lock */
static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) { unsigned int i, head, num; unsigned int mask = client->bufsize - 1; bool is_report; struct input_event *ev; BUG_ON(type == EV_SYN); head = client->tail; client->packet_head = client->tail; /* init to 1 so a leading SYN_REPORT will not be dropped */ num = 1; for (i = client->tail; i != client->head; i = (i + 1) & mask) { ev = &client->buffer[i]; is_report = ev->type == EV_SYN && ev->code == SYN_REPORT; if (ev->type == type) { /* drop matched entry */ continue; } else if (is_report && !num) { /* drop empty SYN_REPORT groups */ continue; } else if (head != i) { /* move entry to fill the gap */ client->buffer[head].time = ev->time; client->buffer[head].type = ev->type; client->buffer[head].code = ev->code; client->buffer[head].value = ev->value; } num++; head = (head + 1) & mask; if (is_report) { num = 0; client->packet_head = head; } } client->head = head; }

Contributors

PersonTokensPropCommitsCommitProp
David Herrmann238100.00%1100.00%
Total238100.00%1100.00%


static void __evdev_queue_syn_dropped(struct evdev_client *client) { struct input_event ev; ktime_t time; time = client->clk_type == EV_CLK_REAL ? ktime_get_real() : client->clk_type == EV_CLK_MONO ? ktime_get() : ktime_get_boottime(); ev.time = ktime_to_timeval(time); ev.type = EV_SYN; ev.code = SYN_DROPPED; ev.value = 0; client->buffer[client->head++] = ev; client->head &= client->bufsize - 1; if (unlikely(client->head == client->tail)) { /* drop queue but keep our SYN_DROPPED event */ client->tail = (client->head - 1) & (client->bufsize - 1); client->packet_head = client->tail; } }

Contributors

PersonTokensPropCommitsCommitProp
David Herrmann11585.82%125.00%
Aniroop Mathur128.96%125.00%
Thomas Gleixner53.73%125.00%
Dmitry Torokhov21.49%125.00%
Total134100.00%4100.00%


static void evdev_queue_syn_dropped(struct evdev_client *client) { unsigned long flags; spin_lock_irqsave(&client->buffer_lock, flags); __evdev_queue_syn_dropped(client); spin_unlock_irqrestore(&client->buffer_lock, flags); }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov2972.50%150.00%
David Herrmann1127.50%150.00%
Total40100.00%2100.00%


static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid) { unsigned long flags; unsigned int clk_type; switch (clkid) { case CLOCK_REALTIME: clk_type = EV_CLK_REAL; break; case CLOCK_MONOTONIC: clk_type = EV_CLK_MONO; break; case CLOCK_BOOTTIME: clk_type = EV_CLK_BOOT; break; default: return -EINVAL; } if (client->clk_type != clk_type) { client->clk_type = clk_type; /* * Flush pending events and queue SYN_DROPPED event, * but only if the queue is not empty. */ spin_lock_irqsave(&client->buffer_lock, flags); if (client->head != client->tail) { client->packet_head = client->head = client->tail; __evdev_queue_syn_dropped(client); } spin_unlock_irqrestore(&client->buffer_lock, flags); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Anshul Garg5946.46%133.33%
Dmitry Torokhov4837.80%133.33%
Aniroop Mathur2015.75%133.33%
Total127100.00%3100.00%


static void __pass_event(struct evdev_client *client, const struct input_event *event) { client->buffer[client->head++] = *event; client->head &= client->bufsize - 1; if (unlikely(client->head == client->tail)) { /* * This effectively "drops" all unconsumed events, leaving * EV_SYN/SYN_DROPPED plus the newest event in the queue. */ client->tail = (client->head - 2) & (client->bufsize - 1); client->buffer[client->tail].time = event->time; client->buffer[client->tail].type = EV_SYN; client->buffer[client->tail].code = SYN_DROPPED; client->buffer[client->tail].value = 0; client->packet_head = client->tail; } if (event->type == EV_SYN && event->code == SYN_REPORT) { client->packet_head = client->head; kill_fasync(&client->fasync, SIGIO, POLL_IN); } }

Contributors

PersonTokensPropCommitsCommitProp
Jeff Brown10158.05%228.57%
Dmitry Torokhov5028.74%114.29%
Henrik Rydberg158.62%342.86%
Adam Jackson84.60%114.29%
Total174100.00%7100.00%


static void evdev_pass_values(struct evdev_client *client, const struct input_value *vals, unsigned int count, ktime_t *ev_time) { struct evdev *evdev = client->evdev; const struct input_value *v; struct input_event event; bool wakeup = false; if (client->revoked) return; event.time = ktime_to_timeval(ev_time[client->clk_type]); /* Interrupts are disabled, just acquire the lock. */ spin_lock(&client->buffer_lock); for (v = vals; v != vals + count; v++) { if (__evdev_is_filtered(client, v->type, v->code)) continue; if (v->type == EV_SYN && v->code == SYN_REPORT) { /* drop empty SYN_REPORT */ if (client->packet_head == client->head) continue; wakeup = true; } event.type = v->type; event.code = v->code; event.value = v->value; __pass_event(client, &event); } spin_unlock(&client->buffer_lock); if (wakeup) wake_up_interruptible(&evdev->wait); }

Contributors

PersonTokensPropCommitsCommitProp
Henrik Rydberg12664.29%120.00%
David Herrmann5528.06%240.00%
Jeff Brown94.59%120.00%
Aniroop Mathur63.06%120.00%
Total196100.00%5100.00%

/* * Pass incoming events to all connected clients. */
static void evdev_events(struct input_handle *handle, const struct input_value *vals, unsigned int count) { struct evdev *evdev = handle->private; struct evdev_client *client; ktime_t ev_time[EV_CLK_MAX]; ev_time[EV_CLK_MONO] = ktime_get(); ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]); ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO], TK_OFFS_BOOT); rcu_read_lock(); client = rcu_dereference(evdev->grab); if (client) evdev_pass_values(client, vals, count, ev_time); else list_for_each_entry_rcu(client, &evdev->client_list, node) evdev_pass_values(client, vals, count, ev_time); rcu_read_unlock(); }

Contributors

PersonTokensPropCommitsCommitProp
Aniroop Mathur3325.98%110.00%
Linus Torvalds (pre-git)2922.83%110.00%
Dmitry Torokhov2318.11%330.00%
Henrik Rydberg1612.60%110.00%
Zephaniah E. Hull118.66%110.00%
John Stultz97.09%110.00%
Vojtech Pavlik53.94%110.00%
Thomas Gleixner10.79%110.00%
Total127100.00%10100.00%

/* * Pass incoming event to all connected clients. */
static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) { struct input_value vals[] = { { type, code, value } }; evdev_events(handle, vals, 1); }

Contributors

PersonTokensPropCommitsCommitProp
Henrik Rydberg4086.96%133.33%
Linus Torvalds (pre-git)48.70%133.33%
Dmitry Torokhov24.35%133.33%
Total46100.00%3100.00%


static int evdev_fasync(int fd, struct file *file, int on) { struct evdev_client *client = file->private_data; return fasync_helper(fd, file, on, &client->fasync); }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)3790.24%133.33%
Dmitry Torokhov37.32%133.33%
Jonathan Corbet12.44%133.33%
Total41100.00%3100.00%


static int evdev_flush(struct file *file, fl_owner_t id) { struct evdev_client *client = file->private_data; struct evdev *evdev = client->evdev; mutex_lock(&evdev->mutex); if (evdev->exist && !client->revoked) input_flush_device(&evdev->handle, file); mutex_unlock(&evdev->mutex); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov3244.44%228.57%
Vojtech Pavlik3041.67%228.57%
Takashi Iwai45.56%114.29%
David Herrmann34.17%114.29%
Miklos Szeredi34.17%114.29%
Total72100.00%7100.00%


static void evdev_free(struct device *dev) { struct evdev *evdev = container_of(dev, struct evdev, dev); input_put_device(evdev->handle.dev); kfree(evdev); }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov3280.00%375.00%
Christoph Hellwig820.00%125.00%
Total40100.00%4100.00%

/* * Grabs an event device (along with underlying input device). * This function is called with evdev->mutex taken. */
static int evdev_grab(struct evdev *evdev, struct evdev_client *client) { int error; if (evdev->grab) return -EBUSY; error = input_grab_device(&evdev->handle); if (error) return error; rcu_assign_pointer(evdev->grab, client); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov5594.83%150.00%
Christoph Hellwig35.17%150.00%
Total58100.00%2100.00%


static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client) { struct evdev_client *grab = rcu_dereference_protected(evdev->grab, lockdep_is_held(&evdev->mutex)); if (grab != client) return -EINVAL; rcu_assign_pointer(evdev->grab, NULL); synchronize_rcu(); input_release_device(&evdev->handle); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov6797.10%375.00%
Christoph Hellwig22.90%125.00%
Total69100.00%4100.00%


static void evdev_attach_client(struct evdev *evdev, struct evdev_client *client) { spin_lock(&evdev->client_lock); list_add_tail_rcu(&client->node, &evdev->client_list); spin_unlock(&evdev->client_lock); }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov4395.56%150.00%
Christoph Hellwig24.44%150.00%
Total45100.00%2100.00%


static void evdev_detach_client(struct evdev *evdev, struct evdev_client *client) { spin_lock(&evdev->client_lock); list_del_rcu(&client->node); spin_unlock(&evdev->client_lock); synchronize_rcu(); }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov43100.00%2100.00%
Total43100.00%2100.00%


static int evdev_open_device(struct evdev *evdev) { int retval; retval = mutex_lock_interruptible(&evdev->mutex); if (retval) return retval; if (!evdev->exist) retval = -ENODEV; else if (!evdev->open++) { retval = input_open_device(&evdev->handle); if (retval) evdev->open--; } mutex_unlock(&evdev->mutex); return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov7386.90%150.00%
Oliver Neukum1113.10%150.00%
Total84100.00%2100.00%


static void evdev_close_device(struct evdev *evdev) { mutex_lock(&evdev->mutex); if (evdev->exist && !--evdev->open) input_close_device(&evdev->handle); mutex_unlock(&evdev->mutex); }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov47100.00%1100.00%
Total47100.00%1100.00%

/* * Wake up users waiting for IO so they can disconnect from * dead device. */
static void evdev_hangup(struct evdev *evdev) { struct evdev_client *client; spin_lock(&evdev->client_lock); list_for_each_entry(client, &evdev->client_list, node) kill_fasync(&client->fasync, SIGIO, POLL_HUP); spin_unlock(&evdev->client_lock); wake_up_interruptible(&evdev->wait); }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov5795.00%150.00%
Christoph Hellwig35.00%150.00%
Total60100.00%2100.00%


static int evdev_release(struct inode *inode, struct file *file) { struct evdev_client *client = file->private_data; struct evdev *evdev = client->evdev; unsigned int i; mutex_lock(&evdev->mutex); evdev_ungrab(evdev, client); mutex_unlock(&evdev->mutex); evdev_detach_client(evdev, client); for (i = 0; i < EV_CNT; ++i) kfree(client->evmasks[i]); kvfree(client); evdev_close_device(evdev); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
David Herrmann3735.58%116.67%
Dmitry Torokhov3129.81%233.33%
Linus Torvalds (pre-git)2725.96%116.67%
Zephaniah E. Hull76.73%116.67%
Vojtech Pavlik21.92%116.67%
Total104100.00%6100.00%


static unsigned int evdev_compute_buffer_size(struct input_dev *dev) { unsigned int n_events = max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS, EVDEV_MIN_BUFFER_SIZE); return roundup_pow_of_two(n_events); }

Contributors

PersonTokensPropCommitsCommitProp
Henrik Rydberg33100.00%2100.00%
Total33100.00%2100.00%


static int evdev_open(struct inode *inode, struct file *file) { struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev); unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev); unsigned int size = sizeof(struct evdev_client) + bufsize * sizeof(struct input_event); struct evdev_client *client; int error; client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); if (!client) client = vzalloc(size); if (!client) return -ENOMEM; client->bufsize = bufsize; spin_lock_init(&client->buffer_lock); client->evdev = evdev; evdev_attach_client(evdev, client); error = evdev_open_device(evdev); if (error) goto err_free_client; file->private_data = client; nonseekable_open(inode, file); return 0; err_free_client: evdev_detach_client(evdev, client); kvfree(client); return error; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov7542.13%650.00%
Linus Torvalds (pre-git)4424.72%216.67%
Daniel Stone3117.42%18.33%
Henrik Rydberg2514.04%18.33%
Vojtech Pavlik21.12%18.33%
Andrew Morton10.56%18.33%
Total178100.00%12100.00%


static ssize_t evdev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { struct evdev_client *client = file->private_data; struct evdev *evdev = client->evdev; struct input_event event; int retval = 0; if (count != 0 && count < input_event_size()) return -EINVAL; retval = mutex_lock_interruptible(&evdev->mutex); if (retval) return retval; if (!evdev->exist || client->revoked) { retval = -ENODEV; goto out; } while (retval + input_event_size() <= count) { if (input_event_from_user(buffer + retval, &event)) { retval = -EFAULT; goto out; } retval += input_event_size(); input_inject_event(&evdev->handle, event.type, event.code, event.value); } out: mutex_unlock(&evdev->mutex); return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov14784.00%550.00%
Peter Korsgaard179.71%110.00%
Juergen Kreileder42.29%110.00%
David Herrmann42.29%110.00%
Heiko Stübner21.14%110.00%
Philip Langdale10.57%110.00%
Total175100.00%10100.00%


static int evdev_fetch_next_event(struct evdev_client *client, struct input_event *event) { int have_event; spin_lock_irq(&client->buffer_lock); have_event = client->packet_head != client->tail; if (have_event) { *event = client->buffer[client->tail++]; client->tail &= client->bufsize - 1; } spin_unlock_irq(&client->buffer_lock); return have_event; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov7394.81%133.33%
Henrik Rydberg33.90%133.33%
Dima Zavin11.30%133.33%
Total77100.00%3100.00%


static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) { struct evdev_client *client = file->private_data; struct evdev *evdev = client->evdev; struct input_event event; size_t read = 0; int error; if (count != 0 && count < input_event_size()) return -EINVAL; for (;;) { if (!evdev->exist || client->revoked) return -ENODEV; if (client->packet_head == client->tail && (file->f_flags & O_NONBLOCK)) return -EAGAIN; /* * count == 0 is special - no IO is done but we check * for error conditions (see above). */ if (count == 0) break; while (read + input_event_size() <= count && evdev_fetch_next_event(client, &event)) { if (input_event_to_user(buffer + read, &event)) return -EFAULT; read += input_event_size(); } if (read) break; if (!(file->f_flags & O_NONBLOCK)) { error = wait_event_interruptible(evdev->wait, client->packet_head != client->tail || !evdev->exist || client->revoked); if (error) return error; } } return read; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov14768.69%436.36%
Juergen Kreileder3918.22%19.09%
Dima Zavin136.07%218.18%
David Herrmann83.74%19.09%
Philip Langdale41.87%19.09%
Heiko Stübner20.93%19.09%
Jeff Brown10.47%19.09%
Total214100.00%11100.00%

/* No kernel lock - fine */
static unsigned int evdev_poll(struct file *file, poll_table *wait) { struct evdev_client *client = file->private_data; struct evdev *evdev = client->evdev; unsigned int mask; poll_wait(file, &evdev->wait, wait); if (evdev->exist && !client->revoked) mask = POLLOUT | POLLWRNORM; else mask = POLLHUP | POLLERR; if (client->packet_head != client->tail) mask |= POLLIN | POLLRDNORM; return mask; }

Contributors

PersonTokensPropCommitsCommitProp
Dmitry Torokhov3638.71%233.33%
Juergen Kreileder2627.96%116.67%
Linus Torvalds (pre-git)1617.20%116.67%
David Herrmann1415.05%116.67%
Jeff Brown11.08%116.67%
Total93100.00%6100.00%

#ifdef CONFIG_COMPAT #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8) #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1) #ifdef __BIG_ENDIAN
static int bits_to_user(unsigned long *bits, unsigned int maxbit, unsigned int maxlen, void __user *p, int compat) { int len, i; if (compat) { len = BITS_TO_LONGS_COMPAT(maxbit)