]>
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 | { | |
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 | ||
46 | static int kovaplus_send_control(struct usb_device *usb_dev, uint value, | |
47 | enum kovaplus_control_requests request) | |
48 | { | |
49 | int retval; | |
7392d73b | 50 | struct roccat_common2_control control; |
0e70f97f SA |
51 | |
52 | if ((request == KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS || | |
53 | request == KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) && | |
54 | value > 4) | |
55 | return -EINVAL; | |
56 | ||
4728f2dc | 57 | control.command = ROCCAT_COMMON_COMMAND_CONTROL; |
0e70f97f SA |
58 | control.value = value; |
59 | control.request = request; | |
60 | ||
7392d73b SA |
61 | retval = roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL, |
62 | &control, sizeof(struct roccat_common2_control)); | |
0e70f97f SA |
63 | |
64 | return retval; | |
65 | } | |
66 | ||
0e70f97f SA |
67 | static 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 | ||
0e70f97f SA |
73 | static 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 | ||
7392d73b | 83 | return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_SETTINGS, |
94a8fcf9 | 84 | buf, KOVAPLUS_SIZE_PROFILE_SETTINGS); |
0e70f97f SA |
85 | } |
86 | ||
87 | static 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 | ||
7392d73b | 97 | return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_BUTTONS, |
94a8fcf9 | 98 | buf, KOVAPLUS_SIZE_PROFILE_BUTTONS); |
0e70f97f SA |
99 | } |
100 | ||
101 | /* retval is 0-4 on success, < 0 on error */ | |
102 | static int kovaplus_get_actual_profile(struct usb_device *usb_dev) | |
103 | { | |
104 | struct kovaplus_actual_profile buf; | |
105 | int retval; | |
106 | ||
7392d73b | 107 | retval = roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_ACTUAL_PROFILE, |
0e70f97f SA |
108 | &buf, sizeof(struct kovaplus_actual_profile)); |
109 | ||
110 | return retval ? retval : buf.actual_profile; | |
111 | } | |
112 | ||
113 | static 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 | ||
7392d73b | 122 | return roccat_common2_send_with_status(usb_dev, |
4728f2dc | 123 | KOVAPLUS_COMMAND_ACTUAL_PROFILE, |
0e70f97f SA |
124 | &buf, sizeof(struct kovaplus_actual_profile)); |
125 | } | |
126 | ||
94a8fcf9 SA |
127 | static 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) | |
0e70f97f SA |
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)); | |
94a8fcf9 SA |
134 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
135 | int retval; | |
0e70f97f | 136 | |
94a8fcf9 | 137 | if (off >= real_size) |
0e70f97f SA |
138 | return 0; |
139 | ||
94a8fcf9 SA |
140 | if (off != 0 || count != real_size) |
141 | return -EINVAL; | |
0e70f97f SA |
142 | |
143 | mutex_lock(&kovaplus->kovaplus_lock); | |
94a8fcf9 | 144 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
0e70f97f SA |
145 | mutex_unlock(&kovaplus->kovaplus_lock); |
146 | ||
94a8fcf9 SA |
147 | if (retval) |
148 | return retval; | |
149 | ||
150 | return real_size; | |
0e70f97f SA |
151 | } |
152 | ||
94a8fcf9 SA |
153 | static 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) | |
0e70f97f SA |
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)); | |
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 SA |
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 | } | |
0e70f97f | 213 | |
94a8fcf9 SA |
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 \ | |
0e70f97f SA |
219 | } |
220 | ||
172e2abc | 221 | KOVAPLUS_SYSFS_W(control, CONTROL) |
94a8fcf9 | 222 | KOVAPLUS_SYSFS_RW(info, INFO) |
172e2abc SA |
223 | KOVAPLUS_SYSFS_RW(profile_settings, PROFILE_SETTINGS) |
224 | KOVAPLUS_SYSFS_RW(profile_buttons, PROFILE_BUTTONS) | |
94a8fcf9 SA |
225 | |
226 | static ssize_t kovaplus_sysfs_read_profilex_settings(struct file *fp, | |
0e70f97f SA |
227 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
228 | loff_t off, size_t count) | |
229 | { | |
230 | struct device *dev = | |
231 | container_of(kobj, struct device, kobj)->parent->parent; | |
0e70f97f | 232 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
94a8fcf9 | 233 | ssize_t retval; |
0e70f97f | 234 | |
94a8fcf9 SA |
235 | retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private), |
236 | KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS); | |
237 | if (retval) | |
238 | return retval; | |
0e70f97f | 239 | |
94a8fcf9 SA |
240 | return kovaplus_sysfs_read(fp, kobj, buf, off, count, |
241 | KOVAPLUS_SIZE_PROFILE_SETTINGS, | |
242 | KOVAPLUS_COMMAND_PROFILE_SETTINGS); | |
243 | } | |
0e70f97f | 244 | |
94a8fcf9 SA |
245 | static ssize_t kovaplus_sysfs_read_profilex_buttons(struct file *fp, |
246 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
247 | loff_t off, size_t count) | |
248 | { | |
249 | struct device *dev = | |
250 | container_of(kobj, struct device, kobj)->parent->parent; | |
251 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
252 | ssize_t retval; | |
0e70f97f | 253 | |
94a8fcf9 SA |
254 | retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private), |
255 | KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS); | |
0e70f97f SA |
256 | if (retval) |
257 | return retval; | |
258 | ||
94a8fcf9 SA |
259 | return kovaplus_sysfs_read(fp, kobj, buf, off, count, |
260 | KOVAPLUS_SIZE_PROFILE_BUTTONS, | |
261 | KOVAPLUS_COMMAND_PROFILE_BUTTONS); | |
0e70f97f SA |
262 | } |
263 | ||
264 | static ssize_t kovaplus_sysfs_show_actual_profile(struct device *dev, | |
265 | struct device_attribute *attr, char *buf) | |
266 | { | |
267 | struct kovaplus_device *kovaplus = | |
268 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
269 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_profile); | |
270 | } | |
271 | ||
272 | static ssize_t kovaplus_sysfs_set_actual_profile(struct device *dev, | |
273 | struct device_attribute *attr, char const *buf, size_t size) | |
274 | { | |
275 | struct kovaplus_device *kovaplus; | |
276 | struct usb_device *usb_dev; | |
277 | unsigned long profile; | |
278 | int retval; | |
6b9a57b9 | 279 | struct kovaplus_roccat_report roccat_report; |
0e70f97f SA |
280 | |
281 | dev = dev->parent->parent; | |
282 | kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
283 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
284 | ||
285 | retval = strict_strtoul(buf, 10, &profile); | |
286 | if (retval) | |
287 | return retval; | |
288 | ||
289 | if (profile >= 5) | |
290 | return -EINVAL; | |
291 | ||
292 | mutex_lock(&kovaplus->kovaplus_lock); | |
293 | retval = kovaplus_set_actual_profile(usb_dev, profile); | |
6b9a57b9 SA |
294 | if (retval) { |
295 | mutex_unlock(&kovaplus->kovaplus_lock); | |
296 | return retval; | |
297 | } | |
298 | ||
901e64db | 299 | kovaplus_profile_activated(kovaplus, profile); |
6b9a57b9 SA |
300 | |
301 | roccat_report.type = KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1; | |
302 | roccat_report.profile = profile + 1; | |
303 | roccat_report.button = 0; | |
304 | roccat_report.data1 = profile + 1; | |
305 | roccat_report.data2 = 0; | |
306 | roccat_report_event(kovaplus->chrdev_minor, | |
307 | (uint8_t const *)&roccat_report); | |
308 | ||
0e70f97f | 309 | mutex_unlock(&kovaplus->kovaplus_lock); |
0e70f97f SA |
310 | |
311 | return size; | |
312 | } | |
46a58c44 GKH |
313 | static DEVICE_ATTR(actual_profile, 0660, |
314 | kovaplus_sysfs_show_actual_profile, | |
315 | kovaplus_sysfs_set_actual_profile); | |
0e70f97f SA |
316 | |
317 | static ssize_t kovaplus_sysfs_show_actual_cpi(struct device *dev, | |
318 | struct device_attribute *attr, char *buf) | |
319 | { | |
320 | struct kovaplus_device *kovaplus = | |
321 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
322 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_cpi); | |
323 | } | |
46a58c44 | 324 | static DEVICE_ATTR(actual_cpi, 0440, kovaplus_sysfs_show_actual_cpi, NULL); |
0e70f97f SA |
325 | |
326 | static ssize_t kovaplus_sysfs_show_actual_sensitivity_x(struct device *dev, | |
327 | struct device_attribute *attr, char *buf) | |
328 | { | |
329 | struct kovaplus_device *kovaplus = | |
330 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
331 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_x_sensitivity); | |
332 | } | |
46a58c44 GKH |
333 | static DEVICE_ATTR(actual_sensitivity_x, 0440, |
334 | kovaplus_sysfs_show_actual_sensitivity_x, NULL); | |
0e70f97f SA |
335 | |
336 | static ssize_t kovaplus_sysfs_show_actual_sensitivity_y(struct device *dev, | |
337 | struct device_attribute *attr, char *buf) | |
338 | { | |
339 | struct kovaplus_device *kovaplus = | |
340 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
341 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_y_sensitivity); | |
342 | } | |
46a58c44 GKH |
343 | static DEVICE_ATTR(actual_sensitivity_y, 0440, |
344 | kovaplus_sysfs_show_actual_sensitivity_y, NULL); | |
0e70f97f SA |
345 | |
346 | static ssize_t kovaplus_sysfs_show_firmware_version(struct device *dev, | |
347 | struct device_attribute *attr, char *buf) | |
348 | { | |
94a8fcf9 SA |
349 | struct kovaplus_device *kovaplus; |
350 | struct usb_device *usb_dev; | |
351 | struct kovaplus_info info; | |
352 | ||
353 | dev = dev->parent->parent; | |
354 | kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
355 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
356 | ||
357 | mutex_lock(&kovaplus->kovaplus_lock); | |
358 | roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_INFO, | |
359 | &info, KOVAPLUS_SIZE_INFO); | |
360 | mutex_unlock(&kovaplus->kovaplus_lock); | |
361 | ||
362 | return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version); | |
0e70f97f | 363 | } |
46a58c44 GKH |
364 | static DEVICE_ATTR(firmware_version, 0440, |
365 | kovaplus_sysfs_show_firmware_version, NULL); | |
0e70f97f | 366 | |
46a58c44 GKH |
367 | static struct attribute *kovaplus_attrs[] = { |
368 | &dev_attr_actual_cpi.attr, | |
369 | &dev_attr_firmware_version.attr, | |
370 | &dev_attr_actual_profile.attr, | |
371 | &dev_attr_actual_sensitivity_x.attr, | |
372 | &dev_attr_actual_sensitivity_y.attr, | |
373 | NULL, | |
0e70f97f | 374 | }; |
46a58c44 | 375 | ATTRIBUTE_GROUPS(kovaplus); |
0e70f97f SA |
376 | |
377 | static struct bin_attribute kovaplus_bin_attributes[] = { | |
172e2abc | 378 | KOVAPLUS_BIN_ATTRIBUTE_W(control, CONTROL), |
94a8fcf9 | 379 | KOVAPLUS_BIN_ATTRIBUTE_RW(info, INFO), |
172e2abc SA |
380 | KOVAPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS), |
381 | KOVAPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS), | |
0e70f97f SA |
382 | { |
383 | .attr = { .name = "profile1_settings", .mode = 0440 }, | |
94a8fcf9 | 384 | .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, |
0e70f97f SA |
385 | .read = kovaplus_sysfs_read_profilex_settings, |
386 | .private = &profile_numbers[0] | |
387 | }, | |
388 | { | |
389 | .attr = { .name = "profile2_settings", .mode = 0440 }, | |
94a8fcf9 | 390 | .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, |
0e70f97f SA |
391 | .read = kovaplus_sysfs_read_profilex_settings, |
392 | .private = &profile_numbers[1] | |
393 | }, | |
394 | { | |
395 | .attr = { .name = "profile3_settings", .mode = 0440 }, | |
94a8fcf9 | 396 | .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, |
0e70f97f SA |
397 | .read = kovaplus_sysfs_read_profilex_settings, |
398 | .private = &profile_numbers[2] | |
399 | }, | |
400 | { | |
401 | .attr = { .name = "profile4_settings", .mode = 0440 }, | |
94a8fcf9 | 402 | .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, |
0e70f97f SA |
403 | .read = kovaplus_sysfs_read_profilex_settings, |
404 | .private = &profile_numbers[3] | |
405 | }, | |
406 | { | |
407 | .attr = { .name = "profile5_settings", .mode = 0440 }, | |
94a8fcf9 | 408 | .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, |
0e70f97f SA |
409 | .read = kovaplus_sysfs_read_profilex_settings, |
410 | .private = &profile_numbers[4] | |
411 | }, | |
0e70f97f SA |
412 | { |
413 | .attr = { .name = "profile1_buttons", .mode = 0440 }, | |
94a8fcf9 | 414 | .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, |
0e70f97f SA |
415 | .read = kovaplus_sysfs_read_profilex_buttons, |
416 | .private = &profile_numbers[0] | |
417 | }, | |
418 | { | |
419 | .attr = { .name = "profile2_buttons", .mode = 0440 }, | |
94a8fcf9 | 420 | .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, |
0e70f97f SA |
421 | .read = kovaplus_sysfs_read_profilex_buttons, |
422 | .private = &profile_numbers[1] | |
423 | }, | |
424 | { | |
425 | .attr = { .name = "profile3_buttons", .mode = 0440 }, | |
94a8fcf9 | 426 | .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, |
0e70f97f SA |
427 | .read = kovaplus_sysfs_read_profilex_buttons, |
428 | .private = &profile_numbers[2] | |
429 | }, | |
430 | { | |
431 | .attr = { .name = "profile4_buttons", .mode = 0440 }, | |
94a8fcf9 | 432 | .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, |
0e70f97f SA |
433 | .read = kovaplus_sysfs_read_profilex_buttons, |
434 | .private = &profile_numbers[3] | |
435 | }, | |
436 | { | |
437 | .attr = { .name = "profile5_buttons", .mode = 0440 }, | |
94a8fcf9 | 438 | .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, |
0e70f97f SA |
439 | .read = kovaplus_sysfs_read_profilex_buttons, |
440 | .private = &profile_numbers[4] | |
441 | }, | |
442 | __ATTR_NULL | |
443 | }; | |
444 | ||
445 | static int kovaplus_init_kovaplus_device_struct(struct usb_device *usb_dev, | |
446 | struct kovaplus_device *kovaplus) | |
447 | { | |
448 | int retval, i; | |
449 | static uint wait = 70; /* device will freeze with just 60 */ | |
450 | ||
451 | mutex_init(&kovaplus->kovaplus_lock); | |
452 | ||
0e70f97f SA |
453 | for (i = 0; i < 5; ++i) { |
454 | msleep(wait); | |
455 | retval = kovaplus_get_profile_settings(usb_dev, | |
456 | &kovaplus->profile_settings[i], i); | |
457 | if (retval) | |
458 | return retval; | |
459 | ||
460 | msleep(wait); | |
461 | retval = kovaplus_get_profile_buttons(usb_dev, | |
462 | &kovaplus->profile_buttons[i], i); | |
463 | if (retval) | |
464 | return retval; | |
465 | } | |
466 | ||
467 | msleep(wait); | |
468 | retval = kovaplus_get_actual_profile(usb_dev); | |
469 | if (retval < 0) | |
470 | return retval; | |
471 | kovaplus_profile_activated(kovaplus, retval); | |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
476 | static int kovaplus_init_specials(struct hid_device *hdev) | |
477 | { | |
478 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
479 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
480 | struct kovaplus_device *kovaplus; | |
481 | int retval; | |
482 | ||
483 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
484 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
485 | ||
486 | kovaplus = kzalloc(sizeof(*kovaplus), GFP_KERNEL); | |
487 | if (!kovaplus) { | |
488 | hid_err(hdev, "can't alloc device descriptor\n"); | |
489 | return -ENOMEM; | |
490 | } | |
491 | hid_set_drvdata(hdev, kovaplus); | |
492 | ||
493 | retval = kovaplus_init_kovaplus_device_struct(usb_dev, kovaplus); | |
494 | if (retval) { | |
495 | hid_err(hdev, "couldn't init struct kovaplus_device\n"); | |
496 | goto exit_free; | |
497 | } | |
498 | ||
8211e460 SA |
499 | retval = roccat_connect(kovaplus_class, hdev, |
500 | sizeof(struct kovaplus_roccat_report)); | |
0e70f97f SA |
501 | if (retval < 0) { |
502 | hid_err(hdev, "couldn't init char dev\n"); | |
503 | } else { | |
504 | kovaplus->chrdev_minor = retval; | |
505 | kovaplus->roccat_claimed = 1; | |
506 | } | |
507 | ||
508 | } else { | |
509 | hid_set_drvdata(hdev, NULL); | |
510 | } | |
511 | ||
512 | return 0; | |
513 | exit_free: | |
514 | kfree(kovaplus); | |
515 | return retval; | |
516 | } | |
517 | ||
518 | static void kovaplus_remove_specials(struct hid_device *hdev) | |
519 | { | |
520 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
521 | struct kovaplus_device *kovaplus; | |
522 | ||
523 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
524 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
525 | kovaplus = hid_get_drvdata(hdev); | |
526 | if (kovaplus->roccat_claimed) | |
527 | roccat_disconnect(kovaplus->chrdev_minor); | |
528 | kfree(kovaplus); | |
529 | } | |
530 | } | |
531 | ||
532 | static int kovaplus_probe(struct hid_device *hdev, | |
533 | const struct hid_device_id *id) | |
534 | { | |
535 | int retval; | |
536 | ||
537 | retval = hid_parse(hdev); | |
538 | if (retval) { | |
539 | hid_err(hdev, "parse failed\n"); | |
540 | goto exit; | |
541 | } | |
542 | ||
543 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
544 | if (retval) { | |
545 | hid_err(hdev, "hw start failed\n"); | |
546 | goto exit; | |
547 | } | |
548 | ||
549 | retval = kovaplus_init_specials(hdev); | |
550 | if (retval) { | |
551 | hid_err(hdev, "couldn't install mouse\n"); | |
552 | goto exit_stop; | |
553 | } | |
554 | ||
555 | return 0; | |
556 | ||
557 | exit_stop: | |
558 | hid_hw_stop(hdev); | |
559 | exit: | |
560 | return retval; | |
561 | } | |
562 | ||
563 | static void kovaplus_remove(struct hid_device *hdev) | |
564 | { | |
565 | kovaplus_remove_specials(hdev); | |
566 | hid_hw_stop(hdev); | |
567 | } | |
568 | ||
569 | static void kovaplus_keep_values_up_to_date(struct kovaplus_device *kovaplus, | |
570 | u8 const *data) | |
571 | { | |
572 | struct kovaplus_mouse_report_button const *button_report; | |
573 | ||
574 | if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON) | |
575 | return; | |
576 | ||
577 | button_report = (struct kovaplus_mouse_report_button const *)data; | |
578 | ||
579 | switch (button_report->type) { | |
580 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1: | |
581 | kovaplus_profile_activated(kovaplus, button_report->data1 - 1); | |
582 | break; | |
583 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI: | |
584 | kovaplus->actual_cpi = kovaplus_convert_event_cpi(button_report->data1); | |
585 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY: | |
586 | kovaplus->actual_x_sensitivity = button_report->data1; | |
587 | kovaplus->actual_y_sensitivity = button_report->data2; | |
588 | } | |
589 | } | |
590 | ||
591 | static void kovaplus_report_to_chrdev(struct kovaplus_device const *kovaplus, | |
592 | u8 const *data) | |
593 | { | |
594 | struct kovaplus_roccat_report roccat_report; | |
595 | struct kovaplus_mouse_report_button const *button_report; | |
596 | ||
597 | if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON) | |
598 | return; | |
599 | ||
600 | button_report = (struct kovaplus_mouse_report_button const *)data; | |
601 | ||
602 | if (button_report->type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2) | |
603 | return; | |
604 | ||
605 | roccat_report.type = button_report->type; | |
606 | roccat_report.profile = kovaplus->actual_profile + 1; | |
607 | ||
608 | if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO || | |
609 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT || | |
610 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH || | |
611 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) | |
612 | roccat_report.button = button_report->data1; | |
613 | else | |
614 | roccat_report.button = 0; | |
615 | ||
616 | if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI) | |
617 | roccat_report.data1 = kovaplus_convert_event_cpi(button_report->data1); | |
618 | else | |
619 | roccat_report.data1 = button_report->data1; | |
620 | ||
621 | roccat_report.data2 = button_report->data2; | |
622 | ||
623 | roccat_report_event(kovaplus->chrdev_minor, | |
8211e460 | 624 | (uint8_t const *)&roccat_report); |
0e70f97f SA |
625 | } |
626 | ||
627 | static int kovaplus_raw_event(struct hid_device *hdev, | |
628 | struct hid_report *report, u8 *data, int size) | |
629 | { | |
630 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
631 | struct kovaplus_device *kovaplus = hid_get_drvdata(hdev); | |
632 | ||
633 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
634 | != USB_INTERFACE_PROTOCOL_MOUSE) | |
635 | return 0; | |
636 | ||
901e64db SA |
637 | if (kovaplus == NULL) |
638 | return 0; | |
639 | ||
0e70f97f SA |
640 | kovaplus_keep_values_up_to_date(kovaplus, data); |
641 | ||
642 | if (kovaplus->roccat_claimed) | |
643 | kovaplus_report_to_chrdev(kovaplus, data); | |
644 | ||
645 | return 0; | |
646 | } | |
647 | ||
648 | static const struct hid_device_id kovaplus_devices[] = { | |
649 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) }, | |
650 | { } | |
651 | }; | |
652 | ||
653 | MODULE_DEVICE_TABLE(hid, kovaplus_devices); | |
654 | ||
655 | static struct hid_driver kovaplus_driver = { | |
656 | .name = "kovaplus", | |
657 | .id_table = kovaplus_devices, | |
658 | .probe = kovaplus_probe, | |
659 | .remove = kovaplus_remove, | |
660 | .raw_event = kovaplus_raw_event | |
661 | }; | |
662 | ||
663 | static int __init kovaplus_init(void) | |
664 | { | |
665 | int retval; | |
666 | ||
667 | kovaplus_class = class_create(THIS_MODULE, "kovaplus"); | |
668 | if (IS_ERR(kovaplus_class)) | |
669 | return PTR_ERR(kovaplus_class); | |
46a58c44 | 670 | kovaplus_class->dev_groups = kovaplus_groups; |
0e70f97f SA |
671 | kovaplus_class->dev_bin_attrs = kovaplus_bin_attributes; |
672 | ||
673 | retval = hid_register_driver(&kovaplus_driver); | |
674 | if (retval) | |
675 | class_destroy(kovaplus_class); | |
676 | return retval; | |
677 | } | |
678 | ||
679 | static void __exit kovaplus_exit(void) | |
680 | { | |
0e70f97f | 681 | hid_unregister_driver(&kovaplus_driver); |
74b643da | 682 | class_destroy(kovaplus_class); |
0e70f97f SA |
683 | } |
684 | ||
685 | module_init(kovaplus_init); | |
686 | module_exit(kovaplus_exit); | |
687 | ||
688 | MODULE_AUTHOR("Stefan Achatz"); | |
689 | MODULE_DESCRIPTION("USB Roccat Kova[+] driver"); | |
690 | MODULE_LICENSE("GPL v2"); |