1012 lines · 22.5 KB · C
RawDownload
/* * 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/module.h>#include <linux/init.h>#include <linux/input.h>#include <linux/major.h>#include <linux/device.h>#include "input-compat.h" struct evdev {	int open;	int minor;	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;	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 bufsize;	struct input_event buffer[];}; static struct evdev *evdev_table[EVDEV_MINORS];static DEFINE_MUTEX(evdev_table_mutex); static void evdev_pass_event(struct evdev_client *client,			     struct input_event *event){	/* Interrupts are disabled, just acquire the lock. */	spin_lock(&client->buffer_lock); 	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);	} 	spin_unlock(&client->buffer_lock);} /* * Pass incoming event to all connected clients. */static void evdev_event(struct input_handle *handle,			unsigned int type, unsigned int code, int value){	struct evdev *evdev = handle->private;	struct evdev_client *client;	struct input_event event; 	do_gettimeofday(&event.time);	event.type = type;	event.code = code;	event.value = value; 	rcu_read_lock(); 	client = rcu_dereference(evdev->grab);	if (client)		evdev_pass_event(client, &event);	else		list_for_each_entry_rcu(client, &evdev->client_list, node)			evdev_pass_event(client, &event); 	rcu_read_unlock(); 	wake_up_interruptible(&evdev->wait);} 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);} static int evdev_flush(struct file *file, fl_owner_t id){	struct evdev_client *client = file->private_data;	struct evdev *evdev = client->evdev;	int retval; 	retval = mutex_lock_interruptible(&evdev->mutex);	if (retval)		return retval; 	if (!evdev->exist)		retval = -ENODEV;	else		retval = input_flush_device(&evdev->handle, file); 	mutex_unlock(&evdev->mutex);	return retval;} static void evdev_free(struct device *dev){	struct evdev *evdev = container_of(dev, struct evdev, dev); 	input_put_device(evdev->handle.dev);	kfree(evdev);} /* * 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;} static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client){	if (evdev->grab != client)		return  -EINVAL; 	rcu_assign_pointer(evdev->grab, NULL);	synchronize_rcu();	input_release_device(&evdev->handle); 	return 0;} 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);} 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();} 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;} 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);} /* * 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);} static int evdev_release(struct inode *inode, struct file *file){	struct evdev_client *client = file->private_data;	struct evdev *evdev = client->evdev; 	mutex_lock(&evdev->mutex);	if (evdev->grab == client)		evdev_ungrab(evdev, client);	mutex_unlock(&evdev->mutex); 	evdev_detach_client(evdev, client);	kfree(client); 	evdev_close_device(evdev);	put_device(&evdev->dev); 	return 0;} 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);} static int evdev_open(struct inode *inode, struct file *file){	struct evdev *evdev;	struct evdev_client *client;	int i = iminor(inode) - EVDEV_MINOR_BASE;	unsigned int bufsize;	int error; 	if (i >= EVDEV_MINORS)		return -ENODEV; 	error = mutex_lock_interruptible(&evdev_table_mutex);	if (error)		return error;	evdev = evdev_table[i];	if (evdev)		get_device(&evdev->dev);	mutex_unlock(&evdev_table_mutex); 	if (!evdev)		return -ENODEV; 	bufsize = evdev_compute_buffer_size(evdev->handle.dev); 	client = kzalloc(sizeof(struct evdev_client) +				bufsize * sizeof(struct input_event),			 GFP_KERNEL);	if (!client) {		error = -ENOMEM;		goto err_put_evdev;	} 	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);	kfree(client); err_put_evdev:	put_device(&evdev->dev);	return error;} 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; 	if (count < input_event_size())		return -EINVAL; 	retval = mutex_lock_interruptible(&evdev->mutex);	if (retval)		return retval; 	if (!evdev->exist) {		retval = -ENODEV;		goto out;	} 	do {		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);	} while (retval + input_event_size() <= count);  out:	mutex_unlock(&evdev->mutex);	return retval;} 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->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;} 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;	int retval; 	if (count < input_event_size())		return -EINVAL; 	if (client->packet_head == client->tail && evdev->exist &&	    (file->f_flags & O_NONBLOCK))		return -EAGAIN; 	retval = wait_event_interruptible(evdev->wait,		client->packet_head != client->tail || !evdev->exist);	if (retval)		return retval; 	if (!evdev->exist)		return -ENODEV; 	while (retval + input_event_size() <= count &&	       evdev_fetch_next_event(client, &event)) { 		if (input_event_to_user(buffer + retval, &event))			return -EFAULT; 		retval += input_event_size();	} 	return retval;} /* 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); 	mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;	if (client->packet_head != client->tail)		mask |= POLLIN | POLLRDNORM; 	return mask;} #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_ENDIANstatic 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) * sizeof(compat_long_t);		if (len > maxlen)			len = maxlen; 		for (i = 0; i < len / sizeof(compat_long_t); i++)			if (copy_to_user((compat_long_t __user *) p + i,					 (compat_long_t *) bits +						i + 1 - ((i % 2) << 1),					 sizeof(compat_long_t)))				return -EFAULT;	} else {		len = BITS_TO_LONGS(maxbit) * sizeof(long);		if (len > maxlen)			len = maxlen; 		if (copy_to_user(p, bits, len))			return -EFAULT;	} 	return len;}#elsestatic int bits_to_user(unsigned long *bits, unsigned int maxbit,			unsigned int maxlen, void __user *p, int compat){	int len = compat ?			BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :			BITS_TO_LONGS(maxbit) * sizeof(long); 	if (len > maxlen)		len = maxlen; 	return copy_to_user(p, bits, len) ? -EFAULT : len;}#endif /* __BIG_ENDIAN */ #else static int bits_to_user(unsigned long *bits, unsigned int maxbit,			unsigned int maxlen, void __user *p, int compat){	int len = BITS_TO_LONGS(maxbit) * sizeof(long); 	if (len > maxlen)		len = maxlen; 	return copy_to_user(p, bits, len) ? -EFAULT : len;} #endif /* CONFIG_COMPAT */ static int str_to_user(const char *str, unsigned int maxlen, void __user *p){	int len; 	if (!str)

Lines 1–500 of 1012.