Commit | Line | Data |
---|---|---|
0e70f97f SA |
1 | /* |
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> | |
5dc0c983 | 23 | #include <linux/hid-roccat.h> |
0e70f97f | 24 | #include "hid-ids.h" |
0e70f97f SA |
25 | #include "hid-roccat-common.h" |
26 | #include "hid-roccat-kovaplus.h" | |
27 | ||
28 | static uint profile_numbers[5] = {0, 1, 2, 3, 4}; | |
29 | ||
30 | static struct class *kovaplus_class; | |
31 | ||
32 | static uint kovaplus_convert_event_cpi(uint value) | |
33 | { | |
34 | return (value == 7 ? 4 : (value == 4 ? 3 : value)); | |
35 | } | |
36 | ||
37 | static void kovaplus_profile_activated(struct kovaplus_device *kovaplus, | |
38 | uint new_profile_index) | |
39 | { | |
7ad81482 DC |
40 | if (new_profile_index >= ARRAY_SIZE(kovaplus->profile_settings)) |
41 | return; | |
0e70f97f SA |
42 | kovaplus->actual_profile = new_profile_index; |
43 | kovaplus->actual_cpi = kovaplus->profile_settings[new_profile_index].cpi_startup_level; | |
44 | kovaplus->actual_x_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_x; | |
45 | kovaplus->actual_y_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_y; | |
46 | } | |
47 | ||
48 | static int kovaplus_send_control(struct usb_device *usb_dev, uint value, | |
49 | enum kovaplus_control_requests request) | |
50 | { | |
51 | int retval; | |
7392d73b | 52 | struct roccat_common2_control control; |
0e70f97f SA |
53 | |
54 | if ((request == KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS || | |
55 | request == KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) && | |
56 | value > 4) | |
57 | return -EINVAL; | |
58 | ||
4728f2dc | 59 | control.command = ROCCAT_COMMON_COMMAND_CONTROL; |
0e70f97f SA |
60 | control.value = value; |
61 | control.request = request; | |
62 | ||
7392d73b SA |
63 | retval = roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL, |
64 | &control, sizeof(struct roccat_common2_control)); | |
0e70f97f SA |
65 | |
66 | return retval; | |
67 | } | |
68 | ||
0e70f97f SA |
69 | static int kovaplus_select_profile(struct usb_device *usb_dev, uint number, |
70 | enum kovaplus_control_requests request) | |
71 | { | |
72 | return kovaplus_send_control(usb_dev, number, request); | |
73 | } | |
74 | ||
0e70f97f SA |
75 | static int kovaplus_get_profile_settings(struct usb_device *usb_dev, |
76 | struct kovaplus_profile_settings *buf, uint number) | |
77 | { | |
78 | int retval; | |
79 | ||
80 | retval = kovaplus_select_profile(usb_dev, number, | |
81 | KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS); | |
82 | if (retval) | |
83 | return retval; | |
84 | ||
7392d73b | 85 | return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_SETTINGS, |
94a8fcf9 | 86 | buf, KOVAPLUS_SIZE_PROFILE_SETTINGS); |
0e70f97f SA |
87 | } |
88 | ||
89 | static int kovaplus_get_profile_buttons(struct usb_device *usb_dev, | |
90 | struct kovaplus_profile_buttons *buf, int number) | |
91 | { | |
92 | int retval; | |
93 | ||
94 | retval = kovaplus_select_profile(usb_dev, number, | |
95 | KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS); | |
96 | if (retval) | |
97 | return retval; | |
98 | ||
7392d73b | 99 | return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_BUTTONS, |
94a8fcf9 | 100 | buf, KOVAPLUS_SIZE_PROFILE_BUTTONS); |
0e70f97f SA |
101 | } |
102 | ||
103 | /* retval is 0-4 on success, < 0 on error */ | |
104 | static int kovaplus_get_actual_profile(struct usb_device *usb_dev) | |
105 | { | |
106 | struct kovaplus_actual_profile buf; | |
107 | int retval; | |
108 | ||
7392d73b | 109 | retval = roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_ACTUAL_PROFILE, |
0e70f97f SA |
110 | &buf, sizeof(struct kovaplus_actual_profile)); |
111 | ||
112 | return retval ? retval : buf.actual_profile; | |
113 | } | |
114 | ||
115 | static int kovaplus_set_actual_profile(struct usb_device *usb_dev, | |
116 | int new_profile) | |
117 | { | |
118 | struct kovaplus_actual_profile buf; | |
119 | ||
120 | buf.command = KOVAPLUS_COMMAND_ACTUAL_PROFILE; | |
121 | buf.size = sizeof(struct kovaplus_actual_profile); | |
122 | buf.actual_profile = new_profile; | |
123 | ||
7392d73b | 124 | return roccat_common2_send_with_status(usb_dev, |
4728f2dc | 125 | KOVAPLUS_COMMAND_ACTUAL_PROFILE, |
0e70f97f SA |
126 | &buf, sizeof(struct kovaplus_actual_profile)); |
127 | } | |
128 | ||
94a8fcf9 SA |
129 | static ssize_t kovaplus_sysfs_read(struct file *fp, struct kobject *kobj, |
130 | char *buf, loff_t off, size_t count, | |
131 | size_t real_size, uint command) | |
0e70f97f | 132 | { |
2cf83833 | 133 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
0e70f97f | 134 | struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); |
94a8fcf9 SA |
135 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
136 | int retval; | |
0e70f97f | 137 | |
94a8fcf9 | 138 | if (off >= real_size) |
0e70f97f SA |
139 | return 0; |
140 | ||
94a8fcf9 SA |
141 | if (off != 0 || count != real_size) |
142 | return -EINVAL; | |
0e70f97f SA |
143 | |
144 | mutex_lock(&kovaplus->kovaplus_lock); | |
94a8fcf9 | 145 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
0e70f97f SA |
146 | mutex_unlock(&kovaplus->kovaplus_lock); |
147 | ||
94a8fcf9 SA |
148 | if (retval) |
149 | return retval; | |
150 | ||
151 | return real_size; | |
0e70f97f SA |
152 | } |
153 | ||
94a8fcf9 SA |
154 | static ssize_t kovaplus_sysfs_write(struct file *fp, struct kobject *kobj, |
155 | void const *buf, loff_t off, size_t count, | |
156 | size_t real_size, uint command) | |
0e70f97f | 157 | { |
2cf83833 | 158 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
0e70f97f SA |
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)); | |
94a8fcf9 | 161 | int retval; |
0e70f97f | 162 | |
94a8fcf9 | 163 | if (off != 0 || count != real_size) |
0e70f97f SA |
164 | return -EINVAL; |
165 | ||
0e70f97f | 166 | mutex_lock(&kovaplus->kovaplus_lock); |
94a8fcf9 SA |
167 | retval = roccat_common2_send_with_status(usb_dev, command, |
168 | buf, real_size); | |
0e70f97f SA |
169 | mutex_unlock(&kovaplus->kovaplus_lock); |
170 | ||
171 | if (retval) | |
172 | return retval; | |
173 | ||
94a8fcf9 | 174 | return real_size; |
0e70f97f SA |
175 | } |
176 | ||
94a8fcf9 SA |
177 | #define KOVAPLUS_SYSFS_W(thingy, THINGY) \ |
178 | static 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 | } | |
0e70f97f | 185 | |
94a8fcf9 SA |
186 | #define KOVAPLUS_SYSFS_R(thingy, THINGY) \ |
187 | static 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 | } | |
0e70f97f | 194 | |
94a8fcf9 SA |
195 | #define KOVAPLUS_SYSFS_RW(thingy, THINGY) \ |
196 | KOVAPLUS_SYSFS_W(thingy, THINGY) \ | |
197 | KOVAPLUS_SYSFS_R(thingy, THINGY) | |
0e70f97f | 198 | |
94a8fcf9 | 199 | #define KOVAPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \ |
975b53cc GKH |
200 | KOVAPLUS_SYSFS_RW(thingy, THINGY); \ |
201 | static struct bin_attribute bin_attr_##thingy = { \ | |
94a8fcf9 SA |
202 | .attr = { .name = #thingy, .mode = 0660 }, \ |
203 | .size = KOVAPLUS_SIZE_ ## THINGY, \ | |
204 | .read = kovaplus_sysfs_read_ ## thingy, \ | |
205 | .write = kovaplus_sysfs_write_ ## thingy \ | |
206 | } | |
207 | ||
94a8fcf9 | 208 | #define KOVAPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \ |
975b53cc GKH |
209 | KOVAPLUS_SYSFS_W(thingy, THINGY); \ |
210 | static struct bin_attribute bin_attr_##thingy = { \ | |
94a8fcf9 SA |
211 | .attr = { .name = #thingy, .mode = 0220 }, \ |
212 | .size = KOVAPLUS_SIZE_ ## THINGY, \ | |
213 | .write = kovaplus_sysfs_write_ ## thingy \ | |
0e70f97f | 214 | } |
975b53cc GKH |
215 | KOVAPLUS_BIN_ATTRIBUTE_W(control, CONTROL); |
216 | KOVAPLUS_BIN_ATTRIBUTE_RW(info, INFO); | |
217 | KOVAPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS); | |
218 | KOVAPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS); | |
94a8fcf9 SA |
219 | |
220 | static ssize_t kovaplus_sysfs_read_profilex_settings(struct file *fp, | |
0e70f97f SA |
221 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
222 | loff_t off, size_t count) | |
223 | { | |
2cf83833 | 224 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
0e70f97f | 225 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
94a8fcf9 | 226 | ssize_t retval; |
0e70f97f | 227 | |
94a8fcf9 SA |
228 | retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private), |
229 | KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS); | |
230 | if (retval) | |
231 | return retval; | |
0e70f97f | 232 | |
94a8fcf9 SA |
233 | return kovaplus_sysfs_read(fp, kobj, buf, off, count, |
234 | KOVAPLUS_SIZE_PROFILE_SETTINGS, | |
235 | KOVAPLUS_COMMAND_PROFILE_SETTINGS); | |
236 | } | |
0e70f97f | 237 | |
94a8fcf9 SA |
238 | static ssize_t kovaplus_sysfs_read_profilex_buttons(struct file *fp, |
239 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
240 | loff_t off, size_t count) | |
241 | { | |
2cf83833 | 242 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
94a8fcf9 SA |
243 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
244 | ssize_t retval; | |
0e70f97f | 245 | |
94a8fcf9 SA |
246 | retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private), |
247 | KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS); | |
0e70f97f SA |
248 | if (retval) |
249 | return retval; | |
250 | ||
94a8fcf9 SA |
251 | return kovaplus_sysfs_read(fp, kobj, buf, off, count, |
252 | KOVAPLUS_SIZE_PROFILE_BUTTONS, | |
253 | KOVAPLUS_COMMAND_PROFILE_BUTTONS); | |
0e70f97f SA |
254 | } |
255 | ||
975b53cc GKH |
256 | #define PROFILE_ATTR(number) \ |
257 | static struct bin_attribute bin_attr_profile##number##_settings = { \ | |
550dbf47 | 258 | .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \ |
975b53cc GKH |
259 | .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, \ |
260 | .read = kovaplus_sysfs_read_profilex_settings, \ | |
261 | .private = &profile_numbers[number-1], \ | |
262 | }; \ | |
263 | static struct bin_attribute bin_attr_profile##number##_buttons = { \ | |
550dbf47 | 264 | .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \ |
975b53cc GKH |
265 | .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, \ |
266 | .read = kovaplus_sysfs_read_profilex_buttons, \ | |
267 | .private = &profile_numbers[number-1], \ | |
268 | }; | |
269 | PROFILE_ATTR(1); | |
270 | PROFILE_ATTR(2); | |
271 | PROFILE_ATTR(3); | |
272 | PROFILE_ATTR(4); | |
273 | PROFILE_ATTR(5); | |
274 | ||
0e70f97f SA |
275 | static ssize_t kovaplus_sysfs_show_actual_profile(struct device *dev, |
276 | struct device_attribute *attr, char *buf) | |
277 | { | |
278 | struct kovaplus_device *kovaplus = | |
279 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
280 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_profile); | |
281 | } | |
282 | ||
283 | static ssize_t kovaplus_sysfs_set_actual_profile(struct device *dev, | |
284 | struct device_attribute *attr, char const *buf, size_t size) | |
285 | { | |
286 | struct kovaplus_device *kovaplus; | |
287 | struct usb_device *usb_dev; | |
288 | unsigned long profile; | |
289 | int retval; | |
6b9a57b9 | 290 | struct kovaplus_roccat_report roccat_report; |
0e70f97f SA |
291 | |
292 | dev = dev->parent->parent; | |
293 | kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
294 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
295 | ||
dfc450b5 | 296 | retval = kstrtoul(buf, 10, &profile); |
0e70f97f SA |
297 | if (retval) |
298 | return retval; | |
299 | ||
300 | if (profile >= 5) | |
301 | return -EINVAL; | |
302 | ||
303 | mutex_lock(&kovaplus->kovaplus_lock); | |
304 | retval = kovaplus_set_actual_profile(usb_dev, profile); | |
6b9a57b9 SA |
305 | if (retval) { |
306 | mutex_unlock(&kovaplus->kovaplus_lock); | |
307 | return retval; | |
308 | } | |
309 | ||
901e64db | 310 | kovaplus_profile_activated(kovaplus, profile); |
6b9a57b9 SA |
311 | |
312 | roccat_report.type = KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1; | |
313 | roccat_report.profile = profile + 1; | |
314 | roccat_report.button = 0; | |
315 | roccat_report.data1 = profile + 1; | |
316 | roccat_report.data2 = 0; | |
317 | roccat_report_event(kovaplus->chrdev_minor, | |
318 | (uint8_t const *)&roccat_report); | |
319 | ||
0e70f97f | 320 | mutex_unlock(&kovaplus->kovaplus_lock); |
0e70f97f SA |
321 | |
322 | return size; | |
323 | } | |
46a58c44 GKH |
324 | static DEVICE_ATTR(actual_profile, 0660, |
325 | kovaplus_sysfs_show_actual_profile, | |
326 | kovaplus_sysfs_set_actual_profile); | |
0e70f97f SA |
327 | |
328 | static ssize_t kovaplus_sysfs_show_actual_cpi(struct device *dev, | |
329 | struct device_attribute *attr, char *buf) | |
330 | { | |
331 | struct kovaplus_device *kovaplus = | |
332 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
333 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_cpi); | |
334 | } | |
46a58c44 | 335 | static DEVICE_ATTR(actual_cpi, 0440, kovaplus_sysfs_show_actual_cpi, NULL); |
0e70f97f SA |
336 | |
337 | static ssize_t kovaplus_sysfs_show_actual_sensitivity_x(struct device *dev, | |
338 | struct device_attribute *attr, char *buf) | |
339 | { | |
340 | struct kovaplus_device *kovaplus = | |
341 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
342 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_x_sensitivity); | |
343 | } | |
46a58c44 GKH |
344 | static DEVICE_ATTR(actual_sensitivity_x, 0440, |
345 | kovaplus_sysfs_show_actual_sensitivity_x, NULL); | |
0e70f97f SA |
346 | |
347 | static ssize_t kovaplus_sysfs_show_actual_sensitivity_y(struct device *dev, | |
348 | struct device_attribute *attr, char *buf) | |
349 | { | |
350 | struct kovaplus_device *kovaplus = | |
351 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
352 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_y_sensitivity); | |
353 | } | |
46a58c44 GKH |
354 | static DEVICE_ATTR(actual_sensitivity_y, 0440, |
355 | kovaplus_sysfs_show_actual_sensitivity_y, NULL); | |
0e70f97f SA |
356 | |
357 | static ssize_t kovaplus_sysfs_show_firmware_version(struct device *dev, | |
358 | struct device_attribute *attr, char *buf) | |
359 | { | |
94a8fcf9 SA |
360 | struct kovaplus_device *kovaplus; |
361 | struct usb_device *usb_dev; | |
362 | struct kovaplus_info info; | |
363 | ||
364 | dev = dev->parent->parent; | |
365 | kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
366 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
367 | ||
368 | mutex_lock(&kovaplus->kovaplus_lock); | |
369 | roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_INFO, | |
370 | &info, KOVAPLUS_SIZE_INFO); | |
371 | mutex_unlock(&kovaplus->kovaplus_lock); | |
372 | ||
373 | return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version); | |
0e70f97f | 374 | } |
46a58c44 GKH |
375 | static DEVICE_ATTR(firmware_version, 0440, |
376 | kovaplus_sysfs_show_firmware_version, NULL); | |
0e70f97f | 377 | |
46a58c44 GKH |
378 | static struct attribute *kovaplus_attrs[] = { |
379 | &dev_attr_actual_cpi.attr, | |
380 | &dev_attr_firmware_version.attr, | |
381 | &dev_attr_actual_profile.attr, | |
382 | &dev_attr_actual_sensitivity_x.attr, | |
383 | &dev_attr_actual_sensitivity_y.attr, | |
384 | NULL, | |
0e70f97f | 385 | }; |
975b53cc GKH |
386 | |
387 | static struct bin_attribute *kovaplus_bin_attributes[] = { | |
388 | &bin_attr_control, | |
389 | &bin_attr_info, | |
390 | &bin_attr_profile_settings, | |
391 | &bin_attr_profile_buttons, | |
392 | &bin_attr_profile1_settings, | |
393 | &bin_attr_profile2_settings, | |
394 | &bin_attr_profile3_settings, | |
395 | &bin_attr_profile4_settings, | |
396 | &bin_attr_profile5_settings, | |
397 | &bin_attr_profile1_buttons, | |
398 | &bin_attr_profile2_buttons, | |
399 | &bin_attr_profile3_buttons, | |
400 | &bin_attr_profile4_buttons, | |
401 | &bin_attr_profile5_buttons, | |
402 | NULL, | |
403 | }; | |
404 | ||
405 | static const struct attribute_group kovaplus_group = { | |
406 | .attrs = kovaplus_attrs, | |
407 | .bin_attrs = kovaplus_bin_attributes, | |
408 | }; | |
409 | ||
410 | static const struct attribute_group *kovaplus_groups[] = { | |
411 | &kovaplus_group, | |
412 | NULL, | |
0e70f97f SA |
413 | }; |
414 | ||
415 | static int kovaplus_init_kovaplus_device_struct(struct usb_device *usb_dev, | |
416 | struct kovaplus_device *kovaplus) | |
417 | { | |
418 | int retval, i; | |
419 | static uint wait = 70; /* device will freeze with just 60 */ | |
420 | ||
421 | mutex_init(&kovaplus->kovaplus_lock); | |
422 | ||
0e70f97f SA |
423 | for (i = 0; i < 5; ++i) { |
424 | msleep(wait); | |
425 | retval = kovaplus_get_profile_settings(usb_dev, | |
426 | &kovaplus->profile_settings[i], i); | |
427 | if (retval) | |
428 | return retval; | |
429 | ||
430 | msleep(wait); | |
431 | retval = kovaplus_get_profile_buttons(usb_dev, | |
432 | &kovaplus->profile_buttons[i], i); | |
433 | if (retval) | |
434 | return retval; | |
435 | } | |
436 | ||
437 | msleep(wait); | |
438 | retval = kovaplus_get_actual_profile(usb_dev); | |
439 | if (retval < 0) | |
440 | return retval; | |
441 | kovaplus_profile_activated(kovaplus, retval); | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
446 | static int kovaplus_init_specials(struct hid_device *hdev) | |
447 | { | |
448 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
449 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
450 | struct kovaplus_device *kovaplus; | |
451 | int retval; | |
452 | ||
453 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
454 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
455 | ||
456 | kovaplus = kzalloc(sizeof(*kovaplus), GFP_KERNEL); | |
457 | if (!kovaplus) { | |
458 | hid_err(hdev, "can't alloc device descriptor\n"); | |
459 | return -ENOMEM; | |
460 | } | |
461 | hid_set_drvdata(hdev, kovaplus); | |
462 | ||
463 | retval = kovaplus_init_kovaplus_device_struct(usb_dev, kovaplus); | |
464 | if (retval) { | |
465 | hid_err(hdev, "couldn't init struct kovaplus_device\n"); | |
466 | goto exit_free; | |
467 | } | |
468 | ||
8211e460 SA |
469 | retval = roccat_connect(kovaplus_class, hdev, |
470 | sizeof(struct kovaplus_roccat_report)); | |
0e70f97f SA |
471 | if (retval < 0) { |
472 | hid_err(hdev, "couldn't init char dev\n"); | |
473 | } else { | |
474 | kovaplus->chrdev_minor = retval; | |
475 | kovaplus->roccat_claimed = 1; | |
476 | } | |
477 | ||
478 | } else { | |
479 | hid_set_drvdata(hdev, NULL); | |
480 | } | |
481 | ||
482 | return 0; | |
483 | exit_free: | |
484 | kfree(kovaplus); | |
485 | return retval; | |
486 | } | |
487 | ||
488 | static void kovaplus_remove_specials(struct hid_device *hdev) | |
489 | { | |
490 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
491 | struct kovaplus_device *kovaplus; | |
492 | ||
493 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
494 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
495 | kovaplus = hid_get_drvdata(hdev); | |
496 | if (kovaplus->roccat_claimed) | |
497 | roccat_disconnect(kovaplus->chrdev_minor); | |
498 | kfree(kovaplus); | |
499 | } | |
500 | } | |
501 | ||
502 | static int kovaplus_probe(struct hid_device *hdev, | |
503 | const struct hid_device_id *id) | |
504 | { | |
505 | int retval; | |
506 | ||
507 | retval = hid_parse(hdev); | |
508 | if (retval) { | |
509 | hid_err(hdev, "parse failed\n"); | |
510 | goto exit; | |
511 | } | |
512 | ||
513 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
514 | if (retval) { | |
515 | hid_err(hdev, "hw start failed\n"); | |
516 | goto exit; | |
517 | } | |
518 | ||
519 | retval = kovaplus_init_specials(hdev); | |
520 | if (retval) { | |
521 | hid_err(hdev, "couldn't install mouse\n"); | |
522 | goto exit_stop; | |
523 | } | |
524 | ||
525 | return 0; | |
526 | ||
527 | exit_stop: | |
528 | hid_hw_stop(hdev); | |
529 | exit: | |
530 | return retval; | |
531 | } | |
532 | ||
533 | static void kovaplus_remove(struct hid_device *hdev) | |
534 | { | |
535 | kovaplus_remove_specials(hdev); | |
536 | hid_hw_stop(hdev); | |
537 | } | |
538 | ||
539 | static void kovaplus_keep_values_up_to_date(struct kovaplus_device *kovaplus, | |
540 | u8 const *data) | |
541 | { | |
542 | struct kovaplus_mouse_report_button const *button_report; | |
543 | ||
544 | if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON) | |
545 | return; | |
546 | ||
547 | button_report = (struct kovaplus_mouse_report_button const *)data; | |
548 | ||
549 | switch (button_report->type) { | |
550 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1: | |
551 | kovaplus_profile_activated(kovaplus, button_report->data1 - 1); | |
552 | break; | |
553 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI: | |
554 | kovaplus->actual_cpi = kovaplus_convert_event_cpi(button_report->data1); | |
7be63f20 | 555 | break; |
0e70f97f SA |
556 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY: |
557 | kovaplus->actual_x_sensitivity = button_report->data1; | |
558 | kovaplus->actual_y_sensitivity = button_report->data2; | |
7be63f20 SA |
559 | break; |
560 | default: | |
561 | break; | |
0e70f97f SA |
562 | } |
563 | } | |
564 | ||
565 | static void kovaplus_report_to_chrdev(struct kovaplus_device const *kovaplus, | |
566 | u8 const *data) | |
567 | { | |
568 | struct kovaplus_roccat_report roccat_report; | |
569 | struct kovaplus_mouse_report_button const *button_report; | |
570 | ||
571 | if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON) | |
572 | return; | |
573 | ||
574 | button_report = (struct kovaplus_mouse_report_button const *)data; | |
575 | ||
576 | if (button_report->type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2) | |
577 | return; | |
578 | ||
579 | roccat_report.type = button_report->type; | |
580 | roccat_report.profile = kovaplus->actual_profile + 1; | |
581 | ||
582 | if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO || | |
583 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT || | |
584 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH || | |
585 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) | |
586 | roccat_report.button = button_report->data1; | |
587 | else | |
588 | roccat_report.button = 0; | |
589 | ||
590 | if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI) | |
591 | roccat_report.data1 = kovaplus_convert_event_cpi(button_report->data1); | |
592 | else | |
593 | roccat_report.data1 = button_report->data1; | |
594 | ||
595 | roccat_report.data2 = button_report->data2; | |
596 | ||
597 | roccat_report_event(kovaplus->chrdev_minor, | |
8211e460 | 598 | (uint8_t const *)&roccat_report); |
0e70f97f SA |
599 | } |
600 | ||
601 | static int kovaplus_raw_event(struct hid_device *hdev, | |
602 | struct hid_report *report, u8 *data, int size) | |
603 | { | |
604 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
605 | struct kovaplus_device *kovaplus = hid_get_drvdata(hdev); | |
606 | ||
607 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
608 | != USB_INTERFACE_PROTOCOL_MOUSE) | |
609 | return 0; | |
610 | ||
901e64db SA |
611 | if (kovaplus == NULL) |
612 | return 0; | |
613 | ||
0e70f97f SA |
614 | kovaplus_keep_values_up_to_date(kovaplus, data); |
615 | ||
616 | if (kovaplus->roccat_claimed) | |
617 | kovaplus_report_to_chrdev(kovaplus, data); | |
618 | ||
619 | return 0; | |
620 | } | |
621 | ||
622 | static const struct hid_device_id kovaplus_devices[] = { | |
623 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) }, | |
624 | { } | |
625 | }; | |
626 | ||
627 | MODULE_DEVICE_TABLE(hid, kovaplus_devices); | |
628 | ||
629 | static struct hid_driver kovaplus_driver = { | |
630 | .name = "kovaplus", | |
631 | .id_table = kovaplus_devices, | |
632 | .probe = kovaplus_probe, | |
633 | .remove = kovaplus_remove, | |
634 | .raw_event = kovaplus_raw_event | |
635 | }; | |
636 | ||
637 | static int __init kovaplus_init(void) | |
638 | { | |
639 | int retval; | |
640 | ||
641 | kovaplus_class = class_create(THIS_MODULE, "kovaplus"); | |
642 | if (IS_ERR(kovaplus_class)) | |
643 | return PTR_ERR(kovaplus_class); | |
46a58c44 | 644 | kovaplus_class->dev_groups = kovaplus_groups; |
0e70f97f SA |
645 | |
646 | retval = hid_register_driver(&kovaplus_driver); | |
647 | if (retval) | |
648 | class_destroy(kovaplus_class); | |
649 | return retval; | |
650 | } | |
651 | ||
652 | static void __exit kovaplus_exit(void) | |
653 | { | |
0e70f97f | 654 | hid_unregister_driver(&kovaplus_driver); |
74b643da | 655 | class_destroy(kovaplus_class); |
0e70f97f SA |
656 | } |
657 | ||
658 | module_init(kovaplus_init); | |
659 | module_exit(kovaplus_exit); | |
660 | ||
661 | MODULE_AUTHOR("Stefan Achatz"); | |
662 | MODULE_DESCRIPTION("USB Roccat Kova[+] driver"); | |
663 | MODULE_LICENSE("GPL v2"); |