blob: c47540a5d4f32b4861241f91871ab4c4220795ff [file] [log] [blame]
Stefan Achatz47dbdbf2010-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.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000022#include <linux/module.h>
23#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010024#include <linux/hid-roccat.h>
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000025#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010026#include "hid-roccat-common.h"
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000027#include "hid-roccat-koneplus.h"
28
29static uint profile_numbers[5] = {0, 1, 2, 3, 4};
30
31static struct class *koneplus_class;
32
33static void koneplus_profile_activated(struct koneplus_device *koneplus,
34 uint new_profile)
35{
36 koneplus->actual_profile = new_profile;
37}
38
39static int koneplus_send_control(struct usb_device *usb_dev, uint value,
40 enum koneplus_control_requests request)
41{
Stefan Achatz7392d732012-05-20 22:45:04 +020042 struct roccat_common2_control control;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000043
44 if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
45 request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
46 value > 4)
47 return -EINVAL;
48
Stefan Achatz4728f2d2012-05-20 22:44:59 +020049 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
Stefan Achatz5772f632011-01-30 13:38:23 +010050 control.value = value;
51 control.request = request;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000052
Stefan Achatz7392d732012-05-20 22:45:04 +020053 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020054 ROCCAT_COMMON_COMMAND_CONTROL,
Stefan Achatz7392d732012-05-20 22:45:04 +020055 &control, sizeof(struct roccat_common2_control));
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000056}
57
58static int koneplus_get_info(struct usb_device *usb_dev,
59 struct koneplus_info *buf)
60{
Stefan Achatz7392d732012-05-20 22:45:04 +020061 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000062 buf, sizeof(struct koneplus_info));
63}
64
65static int koneplus_get_profile_settings(struct usb_device *usb_dev,
66 struct koneplus_profile_settings *buf, uint number)
67{
68 int retval;
69
Stefan Achatz4728f2d2012-05-20 22:44:59 +020070 retval = koneplus_send_control(usb_dev, number,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000071 KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
72 if (retval)
73 return retval;
74
Stefan Achatz7392d732012-05-20 22:45:04 +020075 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_SETTINGS,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000076 buf, sizeof(struct koneplus_profile_settings));
77}
78
79static int koneplus_set_profile_settings(struct usb_device *usb_dev,
80 struct koneplus_profile_settings const *settings)
81{
Stefan Achatz7392d732012-05-20 22:45:04 +020082 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020083 KONEPLUS_COMMAND_PROFILE_SETTINGS,
Stefan Achatz5772f632011-01-30 13:38:23 +010084 settings, sizeof(struct koneplus_profile_settings));
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000085}
86
87static int koneplus_get_profile_buttons(struct usb_device *usb_dev,
88 struct koneplus_profile_buttons *buf, int number)
89{
90 int retval;
91
Stefan Achatz4728f2d2012-05-20 22:44:59 +020092 retval = koneplus_send_control(usb_dev, number,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000093 KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
94 if (retval)
95 return retval;
96
Stefan Achatz7392d732012-05-20 22:45:04 +020097 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_BUTTONS,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +000098 buf, sizeof(struct koneplus_profile_buttons));
99}
100
101static int koneplus_set_profile_buttons(struct usb_device *usb_dev,
102 struct koneplus_profile_buttons const *buttons)
103{
Stefan Achatz7392d732012-05-20 22:45:04 +0200104 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200105 KONEPLUS_COMMAND_PROFILE_BUTTONS,
Stefan Achatz5772f632011-01-30 13:38:23 +0100106 buttons, sizeof(struct koneplus_profile_buttons));
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000107}
108
109/* retval is 0-4 on success, < 0 on error */
Stefan Achatzb50f3152011-04-13 17:17:52 +0200110static int koneplus_get_actual_profile(struct usb_device *usb_dev)
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000111{
Stefan Achatzb50f3152011-04-13 17:17:52 +0200112 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000113 int retval;
114
Stefan Achatz7392d732012-05-20 22:45:04 +0200115 retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200116 &buf, sizeof(struct koneplus_actual_profile));
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000117
Stefan Achatzb50f3152011-04-13 17:17:52 +0200118 return retval ? retval : buf.actual_profile;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000119}
120
Stefan Achatzb50f3152011-04-13 17:17:52 +0200121static int koneplus_set_actual_profile(struct usb_device *usb_dev,
122 int new_profile)
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000123{
Stefan Achatzb50f3152011-04-13 17:17:52 +0200124 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000125
Stefan Achatzb50f3152011-04-13 17:17:52 +0200126 buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
127 buf.size = sizeof(struct koneplus_actual_profile);
128 buf.actual_profile = new_profile;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000129
Stefan Achatz7392d732012-05-20 22:45:04 +0200130 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200131 KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200132 &buf, sizeof(struct koneplus_actual_profile));
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000133}
134
135static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
136 char *buf, loff_t off, size_t count,
137 size_t real_size, uint command)
138{
139 struct device *dev =
140 container_of(kobj, struct device, kobj)->parent->parent;
141 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
142 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
143 int retval;
144
Stefan Achatzfd82be62011-01-06 09:00:41 +0100145 if (off >= real_size)
146 return 0;
147
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000148 if (off != 0 || count != real_size)
149 return -EINVAL;
150
151 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200152 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000153 mutex_unlock(&koneplus->koneplus_lock);
154
155 if (retval)
156 return retval;
157
158 return real_size;
159}
160
161static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
162 void const *buf, loff_t off, size_t count,
163 size_t real_size, uint command)
164{
165 struct device *dev =
166 container_of(kobj, struct device, kobj)->parent->parent;
167 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
168 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
169 int retval;
170
171 if (off != 0 || count != real_size)
172 return -EINVAL;
173
174 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200175 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200176 buf, real_size);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000177 mutex_unlock(&koneplus->koneplus_lock);
178
179 if (retval)
180 return retval;
181
182 return real_size;
183}
184
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200185static ssize_t koneplus_sysfs_write_talk(struct file *fp,
186 struct kobject *kobj, struct bin_attribute *attr, char *buf,
187 loff_t off, size_t count)
188{
189 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200190 sizeof(struct koneplus_talk), KONEPLUS_COMMAND_TALK);
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200191}
192
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000193static ssize_t koneplus_sysfs_write_macro(struct file *fp,
194 struct kobject *kobj, struct bin_attribute *attr, char *buf,
195 loff_t off, size_t count)
196{
197 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200198 sizeof(struct koneplus_macro), KONEPLUS_COMMAND_MACRO);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000199}
200
201static ssize_t koneplus_sysfs_read_sensor(struct file *fp,
202 struct kobject *kobj, struct bin_attribute *attr, char *buf,
203 loff_t off, size_t count)
204{
205 return koneplus_sysfs_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200206 sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000207}
208
209static ssize_t koneplus_sysfs_write_sensor(struct file *fp,
210 struct kobject *kobj, struct bin_attribute *attr, char *buf,
211 loff_t off, size_t count)
212{
213 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200214 sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000215}
216
217static ssize_t koneplus_sysfs_write_tcu(struct file *fp,
218 struct kobject *kobj, struct bin_attribute *attr, char *buf,
219 loff_t off, size_t count)
220{
221 return koneplus_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200222 sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000223}
224
Stefan Achatz5277e972012-10-17 16:35:42 +0200225static ssize_t koneplus_sysfs_read_tcu(struct file *fp,
226 struct kobject *kobj, struct bin_attribute *attr, char *buf,
227 loff_t off, size_t count)
228{
229 return koneplus_sysfs_read(fp, kobj, buf, off, count,
230 sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
231}
232
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000233static ssize_t koneplus_sysfs_read_tcu_image(struct file *fp,
234 struct kobject *kobj, struct bin_attribute *attr, char *buf,
235 loff_t off, size_t count)
236{
237 return koneplus_sysfs_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200238 sizeof(struct koneplus_tcu_image), KONEPLUS_COMMAND_TCU);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000239}
240
241static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
242 struct kobject *kobj, struct bin_attribute *attr, char *buf,
243 loff_t off, size_t count)
244{
245 struct device *dev =
246 container_of(kobj, struct device, kobj)->parent->parent;
247 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
248
249 if (off >= sizeof(struct koneplus_profile_settings))
250 return 0;
251
252 if (off + count > sizeof(struct koneplus_profile_settings))
253 count = sizeof(struct koneplus_profile_settings) - off;
254
255 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100256 memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000257 count);
258 mutex_unlock(&koneplus->koneplus_lock);
259
260 return count;
261}
262
263static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
264 struct kobject *kobj, struct bin_attribute *attr, char *buf,
265 loff_t off, size_t count)
266{
267 struct device *dev =
268 container_of(kobj, struct device, kobj)->parent->parent;
269 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
270 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
271 int retval = 0;
272 int difference;
273 int profile_number;
274 struct koneplus_profile_settings *profile_settings;
275
276 if (off != 0 || count != sizeof(struct koneplus_profile_settings))
277 return -EINVAL;
278
279 profile_number = ((struct koneplus_profile_settings const *)buf)->number;
280 profile_settings = &koneplus->profile_settings[profile_number];
281
282 mutex_lock(&koneplus->koneplus_lock);
283 difference = memcmp(buf, profile_settings,
284 sizeof(struct koneplus_profile_settings));
285 if (difference) {
286 retval = koneplus_set_profile_settings(usb_dev,
287 (struct koneplus_profile_settings const *)buf);
288 if (!retval)
289 memcpy(profile_settings, buf,
290 sizeof(struct koneplus_profile_settings));
291 }
292 mutex_unlock(&koneplus->koneplus_lock);
293
294 if (retval)
295 return retval;
296
297 return sizeof(struct koneplus_profile_settings);
298}
299
300static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
301 struct kobject *kobj, struct bin_attribute *attr, char *buf,
302 loff_t off, size_t count)
303{
304 struct device *dev =
305 container_of(kobj, struct device, kobj)->parent->parent;
306 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
307
308 if (off >= sizeof(struct koneplus_profile_buttons))
309 return 0;
310
311 if (off + count > sizeof(struct koneplus_profile_buttons))
312 count = sizeof(struct koneplus_profile_buttons) - off;
313
314 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100315 memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000316 count);
317 mutex_unlock(&koneplus->koneplus_lock);
318
319 return count;
320}
321
322static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
323 struct kobject *kobj, struct bin_attribute *attr, char *buf,
324 loff_t off, size_t count)
325{
326 struct device *dev =
327 container_of(kobj, struct device, kobj)->parent->parent;
328 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
329 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
330 int retval = 0;
331 int difference;
332 uint profile_number;
333 struct koneplus_profile_buttons *profile_buttons;
334
335 if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
336 return -EINVAL;
337
338 profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
339 profile_buttons = &koneplus->profile_buttons[profile_number];
340
341 mutex_lock(&koneplus->koneplus_lock);
342 difference = memcmp(buf, profile_buttons,
343 sizeof(struct koneplus_profile_buttons));
344 if (difference) {
345 retval = koneplus_set_profile_buttons(usb_dev,
346 (struct koneplus_profile_buttons const *)buf);
347 if (!retval)
348 memcpy(profile_buttons, buf,
349 sizeof(struct koneplus_profile_buttons));
350 }
351 mutex_unlock(&koneplus->koneplus_lock);
352
353 if (retval)
354 return retval;
355
356 return sizeof(struct koneplus_profile_buttons);
357}
358
Stefan Achatzb50f3152011-04-13 17:17:52 +0200359static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000360 struct device_attribute *attr, char *buf)
361{
362 struct koneplus_device *koneplus =
363 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzb50f3152011-04-13 17:17:52 +0200364 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000365}
366
Stefan Achatzb50f3152011-04-13 17:17:52 +0200367static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000368 struct device_attribute *attr, char const *buf, size_t size)
369{
370 struct koneplus_device *koneplus;
371 struct usb_device *usb_dev;
372 unsigned long profile;
373 int retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200374 struct koneplus_roccat_report roccat_report;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000375
376 dev = dev->parent->parent;
377 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
378 usb_dev = interface_to_usbdev(to_usb_interface(dev));
379
380 retval = strict_strtoul(buf, 10, &profile);
381 if (retval)
382 return retval;
383
Stefan Achatz901e64d2011-06-12 10:02:44 +0200384 if (profile > 4)
385 return -EINVAL;
386
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000387 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200388
389 retval = koneplus_set_actual_profile(usb_dev, profile);
390 if (retval) {
391 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000392 return retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200393 }
394
Stefan Achatz901e64d2011-06-12 10:02:44 +0200395 koneplus_profile_activated(koneplus, profile);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200396
397 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
398 roccat_report.data1 = profile + 1;
399 roccat_report.data2 = 0;
400 roccat_report.profile = profile + 1;
401 roccat_report_event(koneplus->chrdev_minor,
402 (uint8_t const *)&roccat_report);
403
404 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000405
406 return size;
407}
408
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000409static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
410 struct device_attribute *attr, char *buf)
411{
412 struct koneplus_device *koneplus =
413 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
414 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
415}
416
417static struct device_attribute koneplus_attributes[] = {
Stefan Achatzb50f3152011-04-13 17:17:52 +0200418 __ATTR(actual_profile, 0660,
419 koneplus_sysfs_show_actual_profile,
420 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000421 __ATTR(startup_profile, 0660,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200422 koneplus_sysfs_show_actual_profile,
423 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000424 __ATTR(firmware_version, 0440,
425 koneplus_sysfs_show_firmware_version, NULL),
426 __ATTR_NULL
427};
428
429static struct bin_attribute koneplus_bin_attributes[] = {
430 {
Stefan Achatz61c29f52011-03-14 21:43:07 +0100431 .attr = { .name = "sensor", .mode = 0660 },
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000432 .size = sizeof(struct koneplus_sensor),
433 .read = koneplus_sysfs_read_sensor,
434 .write = koneplus_sysfs_write_sensor
435 },
436 {
Stefan Achatz5277e972012-10-17 16:35:42 +0200437 .attr = { .name = "tcu", .mode = 0660 },
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000438 .size = sizeof(struct koneplus_tcu),
Stefan Achatz5277e972012-10-17 16:35:42 +0200439 .read = koneplus_sysfs_read_tcu,
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000440 .write = koneplus_sysfs_write_tcu
441 },
442 {
443 .attr = { .name = "tcu_image", .mode = 0440 },
444 .size = sizeof(struct koneplus_tcu_image),
445 .read = koneplus_sysfs_read_tcu_image
446 },
447 {
448 .attr = { .name = "profile_settings", .mode = 0220 },
449 .size = sizeof(struct koneplus_profile_settings),
450 .write = koneplus_sysfs_write_profile_settings
451 },
452 {
453 .attr = { .name = "profile1_settings", .mode = 0440 },
454 .size = sizeof(struct koneplus_profile_settings),
455 .read = koneplus_sysfs_read_profilex_settings,
456 .private = &profile_numbers[0]
457 },
458 {
459 .attr = { .name = "profile2_settings", .mode = 0440 },
460 .size = sizeof(struct koneplus_profile_settings),
461 .read = koneplus_sysfs_read_profilex_settings,
462 .private = &profile_numbers[1]
463 },
464 {
465 .attr = { .name = "profile3_settings", .mode = 0440 },
466 .size = sizeof(struct koneplus_profile_settings),
467 .read = koneplus_sysfs_read_profilex_settings,
468 .private = &profile_numbers[2]
469 },
470 {
471 .attr = { .name = "profile4_settings", .mode = 0440 },
472 .size = sizeof(struct koneplus_profile_settings),
473 .read = koneplus_sysfs_read_profilex_settings,
474 .private = &profile_numbers[3]
475 },
476 {
477 .attr = { .name = "profile5_settings", .mode = 0440 },
478 .size = sizeof(struct koneplus_profile_settings),
479 .read = koneplus_sysfs_read_profilex_settings,
480 .private = &profile_numbers[4]
481 },
482 {
483 .attr = { .name = "profile_buttons", .mode = 0220 },
484 .size = sizeof(struct koneplus_profile_buttons),
485 .write = koneplus_sysfs_write_profile_buttons
486 },
487 {
488 .attr = { .name = "profile1_buttons", .mode = 0440 },
489 .size = sizeof(struct koneplus_profile_buttons),
490 .read = koneplus_sysfs_read_profilex_buttons,
491 .private = &profile_numbers[0]
492 },
493 {
494 .attr = { .name = "profile2_buttons", .mode = 0440 },
495 .size = sizeof(struct koneplus_profile_buttons),
496 .read = koneplus_sysfs_read_profilex_buttons,
497 .private = &profile_numbers[1]
498 },
499 {
500 .attr = { .name = "profile3_buttons", .mode = 0440 },
501 .size = sizeof(struct koneplus_profile_buttons),
502 .read = koneplus_sysfs_read_profilex_buttons,
503 .private = &profile_numbers[2]
504 },
505 {
506 .attr = { .name = "profile4_buttons", .mode = 0440 },
507 .size = sizeof(struct koneplus_profile_buttons),
508 .read = koneplus_sysfs_read_profilex_buttons,
509 .private = &profile_numbers[3]
510 },
511 {
512 .attr = { .name = "profile5_buttons", .mode = 0440 },
513 .size = sizeof(struct koneplus_profile_buttons),
514 .read = koneplus_sysfs_read_profilex_buttons,
515 .private = &profile_numbers[4]
516 },
517 {
518 .attr = { .name = "macro", .mode = 0220 },
519 .size = sizeof(struct koneplus_macro),
520 .write = koneplus_sysfs_write_macro
521 },
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200522 {
523 .attr = { .name = "talk", .mode = 0220 },
524 .size = sizeof(struct koneplus_talk),
525 .write = koneplus_sysfs_write_talk
526 },
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000527 __ATTR_NULL
528};
529
530static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
531 struct koneplus_device *koneplus)
532{
533 int retval, i;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200534 static uint wait = 200;
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000535
536 mutex_init(&koneplus->koneplus_lock);
537
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000538 retval = koneplus_get_info(usb_dev, &koneplus->info);
539 if (retval)
540 return retval;
541
542 for (i = 0; i < 5; ++i) {
543 msleep(wait);
544 retval = koneplus_get_profile_settings(usb_dev,
545 &koneplus->profile_settings[i], i);
546 if (retval)
547 return retval;
548
549 msleep(wait);
550 retval = koneplus_get_profile_buttons(usb_dev,
551 &koneplus->profile_buttons[i], i);
552 if (retval)
553 return retval;
554 }
555
Stefan Achatzb50f3152011-04-13 17:17:52 +0200556 msleep(wait);
557 retval = koneplus_get_actual_profile(usb_dev);
558 if (retval < 0)
559 return retval;
560 koneplus_profile_activated(koneplus, retval);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000561
562 return 0;
563}
564
565static int koneplus_init_specials(struct hid_device *hdev)
566{
567 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
568 struct usb_device *usb_dev = interface_to_usbdev(intf);
569 struct koneplus_device *koneplus;
570 int retval;
571
572 if (intf->cur_altsetting->desc.bInterfaceProtocol
573 == USB_INTERFACE_PROTOCOL_MOUSE) {
574
575 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
576 if (!koneplus) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100577 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000578 return -ENOMEM;
579 }
580 hid_set_drvdata(hdev, koneplus);
581
582 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
583 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100584 hid_err(hdev, "couldn't init struct koneplus_device\n");
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000585 goto exit_free;
586 }
587
Stefan Achatz8211e462011-01-30 13:38:25 +0100588 retval = roccat_connect(koneplus_class, hdev,
589 sizeof(struct koneplus_roccat_report));
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000590 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100591 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000592 } else {
593 koneplus->chrdev_minor = retval;
594 koneplus->roccat_claimed = 1;
595 }
596 } else {
597 hid_set_drvdata(hdev, NULL);
598 }
599
600 return 0;
601exit_free:
602 kfree(koneplus);
603 return retval;
604}
605
606static void koneplus_remove_specials(struct hid_device *hdev)
607{
608 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
609 struct koneplus_device *koneplus;
610
611 if (intf->cur_altsetting->desc.bInterfaceProtocol
612 == USB_INTERFACE_PROTOCOL_MOUSE) {
613 koneplus = hid_get_drvdata(hdev);
614 if (koneplus->roccat_claimed)
615 roccat_disconnect(koneplus->chrdev_minor);
616 kfree(koneplus);
617 }
618}
619
620static int koneplus_probe(struct hid_device *hdev,
621 const struct hid_device_id *id)
622{
623 int retval;
624
625 retval = hid_parse(hdev);
626 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100627 hid_err(hdev, "parse failed\n");
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000628 goto exit;
629 }
630
631 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
632 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100633 hid_err(hdev, "hw start failed\n");
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000634 goto exit;
635 }
636
637 retval = koneplus_init_specials(hdev);
638 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100639 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000640 goto exit_stop;
641 }
642
643 return 0;
644
645exit_stop:
646 hid_hw_stop(hdev);
647exit:
648 return retval;
649}
650
651static void koneplus_remove(struct hid_device *hdev)
652{
653 koneplus_remove_specials(hdev);
654 hid_hw_stop(hdev);
655}
656
657static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
658 u8 const *data)
659{
660 struct koneplus_mouse_report_button const *button_report;
661
662 switch (data[0]) {
663 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
664 button_report = (struct koneplus_mouse_report_button const *)data;
665 switch (button_report->type) {
666 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
667 koneplus_profile_activated(koneplus, button_report->data1 - 1);
668 break;
669 }
670 break;
671 }
672}
673
674static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
675 u8 const *data)
676{
677 struct koneplus_roccat_report roccat_report;
678 struct koneplus_mouse_report_button const *button_report;
679
680 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
681 return;
682
683 button_report = (struct koneplus_mouse_report_button const *)data;
684
685 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
686 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
687 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
688 return;
689
690 roccat_report.type = button_report->type;
691 roccat_report.data1 = button_report->data1;
692 roccat_report.data2 = button_report->data2;
693 roccat_report.profile = koneplus->actual_profile + 1;
694 roccat_report_event(koneplus->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100695 (uint8_t const *)&roccat_report);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000696}
697
698static int koneplus_raw_event(struct hid_device *hdev,
699 struct hid_report *report, u8 *data, int size)
700{
701 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
702 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
703
704 if (intf->cur_altsetting->desc.bInterfaceProtocol
705 != USB_INTERFACE_PROTOCOL_MOUSE)
706 return 0;
707
Stefan Achatz901e64d2011-06-12 10:02:44 +0200708 if (koneplus == NULL)
709 return 0;
710
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000711 koneplus_keep_values_up_to_date(koneplus, data);
712
713 if (koneplus->roccat_claimed)
714 koneplus_report_to_chrdev(koneplus, data);
715
716 return 0;
717}
718
719static const struct hid_device_id koneplus_devices[] = {
720 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
721 { }
722};
723
724MODULE_DEVICE_TABLE(hid, koneplus_devices);
725
726static struct hid_driver koneplus_driver = {
727 .name = "koneplus",
728 .id_table = koneplus_devices,
729 .probe = koneplus_probe,
730 .remove = koneplus_remove,
731 .raw_event = koneplus_raw_event
732};
733
734static int __init koneplus_init(void)
735{
736 int retval;
737
738 /* class name has to be same as driver name */
739 koneplus_class = class_create(THIS_MODULE, "koneplus");
740 if (IS_ERR(koneplus_class))
741 return PTR_ERR(koneplus_class);
742 koneplus_class->dev_attrs = koneplus_attributes;
743 koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
744
745 retval = hid_register_driver(&koneplus_driver);
746 if (retval)
747 class_destroy(koneplus_class);
748 return retval;
749}
750
751static void __exit koneplus_exit(void)
752{
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000753 hid_unregister_driver(&koneplus_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100754 class_destroy(koneplus_class);
Stefan Achatz47dbdbf2010-11-26 19:57:42 +0000755}
756
757module_init(koneplus_init);
758module_exit(koneplus_exit);
759
760MODULE_AUTHOR("Stefan Achatz");
761MODULE_DESCRIPTION("USB Roccat Kone[+] driver");
762MODULE_LICENSE("GPL v2");