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