^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Roccat driver for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Module roccat is a char device used to report special events of roccat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * hardware to userland. These events include requests for on-screen-display of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * profile or dpi settings or requests for execution of macro sequences that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * not stored in device. The information in these events depends on hid device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * implementation and contains data that is not available in a single hid event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * or else hidraw could have been used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * It is inspired by hidraw, but uses only one circular buffer for all readers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/hid-roccat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define ROCCAT_FIRST_MINOR 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define ROCCAT_MAX_DEVICES 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* should be a power of 2 for performance reason */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define ROCCAT_CBUF_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct roccat_report {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) uint8_t *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct roccat_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int report_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int exist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct hid_device *hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct list_head readers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* protects modifications of readers list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct mutex readers_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * circular_buffer has one writer and multiple readers with their own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * read pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int cbuf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct mutex cbuf_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct roccat_reader {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct roccat_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int cbuf_start;
^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) static int roccat_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct cdev roccat_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* protects modifications of devices array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static DEFINE_MUTEX(devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static ssize_t roccat_read(struct file *file, char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct roccat_reader *reader = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct roccat_device *device = reader->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct roccat_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ssize_t retval = 0, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) mutex_lock(&device->cbuf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* no data? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (reader->cbuf_start == device->cbuf_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) add_wait_queue(&device->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* wait for data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) while (reader->cbuf_start == device->cbuf_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) retval = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (!device->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) mutex_unlock(&device->cbuf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) mutex_lock(&device->cbuf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) remove_wait_queue(&device->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* here we either have data or a reason to return if retval is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) report = &device->cbuf[reader->cbuf_start];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * If report is larger than requested amount of data, rest of report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * is lost!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) len = device->report_size > count ? count : device->report_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (copy_to_user(buffer, report->value, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) retval += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) exit_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) mutex_unlock(&device->cbuf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static __poll_t roccat_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct roccat_reader *reader = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) poll_wait(file, &reader->device->wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (reader->cbuf_start != reader->device->cbuf_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!reader->device->exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return EPOLLERR | EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int roccat_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct roccat_reader *reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct roccat_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!reader)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) mutex_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) device = devices[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pr_emerg("roccat device with minor %d doesn't exist\n", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) error = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto exit_err_devices;
^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) mutex_lock(&device->readers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!device->open++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* power on device on adding first reader */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) error = hid_hw_power(device->hid, PM_HINT_FULLON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) --device->open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) goto exit_err_readers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) error = hid_hw_open(device->hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) hid_hw_power(device->hid, PM_HINT_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) --device->open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto exit_err_readers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) reader->device = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* new reader doesn't get old events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) reader->cbuf_start = device->cbuf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) list_add_tail(&reader->node, &device->readers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) file->private_data = reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) exit_err_readers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) mutex_unlock(&device->readers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) exit_err_devices:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) kfree(reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int roccat_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct roccat_reader *reader = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct roccat_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) mutex_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) device = devices[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) pr_emerg("roccat device with minor %d doesn't exist\n", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) mutex_lock(&device->readers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) list_del(&reader->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) mutex_unlock(&device->readers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kfree(reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!--device->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* removing last reader */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (device->exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) hid_hw_power(device->hid, PM_HINT_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) hid_hw_close(device->hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return 0;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * roccat_report_event() - output data to readers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * @minor: minor device number returned by roccat_connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * @data: pointer to data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * Return value is zero on success, a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * This is called from interrupt handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int roccat_report_event(int minor, u8 const *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct roccat_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct roccat_reader *reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct roccat_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) uint8_t *new_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) device = devices[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) new_value = kmemdup(data, device->report_size, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!new_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) report = &device->cbuf[device->cbuf_end];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* passing NULL is safe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) kfree(report->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) report->value = new_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) list_for_each_entry(reader, &device->readers, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * As we already inserted one element, the buffer can't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * empty. If start and end are equal, buffer is full and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * increase start, so that slow reader misses one event, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * gets the newer ones in the right order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (reader->cbuf_start == device->cbuf_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) wake_up_interruptible(&device->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) EXPORT_SYMBOL_GPL(roccat_report_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * roccat_connect() - create a char device for special event output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * @class: the class thats used to create the device. Meant to hold device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * specific sysfs attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * @hid: the hid device the char device should be connected to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * @report_size: size of reports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * success, a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int roccat_connect(struct class *klass, struct hid_device *hid, int report_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) unsigned int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct roccat_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) mutex_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (devices[minor])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) break;
^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) if (minor < ROCCAT_MAX_DEVICES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) devices[minor] = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) device->dev = device_create(klass, &hid->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MKDEV(roccat_major, minor), NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) "%s%s%d", "roccat", hid->driver->name, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (IS_ERR(device->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) devices[minor] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) temp = PTR_ERR(device->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) init_waitqueue_head(&device->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) INIT_LIST_HEAD(&device->readers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) mutex_init(&device->readers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) mutex_init(&device->cbuf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) device->minor = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) device->hid = hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) device->exist = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) device->cbuf_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) device->report_size = report_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) EXPORT_SYMBOL_GPL(roccat_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /* roccat_disconnect() - remove char device from hid device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * @minor: the minor device number returned by roccat_connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) void roccat_disconnect(int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct roccat_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) mutex_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) device = devices[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) device->exist = 0; /* TODO exist maybe not needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) device_destroy(device->dev->class, MKDEV(roccat_major, minor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) mutex_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) devices[minor] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (device->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) hid_hw_close(device->hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) wake_up_interruptible(&device->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) EXPORT_SYMBOL_GPL(roccat_disconnect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct roccat_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) long retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) mutex_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) device = devices[minor];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (!device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) case ROCCATIOCGREPSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (put_user(device->report_size, (int __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) retval = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) mutex_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static const struct file_operations roccat_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) .read = roccat_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) .poll = roccat_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) .open = roccat_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) .release = roccat_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .unlocked_ioctl = roccat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static int __init roccat_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) dev_t dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) ROCCAT_MAX_DEVICES, "roccat");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) pr_warn("can't get major number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) roccat_major = MAJOR(dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) cdev_init(&roccat_cdev, &roccat_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) pr_warn("cannot add cdev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) goto cleanup_alloc_chrdev_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) cleanup_alloc_chrdev_region:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static void __exit roccat_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) dev_t dev_id = MKDEV(roccat_major, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) cdev_del(&roccat_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) module_init(roccat_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) module_exit(roccat_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) MODULE_AUTHOR("Stefan Achatz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) MODULE_DESCRIPTION("USB Roccat char device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) MODULE_LICENSE("GPL v2");