blob: 1589fd3bcf0f38f5fe598608dc0a4eb0cd9bb32f [file] [log] [blame]
Stefan Achatz0e70f972011-01-30 13:38:24 +01001/*
2 * Roccat Kova[+] driver for Linux
3 *
4 * Copyright (c) 2011 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 Kova[+] is a bigger version of the Pyra with two more side buttons.
16 */
17
18#include <linux/device.h>
19#include <linux/input.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010023#include <linux/hid-roccat.h>
Stefan Achatz0e70f972011-01-30 13:38:24 +010024#include "hid-ids.h"
Stefan Achatz0e70f972011-01-30 13:38:24 +010025#include "hid-roccat-common.h"
26#include "hid-roccat-kovaplus.h"
27
28static uint profile_numbers[5] = {0, 1, 2, 3, 4};
29
30static struct class *kovaplus_class;
31
32static uint kovaplus_convert_event_cpi(uint value)
33{
34 return (value == 7 ? 4 : (value == 4 ? 3 : value));
35}
36
37static void kovaplus_profile_activated(struct kovaplus_device *kovaplus,
38 uint new_profile_index)
39{
40 kovaplus->actual_profile = new_profile_index;
41 kovaplus->actual_cpi = kovaplus->profile_settings[new_profile_index].cpi_startup_level;
42 kovaplus->actual_x_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_x;
43 kovaplus->actual_y_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_y;
44}
45
46static int kovaplus_send_control(struct usb_device *usb_dev, uint value,
47 enum kovaplus_control_requests request)
48{
49 int retval;
Stefan Achatz7392d732012-05-20 22:45:04 +020050 struct roccat_common2_control control;
Stefan Achatz0e70f972011-01-30 13:38:24 +010051
52 if ((request == KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
53 request == KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
54 value > 4)
55 return -EINVAL;
56
Stefan Achatz4728f2d2012-05-20 22:44:59 +020057 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
Stefan Achatz0e70f972011-01-30 13:38:24 +010058 control.value = value;
59 control.request = request;
60
Stefan Achatz7392d732012-05-20 22:45:04 +020061 retval = roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
62 &control, sizeof(struct roccat_common2_control));
Stefan Achatz0e70f972011-01-30 13:38:24 +010063
64 return retval;
65}
66
Stefan Achatz0e70f972011-01-30 13:38:24 +010067static int kovaplus_select_profile(struct usb_device *usb_dev, uint number,
68 enum kovaplus_control_requests request)
69{
70 return kovaplus_send_control(usb_dev, number, request);
71}
72
Stefan Achatz0e70f972011-01-30 13:38:24 +010073static int kovaplus_get_profile_settings(struct usb_device *usb_dev,
74 struct kovaplus_profile_settings *buf, uint number)
75{
76 int retval;
77
78 retval = kovaplus_select_profile(usb_dev, number,
79 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
80 if (retval)
81 return retval;
82
Stefan Achatz7392d732012-05-20 22:45:04 +020083 return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_SETTINGS,
Stefan Achatz94a8fcf2012-11-11 06:20:52 +010084 buf, KOVAPLUS_SIZE_PROFILE_SETTINGS);
Stefan Achatz0e70f972011-01-30 13:38:24 +010085}
86
87static int kovaplus_get_profile_buttons(struct usb_device *usb_dev,
88 struct kovaplus_profile_buttons *buf, int number)
89{
90 int retval;
91
92 retval = kovaplus_select_profile(usb_dev, number,
93 KOVAPLUS_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, KOVAPLUS_COMMAND_PROFILE_BUTTONS,
Stefan Achatz94a8fcf2012-11-11 06:20:52 +010098 buf, KOVAPLUS_SIZE_PROFILE_BUTTONS);
Stefan Achatz0e70f972011-01-30 13:38:24 +010099}
100
101/* retval is 0-4 on success, < 0 on error */
102static int kovaplus_get_actual_profile(struct usb_device *usb_dev)
103{
104 struct kovaplus_actual_profile buf;
105 int retval;
106
Stefan Achatz7392d732012-05-20 22:45:04 +0200107 retval = roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100108 &buf, sizeof(struct kovaplus_actual_profile));
109
110 return retval ? retval : buf.actual_profile;
111}
112
113static int kovaplus_set_actual_profile(struct usb_device *usb_dev,
114 int new_profile)
115{
116 struct kovaplus_actual_profile buf;
117
118 buf.command = KOVAPLUS_COMMAND_ACTUAL_PROFILE;
119 buf.size = sizeof(struct kovaplus_actual_profile);
120 buf.actual_profile = new_profile;
121
Stefan Achatz7392d732012-05-20 22:45:04 +0200122 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200123 KOVAPLUS_COMMAND_ACTUAL_PROFILE,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100124 &buf, sizeof(struct kovaplus_actual_profile));
125}
126
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100127static ssize_t kovaplus_sysfs_read(struct file *fp, struct kobject *kobj,
128 char *buf, loff_t off, size_t count,
129 size_t real_size, uint command)
130{
131 struct device *dev =
132 container_of(kobj, struct device, kobj)->parent->parent;
133 struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
134 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
135 int retval;
136
137 if (off >= real_size)
138 return 0;
139
140 if (off != 0 || count != real_size)
141 return -EINVAL;
142
143 mutex_lock(&kovaplus->kovaplus_lock);
144 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
145 mutex_unlock(&kovaplus->kovaplus_lock);
146
147 if (retval)
148 return retval;
149
150 return real_size;
151}
152
153static ssize_t kovaplus_sysfs_write(struct file *fp, struct kobject *kobj,
154 void const *buf, loff_t off, size_t count,
155 size_t real_size, uint command)
156{
157 struct device *dev =
158 container_of(kobj, struct device, kobj)->parent->parent;
159 struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
160 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
161 int retval;
162
163 if (off != 0 || count != real_size)
164 return -EINVAL;
165
166 mutex_lock(&kovaplus->kovaplus_lock);
167 retval = roccat_common2_send_with_status(usb_dev, command,
168 buf, real_size);
169 mutex_unlock(&kovaplus->kovaplus_lock);
170
171 if (retval)
172 return retval;
173
174 return real_size;
175}
176
177#define KOVAPLUS_SYSFS_W(thingy, THINGY) \
178static ssize_t kovaplus_sysfs_write_ ## thingy(struct file *fp, \
179 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
180 loff_t off, size_t count) \
181{ \
182 return kovaplus_sysfs_write(fp, kobj, buf, off, count, \
183 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
184}
185
186#define KOVAPLUS_SYSFS_R(thingy, THINGY) \
187static ssize_t kovaplus_sysfs_read_ ## thingy(struct file *fp, \
188 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
189 loff_t off, size_t count) \
190{ \
191 return kovaplus_sysfs_read(fp, kobj, buf, off, count, \
192 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
193}
194
195#define KOVAPLUS_SYSFS_RW(thingy, THINGY) \
196KOVAPLUS_SYSFS_W(thingy, THINGY) \
197KOVAPLUS_SYSFS_R(thingy, THINGY)
198
199#define KOVAPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
200{ \
201 .attr = { .name = #thingy, .mode = 0660 }, \
202 .size = KOVAPLUS_SIZE_ ## THINGY, \
203 .read = kovaplus_sysfs_read_ ## thingy, \
204 .write = kovaplus_sysfs_write_ ## thingy \
205}
206
207#define KOVAPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
208{ \
209 .attr = { .name = #thingy, .mode = 0440 }, \
210 .size = KOVAPLUS_SIZE_ ## THINGY, \
211 .read = kovaplus_sysfs_read_ ## thingy, \
212}
213
214#define KOVAPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
215{ \
216 .attr = { .name = #thingy, .mode = 0220 }, \
217 .size = KOVAPLUS_SIZE_ ## THINGY, \
218 .write = kovaplus_sysfs_write_ ## thingy \
219}
220
221KOVAPLUS_SYSFS_RW(info, INFO)
222KOVAPLUS_SYSFS_W(profile_settings, PROFILE_SETTINGS)
223KOVAPLUS_SYSFS_W(profile_buttons, PROFILE_BUTTONS)
224
Stefan Achatz0e70f972011-01-30 13:38:24 +0100225static ssize_t kovaplus_sysfs_read_profilex_settings(struct file *fp,
226 struct kobject *kobj, struct bin_attribute *attr, char *buf,
227 loff_t off, size_t count)
228{
229 struct device *dev =
230 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz0e70f972011-01-30 13:38:24 +0100231 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100232 ssize_t retval;
Stefan Achatz0e70f972011-01-30 13:38:24 +0100233
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100234 retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private),
235 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100236 if (retval)
237 return retval;
238
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100239 return kovaplus_sysfs_read(fp, kobj, buf, off, count,
240 KOVAPLUS_SIZE_PROFILE_SETTINGS,
241 KOVAPLUS_COMMAND_PROFILE_SETTINGS);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100242}
243
244static ssize_t kovaplus_sysfs_read_profilex_buttons(struct file *fp,
245 struct kobject *kobj, struct bin_attribute *attr, char *buf,
246 loff_t off, size_t count)
247{
248 struct device *dev =
249 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatz0e70f972011-01-30 13:38:24 +0100250 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100251 ssize_t retval;
Stefan Achatz0e70f972011-01-30 13:38:24 +0100252
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100253 retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private),
254 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100255 if (retval)
256 return retval;
257
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100258 return kovaplus_sysfs_read(fp, kobj, buf, off, count,
259 KOVAPLUS_SIZE_PROFILE_BUTTONS,
260 KOVAPLUS_COMMAND_PROFILE_BUTTONS);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100261}
262
263static ssize_t kovaplus_sysfs_show_actual_profile(struct device *dev,
264 struct device_attribute *attr, char *buf)
265{
266 struct kovaplus_device *kovaplus =
267 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
268 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_profile);
269}
270
271static ssize_t kovaplus_sysfs_set_actual_profile(struct device *dev,
272 struct device_attribute *attr, char const *buf, size_t size)
273{
274 struct kovaplus_device *kovaplus;
275 struct usb_device *usb_dev;
276 unsigned long profile;
277 int retval;
Stefan Achatz6b9a57b2011-08-27 15:24:45 +0200278 struct kovaplus_roccat_report roccat_report;
Stefan Achatz0e70f972011-01-30 13:38:24 +0100279
280 dev = dev->parent->parent;
281 kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
282 usb_dev = interface_to_usbdev(to_usb_interface(dev));
283
284 retval = strict_strtoul(buf, 10, &profile);
285 if (retval)
286 return retval;
287
288 if (profile >= 5)
289 return -EINVAL;
290
291 mutex_lock(&kovaplus->kovaplus_lock);
292 retval = kovaplus_set_actual_profile(usb_dev, profile);
Stefan Achatz6b9a57b2011-08-27 15:24:45 +0200293 if (retval) {
294 mutex_unlock(&kovaplus->kovaplus_lock);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100295 return retval;
Stefan Achatz6b9a57b2011-08-27 15:24:45 +0200296 }
297
298 kovaplus_profile_activated(kovaplus, profile);
299
300 roccat_report.type = KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1;
301 roccat_report.profile = profile + 1;
302 roccat_report.button = 0;
303 roccat_report.data1 = profile + 1;
304 roccat_report.data2 = 0;
305 roccat_report_event(kovaplus->chrdev_minor,
306 (uint8_t const *)&roccat_report);
307
308 mutex_unlock(&kovaplus->kovaplus_lock);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100309
310 return size;
311}
312
313static ssize_t kovaplus_sysfs_show_actual_cpi(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
316 struct kovaplus_device *kovaplus =
317 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
318 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_cpi);
319}
320
321static ssize_t kovaplus_sysfs_show_actual_sensitivity_x(struct device *dev,
322 struct device_attribute *attr, char *buf)
323{
324 struct kovaplus_device *kovaplus =
325 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
326 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_x_sensitivity);
327}
328
329static ssize_t kovaplus_sysfs_show_actual_sensitivity_y(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
332 struct kovaplus_device *kovaplus =
333 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
334 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_y_sensitivity);
335}
336
337static ssize_t kovaplus_sysfs_show_firmware_version(struct device *dev,
338 struct device_attribute *attr, char *buf)
339{
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100340 struct kovaplus_device *kovaplus;
341 struct usb_device *usb_dev;
342 struct kovaplus_info info;
343
344 dev = dev->parent->parent;
345 kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
346 usb_dev = interface_to_usbdev(to_usb_interface(dev));
347
348 mutex_lock(&kovaplus->kovaplus_lock);
349 roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_INFO,
350 &info, KOVAPLUS_SIZE_INFO);
351 mutex_unlock(&kovaplus->kovaplus_lock);
352
353 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100354}
355
356static struct device_attribute kovaplus_attributes[] = {
357 __ATTR(actual_cpi, 0440,
358 kovaplus_sysfs_show_actual_cpi, NULL),
359 __ATTR(firmware_version, 0440,
360 kovaplus_sysfs_show_firmware_version, NULL),
361 __ATTR(actual_profile, 0660,
362 kovaplus_sysfs_show_actual_profile,
363 kovaplus_sysfs_set_actual_profile),
364 __ATTR(actual_sensitivity_x, 0440,
365 kovaplus_sysfs_show_actual_sensitivity_x, NULL),
366 __ATTR(actual_sensitivity_y, 0440,
367 kovaplus_sysfs_show_actual_sensitivity_y, NULL),
368 __ATTR_NULL
369};
370
371static struct bin_attribute kovaplus_bin_attributes[] = {
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100372 KOVAPLUS_BIN_ATTRIBUTE_RW(info, INFO),
373 KOVAPLUS_BIN_ATTRIBUTE_W(profile_settings, PROFILE_SETTINGS),
374 KOVAPLUS_BIN_ATTRIBUTE_W(profile_buttons, PROFILE_BUTTONS),
Stefan Achatz0e70f972011-01-30 13:38:24 +0100375 {
376 .attr = { .name = "profile1_settings", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100377 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100378 .read = kovaplus_sysfs_read_profilex_settings,
379 .private = &profile_numbers[0]
380 },
381 {
382 .attr = { .name = "profile2_settings", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100383 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100384 .read = kovaplus_sysfs_read_profilex_settings,
385 .private = &profile_numbers[1]
386 },
387 {
388 .attr = { .name = "profile3_settings", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100389 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100390 .read = kovaplus_sysfs_read_profilex_settings,
391 .private = &profile_numbers[2]
392 },
393 {
394 .attr = { .name = "profile4_settings", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100395 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100396 .read = kovaplus_sysfs_read_profilex_settings,
397 .private = &profile_numbers[3]
398 },
399 {
400 .attr = { .name = "profile5_settings", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100401 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100402 .read = kovaplus_sysfs_read_profilex_settings,
403 .private = &profile_numbers[4]
404 },
405 {
Stefan Achatz0e70f972011-01-30 13:38:24 +0100406 .attr = { .name = "profile1_buttons", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100407 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100408 .read = kovaplus_sysfs_read_profilex_buttons,
409 .private = &profile_numbers[0]
410 },
411 {
412 .attr = { .name = "profile2_buttons", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100413 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100414 .read = kovaplus_sysfs_read_profilex_buttons,
415 .private = &profile_numbers[1]
416 },
417 {
418 .attr = { .name = "profile3_buttons", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100419 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100420 .read = kovaplus_sysfs_read_profilex_buttons,
421 .private = &profile_numbers[2]
422 },
423 {
424 .attr = { .name = "profile4_buttons", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100425 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100426 .read = kovaplus_sysfs_read_profilex_buttons,
427 .private = &profile_numbers[3]
428 },
429 {
430 .attr = { .name = "profile5_buttons", .mode = 0440 },
Stefan Achatz94a8fcf2012-11-11 06:20:52 +0100431 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS,
Stefan Achatz0e70f972011-01-30 13:38:24 +0100432 .read = kovaplus_sysfs_read_profilex_buttons,
433 .private = &profile_numbers[4]
434 },
435 __ATTR_NULL
436};
437
438static int kovaplus_init_kovaplus_device_struct(struct usb_device *usb_dev,
439 struct kovaplus_device *kovaplus)
440{
441 int retval, i;
442 static uint wait = 70; /* device will freeze with just 60 */
443
444 mutex_init(&kovaplus->kovaplus_lock);
445
Stefan Achatz0e70f972011-01-30 13:38:24 +0100446 for (i = 0; i < 5; ++i) {
447 msleep(wait);
448 retval = kovaplus_get_profile_settings(usb_dev,
449 &kovaplus->profile_settings[i], i);
450 if (retval)
451 return retval;
452
453 msleep(wait);
454 retval = kovaplus_get_profile_buttons(usb_dev,
455 &kovaplus->profile_buttons[i], i);
456 if (retval)
457 return retval;
458 }
459
460 msleep(wait);
461 retval = kovaplus_get_actual_profile(usb_dev);
462 if (retval < 0)
463 return retval;
464 kovaplus_profile_activated(kovaplus, retval);
465
466 return 0;
467}
468
469static int kovaplus_init_specials(struct hid_device *hdev)
470{
471 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
472 struct usb_device *usb_dev = interface_to_usbdev(intf);
473 struct kovaplus_device *kovaplus;
474 int retval;
475
476 if (intf->cur_altsetting->desc.bInterfaceProtocol
477 == USB_INTERFACE_PROTOCOL_MOUSE) {
478
479 kovaplus = kzalloc(sizeof(*kovaplus), GFP_KERNEL);
480 if (!kovaplus) {
481 hid_err(hdev, "can't alloc device descriptor\n");
482 return -ENOMEM;
483 }
484 hid_set_drvdata(hdev, kovaplus);
485
486 retval = kovaplus_init_kovaplus_device_struct(usb_dev, kovaplus);
487 if (retval) {
488 hid_err(hdev, "couldn't init struct kovaplus_device\n");
489 goto exit_free;
490 }
491
Stefan Achatz8211e462011-01-30 13:38:25 +0100492 retval = roccat_connect(kovaplus_class, hdev,
493 sizeof(struct kovaplus_roccat_report));
Stefan Achatz0e70f972011-01-30 13:38:24 +0100494 if (retval < 0) {
495 hid_err(hdev, "couldn't init char dev\n");
496 } else {
497 kovaplus->chrdev_minor = retval;
498 kovaplus->roccat_claimed = 1;
499 }
500
501 } else {
502 hid_set_drvdata(hdev, NULL);
503 }
504
505 return 0;
506exit_free:
507 kfree(kovaplus);
508 return retval;
509}
510
511static void kovaplus_remove_specials(struct hid_device *hdev)
512{
513 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
514 struct kovaplus_device *kovaplus;
515
516 if (intf->cur_altsetting->desc.bInterfaceProtocol
517 == USB_INTERFACE_PROTOCOL_MOUSE) {
518 kovaplus = hid_get_drvdata(hdev);
519 if (kovaplus->roccat_claimed)
520 roccat_disconnect(kovaplus->chrdev_minor);
521 kfree(kovaplus);
522 }
523}
524
525static int kovaplus_probe(struct hid_device *hdev,
526 const struct hid_device_id *id)
527{
528 int retval;
529
530 retval = hid_parse(hdev);
531 if (retval) {
532 hid_err(hdev, "parse failed\n");
533 goto exit;
534 }
535
536 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
537 if (retval) {
538 hid_err(hdev, "hw start failed\n");
539 goto exit;
540 }
541
542 retval = kovaplus_init_specials(hdev);
543 if (retval) {
544 hid_err(hdev, "couldn't install mouse\n");
545 goto exit_stop;
546 }
547
548 return 0;
549
550exit_stop:
551 hid_hw_stop(hdev);
552exit:
553 return retval;
554}
555
556static void kovaplus_remove(struct hid_device *hdev)
557{
558 kovaplus_remove_specials(hdev);
559 hid_hw_stop(hdev);
560}
561
562static void kovaplus_keep_values_up_to_date(struct kovaplus_device *kovaplus,
563 u8 const *data)
564{
565 struct kovaplus_mouse_report_button const *button_report;
566
567 if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
568 return;
569
570 button_report = (struct kovaplus_mouse_report_button const *)data;
571
572 switch (button_report->type) {
573 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1:
574 kovaplus_profile_activated(kovaplus, button_report->data1 - 1);
575 break;
576 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI:
577 kovaplus->actual_cpi = kovaplus_convert_event_cpi(button_report->data1);
578 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY:
579 kovaplus->actual_x_sensitivity = button_report->data1;
580 kovaplus->actual_y_sensitivity = button_report->data2;
581 }
582}
583
584static void kovaplus_report_to_chrdev(struct kovaplus_device const *kovaplus,
585 u8 const *data)
586{
587 struct kovaplus_roccat_report roccat_report;
588 struct kovaplus_mouse_report_button const *button_report;
589
590 if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
591 return;
592
593 button_report = (struct kovaplus_mouse_report_button const *)data;
594
595 if (button_report->type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2)
596 return;
597
598 roccat_report.type = button_report->type;
599 roccat_report.profile = kovaplus->actual_profile + 1;
600
601 if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO ||
602 roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT ||
603 roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
604 roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER)
605 roccat_report.button = button_report->data1;
606 else
607 roccat_report.button = 0;
608
609 if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI)
610 roccat_report.data1 = kovaplus_convert_event_cpi(button_report->data1);
611 else
612 roccat_report.data1 = button_report->data1;
613
614 roccat_report.data2 = button_report->data2;
615
616 roccat_report_event(kovaplus->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100617 (uint8_t const *)&roccat_report);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100618}
619
620static int kovaplus_raw_event(struct hid_device *hdev,
621 struct hid_report *report, u8 *data, int size)
622{
623 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
624 struct kovaplus_device *kovaplus = hid_get_drvdata(hdev);
625
626 if (intf->cur_altsetting->desc.bInterfaceProtocol
627 != USB_INTERFACE_PROTOCOL_MOUSE)
628 return 0;
629
Stefan Achatz901e64d2011-06-12 10:02:44 +0200630 if (kovaplus == NULL)
631 return 0;
632
Stefan Achatz0e70f972011-01-30 13:38:24 +0100633 kovaplus_keep_values_up_to_date(kovaplus, data);
634
635 if (kovaplus->roccat_claimed)
636 kovaplus_report_to_chrdev(kovaplus, data);
637
638 return 0;
639}
640
641static const struct hid_device_id kovaplus_devices[] = {
642 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) },
643 { }
644};
645
646MODULE_DEVICE_TABLE(hid, kovaplus_devices);
647
648static struct hid_driver kovaplus_driver = {
649 .name = "kovaplus",
650 .id_table = kovaplus_devices,
651 .probe = kovaplus_probe,
652 .remove = kovaplus_remove,
653 .raw_event = kovaplus_raw_event
654};
655
656static int __init kovaplus_init(void)
657{
658 int retval;
659
660 kovaplus_class = class_create(THIS_MODULE, "kovaplus");
661 if (IS_ERR(kovaplus_class))
662 return PTR_ERR(kovaplus_class);
663 kovaplus_class->dev_attrs = kovaplus_attributes;
664 kovaplus_class->dev_bin_attrs = kovaplus_bin_attributes;
665
666 retval = hid_register_driver(&kovaplus_driver);
667 if (retval)
668 class_destroy(kovaplus_class);
669 return retval;
670}
671
672static void __exit kovaplus_exit(void)
673{
Stefan Achatz0e70f972011-01-30 13:38:24 +0100674 hid_unregister_driver(&kovaplus_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100675 class_destroy(kovaplus_class);
Stefan Achatz0e70f972011-01-30 13:38:24 +0100676}
677
678module_init(kovaplus_init);
679module_exit(kovaplus_exit);
680
681MODULE_AUTHOR("Stefan Achatz");
682MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
683MODULE_LICENSE("GPL v2");