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