blob: 1896c69ea512f787b264ed27f82f13d813fd82ea [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stefan Achatz47dbdbff2010-11-26 19:57:42 +00002/*
3 * Roccat Kone[+] driver for Linux
4 *
5 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
6 */
7
8/*
Stefan Achatz47dbdbff2010-11-26 19:57:42 +00009 */
10
11/*
12 * Roccat Kone[+] is an updated/improved version of the Kone with more memory
13 * and functionality and without the non-standard behaviours the Kone had.
Stefan Achatz8e74a2d2012-11-04 09:38:52 +010014 * KoneXTD has same capabilities but updated sensor.
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000015 */
16
17#include <linux/device.h>
18#include <linux/input.h>
19#include <linux/hid.h>
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000020#include <linux/module.h>
21#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010022#include <linux/hid-roccat.h>
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000023#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010024#include "hid-roccat-common.h"
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000025#include "hid-roccat-koneplus.h"
26
27static uint profile_numbers[5] = {0, 1, 2, 3, 4};
28
29static struct class *koneplus_class;
30
31static void koneplus_profile_activated(struct koneplus_device *koneplus,
32 uint new_profile)
33{
34 koneplus->actual_profile = new_profile;
35}
36
37static int koneplus_send_control(struct usb_device *usb_dev, uint value,
38 enum koneplus_control_requests request)
39{
Stefan Achatz7392d732012-05-20 22:45:04 +020040 struct roccat_common2_control control;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000041
42 if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
43 request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
44 value > 4)
45 return -EINVAL;
46
Stefan Achatz4728f2d2012-05-20 22:44:59 +020047 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
Stefan Achatz5772f632011-01-30 13:38:23 +010048 control.value = value;
49 control.request = request;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000050
Stefan Achatz7392d732012-05-20 22:45:04 +020051 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020052 ROCCAT_COMMON_COMMAND_CONTROL,
Stefan Achatz7392d732012-05-20 22:45:04 +020053 &control, sizeof(struct roccat_common2_control));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000054}
55
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000056
57/* retval is 0-4 on success, < 0 on error */
Stefan Achatzb50f3152011-04-13 17:17:52 +020058static int koneplus_get_actual_profile(struct usb_device *usb_dev)
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000059{
Stefan Achatzb50f3152011-04-13 17:17:52 +020060 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000061 int retval;
62
Stefan Achatz7392d732012-05-20 22:45:04 +020063 retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzf114fec2012-11-04 09:39:09 +010064 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000065
Stefan Achatzb50f3152011-04-13 17:17:52 +020066 return retval ? retval : buf.actual_profile;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000067}
68
Stefan Achatzb50f3152011-04-13 17:17:52 +020069static int koneplus_set_actual_profile(struct usb_device *usb_dev,
70 int new_profile)
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000071{
Stefan Achatzb50f3152011-04-13 17:17:52 +020072 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000073
Stefan Achatzb50f3152011-04-13 17:17:52 +020074 buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
Stefan Achatzf114fec2012-11-04 09:39:09 +010075 buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
Stefan Achatzb50f3152011-04-13 17:17:52 +020076 buf.actual_profile = new_profile;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000077
Stefan Achatz7392d732012-05-20 22:45:04 +020078 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020079 KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzf114fec2012-11-04 09:39:09 +010080 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000081}
82
83static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
84 char *buf, loff_t off, size_t count,
85 size_t real_size, uint command)
86{
Geliang Tang2cf83832015-12-27 17:25:24 +080087 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000088 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
89 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
90 int retval;
91
Stefan Achatzfd82be62011-01-06 09:00:41 +010092 if (off >= real_size)
93 return 0;
94
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000095 if (off != 0 || count != real_size)
96 return -EINVAL;
97
98 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +020099 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000100 mutex_unlock(&koneplus->koneplus_lock);
101
102 if (retval)
103 return retval;
104
105 return real_size;
106}
107
108static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
109 void const *buf, loff_t off, size_t count,
110 size_t real_size, uint command)
111{
Geliang Tang2cf83832015-12-27 17:25:24 +0800112 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000113 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
114 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
115 int retval;
116
117 if (off != 0 || count != real_size)
118 return -EINVAL;
119
120 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200121 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200122 buf, real_size);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000123 mutex_unlock(&koneplus->koneplus_lock);
124
125 if (retval)
126 return retval;
127
128 return real_size;
129}
130
Stefan Achatzf114fec2012-11-04 09:39:09 +0100131#define KONEPLUS_SYSFS_W(thingy, THINGY) \
132static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
133 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
134 loff_t off, size_t count) \
135{ \
136 return koneplus_sysfs_write(fp, kobj, buf, off, count, \
137 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
Stefan Achatzfabe51e2012-11-04 09:39:04 +0100138}
139
Stefan Achatzf114fec2012-11-04 09:39:09 +0100140#define KONEPLUS_SYSFS_R(thingy, THINGY) \
141static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
142 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
143 loff_t off, size_t count) \
144{ \
145 return koneplus_sysfs_read(fp, kobj, buf, off, count, \
146 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
Stefan Achatzfabe51e2012-11-04 09:39:04 +0100147}
148
Stefan Achatzf114fec2012-11-04 09:39:09 +0100149#define KONEPLUS_SYSFS_RW(thingy, THINGY) \
150KONEPLUS_SYSFS_W(thingy, THINGY) \
151KONEPLUS_SYSFS_R(thingy, THINGY)
152
153#define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700154KONEPLUS_SYSFS_RW(thingy, THINGY); \
155static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzf114fec2012-11-04 09:39:09 +0100156 .attr = { .name = #thingy, .mode = 0660 }, \
157 .size = KONEPLUS_SIZE_ ## THINGY, \
158 .read = koneplus_sysfs_read_ ## thingy, \
159 .write = koneplus_sysfs_write_ ## thingy \
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200160}
161
Stefan Achatzf114fec2012-11-04 09:39:09 +0100162#define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700163KONEPLUS_SYSFS_R(thingy, THINGY); \
164static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzf114fec2012-11-04 09:39:09 +0100165 .attr = { .name = #thingy, .mode = 0440 }, \
166 .size = KONEPLUS_SIZE_ ## THINGY, \
167 .read = koneplus_sysfs_read_ ## thingy, \
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000168}
169
Stefan Achatzf114fec2012-11-04 09:39:09 +0100170#define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700171KONEPLUS_SYSFS_W(thingy, THINGY); \
172static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzf114fec2012-11-04 09:39:09 +0100173 .attr = { .name = #thingy, .mode = 0220 }, \
174 .size = KONEPLUS_SIZE_ ## THINGY, \
175 .write = koneplus_sysfs_write_ ## thingy \
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000176}
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700177KONEPLUS_BIN_ATTRIBUTE_W(control, CONTROL);
178KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK);
179KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO);
180KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
181KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO);
182KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR);
183KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU);
184KONEPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
185KONEPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000186
187static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
188 struct kobject *kobj, struct bin_attribute *attr, char *buf,
189 loff_t off, size_t count)
190{
Geliang Tang2cf83832015-12-27 17:25:24 +0800191 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000192 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatzf114fec2012-11-04 09:39:09 +0100193 ssize_t retval;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000194
Stefan Achatzf114fec2012-11-04 09:39:09 +0100195 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
196 KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000197 if (retval)
198 return retval;
199
Stefan Achatzf114fec2012-11-04 09:39:09 +0100200 return koneplus_sysfs_read(fp, kobj, buf, off, count,
201 KONEPLUS_SIZE_PROFILE_SETTINGS,
202 KONEPLUS_COMMAND_PROFILE_SETTINGS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000203}
204
205static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
206 struct kobject *kobj, struct bin_attribute *attr, char *buf,
207 loff_t off, size_t count)
208{
Geliang Tang2cf83832015-12-27 17:25:24 +0800209 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000210 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatzf114fec2012-11-04 09:39:09 +0100211 ssize_t retval;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000212
Stefan Achatzf114fec2012-11-04 09:39:09 +0100213 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
214 KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000215 if (retval)
216 return retval;
217
Stefan Achatzf114fec2012-11-04 09:39:09 +0100218 return koneplus_sysfs_read(fp, kobj, buf, off, count,
219 KONEPLUS_SIZE_PROFILE_BUTTONS,
220 KONEPLUS_COMMAND_PROFILE_BUTTONS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000221}
222
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700223#define PROFILE_ATTR(number) \
224static struct bin_attribute bin_attr_profile##number##_settings = { \
Stefan Achatz550dbf42013-09-28 05:57:46 +0200225 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700226 .size = KONEPLUS_SIZE_PROFILE_SETTINGS, \
227 .read = koneplus_sysfs_read_profilex_settings, \
228 .private = &profile_numbers[number-1], \
229}; \
230static struct bin_attribute bin_attr_profile##number##_buttons = { \
Stefan Achatz550dbf42013-09-28 05:57:46 +0200231 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700232 .size = KONEPLUS_SIZE_PROFILE_BUTTONS, \
233 .read = koneplus_sysfs_read_profilex_buttons, \
234 .private = &profile_numbers[number-1], \
235};
236PROFILE_ATTR(1);
237PROFILE_ATTR(2);
238PROFILE_ATTR(3);
239PROFILE_ATTR(4);
240PROFILE_ATTR(5);
241
Stefan Achatzb50f3152011-04-13 17:17:52 +0200242static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000243 struct device_attribute *attr, char *buf)
244{
245 struct koneplus_device *koneplus =
246 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzb50f3152011-04-13 17:17:52 +0200247 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000248}
249
Stefan Achatzb50f3152011-04-13 17:17:52 +0200250static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000251 struct device_attribute *attr, char const *buf, size_t size)
252{
253 struct koneplus_device *koneplus;
254 struct usb_device *usb_dev;
255 unsigned long profile;
256 int retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200257 struct koneplus_roccat_report roccat_report;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000258
259 dev = dev->parent->parent;
260 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
261 usb_dev = interface_to_usbdev(to_usb_interface(dev));
262
Jingoo Handfc450b2013-07-19 15:53:16 +0900263 retval = kstrtoul(buf, 10, &profile);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000264 if (retval)
265 return retval;
266
Stefan Achatz901e64d2011-06-12 10:02:44 +0200267 if (profile > 4)
268 return -EINVAL;
269
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000270 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200271
272 retval = koneplus_set_actual_profile(usb_dev, profile);
273 if (retval) {
274 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000275 return retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200276 }
277
Stefan Achatz901e64d2011-06-12 10:02:44 +0200278 koneplus_profile_activated(koneplus, profile);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200279
280 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
281 roccat_report.data1 = profile + 1;
282 roccat_report.data2 = 0;
283 roccat_report.profile = profile + 1;
284 roccat_report_event(koneplus->chrdev_minor,
285 (uint8_t const *)&roccat_report);
286
287 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000288
289 return size;
290}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700291static DEVICE_ATTR(actual_profile, 0660,
292 koneplus_sysfs_show_actual_profile,
293 koneplus_sysfs_set_actual_profile);
294static DEVICE_ATTR(startup_profile, 0660,
295 koneplus_sysfs_show_actual_profile,
296 koneplus_sysfs_set_actual_profile);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000297
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000298static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
Stefan Achatzf114fec2012-11-04 09:39:09 +0100301 struct koneplus_device *koneplus;
302 struct usb_device *usb_dev;
303 struct koneplus_info info;
304
305 dev = dev->parent->parent;
306 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
307 usb_dev = interface_to_usbdev(to_usb_interface(dev));
308
309 mutex_lock(&koneplus->koneplus_lock);
310 roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
311 &info, KONEPLUS_SIZE_INFO);
312 mutex_unlock(&koneplus->koneplus_lock);
313
314 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000315}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700316static DEVICE_ATTR(firmware_version, 0440,
317 koneplus_sysfs_show_firmware_version, NULL);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000318
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700319static struct attribute *koneplus_attrs[] = {
320 &dev_attr_actual_profile.attr,
321 &dev_attr_startup_profile.attr,
322 &dev_attr_firmware_version.attr,
323 NULL,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000324};
325
Greg Kroah-Hartmaneb3156e2013-08-19 21:50:27 -0700326static struct bin_attribute *koneplus_bin_attributes[] = {
327 &bin_attr_control,
328 &bin_attr_talk,
329 &bin_attr_macro,
330 &bin_attr_tcu_image,
331 &bin_attr_info,
332 &bin_attr_sensor,
333 &bin_attr_tcu,
334 &bin_attr_profile_settings,
335 &bin_attr_profile_buttons,
336 &bin_attr_profile1_settings,
337 &bin_attr_profile2_settings,
338 &bin_attr_profile3_settings,
339 &bin_attr_profile4_settings,
340 &bin_attr_profile5_settings,
341 &bin_attr_profile1_buttons,
342 &bin_attr_profile2_buttons,
343 &bin_attr_profile3_buttons,
344 &bin_attr_profile4_buttons,
345 &bin_attr_profile5_buttons,
346 NULL,
347};
348
349static const struct attribute_group koneplus_group = {
350 .attrs = koneplus_attrs,
351 .bin_attrs = koneplus_bin_attributes,
352};
353
354static const struct attribute_group *koneplus_groups[] = {
355 &koneplus_group,
356 NULL,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000357};
358
359static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
360 struct koneplus_device *koneplus)
361{
Stefan Achatzf114fec2012-11-04 09:39:09 +0100362 int retval;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000363
364 mutex_init(&koneplus->koneplus_lock);
365
Stefan Achatzb50f3152011-04-13 17:17:52 +0200366 retval = koneplus_get_actual_profile(usb_dev);
367 if (retval < 0)
368 return retval;
369 koneplus_profile_activated(koneplus, retval);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000370
371 return 0;
372}
373
374static int koneplus_init_specials(struct hid_device *hdev)
375{
376 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
377 struct usb_device *usb_dev = interface_to_usbdev(intf);
378 struct koneplus_device *koneplus;
379 int retval;
380
381 if (intf->cur_altsetting->desc.bInterfaceProtocol
382 == USB_INTERFACE_PROTOCOL_MOUSE) {
383
384 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
385 if (!koneplus) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100386 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000387 return -ENOMEM;
388 }
389 hid_set_drvdata(hdev, koneplus);
390
391 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
392 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100393 hid_err(hdev, "couldn't init struct koneplus_device\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000394 goto exit_free;
395 }
396
Stefan Achatz8211e462011-01-30 13:38:25 +0100397 retval = roccat_connect(koneplus_class, hdev,
398 sizeof(struct koneplus_roccat_report));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000399 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100400 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000401 } else {
402 koneplus->chrdev_minor = retval;
403 koneplus->roccat_claimed = 1;
404 }
405 } else {
406 hid_set_drvdata(hdev, NULL);
407 }
408
409 return 0;
410exit_free:
411 kfree(koneplus);
412 return retval;
413}
414
415static void koneplus_remove_specials(struct hid_device *hdev)
416{
417 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
418 struct koneplus_device *koneplus;
419
420 if (intf->cur_altsetting->desc.bInterfaceProtocol
421 == USB_INTERFACE_PROTOCOL_MOUSE) {
422 koneplus = hid_get_drvdata(hdev);
423 if (koneplus->roccat_claimed)
424 roccat_disconnect(koneplus->chrdev_minor);
425 kfree(koneplus);
426 }
427}
428
429static int koneplus_probe(struct hid_device *hdev,
430 const struct hid_device_id *id)
431{
432 int retval;
433
Greg Kroah-Hartman93020952021-12-01 19:35:03 +0100434 if (!hid_is_usb(hdev))
435 return -EINVAL;
436
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000437 retval = hid_parse(hdev);
438 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100439 hid_err(hdev, "parse failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000440 goto exit;
441 }
442
443 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
444 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100445 hid_err(hdev, "hw start failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000446 goto exit;
447 }
448
449 retval = koneplus_init_specials(hdev);
450 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100451 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000452 goto exit_stop;
453 }
454
455 return 0;
456
457exit_stop:
458 hid_hw_stop(hdev);
459exit:
460 return retval;
461}
462
463static void koneplus_remove(struct hid_device *hdev)
464{
465 koneplus_remove_specials(hdev);
466 hid_hw_stop(hdev);
467}
468
469static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
470 u8 const *data)
471{
472 struct koneplus_mouse_report_button const *button_report;
473
474 switch (data[0]) {
475 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
476 button_report = (struct koneplus_mouse_report_button const *)data;
477 switch (button_report->type) {
478 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
479 koneplus_profile_activated(koneplus, button_report->data1 - 1);
480 break;
481 }
482 break;
483 }
484}
485
486static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
487 u8 const *data)
488{
489 struct koneplus_roccat_report roccat_report;
490 struct koneplus_mouse_report_button const *button_report;
491
492 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
493 return;
494
495 button_report = (struct koneplus_mouse_report_button const *)data;
496
497 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
498 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
499 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
500 return;
501
502 roccat_report.type = button_report->type;
503 roccat_report.data1 = button_report->data1;
504 roccat_report.data2 = button_report->data2;
505 roccat_report.profile = koneplus->actual_profile + 1;
506 roccat_report_event(koneplus->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100507 (uint8_t const *)&roccat_report);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000508}
509
510static int koneplus_raw_event(struct hid_device *hdev,
511 struct hid_report *report, u8 *data, int size)
512{
513 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
514 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
515
516 if (intf->cur_altsetting->desc.bInterfaceProtocol
517 != USB_INTERFACE_PROTOCOL_MOUSE)
518 return 0;
519
Stefan Achatz901e64d2011-06-12 10:02:44 +0200520 if (koneplus == NULL)
521 return 0;
522
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000523 koneplus_keep_values_up_to_date(koneplus, data);
524
525 if (koneplus->roccat_claimed)
526 koneplus_report_to_chrdev(koneplus, data);
527
528 return 0;
529}
530
531static const struct hid_device_id koneplus_devices[] = {
532 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100533 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000534 { }
535};
536
537MODULE_DEVICE_TABLE(hid, koneplus_devices);
538
539static struct hid_driver koneplus_driver = {
540 .name = "koneplus",
541 .id_table = koneplus_devices,
542 .probe = koneplus_probe,
543 .remove = koneplus_remove,
544 .raw_event = koneplus_raw_event
545};
546
547static int __init koneplus_init(void)
548{
549 int retval;
550
551 /* class name has to be same as driver name */
552 koneplus_class = class_create(THIS_MODULE, "koneplus");
553 if (IS_ERR(koneplus_class))
554 return PTR_ERR(koneplus_class);
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700555 koneplus_class->dev_groups = koneplus_groups;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000556
557 retval = hid_register_driver(&koneplus_driver);
558 if (retval)
559 class_destroy(koneplus_class);
560 return retval;
561}
562
563static void __exit koneplus_exit(void)
564{
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000565 hid_unregister_driver(&koneplus_driver);
Stefan Achatz74b643da2011-01-30 13:38:27 +0100566 class_destroy(koneplus_class);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000567}
568
569module_init(koneplus_init);
570module_exit(koneplus_exit);
571
572MODULE_AUTHOR("Stefan Achatz");
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100573MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000574MODULE_LICENSE("GPL v2");