blob: a57838d152673d76264e48d46c801f3065c78471 [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 Achatz5012aad2010-11-26 19:57:33 +000040/* kone_class is used for creating sysfs attributes via roccat char device */
41static struct class *kone_class;
42
Stefan Achatz14bf62c2010-03-18 16:19:43 +010043static 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 */
59static int kone_check_write(struct usb_device *usb_dev)
60{
Stefan Achatz5772f632011-01-30 13:38:23 +010061 int retval;
62 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +010063
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
Stefan Achatz5772f632011-01-30 13:38:23 +010071 retval = roccat_common_receive(usb_dev,
72 kone_command_confirm_write, &data, 1);
73 if (retval)
74 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +010075
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 */
Stefan Achatz5772f632011-01-30 13:38:23 +010081 } while (data == 3);
Stefan Achatz14bf62c2010-03-18 16:19:43 +010082
Stefan Achatz5772f632011-01-30 13:38:23 +010083 if (data == 1) /* everything alright */
Stefan Achatz14bf62c2010-03-18 16:19:43 +010084 return 0;
Stefan Achatz5772f632011-01-30 13:38:23 +010085
86 /* unknown answer */
87 hid_err(usb_dev, "got retval %d when checking write\n", data);
88 return -EIO;
Stefan Achatz14bf62c2010-03-18 16:19:43 +010089}
90
91/*
92 * Reads settings from mouse and stores it in @buf
Stefan Achatz14bf62c2010-03-18 16:19:43 +010093 * On success returns 0
94 * On failure returns errno
95 */
96static int kone_get_settings(struct usb_device *usb_dev,
97 struct kone_settings *buf)
98{
Stefan Achatz5772f632011-01-30 13:38:23 +010099 return roccat_common_receive(usb_dev, kone_command_settings, buf,
100 sizeof(struct kone_settings));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100101}
102
103/*
104 * Writes settings from @buf to mouse
105 * On success returns 0
106 * On failure returns errno
107 */
108static int kone_set_settings(struct usb_device *usb_dev,
109 struct kone_settings const *settings)
110{
Stefan Achatz5772f632011-01-30 13:38:23 +0100111 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);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100117}
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 */
125static 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 */
151static 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,
Stefan Achatz5772f632011-01-30 13:38:23 +0100162 kone_command_profile, number, (void *)profile,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100163 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 */
180static int kone_get_weight(struct usb_device *usb_dev, int *result)
181{
Stefan Achatz5772f632011-01-30 13:38:23 +0100182 int retval;
183 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100184
Stefan Achatz5772f632011-01-30 13:38:23 +0100185 retval = roccat_common_receive(usb_dev, kone_command_weight, &data, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100186
Stefan Achatz5772f632011-01-30 13:38:23 +0100187 if (retval)
188 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100189
Stefan Achatz5772f632011-01-30 13:38:23 +0100190 *result = (int)data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100191 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 */
199static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
200{
Stefan Achatz5772f632011-01-30 13:38:23 +0100201 int retval;
202 uint16_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100203
Stefan Achatz5772f632011-01-30 13:38:23 +0100204 retval = roccat_common_receive(usb_dev, kone_command_firmware_version,
205 &data, 2);
206 if (retval)
207 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100208
Stefan Achatz5772f632011-01-30 13:38:23 +0100209 *result = le16_to_cpu(data);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100210 return 0;
211}
212
Stephen Rothwell5f277622010-05-21 16:15:32 +1000213static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100214 struct bin_attribute *attr, char *buf,
215 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000216 struct device *dev =
217 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100218 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);
Stefan Achatzcab6b162010-06-20 19:19:06 +0200227 memcpy(buf, ((char const *)&kone->settings) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100228 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 */
Stephen Rothwell5f277622010-05-21 16:15:32 +1000238static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100239 struct bin_attribute *attr, char *buf,
240 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000241 struct device *dev =
242 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100243 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
Stefan Achatz14a057f2010-11-26 19:57:38 +0000275static 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) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000278 struct device *dev =
279 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100280 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);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000289 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100290 mutex_unlock(&kone->kone_lock);
291
292 return count;
293}
294
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100295/* Writes data only if different to stored data */
Stefan Achatz14a057f2010-11-26 19:57:38 +0000296static 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) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000299 struct device *dev =
300 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100301 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
Stefan Achatz14a057f2010-11-26 19:57:38 +0000310 profile = &kone->profiles[*(uint *)(attr->private)];
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100311
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,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000316 (struct kone_profile const *)buf,
317 *(uint *)(attr->private) + 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100318 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
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100329static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000332 struct kone_device *kone =
333 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100334 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
335}
336
337static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
338 struct device_attribute *attr, char *buf)
339{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000340 struct kone_device *kone =
341 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100342 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 */
346static ssize_t kone_sysfs_show_weight(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000349 struct kone_device *kone;
350 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100351 int weight = 0;
352 int retval;
353
Stefan Achatz5012aad2010-11-26 19:57:33 +0000354 dev = dev->parent->parent;
355 kone = hid_get_drvdata(dev_get_drvdata(dev));
356 usb_dev = interface_to_usbdev(to_usb_interface(dev));
357
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100358 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
367static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000370 struct kone_device *kone =
371 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100372 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
373}
374
375static ssize_t kone_sysfs_show_tcu(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000378 struct kone_device *kone =
379 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100380 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
381}
382
383static int kone_tcu_command(struct usb_device *usb_dev, int number)
384{
Stefan Achatz5772f632011-01-30 13:38:23 +0100385 unsigned char value;
386 value = number;
387 return roccat_common_send(usb_dev, kone_command_calibrate, &value, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100388}
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 */
394static ssize_t kone_sysfs_set_tcu(struct device *dev,
395 struct device_attribute *attr, char const *buf, size_t size)
396{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000397 struct kone_device *kone;
398 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100399 int retval;
400 unsigned long state;
401
Stefan Achatz5012aad2010-11-26 19:57:33 +0000402 dev = dev->parent->parent;
403 kone = hid_get_drvdata(dev_get_drvdata(dev));
404 usb_dev = interface_to_usbdev(to_usb_interface(dev));
405
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100406 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) {
Joe Perches4291ee32010-12-09 19:29:03 -0800453 hid_err(usb_dev, "couldn't set tcu state\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100454 /*
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;
466exit_no_settings:
Joe Perches4291ee32010-12-09 19:29:03 -0800467 hid_err(usb_dev, "couldn't read settings\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100468exit_unlock:
469 mutex_unlock(&kone->kone_lock);
470 return retval;
471}
472
473static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000476 struct kone_device *kone =
477 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100478 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
479}
480
481static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
482 struct device_attribute *attr, char const *buf, size_t size)
483{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000484 struct kone_device *kone;
485 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100486 int retval;
487 unsigned long new_startup_profile;
488
Stefan Achatz5012aad2010-11-26 19:57:33 +0000489 dev = dev->parent->parent;
490 kone = hid_get_drvdata(dev_get_drvdata(dev));
491 usb_dev = interface_to_usbdev(to_usb_interface(dev));
492
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100493 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
Stefan Achatz5012aad2010-11-26 19:57:33 +0000519static 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),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100527
Stefan Achatz5012aad2010-11-26 19:57:33 +0000528 /*
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),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100536
Stefan Achatz5012aad2010-11-26 19:57:33 +0000537 /*
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),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100545
Stefan Achatz5012aad2010-11-26 19:57:33 +0000546 /*
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),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100552
Stefan Achatz5012aad2010-11-26 19:57:33 +0000553 /* 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
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100558};
559
Stefan Achatz5012aad2010-11-26 19:57:33 +0000560static 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),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000570 .read = kone_sysfs_read_profilex,
571 .write = kone_sysfs_write_profilex,
572 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000573 },
574 {
575 .attr = { .name = "profile2", .mode = 0660 },
576 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000577 .read = kone_sysfs_read_profilex,
578 .write = kone_sysfs_write_profilex,
579 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000580 },
581 {
582 .attr = { .name = "profile3", .mode = 0660 },
583 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000584 .read = kone_sysfs_read_profilex,
585 .write = kone_sysfs_write_profilex,
586 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000587 },
588 {
589 .attr = { .name = "profile4", .mode = 0660 },
590 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000591 .read = kone_sysfs_read_profilex,
592 .write = kone_sysfs_write_profilex,
593 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000594 },
595 {
596 .attr = { .name = "profile5", .mode = 0660 },
597 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000598 .read = kone_sysfs_read_profilex,
599 .write = kone_sysfs_write_profilex,
600 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000601 },
602 __ATTR_NULL
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100603};
604
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100605static 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 */
640static 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) {
Joe Perches4291ee32010-12-09 19:29:03 -0800652 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100653 return -ENOMEM;
654 }
655 hid_set_drvdata(hdev, kone);
656
657 retval = kone_init_kone_device_struct(usb_dev, kone);
658 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800659 hid_err(hdev, "couldn't init struct kone_device\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100660 goto exit_free;
661 }
Stefan Achatz206f5f22010-05-19 18:55:16 +0200662
Stefan Achatz8211e462011-01-30 13:38:25 +0100663 retval = roccat_connect(kone_class, hdev,
664 sizeof(struct kone_roccat_report));
Stefan Achatz206f5f22010-05-19 18:55:16 +0200665 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800666 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz206f5f22010-05-19 18:55:16 +0200667 /* be tolerant about not getting chrdev */
668 } else {
669 kone->roccat_claimed = 1;
670 kone->chrdev_minor = retval;
671 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100672 } else {
673 hid_set_drvdata(hdev, NULL);
674 }
675
676 return 0;
677exit_free:
678 kfree(kone);
679 return retval;
680}
681
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100682static void kone_remove_specials(struct hid_device *hdev)
683{
684 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200685 struct kone_device *kone;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100686
687 if (intf->cur_altsetting->desc.bInterfaceProtocol
688 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatz206f5f22010-05-19 18:55:16 +0200689 kone = hid_get_drvdata(hdev);
690 if (kone->roccat_claimed)
691 roccat_disconnect(kone->chrdev_minor);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100692 kfree(hid_get_drvdata(hdev));
693 }
694}
695
696static 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) {
Joe Perches4291ee32010-12-09 19:29:03 -0800702 hid_err(hdev, "parse failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100703 goto exit;
704 }
705
706 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
707 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800708 hid_err(hdev, "hw start failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100709 goto exit;
710 }
711
712 retval = kone_init_specials(hdev);
713 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800714 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100715 goto exit_stop;
716 }
717
718 return 0;
719
720exit_stop:
721 hid_hw_stop(hdev);
722exit:
723 return retval;
724}
725
726static void kone_remove(struct hid_device *hdev)
727{
728 kone_remove_specials(hdev);
729 hid_hw_stop(hdev);
730}
731
Stefan Achatz48e70802010-05-18 18:31:04 +0200732/* handle special events and keep actual profile and dpi values up to date */
733static 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
Stefan Achatz206f5f22010-05-19 18:55:16 +0200750static 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,
Stefan Achatz8211e462011-01-30 13:38:25 +0100764 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200765 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,
Stefan Achatz8211e462011-01-30 13:38:25 +0100772 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200773 }
774 break;
775 }
776
777}
778
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100779/*
780 * Is called for keyboard- and mousepart.
781 * Only mousepart gets informations about special events in its extended event
782 * structure.
783 */
784static 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 /*
Stefan Achatz73b35772010-05-19 13:53:22 +0200795 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
796 * Pressed button is reported in each movement event.
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100797 * Workaround sends only one event per press.
798 */
Stefan Achatz73b35772010-05-19 13:53:22 +0200799 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
800 memcpy(&kone->last_mouse_event, event,
801 sizeof(struct kone_mouse_event));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100802 else
Stefan Achatz73b35772010-05-19 13:53:22 +0200803 memset(&event->tilt, 0, 5);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100804
Stefan Achatz48e70802010-05-18 18:31:04 +0200805 kone_keep_values_up_to_date(kone, event);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100806
Stefan Achatz206f5f22010-05-19 18:55:16 +0200807 if (kone->roccat_claimed)
808 kone_report_to_chrdev(kone, event);
809
Stefan Achatz48e70802010-05-18 18:31:04 +0200810 return 0; /* always do further processing */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100811}
812
813static const struct hid_device_id kone_devices[] = {
814 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
815 { }
816};
817
818MODULE_DEVICE_TABLE(hid, kone_devices);
819
820static 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
Stefan Achatz00237bc2010-05-08 17:20:38 +0200828static int __init kone_init(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100829{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000830 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;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100843}
844
Stefan Achatz00237bc2010-05-08 17:20:38 +0200845static void __exit kone_exit(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100846{
847 hid_unregister_driver(&kone_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100848 class_destroy(kone_class);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100849}
850
851module_init(kone_init);
852module_exit(kone_exit);
853
Stefan Achatz1f749d82010-05-12 17:43:34 +0200854MODULE_AUTHOR("Stefan Achatz");
855MODULE_DESCRIPTION("USB Roccat Kone driver");
856MODULE_LICENSE("GPL v2");