0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
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
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
0049 struct mutex readers_lock;
0050
0051
0052
0053
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
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
0085 if (reader->cbuf_start == device->cbuf_end) {
0086 add_wait_queue(&device->wait, &wait);
0087 set_current_state(TASK_INTERRUPTIBLE);
0088
0089
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
0115 if (retval)
0116 goto exit_unlock;
0117
0118 report = &device->cbuf[reader->cbuf_start];
0119
0120
0121
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
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
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
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
0240
0241
0242
0243
0244
0245
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
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
0271
0272
0273
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
0286
0287
0288
0289
0290
0291
0292
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
0349
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;
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");