2 * Roccat driver for Linux
4 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 * Module roccat is a char device used to report special events of roccat
16 * hardware to userland. These events include requests for on-screen-display of
17 * profile or dpi settings or requests for execution of macro sequences that are
18 * not stored in device. The information in these events depends on hid device
19 * implementation and contains data that is not available in a single hid event
20 * or else hidraw could have been used.
21 * It is inspired by hidraw, but uses only one circular buffer for all readers.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/cdev.h>
27 #include <linux/poll.h>
28 #include <linux/sched/signal.h>
29 #include <linux/hid-roccat.h>
30 #include <linux/module.h>
32 #define ROCCAT_FIRST_MINOR 0
33 #define ROCCAT_MAX_DEVICES 8
35 /* should be a power of 2 for performance reason */
36 #define ROCCAT_CBUF_SIZE 16
38 struct roccat_report {
42 struct roccat_device {
47 wait_queue_head_t wait;
49 struct hid_device *hid;
50 struct list_head readers;
51 /* protects modifications of readers list */
52 struct mutex readers_lock;
55 * circular_buffer has one writer and multiple readers with their own
58 struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
60 struct mutex cbuf_lock;
63 struct roccat_reader {
64 struct list_head node;
65 struct roccat_device *device;
69 static int roccat_major;
70 static struct cdev roccat_cdev;
72 static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
73 /* protects modifications of devices array */
74 static DEFINE_MUTEX(devices_lock);
76 static ssize_t roccat_read(struct file *file, char __user *buffer,
77 size_t count, loff_t *ppos)
79 struct roccat_reader *reader = file->private_data;
80 struct roccat_device *device = reader->device;
81 struct roccat_report *report;
82 ssize_t retval = 0, len;
83 DECLARE_WAITQUEUE(wait, current);
85 mutex_lock(&device->cbuf_lock);
88 if (reader->cbuf_start == device->cbuf_end) {
89 add_wait_queue(&device->wait, &wait);
90 set_current_state(TASK_INTERRUPTIBLE);
93 while (reader->cbuf_start == device->cbuf_end) {
94 if (file->f_flags & O_NONBLOCK) {
98 if (signal_pending(current)) {
99 retval = -ERESTARTSYS;
102 if (!device->exist) {
107 mutex_unlock(&device->cbuf_lock);
109 mutex_lock(&device->cbuf_lock);
110 set_current_state(TASK_INTERRUPTIBLE);
113 set_current_state(TASK_RUNNING);
114 remove_wait_queue(&device->wait, &wait);
117 /* here we either have data or a reason to return if retval is set */
121 report = &device->cbuf[reader->cbuf_start];
123 * If report is larger than requested amount of data, rest of report
126 len = device->report_size > count ? count : device->report_size;
128 if (copy_to_user(buffer, report->value, len)) {
133 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
136 mutex_unlock(&device->cbuf_lock);
140 static unsigned int roccat_poll(struct file *file, poll_table *wait)
142 struct roccat_reader *reader = file->private_data;
143 poll_wait(file, &reader->device->wait, wait);
144 if (reader->cbuf_start != reader->device->cbuf_end)
145 return POLLIN | POLLRDNORM;
146 if (!reader->device->exist)
147 return POLLERR | POLLHUP;
151 static int roccat_open(struct inode *inode, struct file *file)
153 unsigned int minor = iminor(inode);
154 struct roccat_reader *reader;
155 struct roccat_device *device;
158 reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
162 mutex_lock(&devices_lock);
164 device = devices[minor];
167 pr_emerg("roccat device with minor %d doesn't exist\n", minor);
169 goto exit_err_devices;
172 mutex_lock(&device->readers_lock);
174 if (!device->open++) {
175 /* power on device on adding first reader */
176 error = hid_hw_power(device->hid, PM_HINT_FULLON);
179 goto exit_err_readers;
182 error = hid_hw_open(device->hid);
184 hid_hw_power(device->hid, PM_HINT_NORMAL);
186 goto exit_err_readers;
190 reader->device = device;
191 /* new reader doesn't get old events */
192 reader->cbuf_start = device->cbuf_end;
194 list_add_tail(&reader->node, &device->readers);
195 file->private_data = reader;
198 mutex_unlock(&device->readers_lock);
200 mutex_unlock(&devices_lock);
206 static int roccat_release(struct inode *inode, struct file *file)
208 unsigned int minor = iminor(inode);
209 struct roccat_reader *reader = file->private_data;
210 struct roccat_device *device;
212 mutex_lock(&devices_lock);
214 device = devices[minor];
216 mutex_unlock(&devices_lock);
217 pr_emerg("roccat device with minor %d doesn't exist\n", minor);
221 mutex_lock(&device->readers_lock);
222 list_del(&reader->node);
223 mutex_unlock(&device->readers_lock);
226 if (!--device->open) {
227 /* removing last reader */
229 hid_hw_power(device->hid, PM_HINT_NORMAL);
230 hid_hw_close(device->hid);
236 mutex_unlock(&devices_lock);
242 * roccat_report_event() - output data to readers
243 * @minor: minor device number returned by roccat_connect()
244 * @data: pointer to data
246 * Return value is zero on success, a negative error code on failure.
248 * This is called from interrupt handler.
250 int roccat_report_event(int minor, u8 const *data)
252 struct roccat_device *device;
253 struct roccat_reader *reader;
254 struct roccat_report *report;
257 device = devices[minor];
259 new_value = kmemdup(data, device->report_size, GFP_ATOMIC);
263 mutex_lock(&device->cbuf_lock);
265 report = &device->cbuf[device->cbuf_end];
267 /* passing NULL is safe */
268 kfree(report->value);
270 report->value = new_value;
271 device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
273 list_for_each_entry(reader, &device->readers, node) {
275 * As we already inserted one element, the buffer can't be
276 * empty. If start and end are equal, buffer is full and we
277 * increase start, so that slow reader misses one event, but
278 * gets the newer ones in the right order.
280 if (reader->cbuf_start == device->cbuf_end)
281 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
284 mutex_unlock(&device->cbuf_lock);
286 wake_up_interruptible(&device->wait);
289 EXPORT_SYMBOL_GPL(roccat_report_event);
292 * roccat_connect() - create a char device for special event output
293 * @class: the class thats used to create the device. Meant to hold device
294 * specific sysfs attributes.
295 * @hid: the hid device the char device should be connected to.
296 * @report_size: size of reports
298 * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
299 * success, a negative error code on failure.
301 int roccat_connect(struct class *klass, struct hid_device *hid, int report_size)
304 struct roccat_device *device;
307 device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
311 mutex_lock(&devices_lock);
313 for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
319 if (minor < ROCCAT_MAX_DEVICES) {
320 devices[minor] = device;
322 mutex_unlock(&devices_lock);
327 device->dev = device_create(klass, &hid->dev,
328 MKDEV(roccat_major, minor), NULL,
329 "%s%s%d", "roccat", hid->driver->name, minor);
331 if (IS_ERR(device->dev)) {
332 devices[minor] = NULL;
333 mutex_unlock(&devices_lock);
334 temp = PTR_ERR(device->dev);
339 mutex_unlock(&devices_lock);
341 init_waitqueue_head(&device->wait);
342 INIT_LIST_HEAD(&device->readers);
343 mutex_init(&device->readers_lock);
344 mutex_init(&device->cbuf_lock);
345 device->minor = minor;
348 device->cbuf_end = 0;
349 device->report_size = report_size;
353 EXPORT_SYMBOL_GPL(roccat_connect);
355 /* roccat_disconnect() - remove char device from hid device
356 * @minor: the minor device number returned by roccat_connect()
358 void roccat_disconnect(int minor)
360 struct roccat_device *device;
362 mutex_lock(&devices_lock);
363 device = devices[minor];
364 mutex_unlock(&devices_lock);
366 device->exist = 0; /* TODO exist maybe not needed */
368 device_destroy(device->dev->class, MKDEV(roccat_major, minor));
370 mutex_lock(&devices_lock);
371 devices[minor] = NULL;
372 mutex_unlock(&devices_lock);
375 hid_hw_close(device->hid);
376 wake_up_interruptible(&device->wait);
381 EXPORT_SYMBOL_GPL(roccat_disconnect);
383 static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
385 struct inode *inode = file_inode(file);
386 struct roccat_device *device;
387 unsigned int minor = iminor(inode);
390 mutex_lock(&devices_lock);
392 device = devices[minor];
399 case ROCCATIOCGREPSIZE:
400 if (put_user(device->report_size, (int __user *)arg))
407 mutex_unlock(&devices_lock);
411 static const struct file_operations roccat_ops = {
412 .owner = THIS_MODULE,
416 .release = roccat_release,
417 .llseek = noop_llseek,
418 .unlocked_ioctl = roccat_ioctl,
421 static int __init roccat_init(void)
426 retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
427 ROCCAT_MAX_DEVICES, "roccat");
429 pr_warn("can't get major number\n");
433 roccat_major = MAJOR(dev_id);
435 cdev_init(&roccat_cdev, &roccat_ops);
436 retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
439 pr_warn("cannot add cdev\n");
440 goto cleanup_alloc_chrdev_region;
445 cleanup_alloc_chrdev_region:
446 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
451 static void __exit roccat_exit(void)
453 dev_t dev_id = MKDEV(roccat_major, 0);
455 cdev_del(&roccat_cdev);
456 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
459 module_init(roccat_init);
460 module_exit(roccat_exit);
462 MODULE_AUTHOR("Stefan Achatz");
463 MODULE_DESCRIPTION("USB Roccat char device");
464 MODULE_LICENSE("GPL v2");