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