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