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) \ |
| 159 | { \ |
| 160 | .attr = { .name = #thingy, .mode = 0660 }, \ |
| 161 | .size = PYRA_SIZE_ ## THINGY, \ |
| 162 | .read = pyra_sysfs_read_ ## thingy, \ |
| 163 | .write = pyra_sysfs_write_ ## thingy \ |
| 164 | } |
| 165 | |
| 166 | #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \ |
| 167 | { \ |
| 168 | .attr = { .name = #thingy, .mode = 0440 }, \ |
| 169 | .size = PYRA_SIZE_ ## THINGY, \ |
| 170 | .read = pyra_sysfs_read_ ## thingy, \ |
| 171 | } |
| 172 | |
| 173 | #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \ |
| 174 | { \ |
| 175 | .attr = { .name = #thingy, .mode = 0220 }, \ |
| 176 | .size = PYRA_SIZE_ ## THINGY, \ |
| 177 | .write = pyra_sysfs_write_ ## thingy \ |
| 178 | } |
| 179 | |
Stefan Achatz | ecbfe7a | 2012-11-11 06:21:10 +0100 | [diff] [blame] | 180 | PYRA_SYSFS_W(control, CONTROL) |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 181 | PYRA_SYSFS_RW(info, INFO) |
Stefan Achatz | ecbfe7a | 2012-11-11 06:21:10 +0100 | [diff] [blame] | 182 | PYRA_SYSFS_RW(profile_settings, PROFILE_SETTINGS) |
| 183 | PYRA_SYSFS_RW(profile_buttons, PROFILE_BUTTONS) |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 184 | PYRA_SYSFS_R(settings, SETTINGS) |
| 185 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 186 | static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp, |
| 187 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 188 | loff_t off, size_t count) |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 189 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 190 | struct device *dev = |
| 191 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 192 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 193 | ssize_t retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 194 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 195 | retval = pyra_send_control(usb_dev, *(uint *)(attr->private), |
| 196 | PYRA_CONTROL_REQUEST_PROFILE_SETTINGS); |
| 197 | if (retval) |
| 198 | return retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 199 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 200 | return pyra_sysfs_read(fp, kobj, buf, off, count, |
| 201 | PYRA_SIZE_PROFILE_SETTINGS, |
| 202 | PYRA_COMMAND_PROFILE_SETTINGS); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 205 | static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp, |
| 206 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 207 | loff_t off, size_t count) |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 208 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 209 | struct device *dev = |
| 210 | container_of(kobj, struct device, 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 | |
| 224 | static ssize_t pyra_sysfs_write_settings(struct file *fp, |
| 225 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 226 | loff_t off, size_t count) |
| 227 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 228 | struct device *dev = |
| 229 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 230 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 231 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 232 | int retval = 0; |
Stefan Achatz | dc186b6 | 2011-08-27 15:24:41 +0200 | [diff] [blame] | 233 | struct pyra_roccat_report roccat_report; |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 234 | struct pyra_settings const *settings; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 235 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 236 | if (off != 0 || count != PYRA_SIZE_SETTINGS) |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 237 | return -EINVAL; |
| 238 | |
| 239 | mutex_lock(&pyra->pyra_lock); |
Stefan Achatz | dc186b6 | 2011-08-27 15:24:41 +0200 | [diff] [blame] | 240 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 241 | settings = (struct pyra_settings const *)buf; |
Stefan Achatz | dc186b6 | 2011-08-27 15:24:41 +0200 | [diff] [blame] | 242 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 243 | retval = pyra_set_settings(usb_dev, settings); |
| 244 | if (retval) { |
| 245 | mutex_unlock(&pyra->pyra_lock); |
| 246 | return retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 247 | } |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 248 | |
| 249 | profile_activated(pyra, settings->startup_profile); |
| 250 | |
| 251 | roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2; |
| 252 | roccat_report.value = settings->startup_profile + 1; |
| 253 | roccat_report.key = 0; |
| 254 | roccat_report_event(pyra->chrdev_minor, |
| 255 | (uint8_t const *)&roccat_report); |
| 256 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 257 | mutex_unlock(&pyra->pyra_lock); |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 258 | return PYRA_SIZE_SETTINGS; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev, |
| 263 | struct device_attribute *attr, char *buf) |
| 264 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 265 | struct pyra_device *pyra = |
| 266 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 267 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi); |
| 268 | } |
| 269 | |
| 270 | static ssize_t pyra_sysfs_show_actual_profile(struct device *dev, |
| 271 | struct device_attribute *attr, char *buf) |
| 272 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 273 | struct pyra_device *pyra = |
| 274 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 275 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 276 | struct pyra_settings settings; |
| 277 | |
| 278 | mutex_lock(&pyra->pyra_lock); |
| 279 | roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS, |
| 280 | &settings, PYRA_SIZE_SETTINGS); |
| 281 | mutex_unlock(&pyra->pyra_lock); |
| 282 | |
| 283 | return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static ssize_t pyra_sysfs_show_firmware_version(struct device *dev, |
| 287 | struct device_attribute *attr, char *buf) |
| 288 | { |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 289 | struct pyra_device *pyra; |
| 290 | struct usb_device *usb_dev; |
| 291 | struct pyra_info info; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 292 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 293 | dev = dev->parent->parent; |
| 294 | pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 295 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 296 | |
| 297 | mutex_lock(&pyra->pyra_lock); |
| 298 | roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO, |
| 299 | &info, PYRA_SIZE_INFO); |
| 300 | mutex_unlock(&pyra->pyra_lock); |
| 301 | |
| 302 | return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 305 | static struct device_attribute pyra_attributes[] = { |
| 306 | __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL), |
| 307 | __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL), |
| 308 | __ATTR(firmware_version, 0440, |
| 309 | pyra_sysfs_show_firmware_version, NULL), |
| 310 | __ATTR(startup_profile, 0440, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 311 | pyra_sysfs_show_actual_profile, NULL), |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 312 | __ATTR_NULL |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 313 | }; |
| 314 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 315 | static struct bin_attribute pyra_bin_attributes[] = { |
Stefan Achatz | ecbfe7a | 2012-11-11 06:21:10 +0100 | [diff] [blame] | 316 | PYRA_BIN_ATTRIBUTE_W(control, CONTROL), |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 317 | PYRA_BIN_ATTRIBUTE_RW(info, INFO), |
Stefan Achatz | ecbfe7a | 2012-11-11 06:21:10 +0100 | [diff] [blame] | 318 | PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS), |
| 319 | PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS), |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 320 | PYRA_BIN_ATTRIBUTE_RW(settings, SETTINGS), |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 321 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 322 | .attr = { .name = "profile1_settings", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 323 | .size = PYRA_SIZE_PROFILE_SETTINGS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 324 | .read = pyra_sysfs_read_profilex_settings, |
| 325 | .private = &profile_numbers[0] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 326 | }, |
| 327 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 328 | .attr = { .name = "profile2_settings", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 329 | .size = PYRA_SIZE_PROFILE_SETTINGS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 330 | .read = pyra_sysfs_read_profilex_settings, |
| 331 | .private = &profile_numbers[1] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 332 | }, |
| 333 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 334 | .attr = { .name = "profile3_settings", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 335 | .size = PYRA_SIZE_PROFILE_SETTINGS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 336 | .read = pyra_sysfs_read_profilex_settings, |
| 337 | .private = &profile_numbers[2] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 338 | }, |
| 339 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 340 | .attr = { .name = "profile4_settings", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 341 | .size = PYRA_SIZE_PROFILE_SETTINGS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 342 | .read = pyra_sysfs_read_profilex_settings, |
| 343 | .private = &profile_numbers[3] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 344 | }, |
| 345 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 346 | .attr = { .name = "profile5_settings", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 347 | .size = PYRA_SIZE_PROFILE_SETTINGS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 348 | .read = pyra_sysfs_read_profilex_settings, |
| 349 | .private = &profile_numbers[4] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 350 | }, |
| 351 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 352 | .attr = { .name = "profile1_buttons", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 353 | .size = PYRA_SIZE_PROFILE_BUTTONS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 354 | .read = pyra_sysfs_read_profilex_buttons, |
| 355 | .private = &profile_numbers[0] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 356 | }, |
| 357 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 358 | .attr = { .name = "profile2_buttons", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 359 | .size = PYRA_SIZE_PROFILE_BUTTONS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 360 | .read = pyra_sysfs_read_profilex_buttons, |
| 361 | .private = &profile_numbers[1] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 362 | }, |
| 363 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 364 | .attr = { .name = "profile3_buttons", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 365 | .size = PYRA_SIZE_PROFILE_BUTTONS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 366 | .read = pyra_sysfs_read_profilex_buttons, |
| 367 | .private = &profile_numbers[2] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 368 | }, |
| 369 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 370 | .attr = { .name = "profile4_buttons", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 371 | .size = PYRA_SIZE_PROFILE_BUTTONS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 372 | .read = pyra_sysfs_read_profilex_buttons, |
| 373 | .private = &profile_numbers[3] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 374 | }, |
| 375 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 376 | .attr = { .name = "profile5_buttons", .mode = 0440 }, |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 377 | .size = PYRA_SIZE_PROFILE_BUTTONS, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 378 | .read = pyra_sysfs_read_profilex_buttons, |
| 379 | .private = &profile_numbers[4] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 380 | }, |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 381 | __ATTR_NULL |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 382 | }; |
| 383 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 384 | static int pyra_init_pyra_device_struct(struct usb_device *usb_dev, |
| 385 | struct pyra_device *pyra) |
| 386 | { |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 387 | struct pyra_settings settings; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 388 | int retval, i; |
| 389 | |
| 390 | mutex_init(&pyra->pyra_lock); |
| 391 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 392 | retval = pyra_get_settings(usb_dev, &settings); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 393 | if (retval) |
| 394 | return retval; |
| 395 | |
| 396 | for (i = 0; i < 5; ++i) { |
| 397 | retval = pyra_get_profile_settings(usb_dev, |
| 398 | &pyra->profile_settings[i], i); |
| 399 | if (retval) |
| 400 | return retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Stefan Achatz | be34380 | 2012-11-11 06:20:58 +0100 | [diff] [blame] | 403 | profile_activated(pyra, settings.startup_profile); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int pyra_init_specials(struct hid_device *hdev) |
| 409 | { |
| 410 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 411 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 412 | struct pyra_device *pyra; |
| 413 | int retval; |
| 414 | |
| 415 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 416 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
| 417 | |
| 418 | pyra = kzalloc(sizeof(*pyra), GFP_KERNEL); |
| 419 | if (!pyra) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 420 | hid_err(hdev, "can't alloc device descriptor\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 421 | return -ENOMEM; |
| 422 | } |
| 423 | hid_set_drvdata(hdev, pyra); |
| 424 | |
| 425 | retval = pyra_init_pyra_device_struct(usb_dev, pyra); |
| 426 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 427 | hid_err(hdev, "couldn't init struct pyra_device\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 428 | goto exit_free; |
| 429 | } |
| 430 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 431 | retval = roccat_connect(pyra_class, hdev, |
| 432 | sizeof(struct pyra_roccat_report)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 433 | if (retval < 0) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 434 | hid_err(hdev, "couldn't init char dev\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 435 | } else { |
| 436 | pyra->chrdev_minor = retval; |
| 437 | pyra->roccat_claimed = 1; |
| 438 | } |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 439 | } else { |
| 440 | hid_set_drvdata(hdev, NULL); |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | exit_free: |
| 445 | kfree(pyra); |
| 446 | return retval; |
| 447 | } |
| 448 | |
| 449 | static void pyra_remove_specials(struct hid_device *hdev) |
| 450 | { |
| 451 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 452 | struct pyra_device *pyra; |
| 453 | |
| 454 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 455 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 456 | pyra = hid_get_drvdata(hdev); |
| 457 | if (pyra->roccat_claimed) |
| 458 | roccat_disconnect(pyra->chrdev_minor); |
| 459 | kfree(hid_get_drvdata(hdev)); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 464 | { |
| 465 | int retval; |
| 466 | |
| 467 | retval = hid_parse(hdev); |
| 468 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 469 | hid_err(hdev, "parse failed\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 470 | goto exit; |
| 471 | } |
| 472 | |
| 473 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 474 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 475 | hid_err(hdev, "hw start failed\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 476 | goto exit; |
| 477 | } |
| 478 | |
| 479 | retval = pyra_init_specials(hdev); |
| 480 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 481 | hid_err(hdev, "couldn't install mouse\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 482 | goto exit_stop; |
| 483 | } |
| 484 | return 0; |
| 485 | |
| 486 | exit_stop: |
| 487 | hid_hw_stop(hdev); |
| 488 | exit: |
| 489 | return retval; |
| 490 | } |
| 491 | |
| 492 | static void pyra_remove(struct hid_device *hdev) |
| 493 | { |
| 494 | pyra_remove_specials(hdev); |
| 495 | hid_hw_stop(hdev); |
| 496 | } |
| 497 | |
| 498 | static void pyra_keep_values_up_to_date(struct pyra_device *pyra, |
| 499 | u8 const *data) |
| 500 | { |
| 501 | struct pyra_mouse_event_button const *button_event; |
| 502 | |
| 503 | switch (data[0]) { |
| 504 | case PYRA_MOUSE_REPORT_NUMBER_BUTTON: |
| 505 | button_event = (struct pyra_mouse_event_button const *)data; |
| 506 | switch (button_event->type) { |
| 507 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: |
| 508 | profile_activated(pyra, button_event->data1 - 1); |
| 509 | break; |
| 510 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: |
| 511 | pyra->actual_cpi = button_event->data1; |
| 512 | break; |
| 513 | } |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static void pyra_report_to_chrdev(struct pyra_device const *pyra, |
| 519 | u8 const *data) |
| 520 | { |
| 521 | struct pyra_roccat_report roccat_report; |
| 522 | struct pyra_mouse_event_button const *button_event; |
| 523 | |
| 524 | if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON) |
| 525 | return; |
| 526 | |
| 527 | button_event = (struct pyra_mouse_event_button const *)data; |
| 528 | |
| 529 | switch (button_event->type) { |
| 530 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: |
| 531 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: |
| 532 | roccat_report.type = button_event->type; |
| 533 | roccat_report.value = button_event->data1; |
| 534 | roccat_report.key = 0; |
| 535 | roccat_report_event(pyra->chrdev_minor, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 536 | (uint8_t const *)&roccat_report); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 537 | break; |
| 538 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO: |
| 539 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT: |
| 540 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH: |
| 541 | if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) { |
| 542 | roccat_report.type = button_event->type; |
| 543 | roccat_report.key = button_event->data1; |
Stefan Achatz | d2b570a | 2010-09-01 12:42:23 +0200 | [diff] [blame] | 544 | /* |
| 545 | * pyra reports profile numbers with range 1-5. |
| 546 | * Keeping this behaviour. |
| 547 | */ |
| 548 | roccat_report.value = pyra->actual_profile + 1; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 549 | roccat_report_event(pyra->chrdev_minor, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 550 | (uint8_t const *)&roccat_report); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 551 | } |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 557 | u8 *data, int size) |
| 558 | { |
| 559 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 560 | struct pyra_device *pyra = hid_get_drvdata(hdev); |
| 561 | |
| 562 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 563 | != USB_INTERFACE_PROTOCOL_MOUSE) |
| 564 | return 0; |
| 565 | |
Stefan Achatz | 901e64d | 2011-06-12 10:02:44 +0200 | [diff] [blame] | 566 | if (pyra == NULL) |
| 567 | return 0; |
| 568 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 569 | pyra_keep_values_up_to_date(pyra, data); |
| 570 | |
| 571 | if (pyra->roccat_claimed) |
| 572 | pyra_report_to_chrdev(pyra, data); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static const struct hid_device_id pyra_devices[] = { |
| 578 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, |
| 579 | USB_DEVICE_ID_ROCCAT_PYRA_WIRED) }, |
Stefan Achatz | 3fce224 | 2011-03-23 18:11:36 +0100 | [diff] [blame] | 580 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, |
| 581 | USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) }, |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 582 | { } |
| 583 | }; |
| 584 | |
| 585 | MODULE_DEVICE_TABLE(hid, pyra_devices); |
| 586 | |
| 587 | static struct hid_driver pyra_driver = { |
| 588 | .name = "pyra", |
| 589 | .id_table = pyra_devices, |
| 590 | .probe = pyra_probe, |
| 591 | .remove = pyra_remove, |
| 592 | .raw_event = pyra_raw_event |
| 593 | }; |
| 594 | |
| 595 | static int __init pyra_init(void) |
| 596 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 597 | int retval; |
| 598 | |
| 599 | /* class name has to be same as driver name */ |
| 600 | pyra_class = class_create(THIS_MODULE, "pyra"); |
| 601 | if (IS_ERR(pyra_class)) |
| 602 | return PTR_ERR(pyra_class); |
| 603 | pyra_class->dev_attrs = pyra_attributes; |
| 604 | pyra_class->dev_bin_attrs = pyra_bin_attributes; |
| 605 | |
| 606 | retval = hid_register_driver(&pyra_driver); |
| 607 | if (retval) |
| 608 | class_destroy(pyra_class); |
| 609 | return retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static void __exit pyra_exit(void) |
| 613 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 614 | hid_unregister_driver(&pyra_driver); |
Stefan Achatz | 74b643d | 2011-01-30 13:38:27 +0100 | [diff] [blame] | 615 | class_destroy(pyra_class); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | module_init(pyra_init); |
| 619 | module_exit(pyra_exit); |
| 620 | |
| 621 | MODULE_AUTHOR("Stefan Achatz"); |
| 622 | MODULE_DESCRIPTION("USB Roccat Pyra driver"); |
| 623 | MODULE_LICENSE("GPL v2"); |