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