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