Linux Audio

Check our new training course

Embedded Linux training

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