blob: 6101816a7ddd8a96ae8ad0e9ecfa297c29fa637f [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
Thomas Meyer4c33a882011-11-17 23:43:40 +010081 buf = kmemdup(data, size, GFP_KERNEL);
Stefan Achatz1edd5b42011-06-01 15:54:17 +020082 if (buf == NULL)
83 return -ENOMEM;
84
Stefan Achatz1edd5b42011-06-01 15:54:17 +020085 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
86 HID_REQ_SET_REPORT,
87 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
88 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
89
90 kfree(buf);
91 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
92}
93
Stefan Achatz5012aad2010-11-26 19:57:33 +000094/* kone_class is used for creating sysfs attributes via roccat char device */
95static struct class *kone_class;
96
Stefan Achatz14bf62c2010-03-18 16:19:43 +010097static void kone_set_settings_checksum(struct kone_settings *settings)
98{
99 uint16_t checksum = 0;
100 unsigned char *address = (unsigned char *)settings;
101 int i;
102
103 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
104 checksum += *address;
105 settings->checksum = cpu_to_le16(checksum);
106}
107
108/*
109 * Checks success after writing data to mouse
110 * On success returns 0
111 * On failure returns errno
112 */
113static int kone_check_write(struct usb_device *usb_dev)
114{
Stefan Achatz5772f632011-01-30 13:38:23 +0100115 int retval;
116 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100117
118 do {
119 /*
120 * Mouse needs 50 msecs until it says ok, but there are
121 * 30 more msecs needed for next write to work.
122 */
123 msleep(80);
124
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200125 retval = kone_receive(usb_dev,
Stefan Achatz5772f632011-01-30 13:38:23 +0100126 kone_command_confirm_write, &data, 1);
127 if (retval)
128 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100129
130 /*
131 * value of 3 seems to mean something like
132 * "not finished yet, but it looks good"
133 * So check again after a moment.
134 */
Stefan Achatz5772f632011-01-30 13:38:23 +0100135 } while (data == 3);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100136
Stefan Achatz5772f632011-01-30 13:38:23 +0100137 if (data == 1) /* everything alright */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100138 return 0;
Stefan Achatz5772f632011-01-30 13:38:23 +0100139
140 /* unknown answer */
Stefan Achatz4ec141a2012-05-20 22:45:08 +0200141 dev_err(&usb_dev->dev, "got retval %d when checking write\n", data);
Stefan Achatz5772f632011-01-30 13:38:23 +0100142 return -EIO;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100143}
144
145/*
146 * Reads settings from mouse and stores it in @buf
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100147 * On success returns 0
148 * On failure returns errno
149 */
150static int kone_get_settings(struct usb_device *usb_dev,
151 struct kone_settings *buf)
152{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200153 return kone_receive(usb_dev, kone_command_settings, buf,
Stefan Achatz5772f632011-01-30 13:38:23 +0100154 sizeof(struct kone_settings));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100155}
156
157/*
158 * Writes settings from @buf to mouse
159 * On success returns 0
160 * On failure returns errno
161 */
162static int kone_set_settings(struct usb_device *usb_dev,
163 struct kone_settings const *settings)
164{
Stefan Achatz5772f632011-01-30 13:38:23 +0100165 int retval;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200166 retval = kone_send(usb_dev, kone_command_settings,
Stefan Achatz5772f632011-01-30 13:38:23 +0100167 settings, sizeof(struct kone_settings));
168 if (retval)
169 return retval;
170 return kone_check_write(usb_dev);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100171}
172
173/*
174 * Reads profile data from mouse and stores it in @buf
175 * @number: profile number to read
176 * On success returns 0
177 * On failure returns errno
178 */
179static int kone_get_profile(struct usb_device *usb_dev,
180 struct kone_profile *buf, int number)
181{
182 int len;
183
184 if (number < 1 || number > 5)
185 return -EINVAL;
186
187 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
188 USB_REQ_CLEAR_FEATURE,
189 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
190 kone_command_profile, number, buf,
191 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
192
193 if (len != sizeof(struct kone_profile))
194 return -EIO;
195
196 return 0;
197}
198
199/*
200 * Writes profile data to mouse.
201 * @number: profile number to write
202 * On success returns 0
203 * On failure returns errno
204 */
205static int kone_set_profile(struct usb_device *usb_dev,
206 struct kone_profile const *profile, int number)
207{
208 int len;
209
210 if (number < 1 || number > 5)
211 return -EINVAL;
212
213 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
214 USB_REQ_SET_CONFIGURATION,
215 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Stefan Achatz5772f632011-01-30 13:38:23 +0100216 kone_command_profile, number, (void *)profile,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100217 sizeof(struct kone_profile),
218 USB_CTRL_SET_TIMEOUT);
219
220 if (len != sizeof(struct kone_profile))
221 return len;
222
223 if (kone_check_write(usb_dev))
224 return -EIO;
225
226 return 0;
227}
228
229/*
230 * Reads value of "fast-clip-weight" and stores it in @result
231 * On success returns 0
232 * On failure returns errno
233 */
234static int kone_get_weight(struct usb_device *usb_dev, int *result)
235{
Stefan Achatz5772f632011-01-30 13:38:23 +0100236 int retval;
237 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100238
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200239 retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100240
Stefan Achatz5772f632011-01-30 13:38:23 +0100241 if (retval)
242 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100243
Stefan Achatz5772f632011-01-30 13:38:23 +0100244 *result = (int)data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100245 return 0;
246}
247
248/*
249 * Reads firmware_version of mouse and stores it in @result
250 * On success returns 0
251 * On failure returns errno
252 */
253static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
254{
Stefan Achatz5772f632011-01-30 13:38:23 +0100255 int retval;
256 uint16_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100257
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200258 retval = kone_receive(usb_dev, kone_command_firmware_version,
Stefan Achatz5772f632011-01-30 13:38:23 +0100259 &data, 2);
260 if (retval)
261 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100262
Stefan Achatz5772f632011-01-30 13:38:23 +0100263 *result = le16_to_cpu(data);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100264 return 0;
265}
266
Stephen Rothwell5f277622010-05-21 16:15:32 +1000267static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100268 struct bin_attribute *attr, char *buf,
269 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000270 struct device *dev =
271 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100272 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
273
274 if (off >= sizeof(struct kone_settings))
275 return 0;
276
277 if (off + count > sizeof(struct kone_settings))
278 count = sizeof(struct kone_settings) - off;
279
280 mutex_lock(&kone->kone_lock);
Stefan Achatzcab6b162010-06-20 19:19:06 +0200281 memcpy(buf, ((char const *)&kone->settings) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100282 mutex_unlock(&kone->kone_lock);
283
284 return count;
285}
286
287/*
288 * Writing settings automatically activates startup_profile.
289 * This function keeps values in kone_device up to date and assumes that in
290 * case of error the old data is still valid
291 */
Stephen Rothwell5f277622010-05-21 16:15:32 +1000292static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100293 struct bin_attribute *attr, char *buf,
294 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000295 struct device *dev =
296 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100297 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
298 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200299 int retval = 0, difference, old_profile;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100300
301 /* I need to get my data in one piece */
302 if (off != 0 || count != sizeof(struct kone_settings))
303 return -EINVAL;
304
305 mutex_lock(&kone->kone_lock);
306 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
307 if (difference) {
308 retval = kone_set_settings(usb_dev,
309 (struct kone_settings const *)buf);
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200310 if (retval) {
311 mutex_unlock(&kone->kone_lock);
312 return retval;
313 }
314
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200315 old_profile = kone->settings.startup_profile;
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200316 memcpy(&kone->settings, buf, sizeof(struct kone_settings));
317
318 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200319
320 if (kone->settings.startup_profile != old_profile)
321 kone_profile_report(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100322 }
323 mutex_unlock(&kone->kone_lock);
324
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100325 return sizeof(struct kone_settings);
326}
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700327static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
328 kone_sysfs_write_settings, sizeof(struct kone_settings));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100329
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}
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700383#define PROFILE_ATTR(number) \
384static struct bin_attribute bin_attr_profile##number = { \
Stefan Achatz550dbf42013-09-28 05:57:46 +0200385 .attr = { .name = "profile" #number, .mode = 0660 }, \
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700386 .size = sizeof(struct kone_profile), \
387 .read = kone_sysfs_read_profilex, \
388 .write = kone_sysfs_write_profilex, \
Greg Kroah-Hartman515ad4d2013-08-19 21:56:46 -0700389 .private = &profile_numbers[number-1], \
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700390};
391PROFILE_ATTR(1);
392PROFILE_ATTR(2);
393PROFILE_ATTR(3);
394PROFILE_ATTR(4);
395PROFILE_ATTR(5);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100396
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100397static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
398 struct device_attribute *attr, char *buf)
399{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000400 struct kone_device *kone =
401 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100402 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
403}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700404static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100405
406static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000409 struct kone_device *kone =
410 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100411 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
412}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700413static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100414
415/* weight is read each time, since we don't get informed when it's changed */
416static ssize_t kone_sysfs_show_weight(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000419 struct kone_device *kone;
420 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100421 int weight = 0;
422 int retval;
423
Stefan Achatz5012aad2010-11-26 19:57:33 +0000424 dev = dev->parent->parent;
425 kone = hid_get_drvdata(dev_get_drvdata(dev));
426 usb_dev = interface_to_usbdev(to_usb_interface(dev));
427
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100428 mutex_lock(&kone->kone_lock);
429 retval = kone_get_weight(usb_dev, &weight);
430 mutex_unlock(&kone->kone_lock);
431
432 if (retval)
433 return retval;
434 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
435}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700436static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100437
438static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000441 struct kone_device *kone =
442 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100443 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
444}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700445static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
446 NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100447
448static ssize_t kone_sysfs_show_tcu(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000451 struct kone_device *kone =
452 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100453 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
454}
455
456static int kone_tcu_command(struct usb_device *usb_dev, int number)
457{
Stefan Achatz5772f632011-01-30 13:38:23 +0100458 unsigned char value;
459 value = number;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200460 return kone_send(usb_dev, kone_command_calibrate, &value, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100461}
462
463/*
464 * Calibrating the tcu is the only action that changes settings data inside the
465 * mouse, so this data needs to be reread
466 */
467static ssize_t kone_sysfs_set_tcu(struct device *dev,
468 struct device_attribute *attr, char const *buf, size_t size)
469{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000470 struct kone_device *kone;
471 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100472 int retval;
473 unsigned long state;
474
Stefan Achatz5012aad2010-11-26 19:57:33 +0000475 dev = dev->parent->parent;
476 kone = hid_get_drvdata(dev_get_drvdata(dev));
477 usb_dev = interface_to_usbdev(to_usb_interface(dev));
478
Jingoo Handfc450b2013-07-19 15:53:16 +0900479 retval = kstrtoul(buf, 10, &state);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100480 if (retval)
481 return retval;
482
483 if (state != 0 && state != 1)
484 return -EINVAL;
485
486 mutex_lock(&kone->kone_lock);
487
488 if (state == 1) { /* state activate */
489 retval = kone_tcu_command(usb_dev, 1);
490 if (retval)
491 goto exit_unlock;
492 retval = kone_tcu_command(usb_dev, 2);
493 if (retval)
494 goto exit_unlock;
495 ssleep(5); /* tcu needs this time for calibration */
496 retval = kone_tcu_command(usb_dev, 3);
497 if (retval)
498 goto exit_unlock;
499 retval = kone_tcu_command(usb_dev, 0);
500 if (retval)
501 goto exit_unlock;
502 retval = kone_tcu_command(usb_dev, 4);
503 if (retval)
504 goto exit_unlock;
505 /*
506 * Kone needs this time to settle things.
507 * Reading settings too early will result in invalid data.
508 * Roccat's driver waits 1 sec, maybe this time could be
509 * shortened.
510 */
511 ssleep(1);
512 }
513
514 /* calibration changes values in settings, so reread */
515 retval = kone_get_settings(usb_dev, &kone->settings);
516 if (retval)
517 goto exit_no_settings;
518
519 /* only write settings back if activation state is different */
520 if (kone->settings.tcu != state) {
521 kone->settings.tcu = state;
522 kone_set_settings_checksum(&kone->settings);
523
524 retval = kone_set_settings(usb_dev, &kone->settings);
525 if (retval) {
Stefan Achatz4ec141a2012-05-20 22:45:08 +0200526 dev_err(&usb_dev->dev, "couldn't set tcu state\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100527 /*
528 * try to reread valid settings into buffer overwriting
529 * first error code
530 */
531 retval = kone_get_settings(usb_dev, &kone->settings);
532 if (retval)
533 goto exit_no_settings;
534 goto exit_unlock;
535 }
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200536 /* calibration resets profile */
537 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100538 }
539
540 retval = size;
541exit_no_settings:
Stefan Achatz4ec141a2012-05-20 22:45:08 +0200542 dev_err(&usb_dev->dev, "couldn't read settings\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100543exit_unlock:
544 mutex_unlock(&kone->kone_lock);
545 return retval;
546}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700547static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100548
549static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
550 struct device_attribute *attr, char *buf)
551{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000552 struct kone_device *kone =
553 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100554 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
555}
556
557static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
558 struct device_attribute *attr, char const *buf, size_t size)
559{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000560 struct kone_device *kone;
561 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100562 int retval;
563 unsigned long new_startup_profile;
564
Stefan Achatz5012aad2010-11-26 19:57:33 +0000565 dev = dev->parent->parent;
566 kone = hid_get_drvdata(dev_get_drvdata(dev));
567 usb_dev = interface_to_usbdev(to_usb_interface(dev));
568
Jingoo Handfc450b2013-07-19 15:53:16 +0900569 retval = kstrtoul(buf, 10, &new_startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100570 if (retval)
571 return retval;
572
573 if (new_startup_profile < 1 || new_startup_profile > 5)
574 return -EINVAL;
575
576 mutex_lock(&kone->kone_lock);
577
578 kone->settings.startup_profile = new_startup_profile;
579 kone_set_settings_checksum(&kone->settings);
580
581 retval = kone_set_settings(usb_dev, &kone->settings);
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200582 if (retval) {
583 mutex_unlock(&kone->kone_lock);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100584 return retval;
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200585 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100586
587 /* changing the startup profile immediately activates this profile */
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200588 kone_profile_activated(kone, new_startup_profile);
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200589 kone_profile_report(kone, new_startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100590
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200591 mutex_unlock(&kone->kone_lock);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100592 return size;
593}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700594static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
595 kone_sysfs_set_startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100596
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700597static struct attribute *kone_attrs[] = {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000598 /*
599 * Read actual dpi settings.
600 * Returns raw value for further processing. Refer to enum
601 * kone_polling_rates to get real value.
602 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700603 &dev_attr_actual_dpi.attr,
604 &dev_attr_actual_profile.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100605
Stefan Achatz5012aad2010-11-26 19:57:33 +0000606 /*
607 * The mouse can be equipped with one of four supplied weights from 5
608 * to 20 grams which are recognized and its value can be read out.
609 * This returns the raw value reported by the mouse for easy evaluation
610 * by software. Refer to enum kone_weights to get corresponding real
611 * weight.
612 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700613 &dev_attr_weight.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100614
Stefan Achatz5012aad2010-11-26 19:57:33 +0000615 /*
616 * Prints firmware version stored in mouse as integer.
617 * The raw value reported by the mouse is returned for easy evaluation,
618 * to get the real version number the decimal point has to be shifted 2
619 * positions to the left. E.g. a value of 138 means 1.38.
620 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700621 &dev_attr_firmware_version.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100622
Stefan Achatz5012aad2010-11-26 19:57:33 +0000623 /*
624 * Prints state of Tracking Control Unit as number where 0 = off and
625 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
626 * activates the tcu
627 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700628 &dev_attr_tcu.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100629
Stefan Achatz5012aad2010-11-26 19:57:33 +0000630 /* Prints and takes the number of the profile the mouse starts with */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700631 &dev_attr_startup_profile.attr,
632 NULL,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100633};
634
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700635static struct bin_attribute *kone_bin_attributes[] = {
636 &bin_attr_settings,
637 &bin_attr_profile1,
638 &bin_attr_profile2,
639 &bin_attr_profile3,
640 &bin_attr_profile4,
641 &bin_attr_profile5,
642 NULL,
643};
644
645static const struct attribute_group kone_group = {
646 .attrs = kone_attrs,
647 .bin_attrs = kone_bin_attributes,
648};
649
650static const struct attribute_group *kone_groups[] = {
651 &kone_group,
652 NULL,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100653};
654
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100655static int kone_init_kone_device_struct(struct usb_device *usb_dev,
656 struct kone_device *kone)
657{
658 uint i;
659 int retval;
660
661 mutex_init(&kone->kone_lock);
662
663 for (i = 0; i < 5; ++i) {
664 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
665 if (retval)
666 return retval;
667 }
668
669 retval = kone_get_settings(usb_dev, &kone->settings);
670 if (retval)
671 return retval;
672
673 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
674 if (retval)
675 return retval;
676
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200677 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100678
679 return 0;
680}
681
682/*
683 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
684 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
685 * module.
686 * Secial behaviour is bound only to mousepart since only mouseevents contain
687 * additional notifications.
688 */
689static int kone_init_specials(struct hid_device *hdev)
690{
691 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
692 struct usb_device *usb_dev = interface_to_usbdev(intf);
693 struct kone_device *kone;
694 int retval;
695
696 if (intf->cur_altsetting->desc.bInterfaceProtocol
697 == USB_INTERFACE_PROTOCOL_MOUSE) {
698
699 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
700 if (!kone) {
Joe Perches4291ee32010-12-09 19:29:03 -0800701 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100702 return -ENOMEM;
703 }
704 hid_set_drvdata(hdev, kone);
705
706 retval = kone_init_kone_device_struct(usb_dev, kone);
707 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800708 hid_err(hdev, "couldn't init struct kone_device\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100709 goto exit_free;
710 }
Stefan Achatz206f5f22010-05-19 18:55:16 +0200711
Stefan Achatz8211e462011-01-30 13:38:25 +0100712 retval = roccat_connect(kone_class, hdev,
713 sizeof(struct kone_roccat_report));
Stefan Achatz206f5f22010-05-19 18:55:16 +0200714 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800715 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz206f5f22010-05-19 18:55:16 +0200716 /* be tolerant about not getting chrdev */
717 } else {
718 kone->roccat_claimed = 1;
719 kone->chrdev_minor = retval;
720 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100721 } else {
722 hid_set_drvdata(hdev, NULL);
723 }
724
725 return 0;
726exit_free:
727 kfree(kone);
728 return retval;
729}
730
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100731static void kone_remove_specials(struct hid_device *hdev)
732{
733 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200734 struct kone_device *kone;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100735
736 if (intf->cur_altsetting->desc.bInterfaceProtocol
737 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatz206f5f22010-05-19 18:55:16 +0200738 kone = hid_get_drvdata(hdev);
739 if (kone->roccat_claimed)
740 roccat_disconnect(kone->chrdev_minor);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100741 kfree(hid_get_drvdata(hdev));
742 }
743}
744
745static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
746{
747 int retval;
748
749 retval = hid_parse(hdev);
750 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800751 hid_err(hdev, "parse failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100752 goto exit;
753 }
754
755 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
756 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800757 hid_err(hdev, "hw start failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100758 goto exit;
759 }
760
761 retval = kone_init_specials(hdev);
762 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800763 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100764 goto exit_stop;
765 }
766
767 return 0;
768
769exit_stop:
770 hid_hw_stop(hdev);
771exit:
772 return retval;
773}
774
775static void kone_remove(struct hid_device *hdev)
776{
777 kone_remove_specials(hdev);
778 hid_hw_stop(hdev);
779}
780
Stefan Achatz48e70802010-05-18 18:31:04 +0200781/* handle special events and keep actual profile and dpi values up to date */
782static void kone_keep_values_up_to_date(struct kone_device *kone,
783 struct kone_mouse_event const *event)
784{
785 switch (event->event) {
786 case kone_mouse_event_switch_profile:
Stefan Achatz1c5784d2011-08-27 15:24:36 +0200787 kone->actual_dpi = kone->profiles[event->value - 1].
788 startup_dpi;
Stefan Achatz48e70802010-05-18 18:31:04 +0200789 case kone_mouse_event_osd_profile:
790 kone->actual_profile = event->value;
Stefan Achatz48e70802010-05-18 18:31:04 +0200791 break;
792 case kone_mouse_event_switch_dpi:
793 case kone_mouse_event_osd_dpi:
794 kone->actual_dpi = event->value;
795 break;
796 }
797}
798
Stefan Achatz206f5f22010-05-19 18:55:16 +0200799static void kone_report_to_chrdev(struct kone_device const *kone,
800 struct kone_mouse_event const *event)
801{
802 struct kone_roccat_report roccat_report;
803
804 switch (event->event) {
805 case kone_mouse_event_switch_profile:
806 case kone_mouse_event_switch_dpi:
807 case kone_mouse_event_osd_profile:
808 case kone_mouse_event_osd_dpi:
809 roccat_report.event = event->event;
810 roccat_report.value = event->value;
811 roccat_report.key = 0;
812 roccat_report_event(kone->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100813 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200814 break;
815 case kone_mouse_event_call_overlong_macro:
Stefan Achatzb42065f2013-04-07 07:08:44 +0200816 case kone_mouse_event_multimedia:
Stefan Achatz206f5f22010-05-19 18:55:16 +0200817 if (event->value == kone_keystroke_action_press) {
Stefan Achatzb42065f2013-04-07 07:08:44 +0200818 roccat_report.event = event->event;
Stefan Achatz206f5f22010-05-19 18:55:16 +0200819 roccat_report.value = kone->actual_profile;
820 roccat_report.key = event->macro_key;
821 roccat_report_event(kone->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100822 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200823 }
824 break;
825 }
826
827}
828
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100829/*
830 * Is called for keyboard- and mousepart.
831 * Only mousepart gets informations about special events in its extended event
832 * structure.
833 */
834static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
835 u8 *data, int size)
836{
837 struct kone_device *kone = hid_get_drvdata(hdev);
838 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
839
840 /* keyboard events are always processed by default handler */
841 if (size != sizeof(struct kone_mouse_event))
842 return 0;
843
Stefan Achatz901e64d2011-06-12 10:02:44 +0200844 if (kone == NULL)
845 return 0;
846
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100847 /*
Stefan Achatz73b35772010-05-19 13:53:22 +0200848 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
849 * Pressed button is reported in each movement event.
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100850 * Workaround sends only one event per press.
851 */
Stefan Achatz73b35772010-05-19 13:53:22 +0200852 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
853 memcpy(&kone->last_mouse_event, event,
854 sizeof(struct kone_mouse_event));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100855 else
Stefan Achatz73b35772010-05-19 13:53:22 +0200856 memset(&event->tilt, 0, 5);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100857
Stefan Achatz48e70802010-05-18 18:31:04 +0200858 kone_keep_values_up_to_date(kone, event);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100859
Stefan Achatz206f5f22010-05-19 18:55:16 +0200860 if (kone->roccat_claimed)
861 kone_report_to_chrdev(kone, event);
862
Stefan Achatz48e70802010-05-18 18:31:04 +0200863 return 0; /* always do further processing */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100864}
865
866static const struct hid_device_id kone_devices[] = {
867 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
868 { }
869};
870
871MODULE_DEVICE_TABLE(hid, kone_devices);
872
873static struct hid_driver kone_driver = {
874 .name = "kone",
875 .id_table = kone_devices,
876 .probe = kone_probe,
877 .remove = kone_remove,
878 .raw_event = kone_raw_event
879};
880
Stefan Achatz00237bc2010-05-08 17:20:38 +0200881static int __init kone_init(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100882{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000883 int retval;
884
885 /* class name has to be same as driver name */
886 kone_class = class_create(THIS_MODULE, "kone");
887 if (IS_ERR(kone_class))
888 return PTR_ERR(kone_class);
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700889 kone_class->dev_groups = kone_groups;
Stefan Achatz5012aad2010-11-26 19:57:33 +0000890
891 retval = hid_register_driver(&kone_driver);
892 if (retval)
893 class_destroy(kone_class);
894 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100895}
896
Stefan Achatz00237bc2010-05-08 17:20:38 +0200897static void __exit kone_exit(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100898{
899 hid_unregister_driver(&kone_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100900 class_destroy(kone_class);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100901}
902
903module_init(kone_init);
904module_exit(kone_exit);
905
Stefan Achatz1f749d82010-05-12 17:43:34 +0200906MODULE_AUTHOR("Stefan Achatz");
907MODULE_DESCRIPTION("USB Roccat Kone driver");
908MODULE_LICENSE("GPL v2");