Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Roccat Kone[+] driver for Linux
0004  *
0005  * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
0006  */
0007 
0008 /*
0009  */
0010 
0011 /*
0012  * Roccat Kone[+] is an updated/improved version of the Kone with more memory
0013  * and functionality and without the non-standard behaviours the Kone had.
0014  * KoneXTD has same capabilities but updated sensor.
0015  */
0016 
0017 #include <linux/device.h>
0018 #include <linux/input.h>
0019 #include <linux/hid.h>
0020 #include <linux/module.h>
0021 #include <linux/slab.h>
0022 #include <linux/hid-roccat.h>
0023 #include "hid-ids.h"
0024 #include "hid-roccat-common.h"
0025 #include "hid-roccat-koneplus.h"
0026 
0027 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
0028 
0029 static struct class *koneplus_class;
0030 
0031 static void koneplus_profile_activated(struct koneplus_device *koneplus,
0032         uint new_profile)
0033 {
0034     koneplus->actual_profile = new_profile;
0035 }
0036 
0037 static int koneplus_send_control(struct usb_device *usb_dev, uint value,
0038         enum koneplus_control_requests request)
0039 {
0040     struct roccat_common2_control control;
0041 
0042     if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
0043             request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
0044             value > 4)
0045         return -EINVAL;
0046 
0047     control.command = ROCCAT_COMMON_COMMAND_CONTROL;
0048     control.value = value;
0049     control.request = request;
0050 
0051     return roccat_common2_send_with_status(usb_dev,
0052             ROCCAT_COMMON_COMMAND_CONTROL,
0053             &control, sizeof(struct roccat_common2_control));
0054 }
0055 
0056 
0057 /* retval is 0-4 on success, < 0 on error */
0058 static int koneplus_get_actual_profile(struct usb_device *usb_dev)
0059 {
0060     struct koneplus_actual_profile buf;
0061     int retval;
0062 
0063     retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
0064             &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
0065 
0066     return retval ? retval : buf.actual_profile;
0067 }
0068 
0069 static int koneplus_set_actual_profile(struct usb_device *usb_dev,
0070         int new_profile)
0071 {
0072     struct koneplus_actual_profile buf;
0073 
0074     buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
0075     buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
0076     buf.actual_profile = new_profile;
0077 
0078     return roccat_common2_send_with_status(usb_dev,
0079             KONEPLUS_COMMAND_ACTUAL_PROFILE,
0080             &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
0081 }
0082 
0083 static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
0084         char *buf, loff_t off, size_t count,
0085         size_t real_size, uint command)
0086 {
0087     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0088     struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
0089     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0090     int retval;
0091 
0092     if (off >= real_size)
0093         return 0;
0094 
0095     if (off != 0 || count != real_size)
0096         return -EINVAL;
0097 
0098     mutex_lock(&koneplus->koneplus_lock);
0099     retval = roccat_common2_receive(usb_dev, command, buf, real_size);
0100     mutex_unlock(&koneplus->koneplus_lock);
0101 
0102     if (retval)
0103         return retval;
0104 
0105     return real_size;
0106 }
0107 
0108 static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
0109         void const *buf, loff_t off, size_t count,
0110         size_t real_size, uint command)
0111 {
0112     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0113     struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
0114     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0115     int retval;
0116 
0117     if (off != 0 || count != real_size)
0118         return -EINVAL;
0119 
0120     mutex_lock(&koneplus->koneplus_lock);
0121     retval = roccat_common2_send_with_status(usb_dev, command,
0122             buf, real_size);
0123     mutex_unlock(&koneplus->koneplus_lock);
0124 
0125     if (retval)
0126         return retval;
0127 
0128     return real_size;
0129 }
0130 
0131 #define KONEPLUS_SYSFS_W(thingy, THINGY) \
0132 static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
0133         struct kobject *kobj, struct bin_attribute *attr, char *buf, \
0134         loff_t off, size_t count) \
0135 { \
0136     return koneplus_sysfs_write(fp, kobj, buf, off, count, \
0137             KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
0138 }
0139 
0140 #define KONEPLUS_SYSFS_R(thingy, THINGY) \
0141 static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
0142         struct kobject *kobj, struct bin_attribute *attr, char *buf, \
0143         loff_t off, size_t count) \
0144 { \
0145     return koneplus_sysfs_read(fp, kobj, buf, off, count, \
0146             KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
0147 }
0148 
0149 #define KONEPLUS_SYSFS_RW(thingy, THINGY) \
0150 KONEPLUS_SYSFS_W(thingy, THINGY) \
0151 KONEPLUS_SYSFS_R(thingy, THINGY)
0152 
0153 #define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
0154 KONEPLUS_SYSFS_RW(thingy, THINGY); \
0155 static struct bin_attribute bin_attr_##thingy = { \
0156     .attr = { .name = #thingy, .mode = 0660 }, \
0157     .size = KONEPLUS_SIZE_ ## THINGY, \
0158     .read = koneplus_sysfs_read_ ## thingy, \
0159     .write = koneplus_sysfs_write_ ## thingy \
0160 }
0161 
0162 #define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
0163 KONEPLUS_SYSFS_R(thingy, THINGY); \
0164 static struct bin_attribute bin_attr_##thingy = { \
0165     .attr = { .name = #thingy, .mode = 0440 }, \
0166     .size = KONEPLUS_SIZE_ ## THINGY, \
0167     .read = koneplus_sysfs_read_ ## thingy, \
0168 }
0169 
0170 #define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
0171 KONEPLUS_SYSFS_W(thingy, THINGY); \
0172 static struct bin_attribute bin_attr_##thingy = { \
0173     .attr = { .name = #thingy, .mode = 0220 }, \
0174     .size = KONEPLUS_SIZE_ ## THINGY, \
0175     .write = koneplus_sysfs_write_ ## thingy \
0176 }
0177 KONEPLUS_BIN_ATTRIBUTE_W(control, CONTROL);
0178 KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK);
0179 KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO);
0180 KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
0181 KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO);
0182 KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR);
0183 KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU);
0184 KONEPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
0185 KONEPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
0186 
0187 static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
0188         struct kobject *kobj, struct bin_attribute *attr, char *buf,
0189         loff_t off, size_t count)
0190 {
0191     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0192     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0193     ssize_t retval;
0194 
0195     retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
0196             KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
0197     if (retval)
0198         return retval;
0199 
0200     return koneplus_sysfs_read(fp, kobj, buf, off, count,
0201             KONEPLUS_SIZE_PROFILE_SETTINGS,
0202             KONEPLUS_COMMAND_PROFILE_SETTINGS);
0203 }
0204 
0205 static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
0206         struct kobject *kobj, struct bin_attribute *attr, char *buf,
0207         loff_t off, size_t count)
0208 {
0209     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0210     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0211     ssize_t retval;
0212 
0213     retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
0214             KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
0215     if (retval)
0216         return retval;
0217 
0218     return koneplus_sysfs_read(fp, kobj, buf, off, count,
0219             KONEPLUS_SIZE_PROFILE_BUTTONS,
0220             KONEPLUS_COMMAND_PROFILE_BUTTONS);
0221 }
0222 
0223 #define PROFILE_ATTR(number)                        \
0224 static struct bin_attribute bin_attr_profile##number##_settings = { \
0225     .attr = { .name = "profile" #number "_settings", .mode = 0440 },    \
0226     .size = KONEPLUS_SIZE_PROFILE_SETTINGS,             \
0227     .read = koneplus_sysfs_read_profilex_settings,          \
0228     .private = &profile_numbers[number-1],              \
0229 };                                  \
0230 static struct bin_attribute bin_attr_profile##number##_buttons = {  \
0231     .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
0232     .size = KONEPLUS_SIZE_PROFILE_BUTTONS,              \
0233     .read = koneplus_sysfs_read_profilex_buttons,           \
0234     .private = &profile_numbers[number-1],              \
0235 };
0236 PROFILE_ATTR(1);
0237 PROFILE_ATTR(2);
0238 PROFILE_ATTR(3);
0239 PROFILE_ATTR(4);
0240 PROFILE_ATTR(5);
0241 
0242 static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
0243         struct device_attribute *attr, char *buf)
0244 {
0245     struct koneplus_device *koneplus =
0246             hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
0247     return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
0248 }
0249 
0250 static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
0251         struct device_attribute *attr, char const *buf, size_t size)
0252 {
0253     struct koneplus_device *koneplus;
0254     struct usb_device *usb_dev;
0255     unsigned long profile;
0256     int retval;
0257     struct koneplus_roccat_report roccat_report;
0258 
0259     dev = dev->parent->parent;
0260     koneplus = hid_get_drvdata(dev_get_drvdata(dev));
0261     usb_dev = interface_to_usbdev(to_usb_interface(dev));
0262 
0263     retval = kstrtoul(buf, 10, &profile);
0264     if (retval)
0265         return retval;
0266 
0267     if (profile > 4)
0268         return -EINVAL;
0269 
0270     mutex_lock(&koneplus->koneplus_lock);
0271 
0272     retval = koneplus_set_actual_profile(usb_dev, profile);
0273     if (retval) {
0274         mutex_unlock(&koneplus->koneplus_lock);
0275         return retval;
0276     }
0277 
0278     koneplus_profile_activated(koneplus, profile);
0279 
0280     roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
0281     roccat_report.data1 = profile + 1;
0282     roccat_report.data2 = 0;
0283     roccat_report.profile = profile + 1;
0284     roccat_report_event(koneplus->chrdev_minor,
0285             (uint8_t const *)&roccat_report);
0286 
0287     mutex_unlock(&koneplus->koneplus_lock);
0288 
0289     return size;
0290 }
0291 static DEVICE_ATTR(actual_profile, 0660,
0292            koneplus_sysfs_show_actual_profile,
0293            koneplus_sysfs_set_actual_profile);
0294 static DEVICE_ATTR(startup_profile, 0660,
0295            koneplus_sysfs_show_actual_profile,
0296            koneplus_sysfs_set_actual_profile);
0297 
0298 static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
0299         struct device_attribute *attr, char *buf)
0300 {
0301     struct koneplus_device *koneplus;
0302     struct usb_device *usb_dev;
0303     struct koneplus_info info;
0304 
0305     dev = dev->parent->parent;
0306     koneplus = hid_get_drvdata(dev_get_drvdata(dev));
0307     usb_dev = interface_to_usbdev(to_usb_interface(dev));
0308 
0309     mutex_lock(&koneplus->koneplus_lock);
0310     roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
0311             &info, KONEPLUS_SIZE_INFO);
0312     mutex_unlock(&koneplus->koneplus_lock);
0313 
0314     return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
0315 }
0316 static DEVICE_ATTR(firmware_version, 0440,
0317            koneplus_sysfs_show_firmware_version, NULL);
0318 
0319 static struct attribute *koneplus_attrs[] = {
0320     &dev_attr_actual_profile.attr,
0321     &dev_attr_startup_profile.attr,
0322     &dev_attr_firmware_version.attr,
0323     NULL,
0324 };
0325 
0326 static struct bin_attribute *koneplus_bin_attributes[] = {
0327     &bin_attr_control,
0328     &bin_attr_talk,
0329     &bin_attr_macro,
0330     &bin_attr_tcu_image,
0331     &bin_attr_info,
0332     &bin_attr_sensor,
0333     &bin_attr_tcu,
0334     &bin_attr_profile_settings,
0335     &bin_attr_profile_buttons,
0336     &bin_attr_profile1_settings,
0337     &bin_attr_profile2_settings,
0338     &bin_attr_profile3_settings,
0339     &bin_attr_profile4_settings,
0340     &bin_attr_profile5_settings,
0341     &bin_attr_profile1_buttons,
0342     &bin_attr_profile2_buttons,
0343     &bin_attr_profile3_buttons,
0344     &bin_attr_profile4_buttons,
0345     &bin_attr_profile5_buttons,
0346     NULL,
0347 };
0348 
0349 static const struct attribute_group koneplus_group = {
0350     .attrs = koneplus_attrs,
0351     .bin_attrs = koneplus_bin_attributes,
0352 };
0353 
0354 static const struct attribute_group *koneplus_groups[] = {
0355     &koneplus_group,
0356     NULL,
0357 };
0358 
0359 static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
0360         struct koneplus_device *koneplus)
0361 {
0362     int retval;
0363 
0364     mutex_init(&koneplus->koneplus_lock);
0365 
0366     retval = koneplus_get_actual_profile(usb_dev);
0367     if (retval < 0)
0368         return retval;
0369     koneplus_profile_activated(koneplus, retval);
0370 
0371     return 0;
0372 }
0373 
0374 static int koneplus_init_specials(struct hid_device *hdev)
0375 {
0376     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0377     struct usb_device *usb_dev = interface_to_usbdev(intf);
0378     struct koneplus_device *koneplus;
0379     int retval;
0380 
0381     if (intf->cur_altsetting->desc.bInterfaceProtocol
0382             == USB_INTERFACE_PROTOCOL_MOUSE) {
0383 
0384         koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
0385         if (!koneplus) {
0386             hid_err(hdev, "can't alloc device descriptor\n");
0387             return -ENOMEM;
0388         }
0389         hid_set_drvdata(hdev, koneplus);
0390 
0391         retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
0392         if (retval) {
0393             hid_err(hdev, "couldn't init struct koneplus_device\n");
0394             goto exit_free;
0395         }
0396 
0397         retval = roccat_connect(koneplus_class, hdev,
0398                 sizeof(struct koneplus_roccat_report));
0399         if (retval < 0) {
0400             hid_err(hdev, "couldn't init char dev\n");
0401         } else {
0402             koneplus->chrdev_minor = retval;
0403             koneplus->roccat_claimed = 1;
0404         }
0405     } else {
0406         hid_set_drvdata(hdev, NULL);
0407     }
0408 
0409     return 0;
0410 exit_free:
0411     kfree(koneplus);
0412     return retval;
0413 }
0414 
0415 static void koneplus_remove_specials(struct hid_device *hdev)
0416 {
0417     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0418     struct koneplus_device *koneplus;
0419 
0420     if (intf->cur_altsetting->desc.bInterfaceProtocol
0421             == USB_INTERFACE_PROTOCOL_MOUSE) {
0422         koneplus = hid_get_drvdata(hdev);
0423         if (koneplus->roccat_claimed)
0424             roccat_disconnect(koneplus->chrdev_minor);
0425         kfree(koneplus);
0426     }
0427 }
0428 
0429 static int koneplus_probe(struct hid_device *hdev,
0430         const struct hid_device_id *id)
0431 {
0432     int retval;
0433 
0434     if (!hid_is_usb(hdev))
0435         return -EINVAL;
0436 
0437     retval = hid_parse(hdev);
0438     if (retval) {
0439         hid_err(hdev, "parse failed\n");
0440         goto exit;
0441     }
0442 
0443     retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0444     if (retval) {
0445         hid_err(hdev, "hw start failed\n");
0446         goto exit;
0447     }
0448 
0449     retval = koneplus_init_specials(hdev);
0450     if (retval) {
0451         hid_err(hdev, "couldn't install mouse\n");
0452         goto exit_stop;
0453     }
0454 
0455     return 0;
0456 
0457 exit_stop:
0458     hid_hw_stop(hdev);
0459 exit:
0460     return retval;
0461 }
0462 
0463 static void koneplus_remove(struct hid_device *hdev)
0464 {
0465     koneplus_remove_specials(hdev);
0466     hid_hw_stop(hdev);
0467 }
0468 
0469 static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
0470         u8 const *data)
0471 {
0472     struct koneplus_mouse_report_button const *button_report;
0473 
0474     switch (data[0]) {
0475     case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
0476         button_report = (struct koneplus_mouse_report_button const *)data;
0477         switch (button_report->type) {
0478         case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
0479             koneplus_profile_activated(koneplus, button_report->data1 - 1);
0480             break;
0481         }
0482         break;
0483     }
0484 }
0485 
0486 static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
0487         u8 const *data)
0488 {
0489     struct koneplus_roccat_report roccat_report;
0490     struct koneplus_mouse_report_button const *button_report;
0491 
0492     if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
0493         return;
0494 
0495     button_report = (struct koneplus_mouse_report_button const *)data;
0496 
0497     if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
0498             button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
0499             button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
0500         return;
0501 
0502     roccat_report.type = button_report->type;
0503     roccat_report.data1 = button_report->data1;
0504     roccat_report.data2 = button_report->data2;
0505     roccat_report.profile = koneplus->actual_profile + 1;
0506     roccat_report_event(koneplus->chrdev_minor,
0507             (uint8_t const *)&roccat_report);
0508 }
0509 
0510 static int koneplus_raw_event(struct hid_device *hdev,
0511         struct hid_report *report, u8 *data, int size)
0512 {
0513     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0514     struct koneplus_device *koneplus = hid_get_drvdata(hdev);
0515 
0516     if (intf->cur_altsetting->desc.bInterfaceProtocol
0517             != USB_INTERFACE_PROTOCOL_MOUSE)
0518         return 0;
0519 
0520     if (koneplus == NULL)
0521         return 0;
0522 
0523     koneplus_keep_values_up_to_date(koneplus, data);
0524 
0525     if (koneplus->roccat_claimed)
0526         koneplus_report_to_chrdev(koneplus, data);
0527 
0528     return 0;
0529 }
0530 
0531 static const struct hid_device_id koneplus_devices[] = {
0532     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
0533     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
0534     { }
0535 };
0536 
0537 MODULE_DEVICE_TABLE(hid, koneplus_devices);
0538 
0539 static struct hid_driver koneplus_driver = {
0540         .name = "koneplus",
0541         .id_table = koneplus_devices,
0542         .probe = koneplus_probe,
0543         .remove = koneplus_remove,
0544         .raw_event = koneplus_raw_event
0545 };
0546 
0547 static int __init koneplus_init(void)
0548 {
0549     int retval;
0550 
0551     /* class name has to be same as driver name */
0552     koneplus_class = class_create(THIS_MODULE, "koneplus");
0553     if (IS_ERR(koneplus_class))
0554         return PTR_ERR(koneplus_class);
0555     koneplus_class->dev_groups = koneplus_groups;
0556 
0557     retval = hid_register_driver(&koneplus_driver);
0558     if (retval)
0559         class_destroy(koneplus_class);
0560     return retval;
0561 }
0562 
0563 static void __exit koneplus_exit(void)
0564 {
0565     hid_unregister_driver(&koneplus_driver);
0566     class_destroy(koneplus_class);
0567 }
0568 
0569 module_init(koneplus_init);
0570 module_exit(koneplus_exit);
0571 
0572 MODULE_AUTHOR("Stefan Achatz");
0573 MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
0574 MODULE_LICENSE("GPL v2");