Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Roccat KonePure driver for Linux |
| 4 | * |
| 5 | * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net> |
| 6 | */ |
| 7 | |
| 8 | /* |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Roccat KonePure is a smaller version of KoneXTD with less buttons and lights. |
| 13 | */ |
| 14 | |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 15 | #include <linux/types.h> |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 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" |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 24 | |
| 25 | enum { |
| 26 | KONEPURE_MOUSE_REPORT_NUMBER_BUTTON = 3, |
| 27 | }; |
| 28 | |
| 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; |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 38 | |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 39 | ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03); |
| 40 | ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(actual_profile, 0x05, 0x03); |
| 41 | ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_settings, 0x06, 0x1f); |
| 42 | ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_buttons, 0x07, 0x3b); |
| 43 | ROCCAT_COMMON2_BIN_ATTRIBUTE_W(macro, 0x08, 0x0822); |
| 44 | ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x09, 0x06); |
| 45 | ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(tcu, 0x0c, 0x04); |
| 46 | ROCCAT_COMMON2_BIN_ATTRIBUTE_R(tcu_image, 0x0c, 0x0404); |
| 47 | ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0x0f, 0x06); |
| 48 | ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x10, 0x10); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 49 | |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 50 | static struct bin_attribute *konepure_bin_attrs[] = { |
Greg Kroah-Hartman | 0f1947f | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 51 | &bin_attr_actual_profile, |
Greg Kroah-Hartman | 0f1947f | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 52 | &bin_attr_control, |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 53 | &bin_attr_info, |
Greg Kroah-Hartman | 0f1947f | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 54 | &bin_attr_talk, |
| 55 | &bin_attr_macro, |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 56 | &bin_attr_sensor, |
| 57 | &bin_attr_tcu, |
Greg Kroah-Hartman | 0f1947f | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 58 | &bin_attr_tcu_image, |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 59 | &bin_attr_profile_settings, |
| 60 | &bin_attr_profile_buttons, |
Greg Kroah-Hartman | 0f1947f | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 61 | NULL, |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Greg Kroah-Hartman | 0f1947f | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 64 | static const struct attribute_group konepure_group = { |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 65 | .bin_attrs = konepure_bin_attrs, |
Greg Kroah-Hartman | 0f1947f | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static const struct attribute_group *konepure_groups[] = { |
| 69 | &konepure_group, |
| 70 | NULL, |
| 71 | }; |
| 72 | |
Ivan Orlov | afdf5dd | 2023-06-20 20:31:42 +0200 | [diff] [blame] | 73 | static const struct class konepure_class = { |
| 74 | .name = "konepure", |
| 75 | .dev_groups = konepure_groups, |
| 76 | }; |
| 77 | |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 78 | static int konepure_init_specials(struct hid_device *hdev) |
| 79 | { |
| 80 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 81 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 82 | struct roccat_common2_device *konepure; |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 83 | int retval; |
| 84 | |
| 85 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 86 | != USB_INTERFACE_PROTOCOL_MOUSE) { |
| 87 | hid_set_drvdata(hdev, NULL); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | konepure = kzalloc(sizeof(*konepure), GFP_KERNEL); |
| 92 | if (!konepure) { |
| 93 | hid_err(hdev, "can't alloc device descriptor\n"); |
| 94 | return -ENOMEM; |
| 95 | } |
| 96 | hid_set_drvdata(hdev, konepure); |
| 97 | |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 98 | retval = roccat_common2_device_init_struct(usb_dev, konepure); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 99 | if (retval) { |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 100 | hid_err(hdev, "couldn't init KonePure device\n"); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 101 | goto exit_free; |
| 102 | } |
| 103 | |
Ivan Orlov | afdf5dd | 2023-06-20 20:31:42 +0200 | [diff] [blame] | 104 | retval = roccat_connect(&konepure_class, hdev, |
| 105 | sizeof(struct konepure_mouse_report_button)); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 106 | if (retval < 0) { |
| 107 | hid_err(hdev, "couldn't init char dev\n"); |
| 108 | } else { |
| 109 | konepure->chrdev_minor = retval; |
| 110 | konepure->roccat_claimed = 1; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | exit_free: |
| 115 | kfree(konepure); |
| 116 | return retval; |
| 117 | } |
| 118 | |
| 119 | static void konepure_remove_specials(struct hid_device *hdev) |
| 120 | { |
| 121 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 122 | struct roccat_common2_device *konepure; |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 123 | |
| 124 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 125 | != USB_INTERFACE_PROTOCOL_MOUSE) |
| 126 | return; |
| 127 | |
| 128 | konepure = hid_get_drvdata(hdev); |
| 129 | if (konepure->roccat_claimed) |
| 130 | roccat_disconnect(konepure->chrdev_minor); |
| 131 | kfree(konepure); |
| 132 | } |
| 133 | |
| 134 | static int konepure_probe(struct hid_device *hdev, |
| 135 | const struct hid_device_id *id) |
| 136 | { |
| 137 | int retval; |
| 138 | |
Greg Kroah-Hartman | 9302095 | 2021-12-01 19:35:03 +0100 | [diff] [blame] | 139 | if (!hid_is_usb(hdev)) |
| 140 | return -EINVAL; |
| 141 | |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 142 | retval = hid_parse(hdev); |
| 143 | if (retval) { |
| 144 | hid_err(hdev, "parse failed\n"); |
| 145 | goto exit; |
| 146 | } |
| 147 | |
| 148 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 149 | if (retval) { |
| 150 | hid_err(hdev, "hw start failed\n"); |
| 151 | goto exit; |
| 152 | } |
| 153 | |
| 154 | retval = konepure_init_specials(hdev); |
| 155 | if (retval) { |
| 156 | hid_err(hdev, "couldn't install mouse\n"); |
| 157 | goto exit_stop; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | |
| 162 | exit_stop: |
| 163 | hid_hw_stop(hdev); |
| 164 | exit: |
| 165 | return retval; |
| 166 | } |
| 167 | |
| 168 | static void konepure_remove(struct hid_device *hdev) |
| 169 | { |
| 170 | konepure_remove_specials(hdev); |
| 171 | hid_hw_stop(hdev); |
| 172 | } |
| 173 | |
| 174 | static int konepure_raw_event(struct hid_device *hdev, |
| 175 | struct hid_report *report, u8 *data, int size) |
| 176 | { |
| 177 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 178 | struct roccat_common2_device *konepure = hid_get_drvdata(hdev); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 179 | |
| 180 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 181 | != USB_INTERFACE_PROTOCOL_MOUSE) |
| 182 | return 0; |
| 183 | |
| 184 | if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON) |
| 185 | return 0; |
| 186 | |
| 187 | if (konepure != NULL && konepure->roccat_claimed) |
| 188 | roccat_report_event(konepure->chrdev_minor, data); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static const struct hid_device_id konepure_devices[] = { |
| 194 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) }, |
Stefan Achatz | a4be0ed | 2013-08-30 14:10:07 +0200 | [diff] [blame] | 195 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE_OPTICAL) }, |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 196 | { } |
| 197 | }; |
| 198 | |
| 199 | MODULE_DEVICE_TABLE(hid, konepure_devices); |
| 200 | |
| 201 | static struct hid_driver konepure_driver = { |
| 202 | .name = "konepure", |
| 203 | .id_table = konepure_devices, |
| 204 | .probe = konepure_probe, |
| 205 | .remove = konepure_remove, |
| 206 | .raw_event = konepure_raw_event |
| 207 | }; |
| 208 | |
| 209 | static int __init konepure_init(void) |
| 210 | { |
| 211 | int retval; |
| 212 | |
Ivan Orlov | afdf5dd | 2023-06-20 20:31:42 +0200 | [diff] [blame] | 213 | retval = class_register(&konepure_class); |
| 214 | if (retval) |
| 215 | return retval; |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 216 | |
| 217 | retval = hid_register_driver(&konepure_driver); |
| 218 | if (retval) |
Ivan Orlov | afdf5dd | 2023-06-20 20:31:42 +0200 | [diff] [blame] | 219 | class_unregister(&konepure_class); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 220 | return retval; |
| 221 | } |
| 222 | |
| 223 | static void __exit konepure_exit(void) |
| 224 | { |
| 225 | hid_unregister_driver(&konepure_driver); |
Ivan Orlov | afdf5dd | 2023-06-20 20:31:42 +0200 | [diff] [blame] | 226 | class_unregister(&konepure_class); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | module_init(konepure_init); |
| 230 | module_exit(konepure_exit); |
| 231 | |
| 232 | MODULE_AUTHOR("Stefan Achatz"); |
Stefan Achatz | a4be0ed | 2013-08-30 14:10:07 +0200 | [diff] [blame] | 233 | MODULE_DESCRIPTION("USB Roccat KonePure/Optical driver"); |
Stefan Achatz | 8936aa3 | 2013-03-10 12:32:44 +0100 | [diff] [blame] | 234 | MODULE_LICENSE("GPL v2"); |