blob: 69592f42757909734ae18ad4f7d63da285b7e31d [file] [log] [blame]
Stefan Achatz47dbdbff2010-11-26 19:57:42 +00001/*
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 an updated/improved version of the Kone with more memory
16 * and functionality and without the non-standard behaviours the Kone had.
Stefan Achatz8e74a2d2012-11-04 09:38:52 +010017 * KoneXTD has same capabilities but updated sensor.
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000018 */
19
20#include <linux/device.h>
21#include <linux/input.h>
22#include <linux/hid.h>
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000023#include <linux/module.h>
24#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010025#include <linux/hid-roccat.h>
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000026#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010027#include "hid-roccat-common.h"
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000028#include "hid-roccat-koneplus.h"
29
30static uint profile_numbers[5] = {0, 1, 2, 3, 4};
31
32static struct class *koneplus_class;
33
34static void koneplus_profile_activated(struct koneplus_device *koneplus,
35 uint new_profile)
36{
37 koneplus->actual_profile = new_profile;
38}
39
40static int koneplus_send_control(struct usb_device *usb_dev, uint value,
41 enum koneplus_control_requests request)
42{
Stefan Achatz7392d732012-05-20 22:45:04 +020043 struct roccat_common2_control control;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000044
45 if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
46 request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
47 value > 4)
48 return -EINVAL;
49
Stefan Achatz4728f2d2012-05-20 22:44:59 +020050 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
Stefan Achatz5772f632011-01-30 13:38:23 +010051 control.value = value;
52 control.request = request;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000053
Stefan Achatz7392d732012-05-20 22:45:04 +020054 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020055 ROCCAT_COMMON_COMMAND_CONTROL,
Stefan Achatz7392d732012-05-20 22:45:04 +020056 &control, sizeof(struct roccat_common2_control));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000057}
58
59static int koneplus_get_info(struct usb_device *usb_dev,
60 struct koneplus_info *buf)
61{
Stefan Achatz7392d732012-05-20 22:45:04 +020062 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000063 buf, sizeof(struct koneplus_info));
64}
65
66static int koneplus_get_profile_settings(struct usb_device *usb_dev,
67 struct koneplus_profile_settings *buf, uint number)
68{
69 int retval;
70
Stefan Achatz4728f2d2012-05-20 22:44:59 +020071 retval = koneplus_send_control(usb_dev, number,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000072 KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
73 if (retval)
74 return retval;
75
Stefan Achatz7392d732012-05-20 22:45:04 +020076 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_SETTINGS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000077 buf, sizeof(struct koneplus_profile_settings));
78}
79
80static int koneplus_set_profile_settings(struct usb_device *usb_dev,
81 struct koneplus_profile_settings const *settings)
82{
Stefan Achatz7392d732012-05-20 22:45:04 +020083 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020084 KONEPLUS_COMMAND_PROFILE_SETTINGS,
Stefan Achatz5772f632011-01-30 13:38:23 +010085 settings, sizeof(struct koneplus_profile_settings));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000086}
87
88static int koneplus_get_profile_buttons(struct usb_device *usb_dev,
89 struct koneplus_profile_buttons *buf, int number)
90{
91 int retval;
92
Stefan Achatz4728f2d2012-05-20 22:44:59 +020093 retval = koneplus_send_control(usb_dev, number,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000094 KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
95 if (retval)
96 return retval;
97
Stefan Achatz7392d732012-05-20 22:45:04 +020098 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_BUTTONS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000099 buf, sizeof(struct koneplus_profile_buttons));
100}
101
102static int koneplus_set_profile_buttons(struct usb_device *usb_dev,
103 struct koneplus_profile_buttons const *buttons)
104{
Stefan Achatz7392d732012-05-20 22:45:04 +0200105 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200106 KONEPLUS_COMMAND_PROFILE_BUTTONS,
Stefan Achatz5772f632011-01-30 13:38:23 +0100107 buttons, sizeof(struct koneplus_profile_buttons));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000108}
109
110/* retval is 0-4 on success, < 0 on error */
Stefan Achatzb50f3152011-04-13 17:17:52 +0200111static int koneplus_get_actual_profile(struct usb_device *usb_dev)
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000112{
Stefan Achatzb50f3152011-04-13 17:17:52 +0200113 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000114 int retval;
115
Stefan Achatz7392d732012-05-20 22:45:04 +0200116 retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200117 &buf, sizeof(struct koneplus_actual_profile));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000118
Stefan Achatzb50f3152011-04-13 17:17:52 +0200119 return retval ? retval : buf.actual_profile;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000120}
121
Stefan Achatzb50f3152011-04-13 17:17:52 +0200122static int koneplus_set_actual_profile(struct usb_device *usb_dev,
123 int new_profile)
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000124{
Stefan Achatzb50f3152011-04-13 17:17:52 +0200125 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000126
Stefan Achatzb50f3152011-04-13 17:17:52 +0200127 buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
128 buf.size = sizeof(struct koneplus_actual_profile);
129 buf.actual_profile = new_profile;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000130
Stefan Achatz7392d732012-05-20 22:45:04 +0200131 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200132 KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200133 &buf, sizeof(struct koneplus_actual_profile));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000134}
135
136static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
137 char *buf, loff_t off, size_t count,
138 size_t real_size, uint command)
139{
140 struct device *dev =
141 container_of(kobj, struct device, kobj)->parent->parent;
142 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
143 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
144 int retval;
145
Stefan Achatzfd82be62011-01-06 09:00:41 +0100146 if (off >= real_size)
147 return 0;
148
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000149 if (off != 0 || count != real_size)
150 return -EINVAL;
151
152 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200153 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000154 mutex_unlock(&koneplus->koneplus_lock);
155
156 if (retval)
157 return retval;
158
159 return real_size;
160}
161
162static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
163 void const *buf, loff_t off, size_t count,
164 size_t real_size, uint command)
165{
166 struct device *dev =
167 container_of(kobj, struct device, kobj)->parent->parent;
168 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
169 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
170 int retval;
171
172 if (off != 0 || count != real_size)
173 return -EINVAL;
174
175 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200176 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200177 buf, real_size);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000178 mutex_unlock(&koneplus->koneplus_lock);
179
180 if (retval)
181 return retval;
182
183 return real_size;
184}
185
Stefan Achatzfabe51e2012-11-04 09:39:04 +0100186static ssize_t koneplus_sysfs_read_info(struct file *fp,
187 struct kobject *kobj, struct bin_attribute *attr, char *buf,
188 loff_t off, size_t count)
189{
190 return koneplus_sysfs_read(fp, kobj, buf, off, count,
191 sizeof(struct koneplus_info), KONEPLUS_COMMAND_INFO);
192}
193
194static ssize_t koneplus_sysfs_write_info(struct file *fp,
195 struct kobject *kobj, struct bin_attribute *attr, char *buf,
196 loff_t off, size_t count)
197{
198 return koneplus_sysfs_write(fp, kobj, buf, off, count,
199 sizeof(struct koneplus_info), KONEPLUS_COMMAND_INFO);
200}
201
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200202static ssize_t koneplus_sysfs_write_talk(struct file *fp,
203 struct kobject *kobj, struct bin_attribute *attr, char *buf,
204 loff_t off, size_t count)
205{
206 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200207 sizeof(struct koneplus_talk), KONEPLUS_COMMAND_TALK);
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200208}
209
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000210static ssize_t koneplus_sysfs_write_macro(struct file *fp,
211 struct kobject *kobj, struct bin_attribute *attr, char *buf,
212 loff_t off, size_t count)
213{
214 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200215 sizeof(struct koneplus_macro), KONEPLUS_COMMAND_MACRO);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000216}
217
218static ssize_t koneplus_sysfs_read_sensor(struct file *fp,
219 struct kobject *kobj, struct bin_attribute *attr, char *buf,
220 loff_t off, size_t count)
221{
222 return koneplus_sysfs_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200223 sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000224}
225
226static ssize_t koneplus_sysfs_write_sensor(struct file *fp,
227 struct kobject *kobj, struct bin_attribute *attr, char *buf,
228 loff_t off, size_t count)
229{
230 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200231 sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000232}
233
234static ssize_t koneplus_sysfs_write_tcu(struct file *fp,
235 struct kobject *kobj, struct bin_attribute *attr, char *buf,
236 loff_t off, size_t count)
237{
238 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200239 sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000240}
241
Stefan Achatz5277e972012-10-17 16:35:42 +0200242static ssize_t koneplus_sysfs_read_tcu(struct file *fp,
243 struct kobject *kobj, struct bin_attribute *attr, char *buf,
244 loff_t off, size_t count)
245{
246 return koneplus_sysfs_read(fp, kobj, buf, off, count,
247 sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
248}
249
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000250static ssize_t koneplus_sysfs_read_tcu_image(struct file *fp,
251 struct kobject *kobj, struct bin_attribute *attr, char *buf,
252 loff_t off, size_t count)
253{
254 return koneplus_sysfs_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200255 sizeof(struct koneplus_tcu_image), KONEPLUS_COMMAND_TCU);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000256}
257
258static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
259 struct kobject *kobj, struct bin_attribute *attr, char *buf,
260 loff_t off, size_t count)
261{
262 struct device *dev =
263 container_of(kobj, struct device, kobj)->parent->parent;
264 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
265
266 if (off >= sizeof(struct koneplus_profile_settings))
267 return 0;
268
269 if (off + count > sizeof(struct koneplus_profile_settings))
270 count = sizeof(struct koneplus_profile_settings) - off;
271
272 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100273 memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000274 count);
275 mutex_unlock(&koneplus->koneplus_lock);
276
277 return count;
278}
279
280static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
281 struct kobject *kobj, struct bin_attribute *attr, char *buf,
282 loff_t off, size_t count)
283{
284 struct device *dev =
285 container_of(kobj, struct device, kobj)->parent->parent;
286 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
287 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
288 int retval = 0;
289 int difference;
290 int profile_number;
291 struct koneplus_profile_settings *profile_settings;
292
293 if (off != 0 || count != sizeof(struct koneplus_profile_settings))
294 return -EINVAL;
295
296 profile_number = ((struct koneplus_profile_settings const *)buf)->number;
297 profile_settings = &koneplus->profile_settings[profile_number];
298
299 mutex_lock(&koneplus->koneplus_lock);
300 difference = memcmp(buf, profile_settings,
301 sizeof(struct koneplus_profile_settings));
302 if (difference) {
303 retval = koneplus_set_profile_settings(usb_dev,
304 (struct koneplus_profile_settings const *)buf);
305 if (!retval)
306 memcpy(profile_settings, buf,
307 sizeof(struct koneplus_profile_settings));
308 }
309 mutex_unlock(&koneplus->koneplus_lock);
310
311 if (retval)
312 return retval;
313
314 return sizeof(struct koneplus_profile_settings);
315}
316
317static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
318 struct kobject *kobj, struct bin_attribute *attr, char *buf,
319 loff_t off, size_t count)
320{
321 struct device *dev =
322 container_of(kobj, struct device, kobj)->parent->parent;
323 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
324
325 if (off >= sizeof(struct koneplus_profile_buttons))
326 return 0;
327
328 if (off + count > sizeof(struct koneplus_profile_buttons))
329 count = sizeof(struct koneplus_profile_buttons) - off;
330
331 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100332 memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000333 count);
334 mutex_unlock(&koneplus->koneplus_lock);
335
336 return count;
337}
338
339static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
340 struct kobject *kobj, struct bin_attribute *attr, char *buf,
341 loff_t off, size_t count)
342{
343 struct device *dev =
344 container_of(kobj, struct device, kobj)->parent->parent;
345 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
346 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
347 int retval = 0;
348 int difference;
349 uint profile_number;
350 struct koneplus_profile_buttons *profile_buttons;
351
352 if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
353 return -EINVAL;
354
355 profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
356 profile_buttons = &koneplus->profile_buttons[profile_number];
357
358 mutex_lock(&koneplus->koneplus_lock);
359 difference = memcmp(buf, profile_buttons,
360 sizeof(struct koneplus_profile_buttons));
361 if (difference) {
362 retval = koneplus_set_profile_buttons(usb_dev,
363 (struct koneplus_profile_buttons const *)buf);
364 if (!retval)
365 memcpy(profile_buttons, buf,
366 sizeof(struct koneplus_profile_buttons));
367 }
368 mutex_unlock(&koneplus->koneplus_lock);
369
370 if (retval)
371 return retval;
372
373 return sizeof(struct koneplus_profile_buttons);
374}
375
Stefan Achatzb50f3152011-04-13 17:17:52 +0200376static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000377 struct device_attribute *attr, char *buf)
378{
379 struct koneplus_device *koneplus =
380 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzb50f3152011-04-13 17:17:52 +0200381 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000382}
383
Stefan Achatzb50f3152011-04-13 17:17:52 +0200384static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000385 struct device_attribute *attr, char const *buf, size_t size)
386{
387 struct koneplus_device *koneplus;
388 struct usb_device *usb_dev;
389 unsigned long profile;
390 int retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200391 struct koneplus_roccat_report roccat_report;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000392
393 dev = dev->parent->parent;
394 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
395 usb_dev = interface_to_usbdev(to_usb_interface(dev));
396
397 retval = strict_strtoul(buf, 10, &profile);
398 if (retval)
399 return retval;
400
Stefan Achatz901e64d2011-06-12 10:02:44 +0200401 if (profile > 4)
402 return -EINVAL;
403
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000404 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200405
406 retval = koneplus_set_actual_profile(usb_dev, profile);
407 if (retval) {
408 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000409 return retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200410 }
411
Stefan Achatz901e64d2011-06-12 10:02:44 +0200412 koneplus_profile_activated(koneplus, profile);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200413
414 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
415 roccat_report.data1 = profile + 1;
416 roccat_report.data2 = 0;
417 roccat_report.profile = profile + 1;
418 roccat_report_event(koneplus->chrdev_minor,
419 (uint8_t const *)&roccat_report);
420
421 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000422
423 return size;
424}
425
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000426static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
429 struct koneplus_device *koneplus =
430 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
431 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
432}
433
434static struct device_attribute koneplus_attributes[] = {
Stefan Achatzb50f3152011-04-13 17:17:52 +0200435 __ATTR(actual_profile, 0660,
436 koneplus_sysfs_show_actual_profile,
437 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000438 __ATTR(startup_profile, 0660,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200439 koneplus_sysfs_show_actual_profile,
440 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000441 __ATTR(firmware_version, 0440,
442 koneplus_sysfs_show_firmware_version, NULL),
443 __ATTR_NULL
444};
445
446static struct bin_attribute koneplus_bin_attributes[] = {
447 {
Stefan Achatzfabe51e2012-11-04 09:39:04 +0100448 .attr = { .name = "info", .mode = 0660 },
449 .size = sizeof(struct koneplus_info),
450 .read = koneplus_sysfs_read_info,
451 .write = koneplus_sysfs_write_info
452 },
453 {
Stefan Achatz61c29f52011-03-14 21:43:07 +0100454 .attr = { .name = "sensor", .mode = 0660 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000455 .size = sizeof(struct koneplus_sensor),
456 .read = koneplus_sysfs_read_sensor,
457 .write = koneplus_sysfs_write_sensor
458 },
459 {
Stefan Achatz5277e972012-10-17 16:35:42 +0200460 .attr = { .name = "tcu", .mode = 0660 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000461 .size = sizeof(struct koneplus_tcu),
Stefan Achatz5277e972012-10-17 16:35:42 +0200462 .read = koneplus_sysfs_read_tcu,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000463 .write = koneplus_sysfs_write_tcu
464 },
465 {
466 .attr = { .name = "tcu_image", .mode = 0440 },
467 .size = sizeof(struct koneplus_tcu_image),
468 .read = koneplus_sysfs_read_tcu_image
469 },
470 {
471 .attr = { .name = "profile_settings", .mode = 0220 },
472 .size = sizeof(struct koneplus_profile_settings),
473 .write = koneplus_sysfs_write_profile_settings
474 },
475 {
476 .attr = { .name = "profile1_settings", .mode = 0440 },
477 .size = sizeof(struct koneplus_profile_settings),
478 .read = koneplus_sysfs_read_profilex_settings,
479 .private = &profile_numbers[0]
480 },
481 {
482 .attr = { .name = "profile2_settings", .mode = 0440 },
483 .size = sizeof(struct koneplus_profile_settings),
484 .read = koneplus_sysfs_read_profilex_settings,
485 .private = &profile_numbers[1]
486 },
487 {
488 .attr = { .name = "profile3_settings", .mode = 0440 },
489 .size = sizeof(struct koneplus_profile_settings),
490 .read = koneplus_sysfs_read_profilex_settings,
491 .private = &profile_numbers[2]
492 },
493 {
494 .attr = { .name = "profile4_settings", .mode = 0440 },
495 .size = sizeof(struct koneplus_profile_settings),
496 .read = koneplus_sysfs_read_profilex_settings,
497 .private = &profile_numbers[3]
498 },
499 {
500 .attr = { .name = "profile5_settings", .mode = 0440 },
501 .size = sizeof(struct koneplus_profile_settings),
502 .read = koneplus_sysfs_read_profilex_settings,
503 .private = &profile_numbers[4]
504 },
505 {
506 .attr = { .name = "profile_buttons", .mode = 0220 },
507 .size = sizeof(struct koneplus_profile_buttons),
508 .write = koneplus_sysfs_write_profile_buttons
509 },
510 {
511 .attr = { .name = "profile1_buttons", .mode = 0440 },
512 .size = sizeof(struct koneplus_profile_buttons),
513 .read = koneplus_sysfs_read_profilex_buttons,
514 .private = &profile_numbers[0]
515 },
516 {
517 .attr = { .name = "profile2_buttons", .mode = 0440 },
518 .size = sizeof(struct koneplus_profile_buttons),
519 .read = koneplus_sysfs_read_profilex_buttons,
520 .private = &profile_numbers[1]
521 },
522 {
523 .attr = { .name = "profile3_buttons", .mode = 0440 },
524 .size = sizeof(struct koneplus_profile_buttons),
525 .read = koneplus_sysfs_read_profilex_buttons,
526 .private = &profile_numbers[2]
527 },
528 {
529 .attr = { .name = "profile4_buttons", .mode = 0440 },
530 .size = sizeof(struct koneplus_profile_buttons),
531 .read = koneplus_sysfs_read_profilex_buttons,
532 .private = &profile_numbers[3]
533 },
534 {
535 .attr = { .name = "profile5_buttons", .mode = 0440 },
536 .size = sizeof(struct koneplus_profile_buttons),
537 .read = koneplus_sysfs_read_profilex_buttons,
538 .private = &profile_numbers[4]
539 },
540 {
541 .attr = { .name = "macro", .mode = 0220 },
542 .size = sizeof(struct koneplus_macro),
543 .write = koneplus_sysfs_write_macro
544 },
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200545 {
546 .attr = { .name = "talk", .mode = 0220 },
547 .size = sizeof(struct koneplus_talk),
548 .write = koneplus_sysfs_write_talk
549 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000550 __ATTR_NULL
551};
552
553static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
554 struct koneplus_device *koneplus)
555{
556 int retval, i;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200557 static uint wait = 200;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000558
559 mutex_init(&koneplus->koneplus_lock);
560
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000561 retval = koneplus_get_info(usb_dev, &koneplus->info);
562 if (retval)
563 return retval;
564
565 for (i = 0; i < 5; ++i) {
566 msleep(wait);
567 retval = koneplus_get_profile_settings(usb_dev,
568 &koneplus->profile_settings[i], i);
569 if (retval)
570 return retval;
571
572 msleep(wait);
573 retval = koneplus_get_profile_buttons(usb_dev,
574 &koneplus->profile_buttons[i], i);
575 if (retval)
576 return retval;
577 }
578
Stefan Achatzb50f3152011-04-13 17:17:52 +0200579 msleep(wait);
580 retval = koneplus_get_actual_profile(usb_dev);
581 if (retval < 0)
582 return retval;
583 koneplus_profile_activated(koneplus, retval);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000584
585 return 0;
586}
587
588static int koneplus_init_specials(struct hid_device *hdev)
589{
590 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
591 struct usb_device *usb_dev = interface_to_usbdev(intf);
592 struct koneplus_device *koneplus;
593 int retval;
594
595 if (intf->cur_altsetting->desc.bInterfaceProtocol
596 == USB_INTERFACE_PROTOCOL_MOUSE) {
597
598 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
599 if (!koneplus) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100600 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000601 return -ENOMEM;
602 }
603 hid_set_drvdata(hdev, koneplus);
604
605 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
606 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100607 hid_err(hdev, "couldn't init struct koneplus_device\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000608 goto exit_free;
609 }
610
Stefan Achatz8211e462011-01-30 13:38:25 +0100611 retval = roccat_connect(koneplus_class, hdev,
612 sizeof(struct koneplus_roccat_report));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000613 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100614 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000615 } else {
616 koneplus->chrdev_minor = retval;
617 koneplus->roccat_claimed = 1;
618 }
619 } else {
620 hid_set_drvdata(hdev, NULL);
621 }
622
623 return 0;
624exit_free:
625 kfree(koneplus);
626 return retval;
627}
628
629static void koneplus_remove_specials(struct hid_device *hdev)
630{
631 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
632 struct koneplus_device *koneplus;
633
634 if (intf->cur_altsetting->desc.bInterfaceProtocol
635 == USB_INTERFACE_PROTOCOL_MOUSE) {
636 koneplus = hid_get_drvdata(hdev);
637 if (koneplus->roccat_claimed)
638 roccat_disconnect(koneplus->chrdev_minor);
639 kfree(koneplus);
640 }
641}
642
643static int koneplus_probe(struct hid_device *hdev,
644 const struct hid_device_id *id)
645{
646 int retval;
647
648 retval = hid_parse(hdev);
649 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100650 hid_err(hdev, "parse failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000651 goto exit;
652 }
653
654 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
655 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100656 hid_err(hdev, "hw start failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000657 goto exit;
658 }
659
660 retval = koneplus_init_specials(hdev);
661 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100662 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000663 goto exit_stop;
664 }
665
666 return 0;
667
668exit_stop:
669 hid_hw_stop(hdev);
670exit:
671 return retval;
672}
673
674static void koneplus_remove(struct hid_device *hdev)
675{
676 koneplus_remove_specials(hdev);
677 hid_hw_stop(hdev);
678}
679
680static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
681 u8 const *data)
682{
683 struct koneplus_mouse_report_button const *button_report;
684
685 switch (data[0]) {
686 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
687 button_report = (struct koneplus_mouse_report_button const *)data;
688 switch (button_report->type) {
689 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
690 koneplus_profile_activated(koneplus, button_report->data1 - 1);
691 break;
692 }
693 break;
694 }
695}
696
697static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
698 u8 const *data)
699{
700 struct koneplus_roccat_report roccat_report;
701 struct koneplus_mouse_report_button const *button_report;
702
703 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
704 return;
705
706 button_report = (struct koneplus_mouse_report_button const *)data;
707
708 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
709 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
710 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
711 return;
712
713 roccat_report.type = button_report->type;
714 roccat_report.data1 = button_report->data1;
715 roccat_report.data2 = button_report->data2;
716 roccat_report.profile = koneplus->actual_profile + 1;
717 roccat_report_event(koneplus->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100718 (uint8_t const *)&roccat_report);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000719}
720
721static int koneplus_raw_event(struct hid_device *hdev,
722 struct hid_report *report, u8 *data, int size)
723{
724 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
725 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
726
727 if (intf->cur_altsetting->desc.bInterfaceProtocol
728 != USB_INTERFACE_PROTOCOL_MOUSE)
729 return 0;
730
Stefan Achatz901e64d2011-06-12 10:02:44 +0200731 if (koneplus == NULL)
732 return 0;
733
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000734 koneplus_keep_values_up_to_date(koneplus, data);
735
736 if (koneplus->roccat_claimed)
737 koneplus_report_to_chrdev(koneplus, data);
738
739 return 0;
740}
741
742static const struct hid_device_id koneplus_devices[] = {
743 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100744 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000745 { }
746};
747
748MODULE_DEVICE_TABLE(hid, koneplus_devices);
749
750static struct hid_driver koneplus_driver = {
751 .name = "koneplus",
752 .id_table = koneplus_devices,
753 .probe = koneplus_probe,
754 .remove = koneplus_remove,
755 .raw_event = koneplus_raw_event
756};
757
758static int __init koneplus_init(void)
759{
760 int retval;
761
762 /* class name has to be same as driver name */
763 koneplus_class = class_create(THIS_MODULE, "koneplus");
764 if (IS_ERR(koneplus_class))
765 return PTR_ERR(koneplus_class);
766 koneplus_class->dev_attrs = koneplus_attributes;
767 koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
768
769 retval = hid_register_driver(&koneplus_driver);
770 if (retval)
771 class_destroy(koneplus_class);
772 return retval;
773}
774
775static void __exit koneplus_exit(void)
776{
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000777 hid_unregister_driver(&koneplus_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100778 class_destroy(koneplus_class);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000779}
780
781module_init(koneplus_init);
782module_exit(koneplus_exit);
783
784MODULE_AUTHOR("Stefan Achatz");
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100785MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000786MODULE_LICENSE("GPL v2");