blob: f5602fec48655016ca0fabdad1b743f71c17ae52 [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.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
Stefan Achatz47dbdbff2010-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 Achatz47dbdbff2010-11-26 19:57:42 +000025#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010026#include "hid-roccat-common.h"
Stefan Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-11-26 19:57:42 +0000111{
Stefan Achatzb50f3152011-04-13 17:17:52 +0200112 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-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 Achatz47dbdbff2010-11-26 19:57:42 +0000117
Stefan Achatzb50f3152011-04-13 17:17:52 +0200118 return retval ? retval : buf.actual_profile;
Stefan Achatz47dbdbff2010-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 Achatz47dbdbff2010-11-26 19:57:42 +0000123{
Stefan Achatzb50f3152011-04-13 17:17:52 +0200124 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-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 Achatz47dbdbff2010-11-26 19:57:42 +0000223}
224
225static ssize_t koneplus_sysfs_read_tcu_image(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,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200230 sizeof(struct koneplus_tcu_image), KONEPLUS_COMMAND_TCU);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000231}
232
233static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
234 struct kobject *kobj, struct bin_attribute *attr, char *buf,
235 loff_t off, size_t count)
236{
237 struct device *dev =
238 container_of(kobj, struct device, kobj)->parent->parent;
239 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
240
241 if (off >= sizeof(struct koneplus_profile_settings))
242 return 0;
243
244 if (off + count > sizeof(struct koneplus_profile_settings))
245 count = sizeof(struct koneplus_profile_settings) - off;
246
247 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100248 memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000249 count);
250 mutex_unlock(&koneplus->koneplus_lock);
251
252 return count;
253}
254
255static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
256 struct kobject *kobj, struct bin_attribute *attr, char *buf,
257 loff_t off, size_t count)
258{
259 struct device *dev =
260 container_of(kobj, struct device, kobj)->parent->parent;
261 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
262 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
263 int retval = 0;
264 int difference;
265 int profile_number;
266 struct koneplus_profile_settings *profile_settings;
267
268 if (off != 0 || count != sizeof(struct koneplus_profile_settings))
269 return -EINVAL;
270
271 profile_number = ((struct koneplus_profile_settings const *)buf)->number;
272 profile_settings = &koneplus->profile_settings[profile_number];
273
274 mutex_lock(&koneplus->koneplus_lock);
275 difference = memcmp(buf, profile_settings,
276 sizeof(struct koneplus_profile_settings));
277 if (difference) {
278 retval = koneplus_set_profile_settings(usb_dev,
279 (struct koneplus_profile_settings const *)buf);
280 if (!retval)
281 memcpy(profile_settings, buf,
282 sizeof(struct koneplus_profile_settings));
283 }
284 mutex_unlock(&koneplus->koneplus_lock);
285
286 if (retval)
287 return retval;
288
289 return sizeof(struct koneplus_profile_settings);
290}
291
292static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
293 struct kobject *kobj, struct bin_attribute *attr, char *buf,
294 loff_t off, size_t count)
295{
296 struct device *dev =
297 container_of(kobj, struct device, kobj)->parent->parent;
298 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
299
300 if (off >= sizeof(struct koneplus_profile_buttons))
301 return 0;
302
303 if (off + count > sizeof(struct koneplus_profile_buttons))
304 count = sizeof(struct koneplus_profile_buttons) - off;
305
306 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100307 memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000308 count);
309 mutex_unlock(&koneplus->koneplus_lock);
310
311 return count;
312}
313
314static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
315 struct kobject *kobj, struct bin_attribute *attr, char *buf,
316 loff_t off, size_t count)
317{
318 struct device *dev =
319 container_of(kobj, struct device, kobj)->parent->parent;
320 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
321 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
322 int retval = 0;
323 int difference;
324 uint profile_number;
325 struct koneplus_profile_buttons *profile_buttons;
326
327 if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
328 return -EINVAL;
329
330 profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
331 profile_buttons = &koneplus->profile_buttons[profile_number];
332
333 mutex_lock(&koneplus->koneplus_lock);
334 difference = memcmp(buf, profile_buttons,
335 sizeof(struct koneplus_profile_buttons));
336 if (difference) {
337 retval = koneplus_set_profile_buttons(usb_dev,
338 (struct koneplus_profile_buttons const *)buf);
339 if (!retval)
340 memcpy(profile_buttons, buf,
341 sizeof(struct koneplus_profile_buttons));
342 }
343 mutex_unlock(&koneplus->koneplus_lock);
344
345 if (retval)
346 return retval;
347
348 return sizeof(struct koneplus_profile_buttons);
349}
350
Stefan Achatzb50f3152011-04-13 17:17:52 +0200351static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000352 struct device_attribute *attr, char *buf)
353{
354 struct koneplus_device *koneplus =
355 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzb50f3152011-04-13 17:17:52 +0200356 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000357}
358
Stefan Achatzb50f3152011-04-13 17:17:52 +0200359static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000360 struct device_attribute *attr, char const *buf, size_t size)
361{
362 struct koneplus_device *koneplus;
363 struct usb_device *usb_dev;
364 unsigned long profile;
365 int retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200366 struct koneplus_roccat_report roccat_report;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000367
368 dev = dev->parent->parent;
369 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
370 usb_dev = interface_to_usbdev(to_usb_interface(dev));
371
372 retval = strict_strtoul(buf, 10, &profile);
373 if (retval)
374 return retval;
375
Stefan Achatz901e64d2011-06-12 10:02:44 +0200376 if (profile > 4)
377 return -EINVAL;
378
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000379 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200380
381 retval = koneplus_set_actual_profile(usb_dev, profile);
382 if (retval) {
383 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000384 return retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200385 }
386
Stefan Achatz901e64d2011-06-12 10:02:44 +0200387 koneplus_profile_activated(koneplus, profile);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200388
389 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
390 roccat_report.data1 = profile + 1;
391 roccat_report.data2 = 0;
392 roccat_report.profile = profile + 1;
393 roccat_report_event(koneplus->chrdev_minor,
394 (uint8_t const *)&roccat_report);
395
396 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000397
398 return size;
399}
400
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000401static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
402 struct device_attribute *attr, char *buf)
403{
404 struct koneplus_device *koneplus =
405 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
406 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
407}
408
409static struct device_attribute koneplus_attributes[] = {
Stefan Achatzb50f3152011-04-13 17:17:52 +0200410 __ATTR(actual_profile, 0660,
411 koneplus_sysfs_show_actual_profile,
412 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000413 __ATTR(startup_profile, 0660,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200414 koneplus_sysfs_show_actual_profile,
415 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000416 __ATTR(firmware_version, 0440,
417 koneplus_sysfs_show_firmware_version, NULL),
418 __ATTR_NULL
419};
420
421static struct bin_attribute koneplus_bin_attributes[] = {
422 {
Stefan Achatz61c29f52011-03-14 21:43:07 +0100423 .attr = { .name = "sensor", .mode = 0660 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000424 .size = sizeof(struct koneplus_sensor),
425 .read = koneplus_sysfs_read_sensor,
426 .write = koneplus_sysfs_write_sensor
427 },
428 {
429 .attr = { .name = "tcu", .mode = 0220 },
430 .size = sizeof(struct koneplus_tcu),
431 .write = koneplus_sysfs_write_tcu
432 },
433 {
434 .attr = { .name = "tcu_image", .mode = 0440 },
435 .size = sizeof(struct koneplus_tcu_image),
436 .read = koneplus_sysfs_read_tcu_image
437 },
438 {
439 .attr = { .name = "profile_settings", .mode = 0220 },
440 .size = sizeof(struct koneplus_profile_settings),
441 .write = koneplus_sysfs_write_profile_settings
442 },
443 {
444 .attr = { .name = "profile1_settings", .mode = 0440 },
445 .size = sizeof(struct koneplus_profile_settings),
446 .read = koneplus_sysfs_read_profilex_settings,
447 .private = &profile_numbers[0]
448 },
449 {
450 .attr = { .name = "profile2_settings", .mode = 0440 },
451 .size = sizeof(struct koneplus_profile_settings),
452 .read = koneplus_sysfs_read_profilex_settings,
453 .private = &profile_numbers[1]
454 },
455 {
456 .attr = { .name = "profile3_settings", .mode = 0440 },
457 .size = sizeof(struct koneplus_profile_settings),
458 .read = koneplus_sysfs_read_profilex_settings,
459 .private = &profile_numbers[2]
460 },
461 {
462 .attr = { .name = "profile4_settings", .mode = 0440 },
463 .size = sizeof(struct koneplus_profile_settings),
464 .read = koneplus_sysfs_read_profilex_settings,
465 .private = &profile_numbers[3]
466 },
467 {
468 .attr = { .name = "profile5_settings", .mode = 0440 },
469 .size = sizeof(struct koneplus_profile_settings),
470 .read = koneplus_sysfs_read_profilex_settings,
471 .private = &profile_numbers[4]
472 },
473 {
474 .attr = { .name = "profile_buttons", .mode = 0220 },
475 .size = sizeof(struct koneplus_profile_buttons),
476 .write = koneplus_sysfs_write_profile_buttons
477 },
478 {
479 .attr = { .name = "profile1_buttons", .mode = 0440 },
480 .size = sizeof(struct koneplus_profile_buttons),
481 .read = koneplus_sysfs_read_profilex_buttons,
482 .private = &profile_numbers[0]
483 },
484 {
485 .attr = { .name = "profile2_buttons", .mode = 0440 },
486 .size = sizeof(struct koneplus_profile_buttons),
487 .read = koneplus_sysfs_read_profilex_buttons,
488 .private = &profile_numbers[1]
489 },
490 {
491 .attr = { .name = "profile3_buttons", .mode = 0440 },
492 .size = sizeof(struct koneplus_profile_buttons),
493 .read = koneplus_sysfs_read_profilex_buttons,
494 .private = &profile_numbers[2]
495 },
496 {
497 .attr = { .name = "profile4_buttons", .mode = 0440 },
498 .size = sizeof(struct koneplus_profile_buttons),
499 .read = koneplus_sysfs_read_profilex_buttons,
500 .private = &profile_numbers[3]
501 },
502 {
503 .attr = { .name = "profile5_buttons", .mode = 0440 },
504 .size = sizeof(struct koneplus_profile_buttons),
505 .read = koneplus_sysfs_read_profilex_buttons,
506 .private = &profile_numbers[4]
507 },
508 {
509 .attr = { .name = "macro", .mode = 0220 },
510 .size = sizeof(struct koneplus_macro),
511 .write = koneplus_sysfs_write_macro
512 },
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200513 {
514 .attr = { .name = "talk", .mode = 0220 },
515 .size = sizeof(struct koneplus_talk),
516 .write = koneplus_sysfs_write_talk
517 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000518 __ATTR_NULL
519};
520
521static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
522 struct koneplus_device *koneplus)
523{
524 int retval, i;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200525 static uint wait = 200;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000526
527 mutex_init(&koneplus->koneplus_lock);
528
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000529 retval = koneplus_get_info(usb_dev, &koneplus->info);
530 if (retval)
531 return retval;
532
533 for (i = 0; i < 5; ++i) {
534 msleep(wait);
535 retval = koneplus_get_profile_settings(usb_dev,
536 &koneplus->profile_settings[i], i);
537 if (retval)
538 return retval;
539
540 msleep(wait);
541 retval = koneplus_get_profile_buttons(usb_dev,
542 &koneplus->profile_buttons[i], i);
543 if (retval)
544 return retval;
545 }
546
Stefan Achatzb50f3152011-04-13 17:17:52 +0200547 msleep(wait);
548 retval = koneplus_get_actual_profile(usb_dev);
549 if (retval < 0)
550 return retval;
551 koneplus_profile_activated(koneplus, retval);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000552
553 return 0;
554}
555
556static int koneplus_init_specials(struct hid_device *hdev)
557{
558 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
559 struct usb_device *usb_dev = interface_to_usbdev(intf);
560 struct koneplus_device *koneplus;
561 int retval;
562
563 if (intf->cur_altsetting->desc.bInterfaceProtocol
564 == USB_INTERFACE_PROTOCOL_MOUSE) {
565
566 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
567 if (!koneplus) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100568 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000569 return -ENOMEM;
570 }
571 hid_set_drvdata(hdev, koneplus);
572
573 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
574 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100575 hid_err(hdev, "couldn't init struct koneplus_device\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000576 goto exit_free;
577 }
578
Stefan Achatz8211e462011-01-30 13:38:25 +0100579 retval = roccat_connect(koneplus_class, hdev,
580 sizeof(struct koneplus_roccat_report));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000581 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100582 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000583 } else {
584 koneplus->chrdev_minor = retval;
585 koneplus->roccat_claimed = 1;
586 }
587 } else {
588 hid_set_drvdata(hdev, NULL);
589 }
590
591 return 0;
592exit_free:
593 kfree(koneplus);
594 return retval;
595}
596
597static void koneplus_remove_specials(struct hid_device *hdev)
598{
599 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
600 struct koneplus_device *koneplus;
601
602 if (intf->cur_altsetting->desc.bInterfaceProtocol
603 == USB_INTERFACE_PROTOCOL_MOUSE) {
604 koneplus = hid_get_drvdata(hdev);
605 if (koneplus->roccat_claimed)
606 roccat_disconnect(koneplus->chrdev_minor);
607 kfree(koneplus);
608 }
609}
610
611static int koneplus_probe(struct hid_device *hdev,
612 const struct hid_device_id *id)
613{
614 int retval;
615
616 retval = hid_parse(hdev);
617 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100618 hid_err(hdev, "parse failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000619 goto exit;
620 }
621
622 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
623 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100624 hid_err(hdev, "hw start failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000625 goto exit;
626 }
627
628 retval = koneplus_init_specials(hdev);
629 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100630 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000631 goto exit_stop;
632 }
633
634 return 0;
635
636exit_stop:
637 hid_hw_stop(hdev);
638exit:
639 return retval;
640}
641
642static void koneplus_remove(struct hid_device *hdev)
643{
644 koneplus_remove_specials(hdev);
645 hid_hw_stop(hdev);
646}
647
648static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
649 u8 const *data)
650{
651 struct koneplus_mouse_report_button const *button_report;
652
653 switch (data[0]) {
654 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
655 button_report = (struct koneplus_mouse_report_button const *)data;
656 switch (button_report->type) {
657 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
658 koneplus_profile_activated(koneplus, button_report->data1 - 1);
659 break;
660 }
661 break;
662 }
663}
664
665static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
666 u8 const *data)
667{
668 struct koneplus_roccat_report roccat_report;
669 struct koneplus_mouse_report_button const *button_report;
670
671 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
672 return;
673
674 button_report = (struct koneplus_mouse_report_button const *)data;
675
676 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
677 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
678 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
679 return;
680
681 roccat_report.type = button_report->type;
682 roccat_report.data1 = button_report->data1;
683 roccat_report.data2 = button_report->data2;
684 roccat_report.profile = koneplus->actual_profile + 1;
685 roccat_report_event(koneplus->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100686 (uint8_t const *)&roccat_report);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000687}
688
689static int koneplus_raw_event(struct hid_device *hdev,
690 struct hid_report *report, u8 *data, int size)
691{
692 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
693 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
694
695 if (intf->cur_altsetting->desc.bInterfaceProtocol
696 != USB_INTERFACE_PROTOCOL_MOUSE)
697 return 0;
698
Stefan Achatz901e64d2011-06-12 10:02:44 +0200699 if (koneplus == NULL)
700 return 0;
701
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000702 koneplus_keep_values_up_to_date(koneplus, data);
703
704 if (koneplus->roccat_claimed)
705 koneplus_report_to_chrdev(koneplus, data);
706
707 return 0;
708}
709
710static const struct hid_device_id koneplus_devices[] = {
711 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
712 { }
713};
714
715MODULE_DEVICE_TABLE(hid, koneplus_devices);
716
717static struct hid_driver koneplus_driver = {
718 .name = "koneplus",
719 .id_table = koneplus_devices,
720 .probe = koneplus_probe,
721 .remove = koneplus_remove,
722 .raw_event = koneplus_raw_event
723};
724
725static int __init koneplus_init(void)
726{
727 int retval;
728
729 /* class name has to be same as driver name */
730 koneplus_class = class_create(THIS_MODULE, "koneplus");
731 if (IS_ERR(koneplus_class))
732 return PTR_ERR(koneplus_class);
733 koneplus_class->dev_attrs = koneplus_attributes;
734 koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
735
736 retval = hid_register_driver(&koneplus_driver);
737 if (retval)
738 class_destroy(koneplus_class);
739 return retval;
740}
741
742static void __exit koneplus_exit(void)
743{
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000744 hid_unregister_driver(&koneplus_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100745 class_destroy(koneplus_class);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000746}
747
748module_init(koneplus_init);
749module_exit(koneplus_exit);
750
751MODULE_AUTHOR("Stefan Achatz");
752MODULE_DESCRIPTION("USB Roccat Kone[+] driver");
753MODULE_LICENSE("GPL v2");