blob: c29265055ac1a3b5d10d209b69ba55ab202c6a5c [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;
Hans Duedal6354b7e2014-10-11 17:01:49 +020049
Stefan Achatz3200a6a2011-08-27 15:24:51 +020050 roccat_report.event = kone_mouse_event_switch_profile;
51 roccat_report.value = new_profile;
52 roccat_report.key = 0;
53 roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
54}
55
Stefan Achatz1edd5b42011-06-01 15:54:17 +020056static int kone_receive(struct usb_device *usb_dev, uint usb_command,
57 void *data, uint size)
58{
59 char *buf;
60 int len;
61
62 buf = kmalloc(size, GFP_KERNEL);
63 if (buf == NULL)
64 return -ENOMEM;
65
66 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
67 HID_REQ_GET_REPORT,
68 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
69 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
70
71 memcpy(data, buf, size);
72 kfree(buf);
73 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
74}
75
76static int kone_send(struct usb_device *usb_dev, uint usb_command,
77 void const *data, uint size)
78{
79 char *buf;
80 int len;
81
Thomas Meyer4c33a882011-11-17 23:43:40 +010082 buf = kmemdup(data, size, GFP_KERNEL);
Stefan Achatz1edd5b42011-06-01 15:54:17 +020083 if (buf == NULL)
84 return -ENOMEM;
85
Stefan Achatz1edd5b42011-06-01 15:54:17 +020086 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
87 HID_REQ_SET_REPORT,
88 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
89 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
90
91 kfree(buf);
92 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
93}
94
Stefan Achatz5012aad2010-11-26 19:57:33 +000095/* kone_class is used for creating sysfs attributes via roccat char device */
96static struct class *kone_class;
97
Stefan Achatz14bf62c2010-03-18 16:19:43 +010098static void kone_set_settings_checksum(struct kone_settings *settings)
99{
100 uint16_t checksum = 0;
101 unsigned char *address = (unsigned char *)settings;
102 int i;
103
104 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
105 checksum += *address;
106 settings->checksum = cpu_to_le16(checksum);
107}
108
109/*
110 * Checks success after writing data to mouse
111 * On success returns 0
112 * On failure returns errno
113 */
114static int kone_check_write(struct usb_device *usb_dev)
115{
Stefan Achatz5772f632011-01-30 13:38:23 +0100116 int retval;
117 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100118
119 do {
120 /*
121 * Mouse needs 50 msecs until it says ok, but there are
122 * 30 more msecs needed for next write to work.
123 */
124 msleep(80);
125
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200126 retval = kone_receive(usb_dev,
Stefan Achatz5772f632011-01-30 13:38:23 +0100127 kone_command_confirm_write, &data, 1);
128 if (retval)
129 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100130
131 /*
132 * value of 3 seems to mean something like
133 * "not finished yet, but it looks good"
134 * So check again after a moment.
135 */
Stefan Achatz5772f632011-01-30 13:38:23 +0100136 } while (data == 3);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100137
Stefan Achatz5772f632011-01-30 13:38:23 +0100138 if (data == 1) /* everything alright */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100139 return 0;
Stefan Achatz5772f632011-01-30 13:38:23 +0100140
141 /* unknown answer */
Stefan Achatz4ec141a2012-05-20 22:45:08 +0200142 dev_err(&usb_dev->dev, "got retval %d when checking write\n", data);
Stefan Achatz5772f632011-01-30 13:38:23 +0100143 return -EIO;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100144}
145
146/*
147 * Reads settings from mouse and stores it in @buf
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100148 * On success returns 0
149 * On failure returns errno
150 */
151static int kone_get_settings(struct usb_device *usb_dev,
152 struct kone_settings *buf)
153{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200154 return kone_receive(usb_dev, kone_command_settings, buf,
Stefan Achatz5772f632011-01-30 13:38:23 +0100155 sizeof(struct kone_settings));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100156}
157
158/*
159 * Writes settings from @buf to mouse
160 * On success returns 0
161 * On failure returns errno
162 */
163static int kone_set_settings(struct usb_device *usb_dev,
164 struct kone_settings const *settings)
165{
Stefan Achatz5772f632011-01-30 13:38:23 +0100166 int retval;
Hans Duedal6354b7e2014-10-11 17:01:49 +0200167
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}
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700329static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
330 kone_sysfs_write_settings, sizeof(struct kone_settings));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100331
Stefan Achatz14a057f2010-11-26 19:57:38 +0000332static ssize_t kone_sysfs_read_profilex(struct file *fp,
333 struct kobject *kobj, struct bin_attribute *attr,
334 char *buf, loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000335 struct device *dev =
336 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100337 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
338
339 if (off >= sizeof(struct kone_profile))
340 return 0;
341
342 if (off + count > sizeof(struct kone_profile))
343 count = sizeof(struct kone_profile) - off;
344
345 mutex_lock(&kone->kone_lock);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000346 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100347 mutex_unlock(&kone->kone_lock);
348
349 return count;
350}
351
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100352/* Writes data only if different to stored data */
Stefan Achatz14a057f2010-11-26 19:57:38 +0000353static ssize_t kone_sysfs_write_profilex(struct file *fp,
354 struct kobject *kobj, struct bin_attribute *attr,
355 char *buf, loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000356 struct device *dev =
357 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100358 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
359 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
360 struct kone_profile *profile;
361 int retval = 0, difference;
362
363 /* I need to get my data in one piece */
364 if (off != 0 || count != sizeof(struct kone_profile))
365 return -EINVAL;
366
Stefan Achatz14a057f2010-11-26 19:57:38 +0000367 profile = &kone->profiles[*(uint *)(attr->private)];
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100368
369 mutex_lock(&kone->kone_lock);
370 difference = memcmp(buf, profile, sizeof(struct kone_profile));
371 if (difference) {
372 retval = kone_set_profile(usb_dev,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000373 (struct kone_profile const *)buf,
374 *(uint *)(attr->private) + 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100375 if (!retval)
376 memcpy(profile, buf, sizeof(struct kone_profile));
377 }
378 mutex_unlock(&kone->kone_lock);
379
380 if (retval)
381 return retval;
382
383 return sizeof(struct kone_profile);
384}
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700385#define PROFILE_ATTR(number) \
386static struct bin_attribute bin_attr_profile##number = { \
Stefan Achatz550dbf42013-09-28 05:57:46 +0200387 .attr = { .name = "profile" #number, .mode = 0660 }, \
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700388 .size = sizeof(struct kone_profile), \
389 .read = kone_sysfs_read_profilex, \
390 .write = kone_sysfs_write_profilex, \
Greg Kroah-Hartman515ad4d2013-08-19 21:56:46 -0700391 .private = &profile_numbers[number-1], \
Hans Duedal6354b7e2014-10-11 17:01:49 +0200392}
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700393PROFILE_ATTR(1);
394PROFILE_ATTR(2);
395PROFILE_ATTR(3);
396PROFILE_ATTR(4);
397PROFILE_ATTR(5);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100398
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100399static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000402 struct kone_device *kone =
403 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100404 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
405}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700406static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100407
408static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
409 struct device_attribute *attr, char *buf)
410{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000411 struct kone_device *kone =
412 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100413 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
414}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700415static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100416
417/* weight is read each time, since we don't get informed when it's changed */
418static ssize_t kone_sysfs_show_weight(struct device *dev,
419 struct device_attribute *attr, char *buf)
420{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000421 struct kone_device *kone;
422 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100423 int weight = 0;
424 int retval;
425
Stefan Achatz5012aad2010-11-26 19:57:33 +0000426 dev = dev->parent->parent;
427 kone = hid_get_drvdata(dev_get_drvdata(dev));
428 usb_dev = interface_to_usbdev(to_usb_interface(dev));
429
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100430 mutex_lock(&kone->kone_lock);
431 retval = kone_get_weight(usb_dev, &weight);
432 mutex_unlock(&kone->kone_lock);
433
434 if (retval)
435 return retval;
436 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
437}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700438static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100439
440static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000443 struct kone_device *kone =
444 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100445 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
446}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700447static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
448 NULL);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100449
450static ssize_t kone_sysfs_show_tcu(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000453 struct kone_device *kone =
454 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100455 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
456}
457
458static int kone_tcu_command(struct usb_device *usb_dev, int number)
459{
Stefan Achatz5772f632011-01-30 13:38:23 +0100460 unsigned char value;
Hans Duedal6354b7e2014-10-11 17:01:49 +0200461
Stefan Achatz5772f632011-01-30 13:38:23 +0100462 value = number;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200463 return kone_send(usb_dev, kone_command_calibrate, &value, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100464}
465
466/*
467 * Calibrating the tcu is the only action that changes settings data inside the
468 * mouse, so this data needs to be reread
469 */
470static ssize_t kone_sysfs_set_tcu(struct device *dev,
471 struct device_attribute *attr, char const *buf, size_t size)
472{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000473 struct kone_device *kone;
474 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100475 int retval;
476 unsigned long state;
477
Stefan Achatz5012aad2010-11-26 19:57:33 +0000478 dev = dev->parent->parent;
479 kone = hid_get_drvdata(dev_get_drvdata(dev));
480 usb_dev = interface_to_usbdev(to_usb_interface(dev));
481
Jingoo Handfc450b2013-07-19 15:53:16 +0900482 retval = kstrtoul(buf, 10, &state);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100483 if (retval)
484 return retval;
485
486 if (state != 0 && state != 1)
487 return -EINVAL;
488
489 mutex_lock(&kone->kone_lock);
490
491 if (state == 1) { /* state activate */
492 retval = kone_tcu_command(usb_dev, 1);
493 if (retval)
494 goto exit_unlock;
495 retval = kone_tcu_command(usb_dev, 2);
496 if (retval)
497 goto exit_unlock;
498 ssleep(5); /* tcu needs this time for calibration */
499 retval = kone_tcu_command(usb_dev, 3);
500 if (retval)
501 goto exit_unlock;
502 retval = kone_tcu_command(usb_dev, 0);
503 if (retval)
504 goto exit_unlock;
505 retval = kone_tcu_command(usb_dev, 4);
506 if (retval)
507 goto exit_unlock;
508 /*
509 * Kone needs this time to settle things.
510 * Reading settings too early will result in invalid data.
511 * Roccat's driver waits 1 sec, maybe this time could be
512 * shortened.
513 */
514 ssleep(1);
515 }
516
517 /* calibration changes values in settings, so reread */
518 retval = kone_get_settings(usb_dev, &kone->settings);
519 if (retval)
520 goto exit_no_settings;
521
522 /* only write settings back if activation state is different */
523 if (kone->settings.tcu != state) {
524 kone->settings.tcu = state;
525 kone_set_settings_checksum(&kone->settings);
526
527 retval = kone_set_settings(usb_dev, &kone->settings);
528 if (retval) {
Stefan Achatz4ec141a2012-05-20 22:45:08 +0200529 dev_err(&usb_dev->dev, "couldn't set tcu state\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100530 /*
531 * try to reread valid settings into buffer overwriting
532 * first error code
533 */
534 retval = kone_get_settings(usb_dev, &kone->settings);
535 if (retval)
536 goto exit_no_settings;
537 goto exit_unlock;
538 }
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200539 /* calibration resets profile */
540 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100541 }
542
543 retval = size;
544exit_no_settings:
Stefan Achatz4ec141a2012-05-20 22:45:08 +0200545 dev_err(&usb_dev->dev, "couldn't read settings\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100546exit_unlock:
547 mutex_unlock(&kone->kone_lock);
548 return retval;
549}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700550static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100551
552static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
553 struct device_attribute *attr, char *buf)
554{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000555 struct kone_device *kone =
556 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100557 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
558}
559
560static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
561 struct device_attribute *attr, char const *buf, size_t size)
562{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000563 struct kone_device *kone;
564 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100565 int retval;
566 unsigned long new_startup_profile;
567
Stefan Achatz5012aad2010-11-26 19:57:33 +0000568 dev = dev->parent->parent;
569 kone = hid_get_drvdata(dev_get_drvdata(dev));
570 usb_dev = interface_to_usbdev(to_usb_interface(dev));
571
Jingoo Handfc450b2013-07-19 15:53:16 +0900572 retval = kstrtoul(buf, 10, &new_startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100573 if (retval)
574 return retval;
575
576 if (new_startup_profile < 1 || new_startup_profile > 5)
577 return -EINVAL;
578
579 mutex_lock(&kone->kone_lock);
580
581 kone->settings.startup_profile = new_startup_profile;
582 kone_set_settings_checksum(&kone->settings);
583
584 retval = kone_set_settings(usb_dev, &kone->settings);
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200585 if (retval) {
586 mutex_unlock(&kone->kone_lock);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100587 return retval;
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200588 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100589
590 /* changing the startup profile immediately activates this profile */
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200591 kone_profile_activated(kone, new_startup_profile);
Stefan Achatz3200a6a2011-08-27 15:24:51 +0200592 kone_profile_report(kone, new_startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100593
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200594 mutex_unlock(&kone->kone_lock);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100595 return size;
596}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700597static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
598 kone_sysfs_set_startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100599
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700600static struct attribute *kone_attrs[] = {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000601 /*
602 * Read actual dpi settings.
603 * Returns raw value for further processing. Refer to enum
604 * kone_polling_rates to get real value.
605 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700606 &dev_attr_actual_dpi.attr,
607 &dev_attr_actual_profile.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100608
Stefan Achatz5012aad2010-11-26 19:57:33 +0000609 /*
610 * The mouse can be equipped with one of four supplied weights from 5
611 * to 20 grams which are recognized and its value can be read out.
612 * This returns the raw value reported by the mouse for easy evaluation
613 * by software. Refer to enum kone_weights to get corresponding real
614 * weight.
615 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700616 &dev_attr_weight.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100617
Stefan Achatz5012aad2010-11-26 19:57:33 +0000618 /*
619 * Prints firmware version stored in mouse as integer.
620 * The raw value reported by the mouse is returned for easy evaluation,
621 * to get the real version number the decimal point has to be shifted 2
622 * positions to the left. E.g. a value of 138 means 1.38.
623 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700624 &dev_attr_firmware_version.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100625
Stefan Achatz5012aad2010-11-26 19:57:33 +0000626 /*
627 * Prints state of Tracking Control Unit as number where 0 = off and
628 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
629 * activates the tcu
630 */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700631 &dev_attr_tcu.attr,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100632
Stefan Achatz5012aad2010-11-26 19:57:33 +0000633 /* Prints and takes the number of the profile the mouse starts with */
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700634 &dev_attr_startup_profile.attr,
635 NULL,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100636};
637
Greg Kroah-Hartman71b230a2013-08-19 21:40:26 -0700638static struct bin_attribute *kone_bin_attributes[] = {
639 &bin_attr_settings,
640 &bin_attr_profile1,
641 &bin_attr_profile2,
642 &bin_attr_profile3,
643 &bin_attr_profile4,
644 &bin_attr_profile5,
645 NULL,
646};
647
648static const struct attribute_group kone_group = {
649 .attrs = kone_attrs,
650 .bin_attrs = kone_bin_attributes,
651};
652
653static const struct attribute_group *kone_groups[] = {
654 &kone_group,
655 NULL,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100656};
657
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100658static int kone_init_kone_device_struct(struct usb_device *usb_dev,
659 struct kone_device *kone)
660{
661 uint i;
662 int retval;
663
664 mutex_init(&kone->kone_lock);
665
666 for (i = 0; i < 5; ++i) {
667 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
668 if (retval)
669 return retval;
670 }
671
672 retval = kone_get_settings(usb_dev, &kone->settings);
673 if (retval)
674 return retval;
675
676 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
677 if (retval)
678 return retval;
679
Stefan Achatzbd9c35d2011-08-27 15:24:48 +0200680 kone_profile_activated(kone, kone->settings.startup_profile);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100681
682 return 0;
683}
684
685/*
686 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
687 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
688 * module.
689 * Secial behaviour is bound only to mousepart since only mouseevents contain
690 * additional notifications.
691 */
692static int kone_init_specials(struct hid_device *hdev)
693{
694 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
695 struct usb_device *usb_dev = interface_to_usbdev(intf);
696 struct kone_device *kone;
697 int retval;
698
699 if (intf->cur_altsetting->desc.bInterfaceProtocol
700 == USB_INTERFACE_PROTOCOL_MOUSE) {
701
702 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
Hans Duedal6354b7e2014-10-11 17:01:49 +0200703 if (!kone)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100704 return -ENOMEM;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100705 hid_set_drvdata(hdev, kone);
706
707 retval = kone_init_kone_device_struct(usb_dev, kone);
708 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800709 hid_err(hdev, "couldn't init struct kone_device\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100710 goto exit_free;
711 }
Stefan Achatz206f5f22010-05-19 18:55:16 +0200712
Stefan Achatz8211e462011-01-30 13:38:25 +0100713 retval = roccat_connect(kone_class, hdev,
714 sizeof(struct kone_roccat_report));
Stefan Achatz206f5f22010-05-19 18:55:16 +0200715 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800716 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz206f5f22010-05-19 18:55:16 +0200717 /* be tolerant about not getting chrdev */
718 } else {
719 kone->roccat_claimed = 1;
720 kone->chrdev_minor = retval;
721 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100722 } else {
723 hid_set_drvdata(hdev, NULL);
724 }
725
726 return 0;
727exit_free:
728 kfree(kone);
729 return retval;
730}
731
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100732static void kone_remove_specials(struct hid_device *hdev)
733{
734 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200735 struct kone_device *kone;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100736
737 if (intf->cur_altsetting->desc.bInterfaceProtocol
738 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatz206f5f22010-05-19 18:55:16 +0200739 kone = hid_get_drvdata(hdev);
740 if (kone->roccat_claimed)
741 roccat_disconnect(kone->chrdev_minor);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100742 kfree(hid_get_drvdata(hdev));
743 }
744}
745
746static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
747{
748 int retval;
749
750 retval = hid_parse(hdev);
751 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800752 hid_err(hdev, "parse failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100753 goto exit;
754 }
755
756 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
757 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800758 hid_err(hdev, "hw start failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100759 goto exit;
760 }
761
762 retval = kone_init_specials(hdev);
763 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800764 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100765 goto exit_stop;
766 }
767
768 return 0;
769
770exit_stop:
771 hid_hw_stop(hdev);
772exit:
773 return retval;
774}
775
776static void kone_remove(struct hid_device *hdev)
777{
778 kone_remove_specials(hdev);
779 hid_hw_stop(hdev);
780}
781
Stefan Achatz48e70802010-05-18 18:31:04 +0200782/* handle special events and keep actual profile and dpi values up to date */
783static void kone_keep_values_up_to_date(struct kone_device *kone,
784 struct kone_mouse_event const *event)
785{
786 switch (event->event) {
787 case kone_mouse_event_switch_profile:
Stefan Achatz1c5784d2011-08-27 15:24:36 +0200788 kone->actual_dpi = kone->profiles[event->value - 1].
789 startup_dpi;
Stefan Achatz48e70802010-05-18 18:31:04 +0200790 case kone_mouse_event_osd_profile:
791 kone->actual_profile = event->value;
Stefan Achatz48e70802010-05-18 18:31:04 +0200792 break;
793 case kone_mouse_event_switch_dpi:
794 case kone_mouse_event_osd_dpi:
795 kone->actual_dpi = event->value;
796 break;
797 }
798}
799
Stefan Achatz206f5f22010-05-19 18:55:16 +0200800static void kone_report_to_chrdev(struct kone_device const *kone,
801 struct kone_mouse_event const *event)
802{
803 struct kone_roccat_report roccat_report;
804
805 switch (event->event) {
806 case kone_mouse_event_switch_profile:
807 case kone_mouse_event_switch_dpi:
808 case kone_mouse_event_osd_profile:
809 case kone_mouse_event_osd_dpi:
810 roccat_report.event = event->event;
811 roccat_report.value = event->value;
812 roccat_report.key = 0;
813 roccat_report_event(kone->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100814 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200815 break;
816 case kone_mouse_event_call_overlong_macro:
Stefan Achatzb42065f2013-04-07 07:08:44 +0200817 case kone_mouse_event_multimedia:
Stefan Achatz206f5f22010-05-19 18:55:16 +0200818 if (event->value == kone_keystroke_action_press) {
Stefan Achatzb42065f2013-04-07 07:08:44 +0200819 roccat_report.event = event->event;
Stefan Achatz206f5f22010-05-19 18:55:16 +0200820 roccat_report.value = kone->actual_profile;
821 roccat_report.key = event->macro_key;
822 roccat_report_event(kone->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100823 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200824 }
825 break;
826 }
827
828}
829
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100830/*
831 * Is called for keyboard- and mousepart.
832 * Only mousepart gets informations about special events in its extended event
833 * structure.
834 */
835static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
836 u8 *data, int size)
837{
838 struct kone_device *kone = hid_get_drvdata(hdev);
839 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
840
841 /* keyboard events are always processed by default handler */
842 if (size != sizeof(struct kone_mouse_event))
843 return 0;
844
Stefan Achatz901e64d2011-06-12 10:02:44 +0200845 if (kone == NULL)
846 return 0;
847
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100848 /*
Stefan Achatz73b35772010-05-19 13:53:22 +0200849 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
850 * Pressed button is reported in each movement event.
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100851 * Workaround sends only one event per press.
852 */
Stefan Achatz73b35772010-05-19 13:53:22 +0200853 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
854 memcpy(&kone->last_mouse_event, event,
855 sizeof(struct kone_mouse_event));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100856 else
Stefan Achatz73b35772010-05-19 13:53:22 +0200857 memset(&event->tilt, 0, 5);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100858
Stefan Achatz48e70802010-05-18 18:31:04 +0200859 kone_keep_values_up_to_date(kone, event);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100860
Stefan Achatz206f5f22010-05-19 18:55:16 +0200861 if (kone->roccat_claimed)
862 kone_report_to_chrdev(kone, event);
863
Stefan Achatz48e70802010-05-18 18:31:04 +0200864 return 0; /* always do further processing */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100865}
866
867static const struct hid_device_id kone_devices[] = {
868 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
869 { }
870};
871
872MODULE_DEVICE_TABLE(hid, kone_devices);
873
874static struct hid_driver kone_driver = {
875 .name = "kone",
876 .id_table = kone_devices,
877 .probe = kone_probe,
878 .remove = kone_remove,
879 .raw_event = kone_raw_event
880};
881
Stefan Achatz00237bc2010-05-08 17:20:38 +0200882static int __init kone_init(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100883{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000884 int retval;
885
886 /* class name has to be same as driver name */
887 kone_class = class_create(THIS_MODULE, "kone");
888 if (IS_ERR(kone_class))
889 return PTR_ERR(kone_class);
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700890 kone_class->dev_groups = kone_groups;
Stefan Achatz5012aad2010-11-26 19:57:33 +0000891
892 retval = hid_register_driver(&kone_driver);
893 if (retval)
894 class_destroy(kone_class);
895 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100896}
897
Stefan Achatz00237bc2010-05-08 17:20:38 +0200898static void __exit kone_exit(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100899{
900 hid_unregister_driver(&kone_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100901 class_destroy(kone_class);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100902}
903
904module_init(kone_init);
905module_exit(kone_exit);
906
Stefan Achatz1f749d82010-05-12 17:43:34 +0200907MODULE_AUTHOR("Stefan Achatz");
908MODULE_DESCRIPTION("USB Roccat Kone driver");
909MODULE_LICENSE("GPL v2");