blob: e2072afb34bbb7dce51c9bdb3b44f5b2867e9bd3 [file] [log] [blame]
Stefan Achatz14bf62c2010-03-18 16:19:43 +01001/*
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 *
Stefan Achatz14bf62c2010-03-18 16:19:43 +010025 * 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>
Stefan Achatz14bf62c2010-03-18 16:19:43 +010031#include <linux/module.h>
Tejun Heoed28f042010-03-30 02:52:41 +090032#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010033#include <linux/hid-roccat.h>
Stefan Achatz14bf62c2010-03-18 16:19:43 +010034#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010035#include "hid-roccat-common.h"
Stefan Achatz14bf62c2010-03-18 16:19:43 +010036#include "hid-roccat-kone.h"
37
Stefan Achatz14a057f2010-11-26 19:57:38 +000038static uint profile_numbers[5] = {0, 1, 2, 3, 4};
39
Stefan Achatzbd9c35d2011-08-27 15:24:48 +020040static 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
Stefan Achatz3200a6a2011-08-27 15:24:51 +020046static void kone_profile_report(struct kone_device *kone, uint new_profile)
47{
48 struct kone_roccat_report roccat_report;
49 roccat_report.event = kone_mouse_event_switch_profile;
50 roccat_report.value = new_profile;
51 roccat_report.key = 0;
52 roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
53}
54
Stefan Achatz1edd5b42011-06-01 15:54:17 +020055static int kone_receive(struct usb_device *usb_dev, uint usb_command,
56 void *data, uint size)
57{
58 char *buf;
59 int len;
60
61 buf = kmalloc(size, GFP_KERNEL);
62 if (buf == NULL)
63 return -ENOMEM;
64
65 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
66 HID_REQ_GET_REPORT,
67 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
68 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
69
70 memcpy(data, buf, size);
71 kfree(buf);
72 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
73}
74
75static int kone_send(struct usb_device *usb_dev, uint usb_command,
76 void const *data, uint size)
77{
78 char *buf;
79 int len;
80
81 buf = kmalloc(size, GFP_KERNEL);
82 if (buf == NULL)
83 return -ENOMEM;
84
85 memcpy(buf, data, size);
86
87 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
88 HID_REQ_SET_REPORT,
89 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
90 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
91
92 kfree(buf);
93 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
94}
95
Stefan Achatz5012aad2010-11-26 19:57:33 +000096/* kone_class is used for creating sysfs attributes via roccat char device */
97static struct class *kone_class;
98
Stefan Achatz14bf62c2010-03-18 16:19:43 +010099static void kone_set_settings_checksum(struct kone_settings *settings)
100{
101 uint16_t checksum = 0;
102 unsigned char *address = (unsigned char *)settings;
103 int i;
104
105 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
106 checksum += *address;
107 settings->checksum = cpu_to_le16(checksum);
108}
109
110/*
111 * Checks success after writing data to mouse
112 * On success returns 0
113 * On failure returns errno
114 */
115static int kone_check_write(struct usb_device *usb_dev)
116{
Stefan Achatz5772f632011-01-30 13:38:23 +0100117 int retval;
118 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100119
120 do {
121 /*
122 * Mouse needs 50 msecs until it says ok, but there are
123 * 30 more msecs needed for next write to work.
124 */
125 msleep(80);
126
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200127 retval = kone_receive(usb_dev,
Stefan Achatz5772f632011-01-30 13:38:23 +0100128 kone_command_confirm_write, &data, 1);
129 if (retval)
130 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100131
132 /*
133 * value of 3 seems to mean something like
134 * "not finished yet, but it looks good"
135 * So check again after a moment.
136 */
Stefan Achatz5772f632011-01-30 13:38:23 +0100137 } while (data == 3);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100138
Stefan Achatz5772f632011-01-30 13:38:23 +0100139 if (data == 1) /* everything alright */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100140 return 0;
Stefan Achatz5772f632011-01-30 13:38:23 +0100141
142 /* unknown answer */
143 hid_err(usb_dev, "got retval %d when checking write\n", data);
144 return -EIO;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100145}
146
147/*
148 * Reads settings from mouse and stores it in @buf
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100149 * On success returns 0
150 * On failure returns errno
151 */
152static int kone_get_settings(struct usb_device *usb_dev,
153 struct kone_settings *buf)
154{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200155 return kone_receive(usb_dev, kone_command_settings, buf,
Stefan Achatz5772f632011-01-30 13:38:23 +0100156 sizeof(struct kone_settings));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100157}
158
159/*
160 * Writes settings from @buf to mouse
161 * On success returns 0
162 * On failure returns errno
163 */
164static int kone_set_settings(struct usb_device *usb_dev,
165 struct kone_settings const *settings)
166{
Stefan Achatz5772f632011-01-30 13:38:23 +0100167 int retval;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200168 retval = kone_send(usb_dev, kone_command_settings,
Stefan Achatz5772f632011-01-30 13:38:23 +0100169 settings, sizeof(struct kone_settings));
170 if (retval)
171 return retval;
172 return kone_check_write(usb_dev);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100173}
174
175/*
176 * Reads profile data from mouse and stores it in @buf
177 * @number: profile number to read
178 * On success returns 0
179 * On failure returns errno
180 */
181static int kone_get_profile(struct usb_device *usb_dev,
182 struct kone_profile *buf, int number)
183{
184 int len;
185
186 if (number < 1 || number > 5)
187 return -EINVAL;
188
189 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
190 USB_REQ_CLEAR_FEATURE,
191 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
192 kone_command_profile, number, buf,
193 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
194
195 if (len != sizeof(struct kone_profile))
196 return -EIO;
197
198 return 0;
199}
200
201/*
202 * Writes profile data to mouse.
203 * @number: profile number to write
204 * On success returns 0
205 * On failure returns errno
206 */
207static int kone_set_profile(struct usb_device *usb_dev,
208 struct kone_profile const *profile, int number)
209{
210 int len;
211
212 if (number < 1 || number > 5)
213 return -EINVAL;
214
215 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
216 USB_REQ_SET_CONFIGURATION,
217 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Stefan Achatz5772f632011-01-30 13:38:23 +0100218 kone_command_profile, number, (void *)profile,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100219 sizeof(struct kone_profile),
220 USB_CTRL_SET_TIMEOUT);
221
222 if (len != sizeof(struct kone_profile))
223 return len;
224
225 if (kone_check_write(usb_dev))
226 return -EIO;
227
228 return 0;
229}
230
231/*
232 * Reads value of "fast-clip-weight" and stores it in @result
233 * On success returns 0
234 * On failure returns errno
235 */
236static int kone_get_weight(struct usb_device *usb_dev, int *result)
237{
Stefan Achatz5772f632011-01-30 13:38:23 +0100238 int retval;
239 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100240
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200241 retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100242
Stefan Achatz5772f632011-01-30 13:38:23 +0100243 if (retval)
244 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100245
Stefan Achatz5772f632011-01-30 13:38:23 +0100246 *result = (int)data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100247 return 0;
248}
249
250/*
251 * Reads firmware_version of mouse and stores it in @result
252 * On success returns 0
253 * On failure returns errno
254 */
255static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
256{
Stefan Achatz5772f632011-01-30 13:38:23 +0100257 int retval;
258 uint16_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100259
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200260 retval = kone_receive(usb_dev, kone_command_firmware_version,
Stefan Achatz5772f632011-01-30 13:38:23 +0100261 &data, 2);
262 if (retval)
263 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100264
Stefan Achatz5772f632011-01-30 13:38:23 +0100265 *result = le16_to_cpu(data);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100266 return 0;
267}
268
Stephen Rothwell5f277622010-05-21 16:15:32 +1000269static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100270 struct bin_attribute *attr, char *buf,
271 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000272 struct device *dev =
273 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100274 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
275
276 if (off >= sizeof(struct kone_settings))
277 return 0;
278
279 if (off + count > sizeof(struct kone_settings))
280 count = sizeof(struct kone_settings) - off;
281
282 mutex_lock(&kone->kone_lock);
Stefan Achatzcab6b162010-06-20 19:19:06 +0200283 memcpy(buf, ((char const *)&kone->settings) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100284 mutex_unlock(&kone->kone_lock);
285
286 return count;
287}
288
289/*
290 * Writing settings automatically activates startup_profile.
291 * This function keeps values in kone_device up to date and assumes that in
292 * case of error the old data is still valid
293 */
Stephen Rothwell5f277622010-05-21 16:15:32 +1000294static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100295 struct bin_attribute *attr, char *buf,
296 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000297 struct device *dev =
298 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100299 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
300 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200301 int retval = 0, difference, old_profile;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100302
303 /* I need to get my data in one piece */
304 if (off != 0 || count != sizeof(struct kone_settings))
305 return -EINVAL;
306
307 mutex_lock(&kone->kone_lock);
308 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
309 if (difference) {
310 retval = kone_set_settings(usb_dev,
311 (struct kone_settings const *)buf);
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200312 if (retval) {
313 mutex_unlock(&kone->kone_lock);
314 return retval;
315 }
316
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200317 old_profile = kone->settings.startup_profile;
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200318 memcpy(&kone->settings, buf, sizeof(struct kone_settings));
319
320 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200321
322 if (kone->settings.startup_profile != old_profile)
323 kone_profile_report(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100324 }
325 mutex_unlock(&kone->kone_lock);
326
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100327 return sizeof(struct kone_settings);
328}
329
Stefan Achatz14a057f2010-11-26 19:57:38 +0000330static ssize_t kone_sysfs_read_profilex(struct file *fp,
331 struct kobject *kobj, struct bin_attribute *attr,
332 char *buf, loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000333 struct device *dev =
334 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100335 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
336
337 if (off >= sizeof(struct kone_profile))
338 return 0;
339
340 if (off + count > sizeof(struct kone_profile))
341 count = sizeof(struct kone_profile) - off;
342
343 mutex_lock(&kone->kone_lock);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000344 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100345 mutex_unlock(&kone->kone_lock);
346
347 return count;
348}
349
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100350/* Writes data only if different to stored data */
Stefan Achatz14a057f2010-11-26 19:57:38 +0000351static ssize_t kone_sysfs_write_profilex(struct file *fp,
352 struct kobject *kobj, struct bin_attribute *attr,
353 char *buf, loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000354 struct device *dev =
355 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100356 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
357 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
358 struct kone_profile *profile;
359 int retval = 0, difference;
360
361 /* I need to get my data in one piece */
362 if (off != 0 || count != sizeof(struct kone_profile))
363 return -EINVAL;
364
Stefan Achatz14a057f2010-11-26 19:57:38 +0000365 profile = &kone->profiles[*(uint *)(attr->private)];
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100366
367 mutex_lock(&kone->kone_lock);
368 difference = memcmp(buf, profile, sizeof(struct kone_profile));
369 if (difference) {
370 retval = kone_set_profile(usb_dev,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000371 (struct kone_profile const *)buf,
372 *(uint *)(attr->private) + 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100373 if (!retval)
374 memcpy(profile, buf, sizeof(struct kone_profile));
375 }
376 mutex_unlock(&kone->kone_lock);
377
378 if (retval)
379 return retval;
380
381 return sizeof(struct kone_profile);
382}
383
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100384static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000387 struct kone_device *kone =
388 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100389 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
390}
391
392static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000395 struct kone_device *kone =
396 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100397 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
398}
399
400/* weight is read each time, since we don't get informed when it's changed */
401static ssize_t kone_sysfs_show_weight(struct device *dev,
402 struct device_attribute *attr, char *buf)
403{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000404 struct kone_device *kone;
405 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100406 int weight = 0;
407 int retval;
408
Stefan Achatz5012aad2010-11-26 19:57:33 +0000409 dev = dev->parent->parent;
410 kone = hid_get_drvdata(dev_get_drvdata(dev));
411 usb_dev = interface_to_usbdev(to_usb_interface(dev));
412
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100413 mutex_lock(&kone->kone_lock);
414 retval = kone_get_weight(usb_dev, &weight);
415 mutex_unlock(&kone->kone_lock);
416
417 if (retval)
418 return retval;
419 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
420}
421
422static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
423 struct device_attribute *attr, char *buf)
424{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000425 struct kone_device *kone =
426 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100427 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
428}
429
430static ssize_t kone_sysfs_show_tcu(struct device *dev,
431 struct device_attribute *attr, char *buf)
432{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000433 struct kone_device *kone =
434 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100435 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
436}
437
438static int kone_tcu_command(struct usb_device *usb_dev, int number)
439{
Stefan Achatz5772f632011-01-30 13:38:23 +0100440 unsigned char value;
441 value = number;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200442 return kone_send(usb_dev, kone_command_calibrate, &value, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100443}
444
445/*
446 * Calibrating the tcu is the only action that changes settings data inside the
447 * mouse, so this data needs to be reread
448 */
449static ssize_t kone_sysfs_set_tcu(struct device *dev,
450 struct device_attribute *attr, char const *buf, size_t size)
451{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000452 struct kone_device *kone;
453 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100454 int retval;
455 unsigned long state;
456
Stefan Achatz5012aad2010-11-26 19:57:33 +0000457 dev = dev->parent->parent;
458 kone = hid_get_drvdata(dev_get_drvdata(dev));
459 usb_dev = interface_to_usbdev(to_usb_interface(dev));
460
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100461 retval = strict_strtoul(buf, 10, &state);
462 if (retval)
463 return retval;
464
465 if (state != 0 && state != 1)
466 return -EINVAL;
467
468 mutex_lock(&kone->kone_lock);
469
470 if (state == 1) { /* state activate */
471 retval = kone_tcu_command(usb_dev, 1);
472 if (retval)
473 goto exit_unlock;
474 retval = kone_tcu_command(usb_dev, 2);
475 if (retval)
476 goto exit_unlock;
477 ssleep(5); /* tcu needs this time for calibration */
478 retval = kone_tcu_command(usb_dev, 3);
479 if (retval)
480 goto exit_unlock;
481 retval = kone_tcu_command(usb_dev, 0);
482 if (retval)
483 goto exit_unlock;
484 retval = kone_tcu_command(usb_dev, 4);
485 if (retval)
486 goto exit_unlock;
487 /*
488 * Kone needs this time to settle things.
489 * Reading settings too early will result in invalid data.
490 * Roccat's driver waits 1 sec, maybe this time could be
491 * shortened.
492 */
493 ssleep(1);
494 }
495
496 /* calibration changes values in settings, so reread */
497 retval = kone_get_settings(usb_dev, &kone->settings);
498 if (retval)
499 goto exit_no_settings;
500
501 /* only write settings back if activation state is different */
502 if (kone->settings.tcu != state) {
503 kone->settings.tcu = state;
504 kone_set_settings_checksum(&kone->settings);
505
506 retval = kone_set_settings(usb_dev, &kone->settings);
507 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800508 hid_err(usb_dev, "couldn't set tcu state\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100509 /*
510 * try to reread valid settings into buffer overwriting
511 * first error code
512 */
513 retval = kone_get_settings(usb_dev, &kone->settings);
514 if (retval)
515 goto exit_no_settings;
516 goto exit_unlock;
517 }
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200518 /* calibration resets profile */
519 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100520 }
521
522 retval = size;
523exit_no_settings:
Joe Perches4291ee32010-12-09 19:29:03 -0800524 hid_err(usb_dev, "couldn't read settings\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100525exit_unlock:
526 mutex_unlock(&kone->kone_lock);
527 return retval;
528}
529
530static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000533 struct kone_device *kone =
534 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100535 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
536}
537
538static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
539 struct device_attribute *attr, char const *buf, size_t size)
540{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000541 struct kone_device *kone;
542 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100543 int retval;
544 unsigned long new_startup_profile;
545
Stefan Achatz5012aad2010-11-26 19:57:33 +0000546 dev = dev->parent->parent;
547 kone = hid_get_drvdata(dev_get_drvdata(dev));
548 usb_dev = interface_to_usbdev(to_usb_interface(dev));
549
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100550 retval = strict_strtoul(buf, 10, &new_startup_profile);
551 if (retval)
552 return retval;
553
554 if (new_startup_profile < 1 || new_startup_profile > 5)
555 return -EINVAL;
556
557 mutex_lock(&kone->kone_lock);
558
559 kone->settings.startup_profile = new_startup_profile;
560 kone_set_settings_checksum(&kone->settings);
561
562 retval = kone_set_settings(usb_dev, &kone->settings);
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200563 if (retval) {
564 mutex_unlock(&kone->kone_lock);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100565 return retval;
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200566 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100567
568 /* changing the startup profile immediately activates this profile */
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200569 kone_profile_activated(kone, new_startup_profile);
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200570 kone_profile_report(kone, new_startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100571
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200572 mutex_unlock(&kone->kone_lock);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100573 return size;
574}
575
Stefan Achatz5012aad2010-11-26 19:57:33 +0000576static struct device_attribute kone_attributes[] = {
577 /*
578 * Read actual dpi settings.
579 * Returns raw value for further processing. Refer to enum
580 * kone_polling_rates to get real value.
581 */
582 __ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL),
583 __ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100584
Stefan Achatz5012aad2010-11-26 19:57:33 +0000585 /*
586 * The mouse can be equipped with one of four supplied weights from 5
587 * to 20 grams which are recognized and its value can be read out.
588 * This returns the raw value reported by the mouse for easy evaluation
589 * by software. Refer to enum kone_weights to get corresponding real
590 * weight.
591 */
592 __ATTR(weight, 0440, kone_sysfs_show_weight, NULL),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100593
Stefan Achatz5012aad2010-11-26 19:57:33 +0000594 /*
595 * Prints firmware version stored in mouse as integer.
596 * The raw value reported by the mouse is returned for easy evaluation,
597 * to get the real version number the decimal point has to be shifted 2
598 * positions to the left. E.g. a value of 138 means 1.38.
599 */
600 __ATTR(firmware_version, 0440,
601 kone_sysfs_show_firmware_version, NULL),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100602
Stefan Achatz5012aad2010-11-26 19:57:33 +0000603 /*
604 * Prints state of Tracking Control Unit as number where 0 = off and
605 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
606 * activates the tcu
607 */
608 __ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100609
Stefan Achatz5012aad2010-11-26 19:57:33 +0000610 /* Prints and takes the number of the profile the mouse starts with */
611 __ATTR(startup_profile, 0660,
612 kone_sysfs_show_startup_profile,
613 kone_sysfs_set_startup_profile),
614 __ATTR_NULL
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100615};
616
Stefan Achatz5012aad2010-11-26 19:57:33 +0000617static struct bin_attribute kone_bin_attributes[] = {
618 {
619 .attr = { .name = "settings", .mode = 0660 },
620 .size = sizeof(struct kone_settings),
621 .read = kone_sysfs_read_settings,
622 .write = kone_sysfs_write_settings
623 },
624 {
625 .attr = { .name = "profile1", .mode = 0660 },
626 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000627 .read = kone_sysfs_read_profilex,
628 .write = kone_sysfs_write_profilex,
629 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000630 },
631 {
632 .attr = { .name = "profile2", .mode = 0660 },
633 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000634 .read = kone_sysfs_read_profilex,
635 .write = kone_sysfs_write_profilex,
636 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000637 },
638 {
639 .attr = { .name = "profile3", .mode = 0660 },
640 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000641 .read = kone_sysfs_read_profilex,
642 .write = kone_sysfs_write_profilex,
643 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000644 },
645 {
646 .attr = { .name = "profile4", .mode = 0660 },
647 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000648 .read = kone_sysfs_read_profilex,
649 .write = kone_sysfs_write_profilex,
650 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000651 },
652 {
653 .attr = { .name = "profile5", .mode = 0660 },
654 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000655 .read = kone_sysfs_read_profilex,
656 .write = kone_sysfs_write_profilex,
657 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000658 },
659 __ATTR_NULL
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100660};
661
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100662static int kone_init_kone_device_struct(struct usb_device *usb_dev,
663 struct kone_device *kone)
664{
665 uint i;
666 int retval;
667
668 mutex_init(&kone->kone_lock);
669
670 for (i = 0; i < 5; ++i) {
671 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
672 if (retval)
673 return retval;
674 }
675
676 retval = kone_get_settings(usb_dev, &kone->settings);
677 if (retval)
678 return retval;
679
680 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
681 if (retval)
682 return retval;
683
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200684 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100685
686 return 0;
687}
688
689/*
690 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
691 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
692 * module.
693 * Secial behaviour is bound only to mousepart since only mouseevents contain
694 * additional notifications.
695 */
696static int kone_init_specials(struct hid_device *hdev)
697{
698 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
699 struct usb_device *usb_dev = interface_to_usbdev(intf);
700 struct kone_device *kone;
701 int retval;
702
703 if (intf->cur_altsetting->desc.bInterfaceProtocol
704 == USB_INTERFACE_PROTOCOL_MOUSE) {
705
706 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
707 if (!kone) {
Joe Perches4291ee32010-12-09 19:29:03 -0800708 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100709 return -ENOMEM;
710 }
711 hid_set_drvdata(hdev, kone);
712
713 retval = kone_init_kone_device_struct(usb_dev, kone);
714 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800715 hid_err(hdev, "couldn't init struct kone_device\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100716 goto exit_free;
717 }
Stefan Achatz206f5f22010-05-19 18:55:16 +0200718
Stefan Achatz8211e462011-01-30 13:38:25 +0100719 retval = roccat_connect(kone_class, hdev,
720 sizeof(struct kone_roccat_report));
Stefan Achatz206f5f22010-05-19 18:55:16 +0200721 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800722 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz206f5f22010-05-19 18:55:16 +0200723 /* be tolerant about not getting chrdev */
724 } else {
725 kone->roccat_claimed = 1;
726 kone->chrdev_minor = retval;
727 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100728 } else {
729 hid_set_drvdata(hdev, NULL);
730 }
731
732 return 0;
733exit_free:
734 kfree(kone);
735 return retval;
736}
737
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100738static void kone_remove_specials(struct hid_device *hdev)
739{
740 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200741 struct kone_device *kone;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100742
743 if (intf->cur_altsetting->desc.bInterfaceProtocol
744 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatz206f5f22010-05-19 18:55:16 +0200745 kone = hid_get_drvdata(hdev);
746 if (kone->roccat_claimed)
747 roccat_disconnect(kone->chrdev_minor);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100748 kfree(hid_get_drvdata(hdev));
749 }
750}
751
752static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
753{
754 int retval;
755
756 retval = hid_parse(hdev);
757 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800758 hid_err(hdev, "parse failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100759 goto exit;
760 }
761
762 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
763 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800764 hid_err(hdev, "hw start failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100765 goto exit;
766 }
767
768 retval = kone_init_specials(hdev);
769 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800770 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100771 goto exit_stop;
772 }
773
774 return 0;
775
776exit_stop:
777 hid_hw_stop(hdev);
778exit:
779 return retval;
780}
781
782static void kone_remove(struct hid_device *hdev)
783{
784 kone_remove_specials(hdev);
785 hid_hw_stop(hdev);
786}
787
Stefan Achatz48e70802010-05-18 18:31:04 +0200788/* handle special events and keep actual profile and dpi values up to date */
789static void kone_keep_values_up_to_date(struct kone_device *kone,
790 struct kone_mouse_event const *event)
791{
792 switch (event->event) {
793 case kone_mouse_event_switch_profile:
Stefan Achatz1c5784d2011-08-27 15:24:36 +0200794 kone->actual_dpi = kone->profiles[event->value - 1].
795 startup_dpi;
Stefan Achatz48e70802010-05-18 18:31:04 +0200796 case kone_mouse_event_osd_profile:
797 kone->actual_profile = event->value;
Stefan Achatz48e70802010-05-18 18:31:04 +0200798 break;
799 case kone_mouse_event_switch_dpi:
800 case kone_mouse_event_osd_dpi:
801 kone->actual_dpi = event->value;
802 break;
803 }
804}
805
Stefan Achatz206f5f22010-05-19 18:55:16 +0200806static void kone_report_to_chrdev(struct kone_device const *kone,
807 struct kone_mouse_event const *event)
808{
809 struct kone_roccat_report roccat_report;
810
811 switch (event->event) {
812 case kone_mouse_event_switch_profile:
813 case kone_mouse_event_switch_dpi:
814 case kone_mouse_event_osd_profile:
815 case kone_mouse_event_osd_dpi:
816 roccat_report.event = event->event;
817 roccat_report.value = event->value;
818 roccat_report.key = 0;
819 roccat_report_event(kone->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100820 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200821 break;
822 case kone_mouse_event_call_overlong_macro:
823 if (event->value == kone_keystroke_action_press) {
824 roccat_report.event = kone_mouse_event_call_overlong_macro;
825 roccat_report.value = kone->actual_profile;
826 roccat_report.key = event->macro_key;
827 roccat_report_event(kone->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100828 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200829 }
830 break;
831 }
832
833}
834
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100835/*
836 * Is called for keyboard- and mousepart.
837 * Only mousepart gets informations about special events in its extended event
838 * structure.
839 */
840static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
841 u8 *data, int size)
842{
843 struct kone_device *kone = hid_get_drvdata(hdev);
844 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
845
846 /* keyboard events are always processed by default handler */
847 if (size != sizeof(struct kone_mouse_event))
848 return 0;
849
Stefan Achatz901e64d2011-06-12 10:02:44 +0200850 if (kone == NULL)
851 return 0;
852
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100853 /*
Stefan Achatz73b35772010-05-19 13:53:22 +0200854 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
855 * Pressed button is reported in each movement event.
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100856 * Workaround sends only one event per press.
857 */
Stefan Achatz73b35772010-05-19 13:53:22 +0200858 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
859 memcpy(&kone->last_mouse_event, event,
860 sizeof(struct kone_mouse_event));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100861 else
Stefan Achatz73b35772010-05-19 13:53:22 +0200862 memset(&event->tilt, 0, 5);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100863
Stefan Achatz48e70802010-05-18 18:31:04 +0200864 kone_keep_values_up_to_date(kone, event);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100865
Stefan Achatz206f5f22010-05-19 18:55:16 +0200866 if (kone->roccat_claimed)
867 kone_report_to_chrdev(kone, event);
868
Stefan Achatz48e70802010-05-18 18:31:04 +0200869 return 0; /* always do further processing */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100870}
871
872static const struct hid_device_id kone_devices[] = {
873 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
874 { }
875};
876
877MODULE_DEVICE_TABLE(hid, kone_devices);
878
879static struct hid_driver kone_driver = {
880 .name = "kone",
881 .id_table = kone_devices,
882 .probe = kone_probe,
883 .remove = kone_remove,
884 .raw_event = kone_raw_event
885};
886
Stefan Achatz00237bc2010-05-08 17:20:38 +0200887static int __init kone_init(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100888{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000889 int retval;
890
891 /* class name has to be same as driver name */
892 kone_class = class_create(THIS_MODULE, "kone");
893 if (IS_ERR(kone_class))
894 return PTR_ERR(kone_class);
895 kone_class->dev_attrs = kone_attributes;
896 kone_class->dev_bin_attrs = kone_bin_attributes;
897
898 retval = hid_register_driver(&kone_driver);
899 if (retval)
900 class_destroy(kone_class);
901 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100902}
903
Stefan Achatz00237bc2010-05-08 17:20:38 +0200904static void __exit kone_exit(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100905{
906 hid_unregister_driver(&kone_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100907 class_destroy(kone_class);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100908}
909
910module_init(kone_init);
911module_exit(kone_exit);
912
Stefan Achatz1f749d82010-05-12 17:43:34 +0200913MODULE_AUTHOR("Stefan Achatz");
914MODULE_DESCRIPTION("USB Roccat Kone driver");
915MODULE_LICENSE("GPL v2");