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; | |
206f5f2f SA |
70 | static struct cdev roccat_cdev; |
71 | ||
72 | static struct roccat_device *devices[ROCCAT_MAX_DEVICES]; | |
73 | /* protects modifications of devices array */ | |
74 | static DEFINE_MUTEX(devices_lock); | |
75 | ||
76 | static ssize_t roccat_read(struct file *file, char __user *buffer, | |
77 | size_t count, loff_t *ppos) | |
78 | { | |
79 | struct roccat_reader *reader = file->private_data; | |
80 | struct roccat_device *device = reader->device; | |
81 | struct roccat_report *report; | |
82 | ssize_t retval = 0, len; | |
83 | DECLARE_WAITQUEUE(wait, current); | |
84 | ||
85 | mutex_lock(&device->cbuf_lock); | |
86 | ||
87 | /* no data? */ | |
88 | if (reader->cbuf_start == device->cbuf_end) { | |
89 | add_wait_queue(&device->wait, &wait); | |
90 | set_current_state(TASK_INTERRUPTIBLE); | |
91 | ||
92 | /* wait for data */ | |
93 | while (reader->cbuf_start == device->cbuf_end) { | |
94 | if (file->f_flags & O_NONBLOCK) { | |
95 | retval = -EAGAIN; | |
96 | break; | |
97 | } | |
98 | if (signal_pending(current)) { | |
99 | retval = -ERESTARTSYS; | |
100 | break; | |
101 | } | |
102 | if (!device->exist) { | |
103 | retval = -EIO; | |
104 | break; | |
105 | } | |
106 | ||
107 | mutex_unlock(&device->cbuf_lock); | |
108 | schedule(); | |
109 | mutex_lock(&device->cbuf_lock); | |
110 | set_current_state(TASK_INTERRUPTIBLE); | |
111 | } | |
112 | ||
113 | set_current_state(TASK_RUNNING); | |
114 | remove_wait_queue(&device->wait, &wait); | |
115 | } | |
116 | ||
117 | /* here we either have data or a reason to return if retval is set */ | |
118 | if (retval) | |
119 | goto exit_unlock; | |
120 | ||
121 | report = &device->cbuf[reader->cbuf_start]; | |
122 | /* | |
123 | * If report is larger than requested amount of data, rest of report | |
124 | * is lost! | |
125 | */ | |
126 | len = report->len > count ? count : report->len; | |
127 | ||
128 | if (copy_to_user(buffer, report->value, len)) { | |
129 | retval = -EFAULT; | |
130 | goto exit_unlock; | |
131 | } | |
132 | retval += len; | |
133 | reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE; | |
134 | ||
135 | exit_unlock: | |
136 | mutex_unlock(&device->cbuf_lock); | |
137 | return retval; | |
138 | } | |
139 | ||
140 | static unsigned int roccat_poll(struct file *file, poll_table *wait) | |
141 | { | |
142 | struct roccat_reader *reader = file->private_data; | |
143 | poll_wait(file, &reader->device->wait, wait); | |
144 | if (reader->cbuf_start != reader->device->cbuf_end) | |
145 | return POLLIN | POLLRDNORM; | |
146 | if (!reader->device->exist) | |
147 | return POLLERR | POLLHUP; | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static int roccat_open(struct inode *inode, struct file *file) | |
152 | { | |
153 | unsigned int minor = iminor(inode); | |
154 | struct roccat_reader *reader; | |
155 | struct roccat_device *device; | |
156 | int error = 0; | |
157 | ||
158 | reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL); | |
159 | if (!reader) | |
160 | return -ENOMEM; | |
161 | ||
162 | mutex_lock(&devices_lock); | |
163 | ||
164 | device = devices[minor]; | |
165 | ||
166 | mutex_lock(&device->readers_lock); | |
167 | ||
168 | if (!device) { | |
4291ee30 | 169 | pr_emerg("roccat device with minor %d doesn't exist\n", minor); |
206f5f2f | 170 | error = -ENODEV; |
dfe5c7b7 | 171 | goto exit_err; |
206f5f2f SA |
172 | } |
173 | ||
174 | if (!device->open++) { | |
175 | /* power on device on adding first reader */ | |
5bea7660 DT |
176 | error = hid_hw_power(device->hid, PM_HINT_FULLON); |
177 | if (error < 0) { | |
178 | --device->open; | |
179 | goto exit_err; | |
206f5f2f | 180 | } |
5bea7660 DT |
181 | |
182 | error = hid_hw_open(device->hid); | |
206f5f2f | 183 | if (error < 0) { |
5bea7660 | 184 | hid_hw_power(device->hid, PM_HINT_NORMAL); |
206f5f2f | 185 | --device->open; |
dfe5c7b7 | 186 | goto exit_err; |
206f5f2f SA |
187 | } |
188 | } | |
189 | ||
190 | reader->device = device; | |
191 | /* new reader doesn't get old events */ | |
192 | reader->cbuf_start = device->cbuf_end; | |
193 | ||
194 | list_add_tail(&reader->node, &device->readers); | |
195 | file->private_data = reader; | |
196 | ||
197 | exit_unlock: | |
198 | mutex_unlock(&device->readers_lock); | |
199 | mutex_unlock(&devices_lock); | |
200 | return error; | |
dfe5c7b7 JL |
201 | exit_err: |
202 | kfree(reader); | |
203 | goto exit_unlock; | |
206f5f2f SA |
204 | } |
205 | ||
206 | static int roccat_release(struct inode *inode, struct file *file) | |
207 | { | |
208 | unsigned int minor = iminor(inode); | |
209 | struct roccat_reader *reader = file->private_data; | |
210 | struct roccat_device *device; | |
211 | ||
212 | mutex_lock(&devices_lock); | |
213 | ||
214 | device = devices[minor]; | |
215 | if (!device) { | |
216 | mutex_unlock(&devices_lock); | |
4291ee30 | 217 | pr_emerg("roccat device with minor %d doesn't exist\n", minor); |
206f5f2f SA |
218 | return -ENODEV; |
219 | } | |
220 | ||
221 | mutex_lock(&device->readers_lock); | |
222 | list_del(&reader->node); | |
223 | mutex_unlock(&device->readers_lock); | |
224 | kfree(reader); | |
225 | ||
226 | if (!--device->open) { | |
227 | /* removing last reader */ | |
228 | if (device->exist) { | |
5bea7660 DT |
229 | hid_hw_power(device->hid, PM_HINT_NORMAL); |
230 | hid_hw_close(device->hid); | |
206f5f2f SA |
231 | } else { |
232 | kfree(device); | |
233 | } | |
234 | } | |
235 | ||
236 | mutex_unlock(&devices_lock); | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | /* | |
242 | * roccat_report_event() - output data to readers | |
243 | * @minor: minor device number returned by roccat_connect() | |
244 | * @data: pointer to data | |
245 | * @len: size of data | |
246 | * | |
247 | * Return value is zero on success, a negative error code on failure. | |
248 | * | |
249 | * This is called from interrupt handler. | |
250 | */ | |
251 | int roccat_report_event(int minor, u8 const *data, int len) | |
252 | { | |
253 | struct roccat_device *device; | |
254 | struct roccat_reader *reader; | |
255 | struct roccat_report *report; | |
256 | uint8_t *new_value; | |
257 | ||
258 | new_value = kmemdup(data, len, GFP_ATOMIC); | |
259 | if (!new_value) | |
260 | return -ENOMEM; | |
261 | ||
262 | device = devices[minor]; | |
263 | ||
264 | report = &device->cbuf[device->cbuf_end]; | |
265 | ||
266 | /* passing NULL is safe */ | |
267 | kfree(report->value); | |
268 | ||
269 | report->value = new_value; | |
270 | report->len = len; | |
271 | device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE; | |
272 | ||
273 | list_for_each_entry(reader, &device->readers, node) { | |
274 | /* | |
275 | * As we already inserted one element, the buffer can't be | |
276 | * empty. If start and end are equal, buffer is full and we | |
277 | * increase start, so that slow reader misses one event, but | |
278 | * gets the newer ones in the right order. | |
279 | */ | |
280 | if (reader->cbuf_start == device->cbuf_end) | |
281 | reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE; | |
282 | } | |
283 | ||
284 | wake_up_interruptible(&device->wait); | |
285 | return 0; | |
286 | } | |
287 | EXPORT_SYMBOL_GPL(roccat_report_event); | |
288 | ||
289 | /* | |
290 | * roccat_connect() - create a char device for special event output | |
5012aada SA |
291 | * @class: the class thats used to create the device. Meant to hold device |
292 | * specific sysfs attributes. | |
206f5f2f SA |
293 | * @hid: the hid device the char device should be connected to. |
294 | * | |
295 | * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on | |
296 | * success, a negative error code on failure. | |
297 | */ | |
5012aada | 298 | int roccat_connect(struct class *klass, struct hid_device *hid) |
206f5f2f SA |
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 | ||
5012aada | 324 | device->dev = device_create(klass, &hid->dev, |
206f5f2f SA |
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 | ||
347 | return minor; | |
348 | } | |
349 | EXPORT_SYMBOL_GPL(roccat_connect); | |
350 | ||
351 | /* roccat_disconnect() - remove char device from hid device | |
352 | * @minor: the minor device number returned by roccat_connect() | |
353 | */ | |
354 | void roccat_disconnect(int minor) | |
355 | { | |
356 | struct roccat_device *device; | |
357 | ||
358 | mutex_lock(&devices_lock); | |
359 | device = devices[minor]; | |
360 | devices[minor] = NULL; | |
361 | mutex_unlock(&devices_lock); | |
362 | ||
363 | device->exist = 0; /* TODO exist maybe not needed */ | |
364 | ||
5012aada | 365 | device_destroy(device->dev->class, MKDEV(roccat_major, minor)); |
206f5f2f SA |
366 | |
367 | if (device->open) { | |
5bea7660 | 368 | hid_hw_close(device->hid); |
206f5f2f SA |
369 | wake_up_interruptible(&device->wait); |
370 | } else { | |
371 | kfree(device); | |
372 | } | |
373 | } | |
374 | EXPORT_SYMBOL_GPL(roccat_disconnect); | |
375 | ||
376 | static const struct file_operations roccat_ops = { | |
377 | .owner = THIS_MODULE, | |
378 | .read = roccat_read, | |
379 | .poll = roccat_poll, | |
380 | .open = roccat_open, | |
381 | .release = roccat_release, | |
6038f373 | 382 | .llseek = noop_llseek, |
206f5f2f SA |
383 | }; |
384 | ||
385 | static int __init roccat_init(void) | |
386 | { | |
387 | int retval; | |
388 | dev_t dev_id; | |
389 | ||
390 | retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR, | |
391 | ROCCAT_MAX_DEVICES, "roccat"); | |
392 | ||
393 | roccat_major = MAJOR(dev_id); | |
394 | ||
395 | if (retval < 0) { | |
4291ee30 | 396 | pr_warn("can't get major number\n"); |
206f5f2f SA |
397 | return retval; |
398 | } | |
399 | ||
206f5f2f SA |
400 | cdev_init(&roccat_cdev, &roccat_ops); |
401 | cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES); | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
406 | static void __exit roccat_exit(void) | |
407 | { | |
408 | dev_t dev_id = MKDEV(roccat_major, 0); | |
409 | ||
410 | cdev_del(&roccat_cdev); | |
206f5f2f SA |
411 | unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES); |
412 | } | |
413 | ||
414 | module_init(roccat_init); | |
415 | module_exit(roccat_exit); | |
416 | ||
417 | MODULE_AUTHOR("Stefan Achatz"); | |
418 | MODULE_DESCRIPTION("USB Roccat char device"); | |
419 | MODULE_LICENSE("GPL v2"); |