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