blob: 0df408362ef162b7601e2a94fd204eb796fc3a5c [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 Achatz6d1dec82011-05-29 19:32:57 +0200186static ssize_t koneplus_sysfs_write_talk(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_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200191 sizeof(struct koneplus_talk), KONEPLUS_COMMAND_TALK);
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200192}
193
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000194static ssize_t koneplus_sysfs_write_macro(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,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200199 sizeof(struct koneplus_macro), KONEPLUS_COMMAND_MACRO);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000200}
201
202static ssize_t koneplus_sysfs_read_sensor(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_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200207 sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000208}
209
210static ssize_t koneplus_sysfs_write_sensor(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_sensor), KONEPLUS_COMMAND_SENSOR);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000216}
217
218static ssize_t koneplus_sysfs_write_tcu(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_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200223 sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000224}
225
Stefan Achatz5277e972012-10-17 16:35:42 +0200226static ssize_t koneplus_sysfs_read_tcu(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_read(fp, kobj, buf, off, count,
231 sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
232}
233
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000234static ssize_t koneplus_sysfs_read_tcu_image(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_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200239 sizeof(struct koneplus_tcu_image), KONEPLUS_COMMAND_TCU);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000240}
241
242static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
243 struct kobject *kobj, struct bin_attribute *attr, char *buf,
244 loff_t off, size_t count)
245{
246 struct device *dev =
247 container_of(kobj, struct device, kobj)->parent->parent;
248 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
249
250 if (off >= sizeof(struct koneplus_profile_settings))
251 return 0;
252
253 if (off + count > sizeof(struct koneplus_profile_settings))
254 count = sizeof(struct koneplus_profile_settings) - off;
255
256 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100257 memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000258 count);
259 mutex_unlock(&koneplus->koneplus_lock);
260
261 return count;
262}
263
264static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
265 struct kobject *kobj, struct bin_attribute *attr, char *buf,
266 loff_t off, size_t count)
267{
268 struct device *dev =
269 container_of(kobj, struct device, kobj)->parent->parent;
270 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
271 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
272 int retval = 0;
273 int difference;
274 int profile_number;
275 struct koneplus_profile_settings *profile_settings;
276
277 if (off != 0 || count != sizeof(struct koneplus_profile_settings))
278 return -EINVAL;
279
280 profile_number = ((struct koneplus_profile_settings const *)buf)->number;
281 profile_settings = &koneplus->profile_settings[profile_number];
282
283 mutex_lock(&koneplus->koneplus_lock);
284 difference = memcmp(buf, profile_settings,
285 sizeof(struct koneplus_profile_settings));
286 if (difference) {
287 retval = koneplus_set_profile_settings(usb_dev,
288 (struct koneplus_profile_settings const *)buf);
289 if (!retval)
290 memcpy(profile_settings, buf,
291 sizeof(struct koneplus_profile_settings));
292 }
293 mutex_unlock(&koneplus->koneplus_lock);
294
295 if (retval)
296 return retval;
297
298 return sizeof(struct koneplus_profile_settings);
299}
300
301static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
302 struct kobject *kobj, struct bin_attribute *attr, char *buf,
303 loff_t off, size_t count)
304{
305 struct device *dev =
306 container_of(kobj, struct device, kobj)->parent->parent;
307 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
308
309 if (off >= sizeof(struct koneplus_profile_buttons))
310 return 0;
311
312 if (off + count > sizeof(struct koneplus_profile_buttons))
313 count = sizeof(struct koneplus_profile_buttons) - off;
314
315 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100316 memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000317 count);
318 mutex_unlock(&koneplus->koneplus_lock);
319
320 return count;
321}
322
323static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
324 struct kobject *kobj, struct bin_attribute *attr, char *buf,
325 loff_t off, size_t count)
326{
327 struct device *dev =
328 container_of(kobj, struct device, kobj)->parent->parent;
329 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
330 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
331 int retval = 0;
332 int difference;
333 uint profile_number;
334 struct koneplus_profile_buttons *profile_buttons;
335
336 if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
337 return -EINVAL;
338
339 profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
340 profile_buttons = &koneplus->profile_buttons[profile_number];
341
342 mutex_lock(&koneplus->koneplus_lock);
343 difference = memcmp(buf, profile_buttons,
344 sizeof(struct koneplus_profile_buttons));
345 if (difference) {
346 retval = koneplus_set_profile_buttons(usb_dev,
347 (struct koneplus_profile_buttons const *)buf);
348 if (!retval)
349 memcpy(profile_buttons, buf,
350 sizeof(struct koneplus_profile_buttons));
351 }
352 mutex_unlock(&koneplus->koneplus_lock);
353
354 if (retval)
355 return retval;
356
357 return sizeof(struct koneplus_profile_buttons);
358}
359
Stefan Achatzb50f3152011-04-13 17:17:52 +0200360static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000361 struct device_attribute *attr, char *buf)
362{
363 struct koneplus_device *koneplus =
364 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzb50f3152011-04-13 17:17:52 +0200365 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000366}
367
Stefan Achatzb50f3152011-04-13 17:17:52 +0200368static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000369 struct device_attribute *attr, char const *buf, size_t size)
370{
371 struct koneplus_device *koneplus;
372 struct usb_device *usb_dev;
373 unsigned long profile;
374 int retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200375 struct koneplus_roccat_report roccat_report;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000376
377 dev = dev->parent->parent;
378 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
379 usb_dev = interface_to_usbdev(to_usb_interface(dev));
380
381 retval = strict_strtoul(buf, 10, &profile);
382 if (retval)
383 return retval;
384
Stefan Achatz901e64d2011-06-12 10:02:44 +0200385 if (profile > 4)
386 return -EINVAL;
387
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000388 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200389
390 retval = koneplus_set_actual_profile(usb_dev, profile);
391 if (retval) {
392 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000393 return retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200394 }
395
Stefan Achatz901e64d2011-06-12 10:02:44 +0200396 koneplus_profile_activated(koneplus, profile);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200397
398 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
399 roccat_report.data1 = profile + 1;
400 roccat_report.data2 = 0;
401 roccat_report.profile = profile + 1;
402 roccat_report_event(koneplus->chrdev_minor,
403 (uint8_t const *)&roccat_report);
404
405 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000406
407 return size;
408}
409
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000410static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
413 struct koneplus_device *koneplus =
414 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
415 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
416}
417
418static struct device_attribute koneplus_attributes[] = {
Stefan Achatzb50f3152011-04-13 17:17:52 +0200419 __ATTR(actual_profile, 0660,
420 koneplus_sysfs_show_actual_profile,
421 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000422 __ATTR(startup_profile, 0660,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200423 koneplus_sysfs_show_actual_profile,
424 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000425 __ATTR(firmware_version, 0440,
426 koneplus_sysfs_show_firmware_version, NULL),
427 __ATTR_NULL
428};
429
430static struct bin_attribute koneplus_bin_attributes[] = {
431 {
Stefan Achatz61c29f52011-03-14 21:43:07 +0100432 .attr = { .name = "sensor", .mode = 0660 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000433 .size = sizeof(struct koneplus_sensor),
434 .read = koneplus_sysfs_read_sensor,
435 .write = koneplus_sysfs_write_sensor
436 },
437 {
Stefan Achatz5277e972012-10-17 16:35:42 +0200438 .attr = { .name = "tcu", .mode = 0660 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000439 .size = sizeof(struct koneplus_tcu),
Stefan Achatz5277e972012-10-17 16:35:42 +0200440 .read = koneplus_sysfs_read_tcu,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000441 .write = koneplus_sysfs_write_tcu
442 },
443 {
444 .attr = { .name = "tcu_image", .mode = 0440 },
445 .size = sizeof(struct koneplus_tcu_image),
446 .read = koneplus_sysfs_read_tcu_image
447 },
448 {
449 .attr = { .name = "profile_settings", .mode = 0220 },
450 .size = sizeof(struct koneplus_profile_settings),
451 .write = koneplus_sysfs_write_profile_settings
452 },
453 {
454 .attr = { .name = "profile1_settings", .mode = 0440 },
455 .size = sizeof(struct koneplus_profile_settings),
456 .read = koneplus_sysfs_read_profilex_settings,
457 .private = &profile_numbers[0]
458 },
459 {
460 .attr = { .name = "profile2_settings", .mode = 0440 },
461 .size = sizeof(struct koneplus_profile_settings),
462 .read = koneplus_sysfs_read_profilex_settings,
463 .private = &profile_numbers[1]
464 },
465 {
466 .attr = { .name = "profile3_settings", .mode = 0440 },
467 .size = sizeof(struct koneplus_profile_settings),
468 .read = koneplus_sysfs_read_profilex_settings,
469 .private = &profile_numbers[2]
470 },
471 {
472 .attr = { .name = "profile4_settings", .mode = 0440 },
473 .size = sizeof(struct koneplus_profile_settings),
474 .read = koneplus_sysfs_read_profilex_settings,
475 .private = &profile_numbers[3]
476 },
477 {
478 .attr = { .name = "profile5_settings", .mode = 0440 },
479 .size = sizeof(struct koneplus_profile_settings),
480 .read = koneplus_sysfs_read_profilex_settings,
481 .private = &profile_numbers[4]
482 },
483 {
484 .attr = { .name = "profile_buttons", .mode = 0220 },
485 .size = sizeof(struct koneplus_profile_buttons),
486 .write = koneplus_sysfs_write_profile_buttons
487 },
488 {
489 .attr = { .name = "profile1_buttons", .mode = 0440 },
490 .size = sizeof(struct koneplus_profile_buttons),
491 .read = koneplus_sysfs_read_profilex_buttons,
492 .private = &profile_numbers[0]
493 },
494 {
495 .attr = { .name = "profile2_buttons", .mode = 0440 },
496 .size = sizeof(struct koneplus_profile_buttons),
497 .read = koneplus_sysfs_read_profilex_buttons,
498 .private = &profile_numbers[1]
499 },
500 {
501 .attr = { .name = "profile3_buttons", .mode = 0440 },
502 .size = sizeof(struct koneplus_profile_buttons),
503 .read = koneplus_sysfs_read_profilex_buttons,
504 .private = &profile_numbers[2]
505 },
506 {
507 .attr = { .name = "profile4_buttons", .mode = 0440 },
508 .size = sizeof(struct koneplus_profile_buttons),
509 .read = koneplus_sysfs_read_profilex_buttons,
510 .private = &profile_numbers[3]
511 },
512 {
513 .attr = { .name = "profile5_buttons", .mode = 0440 },
514 .size = sizeof(struct koneplus_profile_buttons),
515 .read = koneplus_sysfs_read_profilex_buttons,
516 .private = &profile_numbers[4]
517 },
518 {
519 .attr = { .name = "macro", .mode = 0220 },
520 .size = sizeof(struct koneplus_macro),
521 .write = koneplus_sysfs_write_macro
522 },
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200523 {
524 .attr = { .name = "talk", .mode = 0220 },
525 .size = sizeof(struct koneplus_talk),
526 .write = koneplus_sysfs_write_talk
527 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000528 __ATTR_NULL
529};
530
531static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
532 struct koneplus_device *koneplus)
533{
534 int retval, i;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200535 static uint wait = 200;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000536
537 mutex_init(&koneplus->koneplus_lock);
538
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000539 retval = koneplus_get_info(usb_dev, &koneplus->info);
540 if (retval)
541 return retval;
542
543 for (i = 0; i < 5; ++i) {
544 msleep(wait);
545 retval = koneplus_get_profile_settings(usb_dev,
546 &koneplus->profile_settings[i], i);
547 if (retval)
548 return retval;
549
550 msleep(wait);
551 retval = koneplus_get_profile_buttons(usb_dev,
552 &koneplus->profile_buttons[i], i);
553 if (retval)
554 return retval;
555 }
556
Stefan Achatzb50f3152011-04-13 17:17:52 +0200557 msleep(wait);
558 retval = koneplus_get_actual_profile(usb_dev);
559 if (retval < 0)
560 return retval;
561 koneplus_profile_activated(koneplus, retval);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000562
563 return 0;
564}
565
566static int koneplus_init_specials(struct hid_device *hdev)
567{
568 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
569 struct usb_device *usb_dev = interface_to_usbdev(intf);
570 struct koneplus_device *koneplus;
571 int retval;
572
573 if (intf->cur_altsetting->desc.bInterfaceProtocol
574 == USB_INTERFACE_PROTOCOL_MOUSE) {
575
576 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
577 if (!koneplus) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100578 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000579 return -ENOMEM;
580 }
581 hid_set_drvdata(hdev, koneplus);
582
583 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
584 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100585 hid_err(hdev, "couldn't init struct koneplus_device\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000586 goto exit_free;
587 }
588
Stefan Achatz8211e462011-01-30 13:38:25 +0100589 retval = roccat_connect(koneplus_class, hdev,
590 sizeof(struct koneplus_roccat_report));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000591 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100592 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000593 } else {
594 koneplus->chrdev_minor = retval;
595 koneplus->roccat_claimed = 1;
596 }
597 } else {
598 hid_set_drvdata(hdev, NULL);
599 }
600
601 return 0;
602exit_free:
603 kfree(koneplus);
604 return retval;
605}
606
607static void koneplus_remove_specials(struct hid_device *hdev)
608{
609 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
610 struct koneplus_device *koneplus;
611
612 if (intf->cur_altsetting->desc.bInterfaceProtocol
613 == USB_INTERFACE_PROTOCOL_MOUSE) {
614 koneplus = hid_get_drvdata(hdev);
615 if (koneplus->roccat_claimed)
616 roccat_disconnect(koneplus->chrdev_minor);
617 kfree(koneplus);
618 }
619}
620
621static int koneplus_probe(struct hid_device *hdev,
622 const struct hid_device_id *id)
623{
624 int retval;
625
626 retval = hid_parse(hdev);
627 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100628 hid_err(hdev, "parse failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000629 goto exit;
630 }
631
632 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
633 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100634 hid_err(hdev, "hw start failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000635 goto exit;
636 }
637
638 retval = koneplus_init_specials(hdev);
639 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100640 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000641 goto exit_stop;
642 }
643
644 return 0;
645
646exit_stop:
647 hid_hw_stop(hdev);
648exit:
649 return retval;
650}
651
652static void koneplus_remove(struct hid_device *hdev)
653{
654 koneplus_remove_specials(hdev);
655 hid_hw_stop(hdev);
656}
657
658static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
659 u8 const *data)
660{
661 struct koneplus_mouse_report_button const *button_report;
662
663 switch (data[0]) {
664 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
665 button_report = (struct koneplus_mouse_report_button const *)data;
666 switch (button_report->type) {
667 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
668 koneplus_profile_activated(koneplus, button_report->data1 - 1);
669 break;
670 }
671 break;
672 }
673}
674
675static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
676 u8 const *data)
677{
678 struct koneplus_roccat_report roccat_report;
679 struct koneplus_mouse_report_button const *button_report;
680
681 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
682 return;
683
684 button_report = (struct koneplus_mouse_report_button const *)data;
685
686 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
687 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
688 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
689 return;
690
691 roccat_report.type = button_report->type;
692 roccat_report.data1 = button_report->data1;
693 roccat_report.data2 = button_report->data2;
694 roccat_report.profile = koneplus->actual_profile + 1;
695 roccat_report_event(koneplus->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100696 (uint8_t const *)&roccat_report);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000697}
698
699static int koneplus_raw_event(struct hid_device *hdev,
700 struct hid_report *report, u8 *data, int size)
701{
702 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
703 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
704
705 if (intf->cur_altsetting->desc.bInterfaceProtocol
706 != USB_INTERFACE_PROTOCOL_MOUSE)
707 return 0;
708
Stefan Achatz901e64d2011-06-12 10:02:44 +0200709 if (koneplus == NULL)
710 return 0;
711
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000712 koneplus_keep_values_up_to_date(koneplus, data);
713
714 if (koneplus->roccat_claimed)
715 koneplus_report_to_chrdev(koneplus, data);
716
717 return 0;
718}
719
720static const struct hid_device_id koneplus_devices[] = {
721 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100722 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000723 { }
724};
725
726MODULE_DEVICE_TABLE(hid, koneplus_devices);
727
728static struct hid_driver koneplus_driver = {
729 .name = "koneplus",
730 .id_table = koneplus_devices,
731 .probe = koneplus_probe,
732 .remove = koneplus_remove,
733 .raw_event = koneplus_raw_event
734};
735
736static int __init koneplus_init(void)
737{
738 int retval;
739
740 /* class name has to be same as driver name */
741 koneplus_class = class_create(THIS_MODULE, "koneplus");
742 if (IS_ERR(koneplus_class))
743 return PTR_ERR(koneplus_class);
744 koneplus_class->dev_attrs = koneplus_attributes;
745 koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
746
747 retval = hid_register_driver(&koneplus_driver);
748 if (retval)
749 class_destroy(koneplus_class);
750 return retval;
751}
752
753static void __exit koneplus_exit(void)
754{
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000755 hid_unregister_driver(&koneplus_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100756 class_destroy(koneplus_class);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000757}
758
759module_init(koneplus_init);
760module_exit(koneplus_exit);
761
762MODULE_AUTHOR("Stefan Achatz");
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100763MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000764MODULE_LICENSE("GPL v2");