Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Roccat driver for Linux
0004  *
0005  * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
0006  */
0007 
0008 /*
0009  */
0010 
0011 /*
0012  * Module roccat is a char device used to report special events of roccat
0013  * hardware to userland. These events include requests for on-screen-display of
0014  * profile or dpi settings or requests for execution of macro sequences that are
0015  * not stored in device. The information in these events depends on hid device
0016  * implementation and contains data that is not available in a single hid event
0017  * or else hidraw could have been used.
0018  * It is inspired by hidraw, but uses only one circular buffer for all readers.
0019  */
0020 
0021 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0022 
0023 #include <linux/cdev.h>
0024 #include <linux/poll.h>
0025 #include <linux/sched/signal.h>
0026 #include <linux/hid-roccat.h>
0027 #include <linux/module.h>
0028 
0029 #define ROCCAT_FIRST_MINOR 0
0030 #define ROCCAT_MAX_DEVICES 8
0031 
0032 /* should be a power of 2 for performance reason */
0033 #define ROCCAT_CBUF_SIZE 16
0034 
0035 struct roccat_report {
0036     uint8_t *value;
0037 };
0038 
0039 struct roccat_device {
0040     unsigned int minor;
0041     int report_size;
0042     int open;
0043     int exist;
0044     wait_queue_head_t wait;
0045     struct device *dev;
0046     struct hid_device *hid;
0047     struct list_head readers;
0048     /* protects modifications of readers list */
0049     struct mutex readers_lock;
0050 
0051     /*
0052      * circular_buffer has one writer and multiple readers with their own
0053      * read pointers
0054      */
0055     struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
0056     int cbuf_end;
0057     struct mutex cbuf_lock;
0058 };
0059 
0060 struct roccat_reader {
0061     struct list_head node;
0062     struct roccat_device *device;
0063     int cbuf_start;
0064 };
0065 
0066 static int roccat_major;
0067 static struct cdev roccat_cdev;
0068 
0069 static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
0070 /* protects modifications of devices array */
0071 static DEFINE_MUTEX(devices_lock);
0072 
0073 static ssize_t roccat_read(struct file *file, char __user *buffer,
0074         size_t count, loff_t *ppos)
0075 {
0076     struct roccat_reader *reader = file->private_data;
0077     struct roccat_device *device = reader->device;
0078     struct roccat_report *report;
0079     ssize_t retval = 0, len;
0080     DECLARE_WAITQUEUE(wait, current);
0081 
0082     mutex_lock(&device->cbuf_lock);
0083 
0084     /* no data? */
0085     if (reader->cbuf_start == device->cbuf_end) {
0086         add_wait_queue(&device->wait, &wait);
0087         set_current_state(TASK_INTERRUPTIBLE);
0088 
0089         /* wait for data */
0090         while (reader->cbuf_start == device->cbuf_end) {
0091             if (file->f_flags & O_NONBLOCK) {
0092                 retval = -EAGAIN;
0093                 break;
0094             }
0095             if (signal_pending(current)) {
0096                 retval = -ERESTARTSYS;
0097                 break;
0098             }
0099             if (!device->exist) {
0100                 retval = -EIO;
0101                 break;
0102             }
0103 
0104             mutex_unlock(&device->cbuf_lock);
0105             schedule();
0106             mutex_lock(&device->cbuf_lock);
0107             set_current_state(TASK_INTERRUPTIBLE);
0108         }
0109 
0110         set_current_state(TASK_RUNNING);
0111         remove_wait_queue(&device->wait, &wait);
0112     }
0113 
0114     /* here we either have data or a reason to return if retval is set */
0115     if (retval)
0116         goto exit_unlock;
0117 
0118     report = &device->cbuf[reader->cbuf_start];
0119     /*
0120      * If report is larger than requested amount of data, rest of report
0121      * is lost!
0122      */
0123     len = device->report_size > count ? count : device->report_size;
0124 
0125     if (copy_to_user(buffer, report->value, len)) {
0126         retval = -EFAULT;
0127         goto exit_unlock;
0128     }
0129     retval += len;
0130     reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
0131 
0132 exit_unlock:
0133     mutex_unlock(&device->cbuf_lock);
0134     return retval;
0135 }
0136 
0137 static __poll_t roccat_poll(struct file *file, poll_table *wait)
0138 {
0139     struct roccat_reader *reader = file->private_data;
0140     poll_wait(file, &reader->device->wait, wait);
0141     if (reader->cbuf_start != reader->device->cbuf_end)
0142         return EPOLLIN | EPOLLRDNORM;
0143     if (!reader->device->exist)
0144         return EPOLLERR | EPOLLHUP;
0145     return 0;
0146 }
0147 
0148 static int roccat_open(struct inode *inode, struct file *file)
0149 {
0150     unsigned int minor = iminor(inode);
0151     struct roccat_reader *reader;
0152     struct roccat_device *device;
0153     int error = 0;
0154 
0155     reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
0156     if (!reader)
0157         return -ENOMEM;
0158 
0159     mutex_lock(&devices_lock);
0160 
0161     device = devices[minor];
0162 
0163     if (!device) {
0164         pr_emerg("roccat device with minor %d doesn't exist\n", minor);
0165         error = -ENODEV;
0166         goto exit_err_devices;
0167     }
0168 
0169     mutex_lock(&device->readers_lock);
0170 
0171     if (!device->open++) {
0172         /* power on device on adding first reader */
0173         error = hid_hw_power(device->hid, PM_HINT_FULLON);
0174         if (error < 0) {
0175             --device->open;
0176             goto exit_err_readers;
0177         }
0178 
0179         error = hid_hw_open(device->hid);
0180         if (error < 0) {
0181             hid_hw_power(device->hid, PM_HINT_NORMAL);
0182             --device->open;
0183             goto exit_err_readers;
0184         }
0185     }
0186 
0187     reader->device = device;
0188     /* new reader doesn't get old events */
0189     reader->cbuf_start = device->cbuf_end;
0190 
0191     list_add_tail(&reader->node, &device->readers);
0192     file->private_data = reader;
0193 
0194 exit_err_readers:
0195     mutex_unlock(&device->readers_lock);
0196 exit_err_devices:
0197     mutex_unlock(&devices_lock);
0198     if (error)
0199         kfree(reader);
0200     return error;
0201 }
0202 
0203 static int roccat_release(struct inode *inode, struct file *file)
0204 {
0205     unsigned int minor = iminor(inode);
0206     struct roccat_reader *reader = file->private_data;
0207     struct roccat_device *device;
0208 
0209     mutex_lock(&devices_lock);
0210 
0211     device = devices[minor];
0212     if (!device) {
0213         mutex_unlock(&devices_lock);
0214         pr_emerg("roccat device with minor %d doesn't exist\n", minor);
0215         return -ENODEV;
0216     }
0217 
0218     mutex_lock(&device->readers_lock);
0219     list_del(&reader->node);
0220     mutex_unlock(&device->readers_lock);
0221     kfree(reader);
0222 
0223     if (!--device->open) {
0224         /* removing last reader */
0225         if (device->exist) {
0226             hid_hw_power(device->hid, PM_HINT_NORMAL);
0227             hid_hw_close(device->hid);
0228         } else {
0229             kfree(device);
0230         }
0231     }
0232 
0233     mutex_unlock(&devices_lock);
0234 
0235     return 0;
0236 }
0237 
0238 /*
0239  * roccat_report_event() - output data to readers
0240  * @minor: minor device number returned by roccat_connect()
0241  * @data: pointer to data
0242  *
0243  * Return value is zero on success, a negative error code on failure.
0244  *
0245  * This is called from interrupt handler.
0246  */
0247 int roccat_report_event(int minor, u8 const *data)
0248 {
0249     struct roccat_device *device;
0250     struct roccat_reader *reader;
0251     struct roccat_report *report;
0252     uint8_t *new_value;
0253 
0254     device = devices[minor];
0255 
0256     new_value = kmemdup(data, device->report_size, GFP_ATOMIC);
0257     if (!new_value)
0258         return -ENOMEM;
0259 
0260     report = &device->cbuf[device->cbuf_end];
0261 
0262     /* passing NULL is safe */
0263     kfree(report->value);
0264 
0265     report->value = new_value;
0266     device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
0267 
0268     list_for_each_entry(reader, &device->readers, node) {
0269         /*
0270          * As we already inserted one element, the buffer can't be
0271          * empty. If start and end are equal, buffer is full and we
0272          * increase start, so that slow reader misses one event, but
0273          * gets the newer ones in the right order.
0274          */
0275         if (reader->cbuf_start == device->cbuf_end)
0276             reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
0277     }
0278 
0279     wake_up_interruptible(&device->wait);
0280     return 0;
0281 }
0282 EXPORT_SYMBOL_GPL(roccat_report_event);
0283 
0284 /*
0285  * roccat_connect() - create a char device for special event output
0286  * @class: the class thats used to create the device. Meant to hold device
0287  * specific sysfs attributes.
0288  * @hid: the hid device the char device should be connected to.
0289  * @report_size: size of reports
0290  *
0291  * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
0292  * success, a negative error code on failure.
0293  */
0294 int roccat_connect(struct class *klass, struct hid_device *hid, int report_size)
0295 {
0296     unsigned int minor;
0297     struct roccat_device *device;
0298     int temp;
0299 
0300     device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
0301     if (!device)
0302         return -ENOMEM;
0303 
0304     mutex_lock(&devices_lock);
0305 
0306     for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
0307         if (devices[minor])
0308             continue;
0309         break;
0310     }
0311 
0312     if (minor < ROCCAT_MAX_DEVICES) {
0313         devices[minor] = device;
0314     } else {
0315         mutex_unlock(&devices_lock);
0316         kfree(device);
0317         return -EINVAL;
0318     }
0319 
0320     device->dev = device_create(klass, &hid->dev,
0321             MKDEV(roccat_major, minor), NULL,
0322             "%s%s%d", "roccat", hid->driver->name, minor);
0323 
0324     if (IS_ERR(device->dev)) {
0325         devices[minor] = NULL;
0326         mutex_unlock(&devices_lock);
0327         temp = PTR_ERR(device->dev);
0328         kfree(device);
0329         return temp;
0330     }
0331 
0332     mutex_unlock(&devices_lock);
0333 
0334     init_waitqueue_head(&device->wait);
0335     INIT_LIST_HEAD(&device->readers);
0336     mutex_init(&device->readers_lock);
0337     mutex_init(&device->cbuf_lock);
0338     device->minor = minor;
0339     device->hid = hid;
0340     device->exist = 1;
0341     device->cbuf_end = 0;
0342     device->report_size = report_size;
0343 
0344     return minor;
0345 }
0346 EXPORT_SYMBOL_GPL(roccat_connect);
0347 
0348 /* roccat_disconnect() - remove char device from hid device
0349  * @minor: the minor device number returned by roccat_connect()
0350  */
0351 void roccat_disconnect(int minor)
0352 {
0353     struct roccat_device *device;
0354 
0355     mutex_lock(&devices_lock);
0356     device = devices[minor];
0357     mutex_unlock(&devices_lock);
0358 
0359     device->exist = 0; /* TODO exist maybe not needed */
0360 
0361     device_destroy(device->dev->class, MKDEV(roccat_major, minor));
0362 
0363     mutex_lock(&devices_lock);
0364     devices[minor] = NULL;
0365     mutex_unlock(&devices_lock);
0366 
0367     if (device->open) {
0368         hid_hw_close(device->hid);
0369         wake_up_interruptible(&device->wait);
0370     } else {
0371         kfree(device);
0372     }
0373 }
0374 EXPORT_SYMBOL_GPL(roccat_disconnect);
0375 
0376 static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0377 {
0378     struct inode *inode = file_inode(file);
0379     struct roccat_device *device;
0380     unsigned int minor = iminor(inode);
0381     long retval = 0;
0382 
0383     mutex_lock(&devices_lock);
0384 
0385     device = devices[minor];
0386     if (!device) {
0387         retval = -ENODEV;
0388         goto out;
0389     }
0390 
0391     switch (cmd) {
0392     case ROCCATIOCGREPSIZE:
0393         if (put_user(device->report_size, (int __user *)arg))
0394             retval = -EFAULT;
0395         break;
0396     default:
0397         retval = -ENOTTY;
0398     }
0399 out:
0400     mutex_unlock(&devices_lock);
0401     return retval;
0402 }
0403 
0404 static const struct file_operations roccat_ops = {
0405     .owner = THIS_MODULE,
0406     .read = roccat_read,
0407     .poll = roccat_poll,
0408     .open = roccat_open,
0409     .release = roccat_release,
0410     .llseek = noop_llseek,
0411     .unlocked_ioctl = roccat_ioctl,
0412 };
0413 
0414 static int __init roccat_init(void)
0415 {
0416     int retval;
0417     dev_t dev_id;
0418 
0419     retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
0420             ROCCAT_MAX_DEVICES, "roccat");
0421     if (retval < 0) {
0422         pr_warn("can't get major number\n");
0423         goto error;
0424     }
0425 
0426     roccat_major = MAJOR(dev_id);
0427 
0428     cdev_init(&roccat_cdev, &roccat_ops);
0429     retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
0430 
0431     if (retval < 0) {
0432         pr_warn("cannot add cdev\n");
0433         goto cleanup_alloc_chrdev_region;
0434     }
0435     return 0;
0436 
0437 
0438  cleanup_alloc_chrdev_region:
0439     unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
0440  error:
0441     return retval;
0442 }
0443 
0444 static void __exit roccat_exit(void)
0445 {
0446     dev_t dev_id = MKDEV(roccat_major, 0);
0447 
0448     cdev_del(&roccat_cdev);
0449     unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
0450 }
0451 
0452 module_init(roccat_init);
0453 module_exit(roccat_exit);
0454 
0455 MODULE_AUTHOR("Stefan Achatz");
0456 MODULE_DESCRIPTION("USB Roccat char device");
0457 MODULE_LICENSE("GPL v2");