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 Pyra 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 Pyra is a mobile gamer mouse which comes in wired and wireless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * variant. Wireless variant is not tested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Userland tools can be found at http://sourceforge.net/projects/roccat
^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-pyra.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) /* pyra_class is used for creating sysfs attributes via roccat char device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static struct class *pyra_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static void profile_activated(struct pyra_device *pyra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		unsigned int new_profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (new_profile >= ARRAY_SIZE(pyra->profile_settings))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	pyra->actual_profile = new_profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static int pyra_send_control(struct usb_device *usb_dev, int value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		enum pyra_control_requests request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct roccat_common2_control control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			(value < 0 || value > 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	control.command = ROCCAT_COMMON_COMMAND_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	control.value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	control.request = request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			&control, sizeof(struct roccat_common2_control));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static int pyra_get_profile_settings(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		struct pyra_profile_settings *buf, int number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	retval = pyra_send_control(usb_dev, number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			buf, PYRA_SIZE_PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int pyra_get_settings(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		struct pyra_settings *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			buf, PYRA_SIZE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int pyra_set_settings(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		struct pyra_settings const *settings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return roccat_common2_send_with_status(usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			PYRA_COMMAND_SETTINGS, settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			PYRA_SIZE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static ssize_t pyra_sysfs_read(struct file *fp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		char *buf, loff_t off, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		size_t real_size, uint command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (off >= real_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (off != 0 || count != real_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	mutex_lock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	retval = roccat_common2_receive(usb_dev, command, buf, real_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	mutex_unlock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return real_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static ssize_t pyra_sysfs_write(struct file *fp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		void const *buf, loff_t off, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		size_t real_size, uint command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (off != 0 || count != real_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	mutex_lock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	retval = roccat_common2_send_with_status(usb_dev, command, (void *)buf, real_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	mutex_unlock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return real_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define PYRA_SYSFS_W(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		struct kobject *kobj, struct bin_attribute *attr, char *buf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		loff_t off, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return pyra_sysfs_write(fp, kobj, buf, off, count, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #define PYRA_SYSFS_R(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		struct kobject *kobj, struct bin_attribute *attr, char *buf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		loff_t off, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return pyra_sysfs_read(fp, kobj, buf, off, count, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define PYRA_SYSFS_RW(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) PYRA_SYSFS_W(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) PYRA_SYSFS_R(thingy, THINGY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) PYRA_SYSFS_RW(thingy, THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct bin_attribute bin_attr_##thingy = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	.attr = { .name = #thingy, .mode = 0660 }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	.size = PYRA_SIZE_ ## THINGY, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	.read = pyra_sysfs_read_ ## thingy, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	.write = pyra_sysfs_write_ ## thingy \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) PYRA_SYSFS_R(thingy, THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct bin_attribute bin_attr_##thingy = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	.attr = { .name = #thingy, .mode = 0440 }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	.size = PYRA_SIZE_ ## THINGY, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	.read = pyra_sysfs_read_ ## thingy, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) PYRA_SYSFS_W(thingy, THINGY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static struct bin_attribute bin_attr_##thingy = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.attr = { .name = #thingy, .mode = 0220 }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.size = PYRA_SIZE_ ## THINGY, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	.write = pyra_sysfs_write_ ## thingy \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) PYRA_BIN_ATTRIBUTE_W(control, CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) PYRA_BIN_ATTRIBUTE_RW(info, INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return pyra_sysfs_read(fp, kobj, buf, off, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			PYRA_SIZE_PROFILE_SETTINGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			PYRA_COMMAND_PROFILE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return pyra_sysfs_read(fp, kobj, buf, off, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			PYRA_SIZE_PROFILE_BUTTONS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			PYRA_COMMAND_PROFILE_BUTTONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) #define PROFILE_ATTR(number)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static struct bin_attribute bin_attr_profile##number##_settings = {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	.attr = { .name = "profile" #number "_settings", .mode = 0440 },	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	.size = PYRA_SIZE_PROFILE_SETTINGS,				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	.read = pyra_sysfs_read_profilex_settings,			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	.private = &profile_numbers[number-1],				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) };									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static struct bin_attribute bin_attr_profile##number##_buttons = {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	.attr = { .name = "profile" #number "_buttons", .mode = 0440 },	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	.size = PYRA_SIZE_PROFILE_BUTTONS,				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	.read = pyra_sysfs_read_profilex_buttons,			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	.private = &profile_numbers[number-1],				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) PROFILE_ATTR(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) PROFILE_ATTR(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) PROFILE_ATTR(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) PROFILE_ATTR(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) PROFILE_ATTR(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static ssize_t pyra_sysfs_write_settings(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct pyra_roccat_report roccat_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct pyra_settings const *settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (off != 0 || count != PYRA_SIZE_SETTINGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	settings = (struct pyra_settings const *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (settings->startup_profile >= ARRAY_SIZE(pyra->profile_settings))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	mutex_lock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	retval = pyra_set_settings(usb_dev, settings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		mutex_unlock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	profile_activated(pyra, settings->startup_profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	roccat_report.value = settings->startup_profile + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	roccat_report.key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	roccat_report_event(pyra->chrdev_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			(uint8_t const *)&roccat_report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	mutex_unlock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return PYRA_SIZE_SETTINGS;
^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) PYRA_SYSFS_R(settings, SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static struct bin_attribute bin_attr_settings =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	__BIN_ATTR(settings, (S_IWUSR | S_IRUGO),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		   pyra_sysfs_read_settings, pyra_sysfs_write_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		   PYRA_SIZE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct pyra_device *pyra =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct pyra_device *pyra =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	struct pyra_settings settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	mutex_lock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			&settings, PYRA_SIZE_SETTINGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	mutex_unlock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static DEVICE_ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static DEVICE_ATTR(startup_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct pyra_device *pyra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct usb_device *usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	struct pyra_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	dev = dev->parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	pyra = hid_get_drvdata(dev_get_drvdata(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	usb_dev = interface_to_usbdev(to_usb_interface(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	mutex_lock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			&info, PYRA_SIZE_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	mutex_unlock(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static DEVICE_ATTR(firmware_version, 0440, pyra_sysfs_show_firmware_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		   NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static struct attribute *pyra_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	&dev_attr_actual_cpi.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	&dev_attr_actual_profile.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	&dev_attr_firmware_version.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	&dev_attr_startup_profile.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static struct bin_attribute *pyra_bin_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	&bin_attr_control,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	&bin_attr_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	&bin_attr_profile_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	&bin_attr_profile_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	&bin_attr_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	&bin_attr_profile1_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	&bin_attr_profile2_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	&bin_attr_profile3_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	&bin_attr_profile4_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	&bin_attr_profile5_settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	&bin_attr_profile1_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	&bin_attr_profile2_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	&bin_attr_profile3_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	&bin_attr_profile4_buttons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	&bin_attr_profile5_buttons,
^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 const struct attribute_group pyra_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	.attrs = pyra_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	.bin_attrs = pyra_bin_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static const struct attribute_group *pyra_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	&pyra_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		struct pyra_device *pyra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	struct pyra_settings settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	int retval, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	mutex_init(&pyra->pyra_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	retval = pyra_get_settings(usb_dev, &settings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	for (i = 0; i < 5; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		retval = pyra_get_profile_settings(usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				&pyra->profile_settings[i], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	profile_activated(pyra, settings.startup_profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static int pyra_init_specials(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	struct usb_device *usb_dev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	struct pyra_device *pyra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (intf->cur_altsetting->desc.bInterfaceProtocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			== USB_INTERFACE_PROTOCOL_MOUSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		if (!pyra) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			hid_err(hdev, "can't alloc device descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		hid_set_drvdata(hdev, pyra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		retval = pyra_init_pyra_device_struct(usb_dev, pyra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			hid_err(hdev, "couldn't init struct pyra_device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			goto exit_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		retval = roccat_connect(pyra_class, hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 				sizeof(struct pyra_roccat_report));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			hid_err(hdev, "couldn't init char dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			pyra->chrdev_minor = retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			pyra->roccat_claimed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		hid_set_drvdata(hdev, NULL);
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) exit_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	kfree(pyra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static void pyra_remove_specials(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct pyra_device *pyra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (intf->cur_altsetting->desc.bInterfaceProtocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			== USB_INTERFACE_PROTOCOL_MOUSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		pyra = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		if (pyra->roccat_claimed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			roccat_disconnect(pyra->chrdev_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		kfree(hid_get_drvdata(hdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (!hid_is_usb(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	retval = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		hid_err(hdev, "parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		hid_err(hdev, "hw start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	retval = pyra_init_specials(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		hid_err(hdev, "couldn't install mouse\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		goto exit_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) exit_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static void pyra_remove(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	pyra_remove_specials(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	hid_hw_stop(hdev);
^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 pyra_keep_values_up_to_date(struct pyra_device *pyra,
^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 pyra_mouse_event_button const *button_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	switch (data[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		button_event = (struct pyra_mouse_event_button const *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		switch (button_event->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			profile_activated(pyra, button_event->data1 - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			pyra->actual_cpi = button_event->data1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static void pyra_report_to_chrdev(struct pyra_device const *pyra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		u8 const *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	struct pyra_roccat_report roccat_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	struct pyra_mouse_event_button const *button_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	button_event = (struct pyra_mouse_event_button const *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	switch (button_event->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		roccat_report.type = button_event->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		roccat_report.value = button_event->data1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		roccat_report.key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		roccat_report_event(pyra->chrdev_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 				(uint8_t const *)&roccat_report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			roccat_report.type = button_event->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			roccat_report.key = button_event->data1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			 * pyra reports profile numbers with range 1-5.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			 * Keeping this behaviour.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			roccat_report.value = pyra->actual_profile + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 			roccat_report_event(pyra->chrdev_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 					(uint8_t const *)&roccat_report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		u8 *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	struct pyra_device *pyra = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (intf->cur_altsetting->desc.bInterfaceProtocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 			!= USB_INTERFACE_PROTOCOL_MOUSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	if (pyra == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	pyra_keep_values_up_to_date(pyra, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (pyra->roccat_claimed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		pyra_report_to_chrdev(pyra, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static const struct hid_device_id pyra_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) MODULE_DEVICE_TABLE(hid, pyra_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static struct hid_driver pyra_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		.name = "pyra",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		.id_table = pyra_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		.probe = pyra_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		.remove = pyra_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		.raw_event = pyra_raw_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) static int __init pyra_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	/* class name has to be same as driver name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	pyra_class = class_create(THIS_MODULE, "pyra");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (IS_ERR(pyra_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		return PTR_ERR(pyra_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	pyra_class->dev_groups = pyra_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	retval = hid_register_driver(&pyra_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		class_destroy(pyra_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static void __exit pyra_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	hid_unregister_driver(&pyra_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	class_destroy(pyra_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) module_init(pyra_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) module_exit(pyra_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) MODULE_AUTHOR("Stefan Achatz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) MODULE_DESCRIPTION("USB Roccat Pyra driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) MODULE_LICENSE("GPL v2");