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