blob: 2b8f3a31ffb314aa814e560d6fa7c221c9c3035a [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 Achatz1edd5b42011-06-01 15:54:17 +020040static int kone_receive(struct usb_device *usb_dev, uint usb_command,
41 void *data, uint size)
42{
43 char *buf;
44 int len;
45
46 buf = kmalloc(size, GFP_KERNEL);
47 if (buf == NULL)
48 return -ENOMEM;
49
50 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
51 HID_REQ_GET_REPORT,
52 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
53 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
54
55 memcpy(data, buf, size);
56 kfree(buf);
57 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
58}
59
60static int kone_send(struct usb_device *usb_dev, uint usb_command,
61 void const *data, uint size)
62{
63 char *buf;
64 int len;
65
66 buf = kmalloc(size, GFP_KERNEL);
67 if (buf == NULL)
68 return -ENOMEM;
69
70 memcpy(buf, data, size);
71
72 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
73 HID_REQ_SET_REPORT,
74 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
75 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
76
77 kfree(buf);
78 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
79}
80
Stefan Achatz5012aad2010-11-26 19:57:33 +000081/* kone_class is used for creating sysfs attributes via roccat char device */
82static struct class *kone_class;
83
Stefan Achatz14bf62c2010-03-18 16:19:43 +010084static void kone_set_settings_checksum(struct kone_settings *settings)
85{
86 uint16_t checksum = 0;
87 unsigned char *address = (unsigned char *)settings;
88 int i;
89
90 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
91 checksum += *address;
92 settings->checksum = cpu_to_le16(checksum);
93}
94
95/*
96 * Checks success after writing data to mouse
97 * On success returns 0
98 * On failure returns errno
99 */
100static int kone_check_write(struct usb_device *usb_dev)
101{
Stefan Achatz5772f632011-01-30 13:38:23 +0100102 int retval;
103 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100104
105 do {
106 /*
107 * Mouse needs 50 msecs until it says ok, but there are
108 * 30 more msecs needed for next write to work.
109 */
110 msleep(80);
111
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200112 retval = kone_receive(usb_dev,
Stefan Achatz5772f632011-01-30 13:38:23 +0100113 kone_command_confirm_write, &data, 1);
114 if (retval)
115 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100116
117 /*
118 * value of 3 seems to mean something like
119 * "not finished yet, but it looks good"
120 * So check again after a moment.
121 */
Stefan Achatz5772f632011-01-30 13:38:23 +0100122 } while (data == 3);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100123
Stefan Achatz5772f632011-01-30 13:38:23 +0100124 if (data == 1) /* everything alright */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100125 return 0;
Stefan Achatz5772f632011-01-30 13:38:23 +0100126
127 /* unknown answer */
128 hid_err(usb_dev, "got retval %d when checking write\n", data);
129 return -EIO;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100130}
131
132/*
133 * Reads settings from mouse and stores it in @buf
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100134 * On success returns 0
135 * On failure returns errno
136 */
137static int kone_get_settings(struct usb_device *usb_dev,
138 struct kone_settings *buf)
139{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200140 return kone_receive(usb_dev, kone_command_settings, buf,
Stefan Achatz5772f632011-01-30 13:38:23 +0100141 sizeof(struct kone_settings));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100142}
143
144/*
145 * Writes settings from @buf to mouse
146 * On success returns 0
147 * On failure returns errno
148 */
149static int kone_set_settings(struct usb_device *usb_dev,
150 struct kone_settings const *settings)
151{
Stefan Achatz5772f632011-01-30 13:38:23 +0100152 int retval;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200153 retval = kone_send(usb_dev, kone_command_settings,
Stefan Achatz5772f632011-01-30 13:38:23 +0100154 settings, sizeof(struct kone_settings));
155 if (retval)
156 return retval;
157 return kone_check_write(usb_dev);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100158}
159
160/*
161 * Reads profile data from mouse and stores it in @buf
162 * @number: profile number to read
163 * On success returns 0
164 * On failure returns errno
165 */
166static int kone_get_profile(struct usb_device *usb_dev,
167 struct kone_profile *buf, int number)
168{
169 int len;
170
171 if (number < 1 || number > 5)
172 return -EINVAL;
173
174 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
175 USB_REQ_CLEAR_FEATURE,
176 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
177 kone_command_profile, number, buf,
178 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
179
180 if (len != sizeof(struct kone_profile))
181 return -EIO;
182
183 return 0;
184}
185
186/*
187 * Writes profile data to mouse.
188 * @number: profile number to write
189 * On success returns 0
190 * On failure returns errno
191 */
192static int kone_set_profile(struct usb_device *usb_dev,
193 struct kone_profile const *profile, int number)
194{
195 int len;
196
197 if (number < 1 || number > 5)
198 return -EINVAL;
199
200 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
201 USB_REQ_SET_CONFIGURATION,
202 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Stefan Achatz5772f632011-01-30 13:38:23 +0100203 kone_command_profile, number, (void *)profile,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100204 sizeof(struct kone_profile),
205 USB_CTRL_SET_TIMEOUT);
206
207 if (len != sizeof(struct kone_profile))
208 return len;
209
210 if (kone_check_write(usb_dev))
211 return -EIO;
212
213 return 0;
214}
215
216/*
217 * Reads value of "fast-clip-weight" and stores it in @result
218 * On success returns 0
219 * On failure returns errno
220 */
221static int kone_get_weight(struct usb_device *usb_dev, int *result)
222{
Stefan Achatz5772f632011-01-30 13:38:23 +0100223 int retval;
224 uint8_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100225
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200226 retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100227
Stefan Achatz5772f632011-01-30 13:38:23 +0100228 if (retval)
229 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100230
Stefan Achatz5772f632011-01-30 13:38:23 +0100231 *result = (int)data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100232 return 0;
233}
234
235/*
236 * Reads firmware_version of mouse and stores it in @result
237 * On success returns 0
238 * On failure returns errno
239 */
240static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
241{
Stefan Achatz5772f632011-01-30 13:38:23 +0100242 int retval;
243 uint16_t data;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100244
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200245 retval = kone_receive(usb_dev, kone_command_firmware_version,
Stefan Achatz5772f632011-01-30 13:38:23 +0100246 &data, 2);
247 if (retval)
248 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100249
Stefan Achatz5772f632011-01-30 13:38:23 +0100250 *result = le16_to_cpu(data);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100251 return 0;
252}
253
Stephen Rothwell5f277622010-05-21 16:15:32 +1000254static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100255 struct bin_attribute *attr, char *buf,
256 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000257 struct device *dev =
258 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100259 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
260
261 if (off >= sizeof(struct kone_settings))
262 return 0;
263
264 if (off + count > sizeof(struct kone_settings))
265 count = sizeof(struct kone_settings) - off;
266
267 mutex_lock(&kone->kone_lock);
Stefan Achatzcab6b162010-06-20 19:19:06 +0200268 memcpy(buf, ((char const *)&kone->settings) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100269 mutex_unlock(&kone->kone_lock);
270
271 return count;
272}
273
274/*
275 * Writing settings automatically activates startup_profile.
276 * This function keeps values in kone_device up to date and assumes that in
277 * case of error the old data is still valid
278 */
Stephen Rothwell5f277622010-05-21 16:15:32 +1000279static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100280 struct bin_attribute *attr, char *buf,
281 loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000282 struct device *dev =
283 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100284 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
285 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
286 int retval = 0, difference;
287
288 /* I need to get my data in one piece */
289 if (off != 0 || count != sizeof(struct kone_settings))
290 return -EINVAL;
291
292 mutex_lock(&kone->kone_lock);
293 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
294 if (difference) {
295 retval = kone_set_settings(usb_dev,
296 (struct kone_settings const *)buf);
297 if (!retval)
298 memcpy(&kone->settings, buf,
299 sizeof(struct kone_settings));
300 }
301 mutex_unlock(&kone->kone_lock);
302
303 if (retval)
304 return retval;
305
306 /*
307 * If we get here, treat settings as okay and update actual values
308 * according to startup_profile
309 */
310 kone->actual_profile = kone->settings.startup_profile;
311 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
312
313 return sizeof(struct kone_settings);
314}
315
Stefan Achatz14a057f2010-11-26 19:57:38 +0000316static ssize_t kone_sysfs_read_profilex(struct file *fp,
317 struct kobject *kobj, struct bin_attribute *attr,
318 char *buf, loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000319 struct device *dev =
320 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100321 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
322
323 if (off >= sizeof(struct kone_profile))
324 return 0;
325
326 if (off + count > sizeof(struct kone_profile))
327 count = sizeof(struct kone_profile) - off;
328
329 mutex_lock(&kone->kone_lock);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000330 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100331 mutex_unlock(&kone->kone_lock);
332
333 return count;
334}
335
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100336/* Writes data only if different to stored data */
Stefan Achatz14a057f2010-11-26 19:57:38 +0000337static ssize_t kone_sysfs_write_profilex(struct file *fp,
338 struct kobject *kobj, struct bin_attribute *attr,
339 char *buf, loff_t off, size_t count) {
Stefan Achatz5012aad2010-11-26 19:57:33 +0000340 struct device *dev =
341 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100342 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
343 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
344 struct kone_profile *profile;
345 int retval = 0, difference;
346
347 /* I need to get my data in one piece */
348 if (off != 0 || count != sizeof(struct kone_profile))
349 return -EINVAL;
350
Stefan Achatz14a057f2010-11-26 19:57:38 +0000351 profile = &kone->profiles[*(uint *)(attr->private)];
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100352
353 mutex_lock(&kone->kone_lock);
354 difference = memcmp(buf, profile, sizeof(struct kone_profile));
355 if (difference) {
356 retval = kone_set_profile(usb_dev,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000357 (struct kone_profile const *)buf,
358 *(uint *)(attr->private) + 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100359 if (!retval)
360 memcpy(profile, buf, sizeof(struct kone_profile));
361 }
362 mutex_unlock(&kone->kone_lock);
363
364 if (retval)
365 return retval;
366
367 return sizeof(struct kone_profile);
368}
369
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100370static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
371 struct device_attribute *attr, char *buf)
372{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000373 struct kone_device *kone =
374 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100375 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
376}
377
378static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000381 struct kone_device *kone =
382 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100383 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
384}
385
386/* weight is read each time, since we don't get informed when it's changed */
387static ssize_t kone_sysfs_show_weight(struct device *dev,
388 struct device_attribute *attr, char *buf)
389{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000390 struct kone_device *kone;
391 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100392 int weight = 0;
393 int retval;
394
Stefan Achatz5012aad2010-11-26 19:57:33 +0000395 dev = dev->parent->parent;
396 kone = hid_get_drvdata(dev_get_drvdata(dev));
397 usb_dev = interface_to_usbdev(to_usb_interface(dev));
398
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100399 mutex_lock(&kone->kone_lock);
400 retval = kone_get_weight(usb_dev, &weight);
401 mutex_unlock(&kone->kone_lock);
402
403 if (retval)
404 return retval;
405 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
406}
407
408static ssize_t kone_sysfs_show_firmware_version(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->firmware_version);
414}
415
416static ssize_t kone_sysfs_show_tcu(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000419 struct kone_device *kone =
420 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100421 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
422}
423
424static int kone_tcu_command(struct usb_device *usb_dev, int number)
425{
Stefan Achatz5772f632011-01-30 13:38:23 +0100426 unsigned char value;
427 value = number;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200428 return kone_send(usb_dev, kone_command_calibrate, &value, 1);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100429}
430
431/*
432 * Calibrating the tcu is the only action that changes settings data inside the
433 * mouse, so this data needs to be reread
434 */
435static ssize_t kone_sysfs_set_tcu(struct device *dev,
436 struct device_attribute *attr, char const *buf, size_t size)
437{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000438 struct kone_device *kone;
439 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100440 int retval;
441 unsigned long state;
442
Stefan Achatz5012aad2010-11-26 19:57:33 +0000443 dev = dev->parent->parent;
444 kone = hid_get_drvdata(dev_get_drvdata(dev));
445 usb_dev = interface_to_usbdev(to_usb_interface(dev));
446
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100447 retval = strict_strtoul(buf, 10, &state);
448 if (retval)
449 return retval;
450
451 if (state != 0 && state != 1)
452 return -EINVAL;
453
454 mutex_lock(&kone->kone_lock);
455
456 if (state == 1) { /* state activate */
457 retval = kone_tcu_command(usb_dev, 1);
458 if (retval)
459 goto exit_unlock;
460 retval = kone_tcu_command(usb_dev, 2);
461 if (retval)
462 goto exit_unlock;
463 ssleep(5); /* tcu needs this time for calibration */
464 retval = kone_tcu_command(usb_dev, 3);
465 if (retval)
466 goto exit_unlock;
467 retval = kone_tcu_command(usb_dev, 0);
468 if (retval)
469 goto exit_unlock;
470 retval = kone_tcu_command(usb_dev, 4);
471 if (retval)
472 goto exit_unlock;
473 /*
474 * Kone needs this time to settle things.
475 * Reading settings too early will result in invalid data.
476 * Roccat's driver waits 1 sec, maybe this time could be
477 * shortened.
478 */
479 ssleep(1);
480 }
481
482 /* calibration changes values in settings, so reread */
483 retval = kone_get_settings(usb_dev, &kone->settings);
484 if (retval)
485 goto exit_no_settings;
486
487 /* only write settings back if activation state is different */
488 if (kone->settings.tcu != state) {
489 kone->settings.tcu = state;
490 kone_set_settings_checksum(&kone->settings);
491
492 retval = kone_set_settings(usb_dev, &kone->settings);
493 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800494 hid_err(usb_dev, "couldn't set tcu state\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100495 /*
496 * try to reread valid settings into buffer overwriting
497 * first error code
498 */
499 retval = kone_get_settings(usb_dev, &kone->settings);
500 if (retval)
501 goto exit_no_settings;
502 goto exit_unlock;
503 }
504 }
505
506 retval = size;
507exit_no_settings:
Joe Perches4291ee32010-12-09 19:29:03 -0800508 hid_err(usb_dev, "couldn't read settings\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100509exit_unlock:
510 mutex_unlock(&kone->kone_lock);
511 return retval;
512}
513
514static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000517 struct kone_device *kone =
518 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100519 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
520}
521
522static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
523 struct device_attribute *attr, char const *buf, size_t size)
524{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000525 struct kone_device *kone;
526 struct usb_device *usb_dev;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100527 int retval;
528 unsigned long new_startup_profile;
529
Stefan Achatz5012aad2010-11-26 19:57:33 +0000530 dev = dev->parent->parent;
531 kone = hid_get_drvdata(dev_get_drvdata(dev));
532 usb_dev = interface_to_usbdev(to_usb_interface(dev));
533
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100534 retval = strict_strtoul(buf, 10, &new_startup_profile);
535 if (retval)
536 return retval;
537
538 if (new_startup_profile < 1 || new_startup_profile > 5)
539 return -EINVAL;
540
541 mutex_lock(&kone->kone_lock);
542
543 kone->settings.startup_profile = new_startup_profile;
544 kone_set_settings_checksum(&kone->settings);
545
546 retval = kone_set_settings(usb_dev, &kone->settings);
547
548 mutex_unlock(&kone->kone_lock);
549
550 if (retval)
551 return retval;
552
553 /* changing the startup profile immediately activates this profile */
554 kone->actual_profile = new_startup_profile;
555 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
556
557 return size;
558}
559
Stefan Achatz5012aad2010-11-26 19:57:33 +0000560static struct device_attribute kone_attributes[] = {
561 /*
562 * Read actual dpi settings.
563 * Returns raw value for further processing. Refer to enum
564 * kone_polling_rates to get real value.
565 */
566 __ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL),
567 __ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100568
Stefan Achatz5012aad2010-11-26 19:57:33 +0000569 /*
570 * The mouse can be equipped with one of four supplied weights from 5
571 * to 20 grams which are recognized and its value can be read out.
572 * This returns the raw value reported by the mouse for easy evaluation
573 * by software. Refer to enum kone_weights to get corresponding real
574 * weight.
575 */
576 __ATTR(weight, 0440, kone_sysfs_show_weight, NULL),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100577
Stefan Achatz5012aad2010-11-26 19:57:33 +0000578 /*
579 * Prints firmware version stored in mouse as integer.
580 * The raw value reported by the mouse is returned for easy evaluation,
581 * to get the real version number the decimal point has to be shifted 2
582 * positions to the left. E.g. a value of 138 means 1.38.
583 */
584 __ATTR(firmware_version, 0440,
585 kone_sysfs_show_firmware_version, NULL),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100586
Stefan Achatz5012aad2010-11-26 19:57:33 +0000587 /*
588 * Prints state of Tracking Control Unit as number where 0 = off and
589 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
590 * activates the tcu
591 */
592 __ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu),
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100593
Stefan Achatz5012aad2010-11-26 19:57:33 +0000594 /* Prints and takes the number of the profile the mouse starts with */
595 __ATTR(startup_profile, 0660,
596 kone_sysfs_show_startup_profile,
597 kone_sysfs_set_startup_profile),
598 __ATTR_NULL
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100599};
600
Stefan Achatz5012aad2010-11-26 19:57:33 +0000601static struct bin_attribute kone_bin_attributes[] = {
602 {
603 .attr = { .name = "settings", .mode = 0660 },
604 .size = sizeof(struct kone_settings),
605 .read = kone_sysfs_read_settings,
606 .write = kone_sysfs_write_settings
607 },
608 {
609 .attr = { .name = "profile1", .mode = 0660 },
610 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000611 .read = kone_sysfs_read_profilex,
612 .write = kone_sysfs_write_profilex,
613 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000614 },
615 {
616 .attr = { .name = "profile2", .mode = 0660 },
617 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000618 .read = kone_sysfs_read_profilex,
619 .write = kone_sysfs_write_profilex,
620 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000621 },
622 {
623 .attr = { .name = "profile3", .mode = 0660 },
624 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000625 .read = kone_sysfs_read_profilex,
626 .write = kone_sysfs_write_profilex,
627 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000628 },
629 {
630 .attr = { .name = "profile4", .mode = 0660 },
631 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000632 .read = kone_sysfs_read_profilex,
633 .write = kone_sysfs_write_profilex,
634 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000635 },
636 {
637 .attr = { .name = "profile5", .mode = 0660 },
638 .size = sizeof(struct kone_profile),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000639 .read = kone_sysfs_read_profilex,
640 .write = kone_sysfs_write_profilex,
641 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000642 },
643 __ATTR_NULL
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100644};
645
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100646static int kone_init_kone_device_struct(struct usb_device *usb_dev,
647 struct kone_device *kone)
648{
649 uint i;
650 int retval;
651
652 mutex_init(&kone->kone_lock);
653
654 for (i = 0; i < 5; ++i) {
655 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
656 if (retval)
657 return retval;
658 }
659
660 retval = kone_get_settings(usb_dev, &kone->settings);
661 if (retval)
662 return retval;
663
664 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
665 if (retval)
666 return retval;
667
668 kone->actual_profile = kone->settings.startup_profile;
669 kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
670
671 return 0;
672}
673
674/*
675 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
676 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
677 * module.
678 * Secial behaviour is bound only to mousepart since only mouseevents contain
679 * additional notifications.
680 */
681static int kone_init_specials(struct hid_device *hdev)
682{
683 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
684 struct usb_device *usb_dev = interface_to_usbdev(intf);
685 struct kone_device *kone;
686 int retval;
687
688 if (intf->cur_altsetting->desc.bInterfaceProtocol
689 == USB_INTERFACE_PROTOCOL_MOUSE) {
690
691 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
692 if (!kone) {
Joe Perches4291ee32010-12-09 19:29:03 -0800693 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100694 return -ENOMEM;
695 }
696 hid_set_drvdata(hdev, kone);
697
698 retval = kone_init_kone_device_struct(usb_dev, kone);
699 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800700 hid_err(hdev, "couldn't init struct kone_device\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100701 goto exit_free;
702 }
Stefan Achatz206f5f22010-05-19 18:55:16 +0200703
Stefan Achatz8211e462011-01-30 13:38:25 +0100704 retval = roccat_connect(kone_class, hdev,
705 sizeof(struct kone_roccat_report));
Stefan Achatz206f5f22010-05-19 18:55:16 +0200706 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800707 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz206f5f22010-05-19 18:55:16 +0200708 /* be tolerant about not getting chrdev */
709 } else {
710 kone->roccat_claimed = 1;
711 kone->chrdev_minor = retval;
712 }
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100713 } else {
714 hid_set_drvdata(hdev, NULL);
715 }
716
717 return 0;
718exit_free:
719 kfree(kone);
720 return retval;
721}
722
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100723static void kone_remove_specials(struct hid_device *hdev)
724{
725 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200726 struct kone_device *kone;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100727
728 if (intf->cur_altsetting->desc.bInterfaceProtocol
729 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatz206f5f22010-05-19 18:55:16 +0200730 kone = hid_get_drvdata(hdev);
731 if (kone->roccat_claimed)
732 roccat_disconnect(kone->chrdev_minor);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100733 kfree(hid_get_drvdata(hdev));
734 }
735}
736
737static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
738{
739 int retval;
740
741 retval = hid_parse(hdev);
742 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800743 hid_err(hdev, "parse failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100744 goto exit;
745 }
746
747 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
748 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800749 hid_err(hdev, "hw start failed\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100750 goto exit;
751 }
752
753 retval = kone_init_specials(hdev);
754 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800755 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100756 goto exit_stop;
757 }
758
759 return 0;
760
761exit_stop:
762 hid_hw_stop(hdev);
763exit:
764 return retval;
765}
766
767static void kone_remove(struct hid_device *hdev)
768{
769 kone_remove_specials(hdev);
770 hid_hw_stop(hdev);
771}
772
Stefan Achatz48e70802010-05-18 18:31:04 +0200773/* handle special events and keep actual profile and dpi values up to date */
774static void kone_keep_values_up_to_date(struct kone_device *kone,
775 struct kone_mouse_event const *event)
776{
777 switch (event->event) {
778 case kone_mouse_event_switch_profile:
779 case kone_mouse_event_osd_profile:
780 kone->actual_profile = event->value;
781 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
782 startup_dpi;
783 break;
784 case kone_mouse_event_switch_dpi:
785 case kone_mouse_event_osd_dpi:
786 kone->actual_dpi = event->value;
787 break;
788 }
789}
790
Stefan Achatz206f5f22010-05-19 18:55:16 +0200791static void kone_report_to_chrdev(struct kone_device const *kone,
792 struct kone_mouse_event const *event)
793{
794 struct kone_roccat_report roccat_report;
795
796 switch (event->event) {
797 case kone_mouse_event_switch_profile:
798 case kone_mouse_event_switch_dpi:
799 case kone_mouse_event_osd_profile:
800 case kone_mouse_event_osd_dpi:
801 roccat_report.event = event->event;
802 roccat_report.value = event->value;
803 roccat_report.key = 0;
804 roccat_report_event(kone->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100805 (uint8_t *)&roccat_report);
Stefan Achatz206f5f22010-05-19 18:55:16 +0200806 break;
807 case kone_mouse_event_call_overlong_macro:
808 if (event->value == kone_keystroke_action_press) {
809 roccat_report.event = kone_mouse_event_call_overlong_macro;
810 roccat_report.value = kone->actual_profile;
811 roccat_report.key = event->macro_key;
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 }
815 break;
816 }
817
818}
819
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100820/*
821 * Is called for keyboard- and mousepart.
822 * Only mousepart gets informations about special events in its extended event
823 * structure.
824 */
825static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
826 u8 *data, int size)
827{
828 struct kone_device *kone = hid_get_drvdata(hdev);
829 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
830
831 /* keyboard events are always processed by default handler */
832 if (size != sizeof(struct kone_mouse_event))
833 return 0;
834
Stefan Achatz901e64d2011-06-12 10:02:44 +0200835 if (kone == NULL)
836 return 0;
837
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100838 /*
Stefan Achatz73b35772010-05-19 13:53:22 +0200839 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
840 * Pressed button is reported in each movement event.
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100841 * Workaround sends only one event per press.
842 */
Stefan Achatz73b35772010-05-19 13:53:22 +0200843 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
844 memcpy(&kone->last_mouse_event, event,
845 sizeof(struct kone_mouse_event));
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100846 else
Stefan Achatz73b35772010-05-19 13:53:22 +0200847 memset(&event->tilt, 0, 5);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100848
Stefan Achatz48e70802010-05-18 18:31:04 +0200849 kone_keep_values_up_to_date(kone, event);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100850
Stefan Achatz206f5f22010-05-19 18:55:16 +0200851 if (kone->roccat_claimed)
852 kone_report_to_chrdev(kone, event);
853
Stefan Achatz48e70802010-05-18 18:31:04 +0200854 return 0; /* always do further processing */
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100855}
856
857static const struct hid_device_id kone_devices[] = {
858 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
859 { }
860};
861
862MODULE_DEVICE_TABLE(hid, kone_devices);
863
864static struct hid_driver kone_driver = {
865 .name = "kone",
866 .id_table = kone_devices,
867 .probe = kone_probe,
868 .remove = kone_remove,
869 .raw_event = kone_raw_event
870};
871
Stefan Achatz00237bc2010-05-08 17:20:38 +0200872static int __init kone_init(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100873{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000874 int retval;
875
876 /* class name has to be same as driver name */
877 kone_class = class_create(THIS_MODULE, "kone");
878 if (IS_ERR(kone_class))
879 return PTR_ERR(kone_class);
880 kone_class->dev_attrs = kone_attributes;
881 kone_class->dev_bin_attrs = kone_bin_attributes;
882
883 retval = hid_register_driver(&kone_driver);
884 if (retval)
885 class_destroy(kone_class);
886 return retval;
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100887}
888
Stefan Achatz00237bc2010-05-08 17:20:38 +0200889static void __exit kone_exit(void)
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100890{
891 hid_unregister_driver(&kone_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100892 class_destroy(kone_class);
Stefan Achatz14bf62c2010-03-18 16:19:43 +0100893}
894
895module_init(kone_init);
896module_exit(kone_exit);
897
Stefan Achatz1f749d82010-05-12 17:43:34 +0200898MODULE_AUTHOR("Stefan Achatz");
899MODULE_DESCRIPTION("USB Roccat Kone driver");
900MODULE_LICENSE("GPL v2");