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 a gamer mouse which consists of a mouse part and a keyboard
0013  * part. The keyboard part enables the mouse to execute stored macros with mixed
0014  * key- and button-events.
0015  *
0016  * TODO implement on-the-fly polling-rate change
0017  *      The windows driver has the ability to change the polling rate of the
0018  *      device on the press of a mousebutton.
0019  *      Is it possible to remove and reinstall the urb in raw-event- or any
0020  *      other handler, or to defer this action to be executed somewhere else?
0021  *
0022  * TODO is it possible to overwrite group for sysfs attributes via udev?
0023  */
0024 
0025 #include <linux/device.h>
0026 #include <linux/input.h>
0027 #include <linux/hid.h>
0028 #include <linux/module.h>
0029 #include <linux/slab.h>
0030 #include <linux/hid-roccat.h>
0031 #include "hid-ids.h"
0032 #include "hid-roccat-common.h"
0033 #include "hid-roccat-kone.h"
0034 
0035 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
0036 
0037 static void kone_profile_activated(struct kone_device *kone, uint new_profile)
0038 {
0039     kone->actual_profile = new_profile;
0040     kone->actual_dpi = kone->profiles[new_profile - 1].startup_dpi;
0041 }
0042 
0043 static void kone_profile_report(struct kone_device *kone, uint new_profile)
0044 {
0045     struct kone_roccat_report roccat_report;
0046 
0047     roccat_report.event = kone_mouse_event_switch_profile;
0048     roccat_report.value = new_profile;
0049     roccat_report.key = 0;
0050     roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
0051 }
0052 
0053 static int kone_receive(struct usb_device *usb_dev, uint usb_command,
0054         void *data, uint size)
0055 {
0056     char *buf;
0057     int len;
0058 
0059     buf = kmalloc(size, GFP_KERNEL);
0060     if (buf == NULL)
0061         return -ENOMEM;
0062 
0063     len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
0064             HID_REQ_GET_REPORT,
0065             USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0066             usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
0067 
0068     memcpy(data, buf, size);
0069     kfree(buf);
0070     return ((len < 0) ? len : ((len != size) ? -EIO : 0));
0071 }
0072 
0073 static int kone_send(struct usb_device *usb_dev, uint usb_command,
0074         void const *data, uint size)
0075 {
0076     char *buf;
0077     int len;
0078 
0079     buf = kmemdup(data, size, GFP_KERNEL);
0080     if (buf == NULL)
0081         return -ENOMEM;
0082 
0083     len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
0084             HID_REQ_SET_REPORT,
0085             USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
0086             usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
0087 
0088     kfree(buf);
0089     return ((len < 0) ? len : ((len != size) ? -EIO : 0));
0090 }
0091 
0092 /* kone_class is used for creating sysfs attributes via roccat char device */
0093 static struct class *kone_class;
0094 
0095 static void kone_set_settings_checksum(struct kone_settings *settings)
0096 {
0097     uint16_t checksum = 0;
0098     unsigned char *address = (unsigned char *)settings;
0099     int i;
0100 
0101     for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
0102         checksum += *address;
0103     settings->checksum = cpu_to_le16(checksum);
0104 }
0105 
0106 /*
0107  * Checks success after writing data to mouse
0108  * On success returns 0
0109  * On failure returns errno
0110  */
0111 static int kone_check_write(struct usb_device *usb_dev)
0112 {
0113     int retval;
0114     uint8_t data;
0115 
0116     do {
0117         /*
0118          * Mouse needs 50 msecs until it says ok, but there are
0119          * 30 more msecs needed for next write to work.
0120          */
0121         msleep(80);
0122 
0123         retval = kone_receive(usb_dev,
0124                 kone_command_confirm_write, &data, 1);
0125         if (retval)
0126             return retval;
0127 
0128         /*
0129          * value of 3 seems to mean something like
0130          * "not finished yet, but it looks good"
0131          * So check again after a moment.
0132          */
0133     } while (data == 3);
0134 
0135     if (data == 1) /* everything alright */
0136         return 0;
0137 
0138     /* unknown answer */
0139     dev_err(&usb_dev->dev, "got retval %d when checking write\n", data);
0140     return -EIO;
0141 }
0142 
0143 /*
0144  * Reads settings from mouse and stores it in @buf
0145  * On success returns 0
0146  * On failure returns errno
0147  */
0148 static int kone_get_settings(struct usb_device *usb_dev,
0149         struct kone_settings *buf)
0150 {
0151     return kone_receive(usb_dev, kone_command_settings, buf,
0152             sizeof(struct kone_settings));
0153 }
0154 
0155 /*
0156  * Writes settings from @buf to mouse
0157  * On success returns 0
0158  * On failure returns errno
0159  */
0160 static int kone_set_settings(struct usb_device *usb_dev,
0161         struct kone_settings const *settings)
0162 {
0163     int retval;
0164 
0165     retval = kone_send(usb_dev, kone_command_settings,
0166             settings, sizeof(struct kone_settings));
0167     if (retval)
0168         return retval;
0169     return kone_check_write(usb_dev);
0170 }
0171 
0172 /*
0173  * Reads profile data from mouse and stores it in @buf
0174  * @number: profile number to read
0175  * On success returns 0
0176  * On failure returns errno
0177  */
0178 static int kone_get_profile(struct usb_device *usb_dev,
0179         struct kone_profile *buf, int number)
0180 {
0181     int len;
0182 
0183     if (number < 1 || number > 5)
0184         return -EINVAL;
0185 
0186     len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
0187             USB_REQ_CLEAR_FEATURE,
0188             USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0189             kone_command_profile, number, buf,
0190             sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
0191 
0192     if (len != sizeof(struct kone_profile))
0193         return -EIO;
0194 
0195     return 0;
0196 }
0197 
0198 /*
0199  * Writes profile data to mouse.
0200  * @number: profile number to write
0201  * On success returns 0
0202  * On failure returns errno
0203  */
0204 static int kone_set_profile(struct usb_device *usb_dev,
0205         struct kone_profile const *profile, int number)
0206 {
0207     int len;
0208 
0209     if (number < 1 || number > 5)
0210         return -EINVAL;
0211 
0212     len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
0213             USB_REQ_SET_CONFIGURATION,
0214             USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
0215             kone_command_profile, number, (void *)profile,
0216             sizeof(struct kone_profile),
0217             USB_CTRL_SET_TIMEOUT);
0218 
0219     if (len != sizeof(struct kone_profile))
0220         return len;
0221 
0222     if (kone_check_write(usb_dev))
0223         return -EIO;
0224 
0225     return 0;
0226 }
0227 
0228 /*
0229  * Reads value of "fast-clip-weight" and stores it in @result
0230  * On success returns 0
0231  * On failure returns errno
0232  */
0233 static int kone_get_weight(struct usb_device *usb_dev, int *result)
0234 {
0235     int retval;
0236     uint8_t data;
0237 
0238     retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
0239 
0240     if (retval)
0241         return retval;
0242 
0243     *result = (int)data;
0244     return 0;
0245 }
0246 
0247 /*
0248  * Reads firmware_version of mouse and stores it in @result
0249  * On success returns 0
0250  * On failure returns errno
0251  */
0252 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
0253 {
0254     int retval;
0255     uint16_t data;
0256 
0257     retval = kone_receive(usb_dev, kone_command_firmware_version,
0258             &data, 2);
0259     if (retval)
0260         return retval;
0261 
0262     *result = le16_to_cpu(data);
0263     return 0;
0264 }
0265 
0266 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
0267         struct bin_attribute *attr, char *buf,
0268         loff_t off, size_t count) {
0269     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0270     struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
0271 
0272     if (off >= sizeof(struct kone_settings))
0273         return 0;
0274 
0275     if (off + count > sizeof(struct kone_settings))
0276         count = sizeof(struct kone_settings) - off;
0277 
0278     mutex_lock(&kone->kone_lock);
0279     memcpy(buf, ((char const *)&kone->settings) + off, count);
0280     mutex_unlock(&kone->kone_lock);
0281 
0282     return count;
0283 }
0284 
0285 /*
0286  * Writing settings automatically activates startup_profile.
0287  * This function keeps values in kone_device up to date and assumes that in
0288  * case of error the old data is still valid
0289  */
0290 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
0291         struct bin_attribute *attr, char *buf,
0292         loff_t off, size_t count) {
0293     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0294     struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
0295     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0296     int retval = 0, difference, old_profile;
0297     struct kone_settings *settings = (struct kone_settings *)buf;
0298 
0299     /* I need to get my data in one piece */
0300     if (off != 0 || count != sizeof(struct kone_settings))
0301         return -EINVAL;
0302 
0303     mutex_lock(&kone->kone_lock);
0304     difference = memcmp(settings, &kone->settings,
0305                 sizeof(struct kone_settings));
0306     if (difference) {
0307         if (settings->startup_profile < 1 ||
0308             settings->startup_profile > 5) {
0309             retval = -EINVAL;
0310             goto unlock;
0311         }
0312 
0313         retval = kone_set_settings(usb_dev, settings);
0314         if (retval)
0315             goto unlock;
0316 
0317         old_profile = kone->settings.startup_profile;
0318         memcpy(&kone->settings, settings, sizeof(struct kone_settings));
0319 
0320         kone_profile_activated(kone, kone->settings.startup_profile);
0321 
0322         if (kone->settings.startup_profile != old_profile)
0323             kone_profile_report(kone, kone->settings.startup_profile);
0324     }
0325 unlock:
0326     mutex_unlock(&kone->kone_lock);
0327 
0328     if (retval)
0329         return retval;
0330 
0331     return sizeof(struct kone_settings);
0332 }
0333 static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
0334         kone_sysfs_write_settings, sizeof(struct kone_settings));
0335 
0336 static ssize_t kone_sysfs_read_profilex(struct file *fp,
0337         struct kobject *kobj, struct bin_attribute *attr,
0338         char *buf, loff_t off, size_t count) {
0339     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0340     struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
0341 
0342     if (off >= sizeof(struct kone_profile))
0343         return 0;
0344 
0345     if (off + count > sizeof(struct kone_profile))
0346         count = sizeof(struct kone_profile) - off;
0347 
0348     mutex_lock(&kone->kone_lock);
0349     memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
0350     mutex_unlock(&kone->kone_lock);
0351 
0352     return count;
0353 }
0354 
0355 /* Writes data only if different to stored data */
0356 static ssize_t kone_sysfs_write_profilex(struct file *fp,
0357         struct kobject *kobj, struct bin_attribute *attr,
0358         char *buf, loff_t off, size_t count) {
0359     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0360     struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
0361     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0362     struct kone_profile *profile;
0363     int retval = 0, difference;
0364 
0365     /* I need to get my data in one piece */
0366     if (off != 0 || count != sizeof(struct kone_profile))
0367         return -EINVAL;
0368 
0369     profile = &kone->profiles[*(uint *)(attr->private)];
0370 
0371     mutex_lock(&kone->kone_lock);
0372     difference = memcmp(buf, profile, sizeof(struct kone_profile));
0373     if (difference) {
0374         retval = kone_set_profile(usb_dev,
0375                 (struct kone_profile const *)buf,
0376                 *(uint *)(attr->private) + 1);
0377         if (!retval)
0378             memcpy(profile, buf, sizeof(struct kone_profile));
0379     }
0380     mutex_unlock(&kone->kone_lock);
0381 
0382     if (retval)
0383         return retval;
0384 
0385     return sizeof(struct kone_profile);
0386 }
0387 #define PROFILE_ATTR(number)                    \
0388 static struct bin_attribute bin_attr_profile##number = {    \
0389     .attr = { .name = "profile" #number, .mode = 0660 },    \
0390     .size = sizeof(struct kone_profile),            \
0391     .read = kone_sysfs_read_profilex,           \
0392     .write = kone_sysfs_write_profilex,         \
0393     .private = &profile_numbers[number-1],          \
0394 }
0395 PROFILE_ATTR(1);
0396 PROFILE_ATTR(2);
0397 PROFILE_ATTR(3);
0398 PROFILE_ATTR(4);
0399 PROFILE_ATTR(5);
0400 
0401 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
0402         struct device_attribute *attr, char *buf)
0403 {
0404     struct kone_device *kone =
0405             hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
0406     return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
0407 }
0408 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
0409 
0410 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
0411         struct device_attribute *attr, char *buf)
0412 {
0413     struct kone_device *kone =
0414             hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
0415     return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
0416 }
0417 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
0418 
0419 /* weight is read each time, since we don't get informed when it's changed */
0420 static ssize_t kone_sysfs_show_weight(struct device *dev,
0421         struct device_attribute *attr, char *buf)
0422 {
0423     struct kone_device *kone;
0424     struct usb_device *usb_dev;
0425     int weight = 0;
0426     int retval;
0427 
0428     dev = dev->parent->parent;
0429     kone = hid_get_drvdata(dev_get_drvdata(dev));
0430     usb_dev = interface_to_usbdev(to_usb_interface(dev));
0431 
0432     mutex_lock(&kone->kone_lock);
0433     retval = kone_get_weight(usb_dev, &weight);
0434     mutex_unlock(&kone->kone_lock);
0435 
0436     if (retval)
0437         return retval;
0438     return snprintf(buf, PAGE_SIZE, "%d\n", weight);
0439 }
0440 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
0441 
0442 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
0443         struct device_attribute *attr, char *buf)
0444 {
0445     struct kone_device *kone =
0446             hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
0447     return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
0448 }
0449 static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
0450            NULL);
0451 
0452 static ssize_t kone_sysfs_show_tcu(struct device *dev,
0453         struct device_attribute *attr, char *buf)
0454 {
0455     struct kone_device *kone =
0456             hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
0457     return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
0458 }
0459 
0460 static int kone_tcu_command(struct usb_device *usb_dev, int number)
0461 {
0462     unsigned char value;
0463 
0464     value = number;
0465     return kone_send(usb_dev, kone_command_calibrate, &value, 1);
0466 }
0467 
0468 /*
0469  * Calibrating the tcu is the only action that changes settings data inside the
0470  * mouse, so this data needs to be reread
0471  */
0472 static ssize_t kone_sysfs_set_tcu(struct device *dev,
0473         struct device_attribute *attr, char const *buf, size_t size)
0474 {
0475     struct kone_device *kone;
0476     struct usb_device *usb_dev;
0477     int retval;
0478     unsigned long state;
0479 
0480     dev = dev->parent->parent;
0481     kone = hid_get_drvdata(dev_get_drvdata(dev));
0482     usb_dev = interface_to_usbdev(to_usb_interface(dev));
0483 
0484     retval = kstrtoul(buf, 10, &state);
0485     if (retval)
0486         return retval;
0487 
0488     if (state != 0 && state != 1)
0489         return -EINVAL;
0490 
0491     mutex_lock(&kone->kone_lock);
0492 
0493     if (state == 1) { /* state activate */
0494         retval = kone_tcu_command(usb_dev, 1);
0495         if (retval)
0496             goto exit_unlock;
0497         retval = kone_tcu_command(usb_dev, 2);
0498         if (retval)
0499             goto exit_unlock;
0500         ssleep(5); /* tcu needs this time for calibration */
0501         retval = kone_tcu_command(usb_dev, 3);
0502         if (retval)
0503             goto exit_unlock;
0504         retval = kone_tcu_command(usb_dev, 0);
0505         if (retval)
0506             goto exit_unlock;
0507         retval = kone_tcu_command(usb_dev, 4);
0508         if (retval)
0509             goto exit_unlock;
0510         /*
0511          * Kone needs this time to settle things.
0512          * Reading settings too early will result in invalid data.
0513          * Roccat's driver waits 1 sec, maybe this time could be
0514          * shortened.
0515          */
0516         ssleep(1);
0517     }
0518 
0519     /* calibration changes values in settings, so reread */
0520     retval = kone_get_settings(usb_dev, &kone->settings);
0521     if (retval)
0522         goto exit_no_settings;
0523 
0524     /* only write settings back if activation state is different */
0525     if (kone->settings.tcu != state) {
0526         kone->settings.tcu = state;
0527         kone_set_settings_checksum(&kone->settings);
0528 
0529         retval = kone_set_settings(usb_dev, &kone->settings);
0530         if (retval) {
0531             dev_err(&usb_dev->dev, "couldn't set tcu state\n");
0532             /*
0533              * try to reread valid settings into buffer overwriting
0534              * first error code
0535              */
0536             retval = kone_get_settings(usb_dev, &kone->settings);
0537             if (retval)
0538                 goto exit_no_settings;
0539             goto exit_unlock;
0540         }
0541         /* calibration resets profile */
0542         kone_profile_activated(kone, kone->settings.startup_profile);
0543     }
0544 
0545     retval = size;
0546 exit_no_settings:
0547     dev_err(&usb_dev->dev, "couldn't read settings\n");
0548 exit_unlock:
0549     mutex_unlock(&kone->kone_lock);
0550     return retval;
0551 }
0552 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
0553 
0554 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
0555         struct device_attribute *attr, char *buf)
0556 {
0557     struct kone_device *kone =
0558             hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
0559     return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
0560 }
0561 
0562 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
0563         struct device_attribute *attr, char const *buf, size_t size)
0564 {
0565     struct kone_device *kone;
0566     struct usb_device *usb_dev;
0567     int retval;
0568     unsigned long new_startup_profile;
0569 
0570     dev = dev->parent->parent;
0571     kone = hid_get_drvdata(dev_get_drvdata(dev));
0572     usb_dev = interface_to_usbdev(to_usb_interface(dev));
0573 
0574     retval = kstrtoul(buf, 10, &new_startup_profile);
0575     if (retval)
0576         return retval;
0577 
0578     if (new_startup_profile  < 1 || new_startup_profile > 5)
0579         return -EINVAL;
0580 
0581     mutex_lock(&kone->kone_lock);
0582 
0583     kone->settings.startup_profile = new_startup_profile;
0584     kone_set_settings_checksum(&kone->settings);
0585 
0586     retval = kone_set_settings(usb_dev, &kone->settings);
0587     if (retval) {
0588         mutex_unlock(&kone->kone_lock);
0589         return retval;
0590     }
0591 
0592     /* changing the startup profile immediately activates this profile */
0593     kone_profile_activated(kone, new_startup_profile);
0594     kone_profile_report(kone, new_startup_profile);
0595 
0596     mutex_unlock(&kone->kone_lock);
0597     return size;
0598 }
0599 static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
0600            kone_sysfs_set_startup_profile);
0601 
0602 static struct attribute *kone_attrs[] = {
0603     /*
0604      * Read actual dpi settings.
0605      * Returns raw value for further processing. Refer to enum
0606      * kone_polling_rates to get real value.
0607      */
0608     &dev_attr_actual_dpi.attr,
0609     &dev_attr_actual_profile.attr,
0610 
0611     /*
0612      * The mouse can be equipped with one of four supplied weights from 5
0613      * to 20 grams which are recognized and its value can be read out.
0614      * This returns the raw value reported by the mouse for easy evaluation
0615      * by software. Refer to enum kone_weights to get corresponding real
0616      * weight.
0617      */
0618     &dev_attr_weight.attr,
0619 
0620     /*
0621      * Prints firmware version stored in mouse as integer.
0622      * The raw value reported by the mouse is returned for easy evaluation,
0623      * to get the real version number the decimal point has to be shifted 2
0624      * positions to the left. E.g. a value of 138 means 1.38.
0625      */
0626     &dev_attr_firmware_version.attr,
0627 
0628     /*
0629      * Prints state of Tracking Control Unit as number where 0 = off and
0630      * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
0631      * activates the tcu
0632      */
0633     &dev_attr_tcu.attr,
0634 
0635     /* Prints and takes the number of the profile the mouse starts with */
0636     &dev_attr_startup_profile.attr,
0637     NULL,
0638 };
0639 
0640 static struct bin_attribute *kone_bin_attributes[] = {
0641     &bin_attr_settings,
0642     &bin_attr_profile1,
0643     &bin_attr_profile2,
0644     &bin_attr_profile3,
0645     &bin_attr_profile4,
0646     &bin_attr_profile5,
0647     NULL,
0648 };
0649 
0650 static const struct attribute_group kone_group = {
0651     .attrs = kone_attrs,
0652     .bin_attrs = kone_bin_attributes,
0653 };
0654 
0655 static const struct attribute_group *kone_groups[] = {
0656     &kone_group,
0657     NULL,
0658 };
0659 
0660 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
0661         struct kone_device *kone)
0662 {
0663     uint i;
0664     int retval;
0665 
0666     mutex_init(&kone->kone_lock);
0667 
0668     for (i = 0; i < 5; ++i) {
0669         retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
0670         if (retval)
0671             return retval;
0672     }
0673 
0674     retval = kone_get_settings(usb_dev, &kone->settings);
0675     if (retval)
0676         return retval;
0677 
0678     retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
0679     if (retval)
0680         return retval;
0681 
0682     kone_profile_activated(kone, kone->settings.startup_profile);
0683 
0684     return 0;
0685 }
0686 
0687 /*
0688  * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
0689  * mousepart if usb_hid is compiled into the kernel and kone is compiled as
0690  * module.
0691  * Secial behaviour is bound only to mousepart since only mouseevents contain
0692  * additional notifications.
0693  */
0694 static int kone_init_specials(struct hid_device *hdev)
0695 {
0696     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0697     struct usb_device *usb_dev = interface_to_usbdev(intf);
0698     struct kone_device *kone;
0699     int retval;
0700 
0701     if (intf->cur_altsetting->desc.bInterfaceProtocol
0702             == USB_INTERFACE_PROTOCOL_MOUSE) {
0703 
0704         kone = kzalloc(sizeof(*kone), GFP_KERNEL);
0705         if (!kone)
0706             return -ENOMEM;
0707         hid_set_drvdata(hdev, kone);
0708 
0709         retval = kone_init_kone_device_struct(usb_dev, kone);
0710         if (retval) {
0711             hid_err(hdev, "couldn't init struct kone_device\n");
0712             goto exit_free;
0713         }
0714 
0715         retval = roccat_connect(kone_class, hdev,
0716                 sizeof(struct kone_roccat_report));
0717         if (retval < 0) {
0718             hid_err(hdev, "couldn't init char dev\n");
0719             /* be tolerant about not getting chrdev */
0720         } else {
0721             kone->roccat_claimed = 1;
0722             kone->chrdev_minor = retval;
0723         }
0724     } else {
0725         hid_set_drvdata(hdev, NULL);
0726     }
0727 
0728     return 0;
0729 exit_free:
0730     kfree(kone);
0731     return retval;
0732 }
0733 
0734 static void kone_remove_specials(struct hid_device *hdev)
0735 {
0736     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0737     struct kone_device *kone;
0738 
0739     if (intf->cur_altsetting->desc.bInterfaceProtocol
0740             == USB_INTERFACE_PROTOCOL_MOUSE) {
0741         kone = hid_get_drvdata(hdev);
0742         if (kone->roccat_claimed)
0743             roccat_disconnect(kone->chrdev_minor);
0744         kfree(hid_get_drvdata(hdev));
0745     }
0746 }
0747 
0748 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
0749 {
0750     int retval;
0751 
0752     if (!hid_is_usb(hdev))
0753         return -EINVAL;
0754 
0755     retval = hid_parse(hdev);
0756     if (retval) {
0757         hid_err(hdev, "parse failed\n");
0758         goto exit;
0759     }
0760 
0761     retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0762     if (retval) {
0763         hid_err(hdev, "hw start failed\n");
0764         goto exit;
0765     }
0766 
0767     retval = kone_init_specials(hdev);
0768     if (retval) {
0769         hid_err(hdev, "couldn't install mouse\n");
0770         goto exit_stop;
0771     }
0772 
0773     return 0;
0774 
0775 exit_stop:
0776     hid_hw_stop(hdev);
0777 exit:
0778     return retval;
0779 }
0780 
0781 static void kone_remove(struct hid_device *hdev)
0782 {
0783     kone_remove_specials(hdev);
0784     hid_hw_stop(hdev);
0785 }
0786 
0787 /* handle special events and keep actual profile and dpi values up to date */
0788 static void kone_keep_values_up_to_date(struct kone_device *kone,
0789         struct kone_mouse_event const *event)
0790 {
0791     switch (event->event) {
0792     case kone_mouse_event_switch_profile:
0793         kone->actual_dpi = kone->profiles[event->value - 1].
0794                 startup_dpi;
0795         fallthrough;
0796     case kone_mouse_event_osd_profile:
0797         kone->actual_profile = event->value;
0798         break;
0799     case kone_mouse_event_switch_dpi:
0800     case kone_mouse_event_osd_dpi:
0801         kone->actual_dpi = event->value;
0802         break;
0803     }
0804 }
0805 
0806 static void kone_report_to_chrdev(struct kone_device const *kone,
0807         struct kone_mouse_event const *event)
0808 {
0809     struct kone_roccat_report roccat_report;
0810 
0811     switch (event->event) {
0812     case kone_mouse_event_switch_profile:
0813     case kone_mouse_event_switch_dpi:
0814     case kone_mouse_event_osd_profile:
0815     case kone_mouse_event_osd_dpi:
0816         roccat_report.event = event->event;
0817         roccat_report.value = event->value;
0818         roccat_report.key = 0;
0819         roccat_report_event(kone->chrdev_minor,
0820                 (uint8_t *)&roccat_report);
0821         break;
0822     case kone_mouse_event_call_overlong_macro:
0823     case kone_mouse_event_multimedia:
0824         if (event->value == kone_keystroke_action_press) {
0825             roccat_report.event = event->event;
0826             roccat_report.value = kone->actual_profile;
0827             roccat_report.key = event->macro_key;
0828             roccat_report_event(kone->chrdev_minor,
0829                     (uint8_t *)&roccat_report);
0830         }
0831         break;
0832     }
0833 
0834 }
0835 
0836 /*
0837  * Is called for keyboard- and mousepart.
0838  * Only mousepart gets informations about special events in its extended event
0839  * structure.
0840  */
0841 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
0842         u8 *data, int size)
0843 {
0844     struct kone_device *kone = hid_get_drvdata(hdev);
0845     struct kone_mouse_event *event = (struct kone_mouse_event *)data;
0846 
0847     /* keyboard events are always processed by default handler */
0848     if (size != sizeof(struct kone_mouse_event))
0849         return 0;
0850 
0851     if (kone == NULL)
0852         return 0;
0853 
0854     /*
0855      * Firmware 1.38 introduced new behaviour for tilt and special buttons.
0856      * Pressed button is reported in each movement event.
0857      * Workaround sends only one event per press.
0858      */
0859     if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
0860         memcpy(&kone->last_mouse_event, event,
0861                 sizeof(struct kone_mouse_event));
0862     else
0863         memset(&event->wipe, 0, sizeof(event->wipe));
0864 
0865     kone_keep_values_up_to_date(kone, event);
0866 
0867     if (kone->roccat_claimed)
0868         kone_report_to_chrdev(kone, event);
0869 
0870     return 0; /* always do further processing */
0871 }
0872 
0873 static const struct hid_device_id kone_devices[] = {
0874     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
0875     { }
0876 };
0877 
0878 MODULE_DEVICE_TABLE(hid, kone_devices);
0879 
0880 static struct hid_driver kone_driver = {
0881         .name = "kone",
0882         .id_table = kone_devices,
0883         .probe = kone_probe,
0884         .remove = kone_remove,
0885         .raw_event = kone_raw_event
0886 };
0887 
0888 static int __init kone_init(void)
0889 {
0890     int retval;
0891 
0892     /* class name has to be same as driver name */
0893     kone_class = class_create(THIS_MODULE, "kone");
0894     if (IS_ERR(kone_class))
0895         return PTR_ERR(kone_class);
0896     kone_class->dev_groups = kone_groups;
0897 
0898     retval = hid_register_driver(&kone_driver);
0899     if (retval)
0900         class_destroy(kone_class);
0901     return retval;
0902 }
0903 
0904 static void __exit kone_exit(void)
0905 {
0906     hid_unregister_driver(&kone_driver);
0907     class_destroy(kone_class);
0908 }
0909 
0910 module_init(kone_init);
0911 module_exit(kone_exit);
0912 
0913 MODULE_AUTHOR("Stefan Achatz");
0914 MODULE_DESCRIPTION("USB Roccat Kone driver");
0915 MODULE_LICENSE("GPL v2");