Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Event char devices, giving access to raw input device events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 1999-2002 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #define EVDEV_MINOR_BASE	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #define EVDEV_MINORS		32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #define EVDEV_MIN_BUFFER_SIZE	64U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #define EVDEV_BUF_PACKETS	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/input/mt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "input-compat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) struct evdev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	int open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	struct input_handle handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	struct evdev_client __rcu *grab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	struct list_head client_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	spinlock_t client_lock; /* protects client_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	struct cdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	bool exist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) struct evdev_client {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	unsigned int head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	unsigned int tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	unsigned int packet_head; /* [future] position of the first element of next packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	spinlock_t buffer_lock; /* protects access to buffer, head and tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	struct fasync_struct *fasync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	struct evdev *evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	enum input_clock_type clk_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	bool revoked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	unsigned long *evmasks[EV_CNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	unsigned int bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	struct input_event buffer[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static size_t evdev_get_mask_cnt(unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	static const size_t counts[EV_CNT] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		/* EV_SYN==0 is EV_CNT, _not_ SYN_CNT, see EVIOCGBIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		[EV_SYN]	= EV_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		[EV_KEY]	= KEY_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		[EV_REL]	= REL_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		[EV_ABS]	= ABS_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		[EV_MSC]	= MSC_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		[EV_SW]		= SW_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		[EV_LED]	= LED_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		[EV_SND]	= SND_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		[EV_FF]		= FF_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	return (type < EV_CNT) ? counts[type] : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) /* requires the buffer lock to be held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) static bool __evdev_is_filtered(struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 				unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 				unsigned int code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	unsigned long *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	size_t cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	/* EV_SYN and unknown codes are never filtered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	if (type == EV_SYN || type >= EV_CNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	/* first test whether the type is filtered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	mask = client->evmasks[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	if (mask && !test_bit(type, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	/* unknown values are never filtered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	cnt = evdev_get_mask_cnt(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	if (!cnt || code >= cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	mask = client->evmasks[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	return mask && !test_bit(code, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) /* flush queued events of type @type, caller must hold client->buffer_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	unsigned int i, head, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	unsigned int mask = client->bufsize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	bool is_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct input_event *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	BUG_ON(type == EV_SYN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	head = client->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	client->packet_head = client->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	/* init to 1 so a leading SYN_REPORT will not be dropped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	num = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	for (i = client->tail; i != client->head; i = (i + 1) & mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		ev = &client->buffer[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		if (ev->type == type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			/* drop matched entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		} else if (is_report && !num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 			/* drop empty SYN_REPORT groups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		} else if (head != i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 			/* move entry to fill the gap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 			client->buffer[head] = *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		num++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		head = (head + 1) & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		if (is_report) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 			num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 			client->packet_head = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	client->head = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static void __evdev_queue_syn_dropped(struct evdev_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	ktime_t *ev_time = input_get_timestamp(client->evdev->handle.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	struct timespec64 ts = ktime_to_timespec64(ev_time[client->clk_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	struct input_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	ev.input_event_sec = ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	ev.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	ev.type = EV_SYN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	ev.code = SYN_DROPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	ev.value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	client->buffer[client->head++] = ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	client->head &= client->bufsize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	if (unlikely(client->head == client->tail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		/* drop queue but keep our SYN_DROPPED event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		client->tail = (client->head - 1) & (client->bufsize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		client->packet_head = client->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static void evdev_queue_syn_dropped(struct evdev_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	spin_lock_irqsave(&client->buffer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	__evdev_queue_syn_dropped(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	spin_unlock_irqrestore(&client->buffer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	enum input_clock_type clk_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	switch (clkid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	case CLOCK_REALTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		clk_type = INPUT_CLK_REAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	case CLOCK_MONOTONIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		clk_type = INPUT_CLK_MONO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	case CLOCK_BOOTTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		clk_type = INPUT_CLK_BOOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	if (client->clk_type != clk_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		client->clk_type = clk_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		 * Flush pending events and queue SYN_DROPPED event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		 * but only if the queue is not empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		spin_lock_irqsave(&client->buffer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		if (client->head != client->tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			client->packet_head = client->head = client->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			__evdev_queue_syn_dropped(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		spin_unlock_irqrestore(&client->buffer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) static void __pass_event(struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 			 const struct input_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	client->buffer[client->head++] = *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	client->head &= client->bufsize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	if (unlikely(client->head == client->tail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		 * This effectively "drops" all unconsumed events, leaving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		client->tail = (client->head - 2) & (client->bufsize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		client->buffer[client->tail] = (struct input_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			.input_event_sec = event->input_event_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			.input_event_usec = event->input_event_usec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 			.type = EV_SYN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			.code = SYN_DROPPED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			.value = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		client->packet_head = client->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	if (event->type == EV_SYN && event->code == SYN_REPORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		client->packet_head = client->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		kill_fasync(&client->fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) static void evdev_pass_values(struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			const struct input_value *vals, unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			ktime_t *ev_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	const struct input_value *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	struct input_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	bool wakeup = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	if (client->revoked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	ts = ktime_to_timespec64(ev_time[client->clk_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	event.input_event_sec = ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	event.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	/* Interrupts are disabled, just acquire the lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	spin_lock(&client->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	for (v = vals; v != vals + count; v++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		if (__evdev_is_filtered(client, v->type, v->code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		if (v->type == EV_SYN && v->code == SYN_REPORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			/* drop empty SYN_REPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			if (client->packet_head == client->head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			wakeup = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		event.type = v->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		event.code = v->code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		event.value = v->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		__pass_event(client, &event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	spin_unlock(&client->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	if (wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		wake_up_interruptible_poll(&client->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289)  * Pass incoming events to all connected clients.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) static void evdev_events(struct input_handle *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 			 const struct input_value *vals, unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct evdev *evdev = handle->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	struct evdev_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	ktime_t *ev_time = input_get_timestamp(handle->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	client = rcu_dereference(evdev->grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		evdev_pass_values(client, vals, count, ev_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		list_for_each_entry_rcu(client, &evdev->client_list, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 			evdev_pass_values(client, vals, count, ev_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312)  * Pass incoming event to all connected clients.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) static void evdev_event(struct input_handle *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 			unsigned int type, unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct input_value vals[] = { { type, code, value } };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	evdev_events(handle, vals, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) static int evdev_fasync(int fd, struct file *file, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	struct evdev_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	return fasync_helper(fd, file, on, &client->fasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static void evdev_free(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	struct evdev *evdev = container_of(dev, struct evdev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	input_put_device(evdev->handle.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	kfree(evdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * Grabs an event device (along with underlying input device).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  * This function is called with evdev->mutex taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (evdev->grab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	error = input_grab_device(&evdev->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	rcu_assign_pointer(evdev->grab, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 					lockdep_is_held(&evdev->mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	if (grab != client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		return  -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	rcu_assign_pointer(evdev->grab, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	input_release_device(&evdev->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) static void evdev_attach_client(struct evdev *evdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 				struct evdev_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	spin_lock(&evdev->client_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	list_add_tail_rcu(&client->node, &evdev->client_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	spin_unlock(&evdev->client_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) static void evdev_detach_client(struct evdev *evdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 				struct evdev_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	spin_lock(&evdev->client_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	list_del_rcu(&client->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	spin_unlock(&evdev->client_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) static int evdev_open_device(struct evdev *evdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	retval = mutex_lock_interruptible(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (!evdev->exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	else if (!evdev->open++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		retval = input_open_device(&evdev->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			evdev->open--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	mutex_unlock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) static void evdev_close_device(struct evdev *evdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	mutex_lock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	if (evdev->exist && !--evdev->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		input_close_device(&evdev->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	mutex_unlock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * Wake up users waiting for IO so they can disconnect from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  * dead device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) static void evdev_hangup(struct evdev *evdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	struct evdev_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	spin_lock(&evdev->client_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	list_for_each_entry(client, &evdev->client_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		kill_fasync(&client->fasync, SIGIO, POLL_HUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	spin_unlock(&evdev->client_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) static int evdev_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	struct evdev_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct evdev *evdev = client->evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	mutex_lock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	if (evdev->exist && !client->revoked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		input_flush_device(&evdev->handle, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	evdev_ungrab(evdev, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	mutex_unlock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	evdev_detach_client(evdev, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	for (i = 0; i < EV_CNT; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		bitmap_free(client->evmasks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	kvfree(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	evdev_close_device(evdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	unsigned int n_events =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		    EVDEV_MIN_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	return roundup_pow_of_two(n_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) static int evdev_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	struct evdev_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	client = kvzalloc(struct_size(client, buffer, bufsize), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	if (!client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	init_waitqueue_head(&client->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	client->bufsize = bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	spin_lock_init(&client->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	client->evdev = evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	evdev_attach_client(evdev, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	error = evdev_open_device(evdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		goto err_free_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	file->private_data = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  err_free_client:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	evdev_detach_client(evdev, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	kvfree(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) static ssize_t evdev_write(struct file *file, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			   size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct evdev_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	struct evdev *evdev = client->evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	struct input_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (count != 0 && count < input_event_size())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	retval = mutex_lock_interruptible(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	if (!evdev->exist || client->revoked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	while (retval + input_event_size() <= count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		if (input_event_from_user(buffer + retval, &event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		retval += input_event_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		input_inject_event(&evdev->handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 				   event.type, event.code, event.value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	mutex_unlock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) static int evdev_fetch_next_event(struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 				  struct input_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	int have_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	spin_lock_irq(&client->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	have_event = client->packet_head != client->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (have_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		*event = client->buffer[client->tail++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		client->tail &= client->bufsize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	spin_unlock_irq(&client->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	return have_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) static ssize_t evdev_read(struct file *file, char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			  size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	struct evdev_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	struct evdev *evdev = client->evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	struct input_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	size_t read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (count != 0 && count < input_event_size())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		if (!evdev->exist || client->revoked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		if (client->packet_head == client->tail &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		    (file->f_flags & O_NONBLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		 * count == 0 is special - no IO is done but we check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		 * for error conditions (see above).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		while (read + input_event_size() <= count &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		       evdev_fetch_next_event(client, &event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 			if (input_event_to_user(buffer + read, &event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			read += input_event_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		if (read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		if (!(file->f_flags & O_NONBLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			error = wait_event_interruptible(client->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 					client->packet_head != client->tail ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 					!evdev->exist || client->revoked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	return read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) /* No kernel lock - fine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) static __poll_t evdev_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	struct evdev_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	struct evdev *evdev = client->evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	__poll_t mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	poll_wait(file, &client->wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (evdev->exist && !client->revoked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		mask = EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		mask = EPOLLHUP | EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	if (client->packet_head != client->tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) static int bits_to_user(unsigned long *bits, unsigned int maxbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			unsigned int maxlen, void __user *p, int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	int len, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	if (compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		for (i = 0; i < len / sizeof(compat_long_t); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			if (copy_to_user((compat_long_t __user *) p + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 					 (compat_long_t *) bits +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 						i + 1 - ((i % 2) << 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 					 sizeof(compat_long_t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		len = BITS_TO_LONGS(maxbit) * sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		if (copy_to_user(p, bits, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) static int bits_from_user(unsigned long *bits, unsigned int maxbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 			  unsigned int maxlen, const void __user *p, int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	int len, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	if (compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		if (maxlen % sizeof(compat_long_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		for (i = 0; i < len / sizeof(compat_long_t); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			if (copy_from_user((compat_long_t *) bits +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 						i + 1 - ((i % 2) << 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 					   (compat_long_t __user *) p + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 					   sizeof(compat_long_t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		if (i % 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			*((compat_long_t *) bits + i - 1) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		if (maxlen % sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		len = BITS_TO_LONGS(maxbit) * sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		if (copy_from_user(bits, p, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) static int bits_to_user(unsigned long *bits, unsigned int maxbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			unsigned int maxlen, void __user *p, int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	int len = compat ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			BITS_TO_LONGS(maxbit) * sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	return copy_to_user(p, bits, len) ? -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) static int bits_from_user(unsigned long *bits, unsigned int maxbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			  unsigned int maxlen, const void __user *p, int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	size_t chunk_size = compat ? sizeof(compat_long_t) : sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (maxlen % chunk_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	len = compat ? BITS_TO_LONGS_COMPAT(maxbit) : BITS_TO_LONGS(maxbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	len *= chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	return copy_from_user(bits, p, len) ? -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) #endif /* __BIG_ENDIAN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) static int bits_to_user(unsigned long *bits, unsigned int maxbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			unsigned int maxlen, void __user *p, int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	int len = BITS_TO_LONGS(maxbit) * sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	return copy_to_user(p, bits, len) ? -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) static int bits_from_user(unsigned long *bits, unsigned int maxbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			  unsigned int maxlen, const void __user *p, int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	if (maxlen % sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	len = BITS_TO_LONGS(maxbit) * sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	return copy_from_user(bits, p, len) ? -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) #endif /* CONFIG_COMPAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	len = strlen(str) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	if (len > maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		len = maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	return copy_to_user(p, str, len) ? -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) static int handle_eviocgbit(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			    unsigned int type, unsigned int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 			    void __user *p, int compat_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	unsigned long *bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	case      0: bits = dev->evbit;  len = EV_MAX;  break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	case EV_REL: bits = dev->relbit; len = REL_MAX; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	case EV_FF:  bits = dev->ffbit;  len = FF_MAX;  break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	case EV_SW:  bits = dev->swbit;  len = SW_MAX;  break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	default: return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	return bits_to_user(bits, len, size, p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	struct input_keymap_entry ke = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		.len	= sizeof(unsigned int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		.flags	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	int __user *ip = (int __user *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	/* legacy case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	error = input_get_keycode(dev, &ke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (put_user(ke.keycode, ip + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct input_keymap_entry ke;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (copy_from_user(&ke, p, sizeof(ke)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	error = input_get_keycode(dev, &ke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (copy_to_user(p, &ke, sizeof(ke)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	struct input_keymap_entry ke = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		.len	= sizeof(unsigned int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		.flags	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	int __user *ip = (int __user *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (get_user(ke.keycode, ip + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	return input_set_keycode(dev, &ke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	struct input_keymap_entry ke;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (copy_from_user(&ke, p, sizeof(ke)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	if (ke.len > sizeof(ke.scancode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	return input_set_keycode(dev, &ke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874)  * If we transfer state to the user, we should flush all pending events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875)  * of the same type from the client's queue. Otherwise, they might end up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876)  * with duplicate events, which can screw up client's state tracking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877)  * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878)  * event so user-space will notice missing events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880)  * LOCKING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  * We need to take event_lock before buffer_lock to avoid dead-locks. But we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  * need the even_lock only to guarantee consistent state. We can safely release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883)  * it while flushing the queue. This allows input-core to handle filters while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884)  * we flush the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) static int evdev_handle_get_val(struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 				struct input_dev *dev, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 				unsigned long *bits, unsigned int maxbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 				unsigned int maxlen, void __user *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 				int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	unsigned long *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	mem = bitmap_alloc(maxbit, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	if (!mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	spin_lock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	spin_lock(&client->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	bitmap_copy(mem, bits, maxbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	spin_unlock(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	__evdev_flush_queue(client, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	spin_unlock_irq(&client->buffer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	ret = bits_to_user(mem, maxbit, maxlen, p, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		evdev_queue_syn_dropped(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	bitmap_free(mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) static int evdev_handle_mt_request(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 				   unsigned int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 				   int __user *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	const struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	unsigned int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	int max_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	if (get_user(code, &ip[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (!mt || !input_is_mt_value(code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	max_slots = (size - sizeof(__u32)) / sizeof(__s32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	for (i = 0; i < mt->num_slots && i < max_slots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		int value = input_mt_get_value(&mt->slots[i], code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		if (put_user(value, &ip[1 + i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	client->revoked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	evdev_ungrab(evdev, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	input_flush_device(&evdev->handle, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) /* must be called with evdev-mutex held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) static int evdev_set_mask(struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			  unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			  const void __user *codes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			  u32 codes_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			  int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	unsigned long flags, *mask, *oldmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	size_t cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	/* we allow unknown types and 'codes_size > size' for forward-compat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	cnt = evdev_get_mask_cnt(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	if (!cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	mask = bitmap_zalloc(cnt, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (!mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		bitmap_free(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	spin_lock_irqsave(&client->buffer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	oldmask = client->evmasks[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	client->evmasks[type] = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	spin_unlock_irqrestore(&client->buffer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	bitmap_free(oldmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) /* must be called with evdev-mutex held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) static int evdev_get_mask(struct evdev_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			  unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			  void __user *codes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			  u32 codes_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			  int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	unsigned long *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	size_t cnt, size, xfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	/* we allow unknown types and 'codes_size > size' for forward-compat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	cnt = evdev_get_mask_cnt(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	size = sizeof(unsigned long) * BITS_TO_LONGS(cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	xfer_size = min_t(size_t, codes_size, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (cnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		mask = client->evmasks[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		if (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			error = bits_to_user(mask, cnt - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 					     xfer_size, codes, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			/* fake mask with all bits set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			for (i = 0; i < xfer_size; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 				if (put_user(0xffU, (u8 __user *)codes + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 					return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (xfer_size < codes_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		if (clear_user(codes + xfer_size, codes_size - xfer_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) static long evdev_do_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			   void __user *p, int compat_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	struct evdev_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	struct evdev *evdev = client->evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	struct input_dev *dev = evdev->handle.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	struct input_absinfo abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	struct input_mask mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	struct ff_effect effect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	int __user *ip = (int __user *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	unsigned int i, t, u, v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	/* First we check for fixed-length commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	case EVIOCGVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		return put_user(EV_VERSION, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	case EVIOCGID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	case EVIOCGREP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		if (!test_bit(EV_REP, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		if (put_user(dev->rep[REP_DELAY], ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		if (put_user(dev->rep[REP_PERIOD], ip + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	case EVIOCSREP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		if (!test_bit(EV_REP, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		if (get_user(u, ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (get_user(v, ip + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	case EVIOCRMFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		return input_ff_erase(dev, (int)(unsigned long) p, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	case EVIOCGEFFECTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		i = test_bit(EV_FF, dev->evbit) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 				dev->ff->max_effects : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		if (put_user(i, ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	case EVIOCGRAB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			return evdev_grab(evdev, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			return evdev_ungrab(evdev, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	case EVIOCREVOKE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			return evdev_revoke(evdev, client, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	case EVIOCGMASK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		void __user *codes_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		if (copy_from_user(&mask, p, sizeof(mask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		codes_ptr = (void __user *)(unsigned long)mask.codes_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		return evdev_get_mask(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 				      mask.type, codes_ptr, mask.codes_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 				      compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	case EVIOCSMASK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		const void __user *codes_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		if (copy_from_user(&mask, p, sizeof(mask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		codes_ptr = (const void __user *)(unsigned long)mask.codes_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		return evdev_set_mask(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 				      mask.type, codes_ptr, mask.codes_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 				      compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	case EVIOCSCLOCKID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		if (copy_from_user(&i, p, sizeof(unsigned int)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		return evdev_set_clk_type(client, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	case EVIOCGKEYCODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		return evdev_handle_get_keycode(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	case EVIOCSKEYCODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		return evdev_handle_set_keycode(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	case EVIOCGKEYCODE_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		return evdev_handle_get_keycode_v2(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	case EVIOCSKEYCODE_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		return evdev_handle_set_keycode_v2(dev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	size = _IOC_SIZE(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	/* Now check variable-length commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) #define EVIOC_MASK_SIZE(nr)	((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	switch (EVIOC_MASK_SIZE(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	case EVIOCGPROP(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		return bits_to_user(dev->propbit, INPUT_PROP_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 				    size, p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	case EVIOCGMTSLOTS(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		return evdev_handle_mt_request(dev, size, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	case EVIOCGKEY(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 					    KEY_MAX, size, p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	case EVIOCGLED(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		return evdev_handle_get_val(client, dev, EV_LED, dev->led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 					    LED_MAX, size, p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	case EVIOCGSND(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 					    SND_MAX, size, p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	case EVIOCGSW(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 					    SW_MAX, size, p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	case EVIOCGNAME(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		return str_to_user(dev->name, size, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	case EVIOCGPHYS(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		return str_to_user(dev->phys, size, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	case EVIOCGUNIQ(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		return str_to_user(dev->uniq, size, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	case EVIOC_MASK_SIZE(EVIOCSFF):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		if (input_ff_effect_from_user(p, size, &effect))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		error = input_ff_upload(dev, &effect, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	/* Multi-number variable-length handlers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (_IOC_TYPE(cmd) != 'E')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	if (_IOC_DIR(cmd) == _IOC_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			return handle_eviocgbit(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 						_IOC_NR(cmd) & EV_MAX, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 						p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 			if (!dev->absinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 			t = _IOC_NR(cmd) & ABS_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			abs = dev->absinfo[t];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			if (copy_to_user(p, &abs, min_t(size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 					size, sizeof(struct input_absinfo))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (_IOC_DIR(cmd) == _IOC_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 			if (!dev->absinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			t = _IOC_NR(cmd) & ABS_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			if (copy_from_user(&abs, p, min_t(size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 					size, sizeof(struct input_absinfo))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			if (size < sizeof(struct input_absinfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 				abs.resolution = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 			/* We can't change number of reserved MT slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			if (t == ABS_MT_SLOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			 * Take event lock to ensure that we are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			 * changing device parameters in the middle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			 * of event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			spin_lock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			dev->absinfo[t] = abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 				void __user *p, int compat_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	struct evdev_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	struct evdev *evdev = client->evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	retval = mutex_lock_interruptible(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	if (!evdev->exist || client->revoked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	retval = evdev_do_ioctl(file, cmd, p, compat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	mutex_unlock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) static long evdev_ioctl_compat(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 				unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) static const struct file_operations evdev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	.read		= evdev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	.write		= evdev_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	.poll		= evdev_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	.open		= evdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	.release	= evdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	.unlocked_ioctl	= evdev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	.compat_ioctl	= evdev_ioctl_compat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	.fasync		= evdev_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)  * Mark device non-existent. This disables writes, ioctls and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)  * prevents new users from opening the device. Already posted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)  * blocking reads will stay, however new ones will fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) static void evdev_mark_dead(struct evdev *evdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	mutex_lock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	evdev->exist = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	mutex_unlock(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) static void evdev_cleanup(struct evdev *evdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	struct input_handle *handle = &evdev->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	evdev_mark_dead(evdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	evdev_hangup(evdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	/* evdev is marked dead so no one else accesses evdev->open */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (evdev->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		input_flush_device(handle, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		input_close_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)  * Create new evdev device. Note that input core serializes calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)  * to connect and disconnect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			 const struct input_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	struct evdev *evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	int dev_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	if (minor < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		error = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		pr_err("failed to reserve new minor: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	if (!evdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		goto err_free_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	INIT_LIST_HEAD(&evdev->client_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	spin_lock_init(&evdev->client_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	mutex_init(&evdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	evdev->exist = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	dev_no = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	/* Normalize device number if it falls into legacy range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		dev_no -= EVDEV_MINOR_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	dev_set_name(&evdev->dev, "event%d", dev_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	evdev->handle.dev = input_get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	evdev->handle.name = dev_name(&evdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	evdev->handle.handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	evdev->handle.private = evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	evdev->dev.class = &input_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	evdev->dev.parent = &dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	evdev->dev.release = evdev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	device_initialize(&evdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	error = input_register_handle(&evdev->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		goto err_free_evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	cdev_init(&evdev->cdev, &evdev_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	error = cdev_device_add(&evdev->cdev, &evdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		goto err_cleanup_evdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)  err_cleanup_evdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	evdev_cleanup(evdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	input_unregister_handle(&evdev->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)  err_free_evdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	put_device(&evdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)  err_free_minor:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	input_free_minor(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) static void evdev_disconnect(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	struct evdev *evdev = handle->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	cdev_device_del(&evdev->cdev, &evdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	evdev_cleanup(evdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	input_free_minor(MINOR(evdev->dev.devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	input_unregister_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	put_device(&evdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static const struct input_device_id evdev_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	{ .driver_info = 1 },	/* Matches all devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	{ },			/* Terminating zero entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) MODULE_DEVICE_TABLE(input, evdev_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) static struct input_handler evdev_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	.event		= evdev_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	.events		= evdev_events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	.connect	= evdev_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	.disconnect	= evdev_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	.legacy_minors	= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	.minor		= EVDEV_MINOR_BASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	.name		= "evdev",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	.id_table	= evdev_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) static int __init evdev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	return input_register_handler(&evdev_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) static void __exit evdev_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	input_unregister_handler(&evdev_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) module_init(evdev_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) module_exit(evdev_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) MODULE_DESCRIPTION("Input driver event char devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) MODULE_LICENSE("GPL");