Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Roccat Ryos driver for Linux
0004  *
0005  * Copyright (c) 2013 Stefan Achatz <erazor_de@users.sourceforge.net>
0006  */
0007 
0008 /*
0009  */
0010 
0011 #include <linux/types.h>
0012 #include <linux/device.h>
0013 #include <linux/input.h>
0014 #include <linux/hid.h>
0015 #include <linux/module.h>
0016 #include <linux/slab.h>
0017 #include <linux/hid-roccat.h>
0018 #include "hid-ids.h"
0019 #include "hid-roccat-common.h"
0020 
0021 enum {
0022     RYOS_REPORT_NUMBER_SPECIAL = 3,
0023     RYOS_USB_INTERFACE_PROTOCOL = 0,
0024 };
0025 
0026 struct ryos_report_special {
0027     uint8_t number; /* RYOS_REPORT_NUMBER_SPECIAL */
0028     uint8_t data[4];
0029 } __packed;
0030 
0031 static struct class *ryos_class;
0032 
0033 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
0034 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x05, 0x03);
0035 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_primary, 0x06, 0x7d);
0036 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_function, 0x07, 0x5f);
0037 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_macro, 0x08, 0x23);
0038 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_thumbster, 0x09, 0x17);
0039 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_extra, 0x0a, 0x08);
0040 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_easyzone, 0x0b, 0x126);
0041 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(key_mask, 0x0c, 0x06);
0042 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light, 0x0d, 0x10);
0043 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x0e, 0x7d2);
0044 ROCCAT_COMMON2_BIN_ATTRIBUTE_R(info, 0x0f, 0x08);
0045 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(reset, 0x11, 0x03);
0046 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(light_control, 0x13, 0x08);
0047 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x16, 0x10);
0048 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(stored_lights, 0x17, 0x0566);
0049 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(custom_lights, 0x18, 0x14);
0050 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light_macro, 0x19, 0x07d2);
0051 
0052 static struct bin_attribute *ryos_bin_attrs[] = {
0053     &bin_attr_control,
0054     &bin_attr_profile,
0055     &bin_attr_keys_primary,
0056     &bin_attr_keys_function,
0057     &bin_attr_keys_macro,
0058     &bin_attr_keys_thumbster,
0059     &bin_attr_keys_extra,
0060     &bin_attr_keys_easyzone,
0061     &bin_attr_key_mask,
0062     &bin_attr_light,
0063     &bin_attr_macro,
0064     &bin_attr_info,
0065     &bin_attr_reset,
0066     &bin_attr_light_control,
0067     &bin_attr_talk,
0068     &bin_attr_stored_lights,
0069     &bin_attr_custom_lights,
0070     &bin_attr_light_macro,
0071     NULL,
0072 };
0073 
0074 static const struct attribute_group ryos_group = {
0075     .bin_attrs = ryos_bin_attrs,
0076 };
0077 
0078 static const struct attribute_group *ryos_groups[] = {
0079     &ryos_group,
0080     NULL,
0081 };
0082 
0083 static int ryos_init_specials(struct hid_device *hdev)
0084 {
0085     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0086     struct usb_device *usb_dev = interface_to_usbdev(intf);
0087     struct roccat_common2_device *ryos;
0088     int retval;
0089 
0090     if (intf->cur_altsetting->desc.bInterfaceProtocol
0091             != RYOS_USB_INTERFACE_PROTOCOL) {
0092         hid_set_drvdata(hdev, NULL);
0093         return 0;
0094     }
0095 
0096     ryos = kzalloc(sizeof(*ryos), GFP_KERNEL);
0097     if (!ryos) {
0098         hid_err(hdev, "can't alloc device descriptor\n");
0099         return -ENOMEM;
0100     }
0101     hid_set_drvdata(hdev, ryos);
0102 
0103     retval = roccat_common2_device_init_struct(usb_dev, ryos);
0104     if (retval) {
0105         hid_err(hdev, "couldn't init Ryos device\n");
0106         goto exit_free;
0107     }
0108 
0109     retval = roccat_connect(ryos_class, hdev,
0110             sizeof(struct ryos_report_special));
0111     if (retval < 0) {
0112         hid_err(hdev, "couldn't init char dev\n");
0113     } else {
0114         ryos->chrdev_minor = retval;
0115         ryos->roccat_claimed = 1;
0116     }
0117 
0118     return 0;
0119 exit_free:
0120     kfree(ryos);
0121     return retval;
0122 }
0123 
0124 static void ryos_remove_specials(struct hid_device *hdev)
0125 {
0126     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0127     struct roccat_common2_device *ryos;
0128 
0129     if (intf->cur_altsetting->desc.bInterfaceProtocol
0130             != RYOS_USB_INTERFACE_PROTOCOL)
0131         return;
0132 
0133     ryos = hid_get_drvdata(hdev);
0134     if (ryos->roccat_claimed)
0135         roccat_disconnect(ryos->chrdev_minor);
0136     kfree(ryos);
0137 }
0138 
0139 static int ryos_probe(struct hid_device *hdev,
0140         const struct hid_device_id *id)
0141 {
0142     int retval;
0143 
0144     if (!hid_is_usb(hdev))
0145         return -EINVAL;
0146 
0147     retval = hid_parse(hdev);
0148     if (retval) {
0149         hid_err(hdev, "parse failed\n");
0150         goto exit;
0151     }
0152 
0153     retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0154     if (retval) {
0155         hid_err(hdev, "hw start failed\n");
0156         goto exit;
0157     }
0158 
0159     retval = ryos_init_specials(hdev);
0160     if (retval) {
0161         hid_err(hdev, "couldn't install mouse\n");
0162         goto exit_stop;
0163     }
0164 
0165     return 0;
0166 
0167 exit_stop:
0168     hid_hw_stop(hdev);
0169 exit:
0170     return retval;
0171 }
0172 
0173 static void ryos_remove(struct hid_device *hdev)
0174 {
0175     ryos_remove_specials(hdev);
0176     hid_hw_stop(hdev);
0177 }
0178 
0179 static int ryos_raw_event(struct hid_device *hdev,
0180         struct hid_report *report, u8 *data, int size)
0181 {
0182     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0183     struct roccat_common2_device *ryos = hid_get_drvdata(hdev);
0184 
0185     if (intf->cur_altsetting->desc.bInterfaceProtocol
0186             != RYOS_USB_INTERFACE_PROTOCOL)
0187         return 0;
0188 
0189     if (data[0] != RYOS_REPORT_NUMBER_SPECIAL)
0190         return 0;
0191 
0192     if (ryos != NULL && ryos->roccat_claimed)
0193         roccat_report_event(ryos->chrdev_minor, data);
0194 
0195     return 0;
0196 }
0197 
0198 static const struct hid_device_id ryos_devices[] = {
0199     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK) },
0200     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_GLOW) },
0201     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_PRO) },
0202     { }
0203 };
0204 
0205 MODULE_DEVICE_TABLE(hid, ryos_devices);
0206 
0207 static struct hid_driver ryos_driver = {
0208         .name = "ryos",
0209         .id_table = ryos_devices,
0210         .probe = ryos_probe,
0211         .remove = ryos_remove,
0212         .raw_event = ryos_raw_event
0213 };
0214 
0215 static int __init ryos_init(void)
0216 {
0217     int retval;
0218 
0219     ryos_class = class_create(THIS_MODULE, "ryos");
0220     if (IS_ERR(ryos_class))
0221         return PTR_ERR(ryos_class);
0222     ryos_class->dev_groups = ryos_groups;
0223 
0224     retval = hid_register_driver(&ryos_driver);
0225     if (retval)
0226         class_destroy(ryos_class);
0227     return retval;
0228 }
0229 
0230 static void __exit ryos_exit(void)
0231 {
0232     hid_unregister_driver(&ryos_driver);
0233     class_destroy(ryos_class);
0234 }
0235 
0236 module_init(ryos_init);
0237 module_exit(ryos_exit);
0238 
0239 MODULE_AUTHOR("Stefan Achatz");
0240 MODULE_DESCRIPTION("USB Roccat Ryos MK/Glow/Pro driver");
0241 MODULE_LICENSE("GPL v2");