blob: 9fe445082308511c5e1e2a6d9e76a46bf57ce02e [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
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000059
60/* retval is 0-4 on success, < 0 on error */
Stefan Achatzb50f3152011-04-13 17:17:52 +020061static int koneplus_get_actual_profile(struct usb_device *usb_dev)
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000062{
Stefan Achatzb50f3152011-04-13 17:17:52 +020063 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000064 int retval;
65
Stefan Achatz7392d732012-05-20 22:45:04 +020066 retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzf114fec2012-11-04 09:39:09 +010067 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000068
Stefan Achatzb50f3152011-04-13 17:17:52 +020069 return retval ? retval : buf.actual_profile;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000070}
71
Stefan Achatzb50f3152011-04-13 17:17:52 +020072static int koneplus_set_actual_profile(struct usb_device *usb_dev,
73 int new_profile)
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000074{
Stefan Achatzb50f3152011-04-13 17:17:52 +020075 struct koneplus_actual_profile buf;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000076
Stefan Achatzb50f3152011-04-13 17:17:52 +020077 buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
Stefan Achatzf114fec2012-11-04 09:39:09 +010078 buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
Stefan Achatzb50f3152011-04-13 17:17:52 +020079 buf.actual_profile = new_profile;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000080
Stefan Achatz7392d732012-05-20 22:45:04 +020081 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020082 KONEPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatzf114fec2012-11-04 09:39:09 +010083 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000084}
85
86static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
87 char *buf, loff_t off, size_t count,
88 size_t real_size, uint command)
89{
90 struct device *dev =
91 container_of(kobj, struct device, kobj)->parent->parent;
92 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
93 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
94 int retval;
95
Stefan Achatzfd82be62011-01-06 09:00:41 +010096 if (off >= real_size)
97 return 0;
98
Stefan Achatz47dbdbff2010-11-26 19:57:42 +000099 if (off != 0 || count != real_size)
100 return -EINVAL;
101
102 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200103 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000104 mutex_unlock(&koneplus->koneplus_lock);
105
106 if (retval)
107 return retval;
108
109 return real_size;
110}
111
112static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
113 void const *buf, loff_t off, size_t count,
114 size_t real_size, uint command)
115{
116 struct device *dev =
117 container_of(kobj, struct device, kobj)->parent->parent;
118 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
119 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
120 int retval;
121
122 if (off != 0 || count != real_size)
123 return -EINVAL;
124
125 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200126 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200127 buf, real_size);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000128 mutex_unlock(&koneplus->koneplus_lock);
129
130 if (retval)
131 return retval;
132
133 return real_size;
134}
135
Stefan Achatzf114fec2012-11-04 09:39:09 +0100136#define KONEPLUS_SYSFS_W(thingy, THINGY) \
137static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
138 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
139 loff_t off, size_t count) \
140{ \
141 return koneplus_sysfs_write(fp, kobj, buf, off, count, \
142 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
Stefan Achatzfabe51e2012-11-04 09:39:04 +0100143}
144
Stefan Achatzf114fec2012-11-04 09:39:09 +0100145#define KONEPLUS_SYSFS_R(thingy, THINGY) \
146static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
147 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
148 loff_t off, size_t count) \
149{ \
150 return koneplus_sysfs_read(fp, kobj, buf, off, count, \
151 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
Stefan Achatzfabe51e2012-11-04 09:39:04 +0100152}
153
Stefan Achatzf114fec2012-11-04 09:39:09 +0100154#define KONEPLUS_SYSFS_RW(thingy, THINGY) \
155KONEPLUS_SYSFS_W(thingy, THINGY) \
156KONEPLUS_SYSFS_R(thingy, THINGY)
157
158#define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
159{ \
160 .attr = { .name = #thingy, .mode = 0660 }, \
161 .size = KONEPLUS_SIZE_ ## THINGY, \
162 .read = koneplus_sysfs_read_ ## thingy, \
163 .write = koneplus_sysfs_write_ ## thingy \
Stefan Achatz6d1dec82011-05-29 19:32:57 +0200164}
165
Stefan Achatzf114fec2012-11-04 09:39:09 +0100166#define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
167{ \
168 .attr = { .name = #thingy, .mode = 0440 }, \
169 .size = KONEPLUS_SIZE_ ## THINGY, \
170 .read = koneplus_sysfs_read_ ## thingy, \
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000171}
172
Stefan Achatzf114fec2012-11-04 09:39:09 +0100173#define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
174{ \
175 .attr = { .name = #thingy, .mode = 0220 }, \
176 .size = KONEPLUS_SIZE_ ## THINGY, \
177 .write = koneplus_sysfs_write_ ## thingy \
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000178}
179
Stefan Achatzf114fec2012-11-04 09:39:09 +0100180KONEPLUS_SYSFS_RW(info, INFO)
181KONEPLUS_SYSFS_W(talk, TALK)
182KONEPLUS_SYSFS_W(macro, MACRO)
183KONEPLUS_SYSFS_RW(sensor, SENSOR)
184KONEPLUS_SYSFS_RW(tcu, TCU)
185KONEPLUS_SYSFS_R(tcu_image, TCU_IMAGE)
186KONEPLUS_SYSFS_W(profile_settings, PROFILE_SETTINGS)
187KONEPLUS_SYSFS_W(profile_buttons, PROFILE_BUTTONS)
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000188
189static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
190 struct kobject *kobj, struct bin_attribute *attr, char *buf,
191 loff_t off, size_t count)
192{
193 struct device *dev =
194 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000195 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatzf114fec2012-11-04 09:39:09 +0100196 ssize_t retval;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000197
Stefan Achatzf114fec2012-11-04 09:39:09 +0100198 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
199 KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000200 if (retval)
201 return retval;
202
Stefan Achatzf114fec2012-11-04 09:39:09 +0100203 return koneplus_sysfs_read(fp, kobj, buf, off, count,
204 KONEPLUS_SIZE_PROFILE_SETTINGS,
205 KONEPLUS_COMMAND_PROFILE_SETTINGS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000206}
207
208static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
209 struct kobject *kobj, struct bin_attribute *attr, char *buf,
210 loff_t off, size_t count)
211{
212 struct device *dev =
213 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000214 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatzf114fec2012-11-04 09:39:09 +0100215 ssize_t retval;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000216
Stefan Achatzf114fec2012-11-04 09:39:09 +0100217 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
218 KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000219 if (retval)
220 return retval;
221
Stefan Achatzf114fec2012-11-04 09:39:09 +0100222 return koneplus_sysfs_read(fp, kobj, buf, off, count,
223 KONEPLUS_SIZE_PROFILE_BUTTONS,
224 KONEPLUS_COMMAND_PROFILE_BUTTONS);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000225}
226
Stefan Achatzb50f3152011-04-13 17:17:52 +0200227static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000228 struct device_attribute *attr, char *buf)
229{
230 struct koneplus_device *koneplus =
231 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzb50f3152011-04-13 17:17:52 +0200232 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000233}
234
Stefan Achatzb50f3152011-04-13 17:17:52 +0200235static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000236 struct device_attribute *attr, char const *buf, size_t size)
237{
238 struct koneplus_device *koneplus;
239 struct usb_device *usb_dev;
240 unsigned long profile;
241 int retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200242 struct koneplus_roccat_report roccat_report;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000243
244 dev = dev->parent->parent;
245 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
246 usb_dev = interface_to_usbdev(to_usb_interface(dev));
247
248 retval = strict_strtoul(buf, 10, &profile);
249 if (retval)
250 return retval;
251
Stefan Achatz901e64d2011-06-12 10:02:44 +0200252 if (profile > 4)
253 return -EINVAL;
254
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000255 mutex_lock(&koneplus->koneplus_lock);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200256
257 retval = koneplus_set_actual_profile(usb_dev, profile);
258 if (retval) {
259 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000260 return retval;
Stefan Achatzb50f3152011-04-13 17:17:52 +0200261 }
262
Stefan Achatz901e64d2011-06-12 10:02:44 +0200263 koneplus_profile_activated(koneplus, profile);
Stefan Achatzb50f3152011-04-13 17:17:52 +0200264
265 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
266 roccat_report.data1 = profile + 1;
267 roccat_report.data2 = 0;
268 roccat_report.profile = profile + 1;
269 roccat_report_event(koneplus->chrdev_minor,
270 (uint8_t const *)&roccat_report);
271
272 mutex_unlock(&koneplus->koneplus_lock);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000273
274 return size;
275}
276
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000277static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
278 struct device_attribute *attr, char *buf)
279{
Stefan Achatzf114fec2012-11-04 09:39:09 +0100280 struct koneplus_device *koneplus;
281 struct usb_device *usb_dev;
282 struct koneplus_info info;
283
284 dev = dev->parent->parent;
285 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
286 usb_dev = interface_to_usbdev(to_usb_interface(dev));
287
288 mutex_lock(&koneplus->koneplus_lock);
289 roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
290 &info, KONEPLUS_SIZE_INFO);
291 mutex_unlock(&koneplus->koneplus_lock);
292
293 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000294}
295
296static struct device_attribute koneplus_attributes[] = {
Stefan Achatzb50f3152011-04-13 17:17:52 +0200297 __ATTR(actual_profile, 0660,
298 koneplus_sysfs_show_actual_profile,
299 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000300 __ATTR(startup_profile, 0660,
Stefan Achatzb50f3152011-04-13 17:17:52 +0200301 koneplus_sysfs_show_actual_profile,
302 koneplus_sysfs_set_actual_profile),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000303 __ATTR(firmware_version, 0440,
304 koneplus_sysfs_show_firmware_version, NULL),
305 __ATTR_NULL
306};
307
308static struct bin_attribute koneplus_bin_attributes[] = {
Stefan Achatzf114fec2012-11-04 09:39:09 +0100309 KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO),
310 KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK),
311 KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO),
312 KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR),
313 KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU),
314 KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE),
315 KONEPLUS_BIN_ATTRIBUTE_W(profile_settings, PROFILE_SETTINGS),
316 KONEPLUS_BIN_ATTRIBUTE_W(profile_buttons, PROFILE_BUTTONS),
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000317 {
318 .attr = { .name = "profile1_settings", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100319 .size = KONEPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000320 .read = koneplus_sysfs_read_profilex_settings,
321 .private = &profile_numbers[0]
322 },
323 {
324 .attr = { .name = "profile2_settings", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100325 .size = KONEPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000326 .read = koneplus_sysfs_read_profilex_settings,
327 .private = &profile_numbers[1]
328 },
329 {
330 .attr = { .name = "profile3_settings", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100331 .size = KONEPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000332 .read = koneplus_sysfs_read_profilex_settings,
333 .private = &profile_numbers[2]
334 },
335 {
336 .attr = { .name = "profile4_settings", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100337 .size = KONEPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000338 .read = koneplus_sysfs_read_profilex_settings,
339 .private = &profile_numbers[3]
340 },
341 {
342 .attr = { .name = "profile5_settings", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100343 .size = KONEPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000344 .read = koneplus_sysfs_read_profilex_settings,
345 .private = &profile_numbers[4]
346 },
347 {
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000348 .attr = { .name = "profile1_buttons", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100349 .size = KONEPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000350 .read = koneplus_sysfs_read_profilex_buttons,
351 .private = &profile_numbers[0]
352 },
353 {
354 .attr = { .name = "profile2_buttons", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100355 .size = KONEPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000356 .read = koneplus_sysfs_read_profilex_buttons,
357 .private = &profile_numbers[1]
358 },
359 {
360 .attr = { .name = "profile3_buttons", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100361 .size = KONEPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000362 .read = koneplus_sysfs_read_profilex_buttons,
363 .private = &profile_numbers[2]
364 },
365 {
366 .attr = { .name = "profile4_buttons", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100367 .size = KONEPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000368 .read = koneplus_sysfs_read_profilex_buttons,
369 .private = &profile_numbers[3]
370 },
371 {
372 .attr = { .name = "profile5_buttons", .mode = 0440 },
Stefan Achatzf114fec2012-11-04 09:39:09 +0100373 .size = KONEPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000374 .read = koneplus_sysfs_read_profilex_buttons,
375 .private = &profile_numbers[4]
376 },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000377 __ATTR_NULL
378};
379
380static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
381 struct koneplus_device *koneplus)
382{
Stefan Achatzf114fec2012-11-04 09:39:09 +0100383 int retval;
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000384
385 mutex_init(&koneplus->koneplus_lock);
386
Stefan Achatzb50f3152011-04-13 17:17:52 +0200387 retval = koneplus_get_actual_profile(usb_dev);
388 if (retval < 0)
389 return retval;
390 koneplus_profile_activated(koneplus, retval);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000391
392 return 0;
393}
394
395static int koneplus_init_specials(struct hid_device *hdev)
396{
397 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
398 struct usb_device *usb_dev = interface_to_usbdev(intf);
399 struct koneplus_device *koneplus;
400 int retval;
401
402 if (intf->cur_altsetting->desc.bInterfaceProtocol
403 == USB_INTERFACE_PROTOCOL_MOUSE) {
404
405 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
406 if (!koneplus) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100407 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000408 return -ENOMEM;
409 }
410 hid_set_drvdata(hdev, koneplus);
411
412 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
413 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100414 hid_err(hdev, "couldn't init struct koneplus_device\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000415 goto exit_free;
416 }
417
Stefan Achatz8211e462011-01-30 13:38:25 +0100418 retval = roccat_connect(koneplus_class, hdev,
419 sizeof(struct koneplus_roccat_report));
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000420 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100421 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000422 } else {
423 koneplus->chrdev_minor = retval;
424 koneplus->roccat_claimed = 1;
425 }
426 } else {
427 hid_set_drvdata(hdev, NULL);
428 }
429
430 return 0;
431exit_free:
432 kfree(koneplus);
433 return retval;
434}
435
436static void koneplus_remove_specials(struct hid_device *hdev)
437{
438 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
439 struct koneplus_device *koneplus;
440
441 if (intf->cur_altsetting->desc.bInterfaceProtocol
442 == USB_INTERFACE_PROTOCOL_MOUSE) {
443 koneplus = hid_get_drvdata(hdev);
444 if (koneplus->roccat_claimed)
445 roccat_disconnect(koneplus->chrdev_minor);
446 kfree(koneplus);
447 }
448}
449
450static int koneplus_probe(struct hid_device *hdev,
451 const struct hid_device_id *id)
452{
453 int retval;
454
455 retval = hid_parse(hdev);
456 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100457 hid_err(hdev, "parse failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000458 goto exit;
459 }
460
461 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
462 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100463 hid_err(hdev, "hw start failed\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000464 goto exit;
465 }
466
467 retval = koneplus_init_specials(hdev);
468 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100469 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000470 goto exit_stop;
471 }
472
473 return 0;
474
475exit_stop:
476 hid_hw_stop(hdev);
477exit:
478 return retval;
479}
480
481static void koneplus_remove(struct hid_device *hdev)
482{
483 koneplus_remove_specials(hdev);
484 hid_hw_stop(hdev);
485}
486
487static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
488 u8 const *data)
489{
490 struct koneplus_mouse_report_button const *button_report;
491
492 switch (data[0]) {
493 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
494 button_report = (struct koneplus_mouse_report_button const *)data;
495 switch (button_report->type) {
496 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
497 koneplus_profile_activated(koneplus, button_report->data1 - 1);
498 break;
499 }
500 break;
501 }
502}
503
504static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
505 u8 const *data)
506{
507 struct koneplus_roccat_report roccat_report;
508 struct koneplus_mouse_report_button const *button_report;
509
510 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
511 return;
512
513 button_report = (struct koneplus_mouse_report_button const *)data;
514
515 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
516 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
517 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
518 return;
519
520 roccat_report.type = button_report->type;
521 roccat_report.data1 = button_report->data1;
522 roccat_report.data2 = button_report->data2;
523 roccat_report.profile = koneplus->actual_profile + 1;
524 roccat_report_event(koneplus->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100525 (uint8_t const *)&roccat_report);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000526}
527
528static int koneplus_raw_event(struct hid_device *hdev,
529 struct hid_report *report, u8 *data, int size)
530{
531 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
532 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
533
534 if (intf->cur_altsetting->desc.bInterfaceProtocol
535 != USB_INTERFACE_PROTOCOL_MOUSE)
536 return 0;
537
Stefan Achatz901e64d2011-06-12 10:02:44 +0200538 if (koneplus == NULL)
539 return 0;
540
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000541 koneplus_keep_values_up_to_date(koneplus, data);
542
543 if (koneplus->roccat_claimed)
544 koneplus_report_to_chrdev(koneplus, data);
545
546 return 0;
547}
548
549static const struct hid_device_id koneplus_devices[] = {
550 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100551 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000552 { }
553};
554
555MODULE_DEVICE_TABLE(hid, koneplus_devices);
556
557static struct hid_driver koneplus_driver = {
558 .name = "koneplus",
559 .id_table = koneplus_devices,
560 .probe = koneplus_probe,
561 .remove = koneplus_remove,
562 .raw_event = koneplus_raw_event
563};
564
565static int __init koneplus_init(void)
566{
567 int retval;
568
569 /* class name has to be same as driver name */
570 koneplus_class = class_create(THIS_MODULE, "koneplus");
571 if (IS_ERR(koneplus_class))
572 return PTR_ERR(koneplus_class);
573 koneplus_class->dev_attrs = koneplus_attributes;
574 koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
575
576 retval = hid_register_driver(&koneplus_driver);
577 if (retval)
578 class_destroy(koneplus_class);
579 return retval;
580}
581
582static void __exit koneplus_exit(void)
583{
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000584 hid_unregister_driver(&koneplus_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100585 class_destroy(koneplus_class);
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000586}
587
588module_init(koneplus_init);
589module_exit(koneplus_exit);
590
591MODULE_AUTHOR("Stefan Achatz");
Stefan Achatz8e74a2d2012-11-04 09:38:52 +0100592MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
Stefan Achatz47dbdbff2010-11-26 19:57:42 +0000593MODULE_LICENSE("GPL v2");