Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Roccat Kone[+] driver for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Roccat Kone[+] is an updated/improved version of the Kone with more memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * and functionality and without the non-standard behaviours the Kone had.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * KoneXTD has same capabilities but updated sensor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/hid-roccat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "hid-roccat-common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "hid-roccat-koneplus.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static uint profile_numbers[5] = {0, 1, 2, 3, 4};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static struct class *koneplus_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static void koneplus_profile_activated(struct koneplus_device *koneplus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		uint new_profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	koneplus->actual_profile = new_profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static int koneplus_send_control(struct usb_device *usb_dev, uint value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		enum koneplus_control_requests request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct roccat_common2_control control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			value > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	control.command = ROCCAT_COMMON_COMMAND_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	control.value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	control.request = request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return roccat_common2_send_with_status(usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			ROCCAT_COMMON_COMMAND_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			&control, sizeof(struct roccat_common2_control));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /* retval is 0-4 on success, < 0 on error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int koneplus_get_actual_profile(struct usb_device *usb_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct koneplus_actual_profile buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			&buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return retval ? retval : buf.actual_profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int koneplus_set_actual_profile(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		int new_profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct koneplus_actual_profile buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	buf.actual_profile = new_profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return roccat_common2_send_with_status(usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			KONEPLUS_COMMAND_ACTUAL_PROFILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			&buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		char *buf, loff_t off, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		size_t real_size, uint command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (off >= real_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (off != 0 || count != real_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	mutex_lock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	retval = roccat_common2_receive(usb_dev, command, buf, real_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	mutex_unlock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return real_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		void const *buf, loff_t off, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		size_t real_size, uint command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (off != 0 || count != real_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	mutex_lock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	retval = roccat_common2_send_with_status(usb_dev, command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			buf, real_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	mutex_unlock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return real_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define KONEPLUS_SYSFS_W(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		struct kobject *kobj, struct bin_attribute *attr, char *buf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		loff_t off, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return koneplus_sysfs_write(fp, kobj, buf, off, count, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define KONEPLUS_SYSFS_R(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		struct kobject *kobj, struct bin_attribute *attr, char *buf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		loff_t off, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return koneplus_sysfs_read(fp, kobj, buf, off, count, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define KONEPLUS_SYSFS_RW(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) KONEPLUS_SYSFS_W(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) KONEPLUS_SYSFS_R(thingy, THINGY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) KONEPLUS_SYSFS_RW(thingy, THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static struct bin_attribute bin_attr_##thingy = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	.attr = { .name = #thingy, .mode = 0660 }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	.size = KONEPLUS_SIZE_ ## THINGY, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	.read = koneplus_sysfs_read_ ## thingy, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	.write = koneplus_sysfs_write_ ## thingy \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) KONEPLUS_SYSFS_R(thingy, THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static struct bin_attribute bin_attr_##thingy = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	.attr = { .name = #thingy, .mode = 0440 }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	.size = KONEPLUS_SIZE_ ## THINGY, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	.read = koneplus_sysfs_read_ ## thingy, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) KONEPLUS_SYSFS_W(thingy, THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static struct bin_attribute bin_attr_##thingy = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	.attr = { .name = #thingy, .mode = 0220 }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.size = KONEPLUS_SIZE_ ## THINGY, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.write = koneplus_sysfs_write_ ## thingy \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) KONEPLUS_BIN_ATTRIBUTE_W(control, CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) KONEPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) KONEPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return koneplus_sysfs_read(fp, kobj, buf, off, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			KONEPLUS_SIZE_PROFILE_SETTINGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			KONEPLUS_COMMAND_PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return koneplus_sysfs_read(fp, kobj, buf, off, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			KONEPLUS_SIZE_PROFILE_BUTTONS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			KONEPLUS_COMMAND_PROFILE_BUTTONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) #define PROFILE_ATTR(number)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static struct bin_attribute bin_attr_profile##number##_settings = {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	.attr = { .name = "profile" #number "_settings", .mode = 0440 },	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	.size = KONEPLUS_SIZE_PROFILE_SETTINGS,				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	.read = koneplus_sysfs_read_profilex_settings,			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	.private = &profile_numbers[number-1],				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) };									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static struct bin_attribute bin_attr_profile##number##_buttons = {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	.attr = { .name = "profile" #number "_buttons", .mode = 0440 },	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	.size = KONEPLUS_SIZE_PROFILE_BUTTONS,				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	.read = koneplus_sysfs_read_profilex_buttons,			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	.private = &profile_numbers[number-1],				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) PROFILE_ATTR(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) PROFILE_ATTR(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) PROFILE_ATTR(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) PROFILE_ATTR(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) PROFILE_ATTR(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	struct koneplus_device *koneplus =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		struct device_attribute *attr, char const *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct koneplus_device *koneplus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct usb_device *usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	unsigned long profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct koneplus_roccat_report roccat_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	dev = dev->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	koneplus = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	retval = kstrtoul(buf, 10, &profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (profile > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	mutex_lock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	retval = koneplus_set_actual_profile(usb_dev, profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		mutex_unlock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	koneplus_profile_activated(koneplus, profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	roccat_report.data1 = profile + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	roccat_report.data2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	roccat_report.profile = profile + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	roccat_report_event(koneplus->chrdev_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			(uint8_t const *)&roccat_report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	mutex_unlock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static DEVICE_ATTR(actual_profile, 0660,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		   koneplus_sysfs_show_actual_profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		   koneplus_sysfs_set_actual_profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static DEVICE_ATTR(startup_profile, 0660,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		   koneplus_sysfs_show_actual_profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		   koneplus_sysfs_set_actual_profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	struct koneplus_device *koneplus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct usb_device *usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct koneplus_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	dev = dev->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	koneplus = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	mutex_lock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			&info, KONEPLUS_SIZE_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	mutex_unlock(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static DEVICE_ATTR(firmware_version, 0440,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		   koneplus_sysfs_show_firmware_version, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static struct attribute *koneplus_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	&dev_attr_actual_profile.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	&dev_attr_startup_profile.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	&dev_attr_firmware_version.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static struct bin_attribute *koneplus_bin_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	&bin_attr_control,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	&bin_attr_talk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	&bin_attr_macro,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	&bin_attr_tcu_image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	&bin_attr_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	&bin_attr_sensor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	&bin_attr_tcu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	&bin_attr_profile_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	&bin_attr_profile_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	&bin_attr_profile1_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	&bin_attr_profile2_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	&bin_attr_profile3_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	&bin_attr_profile4_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	&bin_attr_profile5_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	&bin_attr_profile1_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	&bin_attr_profile2_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	&bin_attr_profile3_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	&bin_attr_profile4_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	&bin_attr_profile5_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static const struct attribute_group koneplus_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	.attrs = koneplus_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	.bin_attrs = koneplus_bin_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static const struct attribute_group *koneplus_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	&koneplus_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		struct koneplus_device *koneplus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	mutex_init(&koneplus->koneplus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	retval = koneplus_get_actual_profile(usb_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	koneplus_profile_activated(koneplus, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static int koneplus_init_specials(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct usb_device *usb_dev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	struct koneplus_device *koneplus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	if (intf->cur_altsetting->desc.bInterfaceProtocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			== USB_INTERFACE_PROTOCOL_MOUSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		if (!koneplus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			hid_err(hdev, "can't alloc device descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		hid_set_drvdata(hdev, koneplus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			hid_err(hdev, "couldn't init struct koneplus_device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			goto exit_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		retval = roccat_connect(koneplus_class, hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 				sizeof(struct koneplus_roccat_report));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			hid_err(hdev, "couldn't init char dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			koneplus->chrdev_minor = retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			koneplus->roccat_claimed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		hid_set_drvdata(hdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) exit_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	kfree(koneplus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static void koneplus_remove_specials(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	struct koneplus_device *koneplus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (intf->cur_altsetting->desc.bInterfaceProtocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			== USB_INTERFACE_PROTOCOL_MOUSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		koneplus = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		if (koneplus->roccat_claimed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			roccat_disconnect(koneplus->chrdev_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		kfree(koneplus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static int koneplus_probe(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (!hid_is_usb(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	retval = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		hid_err(hdev, "parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		hid_err(hdev, "hw start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	retval = koneplus_init_specials(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		hid_err(hdev, "couldn't install mouse\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		goto exit_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) exit_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static void koneplus_remove(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	koneplus_remove_specials(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		u8 const *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	struct koneplus_mouse_report_button const *button_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	switch (data[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		button_report = (struct koneplus_mouse_report_button const *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		switch (button_report->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			koneplus_profile_activated(koneplus, button_report->data1 - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		u8 const *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	struct koneplus_roccat_report roccat_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	struct koneplus_mouse_report_button const *button_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	button_report = (struct koneplus_mouse_report_button const *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	roccat_report.type = button_report->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	roccat_report.data1 = button_report->data1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	roccat_report.data2 = button_report->data2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	roccat_report.profile = koneplus->actual_profile + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	roccat_report_event(koneplus->chrdev_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			(uint8_t const *)&roccat_report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static int koneplus_raw_event(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		struct hid_report *report, u8 *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	struct koneplus_device *koneplus = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (intf->cur_altsetting->desc.bInterfaceProtocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			!= USB_INTERFACE_PROTOCOL_MOUSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	if (koneplus == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	koneplus_keep_values_up_to_date(koneplus, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if (koneplus->roccat_claimed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		koneplus_report_to_chrdev(koneplus, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static const struct hid_device_id koneplus_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) MODULE_DEVICE_TABLE(hid, koneplus_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) static struct hid_driver koneplus_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		.name = "koneplus",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		.id_table = koneplus_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		.probe = koneplus_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		.remove = koneplus_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		.raw_event = koneplus_raw_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int __init koneplus_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	/* class name has to be same as driver name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	koneplus_class = class_create(THIS_MODULE, "koneplus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	if (IS_ERR(koneplus_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		return PTR_ERR(koneplus_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	koneplus_class->dev_groups = koneplus_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	retval = hid_register_driver(&koneplus_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		class_destroy(koneplus_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) static void __exit koneplus_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	hid_unregister_driver(&koneplus_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	class_destroy(koneplus_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) module_init(koneplus_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) module_exit(koneplus_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) MODULE_AUTHOR("Stefan Achatz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) MODULE_LICENSE("GPL v2");