blob: cf8eeb33a12571bf47c2c57118ff7935909cd865 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stefan Achatz8936aa32013-03-10 12:32:44 +01002/*
3 * Roccat KonePure driver for Linux
4 *
5 * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
6 */
7
8/*
Stefan Achatz8936aa32013-03-10 12:32:44 +01009 */
10
11/*
12 * Roccat KonePure is a smaller version of KoneXTD with less buttons and lights.
13 */
14
Stefan Achatz71304f52013-10-28 18:52:05 +010015#include <linux/types.h>
Stefan Achatz8936aa32013-03-10 12:32:44 +010016#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"
Stefan Achatz71304f52013-10-28 18:52:05 +010024
25enum {
26 KONEPURE_MOUSE_REPORT_NUMBER_BUTTON = 3,
27};
28
29struct 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;
Stefan Achatz8936aa32013-03-10 12:32:44 +010038
39static struct class *konepure_class;
40
Stefan Achatz71304f52013-10-28 18:52:05 +010041ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
42ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(actual_profile, 0x05, 0x03);
43ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_settings, 0x06, 0x1f);
44ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_buttons, 0x07, 0x3b);
45ROCCAT_COMMON2_BIN_ATTRIBUTE_W(macro, 0x08, 0x0822);
46ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x09, 0x06);
47ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(tcu, 0x0c, 0x04);
48ROCCAT_COMMON2_BIN_ATTRIBUTE_R(tcu_image, 0x0c, 0x0404);
49ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0x0f, 0x06);
50ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x10, 0x10);
Stefan Achatz8936aa32013-03-10 12:32:44 +010051
Stefan Achatz71304f52013-10-28 18:52:05 +010052static struct bin_attribute *konepure_bin_attrs[] = {
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -070053 &bin_attr_actual_profile,
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -070054 &bin_attr_control,
Stefan Achatz71304f52013-10-28 18:52:05 +010055 &bin_attr_info,
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -070056 &bin_attr_talk,
57 &bin_attr_macro,
Stefan Achatz71304f52013-10-28 18:52:05 +010058 &bin_attr_sensor,
59 &bin_attr_tcu,
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -070060 &bin_attr_tcu_image,
Stefan Achatz71304f52013-10-28 18:52:05 +010061 &bin_attr_profile_settings,
62 &bin_attr_profile_buttons,
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -070063 NULL,
Stefan Achatz8936aa32013-03-10 12:32:44 +010064};
65
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -070066static const struct attribute_group konepure_group = {
Stefan Achatz71304f52013-10-28 18:52:05 +010067 .bin_attrs = konepure_bin_attrs,
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -070068};
69
70static const struct attribute_group *konepure_groups[] = {
71 &konepure_group,
72 NULL,
73};
74
Stefan Achatz8936aa32013-03-10 12:32:44 +010075static int konepure_init_specials(struct hid_device *hdev)
76{
77 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
78 struct usb_device *usb_dev = interface_to_usbdev(intf);
Stefan Achatz71304f52013-10-28 18:52:05 +010079 struct roccat_common2_device *konepure;
Stefan Achatz8936aa32013-03-10 12:32:44 +010080 int retval;
81
82 if (intf->cur_altsetting->desc.bInterfaceProtocol
83 != USB_INTERFACE_PROTOCOL_MOUSE) {
84 hid_set_drvdata(hdev, NULL);
85 return 0;
86 }
87
88 konepure = kzalloc(sizeof(*konepure), GFP_KERNEL);
89 if (!konepure) {
90 hid_err(hdev, "can't alloc device descriptor\n");
91 return -ENOMEM;
92 }
93 hid_set_drvdata(hdev, konepure);
94
Stefan Achatz71304f52013-10-28 18:52:05 +010095 retval = roccat_common2_device_init_struct(usb_dev, konepure);
Stefan Achatz8936aa32013-03-10 12:32:44 +010096 if (retval) {
Stefan Achatz71304f52013-10-28 18:52:05 +010097 hid_err(hdev, "couldn't init KonePure device\n");
Stefan Achatz8936aa32013-03-10 12:32:44 +010098 goto exit_free;
99 }
100
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;
108 }
109
110 return 0;
111exit_free:
112 kfree(konepure);
113 return retval;
114}
115
116static void konepure_remove_specials(struct hid_device *hdev)
117{
118 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Stefan Achatz71304f52013-10-28 18:52:05 +0100119 struct roccat_common2_device *konepure;
Stefan Achatz8936aa32013-03-10 12:32:44 +0100120
121 if (intf->cur_altsetting->desc.bInterfaceProtocol
122 != USB_INTERFACE_PROTOCOL_MOUSE)
123 return;
124
125 konepure = hid_get_drvdata(hdev);
126 if (konepure->roccat_claimed)
127 roccat_disconnect(konepure->chrdev_minor);
128 kfree(konepure);
129}
130
131static int konepure_probe(struct hid_device *hdev,
132 const struct hid_device_id *id)
133{
134 int retval;
135
Greg Kroah-Hartman93020952021-12-01 19:35:03 +0100136 if (!hid_is_usb(hdev))
137 return -EINVAL;
138
Stefan Achatz8936aa32013-03-10 12:32:44 +0100139 retval = hid_parse(hdev);
140 if (retval) {
141 hid_err(hdev, "parse failed\n");
142 goto exit;
143 }
144
145 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
146 if (retval) {
147 hid_err(hdev, "hw start failed\n");
148 goto exit;
149 }
150
151 retval = konepure_init_specials(hdev);
152 if (retval) {
153 hid_err(hdev, "couldn't install mouse\n");
154 goto exit_stop;
155 }
156
157 return 0;
158
159exit_stop:
160 hid_hw_stop(hdev);
161exit:
162 return retval;
163}
164
165static void konepure_remove(struct hid_device *hdev)
166{
167 konepure_remove_specials(hdev);
168 hid_hw_stop(hdev);
169}
170
171static int konepure_raw_event(struct hid_device *hdev,
172 struct hid_report *report, u8 *data, int size)
173{
174 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Stefan Achatz71304f52013-10-28 18:52:05 +0100175 struct roccat_common2_device *konepure = hid_get_drvdata(hdev);
Stefan Achatz8936aa32013-03-10 12:32:44 +0100176
177 if (intf->cur_altsetting->desc.bInterfaceProtocol
178 != USB_INTERFACE_PROTOCOL_MOUSE)
179 return 0;
180
181 if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
182 return 0;
183
184 if (konepure != NULL && konepure->roccat_claimed)
185 roccat_report_event(konepure->chrdev_minor, data);
186
187 return 0;
188}
189
190static const struct hid_device_id konepure_devices[] = {
191 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
Stefan Achatza4be0ed2013-08-30 14:10:07 +0200192 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE_OPTICAL) },
Stefan Achatz8936aa32013-03-10 12:32:44 +0100193 { }
194};
195
196MODULE_DEVICE_TABLE(hid, konepure_devices);
197
198static struct hid_driver konepure_driver = {
199 .name = "konepure",
200 .id_table = konepure_devices,
201 .probe = konepure_probe,
202 .remove = konepure_remove,
203 .raw_event = konepure_raw_event
204};
205
206static int __init konepure_init(void)
207{
208 int retval;
209
210 konepure_class = class_create(THIS_MODULE, "konepure");
211 if (IS_ERR(konepure_class))
212 return PTR_ERR(konepure_class);
Greg Kroah-Hartman0f1947f2013-08-19 21:50:28 -0700213 konepure_class->dev_groups = konepure_groups;
Stefan Achatz8936aa32013-03-10 12:32:44 +0100214
215 retval = hid_register_driver(&konepure_driver);
216 if (retval)
217 class_destroy(konepure_class);
218 return retval;
219}
220
221static void __exit konepure_exit(void)
222{
223 hid_unregister_driver(&konepure_driver);
224 class_destroy(konepure_class);
225}
226
227module_init(konepure_init);
228module_exit(konepure_exit);
229
230MODULE_AUTHOR("Stefan Achatz");
Stefan Achatza4be0ed2013-08-30 14:10:07 +0200231MODULE_DESCRIPTION("USB Roccat KonePure/Optical driver");
Stefan Achatz8936aa32013-03-10 12:32:44 +0100232MODULE_LICENSE("GPL v2");