]>
Commit | Line | Data |
---|---|---|
47dbdbff SA |
1 | /* |
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. | |
17 | */ | |
18 | ||
19 | #include <linux/device.h> | |
20 | #include <linux/input.h> | |
21 | #include <linux/hid.h> | |
47dbdbff SA |
22 | #include <linux/module.h> |
23 | #include <linux/slab.h> | |
5dc0c983 | 24 | #include <linux/hid-roccat.h> |
47dbdbff | 25 | #include "hid-ids.h" |
5772f636 | 26 | #include "hid-roccat-common.h" |
47dbdbff SA |
27 | #include "hid-roccat-koneplus.h" |
28 | ||
29 | static uint profile_numbers[5] = {0, 1, 2, 3, 4}; | |
30 | ||
31 | static struct class *koneplus_class; | |
32 | ||
33 | static void koneplus_profile_activated(struct koneplus_device *koneplus, | |
34 | uint new_profile) | |
35 | { | |
36 | koneplus->actual_profile = new_profile; | |
37 | } | |
38 | ||
39 | static int koneplus_send_control(struct usb_device *usb_dev, uint value, | |
40 | enum koneplus_control_requests request) | |
41 | { | |
5772f636 | 42 | struct koneplus_control control; |
47dbdbff SA |
43 | |
44 | if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS || | |
45 | request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) && | |
46 | value > 4) | |
47 | return -EINVAL; | |
48 | ||
5772f636 SA |
49 | control.command = KONEPLUS_COMMAND_CONTROL; |
50 | control.value = value; | |
51 | control.request = request; | |
47dbdbff | 52 | |
5772f636 SA |
53 | return roccat_common_send(usb_dev, KONEPLUS_USB_COMMAND_CONTROL, |
54 | &control, sizeof(struct koneplus_control)); | |
47dbdbff SA |
55 | } |
56 | ||
57 | static int koneplus_receive_control_status(struct usb_device *usb_dev) | |
58 | { | |
59 | int retval; | |
5772f636 | 60 | struct koneplus_control control; |
47dbdbff SA |
61 | |
62 | do { | |
5772f636 SA |
63 | retval = roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_CONTROL, |
64 | &control, sizeof(struct koneplus_control)); | |
47dbdbff SA |
65 | |
66 | /* check if we get a completely wrong answer */ | |
67 | if (retval) | |
5772f636 | 68 | return retval; |
47dbdbff | 69 | |
5772f636 SA |
70 | if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_OK) |
71 | return 0; | |
47dbdbff SA |
72 | |
73 | /* indicates that hardware needs some more time to complete action */ | |
5772f636 | 74 | if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_WAIT) { |
47dbdbff SA |
75 | msleep(500); /* windows driver uses 1000 */ |
76 | continue; | |
77 | } | |
78 | ||
79 | /* seems to be critical - replug necessary */ | |
5772f636 SA |
80 | if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_OVERLOAD) |
81 | return -EINVAL; | |
47dbdbff | 82 | |
a28764ef | 83 | hid_err(usb_dev, "koneplus_receive_control_status: " |
5772f636 SA |
84 | "unknown response value 0x%x\n", control.value); |
85 | return -EINVAL; | |
47dbdbff | 86 | } while (1); |
47dbdbff SA |
87 | } |
88 | ||
89 | static int koneplus_send(struct usb_device *usb_dev, uint command, | |
5772f636 SA |
90 | void const *buf, uint size) |
91 | { | |
92 | int retval; | |
47dbdbff | 93 | |
5772f636 SA |
94 | retval = roccat_common_send(usb_dev, command, buf, size); |
95 | if (retval) | |
96 | return retval; | |
47dbdbff | 97 | |
5772f636 | 98 | return koneplus_receive_control_status(usb_dev); |
47dbdbff SA |
99 | } |
100 | ||
101 | static int koneplus_select_profile(struct usb_device *usb_dev, uint number, | |
102 | enum koneplus_control_requests request) | |
103 | { | |
104 | int retval; | |
105 | ||
106 | retval = koneplus_send_control(usb_dev, number, request); | |
107 | if (retval) | |
108 | return retval; | |
109 | ||
110 | /* allow time to settle things - windows driver uses 500 */ | |
111 | msleep(100); | |
112 | ||
113 | retval = koneplus_receive_control_status(usb_dev); | |
114 | if (retval) | |
115 | return retval; | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | static int koneplus_get_info(struct usb_device *usb_dev, | |
121 | struct koneplus_info *buf) | |
122 | { | |
5772f636 | 123 | return roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_INFO, |
47dbdbff SA |
124 | buf, sizeof(struct koneplus_info)); |
125 | } | |
126 | ||
127 | static int koneplus_get_profile_settings(struct usb_device *usb_dev, | |
128 | struct koneplus_profile_settings *buf, uint number) | |
129 | { | |
130 | int retval; | |
131 | ||
132 | retval = koneplus_select_profile(usb_dev, number, | |
133 | KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS); | |
134 | if (retval) | |
135 | return retval; | |
136 | ||
5772f636 | 137 | return roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_SETTINGS, |
47dbdbff SA |
138 | buf, sizeof(struct koneplus_profile_settings)); |
139 | } | |
140 | ||
141 | static int koneplus_set_profile_settings(struct usb_device *usb_dev, | |
142 | struct koneplus_profile_settings const *settings) | |
143 | { | |
144 | return koneplus_send(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_SETTINGS, | |
5772f636 | 145 | settings, sizeof(struct koneplus_profile_settings)); |
47dbdbff SA |
146 | } |
147 | ||
148 | static int koneplus_get_profile_buttons(struct usb_device *usb_dev, | |
149 | struct koneplus_profile_buttons *buf, int number) | |
150 | { | |
151 | int retval; | |
152 | ||
153 | retval = koneplus_select_profile(usb_dev, number, | |
154 | KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS); | |
155 | if (retval) | |
156 | return retval; | |
157 | ||
5772f636 | 158 | return roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_BUTTONS, |
47dbdbff SA |
159 | buf, sizeof(struct koneplus_profile_buttons)); |
160 | } | |
161 | ||
162 | static int koneplus_set_profile_buttons(struct usb_device *usb_dev, | |
163 | struct koneplus_profile_buttons const *buttons) | |
164 | { | |
165 | return koneplus_send(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_BUTTONS, | |
5772f636 | 166 | buttons, sizeof(struct koneplus_profile_buttons)); |
47dbdbff SA |
167 | } |
168 | ||
169 | /* retval is 0-4 on success, < 0 on error */ | |
b50f315c | 170 | static int koneplus_get_actual_profile(struct usb_device *usb_dev) |
47dbdbff | 171 | { |
b50f315c | 172 | struct koneplus_actual_profile buf; |
47dbdbff SA |
173 | int retval; |
174 | ||
b50f315c SA |
175 | retval = roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_ACTUAL_PROFILE, |
176 | &buf, sizeof(struct koneplus_actual_profile)); | |
47dbdbff | 177 | |
b50f315c | 178 | return retval ? retval : buf.actual_profile; |
47dbdbff SA |
179 | } |
180 | ||
b50f315c SA |
181 | static int koneplus_set_actual_profile(struct usb_device *usb_dev, |
182 | int new_profile) | |
47dbdbff | 183 | { |
b50f315c | 184 | struct koneplus_actual_profile buf; |
47dbdbff | 185 | |
b50f315c SA |
186 | buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE; |
187 | buf.size = sizeof(struct koneplus_actual_profile); | |
188 | buf.actual_profile = new_profile; | |
47dbdbff | 189 | |
b50f315c SA |
190 | return koneplus_send(usb_dev, KONEPLUS_USB_COMMAND_ACTUAL_PROFILE, |
191 | &buf, sizeof(struct koneplus_actual_profile)); | |
47dbdbff SA |
192 | } |
193 | ||
194 | static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj, | |
195 | char *buf, loff_t off, size_t count, | |
196 | size_t real_size, uint command) | |
197 | { | |
198 | struct device *dev = | |
199 | container_of(kobj, struct device, kobj)->parent->parent; | |
200 | struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
201 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
202 | int retval; | |
203 | ||
fd82be6c SA |
204 | if (off >= real_size) |
205 | return 0; | |
206 | ||
47dbdbff SA |
207 | if (off != 0 || count != real_size) |
208 | return -EINVAL; | |
209 | ||
210 | mutex_lock(&koneplus->koneplus_lock); | |
5772f636 | 211 | retval = roccat_common_receive(usb_dev, command, buf, real_size); |
47dbdbff SA |
212 | mutex_unlock(&koneplus->koneplus_lock); |
213 | ||
214 | if (retval) | |
215 | return retval; | |
216 | ||
217 | return real_size; | |
218 | } | |
219 | ||
220 | static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj, | |
221 | void const *buf, loff_t off, size_t count, | |
222 | size_t real_size, uint command) | |
223 | { | |
224 | struct device *dev = | |
225 | container_of(kobj, struct device, kobj)->parent->parent; | |
226 | struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
227 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
228 | int retval; | |
229 | ||
230 | if (off != 0 || count != real_size) | |
231 | return -EINVAL; | |
232 | ||
233 | mutex_lock(&koneplus->koneplus_lock); | |
5772f636 | 234 | retval = koneplus_send(usb_dev, command, buf, real_size); |
47dbdbff SA |
235 | mutex_unlock(&koneplus->koneplus_lock); |
236 | ||
237 | if (retval) | |
238 | return retval; | |
239 | ||
240 | return real_size; | |
241 | } | |
242 | ||
243 | static ssize_t koneplus_sysfs_write_macro(struct file *fp, | |
244 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
245 | loff_t off, size_t count) | |
246 | { | |
247 | return koneplus_sysfs_write(fp, kobj, buf, off, count, | |
248 | sizeof(struct koneplus_macro), KONEPLUS_USB_COMMAND_MACRO); | |
249 | } | |
250 | ||
251 | static ssize_t koneplus_sysfs_read_sensor(struct file *fp, | |
252 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
253 | loff_t off, size_t count) | |
254 | { | |
255 | return koneplus_sysfs_read(fp, kobj, buf, off, count, | |
256 | sizeof(struct koneplus_sensor), KONEPLUS_USB_COMMAND_SENSOR); | |
257 | } | |
258 | ||
259 | static ssize_t koneplus_sysfs_write_sensor(struct file *fp, | |
260 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
261 | loff_t off, size_t count) | |
262 | { | |
263 | return koneplus_sysfs_write(fp, kobj, buf, off, count, | |
264 | sizeof(struct koneplus_sensor), KONEPLUS_USB_COMMAND_SENSOR); | |
265 | } | |
266 | ||
267 | static ssize_t koneplus_sysfs_write_tcu(struct file *fp, | |
268 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
269 | loff_t off, size_t count) | |
270 | { | |
271 | return koneplus_sysfs_write(fp, kobj, buf, off, count, | |
272 | sizeof(struct koneplus_tcu), KONEPLUS_USB_COMMAND_TCU); | |
273 | } | |
274 | ||
275 | static ssize_t koneplus_sysfs_read_tcu_image(struct file *fp, | |
276 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
277 | loff_t off, size_t count) | |
278 | { | |
279 | return koneplus_sysfs_read(fp, kobj, buf, off, count, | |
280 | sizeof(struct koneplus_tcu_image), KONEPLUS_USB_COMMAND_TCU); | |
281 | } | |
282 | ||
283 | static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp, | |
284 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
285 | loff_t off, size_t count) | |
286 | { | |
287 | struct device *dev = | |
288 | container_of(kobj, struct device, kobj)->parent->parent; | |
289 | struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
290 | ||
291 | if (off >= sizeof(struct koneplus_profile_settings)) | |
292 | return 0; | |
293 | ||
294 | if (off + count > sizeof(struct koneplus_profile_settings)) | |
295 | count = sizeof(struct koneplus_profile_settings) - off; | |
296 | ||
297 | mutex_lock(&koneplus->koneplus_lock); | |
5772f636 | 298 | memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off, |
47dbdbff SA |
299 | count); |
300 | mutex_unlock(&koneplus->koneplus_lock); | |
301 | ||
302 | return count; | |
303 | } | |
304 | ||
305 | static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp, | |
306 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
307 | loff_t off, size_t count) | |
308 | { | |
309 | struct device *dev = | |
310 | container_of(kobj, struct device, kobj)->parent->parent; | |
311 | struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
312 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
313 | int retval = 0; | |
314 | int difference; | |
315 | int profile_number; | |
316 | struct koneplus_profile_settings *profile_settings; | |
317 | ||
318 | if (off != 0 || count != sizeof(struct koneplus_profile_settings)) | |
319 | return -EINVAL; | |
320 | ||
321 | profile_number = ((struct koneplus_profile_settings const *)buf)->number; | |
322 | profile_settings = &koneplus->profile_settings[profile_number]; | |
323 | ||
324 | mutex_lock(&koneplus->koneplus_lock); | |
325 | difference = memcmp(buf, profile_settings, | |
326 | sizeof(struct koneplus_profile_settings)); | |
327 | if (difference) { | |
328 | retval = koneplus_set_profile_settings(usb_dev, | |
329 | (struct koneplus_profile_settings const *)buf); | |
330 | if (!retval) | |
331 | memcpy(profile_settings, buf, | |
332 | sizeof(struct koneplus_profile_settings)); | |
333 | } | |
334 | mutex_unlock(&koneplus->koneplus_lock); | |
335 | ||
336 | if (retval) | |
337 | return retval; | |
338 | ||
339 | return sizeof(struct koneplus_profile_settings); | |
340 | } | |
341 | ||
342 | static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp, | |
343 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
344 | loff_t off, size_t count) | |
345 | { | |
346 | struct device *dev = | |
347 | container_of(kobj, struct device, kobj)->parent->parent; | |
348 | struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
349 | ||
350 | if (off >= sizeof(struct koneplus_profile_buttons)) | |
351 | return 0; | |
352 | ||
353 | if (off + count > sizeof(struct koneplus_profile_buttons)) | |
354 | count = sizeof(struct koneplus_profile_buttons) - off; | |
355 | ||
356 | mutex_lock(&koneplus->koneplus_lock); | |
5772f636 | 357 | memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off, |
47dbdbff SA |
358 | count); |
359 | mutex_unlock(&koneplus->koneplus_lock); | |
360 | ||
361 | return count; | |
362 | } | |
363 | ||
364 | static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp, | |
365 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
366 | loff_t off, size_t count) | |
367 | { | |
368 | struct device *dev = | |
369 | container_of(kobj, struct device, kobj)->parent->parent; | |
370 | struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
371 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
372 | int retval = 0; | |
373 | int difference; | |
374 | uint profile_number; | |
375 | struct koneplus_profile_buttons *profile_buttons; | |
376 | ||
377 | if (off != 0 || count != sizeof(struct koneplus_profile_buttons)) | |
378 | return -EINVAL; | |
379 | ||
380 | profile_number = ((struct koneplus_profile_buttons const *)buf)->number; | |
381 | profile_buttons = &koneplus->profile_buttons[profile_number]; | |
382 | ||
383 | mutex_lock(&koneplus->koneplus_lock); | |
384 | difference = memcmp(buf, profile_buttons, | |
385 | sizeof(struct koneplus_profile_buttons)); | |
386 | if (difference) { | |
387 | retval = koneplus_set_profile_buttons(usb_dev, | |
388 | (struct koneplus_profile_buttons const *)buf); | |
389 | if (!retval) | |
390 | memcpy(profile_buttons, buf, | |
391 | sizeof(struct koneplus_profile_buttons)); | |
392 | } | |
393 | mutex_unlock(&koneplus->koneplus_lock); | |
394 | ||
395 | if (retval) | |
396 | return retval; | |
397 | ||
398 | return sizeof(struct koneplus_profile_buttons); | |
399 | } | |
400 | ||
b50f315c | 401 | static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev, |
47dbdbff SA |
402 | struct device_attribute *attr, char *buf) |
403 | { | |
404 | struct koneplus_device *koneplus = | |
405 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
b50f315c | 406 | return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile); |
47dbdbff SA |
407 | } |
408 | ||
b50f315c | 409 | static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev, |
47dbdbff SA |
410 | struct device_attribute *attr, char const *buf, size_t size) |
411 | { | |
412 | struct koneplus_device *koneplus; | |
413 | struct usb_device *usb_dev; | |
414 | unsigned long profile; | |
415 | int retval; | |
b50f315c | 416 | struct koneplus_roccat_report roccat_report; |
47dbdbff SA |
417 | |
418 | dev = dev->parent->parent; | |
419 | koneplus = hid_get_drvdata(dev_get_drvdata(dev)); | |
420 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
421 | ||
422 | retval = strict_strtoul(buf, 10, &profile); | |
423 | if (retval) | |
424 | return retval; | |
425 | ||
426 | mutex_lock(&koneplus->koneplus_lock); | |
b50f315c SA |
427 | |
428 | retval = koneplus_set_actual_profile(usb_dev, profile); | |
429 | if (retval) { | |
430 | mutex_unlock(&koneplus->koneplus_lock); | |
47dbdbff | 431 | return retval; |
b50f315c | 432 | } |
47dbdbff | 433 | |
b50f315c | 434 | koneplus->actual_profile = profile; |
47dbdbff | 435 | |
b50f315c SA |
436 | roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE; |
437 | roccat_report.data1 = profile + 1; | |
438 | roccat_report.data2 = 0; | |
439 | roccat_report.profile = profile + 1; | |
440 | roccat_report_event(koneplus->chrdev_minor, | |
441 | (uint8_t const *)&roccat_report); | |
442 | ||
443 | mutex_unlock(&koneplus->koneplus_lock); | |
444 | ||
445 | return size; | |
47dbdbff SA |
446 | } |
447 | ||
448 | static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev, | |
449 | struct device_attribute *attr, char *buf) | |
450 | { | |
451 | struct koneplus_device *koneplus = | |
452 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
453 | return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version); | |
454 | } | |
455 | ||
456 | static struct device_attribute koneplus_attributes[] = { | |
b50f315c SA |
457 | __ATTR(actual_profile, 0660, |
458 | koneplus_sysfs_show_actual_profile, | |
459 | koneplus_sysfs_set_actual_profile), | |
47dbdbff | 460 | __ATTR(startup_profile, 0660, |
b50f315c SA |
461 | koneplus_sysfs_show_actual_profile, |
462 | koneplus_sysfs_set_actual_profile), | |
47dbdbff SA |
463 | __ATTR(firmware_version, 0440, |
464 | koneplus_sysfs_show_firmware_version, NULL), | |
465 | __ATTR_NULL | |
466 | }; | |
467 | ||
468 | static struct bin_attribute koneplus_bin_attributes[] = { | |
469 | { | |
61c29f5b | 470 | .attr = { .name = "sensor", .mode = 0660 }, |
47dbdbff SA |
471 | .size = sizeof(struct koneplus_sensor), |
472 | .read = koneplus_sysfs_read_sensor, | |
473 | .write = koneplus_sysfs_write_sensor | |
474 | }, | |
475 | { | |
476 | .attr = { .name = "tcu", .mode = 0220 }, | |
477 | .size = sizeof(struct koneplus_tcu), | |
478 | .write = koneplus_sysfs_write_tcu | |
479 | }, | |
480 | { | |
481 | .attr = { .name = "tcu_image", .mode = 0440 }, | |
482 | .size = sizeof(struct koneplus_tcu_image), | |
483 | .read = koneplus_sysfs_read_tcu_image | |
484 | }, | |
485 | { | |
486 | .attr = { .name = "profile_settings", .mode = 0220 }, | |
487 | .size = sizeof(struct koneplus_profile_settings), | |
488 | .write = koneplus_sysfs_write_profile_settings | |
489 | }, | |
490 | { | |
491 | .attr = { .name = "profile1_settings", .mode = 0440 }, | |
492 | .size = sizeof(struct koneplus_profile_settings), | |
493 | .read = koneplus_sysfs_read_profilex_settings, | |
494 | .private = &profile_numbers[0] | |
495 | }, | |
496 | { | |
497 | .attr = { .name = "profile2_settings", .mode = 0440 }, | |
498 | .size = sizeof(struct koneplus_profile_settings), | |
499 | .read = koneplus_sysfs_read_profilex_settings, | |
500 | .private = &profile_numbers[1] | |
501 | }, | |
502 | { | |
503 | .attr = { .name = "profile3_settings", .mode = 0440 }, | |
504 | .size = sizeof(struct koneplus_profile_settings), | |
505 | .read = koneplus_sysfs_read_profilex_settings, | |
506 | .private = &profile_numbers[2] | |
507 | }, | |
508 | { | |
509 | .attr = { .name = "profile4_settings", .mode = 0440 }, | |
510 | .size = sizeof(struct koneplus_profile_settings), | |
511 | .read = koneplus_sysfs_read_profilex_settings, | |
512 | .private = &profile_numbers[3] | |
513 | }, | |
514 | { | |
515 | .attr = { .name = "profile5_settings", .mode = 0440 }, | |
516 | .size = sizeof(struct koneplus_profile_settings), | |
517 | .read = koneplus_sysfs_read_profilex_settings, | |
518 | .private = &profile_numbers[4] | |
519 | }, | |
520 | { | |
521 | .attr = { .name = "profile_buttons", .mode = 0220 }, | |
522 | .size = sizeof(struct koneplus_profile_buttons), | |
523 | .write = koneplus_sysfs_write_profile_buttons | |
524 | }, | |
525 | { | |
526 | .attr = { .name = "profile1_buttons", .mode = 0440 }, | |
527 | .size = sizeof(struct koneplus_profile_buttons), | |
528 | .read = koneplus_sysfs_read_profilex_buttons, | |
529 | .private = &profile_numbers[0] | |
530 | }, | |
531 | { | |
532 | .attr = { .name = "profile2_buttons", .mode = 0440 }, | |
533 | .size = sizeof(struct koneplus_profile_buttons), | |
534 | .read = koneplus_sysfs_read_profilex_buttons, | |
535 | .private = &profile_numbers[1] | |
536 | }, | |
537 | { | |
538 | .attr = { .name = "profile3_buttons", .mode = 0440 }, | |
539 | .size = sizeof(struct koneplus_profile_buttons), | |
540 | .read = koneplus_sysfs_read_profilex_buttons, | |
541 | .private = &profile_numbers[2] | |
542 | }, | |
543 | { | |
544 | .attr = { .name = "profile4_buttons", .mode = 0440 }, | |
545 | .size = sizeof(struct koneplus_profile_buttons), | |
546 | .read = koneplus_sysfs_read_profilex_buttons, | |
547 | .private = &profile_numbers[3] | |
548 | }, | |
549 | { | |
550 | .attr = { .name = "profile5_buttons", .mode = 0440 }, | |
551 | .size = sizeof(struct koneplus_profile_buttons), | |
552 | .read = koneplus_sysfs_read_profilex_buttons, | |
553 | .private = &profile_numbers[4] | |
554 | }, | |
555 | { | |
556 | .attr = { .name = "macro", .mode = 0220 }, | |
557 | .size = sizeof(struct koneplus_macro), | |
558 | .write = koneplus_sysfs_write_macro | |
559 | }, | |
560 | __ATTR_NULL | |
561 | }; | |
562 | ||
563 | static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev, | |
564 | struct koneplus_device *koneplus) | |
565 | { | |
566 | int retval, i; | |
b50f315c | 567 | static uint wait = 200; |
47dbdbff SA |
568 | |
569 | mutex_init(&koneplus->koneplus_lock); | |
570 | ||
47dbdbff SA |
571 | retval = koneplus_get_info(usb_dev, &koneplus->info); |
572 | if (retval) | |
573 | return retval; | |
574 | ||
575 | for (i = 0; i < 5; ++i) { | |
576 | msleep(wait); | |
577 | retval = koneplus_get_profile_settings(usb_dev, | |
578 | &koneplus->profile_settings[i], i); | |
579 | if (retval) | |
580 | return retval; | |
581 | ||
582 | msleep(wait); | |
583 | retval = koneplus_get_profile_buttons(usb_dev, | |
584 | &koneplus->profile_buttons[i], i); | |
585 | if (retval) | |
586 | return retval; | |
587 | } | |
588 | ||
b50f315c SA |
589 | msleep(wait); |
590 | retval = koneplus_get_actual_profile(usb_dev); | |
591 | if (retval < 0) | |
592 | return retval; | |
593 | koneplus_profile_activated(koneplus, retval); | |
47dbdbff SA |
594 | |
595 | return 0; | |
596 | } | |
597 | ||
598 | static int koneplus_init_specials(struct hid_device *hdev) | |
599 | { | |
600 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
601 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
602 | struct koneplus_device *koneplus; | |
603 | int retval; | |
604 | ||
605 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
606 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
607 | ||
608 | koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL); | |
609 | if (!koneplus) { | |
a28764ef | 610 | hid_err(hdev, "can't alloc device descriptor\n"); |
47dbdbff SA |
611 | return -ENOMEM; |
612 | } | |
613 | hid_set_drvdata(hdev, koneplus); | |
614 | ||
615 | retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus); | |
616 | if (retval) { | |
a28764ef | 617 | hid_err(hdev, "couldn't init struct koneplus_device\n"); |
47dbdbff SA |
618 | goto exit_free; |
619 | } | |
620 | ||
8211e460 SA |
621 | retval = roccat_connect(koneplus_class, hdev, |
622 | sizeof(struct koneplus_roccat_report)); | |
47dbdbff | 623 | if (retval < 0) { |
a28764ef | 624 | hid_err(hdev, "couldn't init char dev\n"); |
47dbdbff SA |
625 | } else { |
626 | koneplus->chrdev_minor = retval; | |
627 | koneplus->roccat_claimed = 1; | |
628 | } | |
629 | } else { | |
630 | hid_set_drvdata(hdev, NULL); | |
631 | } | |
632 | ||
633 | return 0; | |
634 | exit_free: | |
635 | kfree(koneplus); | |
636 | return retval; | |
637 | } | |
638 | ||
639 | static void koneplus_remove_specials(struct hid_device *hdev) | |
640 | { | |
641 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
642 | struct koneplus_device *koneplus; | |
643 | ||
644 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
645 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
646 | koneplus = hid_get_drvdata(hdev); | |
647 | if (koneplus->roccat_claimed) | |
648 | roccat_disconnect(koneplus->chrdev_minor); | |
649 | kfree(koneplus); | |
650 | } | |
651 | } | |
652 | ||
653 | static int koneplus_probe(struct hid_device *hdev, | |
654 | const struct hid_device_id *id) | |
655 | { | |
656 | int retval; | |
657 | ||
658 | retval = hid_parse(hdev); | |
659 | if (retval) { | |
a28764ef | 660 | hid_err(hdev, "parse failed\n"); |
47dbdbff SA |
661 | goto exit; |
662 | } | |
663 | ||
664 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
665 | if (retval) { | |
a28764ef | 666 | hid_err(hdev, "hw start failed\n"); |
47dbdbff SA |
667 | goto exit; |
668 | } | |
669 | ||
670 | retval = koneplus_init_specials(hdev); | |
671 | if (retval) { | |
a28764ef | 672 | hid_err(hdev, "couldn't install mouse\n"); |
47dbdbff SA |
673 | goto exit_stop; |
674 | } | |
675 | ||
676 | return 0; | |
677 | ||
678 | exit_stop: | |
679 | hid_hw_stop(hdev); | |
680 | exit: | |
681 | return retval; | |
682 | } | |
683 | ||
684 | static void koneplus_remove(struct hid_device *hdev) | |
685 | { | |
686 | koneplus_remove_specials(hdev); | |
687 | hid_hw_stop(hdev); | |
688 | } | |
689 | ||
690 | static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus, | |
691 | u8 const *data) | |
692 | { | |
693 | struct koneplus_mouse_report_button const *button_report; | |
694 | ||
695 | switch (data[0]) { | |
696 | case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON: | |
697 | button_report = (struct koneplus_mouse_report_button const *)data; | |
698 | switch (button_report->type) { | |
699 | case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE: | |
700 | koneplus_profile_activated(koneplus, button_report->data1 - 1); | |
701 | break; | |
702 | } | |
703 | break; | |
704 | } | |
705 | } | |
706 | ||
707 | static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus, | |
708 | u8 const *data) | |
709 | { | |
710 | struct koneplus_roccat_report roccat_report; | |
711 | struct koneplus_mouse_report_button const *button_report; | |
712 | ||
713 | if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON) | |
714 | return; | |
715 | ||
716 | button_report = (struct koneplus_mouse_report_button const *)data; | |
717 | ||
718 | if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH || | |
719 | button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) && | |
720 | button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS) | |
721 | return; | |
722 | ||
723 | roccat_report.type = button_report->type; | |
724 | roccat_report.data1 = button_report->data1; | |
725 | roccat_report.data2 = button_report->data2; | |
726 | roccat_report.profile = koneplus->actual_profile + 1; | |
727 | roccat_report_event(koneplus->chrdev_minor, | |
8211e460 | 728 | (uint8_t const *)&roccat_report); |
47dbdbff SA |
729 | } |
730 | ||
731 | static int koneplus_raw_event(struct hid_device *hdev, | |
732 | struct hid_report *report, u8 *data, int size) | |
733 | { | |
734 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
735 | struct koneplus_device *koneplus = hid_get_drvdata(hdev); | |
736 | ||
737 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
738 | != USB_INTERFACE_PROTOCOL_MOUSE) | |
739 | return 0; | |
740 | ||
741 | koneplus_keep_values_up_to_date(koneplus, data); | |
742 | ||
743 | if (koneplus->roccat_claimed) | |
744 | koneplus_report_to_chrdev(koneplus, data); | |
745 | ||
746 | return 0; | |
747 | } | |
748 | ||
749 | static const struct hid_device_id koneplus_devices[] = { | |
750 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) }, | |
751 | { } | |
752 | }; | |
753 | ||
754 | MODULE_DEVICE_TABLE(hid, koneplus_devices); | |
755 | ||
756 | static struct hid_driver koneplus_driver = { | |
757 | .name = "koneplus", | |
758 | .id_table = koneplus_devices, | |
759 | .probe = koneplus_probe, | |
760 | .remove = koneplus_remove, | |
761 | .raw_event = koneplus_raw_event | |
762 | }; | |
763 | ||
764 | static int __init koneplus_init(void) | |
765 | { | |
766 | int retval; | |
767 | ||
768 | /* class name has to be same as driver name */ | |
769 | koneplus_class = class_create(THIS_MODULE, "koneplus"); | |
770 | if (IS_ERR(koneplus_class)) | |
771 | return PTR_ERR(koneplus_class); | |
772 | koneplus_class->dev_attrs = koneplus_attributes; | |
773 | koneplus_class->dev_bin_attrs = koneplus_bin_attributes; | |
774 | ||
775 | retval = hid_register_driver(&koneplus_driver); | |
776 | if (retval) | |
777 | class_destroy(koneplus_class); | |
778 | return retval; | |
779 | } | |
780 | ||
781 | static void __exit koneplus_exit(void) | |
782 | { | |
47dbdbff | 783 | hid_unregister_driver(&koneplus_driver); |
74b643da | 784 | class_destroy(koneplus_class); |
47dbdbff SA |
785 | } |
786 | ||
787 | module_init(koneplus_init); | |
788 | module_exit(koneplus_exit); | |
789 | ||
790 | MODULE_AUTHOR("Stefan Achatz"); | |
791 | MODULE_DESCRIPTION("USB Roccat Kone[+] driver"); | |
792 | MODULE_LICENSE("GPL v2"); |