^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) * HID raw devices, giving access to raw HID events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * In comparison to hiddev, this device does not process the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * hid events at all (no parsing, no lookups). This lets applications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * to work on raw hid events as they want to, and avoids a need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * use a transport-specific userspace libhid/libusb libraries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (c) 2007-2014 Jiri Kosina
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/hidraw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int hidraw_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct cdev hidraw_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct class *hidraw_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static DEFINE_MUTEX(minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct hidraw_list *list = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int ret = 0, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) mutex_lock(&list->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) while (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (list->head == list->tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) add_wait_queue(&list->hidraw->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) while (list->head == list->tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!list->hidraw->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* allow O_NONBLOCK to work well from other threads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) mutex_unlock(&list->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) mutex_lock(&list->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) remove_wait_queue(&list->hidraw->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) len = list->buffer[list->tail].len > count ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) count : list->buffer[list->tail].len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (list->buffer[list->tail].value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) kfree(list->buffer[list->tail].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) list->buffer[list->tail].value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mutex_unlock(&list->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return ret;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * The first byte of the report buffer is expected to be a report number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct hid_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) __u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) lockdep_assert_held(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) dev = hidraw_table[minor]->hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (count > HID_MAX_BUFFER_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) hid_warn(dev, "pid %d passed too large report\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (count < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) hid_warn(dev, "pid %d passed too short report\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) buf = memdup_user(buffer, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (IS_ERR(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ret = PTR_ERR(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto out;
^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) if ((report_type == HID_OUTPUT_REPORT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret = hid_hw_output_report(dev, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * compatibility with old implementation of USB-HID and I2C-HID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * if the device does not support receiving output reports,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * on an interrupt endpoint, fallback to SET_REPORT HID command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (ret != -ENOSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) mutex_lock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * This function performs a Get_Report transfer over the control endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * per section 7.2.1 of the HID specification, version 1.1. The first byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * of buffer is the report number to request, or 0x0 if the defice does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * or HID_INPUT_REPORT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) unsigned int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct hid_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) __u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int ret = 0, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned char report_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) lockdep_assert_held(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) dev = hidraw_table[minor]->hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!dev->ll_driver->raw_request) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (count > HID_MAX_BUFFER_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) hid_warn(dev, "pid %d passed too large report\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (count < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) hid_warn(dev, "pid %d passed too short report\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) buf = kmalloc(count, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * Read the first byte from the user. This is the report number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * which is passed to hid_hw_raw_request().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (copy_from_user(&report_number, buffer, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) HID_REQ_GET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) len = (ret < count) ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (copy_to_user(buffer, buf, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static __poll_t hidraw_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct hidraw_list *list = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) __poll_t mask = EPOLLOUT | EPOLLWRNORM; /* hidraw is always writable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) poll_wait(file, &list->hidraw->wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (list->head != list->tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!list->hidraw->exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) mask |= EPOLLERR | EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static int hidraw_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct hidraw *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct hidraw_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto out;
^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) mutex_lock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) goto out_unlock;
^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) dev = hidraw_table[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!dev->open++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) err = hid_hw_power(dev->hid, PM_HINT_FULLON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) dev->open--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) goto out_unlock;
^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) err = hid_hw_open(dev->hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) hid_hw_power(dev->hid, PM_HINT_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) dev->open--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) list->hidraw = hidraw_table[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) mutex_init(&list->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) list_add_tail(&list->node, &hidraw_table[minor]->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) file->private_data = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) kfree(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return err;
^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) static int hidraw_fasync(int fd, struct file *file, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct hidraw_list *list = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return fasync_helper(fd, file, on, &list->fasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void drop_ref(struct hidraw *hidraw, int exists_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (exists_bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) hidraw->exist = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (hidraw->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) hid_hw_close(hidraw->hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) wake_up_interruptible(&hidraw->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) device_destroy(hidraw_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) MKDEV(hidraw_major, hidraw->minor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) --hidraw->open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (!hidraw->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!hidraw->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) hidraw_table[hidraw->minor] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) kfree(hidraw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* close device for last reader */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) hid_hw_close(hidraw->hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static int hidraw_release(struct inode * inode, struct file * file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct hidraw_list *list = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) mutex_lock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) list_del(&list->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) kfree(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) drop_ref(hidraw_table[minor], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static long hidraw_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct hidraw *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) void __user *user_arg = (void __user*) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) mutex_lock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) dev = hidraw_table[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (!dev || !dev->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) case HIDIOCGRDESCSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (put_user(dev->hid->rsize, (int __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) case HIDIOCGRDESC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) __u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (get_user(len, (int __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) else if (copy_to_user(user_arg + offsetof(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct hidraw_report_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) value[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dev->hid->rdesc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) min(dev->hid->rsize, len)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) case HIDIOCGRAWINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct hidraw_devinfo dinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) dinfo.bustype = dev->hid->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) dinfo.vendor = dev->hid->vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) dinfo.product = dev->hid->product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct hid_device *hid = dev->hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (_IOC_TYPE(cmd) != 'H') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) break;
^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) if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int len = _IOC_SIZE(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) int len = _IOC_SIZE(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* Begin Read-only ioctls. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (_IOC_DIR(cmd) != _IOC_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) int len = strlen(hid->name) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (len > _IOC_SIZE(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) len = _IOC_SIZE(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) ret = copy_to_user(user_arg, hid->name, len) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int len = strlen(hid->phys) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (len > _IOC_SIZE(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) len = _IOC_SIZE(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) ret = copy_to_user(user_arg, hid->phys, len) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWUNIQ(0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) int len = strlen(hid->uniq) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (len > _IOC_SIZE(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) len = _IOC_SIZE(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) ret = copy_to_user(user_arg, hid->uniq, len) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) -EFAULT : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) ret = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static const struct file_operations hidraw_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) .read = hidraw_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) .write = hidraw_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) .poll = hidraw_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) .open = hidraw_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) .release = hidraw_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) .unlocked_ioctl = hidraw_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) .fasync = hidraw_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct hidraw *dev = hid->hidraw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct hidraw_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) spin_lock_irqsave(&dev->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) list_for_each_entry(list, &dev->list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (new_head == list->tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) list->buffer[list->head].len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) list->head = new_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) kill_fasync(&list->fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) spin_unlock_irqrestore(&dev->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) wake_up_interruptible(&dev->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) EXPORT_SYMBOL_GPL(hidraw_report_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) int hidraw_connect(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int minor, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct hidraw *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /* we accept any HID device, all applications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) mutex_lock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (hidraw_table[minor])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) hidraw_table[minor] = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) NULL, "%s%d", "hidraw", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (IS_ERR(dev->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) hidraw_table[minor] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) result = PTR_ERR(dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) init_waitqueue_head(&dev->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) spin_lock_init(&dev->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) INIT_LIST_HEAD(&dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) dev->hid = hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) dev->minor = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) dev->exist = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) hid->hidraw = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) EXPORT_SYMBOL_GPL(hidraw_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) void hidraw_disconnect(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct hidraw *hidraw = hid->hidraw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) mutex_lock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) drop_ref(hidraw, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) mutex_unlock(&minors_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) EXPORT_SYMBOL_GPL(hidraw_disconnect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int __init hidraw_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) dev_t dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) HIDRAW_MAX_DEVICES, "hidraw");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) pr_warn("can't get major number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) hidraw_major = MAJOR(dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) hidraw_class = class_create(THIS_MODULE, "hidraw");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (IS_ERR(hidraw_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) result = PTR_ERR(hidraw_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) goto error_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) cdev_init(&hidraw_cdev, &hidraw_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) goto error_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) pr_info("raw HID events driver (C) Jiri Kosina\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) error_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) class_destroy(hidraw_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) error_cdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) void hidraw_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) dev_t dev_id = MKDEV(hidraw_major, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) cdev_del(&hidraw_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) class_destroy(hidraw_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }