uinput.c

来自「linux 内核源代码」· C语言 代码 · 共 661 行 · 第 1/2 页

C
661
字号
/* *  User level driver support for input subsystem * * Heavily based on evdev.c by Vojtech Pavlik * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org> * * Changes/Revisions: *	0.3	09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>) *		- updated ff support for the changes in kernel interface *		- added MODULE_VERSION *	0.2	16/10/2004 (Micah Dowty <micah@navi.cx>) *		- added force feedback support *              - added UI_SET_PHYS *	0.1	20/06/2002 *		- first public version */#include <linux/poll.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/init.h>#include <linux/smp_lock.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/uinput.h>static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value){	struct uinput_device	*udev = input_get_drvdata(dev);	udev->buff[udev->head].type = type;	udev->buff[udev->head].code = code;	udev->buff[udev->head].value = value;	do_gettimeofday(&udev->buff[udev->head].time);	udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;	wake_up_interruptible(&udev->waitq);	return 0;}static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request){	/* Atomically allocate an ID for the given request. Returns 0 on success. */	int id;	int err = -1;	spin_lock(&udev->requests_lock);	for (id = 0; id < UINPUT_NUM_REQUESTS; id++)		if (!udev->requests[id]) {			request->id = id;			udev->requests[id] = request;			err = 0;			break;		}	spin_unlock(&udev->requests_lock);	return err;}static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id){	/* Find an input request, by ID. Returns NULL if the ID isn't valid. */	if (id >= UINPUT_NUM_REQUESTS || id < 0)		return NULL;	return udev->requests[id];}static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request){	/* Allocate slot. If none are available right away, wait. */	return wait_event_interruptible(udev->requests_waitq,					!uinput_request_alloc_id(udev, request));}static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request){	/* Mark slot as available */	udev->requests[request->id] = NULL;	wake_up(&udev->requests_waitq);	complete(&request->done);}static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request){	/* Tell our userspace app about this new request by queueing an input event */	uinput_dev_event(dev, EV_UINPUT, request->code, request->id);	/* Wait for the request to complete */	wait_for_completion(&request->done);	return request->retval;}static void uinput_dev_set_gain(struct input_dev *dev, u16 gain){	uinput_dev_event(dev, EV_FF, FF_GAIN, gain);}static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude){	uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);}static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value){	return uinput_dev_event(dev, EV_FF, effect_id, value);}static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old){	struct uinput_request request;	int retval;	request.id = -1;	init_completion(&request.done);	request.code = UI_FF_UPLOAD;	request.u.upload.effect = effect;	request.u.upload.old = old;	retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);	if (!retval)		retval = uinput_request_submit(dev, &request);	return retval;}static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id){	struct uinput_request request;	int retval;	if (!test_bit(EV_FF, dev->evbit))		return -ENOSYS;	request.id = -1;	init_completion(&request.done);	request.code = UI_FF_ERASE;	request.u.effect_id = effect_id;	retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);	if (!retval)		retval = uinput_request_submit(dev, &request);	return retval;}static void uinput_destroy_device(struct uinput_device *udev){	const char *name, *phys;	if (udev->dev) {		name = udev->dev->name;		phys = udev->dev->phys;		if (udev->state == UIST_CREATED)			input_unregister_device(udev->dev);		else			input_free_device(udev->dev);		kfree(name);		kfree(phys);		udev->dev = NULL;	}	udev->state = UIST_NEW_DEVICE;}static int uinput_create_device(struct uinput_device *udev){	struct input_dev *dev = udev->dev;	int error;	if (udev->state != UIST_SETUP_COMPLETE) {		printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);		return -EINVAL;	}	if (udev->ff_effects_max) {		error = input_ff_create(dev, udev->ff_effects_max);		if (error)			goto fail1;		dev->ff->upload = uinput_dev_upload_effect;		dev->ff->erase = uinput_dev_erase_effect;		dev->ff->playback = uinput_dev_playback;		dev->ff->set_gain = uinput_dev_set_gain;		dev->ff->set_autocenter = uinput_dev_set_autocenter;	}	error = input_register_device(udev->dev);	if (error)		goto fail2;	udev->state = UIST_CREATED;	return 0; fail2:	input_ff_destroy(dev); fail1: uinput_destroy_device(udev);	return error;}static int uinput_open(struct inode *inode, struct file *file){	struct uinput_device *newdev;	newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);	if (!newdev)		return -ENOMEM;	mutex_init(&newdev->mutex);	spin_lock_init(&newdev->requests_lock);	init_waitqueue_head(&newdev->requests_waitq);	init_waitqueue_head(&newdev->waitq);	newdev->state = UIST_NEW_DEVICE;	file->private_data = newdev;	return 0;}static int uinput_validate_absbits(struct input_dev *dev){	unsigned int cnt;	int retval = 0;	for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {		if (!test_bit(cnt, dev->absbit))			continue;		if ((dev->absmax[cnt] <= dev->absmin[cnt])) {			printk(KERN_DEBUG				"%s: invalid abs[%02x] min:%d max:%d\n",				UINPUT_NAME, cnt,				dev->absmin[cnt], dev->absmax[cnt]);			retval = -EINVAL;			break;		}		if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {			printk(KERN_DEBUG				"%s: absflat[%02x] out of range: %d "				"(min:%d/max:%d)\n",				UINPUT_NAME, cnt, dev->absflat[cnt],				dev->absmin[cnt], dev->absmax[cnt]);			retval = -EINVAL;			break;		}	}	return retval;}static int uinput_allocate_device(struct uinput_device *udev){	udev->dev = input_allocate_device();	if (!udev->dev)		return -ENOMEM;	udev->dev->event = uinput_dev_event;	input_set_drvdata(udev->dev, udev);	return 0;}static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count){	struct uinput_user_dev	*user_dev;	struct input_dev	*dev;	char			*name;	int			size;	int			retval;	if (count != sizeof(struct uinput_user_dev))		return -EINVAL;	if (!udev->dev) {		retval = uinput_allocate_device(udev);		if (retval)			return retval;	}	dev = udev->dev;	user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);	if (!user_dev)		return -ENOMEM;	if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {		retval = -EFAULT;		goto exit;	}	udev->ff_effects_max = user_dev->ff_effects_max;	size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;	if (!size) {		retval = -EINVAL;		goto exit;	}	kfree(dev->name);	dev->name = name = kmalloc(size, GFP_KERNEL);	if (!name) {		retval = -ENOMEM;		goto exit;	}	strlcpy(name, user_dev->name, size);	dev->id.bustype	= user_dev->id.bustype;	dev->id.vendor	= user_dev->id.vendor;	dev->id.product	= user_dev->id.product;	dev->id.version	= user_dev->id.version;	size = sizeof(int) * (ABS_MAX + 1);	memcpy(dev->absmax, user_dev->absmax, size);	memcpy(dev->absmin, user_dev->absmin, size);	memcpy(dev->absfuzz, user_dev->absfuzz, size);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?