at commit c03c1a14

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 unsigned int minor = iminor(inode);
215 struct roccat_reader *reader = file->private_data;
216 struct roccat_device *device;
217
218 mutex_lock(&devices_lock);
219
220 device = devices[minor];
221 if (!device) {
222 mutex_unlock(&devices_lock);
223 pr_emerg("roccat device with minor %d doesn't exist\n", minor);
224 return -ENODEV;
225 }
226
227 mutex_lock(&device->readers_lock);
228 list_del(&reader->node);
229 mutex_unlock(&device->readers_lock);
230 kfree(reader);
231
232 if (!--device->open) {
233 /* removing last reader */
234 if (device->exist) {
235 hid_hw_power(device->hid, PM_HINT_NORMAL);
236 hid_hw_close(device->hid);
237 } else {
238 roccat_free_device(device);
239 }
240 }
241
242 mutex_unlock(&devices_lock);
243
244 return 0;
245 }
246
247 /*
248 * roccat_report_event() - output data to readers
249 * @minor: minor device number returned by roccat_connect()
250 * @data: pointer to data
251 *
252 * Return value is zero on success, a negative error code on failure.
253 *
254 * This is called from interrupt handler.
255 */
256 int roccat_report_event(int minor, u8 const *data)
257 {
258 struct roccat_device *device;
259 struct roccat_reader *reader;
260 struct roccat_report *report;
261 uint8_t *new_value;
262
263 device = devices[minor];
264
265 new_value = kmemdup(data, device->report_size, GFP_ATOMIC);
266 if (!new_value)
267 return -ENOMEM;
268
269 mutex_lock(&device->readers_lock);
270 mutex_lock(&device->cbuf_lock);
271
272 report = &device->cbuf[device->cbuf_end];
273
274 /* passing NULL is safe */
275 kfree(report->value);
276
277 report->value = new_value;
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 mutex_unlock(&device->cbuf_lock);
292 mutex_unlock(&device->readers_lock);
293
294 wake_up_interruptible(&device->wait);
295 return 0;
296 }
297 EXPORT_SYMBOL_GPL(roccat_report_event);
298
299 /*
300 * roccat_connect() - create a char device for special event output
301 * @class: the class thats used to create the device. Meant to hold device
302 * specific sysfs attributes.
303 * @hid: the hid device the char device should be connected to.
304 * @report_size: size of reports
305 *
306 * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
307 * success, a negative error code on failure.
308 */
309 int roccat_connect(const struct class *klass, struct hid_device *hid, int report_size)
310 {
311 unsigned int minor;
312 struct roccat_device *device;
313 int temp;
314
315 device = kzalloc_obj(struct roccat_device);
316 if (!device)
317 return -ENOMEM;
318
319 mutex_lock(&devices_lock);
320
321 for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
322 if (devices[minor])
323 continue;
324 break;
325 }
326
327 if (minor < ROCCAT_MAX_DEVICES) {
328 devices[minor] = device;
329 } else {
330 mutex_unlock(&devices_lock);
331 kfree(device);
332 return -EINVAL;
333 }
334
335 device->dev = device_create(klass, &hid->dev,
336 MKDEV(roccat_major, minor), NULL,
337 "%s%s%d", "roccat", hid->driver->name, minor);
338
339 if (IS_ERR(device->dev)) {
340 devices[minor] = NULL;
341 mutex_unlock(&devices_lock);
342 temp = PTR_ERR(device->dev);
343 kfree(device);
344 return temp;
345 }
346
347 init_waitqueue_head(&device->wait);
348 INIT_LIST_HEAD(&device->readers);
349 mutex_init(&device->readers_lock);
350 mutex_init(&device->cbuf_lock);
351 device->minor = minor;
352 device->hid = hid;
353 device->exist = 1;
354 device->cbuf_end = 0;
355 device->report_size = report_size;
356
357 mutex_unlock(&devices_lock);
358 return minor;
359 }
360 EXPORT_SYMBOL_GPL(roccat_connect);
361
362 /* roccat_disconnect() - remove char device from hid device
363 * @minor: the minor device number returned by roccat_connect()
364 */
365 void roccat_disconnect(int minor)
366 {
367 struct roccat_device *device;
368
369 mutex_lock(&devices_lock);
370 device = devices[minor];
371
372 device->exist = 0; /* TODO exist maybe not needed */
373
374 device_destroy(device->dev->class, MKDEV(roccat_major, minor));
375
376 devices[minor] = NULL;
377
378 if (device->open) {
379 hid_hw_close(device->hid);
380 wake_up_interruptible(&device->wait);
381 } else {
382 roccat_free_device(device);
383 }
384
385 mutex_unlock(&devices_lock);
386 }
387 EXPORT_SYMBOL_GPL(roccat_disconnect);
388
389 static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
390 {
391 struct inode *inode = file_inode(file);
392 struct roccat_device *device;
393 unsigned int minor = iminor(inode);
394 long retval = 0;
395
396 mutex_lock(&devices_lock);
397
398 device = devices[minor];
399 if (!device) {
400 retval = -ENODEV;
401 goto out;
402 }
403
404 switch (cmd) {
405 case ROCCATIOCGREPSIZE:
406 if (put_user(device->report_size, (int __user *)arg))
407 retval = -EFAULT;
408 break;
409 default:
410 retval = -ENOTTY;
411 }
412 out:
413 mutex_unlock(&devices_lock);
414 return retval;
415 }
416
417 static const struct file_operations roccat_ops = {
418 .owner = THIS_MODULE,
419 .read = roccat_read,
420 .poll = roccat_poll,
421 .open = roccat_open,
422 .release = roccat_release,
423 .llseek = noop_llseek,
424 .unlocked_ioctl = roccat_ioctl,
425 };
426
427 static int __init roccat_init(void)
428 {
429 int retval;
430 dev_t dev_id;
431
432 retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
433 ROCCAT_MAX_DEVICES, "roccat");
434 if (retval < 0) {
435 pr_warn("can't get major number\n");
436 goto error;
437 }
438
439 roccat_major = MAJOR(dev_id);
440
441 cdev_init(&roccat_cdev, &roccat_ops);
442 retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
443
444 if (retval < 0) {
445 pr_warn("cannot add cdev\n");
446 goto cleanup_alloc_chrdev_region;
447 }
448 return 0;
449
450
451 cleanup_alloc_chrdev_region:
452 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
453 error:
454 return retval;
455 }
456
457 static void __exit roccat_exit(void)
458 {
459 dev_t dev_id = MKDEV(roccat_major, 0);
460
461 cdev_del(&roccat_cdev);
462 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
463 }
464
465 module_init(roccat_init);
466 module_exit(roccat_exit);
467
468 MODULE_AUTHOR("Stefan Achatz");
469 MODULE_DESCRIPTION("USB Roccat char device");
470 MODULE_LICENSE("GPL v2");