Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / hid / hid-roccat-konepure.c
blob5248b3c7cf7859abbd62dab6fed53e88d1b37773
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Roccat KonePure driver for Linux
5 * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
6 */
8 /*
9 */
12 * Roccat KonePure is a smaller version of KoneXTD with less buttons and lights.
15 #include <linux/types.h>
16 #include <linux/device.h>
17 #include <linux/input.h>
18 #include <linux/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/hid-roccat.h>
22 #include "hid-ids.h"
23 #include "hid-roccat-common.h"
25 enum {
26 KONEPURE_MOUSE_REPORT_NUMBER_BUTTON = 3,
29 struct konepure_mouse_report_button {
30 uint8_t report_number; /* always KONEPURE_MOUSE_REPORT_NUMBER_BUTTON */
31 uint8_t zero;
32 uint8_t type;
33 uint8_t data1;
34 uint8_t data2;
35 uint8_t zero2;
36 uint8_t unknown[2];
37 } __packed;
39 static struct class *konepure_class;
41 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
42 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(actual_profile, 0x05, 0x03);
43 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_settings, 0x06, 0x1f);
44 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_buttons, 0x07, 0x3b);
45 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(macro, 0x08, 0x0822);
46 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x09, 0x06);
47 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(tcu, 0x0c, 0x04);
48 ROCCAT_COMMON2_BIN_ATTRIBUTE_R(tcu_image, 0x0c, 0x0404);
49 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0x0f, 0x06);
50 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x10, 0x10);
52 static struct bin_attribute *konepure_bin_attrs[] = {
53 &bin_attr_actual_profile,
54 &bin_attr_control,
55 &bin_attr_info,
56 &bin_attr_talk,
57 &bin_attr_macro,
58 &bin_attr_sensor,
59 &bin_attr_tcu,
60 &bin_attr_tcu_image,
61 &bin_attr_profile_settings,
62 &bin_attr_profile_buttons,
63 NULL,
66 static const struct attribute_group konepure_group = {
67 .bin_attrs = konepure_bin_attrs,
70 static const struct attribute_group *konepure_groups[] = {
71 &konepure_group,
72 NULL,
75 static int konepure_init_specials(struct hid_device *hdev)
77 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
78 struct usb_device *usb_dev = interface_to_usbdev(intf);
79 struct roccat_common2_device *konepure;
80 int retval;
82 if (intf->cur_altsetting->desc.bInterfaceProtocol
83 != USB_INTERFACE_PROTOCOL_MOUSE) {
84 hid_set_drvdata(hdev, NULL);
85 return 0;
88 konepure = kzalloc(sizeof(*konepure), GFP_KERNEL);
89 if (!konepure) {
90 hid_err(hdev, "can't alloc device descriptor\n");
91 return -ENOMEM;
93 hid_set_drvdata(hdev, konepure);
95 retval = roccat_common2_device_init_struct(usb_dev, konepure);
96 if (retval) {
97 hid_err(hdev, "couldn't init KonePure device\n");
98 goto exit_free;
101 retval = roccat_connect(konepure_class, hdev,
102 sizeof(struct konepure_mouse_report_button));
103 if (retval < 0) {
104 hid_err(hdev, "couldn't init char dev\n");
105 } else {
106 konepure->chrdev_minor = retval;
107 konepure->roccat_claimed = 1;
110 return 0;
111 exit_free:
112 kfree(konepure);
113 return retval;
116 static void konepure_remove_specials(struct hid_device *hdev)
118 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
119 struct roccat_common2_device *konepure;
121 if (intf->cur_altsetting->desc.bInterfaceProtocol
122 != USB_INTERFACE_PROTOCOL_MOUSE)
123 return;
125 konepure = hid_get_drvdata(hdev);
126 if (konepure->roccat_claimed)
127 roccat_disconnect(konepure->chrdev_minor);
128 kfree(konepure);
131 static int konepure_probe(struct hid_device *hdev,
132 const struct hid_device_id *id)
134 int retval;
136 retval = hid_parse(hdev);
137 if (retval) {
138 hid_err(hdev, "parse failed\n");
139 goto exit;
142 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
143 if (retval) {
144 hid_err(hdev, "hw start failed\n");
145 goto exit;
148 retval = konepure_init_specials(hdev);
149 if (retval) {
150 hid_err(hdev, "couldn't install mouse\n");
151 goto exit_stop;
154 return 0;
156 exit_stop:
157 hid_hw_stop(hdev);
158 exit:
159 return retval;
162 static void konepure_remove(struct hid_device *hdev)
164 konepure_remove_specials(hdev);
165 hid_hw_stop(hdev);
168 static int konepure_raw_event(struct hid_device *hdev,
169 struct hid_report *report, u8 *data, int size)
171 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
172 struct roccat_common2_device *konepure = hid_get_drvdata(hdev);
174 if (intf->cur_altsetting->desc.bInterfaceProtocol
175 != USB_INTERFACE_PROTOCOL_MOUSE)
176 return 0;
178 if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
179 return 0;
181 if (konepure != NULL && konepure->roccat_claimed)
182 roccat_report_event(konepure->chrdev_minor, data);
184 return 0;
187 static const struct hid_device_id konepure_devices[] = {
188 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
189 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE_OPTICAL) },
193 MODULE_DEVICE_TABLE(hid, konepure_devices);
195 static struct hid_driver konepure_driver = {
196 .name = "konepure",
197 .id_table = konepure_devices,
198 .probe = konepure_probe,
199 .remove = konepure_remove,
200 .raw_event = konepure_raw_event
203 static int __init konepure_init(void)
205 int retval;
207 konepure_class = class_create(THIS_MODULE, "konepure");
208 if (IS_ERR(konepure_class))
209 return PTR_ERR(konepure_class);
210 konepure_class->dev_groups = konepure_groups;
212 retval = hid_register_driver(&konepure_driver);
213 if (retval)
214 class_destroy(konepure_class);
215 return retval;
218 static void __exit konepure_exit(void)
220 hid_unregister_driver(&konepure_driver);
221 class_destroy(konepure_class);
224 module_init(konepure_init);
225 module_exit(konepure_exit);
227 MODULE_AUTHOR("Stefan Achatz");
228 MODULE_DESCRIPTION("USB Roccat KonePure/Optical driver");
229 MODULE_LICENSE("GPL v2");