diff options
Diffstat (limited to 'drivers/hid/hid-roccat-kone.c')
-rw-r--r-- | drivers/hid/hid-roccat-kone.c | 994 |
1 files changed, 994 insertions, 0 deletions
diff --git a/drivers/hid/hid-roccat-kone.c b/drivers/hid/hid-roccat-kone.c new file mode 100644 index 000000000000..66e694054ba2 --- /dev/null +++ b/drivers/hid/hid-roccat-kone.c | |||
@@ -0,0 +1,994 @@ | |||
1 | /* | ||
2 | * Roccat Kone 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 | * Roccat Kone is a gamer mouse which consists of a mouse part and a keyboard | ||
16 | * part. The keyboard part enables the mouse to execute stored macros with mixed | ||
17 | * key- and button-events. | ||
18 | * | ||
19 | * TODO implement on-the-fly polling-rate change | ||
20 | * The windows driver has the ability to change the polling rate of the | ||
21 | * device on the press of a mousebutton. | ||
22 | * Is it possible to remove and reinstall the urb in raw-event- or any | ||
23 | * other handler, or to defer this action to be executed somewhere else? | ||
24 | * | ||
25 | * TODO implement notification mechanism for overlong macro execution | ||
26 | * If user wants to execute an overlong macro only the names of macroset | ||
27 | * and macro are given. Should userland tap hidraw or is there an | ||
28 | * additional streaming mechanism? | ||
29 | * | ||
30 | * TODO is it possible to overwrite group for sysfs attributes via udev? | ||
31 | */ | ||
32 | |||
33 | #include <linux/device.h> | ||
34 | #include <linux/input.h> | ||
35 | #include <linux/hid.h> | ||
36 | #include <linux/usb.h> | ||
37 | #include <linux/module.h> | ||
38 | #include <linux/slab.h> | ||
39 | #include "hid-ids.h" | ||
40 | #include "hid-roccat-kone.h" | ||
41 | |||
42 | static void kone_set_settings_checksum(struct kone_settings *settings) | ||
43 | { | ||
44 | uint16_t checksum = 0; | ||
45 | unsigned char *address = (unsigned char *)settings; | ||
46 | int i; | ||
47 | |||
48 | for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address) | ||
49 | checksum += *address; | ||
50 | settings->checksum = cpu_to_le16(checksum); | ||
51 | } | ||
52 | |||
53 | /* | ||
54 | * Checks success after writing data to mouse | ||
55 | * On success returns 0 | ||
56 | * On failure returns errno | ||
57 | */ | ||
58 | static int kone_check_write(struct usb_device *usb_dev) | ||
59 | { | ||
60 | int len; | ||
61 | unsigned char *data; | ||
62 | |||
63 | data = kmalloc(1, GFP_KERNEL); | ||
64 | if (!data) | ||
65 | return -ENOMEM; | ||
66 | |||
67 | do { | ||
68 | /* | ||
69 | * Mouse needs 50 msecs until it says ok, but there are | ||
70 | * 30 more msecs needed for next write to work. | ||
71 | */ | ||
72 | msleep(80); | ||
73 | |||
74 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), | ||
75 | USB_REQ_CLEAR_FEATURE, | ||
76 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | | ||
77 | USB_DIR_IN, | ||
78 | kone_command_confirm_write, 0, data, 1, | ||
79 | USB_CTRL_SET_TIMEOUT); | ||
80 | |||
81 | if (len != 1) { | ||
82 | kfree(data); | ||
83 | return -EIO; | ||
84 | } | ||
85 | |||
86 | /* | ||
87 | * value of 3 seems to mean something like | ||
88 | * "not finished yet, but it looks good" | ||
89 | * So check again after a moment. | ||
90 | */ | ||
91 | } while (*data == 3); | ||
92 | |||
93 | if (*data == 1) { /* everything alright */ | ||
94 | kfree(data); | ||
95 | return 0; | ||
96 | } else { /* unknown answer */ | ||
97 | dev_err(&usb_dev->dev, "got retval %d when checking write\n", | ||
98 | *data); | ||
99 | kfree(data); | ||
100 | return -EIO; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | /* | ||
105 | * Reads settings from mouse and stores it in @buf | ||
106 | * @buf has to be alloced with GFP_KERNEL | ||
107 | * On success returns 0 | ||
108 | * On failure returns errno | ||
109 | */ | ||
110 | static int kone_get_settings(struct usb_device *usb_dev, | ||
111 | struct kone_settings *buf) | ||
112 | { | ||
113 | int len; | ||
114 | |||
115 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), | ||
116 | USB_REQ_CLEAR_FEATURE, | ||
117 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, | ||
118 | kone_command_settings, 0, buf, | ||
119 | sizeof(struct kone_settings), USB_CTRL_SET_TIMEOUT); | ||
120 | |||
121 | if (len != sizeof(struct kone_settings)) | ||
122 | return -EIO; | ||
123 | |||
124 | return 0; | ||
125 | } | ||
126 | |||
127 | /* | ||
128 | * Writes settings from @buf to mouse | ||
129 | * On success returns 0 | ||
130 | * On failure returns errno | ||
131 | */ | ||
132 | static int kone_set_settings(struct usb_device *usb_dev, | ||
133 | struct kone_settings const *settings) | ||
134 | { | ||
135 | int len; | ||
136 | |||
137 | len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), | ||
138 | USB_REQ_SET_CONFIGURATION, | ||
139 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, | ||
140 | kone_command_settings, 0, (char *)settings, | ||
141 | sizeof(struct kone_settings), | ||
142 | USB_CTRL_SET_TIMEOUT); | ||
143 | |||
144 | if (len != sizeof(struct kone_settings)) | ||
145 | return -EIO; | ||
146 | |||
147 | if (kone_check_write(usb_dev)) | ||
148 | return -EIO; | ||
149 | |||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | /* | ||
154 | * Reads profile data from mouse and stores it in @buf | ||
155 | * @number: profile number to read | ||
156 | * On success returns 0 | ||
157 | * On failure returns errno | ||
158 | */ | ||
159 | static int kone_get_profile(struct usb_device *usb_dev, | ||
160 | struct kone_profile *buf, int number) | ||
161 | { | ||
162 | int len; | ||
163 | |||
164 | if (number < 1 || number > 5) | ||
165 | return -EINVAL; | ||
166 | |||
167 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), | ||
168 | USB_REQ_CLEAR_FEATURE, | ||
169 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, | ||
170 | kone_command_profile, number, buf, | ||
171 | sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT); | ||
172 | |||
173 | if (len != sizeof(struct kone_profile)) | ||
174 | return -EIO; | ||
175 | |||
176 | return 0; | ||
177 | } | ||
178 | |||
179 | /* | ||
180 | * Writes profile data to mouse. | ||
181 | * @number: profile number to write | ||
182 | * On success returns 0 | ||
183 | * On failure returns errno | ||
184 | */ | ||
185 | static int kone_set_profile(struct usb_device *usb_dev, | ||
186 | struct kone_profile const *profile, int number) | ||
187 | { | ||
188 | int len; | ||
189 | |||
190 | if (number < 1 || number > 5) | ||
191 | return -EINVAL; | ||
192 | |||
193 | len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), | ||
194 | USB_REQ_SET_CONFIGURATION, | ||
195 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, | ||
196 | kone_command_profile, number, (char *)profile, | ||
197 | sizeof(struct kone_profile), | ||
198 | USB_CTRL_SET_TIMEOUT); | ||
199 | |||
200 | if (len != sizeof(struct kone_profile)) | ||
201 | return len; | ||
202 | |||
203 | if (kone_check_write(usb_dev)) | ||
204 | return -EIO; | ||
205 | |||
206 | return 0; | ||
207 | } | ||
208 | |||
209 | /* | ||
210 | * Reads value of "fast-clip-weight" and stores it in @result | ||
211 | * On success returns 0 | ||
212 | * On failure returns errno | ||
213 | */ | ||
214 | static int kone_get_weight(struct usb_device *usb_dev, int *result) | ||
215 | { | ||
216 | int len; | ||
217 | uint8_t *data; | ||
218 | |||
219 | data = kmalloc(1, GFP_KERNEL); | ||
220 | if (!data) | ||
221 | return -ENOMEM; | ||
222 | |||
223 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), | ||
224 | USB_REQ_CLEAR_FEATURE, | ||
225 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, | ||
226 | kone_command_weight, 0, data, 1, USB_CTRL_SET_TIMEOUT); | ||
227 | |||
228 | if (len != 1) { | ||
229 | kfree(data); | ||
230 | return -EIO; | ||
231 | } | ||
232 | *result = (int)*data; | ||
233 | kfree(data); | ||
234 | return 0; | ||
235 | } | ||
236 | |||
237 | /* | ||
238 | * Reads firmware_version of mouse and stores it in @result | ||
239 | * On success returns 0 | ||
240 | * On failure returns errno | ||
241 | */ | ||
242 | static int kone_get_firmware_version(struct usb_device *usb_dev, int *result) | ||
243 | { | ||
244 | int len; | ||
245 | unsigned char *data; | ||
246 | |||
247 | data = kmalloc(2, GFP_KERNEL); | ||
248 | if (!data) | ||
249 | return -ENOMEM; | ||
250 | |||
251 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), | ||
252 | USB_REQ_CLEAR_FEATURE, | ||
253 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, | ||
254 | kone_command_firmware_version, 0, data, 2, | ||
255 | USB_CTRL_SET_TIMEOUT); | ||
256 | |||
257 | if (len != 2) { | ||
258 | kfree(data); | ||
259 | return -EIO; | ||
260 | } | ||
261 | *result = le16_to_cpu(*data); | ||
262 | kfree(data); | ||
263 | return 0; | ||
264 | } | ||
265 | |||
266 | static ssize_t kone_sysfs_read_settings(struct kobject *kobj, | ||
267 | struct bin_attribute *attr, char *buf, | ||
268 | loff_t off, size_t count) { | ||
269 | struct device *dev = container_of(kobj, struct device, kobj); | ||
270 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
271 | |||
272 | if (off >= sizeof(struct kone_settings)) | ||
273 | return 0; | ||
274 | |||
275 | if (off + count > sizeof(struct kone_settings)) | ||
276 | count = sizeof(struct kone_settings) - off; | ||
277 | |||
278 | mutex_lock(&kone->kone_lock); | ||
279 | memcpy(buf, &kone->settings + off, count); | ||
280 | mutex_unlock(&kone->kone_lock); | ||
281 | |||
282 | return count; | ||
283 | } | ||
284 | |||
285 | /* | ||
286 | * Writing settings automatically activates startup_profile. | ||
287 | * This function keeps values in kone_device up to date and assumes that in | ||
288 | * case of error the old data is still valid | ||
289 | */ | ||
290 | static ssize_t kone_sysfs_write_settings(struct kobject *kobj, | ||
291 | struct bin_attribute *attr, char *buf, | ||
292 | loff_t off, size_t count) { | ||
293 | struct device *dev = container_of(kobj, struct device, kobj); | ||
294 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
295 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | ||
296 | int retval = 0, difference; | ||
297 | |||
298 | /* I need to get my data in one piece */ | ||
299 | if (off != 0 || count != sizeof(struct kone_settings)) | ||
300 | return -EINVAL; | ||
301 | |||
302 | mutex_lock(&kone->kone_lock); | ||
303 | difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings)); | ||
304 | if (difference) { | ||
305 | retval = kone_set_settings(usb_dev, | ||
306 | (struct kone_settings const *)buf); | ||
307 | if (!retval) | ||
308 | memcpy(&kone->settings, buf, | ||
309 | sizeof(struct kone_settings)); | ||
310 | } | ||
311 | mutex_unlock(&kone->kone_lock); | ||
312 | |||
313 | if (retval) | ||
314 | return retval; | ||
315 | |||
316 | /* | ||
317 | * If we get here, treat settings as okay and update actual values | ||
318 | * according to startup_profile | ||
319 | */ | ||
320 | kone->actual_profile = kone->settings.startup_profile; | ||
321 | kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi; | ||
322 | |||
323 | return sizeof(struct kone_settings); | ||
324 | } | ||
325 | |||
326 | static ssize_t kone_sysfs_read_profilex(struct kobject *kobj, | ||
327 | struct bin_attribute *attr, char *buf, | ||
328 | loff_t off, size_t count, int number) { | ||
329 | struct device *dev = container_of(kobj, struct device, kobj); | ||
330 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
331 | |||
332 | if (off >= sizeof(struct kone_profile)) | ||
333 | return 0; | ||
334 | |||
335 | if (off + count > sizeof(struct kone_profile)) | ||
336 | count = sizeof(struct kone_profile) - off; | ||
337 | |||
338 | mutex_lock(&kone->kone_lock); | ||
339 | memcpy(buf, &kone->profiles[number - 1], sizeof(struct kone_profile)); | ||
340 | mutex_unlock(&kone->kone_lock); | ||
341 | |||
342 | return count; | ||
343 | } | ||
344 | |||
345 | static ssize_t kone_sysfs_read_profile1(struct kobject *kobj, | ||
346 | struct bin_attribute *attr, char *buf, | ||
347 | loff_t off, size_t count) { | ||
348 | return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1); | ||
349 | } | ||
350 | |||
351 | static ssize_t kone_sysfs_read_profile2(struct kobject *kobj, | ||
352 | struct bin_attribute *attr, char *buf, | ||
353 | loff_t off, size_t count) { | ||
354 | return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2); | ||
355 | } | ||
356 | |||
357 | static ssize_t kone_sysfs_read_profile3(struct kobject *kobj, | ||
358 | struct bin_attribute *attr, char *buf, | ||
359 | loff_t off, size_t count) { | ||
360 | return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3); | ||
361 | } | ||
362 | |||
363 | static ssize_t kone_sysfs_read_profile4(struct kobject *kobj, | ||
364 | struct bin_attribute *attr, char *buf, | ||
365 | loff_t off, size_t count) { | ||
366 | return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4); | ||
367 | } | ||
368 | |||
369 | static ssize_t kone_sysfs_read_profile5(struct kobject *kobj, | ||
370 | struct bin_attribute *attr, char *buf, | ||
371 | loff_t off, size_t count) { | ||
372 | return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5); | ||
373 | } | ||
374 | |||
375 | /* Writes data only if different to stored data */ | ||
376 | static ssize_t kone_sysfs_write_profilex(struct kobject *kobj, | ||
377 | struct bin_attribute *attr, char *buf, | ||
378 | loff_t off, size_t count, int number) { | ||
379 | struct device *dev = container_of(kobj, struct device, kobj); | ||
380 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
381 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | ||
382 | struct kone_profile *profile; | ||
383 | int retval = 0, difference; | ||
384 | |||
385 | /* I need to get my data in one piece */ | ||
386 | if (off != 0 || count != sizeof(struct kone_profile)) | ||
387 | return -EINVAL; | ||
388 | |||
389 | profile = &kone->profiles[number - 1]; | ||
390 | |||
391 | mutex_lock(&kone->kone_lock); | ||
392 | difference = memcmp(buf, profile, sizeof(struct kone_profile)); | ||
393 | if (difference) { | ||
394 | retval = kone_set_profile(usb_dev, | ||
395 | (struct kone_profile const *)buf, number); | ||
396 | if (!retval) | ||
397 | memcpy(profile, buf, sizeof(struct kone_profile)); | ||
398 | } | ||
399 | mutex_unlock(&kone->kone_lock); | ||
400 | |||
401 | if (retval) | ||
402 | return retval; | ||
403 | |||
404 | return sizeof(struct kone_profile); | ||
405 | } | ||
406 | |||
407 | static ssize_t kone_sysfs_write_profile1(struct kobject *kobj, | ||
408 | struct bin_attribute *attr, char *buf, | ||
409 | loff_t off, size_t count) { | ||
410 | return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1); | ||
411 | } | ||
412 | |||
413 | static ssize_t kone_sysfs_write_profile2(struct kobject *kobj, | ||
414 | struct bin_attribute *attr, char *buf, | ||
415 | loff_t off, size_t count) { | ||
416 | return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2); | ||
417 | } | ||
418 | |||
419 | static ssize_t kone_sysfs_write_profile3(struct kobject *kobj, | ||
420 | struct bin_attribute *attr, char *buf, | ||
421 | loff_t off, size_t count) { | ||
422 | return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3); | ||
423 | } | ||
424 | |||
425 | static ssize_t kone_sysfs_write_profile4(struct kobject *kobj, | ||
426 | struct bin_attribute *attr, char *buf, | ||
427 | loff_t off, size_t count) { | ||
428 | return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4); | ||
429 | } | ||
430 | |||
431 | static ssize_t kone_sysfs_write_profile5(struct kobject *kobj, | ||
432 | struct bin_attribute *attr, char *buf, | ||
433 | loff_t off, size_t count) { | ||
434 | return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5); | ||
435 | } | ||
436 | |||
437 | static ssize_t kone_sysfs_show_actual_profile(struct device *dev, | ||
438 | struct device_attribute *attr, char *buf) | ||
439 | { | ||
440 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
441 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile); | ||
442 | } | ||
443 | |||
444 | static ssize_t kone_sysfs_show_actual_dpi(struct device *dev, | ||
445 | struct device_attribute *attr, char *buf) | ||
446 | { | ||
447 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
448 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi); | ||
449 | } | ||
450 | |||
451 | /* weight is read each time, since we don't get informed when it's changed */ | ||
452 | static ssize_t kone_sysfs_show_weight(struct device *dev, | ||
453 | struct device_attribute *attr, char *buf) | ||
454 | { | ||
455 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
456 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | ||
457 | int weight = 0; | ||
458 | int retval; | ||
459 | |||
460 | mutex_lock(&kone->kone_lock); | ||
461 | retval = kone_get_weight(usb_dev, &weight); | ||
462 | mutex_unlock(&kone->kone_lock); | ||
463 | |||
464 | if (retval) | ||
465 | return retval; | ||
466 | return snprintf(buf, PAGE_SIZE, "%d\n", weight); | ||
467 | } | ||
468 | |||
469 | static ssize_t kone_sysfs_show_firmware_version(struct device *dev, | ||
470 | struct device_attribute *attr, char *buf) | ||
471 | { | ||
472 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
473 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version); | ||
474 | } | ||
475 | |||
476 | static ssize_t kone_sysfs_show_tcu(struct device *dev, | ||
477 | struct device_attribute *attr, char *buf) | ||
478 | { | ||
479 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
480 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu); | ||
481 | } | ||
482 | |||
483 | static int kone_tcu_command(struct usb_device *usb_dev, int number) | ||
484 | { | ||
485 | int len; | ||
486 | char *value; | ||
487 | |||
488 | value = kmalloc(1, GFP_KERNEL); | ||
489 | if (!value) | ||
490 | return -ENOMEM; | ||
491 | |||
492 | *value = number; | ||
493 | |||
494 | len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), | ||
495 | USB_REQ_SET_CONFIGURATION, | ||
496 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, | ||
497 | kone_command_calibrate, 0, value, 1, | ||
498 | USB_CTRL_SET_TIMEOUT); | ||
499 | |||
500 | kfree(value); | ||
501 | return ((len != 1) ? -EIO : 0); | ||
502 | } | ||
503 | |||
504 | /* | ||
505 | * Calibrating the tcu is the only action that changes settings data inside the | ||
506 | * mouse, so this data needs to be reread | ||
507 | */ | ||
508 | static ssize_t kone_sysfs_set_tcu(struct device *dev, | ||
509 | struct device_attribute *attr, char const *buf, size_t size) | ||
510 | { | ||
511 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
512 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | ||
513 | int retval; | ||
514 | unsigned long state; | ||
515 | |||
516 | retval = strict_strtoul(buf, 10, &state); | ||
517 | if (retval) | ||
518 | return retval; | ||
519 | |||
520 | if (state != 0 && state != 1) | ||
521 | return -EINVAL; | ||
522 | |||
523 | mutex_lock(&kone->kone_lock); | ||
524 | |||
525 | if (state == 1) { /* state activate */ | ||
526 | retval = kone_tcu_command(usb_dev, 1); | ||
527 | if (retval) | ||
528 | goto exit_unlock; | ||
529 | retval = kone_tcu_command(usb_dev, 2); | ||
530 | if (retval) | ||
531 | goto exit_unlock; | ||
532 | ssleep(5); /* tcu needs this time for calibration */ | ||
533 | retval = kone_tcu_command(usb_dev, 3); | ||
534 | if (retval) | ||
535 | goto exit_unlock; | ||
536 | retval = kone_tcu_command(usb_dev, 0); | ||
537 | if (retval) | ||
538 | goto exit_unlock; | ||
539 | retval = kone_tcu_command(usb_dev, 4); | ||
540 | if (retval) | ||
541 | goto exit_unlock; | ||
542 | /* | ||
543 | * Kone needs this time to settle things. | ||
544 | * Reading settings too early will result in invalid data. | ||
545 | * Roccat's driver waits 1 sec, maybe this time could be | ||
546 | * shortened. | ||
547 | */ | ||
548 | ssleep(1); | ||
549 | } | ||
550 | |||
551 | /* calibration changes values in settings, so reread */ | ||
552 | retval = kone_get_settings(usb_dev, &kone->settings); | ||
553 | if (retval) | ||
554 | goto exit_no_settings; | ||
555 | |||
556 | /* only write settings back if activation state is different */ | ||
557 | if (kone->settings.tcu != state) { | ||
558 | kone->settings.tcu = state; | ||
559 | kone_set_settings_checksum(&kone->settings); | ||
560 | |||
561 | retval = kone_set_settings(usb_dev, &kone->settings); | ||
562 | if (retval) { | ||
563 | dev_err(&usb_dev->dev, "couldn't set tcu state\n"); | ||
564 | /* | ||
565 | * try to reread valid settings into buffer overwriting | ||
566 | * first error code | ||
567 | */ | ||
568 | retval = kone_get_settings(usb_dev, &kone->settings); | ||
569 | if (retval) | ||
570 | goto exit_no_settings; | ||
571 | goto exit_unlock; | ||
572 | } | ||
573 | } | ||
574 | |||
575 | retval = size; | ||
576 | exit_no_settings: | ||
577 | dev_err(&usb_dev->dev, "couldn't read settings\n"); | ||
578 | exit_unlock: | ||
579 | mutex_unlock(&kone->kone_lock); | ||
580 | return retval; | ||
581 | } | ||
582 | |||
583 | static ssize_t kone_sysfs_show_startup_profile(struct device *dev, | ||
584 | struct device_attribute *attr, char *buf) | ||
585 | { | ||
586 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
587 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile); | ||
588 | } | ||
589 | |||
590 | static ssize_t kone_sysfs_set_startup_profile(struct device *dev, | ||
591 | struct device_attribute *attr, char const *buf, size_t size) | ||
592 | { | ||
593 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); | ||
594 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | ||
595 | int retval; | ||
596 | unsigned long new_startup_profile; | ||
597 | |||
598 | retval = strict_strtoul(buf, 10, &new_startup_profile); | ||
599 | if (retval) | ||
600 | return retval; | ||
601 | |||
602 | if (new_startup_profile < 1 || new_startup_profile > 5) | ||
603 | return -EINVAL; | ||
604 | |||
605 | mutex_lock(&kone->kone_lock); | ||
606 | |||
607 | kone->settings.startup_profile = new_startup_profile; | ||
608 | kone_set_settings_checksum(&kone->settings); | ||
609 | |||
610 | retval = kone_set_settings(usb_dev, &kone->settings); | ||
611 | |||
612 | mutex_unlock(&kone->kone_lock); | ||
613 | |||
614 | if (retval) | ||
615 | return retval; | ||
616 | |||
617 | /* changing the startup profile immediately activates this profile */ | ||
618 | kone->actual_profile = new_startup_profile; | ||
619 | kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi; | ||
620 | |||
621 | return size; | ||
622 | } | ||
623 | |||
624 | /* | ||
625 | * This file is used by userland software to find devices that are handled by | ||
626 | * this driver. This provides a consistent way for actual and older kernels | ||
627 | * where this driver replaced usbhid instead of generic-usb. | ||
628 | * Driver capabilities are determined by version number. | ||
629 | */ | ||
630 | static ssize_t kone_sysfs_show_driver_version(struct device *dev, | ||
631 | struct device_attribute *attr, char *buf) | ||
632 | { | ||
633 | return snprintf(buf, PAGE_SIZE, ROCCAT_KONE_DRIVER_VERSION "\n"); | ||
634 | } | ||
635 | |||
636 | /* | ||
637 | * Read actual dpi settings. | ||
638 | * Returns raw value for further processing. Refer to enum kone_polling_rates to | ||
639 | * get real value. | ||
640 | */ | ||
641 | static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL); | ||
642 | |||
643 | static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL); | ||
644 | |||
645 | /* | ||
646 | * The mouse can be equipped with one of four supplied weights from 5 to 20 | ||
647 | * grams which are recognized and its value can be read out. | ||
648 | * This returns the raw value reported by the mouse for easy evaluation by | ||
649 | * software. Refer to enum kone_weights to get corresponding real weight. | ||
650 | */ | ||
651 | static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL); | ||
652 | |||
653 | /* | ||
654 | * Prints firmware version stored in mouse as integer. | ||
655 | * The raw value reported by the mouse is returned for easy evaluation, to get | ||
656 | * the real version number the decimal point has to be shifted 2 positions to | ||
657 | * the left. E.g. a value of 138 means 1.38. | ||
658 | */ | ||
659 | static DEVICE_ATTR(firmware_version, 0440, | ||
660 | kone_sysfs_show_firmware_version, NULL); | ||
661 | |||
662 | /* | ||
663 | * Prints state of Tracking Control Unit as number where 0 = off and 1 = on | ||
664 | * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu | ||
665 | */ | ||
666 | static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu); | ||
667 | |||
668 | /* Prints and takes the number of the profile the mouse starts with */ | ||
669 | static DEVICE_ATTR(startup_profile, 0660, | ||
670 | kone_sysfs_show_startup_profile, | ||
671 | kone_sysfs_set_startup_profile); | ||
672 | |||
673 | static DEVICE_ATTR(kone_driver_version, 0440, | ||
674 | kone_sysfs_show_driver_version, NULL); | ||
675 | |||
676 | static struct attribute *kone_attributes[] = { | ||
677 | &dev_attr_actual_dpi.attr, | ||
678 | &dev_attr_actual_profile.attr, | ||
679 | &dev_attr_weight.attr, | ||
680 | &dev_attr_firmware_version.attr, | ||
681 | &dev_attr_tcu.attr, | ||
682 | &dev_attr_startup_profile.attr, | ||
683 | &dev_attr_kone_driver_version.attr, | ||
684 | NULL | ||
685 | }; | ||
686 | |||
687 | static struct attribute_group kone_attribute_group = { | ||
688 | .attrs = kone_attributes | ||
689 | }; | ||
690 | |||
691 | static struct bin_attribute kone_settings_attr = { | ||
692 | .attr = { .name = "settings", .mode = 0660 }, | ||
693 | .size = sizeof(struct kone_settings), | ||
694 | .read = kone_sysfs_read_settings, | ||
695 | .write = kone_sysfs_write_settings | ||
696 | }; | ||
697 | |||
698 | static struct bin_attribute kone_profile1_attr = { | ||
699 | .attr = { .name = "profile1", .mode = 0660 }, | ||
700 | .size = sizeof(struct kone_profile), | ||
701 | .read = kone_sysfs_read_profile1, | ||
702 | .write = kone_sysfs_write_profile1 | ||
703 | }; | ||
704 | |||
705 | static struct bin_attribute kone_profile2_attr = { | ||
706 | .attr = { .name = "profile2", .mode = 0660 }, | ||
707 | .size = sizeof(struct kone_profile), | ||
708 | .read = kone_sysfs_read_profile2, | ||
709 | .write = kone_sysfs_write_profile2 | ||
710 | }; | ||
711 | |||
712 | static struct bin_attribute kone_profile3_attr = { | ||
713 | .attr = { .name = "profile3", .mode = 0660 }, | ||
714 | .size = sizeof(struct kone_profile), | ||
715 | .read = kone_sysfs_read_profile3, | ||
716 | .write = kone_sysfs_write_profile3 | ||
717 | }; | ||
718 | |||
719 | static struct bin_attribute kone_profile4_attr = { | ||
720 | .attr = { .name = "profile4", .mode = 0660 }, | ||
721 | .size = sizeof(struct kone_profile), | ||
722 | .read = kone_sysfs_read_profile4, | ||
723 | .write = kone_sysfs_write_profile4 | ||
724 | }; | ||
725 | |||
726 | static struct bin_attribute kone_profile5_attr = { | ||
727 | .attr = { .name = "profile5", .mode = 0660 }, | ||
728 | .size = sizeof(struct kone_profile), | ||
729 | .read = kone_sysfs_read_profile5, | ||
730 | .write = kone_sysfs_write_profile5 | ||
731 | }; | ||
732 | |||
733 | static int kone_create_sysfs_attributes(struct usb_interface *intf) | ||
734 | { | ||
735 | int retval; | ||
736 | |||
737 | retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group); | ||
738 | if (retval) | ||
739 | goto exit_1; | ||
740 | |||
741 | retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr); | ||
742 | if (retval) | ||
743 | goto exit_2; | ||
744 | |||
745 | retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr); | ||
746 | if (retval) | ||
747 | goto exit_3; | ||
748 | |||
749 | retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr); | ||
750 | if (retval) | ||
751 | goto exit_4; | ||
752 | |||
753 | retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr); | ||
754 | if (retval) | ||
755 | goto exit_5; | ||
756 | |||
757 | retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr); | ||
758 | if (retval) | ||
759 | goto exit_6; | ||
760 | |||
761 | retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr); | ||
762 | if (retval) | ||
763 | goto exit_7; | ||
764 | |||
765 | return 0; | ||
766 | |||
767 | exit_7: | ||
768 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr); | ||
769 | exit_6: | ||
770 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr); | ||
771 | exit_5: | ||
772 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr); | ||
773 | exit_4: | ||
774 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr); | ||
775 | exit_3: | ||
776 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr); | ||
777 | exit_2: | ||
778 | sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group); | ||
779 | exit_1: | ||
780 | return retval; | ||
781 | } | ||
782 | |||
783 | static void kone_remove_sysfs_attributes(struct usb_interface *intf) | ||
784 | { | ||
785 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile5_attr); | ||
786 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr); | ||
787 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr); | ||
788 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr); | ||
789 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr); | ||
790 | sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr); | ||
791 | sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group); | ||
792 | } | ||
793 | |||
794 | static int kone_init_kone_device_struct(struct usb_device *usb_dev, | ||
795 | struct kone_device *kone) | ||
796 | { | ||
797 | uint i; | ||
798 | int retval; | ||
799 | |||
800 | mutex_init(&kone->kone_lock); | ||
801 | |||
802 | for (i = 0; i < 5; ++i) { | ||
803 | retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1); | ||
804 | if (retval) | ||
805 | return retval; | ||
806 | } | ||
807 | |||
808 | retval = kone_get_settings(usb_dev, &kone->settings); | ||
809 | if (retval) | ||
810 | return retval; | ||
811 | |||
812 | retval = kone_get_firmware_version(usb_dev, &kone->firmware_version); | ||
813 | if (retval) | ||
814 | return retval; | ||
815 | |||
816 | kone->actual_profile = kone->settings.startup_profile; | ||
817 | kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi; | ||
818 | |||
819 | return 0; | ||
820 | } | ||
821 | |||
822 | /* | ||
823 | * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to | ||
824 | * mousepart if usb_hid is compiled into the kernel and kone is compiled as | ||
825 | * module. | ||
826 | * Secial behaviour is bound only to mousepart since only mouseevents contain | ||
827 | * additional notifications. | ||
828 | */ | ||
829 | static int kone_init_specials(struct hid_device *hdev) | ||
830 | { | ||
831 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | ||
832 | struct usb_device *usb_dev = interface_to_usbdev(intf); | ||
833 | struct kone_device *kone; | ||
834 | int retval; | ||
835 | |||
836 | if (intf->cur_altsetting->desc.bInterfaceProtocol | ||
837 | == USB_INTERFACE_PROTOCOL_MOUSE) { | ||
838 | |||
839 | kone = kzalloc(sizeof(*kone), GFP_KERNEL); | ||
840 | if (!kone) { | ||
841 | dev_err(&hdev->dev, "can't alloc device descriptor\n"); | ||
842 | return -ENOMEM; | ||
843 | } | ||
844 | hid_set_drvdata(hdev, kone); | ||
845 | |||
846 | retval = kone_init_kone_device_struct(usb_dev, kone); | ||
847 | if (retval) { | ||
848 | dev_err(&hdev->dev, | ||
849 | "couldn't init struct kone_device\n"); | ||
850 | goto exit_free; | ||
851 | } | ||
852 | retval = kone_create_sysfs_attributes(intf); | ||
853 | if (retval) { | ||
854 | dev_err(&hdev->dev, "cannot create sysfs files\n"); | ||
855 | goto exit_free; | ||
856 | } | ||
857 | } else { | ||
858 | hid_set_drvdata(hdev, NULL); | ||
859 | } | ||
860 | |||
861 | return 0; | ||
862 | exit_free: | ||
863 | kfree(kone); | ||
864 | return retval; | ||
865 | } | ||
866 | |||
867 | |||
868 | static void kone_remove_specials(struct hid_device *hdev) | ||
869 | { | ||
870 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | ||
871 | |||
872 | if (intf->cur_altsetting->desc.bInterfaceProtocol | ||
873 | == USB_INTERFACE_PROTOCOL_MOUSE) { | ||
874 | kone_remove_sysfs_attributes(intf); | ||
875 | kfree(hid_get_drvdata(hdev)); | ||
876 | } | ||
877 | } | ||
878 | |||
879 | static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id) | ||
880 | { | ||
881 | int retval; | ||
882 | |||
883 | retval = hid_parse(hdev); | ||
884 | if (retval) { | ||
885 | dev_err(&hdev->dev, "parse failed\n"); | ||
886 | goto exit; | ||
887 | } | ||
888 | |||
889 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | ||
890 | if (retval) { | ||
891 | dev_err(&hdev->dev, "hw start failed\n"); | ||
892 | goto exit; | ||
893 | } | ||
894 | |||
895 | retval = kone_init_specials(hdev); | ||
896 | if (retval) { | ||
897 | dev_err(&hdev->dev, "couldn't install mouse\n"); | ||
898 | goto exit_stop; | ||
899 | } | ||
900 | |||
901 | return 0; | ||
902 | |||
903 | exit_stop: | ||
904 | hid_hw_stop(hdev); | ||
905 | exit: | ||
906 | return retval; | ||
907 | } | ||
908 | |||
909 | static void kone_remove(struct hid_device *hdev) | ||
910 | { | ||
911 | kone_remove_specials(hdev); | ||
912 | hid_hw_stop(hdev); | ||
913 | } | ||
914 | |||
915 | /* handle special events and keep actual profile and dpi values up to date */ | ||
916 | static void kone_keep_values_up_to_date(struct kone_device *kone, | ||
917 | struct kone_mouse_event const *event) | ||
918 | { | ||
919 | switch (event->event) { | ||
920 | case kone_mouse_event_switch_profile: | ||
921 | case kone_mouse_event_osd_profile: | ||
922 | kone->actual_profile = event->value; | ||
923 | kone->actual_dpi = kone->profiles[kone->actual_profile - 1]. | ||
924 | startup_dpi; | ||
925 | break; | ||
926 | case kone_mouse_event_switch_dpi: | ||
927 | case kone_mouse_event_osd_dpi: | ||
928 | kone->actual_dpi = event->value; | ||
929 | break; | ||
930 | } | ||
931 | } | ||
932 | |||
933 | /* | ||
934 | * Is called for keyboard- and mousepart. | ||
935 | * Only mousepart gets informations about special events in its extended event | ||
936 | * structure. | ||
937 | */ | ||
938 | static int kone_raw_event(struct hid_device *hdev, struct hid_report *report, | ||
939 | u8 *data, int size) | ||
940 | { | ||
941 | struct kone_device *kone = hid_get_drvdata(hdev); | ||
942 | struct kone_mouse_event *event = (struct kone_mouse_event *)data; | ||
943 | |||
944 | /* keyboard events are always processed by default handler */ | ||
945 | if (size != sizeof(struct kone_mouse_event)) | ||
946 | return 0; | ||
947 | |||
948 | /* | ||
949 | * Firmware 1.38 introduced new behaviour for tilt and special buttons. | ||
950 | * Pressed button is reported in each movement event. | ||
951 | * Workaround sends only one event per press. | ||
952 | */ | ||
953 | if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5)) | ||
954 | memcpy(&kone->last_mouse_event, event, | ||
955 | sizeof(struct kone_mouse_event)); | ||
956 | else | ||
957 | memset(&event->tilt, 0, 5); | ||
958 | |||
959 | kone_keep_values_up_to_date(kone, event); | ||
960 | |||
961 | return 0; /* always do further processing */ | ||
962 | } | ||
963 | |||
964 | static const struct hid_device_id kone_devices[] = { | ||
965 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) }, | ||
966 | { } | ||
967 | }; | ||
968 | |||
969 | MODULE_DEVICE_TABLE(hid, kone_devices); | ||
970 | |||
971 | static struct hid_driver kone_driver = { | ||
972 | .name = "kone", | ||
973 | .id_table = kone_devices, | ||
974 | .probe = kone_probe, | ||
975 | .remove = kone_remove, | ||
976 | .raw_event = kone_raw_event | ||
977 | }; | ||
978 | |||
979 | static int __init kone_init(void) | ||
980 | { | ||
981 | return hid_register_driver(&kone_driver); | ||
982 | } | ||
983 | |||
984 | static void __exit kone_exit(void) | ||
985 | { | ||
986 | hid_unregister_driver(&kone_driver); | ||
987 | } | ||
988 | |||
989 | module_init(kone_init); | ||
990 | module_exit(kone_exit); | ||
991 | |||
992 | MODULE_AUTHOR("Stefan Achatz"); | ||
993 | MODULE_DESCRIPTION("USB Roccat Kone driver"); | ||
994 | MODULE_LICENSE("GPL v2"); | ||