Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Roccat KonePure driver for Linux
0004  *
0005  * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
0006  */
0007 
0008 /*
0009  */
0010 
0011 /*
0012  * Roccat KonePure is a smaller version of KoneXTD with less buttons and lights.
0013  */
0014 
0015 #include <linux/types.h>
0016 #include <linux/device.h>
0017 #include <linux/input.h>
0018 #include <linux/hid.h>
0019 #include <linux/module.h>
0020 #include <linux/slab.h>
0021 #include <linux/hid-roccat.h>
0022 #include "hid-ids.h"
0023 #include "hid-roccat-common.h"
0024 
0025 enum {
0026     KONEPURE_MOUSE_REPORT_NUMBER_BUTTON = 3,
0027 };
0028 
0029 struct konepure_mouse_report_button {
0030     uint8_t report_number; /* always KONEPURE_MOUSE_REPORT_NUMBER_BUTTON */
0031     uint8_t zero;
0032     uint8_t type;
0033     uint8_t data1;
0034     uint8_t data2;
0035     uint8_t zero2;
0036     uint8_t unknown[2];
0037 } __packed;
0038 
0039 static struct class *konepure_class;
0040 
0041 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
0042 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(actual_profile, 0x05, 0x03);
0043 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_settings, 0x06, 0x1f);
0044 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_buttons, 0x07, 0x3b);
0045 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(macro, 0x08, 0x0822);
0046 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x09, 0x06);
0047 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(tcu, 0x0c, 0x04);
0048 ROCCAT_COMMON2_BIN_ATTRIBUTE_R(tcu_image, 0x0c, 0x0404);
0049 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0x0f, 0x06);
0050 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x10, 0x10);
0051 
0052 static struct bin_attribute *konepure_bin_attrs[] = {
0053     &bin_attr_actual_profile,
0054     &bin_attr_control,
0055     &bin_attr_info,
0056     &bin_attr_talk,
0057     &bin_attr_macro,
0058     &bin_attr_sensor,
0059     &bin_attr_tcu,
0060     &bin_attr_tcu_image,
0061     &bin_attr_profile_settings,
0062     &bin_attr_profile_buttons,
0063     NULL,
0064 };
0065 
0066 static const struct attribute_group konepure_group = {
0067     .bin_attrs = konepure_bin_attrs,
0068 };
0069 
0070 static const struct attribute_group *konepure_groups[] = {
0071     &konepure_group,
0072     NULL,
0073 };
0074 
0075 static int konepure_init_specials(struct hid_device *hdev)
0076 {
0077     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0078     struct usb_device *usb_dev = interface_to_usbdev(intf);
0079     struct roccat_common2_device *konepure;
0080     int retval;
0081 
0082     if (intf->cur_altsetting->desc.bInterfaceProtocol
0083             != USB_INTERFACE_PROTOCOL_MOUSE) {
0084         hid_set_drvdata(hdev, NULL);
0085         return 0;
0086     }
0087 
0088     konepure = kzalloc(sizeof(*konepure), GFP_KERNEL);
0089     if (!konepure) {
0090         hid_err(hdev, "can't alloc device descriptor\n");
0091         return -ENOMEM;
0092     }
0093     hid_set_drvdata(hdev, konepure);
0094 
0095     retval = roccat_common2_device_init_struct(usb_dev, konepure);
0096     if (retval) {
0097         hid_err(hdev, "couldn't init KonePure device\n");
0098         goto exit_free;
0099     }
0100 
0101     retval = roccat_connect(konepure_class, hdev,
0102             sizeof(struct konepure_mouse_report_button));
0103     if (retval < 0) {
0104         hid_err(hdev, "couldn't init char dev\n");
0105     } else {
0106         konepure->chrdev_minor = retval;
0107         konepure->roccat_claimed = 1;
0108     }
0109 
0110     return 0;
0111 exit_free:
0112     kfree(konepure);
0113     return retval;
0114 }
0115 
0116 static void konepure_remove_specials(struct hid_device *hdev)
0117 {
0118     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0119     struct roccat_common2_device *konepure;
0120 
0121     if (intf->cur_altsetting->desc.bInterfaceProtocol
0122             != USB_INTERFACE_PROTOCOL_MOUSE)
0123         return;
0124 
0125     konepure = hid_get_drvdata(hdev);
0126     if (konepure->roccat_claimed)
0127         roccat_disconnect(konepure->chrdev_minor);
0128     kfree(konepure);
0129 }
0130 
0131 static int konepure_probe(struct hid_device *hdev,
0132         const struct hid_device_id *id)
0133 {
0134     int retval;
0135 
0136     if (!hid_is_usb(hdev))
0137         return -EINVAL;
0138 
0139     retval = hid_parse(hdev);
0140     if (retval) {
0141         hid_err(hdev, "parse failed\n");
0142         goto exit;
0143     }
0144 
0145     retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0146     if (retval) {
0147         hid_err(hdev, "hw start failed\n");
0148         goto exit;
0149     }
0150 
0151     retval = konepure_init_specials(hdev);
0152     if (retval) {
0153         hid_err(hdev, "couldn't install mouse\n");
0154         goto exit_stop;
0155     }
0156 
0157     return 0;
0158 
0159 exit_stop:
0160     hid_hw_stop(hdev);
0161 exit:
0162     return retval;
0163 }
0164 
0165 static void konepure_remove(struct hid_device *hdev)
0166 {
0167     konepure_remove_specials(hdev);
0168     hid_hw_stop(hdev);
0169 }
0170 
0171 static int konepure_raw_event(struct hid_device *hdev,
0172         struct hid_report *report, u8 *data, int size)
0173 {
0174     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0175     struct roccat_common2_device *konepure = hid_get_drvdata(hdev);
0176 
0177     if (intf->cur_altsetting->desc.bInterfaceProtocol
0178             != USB_INTERFACE_PROTOCOL_MOUSE)
0179         return 0;
0180 
0181     if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
0182         return 0;
0183 
0184     if (konepure != NULL && konepure->roccat_claimed)
0185         roccat_report_event(konepure->chrdev_minor, data);
0186 
0187     return 0;
0188 }
0189 
0190 static const struct hid_device_id konepure_devices[] = {
0191     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
0192     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE_OPTICAL) },
0193     { }
0194 };
0195 
0196 MODULE_DEVICE_TABLE(hid, konepure_devices);
0197 
0198 static struct hid_driver konepure_driver = {
0199         .name = "konepure",
0200         .id_table = konepure_devices,
0201         .probe = konepure_probe,
0202         .remove = konepure_remove,
0203         .raw_event = konepure_raw_event
0204 };
0205 
0206 static int __init konepure_init(void)
0207 {
0208     int retval;
0209 
0210     konepure_class = class_create(THIS_MODULE, "konepure");
0211     if (IS_ERR(konepure_class))
0212         return PTR_ERR(konepure_class);
0213     konepure_class->dev_groups = konepure_groups;
0214 
0215     retval = hid_register_driver(&konepure_driver);
0216     if (retval)
0217         class_destroy(konepure_class);
0218     return retval;
0219 }
0220 
0221 static void __exit konepure_exit(void)
0222 {
0223     hid_unregister_driver(&konepure_driver);
0224     class_destroy(konepure_class);
0225 }
0226 
0227 module_init(konepure_init);
0228 module_exit(konepure_exit);
0229 
0230 MODULE_AUTHOR("Stefan Achatz");
0231 MODULE_DESCRIPTION("USB Roccat KonePure/Optical driver");
0232 MODULE_LICENSE("GPL v2");