Commit | Line | Data |
---|---|---|
206f5f2f SA |
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 | ||
4291ee30 JP |
24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
25 | ||
206f5f2f SA |
26 | #include <linux/cdev.h> |
27 | #include <linux/poll.h> | |
28 | #include <linux/sched.h> | |
29 | ||
30 | #include "hid-roccat.h" | |
31 | ||
32 | #define ROCCAT_FIRST_MINOR 0 | |
33 | #define ROCCAT_MAX_DEVICES 8 | |
34 | ||
35 | /* should be a power of 2 for performance reason */ | |
36 | #define ROCCAT_CBUF_SIZE 16 | |
37 | ||
38 | struct roccat_report { | |
39 | uint8_t *value; | |
40 | int len; | |
41 | }; | |
42 | ||
43 | struct roccat_device { | |
44 | unsigned int minor; | |
45 | int open; | |
46 | int exist; | |
47 | wait_queue_head_t wait; | |
48 | struct device *dev; | |
49 | struct hid_device *hid; | |
50 | struct list_head readers; | |
51 | /* protects modifications of readers list */ | |
52 | struct mutex readers_lock; | |
53 | ||
54 | /* | |
55 | * circular_buffer has one writer and multiple readers with their own | |
56 | * read pointers | |
57 | */ | |
58 | struct roccat_report cbuf[ROCCAT_CBUF_SIZE]; | |
59 | int cbuf_end; | |
60 | struct mutex cbuf_lock; | |
61 | }; | |
62 | ||
63 | struct roccat_reader { | |
64 | struct list_head node; | |
65 | struct roccat_device *device; | |
66 | int cbuf_start; | |
67 | }; | |
68 | ||
69 | static int roccat_major; | |
70 | static struct class *roccat_class; | |
71 | static struct cdev roccat_cdev; | |
72 | ||
73 | static struct roccat_device *devices[ROCCAT_MAX_DEVICES]; | |
74 | /* protects modifications of devices array */ | |
75 | static DEFINE_MUTEX(devices_lock); | |
76 | ||
77 | static ssize_t roccat_read(struct file *file, char __user *buffer, | |
78 | size_t count, loff_t *ppos) | |
79 | { | |
80 | struct roccat_reader *reader = file->private_data; | |
81 | struct roccat_device *device = reader->device; | |
82 | struct roccat_report *report; | |
83 | ssize_t retval = 0, len; | |
84 | DECLARE_WAITQUEUE(wait, current); | |
85 | ||
86 | mutex_lock(&device->cbuf_lock); | |
87 | ||
88 | /* no data? */ | |
89 | if (reader->cbuf_start == device->cbuf_end) { | |
90 | add_wait_queue(&device->wait, &wait); | |
91 | set_current_state(TASK_INTERRUPTIBLE); | |
92 | ||
93 | /* wait for data */ | |
94 | while (reader->cbuf_start == device->cbuf_end) { | |
95 | if (file->f_flags & O_NONBLOCK) { | |
96 | retval = -EAGAIN; | |
97 | break; | |
98 | } | |
99 | if (signal_pending(current)) { | |
100 | retval = -ERESTARTSYS; | |
101 | break; | |
102 | } | |
103 | if (!device->exist) { | |
104 | retval = -EIO; | |
105 | break; | |
106 | } | |
107 | ||
108 | mutex_unlock(&device->cbuf_lock); | |
109 | schedule(); | |
110 | mutex_lock(&device->cbuf_lock); | |
111 | set_current_state(TASK_INTERRUPTIBLE); | |
112 | } | |
113 | ||
114 | set_current_state(TASK_RUNNING); | |
115 | remove_wait_queue(&device->wait, &wait); | |
116 | } | |
117 | ||
118 | /* here we either have data or a reason to return if retval is set */ | |
119 | if (retval) | |
120 | goto exit_unlock; | |
121 | ||
122 | report = &device->cbuf[reader->cbuf_start]; | |
123 | /* | |
124 | * If report is larger than requested amount of data, rest of report | |
125 | * is lost! | |
126 | */ | |
127 | len = report->len > count ? count : report->len; | |
128 | ||
129 | if (copy_to_user(buffer, report->value, len)) { | |
130 | retval = -EFAULT; | |
131 | goto exit_unlock; | |
132 | } | |
133 | retval += len; | |
134 | reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE; | |
135 | ||
136 | exit_unlock: | |
137 | mutex_unlock(&device->cbuf_lock); | |
138 | return retval; | |
139 | } | |
140 | ||
141 | static unsigned int roccat_poll(struct file *file, poll_table *wait) | |
142 | { | |
143 | struct roccat_reader *reader = file->private_data; | |
144 | poll_wait(file, &reader->device->wait, wait); | |
145 | if (reader->cbuf_start != reader->device->cbuf_end) | |
146 | return POLLIN | POLLRDNORM; | |
147 | if (!reader->device->exist) | |
148 | return POLLERR | POLLHUP; | |
149 | return 0; | |
150 | } | |
151 | ||
152 | static int roccat_open(struct inode *inode, struct file *file) | |
153 | { | |
154 | unsigned int minor = iminor(inode); | |
155 | struct roccat_reader *reader; | |
156 | struct roccat_device *device; | |
157 | int error = 0; | |
158 | ||
159 | reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL); | |
160 | if (!reader) | |
161 | return -ENOMEM; | |
162 | ||
163 | mutex_lock(&devices_lock); | |
164 | ||
165 | device = devices[minor]; | |
166 | ||
167 | mutex_lock(&device->readers_lock); | |
168 | ||
169 | if (!device) { | |
4291ee30 | 170 | pr_emerg("roccat device with minor %d doesn't exist\n", minor); |
206f5f2f | 171 | error = -ENODEV; |
dfe5c7b7 | 172 | goto exit_err; |
206f5f2f SA |
173 | } |
174 | ||
175 | if (!device->open++) { | |
176 | /* power on device on adding first reader */ | |
5bea7660 DT |
177 | error = hid_hw_power(device->hid, PM_HINT_FULLON); |
178 | if (error < 0) { | |
179 | --device->open; | |
180 | goto exit_err; | |
206f5f2f | 181 | } |
5bea7660 DT |
182 | |
183 | error = hid_hw_open(device->hid); | |
206f5f2f | 184 | if (error < 0) { |
5bea7660 | 185 | hid_hw_power(device->hid, PM_HINT_NORMAL); |
206f5f2f | 186 | --device->open; |
dfe5c7b7 | 187 | goto exit_err; |
206f5f2f SA |
188 | } |
189 | } | |
190 | ||
191 | reader->device = device; | |
192 | /* new reader doesn't get old events */ | |
193 | reader->cbuf_start = device->cbuf_end; | |
194 | ||
195 | list_add_tail(&reader->node, &device->readers); | |
196 | file->private_data = reader; | |
197 | ||
198 | exit_unlock: | |
199 | mutex_unlock(&device->readers_lock); | |
200 | mutex_unlock(&devices_lock); | |
201 | return error; | |
dfe5c7b7 JL |
202 | exit_err: |
203 | kfree(reader); | |
204 | goto exit_unlock; | |
206f5f2f SA |
205 | } |
206 | ||
207 | static int roccat_release(struct inode *inode, struct file *file) | |
208 | { | |
209 | unsigned int minor = iminor(inode); | |
210 | struct roccat_reader *reader = file->private_data; | |
211 | struct roccat_device *device; | |
212 | ||
213 | mutex_lock(&devices_lock); | |
214 | ||
215 | device = devices[minor]; | |
216 | if (!device) { | |
217 | mutex_unlock(&devices_lock); | |
4291ee30 | 218 | pr_emerg("roccat device with minor %d doesn't exist\n", minor); |
206f5f2f SA |
219 | return -ENODEV; |
220 | } | |
221 | ||
222 | mutex_lock(&device->readers_lock); | |
223 | list_del(&reader->node); | |
224 | mutex_unlock(&device->readers_lock); | |
225 | kfree(reader); | |
226 | ||
227 | if (!--device->open) { | |
228 | /* removing last reader */ | |
229 | if (device->exist) { | |
5bea7660 DT |
230 | hid_hw_power(device->hid, PM_HINT_NORMAL); |
231 | hid_hw_close(device->hid); | |
206f5f2f SA |
232 | } else { |
233 | kfree(device); | |
234 | } | |
235 | } | |
236 | ||
237 | mutex_unlock(&devices_lock); | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | /* | |
243 | * roccat_report_event() - output data to readers | |
244 | * @minor: minor device number returned by roccat_connect() | |
245 | * @data: pointer to data | |
246 | * @len: size of data | |
247 | * | |
248 | * Return value is zero on success, a negative error code on failure. | |
249 | * | |
250 | * This is called from interrupt handler. | |
251 | */ | |
252 | int roccat_report_event(int minor, u8 const *data, int len) | |
253 | { | |
254 | struct roccat_device *device; | |
255 | struct roccat_reader *reader; | |
256 | struct roccat_report *report; | |
257 | uint8_t *new_value; | |
258 | ||
259 | new_value = kmemdup(data, len, GFP_ATOMIC); | |
260 | if (!new_value) | |
261 | return -ENOMEM; | |
262 | ||
263 | device = devices[minor]; | |
264 | ||
265 | report = &device->cbuf[device->cbuf_end]; | |
266 | ||
267 | /* passing NULL is safe */ | |
268 | kfree(report->value); | |
269 | ||
270 | report->value = new_value; | |
271 | report->len = len; | |
272 | device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE; | |
273 | ||
274 | list_for_each_entry(reader, &device->readers, node) { | |
275 | /* | |
276 | * As we already inserted one element, the buffer can't be | |
277 | * empty. If start and end are equal, buffer is full and we | |
278 | * increase start, so that slow reader misses one event, but | |
279 | * gets the newer ones in the right order. | |
280 | */ | |
281 | if (reader->cbuf_start == device->cbuf_end) | |
282 | reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE; | |
283 | } | |
284 | ||
285 | wake_up_interruptible(&device->wait); | |
286 | return 0; | |
287 | } | |
288 | EXPORT_SYMBOL_GPL(roccat_report_event); | |
289 | ||
290 | /* | |
291 | * roccat_connect() - create a char device for special event output | |
292 | * @hid: the hid device the char device should be connected to. | |
293 | * | |
294 | * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on | |
295 | * success, a negative error code on failure. | |
296 | */ | |
297 | int roccat_connect(struct hid_device *hid) | |
298 | { | |
299 | unsigned int minor; | |
300 | struct roccat_device *device; | |
301 | int temp; | |
302 | ||
303 | device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL); | |
304 | if (!device) | |
305 | return -ENOMEM; | |
306 | ||
307 | mutex_lock(&devices_lock); | |
308 | ||
309 | for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) { | |
310 | if (devices[minor]) | |
311 | continue; | |
312 | break; | |
313 | } | |
314 | ||
315 | if (minor < ROCCAT_MAX_DEVICES) { | |
316 | devices[minor] = device; | |
317 | } else { | |
318 | mutex_unlock(&devices_lock); | |
319 | kfree(device); | |
320 | return -EINVAL; | |
321 | } | |
322 | ||
323 | device->dev = device_create(roccat_class, &hid->dev, | |
324 | MKDEV(roccat_major, minor), NULL, | |
325 | "%s%s%d", "roccat", hid->driver->name, minor); | |
326 | ||
327 | if (IS_ERR(device->dev)) { | |
328 | devices[minor] = NULL; | |
329 | mutex_unlock(&devices_lock); | |
330 | temp = PTR_ERR(device->dev); | |
331 | kfree(device); | |
332 | return temp; | |
333 | } | |
334 | ||
335 | mutex_unlock(&devices_lock); | |
336 | ||
337 | init_waitqueue_head(&device->wait); | |
338 | INIT_LIST_HEAD(&device->readers); | |
339 | mutex_init(&device->readers_lock); | |
340 | mutex_init(&device->cbuf_lock); | |
341 | device->minor = minor; | |
342 | device->hid = hid; | |
343 | device->exist = 1; | |
344 | device->cbuf_end = 0; | |
345 | ||
346 | return minor; | |
347 | } | |
348 | EXPORT_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 | */ | |
353 | void roccat_disconnect(int minor) | |
354 | { | |
355 | struct roccat_device *device; | |
356 | ||
357 | mutex_lock(&devices_lock); | |
358 | device = devices[minor]; | |
359 | devices[minor] = NULL; | |
360 | mutex_unlock(&devices_lock); | |
361 | ||
362 | device->exist = 0; /* TODO exist maybe not needed */ | |
363 | ||
364 | device_destroy(roccat_class, MKDEV(roccat_major, minor)); | |
365 | ||
366 | if (device->open) { | |
5bea7660 | 367 | hid_hw_close(device->hid); |
206f5f2f SA |
368 | wake_up_interruptible(&device->wait); |
369 | } else { | |
370 | kfree(device); | |
371 | } | |
372 | } | |
373 | EXPORT_SYMBOL_GPL(roccat_disconnect); | |
374 | ||
375 | static const struct file_operations roccat_ops = { | |
376 | .owner = THIS_MODULE, | |
377 | .read = roccat_read, | |
378 | .poll = roccat_poll, | |
379 | .open = roccat_open, | |
380 | .release = roccat_release, | |
6038f373 | 381 | .llseek = noop_llseek, |
206f5f2f SA |
382 | }; |
383 | ||
384 | static int __init roccat_init(void) | |
385 | { | |
386 | int retval; | |
387 | dev_t dev_id; | |
388 | ||
389 | retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR, | |
390 | ROCCAT_MAX_DEVICES, "roccat"); | |
391 | ||
392 | roccat_major = MAJOR(dev_id); | |
393 | ||
394 | if (retval < 0) { | |
4291ee30 | 395 | pr_warn("can't get major number\n"); |
206f5f2f SA |
396 | return retval; |
397 | } | |
398 | ||
399 | roccat_class = class_create(THIS_MODULE, "roccat"); | |
400 | if (IS_ERR(roccat_class)) { | |
401 | retval = PTR_ERR(roccat_class); | |
402 | unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES); | |
403 | return retval; | |
404 | } | |
405 | ||
406 | cdev_init(&roccat_cdev, &roccat_ops); | |
407 | cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES); | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
412 | static void __exit roccat_exit(void) | |
413 | { | |
414 | dev_t dev_id = MKDEV(roccat_major, 0); | |
415 | ||
416 | cdev_del(&roccat_cdev); | |
417 | class_destroy(roccat_class); | |
418 | unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES); | |
419 | } | |
420 | ||
421 | module_init(roccat_init); | |
422 | module_exit(roccat_exit); | |
423 | ||
424 | MODULE_AUTHOR("Stefan Achatz"); | |
425 | MODULE_DESCRIPTION("USB Roccat char device"); | |
426 | MODULE_LICENSE("GPL v2"); |