xref: /linux/drivers/hid/hid-roccat.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
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 	mutex_unlock(&devices_lock);
348 
349 	init_waitqueue_head(&device->wait);
350 	INIT_LIST_HEAD(&device->readers);
351 	mutex_init(&device->readers_lock);
352 	mutex_init(&device->cbuf_lock);
353 	device->minor = minor;
354 	device->hid = hid;
355 	device->exist = 1;
356 	device->cbuf_end = 0;
357 	device->report_size = report_size;
358 
359 	return minor;
360 }
361 EXPORT_SYMBOL_GPL(roccat_connect);
362 
363 /* roccat_disconnect() - remove char device from hid device
364  * @minor: the minor device number returned by roccat_connect()
365  */
366 void roccat_disconnect(int minor)
367 {
368 	struct roccat_device *device;
369 
370 	mutex_lock(&devices_lock);
371 	device = devices[minor];
372 	mutex_unlock(&devices_lock);
373 
374 	device->exist = 0; /* TODO exist maybe not needed */
375 
376 	device_destroy(device->dev->class, MKDEV(roccat_major, minor));
377 
378 	mutex_lock(&devices_lock);
379 	devices[minor] = NULL;
380 	mutex_unlock(&devices_lock);
381 
382 	if (device->open) {
383 		hid_hw_close(device->hid);
384 		wake_up_interruptible(&device->wait);
385 	} else {
386 		roccat_free_device(device);
387 	}
388 }
389 EXPORT_SYMBOL_GPL(roccat_disconnect);
390 
391 static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
392 {
393 	struct inode *inode = file_inode(file);
394 	struct roccat_device *device;
395 	unsigned int minor = iminor(inode);
396 	long retval = 0;
397 
398 	mutex_lock(&devices_lock);
399 
400 	device = devices[minor];
401 	if (!device) {
402 		retval = -ENODEV;
403 		goto out;
404 	}
405 
406 	switch (cmd) {
407 	case ROCCATIOCGREPSIZE:
408 		if (put_user(device->report_size, (int __user *)arg))
409 			retval = -EFAULT;
410 		break;
411 	default:
412 		retval = -ENOTTY;
413 	}
414 out:
415 	mutex_unlock(&devices_lock);
416 	return retval;
417 }
418 
419 static const struct file_operations roccat_ops = {
420 	.owner = THIS_MODULE,
421 	.read = roccat_read,
422 	.poll = roccat_poll,
423 	.open = roccat_open,
424 	.release = roccat_release,
425 	.llseek = noop_llseek,
426 	.unlocked_ioctl = roccat_ioctl,
427 };
428 
429 static int __init roccat_init(void)
430 {
431 	int retval;
432 	dev_t dev_id;
433 
434 	retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
435 			ROCCAT_MAX_DEVICES, "roccat");
436 	if (retval < 0) {
437 		pr_warn("can't get major number\n");
438 		goto error;
439 	}
440 
441 	roccat_major = MAJOR(dev_id);
442 
443 	cdev_init(&roccat_cdev, &roccat_ops);
444 	retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
445 
446 	if (retval < 0) {
447 		pr_warn("cannot add cdev\n");
448 		goto cleanup_alloc_chrdev_region;
449 	}
450 	return 0;
451 
452 
453  cleanup_alloc_chrdev_region:
454 	unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
455  error:
456 	return retval;
457 }
458 
459 static void __exit roccat_exit(void)
460 {
461 	dev_t dev_id = MKDEV(roccat_major, 0);
462 
463 	cdev_del(&roccat_cdev);
464 	unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
465 }
466 
467 module_init(roccat_init);
468 module_exit(roccat_exit);
469 
470 MODULE_AUTHOR("Stefan Achatz");
471 MODULE_DESCRIPTION("USB Roccat char device");
472 MODULE_LICENSE("GPL v2");
473