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 #include <linux/cdev.h>
25 #include <linux/poll.h>
26 #include <linux/sched.h>
28 #include "hid-roccat.h"
30 #define ROCCAT_FIRST_MINOR 0
31 #define ROCCAT_MAX_DEVICES 8
33 /* should be a power of 2 for performance reason */
34 #define ROCCAT_CBUF_SIZE 16
36 struct roccat_report {
41 struct roccat_device {
45 wait_queue_head_t wait;
47 struct hid_device *hid;
48 struct list_head readers;
49 /* protects modifications of readers list */
50 struct mutex readers_lock;
53 * circular_buffer has one writer and multiple readers with their own
56 struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
58 struct mutex cbuf_lock;
61 struct roccat_reader {
62 struct list_head node;
63 struct roccat_device *device;
67 static int roccat_major;
68 static struct class *roccat_class;
69 static struct cdev roccat_cdev;
71 static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
72 /* protects modifications of devices array */
73 static DEFINE_MUTEX(devices_lock);
75 static ssize_t roccat_read(struct file *file, char __user *buffer,
76 size_t count, loff_t *ppos)
78 struct roccat_reader *reader = file->private_data;
79 struct roccat_device *device = reader->device;
80 struct roccat_report *report;
81 ssize_t retval = 0, len;
82 DECLARE_WAITQUEUE(wait, current);
84 mutex_lock(&device->cbuf_lock);
87 if (reader->cbuf_start == device->cbuf_end) {
88 add_wait_queue(&device->wait, &wait);
89 set_current_state(TASK_INTERRUPTIBLE);
92 while (reader->cbuf_start == device->cbuf_end) {
93 if (file->f_flags & O_NONBLOCK) {
97 if (signal_pending(current)) {
98 retval = -ERESTARTSYS;
101 if (!device->exist) {
106 mutex_unlock(&device->cbuf_lock);
108 mutex_lock(&device->cbuf_lock);
109 set_current_state(TASK_INTERRUPTIBLE);
112 set_current_state(TASK_RUNNING);
113 remove_wait_queue(&device->wait, &wait);
116 /* here we either have data or a reason to return if retval is set */
120 report = &device->cbuf[reader->cbuf_start];
122 * If report is larger than requested amount of data, rest of report
125 len = report->len > count ? count : report->len;
127 if (copy_to_user(buffer, report->value, len)) {
132 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
135 mutex_unlock(&device->cbuf_lock);
139 static unsigned int roccat_poll(struct file *file, poll_table *wait)
141 struct roccat_reader *reader = file->private_data;
142 poll_wait(file, &reader->device->wait, wait);
143 if (reader->cbuf_start != reader->device->cbuf_end)
144 return POLLIN | POLLRDNORM;
145 if (!reader->device->exist)
146 return POLLERR | POLLHUP;
150 static int roccat_open(struct inode *inode, struct file *file)
152 unsigned int minor = iminor(inode);
153 struct roccat_reader *reader;
154 struct roccat_device *device;
157 reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
161 mutex_lock(&devices_lock);
163 device = devices[minor];
165 mutex_lock(&device->readers_lock);
168 printk(KERN_EMERG "roccat device with minor %d doesn't exist\n",
174 if (!device->open++) {
175 /* power on device on adding first reader */
176 if (device->hid->ll_driver->power) {
177 error = device->hid->ll_driver->power(device->hid,
184 error = device->hid->ll_driver->open(device->hid);
186 if (device->hid->ll_driver->power)
187 device->hid->ll_driver->power(device->hid,
194 reader->device = device;
195 /* new reader doesn't get old events */
196 reader->cbuf_start = device->cbuf_end;
198 list_add_tail(&reader->node, &device->readers);
199 file->private_data = reader;
202 mutex_unlock(&device->readers_lock);
203 mutex_unlock(&devices_lock);
207 static int roccat_release(struct inode *inode, struct file *file)
209 unsigned int minor = iminor(inode);
210 struct roccat_reader *reader = file->private_data;
211 struct roccat_device *device;
213 mutex_lock(&devices_lock);
215 device = devices[minor];
217 mutex_unlock(&devices_lock);
218 printk(KERN_EMERG "roccat device with minor %d doesn't exist\n",
223 mutex_lock(&device->readers_lock);
224 list_del(&reader->node);
225 mutex_unlock(&device->readers_lock);
228 if (!--device->open) {
229 /* removing last reader */
231 if (device->hid->ll_driver->power)
232 device->hid->ll_driver->power(device->hid,
234 device->hid->ll_driver->close(device->hid);
240 mutex_unlock(&devices_lock);
246 * roccat_report_event() - output data to readers
247 * @minor: minor device number returned by roccat_connect()
248 * @data: pointer to data
251 * Return value is zero on success, a negative error code on failure.
253 * This is called from interrupt handler.
255 int roccat_report_event(int minor, u8 const *data, int len)
257 struct roccat_device *device;
258 struct roccat_reader *reader;
259 struct roccat_report *report;
262 new_value = kmemdup(data, len, GFP_ATOMIC);
266 device = devices[minor];
268 report = &device->cbuf[device->cbuf_end];
270 /* passing NULL is safe */
271 kfree(report->value);
273 report->value = new_value;
275 device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
277 list_for_each_entry(reader, &device->readers, node) {
279 * As we already inserted one element, the buffer can't be
280 * empty. If start and end are equal, buffer is full and we
281 * increase start, so that slow reader misses one event, but
282 * gets the newer ones in the right order.
284 if (reader->cbuf_start == device->cbuf_end)
285 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
288 wake_up_interruptible(&device->wait);
291 EXPORT_SYMBOL_GPL(roccat_report_event);
294 * roccat_connect() - create a char device for special event output
295 * @hid: the hid device the char device should be connected to.
297 * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
298 * success, a negative error code on failure.
300 int roccat_connect(struct hid_device *hid)
303 struct roccat_device *device;
306 device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
310 mutex_lock(&devices_lock);
312 for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
318 if (minor < ROCCAT_MAX_DEVICES) {
319 devices[minor] = device;
321 mutex_unlock(&devices_lock);
326 device->dev = device_create(roccat_class, &hid->dev,
327 MKDEV(roccat_major, minor), NULL,
328 "%s%s%d", "roccat", hid->driver->name, minor);
330 if (IS_ERR(device->dev)) {
331 devices[minor] = NULL;
332 mutex_unlock(&devices_lock);
333 temp = PTR_ERR(device->dev);
338 mutex_unlock(&devices_lock);
340 init_waitqueue_head(&device->wait);
341 INIT_LIST_HEAD(&device->readers);
342 mutex_init(&device->readers_lock);
343 mutex_init(&device->cbuf_lock);
344 device->minor = minor;
347 device->cbuf_end = 0;
351 EXPORT_SYMBOL_GPL(roccat_connect);
353 /* roccat_disconnect() - remove char device from hid device
354 * @minor: the minor device number returned by roccat_connect()
356 void roccat_disconnect(int minor)
358 struct roccat_device *device;
360 mutex_lock(&devices_lock);
361 device = devices[minor];
362 devices[minor] = NULL;
363 mutex_unlock(&devices_lock);
365 device->exist = 0; /* TODO exist maybe not needed */
367 device_destroy(roccat_class, MKDEV(roccat_major, minor));
370 device->hid->ll_driver->close(device->hid);
371 wake_up_interruptible(&device->wait);
376 EXPORT_SYMBOL_GPL(roccat_disconnect);
378 static const struct file_operations roccat_ops = {
379 .owner = THIS_MODULE,
383 .release = roccat_release,
386 static int __init roccat_init(void)
391 retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
392 ROCCAT_MAX_DEVICES, "roccat");
394 roccat_major = MAJOR(dev_id);
397 printk(KERN_WARNING "roccat: can't get major number\n");
401 roccat_class = class_create(THIS_MODULE, "roccat");
402 if (IS_ERR(roccat_class)) {
403 retval = PTR_ERR(roccat_class);
404 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
408 cdev_init(&roccat_cdev, &roccat_ops);
409 cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
414 static void __exit roccat_exit(void)
416 dev_t dev_id = MKDEV(roccat_major, 0);
418 cdev_del(&roccat_cdev);
419 class_destroy(roccat_class);
420 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
423 module_init(roccat_init);
424 module_exit(roccat_exit);
426 MODULE_AUTHOR("Stefan Achatz");
427 MODULE_DESCRIPTION("USB Roccat char device");
428 MODULE_LICENSE("GPL v2");