1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Roccat driver for Linux
5 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
12 * Module roccat is a char device used to report special events of roccat
13 * hardware to userland. These events include requests for on-screen-display of
14 * profile or dpi settings or requests for execution of macro sequences that are
15 * not stored in device. The information in these events depends on hid device
16 * implementation and contains data that is not available in a single hid event
17 * or else hidraw could have been used.
18 * It is inspired by hidraw, but uses only one circular buffer for all readers.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/cdev.h>
24 #include <linux/poll.h>
25 #include <linux/sched/signal.h>
26 #include <linux/hid-roccat.h>
27 #include <linux/module.h>
29 #define ROCCAT_FIRST_MINOR 0
30 #define ROCCAT_MAX_DEVICES 8
32 /* should be a power of 2 for performance reason */
33 #define ROCCAT_CBUF_SIZE 16
35 struct roccat_report {
39 struct roccat_device {
44 wait_queue_head_t wait;
46 struct hid_device *hid;
47 struct list_head readers;
48 /* protects modifications of readers list */
49 struct mutex readers_lock;
52 * circular_buffer has one writer and multiple readers with their own
55 struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
57 struct mutex cbuf_lock;
60 struct roccat_reader {
61 struct list_head node;
62 struct roccat_device *device;
66 static int roccat_major;
67 static struct cdev roccat_cdev;
69 static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
70 /* protects modifications of devices array */
71 static DEFINE_MUTEX(devices_lock);
73 static ssize_t roccat_read(struct file *file, char __user *buffer,
74 size_t count, loff_t *ppos)
76 struct roccat_reader *reader = file->private_data;
77 struct roccat_device *device = reader->device;
78 struct roccat_report *report;
79 ssize_t retval = 0, len;
80 DECLARE_WAITQUEUE(wait, current);
82 mutex_lock(&device->cbuf_lock);
85 if (reader->cbuf_start == device->cbuf_end) {
86 add_wait_queue(&device->wait, &wait);
87 set_current_state(TASK_INTERRUPTIBLE);
90 while (reader->cbuf_start == device->cbuf_end) {
91 if (file->f_flags & O_NONBLOCK) {
95 if (signal_pending(current)) {
96 retval = -ERESTARTSYS;
104 mutex_unlock(&device->cbuf_lock);
106 mutex_lock(&device->cbuf_lock);
107 set_current_state(TASK_INTERRUPTIBLE);
110 set_current_state(TASK_RUNNING);
111 remove_wait_queue(&device->wait, &wait);
114 /* here we either have data or a reason to return if retval is set */
118 report = &device->cbuf[reader->cbuf_start];
120 * If report is larger than requested amount of data, rest of report
123 len = device->report_size > count ? count : device->report_size;
125 if (copy_to_user(buffer, report->value, len)) {
130 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
133 mutex_unlock(&device->cbuf_lock);
137 static __poll_t roccat_poll(struct file *file, poll_table *wait)
139 struct roccat_reader *reader = file->private_data;
140 poll_wait(file, &reader->device->wait, wait);
141 if (reader->cbuf_start != reader->device->cbuf_end)
142 return EPOLLIN | EPOLLRDNORM;
143 if (!reader->device->exist)
144 return EPOLLERR | EPOLLHUP;
148 static int roccat_open(struct inode *inode, struct file *file)
150 unsigned int minor = iminor(inode);
151 struct roccat_reader *reader;
152 struct roccat_device *device;
155 reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
159 mutex_lock(&devices_lock);
161 device = devices[minor];
164 pr_emerg("roccat device with minor %d doesn't exist\n", minor);
166 goto exit_err_devices;
169 mutex_lock(&device->readers_lock);
171 if (!device->open++) {
172 /* power on device on adding first reader */
173 error = hid_hw_power(device->hid, PM_HINT_FULLON);
176 goto exit_err_readers;
179 error = hid_hw_open(device->hid);
181 hid_hw_power(device->hid, PM_HINT_NORMAL);
183 goto exit_err_readers;
187 reader->device = device;
188 /* new reader doesn't get old events */
189 reader->cbuf_start = device->cbuf_end;
191 list_add_tail(&reader->node, &device->readers);
192 file->private_data = reader;
195 mutex_unlock(&device->readers_lock);
197 mutex_unlock(&devices_lock);
203 static int roccat_release(struct inode *inode, struct file *file)
205 unsigned int minor = iminor(inode);
206 struct roccat_reader *reader = file->private_data;
207 struct roccat_device *device;
209 mutex_lock(&devices_lock);
211 device = devices[minor];
213 mutex_unlock(&devices_lock);
214 pr_emerg("roccat device with minor %d doesn't exist\n", minor);
218 mutex_lock(&device->readers_lock);
219 list_del(&reader->node);
220 mutex_unlock(&device->readers_lock);
223 if (!--device->open) {
224 /* removing last reader */
226 hid_hw_power(device->hid, PM_HINT_NORMAL);
227 hid_hw_close(device->hid);
233 mutex_unlock(&devices_lock);
239 * roccat_report_event() - output data to readers
240 * @minor: minor device number returned by roccat_connect()
241 * @data: pointer to data
243 * Return value is zero on success, a negative error code on failure.
245 * This is called from interrupt handler.
247 int roccat_report_event(int minor, u8 const *data)
249 struct roccat_device *device;
250 struct roccat_reader *reader;
251 struct roccat_report *report;
254 device = devices[minor];
256 new_value = kmemdup(data, device->report_size, GFP_ATOMIC);
260 mutex_lock(&device->cbuf_lock);
262 report = &device->cbuf[device->cbuf_end];
264 /* passing NULL is safe */
265 kfree(report->value);
267 report->value = new_value;
268 device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
270 list_for_each_entry(reader, &device->readers, node) {
272 * As we already inserted one element, the buffer can't be
273 * empty. If start and end are equal, buffer is full and we
274 * increase start, so that slow reader misses one event, but
275 * gets the newer ones in the right order.
277 if (reader->cbuf_start == device->cbuf_end)
278 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
281 mutex_unlock(&device->cbuf_lock);
283 wake_up_interruptible(&device->wait);
286 EXPORT_SYMBOL_GPL(roccat_report_event);
289 * roccat_connect() - create a char device for special event output
290 * @class: the class thats used to create the device. Meant to hold device
291 * specific sysfs attributes.
292 * @hid: the hid device the char device should be connected to.
293 * @report_size: size of reports
295 * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
296 * success, a negative error code on failure.
298 int roccat_connect(const struct class *klass, struct hid_device *hid, int report_size)
301 struct roccat_device *device;
304 device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
308 mutex_lock(&devices_lock);
310 for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
316 if (minor < ROCCAT_MAX_DEVICES) {
317 devices[minor] = device;
319 mutex_unlock(&devices_lock);
324 device->dev = device_create(klass, &hid->dev,
325 MKDEV(roccat_major, minor), NULL,
326 "%s%s%d", "roccat", hid->driver->name, minor);
328 if (IS_ERR(device->dev)) {
329 devices[minor] = NULL;
330 mutex_unlock(&devices_lock);
331 temp = PTR_ERR(device->dev);
336 mutex_unlock(&devices_lock);
338 init_waitqueue_head(&device->wait);
339 INIT_LIST_HEAD(&device->readers);
340 mutex_init(&device->readers_lock);
341 mutex_init(&device->cbuf_lock);
342 device->minor = minor;
345 device->cbuf_end = 0;
346 device->report_size = report_size;
350 EXPORT_SYMBOL_GPL(roccat_connect);
352 /* roccat_disconnect() - remove char device from hid device
353 * @minor: the minor device number returned by roccat_connect()
355 void roccat_disconnect(int minor)
357 struct roccat_device *device;
359 mutex_lock(&devices_lock);
360 device = devices[minor];
361 mutex_unlock(&devices_lock);
363 device->exist = 0; /* TODO exist maybe not needed */
365 device_destroy(device->dev->class, MKDEV(roccat_major, minor));
367 mutex_lock(&devices_lock);
368 devices[minor] = NULL;
369 mutex_unlock(&devices_lock);
372 hid_hw_close(device->hid);
373 wake_up_interruptible(&device->wait);
378 EXPORT_SYMBOL_GPL(roccat_disconnect);
380 static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
382 struct inode *inode = file_inode(file);
383 struct roccat_device *device;
384 unsigned int minor = iminor(inode);
387 mutex_lock(&devices_lock);
389 device = devices[minor];
396 case ROCCATIOCGREPSIZE:
397 if (put_user(device->report_size, (int __user *)arg))
404 mutex_unlock(&devices_lock);
408 static const struct file_operations roccat_ops = {
409 .owner = THIS_MODULE,
413 .release = roccat_release,
414 .llseek = noop_llseek,
415 .unlocked_ioctl = roccat_ioctl,
418 static int __init roccat_init(void)
423 retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
424 ROCCAT_MAX_DEVICES, "roccat");
426 pr_warn("can't get major number\n");
430 roccat_major = MAJOR(dev_id);
432 cdev_init(&roccat_cdev, &roccat_ops);
433 retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
436 pr_warn("cannot add cdev\n");
437 goto cleanup_alloc_chrdev_region;
442 cleanup_alloc_chrdev_region:
443 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
448 static void __exit roccat_exit(void)
450 dev_t dev_id = MKDEV(roccat_major, 0);
452 cdev_del(&roccat_cdev);
453 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
456 module_init(roccat_init);
457 module_exit(roccat_exit);
459 MODULE_AUTHOR("Stefan Achatz");
460 MODULE_DESCRIPTION("USB Roccat char device");
461 MODULE_LICENSE("GPL v2");