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